OLD | NEW |
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/threading/sequenced_worker_pool.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" |
10 #include "ios/web/public/browser_state.h" | 11 #include "ios/web/public/browser_state.h" |
11 #include "ios/web/public/favicon_status.h" | 12 #include "ios/web/public/favicon_status.h" |
12 #include "ios/web/public/navigation_item.h" | 13 #include "ios/web/public/navigation_item.h" |
13 #include "ios/web/public/navigation_manager.h" | 14 #include "ios/web/public/navigation_manager.h" |
14 #include "ios/web/public/web_state/web_state.h" | 15 #include "ios/web/public/web_state/web_state.h" |
| 16 #include "ios/web/public/web_thread.h" |
| 17 #include "skia/ext/skia_utils_ios.h" |
| 18 #include "third_party/skia/include/core/SkBitmap.h" |
15 #include "ui/gfx/image/image.h" | 19 #include "ui/gfx/image/image.h" |
16 | 20 |
17 DEFINE_WEB_STATE_USER_DATA_KEY(favicon::WebFaviconDriver); | 21 DEFINE_WEB_STATE_USER_DATA_KEY(favicon::WebFaviconDriver); |
18 | 22 |
| 23 // Callback for the download of favicon. |
| 24 using ImageDownloadCallback = |
| 25 base::Callback<void(int image_id, |
| 26 int http_status_code, |
| 27 const GURL& image_url, |
| 28 const std::vector<SkBitmap>& bitmaps, |
| 29 const std::vector<gfx::Size>& sizes)>; |
| 30 |
19 namespace favicon { | 31 namespace favicon { |
20 | 32 |
21 // static | 33 // static |
22 void WebFaviconDriver::CreateForWebState( | 34 void WebFaviconDriver::CreateForWebState( |
23 web::WebState* web_state, | 35 web::WebState* web_state, |
24 FaviconService* favicon_service, | 36 FaviconService* favicon_service, |
25 history::HistoryService* history_service, | 37 history::HistoryService* history_service, |
26 bookmarks::BookmarkModel* bookmark_model) { | 38 bookmarks::BookmarkModel* bookmark_model) { |
27 if (FromWebState(web_state)) | 39 if (FromWebState(web_state)) |
28 return; | 40 return; |
(...skipping 19 matching lines...) Expand all Loading... |
48 web_state()->GetNavigationManager()->GetLastCommittedItem(); | 60 web_state()->GetNavigationManager()->GetLastCommittedItem(); |
49 return item ? item->GetFavicon().valid : false; | 61 return item ? item->GetFavicon().valid : false; |
50 } | 62 } |
51 | 63 |
52 int WebFaviconDriver::StartDownload(const GURL& url, int max_image_size) { | 64 int WebFaviconDriver::StartDownload(const GURL& url, int max_image_size) { |
53 if (WasUnableToDownloadFavicon(url)) { | 65 if (WasUnableToDownloadFavicon(url)) { |
54 DVLOG(1) << "Skip Failed FavIcon: " << url; | 66 DVLOG(1) << "Skip Failed FavIcon: " << url; |
55 return 0; | 67 return 0; |
56 } | 68 } |
57 | 69 |
58 return web_state()->DownloadImage( | 70 static int downloaded_image_count = 0; |
59 url, true, max_image_size, false, | 71 int local_download_id = ++downloaded_image_count; |
60 base::Bind(&FaviconDriverImpl::DidDownloadFavicon, | 72 |
61 base::Unretained(this))); | 73 ImageDownloadCallback local_image_callback = base::Bind( |
| 74 &FaviconDriverImpl::DidDownloadFavicon, base::Unretained(this)); |
| 75 GURL local_url(url); |
| 76 |
| 77 image_fetcher::IOSImageDataFetcherCallback local_callback = |
| 78 ^(NSData* data, const image_fetcher::RequestMetadata& metadata) { |
| 79 if (metadata.response_code == |
| 80 image_fetcher::ImageDataFetcher::RESPONSE_CODE_INVALID) |
| 81 return; |
| 82 |
| 83 std::vector<SkBitmap> frames; |
| 84 std::vector<gfx::Size> sizes; |
| 85 if (data) { |
| 86 frames = skia::ImageDataToSkBitmaps(data); |
| 87 for (const auto& frame : frames) { |
| 88 sizes.push_back(gfx::Size(frame.width(), frame.height())); |
| 89 } |
| 90 } |
| 91 local_image_callback.Run(local_download_id, metadata.response_code, |
| 92 local_url, frames, sizes); |
| 93 }; |
| 94 image_fetcher_.FetchImageDataWebpDecoded(url, local_callback); |
| 95 |
| 96 return downloaded_image_count; |
62 } | 97 } |
63 | 98 |
64 bool WebFaviconDriver::IsOffTheRecord() { | 99 bool WebFaviconDriver::IsOffTheRecord() { |
65 DCHECK(web_state()); | 100 DCHECK(web_state()); |
66 return web_state()->GetBrowserState()->IsOffTheRecord(); | 101 return web_state()->GetBrowserState()->IsOffTheRecord(); |
67 } | 102 } |
68 | 103 |
69 GURL WebFaviconDriver::GetActiveURL() { | 104 GURL WebFaviconDriver::GetActiveURL() { |
70 web::NavigationItem* item = | 105 web::NavigationItem* item = |
71 web_state()->GetNavigationManager()->GetVisibleItem(); | 106 web_state()->GetNavigationManager()->GetVisibleItem(); |
(...skipping 17 matching lines...) Expand all Loading... |
89 | 124 |
90 NotifyFaviconUpdatedObservers(notification_icon_type, icon_url, | 125 NotifyFaviconUpdatedObservers(notification_icon_type, icon_url, |
91 icon_url_changed, image); | 126 icon_url_changed, image); |
92 } | 127 } |
93 | 128 |
94 WebFaviconDriver::WebFaviconDriver(web::WebState* web_state, | 129 WebFaviconDriver::WebFaviconDriver(web::WebState* web_state, |
95 FaviconService* favicon_service, | 130 FaviconService* favicon_service, |
96 history::HistoryService* history_service, | 131 history::HistoryService* history_service, |
97 bookmarks::BookmarkModel* bookmark_model) | 132 bookmarks::BookmarkModel* bookmark_model) |
98 : web::WebStateObserver(web_state), | 133 : web::WebStateObserver(web_state), |
99 FaviconDriverImpl(favicon_service, history_service, bookmark_model) { | 134 FaviconDriverImpl(favicon_service, history_service, bookmark_model), |
100 } | 135 image_fetcher_(web_state->GetBrowserState()->GetRequestContext(), |
| 136 web::WebThread::GetBlockingPool()) {} |
101 | 137 |
102 WebFaviconDriver::~WebFaviconDriver() { | 138 WebFaviconDriver::~WebFaviconDriver() { |
103 } | 139 } |
104 | 140 |
105 void WebFaviconDriver::FaviconUrlUpdated( | 141 void WebFaviconDriver::FaviconUrlUpdated( |
106 const std::vector<web::FaviconURL>& candidates) { | 142 const std::vector<web::FaviconURL>& candidates) { |
107 DCHECK(!candidates.empty()); | 143 DCHECK(!candidates.empty()); |
108 OnUpdateFaviconURL(GetActiveURL(), FaviconURLsFromWebFaviconURLs(candidates)); | 144 OnUpdateFaviconURL(GetActiveURL(), FaviconURLsFromWebFaviconURLs(candidates)); |
109 } | 145 } |
110 | 146 |
111 } // namespace favicon | 147 } // namespace favicon |
OLD | NEW |