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/shell_window.h"
8 #include "apps/shell_window_registry.h"
9 #include "ash/wm/window_state.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/lazy_instance.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/prefs/pref_service.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/chromeos/app_mode/kiosk_app_update_service.h"
17 #include "chrome/browser/lifetime/application_lifetime.h"
18 #include "chrome/browser/policy/browser_policy_connector.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/browser_list.h"
22 #include "chrome/browser/ui/browser_list_observer.h"
23 #include "chrome/browser/ui/browser_window.h"
24 #include "chrome/browser/ui/tabs/tab_strip_model.h"
25 #include "chrome/common/pref_names.h"
26 #include "content/public/browser/web_contents.h"
28 using apps::ShellWindowRegistry
;
34 // AppWindowHandler watches for app window and exits the session when the
35 // last app window is closed. It also initializes the kiosk app window so
36 // that it receives all function keys as a temp solution before underlying
37 // http:://crbug.com/166928 is fixed..
38 class AppWindowHandler
: public ShellWindowRegistry::Observer
{
40 AppWindowHandler() : window_registry_(NULL
) {}
41 virtual ~AppWindowHandler() {}
43 void Init(Profile
* profile
) {
44 DCHECK(!window_registry_
);
45 window_registry_
= ShellWindowRegistry::Get(profile
);
47 window_registry_
->AddObserver(this);
51 // apps::ShellWindowRegistry::Observer overrides:
52 virtual void OnShellWindowAdded(apps::ShellWindow
* shell_window
) OVERRIDE
{
53 // Set flags to allow kiosk app to receive all function keys.
54 // TODO(xiyuan): Remove this after http:://crbug.com/166928.
55 ash::wm::WindowState
* window_state
=
56 ash::wm::GetWindowState(shell_window
->GetNativeWindow());
57 window_state
->set_top_row_keys_are_function_keys(true);
59 virtual void OnShellWindowIconChanged(apps::ShellWindow
* shell_window
)
61 virtual void OnShellWindowRemoved(apps::ShellWindow
* shell_window
) OVERRIDE
{
62 if (window_registry_
->shell_windows().empty()) {
63 chrome::AttemptUserExit();
64 window_registry_
->RemoveObserver(this);
68 apps::ShellWindowRegistry
* window_registry_
;
70 DISALLOW_COPY_AND_ASSIGN(AppWindowHandler
);
73 base::LazyInstance
<AppWindowHandler
> app_window_handler
74 = LAZY_INSTANCE_INITIALIZER
;
76 // BrowserWindowHandler monitors Browser object being created during
77 // a kiosk session, log info such as URL so that the code path could be
78 // fixed and closes the just opened browser window.
79 class BrowserWindowHandler
: public chrome::BrowserListObserver
{
81 BrowserWindowHandler() {
82 BrowserList::AddObserver(this);
84 virtual ~BrowserWindowHandler() {
85 BrowserList::RemoveObserver(this);
89 void HandleBrowser(Browser
* browser
) {
90 content::WebContents
* active_tab
=
91 browser
->tab_strip_model()->GetActiveWebContents();
92 std::string url_string
=
93 active_tab
? active_tab
->GetURL().spec() : std::string();
94 LOG(WARNING
) << "Browser opened in kiosk session"
95 << ", url=" << url_string
;
97 browser
->window()->Close();
100 // chrome::BrowserListObserver overrides:
101 virtual void OnBrowserAdded(Browser
* browser
) OVERRIDE
{
102 base::MessageLoop::current()->PostTask(
104 base::Bind(&BrowserWindowHandler::HandleBrowser
,
105 base::Unretained(this), // LazyInstance, always valid
109 DISALLOW_COPY_AND_ASSIGN(BrowserWindowHandler
);
112 base::LazyInstance
<BrowserWindowHandler
> browser_window_handler
113 = LAZY_INSTANCE_INITIALIZER
;
117 void InitAppSession(Profile
* profile
, const std::string
& app_id
) {
118 // Binds the session lifetime with app window counts.
119 CHECK(app_window_handler
== NULL
);
120 app_window_handler
.Get().Init(profile
);
122 CHECK(browser_window_handler
== NULL
);
123 browser_window_handler
.Get();
125 // Set the app_id for the current instance of KioskAppUpdateService.
126 KioskAppUpdateService
* update_service
=
127 KioskAppUpdateServiceFactory::GetForProfile(profile
);
128 DCHECK(update_service
);
130 update_service
->set_app_id(app_id
);
132 // If the device is not enterprise managed, set prefs to reboot after update.
133 if (!g_browser_process
->browser_policy_connector()->IsEnterpriseManaged()) {
134 PrefService
* local_state
= g_browser_process
->local_state();
135 local_state
->SetBoolean(prefs::kRebootAfterUpdate
, true);
139 } // namespace chromeos