Chromium Code Reviews| 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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
| 14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "components/favicon/core/favicon_service.h" | 15 #include "components/favicon/core/favicon_service.h" |
| 16 #include "components/favicon_base/favicon_util.h" | 16 #include "components/favicon_base/favicon_util.h" |
| 17 #include "components/favicon_base/select_favicon_frames.h" | 17 #include "components/favicon_base/select_favicon_frames.h" |
| 18 #include "skia/ext/image_operations.h" | 18 #include "skia/ext/image_operations.h" |
| 19 #include "ui/gfx/codec/png_codec.h" | 19 #include "ui/gfx/codec/png_codec.h" |
| 20 #include "ui/gfx/image/image_skia.h" | 20 #include "ui/gfx/image/image_skia.h" |
| 21 #include "ui/gfx/image/image_util.h" | 21 #include "ui/gfx/image/image_util.h" |
| 22 | 22 |
| 23 namespace favicon { | 23 namespace favicon { |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 const int kNonTouchLargestIconSize = 192; | |
| 26 // Size (along each axis) of a touch icon. This currently corresponds to | 27 // Size (along each axis) of a touch icon. This currently corresponds to |
| 27 // the apple touch icon for iPad. | 28 // the apple touch icon for iPad. |
| 28 const int kTouchIconSize = 144; | 29 const int kTouchIconSize = 144; |
| 29 | 30 |
| 30 bool DoUrlAndIconMatch(const FaviconURL& favicon_url, | |
| 31 const GURL& url, | |
| 32 favicon_base::IconType icon_type) { | |
| 33 return favicon_url.icon_url == url && favicon_url.icon_type == icon_type; | |
| 34 } | |
| 35 | |
| 36 // Returns true if all of the icon URLs and icon types in |bitmap_results| are | 31 // Returns true if all of the icon URLs and icon types in |bitmap_results| are |
| 37 // identical and if they match the icon URL and icon type in |favicon_url|. | 32 // identical and if they match |icon_url| and |icon_type|. Returns false if |
| 38 // Returns false if |bitmap_results| is empty. | 33 // |bitmap_results| is empty. |
| 39 bool DoUrlsAndIconsMatch( | 34 bool DoUrlsAndIconsMatch( |
| 40 const FaviconURL& favicon_url, | 35 const GURL& icon_url, |
| 36 favicon_base::IconType icon_type, | |
| 41 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { | 37 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { |
| 42 if (bitmap_results.empty()) | 38 if (bitmap_results.empty()) |
| 43 return false; | 39 return false; |
| 44 | 40 |
| 45 const favicon_base::IconType icon_type = favicon_url.icon_type; | |
| 46 | |
| 47 for (const auto& bitmap_result : bitmap_results) { | 41 for (const auto& bitmap_result : bitmap_results) { |
| 48 if (favicon_url.icon_url != bitmap_result.icon_url || | 42 if (icon_url != bitmap_result.icon_url || |
| 49 icon_type != bitmap_result.icon_type) { | 43 icon_type != bitmap_result.icon_type) { |
| 50 return false; | 44 return false; |
| 51 } | 45 } |
| 52 } | 46 } |
| 53 return true; | 47 return true; |
| 54 } | 48 } |
| 55 | 49 |
| 56 // Return true if |bitmap_result| is expired. | 50 // Return true if |bitmap_result| is expired. |
| 57 bool IsExpired(const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 51 bool IsExpired(const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| 58 return bitmap_result.expired; | 52 return bitmap_result.expired; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 return false; | 100 return false; |
| 107 } | 101 } |
| 108 | 102 |
| 109 // Returns true if at least one of |bitmap_results| is valid. | 103 // Returns true if at least one of |bitmap_results| is valid. |
| 110 bool HasValidResult( | 104 bool HasValidResult( |
| 111 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { | 105 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { |
| 112 return std::find_if(bitmap_results.begin(), bitmap_results.end(), IsValid) != | 106 return std::find_if(bitmap_results.begin(), bitmap_results.end(), IsValid) != |
| 113 bitmap_results.end(); | 107 bitmap_results.end(); |
| 114 } | 108 } |
| 115 | 109 |
| 116 // Returns the index of the entry with the largest area. | 110 std::vector<int> GetDesiredPixelSizes( |
| 117 int GetLargestSizeIndex(const std::vector<gfx::Size>& sizes) { | 111 FaviconDriverObserver::NotificationIconType handler_type) { |
| 118 DCHECK(!sizes.empty()); | 112 switch (handler_type) { |
| 119 size_t ret = 0; | 113 case FaviconDriverObserver::NON_TOUCH_16_DIP: { |
| 120 for (size_t i = 1; i < sizes.size(); ++i) { | 114 std::vector<int> pixel_sizes; |
| 121 if (sizes[ret].GetArea() < sizes[i].GetArea()) | 115 for (float scale_factor : favicon_base::GetFaviconScales()) |
| 122 ret = i; | 116 pixel_sizes.push_back(scale_factor * gfx::kFaviconSize); |
|
pkotwicz
2017/03/27 00:49:06
Nit: Round up the way that GetPixelSizesForFavicon
mastiz
2017/03/27 10:09:36
Done.
| |
| 117 return pixel_sizes; | |
| 118 } | |
| 119 case FaviconDriverObserver::NON_TOUCH_LARGEST: | |
| 120 return std::vector<int>(1U, kNonTouchLargestIconSize); | |
| 121 case FaviconDriverObserver::TOUCH_LARGEST: | |
| 122 return std::vector<int>(1U, kTouchIconSize); | |
| 123 } | 123 } |
| 124 return static_cast<int>(ret); | 124 NOTREACHED(); |
| 125 } | |
| 126 | |
| 127 // Return the index of a size which is same as the given |size|, -1 returned if | |
| 128 // there is no such bitmap. | |
| 129 int GetIndexBySize(const std::vector<gfx::Size>& sizes, | |
| 130 const gfx::Size& size) { | |
| 131 DCHECK(!sizes.empty()); | |
| 132 std::vector<gfx::Size>::const_iterator i = | |
| 133 std::find(sizes.begin(), sizes.end(), size); | |
| 134 if (i == sizes.end()) | |
| 135 return -1; | |
| 136 | |
| 137 return static_cast<int>(i - sizes.begin()); | |
| 138 } | |
| 139 | |
| 140 // Compare function used for std::stable_sort to sort as descend. | |
| 141 bool CompareIconSize(const FaviconURL& b1, const FaviconURL& b2) { | |
| 142 int area1 = 0; | |
| 143 if (!b1.icon_sizes.empty()) | |
| 144 area1 = b1.icon_sizes.front().GetArea(); | |
| 145 | |
| 146 int area2 = 0; | |
| 147 if (!b2.icon_sizes.empty()) | |
| 148 area2 = b2.icon_sizes.front().GetArea(); | |
| 149 | |
| 150 return area1 > area2; | |
| 151 } | |
| 152 | |
| 153 // Sorts the entries in |image_urls| by icon size in descending order. | |
| 154 // Discards all but the largest size for each FaviconURL. | |
| 155 void SortAndPruneImageUrls(std::vector<FaviconURL>* image_urls) { | |
| 156 // Not using const-reference since the loop mutates FaviconURL::icon_sizes. | |
| 157 for (FaviconURL& image_url : *image_urls) { | |
| 158 if (image_url.icon_sizes.empty()) | |
| 159 continue; | |
| 160 | |
| 161 gfx::Size largest = | |
| 162 image_url.icon_sizes[GetLargestSizeIndex(image_url.icon_sizes)]; | |
| 163 image_url.icon_sizes.clear(); | |
| 164 image_url.icon_sizes.push_back(largest); | |
| 165 } | |
| 166 std::stable_sort(image_urls->begin(), image_urls->end(), CompareIconSize); | |
| 167 } | |
| 168 | |
| 169 // Checks whether two FaviconURLs are equal ignoring the icon sizes. | |
| 170 bool FaviconURLsEqualIgnoringSizes(const FaviconURL& u1, const FaviconURL& u2) { | |
| 171 return u1.icon_type == u2.icon_type && u1.icon_url == u2.icon_url; | |
| 172 } | 125 } |
| 173 | 126 |
| 174 } // namespace | 127 } // namespace |
| 175 | 128 |
| 176 //////////////////////////////////////////////////////////////////////////////// | 129 //////////////////////////////////////////////////////////////////////////////// |
| 177 | 130 |
| 178 FaviconHandler::FaviconCandidate::FaviconCandidate() | 131 // static |
| 179 : score(0), icon_type(favicon_base::INVALID_ICON) { | 132 FaviconHandler::FaviconCandidate |
| 133 FaviconHandler::FaviconCandidate::FromFaviconURL( | |
| 134 const favicon::FaviconURL& favicon_url, | |
| 135 const std::vector<int>& desired_pixel_sizes) { | |
| 136 FaviconCandidate candidate; | |
| 137 candidate.icon_url = favicon_url.icon_url; | |
| 138 candidate.icon_type = favicon_url.icon_type; | |
| 139 SelectFaviconFrameIndices(favicon_url.icon_sizes, desired_pixel_sizes, | |
| 140 /*best_indices=*/nullptr, &candidate.score); | |
|
pkotwicz
2017/03/27 00:49:06
https://www.reddit.com/ specifies two icons:
<lin
mastiz
2017/03/27 10:09:36
I prefer implementing such logic in a dedicated pa
pkotwicz
2017/03/27 19:12:44
If you "assign 16x16 to sizes for .ico files if si
mastiz
2017/03/28 09:09:30
Done. Filed a bug against you since our team is un
| |
| 141 return candidate; | |
| 180 } | 142 } |
| 181 | 143 |
| 182 FaviconHandler::FaviconCandidate::~FaviconCandidate() { | |
| 183 } | |
| 184 | |
| 185 FaviconHandler::FaviconCandidate::FaviconCandidate( | |
| 186 const GURL& image_url, | |
| 187 const gfx::Image& image, | |
| 188 float score, | |
| 189 favicon_base::IconType icon_type) | |
| 190 : image_url(image_url), | |
| 191 image(image), | |
| 192 score(score), | |
| 193 icon_type(icon_type) {} | |
| 194 | |
| 195 //////////////////////////////////////////////////////////////////////////////// | 144 //////////////////////////////////////////////////////////////////////////////// |
| 196 | 145 |
| 197 FaviconHandler::FaviconHandler( | 146 FaviconHandler::FaviconHandler( |
| 198 FaviconService* service, | 147 FaviconService* service, |
| 199 Delegate* delegate, | 148 Delegate* delegate, |
| 200 FaviconDriverObserver::NotificationIconType handler_type) | 149 FaviconDriverObserver::NotificationIconType handler_type) |
| 201 : handler_type_(handler_type), | 150 : handler_type_(handler_type), |
| 202 got_favicon_from_history_(false), | 151 got_favicon_from_history_(false), |
| 203 initial_history_result_expired_or_incomplete_(false), | 152 initial_history_result_expired_or_incomplete_(false), |
| 204 redownload_icons_(false), | 153 redownload_icons_(false), |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 231 | 180 |
| 232 void FaviconHandler::FetchFavicon(const GURL& url) { | 181 void FaviconHandler::FetchFavicon(const GURL& url) { |
| 233 cancelable_task_tracker_.TryCancelAll(); | 182 cancelable_task_tracker_.TryCancelAll(); |
| 234 | 183 |
| 235 url_ = url; | 184 url_ = url; |
| 236 | 185 |
| 237 initial_history_result_expired_or_incomplete_ = false; | 186 initial_history_result_expired_or_incomplete_ = false; |
| 238 redownload_icons_ = false; | 187 redownload_icons_ = false; |
| 239 got_favicon_from_history_ = false; | 188 got_favicon_from_history_ = false; |
| 240 download_request_.Cancel(); | 189 download_request_.Cancel(); |
| 241 image_urls_.clear(); | 190 candidates_.clear(); |
| 242 notification_icon_url_ = GURL(); | 191 notification_icon_url_ = GURL(); |
| 243 notification_icon_type_ = favicon_base::INVALID_ICON; | 192 notification_icon_type_ = favicon_base::INVALID_ICON; |
| 244 current_candidate_index_ = 0u; | 193 current_candidate_index_ = 0u; |
| 245 best_favicon_candidate_ = FaviconCandidate(); | 194 best_favicon_ = DownloadedFavicon(); |
| 246 | 195 |
| 247 // Request the favicon from the history service. In parallel to this the | 196 // Request the favicon from the history service. In parallel to this the |
| 248 // renderer is going to notify us (well WebContents) when the favicon url is | 197 // renderer is going to notify us (well WebContents) when the favicon url is |
| 249 // available. | 198 // available. |
| 250 if (service_) { | 199 if (service_) { |
| 251 service_->GetFaviconForPageURL( | 200 service_->GetFaviconForPageURL( |
| 252 url_, icon_types_, preferred_icon_size(), | 201 url_, icon_types_, preferred_icon_size(), |
| 253 base::Bind( | 202 base::Bind( |
| 254 &FaviconHandler::OnFaviconDataForInitialURLFromFaviconService, | 203 &FaviconHandler::OnFaviconDataForInitialURLFromFaviconService, |
| 255 base::Unretained(this)), | 204 base::Unretained(this)), |
| 256 &cancelable_task_tracker_); | 205 &cancelable_task_tracker_); |
| 257 } | 206 } |
| 258 } | 207 } |
| 259 | 208 |
| 260 bool FaviconHandler::UpdateFaviconCandidate(const GURL& image_url, | 209 bool FaviconHandler::UpdateFaviconCandidate( |
| 261 const gfx::Image& image, | 210 const DownloadedFavicon& downloaded_favicon) { |
| 262 float score, | 211 if (downloaded_favicon.candidate.score > best_favicon_.candidate.score) |
| 263 favicon_base::IconType icon_type) { | 212 best_favicon_ = downloaded_favicon; |
| 264 bool replace_best_favicon_candidate = false; | 213 |
| 265 bool exact_match = false; | |
| 266 if (download_largest_icon_) { | 214 if (download_largest_icon_) { |
| 267 replace_best_favicon_candidate = | |
| 268 image.Size().GetArea() > | |
| 269 best_favicon_candidate_.image.Size().GetArea(); | |
| 270 | |
| 271 gfx::Size largest = best_favicon_candidate_.image.Size(); | |
| 272 if (replace_best_favicon_candidate) | |
| 273 largest = image.Size(); | |
| 274 | |
| 275 // The size of the downloaded icon may not match the declared size. Stop | 215 // The size of the downloaded icon may not match the declared size. Stop |
| 276 // downloading if: | 216 // downloading if: |
| 277 // - current candidate is only candidate. | 217 // - current candidate is only candidate. |
| 278 // - next candidate doesn't have sizes attributes, in this case, the rest | 218 // - next candidate doesn't have sizes attributes, in this case, the rest |
| 279 // candidates don't have sizes attribute either, stop downloading now, | 219 // candidates don't have sizes attribute either, stop downloading now, |
| 280 // otherwise, all favicon without sizes attribute are downloaded. | 220 // otherwise, all favicon without sizes attribute are downloaded. |
| 281 // - next candidate has sizes attribute and it is not larger than largest, | 221 // - next candidate has sizes attribute and it is not larger than largest, |
| 282 // - current candidate is maximal one we want. | 222 // - current candidate is maximal one we want. |
|
pkotwicz
2017/03/27 00:49:06
Nit: Can you please update the comment?
I think t
mastiz
2017/03/27 10:09:36
Done.
pkotwicz
2017/03/27 19:12:44
I don't see any changes to the comment
mastiz
2017/03/28 09:09:30
Sorry, this is the second time I fail to export an
| |
| 283 const int maximal_size = GetMaximalIconSize(icon_type); | 223 return current_candidate_index_ + 1 >= candidates_.size() || |
| 284 if (current_candidate_index_ + 1 >= image_urls_.size()) { | 224 candidates_[current_candidate_index_ + 1].score <= |
| 285 exact_match = true; | 225 best_favicon_.candidate.score; |
| 286 } else { | |
| 287 FaviconURL next_image_url = image_urls_[current_candidate_index_ + 1]; | |
| 288 exact_match = next_image_url.icon_sizes.empty() || | |
| 289 next_image_url.icon_sizes[0].GetArea() <= largest.GetArea() || | |
| 290 (image.Size().width() == maximal_size && | |
| 291 image.Size().height() == maximal_size); | |
| 292 } | |
| 293 } else { | 226 } else { |
| 294 exact_match = score == 1 || preferred_icon_size() == 0; | 227 return best_favicon_.candidate.score == 1; |
| 295 replace_best_favicon_candidate = | |
| 296 exact_match || | |
| 297 best_favicon_candidate_.icon_type == favicon_base::INVALID_ICON || | |
| 298 score > best_favicon_candidate_.score; | |
| 299 } | 228 } |
| 300 if (replace_best_favicon_candidate) { | |
| 301 best_favicon_candidate_ = | |
| 302 FaviconCandidate(image_url, image, score, icon_type); | |
| 303 } | |
| 304 return exact_match; | |
| 305 } | 229 } |
| 306 | 230 |
| 307 void FaviconHandler::SetFavicon(const GURL& icon_url, | 231 void FaviconHandler::SetFavicon(const GURL& icon_url, |
| 308 const gfx::Image& image, | 232 const gfx::Image& image, |
| 309 favicon_base::IconType icon_type) { | 233 favicon_base::IconType icon_type) { |
| 310 if (service_ && ShouldSaveFavicon()) | 234 if (service_ && ShouldSaveFavicon()) |
| 311 service_->SetFavicons(url_, icon_url, icon_type, image); | 235 service_->SetFavicons(url_, icon_url, icon_type, image); |
| 312 | 236 |
| 313 NotifyFaviconUpdated(icon_url, icon_type, image); | 237 NotifyFaviconUpdated(icon_url, icon_type, image); |
| 314 } | 238 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 347 notification_icon_url_ = icon_url; | 271 notification_icon_url_ = icon_url; |
| 348 notification_icon_type_ = icon_type; | 272 notification_icon_type_ = icon_type; |
| 349 } | 273 } |
| 350 | 274 |
| 351 void FaviconHandler::OnUpdateFaviconURL( | 275 void FaviconHandler::OnUpdateFaviconURL( |
| 352 const GURL& page_url, | 276 const GURL& page_url, |
| 353 const std::vector<FaviconURL>& candidates) { | 277 const std::vector<FaviconURL>& candidates) { |
| 354 if (page_url != url_) | 278 if (page_url != url_) |
| 355 return; | 279 return; |
| 356 | 280 |
| 357 std::vector<FaviconURL> pruned_candidates; | 281 std::vector<FaviconCandidate> sorted_candidates; |
| 282 const std::vector<int> desired_pixel_sizes = | |
| 283 GetDesiredPixelSizes(handler_type_); | |
| 358 for (const FaviconURL& candidate : candidates) { | 284 for (const FaviconURL& candidate : candidates) { |
| 359 if (!candidate.icon_url.is_empty() && (candidate.icon_type & icon_types_)) | 285 if (!candidate.icon_url.is_empty() && (candidate.icon_type & icon_types_)) |
|
pkotwicz
2017/03/27 19:12:43
Nit: Can you please add braces since the inner cod
mastiz
2017/03/28 09:09:30
Done.
| |
| 360 pruned_candidates.push_back(candidate); | 286 sorted_candidates.push_back( |
| 287 FaviconCandidate::FromFaviconURL(candidate, desired_pixel_sizes)); | |
| 361 } | 288 } |
| 362 | 289 |
| 363 if (download_largest_icon_) | 290 std::stable_sort(sorted_candidates.begin(), sorted_candidates.end(), |
| 364 SortAndPruneImageUrls(&pruned_candidates); | 291 &FaviconCandidate::CompareScore); |
| 365 | 292 |
| 366 // Ignore FaviconURL::icon_sizes because FaviconURL::icon_sizes is not stored | 293 // Note that icon sizes are ignored because FaviconCandidate doesn't store |
| 367 // in the history database. | 294 // icon sizes. This is important because icon sizes are not stored in the |
| 368 if (image_urls_.size() == pruned_candidates.size() && | 295 // history database. |
|
pkotwicz
2017/03/27 19:12:43
This comment is no longer valid since FaviconCandi
mastiz
2017/03/28 09:09:30
Done, removed.
| |
| 369 std::equal(pruned_candidates.begin(), pruned_candidates.end(), | 296 if (candidates_.size() == sorted_candidates.size() && |
| 370 image_urls_.begin(), FaviconURLsEqualIgnoringSizes)) { | 297 std::equal(sorted_candidates.begin(), sorted_candidates.end(), |
| 298 candidates_.begin(), &FaviconCandidate::Equals)) { | |
| 371 return; | 299 return; |
| 372 } | 300 } |
| 373 | 301 |
| 374 download_request_.Cancel(); | 302 download_request_.Cancel(); |
| 375 image_urls_ = pruned_candidates; | 303 candidates_ = std::move(sorted_candidates); |
| 376 current_candidate_index_ = 0u; | 304 current_candidate_index_ = 0u; |
| 377 best_favicon_candidate_ = FaviconCandidate(); | 305 best_favicon_ = DownloadedFavicon(); |
| 378 | 306 |
| 379 // TODO(davemoore) Should clear on empty url. Currently we ignore it. | 307 // TODO(davemoore) Should clear on empty url. Currently we ignore it. |
| 380 // This appears to be what FF does as well. | 308 // This appears to be what FF does as well. |
| 381 if (current_candidate() && got_favicon_from_history_) | 309 if (current_candidate() && got_favicon_from_history_) |
| 382 OnGotInitialHistoryDataAndIconURLCandidates(); | 310 OnGotInitialHistoryDataAndIconURLCandidates(); |
| 383 } | 311 } |
| 384 | 312 |
| 385 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) { | 313 // static |
| 386 switch (icon_type) { | 314 int FaviconHandler::GetMaximalIconSize( |
| 387 case favicon_base::FAVICON: | 315 FaviconDriverObserver::NotificationIconType handler_type) { |
| 388 #if defined(OS_ANDROID) | 316 int max_size = 0; |
| 389 return 192; | 317 for (int size : GetDesiredPixelSizes(handler_type)) |
| 390 #else | 318 max_size = std::max(max_size, size); |
| 391 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize; | 319 return max_size; |
| 392 #endif | |
| 393 case favicon_base::TOUCH_ICON: | |
| 394 case favicon_base::TOUCH_PRECOMPOSED_ICON: | |
| 395 return kTouchIconSize; | |
| 396 case favicon_base::INVALID_ICON: | |
| 397 return 0; | |
| 398 } | |
| 399 NOTREACHED(); | |
| 400 return 0; | |
| 401 } | 320 } |
| 402 | 321 |
| 403 void FaviconHandler::OnGotInitialHistoryDataAndIconURLCandidates() { | 322 void FaviconHandler::OnGotInitialHistoryDataAndIconURLCandidates() { |
| 404 if (!initial_history_result_expired_or_incomplete_ && | 323 if (!initial_history_result_expired_or_incomplete_ && |
| 405 DoUrlAndIconMatch(*current_candidate(), notification_icon_url_, | 324 current_candidate()->icon_url == notification_icon_url_ && |
| 406 notification_icon_type_)) { | 325 current_candidate()->icon_type == notification_icon_type_) { |
| 407 // - The data from history is valid and not expired. | 326 // - The data from history is valid and not expired. |
| 408 // - The icon URL of the history data matches one of the page's icon URLs. | 327 // - The icon URL of the history data matches one of the page's icon URLs. |
| 409 // - The icon URL of the history data matches the icon URL of the last | 328 // - The icon URL of the history data matches the icon URL of the last |
| 410 // OnFaviconAvailable() notification. | 329 // OnFaviconAvailable() notification. |
| 411 // We are done. No additional downloads or history requests are needed. | 330 // We are done. No additional downloads or history requests are needed. |
| 412 // TODO: Store all of the icon URLs associated with a page in history so | 331 // TODO: Store all of the icon URLs associated with a page in history so |
| 413 // that we can check whether the page's icon URLs match the page's icon URLs | 332 // that we can check whether the page's icon URLs match the page's icon URLs |
| 414 // at the time that the favicon data was stored to the history database. | 333 // at the time that the favicon data was stored to the history database. |
| 415 return; | 334 return; |
| 416 } | 335 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 432 DVLOG(1) << "Failed to Download Favicon:" << image_url; | 351 DVLOG(1) << "Failed to Download Favicon:" << image_url; |
| 433 if (service_) | 352 if (service_) |
| 434 service_->UnableToDownloadFavicon(image_url); | 353 service_->UnableToDownloadFavicon(image_url); |
| 435 } | 354 } |
| 436 | 355 |
| 437 bool request_next_icon = true; | 356 bool request_next_icon = true; |
| 438 if (!bitmaps.empty()) { | 357 if (!bitmaps.empty()) { |
| 439 float score = 0.0f; | 358 float score = 0.0f; |
| 440 gfx::ImageSkia image_skia; | 359 gfx::ImageSkia image_skia; |
| 441 if (download_largest_icon_) { | 360 if (download_largest_icon_) { |
| 442 int index = -1; | 361 std::vector<size_t> best_indices; |
| 443 // Use the largest bitmap if FaviconURL doesn't have sizes attribute. | 362 SelectFaviconFrameIndices(original_bitmap_sizes, |
| 444 if (current_candidate()->icon_sizes.empty()) { | 363 GetDesiredPixelSizes(handler_type_), |
| 445 index = GetLargestSizeIndex(original_bitmap_sizes); | 364 &best_indices, &score); |
| 446 } else { | 365 DCHECK_EQ(1U, best_indices.size()); |
| 447 index = GetIndexBySize(original_bitmap_sizes, | 366 image_skia = |
| 448 current_candidate()->icon_sizes[0]); | 367 gfx::ImageSkia::CreateFrom1xBitmap(bitmaps[best_indices.front()]); |
| 449 // Find largest bitmap if there is no one exactly matched. | |
| 450 if (index == -1) | |
| 451 index = GetLargestSizeIndex(original_bitmap_sizes); | |
| 452 } | |
| 453 image_skia = gfx::ImageSkia(gfx::ImageSkiaRep(bitmaps[index], 1)); | |
| 454 } else { | 368 } else { |
| 455 image_skia = CreateFaviconImageSkia(bitmaps, | 369 image_skia = CreateFaviconImageSkia(bitmaps, |
| 456 original_bitmap_sizes, | 370 original_bitmap_sizes, |
| 457 preferred_icon_size(), | 371 preferred_icon_size(), |
| 458 &score); | 372 &score); |
| 459 } | 373 } |
| 460 | 374 |
| 461 if (!image_skia.isNull()) { | 375 if (!image_skia.isNull()) { |
| 462 gfx::Image image(image_skia); | |
| 463 // The downloaded icon is still valid when there is no FaviconURL update | 376 // The downloaded icon is still valid when there is no FaviconURL update |
| 464 // during the downloading. | 377 // during the downloading. |
| 465 request_next_icon = | 378 DownloadedFavicon downloaded_favicon; |
| 466 !UpdateFaviconCandidate(image_url, image, score, icon_type); | 379 downloaded_favicon.image = gfx::Image(image_skia); |
| 380 downloaded_favicon.candidate.icon_url = image_url; | |
| 381 downloaded_favicon.candidate.icon_type = icon_type; | |
| 382 downloaded_favicon.candidate.score = score; | |
| 383 request_next_icon = !UpdateFaviconCandidate(downloaded_favicon); | |
| 467 } | 384 } |
| 468 } | 385 } |
| 469 | 386 |
| 470 if (request_next_icon && current_candidate_index_ + 1 < image_urls_.size()) { | 387 if (request_next_icon && current_candidate_index_ + 1 < candidates_.size()) { |
| 471 // Process the next candidate. | 388 // Process the next candidate. |
| 472 ++current_candidate_index_; | 389 ++current_candidate_index_; |
| 473 DownloadCurrentCandidateOrAskFaviconService(); | 390 DownloadCurrentCandidateOrAskFaviconService(); |
| 474 } else { | 391 } else { |
| 475 // We have either found the ideal candidate or run out of candidates. | 392 // We have either found the ideal candidate or run out of candidates. |
| 476 if (best_favicon_candidate_.icon_type != favicon_base::INVALID_ICON) { | 393 if (best_favicon_.candidate.icon_type != favicon_base::INVALID_ICON) { |
| 477 // No more icons to request, set the favicon from the candidate. | 394 // No more icons to request, set the favicon from the candidate. |
| 478 SetFavicon(best_favicon_candidate_.image_url, | 395 SetFavicon(best_favicon_.candidate.icon_url, best_favicon_.image, |
| 479 best_favicon_candidate_.image, | 396 best_favicon_.candidate.icon_type); |
| 480 best_favicon_candidate_.icon_type); | |
| 481 } | 397 } |
| 482 // Clear download related state. | 398 // Clear download related state. |
| 483 current_candidate_index_ = image_urls_.size(); | 399 current_candidate_index_ = candidates_.size(); |
| 484 best_favicon_candidate_ = FaviconCandidate(); | 400 best_favicon_ = DownloadedFavicon(); |
| 485 } | 401 } |
| 486 } | 402 } |
| 487 | 403 |
| 404 const std::vector<GURL> FaviconHandler::GetIconURLs() const { | |
| 405 std::vector<GURL> icon_urls; | |
| 406 for (const FaviconCandidate& candidate : candidates_) | |
| 407 icon_urls.push_back(candidate.icon_url); | |
| 408 return icon_urls; | |
| 409 } | |
| 410 | |
| 488 bool FaviconHandler::HasPendingTasksForTest() { | 411 bool FaviconHandler::HasPendingTasksForTest() { |
| 489 return !download_request_.IsCancelled() || | 412 return !download_request_.IsCancelled() || |
| 490 cancelable_task_tracker_.HasTrackedTasks(); | 413 cancelable_task_tracker_.HasTrackedTasks(); |
| 491 } | 414 } |
| 492 | 415 |
| 493 bool FaviconHandler::ShouldSaveFavicon() { | 416 bool FaviconHandler::ShouldSaveFavicon() { |
| 494 if (!delegate_->IsOffTheRecord()) | 417 if (!delegate_->IsOffTheRecord()) |
| 495 return true; | 418 return true; |
| 496 | 419 |
| 497 // Always save favicon if the page is bookmarked. | 420 // Always save favicon if the page is bookmarked. |
| 498 return delegate_->IsBookmarked(url_); | 421 return delegate_->IsBookmarked(url_); |
| 499 } | 422 } |
| 500 | 423 |
| 501 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( | 424 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( |
| 502 const std::vector<favicon_base::FaviconRawBitmapResult>& | 425 const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 503 favicon_bitmap_results) { | 426 favicon_bitmap_results) { |
| 504 got_favicon_from_history_ = true; | 427 got_favicon_from_history_ = true; |
| 505 bool has_valid_result = HasValidResult(favicon_bitmap_results); | 428 bool has_valid_result = HasValidResult(favicon_bitmap_results); |
| 506 initial_history_result_expired_or_incomplete_ = | 429 initial_history_result_expired_or_incomplete_ = |
| 507 !has_valid_result || | 430 !has_valid_result || |
| 508 HasExpiredOrIncompleteResult(preferred_icon_size(), | 431 HasExpiredOrIncompleteResult(preferred_icon_size(), |
| 509 favicon_bitmap_results); | 432 favicon_bitmap_results); |
| 510 redownload_icons_ = initial_history_result_expired_or_incomplete_ && | 433 redownload_icons_ = initial_history_result_expired_or_incomplete_ && |
| 511 !favicon_bitmap_results.empty(); | 434 !favicon_bitmap_results.empty(); |
| 512 | 435 |
| 513 if (has_valid_result && | 436 if (has_valid_result && (!current_candidate() || |
| 514 (!current_candidate() || | 437 DoUrlsAndIconsMatch(current_candidate()->icon_url, |
| 515 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { | 438 current_candidate()->icon_type, |
| 439 favicon_bitmap_results))) { | |
| 516 // The db knows the favicon (although it may be out of date) and the entry | 440 // The db knows the favicon (although it may be out of date) and the entry |
| 517 // doesn't have an icon. Set the favicon now, and if the favicon turns out | 441 // doesn't have an icon. Set the favicon now, and if the favicon turns out |
| 518 // to be expired (or the wrong url) we'll fetch later on. This way the | 442 // to be expired (or the wrong url) we'll fetch later on. This way the |
| 519 // user doesn't see a flash of the default favicon. | 443 // user doesn't see a flash of the default favicon. |
| 520 NotifyFaviconUpdated(favicon_bitmap_results); | 444 NotifyFaviconUpdated(favicon_bitmap_results); |
| 521 } | 445 } |
| 522 | 446 |
| 523 if (current_candidate()) | 447 if (current_candidate()) |
| 524 OnGotInitialHistoryDataAndIconURLCandidates(); | 448 OnGotInitialHistoryDataAndIconURLCandidates(); |
| 525 } | 449 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 566 | 490 |
| 567 if (has_valid_result) { | 491 if (has_valid_result) { |
| 568 // There is a valid favicon. Notify any observers. It is useful to notify | 492 // There is a valid favicon. Notify any observers. It is useful to notify |
| 569 // the observers even if the favicon is expired or incomplete (incorrect | 493 // the observers even if the favicon is expired or incomplete (incorrect |
| 570 // size) because temporarily showing the user an expired favicon or | 494 // size) because temporarily showing the user an expired favicon or |
| 571 // streched favicon is preferable to showing the user the default favicon. | 495 // streched favicon is preferable to showing the user the default favicon. |
| 572 NotifyFaviconUpdated(favicon_bitmap_results); | 496 NotifyFaviconUpdated(favicon_bitmap_results); |
| 573 } | 497 } |
| 574 | 498 |
| 575 if (!current_candidate() || | 499 if (!current_candidate() || |
| 576 (has_results && | 500 (has_results && !DoUrlsAndIconsMatch(current_candidate()->icon_url, |
| 577 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { | 501 current_candidate()->icon_type, |
| 502 favicon_bitmap_results))) { | |
| 578 // The icon URLs have been updated since the favicon data was requested. | 503 // The icon URLs have been updated since the favicon data was requested. |
| 579 return; | 504 return; |
| 580 } | 505 } |
| 581 | 506 |
| 582 if (has_expired_or_incomplete_result) { | 507 if (has_expired_or_incomplete_result) { |
| 583 ScheduleDownload(current_candidate()->icon_url, | 508 ScheduleDownload(current_candidate()->icon_url, |
| 584 current_candidate()->icon_type); | 509 current_candidate()->icon_type); |
| 585 } | 510 } |
| 586 } | 511 } |
| 587 | 512 |
| 588 void FaviconHandler::ScheduleDownload(const GURL& image_url, | 513 void FaviconHandler::ScheduleDownload(const GURL& image_url, |
| 589 favicon_base::IconType icon_type) { | 514 favicon_base::IconType icon_type) { |
| 590 DCHECK(image_url.is_valid()); | 515 DCHECK(image_url.is_valid()); |
| 591 // Note that CancelableCallback starts cancelled. | 516 // Note that CancelableCallback starts cancelled. |
| 592 DCHECK(download_request_.IsCancelled()) << "More than one ongoing download"; | 517 DCHECK(download_request_.IsCancelled()) << "More than one ongoing download"; |
| 593 if (service_ && service_->WasUnableToDownloadFavicon(image_url)) { | 518 if (service_ && service_->WasUnableToDownloadFavicon(image_url)) { |
| 594 DVLOG(1) << "Skip Failed FavIcon: " << image_url; | 519 DVLOG(1) << "Skip Failed FavIcon: " << image_url; |
| 595 OnDidDownloadFavicon(icon_type, 0, 0, image_url, std::vector<SkBitmap>(), | 520 OnDidDownloadFavicon(icon_type, 0, 0, image_url, std::vector<SkBitmap>(), |
| 596 std::vector<gfx::Size>()); | 521 std::vector<gfx::Size>()); |
| 597 return; | 522 return; |
| 598 } | 523 } |
| 599 download_request_.Reset(base::Bind(&FaviconHandler::OnDidDownloadFavicon, | 524 download_request_.Reset(base::Bind(&FaviconHandler::OnDidDownloadFavicon, |
| 600 base::Unretained(this), icon_type)); | 525 base::Unretained(this), icon_type)); |
| 601 // A max bitmap size is specified to avoid receiving huge bitmaps in | 526 // A max bitmap size is specified to avoid receiving huge bitmaps in |
| 602 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() | 527 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() |
| 603 // for more details about the max bitmap size. | 528 // for more details about the max bitmap size. |
| 604 const int download_id = delegate_->DownloadImage( | 529 const int download_id = |
| 605 image_url, GetMaximalIconSize(icon_type), download_request_.callback()); | 530 delegate_->DownloadImage(image_url, GetMaximalIconSize(handler_type_), |
| 531 download_request_.callback()); | |
| 606 DCHECK_NE(download_id, 0); | 532 DCHECK_NE(download_id, 0); |
| 607 } | 533 } |
| 608 | 534 |
| 609 } // namespace favicon | 535 } // namespace favicon |
| OLD | NEW |