| 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/core/large_icon_service.h" | 5 #include "components/favicon/core/large_icon_service.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/strings/stringprintf.h" |
| 14 #include "base/task_runner.h" | 16 #include "base/task_runner.h" |
| 15 #include "base/threading/sequenced_worker_pool.h" | 17 #include "base/threading/sequenced_worker_pool.h" |
| 18 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 16 #include "components/favicon/core/favicon_service.h" | 20 #include "components/favicon/core/favicon_service.h" |
| 17 #include "components/favicon_base/fallback_icon_style.h" | 21 #include "components/favicon_base/fallback_icon_style.h" |
| 18 #include "components/favicon_base/favicon_types.h" | 22 #include "components/favicon_base/favicon_types.h" |
| 23 #include "components/favicon_base/favicon_util.h" |
| 24 #include "components/image_fetcher/image_fetcher.h" |
| 19 #include "skia/ext/image_operations.h" | 25 #include "skia/ext/image_operations.h" |
| 20 #include "ui/gfx/codec/png_codec.h" | 26 #include "ui/gfx/codec/png_codec.h" |
| 21 #include "ui/gfx/geometry/size.h" | 27 #include "ui/gfx/geometry/size.h" |
| 22 | 28 |
| 29 namespace favicon { |
| 23 namespace { | 30 namespace { |
| 24 | 31 |
| 32 const char kGoogleServerV2RequestFormat[] = |
| 33 "https://t0.gstatic.com/" |
| 34 "faviconV2?user=chrome&drop_404_icon=true&url=%s&size=%d&" |
| 35 "min_size=%d&max_size=%d"; |
| 36 const int kGoogleServerV2MaxSizeInPixel = 256; |
| 37 const int kGoogleServerV2DesiredSizeInPixel = 192; |
| 38 |
| 39 GURL GetIconUrlForGoogleServerV2(const GURL& page_url, |
| 40 int min_source_size_in_pixel) { |
| 41 return GURL(base::StringPrintf( |
| 42 kGoogleServerV2RequestFormat, page_url.spec().c_str(), |
| 43 kGoogleServerV2DesiredSizeInPixel, min_source_size_in_pixel, |
| 44 kGoogleServerV2MaxSizeInPixel)); |
| 45 } |
| 46 |
| 25 // Processes the bitmap data returned from the FaviconService as part of a | 47 // Processes the bitmap data returned from the FaviconService as part of a |
| 26 // LargeIconService request. | 48 // LargeIconService request. |
| 27 class LargeIconWorker : public base::RefCountedThreadSafe<LargeIconWorker> { | 49 class LargeIconWorker : public base::RefCountedThreadSafe<LargeIconWorker> { |
| 28 public: | 50 public: |
| 29 LargeIconWorker(int min_source_size_in_pixel, | 51 LargeIconWorker(int min_source_size_in_pixel, |
| 30 int desired_size_in_pixel, | 52 int desired_size_in_pixel, |
| 31 favicon_base::LargeIconCallback callback, | 53 favicon_base::LargeIconCallback callback, |
| 32 scoped_refptr<base::TaskRunner> background_task_runner, | 54 scoped_refptr<base::TaskRunner> background_task_runner, |
| 33 base::CancelableTaskTracker* tracker); | 55 base::CancelableTaskTracker* tracker); |
| 34 | 56 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 gfx::Size(desired_size_in_pixel_, desired_size_in_pixel_); | 175 gfx::Size(desired_size_in_pixel_, desired_size_in_pixel_); |
| 154 resized_bitmap_result->bitmap_data = | 176 resized_bitmap_result->bitmap_data = |
| 155 base::RefCountedBytes::TakeVector(&bitmap_data); | 177 base::RefCountedBytes::TakeVector(&bitmap_data); |
| 156 return true; | 178 return true; |
| 157 } | 179 } |
| 158 | 180 |
| 159 void LargeIconWorker::OnIconProcessingComplete() { | 181 void LargeIconWorker::OnIconProcessingComplete() { |
| 160 callback_.Run(*result_); | 182 callback_.Run(*result_); |
| 161 } | 183 } |
| 162 | 184 |
| 185 void OnFetchIconFromGoogleServerComplete( |
| 186 FaviconService* favicon_service, |
| 187 const GURL& page_url, |
| 188 const base::Callback<void(bool success)>& callback, |
| 189 const std::string& icon_url, |
| 190 const gfx::Image& image, |
| 191 const image_fetcher::RequestMetadata& metadata) { |
| 192 if (image.IsEmpty()) { |
| 193 DLOG(WARNING) << "large icon server fetch empty " << icon_url; |
| 194 favicon_service->UnableToDownloadFavicon(GURL(icon_url)); |
| 195 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 196 base::Bind(callback, false)); |
| 197 return; |
| 198 } |
| 199 |
| 200 // TODO(crbug.com/699542): Extract the original icon url from the response |
| 201 // headers if available and use it instead of |icon_url|. Possibly the type |
| 202 // too, because using TOUCH_ICON is a hacky way that allows us to not |
| 203 // interfere with sync. |
| 204 |
| 205 // Write fetched icons to FaviconService's cache, but only if no icon was |
| 206 // available (clients are encouraged to do this in advance, but meanwhile |
| 207 // something else could've been written). By marking the icons initially |
| 208 // expired (out-of-date), they will be refetched when we visit the original |
| 209 // page any time in the future. |
| 210 favicon_service->SetLastResortFavicons(page_url, GURL(icon_url), |
| 211 favicon_base::IconType::TOUCH_ICON, |
| 212 image, callback); |
| 213 } |
| 214 |
| 163 } // namespace | 215 } // namespace |
| 164 | 216 |
| 165 namespace favicon { | |
| 166 | |
| 167 LargeIconService::LargeIconService( | 217 LargeIconService::LargeIconService( |
| 168 FaviconService* favicon_service, | 218 FaviconService* favicon_service, |
| 169 const scoped_refptr<base::TaskRunner>& background_task_runner) | 219 const scoped_refptr<base::TaskRunner>& background_task_runner, |
| 220 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) |
| 170 : favicon_service_(favicon_service), | 221 : favicon_service_(favicon_service), |
| 171 background_task_runner_(background_task_runner) { | 222 background_task_runner_(background_task_runner), |
| 223 image_fetcher_(std::move(image_fetcher)) { |
| 172 large_icon_types_.push_back(favicon_base::IconType::FAVICON); | 224 large_icon_types_.push_back(favicon_base::IconType::FAVICON); |
| 173 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON); | 225 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON); |
| 174 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON); | 226 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON); |
| 175 } | 227 } |
| 176 | 228 |
| 177 LargeIconService::~LargeIconService() { | 229 LargeIconService::~LargeIconService() { |
| 178 } | 230 } |
| 179 | 231 |
| 180 base::CancelableTaskTracker::TaskId | 232 base::CancelableTaskTracker::TaskId |
| 181 LargeIconService::GetLargeIconOrFallbackStyle( | 233 LargeIconService::GetLargeIconOrFallbackStyle( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 194 // TODO(beaudoin): For now this is just a wrapper around | 246 // TODO(beaudoin): For now this is just a wrapper around |
| 195 // GetLargestRawFaviconForPageURL. Add the logic required to select the best | 247 // GetLargestRawFaviconForPageURL. Add the logic required to select the best |
| 196 // possible large icon. Also add logic to fetch-on-demand when the URL of | 248 // possible large icon. Also add logic to fetch-on-demand when the URL of |
| 197 // a large icon is known but its bitmap is not available. | 249 // a large icon is known but its bitmap is not available. |
| 198 return favicon_service_->GetLargestRawFaviconForPageURL( | 250 return favicon_service_->GetLargestRawFaviconForPageURL( |
| 199 page_url, large_icon_types_, min_source_size_in_pixel, | 251 page_url, large_icon_types_, min_source_size_in_pixel, |
| 200 base::Bind(&LargeIconWorker::OnIconLookupComplete, worker), | 252 base::Bind(&LargeIconWorker::OnIconLookupComplete, worker), |
| 201 tracker); | 253 tracker); |
| 202 } | 254 } |
| 203 | 255 |
| 256 void LargeIconService:: |
| 257 GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( |
| 258 const GURL& page_url, |
| 259 int min_source_size_in_pixel, |
| 260 const base::Callback<void(bool success)>& callback) { |
| 261 DCHECK_LE(0, min_source_size_in_pixel); |
| 262 |
| 263 const GURL icon_url = |
| 264 GetIconUrlForGoogleServerV2(page_url, min_source_size_in_pixel); |
| 265 |
| 266 // Do not download if there is a previous cache miss recorded for |icon_url|. |
| 267 if (!image_fetcher_ || |
| 268 favicon_service_->WasUnableToDownloadFavicon(icon_url)) { |
| 269 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 270 base::Bind(callback, false)); |
| 271 return; |
| 272 } |
| 273 |
| 274 image_fetcher_->SetDataUseServiceName( |
| 275 data_use_measurement::DataUseUserData::LARGE_ICON_SERVICE); |
| 276 image_fetcher_->StartOrQueueNetworkRequest( |
| 277 icon_url.spec(), icon_url, |
| 278 base::Bind(&OnFetchIconFromGoogleServerComplete, favicon_service_, |
| 279 page_url, callback)); |
| 280 } |
| 281 |
| 204 } // namespace favicon | 282 } // namespace favicon |
| OLD | NEW |