OLD | NEW |
| (Empty) |
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/common/session/session_state_delegate.h" | |
8 #include "ash/common/system/tray/system_tray_notifier.h" | |
9 #include "ash/common/wm_shell.h" | |
10 #include "ash/shell.h" | |
11 #include "ash/wm/power_button_controller.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/manager/chromeos/display_configurator.h" | |
18 | |
19 namespace ash { | |
20 | |
21 namespace { | |
22 | |
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 } | |
30 } | |
31 | |
32 // Tells the compositor for each of the displays to resume sending rendering | |
33 // requests to the GPU. | |
34 void ResumeRenderingRequests() { | |
35 for (aura::Window* window : Shell::GetAllRootWindows()) | |
36 window->GetHost()->compositor()->SetVisible(true); | |
37 } | |
38 | |
39 void OnSuspendDisplaysCompleted(const base::Closure& suspend_callback, | |
40 bool status) { | |
41 suspend_callback.Run(); | |
42 } | |
43 | |
44 } // namespace | |
45 | |
46 PowerEventObserver::PowerEventObserver() | |
47 : screen_locked_(false), waiting_for_lock_screen_animations_(false) { | |
48 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( | |
49 this); | |
50 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->AddObserver( | |
51 this); | |
52 } | |
53 | |
54 PowerEventObserver::~PowerEventObserver() { | |
55 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( | |
56 this); | |
57 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->RemoveObserver( | |
58 this); | |
59 } | |
60 | |
61 void PowerEventObserver::OnLockAnimationsComplete() { | |
62 VLOG(1) << "Screen locker animations have completed."; | |
63 waiting_for_lock_screen_animations_ = false; | |
64 | |
65 if (!screen_lock_callback_.is_null()) { | |
66 StopRenderingRequests(); | |
67 | |
68 screen_lock_callback_.Run(); | |
69 screen_lock_callback_.Reset(); | |
70 } | |
71 } | |
72 | |
73 void PowerEventObserver::BrightnessChanged(int level, bool user_initiated) { | |
74 Shell::GetInstance()->power_button_controller()->OnScreenBrightnessChanged( | |
75 static_cast<double>(level)); | |
76 } | |
77 | |
78 void PowerEventObserver::SuspendImminent() { | |
79 SessionStateDelegate* delegate = WmShell::Get()->GetSessionStateDelegate(); | |
80 | |
81 // This class is responsible for disabling all rendering requests at suspend | |
82 // time and then enabling them at resume time. When the | |
83 // auto-screen-lock pref is not set this is easy to do since | |
84 // StopRenderingRequests() is just called directly from this function. If the | |
85 // auto-screen-lock pref _is_ set, then the suspend needs to be delayed | |
86 // until the lock screen is fully visible. While it is sufficient from a | |
87 // security perspective to block only until the lock screen is ready, which | |
88 // guarantees that the contents of the user's screen are no longer visible, | |
89 // this leads to poor UX on the first resume since neither the user pod nor | |
90 // the header bar will be visible for a few hundred milliseconds until the GPU | |
91 // process starts rendering again. To deal with this, the suspend is delayed | |
92 // until all the lock screen animations have completed and the suspend request | |
93 // is unblocked from OnLockAnimationsComplete(). | |
94 if (!screen_locked_ && delegate->ShouldLockScreenAutomatically() && | |
95 delegate->CanLockScreen()) { | |
96 screen_lock_callback_ = chromeos::DBusThreadManager::Get() | |
97 ->GetPowerManagerClient() | |
98 ->GetSuspendReadinessCallback(); | |
99 VLOG(1) << "Requesting screen lock from PowerEventObserver"; | |
100 chromeos::DBusThreadManager::Get() | |
101 ->GetSessionManagerClient() | |
102 ->RequestLockScreen(); | |
103 } else if (waiting_for_lock_screen_animations_) { | |
104 // The lock-before-suspending pref has been set and the lock screen is ready | |
105 // but the animations have not completed yet. This can happen if a suspend | |
106 // request is canceled after the lock screen is ready but before the | |
107 // animations have completed and then another suspend request is immediately | |
108 // started. In practice, it is highly unlikely that this will ever happen | |
109 // but it's better to be safe since the cost of not dealing with it properly | |
110 // is a memory leak in the GPU and weird artifacts on the screen. | |
111 screen_lock_callback_ = chromeos::DBusThreadManager::Get() | |
112 ->GetPowerManagerClient() | |
113 ->GetSuspendReadinessCallback(); | |
114 } else { | |
115 // The lock-before-suspending pref is not set or the screen has already been | |
116 // locked and the animations have completed. Rendering can be stopped now. | |
117 StopRenderingRequests(); | |
118 } | |
119 | |
120 ui::UserActivityDetector::Get()->OnDisplayPowerChanging(); | |
121 | |
122 // TODO(derat): After mus exposes a method for suspending displays, call it | |
123 // here: http://crbug.com/692193 | |
124 if (!WmShell::Get()->IsRunningInMash()) { | |
125 Shell::GetInstance()->display_configurator()->SuspendDisplays(base::Bind( | |
126 &OnSuspendDisplaysCompleted, chromeos::DBusThreadManager::Get() | |
127 ->GetPowerManagerClient() | |
128 ->GetSuspendReadinessCallback())); | |
129 } | |
130 } | |
131 | |
132 void PowerEventObserver::SuspendDone(const base::TimeDelta& sleep_duration) { | |
133 // TODO(derat): After mus exposes a method for resuming displays, call it | |
134 // here: http://crbug.com/692193 | |
135 if (!WmShell::Get()->IsRunningInMash()) | |
136 Shell::GetInstance()->display_configurator()->ResumeDisplays(); | |
137 WmShell::Get()->system_tray_notifier()->NotifyRefreshClock(); | |
138 | |
139 // If the suspend request was being blocked while waiting for the lock | |
140 // animation to complete, clear the blocker since the suspend has already | |
141 // completed. This prevents rendering requests from being blocked after a | |
142 // resume if the lock screen took too long to show. | |
143 screen_lock_callback_.Reset(); | |
144 | |
145 ResumeRenderingRequests(); | |
146 } | |
147 | |
148 void PowerEventObserver::ScreenIsLocked() { | |
149 screen_locked_ = true; | |
150 waiting_for_lock_screen_animations_ = true; | |
151 | |
152 // The screen is now locked but the pending suspend, if any, will be blocked | |
153 // until all the animations have completed. | |
154 if (!screen_lock_callback_.is_null()) { | |
155 VLOG(1) << "Screen locked due to suspend"; | |
156 } else { | |
157 VLOG(1) << "Screen locked without suspend"; | |
158 } | |
159 } | |
160 | |
161 void PowerEventObserver::ScreenIsUnlocked() { | |
162 screen_locked_ = false; | |
163 } | |
164 | |
165 } // namespace ash | |
OLD | NEW |