| 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" | |
| 18 #include "ui/gfx/image/image.h" | 15 #include "ui/gfx/image/image.h" |
| 19 | 16 |
| 20 DEFINE_WEB_STATE_USER_DATA_KEY(favicon::WebFaviconDriver); | 17 DEFINE_WEB_STATE_USER_DATA_KEY(favicon::WebFaviconDriver); |
| 21 | 18 |
| 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 | |
| 30 namespace favicon { | 19 namespace favicon { |
| 31 | 20 |
| 32 // static | 21 // static |
| 33 void WebFaviconDriver::CreateForWebState( | 22 void WebFaviconDriver::CreateForWebState( |
| 34 web::WebState* web_state, | 23 web::WebState* web_state, |
| 35 FaviconService* favicon_service, | 24 FaviconService* favicon_service, |
| 36 history::HistoryService* history_service, | 25 history::HistoryService* history_service, |
| 37 bookmarks::BookmarkModel* bookmark_model) { | 26 bookmarks::BookmarkModel* bookmark_model) { |
| 38 if (FromWebState(web_state)) | 27 if (FromWebState(web_state)) |
| 39 return; | 28 return; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 59 web_state()->GetNavigationManager()->GetLastCommittedItem(); | 48 web_state()->GetNavigationManager()->GetLastCommittedItem(); |
| 60 return item ? item->GetFavicon().valid : false; | 49 return item ? item->GetFavicon().valid : false; |
| 61 } | 50 } |
| 62 | 51 |
| 63 int WebFaviconDriver::StartDownload(const GURL& url, int max_image_size) { | 52 int WebFaviconDriver::StartDownload(const GURL& url, int max_image_size) { |
| 64 if (WasUnableToDownloadFavicon(url)) { | 53 if (WasUnableToDownloadFavicon(url)) { |
| 65 DVLOG(1) << "Skip Failed FavIcon: " << url; | 54 DVLOG(1) << "Skip Failed FavIcon: " << url; |
| 66 return 0; | 55 return 0; |
| 67 } | 56 } |
| 68 | 57 |
| 69 static int downloaded_image_count = 0; | 58 return web_state()->DownloadImage( |
| 70 int local_download_id = ++downloaded_image_count; | 59 url, true, max_image_size, false, |
| 71 | 60 base::Bind(&FaviconDriverImpl::DidDownloadFavicon, |
| 72 ImageDownloadCallback local_image_callback = base::Bind( | 61 base::Unretained(this))); |
| 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; | |
| 96 } | 62 } |
| 97 | 63 |
| 98 bool WebFaviconDriver::IsOffTheRecord() { | 64 bool WebFaviconDriver::IsOffTheRecord() { |
| 99 DCHECK(web_state()); | 65 DCHECK(web_state()); |
| 100 return web_state()->GetBrowserState()->IsOffTheRecord(); | 66 return web_state()->GetBrowserState()->IsOffTheRecord(); |
| 101 } | 67 } |
| 102 | 68 |
| 103 GURL WebFaviconDriver::GetActiveURL() { | 69 GURL WebFaviconDriver::GetActiveURL() { |
| 104 web::NavigationItem* item = | 70 web::NavigationItem* item = |
| 105 web_state()->GetNavigationManager()->GetVisibleItem(); | 71 web_state()->GetNavigationManager()->GetVisibleItem(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 123 | 89 |
| 124 NotifyFaviconUpdatedObservers(notification_icon_type, icon_url, | 90 NotifyFaviconUpdatedObservers(notification_icon_type, icon_url, |
| 125 icon_url_changed, image); | 91 icon_url_changed, image); |
| 126 } | 92 } |
| 127 | 93 |
| 128 WebFaviconDriver::WebFaviconDriver(web::WebState* web_state, | 94 WebFaviconDriver::WebFaviconDriver(web::WebState* web_state, |
| 129 FaviconService* favicon_service, | 95 FaviconService* favicon_service, |
| 130 history::HistoryService* history_service, | 96 history::HistoryService* history_service, |
| 131 bookmarks::BookmarkModel* bookmark_model) | 97 bookmarks::BookmarkModel* bookmark_model) |
| 132 : web::WebStateObserver(web_state), | 98 : web::WebStateObserver(web_state), |
| 133 FaviconDriverImpl(favicon_service, history_service, bookmark_model), | 99 FaviconDriverImpl(favicon_service, history_service, bookmark_model) { |
| 134 image_fetcher_(web_state->GetBrowserState()->GetRequestContext(), | 100 } |
| 135 web::WebThread::GetBlockingPool()) {} | |
| 136 | 101 |
| 137 WebFaviconDriver::~WebFaviconDriver() { | 102 WebFaviconDriver::~WebFaviconDriver() { |
| 138 } | 103 } |
| 139 | 104 |
| 140 void WebFaviconDriver::FaviconUrlUpdated( | 105 void WebFaviconDriver::FaviconUrlUpdated( |
| 141 const std::vector<web::FaviconURL>& candidates) { | 106 const std::vector<web::FaviconURL>& candidates) { |
| 142 DCHECK(!candidates.empty()); | 107 DCHECK(!candidates.empty()); |
| 143 OnUpdateFaviconURL(GetActiveURL(), FaviconURLsFromWebFaviconURLs(candidates)); | 108 OnUpdateFaviconURL(GetActiveURL(), FaviconURLsFromWebFaviconURLs(candidates)); |
| 144 } | 109 } |
| 145 | 110 |
| 146 } // namespace favicon | 111 } // namespace favicon |
| OLD | NEW |