| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_FAVICON_CORE_BROWSER_FAVICON_HANDLER_H_ | |
| 6 #define COMPONENTS_FAVICON_CORE_BROWSER_FAVICON_HANDLER_H_ | |
| 7 | |
| 8 #include <deque> | |
| 9 #include <map> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/callback_forward.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/task/cancelable_task_tracker.h" | |
| 16 #include "components/favicon/core/favicon_url.h" | |
| 17 #include "components/favicon_base/favicon_callback.h" | |
| 18 #include "ui/gfx/favicon_size.h" | |
| 19 #include "ui/gfx/image/image.h" | |
| 20 #include "url/gurl.h" | |
| 21 | |
| 22 class FaviconClient; | |
| 23 class FaviconDriver; | |
| 24 class FaviconService; | |
| 25 class SkBitmap; | |
| 26 | |
| 27 namespace base { | |
| 28 class RefCountedMemory; | |
| 29 } | |
| 30 | |
| 31 // FaviconHandler works with FaviconDriver to fetch the specific type of | |
| 32 // favicon. | |
| 33 // | |
| 34 // FetchFavicon requests the favicon from the favicon service which in turn | |
| 35 // requests the favicon from the history database. At this point | |
| 36 // we only know the URL of the page, and not necessarily the url of the | |
| 37 // favicon. To ensure we handle reloading stale favicons as well as | |
| 38 // reloading a favicon on page reload we always request the favicon from | |
| 39 // history regardless of whether the active favicon is valid. | |
| 40 // | |
| 41 // After the navigation two types of events are delivered (which is | |
| 42 // first depends upon who is faster): notification from the history | |
| 43 // db on our request for the favicon | |
| 44 // (OnFaviconDataForInitialURLFromFaviconService), or a message from the | |
| 45 // renderer giving us the URL of the favicon for the page (SetFaviconURL). | |
| 46 // . If the history db has a valid up to date favicon for the page, we update | |
| 47 // the current page and use the favicon. | |
| 48 // . When we receive the favicon url if it matches that of the current page | |
| 49 // and the current page's favicon is set, we do nothing (everything is | |
| 50 // ok). | |
| 51 // . On the other hand if the database does not know the favicon for url, or | |
| 52 // the favicon is out date, or the URL from the renderer does not match that | |
| 53 // of the current page we proceed to DownloadFaviconOrAskHistory. Before we | |
| 54 // invoke DownloadFaviconOrAskHistory we wait until we've received both | |
| 55 // the favicon url and the callback from history. We wait to ensure we | |
| 56 // truly know both the favicon url and the state of the database. | |
| 57 // | |
| 58 // DownloadFaviconOrAskHistory does the following: | |
| 59 // . If we have a valid favicon, but it is expired we ask the renderer to | |
| 60 // download the favicon. | |
| 61 // . Otherwise we ask the history database to update the mapping from | |
| 62 // page url to favicon url and call us back with the favicon. Remember, it is | |
| 63 // possible for the db to already have the favicon, just not the mapping | |
| 64 // between page to favicon url. The callback for this is OnFaviconData. | |
| 65 // | |
| 66 // OnFaviconData either updates the favicon of the current page (if the | |
| 67 // db knew about the favicon), or requests the renderer to download the | |
| 68 // favicon. | |
| 69 // | |
| 70 // When the renderer downloads favicons, it considers the entire list of | |
| 71 // favicon candidates, if |download_largest_favicon_| is true, the largest | |
| 72 // favicon will be used, otherwise the one that best matches the preferred size | |
| 73 // is chosen (or the first one if there is no preferred size). Once the | |
| 74 // matching favicon has been determined, SetFavicon is called which updates | |
| 75 // the page's favicon and notifies the database to save the favicon. | |
| 76 | |
| 77 class FaviconHandler { | |
| 78 public: | |
| 79 enum Type { FAVICON, TOUCH, LARGE }; | |
| 80 | |
| 81 FaviconHandler(FaviconService* service, | |
| 82 FaviconClient* client, | |
| 83 FaviconDriver* driver, | |
| 84 Type handler_type, | |
| 85 bool download_largest_icon); | |
| 86 virtual ~FaviconHandler(); | |
| 87 | |
| 88 // Returns the bit mask of favicon_base::IconType based on the handler's type. | |
| 89 static int GetIconTypesFromHandlerType(Type icon_type); | |
| 90 | |
| 91 // Initiates loading the favicon for the specified url. | |
| 92 void FetchFavicon(const GURL& url); | |
| 93 | |
| 94 // Message Handler. Must be public, because also called from | |
| 95 // PrerenderContents. Collects the |image_urls| list. | |
| 96 void OnUpdateFaviconURL(const std::vector<favicon::FaviconURL>& candidates); | |
| 97 | |
| 98 // Processes the current image_urls_ entry, requesting the image from the | |
| 99 // history / download service. | |
| 100 void ProcessCurrentUrl(); | |
| 101 | |
| 102 // Message handler for ImageHostMsg_DidDownloadImage. Called when the image | |
| 103 // at |image_url| has been downloaded. | |
| 104 // |bitmaps| is a list of all the frames of the image at |image_url|. | |
| 105 // |original_bitmap_sizes| are the sizes of |bitmaps| before they were resized | |
| 106 // to the maximum bitmap size passed to DownloadFavicon(). | |
| 107 void OnDidDownloadFavicon( | |
| 108 int id, | |
| 109 const GURL& image_url, | |
| 110 const std::vector<SkBitmap>& bitmaps, | |
| 111 const std::vector<gfx::Size>& original_bitmap_sizes); | |
| 112 | |
| 113 // For testing. | |
| 114 const std::vector<favicon::FaviconURL>& image_urls() const { | |
| 115 return image_urls_; | |
| 116 } | |
| 117 | |
| 118 protected: | |
| 119 // These virtual methods make FaviconHandler testable and are overridden by | |
| 120 // TestFaviconHandler. | |
| 121 | |
| 122 // Asks the render to download favicon, returns the request id. | |
| 123 virtual int DownloadFavicon(const GURL& image_url, int max_bitmap_size); | |
| 124 | |
| 125 // Ask the favicon from history | |
| 126 virtual void UpdateFaviconMappingAndFetch( | |
| 127 const GURL& page_url, | |
| 128 const GURL& icon_url, | |
| 129 favicon_base::IconType icon_type, | |
| 130 const favicon_base::FaviconResultsCallback& callback, | |
| 131 base::CancelableTaskTracker* tracker); | |
| 132 | |
| 133 virtual void GetFaviconFromFaviconService( | |
| 134 const GURL& icon_url, | |
| 135 favicon_base::IconType icon_type, | |
| 136 const favicon_base::FaviconResultsCallback& callback, | |
| 137 base::CancelableTaskTracker* tracker); | |
| 138 | |
| 139 virtual void GetFaviconForURLFromFaviconService( | |
| 140 const GURL& page_url, | |
| 141 int icon_types, | |
| 142 const favicon_base::FaviconResultsCallback& callback, | |
| 143 base::CancelableTaskTracker* tracker); | |
| 144 | |
| 145 virtual void SetHistoryFavicons(const GURL& page_url, | |
| 146 const GURL& icon_url, | |
| 147 favicon_base::IconType icon_type, | |
| 148 const gfx::Image& image); | |
| 149 | |
| 150 // Returns true if the favicon should be saved. | |
| 151 virtual bool ShouldSaveFavicon(const GURL& url); | |
| 152 | |
| 153 private: | |
| 154 // For testing: | |
| 155 friend class FaviconTabHelperTest; | |
| 156 friend class TestFaviconHandler; | |
| 157 | |
| 158 // Represents an in progress download of an image from the renderer. | |
| 159 struct DownloadRequest { | |
| 160 DownloadRequest(); | |
| 161 ~DownloadRequest(); | |
| 162 | |
| 163 DownloadRequest(const GURL& url, | |
| 164 const GURL& image_url, | |
| 165 favicon_base::IconType icon_type); | |
| 166 | |
| 167 GURL url; | |
| 168 GURL image_url; | |
| 169 favicon_base::IconType icon_type; | |
| 170 }; | |
| 171 | |
| 172 // Used to track a candidate for the favicon. | |
| 173 struct FaviconCandidate { | |
| 174 FaviconCandidate(); | |
| 175 ~FaviconCandidate(); | |
| 176 | |
| 177 FaviconCandidate(const GURL& url, | |
| 178 const GURL& image_url, | |
| 179 const gfx::Image& image, | |
| 180 float score, | |
| 181 favicon_base::IconType icon_type); | |
| 182 | |
| 183 GURL url; | |
| 184 GURL image_url; | |
| 185 gfx::Image image; | |
| 186 float score; | |
| 187 favicon_base::IconType icon_type; | |
| 188 }; | |
| 189 | |
| 190 // Get the maximal icon size in pixels for a icon of type |icon_type| for the | |
| 191 // current platform. | |
| 192 static int GetMaximalIconSize(favicon_base::IconType icon_type); | |
| 193 | |
| 194 // See description above class for details. | |
| 195 void OnFaviconDataForInitialURLFromFaviconService(const std::vector< | |
| 196 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results); | |
| 197 | |
| 198 // If the favicon has expired, asks the renderer to download the favicon. | |
| 199 // Otherwise asks history to update the mapping between page url and icon | |
| 200 // url with a callback to OnFaviconData when done. | |
| 201 void DownloadFaviconOrAskFaviconService(const GURL& page_url, | |
| 202 const GURL& icon_url, | |
| 203 favicon_base::IconType icon_type); | |
| 204 | |
| 205 // See description above class for details. | |
| 206 void OnFaviconData(const std::vector<favicon_base::FaviconRawBitmapResult>& | |
| 207 favicon_bitmap_results); | |
| 208 | |
| 209 // Schedules a download for the specified entry. This adds the request to | |
| 210 // download_requests_. | |
| 211 int ScheduleDownload(const GURL& url, | |
| 212 const GURL& image_url, | |
| 213 favicon_base::IconType icon_type); | |
| 214 | |
| 215 // Updates |favicon_candidate_| and returns true if it is an exact match. | |
| 216 bool UpdateFaviconCandidate(const GURL& url, | |
| 217 const GURL& image_url, | |
| 218 const gfx::Image& image, | |
| 219 float score, | |
| 220 favicon_base::IconType icon_type); | |
| 221 | |
| 222 // Sets the image data for the favicon. | |
| 223 void SetFavicon(const GURL& url, | |
| 224 const GURL& icon_url, | |
| 225 const gfx::Image& image, | |
| 226 favicon_base::IconType icon_type); | |
| 227 | |
| 228 // Notifies |driver_| favicon available. See | |
| 229 // FaviconDriver::NotifyFaviconAvailable() for |is_active_favicon| in detail. | |
| 230 void NotifyFaviconAvailable( | |
| 231 const std::vector<favicon_base::FaviconRawBitmapResult>& | |
| 232 favicon_bitmap_results, | |
| 233 bool is_active_favicon); | |
| 234 void NotifyFaviconAvailable(const GURL& icon_url, | |
| 235 const gfx::Image& image, | |
| 236 bool is_active_favicon); | |
| 237 | |
| 238 // Return the current candidate if any. | |
| 239 favicon::FaviconURL* current_candidate() { | |
| 240 return (!image_urls_.empty()) ? &image_urls_.front() : NULL; | |
| 241 } | |
| 242 | |
| 243 // Returns whether the page's url changed since the favicon was requested. | |
| 244 bool PageChangedSinceFaviconWasRequested(); | |
| 245 | |
| 246 // Returns the preferred size of the image. 0 means no preference (any size | |
| 247 // will do). | |
| 248 int preferred_icon_size() const { | |
| 249 if (download_largest_icon_) | |
| 250 return 0; | |
| 251 return handler_type_ == FAVICON ? gfx::kFaviconSize : 0; | |
| 252 } | |
| 253 | |
| 254 // Sorts the entries in |image_urls_| by icon size in descending order. | |
| 255 // Additionally removes any entries whose sizes are all greater than the max | |
| 256 // allowed size. | |
| 257 void SortAndPruneImageUrls(); | |
| 258 | |
| 259 // Used for FaviconService requests. | |
| 260 base::CancelableTaskTracker cancelable_task_tracker_; | |
| 261 | |
| 262 // URL of the page we're requesting the favicon for. | |
| 263 GURL url_; | |
| 264 | |
| 265 // Whether we are waiting for data from the FaviconService. | |
| 266 bool waiting_for_favicon_service_data_; | |
| 267 | |
| 268 // Whether we got data back for the initial request to the FaviconService. | |
| 269 bool got_favicon_from_history_; | |
| 270 | |
| 271 // Whether the favicon is out of date or the favicon data in | |
| 272 // |history_results_| is known to be incomplete. If true, it means history | |
| 273 // knows about the favicon, but we need to download the favicon because the | |
| 274 // icon has expired or the data in the database is incomplete. | |
| 275 bool favicon_expired_or_incomplete_; | |
| 276 | |
| 277 // Requests to the renderer to download favicons. | |
| 278 typedef std::map<int, DownloadRequest> DownloadRequests; | |
| 279 DownloadRequests download_requests_; | |
| 280 | |
| 281 // The type of the current handler. | |
| 282 const Type handler_type_; | |
| 283 | |
| 284 // The combination of the supported icon types. | |
| 285 const int icon_types_; | |
| 286 | |
| 287 // Whether the largest icon should be downloaded. | |
| 288 const bool download_largest_icon_; | |
| 289 | |
| 290 // The prioritized favicon candidates from the page back from the renderer. | |
| 291 std::vector<favicon::FaviconURL> image_urls_; | |
| 292 | |
| 293 // The FaviconRawBitmapResults from history. | |
| 294 std::vector<favicon_base::FaviconRawBitmapResult> history_results_; | |
| 295 | |
| 296 // The FaviconService which implements favicon operations. May be null during | |
| 297 // testing. | |
| 298 FaviconService* service_; | |
| 299 | |
| 300 // The client which implements embedder-specific Favicon operations. | |
| 301 FaviconClient* client_; // weak | |
| 302 | |
| 303 // This handler's driver. | |
| 304 FaviconDriver* driver_; // weak | |
| 305 | |
| 306 // Best image we've seen so far. As images are downloaded from the page they | |
| 307 // are stored here. When there is an exact match, or no more images are | |
| 308 // available the favicon service and the current page are updated (assuming | |
| 309 // the image is for a favicon). | |
| 310 FaviconCandidate best_favicon_candidate_; | |
| 311 | |
| 312 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); | |
| 313 }; | |
| 314 | |
| 315 #endif // COMPONENTS_FAVICON_CORE_BROWSER_FAVICON_HANDLER_H_ | |
| OLD | NEW |