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

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

Issue 2739173002: Always select best favicon bitmap (Closed)
Patch Set: More comments. Created 3 years, 8 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/favicon_handler.h" 5 #include "components/favicon/core/favicon_handler.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <utility>
9 #include <vector> 10 #include <vector>
10 11
11 #include "base/bind.h" 12 #include "base/bind.h"
12 #include "base/bind_helpers.h" 13 #include "base/bind_helpers.h"
13 #include "base/memory/ref_counted_memory.h" 14 #include "base/memory/ref_counted_memory.h"
14 #include "build/build_config.h" 15 #include "build/build_config.h"
15 #include "components/favicon/core/favicon_service.h" 16 #include "components/favicon/core/favicon_service.h"
16 #include "components/favicon_base/favicon_util.h" 17 #include "components/favicon_base/favicon_util.h"
17 #include "components/favicon_base/select_favicon_frames.h" 18 #include "components/favicon_base/select_favicon_frames.h"
18 #include "skia/ext/image_operations.h" 19 #include "skia/ext/image_operations.h"
19 #include "ui/gfx/codec/png_codec.h" 20 #include "ui/gfx/codec/png_codec.h"
20 #include "ui/gfx/image/image_skia.h" 21 #include "ui/gfx/image/image_skia.h"
21 #include "ui/gfx/image/image_util.h" 22 #include "ui/gfx/image/image_util.h"
22 23
23 namespace favicon { 24 namespace favicon {
24 namespace { 25 namespace {
25 26
27 const int kNonTouchLargestIconSize = 192;
pkotwicz 2017/03/28 20:45:39 Nit: New line
mastiz 2017/03/29 08:53:01 Done.
26 // Size (along each axis) of a touch icon. This currently corresponds to 28 // Size (along each axis) of a touch icon. This currently corresponds to
27 // the apple touch icon for iPad. 29 // the apple touch icon for iPad.
28 const int kTouchIconSize = 144; 30 const int kTouchIconSize = 144;
29 31
30 bool DoUrlAndIconMatch(const FaviconURL& favicon_url,
31 const GURL& url,
32 favicon_base::IconType icon_type) {
33 return favicon_url.icon_url == url && favicon_url.icon_type == icon_type;
34 }
35
36 // Returns true if all of the icon URLs and icon types in |bitmap_results| are 32 // Returns true if all of the icon URLs and icon types in |bitmap_results| are
37 // identical and if they match the icon URL and icon type in |favicon_url|. 33 // identical and if they match |icon_url| and |icon_type|. Returns false if
38 // Returns false if |bitmap_results| is empty. 34 // |bitmap_results| is empty.
39 bool DoUrlsAndIconsMatch( 35 bool DoUrlsAndIconsMatch(
40 const FaviconURL& favicon_url, 36 const GURL& icon_url,
37 favicon_base::IconType icon_type,
41 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { 38 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) {
42 if (bitmap_results.empty()) 39 if (bitmap_results.empty())
43 return false; 40 return false;
44 41
45 const favicon_base::IconType icon_type = favicon_url.icon_type;
46
47 for (const auto& bitmap_result : bitmap_results) { 42 for (const auto& bitmap_result : bitmap_results) {
48 if (favicon_url.icon_url != bitmap_result.icon_url || 43 if (icon_url != bitmap_result.icon_url ||
49 icon_type != bitmap_result.icon_type) { 44 icon_type != bitmap_result.icon_type) {
50 return false; 45 return false;
51 } 46 }
52 } 47 }
53 return true; 48 return true;
54 } 49 }
55 50
56 // Return true if |bitmap_result| is expired. 51 // Return true if |bitmap_result| is expired.
57 bool IsExpired(const favicon_base::FaviconRawBitmapResult& bitmap_result) { 52 bool IsExpired(const favicon_base::FaviconRawBitmapResult& bitmap_result) {
58 return bitmap_result.expired; 53 return bitmap_result.expired;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 return false; 101 return false;
107 } 102 }
108 103
109 // Returns true if at least one of |bitmap_results| is valid. 104 // Returns true if at least one of |bitmap_results| is valid.
110 bool HasValidResult( 105 bool HasValidResult(
111 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { 106 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) {
112 return std::find_if(bitmap_results.begin(), bitmap_results.end(), IsValid) != 107 return std::find_if(bitmap_results.begin(), bitmap_results.end(), IsValid) !=
113 bitmap_results.end(); 108 bitmap_results.end();
114 } 109 }
115 110
116 // Returns the index of the entry with the largest area. 111 std::vector<int> GetDesiredPixelSizes(
117 int GetLargestSizeIndex(const std::vector<gfx::Size>& sizes) { 112 FaviconDriverObserver::NotificationIconType handler_type) {
118 DCHECK(!sizes.empty()); 113 switch (handler_type) {
119 size_t ret = 0; 114 case FaviconDriverObserver::NON_TOUCH_16_DIP: {
120 for (size_t i = 1; i < sizes.size(); ++i) { 115 std::vector<int> pixel_sizes;
121 if (sizes[ret].GetArea() < sizes[i].GetArea()) 116 for (float scale_factor : favicon_base::GetFaviconScales())
pkotwicz 2017/03/28 20:45:39 Can you add braces to the for() ?
mastiz 2017/03/29 08:53:01 Done.
122 ret = i; 117 pixel_sizes.push_back(
118 static_cast<int>(ceil(scale_factor * gfx::kFaviconSize)));
119 return pixel_sizes;
120 }
121 case FaviconDriverObserver::NON_TOUCH_LARGEST:
122 return std::vector<int>(1U, kNonTouchLargestIconSize);
123 case FaviconDriverObserver::TOUCH_LARGEST:
124 return std::vector<int>(1U, kTouchIconSize);
123 } 125 }
124 return static_cast<int>(ret); 126 NOTREACHED();
125 }
126
127 // Return the index of a size which is same as the given |size|, -1 returned if
128 // there is no such bitmap.
129 int GetIndexBySize(const std::vector<gfx::Size>& sizes,
130 const gfx::Size& size) {
131 DCHECK(!sizes.empty());
132 std::vector<gfx::Size>::const_iterator i =
133 std::find(sizes.begin(), sizes.end(), size);
134 if (i == sizes.end())
135 return -1;
136
137 return static_cast<int>(i - sizes.begin());
138 }
139
140 // Compare function used for std::stable_sort to sort as descend.
141 bool CompareIconSize(const FaviconURL& b1, const FaviconURL& b2) {
142 int area1 = 0;
143 if (!b1.icon_sizes.empty())
144 area1 = b1.icon_sizes.front().GetArea();
145
146 int area2 = 0;
147 if (!b2.icon_sizes.empty())
148 area2 = b2.icon_sizes.front().GetArea();
149
150 return area1 > area2;
151 }
152
153 // Sorts the entries in |image_urls| by icon size in descending order.
154 // Discards all but the largest size for each FaviconURL.
155 void SortAndPruneImageUrls(std::vector<FaviconURL>* image_urls) {
156 // Not using const-reference since the loop mutates FaviconURL::icon_sizes.
157 for (FaviconURL& image_url : *image_urls) {
158 if (image_url.icon_sizes.empty())
159 continue;
160
161 gfx::Size largest =
162 image_url.icon_sizes[GetLargestSizeIndex(image_url.icon_sizes)];
163 image_url.icon_sizes.clear();
164 image_url.icon_sizes.push_back(largest);
165 }
166 std::stable_sort(image_urls->begin(), image_urls->end(), CompareIconSize);
167 }
168
169 // Checks whether two FaviconURLs are equal ignoring the icon sizes.
170 bool FaviconURLsEqualIgnoringSizes(const FaviconURL& u1, const FaviconURL& u2) {
171 return u1.icon_type == u2.icon_type && u1.icon_url == u2.icon_url;
172 } 127 }
173 128
174 } // namespace 129 } // namespace
175 130
176 //////////////////////////////////////////////////////////////////////////////// 131 ////////////////////////////////////////////////////////////////////////////////
177 132
178 FaviconHandler::FaviconCandidate::FaviconCandidate() 133 // static
179 : score(0), icon_type(favicon_base::INVALID_ICON) { 134 FaviconHandler::FaviconCandidate
135 FaviconHandler::FaviconCandidate::FromFaviconURL(
136 const favicon::FaviconURL& favicon_url,
137 const std::vector<int>& desired_pixel_sizes) {
138 FaviconCandidate candidate;
139 candidate.icon_url = favicon_url.icon_url;
140 candidate.icon_type = favicon_url.icon_type;
141 // TODO(crbug.com/705900): For candidates without explicit size information,
142 // sizes could be inferred for the most common cases. Namely, .ico files tend
143 // to contain the 16x16 bitmap, which would allow to improve the
144 // prioritization on desktop.
145 SelectFaviconFrameIndices(favicon_url.icon_sizes, desired_pixel_sizes,
146 /*best_indices=*/nullptr, &candidate.score);
147 return candidate;
180 } 148 }
181 149
182 FaviconHandler::FaviconCandidate::~FaviconCandidate() {
183 }
184
185 FaviconHandler::FaviconCandidate::FaviconCandidate(
186 const GURL& image_url,
187 const gfx::Image& image,
188 float score,
189 favicon_base::IconType icon_type)
190 : image_url(image_url),
191 image(image),
192 score(score),
193 icon_type(icon_type) {}
194
195 //////////////////////////////////////////////////////////////////////////////// 150 ////////////////////////////////////////////////////////////////////////////////
196 151
197 FaviconHandler::FaviconHandler( 152 FaviconHandler::FaviconHandler(
198 FaviconService* service, 153 FaviconService* service,
199 Delegate* delegate, 154 Delegate* delegate,
200 FaviconDriverObserver::NotificationIconType handler_type) 155 FaviconDriverObserver::NotificationIconType handler_type)
201 : handler_type_(handler_type), 156 : handler_type_(handler_type),
202 got_favicon_from_history_(false), 157 got_favicon_from_history_(false),
203 initial_history_result_expired_or_incomplete_(false), 158 initial_history_result_expired_or_incomplete_(false),
204 redownload_icons_(false), 159 redownload_icons_(false),
(...skipping 26 matching lines...) Expand all
231 186
232 void FaviconHandler::FetchFavicon(const GURL& url) { 187 void FaviconHandler::FetchFavicon(const GURL& url) {
233 cancelable_task_tracker_.TryCancelAll(); 188 cancelable_task_tracker_.TryCancelAll();
234 189
235 url_ = url; 190 url_ = url;
236 191
237 initial_history_result_expired_or_incomplete_ = false; 192 initial_history_result_expired_or_incomplete_ = false;
238 redownload_icons_ = false; 193 redownload_icons_ = false;
239 got_favicon_from_history_ = false; 194 got_favicon_from_history_ = false;
240 download_request_.Cancel(); 195 download_request_.Cancel();
241 image_urls_.clear(); 196 candidates_.clear();
242 notification_icon_url_ = GURL(); 197 notification_icon_url_ = GURL();
243 notification_icon_type_ = favicon_base::INVALID_ICON; 198 notification_icon_type_ = favicon_base::INVALID_ICON;
244 current_candidate_index_ = 0u; 199 current_candidate_index_ = 0u;
245 best_favicon_candidate_ = FaviconCandidate(); 200 best_favicon_ = DownloadedFavicon();
246 201
247 // Request the favicon from the history service. In parallel to this the 202 // Request the favicon from the history service. In parallel to this the
248 // renderer is going to notify us (well WebContents) when the favicon url is 203 // renderer is going to notify us (well WebContents) when the favicon url is
249 // available. 204 // available.
250 if (service_) { 205 if (service_) {
251 service_->GetFaviconForPageURL( 206 service_->GetFaviconForPageURL(
252 url_, icon_types_, preferred_icon_size(), 207 url_, icon_types_, preferred_icon_size(),
253 base::Bind( 208 base::Bind(
254 &FaviconHandler::OnFaviconDataForInitialURLFromFaviconService, 209 &FaviconHandler::OnFaviconDataForInitialURLFromFaviconService,
255 base::Unretained(this)), 210 base::Unretained(this)),
256 &cancelable_task_tracker_); 211 &cancelable_task_tracker_);
257 } 212 }
258 } 213 }
259 214
260 bool FaviconHandler::UpdateFaviconCandidate(const GURL& image_url, 215 bool FaviconHandler::UpdateFaviconCandidate(
261 const gfx::Image& image, 216 const DownloadedFavicon& downloaded_favicon) {
262 float score, 217 if (downloaded_favicon.candidate.score > best_favicon_.candidate.score)
263 favicon_base::IconType icon_type) { 218 best_favicon_ = downloaded_favicon;
264 bool replace_best_favicon_candidate = false; 219
265 bool exact_match = false;
266 if (download_largest_icon_) { 220 if (download_largest_icon_) {
267 replace_best_favicon_candidate = 221 // The size of the downloaded icon may not match the declared size. It's
268 image.Size().GetArea() > 222 // important to stop downloading if:
269 best_favicon_candidate_.image.Size().GetArea();
270
271 gfx::Size largest = best_favicon_candidate_.image.Size();
272 if (replace_best_favicon_candidate)
273 largest = image.Size();
274
275 // The size of the downloaded icon may not match the declared size. Stop
276 // downloading if:
277 // - current candidate is only candidate. 223 // - current candidate is only candidate.
278 // - next candidate doesn't have sizes attributes, in this case, the rest 224 // - next candidate has sizes attribute and it is not better than the best
279 // candidates don't have sizes attribute either, stop downloading now, 225 // one observed so far, which means any following candidate should also
280 // otherwise, all favicon without sizes attribute are downloaded. 226 // be worse or equal too.
281 // - next candidate has sizes attribute and it is not larger than largest, 227 // - next candidate doesn't have sizes attributes, which means further
282 // - current candidate is maximal one we want. 228 // candidates don't have sizes attribute either (because the score lowest
283 const int maximal_size = GetMaximalIconSize(icon_type); 229 // and hence get sorted last during prioritization). We stop immediately
284 if (current_candidate_index_ + 1 >= image_urls_.size()) { 230 // to avoid downloading them all, although we don't have the certainty
285 exact_match = true; 231 // that no better favicon is among them.
286 } else { 232 return current_candidate_index_ + 1 >= candidates_.size() ||
287 FaviconURL next_image_url = image_urls_[current_candidate_index_ + 1]; 233 candidates_[current_candidate_index_ + 1].score <=
288 exact_match = next_image_url.icon_sizes.empty() || 234 best_favicon_.candidate.score;
289 next_image_url.icon_sizes[0].GetArea() <= largest.GetArea() ||
290 (image.Size().width() == maximal_size &&
291 image.Size().height() == maximal_size);
292 }
293 } else { 235 } else {
294 exact_match = score == 1 || preferred_icon_size() == 0; 236 return best_favicon_.candidate.score == 1;
295 replace_best_favicon_candidate =
296 exact_match ||
297 best_favicon_candidate_.icon_type == favicon_base::INVALID_ICON ||
298 score > best_favicon_candidate_.score;
299 } 237 }
300 if (replace_best_favicon_candidate) {
301 best_favicon_candidate_ =
302 FaviconCandidate(image_url, image, score, icon_type);
303 }
304 return exact_match;
305 } 238 }
306 239
307 void FaviconHandler::SetFavicon(const GURL& icon_url, 240 void FaviconHandler::SetFavicon(const GURL& icon_url,
308 const gfx::Image& image, 241 const gfx::Image& image,
309 favicon_base::IconType icon_type) { 242 favicon_base::IconType icon_type) {
310 if (service_ && ShouldSaveFavicon()) 243 if (service_ && ShouldSaveFavicon())
311 service_->SetFavicons(url_, icon_url, icon_type, image); 244 service_->SetFavicons(url_, icon_url, icon_type, image);
312 245
313 NotifyFaviconUpdated(icon_url, icon_type, image); 246 NotifyFaviconUpdated(icon_url, icon_type, image);
314 } 247 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 notification_icon_url_ = icon_url; 280 notification_icon_url_ = icon_url;
348 notification_icon_type_ = icon_type; 281 notification_icon_type_ = icon_type;
349 } 282 }
350 283
351 void FaviconHandler::OnUpdateFaviconURL( 284 void FaviconHandler::OnUpdateFaviconURL(
352 const GURL& page_url, 285 const GURL& page_url,
353 const std::vector<FaviconURL>& candidates) { 286 const std::vector<FaviconURL>& candidates) {
354 if (page_url != url_) 287 if (page_url != url_)
355 return; 288 return;
356 289
357 std::vector<FaviconURL> pruned_candidates; 290 std::vector<FaviconCandidate> sorted_candidates;
291 const std::vector<int> desired_pixel_sizes =
292 GetDesiredPixelSizes(handler_type_);
358 for (const FaviconURL& candidate : candidates) { 293 for (const FaviconURL& candidate : candidates) {
359 if (!candidate.icon_url.is_empty() && (candidate.icon_type & icon_types_)) 294 if (!candidate.icon_url.is_empty() && (candidate.icon_type & icon_types_)) {
360 pruned_candidates.push_back(candidate); 295 sorted_candidates.push_back(
296 FaviconCandidate::FromFaviconURL(candidate, desired_pixel_sizes));
297 }
361 } 298 }
362 299
363 if (download_largest_icon_) 300 std::stable_sort(sorted_candidates.begin(), sorted_candidates.end(),
364 SortAndPruneImageUrls(&pruned_candidates); 301 &FaviconCandidate::CompareScore);
365 302
366 // Ignore FaviconURL::icon_sizes because FaviconURL::icon_sizes is not stored 303 if (candidates_.size() == sorted_candidates.size() &&
367 // in the history database. 304 std::equal(sorted_candidates.begin(), sorted_candidates.end(),
368 if (image_urls_.size() == pruned_candidates.size() && 305 candidates_.begin(), &FaviconCandidate::Equals)) {
pkotwicz 2017/03/28 20:45:39 Can you use the default struct comparison operator
mastiz 2017/03/29 08:53:01 Done, assuming you meant a custom operator==, sinc
369 std::equal(pruned_candidates.begin(), pruned_candidates.end(),
370 image_urls_.begin(), FaviconURLsEqualIgnoringSizes)) {
371 return; 306 return;
372 } 307 }
373 308
374 download_request_.Cancel(); 309 download_request_.Cancel();
375 image_urls_ = pruned_candidates; 310 candidates_ = std::move(sorted_candidates);
376 current_candidate_index_ = 0u; 311 current_candidate_index_ = 0u;
377 best_favicon_candidate_ = FaviconCandidate(); 312 best_favicon_ = DownloadedFavicon();
378 313
379 // TODO(davemoore) Should clear on empty url. Currently we ignore it. 314 // TODO(davemoore) Should clear on empty url. Currently we ignore it.
380 // This appears to be what FF does as well. 315 // This appears to be what FF does as well.
381 if (current_candidate() && got_favicon_from_history_) 316 if (current_candidate() && got_favicon_from_history_)
382 OnGotInitialHistoryDataAndIconURLCandidates(); 317 OnGotInitialHistoryDataAndIconURLCandidates();
383 } 318 }
384 319
385 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) { 320 // static
386 switch (icon_type) { 321 int FaviconHandler::GetMaximalIconSize(
387 case favicon_base::FAVICON: 322 FaviconDriverObserver::NotificationIconType handler_type) {
388 #if defined(OS_ANDROID) 323 int max_size = 0;
389 return 192; 324 for (int size : GetDesiredPixelSizes(handler_type))
390 #else 325 max_size = std::max(max_size, size);
391 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize; 326 return max_size;
392 #endif
393 case favicon_base::TOUCH_ICON:
394 case favicon_base::TOUCH_PRECOMPOSED_ICON:
395 return kTouchIconSize;
396 case favicon_base::INVALID_ICON:
397 return 0;
398 }
399 NOTREACHED();
400 return 0;
401 } 327 }
402 328
403 void FaviconHandler::OnGotInitialHistoryDataAndIconURLCandidates() { 329 void FaviconHandler::OnGotInitialHistoryDataAndIconURLCandidates() {
404 if (!initial_history_result_expired_or_incomplete_ && 330 if (!initial_history_result_expired_or_incomplete_ &&
405 DoUrlAndIconMatch(*current_candidate(), notification_icon_url_, 331 current_candidate()->icon_url == notification_icon_url_ &&
406 notification_icon_type_)) { 332 current_candidate()->icon_type == notification_icon_type_) {
407 // - The data from history is valid and not expired. 333 // - The data from history is valid and not expired.
408 // - The icon URL of the history data matches one of the page's icon URLs. 334 // - The icon URL of the history data matches one of the page's icon URLs.
409 // - The icon URL of the history data matches the icon URL of the last 335 // - The icon URL of the history data matches the icon URL of the last
410 // OnFaviconAvailable() notification. 336 // OnFaviconAvailable() notification.
411 // We are done. No additional downloads or history requests are needed. 337 // We are done. No additional downloads or history requests are needed.
412 // TODO: Store all of the icon URLs associated with a page in history so 338 // TODO: Store all of the icon URLs associated with a page in history so
413 // that we can check whether the page's icon URLs match the page's icon URLs 339 // that we can check whether the page's icon URLs match the page's icon URLs
414 // at the time that the favicon data was stored to the history database. 340 // at the time that the favicon data was stored to the history database.
415 return; 341 return;
416 } 342 }
(...skipping 15 matching lines...) Expand all
432 DVLOG(1) << "Failed to Download Favicon:" << image_url; 358 DVLOG(1) << "Failed to Download Favicon:" << image_url;
433 if (service_) 359 if (service_)
434 service_->UnableToDownloadFavicon(image_url); 360 service_->UnableToDownloadFavicon(image_url);
435 } 361 }
436 362
437 bool request_next_icon = true; 363 bool request_next_icon = true;
438 if (!bitmaps.empty()) { 364 if (!bitmaps.empty()) {
439 float score = 0.0f; 365 float score = 0.0f;
440 gfx::ImageSkia image_skia; 366 gfx::ImageSkia image_skia;
441 if (download_largest_icon_) { 367 if (download_largest_icon_) {
442 int index = -1; 368 std::vector<size_t> best_indices;
443 // Use the largest bitmap if FaviconURL doesn't have sizes attribute. 369 SelectFaviconFrameIndices(original_bitmap_sizes,
444 if (current_candidate()->icon_sizes.empty()) { 370 GetDesiredPixelSizes(handler_type_),
445 index = GetLargestSizeIndex(original_bitmap_sizes); 371 &best_indices, &score);
446 } else { 372 DCHECK_EQ(1U, best_indices.size());
447 index = GetIndexBySize(original_bitmap_sizes, 373 image_skia =
448 current_candidate()->icon_sizes[0]); 374 gfx::ImageSkia::CreateFrom1xBitmap(bitmaps[best_indices.front()]);
449 // Find largest bitmap if there is no one exactly matched.
450 if (index == -1)
451 index = GetLargestSizeIndex(original_bitmap_sizes);
452 }
453 image_skia = gfx::ImageSkia(gfx::ImageSkiaRep(bitmaps[index], 1));
454 } else { 375 } else {
455 image_skia = CreateFaviconImageSkia(bitmaps, 376 image_skia = CreateFaviconImageSkia(bitmaps,
456 original_bitmap_sizes, 377 original_bitmap_sizes,
457 preferred_icon_size(), 378 preferred_icon_size(),
458 &score); 379 &score);
459 } 380 }
460 381
461 if (!image_skia.isNull()) { 382 if (!image_skia.isNull()) {
462 gfx::Image image(image_skia);
463 // The downloaded icon is still valid when there is no FaviconURL update 383 // The downloaded icon is still valid when there is no FaviconURL update
464 // during the downloading. 384 // during the downloading.
465 request_next_icon = 385 DownloadedFavicon downloaded_favicon;
466 !UpdateFaviconCandidate(image_url, image, score, icon_type); 386 downloaded_favicon.image = gfx::Image(image_skia);
387 downloaded_favicon.candidate.icon_url = image_url;
388 downloaded_favicon.candidate.icon_type = icon_type;
389 downloaded_favicon.candidate.score = score;
390 request_next_icon = !UpdateFaviconCandidate(downloaded_favicon);
467 } 391 }
468 } 392 }
469 393
470 if (request_next_icon && current_candidate_index_ + 1 < image_urls_.size()) { 394 if (request_next_icon && current_candidate_index_ + 1 < candidates_.size()) {
471 // Process the next candidate. 395 // Process the next candidate.
472 ++current_candidate_index_; 396 ++current_candidate_index_;
473 DownloadCurrentCandidateOrAskFaviconService(); 397 DownloadCurrentCandidateOrAskFaviconService();
474 } else { 398 } else {
475 // We have either found the ideal candidate or run out of candidates. 399 // We have either found the ideal candidate or run out of candidates.
476 if (best_favicon_candidate_.icon_type != favicon_base::INVALID_ICON) { 400 if (best_favicon_.candidate.icon_type != favicon_base::INVALID_ICON) {
477 // No more icons to request, set the favicon from the candidate. 401 // No more icons to request, set the favicon from the candidate.
478 SetFavicon(best_favicon_candidate_.image_url, 402 SetFavicon(best_favicon_.candidate.icon_url, best_favicon_.image,
479 best_favicon_candidate_.image, 403 best_favicon_.candidate.icon_type);
480 best_favicon_candidate_.icon_type);
481 } 404 }
482 // Clear download related state. 405 // Clear download related state.
483 current_candidate_index_ = image_urls_.size(); 406 current_candidate_index_ = candidates_.size();
484 best_favicon_candidate_ = FaviconCandidate(); 407 best_favicon_ = DownloadedFavicon();
485 } 408 }
486 } 409 }
487 410
411 const std::vector<GURL> FaviconHandler::GetIconURLs() const {
412 std::vector<GURL> icon_urls;
413 for (const FaviconCandidate& candidate : candidates_)
414 icon_urls.push_back(candidate.icon_url);
415 return icon_urls;
416 }
417
488 bool FaviconHandler::HasPendingTasksForTest() { 418 bool FaviconHandler::HasPendingTasksForTest() {
489 return !download_request_.IsCancelled() || 419 return !download_request_.IsCancelled() ||
490 cancelable_task_tracker_.HasTrackedTasks(); 420 cancelable_task_tracker_.HasTrackedTasks();
491 } 421 }
492 422
493 bool FaviconHandler::ShouldSaveFavicon() { 423 bool FaviconHandler::ShouldSaveFavicon() {
494 if (!delegate_->IsOffTheRecord()) 424 if (!delegate_->IsOffTheRecord())
495 return true; 425 return true;
496 426
497 // Always save favicon if the page is bookmarked. 427 // Always save favicon if the page is bookmarked.
498 return delegate_->IsBookmarked(url_); 428 return delegate_->IsBookmarked(url_);
499 } 429 }
500 430
501 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( 431 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService(
502 const std::vector<favicon_base::FaviconRawBitmapResult>& 432 const std::vector<favicon_base::FaviconRawBitmapResult>&
503 favicon_bitmap_results) { 433 favicon_bitmap_results) {
504 got_favicon_from_history_ = true; 434 got_favicon_from_history_ = true;
505 bool has_valid_result = HasValidResult(favicon_bitmap_results); 435 bool has_valid_result = HasValidResult(favicon_bitmap_results);
506 initial_history_result_expired_or_incomplete_ = 436 initial_history_result_expired_or_incomplete_ =
507 !has_valid_result || 437 !has_valid_result ||
508 HasExpiredOrIncompleteResult(preferred_icon_size(), 438 HasExpiredOrIncompleteResult(preferred_icon_size(),
509 favicon_bitmap_results); 439 favicon_bitmap_results);
510 redownload_icons_ = initial_history_result_expired_or_incomplete_ && 440 redownload_icons_ = initial_history_result_expired_or_incomplete_ &&
511 !favicon_bitmap_results.empty(); 441 !favicon_bitmap_results.empty();
512 442
513 if (has_valid_result && 443 if (has_valid_result && (!current_candidate() ||
514 (!current_candidate() || 444 DoUrlsAndIconsMatch(current_candidate()->icon_url,
515 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { 445 current_candidate()->icon_type,
446 favicon_bitmap_results))) {
516 // The db knows the favicon (although it may be out of date) and the entry 447 // The db knows the favicon (although it may be out of date) and the entry
517 // doesn't have an icon. Set the favicon now, and if the favicon turns out 448 // doesn't have an icon. Set the favicon now, and if the favicon turns out
518 // to be expired (or the wrong url) we'll fetch later on. This way the 449 // to be expired (or the wrong url) we'll fetch later on. This way the
519 // user doesn't see a flash of the default favicon. 450 // user doesn't see a flash of the default favicon.
520 NotifyFaviconUpdated(favicon_bitmap_results); 451 NotifyFaviconUpdated(favicon_bitmap_results);
521 } 452 }
522 453
523 if (current_candidate()) 454 if (current_candidate())
524 OnGotInitialHistoryDataAndIconURLCandidates(); 455 OnGotInitialHistoryDataAndIconURLCandidates();
525 } 456 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 497
567 if (has_valid_result) { 498 if (has_valid_result) {
568 // There is a valid favicon. Notify any observers. It is useful to notify 499 // There is a valid favicon. Notify any observers. It is useful to notify
569 // the observers even if the favicon is expired or incomplete (incorrect 500 // the observers even if the favicon is expired or incomplete (incorrect
570 // size) because temporarily showing the user an expired favicon or 501 // size) because temporarily showing the user an expired favicon or
571 // streched favicon is preferable to showing the user the default favicon. 502 // streched favicon is preferable to showing the user the default favicon.
572 NotifyFaviconUpdated(favicon_bitmap_results); 503 NotifyFaviconUpdated(favicon_bitmap_results);
573 } 504 }
574 505
575 if (!current_candidate() || 506 if (!current_candidate() ||
576 (has_results && 507 (has_results && !DoUrlsAndIconsMatch(current_candidate()->icon_url,
577 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { 508 current_candidate()->icon_type,
509 favicon_bitmap_results))) {
578 // The icon URLs have been updated since the favicon data was requested. 510 // The icon URLs have been updated since the favicon data was requested.
579 return; 511 return;
580 } 512 }
581 513
582 if (has_expired_or_incomplete_result) { 514 if (has_expired_or_incomplete_result) {
583 ScheduleDownload(current_candidate()->icon_url, 515 ScheduleDownload(current_candidate()->icon_url,
584 current_candidate()->icon_type); 516 current_candidate()->icon_type);
585 } 517 }
586 } 518 }
587 519
588 void FaviconHandler::ScheduleDownload(const GURL& image_url, 520 void FaviconHandler::ScheduleDownload(const GURL& image_url,
589 favicon_base::IconType icon_type) { 521 favicon_base::IconType icon_type) {
590 DCHECK(image_url.is_valid()); 522 DCHECK(image_url.is_valid());
591 // Note that CancelableCallback starts cancelled. 523 // Note that CancelableCallback starts cancelled.
592 DCHECK(download_request_.IsCancelled()) << "More than one ongoing download"; 524 DCHECK(download_request_.IsCancelled()) << "More than one ongoing download";
593 if (service_ && service_->WasUnableToDownloadFavicon(image_url)) { 525 if (service_ && service_->WasUnableToDownloadFavicon(image_url)) {
594 DVLOG(1) << "Skip Failed FavIcon: " << image_url; 526 DVLOG(1) << "Skip Failed FavIcon: " << image_url;
595 OnDidDownloadFavicon(icon_type, 0, 0, image_url, std::vector<SkBitmap>(), 527 OnDidDownloadFavicon(icon_type, 0, 0, image_url, std::vector<SkBitmap>(),
596 std::vector<gfx::Size>()); 528 std::vector<gfx::Size>());
597 return; 529 return;
598 } 530 }
599 download_request_.Reset(base::Bind(&FaviconHandler::OnDidDownloadFavicon, 531 download_request_.Reset(base::Bind(&FaviconHandler::OnDidDownloadFavicon,
600 base::Unretained(this), icon_type)); 532 base::Unretained(this), icon_type));
601 // A max bitmap size is specified to avoid receiving huge bitmaps in 533 // A max bitmap size is specified to avoid receiving huge bitmaps in
602 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() 534 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload()
603 // for more details about the max bitmap size. 535 // for more details about the max bitmap size.
604 const int download_id = delegate_->DownloadImage( 536 const int download_id =
605 image_url, GetMaximalIconSize(icon_type), download_request_.callback()); 537 delegate_->DownloadImage(image_url, GetMaximalIconSize(handler_type_),
538 download_request_.callback());
606 DCHECK_NE(download_id, 0); 539 DCHECK_NE(download_id, 0);
607 } 540 }
608 541
609 } // namespace favicon 542 } // namespace favicon
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698