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 "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"
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 FaviconTabHelper
* favicon_tab_helper
=
53 web_contents() ? FaviconTabHelper::FromWebContents(web_contents()) : NULL
;
54 // If favicon_urls() is empty, we are guaranteed that DidUpdateFaviconURLs has
55 // not yet been called for the current page's navigation.
56 return favicon_tab_helper
? favicon_tab_helper
->favicon_urls()
57 : std::vector
<content::FaviconURL
>();
60 void FaviconDownloader::FetchIcons(
61 const std::vector
<content::FaviconURL
>& favicon_urls
) {
62 std::vector
<GURL
> urls
;
63 for (std::vector
<content::FaviconURL
>::const_iterator it
=
65 it
!= favicon_urls
.end(); ++it
) {
66 if (it
->icon_type
!= content::FaviconURL::INVALID_ICON
)
67 urls
.push_back(it
->icon_url
);
72 void FaviconDownloader::FetchIcons(const std::vector
<GURL
>& urls
) {
73 // Download icons; put their download ids into |in_progress_requests_| and
74 // their urls into |processed_urls_|.
75 for (std::vector
<GURL
>::const_iterator it
= urls
.begin();
76 it
!= urls
.end(); ++it
) {
77 // Only start the download if the url hasn't been processed before.
78 if (processed_urls_
.insert(*it
).second
)
79 in_progress_requests_
.insert(DownloadImage(*it
));
82 // If no downloads were initiated, we can proceed directly to running the
84 if (in_progress_requests_
.empty() && got_favicon_urls_
)
85 callback_
.Run(true, favicon_map_
);
88 void FaviconDownloader::DidDownloadFavicon(
91 const GURL
& image_url
,
92 const std::vector
<SkBitmap
>& bitmaps
,
93 const std::vector
<gfx::Size
>& original_bitmap_sizes
) {
94 // Request may have been canceled by DidNavigateMainFrame().
95 if (in_progress_requests_
.erase(id
) == 0)
98 favicon_map_
[image_url
] = bitmaps
;
100 // Once all requests have been resolved, perform post-download tasks.
101 if (in_progress_requests_
.empty() && got_favicon_urls_
)
102 callback_
.Run(true, favicon_map_
);
105 // content::WebContentsObserver overrides:
106 void FaviconDownloader::DidNavigateMainFrame(
107 const content::LoadCommittedDetails
& details
,
108 const content::FrameNavigateParams
& params
) {
109 // Clear all pending requests.
110 in_progress_requests_
.clear();
111 favicon_map_
.clear();
112 callback_
.Run(false, favicon_map_
);
115 void FaviconDownloader::DidUpdateFaviconURL(
116 const std::vector
<content::FaviconURL
>& candidates
) {
117 // Only consider the first candidates we are given. This prevents pages that
118 // change their favicon from spamming us.
119 if (got_favicon_urls_
)
122 got_favicon_urls_
= true;
123 FetchIcons(candidates
);