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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 } | 94 } |
95 | 95 |
96 void FaviconHandler::FetchFavicon(const GURL& url) { | 96 void FaviconHandler::FetchFavicon(const GURL& url) { |
97 cancelable_consumer_.CancelAllRequests(); | 97 cancelable_consumer_.CancelAllRequests(); |
98 | 98 |
99 url_ = url; | 99 url_ = url; |
100 | 100 |
101 favicon_expired_ = got_favicon_from_history_ = false; | 101 favicon_expired_ = got_favicon_from_history_ = false; |
102 current_url_index_ = 0; | 102 current_url_index_ = 0; |
103 urls_.clear(); | 103 urls_.clear(); |
| 104 favicon_candidates_.clear(); |
104 | 105 |
105 // Request the favicon from the history service. In parallel to this the | 106 // Request the favicon from the history service. In parallel to this the |
106 // renderer is going to notify us (well TabContents) when the favicon url is | 107 // renderer is going to notify us (well TabContents) when the favicon url is |
107 // available. | 108 // available. |
108 if (GetFaviconService()) { | 109 if (GetFaviconService()) { |
109 GetFaviconForURL(url_, icon_types_, &cancelable_consumer_, | 110 GetFaviconForURL(url_, icon_types_, &cancelable_consumer_, |
110 base::Bind(&FaviconHandler::OnFaviconDataForInitialURL, | 111 base::Bind(&FaviconHandler::OnFaviconDataForInitialURL, |
111 base::Unretained(this))); | 112 base::Unretained(this))); |
112 } | 113 } |
113 } | 114 } |
114 | 115 |
115 int FaviconHandler::DownloadImage( | 116 int FaviconHandler::DownloadImage( |
116 const GURL& image_url, | 117 const GURL& image_url, |
117 int image_size, | 118 int image_size, |
118 history::IconType icon_type, | 119 history::IconType icon_type, |
119 const FaviconTabHelper::ImageDownloadCallback& callback) { | 120 const FaviconTabHelper::ImageDownloadCallback& callback) { |
120 return ScheduleDownload(GURL(), image_url, image_size, icon_type, callback); | 121 return ScheduleDownload(GURL(), image_url, image_size, icon_type, callback); |
121 } | 122 } |
122 | 123 |
123 FaviconService* FaviconHandler::GetFaviconService() { | 124 FaviconService* FaviconHandler::GetFaviconService() { |
124 return profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); | 125 return profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); |
125 } | 126 } |
126 | 127 |
| 128 void FaviconHandler::AddFaviconCandidate( |
| 129 const GURL& url, |
| 130 const GURL& image_url, |
| 131 const gfx::Image& image, |
| 132 history::IconType icon_type) { |
| 133 FaviconCandidate candidate; |
| 134 candidate.url = url; |
| 135 candidate.image_url = image_url; |
| 136 candidate.image = image; |
| 137 candidate.icon_type = icon_type; |
| 138 favicon_candidates_.push_back(candidate); |
| 139 } |
| 140 |
| 141 void FaviconHandler::SetFaviconFromCandidates() { |
| 142 if (favicon_candidates_.empty()) |
| 143 return; |
| 144 const int preferred_size = preferred_icon_size(); |
| 145 FaviconCandidates::iterator match; |
| 146 if (preferred_size == 0) { |
| 147 // No size preference, use the first icon. |
| 148 match = favicon_candidates_.begin(); |
| 149 } else { |
| 150 // Use the icon closest to preferred_, favoring sizes >= preferred_size. |
| 151 int match_size = -1; |
| 152 for (FaviconCandidates::iterator iter = favicon_candidates_.begin(); |
| 153 iter != favicon_candidates_.end(); ++iter) { |
| 154 FaviconCandidate& candidate = *iter; |
| 155 SkBitmap bitmap = *candidate.image.ToSkBitmap(); |
| 156 int bitmap_size = std::max(bitmap.width(), bitmap.height()); |
| 157 if (match_size < 0 || |
| 158 (bitmap_size >= preferred_size && bitmap_size < match_size) || |
| 159 (match_size < preferred_size && bitmap_size > match_size)) { |
| 160 match = iter; |
| 161 } |
| 162 } |
| 163 } |
| 164 SetFavicon(match->url, match->image_url, match->image, match->icon_type); |
| 165 favicon_candidates_.clear(); |
| 166 } |
| 167 |
127 void FaviconHandler::SetFavicon( | 168 void FaviconHandler::SetFavicon( |
128 const GURL& url, | 169 const GURL& url, |
129 const GURL& image_url, | 170 const GURL& image_url, |
130 const gfx::Image& image, | 171 const gfx::Image& image, |
131 history::IconType icon_type) { | 172 history::IconType icon_type) { |
132 const SkBitmap& bitmap = image; | 173 SkBitmap bitmap = *image.ToSkBitmap(); |
133 const gfx::Image& sized_image = (preferred_icon_size() == 0 || | 174 const gfx::Image& sized_image = (preferred_icon_size() == 0 || |
134 (preferred_icon_size() == bitmap.width() && | 175 (preferred_icon_size() == bitmap.width() && |
135 preferred_icon_size() == bitmap.height())) ? | 176 preferred_icon_size() == bitmap.height())) ? |
136 image : ResizeFaviconIfNeeded(image); | 177 image : ResizeFaviconIfNeeded(image); |
137 | 178 |
138 if (GetFaviconService() && ShouldSaveFavicon(url)) { | 179 if (GetFaviconService() && ShouldSaveFavicon(url)) { |
139 std::vector<unsigned char> image_data; | 180 std::vector<unsigned char> image_data; |
140 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) | 181 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) |
141 SetHistoryFavicon(url, image_url, image_data, icon_type); | 182 SetHistoryFavicon(url, image_url, image_data, icon_type); |
142 } | 183 } |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 } | 270 } |
230 | 271 |
231 if (!i->second.callback.is_null()) { | 272 if (!i->second.callback.is_null()) { |
232 i->second.callback.Run(id, errored, *(&image)); | 273 i->second.callback.Run(id, errored, *(&image)); |
233 } else if (current_candidate() && | 274 } else if (current_candidate() && |
234 DoUrlAndIconMatch(*current_candidate(), image_url, | 275 DoUrlAndIconMatch(*current_candidate(), image_url, |
235 i->second.icon_type)) { | 276 i->second.icon_type)) { |
236 // The downloaded icon is still valid when there is no FaviconURL update | 277 // The downloaded icon is still valid when there is no FaviconURL update |
237 // during the downloading. | 278 // during the downloading. |
238 if (!errored) { | 279 if (!errored) { |
239 SetFavicon(i->second.url, image_url, image, i->second.icon_type); | 280 AddFaviconCandidate(i->second.url, image_url, image, i->second.icon_type); |
240 } else if (GetEntry() && ++current_url_index_ < urls_.size()) { | 281 } |
241 // Copies all candidate except first one and notifies the FaviconHandler, | 282 if ((errored || preferred_icon_size() != 0) && |
| 283 (GetEntry() && ++current_url_index_ < urls_.size())) { |
| 284 // Copies all candidates except the first and notifies the FaviconHandler, |
242 // so the next candidate can be processed. | 285 // so the next candidate can be processed. |
243 std::vector<FaviconURL> new_candidates(urls_.begin() + 1, urls_.end()); | 286 std::vector<FaviconURL> new_candidates(urls_.begin() + 1, urls_.end()); |
244 OnUpdateFaviconURL(0, new_candidates); | 287 OnUpdateFaviconURL(0, new_candidates); |
| 288 } else { |
| 289 // We have all candidates, choose one and set the favicon. |
| 290 SetFaviconFromCandidates(); |
245 } | 291 } |
246 } | 292 } |
247 download_requests_.erase(i); | 293 download_requests_.erase(i); |
248 } | 294 } |
249 | 295 |
250 NavigationEntry* FaviconHandler::GetEntry() { | 296 NavigationEntry* FaviconHandler::GetEntry() { |
251 NavigationEntry* entry = delegate_->GetActiveEntry(); | 297 NavigationEntry* entry = delegate_->GetActiveEntry(); |
252 if (entry && entry->GetURL() == url_) | 298 if (entry && entry->GetURL() == url_) |
253 return entry; | 299 return entry; |
254 | 300 |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 if (width > 0 && height > 0) { | 497 if (width > 0 && height > 0) { |
452 gfx::CalculateFaviconTargetSize(&width, &height); | 498 gfx::CalculateFaviconTargetSize(&width, &height); |
453 return gfx::Image(new SkBitmap( | 499 return gfx::Image(new SkBitmap( |
454 skia::ImageOperations::Resize( | 500 skia::ImageOperations::Resize( |
455 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | 501 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, |
456 width, height))); | 502 width, height))); |
457 } | 503 } |
458 | 504 |
459 return image; | 505 return image; |
460 } | 506 } |
OLD | NEW |