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/app_session_lifetime.h"
7 #include "apps/app_window.h"
8 #include "apps/app_window_registry.h"
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/lazy_instance.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/prefs/pref_service.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/chromeos/app_mode/kiosk_app_update_service.h"
16 #include "chrome/browser/chromeos/app_mode/kiosk_mode_idle_app_name_notification.h"
17 #include "chrome/browser/chromeos/login/demo_mode/demo_app_launcher.h"
18 #include "chrome/browser/chromeos/login/user_manager.h"
19 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
20 #include "chrome/browser/lifetime/application_lifetime.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/browser_list.h"
24 #include "chrome/browser/ui/browser_list_observer.h"
25 #include "chrome/browser/ui/browser_window.h"
26 #include "chrome/browser/ui/tabs/tab_strip_model.h"
27 #include "chrome/common/pref_names.h"
28 #include "content/public/browser/web_contents.h"
30 using apps::AppWindowRegistry;
36 // AppWindowHandler watches for app window and exits the session when the
37 // last app window is closed.
38 class AppWindowHandler : public AppWindowRegistry::Observer {
40 AppWindowHandler() : window_registry_(NULL) {}
41 virtual ~AppWindowHandler() {}
43 void Init(Profile* profile) {
44 DCHECK(!window_registry_);
45 window_registry_ = AppWindowRegistry::Get(profile);
47 window_registry_->AddObserver(this);
51 // apps::AppWindowRegistry::Observer overrides:
52 virtual void OnAppWindowAdded(apps::AppWindow* app_window) OVERRIDE {}
53 virtual void OnAppWindowIconChanged(apps::AppWindow* app_window) OVERRIDE {}
54 virtual void OnAppWindowRemoved(apps::AppWindow* app_window) OVERRIDE {
55 if (window_registry_->app_windows().empty()) {
56 chrome::AttemptUserExit();
57 window_registry_->RemoveObserver(this);
61 apps::AppWindowRegistry* window_registry_;
63 DISALLOW_COPY_AND_ASSIGN(AppWindowHandler);
66 base::LazyInstance<AppWindowHandler> app_window_handler
67 = LAZY_INSTANCE_INITIALIZER;
69 // BrowserWindowHandler monitors Browser object being created during
70 // a kiosk session, log info such as URL so that the code path could be
71 // fixed and closes the just opened browser window.
72 class BrowserWindowHandler : public chrome::BrowserListObserver {
74 BrowserWindowHandler() {
75 BrowserList::AddObserver(this);
77 virtual ~BrowserWindowHandler() {
78 BrowserList::RemoveObserver(this);
82 void HandleBrowser(Browser* browser) {
83 content::WebContents* active_tab =
84 browser->tab_strip_model()->GetActiveWebContents();
85 std::string url_string =
86 active_tab ? active_tab->GetURL().spec() : std::string();
87 LOG(WARNING) << "Browser opened in kiosk session"
88 << ", url=" << url_string;
90 browser->window()->Close();
93 // chrome::BrowserListObserver overrides:
94 virtual void OnBrowserAdded(Browser* browser) OVERRIDE {
95 base::MessageLoop::current()->PostTask(
97 base::Bind(&BrowserWindowHandler::HandleBrowser,
98 base::Unretained(this), // LazyInstance, always valid
102 DISALLOW_COPY_AND_ASSIGN(BrowserWindowHandler);
105 base::LazyInstance<BrowserWindowHandler> browser_window_handler
106 = LAZY_INSTANCE_INITIALIZER;
110 void InitAppSession(Profile* profile, const std::string& app_id) {
111 // Binds the session lifetime with app window counts.
112 CHECK(app_window_handler == NULL);
113 app_window_handler.Get().Init(profile);
115 CHECK(browser_window_handler == NULL);
116 browser_window_handler.Get();
118 // For a demo app, we don't need to either setup the update service or
119 // the idle app name notification.
120 if (DemoAppLauncher::IsDemoAppSession(
121 chromeos::UserManager::Get()->GetActiveUser()->email()))
124 // Set the app_id for the current instance of KioskAppUpdateService.
125 KioskAppUpdateService* update_service =
126 KioskAppUpdateServiceFactory::GetForProfile(profile);
127 DCHECK(update_service);
129 update_service->set_app_id(app_id);
131 // If the device is not enterprise managed, set prefs to reboot after update
132 // and create a user security message which shows the user the application
133 // name and author after some idle timeout.
134 policy::BrowserPolicyConnectorChromeOS* connector =
135 g_browser_process->platform_part()->browser_policy_connector_chromeos();
136 if (!connector->IsEnterpriseManaged()) {
137 PrefService* local_state = g_browser_process->local_state();
138 local_state->SetBoolean(prefs::kRebootAfterUpdate, true);
139 KioskModeIdleAppNameNotification::Initialize();
143 } // namespace chromeos