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 <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/cancelable_callback.h" | 13 #include "base/cancelable_callback.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/optional.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 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 public: | 93 public: |
93 // Mimics WebContents::ImageDownloadCallback. | 94 // Mimics WebContents::ImageDownloadCallback. |
94 typedef base::Callback<void( | 95 typedef base::Callback<void( |
95 int id, | 96 int id, |
96 int status_code, | 97 int status_code, |
97 const GURL& image_url, | 98 const GURL& image_url, |
98 const std::vector<SkBitmap>& bitmaps, | 99 const std::vector<SkBitmap>& bitmaps, |
99 const std::vector<gfx::Size>& original_bitmap_sizes)> | 100 const std::vector<gfx::Size>& original_bitmap_sizes)> |
100 ImageDownloadCallback; | 101 ImageDownloadCallback; |
101 | 102 |
| 103 typedef base::Callback< |
| 104 void(int status_code, const std::vector<favicon::FaviconURL>& favicons)> |
| 105 ManifestDownloadCallback; |
| 106 |
102 // Starts the download for the given favicon. When finished, the callback | 107 // Starts the download for the given favicon. When finished, the callback |
103 // is called with the results. Returns the unique id of the download | 108 // is called with the results. Returns the unique id of the download |
104 // request, which will also be passed to the callback. In case of error, 0 | 109 // request, which will also be passed to the callback. In case of error, 0 |
105 // is returned and no callback will be called. | 110 // is returned and no callback will be called. |
106 // Bitmaps with pixel sizes larger than |max_bitmap_size| are filtered out | 111 // Bitmaps with pixel sizes larger than |max_bitmap_size| are filtered out |
107 // from the bitmap results. If there are no bitmap results <= | 112 // from the bitmap results. If there are no bitmap results <= |
108 // |max_bitmap_size|, the smallest bitmap is resized to |max_bitmap_size| | 113 // |max_bitmap_size|, the smallest bitmap is resized to |max_bitmap_size| |
109 // and is the only result. A |max_bitmap_size| of 0 means unlimited. | 114 // and is the only result. A |max_bitmap_size| of 0 means unlimited. |
110 virtual int DownloadImage(const GURL& url, | 115 virtual int DownloadImage(const GURL& url, |
111 int max_image_size, | 116 int max_image_size, |
112 ImageDownloadCallback callback) = 0; | 117 ImageDownloadCallback callback) = 0; |
113 | 118 |
| 119 // Downloads a WebManifest and returns the favicons listed there. |
| 120 virtual void DownloadManifest(const GURL& url, |
| 121 ManifestDownloadCallback callback) = 0; |
| 122 |
114 // Returns whether the user is operating in an off-the-record context. | 123 // Returns whether the user is operating in an off-the-record context. |
115 virtual bool IsOffTheRecord() = 0; | 124 virtual bool IsOffTheRecord() = 0; |
116 | 125 |
117 // Returns whether |url| is bookmarked. | 126 // Returns whether |url| is bookmarked. |
118 virtual bool IsBookmarked(const GURL& url) = 0; | 127 virtual bool IsBookmarked(const GURL& url) = 0; |
119 | 128 |
120 // Notifies that the favicon image has been updated. Most delegates | 129 // Notifies that the favicon image has been updated. Most delegates |
121 // propagate the notification to FaviconDriverObserver::OnFaviconUpdated(). | 130 // propagate the notification to FaviconDriverObserver::OnFaviconUpdated(). |
122 // See its documentation for details. | 131 // See its documentation for details. |
123 virtual void OnFaviconUpdated( | 132 virtual void OnFaviconUpdated( |
124 const GURL& page_url, | 133 const GURL& page_url, |
125 FaviconDriverObserver::NotificationIconType notification_icon_type, | 134 FaviconDriverObserver::NotificationIconType notification_icon_type, |
126 const GURL& icon_url, | 135 const GURL& icon_url, |
127 bool icon_url_changed, | 136 bool icon_url_changed, |
128 const gfx::Image& image) = 0; | 137 const gfx::Image& image) = 0; |
129 }; | 138 }; |
130 | 139 |
131 // |service| and |delegate| must not be nullptr and must outlive this class. | 140 // |service| and |delegate| must not be nullptr and must outlive this class. |
132 FaviconHandler(FaviconService* service, | 141 FaviconHandler(FaviconService* service, |
133 Delegate* delegate, | 142 Delegate* delegate, |
134 FaviconDriverObserver::NotificationIconType handler_type); | 143 FaviconDriverObserver::NotificationIconType handler_type); |
135 ~FaviconHandler(); | 144 ~FaviconHandler(); |
136 | 145 |
137 // Initiates loading the favicon for the specified url. | 146 // Initiates loading the favicon for the specified url. |
138 void FetchFavicon(const GURL& url); | 147 void FetchFavicon(const GURL& url); |
139 | 148 |
140 // Message Handler. Must be public, because also called from | 149 // Collects the candidate favicons as listed in the HTML head, as well as |
141 // PrerenderContents. Collects the |image_urls| list. | 150 // the WebManifest URL if available. |
142 void OnUpdateFaviconURL(const GURL& page_url, | 151 void OnUpdateCandidates(const GURL& page_url, |
143 const std::vector<favicon::FaviconURL>& candidates); | 152 const std::vector<favicon::FaviconURL>& candidates, |
| 153 const base::Optional<GURL>& manifest_url); |
| 154 |
| 155 // Returns the supported icon types, inferred from the handler type as passed |
| 156 // in the constructor. |
| 157 int icon_types() const { return icon_types_; } |
144 | 158 |
145 // For testing. | 159 // For testing. |
146 const std::vector<GURL> GetIconURLs() const; | 160 const std::vector<GURL> GetIconURLs() const; |
147 | 161 |
148 // Returns whether the handler is waiting for a download to complete or for | 162 // Returns whether the handler is waiting for a download to complete or for |
149 // data from the FaviconService. Reserved for testing. | 163 // data from the FaviconService. Reserved for testing. |
150 bool HasPendingTasksForTest(); | 164 bool HasPendingTasksForTest(); |
151 | 165 |
152 // Get the maximal icon size in pixels for a handler of type |handler_type|. | 166 // Get the maximal icon size in pixels for a handler of type |handler_type|. |
153 static int GetMaximalIconSize( | 167 static int GetMaximalIconSize( |
(...skipping 26 matching lines...) Expand all Loading... |
180 | 194 |
181 struct DownloadedFavicon { | 195 struct DownloadedFavicon { |
182 FaviconCandidate candidate; | 196 FaviconCandidate candidate; |
183 gfx::Image image; | 197 gfx::Image image; |
184 }; | 198 }; |
185 | 199 |
186 // Returns the bit mask of favicon_base::IconType based on the handler's type. | 200 // Returns the bit mask of favicon_base::IconType based on the handler's type. |
187 static int GetIconTypesFromHandlerType( | 201 static int GetIconTypesFromHandlerType( |
188 FaviconDriverObserver::NotificationIconType handler_type); | 202 FaviconDriverObserver::NotificationIconType handler_type); |
189 | 203 |
| 204 // Called with the result of looking up cached icon data for the manifest's |
| 205 // URL, which is used as icon URL. |
| 206 void OnFaviconDataForManifestFromFaviconService( |
| 207 const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 208 favicon_bitmap_results); |
| 209 |
| 210 // Called when the dowloading of a manifest completes. |
| 211 void OnDidDownloadManifest(int status_code, |
| 212 const std::vector<FaviconURL>& candidates); |
| 213 |
| 214 // Called when the actual list of favicon candidates to be processed is |
| 215 // available, which can be either icon URLs listed in the HTML head instead |
| 216 // or, if a Web Manifest was provided, the list of icons there. |
| 217 void OnGotFinalIconURLCandidates(const std::vector<FaviconURL>& candidates); |
| 218 |
190 // Called when the history request for favicon data mapped to |url_| has | 219 // Called when the history request for favicon data mapped to |url_| has |
191 // completed and the renderer has told us the icon URLs used by |url_| | 220 // completed and the renderer has told us the icon URLs used by |url_| |
192 void OnGotInitialHistoryDataAndIconURLCandidates(); | 221 void OnGotInitialHistoryDataAndIconURLCandidates(); |
193 | 222 |
194 // See description above class for details. | 223 // See description above class for details. |
195 void OnFaviconDataForInitialURLFromFaviconService(const std::vector< | 224 void OnFaviconDataForInitialURLFromFaviconService(const std::vector< |
196 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results); | 225 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results); |
197 | 226 |
198 // If the favicon currently mapped to |url_| has expired, downloads the | 227 // If the favicon currently mapped to |url_| has expired, downloads the |
199 // current candidate favicon from the renderer. Otherwise requests data for | 228 // current candidate favicon from the renderer. Otherwise requests data for |
200 // the current favicon from history. If data is requested from history, | 229 // the current favicon from history. If data is requested from history, |
201 // OnFaviconData() is called with the history data once it has been retrieved. | 230 // OnFaviconData() is called with the history data once it has been retrieved. |
202 void DownloadCurrentCandidateOrAskFaviconService(); | 231 void DownloadCurrentCandidateOrAskFaviconService(); |
203 | 232 |
| 233 // Requests the favicon for |icon_url| from the favicon service. Unless in |
| 234 // incognito, it also updates the page URL (url_) to |icon_url| mappings. |
| 235 void GetFaviconAndUpdateMappingsUnlessIncognito( |
| 236 const GURL& icon_url, |
| 237 favicon_base::IconType icon_type, |
| 238 const favicon_base::FaviconResultsCallback& callback); |
| 239 |
204 // See description above class for details. | 240 // See description above class for details. |
205 void OnFaviconData(const std::vector<favicon_base::FaviconRawBitmapResult>& | 241 void OnFaviconData(const std::vector<favicon_base::FaviconRawBitmapResult>& |
206 favicon_bitmap_results); | 242 favicon_bitmap_results); |
207 | 243 |
208 // Schedules a download for the specified entry. This adds the request to | 244 // Schedules a download for |image_url|. The request is stored in |
209 // download_requests_. | 245 // image_download_request_ so it can be cancelled. |
210 void ScheduleDownload(const GURL& image_url, | 246 void ScheduleFaviconDownload(const GURL& image_url, |
211 favicon_base::IconType icon_type); | 247 favicon_base::IconType icon_type); |
212 | 248 |
213 // Triggered when a download of an image has finished. | 249 // Triggered when a download of an image has finished. |
214 void OnDidDownloadFavicon( | 250 void OnDidDownloadFavicon( |
215 favicon_base::IconType icon_type, | 251 favicon_base::IconType icon_type, |
216 int id, | 252 int id, |
217 int http_status_code, | 253 int http_status_code, |
218 const GURL& image_url, | 254 const GURL& image_url, |
219 const std::vector<SkBitmap>& bitmaps, | 255 const std::vector<SkBitmap>& bitmaps, |
220 const std::vector<gfx::Size>& original_bitmap_sizes); | 256 const std::vector<gfx::Size>& original_bitmap_sizes); |
221 | 257 |
(...skipping 26 matching lines...) Expand all Loading... |
248 ? &candidates_[current_candidate_index_] | 284 ? &candidates_[current_candidate_index_] |
249 : nullptr; | 285 : nullptr; |
250 } | 286 } |
251 | 287 |
252 // Returns the preferred size of the image. 0 means no preference (any size | 288 // Returns the preferred size of the image. 0 means no preference (any size |
253 // will do). | 289 // will do). |
254 int preferred_icon_size() const { | 290 int preferred_icon_size() const { |
255 return download_largest_icon_ ? 0 : gfx::kFaviconSize; | 291 return download_largest_icon_ ? 0 : gfx::kFaviconSize; |
256 } | 292 } |
257 | 293 |
258 // Used for FaviconService requests. | 294 // Used for the GetFaviconForPageURL request looking up the page URL, |
259 base::CancelableTaskTracker cancelable_task_tracker_; | 295 // triggered in FetchFavicon. |
| 296 base::CancelableTaskTracker cancelable_task_tracker_for_page_url_; |
260 | 297 |
261 FaviconDriverObserver::NotificationIconType handler_type_; | 298 // Used for various FaviconService methods triggered while processing |
| 299 // candidates. |
| 300 base::CancelableTaskTracker cancelable_task_tracker_for_candidates_; |
| 301 |
| 302 const FaviconDriverObserver::NotificationIconType handler_type_; |
262 | 303 |
263 // URL of the page we're requesting the favicon for. | 304 // URL of the page we're requesting the favicon for. |
264 GURL url_; | 305 GURL url_; |
265 | 306 |
266 // Whether we got data back for the initial request to the FaviconService. | 307 // Whether we got data back for the initial request to the FaviconService. |
267 bool got_favicon_from_history_; | 308 bool got_favicon_from_history_; |
268 | 309 |
269 // Whether the history data returned in | 310 // Whether the history data returned in |
270 // OnFaviconDataForInitialURLFromFaviconService() is out of date or is known | 311 // OnFaviconDataForInitialURLFromFaviconService() is out of date or is known |
271 // to be incomplete. If true, it means there is a favicon mapped to |url_| in | 312 // to be incomplete. If true, it means there is a favicon mapped to |url_| in |
272 // history, but we need to redownload the icon URLs because the icon in the | 313 // history, but we need to redownload the icon URLs because the icon in the |
273 // database has expired or the data in the database is incomplete. | 314 // database has expired or the data in the database is incomplete. |
274 bool initial_history_result_expired_or_incomplete_; | 315 bool initial_history_result_expired_or_incomplete_; |
275 | 316 |
276 // Whether FaviconHandler should ignore history state and determine the | 317 // Whether FaviconHandler should ignore history state and determine the |
277 // optimal icon URL out of |image_urls_| for |url_| by downloading | 318 // optimal icon URL out of |image_urls_| for |url_| by downloading |
278 // |image_urls_| one by one. | 319 // |image_urls_| one by one. |
279 bool redownload_icons_; | 320 bool redownload_icons_; |
280 | 321 |
| 322 // Requests to the renderer to download a manifest. |
| 323 base::CancelableCallback<Delegate::ManifestDownloadCallback::RunType> |
| 324 manifest_download_request_; |
| 325 |
281 // Requests to the renderer to download favicons. | 326 // Requests to the renderer to download favicons. |
282 base::CancelableCallback<Delegate::ImageDownloadCallback::RunType> | 327 base::CancelableCallback<Delegate::ImageDownloadCallback::RunType> |
283 download_request_; | 328 image_download_request_; |
284 | 329 |
285 // The combination of the supported icon types. | 330 // The combination of the supported icon types. |
286 const int icon_types_; | 331 const int icon_types_; |
287 | 332 |
288 // Whether the largest icon should be downloaded. | 333 // Whether the largest icon should be downloaded. |
289 const bool download_largest_icon_; | 334 const bool download_largest_icon_; |
290 | 335 |
| 336 // The manifest URL from the renderer. |
| 337 base::Optional<GURL> manifest_url_; |
| 338 |
| 339 // Original list of candidates provided to OnUpdateCandidates(), stored to |
| 340 // be able to fall back to, in case a manifest was provided and downloading it |
| 341 // failed (or provided no icons). |
| 342 std::vector<FaviconURL> non_manifest_original_candidates_; |
| 343 |
291 // The prioritized favicon candidates from the page back from the renderer. | 344 // The prioritized favicon candidates from the page back from the renderer. |
292 std::vector<FaviconCandidate> candidates_; | 345 std::vector<FaviconCandidate> candidates_; |
293 | 346 |
294 // The icon URL and the icon type of the favicon in the most recent | 347 // The icon URL and the icon type of the favicon in the most recent |
295 // FaviconDriver::OnFaviconAvailable() notification. | 348 // FaviconDriver::OnFaviconAvailable() notification. |
296 GURL notification_icon_url_; | 349 GURL notification_icon_url_; |
297 favicon_base::IconType notification_icon_type_; | 350 favicon_base::IconType notification_icon_type_; |
298 | 351 |
299 // The FaviconService which implements favicon operations. May be null during | 352 // The FaviconService which implements favicon operations. May be null during |
300 // testing. | 353 // testing. |
(...skipping 15 matching lines...) Expand all Loading... |
316 // UpdateFaviconCandidate()), the favicon service and the delegate are | 369 // UpdateFaviconCandidate()), the favicon service and the delegate are |
317 // notified. | 370 // notified. |
318 DownloadedFavicon best_favicon_; | 371 DownloadedFavicon best_favicon_; |
319 | 372 |
320 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); | 373 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); |
321 }; | 374 }; |
322 | 375 |
323 } // namespace favicon | 376 } // namespace favicon |
324 | 377 |
325 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ | 378 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ |
OLD | NEW |