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 COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ | 5 #ifndef COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ |
6 #define COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ | 6 #define COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/weak_ptr.h" |
16 #include "base/task/cancelable_task_tracker.h" | 17 #include "base/task/cancelable_task_tracker.h" |
17 #include "components/favicon/core/favicon_driver_observer.h" | 18 #include "components/favicon/core/favicon_driver_observer.h" |
18 #include "components/favicon/core/favicon_url.h" | 19 #include "components/favicon/core/favicon_url.h" |
19 #include "components/favicon_base/favicon_callback.h" | 20 #include "components/favicon_base/favicon_callback.h" |
20 #include "ui/gfx/favicon_size.h" | 21 #include "ui/gfx/favicon_size.h" |
21 #include "ui/gfx/image/image.h" | 22 #include "ui/gfx/image/image.h" |
22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
23 | 24 |
24 class SkBitmap; | 25 class SkBitmap; |
25 | 26 |
26 namespace favicon { | 27 namespace favicon { |
27 | 28 |
28 class FaviconDriver; | |
29 class FaviconService; | 29 class FaviconService; |
30 class TestFaviconHandler; | 30 class TestFaviconHandler; |
31 | 31 |
32 // FaviconHandler works with FaviconDriver to fetch the specific type of | 32 // FaviconHandler works with FaviconDriver to fetch the specific type of |
33 // favicon. | 33 // favicon. |
34 // | 34 // |
35 // FetchFavicon requests the favicon from the favicon service which in turn | 35 // FetchFavicon requests the favicon from the favicon service which in turn |
36 // requests the favicon from the history database. At this point | 36 // requests the favicon from the history database. At this point |
37 // we only know the URL of the page, and not necessarily the url of the | 37 // we only know the URL of the page, and not necessarily the url of the |
38 // favicon. To ensure we handle reloading stale favicons as well as | 38 // favicon. To ensure we handle reloading stale favicons as well as |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 // | 70 // |
71 // When the renderer downloads favicons, it considers the entire list of | 71 // When the renderer downloads favicons, it considers the entire list of |
72 // favicon candidates, if |download_largest_favicon_| is true, the largest | 72 // favicon candidates, if |download_largest_favicon_| is true, the largest |
73 // favicon will be used, otherwise the one that best matches the preferred size | 73 // favicon will be used, otherwise the one that best matches the preferred size |
74 // is chosen (or the first one if there is no preferred size). Once the | 74 // is chosen (or the first one if there is no preferred size). Once the |
75 // matching favicon has been determined, SetFavicon is called which updates | 75 // matching favicon has been determined, SetFavicon is called which updates |
76 // the page's favicon and notifies the database to save the favicon. | 76 // the page's favicon and notifies the database to save the favicon. |
77 | 77 |
78 class FaviconHandler { | 78 class FaviconHandler { |
79 public: | 79 public: |
| 80 class Delegate { |
| 81 public: |
| 82 // Mimics WebContents::ImageDownloadCallback. |
| 83 typedef base::Callback<void( |
| 84 int id, |
| 85 int status_code, |
| 86 const GURL& image_url, |
| 87 const std::vector<SkBitmap>& bitmaps, |
| 88 const std::vector<gfx::Size>& original_bitmap_sizes)> |
| 89 ImageDownloadCallback; |
| 90 |
| 91 // Starts the download for the given favicon. When finished, the callback |
| 92 // is called with the results. Returns the unique id of the download |
| 93 // request, which will also be passed to the callback. In case of error, 0 |
| 94 // is returned and no callback will be called. |
| 95 // Bitmaps with pixel sizes larger than |max_bitmap_size| are filtered out |
| 96 // from the bitmap results. If there are no bitmap results <= |
| 97 // |max_bitmap_size|, the smallest bitmap is resized to |max_bitmap_size| |
| 98 // and is the only result. A |max_bitmap_size| of 0 means unlimited. |
| 99 virtual int DownloadImage(const GURL& url, |
| 100 int max_image_size, |
| 101 ImageDownloadCallback callback) = 0; |
| 102 |
| 103 // Returns whether the user is operating in an off-the-record context. |
| 104 virtual bool IsOffTheRecord() = 0; |
| 105 |
| 106 // Returns whether |url| is bookmarked. |
| 107 virtual bool IsBookmarked(const GURL& url) = 0; |
| 108 |
| 109 // Notifies that the favicon image has been updated. Most delegates |
| 110 // propagate the notification to FaviconDriverObserver::OnFaviconUpdated(). |
| 111 // See its documentation for details. |
| 112 virtual void OnFaviconUpdated( |
| 113 const GURL& page_url, |
| 114 FaviconDriverObserver::NotificationIconType notification_icon_type, |
| 115 const GURL& icon_url, |
| 116 bool icon_url_changed, |
| 117 const gfx::Image& image) = 0; |
| 118 }; |
| 119 |
| 120 // |delegate| must not be nullptr and must outlive this class. |
80 FaviconHandler(FaviconService* service, | 121 FaviconHandler(FaviconService* service, |
81 FaviconDriver* driver, | 122 Delegate* delegate, |
82 FaviconDriverObserver::NotificationIconType handler_type); | 123 FaviconDriverObserver::NotificationIconType handler_type); |
83 virtual ~FaviconHandler(); | 124 virtual ~FaviconHandler(); |
84 | 125 |
85 // Returns the bit mask of favicon_base::IconType based on the handler's type. | |
86 static int GetIconTypesFromHandlerType( | |
87 FaviconDriverObserver::NotificationIconType handler_type); | |
88 | |
89 // Initiates loading the favicon for the specified url. | 126 // Initiates loading the favicon for the specified url. |
90 void FetchFavicon(const GURL& url); | 127 void FetchFavicon(const GURL& url); |
91 | 128 |
92 // Message Handler. Must be public, because also called from | 129 // Message Handler. Must be public, because also called from |
93 // PrerenderContents. Collects the |image_urls| list. | 130 // PrerenderContents. Collects the |image_urls| list. |
94 void OnUpdateFaviconURL(const GURL& page_url, | 131 void OnUpdateFaviconURL(const GURL& page_url, |
95 const std::vector<favicon::FaviconURL>& candidates); | 132 const std::vector<favicon::FaviconURL>& candidates); |
96 | 133 |
97 // Called when the history request for favicon data mapped to |url_| has | |
98 // completed and the renderer has told us the icon URLs used by |url_| | |
99 void OnGotInitialHistoryDataAndIconURLCandidates(); | |
100 | |
101 // Message handler for ImageHostMsg_DidDownloadImage. Called when the image | |
102 // at |image_url| has been downloaded. | |
103 // |bitmaps| is a list of all the frames of the image at |image_url|. | |
104 // |original_bitmap_sizes| are the sizes of |bitmaps| before they were resized | |
105 // to the maximum bitmap size passed to DownloadFavicon(). | |
106 void OnDidDownloadFavicon( | |
107 int id, | |
108 const GURL& image_url, | |
109 const std::vector<SkBitmap>& bitmaps, | |
110 const std::vector<gfx::Size>& original_bitmap_sizes); | |
111 | |
112 // For testing. | 134 // For testing. |
113 const std::vector<favicon::FaviconURL>& image_urls() const { | 135 const std::vector<favicon::FaviconURL>& image_urls() const { |
114 return image_urls_; | 136 return image_urls_; |
115 } | 137 } |
116 | 138 |
117 // Returns whether the handler is waiting for a download to complete or for | 139 // Returns whether the handler is waiting for a download to complete or for |
118 // data from the FaviconService. Reserved for testing. | 140 // data from the FaviconService. Reserved for testing. |
119 bool HasPendingTasksForTest(); | 141 bool HasPendingTasksForTest(); |
120 | 142 |
121 protected: | 143 protected: |
122 // These virtual methods make FaviconHandler testable and are overridden by | 144 // These virtual methods make FaviconHandler testable and are overridden by |
123 // TestFaviconHandler. | 145 // TestFaviconHandler. |
124 | 146 |
125 // Asks the render to download favicon, returns the request id. | |
126 virtual int DownloadFavicon(const GURL& image_url, int max_bitmap_size); | |
127 | |
128 // Ask the favicon from history | 147 // Ask the favicon from history |
129 virtual void UpdateFaviconMappingAndFetch( | 148 virtual void UpdateFaviconMappingAndFetch( |
130 const GURL& page_url, | 149 const GURL& page_url, |
131 const GURL& icon_url, | 150 const GURL& icon_url, |
132 favicon_base::IconType icon_type, | 151 favicon_base::IconType icon_type, |
133 const favicon_base::FaviconResultsCallback& callback, | 152 const favicon_base::FaviconResultsCallback& callback, |
134 base::CancelableTaskTracker* tracker); | 153 base::CancelableTaskTracker* tracker); |
135 | 154 |
136 virtual void GetFaviconFromFaviconService( | 155 virtual void GetFaviconFromFaviconService( |
137 const GURL& icon_url, | 156 const GURL& icon_url, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 const gfx::Image& image, | 196 const gfx::Image& image, |
178 float score, | 197 float score, |
179 favicon_base::IconType icon_type); | 198 favicon_base::IconType icon_type); |
180 | 199 |
181 GURL image_url; | 200 GURL image_url; |
182 gfx::Image image; | 201 gfx::Image image; |
183 float score; | 202 float score; |
184 favicon_base::IconType icon_type; | 203 favicon_base::IconType icon_type; |
185 }; | 204 }; |
186 | 205 |
| 206 // Returns the bit mask of favicon_base::IconType based on the handler's type. |
| 207 static int GetIconTypesFromHandlerType( |
| 208 FaviconDriverObserver::NotificationIconType handler_type); |
| 209 |
187 // Get the maximal icon size in pixels for a icon of type |icon_type| for the | 210 // Get the maximal icon size in pixels for a icon of type |icon_type| for the |
188 // current platform. | 211 // current platform. |
189 static int GetMaximalIconSize(favicon_base::IconType icon_type); | 212 static int GetMaximalIconSize(favicon_base::IconType icon_type); |
190 | 213 |
| 214 // Called when the history request for favicon data mapped to |url_| has |
| 215 // completed and the renderer has told us the icon URLs used by |url_| |
| 216 void OnGotInitialHistoryDataAndIconURLCandidates(); |
| 217 |
191 // See description above class for details. | 218 // See description above class for details. |
192 void OnFaviconDataForInitialURLFromFaviconService(const std::vector< | 219 void OnFaviconDataForInitialURLFromFaviconService(const std::vector< |
193 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results); | 220 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results); |
194 | 221 |
195 // If the favicon currently mapped to |url_| has expired, downloads the | 222 // If the favicon currently mapped to |url_| has expired, downloads the |
196 // current candidate favicon from the renderer. Otherwise requests data for | 223 // current candidate favicon from the renderer. Otherwise requests data for |
197 // the current favicon from history. If data is requested from history, | 224 // the current favicon from history. If data is requested from history, |
198 // OnFaviconData() is called with the history data once it has been retrieved. | 225 // OnFaviconData() is called with the history data once it has been retrieved. |
199 void DownloadCurrentCandidateOrAskFaviconService(); | 226 void DownloadCurrentCandidateOrAskFaviconService(); |
200 | 227 |
201 // See description above class for details. | 228 // See description above class for details. |
202 void OnFaviconData(const std::vector<favicon_base::FaviconRawBitmapResult>& | 229 void OnFaviconData(const std::vector<favicon_base::FaviconRawBitmapResult>& |
203 favicon_bitmap_results); | 230 favicon_bitmap_results); |
204 | 231 |
205 // Schedules a download for the specified entry. This adds the request to | 232 // Schedules a download for the specified entry. This adds the request to |
206 // download_requests_. | 233 // download_requests_. |
207 void ScheduleDownload(const GURL& image_url, | 234 void ScheduleDownload(const GURL& image_url, |
208 favicon_base::IconType icon_type); | 235 favicon_base::IconType icon_type); |
209 | 236 |
| 237 // Triggered when a download of an image has finished. |
| 238 void OnDidDownloadFavicon( |
| 239 int id, |
| 240 int http_status_code, |
| 241 const GURL& image_url, |
| 242 const std::vector<SkBitmap>& bitmaps, |
| 243 const std::vector<gfx::Size>& original_bitmap_sizes); |
| 244 |
210 // Updates |favicon_candidate_| and returns true if it is an exact match. | 245 // Updates |favicon_candidate_| and returns true if it is an exact match. |
211 bool UpdateFaviconCandidate(const GURL& image_url, | 246 bool UpdateFaviconCandidate(const GURL& image_url, |
212 const gfx::Image& image, | 247 const gfx::Image& image, |
213 float score, | 248 float score, |
214 favicon_base::IconType icon_type); | 249 favicon_base::IconType icon_type); |
215 | 250 |
216 // Sets the image data for the favicon. | 251 // Sets the image data for the favicon. |
217 void SetFavicon(const GURL& icon_url, | 252 void SetFavicon(const GURL& icon_url, |
218 const gfx::Image& image, | 253 const gfx::Image& image, |
219 favicon_base::IconType icon_type); | 254 favicon_base::IconType icon_type); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 | 316 |
282 // The icon URL and the icon type of the favicon in the most recent | 317 // The icon URL and the icon type of the favicon in the most recent |
283 // FaviconDriver::OnFaviconAvailable() notification. | 318 // FaviconDriver::OnFaviconAvailable() notification. |
284 GURL notification_icon_url_; | 319 GURL notification_icon_url_; |
285 favicon_base::IconType notification_icon_type_; | 320 favicon_base::IconType notification_icon_type_; |
286 | 321 |
287 // The FaviconService which implements favicon operations. May be null during | 322 // The FaviconService which implements favicon operations. May be null during |
288 // testing. | 323 // testing. |
289 FaviconService* service_; | 324 FaviconService* service_; |
290 | 325 |
291 // This handler's driver, owns this object. | 326 // This handler's delegate. |
292 FaviconDriver* driver_; | 327 Delegate* delegate_; |
293 | 328 |
294 // The index of the favicon URL in |image_urls_| which is currently being | 329 // The index of the favicon URL in |image_urls_| which is currently being |
295 // requested from history or downloaded. | 330 // requested from history or downloaded. |
296 size_t current_candidate_index_; | 331 size_t current_candidate_index_; |
297 | 332 |
298 // Best image we've seen so far. As images are downloaded from the page they | 333 // Best image we've seen so far. As images are downloaded from the page they |
299 // are stored here. When there is an exact match, or no more images are | 334 // are stored here. When there is an exact match, or no more images are |
300 // available the favicon service and the current page are updated (assuming | 335 // available the favicon service and the current page are updated (assuming |
301 // the image is for a favicon). | 336 // the image is for a favicon). |
302 FaviconCandidate best_favicon_candidate_; | 337 FaviconCandidate best_favicon_candidate_; |
303 | 338 |
| 339 base::WeakPtrFactory<FaviconHandler> weak_ptr_factory_; |
| 340 |
304 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); | 341 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); |
305 }; | 342 }; |
306 | 343 |
307 } // namespace favicon | 344 } // namespace favicon |
308 | 345 |
309 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ | 346 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ |
OLD | NEW |