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