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 // |
(...skipping 19 matching lines...) Expand all Loading... |
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. |
74 // | 71 // |
75 // When the renderer downloads the favicon SetFavicon is invoked, | 72 // When the renderer downloads favicons, it considers the entire list of |
76 // at which point we update the favicon of the NavigationEntry and notify | 73 // favicon candidates and chooses the one that best matches the preferred size |
77 // the database to save the favicon. | 74 // (or the first one if there is no preferred size). Once the matching favicon |
| 75 // has been determined, SetFavicon is called which updates the favicon of the |
| 76 // NavigationEntry and notifies the database to save the favicon. |
78 | 77 |
79 class FaviconHandler { | 78 class FaviconHandler { |
80 public: | 79 public: |
81 enum Type { | 80 enum Type { |
82 FAVICON, | 81 FAVICON, |
83 TOUCH, | 82 TOUCH, |
84 }; | 83 }; |
85 | 84 |
86 FaviconHandler(Profile* profile, | 85 FaviconHandler(Profile* profile, |
87 FaviconHandlerDelegate* delegate, | 86 FaviconHandlerDelegate* delegate, |
88 Type icon_type); | 87 Type icon_type); |
89 virtual ~FaviconHandler(); | 88 virtual ~FaviconHandler(); |
90 | 89 |
91 // Initiates loading the favicon for the specified url. | 90 // Initiates loading the favicon for the specified url. |
92 void FetchFavicon(const GURL& url); | 91 void FetchFavicon(const GURL& url); |
93 | 92 |
94 // Initiates loading an image from given |image_url|. Returns a download id | 93 // Initiates loading an image from given |image_url|. Returns a download id |
95 // for caller to track the request. When download completes, |callback| is | 94 // for caller to track the request. When download completes, |callback| is |
96 // called with the three params: the download_id, a boolean flag to indicate | 95 // called with the three params: the download_id, a boolean flag to indicate |
97 // whether the download succeeds and a SkBitmap as the downloaded image. | 96 // 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 | 97 // 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, | 98 // downloaded image is not resized to the given image_size. If 0 is passed, |
100 // the first frame of the image is returned. | 99 // the first frame of the image is returned. |
101 int DownloadImage(const GURL& image_url, | 100 int DownloadImage(const GURL& image_url, |
102 int image_size, | 101 int image_size, |
103 history::IconType icon_type, | 102 history::IconType icon_type, |
104 const FaviconTabHelper::ImageDownloadCallback& callback); | 103 const FaviconTabHelper::ImageDownloadCallback& callback); |
105 | 104 |
106 // Message Handler. Must be public, because also called from | 105 // Message Handler. Must be public, because also called from |
107 // PrerenderContents. | 106 // PrerenderContents. Collects the |image_urls| list. |
108 void OnUpdateFaviconURL(int32 page_id, | 107 void OnUpdateFaviconURL(int32 page_id, |
109 const std::vector<FaviconURL>& candidates); | 108 const std::vector<FaviconURL>& candidates); |
110 | 109 |
| 110 // Processes the current image_irls_ entry, requesting the image from the |
| 111 // history / download service. |
| 112 void ProcessCurrentUrl(); |
| 113 |
111 void OnDidDownloadFavicon(int id, | 114 void OnDidDownloadFavicon(int id, |
112 const GURL& image_url, | 115 const GURL& image_url, |
113 bool errored, | 116 bool errored, |
114 const gfx::Image& image); | 117 const gfx::Image& image); |
115 | 118 |
116 protected: | 119 protected: |
117 // These virtual methods make FaviconHandler testable and are overridden by | 120 // These virtual methods make FaviconHandler testable and are overridden by |
118 // TestFaviconHandler. | 121 // TestFaviconHandler. |
119 | 122 |
120 // Return the NavigationEntry for the active entry, or NULL if the active | 123 // 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, | 168 const GURL& image_url, |
166 const FaviconTabHelper::ImageDownloadCallback& callback, | 169 const FaviconTabHelper::ImageDownloadCallback& callback, |
167 history::IconType icon_type); | 170 history::IconType icon_type); |
168 | 171 |
169 GURL url; | 172 GURL url; |
170 GURL image_url; | 173 GURL image_url; |
171 FaviconTabHelper::ImageDownloadCallback callback; | 174 FaviconTabHelper::ImageDownloadCallback callback; |
172 history::IconType icon_type; | 175 history::IconType icon_type; |
173 }; | 176 }; |
174 | 177 |
| 178 struct FaviconCandidate { |
| 179 FaviconCandidate(); |
| 180 ~FaviconCandidate(); |
| 181 |
| 182 FaviconCandidate(const GURL& url, |
| 183 const GURL& image_url, |
| 184 const gfx::Image& image, |
| 185 const SkBitmap& bitmap, |
| 186 history::IconType icon_type); |
| 187 |
| 188 GURL url; |
| 189 GURL image_url; |
| 190 gfx::Image image; |
| 191 SkBitmap bitmap; |
| 192 history::IconType icon_type; |
| 193 }; |
| 194 |
175 // See description above class for details. | 195 // See description above class for details. |
176 void OnFaviconDataForInitialURL(FaviconService::Handle handle, | 196 void OnFaviconDataForInitialURL(FaviconService::Handle handle, |
177 history::FaviconData favicon); | 197 history::FaviconData favicon); |
178 | 198 |
179 // If the favicon has expired, asks the renderer to download the favicon. | 199 // 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 | 200 // Otherwise asks history to update the mapping between page url and icon |
181 // url with a callback to OnFaviconData when done. | 201 // url with a callback to OnFaviconData when done. |
182 void DownloadFaviconOrAskHistory(const GURL& page_url, | 202 void DownloadFaviconOrAskHistory(const GURL& page_url, |
183 const GURL& icon_url, | 203 const GURL& icon_url, |
184 history::IconType icon_type); | 204 history::IconType icon_type); |
185 | 205 |
186 // See description above class for details. | 206 // See description above class for details. |
187 void OnFaviconData(FaviconService::Handle handle, | 207 void OnFaviconData(FaviconService::Handle handle, |
188 history::FaviconData favicon); | 208 history::FaviconData favicon); |
189 | 209 |
190 // Schedules a download for the specified entry. This adds the request to | 210 // Schedules a download for the specified entry. This adds the request to |
191 // download_requests_. | 211 // download_requests_. |
192 int ScheduleDownload(const GURL& url, | 212 int ScheduleDownload(const GURL& url, |
193 const GURL& image_url, | 213 const GURL& image_url, |
194 int image_size, | 214 int image_size, |
195 history::IconType icon_type, | 215 history::IconType icon_type, |
196 const FaviconTabHelper::ImageDownloadCallback& callback); | 216 const FaviconTabHelper::ImageDownloadCallback& callback); |
197 | 217 |
198 // Sets the image data for the favicon. This is invoked asynchronously after | 218 // Updates |favicon_candidate_| and returns true if it is an exact match. |
199 // we request the TabContents to download the favicon. | 219 bool UpdateFaviconCandidate(const GURL& url, |
| 220 const GURL& image_url, |
| 221 const gfx::Image& image, |
| 222 history::IconType icon_type); |
| 223 |
| 224 // Sets the image data for the favicon. |
200 void SetFavicon(const GURL& url, | 225 void SetFavicon(const GURL& url, |
201 const GURL& icon_url, | 226 const GURL& icon_url, |
202 const gfx::Image& image, | 227 const gfx::Image& image, |
| 228 const SkBitmap& bitmap, |
203 history::IconType icon_type); | 229 history::IconType icon_type); |
204 | 230 |
205 // Converts the FAVICON's image data to an SkBitmap and sets it on the | 231 // Converts the FAVICON's image data to an SkBitmap and sets it on the |
206 // NavigationEntry. | 232 // NavigationEntry. |
207 // If the TabContents has a delegate, it is notified of the new favicon | 233 // If the TabContents has a delegate, it is notified of the new favicon |
208 // (INVALIDATE_FAVICON). | 234 // (INVALIDATE_FAVICON). |
209 void UpdateFavicon(content::NavigationEntry* entry, | 235 void UpdateFavicon(content::NavigationEntry* entry, |
210 scoped_refptr<RefCountedMemory> data); | 236 scoped_refptr<RefCountedMemory> data); |
211 void UpdateFavicon(content::NavigationEntry* entry, const gfx::Image* image); | 237 void UpdateFavicon(content::NavigationEntry* entry, const gfx::Image* image); |
212 | 238 |
213 // If the image is not already at its preferred size, scales the image such | 239 // 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 | 240 // that either the width and/or height == gfx::kFaviconSize. Does nothing if |
215 // image is empty. | 241 // the image is empty. |
216 gfx::Image ResizeFaviconIfNeeded(const gfx::Image& image); | 242 gfx::Image ResizeFaviconIfNeeded(const gfx::Image& image); |
217 | 243 |
218 void FetchFaviconInternal(); | 244 void FetchFaviconInternal(); |
219 | 245 |
220 // Return the current candidate if any. | 246 // Return the current candidate if any. |
221 FaviconURL* current_candidate() { | 247 FaviconURL* current_candidate() { |
222 return (urls_.size() > current_url_index_) ? | 248 return (image_urls_.size() > 0) ? &image_urls_[0] : NULL; |
223 &urls_[current_url_index_] : NULL; | |
224 } | 249 } |
225 | 250 |
226 // Returns the preferred_icon_size according icon_types_, 0 means no | 251 // Returns the preferred_icon_size according icon_types_, 0 means no |
227 // preference. | 252 // preference. |
228 int preferred_icon_size() { | 253 int preferred_icon_size() { |
229 return icon_types_ == history::FAVICON ? gfx::kFaviconSize : 0; | 254 return icon_types_ == history::FAVICON ? gfx::kFaviconSize : 0; |
230 } | 255 } |
231 | 256 |
232 // Used for history requests. | 257 // Used for history requests. |
233 CancelableRequestConsumer cancelable_consumer_; | 258 CancelableRequestConsumer cancelable_consumer_; |
(...skipping 12 matching lines...) Expand all Loading... |
246 bool favicon_expired_; | 271 bool favicon_expired_; |
247 | 272 |
248 // Requests to the renderer to download favicons. | 273 // Requests to the renderer to download favicons. |
249 typedef std::map<int, DownloadRequest> DownloadRequests; | 274 typedef std::map<int, DownloadRequest> DownloadRequests; |
250 DownloadRequests download_requests_; | 275 DownloadRequests download_requests_; |
251 | 276 |
252 // The combination of the supported icon types. | 277 // The combination of the supported icon types. |
253 const int icon_types_; | 278 const int icon_types_; |
254 | 279 |
255 // The prioritized favicon candidates from the page back from the renderer. | 280 // The prioritized favicon candidates from the page back from the renderer. |
256 std::vector<FaviconURL> urls_; | 281 std::deque<FaviconURL> image_urls_; |
257 | |
258 // The current candidate's index in urls_. | |
259 size_t current_url_index_; | |
260 | 282 |
261 // The FaviconData from history. | 283 // The FaviconData from history. |
262 history::FaviconData history_icon_; | 284 history::FaviconData history_icon_; |
263 | 285 |
264 // The Profile associated with this handler. | 286 // The Profile associated with this handler. |
265 Profile* profile_; | 287 Profile* profile_; |
266 | 288 |
267 // This handler's delegate. | 289 // This handler's delegate. |
268 FaviconHandlerDelegate* delegate_; // weak | 290 FaviconHandlerDelegate* delegate_; // weak |
269 | 291 |
| 292 // Current favicon candidate. |
| 293 FaviconCandidate favicon_candidate_; |
| 294 |
270 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); | 295 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); |
271 }; | 296 }; |
272 | 297 |
273 #endif // CHROME_BROWSER_FAVICON_FAVICON_HANDLER_H_ | 298 #endif // CHROME_BROWSER_FAVICON_FAVICON_HANDLER_H_ |
OLD | NEW |