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 got_favicon_urls_(false),
20 extra_favicon_urls_(extra_favicon_urls
),
22 weak_ptr_factory_(this) {
25 FaviconDownloader::~FaviconDownloader() {
28 void FaviconDownloader::Start() {
29 FetchIcons(extra_favicon_urls_
);
30 // If the candidates aren't loaded, icons will be fetched when
31 // DidUpdateFaviconURL() is called.
32 std::vector
<content::FaviconURL
> favicon_tab_helper_urls
=
33 GetFaviconURLsFromWebContents();
34 if (!favicon_tab_helper_urls
.empty()) {
35 got_favicon_urls_
= true;
36 FetchIcons(favicon_tab_helper_urls
);
40 int FaviconDownloader::DownloadImage(const GURL
& url
) {
41 return web_contents()->DownloadImage(
45 false, // normal cache policy
46 base::Bind(&FaviconDownloader::DidDownloadFavicon
,
47 weak_ptr_factory_
.GetWeakPtr()));
50 std::vector
<content::FaviconURL
>
51 FaviconDownloader::GetFaviconURLsFromWebContents() {
52 favicon::ContentFaviconDriver
* content_favicon_driver
=
54 ? favicon::ContentFaviconDriver::FromWebContents(web_contents())
56 // If favicon_urls() is empty, we are guaranteed that DidUpdateFaviconURLs has
57 // not yet been called for the current page's navigation.
58 return content_favicon_driver
? content_favicon_driver
->favicon_urls()
59 : std::vector
<content::FaviconURL
>();
62 void FaviconDownloader::FetchIcons(
63 const std::vector
<content::FaviconURL
>& favicon_urls
) {
64 std::vector
<GURL
> urls
;
65 for (std::vector
<content::FaviconURL
>::const_iterator it
=
67 it
!= favicon_urls
.end(); ++it
) {
68 if (it
->icon_type
!= content::FaviconURL::INVALID_ICON
)
69 urls
.push_back(it
->icon_url
);
74 void FaviconDownloader::FetchIcons(const std::vector
<GURL
>& urls
) {
75 // Download icons; put their download ids into |in_progress_requests_| and
76 // their urls into |processed_urls_|.
77 for (std::vector
<GURL
>::const_iterator it
= urls
.begin();
78 it
!= urls
.end(); ++it
) {
79 // Only start the download if the url hasn't been processed before.
80 if (processed_urls_
.insert(*it
).second
)
81 in_progress_requests_
.insert(DownloadImage(*it
));
84 // If no downloads were initiated, we can proceed directly to running the
86 if (in_progress_requests_
.empty() && got_favicon_urls_
)
87 callback_
.Run(true, favicon_map_
);
90 void FaviconDownloader::DidDownloadFavicon(
93 const GURL
& image_url
,
94 const std::vector
<SkBitmap
>& bitmaps
,
95 const std::vector
<gfx::Size
>& original_bitmap_sizes
) {
96 // Request may have been canceled by DidNavigateMainFrame().
97 if (in_progress_requests_
.erase(id
) == 0)
100 favicon_map_
[image_url
] = bitmaps
;
102 // Once all requests have been resolved, perform post-download tasks.
103 if (in_progress_requests_
.empty() && got_favicon_urls_
)
104 callback_
.Run(true, favicon_map_
);
107 // content::WebContentsObserver overrides:
108 void FaviconDownloader::DidNavigateMainFrame(
109 const content::LoadCommittedDetails
& details
,
110 const content::FrameNavigateParams
& params
) {
111 // Clear all pending requests.
112 in_progress_requests_
.clear();
113 favicon_map_
.clear();
114 callback_
.Run(false, favicon_map_
);
117 void FaviconDownloader::DidUpdateFaviconURL(
118 const std::vector
<content::FaviconURL
>& candidates
) {
119 // Only consider the first candidates we are given. This prevents pages that
120 // change their favicon from spamming us.
121 if (got_favicon_urls_
)
124 got_favicon_urls_
= true;
125 FetchIcons(candidates
);