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_FAVICON_HANDLER_H_ | 5 #ifndef CHROME_BROWSER_FAVICON_FAVICON_HANDLER_H_ |
6 #define CHROME_BROWSER_FAVICON_FAVICON_HANDLER_H_ | 6 #define CHROME_BROWSER_FAVICON_FAVICON_HANDLER_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_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "chrome/browser/cancelable_request.h" | 14 #include "chrome/browser/cancelable_request.h" |
15 #include "chrome/browser/favicon/favicon_service.h" | 15 #include "chrome/browser/favicon/favicon_service.h" |
16 #include "chrome/browser/favicon/favicon_tab_helper.h" | 16 #include "chrome/browser/favicon/favicon_tab_helper.h" |
17 #include "chrome/common/favicon_url.h" | 17 #include "chrome/common/favicon_url.h" |
18 #include "chrome/common/ref_counted_util.h" | 18 #include "chrome/common/ref_counted_util.h" |
19 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
20 #include "third_party/skia/include/core/SkBitmap.h" | |
20 #include "ui/gfx/favicon_size.h" | 21 #include "ui/gfx/favicon_size.h" |
22 #include "ui/gfx/image/image.h" | |
21 | 23 |
22 class FaviconHandlerDelegate; | 24 class FaviconHandlerDelegate; |
23 class Profile; | 25 class Profile; |
24 class RefCountedMemory; | 26 class RefCountedMemory; |
25 class SkBitmap; | |
26 class TabContents; | 27 class TabContents; |
27 | 28 |
28 namespace content { | 29 namespace content { |
29 class NavigationEntry; | 30 class NavigationEntry; |
30 } | 31 } |
31 | 32 |
32 namespace gfx { | |
33 class Image; | |
34 } | |
35 | |
36 // FaviconHandler works with FaviconTabHelper to fetch the specific type of | 33 // FaviconHandler works with FaviconTabHelper to fetch the specific type of |
37 // favicon. | 34 // favicon. |
38 // | 35 // |
39 // FetchFavicon requests the favicon from the favicon service which in turn | 36 // FetchFavicon requests the favicon from the favicon service which in turn |
40 // requests the favicon from the history database. At this point | 37 // requests the favicon from the history database. At this point |
41 // we only know the URL of the page, and not necessarily the url of the | 38 // we only know the URL of the page, and not necessarily the url of the |
42 // favicon. To ensure we handle reloading stale favicons as well as | 39 // favicon. To ensure we handle reloading stale favicons as well as |
43 // reloading a favicon on page reload we always request the favicon from | 40 // reloading a favicon on page reload we always request the favicon from |
44 // history regardless of whether the NavigationEntry has a favicon. | 41 // history regardless of whether the NavigationEntry has a favicon. |
45 // | 42 // |
46 // After the navigation two types of events are delivered (which is | 43 // After the navigation two types of events are delivered (which is |
47 // first depends upon who is faster): notification from the history | 44 // first depends upon who is faster): notification from the history |
48 // db on our request for the favicon (OnFaviconDataForInitialURL), | 45 // db on our request for the favicon (OnFaviconDataForInitialURL), |
49 // or a message from the renderer giving us the URL of the favicon for | 46 // or a message from the renderer giving us the URL of the favicon for |
50 // the page (SetFaviconURL). | 47 // the page (SetFaviconURL). |
51 // . If the history db has a valid up to date favicon for the page, we update | 48 // . If the history db has a valid up to date favicon for the page, we update |
52 // the NavigationEntry and use the favicon. | 49 // the NavigationEntry and use the favicon. |
53 // . When we receive the favicon url if it matches that of the NavigationEntry | 50 // . When we receive the favicon url if it matches that of the NavigationEntry |
54 // and the NavigationEntry's favicon is set, we do nothing (everything is | 51 // and the NavigationEntry's favicon is set, we do nothing (everything is |
55 // ok). | 52 // ok). |
56 // . On the other hand if the database does not know the favicon for url, or | 53 // . On the other hand if the database does not know the favicon for url, or |
57 // the favicon is out date, or the URL from the renderer does not match that | 54 // the favicon is out date, or the URL from the renderer does not match that |
58 // NavigationEntry we proceed to DownloadFaviconOrAskHistory. Before we | 55 // NavigationEntry we proceed to DownloadFaviconOrAskHistory. Before we |
59 // invoke DownloadFaviconOrAskHistory we wait until we've received both | 56 // invoke DownloadFaviconOrAskHistory we wait until we've received both |
60 // the favicon url and the callback from history. We wait to ensure we | 57 // the favicon url and the callback from history. We wait to ensure we |
61 // truly know both the favicon url and the state of the database. | 58 // truly know both the favicon url and the state of the database. |
62 // | 59 // |
63 // DownloadFaviconOrAskHistory does the following: | 60 // DownloadFaviconOrAskHistory does the following: |
sky
2012/03/16 23:29:15
Update comments with the new flow.
stevenjb
2012/03/17 00:23:07
Doh. Thanks. Done.
| |
64 // . If we have a valid favicon, but it is expired we ask the renderer to | 61 // . If we have a valid favicon, but it is expired we ask the renderer to |
65 // download the favicon. | 62 // download the favicon. |
66 // . Otherwise we ask the history database to update the mapping from | 63 // . Otherwise we ask the history database to update the mapping from |
67 // page url to favicon url and call us back with the favicon. Remember, it is | 64 // page url to favicon url and call us back with the favicon. Remember, it is |
68 // possible for the db to already have the favicon, just not the mapping | 65 // possible for the db to already have the favicon, just not the mapping |
69 // between page to favicon url. The callback for this is OnFaviconData. | 66 // between page to favicon url. The callback for this is OnFaviconData. |
70 // | 67 // |
71 // OnFaviconData either updates the favicon of the NavigationEntry (if the | 68 // OnFaviconData either updates the favicon of the NavigationEntry (if the |
72 // db knew about the favicon), or requests the renderer to download the | 69 // db knew about the favicon), or requests the renderer to download the |
73 // favicon. | 70 // favicon. |
(...skipping 23 matching lines...) Expand all Loading... | |
97 // whether the download succeeds and a SkBitmap as the downloaded image. | 94 // whether the download succeeds and a SkBitmap as the downloaded image. |
98 // Note that |image_size| is a hint for images with multiple sizes. The | 95 // Note that |image_size| is a hint for images with multiple sizes. The |
99 // downloaded image is not resized to the given image_size. If 0 is passed, | 96 // downloaded image is not resized to the given image_size. If 0 is passed, |
100 // the first frame of the image is returned. | 97 // the first frame of the image is returned. |
101 int DownloadImage(const GURL& image_url, | 98 int DownloadImage(const GURL& image_url, |
102 int image_size, | 99 int image_size, |
103 history::IconType icon_type, | 100 history::IconType icon_type, |
104 const FaviconTabHelper::ImageDownloadCallback& callback); | 101 const FaviconTabHelper::ImageDownloadCallback& callback); |
105 | 102 |
106 // Message Handler. Must be public, because also called from | 103 // Message Handler. Must be public, because also called from |
107 // PrerenderContents. | 104 // PrerenderContents. Collects the |image_urls| list. |
108 void OnUpdateFaviconURL(int32 page_id, | 105 void OnUpdateFaviconURL(int32 page_id, |
109 const std::vector<FaviconURL>& candidates); | 106 const std::vector<FaviconURL>& candidates); |
110 | 107 |
108 // Processes the current image_irls_ entry, requesting the image from the | |
109 // history / download service. | |
110 void ProcessCurrentUrl(); | |
111 | |
111 void OnDidDownloadFavicon(int id, | 112 void OnDidDownloadFavicon(int id, |
112 const GURL& image_url, | 113 const GURL& image_url, |
113 bool errored, | 114 bool errored, |
114 const gfx::Image& image); | 115 const gfx::Image& image); |
115 | 116 |
116 protected: | 117 protected: |
117 // These virtual methods make FaviconHandler testable and are overridden by | 118 // These virtual methods make FaviconHandler testable and are overridden by |
118 // TestFaviconHandler. | 119 // TestFaviconHandler. |
119 | 120 |
120 // Return the NavigationEntry for the active entry, or NULL if the active | 121 // Return the NavigationEntry for the active entry, or NULL if the active |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 const GURL& image_url, | 166 const GURL& image_url, |
166 const FaviconTabHelper::ImageDownloadCallback& callback, | 167 const FaviconTabHelper::ImageDownloadCallback& callback, |
167 history::IconType icon_type); | 168 history::IconType icon_type); |
168 | 169 |
169 GURL url; | 170 GURL url; |
170 GURL image_url; | 171 GURL image_url; |
171 FaviconTabHelper::ImageDownloadCallback callback; | 172 FaviconTabHelper::ImageDownloadCallback callback; |
172 history::IconType icon_type; | 173 history::IconType icon_type; |
173 }; | 174 }; |
174 | 175 |
176 struct FaviconCandidate { | |
177 FaviconCandidate(); | |
178 ~FaviconCandidate(); | |
179 | |
180 FaviconCandidate(const GURL& url, | |
181 const GURL& image_url, | |
182 const gfx::Image& image, | |
183 const SkBitmap& bitmap, | |
184 history::IconType icon_type); | |
185 | |
186 GURL url; | |
187 GURL image_url; | |
188 gfx::Image image; | |
189 SkBitmap bitmap; | |
190 history::IconType icon_type; | |
191 }; | |
192 | |
175 // See description above class for details. | 193 // See description above class for details. |
176 void OnFaviconDataForInitialURL(FaviconService::Handle handle, | 194 void OnFaviconDataForInitialURL(FaviconService::Handle handle, |
177 history::FaviconData favicon); | 195 history::FaviconData favicon); |
178 | 196 |
179 // If the favicon has expired, asks the renderer to download the favicon. | 197 // If the favicon has expired, asks the renderer to download the favicon. |
180 // Otherwise asks history to update the mapping between page url and icon | 198 // Otherwise asks history to update the mapping between page url and icon |
181 // url with a callback to OnFaviconData when done. | 199 // url with a callback to OnFaviconData when done. |
182 void DownloadFaviconOrAskHistory(const GURL& page_url, | 200 void DownloadFaviconOrAskHistory(const GURL& page_url, |
183 const GURL& icon_url, | 201 const GURL& icon_url, |
184 history::IconType icon_type); | 202 history::IconType icon_type); |
185 | 203 |
186 // See description above class for details. | 204 // See description above class for details. |
187 void OnFaviconData(FaviconService::Handle handle, | 205 void OnFaviconData(FaviconService::Handle handle, |
188 history::FaviconData favicon); | 206 history::FaviconData favicon); |
189 | 207 |
190 // Schedules a download for the specified entry. This adds the request to | 208 // Schedules a download for the specified entry. This adds the request to |
191 // download_requests_. | 209 // download_requests_. |
192 int ScheduleDownload(const GURL& url, | 210 int ScheduleDownload(const GURL& url, |
193 const GURL& image_url, | 211 const GURL& image_url, |
194 int image_size, | 212 int image_size, |
195 history::IconType icon_type, | 213 history::IconType icon_type, |
196 const FaviconTabHelper::ImageDownloadCallback& callback); | 214 const FaviconTabHelper::ImageDownloadCallback& callback); |
197 | 215 |
198 // Sets the image data for the favicon. This is invoked asynchronously after | 216 // Updates |favicon_candidate_| and returns true if it is an exact match. |
199 // we request the TabContents to download the favicon. | 217 bool UpdateFaviconCandidate(const GURL& url, |
218 const GURL& image_url, | |
219 const gfx::Image& image, | |
220 history::IconType icon_type); | |
221 | |
222 // Sets the image data for the favicon. | |
200 void SetFavicon(const GURL& url, | 223 void SetFavicon(const GURL& url, |
201 const GURL& icon_url, | 224 const GURL& icon_url, |
202 const gfx::Image& image, | 225 const gfx::Image& image, |
226 const SkBitmap& bitmap, | |
203 history::IconType icon_type); | 227 history::IconType icon_type); |
204 | 228 |
205 // Converts the FAVICON's image data to an SkBitmap and sets it on the | 229 // Converts the FAVICON's image data to an SkBitmap and sets it on the |
206 // NavigationEntry. | 230 // NavigationEntry. |
207 // If the TabContents has a delegate, it is notified of the new favicon | 231 // If the TabContents has a delegate, it is notified of the new favicon |
208 // (INVALIDATE_FAVICON). | 232 // (INVALIDATE_FAVICON). |
209 void UpdateFavicon(content::NavigationEntry* entry, | 233 void UpdateFavicon(content::NavigationEntry* entry, |
210 scoped_refptr<RefCountedMemory> data); | 234 scoped_refptr<RefCountedMemory> data); |
211 void UpdateFavicon(content::NavigationEntry* entry, const gfx::Image* image); | 235 void UpdateFavicon(content::NavigationEntry* entry, const gfx::Image* image); |
212 | 236 |
213 // If the image is not already at its preferred size, scales the image such | 237 // If the image is not already at its preferred size, scales the image such |
214 // that either the width and/or height is 16 pixels wide. Does nothing if the | 238 // that either the width and/or height == gfx::kFaviconSize. Does nothing if |
215 // image is empty. | 239 // the image is empty. |
216 gfx::Image ResizeFaviconIfNeeded(const gfx::Image& image); | 240 gfx::Image ResizeFaviconIfNeeded(const gfx::Image& image); |
217 | 241 |
218 void FetchFaviconInternal(); | 242 void FetchFaviconInternal(); |
219 | 243 |
220 // Return the current candidate if any. | 244 // Return the current candidate if any. |
221 FaviconURL* current_candidate() { | 245 FaviconURL* current_candidate() { |
222 return (urls_.size() > current_url_index_) ? | 246 return (image_urls_.size() > 0) ? &image_urls_[0] : NULL; |
223 &urls_[current_url_index_] : NULL; | |
224 } | 247 } |
225 | 248 |
226 // Returns the preferred_icon_size according icon_types_, 0 means no | 249 // Returns the preferred_icon_size according icon_types_, 0 means no |
227 // preference. | 250 // preference. |
228 int preferred_icon_size() { | 251 int preferred_icon_size() { |
229 return icon_types_ == history::FAVICON ? gfx::kFaviconSize : 0; | 252 return icon_types_ == history::FAVICON ? gfx::kFaviconSize : 0; |
230 } | 253 } |
231 | 254 |
232 // Used for history requests. | 255 // Used for history requests. |
233 CancelableRequestConsumer cancelable_consumer_; | 256 CancelableRequestConsumer cancelable_consumer_; |
(...skipping 12 matching lines...) Expand all Loading... | |
246 bool favicon_expired_; | 269 bool favicon_expired_; |
247 | 270 |
248 // Requests to the renderer to download favicons. | 271 // Requests to the renderer to download favicons. |
249 typedef std::map<int, DownloadRequest> DownloadRequests; | 272 typedef std::map<int, DownloadRequest> DownloadRequests; |
250 DownloadRequests download_requests_; | 273 DownloadRequests download_requests_; |
251 | 274 |
252 // The combination of the supported icon types. | 275 // The combination of the supported icon types. |
253 const int icon_types_; | 276 const int icon_types_; |
254 | 277 |
255 // The prioritized favicon candidates from the page back from the renderer. | 278 // The prioritized favicon candidates from the page back from the renderer. |
256 std::vector<FaviconURL> urls_; | 279 std::deque<FaviconURL> image_urls_; |
257 | |
258 // The current candidate's index in urls_. | |
259 size_t current_url_index_; | |
260 | 280 |
261 // The FaviconData from history. | 281 // The FaviconData from history. |
262 history::FaviconData history_icon_; | 282 history::FaviconData history_icon_; |
263 | 283 |
264 // The Profile associated with this handler. | 284 // The Profile associated with this handler. |
265 Profile* profile_; | 285 Profile* profile_; |
266 | 286 |
267 // This handler's delegate. | 287 // This handler's delegate. |
268 FaviconHandlerDelegate* delegate_; // weak | 288 FaviconHandlerDelegate* delegate_; // weak |
269 | 289 |
290 // Current favicon candidate. | |
291 FaviconCandidate favicon_candidate_; | |
292 | |
270 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); | 293 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); |
271 }; | 294 }; |
272 | 295 |
273 #endif // CHROME_BROWSER_FAVICON_FAVICON_HANDLER_H_ | 296 #endif // CHROME_BROWSER_FAVICON_FAVICON_HANDLER_H_ |
OLD | NEW |