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. 4 5#include "chrome/browser/chromeos/app_mode/app_session_lifetime.h" 6 7#include "apps/shell_window_registry.h" 8#include "base/basictypes.h" 9#include "base/lazy_instance.h" 10#include "base/prefs/pref_service.h" 11#include "chrome/browser/browser_process.h" 12#include "chrome/browser/chromeos/app_mode/kiosk_app_update_service.h" 13#include "chrome/browser/lifetime/application_lifetime.h" 14#include "chrome/browser/policy/browser_policy_connector.h" 15#include "chrome/common/pref_names.h" 16 17using apps::ShellWindowRegistry; 18 19namespace chromeos { 20 21namespace { 22 23// AppWindowWatcher watches for app window and exits the session when the 24// last app window is closed. 25class AppWindowWatcher : public ShellWindowRegistry::Observer { 26 public: 27 AppWindowWatcher() : window_registry_(NULL) {} 28 virtual ~AppWindowWatcher() {} 29 30 void Init(Profile* profile) { 31 DCHECK(!window_registry_); 32 window_registry_ = ShellWindowRegistry::Get(profile); 33 if (window_registry_) 34 window_registry_->AddObserver(this); 35 } 36 37 private: 38 // apps::ShellWindowRegistry::Observer overrides: 39 virtual void OnShellWindowAdded(apps::ShellWindow* shell_window) OVERRIDE {} 40 virtual void OnShellWindowIconChanged(apps::ShellWindow* shell_window) 41 OVERRIDE {} 42 virtual void OnShellWindowRemoved(apps::ShellWindow* shell_window) OVERRIDE { 43 if (window_registry_->shell_windows().empty()) { 44 chrome::AttemptUserExit(); 45 window_registry_->RemoveObserver(this); 46 } 47 } 48 49 apps::ShellWindowRegistry* window_registry_; 50 51 DISALLOW_COPY_AND_ASSIGN(AppWindowWatcher); 52}; 53 54base::LazyInstance<AppWindowWatcher> app_window_watcher 55 = LAZY_INSTANCE_INITIALIZER; 56 57} // namespace 58 59void InitAppSession(Profile* profile, const std::string& app_id) { 60 // Binds the session lifetime with app window counts. 61 CHECK(app_window_watcher == NULL); 62 app_window_watcher.Get().Init(profile); 63 64 // Set the app_id for the current instance of KioskAppUpdateService. 65 KioskAppUpdateService* update_service = 66 KioskAppUpdateServiceFactory::GetForProfile(profile); 67 DCHECK(update_service); 68 if (update_service) 69 update_service->set_app_id(app_id); 70 71 // If the device is not enterprise managed, set prefs to reboot after update. 72 if (!g_browser_process->browser_policy_connector()->IsEnterpriseManaged()) { 73 PrefService* local_state = g_browser_process->local_state(); 74 local_state->SetBoolean(prefs::kRebootAfterUpdate, true); 75 } 76} 77 78} // namespace chromeos 79