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 29 matching lines...) Expand all Loading... | |
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 FaviconService requests. |
259 base::CancelableTaskTracker cancelable_task_tracker_; | 295 base::CancelableTaskTracker cancelable_task_tracker_; |
260 | 296 |
261 FaviconDriverObserver::NotificationIconType handler_type_; | 297 const FaviconDriverObserver::NotificationIconType handler_type_; |
262 | 298 |
263 // URL of the page we're requesting the favicon for. | 299 // URL of the page we're requesting the favicon for. |
264 GURL url_; | 300 GURL url_; |
265 | 301 |
266 // Whether we got data back for the initial request to the FaviconService. | 302 // Whether we got data back for the initial request to the FaviconService. |
267 bool got_favicon_from_history_; | 303 bool got_favicon_from_history_; |
268 | 304 |
269 // Whether the history data returned in | 305 // Whether the history data returned in |
270 // OnFaviconDataForInitialURLFromFaviconService() is out of date or is known | 306 // OnFaviconDataForInitialURLFromFaviconService() is out of date or is known |
271 // to be incomplete. If true, it means there is a favicon mapped to |url_| in | 307 // 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 | 308 // 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. | 309 // database has expired or the data in the database is incomplete. |
274 bool initial_history_result_expired_or_incomplete_; | 310 bool initial_history_result_expired_or_incomplete_; |
275 | 311 |
276 // Whether FaviconHandler should ignore history state and determine the | 312 // Whether FaviconHandler should ignore history state and determine the |
277 // optimal icon URL out of |image_urls_| for |url_| by downloading | 313 // optimal icon URL out of |image_urls_| for |url_| by downloading |
278 // |image_urls_| one by one. | 314 // |image_urls_| one by one. |
279 bool redownload_icons_; | 315 bool redownload_icons_; |
280 | 316 |
317 // Requests to the renderer to download a manifest. | |
318 base::CancelableCallback<Delegate::ManifestDownloadCallback::RunType> | |
319 manifest_download_request_; | |
320 | |
281 // Requests to the renderer to download favicons. | 321 // Requests to the renderer to download favicons. |
282 base::CancelableCallback<Delegate::ImageDownloadCallback::RunType> | 322 base::CancelableCallback<Delegate::ImageDownloadCallback::RunType> |
283 download_request_; | 323 image_download_request_; |
284 | 324 |
285 // The combination of the supported icon types. | 325 // The combination of the supported icon types. |
286 const int icon_types_; | 326 const int icon_types_; |
287 | 327 |
288 // Whether the largest icon should be downloaded. | 328 // Whether the largest icon should be downloaded. |
289 const bool download_largest_icon_; | 329 const bool download_largest_icon_; |
290 | 330 |
331 // The manifest URL from the renderer. | |
332 base::Optional<GURL> manifest_url_; | |
pkotwicz
2017/04/24 14:42:15
Can you use an empty GURL() instead of base::Optio
mastiz
2017/04/27 13:54:50
I've no objection to that, but let's defer this di
pkotwicz
2017/05/01 04:56:53
OK
| |
333 | |
334 // Original list of candidates provided to OnUpdateCandidates(), stored to | |
335 // be able to fall back to, in case a manifest was provided and downloading it | |
336 // failed (or provided no icons). | |
337 std::vector<FaviconURL> non_manifest_original_candidates_; | |
338 | |
291 // The prioritized favicon candidates from the page back from the renderer. | 339 // The prioritized favicon candidates from the page back from the renderer. |
292 std::vector<FaviconCandidate> candidates_; | 340 std::vector<FaviconCandidate> candidates_; |
293 | 341 |
294 // The icon URL and the icon type of the favicon in the most recent | 342 // The icon URL and the icon type of the favicon in the most recent |
295 // FaviconDriver::OnFaviconAvailable() notification. | 343 // FaviconDriver::OnFaviconAvailable() notification. |
296 GURL notification_icon_url_; | 344 GURL notification_icon_url_; |
297 favicon_base::IconType notification_icon_type_; | 345 favicon_base::IconType notification_icon_type_; |
298 | 346 |
299 // The FaviconService which implements favicon operations. May be null during | 347 // The FaviconService which implements favicon operations. May be null during |
300 // testing. | 348 // testing. |
(...skipping 15 matching lines...) Expand all Loading... | |
316 // UpdateFaviconCandidate()), the favicon service and the delegate are | 364 // UpdateFaviconCandidate()), the favicon service and the delegate are |
317 // notified. | 365 // notified. |
318 DownloadedFavicon best_favicon_; | 366 DownloadedFavicon best_favicon_; |
319 | 367 |
320 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); | 368 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); |
321 }; | 369 }; |
322 | 370 |
323 } // namespace favicon | 371 } // namespace favicon |
324 | 372 |
325 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ | 373 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ |
OLD | NEW |