1 // Copyright (c) 2012 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.
5 #ifndef CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_
6 #define CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "chrome/browser/favicon/favicon_handler_delegate.h"
13 #include "content/public/browser/web_contents_observer.h"
14 #include "content/public/browser/web_contents_user_data.h"
15 #include "content/public/common/favicon_url.h"
26 // FaviconTabHelper works with FaviconHandlers to fetch the favicons.
28 // FetchFavicon fetches the given page's icons. It requests the icons from the
29 // history backend. If the icon is not available or expired, the icon will be
30 // downloaded and saved in the history backend.
32 class FaviconTabHelper : public content::WebContentsObserver,
33 public FaviconHandlerDelegate,
34 public content::WebContentsUserData<FaviconTabHelper> {
36 virtual ~FaviconTabHelper();
38 // Initiates loading the favicon for the specified url.
39 void FetchFavicon(const GURL& url);
41 // Returns the favicon for this tab, or IDR_DEFAULT_FAVICON if the tab does
42 // not have a favicon. The default implementation uses the current navigation
43 // entry. This will return an empty bitmap if there are no navigation
44 // entries, which should rarely happen.
45 gfx::Image GetFavicon() const;
47 // Returns true if we have the favicon for the page.
48 bool FaviconIsValid() const;
50 // Returns whether the favicon should be displayed. If this returns false, no
51 // space is provided for the favicon, and the favicon is never displayed.
52 virtual bool ShouldDisplayFavicon();
54 // Returns the current tab's favicon urls. If this is empty,
55 // DidUpdateFaviconURL has not yet been called for the current navigation.
56 const std::vector<content::FaviconURL>& favicon_urls() const {
60 // content::WebContentsObserver override. Must be public, because also
61 // called from PrerenderContents.
62 virtual void DidUpdateFaviconURL(
64 const std::vector<content::FaviconURL>& candidates) OVERRIDE;
66 // Saves the favicon for the current page.
69 // FaviconHandlerDelegate methods.
70 virtual content::NavigationEntry* GetActiveEntry() OVERRIDE;
71 virtual int StartDownload(const GURL& url, int max_bitmap_size) OVERRIDE;
72 virtual void NotifyFaviconUpdated(bool icon_url_changed) OVERRIDE;
74 // Favicon download callback.
75 void DidDownloadFavicon(
78 const GURL& image_url,
79 const std::vector<SkBitmap>& bitmaps,
80 const std::vector<gfx::Size>& original_bitmap_sizes);
83 explicit FaviconTabHelper(content::WebContents* web_contents);
84 friend class content::WebContentsUserData<FaviconTabHelper>;
86 // content::WebContentsObserver overrides.
87 virtual void DidStartNavigationToPendingEntry(
89 content::NavigationController::ReloadType reload_type) OVERRIDE;
90 virtual void DidNavigateMainFrame(
91 const content::LoadCommittedDetails& details,
92 const content::FrameNavigateParams& params) OVERRIDE;
96 std::vector<content::FaviconURL> favicon_urls_;
98 scoped_ptr<FaviconHandler> favicon_handler_;
100 // Handles downloading touchicons. It is NULL if
101 // browser_defaults::kEnableTouchIcon is false.
102 scoped_ptr<FaviconHandler> touch_icon_handler_;
104 DISALLOW_COPY_AND_ASSIGN(FaviconTabHelper);
107 #endif // CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_