| 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/request_metadata.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/faviconV2?" | 34 "https://t0.gstatic.com/faviconV2?" |
| 34 "client=chrome&drop_404_icon=true&size=%d&min_size=%d&max_size=%d&" | 35 "client=chrome&drop_404_icon=true&size=%d&min_size=%d&max_size=%d&" |
| 35 "fallback_opts=TYPE,SIZE,URL&url=%s"; | 36 "fallback_opts=TYPE,SIZE,URL&url=%s"; |
| 36 const int kGoogleServerV2MaxSizeInPixel = 128; | 37 const int kGoogleServerV2MaxSizeInPixel = 128; |
| 37 const int kGoogleServerV2DesiredSizeInPixel = 64; | 38 const int kGoogleServerV2DesiredSizeInPixel = 64; |
| 38 | 39 |
| 39 GURL TrimPageUrlForGoogleServer(const GURL& page_url) { | 40 GURL TrimPageUrlForGoogleServer(const GURL& page_url) { |
| 40 if (!page_url.SchemeIsHTTPOrHTTPS() || page_url.HostIsIPAddress()) | 41 if (!page_url.SchemeIsHTTPOrHTTPS() || page_url.HostIsIPAddress()) |
| 41 return GURL(); | 42 return GURL(); |
| 42 | 43 |
| 43 url::Replacements<char> replacements; | 44 url::Replacements<char> replacements; |
| 44 replacements.ClearUsername(); | 45 replacements.ClearUsername(); |
| 45 replacements.ClearPassword(); | 46 replacements.ClearPassword(); |
| 46 replacements.ClearQuery(); | 47 replacements.ClearQuery(); |
| 47 replacements.ClearRef(); | 48 replacements.ClearRef(); |
| 48 return page_url.ReplaceComponents(replacements); | 49 return page_url.ReplaceComponents(replacements); |
| 49 } | 50 } |
| 50 | 51 |
| 51 GURL GetIconUrlForGoogleServerV2(const GURL& page_url, | 52 GURL GetRequestUrlForGoogleServerV2(const GURL& page_url, |
| 52 int min_source_size_in_pixel) { | 53 int min_source_size_in_pixel) { |
| 53 return GURL(base::StringPrintf( | 54 return GURL(base::StringPrintf( |
| 54 kGoogleServerV2RequestFormat, kGoogleServerV2DesiredSizeInPixel, | 55 kGoogleServerV2RequestFormat, kGoogleServerV2DesiredSizeInPixel, |
| 55 min_source_size_in_pixel, kGoogleServerV2MaxSizeInPixel, | 56 min_source_size_in_pixel, kGoogleServerV2MaxSizeInPixel, |
| 56 page_url.spec().c_str())); | 57 page_url.spec().c_str())); |
| 57 } | 58 } |
| 58 | 59 |
| 59 bool IsDbResultAdequate(const favicon_base::FaviconRawBitmapResult& db_result, | 60 bool IsDbResultAdequate(const favicon_base::FaviconRawBitmapResult& db_result, |
| 60 int min_source_size) { | 61 int min_source_size) { |
| 61 return db_result.is_valid() && | 62 return db_result.is_valid() && |
| 62 db_result.pixel_size.width() == db_result.pixel_size.height() && | 63 db_result.pixel_size.width() == db_result.pixel_size.height() && |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 return; | 214 return; |
| 214 } | 215 } |
| 215 image_callback_.Run( | 216 image_callback_.Run( |
| 216 favicon_base::LargeIconImageResult(fallback_icon_style_.release())); | 217 favicon_base::LargeIconImageResult(fallback_icon_style_.release())); |
| 217 } | 218 } |
| 218 | 219 |
| 219 void OnFetchIconFromGoogleServerComplete( | 220 void OnFetchIconFromGoogleServerComplete( |
| 220 FaviconService* favicon_service, | 221 FaviconService* favicon_service, |
| 221 const GURL& page_url, | 222 const GURL& page_url, |
| 222 const base::Callback<void(bool success)>& callback, | 223 const base::Callback<void(bool success)>& callback, |
| 223 const std::string& icon_url, | 224 const std::string& server_request_url, |
| 224 const gfx::Image& image, | 225 const gfx::Image& image, |
| 225 const image_fetcher::RequestMetadata& metadata) { | 226 const image_fetcher::RequestMetadata& metadata) { |
| 226 if (image.IsEmpty()) { | 227 if (image.IsEmpty()) { |
| 227 DLOG(WARNING) << "large icon server fetch empty " << icon_url; | 228 DLOG(WARNING) << "large icon server fetch empty " << server_request_url; |
| 228 favicon_service->UnableToDownloadFavicon(GURL(icon_url)); | 229 favicon_service->UnableToDownloadFavicon(GURL(server_request_url)); |
| 229 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 230 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 230 base::Bind(callback, false)); | 231 base::Bind(callback, false)); |
| 231 return; | 232 return; |
| 232 } | 233 } |
| 233 | 234 |
| 234 // TODO(crbug.com/699542): Extract the original icon url from the response | 235 // If given, use the original favicon URL from Content-Location http header. |
| 235 // headers if available and use it instead of |icon_url|. Possibly the type | 236 // Otherwise, use the request URL as fallback. |
| 236 // too, because using TOUCH_ICON is a hacky way that allows us to not | 237 std::string original_icon_url = metadata.content_location_header; |
| 237 // interfere with sync. | 238 if (original_icon_url.empty()) { |
| 239 original_icon_url = server_request_url; |
| 240 } |
| 238 | 241 |
| 239 // Write fetched icons to FaviconService's cache, but only if no icon was | 242 // Write fetched icons to FaviconService's cache, but only if no icon was |
| 240 // available (clients are encouraged to do this in advance, but meanwhile | 243 // available (clients are encouraged to do this in advance, but meanwhile |
| 241 // something else could've been written). By marking the icons initially | 244 // something else could've been written). By marking the icons initially |
| 242 // expired (out-of-date), they will be refetched when we visit the original | 245 // expired (out-of-date), they will be refetched when we visit the original |
| 243 // page any time in the future. | 246 // page any time in the future. |
| 244 favicon_service->SetLastResortFavicons(page_url, GURL(icon_url), | 247 favicon_service->SetLastResortFavicons(page_url, GURL(original_icon_url), |
| 245 favicon_base::IconType::TOUCH_ICON, | 248 favicon_base::IconType::TOUCH_ICON, |
| 246 image, callback); | 249 image, callback); |
| 247 } | 250 } |
| 248 | 251 |
| 249 } // namespace | 252 } // namespace |
| 250 | 253 |
| 251 LargeIconService::LargeIconService( | 254 LargeIconService::LargeIconService( |
| 252 FaviconService* favicon_service, | 255 FaviconService* favicon_service, |
| 253 const scoped_refptr<base::TaskRunner>& background_task_runner, | 256 const scoped_refptr<base::TaskRunner>& background_task_runner, |
| 254 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) | 257 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 } | 291 } |
| 289 | 292 |
| 290 void LargeIconService:: | 293 void LargeIconService:: |
| 291 GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( | 294 GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( |
| 292 const GURL& page_url, | 295 const GURL& page_url, |
| 293 int min_source_size_in_pixel, | 296 int min_source_size_in_pixel, |
| 294 const base::Callback<void(bool success)>& callback) { | 297 const base::Callback<void(bool success)>& callback) { |
| 295 DCHECK_LE(0, min_source_size_in_pixel); | 298 DCHECK_LE(0, min_source_size_in_pixel); |
| 296 | 299 |
| 297 const GURL trimmed_page_url = TrimPageUrlForGoogleServer(page_url); | 300 const GURL trimmed_page_url = TrimPageUrlForGoogleServer(page_url); |
| 298 const GURL icon_url = | 301 const GURL server_request_url = GetRequestUrlForGoogleServerV2( |
| 299 GetIconUrlForGoogleServerV2(trimmed_page_url, min_source_size_in_pixel); | 302 trimmed_page_url, min_source_size_in_pixel); |
| 300 | 303 |
| 301 // Do not download if the URL is invalid after trimming, or there is a | 304 // Do not download if the URL is invalid after trimming, or there is a |
| 302 // previous cache miss recorded for |icon_url|. | 305 // previous cache miss recorded for |server_request_url|. |
| 303 if (!trimmed_page_url.is_valid() || !image_fetcher_ || | 306 if (!trimmed_page_url.is_valid() || !image_fetcher_ || |
| 304 favicon_service_->WasUnableToDownloadFavicon(icon_url)) { | 307 favicon_service_->WasUnableToDownloadFavicon(server_request_url)) { |
| 305 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 308 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 306 base::Bind(callback, false)); | 309 base::Bind(callback, false)); |
| 307 return; | 310 return; |
| 308 } | 311 } |
| 309 | 312 |
| 310 image_fetcher_->SetDataUseServiceName( | 313 image_fetcher_->SetDataUseServiceName( |
| 311 data_use_measurement::DataUseUserData::LARGE_ICON_SERVICE); | 314 data_use_measurement::DataUseUserData::LARGE_ICON_SERVICE); |
| 312 image_fetcher_->StartOrQueueNetworkRequest( | 315 image_fetcher_->StartOrQueueNetworkRequest( |
| 313 icon_url.spec(), icon_url, | 316 server_request_url.spec(), server_request_url, |
| 314 base::Bind(&OnFetchIconFromGoogleServerComplete, favicon_service_, | 317 base::Bind(&OnFetchIconFromGoogleServerComplete, favicon_service_, |
| 315 page_url, callback)); | 318 page_url, callback)); |
| 316 } | 319 } |
| 317 | 320 |
| 318 base::CancelableTaskTracker::TaskId | 321 base::CancelableTaskTracker::TaskId |
| 319 LargeIconService::GetLargeIconOrFallbackStyleImpl( | 322 LargeIconService::GetLargeIconOrFallbackStyleImpl( |
| 320 const GURL& page_url, | 323 const GURL& page_url, |
| 321 int min_source_size_in_pixel, | 324 int min_source_size_in_pixel, |
| 322 int desired_size_in_pixel, | 325 int desired_size_in_pixel, |
| 323 const favicon_base::LargeIconCallback& raw_bitmap_callback, | 326 const favicon_base::LargeIconCallback& raw_bitmap_callback, |
| 324 const favicon_base::LargeIconImageCallback& image_callback, | 327 const favicon_base::LargeIconImageCallback& image_callback, |
| 325 base::CancelableTaskTracker* tracker) { | 328 base::CancelableTaskTracker* tracker) { |
| 326 DCHECK_LE(1, min_source_size_in_pixel); | 329 DCHECK_LE(1, min_source_size_in_pixel); |
| 327 DCHECK_LE(0, desired_size_in_pixel); | 330 DCHECK_LE(0, desired_size_in_pixel); |
| 328 | 331 |
| 329 scoped_refptr<LargeIconWorker> worker = new LargeIconWorker( | 332 scoped_refptr<LargeIconWorker> worker = new LargeIconWorker( |
| 330 min_source_size_in_pixel, desired_size_in_pixel, raw_bitmap_callback, | 333 min_source_size_in_pixel, desired_size_in_pixel, raw_bitmap_callback, |
| 331 image_callback, background_task_runner_, tracker); | 334 image_callback, background_task_runner_, tracker); |
| 332 | 335 |
| 333 // TODO(beaudoin): For now this is just a wrapper around | 336 // TODO(beaudoin): For now this is just a wrapper around |
| 334 // GetLargestRawFaviconForPageURL. Add the logic required to select the best | 337 // GetLargestRawFaviconForPageURL. Add the logic required to select the best |
| 335 // possible large icon. Also add logic to fetch-on-demand when the URL of | 338 // possible large icon. Also add logic to fetch-on-demand when the URL of |
| 336 // a large icon is known but its bitmap is not available. | 339 // a large icon is known but its bitmap is not available. |
| 337 return favicon_service_->GetLargestRawFaviconForPageURL( | 340 return favicon_service_->GetLargestRawFaviconForPageURL( |
| 338 page_url, large_icon_types_, min_source_size_in_pixel, | 341 page_url, large_icon_types_, min_source_size_in_pixel, |
| 339 base::Bind(&LargeIconWorker::OnIconLookupComplete, worker), tracker); | 342 base::Bind(&LargeIconWorker::OnIconLookupComplete, worker), tracker); |
| 340 } | 343 } |
| 341 | 344 |
| 342 } // namespace favicon | 345 } // namespace favicon |
| OLD | NEW |