| 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_service.h" | 5 #include "chrome/browser/favicon/favicon_service.h" |
| 6 | 6 |
| 7 #include "base/message_loop_proxy.h" | 7 #include "base/message_loop_proxy.h" |
| 8 #include "chrome/browser/favicon/favicon_util.h" | 8 #include "chrome/browser/favicon/favicon_util.h" |
| 9 #include "chrome/browser/history/history.h" | 9 #include "chrome/browser/history/history.h" |
| 10 #include "chrome/browser/history/history_backend.h" | 10 #include "chrome/browser/history/history_backend.h" |
| 11 #include "chrome/browser/history/history_service_factory.h" | 11 #include "chrome/browser/history/history_service_factory.h" |
| 12 #include "chrome/browser/history/select_favicon_frames.h" | 12 #include "chrome/browser/history/select_favicon_frames.h" |
| 13 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" | 13 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" |
| 14 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 15 #include "extensions/common/constants.h" | 15 #include "extensions/common/constants.h" |
| 16 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 #include "ui/gfx/codec/png_codec.h" | 17 #include "ui/gfx/codec/png_codec.h" |
| 18 #include "ui/gfx/favicon_size.h" | 18 #include "ui/gfx/favicon_size.h" |
| 19 #include "ui/gfx/image/image_skia.h" | 19 #include "ui/gfx/image/image_skia.h" |
| 20 | 20 |
| 21 using base::Bind; | 21 using base::Bind; |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 void CancelOrRunFaviconResultsCallback( | 25 void CancelOrRunFaviconResultsCallback( |
| 26 const CancelableTaskTracker::IsCanceledCallback& is_canceled, | 26 const CancelableTaskTracker::IsCanceledCallback& is_canceled, |
| 27 const FaviconService::FaviconResultsCallback& callback, | 27 const FaviconService::FaviconResultsCallback& callback, |
| 28 const std::vector<history::FaviconBitmapResult>& results, | 28 const std::vector<history::FaviconBitmapResult>& results) { |
| 29 const history::IconURLSizesMap& size_map) { | |
| 30 if (is_canceled.Run()) | 29 if (is_canceled.Run()) |
| 31 return; | 30 return; |
| 32 callback.Run(results, size_map); | 31 callback.Run(results); |
| 33 } | 32 } |
| 34 | 33 |
| 35 // Helper to run callback with empty results if we cannot get the history | 34 // Helper to run callback with empty results if we cannot get the history |
| 36 // service. | 35 // service. |
| 37 CancelableTaskTracker::TaskId RunWithEmptyResultAsync( | 36 CancelableTaskTracker::TaskId RunWithEmptyResultAsync( |
| 38 const FaviconService::FaviconResultsCallback& callback, | 37 const FaviconService::FaviconResultsCallback& callback, |
| 39 CancelableTaskTracker* tracker) { | 38 CancelableTaskTracker* tracker) { |
| 40 return tracker->PostTask( | 39 return tracker->PostTask( |
| 41 base::MessageLoopProxy::current(), | 40 base::MessageLoopProxy::current(), |
| 42 FROM_HERE, | 41 FROM_HERE, |
| 43 Bind(callback, | 42 Bind(callback, std::vector<history::FaviconBitmapResult>())); |
| 44 std::vector<history::FaviconBitmapResult>(), | |
| 45 history::IconURLSizesMap())); | |
| 46 } | 43 } |
| 47 | 44 |
| 48 } // namespace | 45 } // namespace |
| 49 | 46 |
| 50 FaviconService::FaviconService(HistoryService* history_service) | 47 FaviconService::FaviconService(HistoryService* history_service) |
| 51 : history_service_(history_service) { | 48 : history_service_(history_service) { |
| 52 } | 49 } |
| 53 | 50 |
| 54 // static | 51 // static |
| 55 void FaviconService::FaviconResultsCallbackRunner( | 52 void FaviconService::FaviconResultsCallbackRunner( |
| 56 const FaviconResultsCallback& callback, | 53 const FaviconResultsCallback& callback, |
| 57 const std::vector<history::FaviconBitmapResult>* results, | 54 const std::vector<history::FaviconBitmapResult>* results) { |
| 58 const history::IconURLSizesMap* size_map) { | 55 callback.Run(*results); |
| 59 callback.Run(*results, *size_map); | |
| 60 } | 56 } |
| 61 | 57 |
| 62 CancelableTaskTracker::TaskId FaviconService::GetFaviconImage( | 58 CancelableTaskTracker::TaskId FaviconService::GetFaviconImage( |
| 63 const GURL& icon_url, | 59 const GURL& icon_url, |
| 64 history::IconType icon_type, | 60 history::IconType icon_type, |
| 65 int desired_size_in_dip, | 61 int desired_size_in_dip, |
| 66 const FaviconImageCallback& callback, | 62 const FaviconImageCallback& callback, |
| 67 CancelableTaskTracker* tracker) { | 63 CancelableTaskTracker* tracker) { |
| 68 FaviconResultsCallback callback_runner = | 64 FaviconResultsCallback callback_runner = |
| 69 Bind(&FaviconService::RunFaviconImageCallbackWithBitmapResults, | 65 Bind(&FaviconService::RunFaviconImageCallbackWithBitmapResults, |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 const GURL& icon_url, | 232 const GURL& icon_url, |
| 237 history::IconType icon_type, | 233 history::IconType icon_type, |
| 238 const gfx::Image& image) { | 234 const gfx::Image& image) { |
| 239 if (!history_service_) | 235 if (!history_service_) |
| 240 return; | 236 return; |
| 241 | 237 |
| 242 gfx::ImageSkia image_skia = image.AsImageSkia(); | 238 gfx::ImageSkia image_skia = image.AsImageSkia(); |
| 243 image_skia.EnsureRepsForSupportedScaleFactors(); | 239 image_skia.EnsureRepsForSupportedScaleFactors(); |
| 244 const std::vector<gfx::ImageSkiaRep>& image_reps = image_skia.image_reps(); | 240 const std::vector<gfx::ImageSkiaRep>& image_reps = image_skia.image_reps(); |
| 245 std::vector<history::FaviconBitmapData> favicon_bitmap_data; | 241 std::vector<history::FaviconBitmapData> favicon_bitmap_data; |
| 246 history::FaviconSizes favicon_sizes; | |
| 247 for (size_t i = 0; i < image_reps.size(); ++i) { | 242 for (size_t i = 0; i < image_reps.size(); ++i) { |
| 248 scoped_refptr<base::RefCountedBytes> bitmap_data( | 243 scoped_refptr<base::RefCountedBytes> bitmap_data( |
| 249 new base::RefCountedBytes()); | 244 new base::RefCountedBytes()); |
| 250 if (gfx::PNGCodec::EncodeBGRASkBitmap(image_reps[i].sk_bitmap(), | 245 if (gfx::PNGCodec::EncodeBGRASkBitmap(image_reps[i].sk_bitmap(), |
| 251 false, | 246 false, |
| 252 &bitmap_data->data())) { | 247 &bitmap_data->data())) { |
| 253 gfx::Size pixel_size(image_reps[i].pixel_width(), | 248 gfx::Size pixel_size(image_reps[i].pixel_width(), |
| 254 image_reps[i].pixel_height()); | 249 image_reps[i].pixel_height()); |
| 255 history::FaviconBitmapData bitmap_data_element; | 250 history::FaviconBitmapData bitmap_data_element; |
| 256 bitmap_data_element.bitmap_data = bitmap_data; | 251 bitmap_data_element.bitmap_data = bitmap_data; |
| 257 bitmap_data_element.pixel_size = pixel_size; | 252 bitmap_data_element.pixel_size = pixel_size; |
| 258 bitmap_data_element.icon_url = icon_url; | 253 bitmap_data_element.icon_url = icon_url; |
| 259 | 254 |
| 260 favicon_bitmap_data.push_back(bitmap_data_element); | 255 favicon_bitmap_data.push_back(bitmap_data_element); |
| 261 | |
| 262 // Construct favicon sizes from a guess at what the HTML 5 'sizes' | |
| 263 // attribute in the link tag is. | |
| 264 // TODO(pkotwicz): Plumb the HTML 5 sizes attribute to FaviconHandler. | |
| 265 favicon_sizes.push_back(pixel_size); | |
| 266 } | 256 } |
| 267 } | 257 } |
| 268 | 258 |
| 269 // TODO(pkotwicz): Tell the database about all the icon URLs associated | 259 history_service_->SetFavicons(page_url, icon_type, favicon_bitmap_data); |
| 270 // with |page_url|. | |
| 271 history::IconURLSizesMap icon_url_sizes; | |
| 272 icon_url_sizes[icon_url] = favicon_sizes; | |
| 273 | |
| 274 history_service_->SetFavicons(page_url, icon_type, favicon_bitmap_data, | |
| 275 icon_url_sizes); | |
| 276 } | 260 } |
| 277 | 261 |
| 278 FaviconService::~FaviconService() {} | 262 FaviconService::~FaviconService() {} |
| 279 | 263 |
| 280 CancelableTaskTracker::TaskId FaviconService::GetFaviconForURLImpl( | 264 CancelableTaskTracker::TaskId FaviconService::GetFaviconForURLImpl( |
| 281 const FaviconForURLParams& params, | 265 const FaviconForURLParams& params, |
| 282 const std::vector<ui::ScaleFactor>& desired_scale_factors, | 266 const std::vector<ui::ScaleFactor>& desired_scale_factors, |
| 283 const FaviconResultsCallback& callback, | 267 const FaviconResultsCallback& callback, |
| 284 CancelableTaskTracker* tracker) { | 268 CancelableTaskTracker* tracker) { |
| 285 if (params.page_url.SchemeIs(chrome::kChromeUIScheme) || | 269 if (params.page_url.SchemeIs(chrome::kChromeUIScheme) || |
| (...skipping 15 matching lines...) Expand all Loading... |
| 301 callback, | 285 callback, |
| 302 tracker); | 286 tracker); |
| 303 } else { | 287 } else { |
| 304 return RunWithEmptyResultAsync(callback, tracker); | 288 return RunWithEmptyResultAsync(callback, tracker); |
| 305 } | 289 } |
| 306 } | 290 } |
| 307 | 291 |
| 308 void FaviconService::RunFaviconImageCallbackWithBitmapResults( | 292 void FaviconService::RunFaviconImageCallbackWithBitmapResults( |
| 309 const FaviconImageCallback& callback, | 293 const FaviconImageCallback& callback, |
| 310 int desired_size_in_dip, | 294 int desired_size_in_dip, |
| 311 const std::vector<history::FaviconBitmapResult>& favicon_bitmap_results, | 295 const std::vector<history::FaviconBitmapResult>& favicon_bitmap_results) { |
| 312 const history::IconURLSizesMap& icon_url_sizes_map) { | |
| 313 history::FaviconImageResult image_result; | 296 history::FaviconImageResult image_result; |
| 314 image_result.image = FaviconUtil::SelectFaviconFramesFromPNGs( | 297 image_result.image = FaviconUtil::SelectFaviconFramesFromPNGs( |
| 315 favicon_bitmap_results, | 298 favicon_bitmap_results, |
| 316 FaviconUtil::GetFaviconScaleFactors(), | 299 FaviconUtil::GetFaviconScaleFactors(), |
| 317 desired_size_in_dip); | 300 desired_size_in_dip); |
| 318 image_result.icon_url = image_result.image.IsEmpty() ? | 301 image_result.icon_url = image_result.image.IsEmpty() ? |
| 319 GURL() : favicon_bitmap_results[0].icon_url; | 302 GURL() : favicon_bitmap_results[0].icon_url; |
| 320 callback.Run(image_result); | 303 callback.Run(image_result); |
| 321 } | 304 } |
| 322 | 305 |
| 323 void FaviconService::RunFaviconRawCallbackWithBitmapResults( | 306 void FaviconService::RunFaviconRawCallbackWithBitmapResults( |
| 324 const FaviconRawCallback& callback, | 307 const FaviconRawCallback& callback, |
| 325 int desired_size_in_dip, | 308 int desired_size_in_dip, |
| 326 ui::ScaleFactor desired_scale_factor, | 309 ui::ScaleFactor desired_scale_factor, |
| 327 const std::vector<history::FaviconBitmapResult>& favicon_bitmap_results, | 310 const std::vector<history::FaviconBitmapResult>& favicon_bitmap_results) { |
| 328 const history::IconURLSizesMap& icon_url_sizes_map) { | |
| 329 if (favicon_bitmap_results.empty() || !favicon_bitmap_results[0].is_valid()) { | 311 if (favicon_bitmap_results.empty() || !favicon_bitmap_results[0].is_valid()) { |
| 330 callback.Run(history::FaviconBitmapResult()); | 312 callback.Run(history::FaviconBitmapResult()); |
| 331 return; | 313 return; |
| 332 } | 314 } |
| 333 | 315 |
| 334 DCHECK_EQ(1u, favicon_bitmap_results.size()); | 316 DCHECK_EQ(1u, favicon_bitmap_results.size()); |
| 335 history::FaviconBitmapResult bitmap_result = favicon_bitmap_results[0]; | 317 history::FaviconBitmapResult bitmap_result = favicon_bitmap_results[0]; |
| 336 | 318 |
| 337 // If the desired size is 0, SelectFaviconFrames() will return the largest | 319 // If the desired size is 0, SelectFaviconFrames() will return the largest |
| 338 // bitmap without doing any resizing. As |favicon_bitmap_results| has bitmap | 320 // bitmap without doing any resizing. As |favicon_bitmap_results| has bitmap |
| (...skipping 25 matching lines...) Expand all Loading... |
| 364 if (!gfx::PNGCodec::EncodeBGRASkBitmap(resized_image.AsBitmap(), false, | 346 if (!gfx::PNGCodec::EncodeBGRASkBitmap(resized_image.AsBitmap(), false, |
| 365 &resized_bitmap_data)) { | 347 &resized_bitmap_data)) { |
| 366 callback.Run(history::FaviconBitmapResult()); | 348 callback.Run(history::FaviconBitmapResult()); |
| 367 return; | 349 return; |
| 368 } | 350 } |
| 369 | 351 |
| 370 bitmap_result.bitmap_data = base::RefCountedBytes::TakeVector( | 352 bitmap_result.bitmap_data = base::RefCountedBytes::TakeVector( |
| 371 &resized_bitmap_data); | 353 &resized_bitmap_data); |
| 372 callback.Run(bitmap_result); | 354 callback.Run(bitmap_result); |
| 373 } | 355 } |
| OLD | NEW |