Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/favicon/favicon_downloader.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "chrome/browser/favicon/favicon_tab_helper.h" | |
| 9 #include "content/public/browser/web_contents.h" | |
| 10 #include "content/public/common/favicon_url.h" | |
| 11 #include "third_party/skia/include/core/SkBitmap.h" | |
|
pkotwicz
2013/12/03 19:06:49
You should include "ui/gfx/size.h"
calamity
2013/12/04 05:31:48
Done.
| |
| 12 | |
| 13 FaviconDownloader::FaviconDownloader(content::WebContents* web_contents, | |
| 14 std::vector<GURL> extra_favicon_urls, | |
| 15 FaviconDownloaderCallback callback) | |
| 16 : content::WebContentsObserver(web_contents), | |
| 17 favicon_url_candidates_(NULL), | |
| 18 extra_favicon_urls_(extra_favicon_urls), | |
| 19 callback_(callback), | |
| 20 weak_ptr_factory_(this) { | |
| 21 } | |
| 22 | |
| 23 FaviconDownloader::~FaviconDownloader() { | |
| 24 } | |
| 25 | |
| 26 void FaviconDownloader::Start() { | |
|
pkotwicz
2013/12/03 19:06:49
If |extra_favicon_urls_| is non empty, you can sta
calamity
2013/12/04 05:31:48
Done.
| |
| 27 PopulateFaviconURLsFromWebContents(); | |
| 28 // If the candidates aren't loaded, icons will be fetched when | |
| 29 // DidUpdateFaviconURL() is called. | |
| 30 if (favicon_url_candidates_) | |
| 31 FetchIcons(); | |
| 32 } | |
| 33 | |
| 34 int FaviconDownloader::DownloadImage(const GURL& url) { | |
| 35 return web_contents()->DownloadImage( | |
| 36 url, | |
| 37 true, // is_favicon | |
| 38 0, // no max size | |
| 39 base::Bind(&FaviconDownloader::DidDownloadFavicon, | |
| 40 weak_ptr_factory_.GetWeakPtr())); | |
| 41 } | |
| 42 | |
| 43 void FaviconDownloader::PopulateFaviconURLsFromWebContents() { | |
| 44 FaviconTabHelper* favicon_tab_helper = | |
| 45 web_contents() ? FaviconTabHelper::FromWebContents(web_contents()) : NULL; | |
| 46 favicon_url_candidates_ = | |
| 47 favicon_tab_helper ? favicon_tab_helper->GetFaviconURLs() : NULL; | |
| 48 } | |
| 49 | |
| 50 void FaviconDownloader::FetchIcons() { | |
| 51 // Ensure we only download each URL once. | |
| 52 std::set<GURL> favicon_urls; | |
| 53 for (std::vector<content::FaviconURL>::const_iterator it = | |
| 54 favicon_url_candidates_->begin(); | |
| 55 it != favicon_url_candidates_->end(); ++it) { | |
| 56 if (it->icon_type != content::FaviconURL::INVALID_ICON) | |
| 57 favicon_urls.insert(it->icon_url); | |
| 58 } | |
| 59 | |
| 60 for (std::vector<GURL>::const_iterator it = extra_favicon_urls_.begin(); | |
| 61 it != extra_favicon_urls_.end(); ++it) | |
| 62 favicon_urls.insert(*it); | |
| 63 | |
| 64 // Download icons and put their ids into |in_progress_requests_|. | |
| 65 for (std::set<GURL>::const_iterator it = favicon_urls.begin(); | |
| 66 it != favicon_urls.end(); ++it) | |
| 67 in_progress_requests_.insert(DownloadImage(*it)); | |
| 68 | |
| 69 // If no downloads were initiated, we can proceed directly to running the | |
| 70 // callback. | |
| 71 if (in_progress_requests_.empty()) | |
| 72 callback_.Run(true, favicon_map_); | |
| 73 } | |
| 74 | |
| 75 void FaviconDownloader::DidDownloadFavicon( | |
| 76 int id, | |
| 77 int http_status_code, | |
| 78 const GURL& image_url, | |
| 79 const std::vector<SkBitmap>& bitmaps, | |
| 80 const std::vector<gfx::Size>& original_bitmap_sizes) { | |
| 81 // Request canceled by DidNavigateMainFrame(). | |
|
pkotwicz
2013/12/03 19:06:49
How about: "Request may have been canceled by DidN
calamity
2013/12/04 05:31:48
Done.
| |
| 82 if (in_progress_requests_.erase(id) == 0) | |
| 83 return; | |
| 84 | |
| 85 favicon_map_[image_url] = bitmaps; | |
| 86 | |
| 87 // Once all requests have been resolved, perform post-download tasks. | |
| 88 if (in_progress_requests_.empty()) | |
| 89 callback_.Run(true, favicon_map_); | |
| 90 } | |
| 91 | |
| 92 // content::WebContentsObserver overrides: | |
| 93 void FaviconDownloader::DidNavigateMainFrame( | |
| 94 const content::LoadCommittedDetails& details, | |
| 95 const content::FrameNavigateParams& params) { | |
| 96 // Clear all pending requests. | |
| 97 in_progress_requests_.clear(); | |
| 98 favicon_map_.clear(); | |
| 99 callback_.Run(false, favicon_map_); | |
| 100 } | |
| 101 | |
| 102 void FaviconDownloader::DidUpdateFaviconURL( | |
| 103 int32 page_id, | |
| 104 const std::vector<content::FaviconURL>& candidates) { | |
| 105 // Only consider the first candidates we are given. This prevents pages that | |
| 106 // change their favicon from spamming us. | |
| 107 if (favicon_url_candidates_) | |
| 108 return; | |
| 109 | |
| 110 favicon_url_candidates_ = &candidates; | |
|
pkotwicz
2013/12/03 19:06:49
You need to make a copy here. |candidates| does no
calamity
2013/12/04 05:31:48
Done.
| |
| 111 FetchIcons(); | |
| 112 } | |
| OLD | NEW |