Home | History | Annotate | Download | only in power
      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.
      4 
      5 #include "ash/system/chromeos/power/power_event_observer.h"
      6 
      7 #include "ash/session/session_state_delegate.h"
      8 #include "ash/shell.h"
      9 #include "ash/system/tray/system_tray_notifier.h"
     10 #include "ash/wm/power_button_controller.h"
     11 #include "base/prefs/pref_service.h"
     12 #include "chromeos/dbus/dbus_thread_manager.h"
     13 #include "ui/display/chromeos/display_configurator.h"
     14 #include "ui/wm/core/user_activity_detector.h"
     15 
     16 namespace ash {
     17 
     18 PowerEventObserver::PowerEventObserver()
     19     : screen_locked_(false) {
     20   chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
     21       AddObserver(this);
     22   chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
     23       AddObserver(this);
     24 }
     25 
     26 PowerEventObserver::~PowerEventObserver() {
     27   chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
     28       RemoveObserver(this);
     29   chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
     30       RemoveObserver(this);
     31 }
     32 
     33 void PowerEventObserver::BrightnessChanged(int level, bool user_initiated) {
     34   Shell::GetInstance()->power_button_controller()->OnScreenBrightnessChanged(
     35       static_cast<double>(level));
     36 }
     37 
     38 void PowerEventObserver::SuspendImminent() {
     39   Shell* shell = Shell::GetInstance();
     40   SessionStateDelegate* delegate = shell->session_state_delegate();
     41 
     42   // If the lock-before-suspending pref is set, get a callback to block
     43   // suspend and ask the session manager to lock the screen.
     44   if (!screen_locked_ && delegate->ShouldLockScreenBeforeSuspending() &&
     45       delegate->CanLockScreen()) {
     46     screen_lock_callback_ = chromeos::DBusThreadManager::Get()->
     47         GetPowerManagerClient()->GetSuspendReadinessCallback();
     48     VLOG(1) << "Requesting screen lock from PowerEventObserver";
     49     chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
     50         RequestLockScreen();
     51   }
     52 
     53   shell->user_activity_detector()->OnDisplayPowerChanging();
     54   shell->display_configurator()->SuspendDisplays();
     55 }
     56 
     57 void PowerEventObserver::SuspendDone(const base::TimeDelta& sleep_duration) {
     58   Shell::GetInstance()->display_configurator()->ResumeDisplays();
     59   Shell::GetInstance()->system_tray_notifier()->NotifyRefreshClock();
     60 }
     61 
     62 void PowerEventObserver::ScreenIsLocked() {
     63   screen_locked_ = true;
     64 
     65   // Stop blocking suspend after the screen is locked.
     66   if (!screen_lock_callback_.is_null()) {
     67     VLOG(1) << "Screen locked due to suspend";
     68     // Run the callback asynchronously.  ScreenIsLocked() is currently
     69     // called asynchronously after RequestLockScreen(), but this guards
     70     // against it being made synchronous later.
     71     base::MessageLoop::current()->PostTask(FROM_HERE, screen_lock_callback_);
     72     screen_lock_callback_.Reset();
     73   } else {
     74     VLOG(1) << "Screen locked without suspend";
     75   }
     76 }
     77 
     78 void PowerEventObserver::ScreenIsUnlocked() {
     79   screen_locked_ = false;
     80 }
     81 
     82 }  // namespace ash
     83