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