Upstream version 5.34.104.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/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"
29
30 using apps::AppWindowRegistry;
31
32 namespace chromeos {
33
34 namespace {
35
36 // AppWindowHandler watches for app window and exits the session when the
37 // last app window is closed.
38 class AppWindowHandler : public AppWindowRegistry::Observer {
39  public:
40   AppWindowHandler() : window_registry_(NULL) {}
41   virtual ~AppWindowHandler() {}
42
43   void Init(Profile* profile) {
44     DCHECK(!window_registry_);
45     window_registry_ = AppWindowRegistry::Get(profile);
46     if (window_registry_)
47       window_registry_->AddObserver(this);
48   }
49
50  private:
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);
58     }
59   }
60
61   apps::AppWindowRegistry* window_registry_;
62
63   DISALLOW_COPY_AND_ASSIGN(AppWindowHandler);
64 };
65
66 base::LazyInstance<AppWindowHandler> app_window_handler
67     = LAZY_INSTANCE_INITIALIZER;
68
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 {
73  public:
74   BrowserWindowHandler() {
75     BrowserList::AddObserver(this);
76   }
77   virtual ~BrowserWindowHandler() {
78     BrowserList::RemoveObserver(this);
79   }
80
81  private:
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;
89
90     browser->window()->Close();
91   }
92
93   // chrome::BrowserListObserver overrides:
94   virtual void OnBrowserAdded(Browser* browser) OVERRIDE {
95     base::MessageLoop::current()->PostTask(
96         FROM_HERE,
97         base::Bind(&BrowserWindowHandler::HandleBrowser,
98                    base::Unretained(this),  // LazyInstance, always valid
99                    browser));
100   }
101
102   DISALLOW_COPY_AND_ASSIGN(BrowserWindowHandler);
103 };
104
105 base::LazyInstance<BrowserWindowHandler> browser_window_handler
106     = LAZY_INSTANCE_INITIALIZER;
107
108 }  // namespace
109
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);
114
115   CHECK(browser_window_handler == NULL);
116   browser_window_handler.Get();
117
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()))
122     return;
123
124   // Set the app_id for the current instance of KioskAppUpdateService.
125   KioskAppUpdateService* update_service =
126       KioskAppUpdateServiceFactory::GetForProfile(profile);
127   DCHECK(update_service);
128   if (update_service)
129     update_service->set_app_id(app_id);
130
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();
140   }
141 }
142
143 }  // namespace chromeos