| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/fav_icon_helper.h" | 5 #include "chrome/browser/fav_icon_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 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 cancelable_consumer_.CancelAllRequests(); | 45 cancelable_consumer_.CancelAllRequests(); |
| 46 | 46 |
| 47 url_ = url; | 47 url_ = url; |
| 48 | 48 |
| 49 fav_icon_expired_ = got_fav_icon_from_history_ = got_fav_icon_url_ = false; | 49 fav_icon_expired_ = got_fav_icon_from_history_ = got_fav_icon_url_ = false; |
| 50 | 50 |
| 51 // Request the favicon from the history service. In parallel to this the | 51 // 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 | 52 // renderer is going to notify us (well TabContents) when the favicon url is |
| 53 // available. | 53 // available. |
| 54 if (GetFaviconService()) { | 54 if (GetFaviconService()) { |
| 55 GetFaviconService()->GetFaviconForURL(url_, &cancelable_consumer_, | 55 GetFaviconService()->GetFaviconForURL( |
| 56 url_, |
| 57 history::FAV_ICON, |
| 58 &cancelable_consumer_, |
| 56 NewCallback(this, &FavIconHelper::OnFavIconDataForInitialURL)); | 59 NewCallback(this, &FavIconHelper::OnFavIconDataForInitialURL)); |
| 57 } | 60 } |
| 58 } | 61 } |
| 59 | 62 |
| 60 int FavIconHelper::DownloadImage(const GURL& image_url, | 63 int FavIconHelper::DownloadImage(const GURL& image_url, |
| 61 int image_size, | 64 int image_size, |
| 62 ImageDownloadCallback* callback) { | 65 ImageDownloadCallback* callback) { |
| 63 DCHECK(callback); // Must provide a callback. | 66 DCHECK(callback); // Must provide a callback. |
| 64 return ScheduleDownload(GURL(), image_url, image_size, callback); | 67 return ScheduleDownload(GURL(), image_url, image_size, callback); |
| 65 } | 68 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 76 const GURL& url, | 79 const GURL& url, |
| 77 const GURL& image_url, | 80 const GURL& image_url, |
| 78 const SkBitmap& image) { | 81 const SkBitmap& image) { |
| 79 const SkBitmap& sized_image = | 82 const SkBitmap& sized_image = |
| 80 (image.width() == kFavIconSize && image.height() == kFavIconSize) | 83 (image.width() == kFavIconSize && image.height() == kFavIconSize) |
| 81 ? image : ConvertToFavIconSize(image); | 84 ? image : ConvertToFavIconSize(image); |
| 82 | 85 |
| 83 if (GetFaviconService() && ShouldSaveFavicon(url)) { | 86 if (GetFaviconService() && ShouldSaveFavicon(url)) { |
| 84 std::vector<unsigned char> image_data; | 87 std::vector<unsigned char> image_data; |
| 85 gfx::PNGCodec::EncodeBGRASkBitmap(sized_image, false, &image_data); | 88 gfx::PNGCodec::EncodeBGRASkBitmap(sized_image, false, &image_data); |
| 86 GetFaviconService()->SetFavicon(url, image_url, image_data); | 89 GetFaviconService()->SetFavicon( |
| 90 url, image_url, image_data, history::FAV_ICON); |
| 87 } | 91 } |
| 88 | 92 |
| 89 if (url == url_) { | 93 if (url == url_) { |
| 90 NavigationEntry* entry = GetEntry(); | 94 NavigationEntry* entry = GetEntry(); |
| 91 if (entry) | 95 if (entry) |
| 92 UpdateFavIcon(entry, sized_image); | 96 UpdateFavIcon(entry, sized_image); |
| 93 } | 97 } |
| 94 } | 98 } |
| 95 | 99 |
| 96 void FavIconHelper::UpdateFavIcon(NavigationEntry* entry, | 100 void FavIconHelper::UpdateFavIcon(NavigationEntry* entry, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 // If the URL has changed out from under us (as will happen with redirects) | 182 // If the URL has changed out from under us (as will happen with redirects) |
| 179 // return NULL. | 183 // return NULL. |
| 180 return NULL; | 184 return NULL; |
| 181 } | 185 } |
| 182 | 186 |
| 183 void FavIconHelper::OnFavIconDataForInitialURL( | 187 void FavIconHelper::OnFavIconDataForInitialURL( |
| 184 FaviconService::Handle handle, | 188 FaviconService::Handle handle, |
| 185 bool know_favicon, | 189 bool know_favicon, |
| 186 scoped_refptr<RefCountedMemory> data, | 190 scoped_refptr<RefCountedMemory> data, |
| 187 bool expired, | 191 bool expired, |
| 188 GURL icon_url) { | 192 GURL icon_url, |
| 193 history::IconType) { |
| 189 NavigationEntry* entry = GetEntry(); | 194 NavigationEntry* entry = GetEntry(); |
| 190 if (!entry) | 195 if (!entry) |
| 191 return; | 196 return; |
| 192 | 197 |
| 193 got_fav_icon_from_history_ = true; | 198 got_fav_icon_from_history_ = true; |
| 194 | 199 |
| 195 fav_icon_expired_ = (know_favicon && expired); | 200 fav_icon_expired_ = (know_favicon && expired); |
| 196 | 201 |
| 197 if (know_favicon && !entry->favicon().is_valid() && | 202 if (know_favicon && !entry->favicon().is_valid() && |
| 198 (!got_fav_icon_url_ || entry->favicon().url() == icon_url)) { | 203 (!got_fav_icon_url_ || entry->favicon().url() == icon_url)) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 228 if (fav_icon_expired_) { | 233 if (fav_icon_expired_) { |
| 229 // We have the mapping, but the favicon is out of date. Download it now. | 234 // We have the mapping, but the favicon is out of date. Download it now. |
| 230 ScheduleDownload(entry->url(), entry->favicon().url(), kFavIconSize, NULL); | 235 ScheduleDownload(entry->url(), entry->favicon().url(), kFavIconSize, NULL); |
| 231 } else if (GetFaviconService()) { | 236 } else if (GetFaviconService()) { |
| 232 // We don't know the favicon, but we may have previously downloaded the | 237 // We don't know the favicon, but we may have previously downloaded the |
| 233 // favicon for another page that shares the same favicon. Ask for the | 238 // favicon for another page that shares the same favicon. Ask for the |
| 234 // favicon given the favicon URL. | 239 // favicon given the favicon URL. |
| 235 if (profile()->IsOffTheRecord()) { | 240 if (profile()->IsOffTheRecord()) { |
| 236 GetFaviconService()->GetFavicon( | 241 GetFaviconService()->GetFavicon( |
| 237 entry->favicon().url(), | 242 entry->favicon().url(), |
| 243 history::FAV_ICON, |
| 238 &cancelable_consumer_, | 244 &cancelable_consumer_, |
| 239 NewCallback(this, &FavIconHelper::OnFavIconData)); | 245 NewCallback(this, &FavIconHelper::OnFavIconData)); |
| 240 } else { | 246 } else { |
| 241 // Ask the history service for the icon. This does two things: | 247 // Ask the history service for the icon. This does two things: |
| 242 // 1. Attempts to fetch the favicon data from the database. | 248 // 1. Attempts to fetch the favicon data from the database. |
| 243 // 2. If the favicon exists in the database, this updates the database to | 249 // 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. | 250 // include the mapping between the page url and the favicon url. |
| 245 // This is asynchronous. The history service will call back when done. | 251 // This is asynchronous. The history service will call back when done. |
| 246 // Issue the request and associate the current page ID with it. | 252 // Issue the request and associate the current page ID with it. |
| 247 GetFaviconService()->UpdateFaviconMappingAndFetch( | 253 GetFaviconService()->UpdateFaviconMappingAndFetch( |
| 248 entry->url(), | 254 entry->url(), |
| 249 entry->favicon().url(), &cancelable_consumer_, | 255 entry->favicon().url(), |
| 256 history::FAV_ICON, |
| 257 &cancelable_consumer_, |
| 250 NewCallback(this, &FavIconHelper::OnFavIconData)); | 258 NewCallback(this, &FavIconHelper::OnFavIconData)); |
| 251 } | 259 } |
| 252 } | 260 } |
| 253 } | 261 } |
| 254 | 262 |
| 255 void FavIconHelper::OnFavIconData( | 263 void FavIconHelper::OnFavIconData( |
| 256 FaviconService::Handle handle, | 264 FaviconService::Handle handle, |
| 257 bool know_favicon, | 265 bool know_favicon, |
| 258 scoped_refptr<RefCountedMemory> data, | 266 scoped_refptr<RefCountedMemory> data, |
| 259 bool expired, | 267 bool expired, |
| 260 GURL icon_url) { | 268 GURL icon_url, |
| 269 history::IconType) { |
| 261 NavigationEntry* entry = GetEntry(); | 270 NavigationEntry* entry = GetEntry(); |
| 262 if (!entry) | 271 if (!entry) |
| 263 return; | 272 return; |
| 264 | 273 |
| 265 // No need to update the favicon url. By the time we get here | 274 // No need to update the favicon url. By the time we get here |
| 266 // UpdateFavIconURL will have set the favicon url. | 275 // UpdateFavIconURL will have set the favicon url. |
| 267 | 276 |
| 268 if (know_favicon && data.get() && data->size()) { | 277 if (know_favicon && data.get() && data->size()) { |
| 269 // There is a favicon, set it now. If expired we'll download the current | 278 // There is a favicon, set it now. If expired we'll download the current |
| 270 // one again, but at least the user will get some icon instead of the | 279 // one again, but at least the user will get some icon instead of the |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 } | 316 } |
| 308 | 317 |
| 309 bool FavIconHelper::ShouldSaveFavicon(const GURL& url) { | 318 bool FavIconHelper::ShouldSaveFavicon(const GURL& url) { |
| 310 if (!profile()->IsOffTheRecord()) | 319 if (!profile()->IsOffTheRecord()) |
| 311 return true; | 320 return true; |
| 312 | 321 |
| 313 // Otherwise store the favicon if the page is bookmarked. | 322 // Otherwise store the favicon if the page is bookmarked. |
| 314 BookmarkModel* bookmark_model = profile()->GetBookmarkModel(); | 323 BookmarkModel* bookmark_model = profile()->GetBookmarkModel(); |
| 315 return bookmark_model && bookmark_model->IsBookmarked(url); | 324 return bookmark_model && bookmark_model->IsBookmarked(url); |
| 316 } | 325 } |
| OLD | NEW |