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, | |
Luis Héctor Chávez
2017/04/03 22:58:55
Sorry, should have been a bit clearer here. There'
Sergey Poromov
2017/04/06 10:29:58
Done. Please check that I did it right.
Also, I ca
| |
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 (result == SUCCESS) | |
84 delegate_->OnIconLoadSuccess(icon_); | |
85 else | |
86 delegate_->OnIconLoadFailure(); | |
87 } | |
88 | |
89 void KioskAppIconLoader::ReportResultOnUIThread(LoadResult result) { | |
90 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
91 | |
92 NotifyDelegate(result); | |
93 } | |
94 | |
95 } // namespace chromeos | |
OLD | NEW |