Index: chrome/browser/chromeos/app_mode/kiosk_app_icon_loader.cc |
diff --git a/chrome/browser/chromeos/app_mode/kiosk_app_icon_loader.cc b/chrome/browser/chromeos/app_mode/kiosk_app_icon_loader.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f0bf2409e5107626b0623f30583c90e77ed14150 |
--- /dev/null |
+++ b/chrome/browser/chromeos/app_mode/kiosk_app_icon_loader.cc |
@@ -0,0 +1,98 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/chromeos/app_mode/kiosk_app_icon_loader.h" |
+ |
+#include <vector> |
+ |
+#include "base/bind.h" |
+#include "base/files/file_path.h" |
+#include "base/files/file_util.h" |
+#include "base/threading/sequenced_worker_pool.h" |
+#include "chrome/browser/image_decoder.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+using content::BrowserThread; |
+ |
+namespace chromeos { |
+ |
+class KioskAppIconLoader::IconImageRequest : public ImageDecoder::ImageRequest { |
+ public: |
+ IconImageRequest(const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
+ KioskAppIconLoader* icon_loader) |
+ : ImageRequest(task_runner), icon_loader_(icon_loader) {} |
+ |
+ void OnImageDecoded(const SkBitmap& decoded_image) override { |
+ icon_loader_->icon_ = gfx::ImageSkia::CreateFrom1xBitmap(decoded_image); |
+ icon_loader_->icon_.MakeThreadSafe(); |
+ icon_loader_->ReportResultOnBlockingPool(SUCCESS); |
+ delete this; |
+ } |
+ |
+ void OnDecodeImageFailed() override { |
+ icon_loader_->ReportResultOnBlockingPool(FAILED_TO_DECODE); |
+ delete this; |
+ } |
+ |
+ private: |
+ ~IconImageRequest() override = default; |
+ KioskAppIconLoader* icon_loader_; |
Luis Héctor Chávez
2017/03/30 16:49:54
KioskAppIconLoader* const icon_loader_;
Sergey Poromov
2017/03/30 18:22:08
Done.
|
+}; |
+ |
+KioskAppIconLoader::KioskAppIconLoader(const base::WeakPtr<Delegate>& delegate) |
+ : delegate_(delegate) {} |
+ |
+KioskAppIconLoader::~KioskAppIconLoader() = default; |
+ |
+void KioskAppIconLoader::Start(const base::FilePath& icon_path) { |
+ base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
+ base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); |
+ task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( |
+ token, base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
+ task_runner_->PostTask(FROM_HERE, |
+ base::Bind(&KioskAppIconLoader::LoadOnBlockingPool, |
+ base::Unretained(this), icon_path)); |
Luis Héctor Chávez
2017/03/30 16:49:54
base::Unretained(this) is unsafe in all this class
Sergey Poromov
2017/03/30 18:22:08
KioskAppIconLoader is self-owned and as I can see
Luis Héctor Chávez
2017/03/30 19:52:27
Looked at the impl of this particular flavor of ta
xiyuan
2017/03/30 22:23:56
Not sure why deleting |task_runner_| is relevant h
Sergey Poromov
2017/03/31 12:56:24
I agree that this pattern is suitable here, as Kio
|
+} |
+ |
+void KioskAppIconLoader::LoadOnBlockingPool(const base::FilePath& icon_path) { |
+ DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
+ |
+ std::string data; |
+ if (!base::ReadFileToString(base::FilePath(icon_path), &data)) { |
+ ReportResultOnBlockingPool(FAILED_TO_LOAD); |
+ return; |
+ } |
+ raw_icon_ = base::RefCountedString::TakeString(&data); |
+ |
+ IconImageRequest* image_request = new IconImageRequest(task_runner_, this); |
Luis Héctor Chávez
2017/03/30 16:49:54
Please note that you are transferring ownership of
Sergey Poromov
2017/03/30 18:22:08
Done.
|
+ ImageDecoder::Start(image_request, raw_icon_->data()); |
Luis Héctor Chávez
2017/03/30 16:49:54
Do you need the refcounted string? Why not transfe
Sergey Poromov
2017/03/30 18:22:07
Done for first comment.
I think refactoring to us
|
+} |
+ |
+void KioskAppIconLoader::ReportResultOnBlockingPool(LoadResult result) { |
+ DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
+ |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind(&KioskAppIconLoader::ReportResultOnUIThread, |
+ base::Unretained(this), result)); |
+} |
+ |
+void KioskAppIconLoader::NotifyDelegate(LoadResult result) { |
+ if (!delegate_) |
+ return; |
+ |
+ if (result == SUCCESS) |
+ delegate_->OnIconLoadSuccess(icon_); |
+ else |
+ delegate_->OnIconLoadFailure(); |
+} |
+ |
+void KioskAppIconLoader::ReportResultOnUIThread(LoadResult result) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ |
+ NotifyDelegate(result); |
+ delete this; |
+} |
+ |
+} // namespace chromeos |