[email protected] | 89869790 | 2013-12-18 03:25:46 | [diff] [blame] | 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 | #ifndef CHROME_BROWSER_EXTENSIONS_FAVICON_DOWNLOADER_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_FAVICON_DOWNLOADER_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <set> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/callback.h" |
| 13 | #include "base/memory/weak_ptr.h" |
| 14 | #include "content/public/browser/web_contents_observer.h" |
| 15 | |
| 16 | class SkBitmap; |
| 17 | |
| 18 | namespace content { |
| 19 | struct FaviconURL; |
| 20 | } |
| 21 | |
| 22 | namespace gfx { |
| 23 | class Size; |
| 24 | } |
| 25 | |
| 26 | // Class to help download all favicons for a tab. |
| 27 | class FaviconDownloader : public content::WebContentsObserver { |
| 28 | public: |
| 29 | typedef std::map<GURL, std::vector<SkBitmap> > FaviconMap; |
| 30 | typedef base::Callback<void( |
| 31 | bool, /* success */ |
| 32 | /* A map of icon urls to the bitmaps provided by that url. */ |
| 33 | const FaviconMap&)> |
| 34 | FaviconDownloaderCallback; |
| 35 | // |extra_favicon_urls| allows callers to provide icon urls that aren't |
dominickn | b8ead7ac | 2015-09-08 07:44:37 | [diff] [blame] | 36 | // provided by the renderer (e.g touch icons on non-android environments). |
| 37 | // |skip_page_favicons| instructs the downloader to not query the page |
| 38 | // for favicons (e.g. when a favicon URL has already been provided in |
| 39 | // |extra_favicon_urls|). |
[email protected] | 89869790 | 2013-12-18 03:25:46 | [diff] [blame] | 40 | FaviconDownloader(content::WebContents* web_contents, |
| 41 | const std::vector<GURL>& extra_favicon_urls, |
| 42 | FaviconDownloaderCallback callback); |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 43 | ~FaviconDownloader() override; |
[email protected] | 89869790 | 2013-12-18 03:25:46 | [diff] [blame] | 44 | |
dominickn | b8ead7ac | 2015-09-08 07:44:37 | [diff] [blame] | 45 | void SkipPageFavicons(); |
| 46 | |
[email protected] | 89869790 | 2013-12-18 03:25:46 | [diff] [blame] | 47 | void Start(); |
| 48 | |
| 49 | private: |
| 50 | friend class TestFaviconDownloader; |
| 51 | |
| 52 | // Initiates a download of the image at |url| and returns the download id. |
| 53 | // This is overridden in testing. |
| 54 | virtual int DownloadImage(const GURL& url); |
| 55 | |
sdefresne | 45582697 | 2015-04-10 15:25:15 | [diff] [blame] | 56 | // Queries FaviconDriver for the page's current favicon URLs. |
[email protected] | 89869790 | 2013-12-18 03:25:46 | [diff] [blame] | 57 | // This is overridden in testing. |
| 58 | virtual std::vector<content::FaviconURL> GetFaviconURLsFromWebContents(); |
| 59 | |
| 60 | // Fetches icons for the given urls. |
| 61 | // |callback_| is run when all downloads complete. |
| 62 | void FetchIcons(const std::vector<content::FaviconURL>& favicon_urls); |
| 63 | void FetchIcons(const std::vector<GURL>& urls); |
| 64 | |
| 65 | // Icon download callback. |
| 66 | void DidDownloadFavicon(int id, |
| 67 | int http_status_code, |
| 68 | const GURL& image_url, |
| 69 | const std::vector<SkBitmap>& bitmaps, |
| 70 | const std::vector<gfx::Size>& original_bitmap_sizes); |
| 71 | |
| 72 | // content::WebContentsObserver overrides: |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 73 | void DidNavigateMainFrame( |
[email protected] | 89869790 | 2013-12-18 03:25:46 | [diff] [blame] | 74 | const content::LoadCommittedDetails& details, |
mostynb | a15bee1 | 2014-10-04 00:40:32 | [diff] [blame] | 75 | const content::FrameNavigateParams& params) override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 76 | void DidUpdateFaviconURL( |
mostynb | a15bee1 | 2014-10-04 00:40:32 | [diff] [blame] | 77 | const std::vector<content::FaviconURL>& candidates) override; |
[email protected] | 89869790 | 2013-12-18 03:25:46 | [diff] [blame] | 78 | |
dominickn | b8ead7ac | 2015-09-08 07:44:37 | [diff] [blame] | 79 | // Whether we need to fetch favicons from the renderer. |
| 80 | bool need_favicon_urls_; |
[email protected] | 89869790 | 2013-12-18 03:25:46 | [diff] [blame] | 81 | |
| 82 | // URLs that aren't given by WebContentsObserver::DidUpdateFaviconURL() that |
| 83 | // should be used for this favicon. This is necessary in order to get touch |
| 84 | // icons on non-android environments. |
| 85 | std::vector<GURL> extra_favicon_urls_; |
| 86 | |
| 87 | // The icons which were downloaded. Populated by FetchIcons(). |
| 88 | FaviconMap favicon_map_; |
| 89 | |
| 90 | // Request ids of in-progress requests. |
| 91 | std::set<int> in_progress_requests_; |
| 92 | |
| 93 | // Urls for which a download has already been initiated. Used to prevent |
| 94 | // duplicate downloads of the same url. |
| 95 | std::set<GURL> processed_urls_; |
| 96 | |
| 97 | // Callback to run on favicon download completion. |
| 98 | FaviconDownloaderCallback callback_; |
| 99 | |
| 100 | base::WeakPtrFactory<FaviconDownloader> weak_ptr_factory_; |
| 101 | |
| 102 | DISALLOW_COPY_AND_ASSIGN(FaviconDownloader); |
| 103 | }; |
| 104 | |
| 105 | #endif // CHROME_BROWSER_EXTENSIONS_FAVICON_DOWNLOADER_H_ |