| 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 "components/favicon/core/browser/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 "components/favicon/core/browser/favicon_client.h" | |
| 16 #include "components/favicon/core/browser/favicon_service.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 size_t 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 static_cast<int>(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 handler_type, | |
| 206 bool download_largest_icon) | |
| 207 : got_favicon_from_history_(false), | |
| 208 favicon_expired_or_incomplete_(false), | |
| 209 handler_type_(handler_type), | |
| 210 icon_types_(FaviconHandler::GetIconTypesFromHandlerType(handler_type)), | |
| 211 download_largest_icon_(download_largest_icon), | |
| 212 service_(service), | |
| 213 client_(client), | |
| 214 driver_(driver) { | |
| 215 DCHECK(driver_); | |
| 216 } | |
| 217 | |
| 218 FaviconHandler::~FaviconHandler() { | |
| 219 } | |
| 220 | |
| 221 // static | |
| 222 int FaviconHandler::GetIconTypesFromHandlerType( | |
| 223 FaviconHandler::Type handler_type) { | |
| 224 switch (handler_type) { | |
| 225 case FAVICON: | |
| 226 return favicon_base::FAVICON; | |
| 227 case TOUCH: // Falls through. | |
| 228 case LARGE: | |
| 229 return favicon_base::TOUCH_ICON | favicon_base::TOUCH_PRECOMPOSED_ICON; | |
| 230 default: | |
| 231 NOTREACHED(); | |
| 232 } | |
| 233 return 0; | |
| 234 } | |
| 235 | |
| 236 void FaviconHandler::FetchFavicon(const GURL& url) { | |
| 237 cancelable_task_tracker_.TryCancelAll(); | |
| 238 | |
| 239 url_ = url; | |
| 240 | |
| 241 favicon_expired_or_incomplete_ = got_favicon_from_history_ = false; | |
| 242 image_urls_.clear(); | |
| 243 | |
| 244 // Request the favicon from the history service. In parallel to this the | |
| 245 // renderer is going to notify us (well WebContents) when the favicon url is | |
| 246 // available. | |
| 247 GetFaviconForURLFromFaviconService( | |
| 248 url_, icon_types_, | |
| 249 base::Bind(&FaviconHandler::OnFaviconDataForInitialURLFromFaviconService, | |
| 250 base::Unretained(this)), | |
| 251 &cancelable_task_tracker_); | |
| 252 } | |
| 253 | |
| 254 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url, | |
| 255 const GURL& image_url, | |
| 256 const gfx::Image& image, | |
| 257 float score, | |
| 258 favicon_base::IconType icon_type) { | |
| 259 bool replace_best_favicon_candidate = false; | |
| 260 bool exact_match = false; | |
| 261 if (download_largest_icon_) { | |
| 262 replace_best_favicon_candidate = | |
| 263 image.Size().GetArea() > | |
| 264 best_favicon_candidate_.image.Size().GetArea(); | |
| 265 | |
| 266 gfx::Size largest = best_favicon_candidate_.image.Size(); | |
| 267 if (replace_best_favicon_candidate) | |
| 268 largest = image.Size(); | |
| 269 | |
| 270 // The size of the downloaded icon may not match the declared size. Stop | |
| 271 // downloading if: | |
| 272 // - current candidate is only candidate. | |
| 273 // - next candidate doesn't have sizes attributes, in this case, the rest | |
| 274 // candidates don't have sizes attribute either, stop downloading now, | |
| 275 // otherwise, all favicon without sizes attribute are downloaded. | |
| 276 // - next candidate has sizes attribute and it is not larger than largest, | |
| 277 // - current candidate is maximal one we want. | |
| 278 const int maximal_size = GetMaximalIconSize(icon_type); | |
| 279 exact_match = image_urls_.size() == 1 || | |
| 280 image_urls_[1].icon_sizes.empty() || | |
| 281 image_urls_[1].icon_sizes[0].GetArea() <= largest.GetArea() || | |
| 282 (image.Size().width() == maximal_size && | |
| 283 image.Size().height() == maximal_size); | |
| 284 } else { | |
| 285 exact_match = score == 1 || preferred_icon_size() == 0; | |
| 286 replace_best_favicon_candidate = | |
| 287 exact_match || | |
| 288 best_favicon_candidate_.icon_type == favicon_base::INVALID_ICON || | |
| 289 score > best_favicon_candidate_.score; | |
| 290 } | |
| 291 if (replace_best_favicon_candidate) { | |
| 292 best_favicon_candidate_ = FaviconCandidate( | |
| 293 url, image_url, image, score, icon_type); | |
| 294 } | |
| 295 return exact_match; | |
| 296 } | |
| 297 | |
| 298 void FaviconHandler::SetFavicon(const GURL& url, | |
| 299 const GURL& icon_url, | |
| 300 const gfx::Image& image, | |
| 301 favicon_base::IconType icon_type) { | |
| 302 if (ShouldSaveFavicon(url)) | |
| 303 SetHistoryFavicons(url, icon_url, icon_type, image); | |
| 304 | |
| 305 if (!UrlMatches(url, url_) || PageChangedSinceFaviconWasRequested()) | |
| 306 return; | |
| 307 | |
| 308 NotifyFaviconAvailable( | |
| 309 icon_url, | |
| 310 image, | |
| 311 icon_type == favicon_base::FAVICON && !download_largest_icon_); | |
| 312 } | |
| 313 | |
| 314 void FaviconHandler::NotifyFaviconAvailable( | |
| 315 const std::vector<favicon_base::FaviconRawBitmapResult>& | |
| 316 favicon_bitmap_results, | |
| 317 bool is_active_favicon) { | |
| 318 gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs( | |
| 319 favicon_bitmap_results, | |
| 320 favicon_base::GetFaviconScales(), | |
| 321 preferred_icon_size()); | |
| 322 // The history service sends back results for a single icon URL, so it does | |
| 323 // not matter which result we get the |icon_url| from. | |
| 324 const GURL icon_url = favicon_bitmap_results.empty() ? | |
| 325 GURL() : favicon_bitmap_results[0].icon_url; | |
| 326 NotifyFaviconAvailable(icon_url, resized_image, is_active_favicon); | |
| 327 } | |
| 328 | |
| 329 void FaviconHandler::NotifyFaviconAvailable(const GURL& icon_url, | |
| 330 const gfx::Image& image, | |
| 331 bool is_active_favicon) { | |
| 332 gfx::Image image_with_adjusted_colorspace = image; | |
| 333 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace); | |
| 334 | |
| 335 driver_->OnFaviconAvailable( | |
| 336 image_with_adjusted_colorspace, icon_url, is_active_favicon); | |
| 337 } | |
| 338 | |
| 339 void FaviconHandler::OnUpdateFaviconURL( | |
| 340 const std::vector<FaviconURL>& candidates) { | |
| 341 image_urls_.clear(); | |
| 342 best_favicon_candidate_ = FaviconCandidate(); | |
| 343 for (const FaviconURL& candidate : candidates) { | |
| 344 if (!candidate.icon_url.is_empty() && (candidate.icon_type & icon_types_)) | |
| 345 image_urls_.push_back(candidate); | |
| 346 } | |
| 347 | |
| 348 if (download_largest_icon_) | |
| 349 SortAndPruneImageUrls(); | |
| 350 | |
| 351 // TODO(davemoore) Should clear on empty url. Currently we ignore it. | |
| 352 // This appears to be what FF does as well. | |
| 353 if (!image_urls_.empty()) | |
| 354 ProcessCurrentUrl(); | |
| 355 } | |
| 356 | |
| 357 void FaviconHandler::ProcessCurrentUrl() { | |
| 358 DCHECK(!image_urls_.empty()); | |
| 359 | |
| 360 // current_candidate() may return NULL if download_largest_icon_ is true and | |
| 361 // all the sizes are larger than the max. | |
| 362 if (PageChangedSinceFaviconWasRequested() || !current_candidate()) | |
| 363 return; | |
| 364 | |
| 365 if (current_candidate()->icon_type == favicon_base::FAVICON && | |
| 366 !download_largest_icon_) { | |
| 367 if (!favicon_expired_or_incomplete_ && | |
| 368 driver_->GetActiveFaviconValidity() && | |
| 369 DoUrlAndIconMatch(*current_candidate(), | |
| 370 driver_->GetActiveFaviconURL(), | |
| 371 favicon_base::FAVICON)) | |
| 372 return; | |
| 373 } else if (!favicon_expired_or_incomplete_ && got_favicon_from_history_ && | |
| 374 HasValidResult(history_results_) && | |
| 375 DoUrlsAndIconsMatch(*current_candidate(), history_results_)) { | |
| 376 return; | |
| 377 } | |
| 378 | |
| 379 if (got_favicon_from_history_) | |
| 380 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), | |
| 381 current_candidate()->icon_url, | |
| 382 current_candidate()->icon_type); | |
| 383 } | |
| 384 | |
| 385 void FaviconHandler::OnDidDownloadFavicon( | |
| 386 int id, | |
| 387 const GURL& image_url, | |
| 388 const std::vector<SkBitmap>& bitmaps, | |
| 389 const std::vector<gfx::Size>& original_bitmap_sizes) { | |
| 390 DownloadRequests::iterator i = download_requests_.find(id); | |
| 391 if (i == download_requests_.end()) { | |
| 392 // Currently WebContents notifies us of ANY downloads so that it is | |
| 393 // possible to get here. | |
| 394 return; | |
| 395 } | |
| 396 | |
| 397 DownloadRequest download_request = i->second; | |
| 398 download_requests_.erase(i); | |
| 399 | |
| 400 if (current_candidate() && | |
| 401 DoUrlAndIconMatch(*current_candidate(), | |
| 402 image_url, | |
| 403 download_request.icon_type)) { | |
| 404 bool request_next_icon = true; | |
| 405 float score = 0.0f; | |
| 406 gfx::ImageSkia image_skia; | |
| 407 if (download_largest_icon_ && !bitmaps.empty()) { | |
| 408 int index = -1; | |
| 409 // Use the largest bitmap if FaviconURL doesn't have sizes attribute. | |
| 410 if (current_candidate()->icon_sizes.empty()) { | |
| 411 index = GetLargestSizeIndex(original_bitmap_sizes); | |
| 412 } else { | |
| 413 index = GetIndexBySize(original_bitmap_sizes, | |
| 414 current_candidate()->icon_sizes[0]); | |
| 415 // Find largest bitmap if there is no one exactly matched. | |
| 416 if (index == -1) | |
| 417 index = GetLargestSizeIndex(original_bitmap_sizes); | |
| 418 } | |
| 419 image_skia = gfx::ImageSkia(gfx::ImageSkiaRep(bitmaps[index], 1)); | |
| 420 } else { | |
| 421 image_skia = CreateFaviconImageSkia(bitmaps, | |
| 422 original_bitmap_sizes, | |
| 423 preferred_icon_size(), | |
| 424 &score); | |
| 425 } | |
| 426 | |
| 427 if (!image_skia.isNull()) { | |
| 428 gfx::Image image(image_skia); | |
| 429 // The downloaded icon is still valid when there is no FaviconURL update | |
| 430 // during the downloading. | |
| 431 if (!bitmaps.empty()) { | |
| 432 request_next_icon = !UpdateFaviconCandidate( | |
| 433 download_request.url, image_url, image, score, | |
| 434 download_request.icon_type); | |
| 435 } | |
| 436 } | |
| 437 if (request_next_icon && !PageChangedSinceFaviconWasRequested() && | |
| 438 image_urls_.size() > 1) { | |
| 439 // Remove the first member of image_urls_ and process the remaining. | |
| 440 image_urls_.erase(image_urls_.begin()); | |
| 441 ProcessCurrentUrl(); | |
| 442 } else if (best_favicon_candidate_.icon_type != | |
| 443 favicon_base::INVALID_ICON) { | |
| 444 // No more icons to request, set the favicon from the candidate. | |
| 445 SetFavicon(best_favicon_candidate_.url, | |
| 446 best_favicon_candidate_.image_url, | |
| 447 best_favicon_candidate_.image, | |
| 448 best_favicon_candidate_.icon_type); | |
| 449 // Reset candidate. | |
| 450 image_urls_.clear(); | |
| 451 download_requests_.clear(); | |
| 452 best_favicon_candidate_ = FaviconCandidate(); | |
| 453 } | |
| 454 } | |
| 455 } | |
| 456 | |
| 457 bool FaviconHandler::PageChangedSinceFaviconWasRequested() { | |
| 458 if (UrlMatches(driver_->GetActiveURL(), url_) && url_.is_valid()) { | |
| 459 return false; | |
| 460 } | |
| 461 // If the URL has changed out from under us (as will happen with redirects) | |
| 462 // return true. | |
| 463 return true; | |
| 464 } | |
| 465 | |
| 466 int FaviconHandler::DownloadFavicon(const GURL& image_url, | |
| 467 int max_bitmap_size) { | |
| 468 if (!image_url.is_valid()) { | |
| 469 NOTREACHED(); | |
| 470 return 0; | |
| 471 } | |
| 472 return driver_->StartDownload(image_url, max_bitmap_size); | |
| 473 } | |
| 474 | |
| 475 void FaviconHandler::UpdateFaviconMappingAndFetch( | |
| 476 const GURL& page_url, | |
| 477 const GURL& icon_url, | |
| 478 favicon_base::IconType icon_type, | |
| 479 const favicon_base::FaviconResultsCallback& callback, | |
| 480 base::CancelableTaskTracker* tracker) { | |
| 481 // TODO(pkotwicz): pass in all of |image_urls_| to | |
| 482 // UpdateFaviconMappingsAndFetch(). | |
| 483 if (service_) { | |
| 484 std::vector<GURL> icon_urls; | |
| 485 icon_urls.push_back(icon_url); | |
| 486 service_->UpdateFaviconMappingsAndFetch(page_url, icon_urls, icon_type, | |
| 487 preferred_icon_size(), callback, | |
| 488 tracker); | |
| 489 } | |
| 490 } | |
| 491 | |
| 492 void FaviconHandler::GetFaviconFromFaviconService( | |
| 493 const GURL& icon_url, | |
| 494 favicon_base::IconType icon_type, | |
| 495 const favicon_base::FaviconResultsCallback& callback, | |
| 496 base::CancelableTaskTracker* tracker) { | |
| 497 if (service_) { | |
| 498 service_->GetFavicon(icon_url, icon_type, preferred_icon_size(), callback, | |
| 499 tracker); | |
| 500 } | |
| 501 } | |
| 502 | |
| 503 void FaviconHandler::GetFaviconForURLFromFaviconService( | |
| 504 const GURL& page_url, | |
| 505 int icon_types, | |
| 506 const favicon_base::FaviconResultsCallback& callback, | |
| 507 base::CancelableTaskTracker* tracker) { | |
| 508 if (service_) { | |
| 509 service_->GetFaviconForPageURL(page_url, icon_types, preferred_icon_size(), | |
| 510 callback, tracker); | |
| 511 } | |
| 512 } | |
| 513 | |
| 514 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, | |
| 515 const GURL& icon_url, | |
| 516 favicon_base::IconType icon_type, | |
| 517 const gfx::Image& image) { | |
| 518 // TODO(huangs): Get the following to garbage collect if handler_type_ == ALL. | |
| 519 if (service_) { | |
| 520 service_->SetFavicons(page_url, icon_url, icon_type, image); | |
| 521 } | |
| 522 } | |
| 523 | |
| 524 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) { | |
| 525 if (!driver_->IsOffTheRecord()) | |
| 526 return true; | |
| 527 | |
| 528 // Otherwise store the favicon if the page is bookmarked. | |
| 529 return client_->IsBookmarked(url); | |
| 530 } | |
| 531 | |
| 532 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) { | |
| 533 switch (icon_type) { | |
| 534 case favicon_base::FAVICON: | |
| 535 #if defined(OS_ANDROID) | |
| 536 return 192; | |
| 537 #else | |
| 538 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize; | |
| 539 #endif | |
| 540 case favicon_base::TOUCH_ICON: | |
| 541 case favicon_base::TOUCH_PRECOMPOSED_ICON: | |
| 542 return kTouchIconSize; | |
| 543 case favicon_base::INVALID_ICON: | |
| 544 return 0; | |
| 545 } | |
| 546 NOTREACHED(); | |
| 547 return 0; | |
| 548 } | |
| 549 | |
| 550 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( | |
| 551 const std::vector<favicon_base::FaviconRawBitmapResult>& | |
| 552 favicon_bitmap_results) { | |
| 553 if (PageChangedSinceFaviconWasRequested()) | |
| 554 return; | |
| 555 got_favicon_from_history_ = true; | |
| 556 history_results_ = favicon_bitmap_results; | |
| 557 bool has_results = !favicon_bitmap_results.empty(); | |
| 558 favicon_expired_or_incomplete_ = has_results && HasExpiredOrIncompleteResult( | |
| 559 preferred_icon_size(), favicon_bitmap_results); | |
| 560 bool has_valid_result = HasValidResult(favicon_bitmap_results); | |
| 561 | |
| 562 if (has_results && handler_type_ == FAVICON && | |
| 563 !download_largest_icon_ && !driver_->GetActiveFaviconValidity() && | |
| 564 (!current_candidate() || | |
| 565 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { | |
| 566 if (has_valid_result) { | |
| 567 // The db knows the favicon (although it may be out of date) and the entry | |
| 568 // doesn't have an icon. Set the favicon now, and if the favicon turns out | |
| 569 // to be expired (or the wrong url) we'll fetch later on. This way the | |
| 570 // user doesn't see a flash of the default favicon. | |
| 571 NotifyFaviconAvailable(favicon_bitmap_results, true); | |
| 572 } else { | |
| 573 // If |favicon_bitmap_results| does not have any valid results, treat the | |
| 574 // favicon as if it's expired. | |
| 575 // TODO(pkotwicz): Do something better. | |
| 576 favicon_expired_or_incomplete_ = true; | |
| 577 } | |
| 578 } | |
| 579 if (has_results && !favicon_expired_or_incomplete_) { | |
| 580 if (current_candidate() && | |
| 581 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) { | |
| 582 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will | |
| 583 // update the mapping for this url and download the favicon if we don't | |
| 584 // already have it. | |
| 585 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), | |
| 586 current_candidate()->icon_url, | |
| 587 current_candidate()->icon_type); | |
| 588 } | |
| 589 } else if (current_candidate()) { | |
| 590 // We know the official url for the favicon, but either don't have the | |
| 591 // favicon or it's expired. Continue on to DownloadFaviconOrAskHistory to | |
| 592 // either download or check history again. | |
| 593 DownloadFaviconOrAskFaviconService(driver_->GetActiveURL(), | |
| 594 current_candidate()->icon_url, | |
| 595 current_candidate()->icon_type); | |
| 596 } | |
| 597 // else we haven't got the icon url. When we get it we'll ask the | |
| 598 // renderer to download the icon. | |
| 599 | |
| 600 if (has_valid_result && (handler_type_ != FAVICON || download_largest_icon_)) | |
| 601 NotifyFaviconAvailable(favicon_bitmap_results, false); | |
| 602 } | |
| 603 | |
| 604 void FaviconHandler::DownloadFaviconOrAskFaviconService( | |
| 605 const GURL& page_url, | |
| 606 const GURL& icon_url, | |
| 607 favicon_base::IconType icon_type) { | |
| 608 if (favicon_expired_or_incomplete_) { | |
| 609 // We have the mapping, but the favicon is out of date. Download it now. | |
| 610 ScheduleDownload(page_url, icon_url, icon_type); | |
| 611 } else { | |
| 612 // We don't know the favicon, but we may have previously downloaded the | |
| 613 // favicon for another page that shares the same favicon. Ask for the | |
| 614 // favicon given the favicon URL. | |
| 615 if (driver_->IsOffTheRecord()) { | |
| 616 GetFaviconFromFaviconService( | |
| 617 icon_url, icon_type, | |
| 618 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), | |
| 619 &cancelable_task_tracker_); | |
| 620 } else { | |
| 621 // Ask the history service for the icon. This does two things: | |
| 622 // 1. Attempts to fetch the favicon data from the database. | |
| 623 // 2. If the favicon exists in the database, this updates the database to | |
| 624 // include the mapping between the page url and the favicon url. | |
| 625 // This is asynchronous. The history service will call back when done. | |
| 626 UpdateFaviconMappingAndFetch( | |
| 627 page_url, icon_url, icon_type, | |
| 628 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), | |
| 629 &cancelable_task_tracker_); | |
| 630 } | |
| 631 } | |
| 632 } | |
| 633 | |
| 634 void FaviconHandler::OnFaviconData(const std::vector< | |
| 635 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) { | |
| 636 if (PageChangedSinceFaviconWasRequested()) | |
| 637 return; | |
| 638 | |
| 639 bool has_results = !favicon_bitmap_results.empty(); | |
| 640 bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult( | |
| 641 preferred_icon_size(), favicon_bitmap_results); | |
| 642 bool has_valid_result = HasValidResult(favicon_bitmap_results); | |
| 643 | |
| 644 if (has_results && handler_type_ == FAVICON && !download_largest_icon_) { | |
| 645 if (has_valid_result) { | |
| 646 // There is a favicon, set it now. If expired we'll download the current | |
| 647 // one again, but at least the user will get some icon instead of the | |
| 648 // default and most likely the current one is fine anyway. | |
| 649 NotifyFaviconAvailable(favicon_bitmap_results, true); | |
| 650 } | |
| 651 if (has_expired_or_incomplete_result) { | |
| 652 // The favicon is out of date. Request the current one. | |
| 653 ScheduleDownload(driver_->GetActiveURL(), | |
| 654 driver_->GetActiveFaviconURL(), | |
| 655 favicon_base::FAVICON); | |
| 656 } | |
| 657 } else if (current_candidate() && | |
| 658 (!has_results || has_expired_or_incomplete_result || | |
| 659 !(DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)))) { | |
| 660 // We don't know the favicon, it is out of date or its type is not same as | |
| 661 // one got from page. Request the current one. | |
| 662 ScheduleDownload(driver_->GetActiveURL(), | |
| 663 current_candidate()->icon_url, | |
| 664 current_candidate()->icon_type); | |
| 665 } | |
| 666 history_results_ = favicon_bitmap_results; | |
| 667 | |
| 668 if (has_valid_result && | |
| 669 (handler_type_ != FAVICON || download_largest_icon_)) { | |
| 670 NotifyFaviconAvailable(favicon_bitmap_results, false); | |
| 671 } | |
| 672 } | |
| 673 | |
| 674 int FaviconHandler::ScheduleDownload(const GURL& url, | |
| 675 const GURL& image_url, | |
| 676 favicon_base::IconType icon_type) { | |
| 677 // A max bitmap size is specified to avoid receiving huge bitmaps in | |
| 678 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() | |
| 679 // for more details about the max bitmap size. | |
| 680 const int download_id = DownloadFavicon(image_url, | |
| 681 GetMaximalIconSize(icon_type)); | |
| 682 if (download_id) { | |
| 683 // Download ids should be unique. | |
| 684 DCHECK(download_requests_.find(download_id) == download_requests_.end()); | |
| 685 download_requests_[download_id] = | |
| 686 DownloadRequest(url, image_url, icon_type); | |
| 687 } | |
| 688 | |
| 689 return download_id; | |
| 690 } | |
| 691 | |
| 692 void FaviconHandler::SortAndPruneImageUrls() { | |
| 693 // Not using const-reference since the loop mutates FaviconURL::icon_sizes. | |
| 694 for (favicon::FaviconURL& image_url : image_urls_) { | |
| 695 if (image_url.icon_sizes.empty()) | |
| 696 continue; | |
| 697 | |
| 698 gfx::Size largest = | |
| 699 image_url.icon_sizes[GetLargestSizeIndex(image_url.icon_sizes)]; | |
| 700 image_url.icon_sizes.clear(); | |
| 701 image_url.icon_sizes.push_back(largest); | |
| 702 } | |
| 703 std::stable_sort(image_urls_.begin(), image_urls_.end(), | |
| 704 CompareIconSize); | |
| 705 } | |
| OLD | NEW |