// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef CHROME_BROWSER_EXTENSIONS_FAVICON_DOWNLOADER_H_ #define CHROME_BROWSER_EXTENSIONS_FAVICON_DOWNLOADER_H_ #include #include #include #include "base/callback.h" #include "base/memory/weak_ptr.h" #include "content/public/browser/web_contents_observer.h" class SkBitmap; namespace content { struct FaviconURL; } namespace gfx { class Size; } // Class to help download all favicons for a tab. class FaviconDownloader : public content::WebContentsObserver { public: typedef std::map > FaviconMap; typedef base::Callback FaviconDownloaderCallback; // |extra_favicon_urls| allows callers to provide icon urls that aren't // |provided by the renderer (e.g touch icons on non-android environments). FaviconDownloader(content::WebContents* web_contents, const std::vector& extra_favicon_urls, FaviconDownloaderCallback callback); ~FaviconDownloader() override; void Start(); private: friend class TestFaviconDownloader; // Initiates a download of the image at |url| and returns the download id. // This is overridden in testing. virtual int DownloadImage(const GURL& url); // Queries FaviconTabHelper for the page's current favicon URLs. // This is overridden in testing. virtual std::vector GetFaviconURLsFromWebContents(); // Fetches icons for the given urls. // |callback_| is run when all downloads complete. void FetchIcons(const std::vector& favicon_urls); void FetchIcons(const std::vector& urls); // Icon download callback. void DidDownloadFavicon(int id, int http_status_code, const GURL& image_url, const std::vector& bitmaps, const std::vector& original_bitmap_sizes); // content::WebContentsObserver overrides: void DidNavigateMainFrame( const content::LoadCommittedDetails& details, const content::FrameNavigateParams& params) override; void DidUpdateFaviconURL( const std::vector& candidates) override; // Whether we have received favicons from the renderer. bool got_favicon_urls_; // URLs that aren't given by WebContentsObserver::DidUpdateFaviconURL() that // should be used for this favicon. This is necessary in order to get touch // icons on non-android environments. std::vector extra_favicon_urls_; // The icons which were downloaded. Populated by FetchIcons(). FaviconMap favicon_map_; // Request ids of in-progress requests. std::set in_progress_requests_; // Urls for which a download has already been initiated. Used to prevent // duplicate downloads of the same url. std::set processed_urls_; // Callback to run on favicon download completion. FaviconDownloaderCallback callback_; base::WeakPtrFactory weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(FaviconDownloader); }; #endif // CHROME_BROWSER_EXTENSIONS_FAVICON_DOWNLOADER_H_