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