Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / chromeos / power / power_button_observer.cc
blobe7a27180f2dd82c9227fee2fe952dc8d4f80237e
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"
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"
18 namespace ash {
19 class LockStateControllerDelegate;
22 namespace chromeos {
24 namespace {
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();
33 } // namespace
35 PowerButtonObserver::PowerButtonObserver() {
36 ash::Shell::GetInstance()->lock_state_controller()->
37 SetDelegate(scoped_ptr<ash::LockStateControllerDelegate>(
38 new SessionStateControllerDelegateChromeos));
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());
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) {
72 switch (type) {
73 case chrome::NOTIFICATION_LOGIN_USER_CHANGED: {
74 ash::Shell::GetInstance()->OnLoginStateChanged(GetCurrentLoginStatus());
75 break;
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;
85 default:
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