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/extensions/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" | |
12 #include "ui/gfx/size.h" | |
13 | |
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), | |
21 callback_(callback), | |
22 weak_ptr_factory_(this) { | |
23 } | |
24 | |
25 FaviconDownloader::~FaviconDownloader() { | |
26 } | |
27 | |
28 void FaviconDownloader::Start() { | |
29 // If the candidates aren't loaded, icons will be fetched when | |
30 // DidUpdateFaviconURL() is called. | |
31 FetchIcons(extra_favicon_urls_); | |
32 std::vector<content::FaviconURL> favicon_tab_helper_urls = | |
33 GetFaviconURLsFromWebContents(); | |
34 if (!favicon_tab_helper_urls.empty()) { | |
benwells
2013/12/12 21:17:57
Could this ever be empty, but all favicon urls hav
calamity
2013/12/13 03:11:55
No, WebContentsObserver::DidUpdateFaviconURL is on
| |
35 got_favicon_urls_ = true; | |
36 FetchIcons(favicon_tab_helper_urls); | |
37 } | |
38 } | |
39 | |
40 int FaviconDownloader::DownloadImage(const GURL& url) { | |
41 return web_contents()->DownloadImage( | |
42 url, | |
43 true, // is_favicon | |
44 0, // no max size | |
45 base::Bind(&FaviconDownloader::DidDownloadFavicon, | |
46 weak_ptr_factory_.GetWeakPtr())); | |
47 } | |
48 | |
49 std::vector<content::FaviconURL> | |
50 FaviconDownloader::GetFaviconURLsFromWebContents() { | |
51 FaviconTabHelper* favicon_tab_helper = | |
52 web_contents() ? FaviconTabHelper::FromWebContents(web_contents()) : NULL; | |
53 return favicon_tab_helper ? favicon_tab_helper->favicon_urls() | |
54 : std::vector<content::FaviconURL>(); | |
55 } | |
56 | |
57 void FaviconDownloader::FetchIcons( | |
58 const std::vector<content::FaviconURL>& favicon_urls) { | |
59 std::vector<GURL> urls; | |
60 for (std::vector<content::FaviconURL>::const_iterator it = | |
61 favicon_urls.begin(); | |
62 it != favicon_urls.end(); ++it) { | |
63 if (it->icon_type != content::FaviconURL::INVALID_ICON) | |
64 urls.push_back(it->icon_url); | |
65 } | |
66 FetchIcons(urls); | |
67 } | |
68 | |
69 void FaviconDownloader::FetchIcons(const std::vector<GURL>& urls) { | |
70 // Download icons; put their download ids into |in_progress_requests_| and | |
71 // their urls into |processed_urls_|. | |
72 for (std::vector<GURL>::const_iterator it = urls.begin(); | |
73 it != urls.end(); ++it) { | |
74 // Only start the download if the url hasn't been processed before. | |
75 if (processed_urls_.insert(*it).second) | |
76 in_progress_requests_.insert(DownloadImage(*it)); | |
77 } | |
78 | |
79 // If no downloads were initiated, we can proceed directly to running the | |
80 // callback. | |
81 if (in_progress_requests_.empty() && got_favicon_urls_) | |
82 callback_.Run(true, favicon_map_); | |
83 } | |
84 | |
85 void FaviconDownloader::DidDownloadFavicon( | |
86 int id, | |
87 int http_status_code, | |
88 const GURL& image_url, | |
89 const std::vector<SkBitmap>& bitmaps, | |
90 const std::vector<gfx::Size>& original_bitmap_sizes) { | |
91 // Request may have been canceled by DidNavigateMainFrame(). | |
92 if (in_progress_requests_.erase(id) == 0) | |
93 return; | |
94 | |
95 favicon_map_[image_url] = bitmaps; | |
96 | |
97 // Once all requests have been resolved, perform post-download tasks. | |
98 if (in_progress_requests_.empty() && got_favicon_urls_) | |
99 callback_.Run(true, favicon_map_); | |
100 } | |
101 | |
102 // content::WebContentsObserver overrides: | |
103 void FaviconDownloader::DidNavigateMainFrame( | |
104 const content::LoadCommittedDetails& details, | |
105 const content::FrameNavigateParams& params) { | |
106 // Clear all pending requests. | |
107 in_progress_requests_.clear(); | |
108 favicon_map_.clear(); | |
109 callback_.Run(false, favicon_map_); | |
110 } | |
111 | |
112 void FaviconDownloader::DidUpdateFaviconURL( | |
113 int32 page_id, | |
114 const std::vector<content::FaviconURL>& candidates) { | |
115 // Only consider the first candidates we are given. This prevents pages that | |
116 // change their favicon from spamming us. | |
117 if (got_favicon_urls_) | |
118 return; | |
119 | |
120 got_favicon_urls_ = true; | |
121 FetchIcons(candidates); | |
122 } | |
OLD | NEW |