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.
5 #include "chrome/browser/extensions/favicon_downloader.h"
8 #include "components/favicon/content/content_favicon_driver.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"
12 #include "ui/gfx/geometry/size.h"
14 FaviconDownloader::FaviconDownloader(
15 content::WebContents
* web_contents
,
16 const std::vector
<GURL
>& extra_favicon_urls
,
17 FaviconDownloaderCallback callback
)
18 : content::WebContentsObserver(web_contents
),
19 need_favicon_urls_(true),
20 extra_favicon_urls_(extra_favicon_urls
),
22 weak_ptr_factory_(this) {
25 FaviconDownloader::~FaviconDownloader() {
28 void FaviconDownloader::SkipPageFavicons() {
29 need_favicon_urls_
= false;
32 void FaviconDownloader::Start() {
33 FetchIcons(extra_favicon_urls_
);
34 // If the candidates aren't loaded, icons will be fetched when
35 // DidUpdateFaviconURL() is called.
37 if (need_favicon_urls_
) {
38 std::vector
<content::FaviconURL
> favicon_tab_helper_urls
=
39 GetFaviconURLsFromWebContents();
40 if (!favicon_tab_helper_urls
.empty()) {
41 need_favicon_urls_
= false;
42 FetchIcons(favicon_tab_helper_urls
);
47 int FaviconDownloader::DownloadImage(const GURL
& url
) {
48 return web_contents()->DownloadImage(
52 false, // normal cache policy
53 base::Bind(&FaviconDownloader::DidDownloadFavicon
,
54 weak_ptr_factory_
.GetWeakPtr()));
57 std::vector
<content::FaviconURL
>
58 FaviconDownloader::GetFaviconURLsFromWebContents() {
59 favicon::ContentFaviconDriver
* content_favicon_driver
=
61 ? favicon::ContentFaviconDriver::FromWebContents(web_contents())
63 // If favicon_urls() is empty, we are guaranteed that DidUpdateFaviconURLs has
64 // not yet been called for the current page's navigation.
65 return content_favicon_driver
? content_favicon_driver
->favicon_urls()
66 : std::vector
<content::FaviconURL
>();
69 void FaviconDownloader::FetchIcons(
70 const std::vector
<content::FaviconURL
>& favicon_urls
) {
71 std::vector
<GURL
> urls
;
72 for (std::vector
<content::FaviconURL
>::const_iterator it
=
74 it
!= favicon_urls
.end(); ++it
) {
75 if (it
->icon_type
!= content::FaviconURL::INVALID_ICON
)
76 urls
.push_back(it
->icon_url
);
81 void FaviconDownloader::FetchIcons(const std::vector
<GURL
>& urls
) {
82 // Download icons; put their download ids into |in_progress_requests_| and
83 // their urls into |processed_urls_|.
84 for (std::vector
<GURL
>::const_iterator it
= urls
.begin();
85 it
!= urls
.end(); ++it
) {
86 // Only start the download if the url hasn't been processed before.
87 if (processed_urls_
.insert(*it
).second
)
88 in_progress_requests_
.insert(DownloadImage(*it
));
91 // If no downloads were initiated, we can proceed directly to running the
93 if (in_progress_requests_
.empty() && !need_favicon_urls_
)
94 callback_
.Run(true, favicon_map_
);
97 void FaviconDownloader::DidDownloadFavicon(
100 const GURL
& image_url
,
101 const std::vector
<SkBitmap
>& bitmaps
,
102 const std::vector
<gfx::Size
>& original_bitmap_sizes
) {
103 // Request may have been canceled by DidNavigateMainFrame().
104 if (in_progress_requests_
.erase(id
) == 0)
107 favicon_map_
[image_url
] = bitmaps
;
109 // Once all requests have been resolved, perform post-download tasks.
110 if (in_progress_requests_
.empty() && !need_favicon_urls_
)
111 callback_
.Run(true, favicon_map_
);
114 // content::WebContentsObserver overrides:
115 void FaviconDownloader::DidNavigateMainFrame(
116 const content::LoadCommittedDetails
& details
,
117 const content::FrameNavigateParams
& params
) {
118 // Clear all pending requests.
119 in_progress_requests_
.clear();
120 favicon_map_
.clear();
121 callback_
.Run(false, favicon_map_
);
124 void FaviconDownloader::DidUpdateFaviconURL(
125 const std::vector
<content::FaviconURL
>& candidates
) {
126 // Only consider the first candidates we are given. This prevents pages that
127 // change their favicon from spamming us.
128 if (!need_favicon_urls_
)
131 need_favicon_urls_
= false;
132 FetchIcons(candidates
);