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 #include "chrome/browser/favicon_helper.h" | 5 #include "chrome/browser/favicon_helper.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/ref_counted_memory.h" | 12 #include "base/ref_counted_memory.h" |
| 13 #include "chrome/browser/bookmarks/bookmark_model.h" | 13 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/render_messages.h" | 15 #include "chrome/common/render_messages.h" |
| 16 #include "content/browser/renderer_host/render_view_host.h" | 16 #include "content/browser/renderer_host/render_view_host.h" |
| 17 #include "content/browser/tab_contents/navigation_controller.h" | 17 #include "content/browser/tab_contents/navigation_controller.h" |
| 18 #include "content/browser/tab_contents/navigation_entry.h" | 18 #include "content/browser/tab_contents/navigation_entry.h" |
| 19 #include "content/browser/tab_contents/tab_contents_delegate.h" | 19 #include "content/browser/tab_contents/tab_contents_delegate.h" |
| 20 #include "content/browser/tab_contents/tab_contents.h" | 20 #include "content/browser/tab_contents/tab_contents.h" |
| 21 #include "skia/ext/image_operations.h" | 21 #include "skia/ext/image_operations.h" |
| 22 #include "ui/gfx/codec/png_codec.h" | 22 #include "ui/gfx/codec/png_codec.h" |
| 23 #include "ui/gfx/favicon_size.h" | |
| 24 | 23 |
| 25 FaviconHelper::FaviconHelper(TabContents* tab_contents) | 24 FaviconHelper::DownloadRequest::DownloadRequest() |
| 25 : callback(NULL), | |
| 26 icon_type(history::INVALID_ICON) { | |
| 27 } | |
| 28 | |
| 29 FaviconHelper::DownloadRequest::DownloadRequest(const GURL& url, | |
| 30 const GURL& image_url, | |
| 31 ImageDownloadCallback* callback, | |
| 32 history::IconType icon_type) | |
| 33 : url(url), | |
| 34 image_url(image_url), | |
| 35 callback(callback), | |
| 36 icon_type(icon_type) { | |
| 37 } | |
| 38 | |
| 39 FaviconHelper::FaviconHelper(TabContents* tab_contents, | |
| 40 Type icon_type) | |
| 26 : TabContentsObserver(tab_contents), | 41 : TabContentsObserver(tab_contents), |
| 27 got_favicon_url_(false), | |
| 28 got_favicon_from_history_(false), | 42 got_favicon_from_history_(false), |
| 29 favicon_expired_(false) { | 43 favicon_expired_(false), |
| 44 icon_types_(icon_type == FAVICON ? history::FAVICON : | |
| 45 history::TOUCH_ICON | history::TOUCH_PRECOMPOSED_ICON), | |
| 46 current_url_index_(0) { | |
| 30 } | 47 } |
| 31 | 48 |
| 32 FaviconHelper::~FaviconHelper() { | 49 FaviconHelper::~FaviconHelper() { |
| 33 SkBitmap empty_image; | 50 SkBitmap empty_image; |
| 34 | 51 |
| 35 // Call pending download callbacks with error to allow caller to clean up. | 52 // Call pending download callbacks with error to allow caller to clean up. |
| 36 for (DownloadRequests::iterator i = download_requests_.begin(); | 53 for (DownloadRequests::iterator i = download_requests_.begin(); |
| 37 i != download_requests_.end(); ++i) { | 54 i != download_requests_.end(); ++i) { |
| 38 if (i->second.callback) { | 55 if (i->second.callback) { |
| 39 i->second.callback->Run(i->first, true, empty_image); | 56 i->second.callback->Run(i->first, true, empty_image); |
| 40 } | 57 } |
| 41 } | 58 } |
| 42 } | 59 } |
| 43 | 60 |
| 44 void FaviconHelper::FetchFavicon(const GURL& url) { | 61 void FaviconHelper::FetchFavicon(const GURL& url) { |
| 45 cancelable_consumer_.CancelAllRequests(); | 62 cancelable_consumer_.CancelAllRequests(); |
| 46 | 63 |
| 47 url_ = url; | 64 url_ = url; |
| 48 | 65 |
| 49 favicon_expired_ = got_favicon_from_history_ = got_favicon_url_ = false; | 66 favicon_expired_ = got_favicon_from_history_ = false; |
| 67 current_url_index_ = 0; | |
| 68 urls_.clear(); | |
| 50 | 69 |
| 51 // Request the favicon from the history service. In parallel to this the | 70 // Request the favicon from the history service. In parallel to this the |
| 52 // renderer is going to notify us (well TabContents) when the favicon url is | 71 // renderer is going to notify us (well TabContents) when the favicon url is |
| 53 // available. | 72 // available. |
| 54 if (GetFaviconService()) { | 73 if (GetFaviconService()) { |
| 55 GetFaviconService()->GetFaviconForURL(url_, history::FAVICON, | 74 GetFaviconForURL(url_, icon_types_, &cancelable_consumer_, |
| 56 &cancelable_consumer_, | |
| 57 NewCallback(this, &FaviconHelper::OnFaviconDataForInitialURL)); | 75 NewCallback(this, &FaviconHelper::OnFaviconDataForInitialURL)); |
| 58 } | 76 } |
| 59 } | 77 } |
| 60 | 78 |
| 61 int FaviconHelper::DownloadImage(const GURL& image_url, | 79 int FaviconHelper::DownloadImage(const GURL& image_url, |
| 62 int image_size, | 80 int image_size, |
| 81 history::IconType icon_type, | |
| 63 ImageDownloadCallback* callback) { | 82 ImageDownloadCallback* callback) { |
| 64 DCHECK(callback); // Must provide a callback. | 83 DCHECK(callback); // Must provide a callback. |
| 65 return ScheduleDownload(GURL(), image_url, image_size, callback); | 84 return ScheduleDownload(GURL(), image_url, image_size, icon_type, callback); |
| 66 } | 85 } |
| 67 | 86 |
| 68 Profile* FaviconHelper::profile() { | 87 Profile* FaviconHelper::profile() { |
| 69 return tab_contents()->profile(); | 88 return tab_contents()->profile(); |
| 70 } | 89 } |
| 71 | 90 |
| 72 FaviconService* FaviconHelper::GetFaviconService() { | |
| 73 return profile()->GetFaviconService(Profile::EXPLICIT_ACCESS); | |
| 74 } | |
| 75 | |
| 76 void FaviconHelper::SetFavicon( | 91 void FaviconHelper::SetFavicon( |
| 77 const GURL& url, | 92 const GURL& url, |
| 78 const GURL& image_url, | 93 const GURL& image_url, |
| 79 const SkBitmap& image) { | 94 const SkBitmap& image, |
| 80 const SkBitmap& sized_image = | 95 history::IconType icon_type) { |
| 81 (image.width() == kFaviconSize && image.height() == kFaviconSize) | 96 const SkBitmap& sized_image = (preferred_icon_size() == 0 || |
| 82 ? image : ConvertToFaviconSize(image); | 97 (preferred_icon_size() == image.width() && |
| 98 preferred_icon_size() == image.height())) ? | |
|
sky
2011/03/29 16:07:22
align with p on previous line, eg:
(preferred_i
michaelbai
2011/04/08 22:33:35
Done.
| |
| 99 image : ConvertToFaviconSize(image); | |
| 83 | 100 |
| 84 if (GetFaviconService() && ShouldSaveFavicon(url)) { | 101 if (GetFaviconService() && ShouldSaveFavicon(url)) { |
| 85 std::vector<unsigned char> image_data; | 102 std::vector<unsigned char> image_data; |
| 86 gfx::PNGCodec::EncodeBGRASkBitmap(sized_image, false, &image_data); | 103 gfx::PNGCodec::EncodeBGRASkBitmap(sized_image, false, &image_data); |
| 87 GetFaviconService()->SetFavicon(url, image_url, image_data, | 104 SetHistoryFavicon(url, image_url, image_data, icon_type); |
| 88 history::FAVICON); | |
| 89 } | 105 } |
| 90 | 106 |
| 91 if (url == url_) { | 107 if (url == url_ && icon_type == history::FAVICON) { |
| 92 NavigationEntry* entry = GetEntry(); | 108 NavigationEntry* entry = GetEntry(); |
| 93 if (entry) | 109 if (entry) |
| 94 UpdateFavicon(entry, sized_image); | 110 UpdateFavicon(entry, sized_image); |
| 95 } | 111 } |
| 96 } | 112 } |
| 97 | 113 |
| 98 void FaviconHelper::UpdateFavicon(NavigationEntry* entry, | 114 void FaviconHelper::UpdateFavicon(NavigationEntry* entry, |
| 99 scoped_refptr<RefCountedMemory> data) { | 115 scoped_refptr<RefCountedMemory> data) { |
| 100 SkBitmap image; | 116 SkBitmap image; |
| 101 gfx::PNGCodec::Decode(data->front(), data->size(), &image); | 117 gfx::PNGCodec::Decode(data->front(), data->size(), &image); |
| 102 UpdateFavicon(entry, image); | 118 UpdateFavicon(entry, image); |
| 103 } | 119 } |
| 104 | 120 |
| 105 void FaviconHelper::UpdateFavicon(NavigationEntry* entry, | 121 void FaviconHelper::UpdateFavicon(NavigationEntry* entry, |
| 106 const SkBitmap& image) { | 122 const SkBitmap& image) { |
| 107 // No matter what happens, we need to mark the favicon as being set. | 123 // No matter what happens, we need to mark the favicon as being set. |
| 108 entry->favicon().set_is_valid(true); | 124 entry->favicon().set_is_valid(true); |
| 109 | 125 |
| 110 if (image.empty()) | 126 if (image.empty()) |
| 111 return; | 127 return; |
| 112 | 128 |
| 113 entry->favicon().set_bitmap(image); | 129 entry->favicon().set_bitmap(image); |
| 114 tab_contents()->NotifyNavigationStateChanged(TabContents::INVALIDATE_TAB); | 130 tab_contents()->NotifyNavigationStateChanged(TabContents::INVALIDATE_TAB); |
| 115 } | 131 } |
| 116 | 132 |
| 117 void FaviconHelper::OnUpdateFaviconURL(int32 page_id, const GURL& icon_url) { | 133 void FaviconHelper::OnUpdateFaviconURL( |
| 118 // TODO(davemoore) Should clear on empty url. Currently we ignore it. | 134 int32 page_id, |
| 119 // This appears to be what FF does as well. | 135 const std::vector<FaviconURL>& candidates) { |
| 120 if (icon_url.is_empty()) | |
| 121 return; | |
| 122 | |
| 123 NavigationEntry* entry = GetEntry(); | 136 NavigationEntry* entry = GetEntry(); |
| 124 if (!entry) | 137 if (!entry) |
| 125 return; | 138 return; |
| 126 | 139 |
| 127 got_favicon_url_ = true; | 140 bool got_favicon_url_update = false; |
| 141 for (std::vector<FaviconURL>::const_iterator i = candidates.begin(); | |
| 142 i != candidates.end(); ++i) { | |
| 143 if (!i->icon_url.is_empty() && (i->icon_type & icon_types_)) { | |
| 144 if (!got_favicon_url_update) { | |
| 145 got_favicon_url_update = true; | |
| 146 urls_.clear(); | |
| 147 current_url_index_ = 0; | |
| 148 } | |
| 149 urls_.push_back(*i); | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 // TODO(davemoore) Should clear on empty url. Currently we ignore it. | |
| 154 // This appears to be what FF does as well. | |
| 155 // No URL was added. | |
| 156 if (!got_favicon_url_update) | |
| 157 return; | |
| 128 | 158 |
| 129 if (!GetFaviconService()) | 159 if (!GetFaviconService()) |
| 130 return; | 160 return; |
| 131 | 161 |
| 132 if (!favicon_expired_ && entry->favicon().is_valid() && | 162 // For FAVICON. |
| 133 entry->favicon().url() == icon_url) { | 163 if (current_candidate()->icon_type == ::FAVICON) { |
| 134 // We already have the icon, no need to proceed. | 164 if (!favicon_expired_ && entry->favicon().is_valid() && |
| 135 return; | 165 do_url_and_icon_match(*current_candidate(), entry->favicon().url(), |
| 166 history::FAVICON)) | |
| 167 return; | |
| 168 | |
| 169 entry->favicon().set_url(current_candidate()->icon_url); | |
| 136 } | 170 } |
| 137 | 171 |
| 138 entry->favicon().set_url(icon_url); | 172 // For the icons other than FAVICON. |
| 173 if (!favicon_expired_ && current_candidate()->icon_type != ::FAVICON && | |
|
sky
2011/03/29 16:07:22
This should be an else from 163, eg:
if (current_
michaelbai
2011/04/08 22:33:35
Done.
| |
| 174 got_favicon_from_history_ && history_icon_.is_valid() && | |
| 175 do_url_and_icon_match(*current_candidate(), history_icon_.icon_url, | |
| 176 history_icon_.icon_type)) | |
| 177 return; | |
| 139 | 178 |
| 140 if (got_favicon_from_history_) | 179 if (got_favicon_from_history_) |
| 141 DownloadFaviconOrAskHistory(entry); | 180 DownloadFaviconOrAskHistory(entry->url(), current_candidate()->icon_url, |
| 181 static_cast<history::IconType>(current_candidate()->icon_type)); | |
| 182 } | |
| 183 | |
| 184 NavigationEntry* FaviconHelper::GetEntry() { | |
| 185 NavigationEntry* entry = tab_contents()->controller().GetActiveEntry(); | |
| 186 if (entry && entry->url() == url_ && | |
| 187 tab_contents()->IsActiveEntry(entry->page_id())) { | |
| 188 return entry; | |
| 189 } | |
| 190 // If the URL has changed out from under us (as will happen with redirects) | |
| 191 // return NULL. | |
| 192 return NULL; | |
| 193 } | |
| 194 | |
| 195 int FaviconHelper::DownloadFavicon(const GURL& image_url, int image_size) { | |
| 196 return tab_contents()->render_view_host()->DownloadFavicon(image_url, | |
| 197 image_size); | |
| 198 } | |
| 199 | |
| 200 void FaviconHelper::UpdateFaviconMappingAndFetch( | |
| 201 const GURL& page_url, | |
| 202 const GURL& icon_url, | |
| 203 history::IconType icon_type, | |
| 204 CancelableRequestConsumerBase* consumer, | |
| 205 FaviconService::FaviconDataCallback* callback) { | |
| 206 GetFaviconService()->UpdateFaviconMappingAndFetch(page_url, icon_url, | |
| 207 icon_type, consumer, callback); | |
| 208 } | |
| 209 | |
| 210 void FaviconHelper::GetFavicon( | |
| 211 const GURL& icon_url, | |
| 212 history::IconType icon_type, | |
| 213 CancelableRequestConsumerBase* consumer, | |
| 214 FaviconService::FaviconDataCallback* callback) { | |
| 215 GetFaviconService()->GetFavicon(icon_url, icon_type, consumer, callback); | |
| 216 } | |
| 217 | |
| 218 void FaviconHelper::GetFaviconForURL( | |
| 219 const GURL& page_url, | |
| 220 int icon_types, | |
| 221 CancelableRequestConsumerBase* consumer, | |
| 222 FaviconService::FaviconDataCallback* callback) { | |
| 223 GetFaviconService()->GetFaviconForURL(page_url, icon_types, consumer, | |
| 224 callback); | |
| 225 } | |
| 226 | |
| 227 void FaviconHelper::SetHistoryFavicon( | |
| 228 const GURL& page_url, | |
| 229 const GURL& icon_url, | |
| 230 const std::vector<unsigned char>& image_data, | |
| 231 history::IconType icon_type) { | |
| 232 GetFaviconService()->SetFavicon(page_url, icon_url, image_data, icon_type); | |
| 233 } | |
| 234 | |
| 235 FaviconService* FaviconHelper::GetFaviconService() { | |
| 236 return profile()->GetFaviconService(Profile::EXPLICIT_ACCESS); | |
| 237 } | |
| 238 | |
| 239 bool FaviconHelper::ShouldSaveFavicon(const GURL& url) { | |
| 240 if (!profile()->IsOffTheRecord()) | |
| 241 return true; | |
| 242 | |
| 243 // Otherwise store the favicon if the page is bookmarked. | |
| 244 BookmarkModel* bookmark_model = profile()->GetBookmarkModel(); | |
| 245 return bookmark_model && bookmark_model->IsBookmarked(url); | |
| 246 } | |
| 247 | |
| 248 history::IconType FaviconHelper::ToHistoryIconType(IconType icon_type) { | |
| 249 switch(icon_type) { | |
| 250 case ::FAVICON: | |
| 251 return history::FAVICON; | |
| 252 case ::TOUCH_ICON: | |
| 253 return history::TOUCH_ICON; | |
| 254 case ::TOUCH_PRECOMPOSED_ICON: | |
| 255 return history::TOUCH_PRECOMPOSED_ICON; | |
| 256 case ::INVALID_ICON: | |
| 257 return history::INVALID_ICON; | |
| 258 } | |
| 259 NOTREACHED(); | |
| 260 // Shouldn't reach here, just make compiler happy. | |
| 261 return history::INVALID_ICON; | |
| 142 } | 262 } |
| 143 | 263 |
| 144 bool FaviconHelper::OnMessageReceived(const IPC::Message& message) { | 264 bool FaviconHelper::OnMessageReceived(const IPC::Message& message) { |
| 145 bool handled = true; | 265 bool message_handled = true; |
| 146 IPC_BEGIN_MESSAGE_MAP(FaviconHelper, message) | 266 IPC_BEGIN_MESSAGE_MAP(FaviconHelper, message) |
| 147 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateFaviconURL, OnUpdateFaviconURL) | |
| 148 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDownloadFavicon, OnDidDownloadFavicon) | 267 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDownloadFavicon, OnDidDownloadFavicon) |
| 149 IPC_MESSAGE_UNHANDLED(handled = false) | 268 IPC_MESSAGE_UNHANDLED(message_handled = false) |
| 150 IPC_END_MESSAGE_MAP() | 269 IPC_END_MESSAGE_MAP() |
| 151 return handled; | 270 return message_handled; |
| 152 } | 271 } |
| 153 | 272 |
| 154 void FaviconHelper::OnDidDownloadFavicon(int id, | 273 void FaviconHelper::OnDidDownloadFavicon(int id, |
| 155 const GURL& image_url, | 274 const GURL& image_url, |
| 156 bool errored, | 275 bool errored, |
| 157 const SkBitmap& image) { | 276 const SkBitmap& image) { |
| 158 DownloadRequests::iterator i = download_requests_.find(id); | 277 DownloadRequests::iterator i = download_requests_.find(id); |
| 159 if (i == download_requests_.end()) { | 278 if (i == download_requests_.end()) { |
| 160 // Currently TabContents notifies us of ANY downloads so that it is | 279 // Currently TabContents notifies us of ANY downloads so that it is |
| 161 // possible to get here. | 280 // possible to get here. |
| 162 return; | 281 return; |
| 163 } | 282 } |
| 164 | 283 |
| 165 if (i->second.callback) { | 284 if (i->second.callback) { |
| 166 i->second.callback->Run(id, errored, image); | 285 i->second.callback->Run(id, errored, image); |
| 167 } else if (!errored) { | 286 } else if (current_candidate() && |
| 168 SetFavicon(i->second.url, image_url, image); | 287 do_url_and_icon_match(*current_candidate(), image_url, |
| 288 i->second.icon_type)) { | |
| 289 // The downloaded icon is still valid when there is no FaviconURL update | |
| 290 // during the downloading. | |
| 291 if (!errored) { | |
| 292 SetFavicon(i->second.url, image_url, image, i->second.icon_type); | |
| 293 } | |
| 294 else if (GetEntry() && ++current_url_index_ < urls_.size()) { | |
| 295 // Copies all candidate except first one and notifies the FaviconHelper, | |
| 296 // so the next candidate can be processed. | |
| 297 std::vector<FaviconURL> new_candidates(++urls_.begin(), urls_.end()); | |
| 298 OnUpdateFaviconURL(0, new_candidates); | |
| 299 } | |
| 169 } | 300 } |
| 170 | |
| 171 download_requests_.erase(i); | 301 download_requests_.erase(i); |
| 172 } | 302 } |
| 173 | 303 |
| 174 NavigationEntry* FaviconHelper::GetEntry() { | 304 void FaviconHelper::OnFaviconDataForInitialURL(FaviconService::Handle handle, |
| 175 NavigationEntry* entry = tab_contents()->controller().GetActiveEntry(); | 305 history::FaviconData favicon) { |
| 176 if (entry && entry->url() == url_ && | |
| 177 tab_contents()->IsActiveEntry(entry->page_id())) { | |
| 178 return entry; | |
| 179 } | |
| 180 // If the URL has changed out from under us (as will happen with redirects) | |
| 181 // return NULL. | |
| 182 return NULL; | |
| 183 } | |
| 184 | |
| 185 void FaviconHelper::OnFaviconDataForInitialURL( | |
| 186 FaviconService::Handle handle, | |
| 187 history::FaviconData favicon) { | |
| 188 NavigationEntry* entry = GetEntry(); | 306 NavigationEntry* entry = GetEntry(); |
| 189 if (!entry) | 307 if (!entry) |
| 190 return; | 308 return; |
| 191 | 309 |
| 192 got_favicon_from_history_ = true; | 310 got_favicon_from_history_ = true; |
| 311 history_icon_ = favicon; | |
| 193 | 312 |
| 194 favicon_expired_ = (favicon.known_icon && favicon.expired); | 313 favicon_expired_ = (favicon.known_icon && favicon.expired); |
| 195 | 314 |
| 196 if (favicon.known_icon && !entry->favicon().is_valid() && | 315 if (favicon.known_icon && favicon.icon_type == history::FAVICON && |
| 197 (!got_favicon_url_ || entry->favicon().url() == favicon.icon_url)) { | 316 !entry->favicon().is_valid() && |
| 317 (!current_candidate() || do_url_and_icon_match(*current_candidate(), | |
| 318 favicon.icon_url, favicon.icon_type))) { | |
| 198 // The db knows the favicon (although it may be out of date) and the entry | 319 // The db knows the favicon (although it may be out of date) and the entry |
| 199 // doesn't have an icon. Set the favicon now, and if the favicon turns out | 320 // doesn't have an icon. Set the favicon now, and if the favicon turns out |
| 200 // to be expired (or the wrong url) we'll fetch later on. This way the | 321 // to be expired (or the wrong url) we'll fetch later on. This way the |
| 201 // user doesn't see a flash of the default favicon. | 322 // user doesn't see a flash of the default favicon. |
| 202 entry->favicon().set_url(favicon.icon_url); | 323 entry->favicon().set_url(favicon.icon_url); |
| 203 if (favicon.is_valid()) | 324 if (favicon.is_valid()) |
| 204 UpdateFavicon(entry, favicon.image_data); | 325 UpdateFavicon(entry, favicon.image_data); |
| 205 entry->favicon().set_is_valid(true); | 326 entry->favicon().set_is_valid(true); |
| 206 } | 327 } |
| 207 | 328 |
| 208 if (favicon.known_icon && !favicon.expired) { | 329 if (favicon.known_icon && !favicon.expired) { |
| 209 if (got_favicon_url_ && entry->favicon().url() != favicon.icon_url) { | 330 if (current_candidate() && !do_url_and_icon_match(*current_candidate(), |
| 210 // Mapping in the database is wrong. DownloadFaviconOrAskHistory will | 331 favicon.icon_url, favicon.icon_type)) { |
| 332 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will | |
| 211 // update the mapping for this url and download the favicon if we don't | 333 // update the mapping for this url and download the favicon if we don't |
| 212 // already have it. | 334 // already have it. |
| 213 DownloadFaviconOrAskHistory(entry); | 335 DownloadFaviconOrAskHistory(entry->url(), current_candidate()->icon_url, |
| 336 static_cast<history::IconType>(current_candidate()->icon_type)); | |
| 214 } | 337 } |
| 215 } else if (got_favicon_url_) { | 338 } else if (current_candidate()) { |
| 216 // We know the official url for the favicon, by either don't have the | 339 // We know the official url for the favicon, by either don't have the |
| 217 // favicon or its expired. Continue on to DownloadFaviconOrAskHistory to | 340 // favicon or its expired. Continue on to DownloadFaviconOrAskHistory to |
| 218 // either download or check history again. | 341 // either download or check history again. |
| 219 DownloadFaviconOrAskHistory(entry); | 342 DownloadFaviconOrAskHistory(entry->url(), current_candidate()->icon_url, |
| 343 ToHistoryIconType(current_candidate()->icon_type)); | |
| 220 } | 344 } |
| 221 // else we haven't got the icon url. When we get it we'll ask the | 345 // else we haven't got the icon url. When we get it we'll ask the |
| 222 // renderer to download the icon. | 346 // renderer to download the icon. |
| 223 } | 347 } |
| 224 | 348 |
| 225 void FaviconHelper::DownloadFaviconOrAskHistory(NavigationEntry* entry) { | 349 void FaviconHelper::DownloadFaviconOrAskHistory(const GURL& page_url, |
| 226 DCHECK(entry); // We should only get here if entry is valid. | 350 const GURL& icon_url, |
| 351 history::IconType icon_type) { | |
| 227 if (favicon_expired_) { | 352 if (favicon_expired_) { |
| 228 // We have the mapping, but the favicon is out of date. Download it now. | 353 // We have the mapping, but the favicon is out of date. Download it now. |
| 229 ScheduleDownload(entry->url(), entry->favicon().url(), kFaviconSize, NULL); | 354 ScheduleDownload(page_url, icon_url, preferred_icon_size(), icon_type, |
| 355 NULL); | |
| 230 } else if (GetFaviconService()) { | 356 } else if (GetFaviconService()) { |
| 231 // We don't know the favicon, but we may have previously downloaded the | 357 // We don't know the favicon, but we may have previously downloaded the |
| 232 // favicon for another page that shares the same favicon. Ask for the | 358 // favicon for another page that shares the same favicon. Ask for the |
| 233 // favicon given the favicon URL. | 359 // favicon given the favicon URL. |
| 234 if (profile()->IsOffTheRecord()) { | 360 if (profile()->IsOffTheRecord()) { |
| 235 GetFaviconService()->GetFavicon( | 361 GetFavicon(icon_url, icon_type, &cancelable_consumer_, |
| 236 entry->favicon().url(), | |
| 237 history::FAVICON, | |
| 238 &cancelable_consumer_, | |
| 239 NewCallback(this, &FaviconHelper::OnFaviconData)); | 362 NewCallback(this, &FaviconHelper::OnFaviconData)); |
| 240 } else { | 363 } else { |
| 241 // Ask the history service for the icon. This does two things: | 364 // Ask the history service for the icon. This does two things: |
| 242 // 1. Attempts to fetch the favicon data from the database. | 365 // 1. Attempts to fetch the favicon data from the database. |
| 243 // 2. If the favicon exists in the database, this updates the database to | 366 // 2. If the favicon exists in the database, this updates the database to |
| 244 // include the mapping between the page url and the favicon url. | 367 // include the mapping between the page url and the favicon url. |
| 245 // This is asynchronous. The history service will call back when done. | 368 // This is asynchronous. The history service will call back when done. |
| 246 // Issue the request and associate the current page ID with it. | 369 // Issue the request and associate the current page ID with it. |
| 247 GetFaviconService()->UpdateFaviconMappingAndFetch( | 370 UpdateFaviconMappingAndFetch(page_url, icon_url, icon_type, |
| 248 entry->url(), | |
| 249 entry->favicon().url(), | |
| 250 history::FAVICON, | |
| 251 &cancelable_consumer_, | 371 &cancelable_consumer_, |
| 252 NewCallback(this, &FaviconHelper::OnFaviconData)); | 372 NewCallback(this, &FaviconHelper::OnFaviconData)); |
| 253 } | 373 } |
| 254 } | 374 } |
| 255 } | 375 } |
| 256 | 376 |
| 257 void FaviconHelper::OnFaviconData( | 377 void FaviconHelper::OnFaviconData(FaviconService::Handle handle, |
| 258 FaviconService::Handle handle, | 378 history::FaviconData favicon) { |
| 259 history::FaviconData favicon) { | 379 // We shouldn't be here if there is no candidate. |
| 380 DCHECK(current_candidate()); | |
| 260 NavigationEntry* entry = GetEntry(); | 381 NavigationEntry* entry = GetEntry(); |
| 261 if (!entry) | 382 if (!entry) |
| 262 return; | 383 return; |
| 263 | 384 |
| 264 // No need to update the favicon url. By the time we get here | 385 // No need to update the favicon url. By the time we get here |
| 265 // UpdateFaviconURL will have set the favicon url. | 386 // UpdateFaviconURL will have set the favicon url. |
| 266 | 387 if (favicon.is_valid() && favicon.icon_type == history::FAVICON) { |
| 267 if (favicon.is_valid()) { | |
| 268 // There is a favicon, set it now. If expired we'll download the current | 388 // There is a favicon, set it now. If expired we'll download the current |
| 269 // one again, but at least the user will get some icon instead of the | 389 // one again, but at least the user will get some icon instead of the |
| 270 // default and most likely the current one is fine anyway. | 390 // default and most likely the current one is fine anyway. |
| 271 UpdateFavicon(entry, favicon.image_data); | 391 UpdateFavicon(entry, favicon.image_data); |
| 272 } | 392 } |
| 273 | 393 |
| 274 if (!favicon.known_icon || favicon.expired) { | 394 if (!favicon.known_icon || favicon.expired || |
| 275 // We don't know the favicon, or it is out of date. Request the current one. | 395 !do_url_and_icon_match(*current_candidate(), favicon.icon_url, |
| 276 ScheduleDownload(entry->url(), entry->favicon().url(), kFaviconSize, NULL); | 396 favicon.icon_type)) { |
| 397 // We don't know the favicon, it is out of date or its type is not same as | |
| 398 // one got from page. Request the current one. | |
| 399 ScheduleDownload(entry->url(), current_candidate()->icon_url, | |
| 400 preferred_icon_size(), | |
| 401 ToHistoryIconType(current_candidate()->icon_type), NULL); | |
| 277 } | 402 } |
| 403 history_icon_ = favicon; | |
| 278 } | 404 } |
| 279 | 405 |
| 280 int FaviconHelper::ScheduleDownload(const GURL& url, | 406 int FaviconHelper::ScheduleDownload(const GURL& url, |
| 281 const GURL& image_url, | 407 const GURL& image_url, |
| 282 int image_size, | 408 int image_size, |
| 409 history::IconType icon_type, | |
| 283 ImageDownloadCallback* callback) { | 410 ImageDownloadCallback* callback) { |
| 284 const int download_id = tab_contents()->render_view_host()->DownloadFavicon( | 411 const int download_id = DownloadFavicon(image_url, image_size); |
| 285 image_url, image_size); | |
| 286 | |
| 287 if (download_id) { | 412 if (download_id) { |
| 288 // Download ids should be unique. | 413 // Download ids should be unique. |
| 289 DCHECK(download_requests_.find(download_id) == download_requests_.end()); | 414 DCHECK(download_requests_.find(download_id) == download_requests_.end()); |
| 290 download_requests_[download_id] = DownloadRequest(url, image_url, callback); | 415 download_requests_[download_id] = |
| 416 DownloadRequest(url, image_url, callback, icon_type); | |
| 291 } | 417 } |
| 292 | 418 |
| 293 return download_id; | 419 return download_id; |
| 294 } | 420 } |
| 295 | 421 |
| 296 SkBitmap FaviconHelper::ConvertToFaviconSize(const SkBitmap& image) { | 422 SkBitmap FaviconHelper::ConvertToFaviconSize(const SkBitmap& image) { |
| 297 int width = image.width(); | 423 int width = image.width(); |
| 298 int height = image.height(); | 424 int height = image.height(); |
| 299 if (width > 0 && height > 0) { | 425 if (width > 0 && height > 0) { |
| 300 calc_favicon_target_size(&width, &height); | 426 calc_favicon_target_size(&width, &height); |
| 301 return skia::ImageOperations::Resize( | 427 return skia::ImageOperations::Resize( |
| 302 image, skia::ImageOperations::RESIZE_LANCZOS3, | 428 image, skia::ImageOperations::RESIZE_LANCZOS3, |
| 303 width, height); | 429 width, height); |
| 304 } | 430 } |
| 305 return image; | 431 return image; |
| 306 } | 432 } |
| 307 | |
| 308 bool FaviconHelper::ShouldSaveFavicon(const GURL& url) { | |
| 309 if (!profile()->IsOffTheRecord()) | |
| 310 return true; | |
| 311 | |
| 312 // Otherwise store the favicon if the page is bookmarked. | |
| 313 BookmarkModel* bookmark_model = profile()->GetBookmarkModel(); | |
| 314 return bookmark_model && bookmark_model->IsBookmarked(url); | |
| 315 } | |
| OLD | NEW |