Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / favicon / favicon_tab_helper.h
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   // 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 {
57     return favicon_urls_;
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
96   std::vector<content::FaviconURL> favicon_urls_;
97
98   scoped_ptr<FaviconHandler> favicon_handler_;
99
100   // Handles downloading touchicons. It is NULL if
101   // browser_defaults::kEnableTouchIcon is false.
102   scoped_ptr<FaviconHandler> touch_icon_handler_;
103
104   DISALLOW_COPY_AND_ASSIGN(FaviconTabHelper);
105 };
106
107 #endif  // CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_