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