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 "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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 44 | 44 |
| 45 bool DoUrlAndIconMatch(const FaviconURL& favicon_url, | 45 bool DoUrlAndIconMatch(const FaviconURL& favicon_url, |
| 46 const GURL& url, | 46 const GURL& url, |
| 47 history::IconType icon_type) { | 47 history::IconType icon_type) { |
| 48 return favicon_url.icon_url == url && | 48 return favicon_url.icon_url == url && |
| 49 favicon_url.icon_type == static_cast<FaviconURL::IconType>(icon_type); | 49 favicon_url.icon_type == static_cast<FaviconURL::IconType>(icon_type); |
| 50 } | 50 } |
| 51 | 51 |
| 52 } // namespace | 52 } // namespace |
| 53 | 53 |
| 54 //////////////////////////////////////////////////////////////////////////////// | |
| 55 | |
| 54 FaviconHandler::DownloadRequest::DownloadRequest() | 56 FaviconHandler::DownloadRequest::DownloadRequest() |
| 55 : icon_type(history::INVALID_ICON) { | 57 : icon_type(history::INVALID_ICON) { |
| 56 } | 58 } |
| 57 | 59 |
| 58 FaviconHandler::DownloadRequest::~DownloadRequest() { | 60 FaviconHandler::DownloadRequest::~DownloadRequest() { |
| 59 } | 61 } |
| 60 | 62 |
| 61 FaviconHandler::DownloadRequest::DownloadRequest( | 63 FaviconHandler::DownloadRequest::DownloadRequest( |
| 62 const GURL& url, | 64 const GURL& url, |
| 63 const GURL& image_url, | 65 const GURL& image_url, |
| 64 const FaviconTabHelper::ImageDownloadCallback& callback, | 66 const FaviconTabHelper::ImageDownloadCallback& callback, |
| 65 history::IconType icon_type) | 67 history::IconType icon_type) |
| 66 : url(url), | 68 : url(url), |
| 67 image_url(image_url), | 69 image_url(image_url), |
| 68 callback(callback), | 70 callback(callback), |
| 69 icon_type(icon_type) { | 71 icon_type(icon_type) { |
| 70 } | 72 } |
| 71 | 73 |
| 74 //////////////////////////////////////////////////////////////////////////////// | |
| 75 | |
| 76 FaviconHandler::FaviconCandidate::FaviconCandidate() | |
| 77 : icon_type(history::INVALID_ICON) { | |
| 78 } | |
| 79 | |
| 80 FaviconHandler::FaviconCandidate::~FaviconCandidate() { | |
| 81 } | |
| 82 | |
| 83 FaviconHandler::FaviconCandidate::FaviconCandidate( | |
| 84 const GURL& url, | |
| 85 const GURL& image_url, | |
| 86 const gfx::Image& image, | |
| 87 history::IconType icon_type) | |
| 88 : url(url), | |
| 89 image_url(image_url), | |
| 90 image(image), | |
| 91 icon_type(icon_type) { | |
| 92 } | |
| 93 | |
| 94 //////////////////////////////////////////////////////////////////////////////// | |
| 95 | |
| 72 FaviconHandler::FaviconHandler(Profile* profile, | 96 FaviconHandler::FaviconHandler(Profile* profile, |
| 73 FaviconHandlerDelegate* delegate, | 97 FaviconHandlerDelegate* delegate, |
| 74 Type icon_type) | 98 Type icon_type) |
| 75 : got_favicon_from_history_(false), | 99 : got_favicon_from_history_(false), |
| 76 favicon_expired_(false), | 100 favicon_expired_(false), |
| 77 icon_types_(icon_type == FAVICON ? history::FAVICON : | 101 icon_types_(icon_type == FAVICON ? history::FAVICON : |
| 78 history::TOUCH_ICON | history::TOUCH_PRECOMPOSED_ICON), | 102 history::TOUCH_ICON | history::TOUCH_PRECOMPOSED_ICON), |
| 79 current_url_index_(0), | 103 current_url_index_(0), |
| 80 profile_(profile), | 104 profile_(profile), |
| 81 delegate_(delegate) { | 105 delegate_(delegate) { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 94 } | 118 } |
| 95 | 119 |
| 96 void FaviconHandler::FetchFavicon(const GURL& url) { | 120 void FaviconHandler::FetchFavicon(const GURL& url) { |
| 97 cancelable_consumer_.CancelAllRequests(); | 121 cancelable_consumer_.CancelAllRequests(); |
| 98 | 122 |
| 99 url_ = url; | 123 url_ = url; |
| 100 | 124 |
| 101 favicon_expired_ = got_favicon_from_history_ = false; | 125 favicon_expired_ = got_favicon_from_history_ = false; |
| 102 current_url_index_ = 0; | 126 current_url_index_ = 0; |
| 103 urls_.clear(); | 127 urls_.clear(); |
| 128 favicon_candidates_.clear(); | |
| 104 | 129 |
| 105 // Request the favicon from the history service. In parallel to this the | 130 // 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 | 131 // renderer is going to notify us (well TabContents) when the favicon url is |
| 107 // available. | 132 // available. |
| 108 if (GetFaviconService()) { | 133 if (GetFaviconService()) { |
| 109 GetFaviconForURL(url_, icon_types_, &cancelable_consumer_, | 134 GetFaviconForURL(url_, icon_types_, &cancelable_consumer_, |
| 110 base::Bind(&FaviconHandler::OnFaviconDataForInitialURL, | 135 base::Bind(&FaviconHandler::OnFaviconDataForInitialURL, |
| 111 base::Unretained(this))); | 136 base::Unretained(this))); |
| 112 } | 137 } |
| 113 } | 138 } |
| 114 | 139 |
| 115 int FaviconHandler::DownloadImage( | 140 int FaviconHandler::DownloadImage( |
| 116 const GURL& image_url, | 141 const GURL& image_url, |
| 117 int image_size, | 142 int image_size, |
| 118 history::IconType icon_type, | 143 history::IconType icon_type, |
| 119 const FaviconTabHelper::ImageDownloadCallback& callback) { | 144 const FaviconTabHelper::ImageDownloadCallback& callback) { |
| 120 return ScheduleDownload(GURL(), image_url, image_size, icon_type, callback); | 145 return ScheduleDownload(GURL(), image_url, image_size, icon_type, callback); |
| 121 } | 146 } |
| 122 | 147 |
| 123 FaviconService* FaviconHandler::GetFaviconService() { | 148 FaviconService* FaviconHandler::GetFaviconService() { |
| 124 return profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); | 149 return profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); |
| 125 } | 150 } |
| 126 | 151 |
| 152 void FaviconHandler::SetFaviconFromCandidates() { | |
| 153 if (favicon_candidates_.empty()) | |
| 154 return; | |
| 155 const int preferred_size = preferred_icon_size(); | |
| 156 FaviconCandidates::iterator match; | |
| 157 if (preferred_size == 0) { | |
| 158 // No size preference, use the first icon. | |
| 159 match = favicon_candidates_.begin(); | |
| 160 } else { | |
| 161 // Use the icon closest to preferred_size, favoring sizes >= preferred_size. | |
| 162 int match_size = -1; | |
| 163 for (FaviconCandidates::iterator iter = favicon_candidates_.begin(); | |
| 164 iter != favicon_candidates_.end(); ++iter) { | |
| 165 FaviconCandidate& candidate = *iter; | |
| 166 SkBitmap bitmap = *candidate.image.ToSkBitmap(); | |
| 167 int bitmap_size = std::max(bitmap.width(), bitmap.height()); | |
| 168 if (match_size < 0 || | |
| 169 (bitmap_size >= preferred_size && bitmap_size < match_size) || | |
| 170 (match_size < preferred_size && bitmap_size > match_size)) { | |
| 171 match = iter; | |
| 172 match_size = bitmap_size; | |
| 173 if (bitmap_size == preferred_size) | |
| 174 break; | |
| 175 } | |
| 176 } | |
| 177 } | |
| 178 SetFavicon(match->url, match->image_url, match->image, match->icon_type); | |
| 179 favicon_candidates_.clear(); | |
| 180 } | |
| 181 | |
| 127 void FaviconHandler::SetFavicon( | 182 void FaviconHandler::SetFavicon( |
| 128 const GURL& url, | 183 const GURL& url, |
| 129 const GURL& image_url, | 184 const GURL& image_url, |
| 130 const gfx::Image& image, | 185 const gfx::Image& image, |
| 131 history::IconType icon_type) { | 186 history::IconType icon_type) { |
| 132 const SkBitmap& bitmap = image; | 187 SkBitmap bitmap = *image.ToSkBitmap(); |
| 133 const gfx::Image& sized_image = (preferred_icon_size() == 0 || | 188 const gfx::Image& sized_image = (preferred_icon_size() == 0 || |
| 134 (preferred_icon_size() == bitmap.width() && | 189 (preferred_icon_size() == bitmap.width() && |
| 135 preferred_icon_size() == bitmap.height())) ? | 190 preferred_icon_size() == bitmap.height())) ? |
| 136 image : ResizeFaviconIfNeeded(image); | 191 image : ResizeFaviconIfNeeded(image); |
| 137 | 192 |
| 138 if (GetFaviconService() && ShouldSaveFavicon(url)) { | 193 if (GetFaviconService() && ShouldSaveFavicon(url)) { |
| 139 std::vector<unsigned char> image_data; | 194 std::vector<unsigned char> image_data; |
| 140 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) | 195 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) |
| 141 SetHistoryFavicon(url, image_url, image_data, icon_type); | 196 SetHistoryFavicon(url, image_url, image_data, icon_type); |
| 142 } | 197 } |
| 143 | 198 |
| 144 if (url == url_ && icon_type == history::FAVICON) { | 199 if (url == url_ && icon_type == history::FAVICON) { |
| 145 NavigationEntry* entry = GetEntry(); | 200 NavigationEntry* entry = GetEntry(); |
| 146 if (entry) | 201 if (entry) { |
| 202 entry->GetFavicon().url = image_url; | |
| 147 UpdateFavicon(entry, &sized_image); | 203 UpdateFavicon(entry, &sized_image); |
| 204 } | |
| 148 } | 205 } |
| 149 } | 206 } |
| 150 | 207 |
| 151 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, | 208 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, |
| 152 scoped_refptr<RefCountedMemory> data) { | 209 scoped_refptr<RefCountedMemory> data) { |
| 153 scoped_ptr<gfx::Image> image(gfx::ImageFromPNGEncodedData(data->front(), | 210 scoped_ptr<gfx::Image> image(gfx::ImageFromPNGEncodedData(data->front(), |
| 154 data->size())); | 211 data->size())); |
| 155 UpdateFavicon(entry, image.get()); | 212 UpdateFavicon(entry, image.get()); |
| 156 } | 213 } |
| 157 | 214 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 } | 286 } |
| 230 | 287 |
| 231 if (!i->second.callback.is_null()) { | 288 if (!i->second.callback.is_null()) { |
| 232 i->second.callback.Run(id, errored, *(&image)); | 289 i->second.callback.Run(id, errored, *(&image)); |
| 233 } else if (current_candidate() && | 290 } else if (current_candidate() && |
| 234 DoUrlAndIconMatch(*current_candidate(), image_url, | 291 DoUrlAndIconMatch(*current_candidate(), image_url, |
| 235 i->second.icon_type)) { | 292 i->second.icon_type)) { |
| 236 // The downloaded icon is still valid when there is no FaviconURL update | 293 // The downloaded icon is still valid when there is no FaviconURL update |
| 237 // during the downloading. | 294 // during the downloading. |
| 238 if (!errored) { | 295 if (!errored) { |
| 239 SetFavicon(i->second.url, image_url, image, i->second.icon_type); | 296 favicon_candidates_.push_back(FaviconCandidate( |
| 240 } else if (GetEntry() && ++current_url_index_ < urls_.size()) { | 297 i->second.url, image_url, image, i->second.icon_type)); |
|
michaelbai
2012/03/14 22:34:14
Would you like stop fetch the rest of icons if the
stevenjb
2012/03/15 00:45:53
Yes that would be better. I thought of this but di
michaelbai
2012/03/15 01:12:00
It seemed you don't need favicon_candidates_ vecto
stevenjb
2012/03/15 21:05:10
That's a fair point. I did another round of re-fac
| |
| 241 // Copies all candidate except first one and notifies the FaviconHandler, | 298 } |
| 299 if ((errored || preferred_icon_size() != 0) && | |
| 300 (GetEntry() && ++current_url_index_ < urls_.size())) { | |
| 301 // Copies all candidates except the first and notifies the FaviconHandler, | |
| 242 // so the next candidate can be processed. | 302 // so the next candidate can be processed. |
| 243 std::vector<FaviconURL> new_candidates(urls_.begin() + 1, urls_.end()); | 303 std::vector<FaviconURL> new_candidates(urls_.begin() + 1, urls_.end()); |
| 244 OnUpdateFaviconURL(0, new_candidates); | 304 OnUpdateFaviconURL(0, new_candidates); |
| 305 } else { | |
| 306 // We have all candidates, choose one and set the favicon. | |
| 307 SetFaviconFromCandidates(); | |
| 245 } | 308 } |
| 246 } | 309 } |
| 247 download_requests_.erase(i); | 310 download_requests_.erase(i); |
| 248 } | 311 } |
| 249 | 312 |
| 250 NavigationEntry* FaviconHandler::GetEntry() { | 313 NavigationEntry* FaviconHandler::GetEntry() { |
| 251 NavigationEntry* entry = delegate_->GetActiveEntry(); | 314 NavigationEntry* entry = delegate_->GetActiveEntry(); |
| 252 if (entry && entry->GetURL() == url_) | 315 if (entry && entry->GetURL() == url_) |
| 253 return entry; | 316 return entry; |
| 254 | 317 |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 451 if (width > 0 && height > 0) { | 514 if (width > 0 && height > 0) { |
| 452 gfx::CalculateFaviconTargetSize(&width, &height); | 515 gfx::CalculateFaviconTargetSize(&width, &height); |
| 453 return gfx::Image(new SkBitmap( | 516 return gfx::Image(new SkBitmap( |
| 454 skia::ImageOperations::Resize( | 517 skia::ImageOperations::Resize( |
| 455 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | 518 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, |
| 456 width, height))); | 519 width, height))); |
| 457 } | 520 } |
| 458 | 521 |
| 459 return image; | 522 return image; |
| 460 } | 523 } |
| OLD | NEW |