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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 return history::INVALID_ICON; | 42 return history::INVALID_ICON; |
| 43 } | 43 } |
| 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 std::string UrlWithoutFragment(const GURL& gurl) { | |
| 53 std::string url = gurl.spec(); | |
|
sky
2012/03/25 18:46:46
I believe you want ReplaceComponents.
stevenjb
2012/03/25 22:00:15
So I do, thanks.
Done.
| |
| 54 std::string::size_type n = url.rfind(gurl.ref()); | |
| 55 if (n != std::string::npos && n > 0) | |
| 56 url = url.substr(0, n-1); // Trim fragment, including the leading '#'. | |
| 57 return url; | |
| 58 } | |
| 59 | |
| 60 bool UrlMatches(const GURL& gurl_a, const GURL& gurl_b) { | |
| 61 return UrlWithoutFragment(gurl_a) == UrlWithoutFragment(gurl_b); | |
| 62 } | |
| 63 | |
| 52 } // namespace | 64 } // namespace |
| 53 | 65 |
| 54 //////////////////////////////////////////////////////////////////////////////// | 66 //////////////////////////////////////////////////////////////////////////////// |
| 55 | 67 |
| 56 FaviconHandler::DownloadRequest::DownloadRequest() | 68 FaviconHandler::DownloadRequest::DownloadRequest() |
| 57 : icon_type(history::INVALID_ICON) { | 69 : icon_type(history::INVALID_ICON) { |
| 58 } | 70 } |
| 59 | 71 |
| 60 FaviconHandler::DownloadRequest::~DownloadRequest() { | 72 FaviconHandler::DownloadRequest::~DownloadRequest() { |
| 61 } | 73 } |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 | 159 |
| 148 FaviconService* FaviconHandler::GetFaviconService() { | 160 FaviconService* FaviconHandler::GetFaviconService() { |
| 149 return profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); | 161 return profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); |
| 150 } | 162 } |
| 151 | 163 |
| 152 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url, | 164 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url, |
| 153 const GURL& image_url, | 165 const GURL& image_url, |
| 154 const gfx::Image& image, | 166 const gfx::Image& image, |
| 155 history::IconType icon_type) { | 167 history::IconType icon_type) { |
| 156 bool update_candidate = false; | 168 bool update_candidate = false; |
| 157 bool exact_match = false; | |
| 158 SkBitmap bitmap = *(image.ToSkBitmap()); | 169 SkBitmap bitmap = *(image.ToSkBitmap()); |
| 159 int bitmap_size = std::max(bitmap.width(), bitmap.height()); | 170 int bitmap_size = std::max(bitmap.width(), bitmap.height()); |
| 171 bool exact_match = (bitmap_size == preferred_icon_size()); | |
| 160 if (preferred_icon_size() == 0) { | 172 if (preferred_icon_size() == 0) { |
| 173 // No preferred size, use this icon. | |
| 161 update_candidate = true; | 174 update_candidate = true; |
| 162 exact_match = true; | 175 exact_match = true; |
| 163 } else if (favicon_candidate_.icon_type == history::INVALID_ICON) { | 176 } else if (favicon_candidate_.icon_type == history::INVALID_ICON) { |
| 164 // No current candidate, use this. | 177 // No current candidate, use this. |
| 165 update_candidate = true; | 178 update_candidate = true; |
| 166 } else { | 179 } else { |
| 167 if (bitmap_size == preferred_icon_size()) { | 180 if (bitmap_size == preferred_icon_size()) { |
| 168 // Exact match, use this. | 181 // Exact match, use this. |
| 169 update_candidate = true; | 182 update_candidate = true; |
| 170 exact_match = true; | |
| 171 } else { | 183 } else { |
| 172 // Compare against current candidate. | 184 // Compare against current candidate. |
| 173 int cur_size = favicon_candidate_.bitmap_size; | 185 int cur_size = favicon_candidate_.bitmap_size; |
| 174 if ((bitmap_size >= preferred_icon_size() && bitmap_size < cur_size) || | 186 if ((bitmap_size >= preferred_icon_size() && bitmap_size < cur_size) || |
| 175 (cur_size < preferred_icon_size() && bitmap_size > cur_size)) { | 187 (cur_size < preferred_icon_size() && bitmap_size > cur_size)) { |
| 176 update_candidate = true; | 188 update_candidate = true; |
| 177 } | 189 } |
| 178 } | 190 } |
| 179 } | 191 } |
| 180 if (update_candidate) { | 192 if (update_candidate) { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 194 (preferred_icon_size() == bitmap.width() && | 206 (preferred_icon_size() == bitmap.width() && |
| 195 preferred_icon_size() == bitmap.height())) ? | 207 preferred_icon_size() == bitmap.height())) ? |
| 196 image : ResizeFaviconIfNeeded(image); | 208 image : ResizeFaviconIfNeeded(image); |
| 197 | 209 |
| 198 if (GetFaviconService() && ShouldSaveFavicon(url)) { | 210 if (GetFaviconService() && ShouldSaveFavicon(url)) { |
| 199 std::vector<unsigned char> image_data; | 211 std::vector<unsigned char> image_data; |
| 200 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) | 212 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) |
| 201 SetHistoryFavicon(url, image_url, image_data, icon_type); | 213 SetHistoryFavicon(url, image_url, image_data, icon_type); |
| 202 } | 214 } |
| 203 | 215 |
| 204 if (url == url_ && icon_type == history::FAVICON) { | 216 if (UrlMatches(url, url_) && icon_type == history::FAVICON) { |
| 205 NavigationEntry* entry = GetEntry(); | 217 NavigationEntry* entry = GetEntry(); |
| 206 if (entry) { | 218 if (entry) { |
| 207 entry->GetFavicon().url = image_url; | 219 entry->GetFavicon().url = image_url; |
| 208 UpdateFavicon(entry, &sized_image); | 220 UpdateFavicon(entry, &sized_image); |
| 209 } | 221 } |
| 210 } | 222 } |
| 211 } | 223 } |
| 212 | 224 |
| 213 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, | 225 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, |
| 214 scoped_refptr<RefCountedMemory> data) { | 226 scoped_refptr<RefCountedMemory> data) { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 // Reset candidate. | 328 // Reset candidate. |
| 317 image_urls_.clear(); | 329 image_urls_.clear(); |
| 318 favicon_candidate_ = FaviconCandidate(); | 330 favicon_candidate_ = FaviconCandidate(); |
| 319 } | 331 } |
| 320 } | 332 } |
| 321 download_requests_.erase(i); | 333 download_requests_.erase(i); |
| 322 } | 334 } |
| 323 | 335 |
| 324 NavigationEntry* FaviconHandler::GetEntry() { | 336 NavigationEntry* FaviconHandler::GetEntry() { |
| 325 NavigationEntry* entry = delegate_->GetActiveEntry(); | 337 NavigationEntry* entry = delegate_->GetActiveEntry(); |
| 326 if (entry && entry->GetURL() == url_) | 338 if (entry && UrlMatches(entry->GetURL(), url_)) |
| 327 return entry; | 339 return entry; |
| 328 | 340 |
| 329 // If the URL has changed out from under us (as will happen with redirects) | 341 // If the URL has changed out from under us (as will happen with redirects) |
| 330 // return NULL. | 342 // return NULL. |
| 331 return NULL; | 343 return NULL; |
| 332 } | 344 } |
| 333 | 345 |
| 334 int FaviconHandler::DownloadFavicon(const GURL& image_url, int image_size) { | 346 int FaviconHandler::DownloadFavicon(const GURL& image_url, int image_size) { |
| 335 if (!image_url.is_valid()) { | 347 if (!image_url.is_valid()) { |
| 336 NOTREACHED(); | 348 NOTREACHED(); |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 524 int height = bitmap.height(); | 536 int height = bitmap.height(); |
| 525 if (width > 0 && height > 0) { | 537 if (width > 0 && height > 0) { |
| 526 gfx::CalculateFaviconTargetSize(&width, &height); | 538 gfx::CalculateFaviconTargetSize(&width, &height); |
| 527 return gfx::Image(skia::ImageOperations::Resize( | 539 return gfx::Image(skia::ImageOperations::Resize( |
| 528 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | 540 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, |
| 529 width, height)); | 541 width, height)); |
| 530 } | 542 } |
| 531 | 543 |
| 532 return image; | 544 return image; |
| 533 } | 545 } |
| OLD | NEW |