Chromium Code Reviews| 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* 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.
| |
| 41 }; | |
| 42 | |
| 43 KioskAppIconLoader::KioskAppIconLoader(const base::WeakPtr<Delegate>& delegate) | |
| 44 : delegate_(delegate) {} | |
| 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 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
| |
| 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 raw_icon_ = base::RefCountedString::TakeString(&data); | |
| 67 | |
| 68 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.
| |
| 69 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
| |
| 70 } | |
| 71 | |
| 72 void KioskAppIconLoader::ReportResultOnBlockingPool(LoadResult result) { | |
| 73 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 74 | |
| 75 BrowserThread::PostTask( | |
| 76 BrowserThread::UI, FROM_HERE, | |
| 77 base::Bind(&KioskAppIconLoader::ReportResultOnUIThread, | |
| 78 base::Unretained(this), result)); | |
| 79 } | |
| 80 | |
| 81 void KioskAppIconLoader::NotifyDelegate(LoadResult result) { | |
| 82 if (!delegate_) | |
| 83 return; | |
| 84 | |
| 85 if (result == SUCCESS) | |
| 86 delegate_->OnIconLoadSuccess(icon_); | |
| 87 else | |
| 88 delegate_->OnIconLoadFailure(); | |
| 89 } | |
| 90 | |
| 91 void KioskAppIconLoader::ReportResultOnUIThread(LoadResult result) { | |
| 92 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 93 | |
| 94 NotifyDelegate(result); | |
| 95 delete this; | |
| 96 } | |
| 97 | |
| 98 } // namespace chromeos | |
| OLD | NEW |