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