Home | History | Annotate | Download | only in favicon
      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,
     72                             int preferred_image_size,
     73                             int max_image_size) OVERRIDE;
     74   virtual void NotifyFaviconUpdated(bool icon_url_changed) OVERRIDE;
     75 
     76   // Favicon download callback.
     77   void DidDownloadFavicon(
     78       int id,
     79       int http_status_code,
     80       const GURL& image_url,
     81       int requested_size,
     82       const std::vector<SkBitmap>& bitmaps);
     83 
     84  private:
     85   explicit FaviconTabHelper(content::WebContents* web_contents);
     86   friend class content::WebContentsUserData<FaviconTabHelper>;
     87 
     88   // content::WebContentsObserver overrides.
     89   virtual void NavigateToPendingEntry(
     90       const GURL& url,
     91       content::NavigationController::ReloadType reload_type) OVERRIDE;
     92   virtual void DidNavigateMainFrame(
     93       const content::LoadCommittedDetails& details,
     94       const content::FrameNavigateParams& params) OVERRIDE;
     95 
     96   Profile* profile_;
     97   bool should_fetch_icons_;
     98 
     99   scoped_ptr<FaviconHandler> favicon_handler_;
    100 
    101   // Handles downloading touchicons. It is NULL if
    102   // browser_defaults::kEnableTouchIcon is false.
    103   scoped_ptr<FaviconHandler> touch_icon_handler_;
    104 
    105   DISALLOW_COPY_AND_ASSIGN(FaviconTabHelper);
    106 };
    107 
    108 #endif  // CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_
    109