| 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 CHROME_BROWSER_FAVICON_FAVICON_HANDLER_H_ | |
| 6 #define CHROME_BROWSER_FAVICON_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 }; | |
| 80 | |
| 81 FaviconHandler(FaviconService* service, | |
| 82 FaviconClient* client, | |
| 83 FaviconDriver* driver, | |
| 84 Type icon_type, | |
| 85 bool download_largest_icon); | |
| 86 virtual ~FaviconHandler(); | |
| 87 | |
| 88 // Initiates loading the favicon for the specified url. | |
| 89 void FetchFavicon(const GURL& url); | |
| 90 | |
| 91 // Message Handler. Must be public, because also called from | |
| 92 // PrerenderContents. Collects the |image_urls| list. | |
| 93 void OnUpdateFaviconURL(const std::vector<favicon::FaviconURL>& candidates); | |
| 94 | |
| 95 // Processes the current image_irls_ entry, requesting the image from the | |
| 96 // history / download service. | |
| 97 void ProcessCurrentUrl(); | |
| 98 | |
| 99 // Message handler for ImageHostMsg_DidDownloadImage. Called when the image | |
| 100 // at |image_url| has been downloaded. | |
| 101 // |bitmaps| is a list of all the frames of the image at |image_url|. | |
| 102 // |original_bitmap_sizes| are the sizes of |bitmaps| before they were resized | |
| 103 // to the maximum bitmap size passed to DownloadFavicon(). | |
| 104 void OnDidDownloadFavicon( | |
| 105 int id, | |
| 106 const GURL& image_url, | |
| 107 const std::vector<SkBitmap>& bitmaps, | |
| 108 const std::vector<gfx::Size>& original_bitmap_sizes); | |
| 109 | |
| 110 // For testing. | |
| 111 const std::vector<favicon::FaviconURL>& image_urls() const { | |
| 112 return image_urls_; | |
| 113 } | |
| 114 | |
| 115 protected: | |
| 116 // These virtual methods make FaviconHandler testable and are overridden by | |
| 117 // TestFaviconHandler. | |
| 118 | |
| 119 // Asks the render to download favicon, returns the request id. | |
| 120 virtual int DownloadFavicon(const GURL& image_url, int max_bitmap_size); | |
| 121 | |
| 122 // Ask the favicon from history | |
| 123 virtual void UpdateFaviconMappingAndFetch( | |
| 124 const GURL& page_url, | |
| 125 const GURL& icon_url, | |
| 126 favicon_base::IconType icon_type, | |
| 127 const favicon_base::FaviconResultsCallback& callback, | |
| 128 base::CancelableTaskTracker* tracker); | |
| 129 | |
| 130 virtual void GetFaviconFromFaviconService( | |
| 131 const GURL& icon_url, | |
| 132 favicon_base::IconType icon_type, | |
| 133 const favicon_base::FaviconResultsCallback& callback, | |
| 134 base::CancelableTaskTracker* tracker); | |
| 135 | |
| 136 virtual void GetFaviconForURLFromFaviconService( | |
| 137 const GURL& page_url, | |
| 138 int icon_types, | |
| 139 const favicon_base::FaviconResultsCallback& callback, | |
| 140 base::CancelableTaskTracker* tracker); | |
| 141 | |
| 142 virtual void SetHistoryFavicons(const GURL& page_url, | |
| 143 const GURL& icon_url, | |
| 144 favicon_base::IconType icon_type, | |
| 145 const gfx::Image& image); | |
| 146 | |
| 147 // Returns true if the favicon should be saved. | |
| 148 virtual bool ShouldSaveFavicon(const GURL& url); | |
| 149 | |
| 150 private: | |
| 151 // For testing: | |
| 152 friend class FaviconTabHelperTest; | |
| 153 friend class TestFaviconHandler; | |
| 154 | |
| 155 // Represents an in progress download of an image from the renderer. | |
| 156 struct DownloadRequest { | |
| 157 DownloadRequest(); | |
| 158 ~DownloadRequest(); | |
| 159 | |
| 160 DownloadRequest(const GURL& url, | |
| 161 const GURL& image_url, | |
| 162 favicon_base::IconType icon_type); | |
| 163 | |
| 164 GURL url; | |
| 165 GURL image_url; | |
| 166 favicon_base::IconType icon_type; | |
| 167 }; | |
| 168 | |
| 169 // Used to track a candidate for the favicon. | |
| 170 struct FaviconCandidate { | |
| 171 FaviconCandidate(); | |
| 172 ~FaviconCandidate(); | |
| 173 | |
| 174 FaviconCandidate(const GURL& url, | |
| 175 const GURL& image_url, | |
| 176 const gfx::Image& image, | |
| 177 float score, | |
| 178 favicon_base::IconType icon_type); | |
| 179 | |
| 180 GURL url; | |
| 181 GURL image_url; | |
| 182 gfx::Image image; | |
| 183 float score; | |
| 184 favicon_base::IconType icon_type; | |
| 185 }; | |
| 186 | |
| 187 // Get the maximal icon size in pixels for a icon of type |icon_type| for the | |
| 188 // current platform. | |
| 189 static int GetMaximalIconSize(favicon_base::IconType icon_type); | |
| 190 | |
| 191 // See description above class for details. | |
| 192 void OnFaviconDataForInitialURLFromFaviconService(const std::vector< | |
| 193 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results); | |
| 194 | |
| 195 // If the favicon has expired, asks the renderer to download the favicon. | |
| 196 // Otherwise asks history to update the mapping between page url and icon | |
| 197 // url with a callback to OnFaviconData when done. | |
| 198 void DownloadFaviconOrAskFaviconService(const GURL& page_url, | |
| 199 const GURL& icon_url, | |
| 200 favicon_base::IconType icon_type); | |
| 201 | |
| 202 // See description above class for details. | |
| 203 void OnFaviconData(const std::vector<favicon_base::FaviconRawBitmapResult>& | |
| 204 favicon_bitmap_results); | |
| 205 | |
| 206 // Schedules a download for the specified entry. This adds the request to | |
| 207 // download_requests_. | |
| 208 int ScheduleDownload(const GURL& url, | |
| 209 const GURL& image_url, | |
| 210 favicon_base::IconType icon_type); | |
| 211 | |
| 212 // Updates |favicon_candidate_| and returns true if it is an exact match. | |
| 213 bool UpdateFaviconCandidate(const GURL& url, | |
| 214 const GURL& image_url, | |
| 215 const gfx::Image& image, | |
| 216 float score, | |
| 217 favicon_base::IconType icon_type); | |
| 218 | |
| 219 // Sets the image data for the favicon. | |
| 220 void SetFavicon(const GURL& url, | |
| 221 const GURL& icon_url, | |
| 222 const gfx::Image& image, | |
| 223 favicon_base::IconType icon_type); | |
| 224 | |
| 225 // Notifies |driver_| favicon available. See | |
| 226 // FaviconDriver::NotifyFaviconAvailable() for |is_active_favicon| in detail. | |
| 227 void NotifyFaviconAvailable( | |
| 228 const std::vector<favicon_base::FaviconRawBitmapResult>& | |
| 229 favicon_bitmap_results, | |
| 230 bool is_active_favicon); | |
| 231 void NotifyFaviconAvailable(const GURL& icon_url, | |
| 232 const gfx::Image& image, | |
| 233 bool is_active_favicon); | |
| 234 | |
| 235 // Return the current candidate if any. | |
| 236 favicon::FaviconURL* current_candidate() { | |
| 237 return (!image_urls_.empty()) ? &image_urls_.front() : NULL; | |
| 238 } | |
| 239 | |
| 240 // Returns whether the page's url changed since the favicon was requested. | |
| 241 bool PageChangedSinceFaviconWasRequested(); | |
| 242 | |
| 243 // Returns the preferred size of the image. 0 means no preference (any size | |
| 244 // will do). | |
| 245 int preferred_icon_size() const { | |
| 246 if (download_largest_icon_) | |
| 247 return 0; | |
| 248 return icon_types_ == favicon_base::FAVICON ? gfx::kFaviconSize : 0; | |
| 249 } | |
| 250 | |
| 251 // Sorts the entries in |image_urls_| by icon size in descending order. | |
| 252 // Additionally removes any entries whose sizes are all greater than the max | |
| 253 // allowed size. | |
| 254 void SortAndPruneImageUrls(); | |
| 255 | |
| 256 // Used for FaviconService requests. | |
| 257 base::CancelableTaskTracker cancelable_task_tracker_; | |
| 258 | |
| 259 // URL of the page we're requesting the favicon for. | |
| 260 GURL url_; | |
| 261 | |
| 262 // Whether we are waiting for data from the FaviconService. | |
| 263 bool waiting_for_favicon_service_data_; | |
| 264 | |
| 265 // Whether we got data back for the initial request to the FaviconService. | |
| 266 bool got_favicon_from_history_; | |
| 267 | |
| 268 // Whether the favicon is out of date or the favicon data in | |
| 269 // |history_results_| is known to be incomplete. If true, it means history | |
| 270 // knows about the favicon, but we need to download the favicon because the | |
| 271 // icon has expired or the data in the database is incomplete. | |
| 272 bool favicon_expired_or_incomplete_; | |
| 273 | |
| 274 // Requests to the renderer to download favicons. | |
| 275 typedef std::map<int, DownloadRequest> DownloadRequests; | |
| 276 DownloadRequests download_requests_; | |
| 277 | |
| 278 // The combination of the supported icon types. | |
| 279 const int icon_types_; | |
| 280 | |
| 281 // Whether the largest icon should be downloaded. | |
| 282 const bool download_largest_icon_; | |
| 283 | |
| 284 // The prioritized favicon candidates from the page back from the renderer. | |
| 285 std::vector<favicon::FaviconURL> image_urls_; | |
| 286 | |
| 287 // The FaviconRawBitmapResults from history. | |
| 288 std::vector<favicon_base::FaviconRawBitmapResult> history_results_; | |
| 289 | |
| 290 // The FaviconService which implements favicon operations. May be null during | |
| 291 // testing. | |
| 292 FaviconService* service_; | |
| 293 | |
| 294 // The client which implements embedder-specific Favicon operations. | |
| 295 FaviconClient* client_; // weak | |
| 296 | |
| 297 // This handler's driver. | |
| 298 FaviconDriver* driver_; // weak | |
| 299 | |
| 300 // Best image we've seen so far. As images are downloaded from the page they | |
| 301 // are stored here. When there is an exact match, or no more images are | |
| 302 // available the favicon service and the current page are updated (assuming | |
| 303 // the image is for a favicon). | |
| 304 FaviconCandidate best_favicon_candidate_; | |
| 305 | |
| 306 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); | |
| 307 }; | |
| 308 | |
| 309 #endif // CHROME_BROWSER_FAVICON_FAVICON_HANDLER_H_ | |
| OLD | NEW |