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 const SkBitmap& bitmap, | |
| 88 history::IconType icon_type) | |
| 89 : url(url), | |
| 90 image_url(image_url), | |
| 91 image(image), | |
| 92 bitmap(bitmap), | |
| 93 icon_type(icon_type) { | |
| 94 } | |
| 95 | |
| 96 //////////////////////////////////////////////////////////////////////////////// | |
| 97 | |
| 72 FaviconHandler::FaviconHandler(Profile* profile, | 98 FaviconHandler::FaviconHandler(Profile* profile, |
| 73 FaviconHandlerDelegate* delegate, | 99 FaviconHandlerDelegate* delegate, |
| 74 Type icon_type) | 100 Type icon_type) |
| 75 : got_favicon_from_history_(false), | 101 : got_favicon_from_history_(false), |
| 76 favicon_expired_(false), | 102 favicon_expired_(false), |
| 77 icon_types_(icon_type == FAVICON ? history::FAVICON : | 103 icon_types_(icon_type == FAVICON ? history::FAVICON : |
| 78 history::TOUCH_ICON | history::TOUCH_PRECOMPOSED_ICON), | 104 history::TOUCH_ICON | history::TOUCH_PRECOMPOSED_ICON), |
| 79 current_url_index_(0), | |
| 80 profile_(profile), | 105 profile_(profile), |
| 81 delegate_(delegate) { | 106 delegate_(delegate) { |
| 82 DCHECK(profile_); | 107 DCHECK(profile_); |
| 83 DCHECK(delegate_); | 108 DCHECK(delegate_); |
| 84 } | 109 } |
| 85 | 110 |
| 86 FaviconHandler::~FaviconHandler() { | 111 FaviconHandler::~FaviconHandler() { |
| 87 // Call pending download callbacks with error to allow caller to clean up. | 112 // Call pending download callbacks with error to allow caller to clean up. |
| 88 for (DownloadRequests::iterator i = download_requests_.begin(); | 113 for (DownloadRequests::iterator i = download_requests_.begin(); |
| 89 i != download_requests_.end(); ++i) { | 114 i != download_requests_.end(); ++i) { |
| 90 if (!i->second.callback.is_null()) { | 115 if (!i->second.callback.is_null()) { |
| 91 i->second.callback.Run(i->first, true, SkBitmap()); | 116 i->second.callback.Run(i->first, true, SkBitmap()); |
| 92 } | 117 } |
| 93 } | 118 } |
| 94 } | 119 } |
| 95 | 120 |
| 96 void FaviconHandler::FetchFavicon(const GURL& url) { | 121 void FaviconHandler::FetchFavicon(const GURL& url) { |
| 97 cancelable_consumer_.CancelAllRequests(); | 122 cancelable_consumer_.CancelAllRequests(); |
| 98 | 123 |
| 99 url_ = url; | 124 url_ = url; |
| 100 | 125 |
| 101 favicon_expired_ = got_favicon_from_history_ = false; | 126 favicon_expired_ = got_favicon_from_history_ = false; |
| 102 current_url_index_ = 0; | |
| 103 urls_.clear(); | |
| 104 | 127 |
| 105 // Request the favicon from the history service. In parallel to this the | 128 // 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 | 129 // renderer is going to notify us (well TabContents) when the favicon url is |
| 107 // available. | 130 // available. |
| 108 if (GetFaviconService()) { | 131 if (GetFaviconService()) { |
| 109 GetFaviconForURL(url_, icon_types_, &cancelable_consumer_, | 132 GetFaviconForURL(url_, icon_types_, &cancelable_consumer_, |
| 110 base::Bind(&FaviconHandler::OnFaviconDataForInitialURL, | 133 base::Bind(&FaviconHandler::OnFaviconDataForInitialURL, |
| 111 base::Unretained(this))); | 134 base::Unretained(this))); |
| 112 } | 135 } |
| 113 } | 136 } |
| 114 | 137 |
| 115 int FaviconHandler::DownloadImage( | 138 int FaviconHandler::DownloadImage( |
| 116 const GURL& image_url, | 139 const GURL& image_url, |
| 117 int image_size, | 140 int image_size, |
| 118 history::IconType icon_type, | 141 history::IconType icon_type, |
| 119 const FaviconTabHelper::ImageDownloadCallback& callback) { | 142 const FaviconTabHelper::ImageDownloadCallback& callback) { |
| 120 return ScheduleDownload(GURL(), image_url, image_size, icon_type, callback); | 143 return ScheduleDownload(GURL(), image_url, image_size, icon_type, callback); |
| 121 } | 144 } |
| 122 | 145 |
| 123 FaviconService* FaviconHandler::GetFaviconService() { | 146 FaviconService* FaviconHandler::GetFaviconService() { |
| 124 return profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); | 147 return profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); |
| 125 } | 148 } |
| 126 | 149 |
| 150 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url, | |
| 151 const GURL& image_url, | |
| 152 const gfx::Image& image, | |
| 153 history::IconType icon_type) { | |
| 154 bool update_candidate = false; | |
| 155 bool exact_match = false; | |
| 156 SkBitmap bitmap = *(image.ToSkBitmap()); | |
| 157 if (preferred_icon_size() == 0) { | |
| 158 update_candidate = true; | |
| 159 exact_match = true; | |
| 160 } else if (favicon_candidate_.icon_type == history::INVALID_ICON) { | |
| 161 // No current candidate, use this. | |
| 162 update_candidate = true; | |
| 163 } else { | |
| 164 int bitmap_size = std::max(bitmap.width(), bitmap.height()); | |
| 165 if (bitmap_size == preferred_icon_size()) { | |
| 166 // Exact match, use this. | |
| 167 update_candidate = true; | |
| 168 exact_match = true; | |
| 169 } else { | |
| 170 // Compare against current candidate. | |
| 171 int cur_size = std::max(favicon_candidate_.bitmap.width(), | |
| 172 favicon_candidate_.bitmap.height()); | |
| 173 if ((bitmap_size >= preferred_icon_size() && bitmap_size < cur_size) || | |
| 174 (cur_size < preferred_icon_size() && bitmap_size > cur_size)) { | |
| 175 update_candidate = true; | |
| 176 } | |
| 177 } | |
| 178 } | |
| 179 if (update_candidate) { | |
| 180 favicon_candidate_ = FaviconCandidate( | |
| 181 url, image_url, image, bitmap, icon_type); | |
| 182 } | |
| 183 return exact_match; | |
| 184 } | |
| 185 | |
| 127 void FaviconHandler::SetFavicon( | 186 void FaviconHandler::SetFavicon( |
| 128 const GURL& url, | 187 const GURL& url, |
| 129 const GURL& image_url, | 188 const GURL& image_url, |
| 130 const gfx::Image& image, | 189 const gfx::Image& image, |
| 190 const SkBitmap& bitmap, | |
| 131 history::IconType icon_type) { | 191 history::IconType icon_type) { |
| 132 const SkBitmap& bitmap = image; | |
| 133 const gfx::Image& sized_image = (preferred_icon_size() == 0 || | 192 const gfx::Image& sized_image = (preferred_icon_size() == 0 || |
| 134 (preferred_icon_size() == bitmap.width() && | 193 (preferred_icon_size() == bitmap.width() && |
| 135 preferred_icon_size() == bitmap.height())) ? | 194 preferred_icon_size() == bitmap.height())) ? |
| 136 image : ResizeFaviconIfNeeded(image); | 195 image : ResizeFaviconIfNeeded(image); |
| 137 | 196 |
| 138 if (GetFaviconService() && ShouldSaveFavicon(url)) { | 197 if (GetFaviconService() && ShouldSaveFavicon(url)) { |
| 139 std::vector<unsigned char> image_data; | 198 std::vector<unsigned char> image_data; |
| 140 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) | 199 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) |
| 141 SetHistoryFavicon(url, image_url, image_data, icon_type); | 200 SetHistoryFavicon(url, image_url, image_data, icon_type); |
| 142 } | 201 } |
| 143 | 202 |
| 144 if (url == url_ && icon_type == history::FAVICON) { | 203 if (url == url_ && icon_type == history::FAVICON) { |
| 145 NavigationEntry* entry = GetEntry(); | 204 NavigationEntry* entry = GetEntry(); |
| 146 if (entry) | 205 if (entry) { |
| 206 entry->GetFavicon().url = image_url; | |
| 147 UpdateFavicon(entry, &sized_image); | 207 UpdateFavicon(entry, &sized_image); |
| 208 } | |
| 148 } | 209 } |
| 149 } | 210 } |
| 150 | 211 |
| 151 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, | 212 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, |
| 152 scoped_refptr<RefCountedMemory> data) { | 213 scoped_refptr<RefCountedMemory> data) { |
| 153 scoped_ptr<gfx::Image> image(gfx::ImageFromPNGEncodedData(data->front(), | 214 scoped_ptr<gfx::Image> image(gfx::ImageFromPNGEncodedData(data->front(), |
| 154 data->size())); | 215 data->size())); |
| 155 UpdateFavicon(entry, image.get()); | 216 UpdateFavicon(entry, image.get()); |
| 156 } | 217 } |
| 157 | 218 |
| 158 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, | 219 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, |
| 159 const gfx::Image* image) { | 220 const gfx::Image* image) { |
| 160 // No matter what happens, we need to mark the favicon as being set. | 221 // No matter what happens, we need to mark the favicon as being set. |
| 161 entry->GetFavicon().valid = true; | 222 entry->GetFavicon().valid = true; |
| 162 | 223 |
| 163 if (!image) | 224 if (!image) |
| 164 return; | 225 return; |
| 165 | 226 |
| 166 entry->GetFavicon().bitmap = *image; | 227 entry->GetFavicon().bitmap = *image; |
| 167 delegate_->NotifyFaviconUpdated(); | 228 delegate_->NotifyFaviconUpdated(); |
| 168 } | 229 } |
| 169 | 230 |
| 170 void FaviconHandler::OnUpdateFaviconURL( | 231 void FaviconHandler::OnUpdateFaviconURL( |
|
sky
2012/03/19 20:34:56
I'm pretty sure that the page can trigger this to
stevenjb
2012/03/19 20:48:26
As long as this is always getting called with a co
| |
| 171 int32 page_id, | 232 int32 page_id, |
| 172 const std::vector<FaviconURL>& candidates) { | 233 const std::vector<FaviconURL>& candidates) { |
| 173 NavigationEntry* entry = GetEntry(); | |
| 174 if (!entry) | |
| 175 return; | |
| 176 | 234 |
| 177 bool got_favicon_url_update = false; | 235 image_urls_.clear(); |
| 236 favicon_candidate_ = FaviconCandidate(); | |
| 178 for (std::vector<FaviconURL>::const_iterator i = candidates.begin(); | 237 for (std::vector<FaviconURL>::const_iterator i = candidates.begin(); |
| 179 i != candidates.end(); ++i) { | 238 i != candidates.end(); ++i) { |
| 180 if (!i->icon_url.is_empty() && (i->icon_type & icon_types_)) { | 239 if (!i->icon_url.is_empty() && (i->icon_type & icon_types_)) |
| 181 if (!got_favicon_url_update) { | 240 image_urls_.push_back(*i); |
| 182 got_favicon_url_update = true; | |
| 183 urls_.clear(); | |
| 184 current_url_index_ = 0; | |
| 185 } | |
| 186 urls_.push_back(*i); | |
| 187 } | |
| 188 } | 241 } |
| 189 | 242 |
| 190 // TODO(davemoore) Should clear on empty url. Currently we ignore it. | 243 // TODO(davemoore) Should clear on empty url. Currently we ignore it. |
| 191 // This appears to be what FF does as well. | 244 // This appears to be what FF does as well. |
| 192 // No URL was added. | 245 if (image_urls_.empty()) |
| 193 if (!got_favicon_url_update) | |
| 194 return; | 246 return; |
| 195 | 247 |
| 196 if (!GetFaviconService()) | 248 if (!GetFaviconService()) |
| 197 return; | 249 return; |
| 198 | 250 |
| 251 ProcessCurrentUrl(); | |
| 252 } | |
| 253 | |
| 254 void FaviconHandler::ProcessCurrentUrl() { | |
| 255 DCHECK(!image_urls_.empty()); | |
| 256 | |
| 257 NavigationEntry* entry = GetEntry(); | |
| 258 if (!entry) | |
| 259 return; | |
| 260 | |
| 199 // For FAVICON. | 261 // For FAVICON. |
| 200 if (current_candidate()->icon_type == FaviconURL::FAVICON) { | 262 if (current_candidate()->icon_type == FaviconURL::FAVICON) { |
| 201 if (!favicon_expired_ && entry->GetFavicon().valid && | 263 if (!favicon_expired_ && entry->GetFavicon().valid && |
| 202 DoUrlAndIconMatch(*current_candidate(), entry->GetFavicon().url, | 264 DoUrlAndIconMatch(*current_candidate(), entry->GetFavicon().url, |
| 203 history::FAVICON)) | 265 history::FAVICON)) |
| 204 return; | 266 return; |
| 205 | 267 |
| 206 entry->GetFavicon().url = current_candidate()->icon_url; | 268 entry->GetFavicon().url = current_candidate()->icon_url; |
| 207 } else if (!favicon_expired_ && got_favicon_from_history_ && | 269 } else if (!favicon_expired_ && got_favicon_from_history_ && |
| 208 history_icon_.is_valid() && | 270 history_icon_.is_valid() && |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 228 return; | 290 return; |
| 229 } | 291 } |
| 230 | 292 |
| 231 if (!i->second.callback.is_null()) { | 293 if (!i->second.callback.is_null()) { |
| 232 i->second.callback.Run(id, errored, *(&image)); | 294 i->second.callback.Run(id, errored, *(&image)); |
| 233 } else if (current_candidate() && | 295 } else if (current_candidate() && |
| 234 DoUrlAndIconMatch(*current_candidate(), image_url, | 296 DoUrlAndIconMatch(*current_candidate(), image_url, |
| 235 i->second.icon_type)) { | 297 i->second.icon_type)) { |
| 236 // The downloaded icon is still valid when there is no FaviconURL update | 298 // The downloaded icon is still valid when there is no FaviconURL update |
| 237 // during the downloading. | 299 // during the downloading. |
| 300 bool request_next_icon = true; | |
| 238 if (!errored) { | 301 if (!errored) { |
| 239 SetFavicon(i->second.url, image_url, image, i->second.icon_type); | 302 request_next_icon = !UpdateFaviconCandidate( |
| 240 } else if (GetEntry() && ++current_url_index_ < urls_.size()) { | 303 i->second.url, image_url, image, i->second.icon_type); |
| 241 // Copies all candidate except first one and notifies the FaviconHandler, | 304 } |
| 242 // so the next candidate can be processed. | 305 if (request_next_icon && GetEntry() && image_urls_.size() > 1) { |
| 243 std::vector<FaviconURL> new_candidates(urls_.begin() + 1, urls_.end()); | 306 // Remove the first member of image_urls_ and process the remaining. |
| 244 OnUpdateFaviconURL(0, new_candidates); | 307 image_urls_.pop_front(); |
| 308 ProcessCurrentUrl(); | |
| 309 } else if (favicon_candidate_.icon_type != history::INVALID_ICON) { | |
| 310 // No more icons to request, set the favicon from the candidate. | |
| 311 SetFavicon(favicon_candidate_.url, favicon_candidate_.image_url, | |
| 312 favicon_candidate_.image, favicon_candidate_.bitmap, | |
| 313 favicon_candidate_.icon_type); | |
| 314 // Reset candidate. | |
| 315 favicon_candidate_ = FaviconCandidate(); | |
| 245 } | 316 } |
| 246 } | 317 } |
| 247 download_requests_.erase(i); | 318 download_requests_.erase(i); |
| 248 } | 319 } |
| 249 | 320 |
| 250 NavigationEntry* FaviconHandler::GetEntry() { | 321 NavigationEntry* FaviconHandler::GetEntry() { |
| 251 NavigationEntry* entry = delegate_->GetActiveEntry(); | 322 NavigationEntry* entry = delegate_->GetActiveEntry(); |
| 252 if (entry && entry->GetURL() == url_) | 323 if (entry && entry->GetURL() == url_) |
| 253 return entry; | 324 return entry; |
| 254 | 325 |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 450 int height = bitmap.height(); | 521 int height = bitmap.height(); |
| 451 if (width > 0 && height > 0) { | 522 if (width > 0 && height > 0) { |
| 452 gfx::CalculateFaviconTargetSize(&width, &height); | 523 gfx::CalculateFaviconTargetSize(&width, &height); |
| 453 return gfx::Image(skia::ImageOperations::Resize( | 524 return gfx::Image(skia::ImageOperations::Resize( |
| 454 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | 525 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, |
| 455 width, height)); | 526 width, height)); |
| 456 } | 527 } |
| 457 | 528 |
| 458 return image; | 529 return image; |
| 459 } | 530 } |
| OLD | NEW |