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/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 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
14 #include "chrome/browser/bookmarks/bookmark_model.h" | 14 #include "chrome/browser/bookmarks/bookmark_model.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/common/icon_messages.h" | 16 #include "chrome/common/icon_messages.h" |
17 #include "content/public/browser/favicon_status.h" | 17 #include "content/public/browser/favicon_status.h" |
18 #include "content/public/browser/navigation_entry.h" | 18 #include "content/public/browser/navigation_entry.h" |
19 #include "skia/ext/image_operations.h" | 19 #include "skia/ext/image_operations.h" |
20 #include "ui/gfx/codec/png_codec.h" | 20 #include "ui/gfx/codec/png_codec.h" |
21 #include "ui/gfx/image/image.h" | 21 #include "ui/gfx/image/image.h" |
22 #include "ui/gfx/image/image_util.h" | 22 #include "ui/gfx/image/image_util.h" |
23 | 23 |
| 24 using content::NavigationEntry; |
| 25 |
24 namespace { | 26 namespace { |
25 | 27 |
26 // Returns history::IconType the given icon_type corresponds to. | 28 // Returns history::IconType the given icon_type corresponds to. |
27 history::IconType ToHistoryIconType(FaviconURL::IconType icon_type) { | 29 history::IconType ToHistoryIconType(FaviconURL::IconType icon_type) { |
28 switch (icon_type) { | 30 switch (icon_type) { |
29 case FaviconURL::FAVICON: | 31 case FaviconURL::FAVICON: |
30 return history::FAVICON; | 32 return history::FAVICON; |
31 case FaviconURL::TOUCH_ICON: | 33 case FaviconURL::TOUCH_ICON: |
32 return history::TOUCH_ICON; | 34 return history::TOUCH_ICON; |
33 case FaviconURL::TOUCH_PRECOMPOSED_ICON: | 35 case FaviconURL::TOUCH_PRECOMPOSED_ICON: |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 preferred_icon_size() == bitmap.height())) ? | 135 preferred_icon_size() == bitmap.height())) ? |
134 image : ResizeFaviconIfNeeded(image); | 136 image : ResizeFaviconIfNeeded(image); |
135 | 137 |
136 if (GetFaviconService() && ShouldSaveFavicon(url)) { | 138 if (GetFaviconService() && ShouldSaveFavicon(url)) { |
137 std::vector<unsigned char> image_data; | 139 std::vector<unsigned char> image_data; |
138 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) | 140 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) |
139 SetHistoryFavicon(url, image_url, image_data, icon_type); | 141 SetHistoryFavicon(url, image_url, image_data, icon_type); |
140 } | 142 } |
141 | 143 |
142 if (url == url_ && icon_type == history::FAVICON) { | 144 if (url == url_ && icon_type == history::FAVICON) { |
143 content::NavigationEntry* entry = GetEntry(); | 145 NavigationEntry* entry = GetEntry(); |
144 if (entry) | 146 if (entry) |
145 UpdateFavicon(entry, &sized_image); | 147 UpdateFavicon(entry, &sized_image); |
146 } | 148 } |
147 } | 149 } |
148 | 150 |
149 void FaviconHandler::UpdateFavicon(content::NavigationEntry* entry, | 151 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, |
150 scoped_refptr<RefCountedMemory> data) { | 152 scoped_refptr<RefCountedMemory> data) { |
151 scoped_ptr<gfx::Image> image(gfx::ImageFromPNGEncodedData(data->front(), | 153 scoped_ptr<gfx::Image> image(gfx::ImageFromPNGEncodedData(data->front(), |
152 data->size())); | 154 data->size())); |
153 UpdateFavicon(entry, image.get()); | 155 UpdateFavicon(entry, image.get()); |
154 } | 156 } |
155 | 157 |
156 void FaviconHandler::UpdateFavicon(content::NavigationEntry* entry, | 158 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, |
157 const gfx::Image* image) { | 159 const gfx::Image* image) { |
158 // No matter what happens, we need to mark the favicon as being set. | 160 // No matter what happens, we need to mark the favicon as being set. |
159 entry->GetFavicon().valid = true; | 161 entry->GetFavicon().valid = true; |
160 | 162 |
161 if (!image) | 163 if (!image) |
162 return; | 164 return; |
163 | 165 |
164 entry->GetFavicon().bitmap = *image; | 166 entry->GetFavicon().bitmap = *image; |
165 delegate_->NotifyFaviconUpdated(); | 167 delegate_->NotifyFaviconUpdated(); |
166 } | 168 } |
167 | 169 |
168 void FaviconHandler::OnUpdateFaviconURL( | 170 void FaviconHandler::OnUpdateFaviconURL( |
169 int32 page_id, | 171 int32 page_id, |
170 const std::vector<FaviconURL>& candidates) { | 172 const std::vector<FaviconURL>& candidates) { |
171 content::NavigationEntry* entry = GetEntry(); | 173 NavigationEntry* entry = GetEntry(); |
172 if (!entry) | 174 if (!entry) |
173 return; | 175 return; |
174 | 176 |
175 bool got_favicon_url_update = false; | 177 bool got_favicon_url_update = false; |
176 for (std::vector<FaviconURL>::const_iterator i = candidates.begin(); | 178 for (std::vector<FaviconURL>::const_iterator i = candidates.begin(); |
177 i != candidates.end(); ++i) { | 179 i != candidates.end(); ++i) { |
178 if (!i->icon_url.is_empty() && (i->icon_type & icon_types_)) { | 180 if (!i->icon_url.is_empty() && (i->icon_type & icon_types_)) { |
179 if (!got_favicon_url_update) { | 181 if (!got_favicon_url_update) { |
180 got_favicon_url_update = true; | 182 got_favicon_url_update = true; |
181 urls_.clear(); | 183 urls_.clear(); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 } else if (GetEntry() && ++current_url_index_ < urls_.size()) { | 240 } else if (GetEntry() && ++current_url_index_ < urls_.size()) { |
239 // Copies all candidate except first one and notifies the FaviconHandler, | 241 // Copies all candidate except first one and notifies the FaviconHandler, |
240 // so the next candidate can be processed. | 242 // so the next candidate can be processed. |
241 std::vector<FaviconURL> new_candidates(++urls_.begin(), urls_.end()); | 243 std::vector<FaviconURL> new_candidates(++urls_.begin(), urls_.end()); |
242 OnUpdateFaviconURL(0, new_candidates); | 244 OnUpdateFaviconURL(0, new_candidates); |
243 } | 245 } |
244 } | 246 } |
245 download_requests_.erase(i); | 247 download_requests_.erase(i); |
246 } | 248 } |
247 | 249 |
248 content::NavigationEntry* FaviconHandler::GetEntry() { | 250 NavigationEntry* FaviconHandler::GetEntry() { |
249 content::NavigationEntry* entry = delegate_->GetActiveEntry(); | 251 NavigationEntry* entry = delegate_->GetActiveEntry(); |
250 if (entry && entry->GetURL() == url_) | 252 if (entry && entry->GetURL() == url_) |
251 return entry; | 253 return entry; |
252 | 254 |
253 // If the URL has changed out from under us (as will happen with redirects) | 255 // If the URL has changed out from under us (as will happen with redirects) |
254 // return NULL. | 256 // return NULL. |
255 return NULL; | 257 return NULL; |
256 } | 258 } |
257 | 259 |
258 int FaviconHandler::DownloadFavicon(const GURL& image_url, int image_size) { | 260 int FaviconHandler::DownloadFavicon(const GURL& image_url, int image_size) { |
259 if (!image_url.is_valid()) { | 261 if (!image_url.is_valid()) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 return true; | 308 return true; |
307 | 309 |
308 // Otherwise store the favicon if the page is bookmarked. | 310 // Otherwise store the favicon if the page is bookmarked. |
309 BookmarkModel* bookmark_model = profile_->GetBookmarkModel(); | 311 BookmarkModel* bookmark_model = profile_->GetBookmarkModel(); |
310 return bookmark_model && bookmark_model->IsBookmarked(url); | 312 return bookmark_model && bookmark_model->IsBookmarked(url); |
311 } | 313 } |
312 | 314 |
313 void FaviconHandler::OnFaviconDataForInitialURL( | 315 void FaviconHandler::OnFaviconDataForInitialURL( |
314 FaviconService::Handle handle, | 316 FaviconService::Handle handle, |
315 history::FaviconData favicon) { | 317 history::FaviconData favicon) { |
316 content::NavigationEntry* entry = GetEntry(); | 318 NavigationEntry* entry = GetEntry(); |
317 if (!entry) | 319 if (!entry) |
318 return; | 320 return; |
319 | 321 |
320 got_favicon_from_history_ = true; | 322 got_favicon_from_history_ = true; |
321 history_icon_ = favicon; | 323 history_icon_ = favicon; |
322 | 324 |
323 favicon_expired_ = (favicon.known_icon && favicon.expired); | 325 favicon_expired_ = (favicon.known_icon && favicon.expired); |
324 | 326 |
325 if (favicon.known_icon && favicon.icon_type == history::FAVICON && | 327 if (favicon.known_icon && favicon.icon_type == history::FAVICON && |
326 !entry->GetFavicon().valid && | 328 !entry->GetFavicon().valid && |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 // Issue the request and associate the current page ID with it. | 385 // Issue the request and associate the current page ID with it. |
384 UpdateFaviconMappingAndFetch(page_url, icon_url, icon_type, | 386 UpdateFaviconMappingAndFetch(page_url, icon_url, icon_type, |
385 &cancelable_consumer_, | 387 &cancelable_consumer_, |
386 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this))); | 388 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this))); |
387 } | 389 } |
388 } | 390 } |
389 } | 391 } |
390 | 392 |
391 void FaviconHandler::OnFaviconData(FaviconService::Handle handle, | 393 void FaviconHandler::OnFaviconData(FaviconService::Handle handle, |
392 history::FaviconData favicon) { | 394 history::FaviconData favicon) { |
393 content::NavigationEntry* entry = GetEntry(); | 395 NavigationEntry* entry = GetEntry(); |
394 if (!entry) | 396 if (!entry) |
395 return; | 397 return; |
396 | 398 |
397 // No need to update the favicon url. By the time we get here | 399 // No need to update the favicon url. By the time we get here |
398 // UpdateFaviconURL will have set the favicon url. | 400 // UpdateFaviconURL will have set the favicon url. |
399 if (favicon.icon_type == history::FAVICON) { | 401 if (favicon.icon_type == history::FAVICON) { |
400 if (favicon.is_valid()) { | 402 if (favicon.is_valid()) { |
401 // There is a favicon, set it now. If expired we'll download the current | 403 // There is a favicon, set it now. If expired we'll download the current |
402 // one again, but at least the user will get some icon instead of the | 404 // one again, but at least the user will get some icon instead of the |
403 // default and most likely the current one is fine anyway. | 405 // default and most likely the current one is fine anyway. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 if (width > 0 && height > 0) { | 451 if (width > 0 && height > 0) { |
450 gfx::CalculateFaviconTargetSize(&width, &height); | 452 gfx::CalculateFaviconTargetSize(&width, &height); |
451 return gfx::Image(new SkBitmap( | 453 return gfx::Image(new SkBitmap( |
452 skia::ImageOperations::Resize( | 454 skia::ImageOperations::Resize( |
453 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | 455 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, |
454 width, height))); | 456 width, height))); |
455 } | 457 } |
456 | 458 |
457 return image; | 459 return image; |
458 } | 460 } |
OLD | NEW |