| 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/feature_list.h" |
| 10 #include "base/location.h" | 11 #include "base/location.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/metrics/field_trial_params.h" |
| 15 #include "base/metrics/histogram_macros.h" | 17 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 17 #include "base/task_runner.h" | 19 #include "base/task_runner.h" |
| 18 #include "base/threading/sequenced_worker_pool.h" | 20 #include "base/threading/sequenced_worker_pool.h" |
| 19 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
| 20 #include "components/data_use_measurement/core/data_use_user_data.h" | 22 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 21 #include "components/favicon/core/favicon_service.h" | 23 #include "components/favicon/core/favicon_service.h" |
| 22 #include "components/favicon_base/fallback_icon_style.h" | 24 #include "components/favicon_base/fallback_icon_style.h" |
| 23 #include "components/favicon_base/favicon_types.h" | 25 #include "components/favicon_base/favicon_types.h" |
| 24 #include "components/favicon_base/favicon_util.h" | 26 #include "components/favicon_base/favicon_util.h" |
| 25 #include "components/image_fetcher/core/request_metadata.h" | 27 #include "components/image_fetcher/core/request_metadata.h" |
| 26 #include "skia/ext/image_operations.h" | 28 #include "skia/ext/image_operations.h" |
| 27 #include "ui/gfx/codec/png_codec.h" | 29 #include "ui/gfx/codec/png_codec.h" |
| 28 #include "ui/gfx/geometry/size.h" | 30 #include "ui/gfx/geometry/size.h" |
| 29 #include "url/url_canon.h" | 31 #include "url/url_canon.h" |
| 30 | 32 |
| 31 namespace favicon { | 33 namespace favicon { |
| 32 namespace { | 34 namespace { |
| 33 | 35 |
| 36 // This feature is only used for accessing field trial parameters, not for |
| 37 // switching on/off the code. |
| 38 const base::Feature kLargeIconServiceFetchingFeature{ |
| 39 "LargeIconServiceFetching", base::FEATURE_ENABLED_BY_DEFAULT}; |
| 40 |
| 34 const char kGoogleServerV2RequestFormat[] = | 41 const char kGoogleServerV2RequestFormat[] = |
| 35 "https://t0.gstatic.com/faviconV2?" | 42 "https://t0.gstatic.com/faviconV2?" |
| 36 "client=chrome&drop_404_icon=true&size=%d&min_size=%d&max_size=%d&" | 43 "client=chrome&drop_404_icon=true&size=32&min_size=%d&max_size=64&" |
| 37 "fallback_opts=TYPE,SIZE,URL&url=%s"; | 44 "fallback_opts=TYPE,SIZE,URL&url=%s"; |
| 38 const int kGoogleServerV2MaxSizeInPixel = 128; | 45 const char kGoogleServerV2RequestFormatParam[] = "request_format"; |
| 39 const int kGoogleServerV2DesiredSizeInPixel = 64; | |
| 40 | 46 |
| 41 GURL TrimPageUrlForGoogleServer(const GURL& page_url) { | 47 GURL TrimPageUrlForGoogleServer(const GURL& page_url) { |
| 42 if (!page_url.SchemeIsHTTPOrHTTPS() || page_url.HostIsIPAddress()) | 48 if (!page_url.SchemeIsHTTPOrHTTPS() || page_url.HostIsIPAddress()) |
| 43 return GURL(); | 49 return GURL(); |
| 44 | 50 |
| 45 url::Replacements<char> replacements; | 51 url::Replacements<char> replacements; |
| 46 replacements.ClearUsername(); | 52 replacements.ClearUsername(); |
| 47 replacements.ClearPassword(); | 53 replacements.ClearPassword(); |
| 48 replacements.ClearQuery(); | 54 replacements.ClearQuery(); |
| 49 replacements.ClearRef(); | 55 replacements.ClearRef(); |
| 50 return page_url.ReplaceComponents(replacements); | 56 return page_url.ReplaceComponents(replacements); |
| 51 } | 57 } |
| 52 | 58 |
| 53 GURL GetRequestUrlForGoogleServerV2(const GURL& page_url, | 59 GURL GetRequestUrlForGoogleServerV2(const GURL& page_url, |
| 54 int min_source_size_in_pixel) { | 60 int min_source_size_in_pixel) { |
| 61 std::string url_format = base::GetFieldTrialParamValueByFeature( |
| 62 kLargeIconServiceFetchingFeature, kGoogleServerV2RequestFormatParam); |
| 63 |
| 55 return GURL(base::StringPrintf( | 64 return GURL(base::StringPrintf( |
| 56 kGoogleServerV2RequestFormat, kGoogleServerV2DesiredSizeInPixel, | 65 url_format.empty() ? kGoogleServerV2RequestFormat : url_format.c_str(), |
| 57 min_source_size_in_pixel, kGoogleServerV2MaxSizeInPixel, | 66 min_source_size_in_pixel, page_url.spec().c_str())); |
| 58 page_url.spec().c_str())); | |
| 59 } | 67 } |
| 60 | 68 |
| 61 bool IsDbResultAdequate(const favicon_base::FaviconRawBitmapResult& db_result, | 69 bool IsDbResultAdequate(const favicon_base::FaviconRawBitmapResult& db_result, |
| 62 int min_source_size) { | 70 int min_source_size) { |
| 63 return db_result.is_valid() && | 71 return db_result.is_valid() && |
| 64 db_result.pixel_size.width() == db_result.pixel_size.height() && | 72 db_result.pixel_size.width() == db_result.pixel_size.height() && |
| 65 db_result.pixel_size.width() >= min_source_size; | 73 db_result.pixel_size.width() >= min_source_size; |
| 66 } | 74 } |
| 67 | 75 |
| 68 // Wraps the PNG data in |db_result| in a gfx::Image. If |desired_size| is not | 76 // Wraps the PNG data in |db_result| in a gfx::Image. If |desired_size| is not |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 int min_source_size_in_pixel, | 322 int min_source_size_in_pixel, |
| 315 const base::Callback<void(bool success)>& callback) { | 323 const base::Callback<void(bool success)>& callback) { |
| 316 DCHECK_LE(0, min_source_size_in_pixel); | 324 DCHECK_LE(0, min_source_size_in_pixel); |
| 317 | 325 |
| 318 const GURL trimmed_page_url = TrimPageUrlForGoogleServer(page_url); | 326 const GURL trimmed_page_url = TrimPageUrlForGoogleServer(page_url); |
| 319 const GURL server_request_url = GetRequestUrlForGoogleServerV2( | 327 const GURL server_request_url = GetRequestUrlForGoogleServerV2( |
| 320 trimmed_page_url, min_source_size_in_pixel); | 328 trimmed_page_url, min_source_size_in_pixel); |
| 321 | 329 |
| 322 // Do not download if the URL is invalid after trimming, or there is a | 330 // Do not download if the URL is invalid after trimming, or there is a |
| 323 // previous cache miss recorded for |server_request_url|. | 331 // previous cache miss recorded for |server_request_url|. |
| 324 if (!trimmed_page_url.is_valid() || !image_fetcher_ || | 332 if (!server_request_url.is_valid() || !trimmed_page_url.is_valid() || |
| 333 !image_fetcher_ || |
| 325 favicon_service_->WasUnableToDownloadFavicon(server_request_url)) { | 334 favicon_service_->WasUnableToDownloadFavicon(server_request_url)) { |
| 326 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 335 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 327 base::Bind(callback, false)); | 336 base::Bind(callback, false)); |
| 328 return; | 337 return; |
| 329 } | 338 } |
| 330 | 339 |
| 331 image_fetcher_->SetDataUseServiceName( | 340 image_fetcher_->SetDataUseServiceName( |
| 332 data_use_measurement::DataUseUserData::LARGE_ICON_SERVICE); | 341 data_use_measurement::DataUseUserData::LARGE_ICON_SERVICE); |
| 333 image_fetcher_->StartOrQueueNetworkRequest( | 342 image_fetcher_->StartOrQueueNetworkRequest( |
| 334 server_request_url.spec(), server_request_url, | 343 server_request_url.spec(), server_request_url, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 354 // TODO(beaudoin): For now this is just a wrapper around | 363 // TODO(beaudoin): For now this is just a wrapper around |
| 355 // GetLargestRawFaviconForPageURL. Add the logic required to select the best | 364 // GetLargestRawFaviconForPageURL. Add the logic required to select the best |
| 356 // possible large icon. Also add logic to fetch-on-demand when the URL of | 365 // possible large icon. Also add logic to fetch-on-demand when the URL of |
| 357 // a large icon is known but its bitmap is not available. | 366 // a large icon is known but its bitmap is not available. |
| 358 return favicon_service_->GetLargestRawFaviconForPageURL( | 367 return favicon_service_->GetLargestRawFaviconForPageURL( |
| 359 page_url, large_icon_types_, min_source_size_in_pixel, | 368 page_url, large_icon_types_, min_source_size_in_pixel, |
| 360 base::Bind(&LargeIconWorker::OnIconLookupComplete, worker), tracker); | 369 base::Bind(&LargeIconWorker::OnIconLookupComplete, worker), tracker); |
| 361 } | 370 } |
| 362 | 371 |
| 363 } // namespace favicon | 372 } // namespace favicon |
| OLD | NEW |