Index: chrome/browser/favicon/favicon_downloader.cc |
diff --git a/chrome/browser/favicon/favicon_downloader.cc b/chrome/browser/favicon/favicon_downloader.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a3875092878c806e438f2354825a4debf9c7dcbd |
--- /dev/null |
+++ b/chrome/browser/favicon/favicon_downloader.cc |
@@ -0,0 +1,114 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/favicon/favicon_downloader.h" |
+ |
+#include "base/bind.h" |
+#include "chrome/browser/favicon/favicon_tab_helper.h" |
+#include "content/public/browser/web_contents.h" |
+#include "content/public/common/favicon_url.h" |
+#include "third_party/skia/include/core/SkBitmap.h" |
+ |
+FaviconDownloader::FaviconDownloader(content::WebContents* web_contents, |
+ FaviconDownloaderCallback callback) |
+ : content::WebContentsObserver(web_contents), |
+ favicon_url_candidates_(NULL), |
+ weak_ptr_factory_(this), |
+ callback_(callback) { |
pkotwicz
2013/12/02 01:00:57
Nit: Initialization order should match that in the
calamity
2013/12/03 05:52:15
Done.
|
+} |
+ |
+FaviconDownloader::~FaviconDownloader() { |
+} |
+ |
+void FaviconDownloader::AddExtraFaviconUrl(const GURL& url) { |
+ extra_favicon_urls_.push_back(url); |
+} |
+ |
+void FaviconDownloader::Start() { |
+ PopulateFaviconURLsFromWebContents(); |
+ // If the candidates aren't loaded, icons will be fetched when |
+ // DidUpdateFaviconURL() is called. |
+ if (favicon_url_candidates_) |
+ FetchIcons(); |
+} |
+ |
+int FaviconDownloader::DownloadImage(const GURL& url) { |
+ return web_contents()->DownloadImage( |
+ url, |
+ true, // is_favicon |
+ 0, // no max size |
+ base::Bind(&FaviconDownloader::DidDownloadFavicon, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void FaviconDownloader::PopulateFaviconURLsFromWebContents() { |
+ FaviconTabHelper* favicon_tab_helper = |
+ web_contents() ? FaviconTabHelper::FromWebContents(web_contents()) : NULL; |
+ favicon_url_candidates_ = |
+ favicon_tab_helper ? favicon_tab_helper->GetFaviconURLs() : NULL; |
+} |
+ |
+void FaviconDownloader::FetchIcons() { |
+ // Ensure we only download each URL once. |
+ std::set<GURL> favicon_urls; |
+ for (std::vector<content::FaviconURL>::const_iterator it = |
+ favicon_url_candidates_->begin(); |
+ it != favicon_url_candidates_->end(); ++it) { |
+ if (it->icon_type != content::FaviconURL::INVALID_ICON) |
+ favicon_urls.insert(it->icon_url); |
+ } |
+ |
+ for (std::vector<GURL>::const_iterator it = extra_favicon_urls_.begin(); |
+ it != extra_favicon_urls_.end(); ++it) |
+ favicon_urls.insert(*it); |
+ |
+ // Download icons and put their ids into |in_progress_requests_|. |
+ for (std::set<GURL>::const_iterator it = favicon_urls.begin(); |
+ it != favicon_urls.end(); ++it) |
+ in_progress_requests_.insert(DownloadImage(*it)); |
+ |
+ // If no downloads were initiated, we can proceed directly to running the |
+ // callback. |
+ if (in_progress_requests_.empty()) |
+ callback_.Run(true, favicon_map_); |
+} |
+ |
+void FaviconDownloader::DidDownloadFavicon( |
+ int id, |
+ int http_status_code, |
+ const GURL& image_url, |
+ const std::vector<SkBitmap>& bitmaps, |
+ const std::vector<gfx::Size>& original_bitmap_sizes) { |
+ // Request canceled by DidUpdateFaviconURL() or DidNavigateMainFrame(). |
pkotwicz
2013/12/02 01:00:57
Nit: DidUpdateFaviconURL() does not cancel request
calamity
2013/12/03 05:52:15
Done.
|
+ if (in_progress_requests_.erase(id) == 0) |
+ return; |
+ |
+ favicon_map_[image_url] = bitmaps; |
+ |
+ // Once all requests have been resolved, perform post-download tasks. |
+ if (in_progress_requests_.empty()) |
+ callback_.Run(true, favicon_map_); |
+} |
+ |
+// content::WebContentsObserver overrides: |
+void FaviconDownloader::DidNavigateMainFrame( |
+ const content::LoadCommittedDetails& details, |
+ const content::FrameNavigateParams& params) { |
+ // Clear all pending requests. |
+ in_progress_requests_.clear(); |
+ favicon_map_.clear(); |
+ callback_.Run(false, favicon_map_); |
+} |
+ |
+void FaviconDownloader::DidUpdateFaviconURL( |
+ int32 page_id, |
+ const std::vector<content::FaviconURL>& candidates) { |
+ // Only consider the first candidates we are given. This prevents pages that |
+ // change their favicon from spamming us. |
+ if (favicon_url_candidates_) |
+ return; |
+ |
+ favicon_url_candidates_ = &candidates; |
+ FetchIcons(); |
+} |