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

Side by Side Diff: chrome/browser/favicon/favicon_handler.cc

Issue 10911149: Cleanup FaviconHandler (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 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 | Annotate | Revision Log
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 "chrome/browser/favicon/favicon_handler.h" 5 #include "chrome/browser/favicon/favicon_handler.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/memory/ref_counted_memory.h" 13 #include "base/memory/ref_counted_memory.h"
14 #include "chrome/browser/bookmarks/bookmark_model.h" 14 #include "chrome/browser/bookmarks/bookmark_model.h"
15 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 15 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
16 #include "chrome/browser/favicon/favicon_service_factory.h" 16 #include "chrome/browser/favicon/favicon_service_factory.h"
17 #include "chrome/browser/favicon/favicon_util.h" 17 #include "chrome/browser/favicon/favicon_util.h"
18 #include "chrome/browser/history/select_favicon_frames.h"
18 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/common/icon_messages.h" 20 #include "chrome/common/icon_messages.h"
20 #include "content/public/browser/favicon_status.h" 21 #include "content/public/browser/favicon_status.h"
21 #include "content/public/browser/navigation_entry.h" 22 #include "content/public/browser/navigation_entry.h"
22 #include "skia/ext/image_operations.h" 23 #include "skia/ext/image_operations.h"
23 #include "ui/gfx/codec/png_codec.h" 24 #include "ui/gfx/codec/png_codec.h"
24 #include "ui/gfx/image/image.h" 25 #include "ui/gfx/image/image.h"
25 #include "ui/gfx/image/image_skia.h" 26 #include "ui/gfx/image/image_skia.h"
26 #include "ui/gfx/image/image_util.h" 27 #include "ui/gfx/image/image_util.h"
27 28
(...skipping 18 matching lines...) Expand all
46 return history::INVALID_ICON; 47 return history::INVALID_ICON;
47 } 48 }
48 49
49 bool DoUrlAndIconMatch(const FaviconURL& favicon_url, 50 bool DoUrlAndIconMatch(const FaviconURL& favicon_url,
50 const GURL& url, 51 const GURL& url,
51 history::IconType icon_type) { 52 history::IconType icon_type) {
52 return favicon_url.icon_url == url && 53 return favicon_url.icon_url == url &&
53 favicon_url.icon_type == static_cast<FaviconURL::IconType>(icon_type); 54 favicon_url.icon_type == static_cast<FaviconURL::IconType>(icon_type);
54 } 55 }
55 56
57 // Returns true if all of the icon URLs and icon types in |bitmap_results| are
58 // identical and if they match the icon URL and icon type in |favicon_url|.
59 // Returns false if |bitmap_results| is empty.
60 bool DoUrlsAndIconsMatch(
61 const FaviconURL& favicon_url,
62 const std::vector<history::FaviconBitmapResult>& bitmap_results) {
63 if (bitmap_results.empty())
64 return false;
65
66 history::IconType icon_type = ToHistoryIconType(favicon_url.icon_type);
67
68 for (size_t i = 0; i < bitmap_results.size(); ++i) {
69 if (favicon_url.icon_url != bitmap_results[i].icon_url ||
70 icon_type != bitmap_results[i].icon_type) {
71 return false;
72 }
73 }
74 return true;
75 }
76
56 std::string UrlWithoutFragment(const GURL& gurl) { 77 std::string UrlWithoutFragment(const GURL& gurl) {
57 GURL::Replacements replacements; 78 GURL::Replacements replacements;
58 replacements.ClearRef(); 79 replacements.ClearRef();
59 return gurl.ReplaceComponents(replacements).spec(); 80 return gurl.ReplaceComponents(replacements).spec();
60 } 81 }
61 82
62 bool UrlMatches(const GURL& gurl_a, const GURL& gurl_b) { 83 bool UrlMatches(const GURL& gurl_a, const GURL& gurl_b) {
63 return UrlWithoutFragment(gurl_a) == UrlWithoutFragment(gurl_b); 84 return UrlWithoutFragment(gurl_a) == UrlWithoutFragment(gurl_b);
64 } 85 }
65 86
66 // Returns true if at least one of the bitmaps in |favicon_bitmap_results| is 87 // Returns true if at least one of the bitmaps in |favicon_bitmap_results| is
67 // expired. 88 // expired.
68 bool HasExpiredFaviconResult( 89 bool HasExpiredResult(
69 const std::vector<history::FaviconBitmapResult>& favicon_bitmap_results) { 90 const std::vector<history::FaviconBitmapResult>& bitmap_results) {
70 for (size_t i = 0; i < favicon_bitmap_results.size(); ++i) { 91 for (size_t i = 0; i < bitmap_results.size(); ++i) {
71 if (favicon_bitmap_results[i].expired) 92 if (bitmap_results[i].expired)
72 return true; 93 return true;
73 } 94 }
74 return false; 95 return false;
96 }
97
98 // Returns true if at least one of |bitmap_results| is valid.
99 bool HasValidResult(
rjkroege 2012/09/10 15:57:08 You have several very similar loops. Your code cou
pkotwicz 2012/09/10 17:34:38 Done.
100 const std::vector<history::FaviconBitmapResult>& bitmap_results) {
101 for (size_t i = 0; i < bitmap_results.size(); ++i) {
102 if (bitmap_results[i].is_valid())
103 return true;
104 }
105 return false;
75 } 106 }
76 107
77 } // namespace 108 } // namespace
78 109
79 //////////////////////////////////////////////////////////////////////////////// 110 ////////////////////////////////////////////////////////////////////////////////
80 111
81 FaviconHandler::DownloadRequest::DownloadRequest() 112 FaviconHandler::DownloadRequest::DownloadRequest()
82 : icon_type(history::INVALID_ICON) { 113 : icon_type(history::INVALID_ICON) {
83 } 114 }
84 115
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 } 233 }
203 if (update_candidate) { 234 if (update_candidate) {
204 favicon_candidate_ = FaviconCandidate( 235 favicon_candidate_ = FaviconCandidate(
205 url, image_url, image, score, icon_type); 236 url, image_url, image, score, icon_type);
206 } 237 }
207 return exact_match; 238 return exact_match;
208 } 239 }
209 240
210 void FaviconHandler::SetFavicon( 241 void FaviconHandler::SetFavicon(
211 const GURL& url, 242 const GURL& url,
212 const GURL& image_url, 243 const GURL& icon_url,
213 const gfx::Image& image, 244 const gfx::Image& image,
214 history::IconType icon_type) { 245 history::IconType icon_type) {
215 SkBitmap bitmap = *(image.ToSkBitmap()); 246 SkBitmap bitmap = *(image.ToSkBitmap());
216 const gfx::Image& sized_image = (preferred_icon_size() == 0 ||
217 (preferred_icon_size() == bitmap.width() &&
218 preferred_icon_size() == bitmap.height())) ?
219 image : ResizeFaviconIfNeeded(image);
220 247
221 if (GetFaviconService() && ShouldSaveFavicon(url)) { 248 if (GetFaviconService() && ShouldSaveFavicon(url)) {
222 std::vector<unsigned char> image_data; 249 std::vector<unsigned char> image_data;
223 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) 250 if (gfx::PNGEncodedDataFromImage(image, &image_data))
224 SetHistoryFavicon(url, image_url, image_data, icon_type); 251 SetHistoryFavicon(url, icon_url, image_data, icon_type);
225 } 252 }
226 253
227 if (UrlMatches(url, url_) && icon_type == history::FAVICON) { 254 if (UrlMatches(url, url_) && icon_type == history::FAVICON) {
228 NavigationEntry* entry = GetEntry(); 255 NavigationEntry* entry = GetEntry();
229 if (entry) { 256 if (entry) {
230 entry->GetFavicon().url = image_url; 257 entry->GetFavicon().url = icon_url;
231 UpdateFavicon(entry, &sized_image); 258 UpdateFavicon(entry, &image);
232 } 259 }
233 } 260 }
234 } 261 }
235 262
236 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, 263 void FaviconHandler::UpdateFavicon(NavigationEntry* entry,
237 const std::vector<history::FaviconBitmapResult>& favicon_bitmap_results) { 264 const std::vector<history::FaviconBitmapResult>& favicon_bitmap_results) {
238 gfx::Image resized_image = FaviconUtil::SelectFaviconFramesFromPNGs( 265 gfx::Image resized_image = FaviconUtil::SelectFaviconFramesFromPNGs(
239 favicon_bitmap_results, 266 favicon_bitmap_results,
240 ui::GetSupportedScaleFactors(), 267 ui::GetSupportedScaleFactors(),
241 preferred_icon_size()); 268 preferred_icon_size());
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 bitmap_result.icon_type)) { 328 bitmap_result.icon_type)) {
302 return; 329 return;
303 } 330 }
304 } 331 }
305 332
306 if (got_favicon_from_history_) 333 if (got_favicon_from_history_)
307 DownloadFaviconOrAskHistory(entry->GetURL(), current_candidate()->icon_url, 334 DownloadFaviconOrAskHistory(entry->GetURL(), current_candidate()->icon_url,
308 ToHistoryIconType(current_candidate()->icon_type)); 335 ToHistoryIconType(current_candidate()->icon_type));
309 } 336 }
310 337
311 void FaviconHandler::OnDidDownloadFavicon(int id, 338 void FaviconHandler::OnDidDownloadFavicon(
312 const GURL& image_url, 339 int id,
313 bool errored, 340 const GURL& image_url,
314 const gfx::Image& image, 341 bool errored,
315 float score) { 342 int requested_size,
343 const std::vector<SkBitmap>& bitmaps) {
316 DownloadRequests::iterator i = download_requests_.find(id); 344 DownloadRequests::iterator i = download_requests_.find(id);
317 if (i == download_requests_.end()) { 345 if (i == download_requests_.end()) {
318 // Currently WebContents notifies us of ANY downloads so that it is 346 // Currently WebContents notifies us of ANY downloads so that it is
319 // possible to get here. 347 // possible to get here.
320 return; 348 return;
321 } 349 }
322 350
323 if (!i->second.callback.is_null()) { 351 if (!i->second.callback.is_null()) {
324 i->second.callback.Run(id, errored, *image.ToSkBitmap()); 352 // Find bitmap which most closely matches |requested_size| and return it in
353 // callback.
354 std::vector<gfx::Size> sizes;
355 for (size_t j = 0; j < bitmaps.size(); ++j)
356 sizes.push_back(gfx::Size(bitmaps[j].width(), bitmaps[j].height()));
357 std::vector<ui::ScaleFactor> scale_factors;
358 scale_factors.push_back(ui::SCALE_FACTOR_100P);
359 std::vector<size_t> selected_bitmap_indices;
360 SelectFaviconFrameIndices(sizes, scale_factors, requested_size,
361 &selected_bitmap_indices, NULL);
362 DCHECK_EQ(1u, selected_bitmap_indices.size());
363 size_t closest_index = selected_bitmap_indices[0];
364 i->second.callback.Run(id, errored, bitmaps[closest_index]);
325 } else if (current_candidate() && 365 } else if (current_candidate() &&
326 DoUrlAndIconMatch(*current_candidate(), image_url, 366 DoUrlAndIconMatch(*current_candidate(), image_url,
327 i->second.icon_type)) { 367 i->second.icon_type)) {
368 float score = 0.0f;
rjkroege 2012/09/10 15:57:08 we don't ever seem to use this value again except
pkotwicz 2012/09/10 17:34:38 I am unsure which parameter you are referring to.
369 std::vector<ui::ScaleFactor> scale_factors = ui::GetSupportedScaleFactors();
370 gfx::Image image(SelectFaviconFrames(bitmaps, scale_factors, requested_size,
371 &score));
372
328 // The downloaded icon is still valid when there is no FaviconURL update 373 // The downloaded icon is still valid when there is no FaviconURL update
329 // during the downloading. 374 // during the downloading.
330 bool request_next_icon = true; 375 bool request_next_icon = true;
331 if (!errored) { 376 if (!errored) {
332 request_next_icon = !UpdateFaviconCandidate( 377 request_next_icon = !UpdateFaviconCandidate(
333 i->second.url, image_url, image, score, i->second.icon_type); 378 i->second.url, image_url, image, score, i->second.icon_type);
334 } 379 }
335 if (request_next_icon && GetEntry() && image_urls_.size() > 1) { 380 if (request_next_icon && GetEntry() && image_urls_.size() > 1) {
336 // Remove the first member of image_urls_ and process the remaining. 381 // Remove the first member of image_urls_ and process the remaining.
337 image_urls_.pop_front(); 382 image_urls_.pop_front();
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 std::vector<history::FaviconBitmapResult> favicon_bitmap_results, 467 std::vector<history::FaviconBitmapResult> favicon_bitmap_results,
423 history::IconURLSizesMap icon_url_sizes) { 468 history::IconURLSizesMap icon_url_sizes) {
424 NavigationEntry* entry = GetEntry(); 469 NavigationEntry* entry = GetEntry();
425 if (!entry) 470 if (!entry)
426 return; 471 return;
427 472
428 got_favicon_from_history_ = true; 473 got_favicon_from_history_ = true;
429 history_results_ = favicon_bitmap_results; 474 history_results_ = favicon_bitmap_results;
430 475
431 bool has_results = !favicon_bitmap_results.empty(); 476 bool has_results = !favicon_bitmap_results.empty();
432 favicon_expired_ = (has_results && 477 favicon_expired_ = (has_results && HasExpiredResult(favicon_bitmap_results));
433 HasExpiredFaviconResult(favicon_bitmap_results));
434 478
435 history::FaviconBitmapResult bitmap_result; 479 if (has_results && icon_types_ == history::FAVICON &&
436 if (has_results)
437 bitmap_result = favicon_bitmap_results[0];
438
439 if (has_results && bitmap_result.icon_type == history::FAVICON &&
440 !entry->GetFavicon().valid && 480 !entry->GetFavicon().valid &&
441 (!current_candidate() || 481 (!current_candidate() ||
442 DoUrlAndIconMatch(*current_candidate(), 482 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) {
443 bitmap_result.icon_url, bitmap_result.icon_type))) {
444 // The db knows the favicon (although it may be out of date) and the entry 483 // The db knows the favicon (although it may be out of date) and the entry
445 // doesn't have an icon. Set the favicon now, and if the favicon turns out 484 // doesn't have an icon. Set the favicon now, and if the favicon turns out
446 // to be expired (or the wrong url) we'll fetch later on. This way the 485 // to be expired (or the wrong url) we'll fetch later on. This way the
447 // user doesn't see a flash of the default favicon. 486 // user doesn't see a flash of the default favicon.
448 entry->GetFavicon().url = bitmap_result.icon_url; 487
449 if (bitmap_result.is_valid()) 488 // The history service sends back results for a single icon URL, so it does
489 // not matter which result we get the |icon_url| from.
490 entry->GetFavicon().url = favicon_bitmap_results[0].icon_url;
491 if (HasValidResult(favicon_bitmap_results))
450 UpdateFavicon(entry, favicon_bitmap_results); 492 UpdateFavicon(entry, favicon_bitmap_results);
451 entry->GetFavicon().valid = true; 493 entry->GetFavicon().valid = true;
452 } 494 }
453 495
454 if (has_results && !bitmap_result.expired) { 496 if (has_results && !HasExpiredResult(favicon_bitmap_results)) {
455 if (current_candidate() && 497 if (current_candidate() &&
456 !DoUrlAndIconMatch(*current_candidate(), 498 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) {
457 bitmap_result.icon_url, bitmap_result.icon_type)) {
458 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will 499 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will
459 // update the mapping for this url and download the favicon if we don't 500 // update the mapping for this url and download the favicon if we don't
460 // already have it. 501 // already have it.
461 DownloadFaviconOrAskHistory(entry->GetURL(), 502 DownloadFaviconOrAskHistory(entry->GetURL(),
462 current_candidate()->icon_url, 503 current_candidate()->icon_url,
463 static_cast<history::IconType>(current_candidate()->icon_type)); 504 static_cast<history::IconType>(current_candidate()->icon_type));
464 } 505 }
465 } else if (current_candidate()) { 506 } else if (current_candidate()) {
466 // We know the official url for the favicon, by either don't have the 507 // We know the official url for the favicon, by either don't have the
467 // favicon or its expired. Continue on to DownloadFaviconOrAskHistory to 508 // favicon or its expired. Continue on to DownloadFaviconOrAskHistory to
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 545
505 void FaviconHandler::OnFaviconData( 546 void FaviconHandler::OnFaviconData(
506 FaviconService::Handle handle, 547 FaviconService::Handle handle,
507 std::vector<history::FaviconBitmapResult> favicon_bitmap_results, 548 std::vector<history::FaviconBitmapResult> favicon_bitmap_results,
508 history::IconURLSizesMap icon_url_sizes) { 549 history::IconURLSizesMap icon_url_sizes) {
509 NavigationEntry* entry = GetEntry(); 550 NavigationEntry* entry = GetEntry();
510 if (!entry) 551 if (!entry)
511 return; 552 return;
512 553
513 bool has_results = !favicon_bitmap_results.empty(); 554 bool has_results = !favicon_bitmap_results.empty();
514 history::FaviconBitmapResult bitmap_result;
515 if (has_results)
516 bitmap_result = favicon_bitmap_results[0];
517 555
518 // No need to update the favicon url. By the time we get here 556 // No need to update the favicon url. By the time we get here
519 // UpdateFaviconURL will have set the favicon url. 557 // UpdateFaviconURL will have set the favicon url.
520 if (has_results && bitmap_result.icon_type == history::FAVICON) { 558 if (has_results && icon_types_ == history::FAVICON) {
521 if (bitmap_result.is_valid()) { 559 // There is a favicon, set it now. If expired we'll download the current
522 // There is a favicon, set it now. If expired we'll download the current 560 // one again, but at least the user will get some icon instead of the
523 // one again, but at least the user will get some icon instead of the 561 // default and most likely the current one is fine anyway.
524 // default and most likely the current one is fine anyway. 562 if (HasValidResult(favicon_bitmap_results))
525 UpdateFavicon(entry, favicon_bitmap_results); 563 UpdateFavicon(entry, favicon_bitmap_results);
526 } 564 if (HasExpiredResult(favicon_bitmap_results)) {
527 if (HasExpiredFaviconResult(favicon_bitmap_results)) {
528 // The favicon is out of date. Request the current one. 565 // The favicon is out of date. Request the current one.
529 ScheduleDownload(entry->GetURL(), entry->GetFavicon().url, 566 ScheduleDownload(entry->GetURL(), entry->GetFavicon().url,
530 preferred_icon_size(), 567 preferred_icon_size(),
531 history::FAVICON, 568 history::FAVICON,
532 FaviconTabHelper::ImageDownloadCallback()); 569 FaviconTabHelper::ImageDownloadCallback());
533 } 570 }
534 } else if (current_candidate() && 571 } else if (current_candidate() &&
535 (!has_results || HasExpiredFaviconResult(favicon_bitmap_results) || 572 (!has_results || HasExpiredResult(favicon_bitmap_results) ||
536 !(DoUrlAndIconMatch(*current_candidate(), bitmap_result.icon_url, 573 !(DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)))) {
537 bitmap_result.icon_type)))) {
538 // We don't know the favicon, it is out of date or its type is not same as 574 // We don't know the favicon, it is out of date or its type is not same as
539 // one got from page. Request the current one. 575 // one got from page. Request the current one.
540 ScheduleDownload(entry->GetURL(), current_candidate()->icon_url, 576 ScheduleDownload(entry->GetURL(), current_candidate()->icon_url,
541 preferred_icon_size(), 577 preferred_icon_size(),
542 ToHistoryIconType(current_candidate()->icon_type), 578 ToHistoryIconType(current_candidate()->icon_type),
543 FaviconTabHelper::ImageDownloadCallback()); 579 FaviconTabHelper::ImageDownloadCallback());
544 } 580 }
545 history_results_ = favicon_bitmap_results; 581 history_results_ = favicon_bitmap_results;
546 } 582 }
547 583
548 int FaviconHandler::ScheduleDownload( 584 int FaviconHandler::ScheduleDownload(
549 const GURL& url, 585 const GURL& url,
550 const GURL& image_url, 586 const GURL& image_url,
551 int image_size, 587 int image_size,
552 history::IconType icon_type, 588 history::IconType icon_type,
553 const FaviconTabHelper::ImageDownloadCallback& callback) { 589 const FaviconTabHelper::ImageDownloadCallback& callback) {
554 const int download_id = DownloadFavicon(image_url, image_size); 590 const int download_id = DownloadFavicon(image_url, image_size);
555 if (download_id) { 591 if (download_id) {
556 // Download ids should be unique. 592 // Download ids should be unique.
557 DCHECK(download_requests_.find(download_id) == download_requests_.end()); 593 DCHECK(download_requests_.find(download_id) == download_requests_.end());
558 download_requests_[download_id] = 594 download_requests_[download_id] =
559 DownloadRequest(url, image_url, callback, icon_type); 595 DownloadRequest(url, image_url, callback, icon_type);
560 } 596 }
561 597
562 return download_id; 598 return download_id;
563 } 599 }
564
565 gfx::Image FaviconHandler::ResizeFaviconIfNeeded(const gfx::Image& image) {
566 // Get an SkBitmap from the gfx::Image.
567 SkBitmap bitmap = *image.ToSkBitmap();
568 int width = bitmap.width();
569 int height = bitmap.height();
570 if (width > 0 && height > 0) {
571 gfx::CalculateFaviconTargetSize(&width, &height);
572 return gfx::Image(skia::ImageOperations::Resize(
573 bitmap, skia::ImageOperations::RESIZE_LANCZOS3,
574 width, height));
575 }
576
577 return image;
578 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698