Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / ash / system / chromeos / power / power_event_observer.cc
blob1510ddd69303de1040aab6c1179f4137fa1c7991
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/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/aura/window.h"
14 #include "ui/aura/window_tree_host.h"
15 #include "ui/base/user_activity/user_activity_detector.h"
16 #include "ui/compositor/compositor.h"
17 #include "ui/display/chromeos/display_configurator.h"
19 namespace ash {
21 namespace {
23 // Tells the compositor for each of the displays to finish all pending
24 // rendering requests and block any new ones.
25 void StopRenderingRequests() {
26 for (aura::Window* window : Shell::GetAllRootWindows()) {
27 ui::Compositor* compositor = window->GetHost()->compositor();
28 compositor->SetVisible(false);
29 compositor->FinishAllRendering();
33 // Tells the compositor for each of the displays to resume sending rendering
34 // requests to the GPU.
35 void ResumeRenderingRequests() {
36 for (aura::Window* window : Shell::GetAllRootWindows())
37 window->GetHost()->compositor()->SetVisible(true);
40 } // namespace
42 PowerEventObserver::PowerEventObserver()
43 : screen_locked_(false), waiting_for_lock_screen_animations_(false) {
44 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
45 AddObserver(this);
46 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
47 AddObserver(this);
50 PowerEventObserver::~PowerEventObserver() {
51 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
52 RemoveObserver(this);
53 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
54 RemoveObserver(this);
57 void PowerEventObserver::OnLockAnimationsComplete() {
58 VLOG(1) << "Screen locker animations have completed.";
59 waiting_for_lock_screen_animations_ = false;
61 if (!screen_lock_callback_.is_null()) {
62 StopRenderingRequests();
64 screen_lock_callback_.Run();
65 screen_lock_callback_.Reset();
69 void PowerEventObserver::BrightnessChanged(int level, bool user_initiated) {
70 Shell::GetInstance()->power_button_controller()->OnScreenBrightnessChanged(
71 static_cast<double>(level));
74 void PowerEventObserver::SuspendImminent() {
75 Shell* shell = Shell::GetInstance();
76 SessionStateDelegate* delegate = shell->session_state_delegate();
78 // This class is responsible for disabling all rendering requests at suspend
79 // time and then enabling them at resume time. When the
80 // lock-before-suspending pref is not set this is easy to do since
81 // StopRenderingRequests() is just called directly from this function. If the
82 // lock-before-suspending pref _is_ set, then the suspend needs to be delayed
83 // until the lock screen is fully visible. While it is sufficient from a
84 // security perspective to block only until the lock screen is ready, which
85 // guarantees that the contents of the user's screen are no longer visible,
86 // this leads to poor UX on the first resume since neither the user pod nor
87 // the header bar will be visible for a few hundred milliseconds until the GPU
88 // process starts rendering again. To deal with this, the suspend is delayed
89 // until all the lock screen animations have completed and the suspend request
90 // is unblocked from OnLockAnimationsComplete().
91 if (!screen_locked_ && delegate->ShouldLockScreenBeforeSuspending() &&
92 delegate->CanLockScreen()) {
93 screen_lock_callback_ = chromeos::DBusThreadManager::Get()->
94 GetPowerManagerClient()->GetSuspendReadinessCallback();
95 VLOG(1) << "Requesting screen lock from PowerEventObserver";
96 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
97 RequestLockScreen();
98 } else if (waiting_for_lock_screen_animations_) {
99 // The lock-before-suspending pref has been set and the lock screen is ready
100 // but the animations have not completed yet. This can happen if a suspend
101 // request is canceled after the lock screen is ready but before the
102 // animations have completed and then another suspend request is immediately
103 // started. In practice, it is highly unlikely that this will ever happen
104 // but it's better to be safe since the cost of not dealing with it properly
105 // is a memory leak in the GPU and weird artifacts on the screen.
106 screen_lock_callback_ = chromeos::DBusThreadManager::Get()
107 ->GetPowerManagerClient()
108 ->GetSuspendReadinessCallback();
109 } else {
110 // The lock-before-suspending pref is not set or the screen has already been
111 // locked and the animations have completed. Rendering can be stopped now.
112 StopRenderingRequests();
115 ui::UserActivityDetector::Get()->OnDisplayPowerChanging();
116 shell->display_configurator()->SuspendDisplays();
119 void PowerEventObserver::SuspendDone(const base::TimeDelta& sleep_duration) {
120 Shell::GetInstance()->display_configurator()->ResumeDisplays();
121 Shell::GetInstance()->system_tray_notifier()->NotifyRefreshClock();
123 // If the suspend request was being blocked while waiting for the lock
124 // animation to complete, clear the blocker since the suspend has already
125 // completed. This prevents rendering requests from being blocked after a
126 // resume if the lock screen took too long to show.
127 screen_lock_callback_.Reset();
129 ResumeRenderingRequests();
132 void PowerEventObserver::ScreenIsLocked() {
133 screen_locked_ = true;
134 waiting_for_lock_screen_animations_ = true;
136 // The screen is now locked but the pending suspend, if any, will be blocked
137 // until all the animations have completed.
138 if (!screen_lock_callback_.is_null()) {
139 VLOG(1) << "Screen locked due to suspend";
140 } else {
141 VLOG(1) << "Screen locked without suspend";
145 void PowerEventObserver::ScreenIsUnlocked() {
146 screen_locked_ = false;
149 } // namespace ash