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

Side by Side Diff: chrome/browser/chromeos/app_mode/arc/arc_kiosk_app_data.cc

Issue 2778053002: Fetch ARC Kiosk app name and icon from Android side. (Closed)
Patch Set: cleanup 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/arc/arc_kiosk_app_data.h"
6
7 #include <utility>
8
9 #include "base/files/file_util.h"
10 #include "base/path_service.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 "chrome/browser/chromeos/app_mode/arc/arc_kiosk_app_manager.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "components/prefs/pref_service.h"
17 #include "components/prefs/scoped_user_pref_update.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "ui/gfx/codec/png_codec.h"
20
21 namespace chromeos {
22
23 namespace {
24 // Keys for local state data.
25 constexpr char kKeyName[] = "name";
26 constexpr char kKeyIcon[] = "icon";
27
28 constexpr char kIconFileExtension[] = ".png";
29 constexpr char kIconCacheDir[] = "arc-kiosk/icon";
30
31 // Save |raw_icon| for given |app_id|.
32 void SaveIconToLocalOnBlockingPool(
33 const base::FilePath& icon_path,
34 scoped_refptr<base::RefCountedString> raw_icon) {
35 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
36
37 base::FilePath dir = icon_path.DirName();
khmel 2017/03/29 17:51:22 nit: const
Sergey Poromov 2017/03/30 15:48:36 Done.
38 if (!base::PathExists(dir))
39 CHECK(base::CreateDirectory(dir));
40
41 CHECK_EQ(
42 static_cast<int>(raw_icon->size()),
43 base::WriteFile(icon_path, raw_icon->data().c_str(), raw_icon->size()));
44 }
45
46 } // namespace
47
48 ArcKioskAppData::ArcKioskAppData(const std::string& app_id,
49 const AccountId& account_id,
50 const std::string& name)
51 : app_id_(app_id), account_id_(account_id), name_(name) {}
52
53 ArcKioskAppData::~ArcKioskAppData() = default;
54
55 bool ArcKioskAppData::operator==(const std::string& app_id) const {
56 return this->app_id_ == app_id;
57 }
58
59 bool ArcKioskAppData::LoadFromCache() {
60 const std::string app_key =
61 std::string(ArcKioskAppManager::kKeyApps) + '.' + app_id_;
62 const std::string name_key = app_key + '.' + kKeyName;
63 const std::string icon_path_key = app_key + '.' + kKeyIcon;
64
65 PrefService* local_state = g_browser_process->local_state();
66 const base::DictionaryValue* dict =
67 local_state->GetDictionary(ArcKioskAppManager::kArcKioskDictionaryName);
68
69 std::string icon_path_string;
70 if (!dict->GetString(name_key, &name_) ||
71 !dict->GetString(icon_path_key, &icon_path_string)) {
72 return false;
73 }
74 icon_path_ = base::FilePath(icon_path_string);
75
76 // KioskAppIconLoader deletes itself when done.
77 (new KioskAppIconLoader(AsWeakPtr()))->Start(icon_path_);
78 return true;
79 }
80
81 void ArcKioskAppData::SetCache(const std::string& name,
82 const gfx::ImageSkia& icon) {
83 DCHECK(!name.empty());
84 DCHECK(!icon.isNull());
85 name_ = name;
86 icon_ = icon;
87
88 std::vector<unsigned char> image_data;
89 CHECK(gfx::PNGCodec::EncodeBGRASkBitmap(*icon.bitmap(), false, &image_data));
90 scoped_refptr<base::RefCountedString> raw_icon(new base::RefCountedString);
91 raw_icon->data().assign(image_data.begin(), image_data.end());
92
93 base::FilePath user_data_dir;
94 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
95 base::FilePath cache_dir = user_data_dir.AppendASCII(kIconCacheDir);
96 base::FilePath icon_path =
97 cache_dir.AppendASCII(app_id_).AddExtension(kIconFileExtension);
98 content::BrowserThread::GetBlockingPool()->PostTask(
99 FROM_HERE,
100 base::Bind(&SaveIconToLocalOnBlockingPool, icon_path, raw_icon));
101
102 icon_path_ = icon_path;
103
104 const std::string app_key =
105 std::string(ArcKioskAppManager::kKeyApps) + '.' + app_id_;
106 const std::string name_key = app_key + '.' + kKeyName;
107 const std::string icon_path_key = app_key + '.' + kKeyIcon;
108
109 PrefService* local_state = g_browser_process->local_state();
110 DictionaryPrefUpdate dict_update(local_state,
111 ArcKioskAppManager::kArcKioskDictionaryName);
112 dict_update->SetString(name_key, name);
113 dict_update->SetString(icon_path_key, icon_path.value());
114 }
115
116 void ArcKioskAppData::ClearCache() {
117 PrefService* local_state = g_browser_process->local_state();
118
119 DictionaryPrefUpdate dict_update(local_state,
120 ArcKioskAppManager::kArcKioskDictionaryName);
121
122 std::string app_key =
123 std::string(ArcKioskAppManager::kKeyApps) + '.' + app_id_;
124 dict_update->Remove(app_key, NULL);
125
126 if (!icon_path_.empty()) {
127 base::PostTaskWithTraits(
128 FROM_HERE,
129 base::TaskTraits().MayBlock().WithPriority(
130 base::TaskPriority::BACKGROUND),
131 base::Bind(base::IgnoreResult(&base::DeleteFile), icon_path_, false));
132 }
133 }
134
135 void ArcKioskAppData::OnIconLoadSuccess(const gfx::ImageSkia& icon) {
136 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
137 icon_ = icon;
138 }
139
140 void ArcKioskAppData::OnIconLoadFailure() {
141 LOG(ERROR) << "Icon Load Failure";
142 // Do nothing
143 }
144
145 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698