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 IconImageRequest : public ImageDecoder::ImageRequest { | |
21 public: | |
22 IconImageRequest(const scoped_refptr<base::SequencedTaskRunner>& task_runner, | |
23 KioskAppIconLoader::ResultCallback result_callback) | |
24 : ImageRequest(task_runner), result_callback_(result_callback) {} | |
25 | |
26 void OnImageDecoded(const SkBitmap& decoded_image) override { | |
27 gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(decoded_image); | |
28 image.MakeThreadSafe(); | |
29 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
30 base::Bind(result_callback_, image)); | |
31 delete this; | |
32 } | |
33 | |
34 void OnDecodeImageFailed() override { | |
35 LOG(ERROR) << "Failed to decode icon image."; | |
36 BrowserThread::PostTask( | |
37 BrowserThread::UI, FROM_HERE, | |
38 base::Bind(result_callback_, base::Optional<gfx::ImageSkia>())); | |
39 delete this; | |
40 } | |
41 | |
42 private: | |
43 ~IconImageRequest() override = default; | |
44 KioskAppIconLoader::ResultCallback result_callback_; | |
45 }; | |
46 | |
47 void LoadOnBlockingPool( | |
48 const base::FilePath& icon_path, | |
49 scoped_refptr<base::SequencedTaskRunner> callback_task_runner, | |
50 KioskAppIconLoader::ResultCallback result_callback) { | |
51 DCHECK(callback_task_runner->RunsTasksOnCurrentThread()); | |
52 | |
53 std::string data; | |
54 if (!base::ReadFileToString(base::FilePath(icon_path), &data)) { | |
55 LOG(ERROR) << "Failed to read icon file."; | |
56 BrowserThread::PostTask( | |
57 BrowserThread::UI, FROM_HERE, | |
58 base::Bind(result_callback, base::Optional<gfx::ImageSkia>())); | |
59 return; | |
60 } | |
61 | |
62 // IconImageRequest will delete itself on completion of ImageDecoder callback. | |
63 IconImageRequest* image_request = | |
64 new IconImageRequest(callback_task_runner, result_callback); | |
65 ImageDecoder::Start(image_request, | |
66 std::vector<uint8_t>(data.begin(), data.end())); | |
67 } | |
68 | |
69 KioskAppIconLoader::KioskAppIconLoader(Delegate* delegate) | |
70 : delegate_(delegate), weak_factory_(this) {} | |
71 | |
72 KioskAppIconLoader::~KioskAppIconLoader() = default; | |
73 | |
74 void KioskAppIconLoader::Start(const base::FilePath& icon_path) { | |
75 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | |
76 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); | |
77 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
78 token, base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
79 task_runner_->PostTask( | |
80 FROM_HERE, | |
81 base::Bind(&LoadOnBlockingPool, icon_path, task_runner_, | |
82 base::Bind(&KioskAppIconLoader::OnImageDecodingFinished, | |
83 weak_factory_.GetWeakPtr()))); | |
84 } | |
85 | |
86 void KioskAppIconLoader::OnImageDecodingFinished( | |
87 base::Optional<gfx::ImageSkia> result) { | |
88 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
89 | |
90 if (result.has_value()) { | |
91 delegate_->OnIconLoadSuccess(result.value()); | |
92 } else { | |
93 delegate_->OnIconLoadFailure(); | |
94 } | |
95 } | |
96 | |
97 } // namespace chromeos | |
OLD | NEW |