Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: components/favicon/core/large_icon_service.cc

Issue 2721363002: Extend LargeIconService to fetch missing favicons from a Google server (Closed)
Patch Set: Add fallback_opts=TYPE Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
27 #include "url/url_canon.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&size=%d&min_size=%d&max_size=%d&"
35 "fallback_opts=TYPE&url=%s";
36 const int kGoogleServerV2MaxSizeInPixel = 256;
37 const int kGoogleServerV2DesiredSizeInPixel = 192;
38
39 GURL TrimPageUrlForGoogleServer(const GURL& page_url) {
40 if (!page_url.SchemeIsHTTPOrHTTPS() || page_url.HostIsIPAddress())
41 return GURL();
42
43 url::Replacements<char> replacements;
44 replacements.ClearUsername();
45 replacements.ClearPassword();
46 replacements.ClearQuery();
47 replacements.ClearRef();
48 return page_url.ReplaceComponents(replacements);
49 }
50
51 GURL GetIconUrlForGoogleServerV2(const GURL& page_url,
52 int min_source_size_in_pixel) {
53 return GURL(base::StringPrintf(
54 kGoogleServerV2RequestFormat, kGoogleServerV2DesiredSizeInPixel,
55 min_source_size_in_pixel, kGoogleServerV2MaxSizeInPixel,
56 page_url.spec().c_str()));
57 }
58
25 // Processes the bitmap data returned from the FaviconService as part of a 59 // Processes the bitmap data returned from the FaviconService as part of a
26 // LargeIconService request. 60 // LargeIconService request.
27 class LargeIconWorker : public base::RefCountedThreadSafe<LargeIconWorker> { 61 class LargeIconWorker : public base::RefCountedThreadSafe<LargeIconWorker> {
28 public: 62 public:
29 LargeIconWorker(int min_source_size_in_pixel, 63 LargeIconWorker(int min_source_size_in_pixel,
30 int desired_size_in_pixel, 64 int desired_size_in_pixel,
31 favicon_base::LargeIconCallback callback, 65 favicon_base::LargeIconCallback callback,
32 scoped_refptr<base::TaskRunner> background_task_runner, 66 scoped_refptr<base::TaskRunner> background_task_runner,
33 base::CancelableTaskTracker* tracker); 67 base::CancelableTaskTracker* tracker);
34 68
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 gfx::Size(desired_size_in_pixel_, desired_size_in_pixel_); 187 gfx::Size(desired_size_in_pixel_, desired_size_in_pixel_);
154 resized_bitmap_result->bitmap_data = 188 resized_bitmap_result->bitmap_data =
155 base::RefCountedBytes::TakeVector(&bitmap_data); 189 base::RefCountedBytes::TakeVector(&bitmap_data);
156 return true; 190 return true;
157 } 191 }
158 192
159 void LargeIconWorker::OnIconProcessingComplete() { 193 void LargeIconWorker::OnIconProcessingComplete() {
160 callback_.Run(*result_); 194 callback_.Run(*result_);
161 } 195 }
162 196
197 void OnFetchIconFromGoogleServerComplete(
198 FaviconService* favicon_service,
199 const GURL& page_url,
200 const base::Callback<void(bool success)>& callback,
201 const std::string& icon_url,
202 const gfx::Image& image,
203 const image_fetcher::RequestMetadata& metadata) {
204 if (image.IsEmpty()) {
205 DLOG(WARNING) << "large icon server fetch empty " << icon_url;
206 favicon_service->UnableToDownloadFavicon(GURL(icon_url));
207 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
208 base::Bind(callback, false));
209 return;
210 }
211
212 // TODO(crbug.com/699542): Extract the original icon url from the response
213 // headers if available and use it instead of |icon_url|. Possibly the type
214 // too, because using TOUCH_ICON is a hacky way that allows us to not
215 // interfere with sync.
216
217 // Write fetched icons to FaviconService's cache, but only if no icon was
218 // available (clients are encouraged to do this in advance, but meanwhile
219 // something else could've been written). By marking the icons initially
220 // expired (out-of-date), they will be refetched when we visit the original
221 // page any time in the future.
222 favicon_service->SetLastResortFavicons(page_url, GURL(icon_url),
223 favicon_base::IconType::TOUCH_ICON,
224 image, callback);
225 }
226
163 } // namespace 227 } // namespace
164 228
165 namespace favicon {
166
167 LargeIconService::LargeIconService( 229 LargeIconService::LargeIconService(
168 FaviconService* favicon_service, 230 FaviconService* favicon_service,
169 const scoped_refptr<base::TaskRunner>& background_task_runner) 231 const scoped_refptr<base::TaskRunner>& background_task_runner,
232 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher)
170 : favicon_service_(favicon_service), 233 : favicon_service_(favicon_service),
171 background_task_runner_(background_task_runner) { 234 background_task_runner_(background_task_runner),
235 image_fetcher_(std::move(image_fetcher)) {
172 large_icon_types_.push_back(favicon_base::IconType::FAVICON); 236 large_icon_types_.push_back(favicon_base::IconType::FAVICON);
173 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON); 237 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON);
174 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON); 238 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON);
175 } 239 }
176 240
177 LargeIconService::~LargeIconService() { 241 LargeIconService::~LargeIconService() {
178 } 242 }
179 243
180 base::CancelableTaskTracker::TaskId 244 base::CancelableTaskTracker::TaskId
181 LargeIconService::GetLargeIconOrFallbackStyle( 245 LargeIconService::GetLargeIconOrFallbackStyle(
(...skipping 12 matching lines...) Expand all
194 // TODO(beaudoin): For now this is just a wrapper around 258 // TODO(beaudoin): For now this is just a wrapper around
195 // GetLargestRawFaviconForPageURL. Add the logic required to select the best 259 // GetLargestRawFaviconForPageURL. Add the logic required to select the best
196 // possible large icon. Also add logic to fetch-on-demand when the URL of 260 // 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. 261 // a large icon is known but its bitmap is not available.
198 return favicon_service_->GetLargestRawFaviconForPageURL( 262 return favicon_service_->GetLargestRawFaviconForPageURL(
199 page_url, large_icon_types_, min_source_size_in_pixel, 263 page_url, large_icon_types_, min_source_size_in_pixel,
200 base::Bind(&LargeIconWorker::OnIconLookupComplete, worker), 264 base::Bind(&LargeIconWorker::OnIconLookupComplete, worker),
201 tracker); 265 tracker);
202 } 266 }
203 267
268 void LargeIconService::
269 GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
270 const GURL& page_url,
271 int min_source_size_in_pixel,
272 const base::Callback<void(bool success)>& callback) {
273 DCHECK_LE(0, min_source_size_in_pixel);
274
275 const GURL trimmed_page_url = TrimPageUrlForGoogleServer(page_url);
276 const GURL icon_url =
277 GetIconUrlForGoogleServerV2(trimmed_page_url, min_source_size_in_pixel);
278
279 // Do not download if the URL is invalid after trimming, or there is a
280 // previous cache miss recorded for |icon_url|.
281 if (!trimmed_page_url.is_valid() || !image_fetcher_ ||
282 favicon_service_->WasUnableToDownloadFavicon(icon_url)) {
283 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
284 base::Bind(callback, false));
285 return;
286 }
287
288 image_fetcher_->SetDataUseServiceName(
289 data_use_measurement::DataUseUserData::LARGE_ICON_SERVICE);
290 image_fetcher_->StartOrQueueNetworkRequest(
291 icon_url.spec(), icon_url,
292 base::Bind(&OnFetchIconFromGoogleServerComplete, favicon_service_,
293 page_url, callback));
294 }
295
204 } // namespace favicon 296 } // namespace favicon
OLDNEW
« no previous file with comments | « components/favicon/core/large_icon_service.h ('k') | components/favicon/core/large_icon_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698