OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_ | 5 #ifndef CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_ |
6 #define CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_ | 6 #define CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_ |
7 | 7 |
8 #include <vector> | 8 #include "base/macros.h" |
| 9 #include "components/favicon/content/content_favicon_driver.h" |
9 | 10 |
10 #include "base/basictypes.h" | 11 // FaviconTabHelper provides helper factory for ContentFaviconDriver. |
11 #include "base/callback.h" | 12 class FaviconTabHelper : public favicon::ContentFaviconDriver { |
12 #include "base/observer_list.h" | |
13 #include "components/favicon/core/favicon_driver.h" | |
14 #include "content/public/browser/web_contents_observer.h" | |
15 #include "content/public/browser/web_contents_user_data.h" | |
16 #include "content/public/common/favicon_url.h" | |
17 | |
18 class GURL; | |
19 class SkBitmap; | |
20 | |
21 namespace gfx { | |
22 class Image; | |
23 } | |
24 | |
25 namespace bookmarks { | |
26 class BookmarkModel; | |
27 } | |
28 | |
29 namespace content { | |
30 struct FaviconStatus; | |
31 } | |
32 | |
33 namespace favicon { | |
34 class FaviconDriverObserver; | |
35 class FaviconHandler; | |
36 class FaviconService; | |
37 } | |
38 | |
39 namespace history { | |
40 class HistoryService; | |
41 } | |
42 | |
43 // FaviconTabHelper works with favicon::FaviconHandlers to fetch the favicons. | |
44 // | |
45 // FetchFavicon fetches the given page's icons. It requests the icons from the | |
46 // history backend. If the icon is not available or expired, the icon will be | |
47 // downloaded and saved in the history backend. | |
48 // | |
49 class FaviconTabHelper : public content::WebContentsObserver, | |
50 public favicon::FaviconDriver, | |
51 public content::WebContentsUserData<FaviconTabHelper> { | |
52 public: | 13 public: |
53 ~FaviconTabHelper() override; | |
54 | |
55 static void CreateForWebContents(content::WebContents* web_contents); | 14 static void CreateForWebContents(content::WebContents* web_contents); |
56 | 15 |
57 // Initiates loading the favicon for the specified url. | 16 // TODO(sdefresne): remove this method once all clients have been ported to |
58 void FetchFavicon(const GURL& url); | 17 // use ContentFaviconDriver::FromWebContents() instead. |
59 | 18 static FaviconTabHelper* FromWebContents(content::WebContents* web_contents); |
60 // Returns the favicon for this tab, or IDR_DEFAULT_FAVICON if the tab does | |
61 // not have a favicon. The default implementation uses the current navigation | |
62 // entry. This will return an empty bitmap if there are no navigation | |
63 // entries, which should rarely happen. | |
64 gfx::Image GetFavicon() const; | |
65 | |
66 // Returns true if we have the favicon for the page. | |
67 bool FaviconIsValid() const; | |
68 | 19 |
69 // Returns whether the favicon should be displayed. If this returns false, no | 20 // Returns whether the favicon should be displayed. If this returns false, no |
70 // space is provided for the favicon, and the favicon is never displayed. | 21 // space is provided for the favicon, and the favicon is never displayed. |
71 bool ShouldDisplayFavicon(); | 22 static bool ShouldDisplayFavicon(content::WebContents* web_contents); |
72 | |
73 // Returns the current tab's favicon urls. If this is empty, | |
74 // DidUpdateFaviconURL has not yet been called for the current navigation. | |
75 const std::vector<content::FaviconURL>& favicon_urls() const { | |
76 return favicon_urls_; | |
77 } | |
78 | |
79 // content::WebContentsObserver override. Must be public, because also | |
80 // called from PrerenderContents. | |
81 void DidUpdateFaviconURL( | |
82 const std::vector<content::FaviconURL>& candidates) override; | |
83 | |
84 // Saves the favicon for the current page. | |
85 void SaveFavicon(); | |
86 | |
87 void AddObserver(favicon::FaviconDriverObserver* observer); | |
88 void RemoveObserver(favicon::FaviconDriverObserver* observer); | |
89 | |
90 // favicon::FaviconDriver methods. | |
91 int StartDownload(const GURL& url, int max_bitmap_size) override; | |
92 bool IsOffTheRecord() override; | |
93 bool IsBookmarked(const GURL& url) override; | |
94 GURL GetActiveURL() override; | |
95 base::string16 GetActiveTitle() override; | |
96 bool GetActiveFaviconValidity() override; | |
97 void SetActiveFaviconValidity(bool valid) override; | |
98 GURL GetActiveFaviconURL() override; | |
99 void SetActiveFaviconURL(const GURL& url) override; | |
100 gfx::Image GetActiveFaviconImage() override; | |
101 void SetActiveFaviconImage(const gfx::Image& image) override; | |
102 void OnFaviconAvailable(const gfx::Image& image, | |
103 const GURL& url, | |
104 bool is_active_favicon) override; | |
105 void NotifyFaviconUpdated(bool icon_url_changed) override; | |
106 | |
107 // Favicon download callback. | |
108 void DidDownloadFavicon( | |
109 int id, | |
110 int http_status_code, | |
111 const GURL& image_url, | |
112 const std::vector<SkBitmap>& bitmaps, | |
113 const std::vector<gfx::Size>& original_bitmap_sizes); | |
114 | 23 |
115 private: | 24 private: |
116 friend class content::WebContentsUserData<FaviconTabHelper>; | |
117 friend class FaviconTabHelperTest; | 25 friend class FaviconTabHelperTest; |
118 | 26 |
119 // Creates a new FaviconTabHelper bound to |web_contents|. Initialize | 27 // Creates a new FaviconTabHelper bound to |web_contents|. Initialize |
120 // |favicon_service_|, |bookmark_model_| and |history_service_| from the | 28 // |favicon_service_|, |bookmark_model_| and |history_service_| from the |
121 // corresponding parameter. | 29 // corresponding parameter. |
122 FaviconTabHelper(content::WebContents* web_contents, | 30 FaviconTabHelper(content::WebContents* web_contents, |
123 favicon::FaviconService* favicon_service, | 31 favicon::FaviconService* favicon_service, |
124 history::HistoryService* history_service, | 32 history::HistoryService* history_service, |
125 bookmarks::BookmarkModel* bookmark_model); | 33 bookmarks::BookmarkModel* bookmark_model); |
126 | 34 |
127 // content::WebContentsObserver overrides. | |
128 void DidStartNavigationToPendingEntry( | |
129 const GURL& url, | |
130 content::NavigationController::ReloadType reload_type) override; | |
131 void DidNavigateMainFrame( | |
132 const content::LoadCommittedDetails& details, | |
133 const content::FrameNavigateParams& params) override; | |
134 | |
135 // Helper method that returns the active navigation entry's favicon. | |
136 content::FaviconStatus& GetFaviconStatus(); | |
137 | |
138 // KeyedService used by FaviconTabHelper. They may be null during testing, but | |
139 // if they are defined, they must outlive the FaviconTabHelper. | |
140 favicon::FaviconService* favicon_service_; | |
141 history::HistoryService* history_service_; | |
142 bookmarks::BookmarkModel* bookmark_model_; | |
143 | |
144 std::vector<content::FaviconURL> favicon_urls_; | |
145 | |
146 // Bypass cache when downloading favicons for this page URL. | |
147 GURL bypass_cache_page_url_; | |
148 | |
149 // FaviconHandlers used to download the different kind of favicons. Both | |
150 // |touch_icon_handler_| and |large_icon_handler_| may be null depending | |
151 // on the platform or variations. | |
152 scoped_ptr<favicon::FaviconHandler> favicon_handler_; | |
153 scoped_ptr<favicon::FaviconHandler> touch_icon_handler_; | |
154 scoped_ptr<favicon::FaviconHandler> large_icon_handler_; | |
155 | |
156 ObserverList<favicon::FaviconDriverObserver> observer_list_; | |
157 | |
158 DISALLOW_COPY_AND_ASSIGN(FaviconTabHelper); | 35 DISALLOW_COPY_AND_ASSIGN(FaviconTabHelper); |
159 }; | 36 }; |
160 | 37 |
161 #endif // CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_ | 38 #endif // CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_H_ |
OLD | NEW |