| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/favicon/favicon_handler.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <cmath> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/bind_helpers.h" | |
| 13 #include "base/memory/ref_counted_memory.h" | |
| 14 #include "build/build_config.h" | |
| 15 #include "chrome/browser/favicon/favicon_service.h" | |
| 16 #include "components/favicon/core/browser/favicon_client.h" | |
| 17 #include "components/favicon/core/favicon_driver.h" | |
| 18 #include "components/favicon_base/favicon_util.h" | |
| 19 #include "components/favicon_base/select_favicon_frames.h" | |
| 20 #include "skia/ext/image_operations.h" | |
| 21 #include "ui/gfx/codec/png_codec.h" | |
| 22 #include "ui/gfx/image/image_skia.h" | |
| 23 #include "ui/gfx/image/image_util.h" | |
| 24 | |
| 25 using favicon::FaviconURL; | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 // Size (along each axis) of a touch icon. This currently corresponds to | |
| 30 // the apple touch icon for iPad. | |
| 31 const int kTouchIconSize = 144; | |
| 32 | |
| 33 bool DoUrlAndIconMatch(const FaviconURL& favicon_url, | |
| 34 const GURL& url, | |
| 35 favicon_base::IconType icon_type) { | |
| 36 return favicon_url.icon_url == url && favicon_url.icon_type == icon_type; | |
| 37 } | |
| 38 | |
| 39 // Returns true if all of the icon URLs and icon types in |bitmap_results| are | |
| 40 // identical and if they match the icon URL and icon type in |favicon_url|. | |
| 41 // Returns false if |bitmap_results| is empty. | |
| 42 bool DoUrlsAndIconsMatch( | |
| 43 const FaviconURL& favicon_url, | |
| 44 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { | |
| 45 if (bitmap_results.empty()) | |
| 46 return false; | |
| 47 | |
| 48 const favicon_base::IconType icon_type = favicon_url.icon_type; | |
| 49 | |
| 50 for (const auto& bitmap_result : bitmap_results) { | |
| 51 if (favicon_url.icon_url != bitmap_result.icon_url || | |
| 52 icon_type != bitmap_result.icon_type) { | |
| 53 return false; | |
| 54 } | |
| 55 } | |
| 56 return true; | |
| 57 } | |
| 58 | |
| 59 std::string UrlWithoutFragment(const GURL& gurl) { | |
| 60 GURL::Replacements replacements; | |
| 61 replacements.ClearRef(); | |
| 62 return gurl.ReplaceComponents(replacements).spec(); | |
| 63 } | |
| 64 | |
| 65 bool UrlMatches(const GURL& gurl_a, const GURL& gurl_b) { | |
| 66 return UrlWithoutFragment(gurl_a) == UrlWithoutFragment(gurl_b); | |
| 67 } | |
| 68 | |
| 69 // Return true if |bitmap_result| is expired. | |
| 70 bool IsExpired(const favicon_base::FaviconRawBitmapResult& bitmap_result) { | |
| 71 return bitmap_result.expired; | |
| 72 } | |
| 73 | |
| 74 // Return true if |bitmap_result| is valid. | |
| 75 bool IsValid(const favicon_base::FaviconRawBitmapResult& bitmap_result) { | |
| 76 return bitmap_result.is_valid(); | |
| 77 } | |
| 78 | |
| 79 // Returns true if at least one of the bitmaps in |bitmap_results| is expired or | |
| 80 // if |bitmap_results| is missing favicons for |desired_size_in_dip| and one of | |
| 81 // the scale factors in favicon_base::GetFaviconScales(). | |
| 82 bool HasExpiredOrIncompleteResult( | |
| 83 int desired_size_in_dip, | |
| 84 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { | |
| 85 // Check if at least one of the bitmaps is expired. | |
| 86 std::vector<favicon_base::FaviconRawBitmapResult>::const_iterator it = | |
| 87 std::find_if(bitmap_results.begin(), bitmap_results.end(), IsExpired); | |
| 88 if (it != bitmap_results.end()) | |
| 89 return true; | |
| 90 | |
| 91 // Any favicon size is good if the desired size is 0. | |
| 92 if (desired_size_in_dip == 0) | |
| 93 return false; | |
| 94 | |
| 95 // Check if the favicon for at least one of the scale factors is missing. | |
| 96 // |bitmap_results| should always be complete for data inserted by | |
| 97 // FaviconHandler as the FaviconHandler stores favicons resized to all | |
| 98 // of favicon_base::GetFaviconScales() into the history backend. | |
| 99 // Examples of when |bitmap_results| can be incomplete: | |
| 100 // - Favicons inserted into the history backend by sync. | |
| 101 // - Favicons for imported bookmarks. | |
| 102 std::vector<gfx::Size> favicon_sizes; | |
| 103 for (const auto& bitmap_result : bitmap_results) | |
| 104 favicon_sizes.push_back(bitmap_result.pixel_size); | |
| 105 | |
| 106 std::vector<float> favicon_scales = favicon_base::GetFaviconScales(); | |
| 107 for (float favicon_scale : favicon_scales) { | |
| 108 int edge_size_in_pixel = std::ceil(desired_size_in_dip * favicon_scale); | |
| 109 auto it = std::find(favicon_sizes.begin(), favicon_sizes.end(), | |
| 110 gfx::Size(edge_size_in_pixel, edge_size_in_pixel)); | |
| 111 if (it == favicon_sizes.end()) | |
| 112 return true; | |
| 113 } | |
| 114 return false; | |
| 115 } | |
| 116 | |
| 117 // Returns true if at least one of |bitmap_results| is valid. | |
| 118 bool HasValidResult( | |
| 119 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { | |
| 120 return std::find_if(bitmap_results.begin(), bitmap_results.end(), IsValid) != | |
| 121 bitmap_results.end(); | |
| 122 } | |
| 123 | |
| 124 // Returns the index of the entry with the largest area. | |
| 125 int GetLargestSizeIndex(const std::vector<gfx::Size>& sizes) { | |
| 126 DCHECK(!sizes.empty()); | |
| 127 int ret = 0; | |
| 128 for (size_t i = 1; i < sizes.size(); ++i) { | |
| 129 if (sizes[ret].GetArea() < sizes[i].GetArea()) | |
| 130 ret = i; | |
| 131 } | |
| 132 return ret; | |
| 133 } | |
| 134 | |
| 135 // Return the index of a size which is same as the given |size|, -1 returned if | |
| 136 // there is no such bitmap. | |
| 137 int GetIndexBySize(const std::vector<gfx::Size>& sizes, | |
| 138 const gfx::Size& size) { | |
| 139 DCHECK(!sizes.empty()); | |
| 140 std::vector<gfx::Size>::const_iterator i = | |
| 141 std::find(sizes.begin(), sizes.end(), size); | |
| 142 if (i == sizes.end()) | |
| 143 return -1; | |
| 144 | |
| 145 return static_cast<int>(i - sizes.begin()); | |
| 146 } | |
| 147 | |
| 148 // Compare function used for std::stable_sort to sort as descend. | |
| 149 bool CompareIconSize(const FaviconURL& b1, const FaviconURL& b2) { | |
| 150 int area1 = 0; | |
| 151 if (!b1.icon_sizes.empty()) | |
| 152 area1 = b1.icon_sizes.front().GetArea(); | |
| 153 | |
| 154 int area2 = 0; | |
| 155 if (!b2.icon_sizes.empty()) | |
| 156 area2 = b2.icon_sizes.front().GetArea(); | |
| 157 | |
| 158 return area1 > area2; | |
| 159 } | |
| 160 | |
| 161 } // namespace | |
| 162 | |
| 163 //////////////////////////////////////////////////////////////////////////////// | |
| 164 | |
| 165 FaviconHandler::DownloadRequest::DownloadRequest() | |
| 166 : icon_type(favicon_base::INVALID_ICON) { | |
| 167 } | |
| 168 | |
| 169 FaviconHandler::DownloadRequest::~DownloadRequest() { | |
| 170 } | |
| 171 | |
| 172 FaviconHandler::DownloadRequest::DownloadRequest( | |
| 173 const GURL& url, | |
| 174 const GURL& image_url, | |
| 175 favicon_base::IconType icon_type) | |
| 176 : url(url), image_url(image_url), icon_type(icon_type) { | |
| 177 } | |
| 178 | |
| 179 //////////////////////////////////////////////////////////////////////////////// | |
| 180 | |
| 181 FaviconHandler::FaviconCandidate::FaviconCandidate() | |
| 182 : score(0), icon_type(favicon_base::INVALID_ICON) { | |
| 183 } | |
| 184 | |
| 185 FaviconHandler::FaviconCandidate::~FaviconCandidate() { | |
| 186 } | |
| 187 | |
| 188 FaviconHandler::FaviconCandidate::FaviconCandidate( | |
| 189 const GURL& url, | |
| 190 const GURL& image_url, | |
| 191 const gfx::Image& image, | |
| 192 float score, | |
| 193 favicon_base::IconType icon_type) | |
| 194 : url(url), | |
| 195 image_url(image_url), | |
| 196 image(image), | |
| 197 score(score), | |
| 198 icon_type(icon_type) {} | |
| 199 | |
| 200 //////////////////////////////////////////////////////////////////////////////// | |
| 201 | |
| 202 FaviconHandler::FaviconHandler(FaviconService* service, | |
| 203 FaviconClient* client, | |
| 204 FaviconDriver* driver, | |
| 205 Type icon_type, | |
| 206 bool download_largest_icon) | |
| 207 : got_favicon_from_history_(false), | |
| 208 favicon_expired_or_incomplete_(false), | |
| 209 icon_types_(icon_type == FAVICON | |
| 210 ? favicon_base::FAVICON | |
| 211 : favicon_base::TOUCH_ICON | | |
| 212 favicon_base::TOUCH_PRECOMPOSED_ICON), | |
| 213 download_largest_icon_(download_largest_icon), | |
| 214 service_(service), | |
| 215 client_(client), | |
| 216 driver_(driver) { | |
| 217 DCHECK(driver_); | |
| 218 } | |
| 219 | |
| 220 FaviconHandler::~FaviconHandler() { | |
| 221 } | |
| 222 | |
| 223 void FaviconHandler::FetchFavicon(const GURL& url) { | |
| 224 cancelable_task_tracker_.TryCancelAll(); | |
| 225 | |
| 226 url_ = url; | |
| 227 | |
| 228 favicon_expired_or_incomplete_ = got_favicon_from_history_ = false; | |
| 229 image_urls_.clear(); | |
| 230 | |
| 231 // Request the favicon from the history service. In parallel to this the | |
| 232 // renderer is going to notify us (well WebContents) when the favicon url is | |
| 233 // available. | |
| 234 GetFaviconForURLFromFaviconService( | |
| 235 url_, icon_types_, | |
| 236 base::Bind(&FaviconHandler::OnFaviconDataForInitialURLFromFaviconService, | |
| 237 base::Unretained(this)), | |
| 238 &cancelable_task_tracker_); | |
| 239 } | |
| 240 | |
| 241 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url, | |
| 242 const GURL& image_url, | |
| 243 const gfx::Image& image, | |
| 244 float score, | |
| 245 favicon_base::IconType icon_type) { | |
| 246 bool replace_best_favicon_candidate = false; | |
| 247 bool exact_match = false; | |
| 248 if (download_largest_icon_) { | |
| 249 replace_best_favicon_candidate = | |
| 250 image.Size().GetArea() > | |
| 251 best_favicon_candidate_.image.Size().GetArea(); | |
| 252 | |
| 253 gfx::Size largest = best_favicon_candidate_.image.Size(); | |
| 254 if (replace_best_favicon_candidate) | |
| 255 largest = image.Size(); | |
| 256 | |
| 257 // The size of the downloaded icon may not match the declared size. Stop | |
| 258 // downloading if: | |
| 259 // - current candidate is only candidate. | |
| 260 // - next candidate doesn't have sizes attributes, in this case, the rest | |
| 261 // candidates don't have sizes attribute either, stop downloading now, | |
| 262 // otherwise, all favicon without sizes attribute are downloaded. | |
| 263 // - next candidate has sizes attribute and it is not larger than largest, | |
| 264 // - current candidate is maximal one we want. | |
| 265 const int maximal_size = GetMaximalIconSize(icon_type); | |
| 266 exact_match = image_urls_.size() == 1 || | |
| 267 image_urls_[1].icon_sizes.empty() || | |
| 268 image_urls_[1].icon_sizes[0].GetArea() <= largest.GetArea() || | |
| 269 (image.Size().width() == maximal_size && | |
| 270 image.Size().height() == maximal_size); | |
| 271 } else { | |
| 272 exact_match = score == 1 || preferred_icon_size() == 0; | |
| 273 replace_best_favicon_candidate = | |
| 274 exact_match || | |
| 275 best_favicon_candidate_.icon_type == favicon_base::INVALID_ICON || | |
| 276 score > best_favicon_candidate_.score; | |
| 277 } | |
| 278 if (replace_best_favicon_candidate) { | |
| 279 best_favicon_candidate_ = FaviconCandidate( | |
| 280 url, image_url, image, score, icon_type); | |
| 281 } | |
| 282 return exact_match; | |
| 283 } | |
| 284 | |
| 285 void FaviconHandler::SetFavicon(const GURL& url, | |
| 286 const GURL& icon_url, | |
| 287 const gfx::Image& image, | |
| 288 favicon_base::IconType icon_type) { | |
| 289 if (ShouldSaveFavicon(url)) | |
| 290 SetHistoryFavicons(url, icon_url, icon_type, image); | |
| 291 | |
| 292 if (!UrlMatches(url, url_) || PageChangedSinceFaviconWasRequested()) | |
| 293 return; | |
| 294 | |
| 295 NotifyFaviconAvailable( | |
| 296 icon_url, | |
| 297 image, | |
| 298 icon_type == favicon_base::FAVICON && !download_largest_icon_); | |
| 299 } | |
| 300 | |
| 301 void FaviconHandler::NotifyFaviconAvailable( | |
| 302 const std::vector<favicon_base::FaviconRawBitmapResult>& | |
| 303 favicon_bitmap_results, | |
| 304 bool is_active_favicon) { | |
| 305 gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs( | |
| 306 favicon_bitmap_results, | |
| 307 favicon_base::GetFaviconScales(), | |
| 308 preferred_icon_size()); | |
| 309 // The history service sends back results for a single icon URL, so it does | |
| 310 // not matter which result we get the |icon_url| from. | |
| 311 const GURL icon_url = favicon_bitmap_results.empty() ? | |
| 312 GURL() : favicon_bitmap_results[0].icon_url; | |
| 313 NotifyFaviconAvailable(icon_url, resized_image, is_active_favicon); | |
| 314 } | |
| 315 | |
| 316 void FaviconHandler::NotifyFaviconAvailable(const GURL& icon_url, | |
| 317 const gfx::Image& image, | |
| 318 bool is_active_favicon) { | |
| 319 gfx::Image image_with_adjusted_colorspace = image; | |
| 320 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace); | |
| 321 | |
| 322 driver_->OnFaviconAvailable( | |
| 323 image_with_adjusted_colorspace, icon_url, is_active_favicon); | |
| 324 } | |
| 325 | |
| 326 void FaviconHandler::OnUpdateFaviconURL( | |
| 327 const std::vector<FaviconURL>& candidates) { | |
| 328 image_urls_.clear(); | |
| 329 best_favicon_candidate_ = FaviconCandidate(); | |
| 330 for (const FaviconURL& candidate : candidates) { | |
| 331 if (!candidate.icon_url.is_empty() && (candidate.icon_type & icon_types_)) | |
| 332 image_urls_.push_back(candidate); | |
| 333 } | |
| 334 | |
| 335 if (download_largest_icon_) | |
| 336 SortAndPruneImageUrls(); | |
| 337 | |
| 338 // TODO(davemoore) Should clear on empty url. Currently we ignore it. | |
| 339 // This appears to be what FF does as well. | |
| 340 if (!image_urls_.empty()) | |
| 341 ProcessCurrentUrl(); | |
| 342 } | |
| 343 | |
| 344 void FaviconHandler::ProcessCurrentUrl() { | |
| 345 DCHECK(!image_urls_.empty()); | |
| 346 | |
| 347 // current_candidate() may return NULL if download_largest_icon_ is true and | |
| 348 // all the sizes are larger than the max. | |
| 349 if (PageChangedSinceFaviconWasRequested() || !current_candidate()) | |
| 350 return; | |
| 351 | |
| 352 if (current_candidate()->icon_type == favicon_base::FAVICON && | |
| 353 !download_largest_icon_) { | |
| 354 if (!favicon_expired_or_incomplete_ && | |
| 355 driver_->GetActiveFaviconValidity() && | |
| 356 DoUrlAndIconMatch(*current_candidate(), | |
| 357 driver_->GetActiveFaviconURL(), | |
| 358 favicon_base::FAVICON)) | |
| 359 return; | |
| 360 } else if (!favicon_expired_or_incomplete_ && got_favicon_from_history_ && | |
| 361 HasValidResult(history_results_) && | |
| 362 DoUrlsAndIconsMatch(*current_candidate(), history_results_)) { | |
| 363 return; | |
| 364 } | |
| 365 | |
| 366 if (got_favicon_from_history_) | |
| 367 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), | |
| 368 current_candidate()->icon_url, | |
| 369 current_candidate()->icon_type); | |
| 370 } | |
| 371 | |
| 372 void FaviconHandler::OnDidDownloadFavicon( | |
| 373 int id, | |
| 374 const GURL& image_url, | |
| 375 const std::vector<SkBitmap>& bitmaps, | |
| 376 const std::vector<gfx::Size>& original_bitmap_sizes) { | |
| 377 DownloadRequests::iterator i = download_requests_.find(id); | |
| 378 if (i == download_requests_.end()) { | |
| 379 // Currently WebContents notifies us of ANY downloads so that it is | |
| 380 // possible to get here. | |
| 381 return; | |
| 382 } | |
| 383 | |
| 384 DownloadRequest download_request = i->second; | |
| 385 download_requests_.erase(i); | |
| 386 | |
| 387 if (current_candidate() && | |
| 388 DoUrlAndIconMatch(*current_candidate(), | |
| 389 image_url, | |
| 390 download_request.icon_type)) { | |
| 391 bool request_next_icon = true; | |
| 392 float score = 0.0f; | |
| 393 gfx::ImageSkia image_skia; | |
| 394 if (download_largest_icon_ && !bitmaps.empty()) { | |
| 395 int index = -1; | |
| 396 // Use the largest bitmap if FaviconURL doesn't have sizes attribute. | |
| 397 if (current_candidate()->icon_sizes.empty()) { | |
| 398 index = GetLargestSizeIndex(original_bitmap_sizes); | |
| 399 } else { | |
| 400 index = GetIndexBySize(original_bitmap_sizes, | |
| 401 current_candidate()->icon_sizes[0]); | |
| 402 // Find largest bitmap if there is no one exactly matched. | |
| 403 if (index == -1) | |
| 404 index = GetLargestSizeIndex(original_bitmap_sizes); | |
| 405 } | |
| 406 image_skia = gfx::ImageSkia(gfx::ImageSkiaRep(bitmaps[index], 1)); | |
| 407 } else { | |
| 408 image_skia = CreateFaviconImageSkia(bitmaps, | |
| 409 original_bitmap_sizes, | |
| 410 preferred_icon_size(), | |
| 411 &score); | |
| 412 } | |
| 413 | |
| 414 if (!image_skia.isNull()) { | |
| 415 gfx::Image image(image_skia); | |
| 416 // The downloaded icon is still valid when there is no FaviconURL update | |
| 417 // during the downloading. | |
| 418 if (!bitmaps.empty()) { | |
| 419 request_next_icon = !UpdateFaviconCandidate( | |
| 420 download_request.url, image_url, image, score, | |
| 421 download_request.icon_type); | |
| 422 } | |
| 423 } | |
| 424 if (request_next_icon && !PageChangedSinceFaviconWasRequested() && | |
| 425 image_urls_.size() > 1) { | |
| 426 // Remove the first member of image_urls_ and process the remaining. | |
| 427 image_urls_.erase(image_urls_.begin()); | |
| 428 ProcessCurrentUrl(); | |
| 429 } else if (best_favicon_candidate_.icon_type != | |
| 430 favicon_base::INVALID_ICON) { | |
| 431 // No more icons to request, set the favicon from the candidate. | |
| 432 SetFavicon(best_favicon_candidate_.url, | |
| 433 best_favicon_candidate_.image_url, | |
| 434 best_favicon_candidate_.image, | |
| 435 best_favicon_candidate_.icon_type); | |
| 436 // Reset candidate. | |
| 437 image_urls_.clear(); | |
| 438 download_requests_.clear(); | |
| 439 best_favicon_candidate_ = FaviconCandidate(); | |
| 440 } | |
| 441 } | |
| 442 } | |
| 443 | |
| 444 bool FaviconHandler::PageChangedSinceFaviconWasRequested() { | |
| 445 if (UrlMatches(driver_->GetActiveURL(), url_) && url_.is_valid()) { | |
| 446 return false; | |
| 447 } | |
| 448 // If the URL has changed out from under us (as will happen with redirects) | |
| 449 // return true. | |
| 450 return true; | |
| 451 } | |
| 452 | |
| 453 int FaviconHandler::DownloadFavicon(const GURL& image_url, | |
| 454 int max_bitmap_size) { | |
| 455 if (!image_url.is_valid()) { | |
| 456 NOTREACHED(); | |
| 457 return 0; | |
| 458 } | |
| 459 return driver_->StartDownload(image_url, max_bitmap_size); | |
| 460 } | |
| 461 | |
| 462 void FaviconHandler::UpdateFaviconMappingAndFetch( | |
| 463 const GURL& page_url, | |
| 464 const GURL& icon_url, | |
| 465 favicon_base::IconType icon_type, | |
| 466 const favicon_base::FaviconResultsCallback& callback, | |
| 467 base::CancelableTaskTracker* tracker) { | |
| 468 // TODO(pkotwicz): pass in all of |image_urls_| to | |
| 469 // UpdateFaviconMappingsAndFetch(). | |
| 470 if (service_) { | |
| 471 std::vector<GURL> icon_urls; | |
| 472 icon_urls.push_back(icon_url); | |
| 473 service_->UpdateFaviconMappingsAndFetch(page_url, icon_urls, icon_type, | |
| 474 preferred_icon_size(), callback, | |
| 475 tracker); | |
| 476 } | |
| 477 } | |
| 478 | |
| 479 void FaviconHandler::GetFaviconFromFaviconService( | |
| 480 const GURL& icon_url, | |
| 481 favicon_base::IconType icon_type, | |
| 482 const favicon_base::FaviconResultsCallback& callback, | |
| 483 base::CancelableTaskTracker* tracker) { | |
| 484 if (service_) { | |
| 485 service_->GetFavicon(icon_url, icon_type, preferred_icon_size(), callback, | |
| 486 tracker); | |
| 487 } | |
| 488 } | |
| 489 | |
| 490 void FaviconHandler::GetFaviconForURLFromFaviconService( | |
| 491 const GURL& page_url, | |
| 492 int icon_types, | |
| 493 const favicon_base::FaviconResultsCallback& callback, | |
| 494 base::CancelableTaskTracker* tracker) { | |
| 495 if (service_) { | |
| 496 service_->GetFaviconForPageURL(page_url, icon_types, preferred_icon_size(), | |
| 497 callback, tracker); | |
| 498 } | |
| 499 } | |
| 500 | |
| 501 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, | |
| 502 const GURL& icon_url, | |
| 503 favicon_base::IconType icon_type, | |
| 504 const gfx::Image& image) { | |
| 505 if (service_) { | |
| 506 service_->SetFavicons(page_url, icon_url, icon_type, image); | |
| 507 } | |
| 508 } | |
| 509 | |
| 510 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) { | |
| 511 if (!driver_->IsOffTheRecord()) | |
| 512 return true; | |
| 513 | |
| 514 // Otherwise store the favicon if the page is bookmarked. | |
| 515 return client_->IsBookmarked(url); | |
| 516 } | |
| 517 | |
| 518 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) { | |
| 519 switch (icon_type) { | |
| 520 case favicon_base::FAVICON: | |
| 521 #if defined(OS_ANDROID) | |
| 522 return 192; | |
| 523 #else | |
| 524 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize; | |
| 525 #endif | |
| 526 case favicon_base::TOUCH_ICON: | |
| 527 case favicon_base::TOUCH_PRECOMPOSED_ICON: | |
| 528 return kTouchIconSize; | |
| 529 case favicon_base::INVALID_ICON: | |
| 530 return 0; | |
| 531 } | |
| 532 NOTREACHED(); | |
| 533 return 0; | |
| 534 } | |
| 535 | |
| 536 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( | |
| 537 const std::vector<favicon_base::FaviconRawBitmapResult>& | |
| 538 favicon_bitmap_results) { | |
| 539 if (PageChangedSinceFaviconWasRequested()) | |
| 540 return; | |
| 541 got_favicon_from_history_ = true; | |
| 542 history_results_ = favicon_bitmap_results; | |
| 543 bool has_results = !favicon_bitmap_results.empty(); | |
| 544 favicon_expired_or_incomplete_ = has_results && HasExpiredOrIncompleteResult( | |
| 545 preferred_icon_size(), favicon_bitmap_results); | |
| 546 bool has_valid_result = HasValidResult(favicon_bitmap_results); | |
| 547 | |
| 548 if (has_results && icon_types_ == favicon_base::FAVICON && | |
| 549 !download_largest_icon_ && !driver_->GetActiveFaviconValidity() && | |
| 550 (!current_candidate() || | |
| 551 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { | |
| 552 if (has_valid_result) { | |
| 553 // The db knows the favicon (although it may be out of date) and the entry | |
| 554 // doesn't have an icon. Set the favicon now, and if the favicon turns out | |
| 555 // to be expired (or the wrong url) we'll fetch later on. This way the | |
| 556 // user doesn't see a flash of the default favicon. | |
| 557 NotifyFaviconAvailable(favicon_bitmap_results, true); | |
| 558 } else { | |
| 559 // If |favicon_bitmap_results| does not have any valid results, treat the | |
| 560 // favicon as if it's expired. | |
| 561 // TODO(pkotwicz): Do something better. | |
| 562 favicon_expired_or_incomplete_ = true; | |
| 563 } | |
| 564 } | |
| 565 if (has_results && !favicon_expired_or_incomplete_) { | |
| 566 if (current_candidate() && | |
| 567 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) { | |
| 568 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will | |
| 569 // update the mapping for this url and download the favicon if we don't | |
| 570 // already have it. | |
| 571 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), | |
| 572 current_candidate()->icon_url, | |
| 573 current_candidate()->icon_type); | |
| 574 } | |
| 575 } else if (current_candidate()) { | |
| 576 // We know the official url for the favicon, but either don't have the | |
| 577 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to | |
| 578 // either download or check history again. | |
| 579 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), | |
| 580 current_candidate()->icon_url, | |
| 581 current_candidate()->icon_type); | |
| 582 } | |
| 583 // else we haven't got the icon url. When we get it we'll ask the | |
| 584 // renderer to download the icon. | |
| 585 | |
| 586 if (has_valid_result && | |
| 587 (icon_types_ != favicon_base::FAVICON || download_largest_icon_)) | |
| 588 NotifyFaviconAvailable(favicon_bitmap_results, false); | |
| 589 } | |
| 590 | |
| 591 void FaviconHandler::DownloadFaviconOrAskFaviconService( | |
| 592 const GURL& page_url, | |
| 593 const GURL& icon_url, | |
| 594 favicon_base::IconType icon_type) { | |
| 595 if (favicon_expired_or_incomplete_) { | |
| 596 // We have the mapping, but the favicon is out of date. Download it now. | |
| 597 ScheduleDownload(page_url, icon_url, icon_type); | |
| 598 } else { | |
| 599 // We don't know the favicon, but we may have previously downloaded the | |
| 600 // favicon for another page that shares the same favicon. Ask for the | |
| 601 // favicon given the favicon URL. | |
| 602 if (driver_->IsOffTheRecord()) { | |
| 603 GetFaviconFromFaviconService( | |
| 604 icon_url, icon_type, | |
| 605 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), | |
| 606 &cancelable_task_tracker_); | |
| 607 } else { | |
| 608 // Ask the history service for the icon. This does two things: | |
| 609 // 1. Attempts to fetch the favicon data from the database. | |
| 610 // 2. If the favicon exists in the database, this updates the database to | |
| 611 // include the mapping between the page url and the favicon url. | |
| 612 // This is asynchronous. The history service will call back when done. | |
| 613 UpdateFaviconMappingAndFetch( | |
| 614 page_url, icon_url, icon_type, | |
| 615 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), | |
| 616 &cancelable_task_tracker_); | |
| 617 } | |
| 618 } | |
| 619 } | |
| 620 | |
| 621 void FaviconHandler::OnFaviconData(const std::vector< | |
| 622 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { | |
| 623 if (PageChangedSinceFaviconWasRequested()) | |
| 624 return; | |
| 625 | |
| 626 bool has_results = !favicon_bitmap_results.empty(); | |
| 627 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult( | |
| 628 preferred_icon_size(), favicon_bitmap_results); | |
| 629 bool has_valid_result = HasValidResult(favicon_bitmap_results); | |
| 630 | |
| 631 if (has_results && icon_types_ == favicon_base::FAVICON && | |
| 632 !download_largest_icon_) { | |
| 633 if (has_valid_result) { | |
| 634 // There is a favicon, set it now. If expired we'll download the current | |
| 635 // one again, but at least the user will get some icon instead of the | |
| 636 // default and most likely the current one is fine anyway. | |
| 637 NotifyFaviconAvailable(favicon_bitmap_results, true); | |
| 638 } | |
| 639 if (has_expired_or_incomplete_result) { | |
| 640 // The favicon is out of date. Request the current one. | |
| 641 ScheduleDownload(driver_->GetActiveURL(), | |
| 642 driver_->GetActiveFaviconURL(), | |
| 643 favicon_base::FAVICON); | |
| 644 } | |
| 645 } else if (current_candidate() && | |
| 646 (!has_results || has_expired_or_incomplete_result || | |
| 647 !(DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)))) { | |
| 648 // We don't know the favicon, it is out of date or its type is not same as | |
| 649 // one got from page. Request the current one. | |
| 650 ScheduleDownload(driver_->GetActiveURL(), | |
| 651 current_candidate()->icon_url, | |
| 652 current_candidate()->icon_type); | |
| 653 } | |
| 654 history_results_ = favicon_bitmap_results; | |
| 655 | |
| 656 if (has_valid_result && | |
| 657 (icon_types_ != favicon_base::FAVICON || download_largest_icon_)) { | |
| 658 NotifyFaviconAvailable(favicon_bitmap_results, false); | |
| 659 } | |
| 660 } | |
| 661 | |
| 662 int FaviconHandler::ScheduleDownload(const GURL& url, | |
| 663 const GURL& image_url, | |
| 664 favicon_base::IconType icon_type) { | |
| 665 // A max bitmap size is specified to avoid receiving huge bitmaps in | |
| 666 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() | |
| 667 // for more details about the max bitmap size. | |
| 668 const int download_id = DownloadFavicon(image_url, | |
| 669 GetMaximalIconSize(icon_type)); | |
| 670 if (download_id) { | |
| 671 // Download ids should be unique. | |
| 672 DCHECK(download_requests_.find(download_id) == download_requests_.end()); | |
| 673 download_requests_[download_id] = | |
| 674 DownloadRequest(url, image_url, icon_type); | |
| 675 } | |
| 676 | |
| 677 return download_id; | |
| 678 } | |
| 679 | |
| 680 void FaviconHandler::SortAndPruneImageUrls() { | |
| 681 // Not using const-reference since the loop mutates FaviconURL::icon_sizes. | |
| 682 for (favicon::FaviconURL& image_url : image_urls_) { | |
| 683 if (image_url.icon_sizes.empty()) | |
| 684 continue; | |
| 685 | |
| 686 gfx::Size largest = | |
| 687 image_url.icon_sizes[GetLargestSizeIndex(image_url.icon_sizes)]; | |
| 688 image_url.icon_sizes.clear(); | |
| 689 image_url.icon_sizes.push_back(largest); | |
| 690 } | |
| 691 std::stable_sort(image_urls_.begin(), image_urls_.end(), | |
| 692 CompareIconSize); | |
| 693 } | |
| OLD | NEW |