Chromium Code Reviews| Index: components/favicon/core/large_icon_service.cc |
| diff --git a/components/favicon/core/large_icon_service.cc b/components/favicon/core/large_icon_service.cc |
| index b411d4abe64f4d2d4f01cee0415e322fa1642ff9..02da596167a3b9e7a775c9ec7d42cde1a2dd747e 100644 |
| --- a/components/favicon/core/large_icon_service.cc |
| +++ b/components/favicon/core/large_icon_service.cc |
| @@ -4,15 +4,38 @@ |
| #include "components/favicon/core/large_icon_service.h" |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "base/task_runner.h" |
| +#include "base/threading/sequenced_worker_pool.h" |
| #include "components/favicon/core/favicon_service.h" |
| #include "components/favicon_base/fallback_icon_style.h" |
| -#include "components/favicon_base/favicon_types.h" |
| +#include "content/public/browser/browser_thread.h" |
| #include "skia/ext/image_operations.h" |
| #include "ui/gfx/codec/png_codec.h" |
| #include "ui/gfx/geometry/size.h" |
| +namespace { |
| + |
| +// Return a TaskRunner used to execute background task. |
|
pkotwicz
2015/05/07 14:52:14
Nit: "Return TaskRunner" (no "a")
huangs
2015/05/07 21:01:58
Done, also using "Returns".
|
| +scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { |
| + return content::BrowserThread::GetBlockingPool() |
| + ->GetTaskRunnerWithShutdownBehavior( |
| + base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| +} |
| + |
| +} // namespace |
| + |
| namespace favicon { |
| +LargeIconService::Session::Session() |
| + : desired_size_in_pixel(0), |
| + tracker(nullptr) { |
| +} |
| + |
| +LargeIconService::Session::~Session() { |
| +} |
| + |
| LargeIconService::LargeIconService(FaviconService* favicon_service) |
| : favicon_service_(favicon_service) { |
| large_icon_types_.push_back(favicon_base::IconType::FAVICON); |
| @@ -29,19 +52,24 @@ base::CancelableTaskTracker::TaskId |
| int desired_size_in_pixel, |
| const favicon_base::LargeIconCallback& callback, |
| base::CancelableTaskTracker* tracker) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + scoped_refptr<LargeIconService::Session> session = |
| + new LargeIconService::Session(); |
| + session->desired_size_in_pixel = desired_size_in_pixel; |
| + session->callback = callback; |
| + session->tracker = tracker; |
| + |
| // TODO(beaudoin): For now this is just a wrapper around |
| // GetLargestRawFaviconForPageURL. Add the logic required to select the best |
| // possible large icon. Also add logic to fetch-on-demand when the URL of |
| // a large icon is known but its bitmap is not available. |
| return favicon_service_->GetLargestRawFaviconForPageURL( |
| - page_url, |
| - large_icon_types_, |
| - desired_size_in_pixel, |
| - base::Bind(&LargeIconService::RunLargeIconCallback, |
| - base::Unretained(this), callback, desired_size_in_pixel), |
| + page_url, large_icon_types_, desired_size_in_pixel, |
| + base::Bind(&LargeIconService::OnIconLookupComplete, session), |
| tracker); |
| } |
| +// static |
| bool LargeIconService::ResizeLargeIconIfValid( |
|
pkotwicz
2015/05/07 14:52:14
Can you please add the "OnBackgroundThread" suffix
huangs
2015/05/07 21:01:58
Done, but "IfValid" wins as suffix.
|
| int desired_size_in_pixel, |
| const favicon_base::FaviconRawBitmapResult& bitmap_result, |
| @@ -58,7 +86,6 @@ bool LargeIconService::ResizeLargeIconIfValid( |
| } |
| *resized_bitmap_result = bitmap_result; |
| - |
| // Special case: Can use |resized_bitmap_result| as is. |
| if (bitmap_result.pixel_size.width() == desired_size_in_pixel) |
| return true; |
| @@ -84,24 +111,47 @@ bool LargeIconService::ResizeLargeIconIfValid( |
| return true; |
| } |
| -void LargeIconService::RunLargeIconCallback( |
| - const favicon_base::LargeIconCallback& callback, |
| - int desired_size_in_pixel, |
| +// static |
| +void LargeIconService::OnIconLookupComplete( |
| + scoped_refptr<LargeIconService::Session> session, |
| const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| + session->bitmap_result = bitmap_result; |
| + session->tracker->PostTask( |
| + GetBackgroundTaskRunner().get(), FROM_HERE, |
| + base::Bind(&LargeIconService::ProcessIconOnBackgroundThread, session)); |
| +} |
| + |
| +// static |
| +void LargeIconService::ProcessIconOnBackgroundThread( |
| + scoped_refptr<LargeIconService::Session> session) { |
| + DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| favicon_base::FaviconRawBitmapResult resized_bitmap_result; |
| - if (ResizeLargeIconIfValid(desired_size_in_pixel, bitmap_result, |
| - &resized_bitmap_result)) { |
| - callback.Run(favicon_base::LargeIconResult(resized_bitmap_result)); |
| - return; |
| + if (ResizeLargeIconIfValid(session->desired_size_in_pixel, |
| + session->bitmap_result, &resized_bitmap_result)) { |
| + session->result.reset( |
| + new favicon_base::LargeIconResult(resized_bitmap_result)); |
| + } else { |
| + // Failed to resize |bitmap_result|, so compute fallback icon style. |
| + scoped_ptr<favicon_base::FallbackIconStyle> fallback_icon_style( |
| + new favicon_base::FallbackIconStyle()); |
| + if (session->bitmap_result.is_valid()) { |
| + favicon_base::SetDominantColorAsBackground( |
| + session->bitmap_result.bitmap_data, fallback_icon_style.get()); |
| + } |
| + session->result.reset( |
| + new favicon_base::LargeIconResult(fallback_icon_style.release())); |
| } |
| - // Failed to resize |bitmap_result|, so compute fallback icon style. |
| - favicon_base::LargeIconResult result(new favicon_base::FallbackIconStyle()); |
| - if (bitmap_result.is_valid()) { |
| - favicon_base::SetDominantColorAsBackground( |
| - bitmap_result.bitmap_data, result.fallback_icon_style.get()); |
| - } |
| - callback.Run(result); |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(&LargeIconService::OnIconProcessingComplete, session)); |
|
pkotwicz
2015/05/07 14:52:14
Is there a reason that you pass the session object
huangs
2015/05/07 21:01:58
Using session->callback and session->result. And
|
| +} |
| + |
| +// static |
| +void LargeIconService::OnIconProcessingComplete( |
| + scoped_refptr<LargeIconService::Session> session) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + session->callback.Run(*session->result); |
| } |
| } // namespace favicon |