- add sources.
[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/lazy_instance.h"
12 #include "base/prefs/pref_service.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chromeos/app_mode/kiosk_app_update_service.h"
15 #include "chrome/browser/lifetime/application_lifetime.h"
16 #include "chrome/browser/policy/browser_policy_connector.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/common/pref_names.h"
19
20 using apps::ShellWindowRegistry;
21
22 namespace chromeos {
23
24 namespace {
25
26 // AppWindowHandler watches for app window and exits the session when the
27 // last app window is closed. It also initializes the kiosk app window so
28 // that it receives all function keys as a temp solution before underlying
29 // http:://crbug.com/166928 is fixed..
30 class AppWindowHandler : public ShellWindowRegistry::Observer {
31  public:
32   AppWindowHandler() : window_registry_(NULL) {}
33   virtual ~AppWindowHandler() {}
34
35   void Init(Profile* profile) {
36     DCHECK(!window_registry_);
37     window_registry_ = ShellWindowRegistry::Get(profile);
38     if (window_registry_)
39       window_registry_->AddObserver(this);
40   }
41
42  private:
43   // apps::ShellWindowRegistry::Observer overrides:
44   virtual void OnShellWindowAdded(apps::ShellWindow* shell_window) OVERRIDE {
45     // Set flags to allow kiosk app to receive all function keys.
46     // TODO(xiyuan): Remove this after http:://crbug.com/166928.
47     ash::wm::WindowState* window_state =
48         ash::wm::GetWindowState(shell_window->GetNativeWindow());
49     window_state->set_top_row_keys_are_function_keys(true);
50   }
51   virtual void OnShellWindowIconChanged(apps::ShellWindow* shell_window)
52     OVERRIDE {}
53   virtual void OnShellWindowRemoved(apps::ShellWindow* shell_window) OVERRIDE {
54     if (window_registry_->shell_windows().empty()) {
55       chrome::AttemptUserExit();
56       window_registry_->RemoveObserver(this);
57     }
58   }
59
60   apps::ShellWindowRegistry* window_registry_;
61
62   DISALLOW_COPY_AND_ASSIGN(AppWindowHandler);
63 };
64
65 base::LazyInstance<AppWindowHandler> app_window_handler
66     = LAZY_INSTANCE_INITIALIZER;
67
68 }  // namespace
69
70 void InitAppSession(Profile* profile, const std::string& app_id) {
71   // Binds the session lifetime with app window counts.
72   CHECK(app_window_handler == NULL);
73   app_window_handler.Get().Init(profile);
74
75   // Set the app_id for the current instance of KioskAppUpdateService.
76   KioskAppUpdateService* update_service =
77       KioskAppUpdateServiceFactory::GetForProfile(profile);
78   DCHECK(update_service);
79   if (update_service)
80     update_service->set_app_id(app_id);
81
82   // If the device is not enterprise managed, set prefs to reboot after update.
83   if (!g_browser_process->browser_policy_connector()->IsEnterpriseManaged()) {
84     PrefService* local_state = g_browser_process->local_state();
85     local_state->SetBoolean(prefs::kRebootAfterUpdate, true);
86   }
87 }
88
89 }  // namespace chromeos