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_path.h" |
| 11 #include "base/files/file_util.h" |
| 12 #include "base/threading/sequenced_worker_pool.h" |
| 13 #include "chrome/browser/image_decoder.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 |
| 16 using content::BrowserThread; |
| 17 |
| 18 namespace chromeos { |
| 19 |
| 20 class KioskAppIconLoader::IconImageRequest : public ImageDecoder::ImageRequest { |
| 21 public: |
| 22 IconImageRequest(const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 23 KioskAppIconLoader* icon_loader) |
| 24 : ImageRequest(task_runner), icon_loader_(icon_loader) {} |
| 25 |
| 26 void OnImageDecoded(const SkBitmap& decoded_image) override { |
| 27 icon_loader_->icon_ = gfx::ImageSkia::CreateFrom1xBitmap(decoded_image); |
| 28 icon_loader_->icon_.MakeThreadSafe(); |
| 29 icon_loader_->ReportResultOnBlockingPool(SUCCESS); |
| 30 delete this; |
| 31 } |
| 32 |
| 33 void OnDecodeImageFailed() override { |
| 34 icon_loader_->ReportResultOnBlockingPool(FAILED_TO_DECODE); |
| 35 delete this; |
| 36 } |
| 37 |
| 38 private: |
| 39 ~IconImageRequest() override = default; |
| 40 KioskAppIconLoader* const icon_loader_; |
| 41 }; |
| 42 |
| 43 KioskAppIconLoader::KioskAppIconLoader(Delegate* delegate) |
| 44 : delegate_(delegate), weak_factory_(this) {} |
| 45 |
| 46 KioskAppIconLoader::~KioskAppIconLoader() = default; |
| 47 |
| 48 void KioskAppIconLoader::Start(const base::FilePath& icon_path) { |
| 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 weak_factory_.GetWeakPtr(), icon_path)); |
| 56 } |
| 57 |
| 58 void KioskAppIconLoader::LoadOnBlockingPool(const base::FilePath& icon_path) { |
| 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 |
| 67 // IconImageRequest will delete itself on completion of ImageDecoder callback. |
| 68 IconImageRequest* image_request = new IconImageRequest(task_runner_, this); |
| 69 ImageDecoder::Start(image_request, |
| 70 std::vector<uint8_t>(data.begin(), data.end())); |
| 71 } |
| 72 |
| 73 void KioskAppIconLoader::ReportResultOnBlockingPool(LoadResult result) { |
| 74 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 75 |
| 76 BrowserThread::PostTask( |
| 77 BrowserThread::UI, FROM_HERE, |
| 78 base::Bind(&KioskAppIconLoader::ReportResultOnUIThread, |
| 79 weak_factory_.GetWeakPtr(), result)); |
| 80 } |
| 81 |
| 82 void KioskAppIconLoader::NotifyDelegate(LoadResult result) { |
| 83 if (!delegate_) |
| 84 return; |
| 85 |
| 86 if (result == SUCCESS) |
| 87 delegate_->OnIconLoadSuccess(icon_); |
| 88 else |
| 89 delegate_->OnIconLoadFailure(); |
| 90 } |
| 91 |
| 92 void KioskAppIconLoader::ReportResultOnUIThread(LoadResult result) { |
| 93 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 94 |
| 95 NotifyDelegate(result); |
| 96 delete this; |
| 97 } |
| 98 |
| 99 } // namespace chromeos |
OLD | NEW |