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