- add sources.
[platform/framework/web/crosswalk.git] / src / ash / system / chromeos / power / power_event_observer.cc
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_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 "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"
15
16 namespace ash {
17 namespace internal {
18
19 PowerEventObserver::PowerEventObserver()
20     : screen_locked_(false) {
21   chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
22       AddObserver(this);
23   chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
24       AddObserver(this);
25 }
26
27 PowerEventObserver::~PowerEventObserver() {
28   chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
29       RemoveObserver(this);
30   chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
31       RemoveObserver(this);
32 }
33
34 void PowerEventObserver::BrightnessChanged(int level, bool user_initiated) {
35   Shell::GetInstance()->power_button_controller()->OnScreenBrightnessChanged(
36       static_cast<double>(level));
37 }
38
39 void PowerEventObserver::SuspendImminent() {
40   Shell* shell = Shell::GetInstance();
41   SessionStateDelegate* delegate = shell->session_state_delegate();
42
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()->
51         RequestLockScreen();
52   }
53
54   shell->user_activity_detector()->OnDisplayPowerChanging();
55   shell->output_configurator()->SuspendDisplays();
56 }
57
58 void PowerEventObserver::SystemResumed(const base::TimeDelta& sleep_duration) {
59   Shell::GetInstance()->output_configurator()->ResumeDisplays();
60   Shell::GetInstance()->system_tray_notifier()->NotifyRefreshClock();
61 }
62
63 void PowerEventObserver::ScreenIsLocked() {
64   screen_locked_ = true;
65
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();
74   } else {
75     VLOG(1) << "Screen locked without suspend";
76   }
77 }
78
79 void PowerEventObserver::ScreenIsUnlocked() {
80   screen_locked_ = false;
81 }
82
83 }  // namespace internal
84 }  // namespace ash