Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / app_mode / kiosk_app_manager.cc
blobd590ff30790d25026027c9ad026b068e5cb4054f
1 // Copyright 2013 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.
5 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
7 #include <map>
8 #include <set>
10 #include "base/barrier_closure.h"
11 #include "base/bind.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h"
14 #include "base/logging.h"
15 #include "base/path_service.h"
16 #include "base/prefs/pref_registry_simple.h"
17 #include "base/prefs/pref_service.h"
18 #include "base/prefs/scoped_user_pref_update.h"
19 #include "base/stl_util.h"
20 #include "base/sys_info.h"
21 #include "chrome/browser/browser_process.h"
22 #include "chrome/browser/chromeos/app_mode/kiosk_app_data.h"
23 #include "chrome/browser/chromeos/app_mode/kiosk_app_external_loader.h"
24 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager_observer.h"
25 #include "chrome/browser/chromeos/app_mode/kiosk_external_updater.h"
26 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h"
27 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_factory.h"
28 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
29 #include "chrome/browser/chromeos/policy/device_local_account.h"
30 #include "chrome/browser/chromeos/settings/cros_settings.h"
31 #include "chrome/browser/extensions/external_loader.h"
32 #include "chrome/browser/extensions/external_provider_impl.h"
33 #include "chrome/browser/lifetime/application_lifetime.h"
34 #include "chrome/common/chrome_paths.h"
35 #include "chrome/common/extensions/extension_constants.h"
36 #include "chromeos/chromeos_paths.h"
37 #include "chromeos/cryptohome/async_method_caller.h"
38 #include "chromeos/dbus/dbus_thread_manager.h"
39 #include "chromeos/settings/cros_settings_names.h"
40 #include "components/ownership/owner_key_util.h"
41 #include "components/user_manager/user_manager.h"
42 #include "content/public/browser/browser_thread.h"
43 #include "extensions/common/extension_urls.h"
45 namespace chromeos {
47 namespace {
49 // Domain that is used for kiosk-app account IDs.
50 const char kKioskAppAccountDomain[] = "kiosk-apps";
52 // Preference for the dictionary of user ids for which cryptohomes have to be
53 // removed upon browser restart.
54 const char kKioskUsersToRemove[] = "kiosk-users-to-remove";
56 std::string GenerateKioskAppAccountId(const std::string& app_id) {
57 return app_id + '@' + kKioskAppAccountDomain;
60 void ScheduleDelayedCryptohomeRemoval(const std::string& user_id,
61 const std::string& app_id) {
62 PrefService* local_state = g_browser_process->local_state();
63 DictionaryPrefUpdate dict_update(local_state, kKioskUsersToRemove);
64 dict_update->SetStringWithoutPathExpansion(user_id, app_id);
65 local_state->CommitPendingWrite();
68 void CancelDelayedCryptohomeRemoval(const std::string& user_id) {
69 PrefService* local_state = g_browser_process->local_state();
70 DictionaryPrefUpdate dict_update(local_state, kKioskUsersToRemove);
71 dict_update->RemoveWithoutPathExpansion(user_id, NULL);
72 local_state->CommitPendingWrite();
75 void OnRemoveAppCryptohomeComplete(const std::string& user_id,
76 const std::string& app,
77 const base::Closure& callback,
78 bool success,
79 cryptohome::MountError return_code) {
80 if (success) {
81 CancelDelayedCryptohomeRemoval(user_id);
82 } else {
83 ScheduleDelayedCryptohomeRemoval(user_id, app);
84 LOG(ERROR) << "Remove cryptohome for " << app
85 << " failed, return code: " << return_code;
87 if (!callback.is_null())
88 callback.Run();
91 void PerformDelayedCryptohomeRemovals(bool service_is_available) {
92 if (!service_is_available) {
93 LOG(ERROR) << "Crypthomed is not available.";
94 return;
97 PrefService* local_state = g_browser_process->local_state();
98 const base::DictionaryValue* dict =
99 local_state->GetDictionary(kKioskUsersToRemove);
100 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) {
101 std::string user_id = it.key();
102 std::string app_id;
103 it.value().GetAsString(&app_id);
104 VLOG(1) << "Removing obsolete crypthome for " << app_id;
105 cryptohome::AsyncMethodCaller::GetInstance()->AsyncRemove(
106 user_id,
107 base::Bind(&OnRemoveAppCryptohomeComplete,
108 user_id,
109 app_id,
110 base::Closure()));
114 // Check for presence of machine owner public key file.
115 void CheckOwnerFilePresence(bool *present) {
116 scoped_refptr<ownership::OwnerKeyUtil> util =
117 OwnerSettingsServiceChromeOSFactory::GetInstance()->GetOwnerKeyUtil();
118 *present = util.get() && util->IsPublicKeyPresent();
121 scoped_refptr<base::SequencedTaskRunner> GetBackgroundTaskRunner() {
122 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
123 CHECK(pool);
124 return pool->GetSequencedTaskRunnerWithShutdownBehavior(
125 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
128 } // namespace
130 // static
131 const char KioskAppManager::kKioskDictionaryName[] = "kiosk";
132 const char KioskAppManager::kKeyApps[] = "apps";
133 const char KioskAppManager::kKeyAutoLoginState[] = "auto_login_state";
134 const char KioskAppManager::kIconCacheDir[] = "kiosk/icon";
135 const char KioskAppManager::kCrxCacheDir[] = "kiosk/crx";
136 const char KioskAppManager::kCrxUnpackDir[] = "kiosk_unpack";
138 // static
139 static base::LazyInstance<KioskAppManager> instance = LAZY_INSTANCE_INITIALIZER;
140 KioskAppManager* KioskAppManager::Get() {
141 return instance.Pointer();
144 // static
145 void KioskAppManager::Shutdown() {
146 if (instance == NULL)
147 return;
149 instance.Pointer()->CleanUp();
152 // static
153 void KioskAppManager::RegisterPrefs(PrefRegistrySimple* registry) {
154 registry->RegisterDictionaryPref(kKioskDictionaryName);
155 registry->RegisterDictionaryPref(kKioskUsersToRemove);
158 // static
159 void KioskAppManager::RemoveObsoleteCryptohomes() {
160 chromeos::CryptohomeClient* client =
161 chromeos::DBusThreadManager::Get()->GetCryptohomeClient();
162 client->WaitForServiceToBeAvailable(
163 base::Bind(&PerformDelayedCryptohomeRemovals));
166 KioskAppManager::App::App(
167 const KioskAppData& data,
168 bool is_extension_pending,
169 bool auto_launched_with_zero_delay)
170 : app_id(data.app_id()),
171 user_id(data.user_id()),
172 name(data.name()),
173 icon(data.icon()),
174 is_loading(data.IsLoading() || is_extension_pending),
175 was_auto_launched_with_zero_delay(auto_launched_with_zero_delay) {
178 KioskAppManager::App::App() : is_loading(false),
179 was_auto_launched_with_zero_delay(false) {}
181 KioskAppManager::App::~App() {}
183 std::string KioskAppManager::GetAutoLaunchApp() const {
184 return auto_launch_app_id_;
187 void KioskAppManager::SetAutoLaunchApp(const std::string& app_id,
188 OwnerSettingsServiceChromeOS* service) {
189 SetAutoLoginState(AUTOLOGIN_REQUESTED);
190 // Clean first, so the proper change callbacks are triggered even
191 // if we are only changing AutoLoginState here.
192 if (!auto_launch_app_id_.empty()) {
193 service->SetString(kAccountsPrefDeviceLocalAccountAutoLoginId,
194 std::string());
197 service->SetString(
198 kAccountsPrefDeviceLocalAccountAutoLoginId,
199 app_id.empty() ? std::string() : GenerateKioskAppAccountId(app_id));
200 service->SetInteger(kAccountsPrefDeviceLocalAccountAutoLoginDelay, 0);
203 void KioskAppManager::SetAppWasAutoLaunchedWithZeroDelay(
204 const std::string& app_id) {
205 DCHECK_EQ(auto_launch_app_id_, app_id);
206 currently_auto_launched_with_zero_delay_app_ = app_id;
209 void KioskAppManager::EnableConsumerKioskAutoLaunch(
210 const KioskAppManager::EnableKioskAutoLaunchCallback& callback) {
211 policy::BrowserPolicyConnectorChromeOS* connector =
212 g_browser_process->platform_part()->browser_policy_connector_chromeos();
213 connector->GetInstallAttributes()->LockDevice(
214 std::string(), // user
215 policy::DEVICE_MODE_CONSUMER_KIOSK_AUTOLAUNCH,
216 std::string(), // device_id
217 base::Bind(
218 &KioskAppManager::OnLockDevice, base::Unretained(this), callback));
221 void KioskAppManager::GetConsumerKioskAutoLaunchStatus(
222 const KioskAppManager::GetConsumerKioskAutoLaunchStatusCallback& callback) {
223 policy::BrowserPolicyConnectorChromeOS* connector =
224 g_browser_process->platform_part()->browser_policy_connector_chromeos();
225 connector->GetInstallAttributes()->ReadImmutableAttributes(
226 base::Bind(&KioskAppManager::OnReadImmutableAttributes,
227 base::Unretained(this),
228 callback));
231 bool KioskAppManager::IsConsumerKioskDeviceWithAutoLaunch() {
232 policy::BrowserPolicyConnectorChromeOS* connector =
233 g_browser_process->platform_part()->browser_policy_connector_chromeos();
234 return connector->GetInstallAttributes() &&
235 connector->GetInstallAttributes()
236 ->IsConsumerKioskDeviceWithAutoLaunch();
239 void KioskAppManager::OnLockDevice(
240 const KioskAppManager::EnableKioskAutoLaunchCallback& callback,
241 policy::EnterpriseInstallAttributes::LockResult result) {
242 if (callback.is_null())
243 return;
245 callback.Run(result == policy::EnterpriseInstallAttributes::LOCK_SUCCESS);
248 void KioskAppManager::OnOwnerFileChecked(
249 const KioskAppManager::GetConsumerKioskAutoLaunchStatusCallback& callback,
250 bool* owner_present) {
251 ownership_established_ = *owner_present;
253 if (callback.is_null())
254 return;
256 // If we have owner already established on the machine, don't let
257 // consumer kiosk to be enabled.
258 if (ownership_established_)
259 callback.Run(CONSUMER_KIOSK_AUTO_LAUNCH_DISABLED);
260 else
261 callback.Run(CONSUMER_KIOSK_AUTO_LAUNCH_CONFIGURABLE);
264 void KioskAppManager::OnReadImmutableAttributes(
265 const KioskAppManager::GetConsumerKioskAutoLaunchStatusCallback&
266 callback) {
267 if (callback.is_null())
268 return;
270 ConsumerKioskAutoLaunchStatus status =
271 CONSUMER_KIOSK_AUTO_LAUNCH_DISABLED;
272 policy::BrowserPolicyConnectorChromeOS* connector =
273 g_browser_process->platform_part()->browser_policy_connector_chromeos();
274 policy::EnterpriseInstallAttributes* attributes =
275 connector->GetInstallAttributes();
276 switch (attributes->GetMode()) {
277 case policy::DEVICE_MODE_NOT_SET: {
278 if (!base::SysInfo::IsRunningOnChromeOS()) {
279 status = CONSUMER_KIOSK_AUTO_LAUNCH_CONFIGURABLE;
280 } else if (!ownership_established_) {
281 bool* owner_present = new bool(false);
282 content::BrowserThread::PostBlockingPoolTaskAndReply(
283 FROM_HERE,
284 base::Bind(&CheckOwnerFilePresence,
285 owner_present),
286 base::Bind(&KioskAppManager::OnOwnerFileChecked,
287 base::Unretained(this),
288 callback,
289 base::Owned(owner_present)));
290 return;
292 break;
294 case policy::DEVICE_MODE_CONSUMER_KIOSK_AUTOLAUNCH:
295 status = CONSUMER_KIOSK_AUTO_LAUNCH_ENABLED;
296 break;
297 default:
298 break;
301 callback.Run(status);
304 void KioskAppManager::SetEnableAutoLaunch(bool value) {
305 SetAutoLoginState(value ? AUTOLOGIN_APPROVED : AUTOLOGIN_REJECTED);
308 bool KioskAppManager::IsAutoLaunchRequested() const {
309 if (GetAutoLaunchApp().empty())
310 return false;
312 // Apps that were installed by the policy don't require machine owner
313 // consent through UI.
314 policy::BrowserPolicyConnectorChromeOS* connector =
315 g_browser_process->platform_part()->browser_policy_connector_chromeos();
316 if (connector->IsEnterpriseManaged())
317 return false;
319 return GetAutoLoginState() == AUTOLOGIN_REQUESTED;
322 bool KioskAppManager::IsAutoLaunchEnabled() const {
323 if (GetAutoLaunchApp().empty())
324 return false;
326 // Apps that were installed by the policy don't require machine owner
327 // consent through UI.
328 policy::BrowserPolicyConnectorChromeOS* connector =
329 g_browser_process->platform_part()->browser_policy_connector_chromeos();
330 if (connector->IsEnterpriseManaged())
331 return true;
333 return GetAutoLoginState() == AUTOLOGIN_APPROVED;
336 void KioskAppManager::AddApp(const std::string& app_id,
337 OwnerSettingsServiceChromeOS* service) {
338 std::vector<policy::DeviceLocalAccount> device_local_accounts =
339 policy::GetDeviceLocalAccounts(CrosSettings::Get());
341 // Don't insert the app if it's already in the list.
342 for (std::vector<policy::DeviceLocalAccount>::const_iterator
343 it = device_local_accounts.begin();
344 it != device_local_accounts.end(); ++it) {
345 if (it->type == policy::DeviceLocalAccount::TYPE_KIOSK_APP &&
346 it->kiosk_app_id == app_id) {
347 return;
351 // Add the new account.
352 device_local_accounts.push_back(policy::DeviceLocalAccount(
353 policy::DeviceLocalAccount::TYPE_KIOSK_APP,
354 GenerateKioskAppAccountId(app_id),
355 app_id,
356 std::string()));
358 policy::SetDeviceLocalAccounts(service, device_local_accounts);
361 void KioskAppManager::RemoveApp(const std::string& app_id,
362 OwnerSettingsServiceChromeOS* service) {
363 // Resets auto launch app if it is the removed app.
364 if (auto_launch_app_id_ == app_id)
365 SetAutoLaunchApp(std::string(), service);
367 std::vector<policy::DeviceLocalAccount> device_local_accounts =
368 policy::GetDeviceLocalAccounts(CrosSettings::Get());
369 if (device_local_accounts.empty())
370 return;
372 // Remove entries that match |app_id|.
373 for (std::vector<policy::DeviceLocalAccount>::iterator
374 it = device_local_accounts.begin();
375 it != device_local_accounts.end(); ++it) {
376 if (it->type == policy::DeviceLocalAccount::TYPE_KIOSK_APP &&
377 it->kiosk_app_id == app_id) {
378 device_local_accounts.erase(it);
379 break;
383 policy::SetDeviceLocalAccounts(service, device_local_accounts);
386 void KioskAppManager::GetApps(Apps* apps) const {
387 apps->clear();
388 apps->reserve(apps_.size());
389 for (size_t i = 0; i < apps_.size(); ++i) {
390 const KioskAppData& app_data = *apps_[i];
391 if (app_data.status() != KioskAppData::STATUS_ERROR) {
392 apps->push_back(App(
393 app_data, external_cache_->IsExtensionPending(app_data.app_id()),
394 app_data.app_id() == currently_auto_launched_with_zero_delay_app_));
399 bool KioskAppManager::GetApp(const std::string& app_id, App* app) const {
400 const KioskAppData* data = GetAppData(app_id);
401 if (!data)
402 return false;
404 *app = App(*data, external_cache_->IsExtensionPending(app_id),
405 app_id == currently_auto_launched_with_zero_delay_app_);
406 return true;
409 bool KioskAppManager::GetDisableBailoutShortcut() const {
410 bool enable;
411 if (CrosSettings::Get()->GetBoolean(
412 kAccountsPrefDeviceLocalAccountAutoLoginBailoutEnabled, &enable)) {
413 return !enable;
416 return false;
419 void KioskAppManager::ClearAppData(const std::string& app_id) {
420 KioskAppData* app_data = GetAppDataMutable(app_id);
421 if (!app_data)
422 return;
424 app_data->ClearCache();
427 void KioskAppManager::UpdateAppDataFromProfile(
428 const std::string& app_id,
429 Profile* profile,
430 const extensions::Extension* app) {
431 KioskAppData* app_data = GetAppDataMutable(app_id);
432 if (!app_data)
433 return;
435 app_data->LoadFromInstalledApp(profile, app);
438 void KioskAppManager::RetryFailedAppDataFetch() {
439 for (size_t i = 0; i < apps_.size(); ++i) {
440 if (apps_[i]->status() == KioskAppData::STATUS_ERROR)
441 apps_[i]->Load();
445 bool KioskAppManager::HasCachedCrx(const std::string& app_id) const {
446 base::FilePath crx_path;
447 std::string version;
448 return GetCachedCrx(app_id, &crx_path, &version);
451 bool KioskAppManager::GetCachedCrx(const std::string& app_id,
452 base::FilePath* file_path,
453 std::string* version) const {
454 return external_cache_->GetExtension(app_id, file_path, version);
457 void KioskAppManager::AddObserver(KioskAppManagerObserver* observer) {
458 observers_.AddObserver(observer);
461 void KioskAppManager::RemoveObserver(KioskAppManagerObserver* observer) {
462 observers_.RemoveObserver(observer);
465 extensions::ExternalLoader* KioskAppManager::CreateExternalLoader() {
466 if (external_loader_created_) {
467 NOTREACHED();
468 return nullptr;
470 external_loader_created_ = true;
471 KioskAppExternalLoader* loader = new KioskAppExternalLoader();
472 external_loader_ = loader->AsWeakPtr();
474 return loader;
477 extensions::ExternalLoader*
478 KioskAppManager::CreateSecondaryAppExternalLoader() {
479 if (secondary_app_external_loader_created_) {
480 NOTREACHED();
481 return nullptr;
483 secondary_app_external_loader_created_ = true;
484 KioskAppExternalLoader* secondary_loader = new KioskAppExternalLoader();
485 secondary_app_external_loader_ = secondary_loader->AsWeakPtr();
487 return secondary_loader;
490 void KioskAppManager::InstallFromCache(const std::string& id) {
491 const base::DictionaryValue* extension = NULL;
492 if (external_cache_->cached_extensions()->GetDictionary(id, &extension)) {
493 scoped_ptr<base::DictionaryValue> prefs(new base::DictionaryValue);
494 base::DictionaryValue* extension_copy = extension->DeepCopy();
495 prefs->Set(id, extension_copy);
496 external_loader_->SetCurrentAppExtensions(prefs.Pass());
497 } else {
498 LOG(ERROR) << "Can't find app in the cached externsions"
499 << " id = " << id;
503 void KioskAppManager::InstallSecondaryApps(
504 const std::vector<std::string>& ids) {
505 if (ids.empty())
506 return;
508 scoped_ptr<base::DictionaryValue> prefs(new base::DictionaryValue);
509 for (const std::string& id : ids) {
510 scoped_ptr<base::DictionaryValue> extension_entry(
511 new base::DictionaryValue);
512 extension_entry->SetStringWithoutPathExpansion(
513 extensions::ExternalProviderImpl::kExternalUpdateUrl,
514 extension_urls::GetWebstoreUpdateUrl().spec());
515 extension_entry->SetBoolean(
516 extensions::ExternalProviderImpl::kIsFromWebstore, true);
517 prefs->Set(id, extension_entry.Pass());
519 secondary_app_external_loader_->SetCurrentAppExtensions(prefs.Pass());
522 void KioskAppManager::UpdateExternalCache() {
523 UpdateAppData();
526 void KioskAppManager::OnKioskAppCacheUpdated(const std::string& app_id) {
527 FOR_EACH_OBSERVER(
528 KioskAppManagerObserver, observers_, OnKioskAppCacheUpdated(app_id));
531 void KioskAppManager::OnKioskAppExternalUpdateComplete(bool success) {
532 FOR_EACH_OBSERVER(KioskAppManagerObserver,
533 observers_,
534 OnKioskAppExternalUpdateComplete(success));
537 void KioskAppManager::PutValidatedExternalExtension(
538 const std::string& app_id,
539 const base::FilePath& crx_path,
540 const std::string& version,
541 const ExternalCache::PutExternalExtensionCallback& callback) {
542 external_cache_->PutExternalExtension(app_id, crx_path, version, callback);
545 KioskAppManager::KioskAppManager()
546 : ownership_established_(false),
547 external_loader_created_(false),
548 secondary_app_external_loader_created_(false) {
549 base::FilePath cache_dir;
550 GetCrxCacheDir(&cache_dir);
551 external_cache_.reset(
552 new ExternalCache(cache_dir,
553 g_browser_process->system_request_context(),
554 GetBackgroundTaskRunner(),
555 this,
556 true /* always_check_updates */,
557 false /* wait_for_cache_initialization */));
558 UpdateAppData();
559 local_accounts_subscription_ =
560 CrosSettings::Get()->AddSettingsObserver(
561 kAccountsPrefDeviceLocalAccounts,
562 base::Bind(&KioskAppManager::UpdateAppData, base::Unretained(this)));
563 local_account_auto_login_id_subscription_ =
564 CrosSettings::Get()->AddSettingsObserver(
565 kAccountsPrefDeviceLocalAccountAutoLoginId,
566 base::Bind(&KioskAppManager::UpdateAppData, base::Unretained(this)));
569 KioskAppManager::~KioskAppManager() {}
571 void KioskAppManager::MonitorKioskExternalUpdate() {
572 base::FilePath cache_dir;
573 GetCrxCacheDir(&cache_dir);
574 base::FilePath unpack_dir;
575 GetCrxUnpackDir(&unpack_dir);
576 usb_stick_updater_.reset(new KioskExternalUpdater(
577 GetBackgroundTaskRunner(), cache_dir, unpack_dir));
580 void KioskAppManager::CleanUp() {
581 local_accounts_subscription_.reset();
582 local_account_auto_login_id_subscription_.reset();
583 apps_.clear();
584 usb_stick_updater_.reset();
585 external_cache_.reset();
588 const KioskAppData* KioskAppManager::GetAppData(
589 const std::string& app_id) const {
590 for (size_t i = 0; i < apps_.size(); ++i) {
591 const KioskAppData* data = apps_[i];
592 if (data->app_id() == app_id)
593 return data;
596 return NULL;
599 KioskAppData* KioskAppManager::GetAppDataMutable(const std::string& app_id) {
600 return const_cast<KioskAppData*>(GetAppData(app_id));
603 void KioskAppManager::UpdateAppData() {
604 // Gets app id to data mapping for existing apps.
605 std::map<std::string, KioskAppData*> old_apps;
606 for (size_t i = 0; i < apps_.size(); ++i)
607 old_apps[apps_[i]->app_id()] = apps_[i];
608 apps_.weak_clear(); // |old_apps| takes ownership
610 auto_launch_app_id_.clear();
611 std::string auto_login_account_id;
612 CrosSettings::Get()->GetString(kAccountsPrefDeviceLocalAccountAutoLoginId,
613 &auto_login_account_id);
615 // Re-populates |apps_| and reuses existing KioskAppData when possible.
616 const std::vector<policy::DeviceLocalAccount> device_local_accounts =
617 policy::GetDeviceLocalAccounts(CrosSettings::Get());
618 for (std::vector<policy::DeviceLocalAccount>::const_iterator
619 it = device_local_accounts.begin();
620 it != device_local_accounts.end(); ++it) {
621 if (it->type != policy::DeviceLocalAccount::TYPE_KIOSK_APP)
622 continue;
624 if (it->account_id == auto_login_account_id)
625 auto_launch_app_id_ = it->kiosk_app_id;
627 std::map<std::string, KioskAppData*>::iterator old_it =
628 old_apps.find(it->kiosk_app_id);
629 if (old_it != old_apps.end()) {
630 apps_.push_back(old_it->second);
631 old_apps.erase(old_it);
632 } else {
633 KioskAppData* new_app = new KioskAppData(
634 this, it->kiosk_app_id, it->user_id, GURL(it->kiosk_app_update_url));
635 apps_.push_back(new_app); // Takes ownership of |new_app|.
636 new_app->Load();
638 CancelDelayedCryptohomeRemoval(it->user_id);
641 base::Closure cryptohomes_barrier_closure;
643 const user_manager::User* active_user =
644 user_manager::UserManager::Get()->GetActiveUser();
645 if (active_user) {
646 std::string active_user_id = active_user->GetUserID();
647 for (const auto& it : old_apps) {
648 if (it.second->user_id() == active_user_id) {
649 VLOG(1) << "Currently running kiosk app removed from policy, exiting";
650 cryptohomes_barrier_closure = BarrierClosure(
651 old_apps.size(), base::Bind(&chrome::AttemptUserExit));
652 break;
657 // Clears cache and deletes the remaining old data.
658 std::vector<std::string> apps_to_remove;
659 for (std::map<std::string, KioskAppData*>::iterator it = old_apps.begin();
660 it != old_apps.end(); ++it) {
661 it->second->ClearCache();
662 cryptohome::AsyncMethodCaller::GetInstance()->AsyncRemove(
663 it->second->user_id(),
664 base::Bind(&OnRemoveAppCryptohomeComplete,
665 it->second->user_id(),
666 it->first,
667 cryptohomes_barrier_closure));
668 apps_to_remove.push_back(it->second->app_id());
670 STLDeleteValues(&old_apps);
671 external_cache_->RemoveExtensions(apps_to_remove);
673 // Request external_cache_ to download new apps and update the existing
674 // apps.
675 scoped_ptr<base::DictionaryValue> prefs(new base::DictionaryValue);
676 for (size_t i = 0; i < apps_.size(); ++i) {
677 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
679 if (apps_[i]->update_url().is_valid()) {
680 entry->SetString(extensions::ExternalProviderImpl::kExternalUpdateUrl,
681 apps_[i]->update_url().spec());
682 } else {
683 entry->SetString(extensions::ExternalProviderImpl::kExternalUpdateUrl,
684 extension_urls::GetWebstoreUpdateUrl().spec());
687 prefs->Set(apps_[i]->app_id(), entry.release());
689 external_cache_->UpdateExtensionsList(prefs.Pass());
691 RetryFailedAppDataFetch();
693 FOR_EACH_OBSERVER(KioskAppManagerObserver, observers_,
694 OnKioskAppsSettingsChanged());
697 void KioskAppManager::GetKioskAppIconCacheDir(base::FilePath* cache_dir) {
698 base::FilePath user_data_dir;
699 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
700 *cache_dir = user_data_dir.AppendASCII(kIconCacheDir);
703 void KioskAppManager::OnKioskAppDataChanged(const std::string& app_id) {
704 FOR_EACH_OBSERVER(KioskAppManagerObserver,
705 observers_,
706 OnKioskAppDataChanged(app_id));
709 void KioskAppManager::OnKioskAppDataLoadFailure(const std::string& app_id) {
710 FOR_EACH_OBSERVER(KioskAppManagerObserver,
711 observers_,
712 OnKioskAppDataLoadFailure(app_id));
715 void KioskAppManager::OnExtensionListsUpdated(
716 const base::DictionaryValue* prefs) {
719 void KioskAppManager::OnExtensionLoadedInCache(const std::string& id) {
720 KioskAppData* app_data = GetAppDataMutable(id);
721 if (!app_data)
722 return;
724 base::FilePath crx_path;
725 std::string version;
726 if (GetCachedCrx(id, &crx_path, &version))
727 app_data->SetCachedCrx(crx_path);
729 FOR_EACH_OBSERVER(KioskAppManagerObserver,
730 observers_,
731 OnKioskExtensionLoadedInCache(id));
735 void KioskAppManager::OnExtensionDownloadFailed(
736 const std::string& id,
737 extensions::ExtensionDownloaderDelegate::Error error) {
738 KioskAppData* app_data = GetAppDataMutable(id);
739 if (!app_data)
740 return;
741 FOR_EACH_OBSERVER(KioskAppManagerObserver,
742 observers_,
743 OnKioskExtensionDownloadFailed(id));
746 KioskAppManager::AutoLoginState KioskAppManager::GetAutoLoginState() const {
747 PrefService* prefs = g_browser_process->local_state();
748 const base::DictionaryValue* dict =
749 prefs->GetDictionary(KioskAppManager::kKioskDictionaryName);
750 int value;
751 if (!dict->GetInteger(kKeyAutoLoginState, &value))
752 return AUTOLOGIN_NONE;
754 return static_cast<AutoLoginState>(value);
757 void KioskAppManager::SetAutoLoginState(AutoLoginState state) {
758 PrefService* prefs = g_browser_process->local_state();
759 DictionaryPrefUpdate dict_update(prefs,
760 KioskAppManager::kKioskDictionaryName);
761 dict_update->SetInteger(kKeyAutoLoginState, state);
762 prefs->CommitPendingWrite();
765 void KioskAppManager::GetCrxCacheDir(base::FilePath* cache_dir) {
766 base::FilePath user_data_dir;
767 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
768 *cache_dir = user_data_dir.AppendASCII(kCrxCacheDir);
771 void KioskAppManager::GetCrxUnpackDir(base::FilePath* unpack_dir) {
772 base::FilePath temp_dir;
773 base::GetTempDir(&temp_dir);
774 *unpack_dir = temp_dir.AppendASCII(kCrxUnpackDir);
777 } // namespace chromeos