Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: components/favicon/ios/web_favicon_driver.mm

Issue 2677993002: Use IOSImageDataFetcherWrapper for favicon (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/favicon/ios/web_favicon_driver.h" 5 #include "components/favicon/ios/web_favicon_driver.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/ptr_util.h"
8 #include "components/favicon/core/favicon_url.h" 9 #include "components/favicon/core/favicon_url.h"
9 #include "components/favicon/ios/favicon_url_util.h" 10 #include "components/favicon/ios/favicon_url_util.h"
11 #import "components/image_fetcher/ios/ios_image_data_fetcher_wrapper.h"
10 #include "ios/web/public/browser_state.h" 12 #include "ios/web/public/browser_state.h"
11 #include "ios/web/public/favicon_status.h" 13 #include "ios/web/public/favicon_status.h"
12 #include "ios/web/public/navigation_item.h" 14 #include "ios/web/public/navigation_item.h"
13 #include "ios/web/public/navigation_manager.h" 15 #include "ios/web/public/navigation_manager.h"
14 #include "ios/web/public/web_state/web_state.h" 16 #include "ios/web/public/web_state/web_state.h"
17 #include "ios/web/public/web_thread.h"
18 #include "net/url_request/url_fetcher.h"
19 #include "skia/ext/skia_utils_ios.h"
20 #include "third_party/skia/include/core/SkBitmap.h"
15 #include "ui/gfx/image/image.h" 21 #include "ui/gfx/image/image.h"
16 22
17 DEFINE_WEB_STATE_USER_DATA_KEY(favicon::WebFaviconDriver); 23 DEFINE_WEB_STATE_USER_DATA_KEY(favicon::WebFaviconDriver);
18 24
25 namespace {
Marc Treib 2017/02/06 12:35:49 nit: no namespace required for a typedef
gambard 2017/02/06 15:58:28 Done.
26 // Callback for the download of favicon.
27 typedef base::Callback<void(
Marc Treib 2017/02/06 12:35:49 nit: I prefer the C++11-style using ImageDownloadC
gambard 2017/02/06 15:58:28 Done.
28 int, /* id */
29 int, /* HTTP status code */
30 const GURL&, /* image_url */
31 const std::vector<SkBitmap>&, /* bitmaps */
32 /* The sizes in pixel of the bitmaps before they were resized due to the
33 max bitmap size passed to DownloadImage(). Each entry in the bitmaps
34 vector corresponds to an entry in the sizes vector. If a bitmap was
35 resized, there should be a single returned bitmap. */
36 const std::vector<gfx::Size>&)>
37 ImageDownloadCallback;
38 } // namespace
39
19 namespace favicon { 40 namespace favicon {
20 41
21 // static 42 // static
22 void WebFaviconDriver::CreateForWebState( 43 void WebFaviconDriver::CreateForWebState(
23 web::WebState* web_state, 44 web::WebState* web_state,
24 FaviconService* favicon_service, 45 FaviconService* favicon_service,
25 history::HistoryService* history_service, 46 history::HistoryService* history_service,
26 bookmarks::BookmarkModel* bookmark_model) { 47 bookmarks::BookmarkModel* bookmark_model) {
27 if (FromWebState(web_state)) 48 if (FromWebState(web_state))
28 return; 49 return;
(...skipping 19 matching lines...) Expand all
48 web_state()->GetNavigationManager()->GetLastCommittedItem(); 69 web_state()->GetNavigationManager()->GetLastCommittedItem();
49 return item ? item->GetFavicon().valid : false; 70 return item ? item->GetFavicon().valid : false;
50 } 71 }
51 72
52 int WebFaviconDriver::StartDownload(const GURL& url, int max_image_size) { 73 int WebFaviconDriver::StartDownload(const GURL& url, int max_image_size) {
53 if (WasUnableToDownloadFavicon(url)) { 74 if (WasUnableToDownloadFavicon(url)) {
54 DVLOG(1) << "Skip Failed FavIcon: " << url; 75 DVLOG(1) << "Skip Failed FavIcon: " << url;
55 return 0; 76 return 0;
56 } 77 }
57 78
58 return web_state()->DownloadImage( 79 static int downloaded_image_count = 0;
59 url, true, max_image_size, false, 80 int local_download_id = ++downloaded_image_count;
60 base::Bind(&FaviconDriverImpl::DidDownloadFavicon, 81
61 base::Unretained(this))); 82 ImageDownloadCallback local_image_callback = base::Bind(
83 &FaviconDriverImpl::DidDownloadFavicon, base::Unretained(this));
84 GURL local_url(url);
85
86 image_fetcher::IOSImageDataFetcherCallback local_callback =
87 ^(const int response_code, NSData* data) {
88 std::vector<SkBitmap> frames;
89 std::vector<gfx::Size> sizes;
90 if (data) {
91 frames = skia::ImageDataToSkBitmaps(data);
92 for (auto& frame : frames) {
Marc Treib 2017/02/06 12:35:49 nit: const auto& ?
gambard 2017/02/06 15:58:28 Done.
93 sizes.push_back(gfx::Size(frame.width(), frame.height()));
94 }
95 }
96 if (response_code != net::URLFetcher::RESPONSE_CODE_INVALID) {
97 local_image_callback.Run(local_download_id, response_code, local_url,
98 frames, sizes);
99 }
100 };
101 image_fetcher_->FetchImageDataWebpDecoded(url, local_callback);
102
103 return downloaded_image_count;
62 } 104 }
63 105
64 bool WebFaviconDriver::IsOffTheRecord() { 106 bool WebFaviconDriver::IsOffTheRecord() {
65 DCHECK(web_state()); 107 DCHECK(web_state());
66 return web_state()->GetBrowserState()->IsOffTheRecord(); 108 return web_state()->GetBrowserState()->IsOffTheRecord();
67 } 109 }
68 110
69 GURL WebFaviconDriver::GetActiveURL() { 111 GURL WebFaviconDriver::GetActiveURL() {
70 web::NavigationItem* item = 112 web::NavigationItem* item =
71 web_state()->GetNavigationManager()->GetVisibleItem(); 113 web_state()->GetNavigationManager()->GetVisibleItem();
(...skipping 18 matching lines...) Expand all
90 NotifyFaviconUpdatedObservers(notification_icon_type, icon_url, 132 NotifyFaviconUpdatedObservers(notification_icon_type, icon_url,
91 icon_url_changed, image); 133 icon_url_changed, image);
92 } 134 }
93 135
94 WebFaviconDriver::WebFaviconDriver(web::WebState* web_state, 136 WebFaviconDriver::WebFaviconDriver(web::WebState* web_state,
95 FaviconService* favicon_service, 137 FaviconService* favicon_service,
96 history::HistoryService* history_service, 138 history::HistoryService* history_service,
97 bookmarks::BookmarkModel* bookmark_model) 139 bookmarks::BookmarkModel* bookmark_model)
98 : web::WebStateObserver(web_state), 140 : web::WebStateObserver(web_state),
99 FaviconDriverImpl(favicon_service, history_service, bookmark_model) { 141 FaviconDriverImpl(favicon_service, history_service, bookmark_model) {
142 // Set up the image fetcher.
143 image_fetcher_ = base::MakeUnique<image_fetcher::IOSImageDataFetcherWrapper>(
Marc Treib 2017/02/06 12:35:49 Looks like this doesn't need to be a pointer?
gambard 2017/02/06 15:58:28 Done.
144 web_state->GetBrowserState()->GetRequestContext(),
145 web::WebThread::GetBlockingPool());
100 } 146 }
101 147
102 WebFaviconDriver::~WebFaviconDriver() { 148 WebFaviconDriver::~WebFaviconDriver() {
103 } 149 }
104 150
105 void WebFaviconDriver::FaviconUrlUpdated( 151 void WebFaviconDriver::FaviconUrlUpdated(
106 const std::vector<web::FaviconURL>& candidates) { 152 const std::vector<web::FaviconURL>& candidates) {
107 DCHECK(!candidates.empty()); 153 DCHECK(!candidates.empty());
108 OnUpdateFaviconURL(GetActiveURL(), FaviconURLsFromWebFaviconURLs(candidates)); 154 OnUpdateFaviconURL(GetActiveURL(), FaviconURLsFromWebFaviconURLs(candidates));
109 } 155 }
110 156
111 } // namespace favicon 157 } // namespace favicon
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698