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