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/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/ptr_util.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/task_runner.h" | 16 #include "base/task_runner.h" |
| 17 #include "base/threading/sequenced_worker_pool.h" | 17 #include "base/threading/sequenced_worker_pool.h" |
| 18 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "components/data_use_measurement/core/data_use_user_data.h" | 19 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 20 #include "components/favicon/core/favicon_service.h" | 20 #include "components/favicon/core/favicon_service.h" |
| 21 #include "components/favicon_base/fallback_icon_style.h" | 21 #include "components/favicon_base/fallback_icon_style.h" |
| 22 #include "components/favicon_base/favicon_types.h" | 22 #include "components/favicon_base/favicon_types.h" |
| 23 #include "components/favicon_base/favicon_util.h" | 23 #include "components/favicon_base/favicon_util.h" |
| 24 #include "components/image_fetcher/core/image_decoder.h" | |
| 24 #include "skia/ext/image_operations.h" | 25 #include "skia/ext/image_operations.h" |
| 25 #include "ui/gfx/codec/png_codec.h" | 26 #include "ui/gfx/codec/png_codec.h" |
| 26 #include "ui/gfx/geometry/size.h" | 27 #include "ui/gfx/geometry/size.h" |
| 27 #include "url/url_canon.h" | 28 #include "url/url_canon.h" |
| 28 | 29 |
| 29 namespace favicon { | 30 namespace favicon { |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 const char kGoogleServerV2RequestFormat[] = | 33 const char kGoogleServerV2RequestFormat[] = |
| 33 "https://t0.gstatic.com/" | 34 "https://t0.gstatic.com/" |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 } | 226 } |
| 226 | 227 |
| 227 } // namespace | 228 } // namespace |
| 228 | 229 |
| 229 LargeIconService::LargeIconService( | 230 LargeIconService::LargeIconService( |
| 230 FaviconService* favicon_service, | 231 FaviconService* favicon_service, |
| 231 const scoped_refptr<base::TaskRunner>& background_task_runner, | 232 const scoped_refptr<base::TaskRunner>& background_task_runner, |
| 232 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) | 233 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) |
| 233 : favicon_service_(favicon_service), | 234 : favicon_service_(favicon_service), |
| 234 background_task_runner_(background_task_runner), | 235 background_task_runner_(background_task_runner), |
| 235 image_fetcher_(std::move(image_fetcher)) { | 236 image_fetcher_(std::move(image_fetcher)), |
| 237 weak_ptr_factory_(this) { | |
| 236 large_icon_types_.push_back(favicon_base::IconType::FAVICON); | 238 large_icon_types_.push_back(favicon_base::IconType::FAVICON); |
| 237 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON); | 239 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON); |
| 238 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON); | 240 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON); |
| 239 } | 241 } |
| 240 | 242 |
| 241 LargeIconService::~LargeIconService() { | 243 LargeIconService::~LargeIconService() { |
| 242 } | 244 } |
| 243 | 245 |
| 244 base::CancelableTaskTracker::TaskId | 246 base::CancelableTaskTracker::TaskId |
| 245 LargeIconService::GetLargeIconOrFallbackStyle( | 247 LargeIconService::GetLargeIconOrFallbackStyle( |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 258 // TODO(beaudoin): For now this is just a wrapper around | 260 // TODO(beaudoin): For now this is just a wrapper around |
| 259 // GetLargestRawFaviconForPageURL. Add the logic required to select the best | 261 // GetLargestRawFaviconForPageURL. Add the logic required to select the best |
| 260 // possible large icon. Also add logic to fetch-on-demand when the URL of | 262 // possible large icon. Also add logic to fetch-on-demand when the URL of |
| 261 // a large icon is known but its bitmap is not available. | 263 // a large icon is known but its bitmap is not available. |
| 262 return favicon_service_->GetLargestRawFaviconForPageURL( | 264 return favicon_service_->GetLargestRawFaviconForPageURL( |
| 263 page_url, large_icon_types_, min_source_size_in_pixel, | 265 page_url, large_icon_types_, min_source_size_in_pixel, |
| 264 base::Bind(&LargeIconWorker::OnIconLookupComplete, worker), | 266 base::Bind(&LargeIconWorker::OnIconLookupComplete, worker), |
| 265 tracker); | 267 tracker); |
| 266 } | 268 } |
| 267 | 269 |
| 270 base::CancelableTaskTracker::TaskId | |
| 271 LargeIconService::GetLargeIconImageOrFallbackStyle( | |
| 272 const GURL& page_url, | |
| 273 int min_source_size_in_pixel, | |
| 274 int desired_size_in_pixel, | |
| 275 const favicon_base::LargeIconImageCallback& callback, | |
| 276 base::CancelableTaskTracker* tracker) { | |
| 277 return GetLargeIconOrFallbackStyle( | |
| 278 page_url, min_source_size_in_pixel, desired_size_in_pixel, | |
| 279 base::Bind(&LargeIconService::DecodeRawBitmap, | |
| 280 weak_ptr_factory_.GetWeakPtr(), callback), | |
| 281 tracker); | |
| 282 } | |
| 283 | |
| 268 void LargeIconService:: | 284 void LargeIconService:: |
| 269 GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( | 285 GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( |
| 270 const GURL& page_url, | 286 const GURL& page_url, |
| 271 int min_source_size_in_pixel, | 287 int min_source_size_in_pixel, |
| 272 const base::Callback<void(bool success)>& callback) { | 288 const base::Callback<void(bool success)>& callback) { |
| 273 DCHECK_LE(0, min_source_size_in_pixel); | 289 DCHECK_LE(0, min_source_size_in_pixel); |
| 274 | 290 |
| 275 const GURL trimmed_page_url = TrimPageUrlForGoogleServer(page_url); | 291 const GURL trimmed_page_url = TrimPageUrlForGoogleServer(page_url); |
| 276 const GURL icon_url = | 292 const GURL icon_url = |
| 277 GetIconUrlForGoogleServerV2(trimmed_page_url, min_source_size_in_pixel); | 293 GetIconUrlForGoogleServerV2(trimmed_page_url, min_source_size_in_pixel); |
| 278 | 294 |
| 279 // Do not download if the URL is invalid after trimming, or there is a | 295 // Do not download if the URL is invalid after trimming, or there is a |
| 280 // previous cache miss recorded for |icon_url|. | 296 // previous cache miss recorded for |icon_url|. |
| 281 if (!trimmed_page_url.is_valid() || !image_fetcher_ || | 297 if (!trimmed_page_url.is_valid() || !image_fetcher_ || |
| 282 favicon_service_->WasUnableToDownloadFavicon(icon_url)) { | 298 favicon_service_->WasUnableToDownloadFavicon(icon_url)) { |
| 283 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 299 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 284 base::Bind(callback, false)); | 300 base::Bind(callback, false)); |
| 285 return; | 301 return; |
| 286 } | 302 } |
| 287 | 303 |
| 288 image_fetcher_->SetDataUseServiceName( | 304 image_fetcher_->SetDataUseServiceName( |
| 289 data_use_measurement::DataUseUserData::LARGE_ICON_SERVICE); | 305 data_use_measurement::DataUseUserData::LARGE_ICON_SERVICE); |
| 290 image_fetcher_->StartOrQueueNetworkRequest( | 306 image_fetcher_->StartOrQueueNetworkRequest( |
| 291 icon_url.spec(), icon_url, | 307 icon_url.spec(), icon_url, |
| 292 base::Bind(&OnFetchIconFromGoogleServerComplete, favicon_service_, | 308 base::Bind(&OnFetchIconFromGoogleServerComplete, favicon_service_, |
| 293 page_url, callback)); | 309 page_url, callback)); |
| 294 } | 310 } |
| 295 | 311 |
| 312 void LargeIconService::DecodeRawBitmap( | |
| 313 const favicon_base::LargeIconImageCallback& callback, | |
| 314 const favicon_base::LargeIconResult& raw_result) { | |
| 315 if (!raw_result.bitmap.is_valid()) { | |
| 316 // Make a copy of the icon style as it cannot be moved out from a const ref. | |
| 317 favicon_base::LargeIconImageResult result( | |
| 318 new favicon_base::FallbackIconStyle( | |
| 319 *raw_result.fallback_icon_style.get())); | |
| 320 callback.Run(result); | |
| 321 return; | |
| 322 } | |
| 323 image_fetcher_->GetImageDecoder()->DecodeImage( | |
| 324 std::string( | |
| 325 reinterpret_cast<const char*>(raw_result.bitmap.bitmap_data->front()), | |
| 326 raw_result.bitmap.bitmap_data->size()), | |
| 327 gfx::Size(), | |
| 328 base::Bind(&LargeIconService::DecodingRawBitmapFinished, | |
| 329 base::Unretained(this), callback)); | |
|
pkotwicz
2017/03/30 19:14:23
Can the decoding occur in the service worker?
Als
jkrcal
2017/03/31 12:31:11
ImageDecoder is not happy about being in the servi
pkotwicz
2017/04/02 20:46:52
I was suggesting postponing switching to gfx::Nati
| |
| 330 } | |
| 331 | |
| 332 void LargeIconService::DecodingRawBitmapFinished( | |
| 333 const favicon_base::LargeIconImageCallback& callback, | |
| 334 const gfx::Image& image) { | |
| 335 favicon_base::LargeIconImageResult result(image); | |
| 336 callback.Run(result); | |
| 337 } | |
| 338 | |
| 296 } // namespace favicon | 339 } // namespace favicon |
| OLD | NEW |