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 "build/build_config.h" | 7 #include "build/build_config.h" |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 history::IconType icon_type) | 78 history::IconType icon_type) |
79 : url(url), | 79 : url(url), |
80 image_url(image_url), | 80 image_url(image_url), |
81 callback(callback), | 81 callback(callback), |
82 icon_type(icon_type) { | 82 icon_type(icon_type) { |
83 } | 83 } |
84 | 84 |
85 //////////////////////////////////////////////////////////////////////////////// | 85 //////////////////////////////////////////////////////////////////////////////// |
86 | 86 |
87 FaviconHandler::FaviconCandidate::FaviconCandidate() | 87 FaviconHandler::FaviconCandidate::FaviconCandidate() |
88 : bitmap_size(0), | 88 : score(0), |
89 icon_type(history::INVALID_ICON) { | 89 icon_type(history::INVALID_ICON) { |
90 } | 90 } |
91 | 91 |
92 FaviconHandler::FaviconCandidate::~FaviconCandidate() { | 92 FaviconHandler::FaviconCandidate::~FaviconCandidate() { |
93 } | 93 } |
94 | 94 |
95 FaviconHandler::FaviconCandidate::FaviconCandidate( | 95 FaviconHandler::FaviconCandidate::FaviconCandidate( |
96 const GURL& url, | 96 const GURL& url, |
97 const GURL& image_url, | 97 const GURL& image_url, |
98 const gfx::Image& image, | 98 const gfx::Image& image, |
99 int bitmap_size, | 99 float score, |
100 history::IconType icon_type) | 100 history::IconType icon_type) |
101 : url(url), | 101 : url(url), |
102 image_url(image_url), | 102 image_url(image_url), |
103 image(image), | 103 image(image), |
104 bitmap_size(bitmap_size), | 104 score(score), |
105 icon_type(icon_type) { | 105 icon_type(icon_type) { |
106 } | 106 } |
107 | 107 |
108 //////////////////////////////////////////////////////////////////////////////// | 108 //////////////////////////////////////////////////////////////////////////////// |
109 | 109 |
110 FaviconHandler::FaviconHandler(Profile* profile, | 110 FaviconHandler::FaviconHandler(Profile* profile, |
111 FaviconHandlerDelegate* delegate, | 111 FaviconHandlerDelegate* delegate, |
112 Type icon_type) | 112 Type icon_type) |
113 : got_favicon_from_history_(false), | 113 : got_favicon_from_history_(false), |
114 favicon_expired_(false), | 114 favicon_expired_(false), |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 return ScheduleDownload(GURL(), image_url, image_size, icon_type, callback); | 156 return ScheduleDownload(GURL(), image_url, image_size, icon_type, callback); |
157 } | 157 } |
158 | 158 |
159 FaviconService* FaviconHandler::GetFaviconService() { | 159 FaviconService* FaviconHandler::GetFaviconService() { |
160 return profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); | 160 return profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); |
161 } | 161 } |
162 | 162 |
163 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url, | 163 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url, |
164 const GURL& image_url, | 164 const GURL& image_url, |
165 const gfx::Image& image, | 165 const gfx::Image& image, |
| 166 float score, |
166 history::IconType icon_type) { | 167 history::IconType icon_type) { |
167 bool update_candidate = false; | 168 bool update_candidate = false; |
168 SkBitmap bitmap = *(image.ToSkBitmap()); | 169 SkBitmap bitmap = *(image.ToSkBitmap()); |
169 int bitmap_size = std::max(bitmap.width(), bitmap.height()); | 170 bool exact_match = score == 1; |
170 bool exact_match = (bitmap_size == preferred_icon_size()); | |
171 if (preferred_icon_size() == 0) { | 171 if (preferred_icon_size() == 0) { |
172 // No preferred size, use this icon. | 172 // No preferred size, use this icon. |
173 update_candidate = true; | 173 update_candidate = true; |
174 exact_match = true; | 174 exact_match = true; |
175 } else if (favicon_candidate_.icon_type == history::INVALID_ICON) { | 175 } else if (favicon_candidate_.icon_type == history::INVALID_ICON) { |
176 // No current candidate, use this. | 176 // No current candidate, use this. |
177 update_candidate = true; | 177 update_candidate = true; |
178 } else { | 178 } else { |
179 if (bitmap_size == preferred_icon_size()) { | 179 if (exact_match) { |
180 // Exact match, use this. | 180 // Exact match, use this. |
181 update_candidate = true; | 181 update_candidate = true; |
182 } else { | 182 } else { |
183 // Compare against current candidate. | 183 // Compare against current candidate. |
184 int cur_size = favicon_candidate_.bitmap_size; | 184 if (score > favicon_candidate_.score) |
185 if ((bitmap_size >= preferred_icon_size() && bitmap_size < cur_size) || | |
186 (cur_size < preferred_icon_size() && bitmap_size > cur_size)) { | |
187 update_candidate = true; | 185 update_candidate = true; |
188 } | |
189 } | 186 } |
190 } | 187 } |
191 if (update_candidate) { | 188 if (update_candidate) { |
192 favicon_candidate_ = FaviconCandidate( | 189 favicon_candidate_ = FaviconCandidate( |
193 url, image_url, image, bitmap_size, icon_type); | 190 url, image_url, image, score, icon_type); |
194 } | 191 } |
195 return exact_match; | 192 return exact_match; |
196 } | 193 } |
197 | 194 |
198 void FaviconHandler::SetFavicon( | 195 void FaviconHandler::SetFavicon( |
199 const GURL& url, | 196 const GURL& url, |
200 const GURL& image_url, | 197 const GURL& image_url, |
201 const gfx::Image& image, | 198 const gfx::Image& image, |
202 history::IconType icon_type) { | 199 history::IconType icon_type) { |
203 SkBitmap bitmap = *(image.ToSkBitmap()); | 200 SkBitmap bitmap = *(image.ToSkBitmap()); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 } | 284 } |
288 | 285 |
289 if (got_favicon_from_history_) | 286 if (got_favicon_from_history_) |
290 DownloadFaviconOrAskHistory(entry->GetURL(), current_candidate()->icon_url, | 287 DownloadFaviconOrAskHistory(entry->GetURL(), current_candidate()->icon_url, |
291 ToHistoryIconType(current_candidate()->icon_type)); | 288 ToHistoryIconType(current_candidate()->icon_type)); |
292 } | 289 } |
293 | 290 |
294 void FaviconHandler::OnDidDownloadFavicon(int id, | 291 void FaviconHandler::OnDidDownloadFavicon(int id, |
295 const GURL& image_url, | 292 const GURL& image_url, |
296 bool errored, | 293 bool errored, |
297 const gfx::Image& image) { | 294 const gfx::Image& image, |
| 295 float score) { |
298 DownloadRequests::iterator i = download_requests_.find(id); | 296 DownloadRequests::iterator i = download_requests_.find(id); |
299 if (i == download_requests_.end()) { | 297 if (i == download_requests_.end()) { |
300 // Currently WebContents notifies us of ANY downloads so that it is | 298 // Currently WebContents notifies us of ANY downloads so that it is |
301 // possible to get here. | 299 // possible to get here. |
302 return; | 300 return; |
303 } | 301 } |
304 | 302 |
305 if (!i->second.callback.is_null()) { | 303 if (!i->second.callback.is_null()) { |
306 i->second.callback.Run(id, errored, *image.ToSkBitmap()); | 304 i->second.callback.Run(id, errored, *image.ToSkBitmap()); |
307 } else if (current_candidate() && | 305 } else if (current_candidate() && |
308 DoUrlAndIconMatch(*current_candidate(), image_url, | 306 DoUrlAndIconMatch(*current_candidate(), image_url, |
309 i->second.icon_type)) { | 307 i->second.icon_type)) { |
310 // The downloaded icon is still valid when there is no FaviconURL update | 308 // The downloaded icon is still valid when there is no FaviconURL update |
311 // during the downloading. | 309 // during the downloading. |
312 bool request_next_icon = true; | 310 bool request_next_icon = true; |
313 if (!errored) { | 311 if (!errored) { |
314 request_next_icon = !UpdateFaviconCandidate( | 312 request_next_icon = !UpdateFaviconCandidate( |
315 i->second.url, image_url, image, i->second.icon_type); | 313 i->second.url, image_url, image, score, i->second.icon_type); |
316 } | 314 } |
317 if (request_next_icon && GetEntry() && image_urls_.size() > 1) { | 315 if (request_next_icon && GetEntry() && image_urls_.size() > 1) { |
318 // Remove the first member of image_urls_ and process the remaining. | 316 // Remove the first member of image_urls_ and process the remaining. |
319 image_urls_.pop_front(); | 317 image_urls_.pop_front(); |
320 ProcessCurrentUrl(); | 318 ProcessCurrentUrl(); |
321 } else if (favicon_candidate_.icon_type != history::INVALID_ICON) { | 319 } else if (favicon_candidate_.icon_type != history::INVALID_ICON) { |
322 // No more icons to request, set the favicon from the candidate. | 320 // No more icons to request, set the favicon from the candidate. |
323 SetFavicon(favicon_candidate_.url, | 321 SetFavicon(favicon_candidate_.url, |
324 favicon_candidate_.image_url, | 322 favicon_candidate_.image_url, |
325 favicon_candidate_.image, | 323 favicon_candidate_.image, |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 int height = bitmap.height(); | 532 int height = bitmap.height(); |
535 if (width > 0 && height > 0) { | 533 if (width > 0 && height > 0) { |
536 gfx::CalculateFaviconTargetSize(&width, &height); | 534 gfx::CalculateFaviconTargetSize(&width, &height); |
537 return gfx::Image(skia::ImageOperations::Resize( | 535 return gfx::Image(skia::ImageOperations::Resize( |
538 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | 536 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, |
539 width, height)); | 537 width, height)); |
540 } | 538 } |
541 | 539 |
542 return image; | 540 return image; |
543 } | 541 } |
OLD | NEW |