Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 CHROME_BROWSER_FAVICON_HELPER_H__ | 5 #ifndef CHROME_BROWSER_FAVICON_HELPER_H__ |
| 6 #define CHROME_BROWSER_FAVICON_HELPER_H__ | 6 #define CHROME_BROWSER_FAVICON_HELPER_H__ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/ref_counted.h" | 13 #include "base/ref_counted.h" |
| 14 #include "chrome/browser/favicon_service.h" | 14 #include "chrome/browser/favicon_service.h" |
| 15 #include "chrome/common/icon_messages.h" | |
| 15 #include "chrome/common/ref_counted_util.h" | 16 #include "chrome/common/ref_counted_util.h" |
| 16 #include "content/browser/cancelable_request.h" | 17 #include "content/browser/cancelable_request.h" |
| 17 #include "content/browser/tab_contents/tab_contents_observer.h" | 18 #include "content/browser/tab_contents/tab_contents_observer.h" |
| 18 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
| 20 #include "ui/gfx/favicon_size.h" | |
| 19 | 21 |
| 20 class NavigationEntry; | 22 class NavigationEntry; |
| 21 class Profile; | 23 class Profile; |
| 22 class RefCountedMemory; | 24 class RefCountedMemory; |
| 23 class SkBitmap; | 25 class SkBitmap; |
| 24 class TabContents; | 26 class TabContents; |
| 25 | 27 |
| 26 // FaviconHelper is used to fetch the favicon for TabContents. | 28 // FaviconHelper is used to fetch the favicon for TabContents. |
| 27 // | 29 // |
| 28 // FetchFavicon requests the favicon from the favicon service which in turn | 30 // FetchFavicon requests the favicon from the favicon service which in turn |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 // OnFaviconData either updates the favicon of the NavigationEntry (if the | 62 // OnFaviconData either updates the favicon of the NavigationEntry (if the |
| 61 // db knew about the favicon), or requests the renderer to download the | 63 // db knew about the favicon), or requests the renderer to download the |
| 62 // favicon. | 64 // favicon. |
| 63 // | 65 // |
| 64 // When the renderer downloads the favicon SetFaviconImageData is invoked, | 66 // When the renderer downloads the favicon SetFaviconImageData is invoked, |
| 65 // at which point we update the favicon of the NavigationEntry and notify | 67 // at which point we update the favicon of the NavigationEntry and notify |
| 66 // the database to save the favicon. | 68 // the database to save the favicon. |
| 67 | 69 |
| 68 class FaviconHelper : public TabContentsObserver { | 70 class FaviconHelper : public TabContentsObserver { |
| 69 public: | 71 public: |
| 70 explicit FaviconHelper(TabContents* tab_contents); | 72 enum Type { |
| 73 FAVICON, | |
| 74 TOUCH, | |
| 75 }; | |
| 76 | |
| 77 FaviconHelper(TabContents* tab_contents, Type icon_type); | |
| 71 virtual ~FaviconHelper(); | 78 virtual ~FaviconHelper(); |
| 72 | 79 |
| 73 // Initiates loading the favicon for the specified url. | 80 // Initiates loading the favicon for the specified url. |
| 74 void FetchFavicon(const GURL& url); | 81 void FetchFavicon(const GURL& url); |
| 75 | 82 |
| 76 // Initiates loading an image from given |image_url|. Returns a download id | 83 // Initiates loading an image from given |image_url|. Returns a download id |
| 77 // for caller to track the request. When download completes, |callback| is | 84 // for caller to track the request. When download completes, |callback| is |
| 78 // called with the three params: the download_id, a boolean flag to indicate | 85 // called with the three params: the download_id, a boolean flag to indicate |
| 79 // whether the download succeeds and a SkBitmap as the downloaded image. | 86 // whether the download succeeds and a SkBitmap as the downloaded image. |
| 80 // Note that |image_size| is a hint for images with multiple sizes. The | 87 // Note that |image_size| is a hint for images with multiple sizes. The |
| 81 // downloaded image is not resized to the given image_size. If 0 is passed, | 88 // downloaded image is not resized to the given image_size. If 0 is passed, |
| 82 // the first frame of the image is returned. | 89 // the first frame of the image is returned. |
| 83 typedef Callback3<int, bool, const SkBitmap&>::Type ImageDownloadCallback; | 90 typedef Callback3<int, bool, const SkBitmap&>::Type ImageDownloadCallback; |
| 84 int DownloadImage(const GURL& image_url, int image_size, | 91 int DownloadImage(const GURL& image_url, |
| 92 int image_size, | |
| 93 history::IconType icon_type, | |
| 85 ImageDownloadCallback* callback); | 94 ImageDownloadCallback* callback); |
| 86 | 95 |
| 87 // Message Handler. Must be public, because also called from | 96 // Message Handler. Must be public, because also called from |
| 88 // PrerenderContents. | 97 // PrerenderContents. |
| 89 void OnUpdateFaviconURL(int32 page_id, const GURL& icon_url); | 98 void OnUpdateFaviconURL(int32 page_id, |
| 99 const std::vector<FaviconURL>& candidates); | |
| 100 | |
| 101 protected: | |
| 102 // These virtual methods make FaviconHelper testable and are overridden by | |
| 103 // TestFaviconHelper | |
| 104 // | |
| 105 // Return the NavigationEntry for the active entry, or NULL if the active | |
| 106 // entries URL does not match that of the URL last passed to FetchFavicon. | |
| 107 virtual NavigationEntry* GetEntry(); | |
| 108 | |
| 109 // Asks the render to download favicon, returns the request id. | |
| 110 virtual int DownloadFavicon(const GURL& image_url, int image_size); | |
| 111 | |
| 112 // Ask the favicon from history | |
| 113 virtual void UpdateFaviconMappingAndFetch( | |
| 114 const GURL& page_url, | |
| 115 const GURL& icon_url, | |
| 116 history::IconType icon_type, | |
| 117 CancelableRequestConsumerBase* consumer, | |
| 118 FaviconService::FaviconDataCallback* callback); | |
| 119 | |
| 120 virtual void GetFavicon( | |
| 121 const GURL& icon_url, | |
| 122 history::IconType icon_type, | |
| 123 CancelableRequestConsumerBase* consumer, | |
| 124 FaviconService::FaviconDataCallback* callback); | |
| 125 | |
| 126 virtual void GetFaviconForURL( | |
| 127 const GURL& page_url, | |
| 128 int icon_types, | |
| 129 CancelableRequestConsumerBase* consumer, | |
| 130 FaviconService::FaviconDataCallback* callback); | |
| 131 | |
| 132 virtual void SetHistoryFavicon(const GURL& page_url, | |
| 133 const GURL& icon_url, | |
| 134 const std::vector<unsigned char>& image_data, | |
| 135 history::IconType icon_type); | |
| 136 | |
| 137 virtual FaviconService* GetFaviconService(); | |
| 138 | |
| 139 // Returns true if the favicon should be saved. | |
| 140 virtual bool ShouldSaveFavicon(const GURL& url); | |
| 90 | 141 |
| 91 private: | 142 private: |
| 143 friend class TestFaviconHelper; // For testing | |
| 144 | |
| 92 struct DownloadRequest { | 145 struct DownloadRequest { |
| 93 DownloadRequest() {} | 146 DownloadRequest(); |
| 147 | |
| 94 DownloadRequest(const GURL& url, | 148 DownloadRequest(const GURL& url, |
| 95 const GURL& image_url, | 149 const GURL& image_url, |
| 96 ImageDownloadCallback* callback) | 150 ImageDownloadCallback* callback, |
| 97 : url(url), | 151 history::IconType icon_type); |
| 98 image_url(image_url), | |
| 99 callback(callback) { } | |
| 100 | 152 |
| 101 GURL url; | 153 GURL url; |
| 102 GURL image_url; | 154 GURL image_url; |
| 103 ImageDownloadCallback* callback; | 155 ImageDownloadCallback* callback; |
| 156 history::IconType icon_type; | |
| 104 }; | 157 }; |
| 105 | 158 |
| 159 static bool do_url_and_icon_match(const FaviconURL& favicon_url, | |
| 160 const GURL& url, | |
| 161 history::IconType icon_type) { | |
| 162 return favicon_url.icon_url == url && | |
| 163 favicon_url.icon_type == static_cast<IconType>(icon_type); | |
| 164 } | |
| 165 | |
| 166 // Returns history::IconType the given icon_type corresponds to. | |
| 167 static history::IconType ToHistoryIconType(IconType icon_type); | |
| 168 | |
| 106 // TabContentsObserver implementation. | 169 // TabContentsObserver implementation. |
| 107 virtual bool OnMessageReceived(const IPC::Message& message); | 170 virtual bool OnMessageReceived(const IPC::Message& message); |
| 108 | 171 |
| 109 void OnDidDownloadFavicon(int id, | 172 void OnDidDownloadFavicon(int id, |
| 110 const GURL& image_url, | 173 const GURL& image_url, |
| 111 bool errored, | 174 bool errored, |
| 112 const SkBitmap& image); | 175 const SkBitmap& image); |
| 113 | 176 |
| 114 // Return the NavigationEntry for the active entry, or NULL if the active | |
| 115 // entries URL does not match that of the URL last passed to FetchFavicon. | |
| 116 NavigationEntry* GetEntry(); | |
| 117 | |
| 118 Profile* profile(); | 177 Profile* profile(); |
| 119 | 178 |
| 120 FaviconService* GetFaviconService(); | |
| 121 | |
| 122 // See description above class for details. | 179 // See description above class for details. |
| 123 void OnFaviconDataForInitialURL(FaviconService::Handle handle, | 180 void OnFaviconDataForInitialURL(FaviconService::Handle handle, |
| 124 history::FaviconData favicon); | 181 history::FaviconData favicon); |
| 125 | 182 |
| 126 // If the favicon has expired, asks the renderer to download the favicon. | 183 // If the favicon has expired, asks the renderer to download the favicon. |
| 127 // Otherwise asks history to update the mapping between page url and icon | 184 // Otherwise asks history to update the mapping between page url and icon |
| 128 // url with a callback to OnFaviconData when done. | 185 // url with a callback to OnFaviconData when done. |
| 129 void DownloadFaviconOrAskHistory(NavigationEntry* entry); | 186 void DownloadFaviconOrAskHistory(const GURL& page_url, |
| 187 const GURL& icon_url, | |
| 188 history::IconType icon_type); | |
| 130 | 189 |
| 131 // See description above class for details. | 190 // See description above class for details. |
| 132 void OnFaviconData(FaviconService::Handle handle, | 191 void OnFaviconData(FaviconService::Handle handle, |
| 133 history::FaviconData favicon); | 192 history::FaviconData favicon); |
| 134 | 193 |
| 135 // Schedules a download for the specified entry. This adds the request to | 194 // Schedules a download for the specified entry. This adds the request to |
| 136 // download_requests_. | 195 // download_requests_. |
| 137 int ScheduleDownload(const GURL& url, const GURL& image_url, int image_size, | 196 int ScheduleDownload(const GURL& url, |
| 197 const GURL& image_url, | |
| 198 int image_size, | |
| 199 history::IconType icon_type, | |
| 138 ImageDownloadCallback* callback); | 200 ImageDownloadCallback* callback); |
| 139 | 201 |
| 140 // Sets the image data for the favicon. This is invoked asynchronously after | 202 // Sets the image data for the favicon. This is invoked asynchronously after |
| 141 // we request the TabContents to download the favicon. | 203 // we request the TabContents to download the favicon. |
| 142 void SetFavicon(const GURL& url, const GURL& icon_url, const SkBitmap& image); | 204 void SetFavicon(const GURL& url, |
| 205 const GURL& icon_url, | |
| 206 const SkBitmap& image, | |
| 207 history::IconType icon_type); | |
| 143 | 208 |
| 144 // Converts the image data to an SkBitmap and sets it on the NavigationEntry. | 209 // Converts the FAVICON's image data to an SkBitmap and sets it on the |
| 210 // NavigationEntry. | |
| 145 // If the TabContents has a delegate, it is notified of the new favicon | 211 // If the TabContents has a delegate, it is notified of the new favicon |
| 146 // (INVALIDATE_FAVICON). | 212 // (INVALIDATE_FAVICON). |
| 147 void UpdateFavicon(NavigationEntry* entry, | 213 void UpdateFavicon(NavigationEntry* entry, |
| 148 scoped_refptr<RefCountedMemory> data); | 214 scoped_refptr<RefCountedMemory> data); |
| 149 void UpdateFavicon(NavigationEntry* entry, const SkBitmap& image); | 215 void UpdateFavicon(NavigationEntry* entry, const SkBitmap& image); |
| 150 | 216 |
| 151 // Scales the image such that either the width and/or height is 16 pixels | 217 // Scales the image such that either the width and/or height is 16 pixels |
| 152 // wide. Does nothing if the image is empty. | 218 // wide. Does nothing if the image is empty. |
| 153 SkBitmap ConvertToFaviconSize(const SkBitmap& image); | 219 SkBitmap ConvertToFaviconSize(const SkBitmap& image); |
| 154 | 220 |
| 155 // Returns true if the favicon should be saved. | 221 void FetchFaviconInternal(); |
| 156 bool ShouldSaveFavicon(const GURL& url); | 222 |
| 223 // Return the current candidate if any. | |
| 224 FaviconURL* current_candidate() { | |
| 225 return (urls_.size() > current_url_index_) ? | |
| 226 &urls_[current_url_index_] : NULL; | |
| 227 } | |
| 228 | |
| 229 // Returns the preferred_icon_size according icon_types_, 0 means no | |
| 230 // preference. | |
| 231 int preferred_icon_size() { | |
|
sky
2011/03/29 16:07:22
const
michaelbai
2011/04/08 22:33:35
Done.
| |
| 232 return icon_types_ == history::FAVICON ? kFaviconSize : 0; | |
| 233 } | |
| 157 | 234 |
| 158 // Used for history requests. | 235 // Used for history requests. |
| 159 CancelableRequestConsumer cancelable_consumer_; | 236 CancelableRequestConsumer cancelable_consumer_; |
| 160 | 237 |
| 161 // URL of the page we're requesting the favicon for. | 238 // URL of the page we're requesting the favicon for. |
| 162 GURL url_; | 239 GURL url_; |
| 163 | 240 |
| 164 // Whether we got the url for the page back from the renderer. | |
| 165 // See "Favicon Details" in tab_contents.cc for more details. | |
| 166 bool got_favicon_url_; | |
| 167 | |
| 168 // Whether we got the initial response for the favicon back from the renderer. | 241 // Whether we got the initial response for the favicon back from the renderer. |
| 169 // See "Favicon Details" in tab_contents.cc for more details. | 242 // See "Favicon Details" in tab_contents.cc for more details. |
| 170 bool got_favicon_from_history_; | 243 bool got_favicon_from_history_; |
| 171 | 244 |
| 172 // Whether the favicon is out of date. If true, it means history knows about | 245 // Whether the favicon is out of date. If true, it means history knows about |
| 173 // the favicon, but we need to download the favicon because the icon has | 246 // the favicon, but we need to download the favicon because the icon has |
| 174 // expired. | 247 // expired. |
| 175 // See "Favicon Details" in tab_contents.cc for more details. | 248 // See "Favicon Details" in tab_contents.cc for more details. |
| 176 bool favicon_expired_; | 249 bool favicon_expired_; |
| 177 | 250 |
| 178 // Requests to the renderer to download favicons. | 251 // Requests to the renderer to download favicons. |
| 179 typedef std::map<int, DownloadRequest> DownloadRequests; | 252 typedef std::map<int, DownloadRequest> DownloadRequests; |
| 180 DownloadRequests download_requests_; | 253 DownloadRequests download_requests_; |
| 181 | 254 |
| 255 // The combination of the supported icon types. | |
| 256 const int icon_types_; | |
| 257 | |
| 258 // The prioritized favicon candidates from the page back from the renderer. | |
| 259 std::vector<FaviconURL> urls_; | |
| 260 | |
| 261 // The current candidate's index in urls_. | |
| 262 size_t current_url_index_; | |
| 263 | |
| 264 // The FaviconData from history. | |
| 265 history::FaviconData history_icon_; | |
| 266 | |
| 182 DISALLOW_COPY_AND_ASSIGN(FaviconHelper); | 267 DISALLOW_COPY_AND_ASSIGN(FaviconHelper); |
| 183 }; | 268 }; |
| 184 | 269 |
| 185 #endif // CHROME_BROWSER_FAVICON_HELPER_H__ | 270 #endif // CHROME_BROWSER_FAVICON_HELPER_H__ |
| OLD | NEW |