Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / app_mode / app_session_lifetime.cc
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.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/chromeos/policy/browser_policy_connector_chromeos.h"
18 #include "chrome/browser/lifetime/application_lifetime.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"
27
28 using apps::ShellWindowRegistry;
29
30 namespace chromeos {
31
32 namespace {
33
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 {
39  public:
40   AppWindowHandler() : window_registry_(NULL) {}
41   virtual ~AppWindowHandler() {}
42
43   void Init(Profile* profile) {
44     DCHECK(!window_registry_);
45     window_registry_ = ShellWindowRegistry::Get(profile);
46     if (window_registry_)
47       window_registry_->AddObserver(this);
48   }
49
50  private:
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);
58   }
59   virtual void OnShellWindowIconChanged(apps::ShellWindow* shell_window)
60     OVERRIDE {}
61   virtual void OnShellWindowRemoved(apps::ShellWindow* shell_window) OVERRIDE {
62     if (window_registry_->shell_windows().empty()) {
63       chrome::AttemptUserExit();
64       window_registry_->RemoveObserver(this);
65     }
66   }
67
68   apps::ShellWindowRegistry* window_registry_;
69
70   DISALLOW_COPY_AND_ASSIGN(AppWindowHandler);
71 };
72
73 base::LazyInstance<AppWindowHandler> app_window_handler
74     = LAZY_INSTANCE_INITIALIZER;
75
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 {
80  public:
81   BrowserWindowHandler() {
82     BrowserList::AddObserver(this);
83   }
84   virtual ~BrowserWindowHandler() {
85     BrowserList::RemoveObserver(this);
86   }
87
88  private:
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;
96
97     browser->window()->Close();
98   }
99
100   // chrome::BrowserListObserver overrides:
101   virtual void OnBrowserAdded(Browser* browser) OVERRIDE {
102     base::MessageLoop::current()->PostTask(
103         FROM_HERE,
104         base::Bind(&BrowserWindowHandler::HandleBrowser,
105                    base::Unretained(this),  // LazyInstance, always valid
106                    browser));
107   }
108
109   DISALLOW_COPY_AND_ASSIGN(BrowserWindowHandler);
110 };
111
112 base::LazyInstance<BrowserWindowHandler> browser_window_handler
113     = LAZY_INSTANCE_INITIALIZER;
114
115 }  // namespace
116
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);
121
122   CHECK(browser_window_handler == NULL);
123   browser_window_handler.Get();
124
125   // Set the app_id for the current instance of KioskAppUpdateService.
126   KioskAppUpdateService* update_service =
127       KioskAppUpdateServiceFactory::GetForProfile(profile);
128   DCHECK(update_service);
129   if (update_service)
130     update_service->set_app_id(app_id);
131
132   // If the device is not enterprise managed, set prefs to reboot after update.
133   policy::BrowserPolicyConnectorChromeOS* connector =
134       g_browser_process->platform_part()->browser_policy_connector_chromeos();
135   if (!connector->IsEnterpriseManaged()) {
136     PrefService* local_state = g_browser_process->local_state();
137     local_state->SetBoolean(prefs::kRebootAfterUpdate, true);
138   }
139 }
140
141 }  // namespace chromeos