Chromium Code Reviews| Index: chrome/browser/chromeos/app_mode/kiosk_app_icon_loader.cc |
| diff --git a/chrome/browser/chromeos/app_mode/kiosk_app_icon_loader.cc b/chrome/browser/chromeos/app_mode/kiosk_app_icon_loader.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..777aca7534abd3aa40abb9bab82c1c36bf5556ae |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/app_mode/kiosk_app_icon_loader.cc |
| @@ -0,0 +1,93 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/app_mode/kiosk_app_icon_loader.h" |
| + |
| +#include <vector> |
| + |
| +#include "base/bind.h" |
| +#include "base/files/file_util.h" |
| +#include "base/threading/sequenced_worker_pool.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace chromeos { |
| + |
| +KioskAppIconLoader::IconImageRequest::IconImageRequest( |
| + const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| + KioskAppIconLoader* icon_loader) |
| + : ImageRequest(task_runner), icon_loader_(icon_loader) {} |
| + |
| +void KioskAppIconLoader::IconImageRequest::OnImageDecoded( |
| + const SkBitmap& decoded_image) { |
| + icon_loader_->icon_ = gfx::ImageSkia::CreateFrom1xBitmap(decoded_image); |
| + icon_loader_->icon_.MakeThreadSafe(); |
| + icon_loader_->ReportResultOnBlockingPool(SUCCESS); |
| + delete this; |
| +} |
| + |
| +void KioskAppIconLoader::IconImageRequest::OnDecodeImageFailed() { |
| + icon_loader_->ReportResultOnBlockingPool(FAILED_TO_DECODE); |
| + delete this; |
| +} |
| + |
| +KioskAppIconLoader::KioskAppIconLoader(const base::WeakPtr<Delegate>& client, |
| + const base::FilePath& icon_path) |
| + : client_(client), icon_path_(icon_path), load_result_(SUCCESS) {} |
| + |
| +KioskAppIconLoader::~KioskAppIconLoader() = default; |
| + |
| +void KioskAppIconLoader::Start() { |
| + base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| + base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); |
| + task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| + token, base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| + task_runner_->PostTask(FROM_HERE, |
| + base::Bind(&KioskAppIconLoader::LoadOnBlockingPool, |
| + base::Unretained(this))); |
|
khmel
2017/03/27 22:56:34
We might need weak pointer binding instead of Unre
Sergey Poromov
2017/03/28 15:25:55
I just refactored to extract this class from Kiosk
xiyuan
2017/03/28 16:50:09
The reading is correct.
IconLoader manages its ow
|
| +} |
| + |
| +void KioskAppIconLoader::LoadOnBlockingPool() { |
| + DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| + |
| + std::string data; |
| + if (!base::ReadFileToString(base::FilePath(icon_path_), &data)) { |
| + ReportResultOnBlockingPool(FAILED_TO_LOAD); |
| + return; |
| + } |
| + raw_icon_ = base::RefCountedString::TakeString(&data); |
| + |
| + IconImageRequest* image_request = new IconImageRequest(task_runner_, this); |
| + ImageDecoder::Start(image_request, raw_icon_->data()); |
| +} |
| + |
| +void KioskAppIconLoader::ReportResultOnBlockingPool(LoadResult result) { |
| + DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| + |
| + load_result_ = result; |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&KioskAppIconLoader::ReportResultOnUIThread, |
| + base::Unretained(this))); |
|
khmel
2017/03/27 22:56:33
Use weak pointer instead of Unretained?
|
| +} |
| + |
| +void KioskAppIconLoader::NotifyClient() { |
| + if (!client_) |
| + return; |
| + |
| + if (load_result_ == SUCCESS) |
|
khmel
2017/03/27 22:56:32
You set and read load_results_ from different thre
|
| + client_->OnIconLoadSuccess(icon_); |
| + else |
| + client_->OnIconLoadFailure(); |
| +} |
| + |
| +void KioskAppIconLoader::ReportResultOnUIThread() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + |
| + NotifyClient(); |
| + delete this; |
| +} |
| + |
| +} // namespace chromeos |