blob: 905fc3b21ad8685e429b1d0910fa34d510cf50bc [file] [log] [blame]
[email protected]898697902013-12-18 03:25:461// 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 Lame849b512017-10-31 06:03:5313#include "base/gtest_prod_util.h"
avia2f4804a2015-12-24 23:11:1314#include "base/macros.h"
[email protected]898697902013-12-18 03:25:4615#include "base/memory/weak_ptr.h"
16#include "content/public/browser/web_contents_observer.h"
17
18class SkBitmap;
19
20namespace content {
21struct FaviconURL;
22}
23
Christopher Lame849b512017-10-31 06:03:5324namespace extensions {
25FORWARD_DECLARE_TEST(BookmarkAppHelperExtensionServiceTest,
26 CreateBookmarkAppWithManifestIcons);
27}
28
[email protected]898697902013-12-18 03:25:4629namespace gfx {
30class Size;
31}
32
33// Class to help download all favicons for a tab.
34class 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
dominicknb8ead7ac2015-09-08 07:44:3743 // 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]898697902013-12-18 03:25:4647 FaviconDownloader(content::WebContents* web_contents,
48 const std::vector<GURL>& extra_favicon_urls,
49 FaviconDownloaderCallback callback);
dchengae36a4a2014-10-21 12:36:3650 ~FaviconDownloader() override;
[email protected]898697902013-12-18 03:25:4651
dominicknb8ead7ac2015-09-08 07:44:3752 void SkipPageFavicons();
53
[email protected]898697902013-12-18 03:25:4654 void Start();
55
56 private:
57 friend class TestFaviconDownloader;
Christopher Lame849b512017-10-31 06:03:5358 FRIEND_TEST_ALL_PREFIXES(extensions::BookmarkAppHelperExtensionServiceTest,
59 CreateBookmarkAppWithManifestIcons);
[email protected]898697902013-12-18 03:25:4660
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
sdefresne455826972015-04-10 15:25:1565 // Queries FaviconDriver for the page's current favicon URLs.
[email protected]898697902013-12-18 03:25:4666 // 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:
jam7e6919e2017-01-28 04:40:5882 void DidFinishNavigation(
83 content::NavigationHandle* navigation_handle) override;
dchengae36a4a2014-10-21 12:36:3684 void DidUpdateFaviconURL(
mostynba15bee12014-10-04 00:40:3285 const std::vector<content::FaviconURL>& candidates) override;
[email protected]898697902013-12-18 03:25:4686
dominicknb8ead7ac2015-09-08 07:44:3787 // Whether we need to fetch favicons from the renderer.
88 bool need_favicon_urls_;
[email protected]898697902013-12-18 03:25:4689
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_