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. 4 5 #ifndef CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_ 6 #define CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_ 7 8 #include <vector> 9 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" 16 17 namespace gfx { 18 class Image; 19 } 20 21 class GURL; 22 class FaviconHandler; 23 class Profile; 24 class SkBitmap; 25 26 // FaviconTabHelper works with FaviconHandlers to fetch the favicons. 27 // 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. 31 // 32 class FaviconTabHelper : public content::WebContentsObserver, 33 public FaviconHandlerDelegate, 34 public content::WebContentsUserData<FaviconTabHelper> { 35 public: 36 virtual ~FaviconTabHelper(); 37 38 // Initiates loading the favicon for the specified url. 39 void FetchFavicon(const GURL& url); 40 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; 46 47 // Returns true if we have the favicon for the page. 48 bool FaviconIsValid() const; 49 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(); 53 54 // Allows the client to determine if they want to fetch the Favicons as 55 // they are discovered. 56 void set_should_fetch_icons(bool fetch) { 57 should_fetch_icons_ = fetch; 58 } 59 60 // content::WebContentsObserver override. Must be public, because also 61 // called from PrerenderContents. 62 virtual void DidUpdateFaviconURL( 63 int32 page_id, 64 const std::vector<content::FaviconURL>& candidates) OVERRIDE; 65 66 // Saves the favicon for the current page. 67 void SaveFavicon(); 68 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; 73 74 // Favicon download callback. 75 void DidDownloadFavicon( 76 int id, 77 int http_status_code, 78 const GURL& image_url, 79 const std::vector<SkBitmap>& bitmaps, 80 const std::vector<gfx::Size>& original_bitmap_sizes); 81 82 private: 83 explicit FaviconTabHelper(content::WebContents* web_contents); 84 friend class content::WebContentsUserData<FaviconTabHelper>; 85 86 // content::WebContentsObserver overrides. 87 virtual void DidStartNavigationToPendingEntry( 88 const GURL& url, 89 content::NavigationController::ReloadType reload_type) OVERRIDE; 90 virtual void DidNavigateMainFrame( 91 const content::LoadCommittedDetails& details, 92 const content::FrameNavigateParams& params) OVERRIDE; 93 94 Profile* profile_; 95 bool should_fetch_icons_; 96 97 scoped_ptr<FaviconHandler> favicon_handler_; 98 99 // Handles downloading touchicons. It is NULL if 100 // browser_defaults::kEnableTouchIcon is false. 101 scoped_ptr<FaviconHandler> touch_icon_handler_; 102 103 DISALLOW_COPY_AND_ASSIGN(FaviconTabHelper); 104 }; 105 106 #endif // CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_ 107