Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/favicon/core/large_icon_service.h" | 5 #include "components/favicon/core/large_icon_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/task_runner.h" | |
| 10 #include "base/threading/sequenced_worker_pool.h" | |
| 7 #include "components/favicon/core/favicon_service.h" | 11 #include "components/favicon/core/favicon_service.h" |
| 8 #include "components/favicon_base/fallback_icon_style.h" | 12 #include "components/favicon_base/fallback_icon_style.h" |
| 9 #include "components/favicon_base/favicon_types.h" | 13 #include "components/favicon_base/favicon_types.h" |
| 14 #include "content/public/browser/browser_thread.h" | |
| 10 #include "skia/ext/image_operations.h" | 15 #include "skia/ext/image_operations.h" |
| 11 #include "ui/gfx/codec/png_codec.h" | 16 #include "ui/gfx/codec/png_codec.h" |
| 12 #include "ui/gfx/geometry/size.h" | 17 #include "ui/gfx/geometry/size.h" |
| 13 | 18 |
| 14 namespace favicon { | 19 namespace favicon { |
| 15 | 20 |
| 16 LargeIconService::LargeIconService(FaviconService* favicon_service) | 21 LargeIconService::LargeIconService(FaviconService* favicon_service) |
| 17 : favicon_service_(favicon_service) { | 22 : favicon_service_(favicon_service) { |
| 18 large_icon_types_.push_back(favicon_base::IconType::FAVICON); | 23 large_icon_types_.push_back(favicon_base::IconType::FAVICON); |
| 19 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON); | 24 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON); |
| 20 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON); | 25 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON); |
| 21 } | 26 } |
| 22 | 27 |
| 23 LargeIconService::~LargeIconService() { | 28 LargeIconService::~LargeIconService() { |
| 24 } | 29 } |
| 25 | 30 |
| 31 // Main entry point, runs on owner thread. | |
| 26 base::CancelableTaskTracker::TaskId | 32 base::CancelableTaskTracker::TaskId |
| 27 LargeIconService::GetLargeIconOrFallbackStyle( | 33 LargeIconService::GetLargeIconOrFallbackStyle( |
| 28 const GURL& page_url, | 34 const GURL& page_url, |
| 29 int desired_size_in_pixel, | 35 int desired_size_in_pixel, |
| 30 const favicon_base::LargeIconCallback& callback, | 36 const favicon_base::LargeIconCallback& callback, |
| 31 base::CancelableTaskTracker* tracker) { | 37 base::CancelableTaskTracker* tracker) { |
| 32 // TODO(beaudoin): For now this is just a wrapper around | 38 // TODO(beaudoin): For now this is just a wrapper around |
| 33 // GetLargestRawFaviconForPageURL. Add the logic required to select the best | 39 // GetLargestRawFaviconForPageURL. Add the logic required to select the best |
| 34 // possible large icon. Also add logic to fetch-on-demand when the URL of | 40 // possible large icon. Also add logic to fetch-on-demand when the URL of |
| 35 // a large icon is known but its bitmap is not available. | 41 // a large icon is known but its bitmap is not available. |
| 36 return favicon_service_->GetLargestRawFaviconForPageURL( | 42 return favicon_service_->GetLargestRawFaviconForPageURL( |
| 37 page_url, | 43 page_url, |
| 38 large_icon_types_, | 44 large_icon_types_, |
| 39 desired_size_in_pixel, | 45 desired_size_in_pixel, |
| 40 base::Bind(&LargeIconService::RunLargeIconCallback, | 46 base::Bind(&LargeIconService::RunLargeIconCallback, |
| 41 base::Unretained(this), callback, desired_size_in_pixel), | 47 base::Unretained(this), |
| 48 callback, | |
| 49 desired_size_in_pixel, | |
| 50 tracker), | |
| 42 tracker); | 51 tracker); |
| 43 } | 52 } |
| 44 | 53 |
| 54 // Runs on background thread. | |
| 45 bool LargeIconService::ResizeLargeIconIfValid( | 55 bool LargeIconService::ResizeLargeIconIfValid( |
| 46 int desired_size_in_pixel, | 56 int desired_size_in_pixel, |
| 47 const favicon_base::FaviconRawBitmapResult& bitmap_result, | 57 const favicon_base::FaviconRawBitmapResult& bitmap_result, |
| 48 favicon_base::FaviconRawBitmapResult* resized_bitmap_result) { | 58 favicon_base::FaviconRawBitmapResult* resized_bitmap_result) { |
| 59 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 49 // Require bitmap to be valid and square. | 60 // Require bitmap to be valid and square. |
| 50 if (!bitmap_result.is_valid() || | 61 if (!bitmap_result.is_valid() || |
| 51 bitmap_result.pixel_size.width() != bitmap_result.pixel_size.height()) | 62 bitmap_result.pixel_size.width() != bitmap_result.pixel_size.height()) |
| 52 return false; | 63 return false; |
| 53 | 64 |
| 54 // Require bitmap to be large enough. It's square, so just check width. | 65 // Require bitmap to be large enough. It's square, so just check width. |
| 55 if (bitmap_result.pixel_size.width() < desired_size_in_pixel) { | 66 if (bitmap_result.pixel_size.width() < desired_size_in_pixel) { |
| 56 // TODO(beaudoin): Potentially relax this, and allow icon to be scaled up. | 67 // TODO(beaudoin): Potentially relax this, and allow icon to be scaled up. |
| 57 return false; | 68 return false; |
| 58 } | 69 } |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 77 if (!gfx::PNGCodec::EncodeBGRASkBitmap(resized_bitmap, false, &bitmap_data)) | 88 if (!gfx::PNGCodec::EncodeBGRASkBitmap(resized_bitmap, false, &bitmap_data)) |
| 78 return false; | 89 return false; |
| 79 | 90 |
| 80 resized_bitmap_result->pixel_size = | 91 resized_bitmap_result->pixel_size = |
| 81 gfx::Size(desired_size_in_pixel, desired_size_in_pixel); | 92 gfx::Size(desired_size_in_pixel, desired_size_in_pixel); |
| 82 resized_bitmap_result->bitmap_data = | 93 resized_bitmap_result->bitmap_data = |
| 83 base::RefCountedBytes::TakeVector(&bitmap_data); | 94 base::RefCountedBytes::TakeVector(&bitmap_data); |
| 84 return true; | 95 return true; |
| 85 } | 96 } |
| 86 | 97 |
| 87 void LargeIconService::RunLargeIconCallback( | 98 // Runs on background thread. |
| 99 void LargeIconService::RunLargeIconCallbackOnBackgroundThread( | |
| 88 const favicon_base::LargeIconCallback& callback, | 100 const favicon_base::LargeIconCallback& callback, |
| 89 int desired_size_in_pixel, | 101 int desired_size_in_pixel, |
| 90 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 102 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| 91 favicon_base::FaviconRawBitmapResult resized_bitmap_result; | 103 favicon_base::FaviconRawBitmapResult resized_bitmap_result; |
| 92 if (ResizeLargeIconIfValid(desired_size_in_pixel, bitmap_result, | 104 if (ResizeLargeIconIfValid(desired_size_in_pixel, bitmap_result, |
| 93 &resized_bitmap_result)) { | 105 &resized_bitmap_result)) { |
| 94 callback.Run(favicon_base::LargeIconResult(resized_bitmap_result)); | 106 callback.Run(favicon_base::LargeIconResult(resized_bitmap_result)); |
| 95 return; | 107 return; |
| 96 } | 108 } |
| 97 | 109 |
| 98 // Failed to resize |bitmap_result|, so compute fallback icon style. | 110 // Failed to resize |bitmap_result|, so compute fallback icon style. |
| 99 favicon_base::LargeIconResult result(new favicon_base::FallbackIconStyle()); | 111 favicon_base::LargeIconResult result(new favicon_base::FallbackIconStyle()); |
| 100 if (bitmap_result.is_valid()) { | 112 if (bitmap_result.is_valid()) { |
| 101 favicon_base::SetDominantColorAsBackground( | 113 favicon_base::SetDominantColorAsBackground( |
| 102 bitmap_result.bitmap_data, result.fallback_icon_style.get()); | 114 bitmap_result.bitmap_data, result.fallback_icon_style.get()); |
| 103 } | 115 } |
| 104 callback.Run(result); | 116 callback.Run(result); |
|
beaudoin
2015/05/05 00:00:52
Shouldn't that callback go back to the initial thr
huangs
2015/05/05 15:22:13
Done.
| |
| 105 } | 117 } |
| 106 | 118 |
| 119 // Runs on owner thread. | |
| 120 void LargeIconService::RunLargeIconCallback( | |
| 121 const favicon_base::LargeIconCallback& callback, | |
| 122 int desired_size_in_pixel, | |
| 123 base::CancelableTaskTracker* tracker, | |
| 124 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); | |
|
beaudoin
2015/05/05 00:00:52
From reading CalledOnValidThread it looks like thi
huangs
2015/05/05 15:22:12
Using a different way to check.
| |
| 126 tracker->PostTask( | |
| 127 GetBackgroundTaskRunner().get(), | |
| 128 FROM_HERE, | |
| 129 base::Bind(&LargeIconService::RunLargeIconCallbackOnBackgroundThread, | |
| 130 base::Unretained(this), | |
| 131 callback, | |
| 132 desired_size_in_pixel, | |
| 133 bitmap_result)); | |
| 134 } | |
| 135 | |
| 136 scoped_refptr<base::TaskRunner> LargeIconService::GetBackgroundTaskRunner() { | |
| 137 return content::BrowserThread::GetBlockingPool()-> | |
| 138 GetTaskRunnerWithShutdownBehavior( | |
| 139 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
| 140 } | |
| 141 | |
| 107 } // namespace favicon | 142 } // namespace favicon |
| OLD | NEW |