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

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: Added missing dependencies. 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"
24 #include "components/image_fetcher/image_fetcher.h"
19 #include "skia/ext/image_operations.h" 25 #include "skia/ext/image_operations.h"
20 #include "ui/gfx/codec/png_codec.h" 26 #include "ui/gfx/codec/png_codec.h"
21 #include "ui/gfx/geometry/size.h" 27 #include "ui/gfx/geometry/size.h"
22 28
29 namespace favicon {
23 namespace { 30 namespace {
24 31
32 using image_fetcher::ImageFetcher;
pkotwicz 2017/03/03 22:33:25 Do you need this using statement? Personally, I am
mastiz 2017/03/06 10:25:33 Removed. I'm also not a big fan of using declarati
33
34 // Type APPLE_TOUCH includes TOUCH_ICONs as well as FAVICONs as fallback.
pkotwicz 2017/03/03 22:33:25 Can you please mention in the comment whether TOUC
mastiz 2017/03/06 10:25:33 Done. I also added a TODO because preference towar
35 const char kGoogleServerV2RequestFormat[] =
36 "https://t0.gstatic.com/"
37 "faviconV2?url=%s&type=APPLE_TOUCH&size=%d&min_size=%d&max_size=%d";
pkotwicz 2017/03/03 22:33:25 I tested this URL in the browser. Is it possible t
mastiz 2017/03/06 10:25:33 jkrcal@ will talk to bing to disable this on the s
38 const int kGoogleServerV2MaxSizeInPixel = 256;
39 const int kGoogleServerV2DesiredSizeInPixel = 192;
40
41 GURL GetIconUrlForGoogleServerV2(const GURL& page_url,
42 int min_source_size_in_pixel) {
pkotwicz 2017/03/03 22:33:25 Don't you want to hard code the minimum size reque
mastiz 2017/03/06 10:25:33 We actually have clients with different requiremen
pkotwicz 2017/03/08 05:38:16 I see your point now
43 return GURL(base::StringPrintf(
44 kGoogleServerV2RequestFormat, page_url.spec().c_str(),
45 kGoogleServerV2DesiredSizeInPixel, min_source_size_in_pixel,
46 kGoogleServerV2MaxSizeInPixel));
47 }
48
25 // Processes the bitmap data returned from the FaviconService as part of a 49 // Processes the bitmap data returned from the FaviconService as part of a
26 // LargeIconService request. 50 // LargeIconService request.
27 class LargeIconWorker : public base::RefCountedThreadSafe<LargeIconWorker> { 51 class LargeIconWorker : public base::RefCountedThreadSafe<LargeIconWorker> {
28 public: 52 public:
29 LargeIconWorker(int min_source_size_in_pixel, 53 LargeIconWorker(int min_source_size_in_pixel,
30 int desired_size_in_pixel, 54 int desired_size_in_pixel,
31 favicon_base::LargeIconCallback callback, 55 favicon_base::LargeIconCallback callback,
32 scoped_refptr<base::TaskRunner> background_task_runner, 56 scoped_refptr<base::TaskRunner> background_task_runner,
33 base::CancelableTaskTracker* tracker); 57 base::CancelableTaskTracker* tracker);
34 58
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 gfx::Size(desired_size_in_pixel_, desired_size_in_pixel_); 177 gfx::Size(desired_size_in_pixel_, desired_size_in_pixel_);
154 resized_bitmap_result->bitmap_data = 178 resized_bitmap_result->bitmap_data =
155 base::RefCountedBytes::TakeVector(&bitmap_data); 179 base::RefCountedBytes::TakeVector(&bitmap_data);
156 return true; 180 return true;
157 } 181 }
158 182
159 void LargeIconWorker::OnIconProcessingComplete() { 183 void LargeIconWorker::OnIconProcessingComplete() {
160 callback_.Run(*result_); 184 callback_.Run(*result_);
161 } 185 }
162 186
187 void OnFetchIconFromGoogleServerComplete(
188 FaviconService* favicon_service,
189 const GURL& page_url,
190 const base::Callback<void(bool success)>& callback,
191 const std::string& icon_url,
192 const gfx::Image& image) {
193 if (image.IsEmpty()) {
194 DLOG(WARNING) << "large icon server fetch empty " << icon_url;
195 favicon_service->UnableToDownloadFavicon(GURL(icon_url));
196 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
197 base::Bind(callback, false));
198 return;
199 }
200
201 // TODO(jkrcal): Extract the original icon url from the response headers if
202 // available and use it instead of |icon_url|.
pkotwicz 2017/03/03 22:33:25 Please add a TODO for getting the icon type too
mastiz 2017/03/06 10:25:33 What would we use this for? I thought we agreed TO
pkotwicz 2017/03/08 05:38:16 I suggested the TODO because always storing as a T
mastiz 2017/03/08 16:37:33 Extended the TODO with this part and clarified the
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->SetExpiredFaviconsIfNoneKnown(
210 page_url, GURL(icon_url), favicon_base::IconType::TOUCH_ICON, image);
211 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
212 base::Bind(callback, true));
213 }
214
163 } // namespace 215 } // namespace
164 216
165 namespace favicon {
166
167 LargeIconService::LargeIconService( 217 LargeIconService::LargeIconService(
168 FaviconService* favicon_service, 218 FaviconService* favicon_service,
169 const scoped_refptr<base::TaskRunner>& background_task_runner) 219 const scoped_refptr<base::TaskRunner>& background_task_runner,
220 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher)
170 : favicon_service_(favicon_service), 221 : favicon_service_(favicon_service),
171 background_task_runner_(background_task_runner) { 222 background_task_runner_(background_task_runner),
223 image_fetcher_(std::move(image_fetcher)) {
172 large_icon_types_.push_back(favicon_base::IconType::FAVICON); 224 large_icon_types_.push_back(favicon_base::IconType::FAVICON);
173 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON); 225 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON);
174 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON); 226 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON);
175 } 227 }
176 228
177 LargeIconService::~LargeIconService() { 229 LargeIconService::~LargeIconService() {
178 } 230 }
179 231
180 base::CancelableTaskTracker::TaskId 232 base::CancelableTaskTracker::TaskId
181 LargeIconService::GetLargeIconOrFallbackStyle( 233 LargeIconService::GetLargeIconOrFallbackStyle(
(...skipping 12 matching lines...) Expand all
194 // TODO(beaudoin): For now this is just a wrapper around 246 // TODO(beaudoin): For now this is just a wrapper around
195 // GetLargestRawFaviconForPageURL. Add the logic required to select the best 247 // GetLargestRawFaviconForPageURL. Add the logic required to select the best
196 // possible large icon. Also add logic to fetch-on-demand when the URL of 248 // 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. 249 // a large icon is known but its bitmap is not available.
198 return favicon_service_->GetLargestRawFaviconForPageURL( 250 return favicon_service_->GetLargestRawFaviconForPageURL(
199 page_url, large_icon_types_, min_source_size_in_pixel, 251 page_url, large_icon_types_, min_source_size_in_pixel,
200 base::Bind(&LargeIconWorker::OnIconLookupComplete, worker), 252 base::Bind(&LargeIconWorker::OnIconLookupComplete, worker),
201 tracker); 253 tracker);
202 } 254 }
203 255
256 void LargeIconService::
257 GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
258 const GURL& page_url,
259 int min_source_size_in_pixel,
260 const base::Callback<void(bool success)>& callback) {
261 DCHECK_LE(0, min_source_size_in_pixel);
262
263 const GURL icon_url =
264 GetIconUrlForGoogleServerV2(page_url, min_source_size_in_pixel);
265
266 // Do not retry if there is a previous cache miss recorded for |icon_url|.
pkotwicz 2017/03/03 22:33:25 How about: "Do not do download if there is a previ
mastiz 2017/03/06 10:25:33 Done.
267 if (!image_fetcher_ ||
268 favicon_service_->WasUnableToDownloadFavicon(icon_url)) {
269 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
270 base::Bind(callback, false));
271 return;
272 }
273
274 image_fetcher_->SetDataUseServiceName(
275 data_use_measurement::DataUseUserData::LARGE_ICON_SERVICE);
276 image_fetcher_->StartOrQueueNetworkRequest(
277 icon_url.spec(), icon_url,
278 base::Bind(&OnFetchIconFromGoogleServerComplete, favicon_service_,
279 page_url, callback));
280 }
281
204 } // namespace favicon 282 } // namespace favicon
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698