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 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
159 return profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); | 159 return profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); |
160 } | 160 } |
161 | 161 |
162 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url, | 162 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url, |
163 const GURL& image_url, | 163 const GURL& image_url, |
164 const gfx::Image& image, | 164 const gfx::Image& image, |
165 history::IconType icon_type) { | 165 history::IconType icon_type) { |
166 bool update_candidate = false; | 166 bool update_candidate = false; |
167 SkBitmap bitmap = *(image.ToSkBitmap()); | 167 SkBitmap bitmap = *(image.ToSkBitmap()); |
168 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()); | 169 fprintf(stderr, "size: %d %d\n", bitmap_size, preferred_icon_size()); |
170 bool exact_match = (bitmap_size == preferred_icon_size() || bitmap_size / 2 == preferred_icon_size()); // XXX | |
170 if (preferred_icon_size() == 0) { | 171 if (preferred_icon_size() == 0) { |
171 // No preferred size, use this icon. | 172 // No preferred size, use this icon. |
172 update_candidate = true; | 173 update_candidate = true; |
173 exact_match = true; | 174 exact_match = true; |
174 } else if (favicon_candidate_.icon_type == history::INVALID_ICON) { | 175 } else if (favicon_candidate_.icon_type == history::INVALID_ICON) { |
175 // No current candidate, use this. | 176 // No current candidate, use this. |
176 update_candidate = true; | 177 update_candidate = true; |
177 } else { | 178 } else { |
178 if (bitmap_size == preferred_icon_size()) { | 179 if (bitmap_size == preferred_icon_size()) { |
179 // Exact match, use this. | 180 // Exact match, use this. |
(...skipping 12 matching lines...) Expand all Loading... | |
192 url, image_url, image, bitmap_size, icon_type); | 193 url, image_url, image, bitmap_size, icon_type); |
193 } | 194 } |
194 return exact_match; | 195 return exact_match; |
195 } | 196 } |
196 | 197 |
197 void FaviconHandler::SetFavicon( | 198 void FaviconHandler::SetFavicon( |
198 const GURL& url, | 199 const GURL& url, |
199 const GURL& image_url, | 200 const GURL& image_url, |
200 const gfx::Image& image, | 201 const gfx::Image& image, |
201 history::IconType icon_type) { | 202 history::IconType icon_type) { |
202 SkBitmap bitmap = *(image.ToSkBitmap()); | 203 SkBitmap bitmap = *(image.ToSkBitmap()); // XXX |
203 const gfx::Image& sized_image = (preferred_icon_size() == 0 || | 204 const gfx::Image& sized_image = (preferred_icon_size() == 0 || |
204 (preferred_icon_size() == bitmap.width() && | 205 (preferred_icon_size() == bitmap.width() && |
205 preferred_icon_size() == bitmap.height())) ? | 206 preferred_icon_size() == bitmap.height())) ? |
206 image : ResizeFaviconIfNeeded(image); | 207 image : ResizeFaviconIfNeeded(image); |
207 | 208 |
208 if (GetFaviconService() && ShouldSaveFavicon(url)) { | 209 if (GetFaviconService() && ShouldSaveFavicon(url)) { |
209 std::vector<unsigned char> image_data; | 210 std::vector<unsigned char> image_data; |
210 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) | 211 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) |
211 SetHistoryFavicon(url, image_url, image_data, icon_type); | 212 SetHistoryFavicon(url, image_url, image_data, icon_type); |
212 } | 213 } |
213 | 214 |
214 if (UrlMatches(url, url_) && icon_type == history::FAVICON) { | 215 if (UrlMatches(url, url_) && icon_type == history::FAVICON) { |
215 NavigationEntry* entry = GetEntry(); | 216 NavigationEntry* entry = GetEntry(); |
216 if (entry) { | 217 if (entry) { |
217 entry->GetFavicon().url = image_url; | 218 entry->GetFavicon().url = image_url; |
218 UpdateFavicon(entry, &sized_image); | 219 //UpdateFavicon(entry, &sized_image); |
220 UpdateFavicon(entry, &image); | |
219 } | 221 } |
220 } | 222 } |
221 } | 223 } |
222 | 224 |
223 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, | 225 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, |
224 scoped_refptr<base::RefCountedMemory> data) { | 226 scoped_refptr<base::RefCountedMemory> data) { |
225 scoped_ptr<gfx::Image> image(gfx::ImageFromPNGEncodedData(data->front(), | 227 scoped_ptr<gfx::Image> image(gfx::ImageFromPNGEncodedData(data->front(), |
226 data->size())); | 228 data->size())); |
227 UpdateFavicon(entry, image.get()); | 229 UpdateFavicon(entry, image.get()); |
228 } | 230 } |
229 | 231 |
230 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, | 232 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, |
231 const gfx::Image* image) { | 233 const gfx::Image* image) { |
232 // No matter what happens, we need to mark the favicon as being set. | 234 // No matter what happens, we need to mark the favicon as being set. |
233 entry->GetFavicon().valid = true; | 235 entry->GetFavicon().valid = true; |
234 | 236 |
235 if (!image) | 237 if (!image) |
236 return; | 238 return; |
237 | 239 |
238 entry->GetFavicon().bitmap = *image->ToSkBitmap(); | 240 entry->GetFavicon().bitmap = *image->ToSkBitmap(); // XXX |
241 entry->GetFavicon().image = *image; | |
239 delegate_->NotifyFaviconUpdated(); | 242 delegate_->NotifyFaviconUpdated(); |
240 } | 243 } |
241 | 244 |
242 void FaviconHandler::OnUpdateFaviconURL( | 245 void FaviconHandler::OnUpdateFaviconURL( |
243 int32 page_id, | 246 int32 page_id, |
244 const std::vector<FaviconURL>& candidates) { | 247 const std::vector<FaviconURL>& candidates) { |
245 | 248 |
246 image_urls_.clear(); | 249 image_urls_.clear(); |
247 favicon_candidate_ = FaviconCandidate(); | 250 favicon_candidate_ = FaviconCandidate(); |
248 for (std::vector<FaviconURL>::const_iterator i = candidates.begin(); | 251 for (std::vector<FaviconURL>::const_iterator i = candidates.begin(); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
334 NavigationEntry* FaviconHandler::GetEntry() { | 337 NavigationEntry* FaviconHandler::GetEntry() { |
335 NavigationEntry* entry = delegate_->GetActiveEntry(); | 338 NavigationEntry* entry = delegate_->GetActiveEntry(); |
336 if (entry && UrlMatches(entry->GetURL(), url_)) | 339 if (entry && UrlMatches(entry->GetURL(), url_)) |
337 return entry; | 340 return entry; |
338 | 341 |
339 // If the URL has changed out from under us (as will happen with redirects) | 342 // If the URL has changed out from under us (as will happen with redirects) |
340 // return NULL. | 343 // return NULL. |
341 return NULL; | 344 return NULL; |
342 } | 345 } |
343 | 346 |
344 int FaviconHandler::DownloadFavicon(const GURL& image_url, int image_size) { | 347 int FaviconHandler::DownloadFavicon(const GURL& image_url, const std::vector<int >& image_size) { |
345 if (!image_url.is_valid()) { | 348 if (!image_url.is_valid()) { |
346 NOTREACHED(); | 349 NOTREACHED(); |
347 return 0; | 350 return 0; |
348 } | 351 } |
349 int id = delegate_->StartDownload(image_url, image_size); | 352 int id = delegate_->StartDownload(image_url, image_size); |
350 return id; | 353 return id; |
351 } | 354 } |
352 | 355 |
353 void FaviconHandler::UpdateFaviconMappingAndFetch( | 356 void FaviconHandler::UpdateFaviconMappingAndFetch( |
354 const GURL& page_url, | 357 const GURL& page_url, |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
436 // We know the official url for the favicon, by either don't have the | 439 // We know the official url for the favicon, by either don't have the |
437 // favicon or its expired. Continue on to DownloadFaviconOrAskHistory to | 440 // favicon or its expired. Continue on to DownloadFaviconOrAskHistory to |
438 // either download or check history again. | 441 // either download or check history again. |
439 DownloadFaviconOrAskHistory(entry->GetURL(), current_candidate()->icon_url, | 442 DownloadFaviconOrAskHistory(entry->GetURL(), current_candidate()->icon_url, |
440 ToHistoryIconType(current_candidate()->icon_type)); | 443 ToHistoryIconType(current_candidate()->icon_type)); |
441 } | 444 } |
442 // else we haven't got the icon url. When we get it we'll ask the | 445 // else we haven't got the icon url. When we get it we'll ask the |
443 // renderer to download the icon. | 446 // renderer to download the icon. |
444 } | 447 } |
445 | 448 |
449 // XXX | |
446 void FaviconHandler::DownloadFaviconOrAskHistory( | 450 void FaviconHandler::DownloadFaviconOrAskHistory( |
447 const GURL& page_url, | 451 const GURL& page_url, |
448 const GURL& icon_url, | 452 const GURL& icon_url, |
449 history::IconType icon_type) { | 453 history::IconType icon_type) { |
450 if (favicon_expired_) { | 454 if (favicon_expired_) { |
451 // We have the mapping, but the favicon is out of date. Download it now. | 455 // We have the mapping, but the favicon is out of date. Download it now. |
452 ScheduleDownload(page_url, icon_url, preferred_icon_size(), icon_type, | 456 ScheduleDownload(page_url, icon_url, preferred_icon_size(), icon_type, |
453 FaviconTabHelper::ImageDownloadCallback()); | 457 FaviconTabHelper::ImageDownloadCallback()); |
454 } else if (GetFaviconService()) { | 458 } else if (GetFaviconService()) { |
455 // We don't know the favicon, but we may have previously downloaded the | 459 // We don't know the favicon, but we may have previously downloaded the |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
507 } | 511 } |
508 history_icon_ = favicon; | 512 history_icon_ = favicon; |
509 } | 513 } |
510 | 514 |
511 int FaviconHandler::ScheduleDownload( | 515 int FaviconHandler::ScheduleDownload( |
512 const GURL& url, | 516 const GURL& url, |
513 const GURL& image_url, | 517 const GURL& image_url, |
514 int image_size, | 518 int image_size, |
515 history::IconType icon_type, | 519 history::IconType icon_type, |
516 const FaviconTabHelper::ImageDownloadCallback& callback) { | 520 const FaviconTabHelper::ImageDownloadCallback& callback) { |
517 const int download_id = DownloadFavicon(image_url, image_size); | 521 std::vector<int> sizes; |
522 sizes.push_back(image_size); | |
523 sizes.push_back(2 * image_size); // XXX mac-specific | |
pkotwicz
2012/08/02 23:47:08
Can you use ui::GetSupportedScaleFactors here inst
| |
524 const int download_id = DownloadFavicon(image_url, sizes); | |
518 if (download_id) { | 525 if (download_id) { |
519 // Download ids should be unique. | 526 // Download ids should be unique. |
520 DCHECK(download_requests_.find(download_id) == download_requests_.end()); | 527 DCHECK(download_requests_.find(download_id) == download_requests_.end()); |
521 download_requests_[download_id] = | 528 download_requests_[download_id] = |
522 DownloadRequest(url, image_url, callback, icon_type); | 529 DownloadRequest(url, image_url, callback, icon_type); |
523 } | 530 } |
524 | 531 |
525 return download_id; | 532 return download_id; |
526 } | 533 } |
527 | 534 |
528 gfx::Image FaviconHandler::ResizeFaviconIfNeeded(const gfx::Image& image) { | 535 gfx::Image FaviconHandler::ResizeFaviconIfNeeded(const gfx::Image& image) { |
529 // Get an SkBitmap from the gfx::Image. | 536 // Get an SkBitmap from the gfx::Image. |
530 SkBitmap bitmap = *image.ToSkBitmap(); | 537 SkBitmap bitmap = *image.ToSkBitmap(); |
531 int width = bitmap.width(); | 538 int width = bitmap.width(); |
532 int height = bitmap.height(); | 539 int height = bitmap.height(); |
533 if (width > 0 && height > 0) { | 540 if (width > 0 && height > 0) { |
534 gfx::CalculateFaviconTargetSize(&width, &height); | 541 gfx::CalculateFaviconTargetSize(&width, &height); |
535 return gfx::Image(skia::ImageOperations::Resize( | 542 return gfx::Image(skia::ImageOperations::Resize( |
536 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | 543 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, |
537 width, height)); | 544 width, height)); |
538 } | 545 } |
539 | 546 |
540 return image; | 547 return image; |
541 } | 548 } |
OLD | NEW |