OLD | NEW |
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 <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 | 47 |
48 for (const auto& bitmap_result : bitmap_results) { | 48 for (const auto& bitmap_result : bitmap_results) { |
49 if (favicon_url.icon_url != bitmap_result.icon_url || | 49 if (favicon_url.icon_url != bitmap_result.icon_url || |
50 icon_type != bitmap_result.icon_type) { | 50 icon_type != bitmap_result.icon_type) { |
51 return false; | 51 return false; |
52 } | 52 } |
53 } | 53 } |
54 return true; | 54 return true; |
55 } | 55 } |
56 | 56 |
57 std::string UrlWithoutFragment(const GURL& gurl) { | |
58 GURL::Replacements replacements; | |
59 replacements.ClearRef(); | |
60 return gurl.ReplaceComponents(replacements).spec(); | |
61 } | |
62 | |
63 bool UrlMatches(const GURL& gurl_a, const GURL& gurl_b) { | |
64 return UrlWithoutFragment(gurl_a) == UrlWithoutFragment(gurl_b); | |
65 } | |
66 | |
67 // Return true if |bitmap_result| is expired. | 57 // Return true if |bitmap_result| is expired. |
68 bool IsExpired(const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 58 bool IsExpired(const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
69 return bitmap_result.expired; | 59 return bitmap_result.expired; |
70 } | 60 } |
71 | 61 |
72 // Return true if |bitmap_result| is valid. | 62 // Return true if |bitmap_result| is valid. |
73 bool IsValid(const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 63 bool IsValid(const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
74 return bitmap_result.is_valid(); | 64 return bitmap_result.is_valid(); |
75 } | 65 } |
76 | 66 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 //////////////////////////////////////////////////////////////////////////////// | 156 //////////////////////////////////////////////////////////////////////////////// |
167 | 157 |
168 FaviconHandler::DownloadRequest::DownloadRequest() | 158 FaviconHandler::DownloadRequest::DownloadRequest() |
169 : icon_type(favicon_base::INVALID_ICON) { | 159 : icon_type(favicon_base::INVALID_ICON) { |
170 } | 160 } |
171 | 161 |
172 FaviconHandler::DownloadRequest::~DownloadRequest() { | 162 FaviconHandler::DownloadRequest::~DownloadRequest() { |
173 } | 163 } |
174 | 164 |
175 FaviconHandler::DownloadRequest::DownloadRequest( | 165 FaviconHandler::DownloadRequest::DownloadRequest( |
176 const GURL& url, | |
177 const GURL& image_url, | 166 const GURL& image_url, |
178 favicon_base::IconType icon_type) | 167 favicon_base::IconType icon_type) |
179 : url(url), image_url(image_url), icon_type(icon_type) { | 168 : image_url(image_url), icon_type(icon_type) { |
180 } | 169 } |
181 | 170 |
182 //////////////////////////////////////////////////////////////////////////////// | 171 //////////////////////////////////////////////////////////////////////////////// |
183 | 172 |
184 FaviconHandler::FaviconCandidate::FaviconCandidate() | 173 FaviconHandler::FaviconCandidate::FaviconCandidate() |
185 : score(0), icon_type(favicon_base::INVALID_ICON) { | 174 : score(0), icon_type(favicon_base::INVALID_ICON) { |
186 } | 175 } |
187 | 176 |
188 FaviconHandler::FaviconCandidate::~FaviconCandidate() { | 177 FaviconHandler::FaviconCandidate::~FaviconCandidate() { |
189 } | 178 } |
190 | 179 |
191 FaviconHandler::FaviconCandidate::FaviconCandidate( | 180 FaviconHandler::FaviconCandidate::FaviconCandidate( |
192 const GURL& url, | |
193 const GURL& image_url, | 181 const GURL& image_url, |
194 const gfx::Image& image, | 182 const gfx::Image& image, |
195 float score, | 183 float score, |
196 favicon_base::IconType icon_type) | 184 favicon_base::IconType icon_type) |
197 : url(url), | 185 : image_url(image_url), |
198 image_url(image_url), | |
199 image(image), | 186 image(image), |
200 score(score), | 187 score(score), |
201 icon_type(icon_type) {} | 188 icon_type(icon_type) {} |
202 | 189 |
203 //////////////////////////////////////////////////////////////////////////////// | 190 //////////////////////////////////////////////////////////////////////////////// |
204 | 191 |
205 FaviconHandler::FaviconHandler(FaviconService* service, | 192 FaviconHandler::FaviconHandler(FaviconService* service, |
206 FaviconDriver* driver, | 193 FaviconDriver* driver, |
207 Type handler_type, | 194 Type handler_type, |
208 bool download_largest_icon) | 195 bool download_largest_icon) |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 // Request the favicon from the history service. In parallel to this the | 235 // Request the favicon from the history service. In parallel to this the |
249 // renderer is going to notify us (well WebContents) when the favicon url is | 236 // renderer is going to notify us (well WebContents) when the favicon url is |
250 // available. | 237 // available. |
251 GetFaviconForURLFromFaviconService( | 238 GetFaviconForURLFromFaviconService( |
252 url_, icon_types_, | 239 url_, icon_types_, |
253 base::Bind(&FaviconHandler::OnFaviconDataForInitialURLFromFaviconService, | 240 base::Bind(&FaviconHandler::OnFaviconDataForInitialURLFromFaviconService, |
254 base::Unretained(this)), | 241 base::Unretained(this)), |
255 &cancelable_task_tracker_); | 242 &cancelable_task_tracker_); |
256 } | 243 } |
257 | 244 |
258 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url, | 245 bool FaviconHandler::UpdateFaviconCandidate(const GURL& image_url, |
259 const GURL& image_url, | |
260 const gfx::Image& image, | 246 const gfx::Image& image, |
261 float score, | 247 float score, |
262 favicon_base::IconType icon_type) { | 248 favicon_base::IconType icon_type) { |
263 bool replace_best_favicon_candidate = false; | 249 bool replace_best_favicon_candidate = false; |
264 bool exact_match = false; | 250 bool exact_match = false; |
265 if (download_largest_icon_) { | 251 if (download_largest_icon_) { |
266 replace_best_favicon_candidate = | 252 replace_best_favicon_candidate = |
267 image.Size().GetArea() > | 253 image.Size().GetArea() > |
268 best_favicon_candidate_.image.Size().GetArea(); | 254 best_favicon_candidate_.image.Size().GetArea(); |
269 | 255 |
(...skipping 16 matching lines...) Expand all Loading... |
286 (image.Size().width() == maximal_size && | 272 (image.Size().width() == maximal_size && |
287 image.Size().height() == maximal_size); | 273 image.Size().height() == maximal_size); |
288 } else { | 274 } else { |
289 exact_match = score == 1 || preferred_icon_size() == 0; | 275 exact_match = score == 1 || preferred_icon_size() == 0; |
290 replace_best_favicon_candidate = | 276 replace_best_favicon_candidate = |
291 exact_match || | 277 exact_match || |
292 best_favicon_candidate_.icon_type == favicon_base::INVALID_ICON || | 278 best_favicon_candidate_.icon_type == favicon_base::INVALID_ICON || |
293 score > best_favicon_candidate_.score; | 279 score > best_favicon_candidate_.score; |
294 } | 280 } |
295 if (replace_best_favicon_candidate) { | 281 if (replace_best_favicon_candidate) { |
296 best_favicon_candidate_ = FaviconCandidate( | 282 best_favicon_candidate_ = |
297 url, image_url, image, score, icon_type); | 283 FaviconCandidate(image_url, image, score, icon_type); |
298 } | 284 } |
299 return exact_match; | 285 return exact_match; |
300 } | 286 } |
301 | 287 |
302 void FaviconHandler::SetFavicon(const GURL& url, | 288 void FaviconHandler::SetFavicon(const GURL& icon_url, |
303 const GURL& icon_url, | |
304 const gfx::Image& image, | 289 const gfx::Image& image, |
305 favicon_base::IconType icon_type) { | 290 favicon_base::IconType icon_type) { |
306 if (ShouldSaveFavicon(url)) | 291 if (ShouldSaveFavicon()) |
307 SetHistoryFavicons(url, icon_url, icon_type, image); | 292 SetHistoryFavicons(url_, icon_url, icon_type, image); |
308 | 293 |
309 NotifyFaviconAvailable(icon_url, image); | 294 NotifyFaviconAvailable(icon_url, image); |
310 } | 295 } |
311 | 296 |
312 void FaviconHandler::NotifyFaviconAvailable( | 297 void FaviconHandler::NotifyFaviconAvailable( |
313 const std::vector<favicon_base::FaviconRawBitmapResult>& | 298 const std::vector<favicon_base::FaviconRawBitmapResult>& |
314 favicon_bitmap_results) { | 299 favicon_bitmap_results) { |
315 gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs( | 300 gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs( |
316 favicon_bitmap_results, | 301 favicon_bitmap_results, |
317 favicon_base::GetFaviconScales(), | 302 favicon_base::GetFaviconScales(), |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 // This appears to be what FF does as well. | 338 // This appears to be what FF does as well. |
354 if (!image_urls_.empty()) | 339 if (!image_urls_.empty()) |
355 ProcessCurrentUrl(); | 340 ProcessCurrentUrl(); |
356 } | 341 } |
357 | 342 |
358 void FaviconHandler::ProcessCurrentUrl() { | 343 void FaviconHandler::ProcessCurrentUrl() { |
359 DCHECK(!image_urls_.empty()); | 344 DCHECK(!image_urls_.empty()); |
360 | 345 |
361 // current_candidate() may return NULL if download_largest_icon_ is true and | 346 // current_candidate() may return NULL if download_largest_icon_ is true and |
362 // all the sizes are larger than the max. | 347 // all the sizes are larger than the max. |
363 if (PageChangedSinceFaviconWasRequested() || !current_candidate()) | 348 if (!current_candidate()) |
364 return; | 349 return; |
365 | 350 |
366 if (current_candidate()->icon_type == favicon_base::FAVICON && | 351 if (current_candidate()->icon_type == favicon_base::FAVICON && |
367 !download_largest_icon_) { | 352 !download_largest_icon_) { |
368 if (!favicon_expired_or_incomplete_ && | 353 if (!favicon_expired_or_incomplete_ && |
369 driver_->GetActiveFaviconValidity() && | 354 driver_->GetActiveFaviconValidity() && |
370 DoUrlAndIconMatch(*current_candidate(), | 355 DoUrlAndIconMatch(*current_candidate(), |
371 driver_->GetActiveFaviconURL(), | 356 driver_->GetActiveFaviconURL(), |
372 favicon_base::FAVICON)) | 357 favicon_base::FAVICON)) |
373 return; | 358 return; |
374 } else if (!favicon_expired_or_incomplete_ && got_favicon_from_history_ && | 359 } else if (!favicon_expired_or_incomplete_ && got_favicon_from_history_ && |
375 HasValidResult(history_results_) && | 360 HasValidResult(history_results_) && |
376 DoUrlsAndIconsMatch(*current_candidate(), history_results_)) { | 361 DoUrlsAndIconsMatch(*current_candidate(), history_results_)) { |
377 return; | 362 return; |
378 } | 363 } |
379 | 364 |
380 if (got_favicon_from_history_) | 365 if (got_favicon_from_history_) |
381 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), | 366 DownloadFaviconOrAskFaviconService(current_candidate()->icon_url, |
382 current_candidate()->icon_url, | |
383 current_candidate()->icon_type); | 367 current_candidate()->icon_type); |
384 } | 368 } |
385 | 369 |
386 void FaviconHandler::OnDidDownloadFavicon( | 370 void FaviconHandler::OnDidDownloadFavicon( |
387 int id, | 371 int id, |
388 const GURL& image_url, | 372 const GURL& image_url, |
389 const std::vector<SkBitmap>& bitmaps, | 373 const std::vector<SkBitmap>& bitmaps, |
390 const std::vector<gfx::Size>& original_bitmap_sizes) { | 374 const std::vector<gfx::Size>& original_bitmap_sizes) { |
391 DownloadRequests::iterator i = download_requests_.find(id); | 375 DownloadRequests::iterator i = download_requests_.find(id); |
392 if (i == download_requests_.end()) { | 376 if (i == download_requests_.end()) { |
393 // Currently WebContents notifies us of ANY downloads so that it is | 377 // Currently WebContents notifies us of ANY downloads so that it is |
394 // possible to get here. | 378 // possible to get here. |
395 return; | 379 return; |
396 } | 380 } |
397 | 381 |
398 DownloadRequest download_request = i->second; | 382 DownloadRequest download_request = i->second; |
399 download_requests_.erase(i); | 383 download_requests_.erase(i); |
400 | 384 |
401 if (PageChangedSinceFaviconWasRequested() || | 385 if (!current_candidate() || |
402 !current_candidate() || | |
403 !DoUrlAndIconMatch(*current_candidate(), | 386 !DoUrlAndIconMatch(*current_candidate(), |
404 image_url, | 387 image_url, |
405 download_request.icon_type)) { | 388 download_request.icon_type)) { |
406 return; | 389 return; |
407 } | 390 } |
408 | 391 |
409 bool request_next_icon = true; | 392 bool request_next_icon = true; |
410 if (!bitmaps.empty()) { | 393 if (!bitmaps.empty()) { |
411 float score = 0.0f; | 394 float score = 0.0f; |
412 gfx::ImageSkia image_skia; | 395 gfx::ImageSkia image_skia; |
(...skipping 14 matching lines...) Expand all Loading... |
427 image_skia = CreateFaviconImageSkia(bitmaps, | 410 image_skia = CreateFaviconImageSkia(bitmaps, |
428 original_bitmap_sizes, | 411 original_bitmap_sizes, |
429 preferred_icon_size(), | 412 preferred_icon_size(), |
430 &score); | 413 &score); |
431 } | 414 } |
432 | 415 |
433 if (!image_skia.isNull()) { | 416 if (!image_skia.isNull()) { |
434 gfx::Image image(image_skia); | 417 gfx::Image image(image_skia); |
435 // The downloaded icon is still valid when there is no FaviconURL update | 418 // The downloaded icon is still valid when there is no FaviconURL update |
436 // during the downloading. | 419 // during the downloading. |
437 request_next_icon = !UpdateFaviconCandidate( | 420 request_next_icon = !UpdateFaviconCandidate(image_url, image, score, |
438 download_request.url, image_url, image, score, | 421 download_request.icon_type); |
439 download_request.icon_type); | |
440 } | 422 } |
441 } | 423 } |
442 | 424 |
443 if (request_next_icon && image_urls_.size() > 1) { | 425 if (request_next_icon && image_urls_.size() > 1) { |
444 // Remove the first member of image_urls_ and process the remaining. | 426 // Remove the first member of image_urls_ and process the remaining. |
445 image_urls_.erase(image_urls_.begin()); | 427 image_urls_.erase(image_urls_.begin()); |
446 ProcessCurrentUrl(); | 428 ProcessCurrentUrl(); |
447 } else { | 429 } else { |
448 // We have either found the ideal candidate or run out of candidates. | 430 // We have either found the ideal candidate or run out of candidates. |
449 if (best_favicon_candidate_.icon_type != favicon_base::INVALID_ICON) { | 431 if (best_favicon_candidate_.icon_type != favicon_base::INVALID_ICON) { |
450 // No more icons to request, set the favicon from the candidate. | 432 // No more icons to request, set the favicon from the candidate. |
451 SetFavicon(best_favicon_candidate_.url, best_favicon_candidate_.image_url, | 433 SetFavicon(best_favicon_candidate_.image_url, |
452 best_favicon_candidate_.image, | 434 best_favicon_candidate_.image, |
453 best_favicon_candidate_.icon_type); | 435 best_favicon_candidate_.icon_type); |
454 } | 436 } |
455 // Clear download related state. | 437 // Clear download related state. |
456 image_urls_.clear(); | 438 image_urls_.clear(); |
457 download_requests_.clear(); | 439 download_requests_.clear(); |
458 best_favicon_candidate_ = FaviconCandidate(); | 440 best_favicon_candidate_ = FaviconCandidate(); |
459 } | 441 } |
460 } | 442 } |
461 | 443 |
462 bool FaviconHandler::HasPendingTasksForTest() { | 444 bool FaviconHandler::HasPendingTasksForTest() { |
463 return !download_requests_.empty() || | 445 return !download_requests_.empty() || |
464 cancelable_task_tracker_.HasTrackedTasks(); | 446 cancelable_task_tracker_.HasTrackedTasks(); |
465 } | 447 } |
466 | 448 |
467 bool FaviconHandler::PageChangedSinceFaviconWasRequested() { | |
468 if (UrlMatches(driver_->GetActiveURL(), url_) && url_.is_valid()) { | |
469 return false; | |
470 } | |
471 // If the URL has changed out from under us (as will happen with redirects) | |
472 // return true. | |
473 return true; | |
474 } | |
475 | |
476 int FaviconHandler::DownloadFavicon(const GURL& image_url, | 449 int FaviconHandler::DownloadFavicon(const GURL& image_url, |
477 int max_bitmap_size) { | 450 int max_bitmap_size) { |
478 if (!image_url.is_valid()) { | 451 if (!image_url.is_valid()) { |
479 NOTREACHED(); | 452 NOTREACHED(); |
480 return 0; | 453 return 0; |
481 } | 454 } |
482 return driver_->StartDownload(image_url, max_bitmap_size); | 455 return driver_->StartDownload(image_url, max_bitmap_size); |
483 } | 456 } |
484 | 457 |
485 void FaviconHandler::UpdateFaviconMappingAndFetch( | 458 void FaviconHandler::UpdateFaviconMappingAndFetch( |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, | 497 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, |
525 const GURL& icon_url, | 498 const GURL& icon_url, |
526 favicon_base::IconType icon_type, | 499 favicon_base::IconType icon_type, |
527 const gfx::Image& image) { | 500 const gfx::Image& image) { |
528 // TODO(huangs): Get the following to garbage collect if handler_type_ == ALL. | 501 // TODO(huangs): Get the following to garbage collect if handler_type_ == ALL. |
529 if (service_) { | 502 if (service_) { |
530 service_->SetFavicons(page_url, icon_url, icon_type, image); | 503 service_->SetFavicons(page_url, icon_url, icon_type, image); |
531 } | 504 } |
532 } | 505 } |
533 | 506 |
534 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) { | 507 bool FaviconHandler::ShouldSaveFavicon() { |
535 if (!driver_->IsOffTheRecord()) | 508 if (!driver_->IsOffTheRecord()) |
536 return true; | 509 return true; |
537 | 510 |
538 // Always save favicon if the page is bookmarked. | 511 // Always save favicon if the page is bookmarked. |
539 return driver_->IsBookmarked(url); | 512 return driver_->IsBookmarked(url_); |
540 } | 513 } |
541 | 514 |
542 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) { | 515 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) { |
543 switch (icon_type) { | 516 switch (icon_type) { |
544 case favicon_base::FAVICON: | 517 case favicon_base::FAVICON: |
545 #if defined(OS_ANDROID) | 518 #if defined(OS_ANDROID) |
546 return 192; | 519 return 192; |
547 #else | 520 #else |
548 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize; | 521 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize; |
549 #endif | 522 #endif |
550 case favicon_base::TOUCH_ICON: | 523 case favicon_base::TOUCH_ICON: |
551 case favicon_base::TOUCH_PRECOMPOSED_ICON: | 524 case favicon_base::TOUCH_PRECOMPOSED_ICON: |
552 return kTouchIconSize; | 525 return kTouchIconSize; |
553 case favicon_base::INVALID_ICON: | 526 case favicon_base::INVALID_ICON: |
554 return 0; | 527 return 0; |
555 } | 528 } |
556 NOTREACHED(); | 529 NOTREACHED(); |
557 return 0; | 530 return 0; |
558 } | 531 } |
559 | 532 |
560 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( | 533 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( |
561 const std::vector<favicon_base::FaviconRawBitmapResult>& | 534 const std::vector<favicon_base::FaviconRawBitmapResult>& |
562 favicon_bitmap_results) { | 535 favicon_bitmap_results) { |
563 if (PageChangedSinceFaviconWasRequested()) | |
564 return; | |
565 got_favicon_from_history_ = true; | 536 got_favicon_from_history_ = true; |
566 history_results_ = favicon_bitmap_results; | 537 history_results_ = favicon_bitmap_results; |
567 bool has_results = !favicon_bitmap_results.empty(); | 538 bool has_results = !favicon_bitmap_results.empty(); |
568 favicon_expired_or_incomplete_ = HasExpiredOrIncompleteResult( | 539 favicon_expired_or_incomplete_ = HasExpiredOrIncompleteResult( |
569 preferred_icon_size(), favicon_bitmap_results); | 540 preferred_icon_size(), favicon_bitmap_results); |
570 bool has_valid_result = HasValidResult(favicon_bitmap_results); | 541 bool has_valid_result = HasValidResult(favicon_bitmap_results); |
571 | 542 |
572 if (has_results && handler_type_ == FAVICON && | 543 if (has_results && handler_type_ == FAVICON && |
573 !download_largest_icon_ && !driver_->GetActiveFaviconValidity() && | 544 !download_largest_icon_ && !driver_->GetActiveFaviconValidity() && |
574 (!current_candidate() || | 545 (!current_candidate() || |
(...skipping 10 matching lines...) Expand all Loading... |
585 // TODO(pkotwicz): Do something better. | 556 // TODO(pkotwicz): Do something better. |
586 favicon_expired_or_incomplete_ = true; | 557 favicon_expired_or_incomplete_ = true; |
587 } | 558 } |
588 } | 559 } |
589 if (has_results && !favicon_expired_or_incomplete_) { | 560 if (has_results && !favicon_expired_or_incomplete_) { |
590 if (current_candidate() && | 561 if (current_candidate() && |
591 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) { | 562 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) { |
592 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will | 563 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will |
593 // update the mapping for this url and download the favicon if we don't | 564 // update the mapping for this url and download the favicon if we don't |
594 // already have it. | 565 // already have it. |
595 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), | 566 DownloadFaviconOrAskFaviconService(current_candidate()->icon_url, |
596 current_candidate()->icon_url, | |
597 current_candidate()->icon_type); | 567 current_candidate()->icon_type); |
598 } | 568 } |
599 } else if (current_candidate()) { | 569 } else if (current_candidate()) { |
600 // We know the official url for the favicon, but either don't have the | 570 // We know the official url for the favicon, but either don't have the |
601 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to | 571 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to |
602 // either download or check history again. | 572 // either download or check history again. |
603 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), | 573 DownloadFaviconOrAskFaviconService(current_candidate()->icon_url, |
604 current_candidate()->icon_url, | |
605 current_candidate()->icon_type); | 574 current_candidate()->icon_type); |
606 } | 575 } |
607 // else we haven't got the icon url. When we get it we'll ask the | 576 // else we haven't got the icon url. When we get it we'll ask the |
608 // renderer to download the icon. | 577 // renderer to download the icon. |
609 | 578 |
610 if (has_valid_result && (handler_type_ != FAVICON || download_largest_icon_)) | 579 if (has_valid_result && (handler_type_ != FAVICON || download_largest_icon_)) |
611 NotifyFaviconAvailable(favicon_bitmap_results); | 580 NotifyFaviconAvailable(favicon_bitmap_results); |
612 } | 581 } |
613 | 582 |
614 void FaviconHandler::DownloadFaviconOrAskFaviconService( | 583 void FaviconHandler::DownloadFaviconOrAskFaviconService( |
615 const GURL& page_url, | |
616 const GURL& icon_url, | 584 const GURL& icon_url, |
617 favicon_base::IconType icon_type) { | 585 favicon_base::IconType icon_type) { |
618 if (favicon_expired_or_incomplete_) { | 586 if (favicon_expired_or_incomplete_) { |
619 // We have the mapping, but the favicon is out of date. Download it now. | 587 // We have the mapping, but the favicon is out of date. Download it now. |
620 ScheduleDownload(page_url, icon_url, icon_type); | 588 ScheduleDownload(icon_url, icon_type); |
621 } else { | 589 } else { |
622 // We don't know the favicon, but we may have previously downloaded the | 590 // We don't know the favicon, but we may have previously downloaded the |
623 // favicon for another page that shares the same favicon. Ask for the | 591 // favicon for another page that shares the same favicon. Ask for the |
624 // favicon given the favicon URL. | 592 // favicon given the favicon URL. |
625 if (driver_->IsOffTheRecord()) { | 593 if (driver_->IsOffTheRecord()) { |
626 GetFaviconFromFaviconService( | 594 GetFaviconFromFaviconService( |
627 icon_url, icon_type, | 595 icon_url, icon_type, |
628 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), | 596 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), |
629 &cancelable_task_tracker_); | 597 &cancelable_task_tracker_); |
630 } else { | 598 } else { |
631 // Ask the history service for the icon. This does two things: | 599 // Ask the history service for the icon. This does two things: |
632 // 1. Attempts to fetch the favicon data from the database. | 600 // 1. Attempts to fetch the favicon data from the database. |
633 // 2. If the favicon exists in the database, this updates the database to | 601 // 2. If the favicon exists in the database, this updates the database to |
634 // include the mapping between the page url and the favicon url. | 602 // include the mapping between the page url and the favicon url. |
635 // This is asynchronous. The history service will call back when done. | 603 // This is asynchronous. The history service will call back when done. |
636 UpdateFaviconMappingAndFetch( | 604 UpdateFaviconMappingAndFetch( |
637 page_url, icon_url, icon_type, | 605 url_, icon_url, icon_type, |
638 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), | 606 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), |
639 &cancelable_task_tracker_); | 607 &cancelable_task_tracker_); |
640 } | 608 } |
641 } | 609 } |
642 } | 610 } |
643 | 611 |
644 void FaviconHandler::OnFaviconData(const std::vector< | 612 void FaviconHandler::OnFaviconData(const std::vector< |
645 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { | 613 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { |
646 if (PageChangedSinceFaviconWasRequested()) | |
647 return; | |
648 | |
649 bool has_results = !favicon_bitmap_results.empty(); | 614 bool has_results = !favicon_bitmap_results.empty(); |
650 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult( | 615 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult( |
651 preferred_icon_size(), favicon_bitmap_results); | 616 preferred_icon_size(), favicon_bitmap_results); |
652 bool has_valid_result = HasValidResult(favicon_bitmap_results); | 617 bool has_valid_result = HasValidResult(favicon_bitmap_results); |
653 history_results_ = favicon_bitmap_results; | 618 history_results_ = favicon_bitmap_results; |
654 | 619 |
655 if (has_valid_result) { | 620 if (has_valid_result) { |
656 // There is a valid favicon. Notify any observers. It is useful to notify | 621 // There is a valid favicon. Notify any observers. It is useful to notify |
657 // the observers even if the favicon is expired or incomplete (incorrect | 622 // the observers even if the favicon is expired or incomplete (incorrect |
658 // size) because temporarily showing the user an expired favicon or | 623 // size) because temporarily showing the user an expired favicon or |
659 // streched favicon is preferable to showing the user the default favicon. | 624 // streched favicon is preferable to showing the user the default favicon. |
660 NotifyFaviconAvailable(favicon_bitmap_results); | 625 NotifyFaviconAvailable(favicon_bitmap_results); |
661 } | 626 } |
662 | 627 |
663 if (!current_candidate() || | 628 if (!current_candidate() || |
664 (has_results && | 629 (has_results && |
665 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { | 630 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { |
666 // The icon URLs have been updated since the favicon data was requested. | 631 // The icon URLs have been updated since the favicon data was requested. |
667 return; | 632 return; |
668 } | 633 } |
669 | 634 |
670 if (!has_results || has_expired_or_incomplete_result) { | 635 if (!has_results || has_expired_or_incomplete_result) { |
671 ScheduleDownload(driver_->GetActiveURL(), | 636 ScheduleDownload(current_candidate()->icon_url, |
672 current_candidate()->icon_url, | |
673 current_candidate()->icon_type); | 637 current_candidate()->icon_type); |
674 } | 638 } |
675 } | 639 } |
676 | 640 |
677 void FaviconHandler::ScheduleDownload(const GURL& url, | 641 void FaviconHandler::ScheduleDownload(const GURL& image_url, |
678 const GURL& image_url, | 642 favicon_base::IconType icon_type) { |
679 favicon_base::IconType icon_type) { | |
680 // A max bitmap size is specified to avoid receiving huge bitmaps in | 643 // A max bitmap size is specified to avoid receiving huge bitmaps in |
681 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() | 644 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() |
682 // for more details about the max bitmap size. | 645 // for more details about the max bitmap size. |
683 const int download_id = DownloadFavicon(image_url, | 646 const int download_id = DownloadFavicon(image_url, |
684 GetMaximalIconSize(icon_type)); | 647 GetMaximalIconSize(icon_type)); |
685 | 648 |
686 // Download ids should be unique. | 649 // Download ids should be unique. |
687 DCHECK(download_requests_.find(download_id) == download_requests_.end()); | 650 DCHECK(download_requests_.find(download_id) == download_requests_.end()); |
688 download_requests_[download_id] = DownloadRequest(url, image_url, icon_type); | 651 download_requests_[download_id] = DownloadRequest(image_url, icon_type); |
689 | 652 |
690 if (download_id == 0) { | 653 if (download_id == 0) { |
691 // If DownloadFavicon() did not start a download, it returns a download id | 654 // If DownloadFavicon() did not start a download, it returns a download id |
692 // of 0. We still need to call OnDidDownloadFavicon() because the method is | 655 // of 0. We still need to call OnDidDownloadFavicon() because the method is |
693 // responsible for initiating the data request for the next candidate. | 656 // responsible for initiating the data request for the next candidate. |
694 OnDidDownloadFavicon(download_id, image_url, std::vector<SkBitmap>(), | 657 OnDidDownloadFavicon(download_id, image_url, std::vector<SkBitmap>(), |
695 std::vector<gfx::Size>()); | 658 std::vector<gfx::Size>()); |
696 | 659 |
697 } | 660 } |
698 } | 661 } |
699 | 662 |
700 void FaviconHandler::SortAndPruneImageUrls() { | 663 void FaviconHandler::SortAndPruneImageUrls() { |
701 // Not using const-reference since the loop mutates FaviconURL::icon_sizes. | 664 // Not using const-reference since the loop mutates FaviconURL::icon_sizes. |
702 for (FaviconURL& image_url : image_urls_) { | 665 for (FaviconURL& image_url : image_urls_) { |
703 if (image_url.icon_sizes.empty()) | 666 if (image_url.icon_sizes.empty()) |
704 continue; | 667 continue; |
705 | 668 |
706 gfx::Size largest = | 669 gfx::Size largest = |
707 image_url.icon_sizes[GetLargestSizeIndex(image_url.icon_sizes)]; | 670 image_url.icon_sizes[GetLargestSizeIndex(image_url.icon_sizes)]; |
708 image_url.icon_sizes.clear(); | 671 image_url.icon_sizes.clear(); |
709 image_url.icon_sizes.push_back(largest); | 672 image_url.icon_sizes.push_back(largest); |
710 } | 673 } |
711 std::stable_sort(image_urls_.begin(), image_urls_.end(), | 674 std::stable_sort(image_urls_.begin(), image_urls_.end(), |
712 CompareIconSize); | 675 CompareIconSize); |
713 } | 676 } |
714 | 677 |
715 } // namespace favicon | 678 } // namespace favicon |
OLD | NEW |