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