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 "chrome/browser/image_decoder.h" |
| 13 #include "content/public/browser/browser_thread.h" |
| 14 |
| 15 using content::BrowserThread; |
| 16 |
| 17 namespace chromeos { |
| 18 |
| 19 class KioskAppIconLoader::IconImageRequest : public ImageDecoder::ImageRequest { |
| 20 public: |
| 21 IconImageRequest(const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 22 KioskAppIconLoader* icon_loader) |
| 23 : ImageRequest(task_runner), icon_loader_(icon_loader) {} |
| 24 |
| 25 void OnImageDecoded(const SkBitmap& decoded_image) override { |
| 26 icon_loader_->icon_ = gfx::ImageSkia::CreateFrom1xBitmap(decoded_image); |
| 27 icon_loader_->icon_.MakeThreadSafe(); |
| 28 icon_loader_->ReportResultOnBlockingPool(SUCCESS); |
| 29 delete this; |
| 30 } |
| 31 |
| 32 void OnDecodeImageFailed() override { |
| 33 icon_loader_->ReportResultOnBlockingPool(FAILED_TO_DECODE); |
| 34 delete this; |
| 35 } |
| 36 |
| 37 private: |
| 38 ~IconImageRequest() override = default; |
| 39 KioskAppIconLoader* icon_loader_; |
| 40 }; |
| 41 |
| 42 KioskAppIconLoader::KioskAppIconLoader(const base::WeakPtr<Delegate>& client, |
| 43 const base::FilePath& icon_path) |
| 44 : client_(client), icon_path_(icon_path), load_result_(SUCCESS) {} |
| 45 |
| 46 KioskAppIconLoader::~KioskAppIconLoader() = default; |
| 47 |
| 48 void KioskAppIconLoader::Start() { |
| 49 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 50 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); |
| 51 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 52 token, base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 53 task_runner_->PostTask(FROM_HERE, |
| 54 base::Bind(&KioskAppIconLoader::LoadOnBlockingPool, |
| 55 base::Unretained(this))); |
| 56 } |
| 57 |
| 58 void KioskAppIconLoader::LoadOnBlockingPool() { |
| 59 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 60 |
| 61 std::string data; |
| 62 if (!base::ReadFileToString(base::FilePath(icon_path_), &data)) { |
| 63 ReportResultOnBlockingPool(FAILED_TO_LOAD); |
| 64 return; |
| 65 } |
| 66 raw_icon_ = base::RefCountedString::TakeString(&data); |
| 67 |
| 68 IconImageRequest* image_request = new IconImageRequest(task_runner_, this); |
| 69 ImageDecoder::Start(image_request, raw_icon_->data()); |
| 70 } |
| 71 |
| 72 void KioskAppIconLoader::ReportResultOnBlockingPool(LoadResult result) { |
| 73 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 74 |
| 75 load_result_ = result; |
| 76 BrowserThread::PostTask( |
| 77 BrowserThread::UI, FROM_HERE, |
| 78 base::Bind(&KioskAppIconLoader::ReportResultOnUIThread, |
| 79 base::Unretained(this))); |
| 80 } |
| 81 |
| 82 void KioskAppIconLoader::NotifyClient() { |
| 83 if (!client_) |
| 84 return; |
| 85 |
| 86 if (load_result_ == SUCCESS) |
| 87 client_->OnIconLoadSuccess(icon_); |
| 88 else |
| 89 client_->OnIconLoadFailure(); |
| 90 } |
| 91 |
| 92 void KioskAppIconLoader::ReportResultOnUIThread() { |
| 93 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 94 |
| 95 NotifyClient(); |
| 96 delete this; |
| 97 } |
| 98 |
| 99 } // namespace chromeos |
OLD | NEW |