Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / chromeos / app_mode / kiosk_app_manager.cc
bloba37df89e7ee7638bc03f773960b6e5dadaf6f6b4
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 NULL;
470 external_loader_created_ = true;
471 KioskAppExternalLoader* loader = new KioskAppExternalLoader();
472 external_loader_ = loader->AsWeakPtr();
474 return loader;
477 void KioskAppManager::InstallFromCache(const std::string& id) {
478 const base::DictionaryValue* extension = NULL;
479 if (external_cache_->cached_extensions()->GetDictionary(id, &extension)) {
480 scoped_ptr<base::DictionaryValue> prefs(new base::DictionaryValue);
481 base::DictionaryValue* extension_copy = extension->DeepCopy();
482 prefs->Set(id, extension_copy);
483 external_loader_->SetCurrentAppExtensions(prefs.Pass());
484 } else {
485 LOG(ERROR) << "Can't find app in the cached externsions"
486 << " id = " << id;
490 void KioskAppManager::UpdateExternalCache() {
491 UpdateAppData();
494 void KioskAppManager::OnKioskAppCacheUpdated(const std::string& app_id) {
495 FOR_EACH_OBSERVER(
496 KioskAppManagerObserver, observers_, OnKioskAppCacheUpdated(app_id));
499 void KioskAppManager::OnKioskAppExternalUpdateComplete(bool success) {
500 FOR_EACH_OBSERVER(KioskAppManagerObserver,
501 observers_,
502 OnKioskAppExternalUpdateComplete(success));
505 void KioskAppManager::PutValidatedExternalExtension(
506 const std::string& app_id,
507 const base::FilePath& crx_path,
508 const std::string& version,
509 const ExternalCache::PutExternalExtensionCallback& callback) {
510 external_cache_->PutExternalExtension(app_id, crx_path, version, callback);
513 KioskAppManager::KioskAppManager()
514 : ownership_established_(false), external_loader_created_(false) {
515 base::FilePath cache_dir;
516 GetCrxCacheDir(&cache_dir);
517 external_cache_.reset(
518 new ExternalCache(cache_dir,
519 g_browser_process->system_request_context(),
520 GetBackgroundTaskRunner(),
521 this,
522 true /* always_check_updates */,
523 false /* wait_for_cache_initialization */));
524 UpdateAppData();
525 local_accounts_subscription_ =
526 CrosSettings::Get()->AddSettingsObserver(
527 kAccountsPrefDeviceLocalAccounts,
528 base::Bind(&KioskAppManager::UpdateAppData, base::Unretained(this)));
529 local_account_auto_login_id_subscription_ =
530 CrosSettings::Get()->AddSettingsObserver(
531 kAccountsPrefDeviceLocalAccountAutoLoginId,
532 base::Bind(&KioskAppManager::UpdateAppData, base::Unretained(this)));
535 KioskAppManager::~KioskAppManager() {}
537 void KioskAppManager::MonitorKioskExternalUpdate() {
538 base::FilePath cache_dir;
539 GetCrxCacheDir(&cache_dir);
540 base::FilePath unpack_dir;
541 GetCrxUnpackDir(&unpack_dir);
542 usb_stick_updater_.reset(new KioskExternalUpdater(
543 GetBackgroundTaskRunner(), cache_dir, unpack_dir));
546 void KioskAppManager::CleanUp() {
547 local_accounts_subscription_.reset();
548 local_account_auto_login_id_subscription_.reset();
549 apps_.clear();
550 usb_stick_updater_.reset();
551 external_cache_.reset();
554 const KioskAppData* KioskAppManager::GetAppData(
555 const std::string& app_id) const {
556 for (size_t i = 0; i < apps_.size(); ++i) {
557 const KioskAppData* data = apps_[i];
558 if (data->app_id() == app_id)
559 return data;
562 return NULL;
565 KioskAppData* KioskAppManager::GetAppDataMutable(const std::string& app_id) {
566 return const_cast<KioskAppData*>(GetAppData(app_id));
569 void KioskAppManager::UpdateAppData() {
570 // Gets app id to data mapping for existing apps.
571 std::map<std::string, KioskAppData*> old_apps;
572 for (size_t i = 0; i < apps_.size(); ++i)
573 old_apps[apps_[i]->app_id()] = apps_[i];
574 apps_.weak_clear(); // |old_apps| takes ownership
576 auto_launch_app_id_.clear();
577 std::string auto_login_account_id;
578 CrosSettings::Get()->GetString(kAccountsPrefDeviceLocalAccountAutoLoginId,
579 &auto_login_account_id);
581 // Re-populates |apps_| and reuses existing KioskAppData when possible.
582 const std::vector<policy::DeviceLocalAccount> device_local_accounts =
583 policy::GetDeviceLocalAccounts(CrosSettings::Get());
584 for (std::vector<policy::DeviceLocalAccount>::const_iterator
585 it = device_local_accounts.begin();
586 it != device_local_accounts.end(); ++it) {
587 if (it->type != policy::DeviceLocalAccount::TYPE_KIOSK_APP)
588 continue;
590 if (it->account_id == auto_login_account_id)
591 auto_launch_app_id_ = it->kiosk_app_id;
593 std::map<std::string, KioskAppData*>::iterator old_it =
594 old_apps.find(it->kiosk_app_id);
595 if (old_it != old_apps.end()) {
596 apps_.push_back(old_it->second);
597 old_apps.erase(old_it);
598 } else {
599 KioskAppData* new_app = new KioskAppData(
600 this, it->kiosk_app_id, it->user_id, GURL(it->kiosk_app_update_url));
601 apps_.push_back(new_app); // Takes ownership of |new_app|.
602 new_app->Load();
604 CancelDelayedCryptohomeRemoval(it->user_id);
607 base::Closure cryptohomes_barrier_closure;
609 const user_manager::User* active_user =
610 user_manager::UserManager::Get()->GetActiveUser();
611 if (active_user) {
612 std::string active_user_id = active_user->GetUserID();
613 for (const auto& it : old_apps) {
614 if (it.second->user_id() == active_user_id) {
615 VLOG(1) << "Currently running kiosk app removed from policy, exiting";
616 cryptohomes_barrier_closure = BarrierClosure(
617 old_apps.size(), base::Bind(&chrome::AttemptUserExit));
618 break;
623 // Clears cache and deletes the remaining old data.
624 std::vector<std::string> apps_to_remove;
625 for (std::map<std::string, KioskAppData*>::iterator it = old_apps.begin();
626 it != old_apps.end(); ++it) {
627 it->second->ClearCache();
628 cryptohome::AsyncMethodCaller::GetInstance()->AsyncRemove(
629 it->second->user_id(),
630 base::Bind(&OnRemoveAppCryptohomeComplete,
631 it->second->user_id(),
632 it->first,
633 cryptohomes_barrier_closure));
634 apps_to_remove.push_back(it->second->app_id());
636 STLDeleteValues(&old_apps);
637 external_cache_->RemoveExtensions(apps_to_remove);
639 // Request external_cache_ to download new apps and update the existing
640 // apps.
641 scoped_ptr<base::DictionaryValue> prefs(new base::DictionaryValue);
642 for (size_t i = 0; i < apps_.size(); ++i) {
643 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
645 if (apps_[i]->update_url().is_valid()) {
646 entry->SetString(extensions::ExternalProviderImpl::kExternalUpdateUrl,
647 apps_[i]->update_url().spec());
648 } else {
649 entry->SetString(extensions::ExternalProviderImpl::kExternalUpdateUrl,
650 extension_urls::GetWebstoreUpdateUrl().spec());
653 prefs->Set(apps_[i]->app_id(), entry.release());
655 external_cache_->UpdateExtensionsList(prefs.Pass());
657 RetryFailedAppDataFetch();
659 FOR_EACH_OBSERVER(KioskAppManagerObserver, observers_,
660 OnKioskAppsSettingsChanged());
663 void KioskAppManager::GetKioskAppIconCacheDir(base::FilePath* cache_dir) {
664 base::FilePath user_data_dir;
665 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
666 *cache_dir = user_data_dir.AppendASCII(kIconCacheDir);
669 void KioskAppManager::OnKioskAppDataChanged(const std::string& app_id) {
670 FOR_EACH_OBSERVER(KioskAppManagerObserver,
671 observers_,
672 OnKioskAppDataChanged(app_id));
675 void KioskAppManager::OnKioskAppDataLoadFailure(const std::string& app_id) {
676 FOR_EACH_OBSERVER(KioskAppManagerObserver,
677 observers_,
678 OnKioskAppDataLoadFailure(app_id));
681 void KioskAppManager::OnExtensionListsUpdated(
682 const base::DictionaryValue* prefs) {
685 void KioskAppManager::OnExtensionLoadedInCache(const std::string& id) {
686 KioskAppData* app_data = GetAppDataMutable(id);
687 if (!app_data)
688 return;
690 base::FilePath crx_path;
691 std::string version;
692 if (GetCachedCrx(id, &crx_path, &version))
693 app_data->SetCachedCrx(crx_path);
695 FOR_EACH_OBSERVER(KioskAppManagerObserver,
696 observers_,
697 OnKioskExtensionLoadedInCache(id));
701 void KioskAppManager::OnExtensionDownloadFailed(
702 const std::string& id,
703 extensions::ExtensionDownloaderDelegate::Error error) {
704 KioskAppData* app_data = GetAppDataMutable(id);
705 if (!app_data)
706 return;
707 FOR_EACH_OBSERVER(KioskAppManagerObserver,
708 observers_,
709 OnKioskExtensionDownloadFailed(id));
712 KioskAppManager::AutoLoginState KioskAppManager::GetAutoLoginState() const {
713 PrefService* prefs = g_browser_process->local_state();
714 const base::DictionaryValue* dict =
715 prefs->GetDictionary(KioskAppManager::kKioskDictionaryName);
716 int value;
717 if (!dict->GetInteger(kKeyAutoLoginState, &value))
718 return AUTOLOGIN_NONE;
720 return static_cast<AutoLoginState>(value);
723 void KioskAppManager::SetAutoLoginState(AutoLoginState state) {
724 PrefService* prefs = g_browser_process->local_state();
725 DictionaryPrefUpdate dict_update(prefs,
726 KioskAppManager::kKioskDictionaryName);
727 dict_update->SetInteger(kKeyAutoLoginState, state);
728 prefs->CommitPendingWrite();
731 void KioskAppManager::GetCrxCacheDir(base::FilePath* cache_dir) {
732 base::FilePath user_data_dir;
733 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
734 *cache_dir = user_data_dir.AppendASCII(kCrxCacheDir);
737 void KioskAppManager::GetCrxUnpackDir(base::FilePath* unpack_dir) {
738 base::FilePath temp_dir;
739 base::GetTempDir(&temp_dir);
740 *unpack_dir = temp_dir.AppendASCII(kCrxUnpackDir);
743 } // namespace chromeos