e7a27180f2dd82c9227fee2fe952dc8d4f80237e
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / power / power_button_observer.cc
1 // Copyright (c) 2012 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/power/power_button_observer.h"
6
7 #include "ash/shell.h"
8 #include "ash/system/tray/system_tray_delegate.h"
9 #include "ash/system/user/login_status.h"
10 #include "ash/wm/power_button_controller.h"
11 #include "base/logging.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/chromeos/login/lock/screen_locker.h"
14 #include "chrome/browser/chromeos/power/session_state_controller_delegate_chromeos.h"
15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "content/public/browser/notification_service.h"
17
18 namespace ash {
19 class LockStateControllerDelegate;
20 }
21
22 namespace chromeos {
23
24 namespace {
25
26 ash::user::LoginStatus GetCurrentLoginStatus() {
27   if (!ash::Shell::GetInstance()->system_tray_delegate())
28     return ash::user::LOGGED_IN_NONE;
29   return ash::Shell::GetInstance()->system_tray_delegate()->
30       GetUserLoginStatus();
31 }
32
33 }  // namespace
34
35 PowerButtonObserver::PowerButtonObserver() {
36   ash::Shell::GetInstance()->lock_state_controller()->
37       SetDelegate(scoped_ptr<ash::LockStateControllerDelegate>(
38           new SessionStateControllerDelegateChromeos));
39
40   registrar_.Add(
41       this,
42       chrome::NOTIFICATION_LOGIN_USER_CHANGED,
43       content::NotificationService::AllSources());
44   registrar_.Add(
45       this,
46       chrome::NOTIFICATION_APP_TERMINATING,
47       content::NotificationService::AllSources());
48   registrar_.Add(
49       this,
50       chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
51       content::NotificationService::AllSources());
52
53   DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this);
54   DBusThreadManager::Get()->GetSessionManagerClient()->AddObserver(this);
55
56   // Tell the controller about the initial state.
57   ash::Shell::GetInstance()->OnLoginStateChanged(GetCurrentLoginStatus());
58
59   const ScreenLocker* locker = ScreenLocker::default_screen_locker();
60   bool locked = locker && locker->locked();
61   ash::Shell::GetInstance()->OnLockStateChanged(locked);
62 }
63
64 PowerButtonObserver::~PowerButtonObserver() {
65   DBusThreadManager::Get()->GetSessionManagerClient()->RemoveObserver(this);
66   DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this);
67 }
68
69 void PowerButtonObserver::Observe(int type,
70                                   const content::NotificationSource& source,
71                                   const content::NotificationDetails& details) {
72   switch (type) {
73     case chrome::NOTIFICATION_LOGIN_USER_CHANGED: {
74       ash::Shell::GetInstance()->OnLoginStateChanged(GetCurrentLoginStatus());
75       break;
76     }
77     case chrome::NOTIFICATION_APP_TERMINATING:
78       ash::Shell::GetInstance()->OnAppTerminating();
79       break;
80     case chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED: {
81       bool locked = *content::Details<bool>(details).ptr();
82       ash::Shell::GetInstance()->OnLockStateChanged(locked);
83       break;
84     }
85     default:
86       NOTREACHED() << "Unexpected notification " << type;
87   }
88 }
89
90 void PowerButtonObserver::PowerButtonEventReceived(
91     bool down, const base::TimeTicks& timestamp) {
92   ash::Shell::GetInstance()->power_button_controller()->
93       OnPowerButtonEvent(down, timestamp);
94 }
95
96 }  // namespace chromeos