OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/favicon_handler.h" | 5 #include "chrome/browser/favicon/favicon_handler.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 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 | 44 |
45 bool DoUrlAndIconMatch(const FaviconURL& favicon_url, | 45 bool DoUrlAndIconMatch(const FaviconURL& favicon_url, |
46 const GURL& url, | 46 const GURL& url, |
47 history::IconType icon_type) { | 47 history::IconType icon_type) { |
48 return favicon_url.icon_url == url && | 48 return favicon_url.icon_url == url && |
49 favicon_url.icon_type == static_cast<FaviconURL::IconType>(icon_type); | 49 favicon_url.icon_type == static_cast<FaviconURL::IconType>(icon_type); |
50 } | 50 } |
51 | 51 |
52 } // namespace | 52 } // namespace |
53 | 53 |
54 //////////////////////////////////////////////////////////////////////////////// | |
55 | |
54 FaviconHandler::DownloadRequest::DownloadRequest() | 56 FaviconHandler::DownloadRequest::DownloadRequest() |
55 : icon_type(history::INVALID_ICON) { | 57 : icon_type(history::INVALID_ICON) { |
56 } | 58 } |
57 | 59 |
58 FaviconHandler::DownloadRequest::~DownloadRequest() { | 60 FaviconHandler::DownloadRequest::~DownloadRequest() { |
59 } | 61 } |
60 | 62 |
61 FaviconHandler::DownloadRequest::DownloadRequest( | 63 FaviconHandler::DownloadRequest::DownloadRequest( |
62 const GURL& url, | 64 const GURL& url, |
63 const GURL& image_url, | 65 const GURL& image_url, |
64 const FaviconTabHelper::ImageDownloadCallback& callback, | 66 const FaviconTabHelper::ImageDownloadCallback& callback, |
65 history::IconType icon_type) | 67 history::IconType icon_type) |
66 : url(url), | 68 : url(url), |
67 image_url(image_url), | 69 image_url(image_url), |
68 callback(callback), | 70 callback(callback), |
69 icon_type(icon_type) { | 71 icon_type(icon_type) { |
70 } | 72 } |
71 | 73 |
74 //////////////////////////////////////////////////////////////////////////////// | |
75 | |
76 FaviconHandler::FaviconCandidate::FaviconCandidate() | |
77 : icon_type(history::INVALID_ICON) { | |
78 } | |
79 | |
80 FaviconHandler::FaviconCandidate::~FaviconCandidate() { | |
81 } | |
82 | |
83 FaviconHandler::FaviconCandidate::FaviconCandidate( | |
84 const GURL& url, | |
85 const GURL& image_url, | |
86 const gfx::Image& image, | |
87 const SkBitmap& bitmap, | |
88 history::IconType icon_type) | |
89 : url(url), | |
90 image_url(image_url), | |
91 image(image), | |
92 bitmap(bitmap), | |
93 icon_type(icon_type) { | |
94 } | |
95 | |
96 //////////////////////////////////////////////////////////////////////////////// | |
97 | |
72 FaviconHandler::FaviconHandler(Profile* profile, | 98 FaviconHandler::FaviconHandler(Profile* profile, |
73 FaviconHandlerDelegate* delegate, | 99 FaviconHandlerDelegate* delegate, |
74 Type icon_type) | 100 Type icon_type) |
75 : got_favicon_from_history_(false), | 101 : got_favicon_from_history_(false), |
76 favicon_expired_(false), | 102 favicon_expired_(false), |
77 icon_types_(icon_type == FAVICON ? history::FAVICON : | 103 icon_types_(icon_type == FAVICON ? history::FAVICON : |
78 history::TOUCH_ICON | history::TOUCH_PRECOMPOSED_ICON), | 104 history::TOUCH_ICON | history::TOUCH_PRECOMPOSED_ICON), |
79 current_url_index_(0), | |
80 profile_(profile), | 105 profile_(profile), |
81 delegate_(delegate) { | 106 delegate_(delegate) { |
82 DCHECK(profile_); | 107 DCHECK(profile_); |
83 DCHECK(delegate_); | 108 DCHECK(delegate_); |
84 } | 109 } |
85 | 110 |
86 FaviconHandler::~FaviconHandler() { | 111 FaviconHandler::~FaviconHandler() { |
87 // Call pending download callbacks with error to allow caller to clean up. | 112 // Call pending download callbacks with error to allow caller to clean up. |
88 for (DownloadRequests::iterator i = download_requests_.begin(); | 113 for (DownloadRequests::iterator i = download_requests_.begin(); |
89 i != download_requests_.end(); ++i) { | 114 i != download_requests_.end(); ++i) { |
90 if (!i->second.callback.is_null()) { | 115 if (!i->second.callback.is_null()) { |
91 i->second.callback.Run(i->first, true, SkBitmap()); | 116 i->second.callback.Run(i->first, true, SkBitmap()); |
92 } | 117 } |
93 } | 118 } |
94 } | 119 } |
95 | 120 |
96 void FaviconHandler::FetchFavicon(const GURL& url) { | 121 void FaviconHandler::FetchFavicon(const GURL& url) { |
97 cancelable_consumer_.CancelAllRequests(); | 122 cancelable_consumer_.CancelAllRequests(); |
98 | 123 |
99 url_ = url; | 124 url_ = url; |
100 | 125 |
101 favicon_expired_ = got_favicon_from_history_ = false; | 126 favicon_expired_ = got_favicon_from_history_ = false; |
102 current_url_index_ = 0; | 127 favicon_candidate_.icon_type = history::INVALID_ICON; |
sky
2012/03/16 23:29:15
For safety reset favicon_candidate_ entirely here,
stevenjb
2012/03/17 00:23:07
Done.
| |
103 urls_.clear(); | |
104 | 128 |
105 // Request the favicon from the history service. In parallel to this the | 129 // Request the favicon from the history service. In parallel to this the |
106 // renderer is going to notify us (well TabContents) when the favicon url is | 130 // renderer is going to notify us (well TabContents) when the favicon url is |
107 // available. | 131 // available. |
108 if (GetFaviconService()) { | 132 if (GetFaviconService()) { |
109 GetFaviconForURL(url_, icon_types_, &cancelable_consumer_, | 133 GetFaviconForURL(url_, icon_types_, &cancelable_consumer_, |
110 base::Bind(&FaviconHandler::OnFaviconDataForInitialURL, | 134 base::Bind(&FaviconHandler::OnFaviconDataForInitialURL, |
111 base::Unretained(this))); | 135 base::Unretained(this))); |
112 } | 136 } |
113 } | 137 } |
114 | 138 |
115 int FaviconHandler::DownloadImage( | 139 int FaviconHandler::DownloadImage( |
116 const GURL& image_url, | 140 const GURL& image_url, |
117 int image_size, | 141 int image_size, |
118 history::IconType icon_type, | 142 history::IconType icon_type, |
119 const FaviconTabHelper::ImageDownloadCallback& callback) { | 143 const FaviconTabHelper::ImageDownloadCallback& callback) { |
120 return ScheduleDownload(GURL(), image_url, image_size, icon_type, callback); | 144 return ScheduleDownload(GURL(), image_url, image_size, icon_type, callback); |
121 } | 145 } |
122 | 146 |
123 FaviconService* FaviconHandler::GetFaviconService() { | 147 FaviconService* FaviconHandler::GetFaviconService() { |
124 return profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); | 148 return profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); |
125 } | 149 } |
126 | 150 |
151 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url, | |
152 const GURL& image_url, | |
153 const gfx::Image& image, | |
154 history::IconType icon_type) { | |
155 bool update_candidate = false; | |
156 bool exact_match = false; | |
157 SkBitmap bitmap = *(image.ToSkBitmap()); | |
158 int preferred_size = preferred_icon_size(); | |
sky
2012/03/16 23:29:15
nit: no need to assign this, use preferred_icon_si
stevenjb
2012/03/17 00:23:07
Done.
| |
159 if (preferred_size == 0) { | |
160 update_candidate = true; | |
161 exact_match = true; | |
162 } else if (favicon_candidate_.icon_type == history::INVALID_ICON) { | |
163 // No current candidate, use this. | |
164 update_candidate = true; | |
165 } else { | |
166 int bitmap_size = std::max(bitmap.width(), bitmap.height()); | |
167 if (bitmap_size == preferred_size) { | |
168 // Exact match, use this. | |
169 update_candidate = true; | |
170 exact_match = true; | |
171 } else { | |
172 // Compare against current candidate. | |
173 int cur_size = std::max(favicon_candidate_.bitmap.width(), | |
174 favicon_candidate_.bitmap.height()); | |
175 if ((bitmap_size >= preferred_size && bitmap_size < cur_size) || | |
176 (cur_size < preferred_size && bitmap_size > cur_size)) { | |
177 update_candidate = true; | |
178 } | |
179 } | |
180 } | |
181 if (update_candidate) { | |
182 favicon_candidate_ = FaviconCandidate( | |
183 url, image_url, image, bitmap, icon_type); | |
184 } | |
185 return exact_match; | |
186 } | |
187 | |
127 void FaviconHandler::SetFavicon( | 188 void FaviconHandler::SetFavicon( |
128 const GURL& url, | 189 const GURL& url, |
129 const GURL& image_url, | 190 const GURL& image_url, |
130 const gfx::Image& image, | 191 const gfx::Image& image, |
192 const SkBitmap& bitmap, | |
131 history::IconType icon_type) { | 193 history::IconType icon_type) { |
132 const SkBitmap& bitmap = image; | |
133 const gfx::Image& sized_image = (preferred_icon_size() == 0 || | 194 const gfx::Image& sized_image = (preferred_icon_size() == 0 || |
134 (preferred_icon_size() == bitmap.width() && | 195 (preferred_icon_size() == bitmap.width() && |
135 preferred_icon_size() == bitmap.height())) ? | 196 preferred_icon_size() == bitmap.height())) ? |
136 image : ResizeFaviconIfNeeded(image); | 197 image : ResizeFaviconIfNeeded(image); |
137 | 198 |
138 if (GetFaviconService() && ShouldSaveFavicon(url)) { | 199 if (GetFaviconService() && ShouldSaveFavicon(url)) { |
139 std::vector<unsigned char> image_data; | 200 std::vector<unsigned char> image_data; |
140 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) | 201 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) |
141 SetHistoryFavicon(url, image_url, image_data, icon_type); | 202 SetHistoryFavicon(url, image_url, image_data, icon_type); |
142 } | 203 } |
143 | 204 |
144 if (url == url_ && icon_type == history::FAVICON) { | 205 if (url == url_ && icon_type == history::FAVICON) { |
145 NavigationEntry* entry = GetEntry(); | 206 NavigationEntry* entry = GetEntry(); |
146 if (entry) | 207 if (entry) { |
208 entry->GetFavicon().url = image_url; | |
147 UpdateFavicon(entry, &sized_image); | 209 UpdateFavicon(entry, &sized_image); |
210 } | |
148 } | 211 } |
149 } | 212 } |
150 | 213 |
151 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, | 214 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, |
152 scoped_refptr<RefCountedMemory> data) { | 215 scoped_refptr<RefCountedMemory> data) { |
153 scoped_ptr<gfx::Image> image(gfx::ImageFromPNGEncodedData(data->front(), | 216 scoped_ptr<gfx::Image> image(gfx::ImageFromPNGEncodedData(data->front(), |
154 data->size())); | 217 data->size())); |
155 UpdateFavicon(entry, image.get()); | 218 UpdateFavicon(entry, image.get()); |
156 } | 219 } |
157 | 220 |
158 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, | 221 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, |
159 const gfx::Image* image) { | 222 const gfx::Image* image) { |
160 // No matter what happens, we need to mark the favicon as being set. | 223 // No matter what happens, we need to mark the favicon as being set. |
161 entry->GetFavicon().valid = true; | 224 entry->GetFavicon().valid = true; |
162 | 225 |
163 if (!image) | 226 if (!image) |
164 return; | 227 return; |
165 | 228 |
166 entry->GetFavicon().bitmap = *image; | 229 entry->GetFavicon().bitmap = *image; |
167 delegate_->NotifyFaviconUpdated(); | 230 delegate_->NotifyFaviconUpdated(); |
168 } | 231 } |
169 | 232 |
170 void FaviconHandler::OnUpdateFaviconURL( | 233 void FaviconHandler::OnUpdateFaviconURL( |
171 int32 page_id, | 234 int32 page_id, |
172 const std::vector<FaviconURL>& candidates) { | 235 const std::vector<FaviconURL>& candidates) { |
173 NavigationEntry* entry = GetEntry(); | |
174 if (!entry) | |
175 return; | |
176 | 236 |
177 bool got_favicon_url_update = false; | 237 // Clear any curently pending download requests since we have an updated |
238 // list of candidates. | |
239 download_requests_.clear(); | |
240 image_urls_.clear(); | |
178 for (std::vector<FaviconURL>::const_iterator i = candidates.begin(); | 241 for (std::vector<FaviconURL>::const_iterator i = candidates.begin(); |
179 i != candidates.end(); ++i) { | 242 i != candidates.end(); ++i) { |
180 if (!i->icon_url.is_empty() && (i->icon_type & icon_types_)) { | 243 if (!i->icon_url.is_empty() && (i->icon_type & icon_types_)) |
181 if (!got_favicon_url_update) { | 244 image_urls_.push_back(*i); |
182 got_favicon_url_update = true; | |
183 urls_.clear(); | |
184 current_url_index_ = 0; | |
185 } | |
186 urls_.push_back(*i); | |
187 } | |
188 } | 245 } |
189 | 246 |
190 // TODO(davemoore) Should clear on empty url. Currently we ignore it. | 247 // TODO(davemoore) Should clear on empty url. Currently we ignore it. |
191 // This appears to be what FF does as well. | 248 // This appears to be what FF does as well. |
192 // No URL was added. | 249 if (image_urls_.empty()) |
193 if (!got_favicon_url_update) | |
194 return; | 250 return; |
195 | 251 |
196 if (!GetFaviconService()) | 252 if (!GetFaviconService()) |
197 return; | 253 return; |
198 | 254 |
255 ProcessCurrentUrl(); | |
256 } | |
257 | |
258 void FaviconHandler::ProcessCurrentUrl() { | |
259 DCHECK(!image_urls_.empty()); | |
260 | |
261 NavigationEntry* entry = GetEntry(); | |
262 if (!entry) | |
263 return; | |
264 | |
199 // For FAVICON. | 265 // For FAVICON. |
200 if (current_candidate()->icon_type == FaviconURL::FAVICON) { | 266 if (current_candidate()->icon_type == FaviconURL::FAVICON) { |
201 if (!favicon_expired_ && entry->GetFavicon().valid && | 267 if (!favicon_expired_ && entry->GetFavicon().valid && |
202 DoUrlAndIconMatch(*current_candidate(), entry->GetFavicon().url, | 268 DoUrlAndIconMatch(*current_candidate(), entry->GetFavicon().url, |
203 history::FAVICON)) | 269 history::FAVICON)) |
204 return; | 270 return; |
205 | 271 |
206 entry->GetFavicon().url = current_candidate()->icon_url; | 272 entry->GetFavicon().url = current_candidate()->icon_url; |
207 } else if (!favicon_expired_ && got_favicon_from_history_ && | 273 } else if (!favicon_expired_ && got_favicon_from_history_ && |
208 history_icon_.is_valid() && | 274 history_icon_.is_valid() && |
(...skipping 19 matching lines...) Expand all Loading... | |
228 return; | 294 return; |
229 } | 295 } |
230 | 296 |
231 if (!i->second.callback.is_null()) { | 297 if (!i->second.callback.is_null()) { |
232 i->second.callback.Run(id, errored, *(&image)); | 298 i->second.callback.Run(id, errored, *(&image)); |
233 } else if (current_candidate() && | 299 } else if (current_candidate() && |
234 DoUrlAndIconMatch(*current_candidate(), image_url, | 300 DoUrlAndIconMatch(*current_candidate(), image_url, |
235 i->second.icon_type)) { | 301 i->second.icon_type)) { |
236 // The downloaded icon is still valid when there is no FaviconURL update | 302 // The downloaded icon is still valid when there is no FaviconURL update |
237 // during the downloading. | 303 // during the downloading. |
304 bool request_next_icon = true; | |
238 if (!errored) { | 305 if (!errored) { |
239 SetFavicon(i->second.url, image_url, image, i->second.icon_type); | 306 bool exact_match = UpdateFaviconCandidate( |
sky
2012/03/16 23:29:15
nit: make this request_next_icon = !UpdateF...
stevenjb
2012/03/17 00:23:07
Done.
| |
240 } else if (GetEntry() && ++current_url_index_ < urls_.size()) { | 307 i->second.url, image_url, image, i->second.icon_type); |
241 // Copies all candidate except first one and notifies the FaviconHandler, | 308 if (exact_match) |
242 // so the next candidate can be processed. | 309 request_next_icon = false; |
243 std::vector<FaviconURL> new_candidates(urls_.begin() + 1, urls_.end()); | 310 } |
244 OnUpdateFaviconURL(0, new_candidates); | 311 if (request_next_icon && GetEntry() && image_urls_.size() > 1) { |
312 // Remove the first member of image_urls_ and process the remaining. | |
313 image_urls_.pop_front(); | |
314 ProcessCurrentUrl(); | |
315 } else if (favicon_candidate_.icon_type != history::INVALID_ICON) { | |
316 // No more icons to request, set the favicon from the candidate. | |
317 SetFavicon(favicon_candidate_.url, favicon_candidate_.image_url, | |
318 favicon_candidate_.image, favicon_candidate_.bitmap, | |
319 favicon_candidate_.icon_type); | |
320 // Reset candidate. | |
321 favicon_candidate_ = FaviconCandidate(); | |
245 } | 322 } |
246 } | 323 } |
247 download_requests_.erase(i); | 324 download_requests_.erase(i); |
248 } | 325 } |
249 | 326 |
250 NavigationEntry* FaviconHandler::GetEntry() { | 327 NavigationEntry* FaviconHandler::GetEntry() { |
251 NavigationEntry* entry = delegate_->GetActiveEntry(); | 328 NavigationEntry* entry = delegate_->GetActiveEntry(); |
252 if (entry && entry->GetURL() == url_) | 329 if (entry && entry->GetURL() == url_) |
253 return entry; | 330 return entry; |
254 | 331 |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
450 int height = bitmap.height(); | 527 int height = bitmap.height(); |
451 if (width > 0 && height > 0) { | 528 if (width > 0 && height > 0) { |
452 gfx::CalculateFaviconTargetSize(&width, &height); | 529 gfx::CalculateFaviconTargetSize(&width, &height); |
453 return gfx::Image(skia::ImageOperations::Resize( | 530 return gfx::Image(skia::ImageOperations::Resize( |
454 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | 531 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, |
455 width, height)); | 532 width, height)); |
456 } | 533 } |
457 | 534 |
458 return image; | 535 return image; |
459 } | 536 } |
OLD | NEW |