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(), |
318 preferred_icon_size()); | 303 preferred_icon_size()); |
319 // The history service sends back results for a single icon URL, so it does | 304 // The history service sends back results for a single icon URL, so it does |
320 // not matter which result we get the |icon_url| from. | 305 // not matter which result we get the |icon_url| from. |
321 const GURL icon_url = favicon_bitmap_results.empty() ? | 306 const GURL icon_url = favicon_bitmap_results.empty() ? |
322 GURL() : favicon_bitmap_results[0].icon_url; | 307 GURL() : favicon_bitmap_results[0].icon_url; |
323 NotifyFaviconAvailable(icon_url, resized_image); | 308 NotifyFaviconAvailable(icon_url, resized_image); |
324 } | 309 } |
325 | 310 |
326 void FaviconHandler::NotifyFaviconAvailable(const GURL& icon_url, | 311 void FaviconHandler::NotifyFaviconAvailable(const GURL& icon_url, |
327 const gfx::Image& image) { | 312 const gfx::Image& image) { |
328 gfx::Image image_with_adjusted_colorspace = image; | 313 gfx::Image image_with_adjusted_colorspace = image; |
329 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace); | 314 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace); |
330 | 315 |
331 bool is_active_favicon = | 316 bool is_active_favicon = |
332 (handler_type_ == FAVICON && !download_largest_icon_); | 317 (handler_type_ == FAVICON && !download_largest_icon_); |
333 | 318 |
334 driver_->OnFaviconAvailable( | 319 driver_->OnFaviconAvailable( |
335 image_with_adjusted_colorspace, icon_url, is_active_favicon); | 320 url_, icon_url, image_with_adjusted_colorspace, is_active_favicon); |
336 } | 321 } |
337 | 322 |
338 void FaviconHandler::OnUpdateFaviconURL( | 323 void FaviconHandler::OnUpdateFaviconURL( |
| 324 const GURL& page_url, |
339 const std::vector<FaviconURL>& candidates) { | 325 const std::vector<FaviconURL>& candidates) { |
| 326 if (page_url != url_) |
| 327 return; |
| 328 |
340 download_requests_.clear(); | 329 download_requests_.clear(); |
341 image_urls_.clear(); | 330 image_urls_.clear(); |
342 best_favicon_candidate_ = FaviconCandidate(); | 331 best_favicon_candidate_ = FaviconCandidate(); |
343 | 332 |
344 for (const FaviconURL& candidate : candidates) { | 333 for (const FaviconURL& candidate : candidates) { |
345 if (!candidate.icon_url.is_empty() && (candidate.icon_type & icon_types_)) | 334 if (!candidate.icon_url.is_empty() && (candidate.icon_type & icon_types_)) |
346 image_urls_.push_back(candidate); | 335 image_urls_.push_back(candidate); |
347 } | 336 } |
348 | 337 |
349 if (download_largest_icon_) | 338 if (download_largest_icon_) |
350 SortAndPruneImageUrls(); | 339 SortAndPruneImageUrls(); |
351 | 340 |
352 // TODO(davemoore) Should clear on empty url. Currently we ignore it. | 341 // TODO(davemoore) Should clear on empty url. Currently we ignore it. |
353 // This appears to be what FF does as well. | 342 // This appears to be what FF does as well. |
354 if (!image_urls_.empty()) | 343 if (!image_urls_.empty()) |
355 ProcessCurrentUrl(); | 344 ProcessCurrentUrl(); |
356 } | 345 } |
357 | 346 |
358 void FaviconHandler::ProcessCurrentUrl() { | 347 void FaviconHandler::ProcessCurrentUrl() { |
359 DCHECK(!image_urls_.empty()); | 348 DCHECK(!image_urls_.empty()); |
360 | 349 |
361 // current_candidate() may return NULL if download_largest_icon_ is true and | 350 // current_candidate() may return NULL if download_largest_icon_ is true and |
362 // all the sizes are larger than the max. | 351 // all the sizes are larger than the max. |
363 if (PageChangedSinceFaviconWasRequested() || !current_candidate()) | 352 if (!current_candidate()) |
364 return; | 353 return; |
365 | 354 |
366 if (current_candidate()->icon_type == favicon_base::FAVICON && | 355 if (current_candidate()->icon_type == favicon_base::FAVICON && |
367 !download_largest_icon_) { | 356 !download_largest_icon_) { |
368 if (!favicon_expired_or_incomplete_ && | 357 if (!favicon_expired_or_incomplete_ && |
369 driver_->GetActiveFaviconValidity() && | 358 driver_->GetActiveFaviconValidity() && |
370 DoUrlAndIconMatch(*current_candidate(), | 359 DoUrlAndIconMatch(*current_candidate(), |
371 driver_->GetActiveFaviconURL(), | 360 driver_->GetActiveFaviconURL(), |
372 favicon_base::FAVICON)) | 361 favicon_base::FAVICON)) |
373 return; | 362 return; |
374 } else if (!favicon_expired_or_incomplete_ && got_favicon_from_history_ && | 363 } else if (!favicon_expired_or_incomplete_ && got_favicon_from_history_ && |
375 HasValidResult(history_results_) && | 364 HasValidResult(history_results_) && |
376 DoUrlsAndIconsMatch(*current_candidate(), history_results_)) { | 365 DoUrlsAndIconsMatch(*current_candidate(), history_results_)) { |
377 return; | 366 return; |
378 } | 367 } |
379 | 368 |
380 if (got_favicon_from_history_) | 369 if (got_favicon_from_history_) |
381 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), | 370 DownloadFaviconOrAskFaviconService(current_candidate()->icon_url, |
382 current_candidate()->icon_url, | |
383 current_candidate()->icon_type); | 371 current_candidate()->icon_type); |
384 } | 372 } |
385 | 373 |
386 void FaviconHandler::OnDidDownloadFavicon( | 374 void FaviconHandler::OnDidDownloadFavicon( |
387 int id, | 375 int id, |
388 const GURL& image_url, | 376 const GURL& image_url, |
389 const std::vector<SkBitmap>& bitmaps, | 377 const std::vector<SkBitmap>& bitmaps, |
390 const std::vector<gfx::Size>& original_bitmap_sizes) { | 378 const std::vector<gfx::Size>& original_bitmap_sizes) { |
391 DownloadRequests::iterator i = download_requests_.find(id); | 379 DownloadRequests::iterator i = download_requests_.find(id); |
392 if (i == download_requests_.end()) { | 380 if (i == download_requests_.end()) { |
393 // Currently WebContents notifies us of ANY downloads so that it is | 381 // Currently WebContents notifies us of ANY downloads so that it is |
394 // possible to get here. | 382 // possible to get here. |
395 return; | 383 return; |
396 } | 384 } |
397 | 385 |
398 DownloadRequest download_request = i->second; | 386 DownloadRequest download_request = i->second; |
399 download_requests_.erase(i); | 387 download_requests_.erase(i); |
400 | 388 |
401 if (PageChangedSinceFaviconWasRequested() || | 389 if (!current_candidate() || |
402 !current_candidate() || | |
403 !DoUrlAndIconMatch(*current_candidate(), | 390 !DoUrlAndIconMatch(*current_candidate(), |
404 image_url, | 391 image_url, |
405 download_request.icon_type)) { | 392 download_request.icon_type)) { |
406 return; | 393 return; |
407 } | 394 } |
408 | 395 |
409 bool request_next_icon = true; | 396 bool request_next_icon = true; |
410 if (!bitmaps.empty()) { | 397 if (!bitmaps.empty()) { |
411 float score = 0.0f; | 398 float score = 0.0f; |
412 gfx::ImageSkia image_skia; | 399 gfx::ImageSkia image_skia; |
(...skipping 14 matching lines...) Expand all Loading... |
427 image_skia = CreateFaviconImageSkia(bitmaps, | 414 image_skia = CreateFaviconImageSkia(bitmaps, |
428 original_bitmap_sizes, | 415 original_bitmap_sizes, |
429 preferred_icon_size(), | 416 preferred_icon_size(), |
430 &score); | 417 &score); |
431 } | 418 } |
432 | 419 |
433 if (!image_skia.isNull()) { | 420 if (!image_skia.isNull()) { |
434 gfx::Image image(image_skia); | 421 gfx::Image image(image_skia); |
435 // The downloaded icon is still valid when there is no FaviconURL update | 422 // The downloaded icon is still valid when there is no FaviconURL update |
436 // during the downloading. | 423 // during the downloading. |
437 request_next_icon = !UpdateFaviconCandidate( | 424 request_next_icon = !UpdateFaviconCandidate(image_url, image, score, |
438 download_request.url, image_url, image, score, | 425 download_request.icon_type); |
439 download_request.icon_type); | |
440 } | 426 } |
441 } | 427 } |
442 | 428 |
443 if (request_next_icon && image_urls_.size() > 1) { | 429 if (request_next_icon && image_urls_.size() > 1) { |
444 // Remove the first member of image_urls_ and process the remaining. | 430 // Remove the first member of image_urls_ and process the remaining. |
445 image_urls_.erase(image_urls_.begin()); | 431 image_urls_.erase(image_urls_.begin()); |
446 ProcessCurrentUrl(); | 432 ProcessCurrentUrl(); |
447 } else { | 433 } else { |
448 // We have either found the ideal candidate or run out of candidates. | 434 // We have either found the ideal candidate or run out of candidates. |
449 if (best_favicon_candidate_.icon_type != favicon_base::INVALID_ICON) { | 435 if (best_favicon_candidate_.icon_type != favicon_base::INVALID_ICON) { |
450 // No more icons to request, set the favicon from the candidate. | 436 // No more icons to request, set the favicon from the candidate. |
451 SetFavicon(best_favicon_candidate_.url, best_favicon_candidate_.image_url, | 437 SetFavicon(best_favicon_candidate_.image_url, |
452 best_favicon_candidate_.image, | 438 best_favicon_candidate_.image, |
453 best_favicon_candidate_.icon_type); | 439 best_favicon_candidate_.icon_type); |
454 } | 440 } |
455 // Clear download related state. | 441 // Clear download related state. |
456 image_urls_.clear(); | 442 image_urls_.clear(); |
457 download_requests_.clear(); | 443 download_requests_.clear(); |
458 best_favicon_candidate_ = FaviconCandidate(); | 444 best_favicon_candidate_ = FaviconCandidate(); |
459 } | 445 } |
460 } | 446 } |
461 | 447 |
462 bool FaviconHandler::HasPendingTasksForTest() { | 448 bool FaviconHandler::HasPendingTasksForTest() { |
463 return !download_requests_.empty() || | 449 return !download_requests_.empty() || |
464 cancelable_task_tracker_.HasTrackedTasks(); | 450 cancelable_task_tracker_.HasTrackedTasks(); |
465 } | 451 } |
466 | 452 |
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, | 453 int FaviconHandler::DownloadFavicon(const GURL& image_url, |
477 int max_bitmap_size) { | 454 int max_bitmap_size) { |
478 if (!image_url.is_valid()) { | 455 if (!image_url.is_valid()) { |
479 NOTREACHED(); | 456 NOTREACHED(); |
480 return 0; | 457 return 0; |
481 } | 458 } |
482 return driver_->StartDownload(image_url, max_bitmap_size); | 459 return driver_->StartDownload(image_url, max_bitmap_size); |
483 } | 460 } |
484 | 461 |
485 void FaviconHandler::UpdateFaviconMappingAndFetch( | 462 void FaviconHandler::UpdateFaviconMappingAndFetch( |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, | 501 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, |
525 const GURL& icon_url, | 502 const GURL& icon_url, |
526 favicon_base::IconType icon_type, | 503 favicon_base::IconType icon_type, |
527 const gfx::Image& image) { | 504 const gfx::Image& image) { |
528 // TODO(huangs): Get the following to garbage collect if handler_type_ == ALL. | 505 // TODO(huangs): Get the following to garbage collect if handler_type_ == ALL. |
529 if (service_) { | 506 if (service_) { |
530 service_->SetFavicons(page_url, icon_url, icon_type, image); | 507 service_->SetFavicons(page_url, icon_url, icon_type, image); |
531 } | 508 } |
532 } | 509 } |
533 | 510 |
534 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) { | 511 bool FaviconHandler::ShouldSaveFavicon() { |
535 if (!driver_->IsOffTheRecord()) | 512 if (!driver_->IsOffTheRecord()) |
536 return true; | 513 return true; |
537 | 514 |
538 // Always save favicon if the page is bookmarked. | 515 // Always save favicon if the page is bookmarked. |
539 return driver_->IsBookmarked(url); | 516 return driver_->IsBookmarked(url_); |
540 } | 517 } |
541 | 518 |
542 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) { | 519 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) { |
543 switch (icon_type) { | 520 switch (icon_type) { |
544 case favicon_base::FAVICON: | 521 case favicon_base::FAVICON: |
545 #if defined(OS_ANDROID) | 522 #if defined(OS_ANDROID) |
546 return 192; | 523 return 192; |
547 #else | 524 #else |
548 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize; | 525 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize; |
549 #endif | 526 #endif |
550 case favicon_base::TOUCH_ICON: | 527 case favicon_base::TOUCH_ICON: |
551 case favicon_base::TOUCH_PRECOMPOSED_ICON: | 528 case favicon_base::TOUCH_PRECOMPOSED_ICON: |
552 return kTouchIconSize; | 529 return kTouchIconSize; |
553 case favicon_base::INVALID_ICON: | 530 case favicon_base::INVALID_ICON: |
554 return 0; | 531 return 0; |
555 } | 532 } |
556 NOTREACHED(); | 533 NOTREACHED(); |
557 return 0; | 534 return 0; |
558 } | 535 } |
559 | 536 |
560 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( | 537 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( |
561 const std::vector<favicon_base::FaviconRawBitmapResult>& | 538 const std::vector<favicon_base::FaviconRawBitmapResult>& |
562 favicon_bitmap_results) { | 539 favicon_bitmap_results) { |
563 if (PageChangedSinceFaviconWasRequested()) | |
564 return; | |
565 got_favicon_from_history_ = true; | 540 got_favicon_from_history_ = true; |
566 history_results_ = favicon_bitmap_results; | 541 history_results_ = favicon_bitmap_results; |
567 bool has_results = !favicon_bitmap_results.empty(); | 542 bool has_results = !favicon_bitmap_results.empty(); |
568 favicon_expired_or_incomplete_ = HasExpiredOrIncompleteResult( | 543 favicon_expired_or_incomplete_ = HasExpiredOrIncompleteResult( |
569 preferred_icon_size(), favicon_bitmap_results); | 544 preferred_icon_size(), favicon_bitmap_results); |
570 bool has_valid_result = HasValidResult(favicon_bitmap_results); | 545 bool has_valid_result = HasValidResult(favicon_bitmap_results); |
571 | 546 |
572 if (has_results && handler_type_ == FAVICON && | 547 if (has_results && handler_type_ == FAVICON && |
573 !download_largest_icon_ && !driver_->GetActiveFaviconValidity() && | 548 !download_largest_icon_ && !driver_->GetActiveFaviconValidity() && |
574 (!current_candidate() || | 549 (!current_candidate() || |
(...skipping 10 matching lines...) Expand all Loading... |
585 // TODO(pkotwicz): Do something better. | 560 // TODO(pkotwicz): Do something better. |
586 favicon_expired_or_incomplete_ = true; | 561 favicon_expired_or_incomplete_ = true; |
587 } | 562 } |
588 } | 563 } |
589 if (has_results && !favicon_expired_or_incomplete_) { | 564 if (has_results && !favicon_expired_or_incomplete_) { |
590 if (current_candidate() && | 565 if (current_candidate() && |
591 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) { | 566 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) { |
592 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will | 567 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will |
593 // update the mapping for this url and download the favicon if we don't | 568 // update the mapping for this url and download the favicon if we don't |
594 // already have it. | 569 // already have it. |
595 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), | 570 DownloadFaviconOrAskFaviconService(current_candidate()->icon_url, |
596 current_candidate()->icon_url, | |
597 current_candidate()->icon_type); | 571 current_candidate()->icon_type); |
598 } | 572 } |
599 } else if (current_candidate()) { | 573 } else if (current_candidate()) { |
600 // We know the official url for the favicon, but either don't have the | 574 // 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 | 575 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to |
602 // either download or check history again. | 576 // either download or check history again. |
603 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), | 577 DownloadFaviconOrAskFaviconService(current_candidate()->icon_url, |
604 current_candidate()->icon_url, | |
605 current_candidate()->icon_type); | 578 current_candidate()->icon_type); |
606 } | 579 } |
607 // else we haven't got the icon url. When we get it we'll ask the | 580 // else we haven't got the icon url. When we get it we'll ask the |
608 // renderer to download the icon. | 581 // renderer to download the icon. |
609 | 582 |
610 if (has_valid_result && (handler_type_ != FAVICON || download_largest_icon_)) | 583 if (has_valid_result && (handler_type_ != FAVICON || download_largest_icon_)) |
611 NotifyFaviconAvailable(favicon_bitmap_results); | 584 NotifyFaviconAvailable(favicon_bitmap_results); |
612 } | 585 } |
613 | 586 |
614 void FaviconHandler::DownloadFaviconOrAskFaviconService( | 587 void FaviconHandler::DownloadFaviconOrAskFaviconService( |
615 const GURL& page_url, | |
616 const GURL& icon_url, | 588 const GURL& icon_url, |
617 favicon_base::IconType icon_type) { | 589 favicon_base::IconType icon_type) { |
618 if (favicon_expired_or_incomplete_) { | 590 if (favicon_expired_or_incomplete_) { |
619 // We have the mapping, but the favicon is out of date. Download it now. | 591 // We have the mapping, but the favicon is out of date. Download it now. |
620 ScheduleDownload(page_url, icon_url, icon_type); | 592 ScheduleDownload(icon_url, icon_type); |
621 } else { | 593 } else { |
622 // We don't know the favicon, but we may have previously downloaded the | 594 // 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 | 595 // favicon for another page that shares the same favicon. Ask for the |
624 // favicon given the favicon URL. | 596 // favicon given the favicon URL. |
625 if (driver_->IsOffTheRecord()) { | 597 if (driver_->IsOffTheRecord()) { |
626 GetFaviconFromFaviconService( | 598 GetFaviconFromFaviconService( |
627 icon_url, icon_type, | 599 icon_url, icon_type, |
628 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), | 600 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), |
629 &cancelable_task_tracker_); | 601 &cancelable_task_tracker_); |
630 } else { | 602 } else { |
631 // Ask the history service for the icon. This does two things: | 603 // Ask the history service for the icon. This does two things: |
632 // 1. Attempts to fetch the favicon data from the database. | 604 // 1. Attempts to fetch the favicon data from the database. |
633 // 2. If the favicon exists in the database, this updates the database to | 605 // 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. | 606 // include the mapping between the page url and the favicon url. |
635 // This is asynchronous. The history service will call back when done. | 607 // This is asynchronous. The history service will call back when done. |
636 UpdateFaviconMappingAndFetch( | 608 UpdateFaviconMappingAndFetch( |
637 page_url, icon_url, icon_type, | 609 url_, icon_url, icon_type, |
638 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), | 610 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), |
639 &cancelable_task_tracker_); | 611 &cancelable_task_tracker_); |
640 } | 612 } |
641 } | 613 } |
642 } | 614 } |
643 | 615 |
644 void FaviconHandler::OnFaviconData(const std::vector< | 616 void FaviconHandler::OnFaviconData(const std::vector< |
645 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { | 617 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { |
646 if (PageChangedSinceFaviconWasRequested()) | |
647 return; | |
648 | |
649 bool has_results = !favicon_bitmap_results.empty(); | 618 bool has_results = !favicon_bitmap_results.empty(); |
650 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult( | 619 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult( |
651 preferred_icon_size(), favicon_bitmap_results); | 620 preferred_icon_size(), favicon_bitmap_results); |
652 bool has_valid_result = HasValidResult(favicon_bitmap_results); | 621 bool has_valid_result = HasValidResult(favicon_bitmap_results); |
653 history_results_ = favicon_bitmap_results; | 622 history_results_ = favicon_bitmap_results; |
654 | 623 |
655 if (has_valid_result) { | 624 if (has_valid_result) { |
656 // There is a valid favicon. Notify any observers. It is useful to notify | 625 // 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 | 626 // the observers even if the favicon is expired or incomplete (incorrect |
658 // size) because temporarily showing the user an expired favicon or | 627 // size) because temporarily showing the user an expired favicon or |
659 // streched favicon is preferable to showing the user the default favicon. | 628 // streched favicon is preferable to showing the user the default favicon. |
660 NotifyFaviconAvailable(favicon_bitmap_results); | 629 NotifyFaviconAvailable(favicon_bitmap_results); |
661 } | 630 } |
662 | 631 |
663 if (!current_candidate() || | 632 if (!current_candidate() || |
664 (has_results && | 633 (has_results && |
665 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { | 634 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { |
666 // The icon URLs have been updated since the favicon data was requested. | 635 // The icon URLs have been updated since the favicon data was requested. |
667 return; | 636 return; |
668 } | 637 } |
669 | 638 |
670 if (!has_results || has_expired_or_incomplete_result) { | 639 if (!has_results || has_expired_or_incomplete_result) { |
671 ScheduleDownload(driver_->GetActiveURL(), | 640 ScheduleDownload(current_candidate()->icon_url, |
672 current_candidate()->icon_url, | |
673 current_candidate()->icon_type); | 641 current_candidate()->icon_type); |
674 } | 642 } |
675 } | 643 } |
676 | 644 |
677 void FaviconHandler::ScheduleDownload(const GURL& url, | 645 void FaviconHandler::ScheduleDownload(const GURL& image_url, |
678 const GURL& image_url, | 646 favicon_base::IconType icon_type) { |
679 favicon_base::IconType icon_type) { | |
680 // A max bitmap size is specified to avoid receiving huge bitmaps in | 647 // A max bitmap size is specified to avoid receiving huge bitmaps in |
681 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() | 648 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() |
682 // for more details about the max bitmap size. | 649 // for more details about the max bitmap size. |
683 const int download_id = DownloadFavicon(image_url, | 650 const int download_id = DownloadFavicon(image_url, |
684 GetMaximalIconSize(icon_type)); | 651 GetMaximalIconSize(icon_type)); |
685 | 652 |
686 // Download ids should be unique. | 653 // Download ids should be unique. |
687 DCHECK(download_requests_.find(download_id) == download_requests_.end()); | 654 DCHECK(download_requests_.find(download_id) == download_requests_.end()); |
688 download_requests_[download_id] = DownloadRequest(url, image_url, icon_type); | 655 download_requests_[download_id] = DownloadRequest(image_url, icon_type); |
689 | 656 |
690 if (download_id == 0) { | 657 if (download_id == 0) { |
691 // If DownloadFavicon() did not start a download, it returns a download id | 658 // 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 | 659 // of 0. We still need to call OnDidDownloadFavicon() because the method is |
693 // responsible for initiating the data request for the next candidate. | 660 // responsible for initiating the data request for the next candidate. |
694 OnDidDownloadFavicon(download_id, image_url, std::vector<SkBitmap>(), | 661 OnDidDownloadFavicon(download_id, image_url, std::vector<SkBitmap>(), |
695 std::vector<gfx::Size>()); | 662 std::vector<gfx::Size>()); |
696 | 663 |
697 } | 664 } |
698 } | 665 } |
699 | 666 |
700 void FaviconHandler::SortAndPruneImageUrls() { | 667 void FaviconHandler::SortAndPruneImageUrls() { |
701 // Not using const-reference since the loop mutates FaviconURL::icon_sizes. | 668 // Not using const-reference since the loop mutates FaviconURL::icon_sizes. |
702 for (FaviconURL& image_url : image_urls_) { | 669 for (FaviconURL& image_url : image_urls_) { |
703 if (image_url.icon_sizes.empty()) | 670 if (image_url.icon_sizes.empty()) |
704 continue; | 671 continue; |
705 | 672 |
706 gfx::Size largest = | 673 gfx::Size largest = |
707 image_url.icon_sizes[GetLargestSizeIndex(image_url.icon_sizes)]; | 674 image_url.icon_sizes[GetLargestSizeIndex(image_url.icon_sizes)]; |
708 image_url.icon_sizes.clear(); | 675 image_url.icon_sizes.clear(); |
709 image_url.icon_sizes.push_back(largest); | 676 image_url.icon_sizes.push_back(largest); |
710 } | 677 } |
711 std::stable_sort(image_urls_.begin(), image_urls_.end(), | 678 std::stable_sort(image_urls_.begin(), image_urls_.end(), |
712 CompareIconSize); | 679 CompareIconSize); |
713 } | 680 } |
714 | 681 |
715 } // namespace favicon | 682 } // namespace favicon |
OLD | NEW |