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

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

Issue 2778053002: Fetch ARC Kiosk app name and icon from Android side. (Closed)
Patch Set: some nits 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <chrome/browser/chromeos/app_mode/arc/arc_kiosk_app_manager.h> 5 #include <chrome/browser/chromeos/app_mode/arc/arc_kiosk_app_manager.h>
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/barrier_closure.h" 10 #include "base/barrier_closure.h"
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/memory/ptr_util.h"
15 #include "base/values.h" 16 #include "base/values.h"
16 #include "chrome/browser/browser_process.h" 17 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/lifetime/application_lifetime.h" 18 #include "chrome/browser/lifetime/application_lifetime.h"
18 #include "chromeos/cryptohome/async_method_caller.h" 19 #include "chromeos/cryptohome/async_method_caller.h"
19 #include "chromeos/cryptohome/cryptohome_parameters.h" 20 #include "chromeos/cryptohome/cryptohome_parameters.h"
20 #include "chromeos/dbus/cryptohome_client.h" 21 #include "chromeos/dbus/cryptohome_client.h"
21 #include "chromeos/dbus/dbus_thread_manager.h" 22 #include "chromeos/dbus/dbus_thread_manager.h"
22 #include "chromeos/settings/cros_settings_names.h" 23 #include "chromeos/settings/cros_settings_names.h"
23 #include "components/arc/arc_util.h" 24 #include "components/arc/arc_util.h"
24 #include "components/prefs/pref_registry_simple.h" 25 #include "components/prefs/pref_registry_simple.h"
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 base::Closure())); 90 base::Closure()));
90 } 91 }
91 } 92 }
92 93
93 // This class is owned by ChromeBrowserMainPartsChromeos. 94 // This class is owned by ChromeBrowserMainPartsChromeos.
94 static ArcKioskAppManager* g_arc_kiosk_app_manager = nullptr; 95 static ArcKioskAppManager* g_arc_kiosk_app_manager = nullptr;
95 96
96 } // namespace 97 } // namespace
97 98
98 // static 99 // static
100 const char ArcKioskAppManager::kArcKioskDictionaryName[] = "arc-kiosk";
101
102 // static
99 void ArcKioskAppManager::RegisterPrefs(PrefRegistrySimple* registry) { 103 void ArcKioskAppManager::RegisterPrefs(PrefRegistrySimple* registry) {
104 registry->RegisterDictionaryPref(kArcKioskDictionaryName);
100 registry->RegisterListPref(kArcKioskUsersToRemove); 105 registry->RegisterListPref(kArcKioskUsersToRemove);
101 } 106 }
102 107
103 // static 108 // static
104 void ArcKioskAppManager::RemoveObsoleteCryptohomes() { 109 void ArcKioskAppManager::RemoveObsoleteCryptohomes() {
105 chromeos::CryptohomeClient* const client = 110 chromeos::CryptohomeClient* const client =
106 chromeos::DBusThreadManager::Get()->GetCryptohomeClient(); 111 chromeos::DBusThreadManager::Get()->GetCryptohomeClient();
107 client->WaitForServiceToBeAvailable( 112 client->WaitForServiceToBeAvailable(
108 base::Bind(&PerformDelayedCryptohomeRemovals)); 113 base::Bind(&PerformDelayedCryptohomeRemovals));
109 } 114 }
110 115
111 ArcKioskAppManager::ArcKioskApp::ArcKioskApp(const ArcKioskApp& other) =
112 default;
113
114 ArcKioskAppManager::ArcKioskApp::ArcKioskApp(
115 const policy::ArcKioskAppBasicInfo& app_info,
116 const AccountId& account_id,
117 const std::string& name)
118 : app_info_(app_info), account_id_(account_id), name_(name) {}
119
120 ArcKioskAppManager::ArcKioskApp::~ArcKioskApp() = default;
121
122 bool ArcKioskAppManager::ArcKioskApp::operator==(
123 const policy::ArcKioskAppBasicInfo& app_info) const {
124 return this->app_info_ == app_info;
125 }
126
127 // static 116 // static
128 ArcKioskAppManager* ArcKioskAppManager::Get() { 117 ArcKioskAppManager* ArcKioskAppManager::Get() {
129 return g_arc_kiosk_app_manager; 118 return g_arc_kiosk_app_manager;
130 } 119 }
131 120
132 ArcKioskAppManager::ArcKioskAppManager() 121 ArcKioskAppManager::ArcKioskAppManager()
133 : auto_launch_account_id_(EmptyAccountId()) { 122 : auto_launch_account_id_(EmptyAccountId()) {
134 DCHECK(!g_arc_kiosk_app_manager); // Only one instance is allowed. 123 DCHECK(!g_arc_kiosk_app_manager); // Only one instance is allowed.
135 UpdateApps(); 124 UpdateApps();
136 local_accounts_subscription_ = CrosSettings::Get()->AddSettingsObserver( 125 local_accounts_subscription_ = CrosSettings::Get()->AddSettingsObserver(
(...skipping 10 matching lines...) Expand all
147 local_accounts_subscription_.reset(); 136 local_accounts_subscription_.reset();
148 local_account_auto_login_id_subscription_.reset(); 137 local_account_auto_login_id_subscription_.reset();
149 apps_.clear(); 138 apps_.clear();
150 g_arc_kiosk_app_manager = nullptr; 139 g_arc_kiosk_app_manager = nullptr;
151 } 140 }
152 141
153 const AccountId& ArcKioskAppManager::GetAutoLaunchAccountId() const { 142 const AccountId& ArcKioskAppManager::GetAutoLaunchAccountId() const {
154 return auto_launch_account_id_; 143 return auto_launch_account_id_;
155 } 144 }
156 145
157 const ArcKioskAppManager::ArcKioskApp* ArcKioskAppManager::GetAppByAccountId( 146 const ArcKioskAppData* ArcKioskAppManager::GetAppByAccountId(
158 const AccountId& account_id) { 147 const AccountId& account_id) {
159 for (auto& app : GetAllApps()) 148 for (auto& app : apps_) {
160 if (app.account_id() == account_id) 149 if (app->account_id() == account_id)
161 return &app; 150 return app.get();
151 }
162 return nullptr; 152 return nullptr;
163 } 153 }
164 154
155 void ArcKioskAppManager::GetAllApps(Apps* apps) const {
156 apps->clear();
157 apps->reserve(apps_.size());
158 for (auto& app : apps_)
159 apps->push_back(app.get());
160 }
161
162 void ArcKioskAppManager::UpdateNameAndIcon(const std::string& app_id,
163 const std::string& name,
164 const gfx::ImageSkia& icon) {
165 for (auto& app : apps_) {
166 if (app->app_id() == app_id) {
167 app->SetCache(name, icon);
168 return;
169 }
170 }
171 }
172
165 void ArcKioskAppManager::AddObserver(ArcKioskAppManagerObserver* observer) { 173 void ArcKioskAppManager::AddObserver(ArcKioskAppManagerObserver* observer) {
166 observers_.AddObserver(observer); 174 observers_.AddObserver(observer);
167 } 175 }
168 176
169 void ArcKioskAppManager::RemoveObserver(ArcKioskAppManagerObserver* observer) { 177 void ArcKioskAppManager::RemoveObserver(ArcKioskAppManagerObserver* observer) {
170 observers_.RemoveObserver(observer); 178 observers_.RemoveObserver(observer);
171 } 179 }
172 180
173 void ArcKioskAppManager::UpdateApps() { 181 void ArcKioskAppManager::UpdateApps() {
174 // Do not populate ARC kiosk apps if ARC kiosk apps can't be run on the 182 // Do not populate ARC kiosk apps if ARC kiosk apps can't be run on the
175 // device. 183 // device.
176 // Apps won't be added to kiosk Apps menu and won't be auto-launched. 184 // Apps won't be added to kiosk Apps menu and won't be auto-launched.
177 if (!arc::IsArcKioskAvailable()) { 185 if (!arc::IsArcKioskAvailable()) {
178 VLOG(1) << "Device doesn't support ARC kiosk"; 186 VLOG(1) << "Device doesn't support ARC kiosk";
179 return; 187 return;
180 } 188 }
181 189
182 // Store current apps. We will compare old and new apps to determine which 190 // Store current apps. We will compare old and new apps to determine which
183 // apps are new, and which were deleted. 191 // apps are new, and which were deleted.
184 ArcKioskApps old_apps(std::move(apps_)); 192 std::map<std::string, std::unique_ptr<ArcKioskAppData>> old_apps;
185 193 for (auto& app : apps_)
194 old_apps[app->app_id()] = std::move(app);
195 apps_.clear();
186 auto_launch_account_id_.clear(); 196 auto_launch_account_id_.clear();
187 auto_launched_with_zero_delay_ = false; 197 auto_launched_with_zero_delay_ = false;
188 std::string auto_login_account_id_from_settings; 198 std::string auto_login_account_id_from_settings;
189 CrosSettings::Get()->GetString(kAccountsPrefDeviceLocalAccountAutoLoginId, 199 CrosSettings::Get()->GetString(kAccountsPrefDeviceLocalAccountAutoLoginId,
190 &auto_login_account_id_from_settings); 200 &auto_login_account_id_from_settings);
191 201
192 // Re-populates |apps_| and reuses existing apps when possible. 202 // Re-populates |apps_| and reuses existing apps when possible.
193 const std::vector<policy::DeviceLocalAccount> device_local_accounts = 203 const std::vector<policy::DeviceLocalAccount> device_local_accounts =
194 policy::GetDeviceLocalAccounts(CrosSettings::Get()); 204 policy::GetDeviceLocalAccounts(CrosSettings::Get());
195 for (auto account : device_local_accounts) { 205 for (auto account : device_local_accounts) {
196 if (account.type != policy::DeviceLocalAccount::TYPE_ARC_KIOSK_APP) 206 if (account.type != policy::DeviceLocalAccount::TYPE_ARC_KIOSK_APP)
197 continue; 207 continue;
198 208
199 const AccountId account_id(AccountId::FromUserEmail(account.user_id)); 209 const AccountId account_id(AccountId::FromUserEmail(account.user_id));
200 210
201 if (account.account_id == auto_login_account_id_from_settings) { 211 if (account.account_id == auto_login_account_id_from_settings) {
202 auto_launch_account_id_ = account_id; 212 auto_launch_account_id_ = account_id;
203 int auto_launch_delay = 0; 213 int auto_launch_delay = 0;
204 CrosSettings::Get()->GetInteger( 214 CrosSettings::Get()->GetInteger(
205 kAccountsPrefDeviceLocalAccountAutoLoginDelay, &auto_launch_delay); 215 kAccountsPrefDeviceLocalAccountAutoLoginDelay, &auto_launch_delay);
206 auto_launched_with_zero_delay_ = auto_launch_delay == 0; 216 auto_launched_with_zero_delay_ = auto_launch_delay == 0;
207 } 217 }
208 218
209 auto old_it = 219 // Apps are keyed by package name. http://crbug.com/665904
210 std::find(old_apps.begin(), old_apps.end(), account.arc_kiosk_app_info); 220 auto old_it = old_apps.find(account.arc_kiosk_app_info.package_name());
211 if (old_it != old_apps.end()) { 221 if (old_it != old_apps.end()) {
212 apps_.push_back(std::move(*old_it)); 222 apps_.push_back(std::move(old_it->second));
213 old_apps.erase(old_it); 223 old_apps.erase(old_it);
214 } else { 224 } else {
215 // Use package name when display name is not specified. 225 // Use package name when display name is not specified.
216 std::string name = account.arc_kiosk_app_info.package_name(); 226 std::string name = account.arc_kiosk_app_info.package_name();
217 if (!account.arc_kiosk_app_info.display_name().empty()) 227 if (!account.arc_kiosk_app_info.display_name().empty())
218 name = account.arc_kiosk_app_info.display_name(); 228 name = account.arc_kiosk_app_info.display_name();
219 apps_.push_back( 229 apps_.push_back(base::MakeUnique<ArcKioskAppData>(
220 ArcKioskApp(account.arc_kiosk_app_info, account_id, name)); 230 account.arc_kiosk_app_info.package_name(), account_id, name));
231 apps_.back()->LoadFromCache();
221 } 232 }
222 CancelDelayedCryptohomeRemoval(cryptohome::Identification(account_id)); 233 CancelDelayedCryptohomeRemoval(cryptohome::Identification(account_id));
223 } 234 }
224 235
225 ClearRemovedApps(old_apps); 236 ClearRemovedApps(old_apps);
226 237
227 for (auto& observer : observers_) { 238 for (auto& observer : observers_) {
228 observer.OnArcKioskAppsChanged(); 239 observer.OnArcKioskAppsChanged();
229 } 240 }
230 } 241 }
231 242
232 void ArcKioskAppManager::ClearRemovedApps( 243 void ArcKioskAppManager::ClearRemovedApps(
233 const std::vector<ArcKioskApp>& old_apps) { 244 const std::map<std::string, std::unique_ptr<ArcKioskAppData>>& old_apps) {
234 // Check if currently active user must be deleted. 245 // Check if currently active user must be deleted.
235 bool active_user_to_be_deleted = false; 246 bool active_user_to_be_deleted = false;
236 const user_manager::User* active_user = 247 const user_manager::User* active_user =
237 user_manager::UserManager::Get()->GetActiveUser(); 248 user_manager::UserManager::Get()->GetActiveUser();
238 if (active_user) { 249 if (active_user) {
239 const AccountId active_account_id = active_user->GetAccountId(); 250 const AccountId active_account_id = active_user->GetAccountId();
240 for (const auto& it : old_apps) { 251 for (const auto& it : old_apps) {
241 if (it.account_id() == active_account_id) { 252 if (it.second->account_id() == active_account_id) {
242 active_user_to_be_deleted = true; 253 active_user_to_be_deleted = true;
243 break; 254 break;
244 } 255 }
245 } 256 }
246 } 257 }
247 258
248 // Remove cryptohome 259 // Remove cryptohome
249 for (auto& entry : old_apps) { 260 for (auto& entry : old_apps) {
250 const cryptohome::Identification cryptohome_id(entry.account_id()); 261 entry.second->ClearCache();
262 const cryptohome::Identification cryptohome_id(entry.second->account_id());
251 if (active_user_to_be_deleted) { 263 if (active_user_to_be_deleted) {
252 // Schedule cryptohome removal after active user logout. 264 // Schedule cryptohome removal after active user logout.
253 ScheduleDelayedCryptohomeRemoval(cryptohome_id); 265 ScheduleDelayedCryptohomeRemoval(cryptohome_id);
254 } else { 266 } else {
255 cryptohome::AsyncMethodCaller::GetInstance()->AsyncRemove( 267 cryptohome::AsyncMethodCaller::GetInstance()->AsyncRemove(
256 cryptohome_id, base::Bind(&OnRemoveAppCryptohomeComplete, 268 cryptohome_id, base::Bind(&OnRemoveAppCryptohomeComplete,
257 cryptohome_id, base::Closure())); 269 cryptohome_id, base::Closure()));
258 } 270 }
259 } 271 }
260 272
261 if (active_user_to_be_deleted) 273 if (active_user_to_be_deleted)
262 chrome::AttemptUserExit(); 274 chrome::AttemptUserExit();
263 } 275 }
264 276
265 } // namespace chromeos 277 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698