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