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