| 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)));
|
| +}
|
| +
|
| +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)));
|
| +}
|
| +
|
| +void KioskAppIconLoader::NotifyClient() {
|
| + if (!client_)
|
| + return;
|
| +
|
| + if (load_result_ == SUCCESS)
|
| + client_->OnIconLoadSuccess(icon_);
|
| + else
|
| + client_->OnIconLoadFailure();
|
| +}
|
| +
|
| +void KioskAppIconLoader::ReportResultOnUIThread() {
|
| + DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
| +
|
| + NotifyClient();
|
| + delete this;
|
| +}
|
| +
|
| +} // namespace chromeos
|
|
|