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/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 18namespace chromeos { 19 20namespace { 21 22ash::user::LoginStatus GetCurrentLoginStatus() { 23 if (!ash::Shell::GetInstance()->system_tray_delegate()) 24 return ash::user::LOGGED_IN_NONE; 25 return ash::Shell::GetInstance()->system_tray_delegate()-> 26 GetUserLoginStatus(); 27} 28 29} // namespace 30 31PowerButtonObserver::PowerButtonObserver() { 32 ash::Shell::GetInstance()->lock_state_controller()-> 33 SetDelegate(new SessionStateControllerDelegateChromeos); 34 35 registrar_.Add( 36 this, 37 chrome::NOTIFICATION_LOGIN_USER_CHANGED, 38 content::NotificationService::AllSources()); 39 registrar_.Add( 40 this, 41 chrome::NOTIFICATION_APP_TERMINATING, 42 content::NotificationService::AllSources()); 43 registrar_.Add( 44 this, 45 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, 46 content::NotificationService::AllSources()); 47 48 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this); 49 DBusThreadManager::Get()->GetSessionManagerClient()->AddObserver(this); 50 51 // Tell the controller about the initial state. 52 ash::Shell::GetInstance()->OnLoginStateChanged(GetCurrentLoginStatus()); 53 54 const ScreenLocker* locker = ScreenLocker::default_screen_locker(); 55 bool locked = locker && locker->locked(); 56 ash::Shell::GetInstance()->OnLockStateChanged(locked); 57} 58 59PowerButtonObserver::~PowerButtonObserver() { 60 DBusThreadManager::Get()->GetSessionManagerClient()->RemoveObserver(this); 61 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this); 62} 63 64void PowerButtonObserver::Observe(int type, 65 const content::NotificationSource& source, 66 const content::NotificationDetails& details) { 67 switch (type) { 68 case chrome::NOTIFICATION_LOGIN_USER_CHANGED: { 69 ash::Shell::GetInstance()->OnLoginStateChanged(GetCurrentLoginStatus()); 70 break; 71 } 72 case chrome::NOTIFICATION_APP_TERMINATING: 73 ash::Shell::GetInstance()->OnAppTerminating(); 74 break; 75 case chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED: { 76 bool locked = *content::Details<bool>(details).ptr(); 77 ash::Shell::GetInstance()->OnLockStateChanged(locked); 78 break; 79 } 80 default: 81 NOTREACHED() << "Unexpected notification " << type; 82 } 83} 84 85void PowerButtonObserver::PowerButtonEventReceived( 86 bool down, const base::TimeTicks& timestamp) { 87 ash::Shell::GetInstance()->power_button_controller()-> 88 OnPowerButtonEvent(down, timestamp); 89} 90 91void PowerButtonObserver::LockScreen() { 92 ash::Shell::GetInstance()->lock_state_controller()->OnStartingLock(); 93} 94 95} // namespace chromeos 96