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