OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/app_mode/kiosk_app_icon_loader.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" |
| 11 #include "base/threading/sequenced_worker_pool.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 |
| 14 using content::BrowserThread; |
| 15 |
| 16 namespace chromeos { |
| 17 |
| 18 KioskAppIconLoader::IconImageRequest::IconImageRequest( |
| 19 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 20 KioskAppIconLoader* icon_loader) |
| 21 : ImageRequest(task_runner), icon_loader_(icon_loader) {} |
| 22 |
| 23 void KioskAppIconLoader::IconImageRequest::OnImageDecoded( |
| 24 const SkBitmap& decoded_image) { |
| 25 icon_loader_->icon_ = gfx::ImageSkia::CreateFrom1xBitmap(decoded_image); |
| 26 icon_loader_->icon_.MakeThreadSafe(); |
| 27 icon_loader_->ReportResultOnBlockingPool(SUCCESS); |
| 28 delete this; |
| 29 } |
| 30 |
| 31 void KioskAppIconLoader::IconImageRequest::OnDecodeImageFailed() { |
| 32 icon_loader_->ReportResultOnBlockingPool(FAILED_TO_DECODE); |
| 33 delete this; |
| 34 } |
| 35 |
| 36 KioskAppIconLoader::KioskAppIconLoader(const base::WeakPtr<Delegate>& client, |
| 37 const base::FilePath& icon_path) |
| 38 : client_(client), icon_path_(icon_path), load_result_(SUCCESS) {} |
| 39 |
| 40 KioskAppIconLoader::~KioskAppIconLoader() = default; |
| 41 |
| 42 void KioskAppIconLoader::Start() { |
| 43 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 44 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); |
| 45 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 46 token, base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 47 task_runner_->PostTask(FROM_HERE, |
| 48 base::Bind(&KioskAppIconLoader::LoadOnBlockingPool, |
| 49 base::Unretained(this))); |
| 50 } |
| 51 |
| 52 void KioskAppIconLoader::LoadOnBlockingPool() { |
| 53 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 54 |
| 55 std::string data; |
| 56 if (!base::ReadFileToString(base::FilePath(icon_path_), &data)) { |
| 57 ReportResultOnBlockingPool(FAILED_TO_LOAD); |
| 58 return; |
| 59 } |
| 60 raw_icon_ = base::RefCountedString::TakeString(&data); |
| 61 |
| 62 IconImageRequest* image_request = new IconImageRequest(task_runner_, this); |
| 63 ImageDecoder::Start(image_request, raw_icon_->data()); |
| 64 } |
| 65 |
| 66 void KioskAppIconLoader::ReportResultOnBlockingPool(LoadResult result) { |
| 67 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 68 |
| 69 load_result_ = result; |
| 70 BrowserThread::PostTask( |
| 71 BrowserThread::UI, FROM_HERE, |
| 72 base::Bind(&KioskAppIconLoader::ReportResultOnUIThread, |
| 73 base::Unretained(this))); |
| 74 } |
| 75 |
| 76 void KioskAppIconLoader::NotifyClient() { |
| 77 if (!client_) |
| 78 return; |
| 79 |
| 80 if (load_result_ == SUCCESS) |
| 81 client_->OnIconLoadSuccess(icon_); |
| 82 else |
| 83 client_->OnIconLoadFailure(); |
| 84 } |
| 85 |
| 86 void KioskAppIconLoader::ReportResultOnUIThread() { |
| 87 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 88 |
| 89 NotifyClient(); |
| 90 delete this; |
| 91 } |
| 92 |
| 93 } // namespace chromeos |
OLD | NEW |