OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef ASH_WM_POWER_BUTTON_CONTROLLER_H_ | 5 #ifndef ASH_WM_POWER_BUTTON_CONTROLLER_H_ |
6 #define ASH_WM_POWER_BUTTON_CONTROLLER_H_ | 6 #define ASH_WM_POWER_BUTTON_CONTROLLER_H_ |
7 | 7 |
8 #include "ash/ash_export.h" | 8 #include "ash/ash_export.h" |
| 9 #include "ash/shell_observer.h" |
9 #include "ash/wm/session_state_animator.h" | 10 #include "ash/wm/session_state_animator.h" |
10 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/time.h" |
| 14 #include "base/timer.h" |
| 15 #include "ui/aura/root_window_observer.h" |
11 | 16 |
12 namespace gfx { | 17 namespace gfx { |
13 class Rect; | 18 class Rect; |
14 class Size; | 19 class Size; |
15 } | 20 } |
16 | 21 |
17 namespace ui { | 22 namespace ui { |
18 class Layer; | 23 class Layer; |
19 } | 24 } |
20 | 25 |
21 namespace ash { | 26 namespace ash { |
22 | 27 |
23 namespace test { | 28 // Performs system-related functions on behalf of PowerButtonController. |
24 class PowerButtonControllerTest; | 29 class ASH_EXPORT PowerButtonControllerDelegate { |
25 } | 30 public: |
| 31 PowerButtonControllerDelegate() {} |
| 32 virtual ~PowerButtonControllerDelegate() {} |
26 | 33 |
27 class SessionStateController; | 34 virtual void RequestLockScreen() = 0; |
| 35 virtual void RequestShutdown() = 0; |
| 36 |
| 37 private: |
| 38 DISALLOW_COPY_AND_ASSIGN(PowerButtonControllerDelegate); |
| 39 }; |
28 | 40 |
29 // Displays onscreen animations and locks or suspends the system in response to | 41 // Displays onscreen animations and locks or suspends the system in response to |
30 // the power button being pressed or released. | 42 // the power button being pressed or released. |
31 class ASH_EXPORT PowerButtonController { | 43 class ASH_EXPORT PowerButtonController : public aura::RootWindowObserver, |
| 44 public ShellObserver { |
32 public: | 45 public: |
33 | 46 |
34 explicit PowerButtonController(SessionStateController* controller); | 47 // Helper class used by tests to access internal state. |
| 48 class ASH_EXPORT TestApi { |
| 49 public: |
| 50 explicit TestApi(PowerButtonController* controller); |
| 51 |
| 52 virtual ~TestApi(); |
| 53 |
| 54 bool lock_timer_is_running() const { |
| 55 return controller_->lock_timer_.IsRunning(); |
| 56 } |
| 57 bool lock_fail_timer_is_running() const { |
| 58 return controller_->lock_fail_timer_.IsRunning(); |
| 59 } |
| 60 bool lock_to_shutdown_timer_is_running() const { |
| 61 return controller_->lock_to_shutdown_timer_.IsRunning(); |
| 62 } |
| 63 bool shutdown_timer_is_running() const { |
| 64 return controller_->shutdown_timer_.IsRunning(); |
| 65 } |
| 66 bool real_shutdown_timer_is_running() const { |
| 67 return controller_->real_shutdown_timer_.IsRunning(); |
| 68 } |
| 69 bool hide_black_layer_timer_is_running() const { |
| 70 return animator_api_->hide_black_layer_timer_is_running(); |
| 71 } |
| 72 |
| 73 void trigger_lock_timeout() { |
| 74 controller_->OnLockTimeout(); |
| 75 controller_->lock_timer_.Stop(); |
| 76 } |
| 77 void trigger_lock_fail_timeout() { |
| 78 controller_->OnLockFailTimeout(); |
| 79 controller_->lock_fail_timer_.Stop(); |
| 80 } |
| 81 void trigger_lock_to_shutdown_timeout() { |
| 82 controller_->OnLockToShutdownTimeout(); |
| 83 controller_->lock_to_shutdown_timer_.Stop(); |
| 84 } |
| 85 void trigger_shutdown_timeout() { |
| 86 controller_->OnShutdownTimeout(); |
| 87 controller_->shutdown_timer_.Stop(); |
| 88 } |
| 89 void trigger_real_shutdown_timeout() { |
| 90 controller_->OnRealShutdownTimeout(); |
| 91 controller_->real_shutdown_timer_.Stop(); |
| 92 } |
| 93 void TriggerHideBlackLayerTimeout() { |
| 94 animator_api_->TriggerHideBlackLayerTimeout(); |
| 95 } |
| 96 |
| 97 // Returns true if containers of a given |container_mask| |
| 98 // were last animated with |type| (probably; the analysis is fairly ad-hoc). |
| 99 // |container_mask| is a bitfield of a Container. |
| 100 bool ContainersAreAnimated(int container_mask, |
| 101 internal::SessionStateAnimator::AnimationType type) const { |
| 102 return animator_api_->ContainersAreAnimated(container_mask, type); |
| 103 } |
| 104 |
| 105 // Returns true if |black_layer_| is non-NULL and visible. |
| 106 bool BlackLayerIsVisible() { |
| 107 return animator_api_->BlackLayerIsVisible(); |
| 108 } |
| 109 |
| 110 // Returns |black_layer_|'s bounds, or an empty rect if the layer is |
| 111 // NULL. |
| 112 gfx::Rect GetBlackLayerBounds() const { |
| 113 return animator_api_->GetBlackLayerBounds(); |
| 114 } |
| 115 |
| 116 private: |
| 117 PowerButtonController* controller_; // not owned |
| 118 |
| 119 scoped_ptr<internal::SessionStateAnimator::TestApi> animator_api_; |
| 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(TestApi); |
| 122 }; |
| 123 |
| 124 PowerButtonController(); |
35 virtual ~PowerButtonController(); | 125 virtual ~PowerButtonController(); |
36 | 126 |
| 127 void set_delegate(PowerButtonControllerDelegate* delegate) { |
| 128 delegate_.reset(delegate); |
| 129 } |
| 130 |
37 void set_has_legacy_power_button_for_test(bool legacy) { | 131 void set_has_legacy_power_button_for_test(bool legacy) { |
38 has_legacy_power_button_ = legacy; | 132 has_legacy_power_button_ = legacy; |
39 } | 133 } |
40 | 134 |
41 // Called when the current screen brightness changes. | 135 // Called when the current screen brightness changes. |
42 void OnScreenBrightnessChanged(double percent); | 136 void OnScreenBrightnessChanged(double percent); |
43 | 137 |
| 138 // Called when Chrome gets a request to display the lock screen. |
| 139 void OnStartingLock(); |
| 140 |
44 // Called when the power or lock buttons are pressed or released. | 141 // Called when the power or lock buttons are pressed or released. |
45 void OnPowerButtonEvent(bool down, const base::TimeTicks& timestamp); | 142 void OnPowerButtonEvent(bool down, const base::TimeTicks& timestamp); |
46 void OnLockButtonEvent(bool down, const base::TimeTicks& timestamp); | 143 void OnLockButtonEvent(bool down, const base::TimeTicks& timestamp); |
47 | 144 |
| 145 // Displays the shutdown animation and requests shutdown when it's done. |
| 146 void RequestShutdown(); |
| 147 |
| 148 virtual void OnRootWindowHostCloseRequested( |
| 149 const aura::RootWindow* root) OVERRIDE; |
| 150 |
| 151 // ShellObserver overrides: |
| 152 virtual void OnLoginStateChanged(user::LoginStatus status) OVERRIDE; |
| 153 virtual void OnAppTerminating() OVERRIDE; |
| 154 virtual void OnLockStateChanged(bool locked) OVERRIDE; |
| 155 |
48 private: | 156 private: |
49 friend class test::PowerButtonControllerTest; | 157 bool LoggedInAsNonGuest() const; |
| 158 |
| 159 // Requests that the screen be locked and starts |lock_fail_timer_|. |
| 160 void OnLockTimeout(); |
| 161 |
| 162 // Aborts the pre-lock animation. |
| 163 void OnLockFailTimeout(); |
| 164 |
| 165 // Displays the pre-shutdown animation and starts |shutdown_timer_|. |
| 166 void OnLockToShutdownTimeout(); |
| 167 |
| 168 // Calls StartShutdownAnimationAndRequestShutdown(). |
| 169 void OnShutdownTimeout(); |
| 170 |
| 171 // Requests that the machine be shut down. |
| 172 void OnRealShutdownTimeout(); |
| 173 |
| 174 // Puts us into the pre-lock or pre-shutdown state. |
| 175 void StartLockTimer(); |
| 176 void StartShutdownTimer(); |
| 177 |
| 178 // Displays the shutdown animation and starts |real_shutdown_timer_|. |
| 179 void StartShutdownAnimationAndRequestShutdown(); |
| 180 |
| 181 scoped_ptr<PowerButtonControllerDelegate> delegate_; |
| 182 |
| 183 // The current login status. |
| 184 user::LoginStatus login_status_; |
| 185 |
| 186 // Original login status during locked. LOGGED_IN_NONE if it's not locked. |
| 187 user::LoginStatus unlocked_login_status_; |
50 | 188 |
51 // Are the power or lock buttons currently held? | 189 // Are the power or lock buttons currently held? |
52 bool power_button_down_; | 190 bool power_button_down_; |
53 bool lock_button_down_; | 191 bool lock_button_down_; |
54 | 192 |
55 // Is the screen currently turned off? | 193 // Is the screen currently turned off? |
56 bool screen_is_off_; | 194 bool screen_is_off_; |
57 | 195 |
| 196 // Are we in the process of shutting the machine down? |
| 197 bool shutting_down_; |
| 198 |
58 // Was a command-line switch set telling us that we're running on hardware | 199 // Was a command-line switch set telling us that we're running on hardware |
59 // that misreports power button releases? | 200 // that misreports power button releases? |
60 bool has_legacy_power_button_; | 201 bool has_legacy_power_button_; |
61 | 202 |
62 SessionStateController* controller_; // Not owned. | 203 // Started when the user first presses the power button while in a |
| 204 // logged-in-as-a-non-guest-user, unlocked state. When it fires, we lock the |
| 205 // screen. |
| 206 base::OneShotTimer<PowerButtonController> lock_timer_; |
| 207 |
| 208 // Started when we request that the screen be locked. When it fires, we |
| 209 // assume that our request got dropped. |
| 210 base::OneShotTimer<PowerButtonController> lock_fail_timer_; |
| 211 |
| 212 // Started when the screen is locked while the power button is held. Adds a |
| 213 // delay between the appearance of the lock screen and the beginning of the |
| 214 // pre-shutdown animation. |
| 215 base::OneShotTimer<PowerButtonController> lock_to_shutdown_timer_; |
| 216 |
| 217 // Started when we begin displaying the pre-shutdown animation. When it |
| 218 // fires, we start the shutdown animation and get ready to request shutdown. |
| 219 base::OneShotTimer<PowerButtonController> shutdown_timer_; |
| 220 |
| 221 // Started when we display the shutdown animation. When it fires, we actually |
| 222 // request shutdown. Gives the animation time to complete before Chrome, X, |
| 223 // etc. are shut down. |
| 224 base::OneShotTimer<PowerButtonController> real_shutdown_timer_; |
| 225 |
| 226 scoped_ptr<internal::SessionStateAnimator> animator_; |
63 | 227 |
64 DISALLOW_COPY_AND_ASSIGN(PowerButtonController); | 228 DISALLOW_COPY_AND_ASSIGN(PowerButtonController); |
65 }; | 229 }; |
66 | 230 |
67 } // namespace ash | 231 } // namespace ash |
68 | 232 |
69 #endif // ASH_WM_POWER_BUTTON_CONTROLLER_H_ | 233 #endif // ASH_WM_POWER_BUTTON_CONTROLLER_H_ |
OLD | NEW |