Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / chromeos / app_mode / app_session_lifetime.cc
blob741a1c13e6d802a15c37749b19517fccffcb614c
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 "base/basictypes.h"
8 #include "base/bind.h"
9 #include "base/lazy_instance.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/pref_service.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
14 #include "chrome/browser/chromeos/app_mode/kiosk_app_update_service.h"
15 #include "chrome/browser/chromeos/app_mode/kiosk_mode_idle_app_name_notification.h"
16 #include "chrome/browser/chromeos/login/demo_mode/demo_app_launcher.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 "chromeos/network/network_state.h"
27 #include "chromeos/network/network_state_handler.h"
28 #include "components/user_manager/user_manager.h"
29 #include "content/public/browser/web_contents.h"
30 #include "extensions/browser/app_window/app_window.h"
31 #include "extensions/browser/app_window/app_window_registry.h"
33 using extensions::AppWindow;
34 using extensions::AppWindowRegistry;
36 namespace chromeos {
38 namespace {
40 // AppWindowHandler watches for app window and exits the session when the
41 // last window of a given app is closed.
42 class AppWindowHandler : public AppWindowRegistry::Observer {
43 public:
44 AppWindowHandler() : window_registry_(NULL) {}
45 ~AppWindowHandler() override {}
47 void Init(Profile* profile, const std::string& app_id) {
48 DCHECK(!window_registry_);
49 window_registry_ = AppWindowRegistry::Get(profile);
50 if (window_registry_)
51 window_registry_->AddObserver(this);
52 app_id_ = app_id;
55 private:
56 // extensions::AppWindowRegistry::Observer overrides:
57 void OnAppWindowRemoved(AppWindow* app_window) override {
58 if (window_registry_->GetAppWindowsForApp(app_id_).empty()) {
59 if (DemoAppLauncher::IsDemoAppSession(
60 user_manager::UserManager::Get()->GetActiveUser()->email())) {
61 // If we were in demo mode, we disabled all our network technologies,
62 // re-enable them.
63 NetworkStateHandler* handler =
64 NetworkHandler::Get()->network_state_handler();
65 handler->SetTechnologyEnabled(
66 NetworkTypePattern::NonVirtual(),
67 true,
68 chromeos::network_handler::ErrorCallback());
70 chrome::AttemptUserExit();
71 window_registry_->RemoveObserver(this);
75 AppWindowRegistry* window_registry_;
76 std::string app_id_;
78 DISALLOW_COPY_AND_ASSIGN(AppWindowHandler);
81 base::LazyInstance<AppWindowHandler> app_window_handler
82 = LAZY_INSTANCE_INITIALIZER;
84 // BrowserWindowHandler monitors Browser object being created during
85 // a kiosk session, log info such as URL so that the code path could be
86 // fixed and closes the just opened browser window.
87 class BrowserWindowHandler : public chrome::BrowserListObserver {
88 public:
89 BrowserWindowHandler() {
90 BrowserList::AddObserver(this);
92 ~BrowserWindowHandler() override { BrowserList::RemoveObserver(this); }
94 private:
95 void HandleBrowser(Browser* browser) {
96 content::WebContents* active_tab =
97 browser->tab_strip_model()->GetActiveWebContents();
98 std::string url_string =
99 active_tab ? active_tab->GetURL().spec() : std::string();
100 LOG(WARNING) << "Browser opened in kiosk session"
101 << ", url=" << url_string;
103 browser->window()->Close();
106 // chrome::BrowserListObserver overrides:
107 void OnBrowserAdded(Browser* browser) override {
108 base::MessageLoop::current()->PostTask(
109 FROM_HERE,
110 base::Bind(&BrowserWindowHandler::HandleBrowser,
111 base::Unretained(this), // LazyInstance, always valid
112 browser));
115 DISALLOW_COPY_AND_ASSIGN(BrowserWindowHandler);
118 base::LazyInstance<BrowserWindowHandler> browser_window_handler
119 = LAZY_INSTANCE_INITIALIZER;
121 } // namespace
123 void InitAppSession(Profile* profile, const std::string& app_id) {
124 // Binds the session lifetime with app window counts.
125 CHECK(app_window_handler == NULL);
126 app_window_handler.Get().Init(profile, app_id);
128 CHECK(browser_window_handler == NULL);
129 browser_window_handler.Get();
131 // For a demo app, we don't need to either setup the update service or
132 // the idle app name notification.
133 if (DemoAppLauncher::IsDemoAppSession(
134 user_manager::UserManager::Get()->GetActiveUser()->email()))
135 return;
137 // Set the app_id for the current instance of KioskAppUpdateService.
138 KioskAppUpdateService* update_service =
139 KioskAppUpdateServiceFactory::GetForProfile(profile);
140 DCHECK(update_service);
141 if (update_service)
142 update_service->Init(app_id);
144 // Start to monitor external update from usb stick.
145 KioskAppManager::Get()->MonitorKioskExternalUpdate();
147 // If the device is not enterprise managed, set prefs to reboot after update
148 // and create a user security message which shows the user the application
149 // name and author after some idle timeout.
150 policy::BrowserPolicyConnectorChromeOS* connector =
151 g_browser_process->platform_part()->browser_policy_connector_chromeos();
152 if (!connector->IsEnterpriseManaged()) {
153 PrefService* local_state = g_browser_process->local_state();
154 local_state->SetBoolean(prefs::kRebootAfterUpdate, true);
155 KioskModeIdleAppNameNotification::Initialize();
159 } // namespace chromeos