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.
5 #include "ash/system/chromeos/power/power_event_observer.h"
7 #include "ash/session_state_delegate.h"
9 #include "ash/system/tray/system_tray_notifier.h"
10 #include "ash/wm/power_button_controller.h"
11 #include "ash/wm/user_activity_detector.h"
12 #include "base/prefs/pref_service.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/display/output_configurator.h"
19 PowerEventObserver::PowerEventObserver()
20 : screen_locked_(false) {
21 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
23 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
27 PowerEventObserver::~PowerEventObserver() {
28 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
30 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
34 void PowerEventObserver::BrightnessChanged(int level, bool user_initiated) {
35 Shell::GetInstance()->power_button_controller()->OnScreenBrightnessChanged(
36 static_cast<double>(level));
39 void PowerEventObserver::SuspendImminent() {
40 Shell* shell = Shell::GetInstance();
41 SessionStateDelegate* delegate = shell->session_state_delegate();
43 // If the lock-before-suspending pref is set, get a callback to block
44 // suspend and ask the session manager to lock the screen.
45 if (!screen_locked_ && delegate->ShouldLockScreenBeforeSuspending() &&
46 delegate->CanLockScreen()) {
47 screen_lock_callback_ = chromeos::DBusThreadManager::Get()->
48 GetPowerManagerClient()->GetSuspendReadinessCallback();
49 VLOG(1) << "Requesting screen lock from PowerEventObserver";
50 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
54 shell->user_activity_detector()->OnDisplayPowerChanging();
55 shell->output_configurator()->SuspendDisplays();
58 void PowerEventObserver::SystemResumed(const base::TimeDelta& sleep_duration) {
59 Shell::GetInstance()->output_configurator()->ResumeDisplays();
60 Shell::GetInstance()->system_tray_notifier()->NotifyRefreshClock();
63 void PowerEventObserver::ScreenIsLocked() {
64 screen_locked_ = true;
66 // Stop blocking suspend after the screen is locked.
67 if (!screen_lock_callback_.is_null()) {
68 VLOG(1) << "Screen locked due to suspend";
69 // Run the callback asynchronously. ScreenIsLocked() is currently
70 // called asynchronously after RequestLockScreen(), but this guards
71 // against it being made synchronous later.
72 base::MessageLoop::current()->PostTask(FROM_HERE, screen_lock_callback_);
73 screen_lock_callback_.Reset();
75 VLOG(1) << "Screen locked without suspend";
79 void PowerEventObserver::ScreenIsUnlocked() {
80 screen_locked_ = false;
83 } // namespace internal