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 // Returns the index of the entry with the best score to match |
| 117 int GetLargestSizeIndex(const std::vector<gfx::Size>& sizes) { | 111 // |target_pixel_size|. Returns the highest score in |*score|. |
| 112 int GetBestScoreIndex(const std::vector<gfx::Size>& sizes, | |
| 113 int target_pixel_size, | |
| 114 float* score) { | |
| 118 DCHECK(!sizes.empty()); | 115 DCHECK(!sizes.empty()); |
| 119 size_t ret = 0; | 116 size_t ret = 0; |
| 120 for (size_t i = 1; i < sizes.size(); ++i) { | 117 float best_score = -1; |
| 121 if (sizes[ret].GetArea() < sizes[i].GetArea()) | 118 for (size_t i = 0; i < sizes.size(); ++i) { |
| 119 float size_score = GetFaviconCandidateScore(sizes[i], target_pixel_size); | |
| 120 if (size_score > best_score) { | |
| 121 best_score = size_score; | |
| 122 ret = i; | 122 ret = i; |
| 123 } | |
| 123 } | 124 } |
| 125 *score = best_score; | |
| 124 return static_cast<int>(ret); | 126 return static_cast<int>(ret); |
| 125 } | 127 } |
| 126 | 128 |
| 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 } | |
| 173 | |
| 174 } // namespace | 129 } // namespace |
| 175 | 130 |
| 176 //////////////////////////////////////////////////////////////////////////////// | 131 //////////////////////////////////////////////////////////////////////////////// |
| 177 | 132 |
| 178 FaviconHandler::FaviconCandidate::FaviconCandidate() | 133 // static |
| 179 : score(0), icon_type(favicon_base::INVALID_ICON) { | 134 FaviconHandler::FaviconCandidate |
| 135 FaviconHandler::FaviconCandidate::FromFaviconURL( | |
| 136 const favicon::FaviconURL& favicon_url, | |
| 137 int target_pixel_size) { | |
| 138 FaviconCandidate candidate; | |
| 139 candidate.icon_url = favicon_url.icon_url; | |
| 140 candidate.icon_type = favicon_url.icon_type; | |
| 141 candidate.score = 0; | |
| 142 for (const gfx::Size& favicon_size : favicon_url.icon_sizes) { | |
| 143 candidate.score = | |
| 144 std::max(candidate.score, | |
| 145 GetFaviconCandidateScore(favicon_size, target_pixel_size)); | |
| 146 } | |
|
pkotwicz
2017/03/23 20:43:25
This score is suboptimal.
If FaviconHandler wants
mastiz
2017/03/24 17:14:59
Done.
pkotwicz
2017/03/26 01:39:53
I don't see any changes?
mastiz
2017/03/26 19:33:16
Now?
| |
| 147 return candidate; | |
| 180 } | 148 } |
| 181 | 149 |
| 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 //////////////////////////////////////////////////////////////////////////////// | 150 //////////////////////////////////////////////////////////////////////////////// |
| 196 | 151 |
| 197 FaviconHandler::FaviconHandler( | 152 FaviconHandler::FaviconHandler( |
| 198 FaviconService* service, | 153 FaviconService* service, |
| 199 Delegate* delegate, | 154 Delegate* delegate, |
| 200 FaviconDriverObserver::NotificationIconType handler_type) | 155 FaviconDriverObserver::NotificationIconType handler_type) |
| 201 : handler_type_(handler_type), | 156 : handler_type_(handler_type), |
| 202 got_favicon_from_history_(false), | 157 got_favicon_from_history_(false), |
| 203 initial_history_result_expired_or_incomplete_(false), | 158 initial_history_result_expired_or_incomplete_(false), |
| 204 redownload_icons_(false), | 159 redownload_icons_(false), |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 231 | 186 |
| 232 void FaviconHandler::FetchFavicon(const GURL& url) { | 187 void FaviconHandler::FetchFavicon(const GURL& url) { |
| 233 cancelable_task_tracker_.TryCancelAll(); | 188 cancelable_task_tracker_.TryCancelAll(); |
| 234 | 189 |
| 235 url_ = url; | 190 url_ = url; |
| 236 | 191 |
| 237 initial_history_result_expired_or_incomplete_ = false; | 192 initial_history_result_expired_or_incomplete_ = false; |
| 238 redownload_icons_ = false; | 193 redownload_icons_ = false; |
| 239 got_favicon_from_history_ = false; | 194 got_favicon_from_history_ = false; |
| 240 download_request_.Cancel(); | 195 download_request_.Cancel(); |
| 241 image_urls_.clear(); | 196 candidates_.clear(); |
| 242 notification_icon_url_ = GURL(); | 197 notification_icon_url_ = GURL(); |
| 243 notification_icon_type_ = favicon_base::INVALID_ICON; | 198 notification_icon_type_ = favicon_base::INVALID_ICON; |
| 244 current_candidate_index_ = 0u; | 199 current_candidate_index_ = 0u; |
| 245 best_favicon_candidate_ = FaviconCandidate(); | 200 best_favicon_ = DownloadedFavicon(); |
| 246 | 201 |
| 247 // Request the favicon from the history service. In parallel to this the | 202 // 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 | 203 // renderer is going to notify us (well WebContents) when the favicon url is |
| 249 // available. | 204 // available. |
| 250 if (service_) { | 205 if (service_) { |
| 251 service_->GetFaviconForPageURL( | 206 service_->GetFaviconForPageURL( |
| 252 url_, icon_types_, preferred_icon_size(), | 207 url_, icon_types_, preferred_icon_size(), |
| 253 base::Bind( | 208 base::Bind( |
| 254 &FaviconHandler::OnFaviconDataForInitialURLFromFaviconService, | 209 &FaviconHandler::OnFaviconDataForInitialURLFromFaviconService, |
| 255 base::Unretained(this)), | 210 base::Unretained(this)), |
| 256 &cancelable_task_tracker_); | 211 &cancelable_task_tracker_); |
| 257 } | 212 } |
| 258 } | 213 } |
| 259 | 214 |
| 260 bool FaviconHandler::UpdateFaviconCandidate(const GURL& image_url, | 215 bool FaviconHandler::UpdateFaviconCandidate( |
| 261 const gfx::Image& image, | 216 const DownloadedFavicon& downloaded_favicon) { |
| 262 float score, | 217 if (downloaded_favicon.candidate.score > best_favicon_.candidate.score) |
| 263 favicon_base::IconType icon_type) { | 218 best_favicon_ = downloaded_favicon; |
| 264 bool replace_best_favicon_candidate = false; | 219 |
| 265 bool exact_match = false; | |
| 266 if (download_largest_icon_) { | 220 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 | 221 // The size of the downloaded icon may not match the declared size. Stop |
| 276 // downloading if: | 222 // downloading if: |
| 277 // - current candidate is only candidate. | 223 // - current candidate is only candidate. |
| 278 // - next candidate doesn't have sizes attributes, in this case, the rest | 224 // - next candidate doesn't have sizes attributes, in this case, the rest |
| 279 // candidates don't have sizes attribute either, stop downloading now, | 225 // candidates don't have sizes attribute either, stop downloading now, |
| 280 // otherwise, all favicon without sizes attribute are downloaded. | 226 // otherwise, all favicon without sizes attribute are downloaded. |
| 281 // - next candidate has sizes attribute and it is not larger than largest, | 227 // - next candidate has sizes attribute and it is not larger than largest, |
| 282 // - current candidate is maximal one we want. | 228 // - current candidate is maximal one we want. |
| 283 const int maximal_size = GetMaximalIconSize(icon_type); | 229 return current_candidate_index_ + 1 >= candidates_.size() || |
| 284 if (current_candidate_index_ + 1 >= image_urls_.size()) { | 230 candidates_[current_candidate_index_ + 1].score <= |
| 285 exact_match = true; | 231 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 { | 232 } else { |
| 294 exact_match = score == 1 || preferred_icon_size() == 0; | 233 return best_favicon_.candidate.score == 1; |
|
pkotwicz
2017/03/23 20:43:25
Good eye in catching that we can remove the
prefe
| |
| 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 } | 234 } |
| 300 if (replace_best_favicon_candidate) { | |
| 301 best_favicon_candidate_ = | |
| 302 FaviconCandidate(image_url, image, score, icon_type); | |
| 303 } | |
| 304 return exact_match; | |
| 305 } | 235 } |
| 306 | 236 |
| 307 void FaviconHandler::SetFavicon(const GURL& icon_url, | 237 void FaviconHandler::SetFavicon(const GURL& icon_url, |
| 308 const gfx::Image& image, | 238 const gfx::Image& image, |
| 309 favicon_base::IconType icon_type) { | 239 favicon_base::IconType icon_type) { |
| 310 if (service_ && ShouldSaveFavicon()) | 240 if (service_ && ShouldSaveFavicon()) |
| 311 service_->SetFavicons(url_, icon_url, icon_type, image); | 241 service_->SetFavicons(url_, icon_url, icon_type, image); |
| 312 | 242 |
| 313 NotifyFaviconUpdated(icon_url, icon_type, image); | 243 NotifyFaviconUpdated(icon_url, icon_type, image); |
| 314 } | 244 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 347 notification_icon_url_ = icon_url; | 277 notification_icon_url_ = icon_url; |
| 348 notification_icon_type_ = icon_type; | 278 notification_icon_type_ = icon_type; |
| 349 } | 279 } |
| 350 | 280 |
| 351 void FaviconHandler::OnUpdateFaviconURL( | 281 void FaviconHandler::OnUpdateFaviconURL( |
| 352 const GURL& page_url, | 282 const GURL& page_url, |
| 353 const std::vector<FaviconURL>& candidates) { | 283 const std::vector<FaviconURL>& candidates) { |
| 354 if (page_url != url_) | 284 if (page_url != url_) |
| 355 return; | 285 return; |
| 356 | 286 |
| 357 std::vector<FaviconURL> pruned_candidates; | 287 std::vector<FaviconCandidate> sorted_candidates; |
| 358 for (const FaviconURL& candidate : candidates) { | 288 for (const FaviconURL& candidate : candidates) { |
| 359 if (!candidate.icon_url.is_empty() && (candidate.icon_type & icon_types_)) | 289 if (!candidate.icon_url.is_empty() && (candidate.icon_type & icon_types_)) |
| 360 pruned_candidates.push_back(candidate); | 290 sorted_candidates.push_back(FaviconCandidate::FromFaviconURL( |
| 291 candidate, GetMaximalIconSize(handler_type_))); | |
| 361 } | 292 } |
| 362 | 293 |
| 363 if (download_largest_icon_) | 294 std::stable_sort(sorted_candidates.begin(), sorted_candidates.end(), |
| 364 SortAndPruneImageUrls(&pruned_candidates); | 295 &FaviconCandidate::CompareScore); |
| 365 | 296 |
| 366 // Ignore FaviconURL::icon_sizes because FaviconURL::icon_sizes is not stored | 297 // Note that icon sizes are ignored because FaviconCandidate doesn't store |
| 367 // in the history database. | 298 // icon sizes. This is important because icon sizes are not stored in the |
| 368 if (image_urls_.size() == pruned_candidates.size() && | 299 // history database. |
| 369 std::equal(pruned_candidates.begin(), pruned_candidates.end(), | 300 if (candidates_.size() == sorted_candidates.size() && |
| 370 image_urls_.begin(), FaviconURLsEqualIgnoringSizes)) { | 301 std::equal(sorted_candidates.begin(), sorted_candidates.end(), |
| 302 candidates_.begin(), &FaviconCandidate::Equals)) { | |
| 371 return; | 303 return; |
| 372 } | 304 } |
| 373 | 305 |
| 374 download_request_.Cancel(); | 306 download_request_.Cancel(); |
| 375 image_urls_ = pruned_candidates; | 307 candidates_ = std::move(sorted_candidates); |
| 376 current_candidate_index_ = 0u; | 308 current_candidate_index_ = 0u; |
| 377 best_favicon_candidate_ = FaviconCandidate(); | 309 best_favicon_ = DownloadedFavicon(); |
| 378 | 310 |
| 379 // TODO(davemoore) Should clear on empty url. Currently we ignore it. | 311 // TODO(davemoore) Should clear on empty url. Currently we ignore it. |
| 380 // This appears to be what FF does as well. | 312 // This appears to be what FF does as well. |
| 381 if (current_candidate() && got_favicon_from_history_) | 313 if (current_candidate() && got_favicon_from_history_) |
| 382 OnGotInitialHistoryDataAndIconURLCandidates(); | 314 OnGotInitialHistoryDataAndIconURLCandidates(); |
| 383 } | 315 } |
| 384 | 316 |
| 385 int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) { | 317 // static |
| 386 switch (icon_type) { | 318 int FaviconHandler::GetMaximalIconSize( |
| 387 case favicon_base::FAVICON: | 319 FaviconDriverObserver::NotificationIconType handler_type) { |
| 388 #if defined(OS_ANDROID) | 320 switch (handler_type) { |
| 389 return 192; | 321 case FaviconDriverObserver::NON_TOUCH_16_DIP: |
| 390 #else | |
| 391 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize; | 322 return gfx::ImageSkia::GetMaxSupportedScale() * gfx::kFaviconSize; |
| 392 #endif | 323 case FaviconDriverObserver::NON_TOUCH_LARGEST: |
| 393 case favicon_base::TOUCH_ICON: | 324 return kNonTouchLargestIconSize; |
| 394 case favicon_base::TOUCH_PRECOMPOSED_ICON: | 325 case FaviconDriverObserver::TOUCH_LARGEST: |
| 395 return kTouchIconSize; | 326 return kTouchIconSize; |
| 396 case favicon_base::INVALID_ICON: | |
| 397 return 0; | |
| 398 } | 327 } |
| 399 NOTREACHED(); | 328 NOTREACHED(); |
| 400 return 0; | 329 return 0; |
| 401 } | 330 } |
| 402 | 331 |
| 403 void FaviconHandler::OnGotInitialHistoryDataAndIconURLCandidates() { | 332 void FaviconHandler::OnGotInitialHistoryDataAndIconURLCandidates() { |
| 404 if (!initial_history_result_expired_or_incomplete_ && | 333 if (!initial_history_result_expired_or_incomplete_ && |
| 405 DoUrlAndIconMatch(*current_candidate(), notification_icon_url_, | 334 current_candidate()->icon_url == notification_icon_url_ && |
| 406 notification_icon_type_)) { | 335 current_candidate()->icon_type == notification_icon_type_) { |
| 407 // - The data from history is valid and not expired. | 336 // - 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. | 337 // - 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 | 338 // - The icon URL of the history data matches the icon URL of the last |
| 410 // OnFaviconAvailable() notification. | 339 // OnFaviconAvailable() notification. |
| 411 // We are done. No additional downloads or history requests are needed. | 340 // 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 | 341 // 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 | 342 // 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. | 343 // at the time that the favicon data was stored to the history database. |
| 415 return; | 344 return; |
| 416 } | 345 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 432 DVLOG(1) << "Failed to Download Favicon:" << image_url; | 361 DVLOG(1) << "Failed to Download Favicon:" << image_url; |
| 433 if (service_) | 362 if (service_) |
| 434 service_->UnableToDownloadFavicon(image_url); | 363 service_->UnableToDownloadFavicon(image_url); |
| 435 } | 364 } |
| 436 | 365 |
| 437 bool request_next_icon = true; | 366 bool request_next_icon = true; |
| 438 if (!bitmaps.empty()) { | 367 if (!bitmaps.empty()) { |
| 439 float score = 0.0f; | 368 float score = 0.0f; |
| 440 gfx::ImageSkia image_skia; | 369 gfx::ImageSkia image_skia; |
| 441 if (download_largest_icon_) { | 370 if (download_largest_icon_) { |
| 442 int index = -1; | 371 image_skia = gfx::ImageSkia(gfx::ImageSkiaRep( |
| 443 // Use the largest bitmap if FaviconURL doesn't have sizes attribute. | 372 bitmaps[GetBestScoreIndex(original_bitmap_sizes, |
| 444 if (current_candidate()->icon_sizes.empty()) { | 373 GetMaximalIconSize(handler_type_), &score)], |
| 445 index = GetLargestSizeIndex(original_bitmap_sizes); | 374 1)); |
|
pkotwicz
2017/03/23 20:43:25
- You could use SelectFaviconFrameIndices() to com
mastiz
2017/03/24 17:14:59
Done.
| |
| 446 } else { | |
| 447 index = GetIndexBySize(original_bitmap_sizes, | |
| 448 current_candidate()->icon_sizes[0]); | |
| 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 { | 375 } else { |
| 455 image_skia = CreateFaviconImageSkia(bitmaps, | 376 image_skia = CreateFaviconImageSkia(bitmaps, |
| 456 original_bitmap_sizes, | 377 original_bitmap_sizes, |
| 457 preferred_icon_size(), | 378 preferred_icon_size(), |
| 458 &score); | 379 &score); |
| 459 } | 380 } |
| 460 | 381 |
| 461 if (!image_skia.isNull()) { | 382 if (!image_skia.isNull()) { |
| 462 gfx::Image image(image_skia); | |
| 463 // The downloaded icon is still valid when there is no FaviconURL update | 383 // The downloaded icon is still valid when there is no FaviconURL update |
| 464 // during the downloading. | 384 // during the downloading. |
| 465 request_next_icon = | 385 DownloadedFavicon downloaded_favicon; |
| 466 !UpdateFaviconCandidate(image_url, image, score, icon_type); | 386 downloaded_favicon.image = gfx::Image(image_skia); |
| 387 downloaded_favicon.candidate.icon_url = image_url; | |
| 388 downloaded_favicon.candidate.icon_type = icon_type; | |
| 389 downloaded_favicon.candidate.score = score; | |
| 390 request_next_icon = !UpdateFaviconCandidate(downloaded_favicon); | |
| 467 } | 391 } |
| 468 } | 392 } |
| 469 | 393 |
| 470 if (request_next_icon && current_candidate_index_ + 1 < image_urls_.size()) { | 394 if (request_next_icon && current_candidate_index_ + 1 < candidates_.size()) { |
| 471 // Process the next candidate. | 395 // Process the next candidate. |
| 472 ++current_candidate_index_; | 396 ++current_candidate_index_; |
| 473 DownloadCurrentCandidateOrAskFaviconService(); | 397 DownloadCurrentCandidateOrAskFaviconService(); |
| 474 } else { | 398 } else { |
| 475 // We have either found the ideal candidate or run out of candidates. | 399 // We have either found the ideal candidate or run out of candidates. |
| 476 if (best_favicon_candidate_.icon_type != favicon_base::INVALID_ICON) { | 400 if (best_favicon_.candidate.icon_type != favicon_base::INVALID_ICON) { |
| 477 // No more icons to request, set the favicon from the candidate. | 401 // No more icons to request, set the favicon from the candidate. |
| 478 SetFavicon(best_favicon_candidate_.image_url, | 402 SetFavicon(best_favicon_.candidate.icon_url, best_favicon_.image, |
| 479 best_favicon_candidate_.image, | 403 best_favicon_.candidate.icon_type); |
| 480 best_favicon_candidate_.icon_type); | |
| 481 } | 404 } |
| 482 // Clear download related state. | 405 // Clear download related state. |
| 483 current_candidate_index_ = image_urls_.size(); | 406 current_candidate_index_ = candidates_.size(); |
| 484 best_favicon_candidate_ = FaviconCandidate(); | 407 best_favicon_ = DownloadedFavicon(); |
| 485 } | 408 } |
| 486 } | 409 } |
| 487 | 410 |
| 411 const std::vector<GURL> FaviconHandler::GetIconURLs() const { | |
| 412 std::vector<GURL> icon_urls; | |
| 413 for (const FaviconCandidate& candidate : candidates_) | |
| 414 icon_urls.push_back(candidate.icon_url); | |
| 415 return icon_urls; | |
| 416 } | |
| 417 | |
| 488 bool FaviconHandler::HasPendingTasksForTest() { | 418 bool FaviconHandler::HasPendingTasksForTest() { |
| 489 return !download_request_.IsCancelled() || | 419 return !download_request_.IsCancelled() || |
| 490 cancelable_task_tracker_.HasTrackedTasks(); | 420 cancelable_task_tracker_.HasTrackedTasks(); |
| 491 } | 421 } |
| 492 | 422 |
| 493 bool FaviconHandler::ShouldSaveFavicon() { | 423 bool FaviconHandler::ShouldSaveFavicon() { |
| 494 if (!delegate_->IsOffTheRecord()) | 424 if (!delegate_->IsOffTheRecord()) |
| 495 return true; | 425 return true; |
| 496 | 426 |
| 497 // Always save favicon if the page is bookmarked. | 427 // Always save favicon if the page is bookmarked. |
| 498 return delegate_->IsBookmarked(url_); | 428 return delegate_->IsBookmarked(url_); |
| 499 } | 429 } |
| 500 | 430 |
| 501 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( | 431 void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( |
| 502 const std::vector<favicon_base::FaviconRawBitmapResult>& | 432 const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 503 favicon_bitmap_results) { | 433 favicon_bitmap_results) { |
| 504 got_favicon_from_history_ = true; | 434 got_favicon_from_history_ = true; |
| 505 bool has_valid_result = HasValidResult(favicon_bitmap_results); | 435 bool has_valid_result = HasValidResult(favicon_bitmap_results); |
| 506 initial_history_result_expired_or_incomplete_ = | 436 initial_history_result_expired_or_incomplete_ = |
| 507 !has_valid_result || | 437 !has_valid_result || |
| 508 HasExpiredOrIncompleteResult(preferred_icon_size(), | 438 HasExpiredOrIncompleteResult(preferred_icon_size(), |
| 509 favicon_bitmap_results); | 439 favicon_bitmap_results); |
| 510 redownload_icons_ = initial_history_result_expired_or_incomplete_ && | 440 redownload_icons_ = initial_history_result_expired_or_incomplete_ && |
| 511 !favicon_bitmap_results.empty(); | 441 !favicon_bitmap_results.empty(); |
| 512 | 442 |
| 513 if (has_valid_result && | 443 if (has_valid_result && (!current_candidate() || |
| 514 (!current_candidate() || | 444 DoUrlsAndIconsMatch(current_candidate()->icon_url, |
| 515 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { | 445 current_candidate()->icon_type, |
| 446 favicon_bitmap_results))) { | |
| 516 // The db knows the favicon (although it may be out of date) and the entry | 447 // 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 | 448 // 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 | 449 // 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. | 450 // user doesn't see a flash of the default favicon. |
| 520 NotifyFaviconUpdated(favicon_bitmap_results); | 451 NotifyFaviconUpdated(favicon_bitmap_results); |
| 521 } | 452 } |
| 522 | 453 |
| 523 if (current_candidate()) | 454 if (current_candidate()) |
| 524 OnGotInitialHistoryDataAndIconURLCandidates(); | 455 OnGotInitialHistoryDataAndIconURLCandidates(); |
| 525 } | 456 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 566 | 497 |
| 567 if (has_valid_result) { | 498 if (has_valid_result) { |
| 568 // There is a valid favicon. Notify any observers. It is useful to notify | 499 // 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 | 500 // the observers even if the favicon is expired or incomplete (incorrect |
| 570 // size) because temporarily showing the user an expired favicon or | 501 // size) because temporarily showing the user an expired favicon or |
| 571 // streched favicon is preferable to showing the user the default favicon. | 502 // streched favicon is preferable to showing the user the default favicon. |
| 572 NotifyFaviconUpdated(favicon_bitmap_results); | 503 NotifyFaviconUpdated(favicon_bitmap_results); |
| 573 } | 504 } |
| 574 | 505 |
| 575 if (!current_candidate() || | 506 if (!current_candidate() || |
| 576 (has_results && | 507 (has_results && !DoUrlsAndIconsMatch(current_candidate()->icon_url, |
| 577 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { | 508 current_candidate()->icon_type, |
| 509 favicon_bitmap_results))) { | |
| 578 // The icon URLs have been updated since the favicon data was requested. | 510 // The icon URLs have been updated since the favicon data was requested. |
| 579 return; | 511 return; |
| 580 } | 512 } |
| 581 | 513 |
| 582 if (has_expired_or_incomplete_result) { | 514 if (has_expired_or_incomplete_result) { |
| 583 ScheduleDownload(current_candidate()->icon_url, | 515 ScheduleDownload(current_candidate()->icon_url, |
| 584 current_candidate()->icon_type); | 516 current_candidate()->icon_type); |
| 585 } | 517 } |
| 586 } | 518 } |
| 587 | 519 |
| 588 void FaviconHandler::ScheduleDownload(const GURL& image_url, | 520 void FaviconHandler::ScheduleDownload(const GURL& image_url, |
| 589 favicon_base::IconType icon_type) { | 521 favicon_base::IconType icon_type) { |
| 590 DCHECK(image_url.is_valid()); | 522 DCHECK(image_url.is_valid()); |
| 591 // Note that CancelableCallback starts cancelled. | 523 // Note that CancelableCallback starts cancelled. |
| 592 DCHECK(download_request_.IsCancelled()) << "More than one ongoing download"; | 524 DCHECK(download_request_.IsCancelled()) << "More than one ongoing download"; |
| 593 if (service_ && service_->WasUnableToDownloadFavicon(image_url)) { | 525 if (service_ && service_->WasUnableToDownloadFavicon(image_url)) { |
| 594 DVLOG(1) << "Skip Failed FavIcon: " << image_url; | 526 DVLOG(1) << "Skip Failed FavIcon: " << image_url; |
| 595 OnDidDownloadFavicon(icon_type, 0, 0, image_url, std::vector<SkBitmap>(), | 527 OnDidDownloadFavicon(icon_type, 0, 0, image_url, std::vector<SkBitmap>(), |
| 596 std::vector<gfx::Size>()); | 528 std::vector<gfx::Size>()); |
| 597 return; | 529 return; |
| 598 } | 530 } |
| 599 download_request_.Reset(base::Bind(&FaviconHandler::OnDidDownloadFavicon, | 531 download_request_.Reset(base::Bind(&FaviconHandler::OnDidDownloadFavicon, |
| 600 base::Unretained(this), icon_type)); | 532 base::Unretained(this), icon_type)); |
| 601 // A max bitmap size is specified to avoid receiving huge bitmaps in | 533 // A max bitmap size is specified to avoid receiving huge bitmaps in |
| 602 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() | 534 // OnDidDownloadFavicon(). See FaviconDriver::StartDownload() |
| 603 // for more details about the max bitmap size. | 535 // for more details about the max bitmap size. |
| 604 const int download_id = delegate_->DownloadImage( | 536 const int download_id = |
| 605 image_url, GetMaximalIconSize(icon_type), download_request_.callback()); | 537 delegate_->DownloadImage(image_url, GetMaximalIconSize(handler_type_), |
| 538 download_request_.callback()); | |
| 606 DCHECK_NE(download_id, 0); | 539 DCHECK_NE(download_id, 0); |
| 607 } | 540 } |
| 608 | 541 |
| 609 } // namespace favicon | 542 } // namespace favicon |
| OLD | NEW |