Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(559)

Side by Side Diff: chrome/browser/chromeos/app_mode/kiosk_app_data_base.cc

Issue 2778053002: Fetch ARC Kiosk app name and icon from Android side. (Closed)
Patch Set: move weak_ptr from KioskAppDataBase to KioskAppIconLoader Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_data_base.h"
6
7 #include <utility>
8
9 #include "base/files/file_util.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/task_scheduler/post_task.h"
12 #include "base/threading/sequenced_worker_pool.h"
13 #include "chrome/browser/browser_process.h"
14 #include "components/prefs/pref_service.h"
15 #include "components/prefs/scoped_user_pref_update.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "ui/gfx/codec/png_codec.h"
18
19 using content::BrowserThread;
20
21 namespace chromeos {
22
23 namespace {
24
25 // Keys for local state data.
26 constexpr char kKeyName[] = "name";
27 constexpr char kKeyIcon[] = "icon";
28
29 // Icon file extension.
30 constexpr char kIconFileExtension[] = ".png";
31
32 // Save |raw_icon| for given |app_id|.
33 void SaveIconToLocalOnBlockingPool(const base::FilePath& icon_path,
34 std::vector<unsigned char> image_data) {
35 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
36
37 const base::FilePath dir = icon_path.DirName();
38 if (!base::PathExists(dir) || !base::CreateDirectory(dir)) {
khmel 2017/03/31 14:37:13 Would it be !base::PathExists(dir) && !base::Creat
Sergey Poromov 2017/03/31 15:08:56 Sure! Thank you! My bad...
xiyuan 2017/03/31 15:10:02 Good catch. It should be &&.
39 LOG(ERROR) << "Failed to create directory to store kiosk icons";
40 return;
41 }
42
43 int wrote = base::WriteFile(
khmel 2017/03/31 14:37:13 tiny nit: here ant at L102, L107 you may use const
Sergey Poromov 2017/03/31 15:08:56 Done.
44 icon_path, reinterpret_cast<char*>(image_data.data()), image_data.size());
45 if (wrote != static_cast<int>(image_data.size())) {
46 LOG(ERROR) << "Failed to write kiosk icon file";
47 }
48 }
49
50 } // namespace
51
52 // static
53 const char KioskAppDataBase::kKeyApps[] = "apps";
54
55 KioskAppDataBase::KioskAppDataBase(const std::string& dictionary_name,
56 const std::string& app_id,
57 const AccountId& account_id)
58 : dictionary_name_(dictionary_name),
59 app_id_(app_id),
60 account_id_(account_id) {}
61
62 KioskAppDataBase::~KioskAppDataBase() = default;
63
64 void KioskAppDataBase::SaveToDictionary(DictionaryPrefUpdate& dict_update) {
65 DCHECK_CURRENTLY_ON(BrowserThread::UI);
66 const std::string app_key = std::string(kKeyApps) + '.' + app_id_;
67 const std::string name_key = app_key + '.' + kKeyName;
68 const std::string icon_path_key = app_key + '.' + kKeyIcon;
69
70 dict_update->SetString(name_key, name_);
71 dict_update->SetString(icon_path_key, icon_path_.value());
72 }
73
74 bool KioskAppDataBase::LoadFromDictionary(const base::DictionaryValue* dict) {
75 DCHECK_CURRENTLY_ON(BrowserThread::UI);
76 const std::string app_key =
77 std::string(KioskAppDataBase::kKeyApps) + '.' + app_id_;
78 const std::string name_key = app_key + '.' + kKeyName;
79 const std::string icon_path_key = app_key + '.' + kKeyIcon;
80
81 std::string icon_path_string;
82 if (!dict->GetString(name_key, &name_) ||
83 !dict->GetString(icon_path_key, &icon_path_string)) {
84 return false;
85 }
86 icon_path_ = base::FilePath(icon_path_string);
87
88 kiosk_app_icon_loader_ = base::MakeUnique<KioskAppIconLoader>(this);
89 kiosk_app_icon_loader_->Start(icon_path_);
90 return true;
91 }
92
93 void KioskAppDataBase::SaveIcon(const SkBitmap& icon,
94 const base::FilePath& cache_dir) {
95 DCHECK_CURRENTLY_ON(BrowserThread::UI);
96 std::vector<unsigned char> image_data;
97 if (!gfx::PNGCodec::EncodeBGRASkBitmap(icon, false, &image_data)) {
98 LOG(ERROR) << "Failed to encode kiosk icon";
99 return;
100 }
101
102 base::FilePath icon_path =
103 cache_dir.AppendASCII(app_id_).AddExtension(kIconFileExtension);
104 BrowserThread::GetBlockingPool()->PostTask(
105 FROM_HERE, base::Bind(&SaveIconToLocalOnBlockingPool, icon_path,
106 base::Passed(std::move(image_data))));
107
108 icon_path_ = icon_path;
109 }
110
111 void KioskAppDataBase::ClearCache() {
112 DCHECK_CURRENTLY_ON(BrowserThread::UI);
113 PrefService* local_state = g_browser_process->local_state();
114
115 DictionaryPrefUpdate dict_update(local_state, dictionary_name());
116
117 std::string app_key = std::string(KioskAppDataBase::kKeyApps) + '.' + app_id_;
118 dict_update->Remove(app_key, nullptr);
119
120 if (!icon_path_.empty()) {
121 base::PostTaskWithTraits(
122 FROM_HERE,
123 base::TaskTraits().MayBlock().WithPriority(
124 base::TaskPriority::BACKGROUND),
125 base::Bind(base::IgnoreResult(&base::DeleteFile), icon_path_, false));
126 }
127 }
128
129 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698