OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_HELPER_H__ | 5 #ifndef CHROME_BROWSER_FAVICON_HELPER_H__ |
6 #define CHROME_BROWSER_FAVICON_HELPER_H__ | 6 #define CHROME_BROWSER_FAVICON_HELPER_H__ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/ref_counted.h" | 13 #include "base/ref_counted.h" |
14 #include "chrome/browser/favicon_service.h" | 14 #include "chrome/browser/favicon_service.h" |
15 #include "chrome/common/icon_messages.h" | |
15 #include "chrome/common/ref_counted_util.h" | 16 #include "chrome/common/ref_counted_util.h" |
16 #include "content/browser/cancelable_request.h" | 17 #include "content/browser/cancelable_request.h" |
17 #include "content/browser/tab_contents/tab_contents_observer.h" | 18 #include "content/browser/tab_contents/tab_contents_observer.h" |
18 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
19 | 20 |
20 class NavigationEntry; | 21 class NavigationEntry; |
21 class Profile; | 22 class Profile; |
22 class RefCountedMemory; | 23 class RefCountedMemory; |
23 class SkBitmap; | 24 class SkBitmap; |
24 class TabContents; | 25 class TabContents; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 // OnFaviconData either updates the favicon of the NavigationEntry (if the | 61 // OnFaviconData either updates the favicon of the NavigationEntry (if the |
61 // db knew about the favicon), or requests the renderer to download the | 62 // db knew about the favicon), or requests the renderer to download the |
62 // favicon. | 63 // favicon. |
63 // | 64 // |
64 // When the renderer downloads the favicon SetFaviconImageData is invoked, | 65 // When the renderer downloads the favicon SetFaviconImageData is invoked, |
65 // at which point we update the favicon of the NavigationEntry and notify | 66 // at which point we update the favicon of the NavigationEntry and notify |
66 // the database to save the favicon. | 67 // the database to save the favicon. |
67 | 68 |
68 class FaviconHelper : public TabContentsObserver { | 69 class FaviconHelper : public TabContentsObserver { |
69 public: | 70 public: |
70 explicit FaviconHelper(TabContents* tab_contents); | 71 enum Types { |
72 FAVICON, | |
73 TOUCH, | |
74 }; | |
75 | |
76 FaviconHelper(TabContents* tab_contents, Types icon_type); | |
71 virtual ~FaviconHelper(); | 77 virtual ~FaviconHelper(); |
72 | 78 |
73 // Initiates loading the favicon for the specified url. | 79 // Initiates loading the favicon for the specified url. |
74 void FetchFavicon(const GURL& url); | 80 void FetchFavicon(const GURL& url); |
75 | 81 |
76 // Initiates loading an image from given |image_url|. Returns a download id | 82 // Initiates loading an image from given |image_url|. Returns a download id |
77 // for caller to track the request. When download completes, |callback| is | 83 // for caller to track the request. When download completes, |callback| is |
78 // called with the three params: the download_id, a boolean flag to indicate | 84 // called with the three params: the download_id, a boolean flag to indicate |
79 // whether the download succeeds and a SkBitmap as the downloaded image. | 85 // whether the download succeeds and a SkBitmap as the downloaded image. |
80 // Note that |image_size| is a hint for images with multiple sizes. The | 86 // Note that |image_size| is a hint for images with multiple sizes. The |
81 // downloaded image is not resized to the given image_size. If 0 is passed, | 87 // downloaded image is not resized to the given image_size. If 0 is passed, |
82 // the first frame of the image is returned. | 88 // the first frame of the image is returned. |
83 typedef Callback3<int, bool, const SkBitmap&>::Type ImageDownloadCallback; | 89 typedef Callback3<int, bool, const SkBitmap&>::Type ImageDownloadCallback; |
84 int DownloadImage(const GURL& image_url, int image_size, | 90 int DownloadImage(const GURL& image_url, |
91 int image_size, | |
92 history::IconType icon_type, | |
85 ImageDownloadCallback* callback); | 93 ImageDownloadCallback* callback); |
86 | 94 |
87 // Message Handler. Must be public, because also called from | 95 // Message Handler. Must be public, because also called from |
88 // PrerenderContents. | 96 // PrerenderContents. |
89 void OnUpdateFaviconURL(int32 page_id, const GURL& icon_url); | 97 void OnUpdateFaviconURL(int32 page_id, |
98 const std::vector<FaviconURL>& candidates); | |
90 | 99 |
91 private: | 100 private: |
92 struct DownloadRequest { | 101 struct DownloadRequest { |
93 DownloadRequest() {} | 102 DownloadRequest() |
103 : callback(NULL), | |
sky
2011/03/23 22:49:31
Move the constructor and destructor to favicon_hel
michaelbai
2011/03/23 23:26:49
Done.
| |
104 icon_type(history::INVALID_ICON) {} | |
105 | |
94 DownloadRequest(const GURL& url, | 106 DownloadRequest(const GURL& url, |
95 const GURL& image_url, | 107 const GURL& image_url, |
96 ImageDownloadCallback* callback) | 108 ImageDownloadCallback* callback, |
109 history::IconType icon_type) | |
97 : url(url), | 110 : url(url), |
98 image_url(image_url), | 111 image_url(image_url), |
99 callback(callback) { } | 112 callback(callback), |
113 icon_type(icon_type) {} | |
100 | 114 |
101 GURL url; | 115 GURL url; |
102 GURL image_url; | 116 GURL image_url; |
103 ImageDownloadCallback* callback; | 117 ImageDownloadCallback* callback; |
118 history::IconType icon_type; | |
104 }; | 119 }; |
105 | 120 |
121 static bool do_url_and_icon_match (const FaviconURL& favicon_url, | |
122 const GURL& url, | |
123 history::IconType icon_type) { | |
124 return favicon_url.icon_url == url && | |
125 favicon_url.icon_type == static_cast<IconType>(icon_type); | |
126 } | |
127 | |
106 // TabContentsObserver implementation. | 128 // TabContentsObserver implementation. |
107 virtual bool OnMessageReceived(const IPC::Message& message); | 129 virtual bool OnMessageReceived(const IPC::Message& message); |
108 | 130 |
109 void OnDidDownloadFavicon(int id, | 131 void OnDidDownloadFavicon(int id, |
110 const GURL& image_url, | 132 const GURL& image_url, |
111 bool errored, | 133 bool errored, |
112 const SkBitmap& image); | 134 const SkBitmap& image); |
113 | 135 |
114 // Return the NavigationEntry for the active entry, or NULL if the active | 136 // Return the NavigationEntry for the active entry, or NULL if the active |
115 // entries URL does not match that of the URL last passed to FetchFavicon. | 137 // entries URL does not match that of the URL last passed to FetchFavicon. |
116 NavigationEntry* GetEntry(); | 138 NavigationEntry* GetEntry(); |
117 | 139 |
118 Profile* profile(); | 140 Profile* profile(); |
119 | 141 |
120 FaviconService* GetFaviconService(); | 142 FaviconService* GetFaviconService(); |
121 | 143 |
122 // See description above class for details. | 144 // See description above class for details. |
123 void OnFaviconDataForInitialURL(FaviconService::Handle handle, | 145 void OnFaviconDataForInitialURL(FaviconService::Handle handle, |
124 history::FaviconData favicon); | 146 history::FaviconData favicon); |
125 | 147 |
126 // If the favicon has expired, asks the renderer to download the favicon. | 148 // If the favicon has expired, asks the renderer to download the favicon. |
127 // Otherwise asks history to update the mapping between page url and icon | 149 // Otherwise asks history to update the mapping between page url and icon |
128 // url with a callback to OnFaviconData when done. | 150 // url with a callback to OnFaviconData when done. |
129 void DownloadFaviconOrAskHistory(NavigationEntry* entry); | 151 void DownloadFaviconOrAskHistory(const GURL& page_url, |
152 const GURL& icon_url, | |
153 history::IconType icon_type); | |
130 | 154 |
131 // See description above class for details. | 155 // See description above class for details. |
132 void OnFaviconData(FaviconService::Handle handle, | 156 void OnFaviconData(FaviconService::Handle handle, |
133 history::FaviconData favicon); | 157 history::FaviconData favicon); |
134 | 158 |
135 // Schedules a download for the specified entry. This adds the request to | 159 // Schedules a download for the specified entry. This adds the request to |
136 // download_requests_. | 160 // download_requests_. |
137 int ScheduleDownload(const GURL& url, const GURL& image_url, int image_size, | 161 int ScheduleDownload(const GURL& url, |
162 const GURL& image_url, | |
163 int image_size, | |
164 history::IconType icon_type, | |
138 ImageDownloadCallback* callback); | 165 ImageDownloadCallback* callback); |
139 | 166 |
140 // Sets the image data for the favicon. This is invoked asynchronously after | 167 // Sets the image data for the favicon. This is invoked asynchronously after |
141 // we request the TabContents to download the favicon. | 168 // we request the TabContents to download the favicon. |
142 void SetFavicon(const GURL& url, const GURL& icon_url, const SkBitmap& image); | 169 void SetFavicon(const GURL& url, |
170 const GURL& icon_url, | |
171 const SkBitmap& image, | |
172 history::IconType icon_type); | |
143 | 173 |
144 // Converts the image data to an SkBitmap and sets it on the NavigationEntry. | 174 // Converts the FAVICON's image data to an SkBitmap and sets it on the |
175 // NavigationEntry. | |
145 // If the TabContents has a delegate, it is notified of the new favicon | 176 // If the TabContents has a delegate, it is notified of the new favicon |
146 // (INVALIDATE_FAVICON). | 177 // (INVALIDATE_FAVICON). |
147 void UpdateFavicon(NavigationEntry* entry, | 178 void UpdateFavicon(NavigationEntry* entry, |
148 scoped_refptr<RefCountedMemory> data); | 179 scoped_refptr<RefCountedMemory> data); |
149 void UpdateFavicon(NavigationEntry* entry, const SkBitmap& image); | 180 void UpdateFavicon(NavigationEntry* entry, const SkBitmap& image); |
150 | 181 |
151 // Scales the image such that either the width and/or height is 16 pixels | 182 // Scales the image such that either the width and/or height is 16 pixels |
152 // wide. Does nothing if the image is empty. | 183 // wide. Does nothing if the image is empty. |
153 SkBitmap ConvertToFaviconSize(const SkBitmap& image); | 184 SkBitmap ConvertToFaviconSize(const SkBitmap& image); |
154 | 185 |
155 // Returns true if the favicon should be saved. | 186 // Returns true if the favicon should be saved. |
156 bool ShouldSaveFavicon(const GURL& url); | 187 bool ShouldSaveFavicon(const GURL& url); |
157 | 188 |
189 // Return the current candidate if any. | |
190 FaviconURL* current_candidate() { | |
191 return (urls_.size() > current_url_index_) ? | |
192 &urls_[current_url_index_] : NULL; | |
193 } | |
194 | |
158 // Used for history requests. | 195 // Used for history requests. |
159 CancelableRequestConsumer cancelable_consumer_; | 196 CancelableRequestConsumer cancelable_consumer_; |
160 | 197 |
161 // URL of the page we're requesting the favicon for. | 198 // URL of the page we're requesting the favicon for. |
162 GURL url_; | 199 GURL url_; |
163 | 200 |
164 // Whether we got the url for the page back from the renderer. | |
165 // See "Favicon Details" in tab_contents.cc for more details. | |
166 bool got_favicon_url_; | |
167 | |
168 // Whether we got the initial response for the favicon back from the renderer. | 201 // Whether we got the initial response for the favicon back from the renderer. |
169 // See "Favicon Details" in tab_contents.cc for more details. | 202 // See "Favicon Details" in tab_contents.cc for more details. |
170 bool got_favicon_from_history_; | 203 bool got_favicon_from_history_; |
171 | 204 |
172 // Whether the favicon is out of date. If true, it means history knows about | 205 // Whether the favicon is out of date. If true, it means history knows about |
173 // the favicon, but we need to download the favicon because the icon has | 206 // the favicon, but we need to download the favicon because the icon has |
174 // expired. | 207 // expired. |
175 // See "Favicon Details" in tab_contents.cc for more details. | 208 // See "Favicon Details" in tab_contents.cc for more details. |
176 bool favicon_expired_; | 209 bool favicon_expired_; |
177 | 210 |
178 // Requests to the renderer to download favicons. | 211 // Requests to the renderer to download favicons. |
179 typedef std::map<int, DownloadRequest> DownloadRequests; | 212 typedef std::map<int, DownloadRequest> DownloadRequests; |
180 DownloadRequests download_requests_; | 213 DownloadRequests download_requests_; |
181 | 214 |
215 // The combination of the supported icon types. | |
216 const int icon_types_; | |
217 | |
218 // The prioritized favicon candidates from the page back from the renderer. | |
219 std::vector<FaviconURL> urls_; | |
220 | |
221 // The current candidate's index in urls_. | |
222 size_t current_url_index_; | |
223 | |
224 // The FaviconData from history. | |
225 history::FaviconData history_icon_; | |
226 | |
227 // The preferred icon size for supported icon types. | |
228 int preferred_icon_size_; | |
229 | |
182 DISALLOW_COPY_AND_ASSIGN(FaviconHelper); | 230 DISALLOW_COPY_AND_ASSIGN(FaviconHelper); |
183 }; | 231 }; |
184 | 232 |
185 #endif // CHROME_BROWSER_FAVICON_HELPER_H__ | 233 #endif // CHROME_BROWSER_FAVICON_HELPER_H__ |
OLD | NEW |