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..4c15cea3e173ba46c9eca90d1833a33254280fa0 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/app_mode/kiosk_app_icon_loader.cc |
| @@ -0,0 +1,99 @@ |
| +// 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_path.h" |
| +#include "base/files/file_util.h" |
| +#include "base/threading/sequenced_worker_pool.h" |
| +#include "chrome/browser/image_decoder.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace chromeos { |
| + |
| +class KioskAppIconLoader::IconImageRequest : public ImageDecoder::ImageRequest { |
| + public: |
| + IconImageRequest(const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| + KioskAppIconLoader* icon_loader) |
| + : ImageRequest(task_runner), icon_loader_(icon_loader) {} |
| + |
| + void OnImageDecoded(const SkBitmap& decoded_image) override { |
| + icon_loader_->icon_ = gfx::ImageSkia::CreateFrom1xBitmap(decoded_image); |
| + icon_loader_->icon_.MakeThreadSafe(); |
| + icon_loader_->ReportResultOnBlockingPool(SUCCESS); |
| + delete this; |
| + } |
| + |
| + void OnDecodeImageFailed() override { |
| + icon_loader_->ReportResultOnBlockingPool(FAILED_TO_DECODE); |
| + delete this; |
| + } |
| + |
| + private: |
| + ~IconImageRequest() override = default; |
| + KioskAppIconLoader* const icon_loader_; |
| +}; |
| + |
| +KioskAppIconLoader::KioskAppIconLoader(Delegate* delegate) |
| + : delegate_(delegate), weak_factory_(this) {} |
| + |
| +KioskAppIconLoader::~KioskAppIconLoader() = default; |
| + |
| +void KioskAppIconLoader::Start(const base::FilePath& icon_path) { |
| + 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, |
| + weak_factory_.GetWeakPtr(), icon_path)); |
| +} |
| + |
| +void KioskAppIconLoader::LoadOnBlockingPool(const base::FilePath& icon_path) { |
| + DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| + |
| + std::string data; |
| + if (!base::ReadFileToString(base::FilePath(icon_path), &data)) { |
| + ReportResultOnBlockingPool(FAILED_TO_LOAD); |
| + return; |
| + } |
| + |
| + // IconImageRequest will delete itself on completion of ImageDecoder callback. |
| + IconImageRequest* image_request = new IconImageRequest(task_runner_, this); |
| + ImageDecoder::Start(image_request, |
| + std::vector<uint8_t>(data.begin(), data.end())); |
| +} |
| + |
| +void KioskAppIconLoader::ReportResultOnBlockingPool(LoadResult result) { |
| + DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| + |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&KioskAppIconLoader::ReportResultOnUIThread, |
| + weak_factory_.GetWeakPtr(), result)); |
| +} |
| + |
| +void KioskAppIconLoader::NotifyDelegate(LoadResult result) { |
| + if (!delegate_) |
|
xiyuan
2017/03/31 15:10:03
nit: no need to check |delegate_| now since |deleg
Sergey Poromov
2017/03/31 18:38:58
Done.
|
| + return; |
| + |
| + if (result == SUCCESS) |
| + delegate_->OnIconLoadSuccess(icon_); |
| + else |
| + delegate_->OnIconLoadFailure(); |
| +} |
| + |
| +void KioskAppIconLoader::ReportResultOnUIThread(LoadResult result) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + |
| + NotifyDelegate(result); |
| + delete this; |
|
xiyuan
2017/03/31 15:10:03
Remove this since |delegate_| delegate owns it now
Sergey Poromov
2017/03/31 18:38:58
Done.
|
| +} |
| + |
| +} // namespace chromeos |