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 "chrome/browser/favicon/favicon_handler.h" | 5 #include "chrome/browser/favicon/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 |
(...skipping 29 matching lines...) Expand all Loading... |
40 // identical and if they match the icon URL and icon type in |favicon_url|. | 40 // identical and if they match the icon URL and icon type in |favicon_url|. |
41 // Returns false if |bitmap_results| is empty. | 41 // Returns false if |bitmap_results| is empty. |
42 bool DoUrlsAndIconsMatch( | 42 bool DoUrlsAndIconsMatch( |
43 const FaviconURL& favicon_url, | 43 const FaviconURL& favicon_url, |
44 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { | 44 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { |
45 if (bitmap_results.empty()) | 45 if (bitmap_results.empty()) |
46 return false; | 46 return false; |
47 | 47 |
48 const favicon_base::IconType icon_type = favicon_url.icon_type; | 48 const favicon_base::IconType icon_type = favicon_url.icon_type; |
49 | 49 |
50 for (size_t i = 0; i < bitmap_results.size(); ++i) { | 50 for (const auto& bitmap_result : bitmap_results) { |
51 if (favicon_url.icon_url != bitmap_results[i].icon_url || | 51 if (favicon_url.icon_url != bitmap_result.icon_url || |
52 icon_type != bitmap_results[i].icon_type) { | 52 icon_type != bitmap_result.icon_type) { |
53 return false; | 53 return false; |
54 } | 54 } |
55 } | 55 } |
56 return true; | 56 return true; |
57 } | 57 } |
58 | 58 |
59 std::string UrlWithoutFragment(const GURL& gurl) { | 59 std::string UrlWithoutFragment(const GURL& gurl) { |
60 GURL::Replacements replacements; | 60 GURL::Replacements replacements; |
61 replacements.ClearRef(); | 61 replacements.ClearRef(); |
62 return gurl.ReplaceComponents(replacements).spec(); | 62 return gurl.ReplaceComponents(replacements).spec(); |
(...skipping 30 matching lines...) Expand all Loading... |
93 return false; | 93 return false; |
94 | 94 |
95 // Check if the favicon for at least one of the scale factors is missing. | 95 // Check if the favicon for at least one of the scale factors is missing. |
96 // |bitmap_results| should always be complete for data inserted by | 96 // |bitmap_results| should always be complete for data inserted by |
97 // FaviconHandler as the FaviconHandler stores favicons resized to all | 97 // FaviconHandler as the FaviconHandler stores favicons resized to all |
98 // of favicon_base::GetFaviconScales() into the history backend. | 98 // of favicon_base::GetFaviconScales() into the history backend. |
99 // Examples of when |bitmap_results| can be incomplete: | 99 // Examples of when |bitmap_results| can be incomplete: |
100 // - Favicons inserted into the history backend by sync. | 100 // - Favicons inserted into the history backend by sync. |
101 // - Favicons for imported bookmarks. | 101 // - Favicons for imported bookmarks. |
102 std::vector<gfx::Size> favicon_sizes; | 102 std::vector<gfx::Size> favicon_sizes; |
103 for (size_t i = 0; i < bitmap_results.size(); ++i) | 103 for (const auto& bitmap_result : bitmap_results) |
104 favicon_sizes.push_back(bitmap_results[i].pixel_size); | 104 favicon_sizes.push_back(bitmap_result.pixel_size); |
105 | 105 |
106 std::vector<float> favicon_scales = favicon_base::GetFaviconScales(); | 106 std::vector<float> favicon_scales = favicon_base::GetFaviconScales(); |
107 for (size_t i = 0; i < favicon_scales.size(); ++i) { | 107 for (float favicon_scale : favicon_scales) { |
108 int edge_size_in_pixel = std::ceil(desired_size_in_dip * favicon_scales[i]); | 108 int edge_size_in_pixel = std::ceil(desired_size_in_dip * favicon_scale); |
109 std::vector<gfx::Size>::iterator it = std::find(favicon_sizes.begin(), | 109 auto it = std::find(favicon_sizes.begin(), favicon_sizes.end(), |
110 favicon_sizes.end(), gfx::Size(edge_size_in_pixel, edge_size_in_pixel)); | 110 gfx::Size(edge_size_in_pixel, edge_size_in_pixel)); |
111 if (it == favicon_sizes.end()) | 111 if (it == favicon_sizes.end()) |
112 return true; | 112 return true; |
113 } | 113 } |
114 return false; | 114 return false; |
115 } | 115 } |
116 | 116 |
117 // Returns true if at least one of |bitmap_results| is valid. | 117 // Returns true if at least one of |bitmap_results| is valid. |
118 bool HasValidResult( | 118 bool HasValidResult( |
119 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { | 119 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results) { |
120 return std::find_if(bitmap_results.begin(), bitmap_results.end(), IsValid) != | 120 return std::find_if(bitmap_results.begin(), bitmap_results.end(), IsValid) != |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 area2 = b2.icon_sizes.front().GetArea(); | 156 area2 = b2.icon_sizes.front().GetArea(); |
157 | 157 |
158 return area1 > area2; | 158 return area1 > area2; |
159 } | 159 } |
160 | 160 |
161 } // namespace | 161 } // namespace |
162 | 162 |
163 //////////////////////////////////////////////////////////////////////////////// | 163 //////////////////////////////////////////////////////////////////////////////// |
164 | 164 |
165 FaviconHandler::DownloadRequest::DownloadRequest() | 165 FaviconHandler::DownloadRequest::DownloadRequest() |
166 : icon_type(favicon_base::INVALID_ICON) {} | 166 : icon_type(favicon_base::INVALID_ICON) { |
| 167 } |
167 | 168 |
168 FaviconHandler::DownloadRequest::~DownloadRequest() { | 169 FaviconHandler::DownloadRequest::~DownloadRequest() { |
169 } | 170 } |
170 | 171 |
171 FaviconHandler::DownloadRequest::DownloadRequest( | 172 FaviconHandler::DownloadRequest::DownloadRequest( |
172 const GURL& url, | 173 const GURL& url, |
173 const GURL& image_url, | 174 const GURL& image_url, |
174 favicon_base::IconType icon_type) | 175 favicon_base::IconType icon_type) |
175 : url(url), image_url(image_url), icon_type(icon_type) {} | 176 : url(url), image_url(image_url), icon_type(icon_type) { |
| 177 } |
176 | 178 |
177 //////////////////////////////////////////////////////////////////////////////// | 179 //////////////////////////////////////////////////////////////////////////////// |
178 | 180 |
179 FaviconHandler::FaviconCandidate::FaviconCandidate() | 181 FaviconHandler::FaviconCandidate::FaviconCandidate() |
180 : score(0), icon_type(favicon_base::INVALID_ICON) {} | 182 : score(0), icon_type(favicon_base::INVALID_ICON) { |
| 183 } |
181 | 184 |
182 FaviconHandler::FaviconCandidate::~FaviconCandidate() { | 185 FaviconHandler::FaviconCandidate::~FaviconCandidate() { |
183 } | 186 } |
184 | 187 |
185 FaviconHandler::FaviconCandidate::FaviconCandidate( | 188 FaviconHandler::FaviconCandidate::FaviconCandidate( |
186 const GURL& url, | 189 const GURL& url, |
187 const GURL& image_url, | 190 const GURL& image_url, |
188 const gfx::Image& image, | 191 const gfx::Image& image, |
189 float score, | 192 float score, |
190 favicon_base::IconType icon_type) | 193 favicon_base::IconType icon_type) |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace); | 320 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace); |
318 | 321 |
319 driver_->OnFaviconAvailable( | 322 driver_->OnFaviconAvailable( |
320 image_with_adjusted_colorspace, icon_url, is_active_favicon); | 323 image_with_adjusted_colorspace, icon_url, is_active_favicon); |
321 } | 324 } |
322 | 325 |
323 void FaviconHandler::OnUpdateFaviconURL( | 326 void FaviconHandler::OnUpdateFaviconURL( |
324 const std::vector<FaviconURL>& candidates) { | 327 const std::vector<FaviconURL>& candidates) { |
325 image_urls_.clear(); | 328 image_urls_.clear(); |
326 best_favicon_candidate_ = FaviconCandidate(); | 329 best_favicon_candidate_ = FaviconCandidate(); |
327 for (std::vector<FaviconURL>::const_iterator i = candidates.begin(); | 330 for (const FaviconURL& candidate : candidates) { |
328 i != candidates.end(); ++i) { | 331 if (!candidate.icon_url.is_empty() && (candidate.icon_type & icon_types_)) |
329 if (!i->icon_url.is_empty() && (i->icon_type & icon_types_)) | 332 image_urls_.push_back(candidate); |
330 image_urls_.push_back(*i); | |
331 } | 333 } |
332 | 334 |
333 if (download_largest_icon_) | 335 if (download_largest_icon_) |
334 SortAndPruneImageUrls(); | 336 SortAndPruneImageUrls(); |
335 | 337 |
336 // TODO(davemoore) Should clear on empty url. Currently we ignore it. | 338 // TODO(davemoore) Should clear on empty url. Currently we ignore it. |
337 // This appears to be what FF does as well. | 339 // This appears to be what FF does as well. |
338 if (!image_urls_.empty()) | 340 if (!image_urls_.empty()) |
339 ProcessCurrentUrl(); | 341 ProcessCurrentUrl(); |
340 } | 342 } |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 // Download ids should be unique. | 665 // Download ids should be unique. |
664 DCHECK(download_requests_.find(download_id) == download_requests_.end()); | 666 DCHECK(download_requests_.find(download_id) == download_requests_.end()); |
665 download_requests_[download_id] = | 667 download_requests_[download_id] = |
666 DownloadRequest(url, image_url, icon_type); | 668 DownloadRequest(url, image_url, icon_type); |
667 } | 669 } |
668 | 670 |
669 return download_id; | 671 return download_id; |
670 } | 672 } |
671 | 673 |
672 void FaviconHandler::SortAndPruneImageUrls() { | 674 void FaviconHandler::SortAndPruneImageUrls() { |
673 for (std::vector<FaviconURL>::iterator i = image_urls_.begin(); | 675 // Not using const-reference since the loop mutates FaviconURL::icon_sizes. |
674 i != image_urls_.end(); ++i) { | 676 for (favicon::FaviconURL& image_url : image_urls_) { |
675 if (i->icon_sizes.empty()) | 677 if (image_url.icon_sizes.empty()) |
676 continue; | 678 continue; |
677 | 679 |
678 gfx::Size largest = i->icon_sizes[GetLargestSizeIndex(i->icon_sizes)]; | 680 gfx::Size largest = |
679 i->icon_sizes.clear(); | 681 image_url.icon_sizes[GetLargestSizeIndex(image_url.icon_sizes)]; |
680 i->icon_sizes.push_back(largest); | 682 image_url.icon_sizes.clear(); |
| 683 image_url.icon_sizes.push_back(largest); |
681 } | 684 } |
682 std::stable_sort(image_urls_.begin(), image_urls_.end(), | 685 std::stable_sort(image_urls_.begin(), image_urls_.end(), |
683 CompareIconSize); | 686 CompareIconSize); |
684 } | 687 } |
OLD | NEW |