Update To 11.40.268.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 "base/observer_list.h"
13 #include "components/favicon/core/browser/favicon_client.h"
14 #include "components/favicon/core/favicon_driver.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "content/public/browser/web_contents_user_data.h"
17 #include "content/public/common/favicon_url.h"
18
19 namespace gfx {
20 class Image;
21 }
22
23 namespace content {
24 struct FaviconStatus;
25 }
26
27 class GURL;
28 class FaviconHandler;
29 class FaviconTabHelperObserver;
30 class Profile;
31 class SkBitmap;
32
33 // FaviconTabHelper works with FaviconHandlers to fetch the favicons.
34 //
35 // FetchFavicon fetches the given page's icons. It requests the icons from the
36 // history backend. If the icon is not available or expired, the icon will be
37 // downloaded and saved in the history backend.
38 //
39 class FaviconTabHelper : public content::WebContentsObserver,
40                          public FaviconDriver,
41                          public content::WebContentsUserData<FaviconTabHelper> {
42  public:
43   ~FaviconTabHelper() override;
44
45   // Initiates loading the favicon for the specified url.
46   void FetchFavicon(const GURL& url);
47
48   // Returns the favicon for this tab, or IDR_DEFAULT_FAVICON if the tab does
49   // not have a favicon. The default implementation uses the current navigation
50   // entry. This will return an empty bitmap if there are no navigation
51   // entries, which should rarely happen.
52   gfx::Image GetFavicon() const;
53
54   // Returns true if we have the favicon for the page.
55   bool FaviconIsValid() const;
56
57   // Returns whether the favicon should be displayed. If this returns false, no
58   // space is provided for the favicon, and the favicon is never displayed.
59   virtual bool ShouldDisplayFavicon();
60
61   // Returns the current tab's favicon urls. If this is empty,
62   // DidUpdateFaviconURL has not yet been called for the current navigation.
63   const std::vector<content::FaviconURL>& favicon_urls() const {
64     return favicon_urls_;
65   }
66
67   // content::WebContentsObserver override. Must be public, because also
68   // called from PrerenderContents.
69   void DidUpdateFaviconURL(
70       const std::vector<content::FaviconURL>& candidates) override;
71
72   // Saves the favicon for the current page.
73   void SaveFavicon();
74
75   void AddObserver(FaviconTabHelperObserver* observer);
76   void RemoveObserver(FaviconTabHelperObserver* observer);
77
78   // FaviconDriver methods.
79   int StartDownload(const GURL& url, int max_bitmap_size) override;
80   bool IsOffTheRecord() override;
81   const gfx::Image GetActiveFaviconImage() override;
82   const GURL GetActiveFaviconURL() override;
83   bool GetActiveFaviconValidity() override;
84   const GURL GetActiveURL() override;
85   void OnFaviconAvailable(const gfx::Image& image,
86                           const GURL& url,
87                           bool is_active_favicon) override;
88
89   // Favicon download callback.
90   void DidDownloadFavicon(
91       int id,
92       int http_status_code,
93       const GURL& image_url,
94       const std::vector<SkBitmap>& bitmaps,
95       const std::vector<gfx::Size>& original_bitmap_sizes);
96
97  private:
98   explicit FaviconTabHelper(content::WebContents* web_contents);
99   friend class content::WebContentsUserData<FaviconTabHelper>;
100
101   // content::WebContentsObserver overrides.
102   void DidStartNavigationToPendingEntry(
103       const GURL& url,
104       content::NavigationController::ReloadType reload_type) override;
105   void DidNavigateMainFrame(
106       const content::LoadCommittedDetails& details,
107       const content::FrameNavigateParams& params) override;
108
109   // Sets whether the page's favicon is valid (if false, the default favicon is
110   // being used). Requires GetActiveURL() to be valid.
111   void SetActiveFaviconValidity(bool validity);
112
113   // Sets the URL of the favicon's bitmap.
114   void SetActiveFaviconURL(GURL url);
115
116   // Sets the bitmap of the current page's favicon.
117   void SetActiveFaviconImage(gfx::Image image);
118
119   // Helper method that returns the active navigation entry's favicon.
120   content::FaviconStatus& GetFaviconStatus();
121
122   Profile* profile_;
123
124   FaviconClient* client_;
125
126   std::vector<content::FaviconURL> favicon_urls_;
127
128   scoped_ptr<FaviconHandler> favicon_handler_;
129
130   // Handles downloading touchicons. It is NULL if
131   // browser_defaults::kEnableTouchIcon is false.
132   scoped_ptr<FaviconHandler> touch_icon_handler_;
133
134   ObserverList<FaviconTabHelperObserver> observer_list_;
135
136   DISALLOW_COPY_AND_ASSIGN(FaviconTabHelper);
137 };
138
139 #endif  // CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_