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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 public: | 80 public: |
80 // Mimics WebContents::ImageDownloadCallback. | 81 // Mimics WebContents::ImageDownloadCallback. |
81 typedef base::Callback<void( | 82 typedef base::Callback<void( |
82 int id, | 83 int id, |
83 int status_code, | 84 int status_code, |
84 const GURL& image_url, | 85 const GURL& image_url, |
85 const std::vector<SkBitmap>& bitmaps, | 86 const std::vector<SkBitmap>& bitmaps, |
86 const std::vector<gfx::Size>& original_bitmap_sizes)> | 87 const std::vector<gfx::Size>& original_bitmap_sizes)> |
87 ImageDownloadCallback; | 88 ImageDownloadCallback; |
88 | 89 |
| 90 typedef base::Callback<void( |
| 91 const std::vector<favicon::FaviconURL>& favicons)> |
| 92 ManifestDownloadCallback; |
| 93 |
89 // Starts the download for the given favicon. When finished, the callback | 94 // Starts the download for the given favicon. When finished, the callback |
90 // is called with the results. Returns the unique id of the download | 95 // is called with the results. Returns the unique id of the download |
91 // request, which will also be passed to the callback. In case of error, 0 | 96 // request, which will also be passed to the callback. In case of error, 0 |
92 // is returned and no callback will be called. | 97 // is returned and no callback will be called. |
93 // Bitmaps with pixel sizes larger than |max_bitmap_size| are filtered out | 98 // Bitmaps with pixel sizes larger than |max_bitmap_size| are filtered out |
94 // from the bitmap results. If there are no bitmap results <= | 99 // from the bitmap results. If there are no bitmap results <= |
95 // |max_bitmap_size|, the smallest bitmap is resized to |max_bitmap_size| | 100 // |max_bitmap_size|, the smallest bitmap is resized to |max_bitmap_size| |
96 // and is the only result. A |max_bitmap_size| of 0 means unlimited. | 101 // and is the only result. A |max_bitmap_size| of 0 means unlimited. |
97 virtual int DownloadImage(const GURL& url, | 102 virtual int DownloadImage(const GURL& url, |
98 int max_image_size, | 103 int max_image_size, |
99 ImageDownloadCallback callback) = 0; | 104 ImageDownloadCallback callback) = 0; |
100 | 105 |
| 106 // Downloads a WebManifest and returns the favicons listed there. |
| 107 virtual void DownloadManifest(const GURL& url, |
| 108 ManifestDownloadCallback callback) = 0; |
| 109 |
101 // Returns whether the user is operating in an off-the-record context. | 110 // Returns whether the user is operating in an off-the-record context. |
102 virtual bool IsOffTheRecord() = 0; | 111 virtual bool IsOffTheRecord() = 0; |
103 | 112 |
104 // Returns whether |url| is bookmarked. | 113 // Returns whether |url| is bookmarked. |
105 virtual bool IsBookmarked(const GURL& url) = 0; | 114 virtual bool IsBookmarked(const GURL& url) = 0; |
106 | 115 |
107 // Notifies that the favicon image has been updated. Most delegates | 116 // Notifies that the favicon image has been updated. Most delegates |
108 // propagate the notification to FaviconDriverObserver::OnFaviconUpdated(). | 117 // propagate the notification to FaviconDriverObserver::OnFaviconUpdated(). |
109 // See its documentation for details. | 118 // See its documentation for details. |
110 virtual void OnFaviconUpdated( | 119 virtual void OnFaviconUpdated( |
111 const GURL& page_url, | 120 const GURL& page_url, |
112 FaviconDriverObserver::NotificationIconType notification_icon_type, | 121 FaviconDriverObserver::NotificationIconType notification_icon_type, |
113 const GURL& icon_url, | 122 const GURL& icon_url, |
114 bool icon_url_changed, | 123 bool icon_url_changed, |
115 const gfx::Image& image) = 0; | 124 const gfx::Image& image) = 0; |
116 }; | 125 }; |
117 | 126 |
118 // |delegate| must not be nullptr and must outlive this class. | 127 // |delegate| must not be nullptr and must outlive this class. |
119 FaviconHandler(FaviconService* service, | 128 FaviconHandler(FaviconService* service, |
120 Delegate* delegate, | 129 Delegate* delegate, |
121 FaviconDriverObserver::NotificationIconType handler_type); | 130 FaviconDriverObserver::NotificationIconType handler_type); |
122 ~FaviconHandler(); | 131 ~FaviconHandler(); |
123 | 132 |
124 // Initiates loading the favicon for the specified url. | 133 // Initiates loading the favicon for the specified url. |
125 void FetchFavicon(const GURL& url); | 134 void FetchFavicon(const GURL& url); |
126 | 135 |
127 // Message Handler. Must be public, because also called from | 136 // Collects the candidate favicons as listed in the HTML head, as well as |
128 // PrerenderContents. Collects the |image_urls| list. | 137 // the WebManifest URL if available. |
129 void OnUpdateFaviconURL(const GURL& page_url, | 138 void OnUpdateCandidates(const GURL& page_url, |
130 const std::vector<favicon::FaviconURL>& candidates); | 139 const std::vector<favicon::FaviconURL>& candidates, |
| 140 const base::Optional<GURL>& manifest_url); |
131 | 141 |
132 // For testing. | 142 // For testing. |
133 const std::vector<GURL> GetIconURLs() const; | 143 const std::vector<GURL> GetIconURLs() const; |
134 | 144 |
135 // Returns whether the handler is waiting for a download to complete or for | 145 // Returns whether the handler is waiting for a download to complete or for |
136 // data from the FaviconService. Reserved for testing. | 146 // data from the FaviconService. Reserved for testing. |
137 bool HasPendingTasksForTest(); | 147 bool HasPendingTasksForTest(); |
138 | 148 |
139 // Get the maximal icon size in pixels for a handler of type |handler_type|. | 149 // Get the maximal icon size in pixels for a handler of type |handler_type|. |
140 static int GetMaximalIconSize( | 150 static int GetMaximalIconSize( |
(...skipping 26 matching lines...) Expand all Loading... |
167 | 177 |
168 struct DownloadedFavicon { | 178 struct DownloadedFavicon { |
169 FaviconCandidate candidate; | 179 FaviconCandidate candidate; |
170 gfx::Image image; | 180 gfx::Image image; |
171 }; | 181 }; |
172 | 182 |
173 // Returns the bit mask of favicon_base::IconType based on the handler's type. | 183 // Returns the bit mask of favicon_base::IconType based on the handler's type. |
174 static int GetIconTypesFromHandlerType( | 184 static int GetIconTypesFromHandlerType( |
175 FaviconDriverObserver::NotificationIconType handler_type); | 185 FaviconDriverObserver::NotificationIconType handler_type); |
176 | 186 |
| 187 // Called with the result of looking up cached icon data for the manifest's |
| 188 // URL, which is used as icon URL. |
| 189 void OnFaviconDataForManifestFromFaviconService( |
| 190 const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 191 favicon_bitmap_results); |
| 192 |
| 193 // Schedules a download for |manifest_url_|, which must not be empty. The |
| 194 // request is stored in |manifest_download_request_| so it can be cancelled. |
| 195 void ScheduleManifestDownload(); |
| 196 |
| 197 // Called when the actual list of favicon candidates to be processed is |
| 198 // available, which can be either icon URLs listed in the HTML head instead |
| 199 // or, if a Web Manifest was provided, the list of icons there. |
| 200 void OnGotFinalIconURLCandidates(const std::vector<FaviconURL>& candidates); |
| 201 |
177 // Called when the history request for favicon data mapped to |url_| has | 202 // Called when the history request for favicon data mapped to |url_| has |
178 // completed and the renderer has told us the icon URLs used by |url_| | 203 // completed and the renderer has told us the icon URLs used by |url_| |
179 void OnGotInitialHistoryDataAndIconURLCandidates(); | 204 void OnGotInitialHistoryDataAndIconURLCandidates(); |
180 | 205 |
181 // See description above class for details. | 206 // See description above class for details. |
182 void OnFaviconDataForInitialURLFromFaviconService(const std::vector< | 207 void OnFaviconDataForInitialURLFromFaviconService(const std::vector< |
183 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results); | 208 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results); |
184 | 209 |
185 // If the favicon currently mapped to |url_| has expired, downloads the | 210 // If the favicon currently mapped to |url_| has expired, downloads the |
186 // current candidate favicon from the renderer. Otherwise requests data for | 211 // current candidate favicon from the renderer. Otherwise requests data for |
187 // the current favicon from history. If data is requested from history, | 212 // the current favicon from history. If data is requested from history, |
188 // OnFaviconData() is called with the history data once it has been retrieved. | 213 // OnFaviconData() is called with the history data once it has been retrieved. |
189 void DownloadCurrentCandidateOrAskFaviconService(); | 214 void DownloadCurrentCandidateOrAskFaviconService(); |
190 | 215 |
| 216 // Requests the favicon for |icon_url| from the favicon service. Unless in |
| 217 // incognito, it also updates the page URL (url_) to |icon_url| mappings. |
| 218 void GetFaviconAndUpdateMappingsUnlessIncognito( |
| 219 const GURL& icon_url, |
| 220 favicon_base::IconType icon_type, |
| 221 const favicon_base::FaviconResultsCallback& callback); |
| 222 |
191 // See description above class for details. | 223 // See description above class for details. |
192 void OnFaviconData(const std::vector<favicon_base::FaviconRawBitmapResult>& | 224 void OnFaviconData(const std::vector<favicon_base::FaviconRawBitmapResult>& |
193 favicon_bitmap_results); | 225 favicon_bitmap_results); |
194 | 226 |
195 // Schedules a download for the specified entry. This adds the request to | 227 // Schedules a download for |image_url|. The request is stored in |
196 // download_requests_. | 228 // image_download_request_ so it can be cancelled. |
197 void ScheduleDownload(const GURL& image_url, | 229 void ScheduleFaviconDownload(const GURL& image_url, |
198 favicon_base::IconType icon_type); | 230 favicon_base::IconType icon_type); |
199 | 231 |
200 // Triggered when a download of an image has finished. | 232 // Triggered when a download of an image has finished. |
201 void OnDidDownloadFavicon( | 233 void OnDidDownloadFavicon( |
202 favicon_base::IconType icon_type, | 234 favicon_base::IconType icon_type, |
203 int id, | 235 int id, |
204 int http_status_code, | 236 int http_status_code, |
205 const GURL& image_url, | 237 const GURL& image_url, |
206 const std::vector<SkBitmap>& bitmaps, | 238 const std::vector<SkBitmap>& bitmaps, |
207 const std::vector<gfx::Size>& original_bitmap_sizes); | 239 const std::vector<gfx::Size>& original_bitmap_sizes); |
208 | 240 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 // to be incomplete. If true, it means there is a favicon mapped to |url_| in | 290 // to be incomplete. If true, it means there is a favicon mapped to |url_| in |
259 // history, but we need to redownload the icon URLs because the icon in the | 291 // history, but we need to redownload the icon URLs because the icon in the |
260 // database has expired or the data in the database is incomplete. | 292 // database has expired or the data in the database is incomplete. |
261 bool initial_history_result_expired_or_incomplete_; | 293 bool initial_history_result_expired_or_incomplete_; |
262 | 294 |
263 // Whether FaviconHandler should ignore history state and determine the | 295 // Whether FaviconHandler should ignore history state and determine the |
264 // optimal icon URL out of |image_urls_| for |url_| by downloading | 296 // optimal icon URL out of |image_urls_| for |url_| by downloading |
265 // |image_urls_| one by one. | 297 // |image_urls_| one by one. |
266 bool redownload_icons_; | 298 bool redownload_icons_; |
267 | 299 |
| 300 // Requests to the renderer to download a manifest. |
| 301 base::CancelableCallback<Delegate::ManifestDownloadCallback::RunType> |
| 302 manifest_download_request_; |
| 303 |
268 // Requests to the renderer to download favicons. | 304 // Requests to the renderer to download favicons. |
269 base::CancelableCallback<Delegate::ImageDownloadCallback::RunType> | 305 base::CancelableCallback<Delegate::ImageDownloadCallback::RunType> |
270 download_request_; | 306 image_download_request_; |
271 | 307 |
272 // The combination of the supported icon types. | 308 // The combination of the supported icon types. |
273 const int icon_types_; | 309 const int icon_types_; |
274 | 310 |
275 // Whether the largest icon should be downloaded. | 311 // Whether the largest icon should be downloaded. |
276 const bool download_largest_icon_; | 312 const bool download_largest_icon_; |
277 | 313 |
| 314 // The manifest URL from the renderer. |
| 315 base::Optional<GURL> manifest_url_; |
| 316 |
278 // The prioritized favicon candidates from the page back from the renderer. | 317 // The prioritized favicon candidates from the page back from the renderer. |
279 std::vector<FaviconCandidate> candidates_; | 318 std::vector<FaviconCandidate> candidates_; |
280 | 319 |
281 // The icon URL and the icon type of the favicon in the most recent | 320 // The icon URL and the icon type of the favicon in the most recent |
282 // FaviconDriver::OnFaviconAvailable() notification. | 321 // FaviconDriver::OnFaviconAvailable() notification. |
283 GURL notification_icon_url_; | 322 GURL notification_icon_url_; |
284 favicon_base::IconType notification_icon_type_; | 323 favicon_base::IconType notification_icon_type_; |
285 | 324 |
286 // The FaviconService which implements favicon operations. May be null during | 325 // The FaviconService which implements favicon operations. May be null during |
287 // testing. | 326 // testing. |
(...skipping 11 matching lines...) Expand all Loading... |
299 // UpdateFaviconCandidate()), the favicon service and the delegate are | 338 // UpdateFaviconCandidate()), the favicon service and the delegate are |
300 // notified. | 339 // notified. |
301 DownloadedFavicon best_favicon_; | 340 DownloadedFavicon best_favicon_; |
302 | 341 |
303 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); | 342 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); |
304 }; | 343 }; |
305 | 344 |
306 } // namespace favicon | 345 } // namespace favicon |
307 | 346 |
308 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ | 347 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ |
OLD | NEW |