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