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.
5 #include "chrome/browser/chromeos/power/power_button_observer.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"
19 class LockStateControllerDelegate
;
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()->
35 PowerButtonObserver::PowerButtonObserver() {
36 ash::Shell::GetInstance()->lock_state_controller()->
37 SetDelegate(scoped_ptr
<ash::LockStateControllerDelegate
>(
38 new SessionStateControllerDelegateChromeos
));
42 chrome::NOTIFICATION_LOGIN_USER_CHANGED
,
43 content::NotificationService::AllSources());
46 chrome::NOTIFICATION_APP_TERMINATING
,
47 content::NotificationService::AllSources());
50 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED
,
51 content::NotificationService::AllSources());
53 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this);
54 DBusThreadManager::Get()->GetSessionManagerClient()->AddObserver(this);
56 // Tell the controller about the initial state.
57 ash::Shell::GetInstance()->OnLoginStateChanged(GetCurrentLoginStatus());
59 const ScreenLocker
* locker
= ScreenLocker::default_screen_locker();
60 bool locked
= locker
&& locker
->locked();
61 ash::Shell::GetInstance()->OnLockStateChanged(locked
);
64 PowerButtonObserver::~PowerButtonObserver() {
65 DBusThreadManager::Get()->GetSessionManagerClient()->RemoveObserver(this);
66 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this);
69 void PowerButtonObserver::Observe(int type
,
70 const content::NotificationSource
& source
,
71 const content::NotificationDetails
& details
) {
73 case chrome::NOTIFICATION_LOGIN_USER_CHANGED
: {
74 ash::Shell::GetInstance()->OnLoginStateChanged(GetCurrentLoginStatus());
77 case chrome::NOTIFICATION_APP_TERMINATING
:
78 ash::Shell::GetInstance()->OnAppTerminating();
80 case chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED
: {
81 bool locked
= *content::Details
<bool>(details
).ptr();
82 ash::Shell::GetInstance()->OnLockStateChanged(locked
);
86 NOTREACHED() << "Unexpected notification " << type
;
90 void PowerButtonObserver::PowerButtonEventReceived(
91 bool down
, const base::TimeTicks
& timestamp
) {
92 ash::Shell::GetInstance()->power_button_controller()->
93 OnPowerButtonEvent(down
, timestamp
);
96 } // namespace chromeos