| OLD | NEW |
| 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 Loading... |
| 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 const char ArcKioskAppManager::kKeyApps[] = "apps"; |
| 102 |
| 103 // static |
| 99 void ArcKioskAppManager::RegisterPrefs(PrefRegistrySimple* registry) { | 104 void ArcKioskAppManager::RegisterPrefs(PrefRegistrySimple* registry) { |
| 105 registry->RegisterDictionaryPref(kArcKioskDictionaryName); |
| 100 registry->RegisterListPref(kArcKioskUsersToRemove); | 106 registry->RegisterListPref(kArcKioskUsersToRemove); |
| 101 } | 107 } |
| 102 | 108 |
| 103 // static | 109 // static |
| 104 void ArcKioskAppManager::RemoveObsoleteCryptohomes() { | 110 void ArcKioskAppManager::RemoveObsoleteCryptohomes() { |
| 105 chromeos::CryptohomeClient* const client = | 111 chromeos::CryptohomeClient* const client = |
| 106 chromeos::DBusThreadManager::Get()->GetCryptohomeClient(); | 112 chromeos::DBusThreadManager::Get()->GetCryptohomeClient(); |
| 107 client->WaitForServiceToBeAvailable( | 113 client->WaitForServiceToBeAvailable( |
| 108 base::Bind(&PerformDelayedCryptohomeRemovals)); | 114 base::Bind(&PerformDelayedCryptohomeRemovals)); |
| 109 } | 115 } |
| 110 | 116 |
| 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 | 117 // static |
| 128 ArcKioskAppManager* ArcKioskAppManager::Get() { | 118 ArcKioskAppManager* ArcKioskAppManager::Get() { |
| 129 return g_arc_kiosk_app_manager; | 119 return g_arc_kiosk_app_manager; |
| 130 } | 120 } |
| 131 | 121 |
| 132 ArcKioskAppManager::ArcKioskAppManager() | 122 ArcKioskAppManager::ArcKioskAppManager() |
| 133 : auto_launch_account_id_(EmptyAccountId()) { | 123 : auto_launch_account_id_(EmptyAccountId()) { |
| 134 DCHECK(!g_arc_kiosk_app_manager); // Only one instance is allowed. | 124 DCHECK(!g_arc_kiosk_app_manager); // Only one instance is allowed. |
| 135 UpdateApps(); | 125 UpdateApps(); |
| 136 local_accounts_subscription_ = CrosSettings::Get()->AddSettingsObserver( | 126 local_accounts_subscription_ = CrosSettings::Get()->AddSettingsObserver( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 147 local_accounts_subscription_.reset(); | 137 local_accounts_subscription_.reset(); |
| 148 local_account_auto_login_id_subscription_.reset(); | 138 local_account_auto_login_id_subscription_.reset(); |
| 149 apps_.clear(); | 139 apps_.clear(); |
| 150 g_arc_kiosk_app_manager = nullptr; | 140 g_arc_kiosk_app_manager = nullptr; |
| 151 } | 141 } |
| 152 | 142 |
| 153 const AccountId& ArcKioskAppManager::GetAutoLaunchAccountId() const { | 143 const AccountId& ArcKioskAppManager::GetAutoLaunchAccountId() const { |
| 154 return auto_launch_account_id_; | 144 return auto_launch_account_id_; |
| 155 } | 145 } |
| 156 | 146 |
| 157 const ArcKioskAppManager::ArcKioskApp* ArcKioskAppManager::GetAppByAccountId( | 147 const ArcKioskAppData* ArcKioskAppManager::GetAppByAccountId( |
| 158 const AccountId& account_id) { | 148 const AccountId& account_id) { |
| 159 for (auto& app : GetAllApps()) | 149 for (auto& app : apps_) |
| 160 if (app.account_id() == account_id) | 150 if (app->account_id() == account_id) |
| 161 return &app; | 151 return app.get(); |
| 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 std::string auto_login_account_id_from_settings; | 197 std::string auto_login_account_id_from_settings; |
| 188 CrosSettings::Get()->GetString(kAccountsPrefDeviceLocalAccountAutoLoginId, | 198 CrosSettings::Get()->GetString(kAccountsPrefDeviceLocalAccountAutoLoginId, |
| 189 &auto_login_account_id_from_settings); | 199 &auto_login_account_id_from_settings); |
| 190 | 200 |
| 191 // Re-populates |apps_| and reuses existing apps when possible. | 201 // Re-populates |apps_| and reuses existing apps when possible. |
| 192 const std::vector<policy::DeviceLocalAccount> device_local_accounts = | 202 const std::vector<policy::DeviceLocalAccount> device_local_accounts = |
| 193 policy::GetDeviceLocalAccounts(CrosSettings::Get()); | 203 policy::GetDeviceLocalAccounts(CrosSettings::Get()); |
| 194 for (auto account : device_local_accounts) { | 204 for (auto account : device_local_accounts) { |
| 195 if (account.type != policy::DeviceLocalAccount::TYPE_ARC_KIOSK_APP) | 205 if (account.type != policy::DeviceLocalAccount::TYPE_ARC_KIOSK_APP) |
| 196 continue; | 206 continue; |
| 197 | 207 |
| 198 const AccountId account_id(AccountId::FromUserEmail(account.user_id)); | 208 const AccountId account_id(AccountId::FromUserEmail(account.user_id)); |
| 199 | 209 |
| 200 if (account.account_id == auto_login_account_id_from_settings) | 210 if (account.account_id == auto_login_account_id_from_settings) |
| 201 auto_launch_account_id_ = account_id; | 211 auto_launch_account_id_ = account_id; |
| 202 | 212 |
| 203 auto old_it = | 213 // Apps are keys by package name. http://crbug.com/665904 |
| 204 std::find(old_apps.begin(), old_apps.end(), account.arc_kiosk_app_info); | 214 auto old_it = old_apps.find(account.arc_kiosk_app_info.package_name()); |
| 205 if (old_it != old_apps.end()) { | 215 if (old_it != old_apps.end()) { |
| 206 apps_.push_back(std::move(*old_it)); | 216 apps_.push_back(std::move(old_it->second)); |
| 207 old_apps.erase(old_it); | 217 old_apps.erase(old_it); |
| 208 } else { | 218 } else { |
| 209 // Use package name when display name is not specified. | 219 // Use package name when display name is not specified. |
| 210 std::string name = account.arc_kiosk_app_info.package_name(); | 220 std::string name = account.arc_kiosk_app_info.package_name(); |
| 211 if (!account.arc_kiosk_app_info.display_name().empty()) | 221 if (!account.arc_kiosk_app_info.display_name().empty()) |
| 212 name = account.arc_kiosk_app_info.display_name(); | 222 name = account.arc_kiosk_app_info.display_name(); |
| 213 apps_.push_back( | 223 apps_.push_back(base::MakeUnique<ArcKioskAppData>( |
| 214 ArcKioskApp(account.arc_kiosk_app_info, account_id, name)); | 224 account.arc_kiosk_app_info.package_name(), account_id, name)); |
| 225 apps_.back()->LoadFromCache(); |
| 215 } | 226 } |
| 216 CancelDelayedCryptohomeRemoval(cryptohome::Identification(account_id)); | 227 CancelDelayedCryptohomeRemoval(cryptohome::Identification(account_id)); |
| 217 } | 228 } |
| 218 | 229 |
| 219 ClearRemovedApps(old_apps); | 230 ClearRemovedApps(old_apps); |
| 220 | 231 |
| 221 for (auto& observer : observers_) { | 232 for (auto& observer : observers_) { |
| 222 observer.OnArcKioskAppsChanged(); | 233 observer.OnArcKioskAppsChanged(); |
| 223 } | 234 } |
| 224 } | 235 } |
| 225 | 236 |
| 226 void ArcKioskAppManager::ClearRemovedApps( | 237 void ArcKioskAppManager::ClearRemovedApps( |
| 227 const std::vector<ArcKioskApp>& old_apps) { | 238 const std::map<std::string, std::unique_ptr<ArcKioskAppData>>& old_apps) { |
| 228 // Check if currently active user must be deleted. | 239 // Check if currently active user must be deleted. |
| 229 bool active_user_to_be_deleted = false; | 240 bool active_user_to_be_deleted = false; |
| 230 const user_manager::User* active_user = | 241 const user_manager::User* active_user = |
| 231 user_manager::UserManager::Get()->GetActiveUser(); | 242 user_manager::UserManager::Get()->GetActiveUser(); |
| 232 if (active_user) { | 243 if (active_user) { |
| 233 const AccountId active_account_id = active_user->GetAccountId(); | 244 const AccountId active_account_id = active_user->GetAccountId(); |
| 234 for (const auto& it : old_apps) { | 245 for (const auto& it : old_apps) { |
| 235 if (it.account_id() == active_account_id) { | 246 if (it.second->account_id() == active_account_id) { |
| 236 active_user_to_be_deleted = true; | 247 active_user_to_be_deleted = true; |
| 237 break; | 248 break; |
| 238 } | 249 } |
| 239 } | 250 } |
| 240 } | 251 } |
| 241 | 252 |
| 242 // Remove cryptohome | 253 // Remove cryptohome |
| 243 for (auto& entry : old_apps) { | 254 for (auto& entry : old_apps) { |
| 244 const cryptohome::Identification cryptohome_id(entry.account_id()); | 255 entry.second->ClearCache(); |
| 256 const cryptohome::Identification cryptohome_id(entry.second->account_id()); |
| 245 if (active_user_to_be_deleted) { | 257 if (active_user_to_be_deleted) { |
| 246 // Schedule cryptohome removal after active user logout. | 258 // Schedule cryptohome removal after active user logout. |
| 247 ScheduleDelayedCryptohomeRemoval(cryptohome_id); | 259 ScheduleDelayedCryptohomeRemoval(cryptohome_id); |
| 248 } else { | 260 } else { |
| 249 cryptohome::AsyncMethodCaller::GetInstance()->AsyncRemove( | 261 cryptohome::AsyncMethodCaller::GetInstance()->AsyncRemove( |
| 250 cryptohome_id, base::Bind(&OnRemoveAppCryptohomeComplete, | 262 cryptohome_id, base::Bind(&OnRemoveAppCryptohomeComplete, |
| 251 cryptohome_id, base::Closure())); | 263 cryptohome_id, base::Closure())); |
| 252 } | 264 } |
| 253 } | 265 } |
| 254 | 266 |
| 255 if (active_user_to_be_deleted) | 267 if (active_user_to_be_deleted) |
| 256 chrome::AttemptUserExit(); | 268 chrome::AttemptUserExit(); |
| 257 } | 269 } |
| 258 | 270 |
| 259 } // namespace chromeos | 271 } // namespace chromeos |
| OLD | NEW |