Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: ash/system/chromeos/power/tablet_power_button_controller.cc

Issue 2546303002: ash: Start shutdown timer on post-resume power button press (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 #include "ash/system/chromeos/power/tablet_power_button_controller.h" 5 #include "ash/system/chromeos/power/tablet_power_button_controller.h"
6 6
7 #include "ash/common/accessibility_delegate.h" 7 #include "ash/common/accessibility_delegate.h"
8 #include "ash/common/session/session_state_delegate.h" 8 #include "ash/common/session/session_state_delegate.h"
9 #include "ash/common/shell_delegate.h" 9 #include "ash/common/shell_delegate.h"
10 #include "ash/common/wm/maximize_mode/maximize_mode_controller.h" 10 #include "ash/common/wm/maximize_mode/maximize_mode_controller.h"
11 #include "ash/common/wm_shell.h" 11 #include "ash/common/wm_shell.h"
12 #include "ash/shell.h" 12 #include "ash/shell.h"
13 #include "ash/wm/lock_state_controller.h" 13 #include "ash/wm/lock_state_controller.h"
14 #include "base/time/default_tick_clock.h" 14 #include "base/time/default_tick_clock.h"
15 #include "chromeos/dbus/dbus_thread_manager.h" 15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "ui/events/devices/input_device_manager.h" 16 #include "ui/events/devices/input_device_manager.h"
17 #include "ui/events/devices/stylus_state.h" 17 #include "ui/events/devices/stylus_state.h"
18 #include "ui/events/event.h" 18 #include "ui/events/event.h"
19 19
20 namespace ash { 20 namespace ash {
21 21
22 namespace { 22 namespace {
23 23
24 // Amount of time the power button must be held to start the pre-shutdown 24 // Amount of time the power button must be held to start the pre-shutdown
25 // animation when in tablet mode. 25 // animation when in tablet mode.
26 constexpr int kShutdownTimeoutMs = 1000; 26 constexpr int kShutdownTimeoutMs = 1000;
27 27
28 // Amount of time since last SuspendDone() that power button event needs to be 28 // Amount of time that power button pressed is considered as the wake source
29 // ignored. 29 // for suspend.
30 constexpr int kIgnorePowerButtonAfterResumeMs = 2000; 30 constexpr int kDurationPowerButtonAsWakeSourceMs = 2000;
Daniel Erat 2016/12/05 17:25:59 this name and comment don't make that much sense t
Qiang(Joe) Xu 2016/12/05 19:33:58 Because I think the code now is not ignoring the e
31 31
32 // Returns true if device is a convertible/tablet device or has 32 // Returns true if device is a convertible/tablet device or has
33 // kAshEnableTouchViewTesting in test, otherwise false. 33 // kAshEnableTouchViewTesting in test, otherwise false.
34 bool IsTabletModeSupported() { 34 bool IsTabletModeSupported() {
35 MaximizeModeController* maximize_mode_controller = 35 MaximizeModeController* maximize_mode_controller =
36 WmShell::Get()->maximize_mode_controller(); 36 WmShell::Get()->maximize_mode_controller();
37 return maximize_mode_controller && 37 return maximize_mode_controller &&
38 maximize_mode_controller->CanEnterMaximizeMode(); 38 maximize_mode_controller->CanEnterMaximizeMode();
39 } 39 }
40 40
41 // Returns true if device is currently in tablet/maximize mode, otherwise false. 41 // Returns true if device is currently in tablet/maximize mode, otherwise false.
42 bool IsTabletModeActive() { 42 bool IsTabletModeActive() {
43 MaximizeModeController* maximize_mode_controller = 43 MaximizeModeController* maximize_mode_controller =
44 WmShell::Get()->maximize_mode_controller(); 44 WmShell::Get()->maximize_mode_controller();
45 return maximize_mode_controller && 45 return maximize_mode_controller &&
46 maximize_mode_controller->IsMaximizeModeWindowManagerEnabled(); 46 maximize_mode_controller->IsMaximizeModeWindowManagerEnabled();
47 } 47 }
48 48
49 void SendA11yScreenAlert(bool alert_screen_off) {
50 WmShell::Get()->accessibility_delegate()->TriggerAccessibilityAlert(
51 alert_screen_off ? A11Y_ALERT_SCREEN_OFF : A11Y_ALERT_SCREEN_ON);
Daniel Erat 2016/12/05 17:26:00 is there any way to test that alerts are sent at t
Qiang(Joe) Xu 2016/12/05 19:33:58 mm.. I don't find it. From the CL I found, there i
52 }
53
49 } // namespace 54 } // namespace
50 55
51 TabletPowerButtonController::TestApi::TestApi( 56 TabletPowerButtonController::TestApi::TestApi(
52 TabletPowerButtonController* controller) 57 TabletPowerButtonController* controller)
53 : controller_(controller) {} 58 : controller_(controller) {}
54 59
55 TabletPowerButtonController::TestApi::~TestApi() {} 60 TabletPowerButtonController::TestApi::~TestApi() {}
56 61
57 bool TabletPowerButtonController::TestApi::ShutdownTimerIsRunning() const { 62 bool TabletPowerButtonController::TestApi::ShutdownTimerIsRunning() const {
58 return controller_->shutdown_timer_.IsRunning(); 63 return controller_->shutdown_timer_.IsRunning();
59 } 64 }
60 65
61 void TabletPowerButtonController::TestApi::TriggerShutdownTimeout() { 66 void TabletPowerButtonController::TestApi::TriggerShutdownTimeout() {
62 DCHECK(ShutdownTimerIsRunning()); 67 DCHECK(ShutdownTimerIsRunning());
63 controller_->OnShutdownTimeout(); 68 controller_->OnShutdownTimeout();
64 controller_->shutdown_timer_.Stop(); 69 controller_->shutdown_timer_.Stop();
65 } 70 }
66 71
67 TabletPowerButtonController::TabletPowerButtonController( 72 TabletPowerButtonController::TabletPowerButtonController(
68 LockStateController* controller) 73 LockStateController* controller)
69 : tick_clock_(new base::DefaultTickClock()), 74 : tick_clock_(new base::DefaultTickClock()),
70 last_resume_time_(base::TimeTicks()), 75 last_resume_time_(base::TimeTicks()),
76 ignore_forcing_off_(false),
71 controller_(controller), 77 controller_(controller),
72 weak_ptr_factory_(this) { 78 weak_ptr_factory_(this) {
73 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( 79 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
74 this); 80 this);
75 ui::InputDeviceManager::GetInstance()->AddObserver(this); 81 ui::InputDeviceManager::GetInstance()->AddObserver(this);
76 Shell::GetInstance()->PrependPreTargetHandler(this); 82 Shell::GetInstance()->PrependPreTargetHandler(this);
77 83
78 GetInitialBacklightsForcedOff(); 84 GetInitialBacklightsForcedOff();
79 } 85 }
80 86
81 TabletPowerButtonController::~TabletPowerButtonController() { 87 TabletPowerButtonController::~TabletPowerButtonController() {
82 Shell::GetInstance()->RemovePreTargetHandler(this); 88 Shell::GetInstance()->RemovePreTargetHandler(this);
83 ui::InputDeviceManager::GetInstance()->RemoveObserver(this); 89 ui::InputDeviceManager::GetInstance()->RemoveObserver(this);
84 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( 90 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(
85 this); 91 this);
86 } 92 }
87 93
88 bool TabletPowerButtonController::ShouldHandlePowerButtonEvents() const { 94 bool TabletPowerButtonController::ShouldHandlePowerButtonEvents() const {
89 return IsTabletModeSupported(); 95 return IsTabletModeSupported();
90 } 96 }
91 97
92 void TabletPowerButtonController::OnPowerButtonEvent( 98 void TabletPowerButtonController::OnPowerButtonEvent(
93 bool down, 99 bool down,
94 const base::TimeTicks& timestamp) { 100 const base::TimeTicks& timestamp) {
95 // When the system resumes in response to the power button being pressed,
96 // Chrome receives powerd's SuspendDone signal and notification that the
97 // backlight has been turned back on before seeing the power button events
98 // that woke the system. Ignore events just after resuming to ensure that we
99 // don't turn the screen off in response to the events.
100 //
101 // TODO(warx): pressing power button should also StartShutdownTimer() in this
102 // case. Reorganize the code to support that.
103 if (timestamp - last_resume_time_ <=
104 base::TimeDelta::FromMilliseconds(kIgnorePowerButtonAfterResumeMs)) {
105 // If backlights are forced off, stop forcing off because resuming system
106 // doesn't handle this.
107 if (down && backlights_forced_off_)
108 SetDisplayForcedOff(false);
109 return;
110 }
111
112 if (down) { 101 if (down) {
102 // When the system resumes in response to the power button being pressed,
103 // Chrome receives powerd's SuspendDone signal and notification that the
104 // baclight has been turned back on before seeing the power button events
105 // that woke the system. Ignore forcing off display just after resuming to
106 // ensure that we don't turn the screen off in response to such event.
107 if (timestamp - last_resume_time_ <=
108 base::TimeDelta::FromMilliseconds(kDurationPowerButtonAsWakeSourceMs)) {
109 ignore_forcing_off_ = true;
110 // When |backlights_forced_off_| is false and system resumes in response
111 // to the power button being pressed, the following SetDisplayForcedOff
112 // call will not send a11y alert because of the early return.
113 if (!backlights_forced_off_)
114 SendA11yScreenAlert(false);
Daniel Erat 2016/12/05 17:26:00 why do you need to call this here? won't this make
Qiang(Joe) Xu 2016/12/05 19:33:58 If we want "tablet power button event" always trig
115 }
113 screen_off_when_power_button_down_ = brightness_level_is_zero_; 116 screen_off_when_power_button_down_ = brightness_level_is_zero_;
114 SetDisplayForcedOff(false); 117 SetDisplayForcedOff(false);
115 StartShutdownTimer(); 118 StartShutdownTimer();
116 } else { 119 } else {
117 if (shutdown_timer_.IsRunning()) { 120 if (shutdown_timer_.IsRunning()) {
118 shutdown_timer_.Stop(); 121 shutdown_timer_.Stop();
119 if (!screen_off_when_power_button_down_) { 122 if (!screen_off_when_power_button_down_ && !ignore_forcing_off_) {
120 SetDisplayForcedOff(true); 123 SetDisplayForcedOff(true);
121 LockScreenIfRequired(); 124 LockScreenIfRequired();
122 } 125 }
123 } 126 }
127 ignore_forcing_off_ = false;
Daniel Erat 2016/12/05 17:26:00 how about just setting this to true/false in the d
Qiang(Joe) Xu 2016/12/05 19:33:58 Done.
124 screen_off_when_power_button_down_ = false; 128 screen_off_when_power_button_down_ = false;
125 129
126 // When power button is released, cancel shutdown animation whenever it is 130 // When power button is released, cancel shutdown animation whenever it is
127 // still cancellable. 131 // still cancellable.
128 if (controller_->CanCancelShutdownAnimation()) 132 if (controller_->CanCancelShutdownAnimation())
129 controller_->CancelShutdownAnimation(); 133 controller_->CancelShutdownAnimation();
130 } 134 }
131 } 135 }
132 136
133 void TabletPowerButtonController::PowerManagerRestarted() { 137 void TabletPowerButtonController::PowerManagerRestarted() {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 chromeos::DBusThreadManager::Get() 193 chromeos::DBusThreadManager::Get()
190 ->GetPowerManagerClient() 194 ->GetPowerManagerClient()
191 ->SetBacklightsForcedOff(forced_off); 195 ->SetBacklightsForcedOff(forced_off);
192 backlights_forced_off_ = forced_off; 196 backlights_forced_off_ = forced_off;
193 197
194 ShellDelegate* delegate = WmShell::Get()->delegate(); 198 ShellDelegate* delegate = WmShell::Get()->delegate();
195 delegate->SetTouchscreenEnabledInPrefs(!forced_off, 199 delegate->SetTouchscreenEnabledInPrefs(!forced_off,
196 true /* use_local_state */); 200 true /* use_local_state */);
197 delegate->UpdateTouchscreenStatusFromPrefs(); 201 delegate->UpdateTouchscreenStatusFromPrefs();
198 202
199 // Send an a11y alert. 203 SendA11yScreenAlert(forced_off);
200 WmShell::Get()->accessibility_delegate()->TriggerAccessibilityAlert(
201 forced_off ? A11Y_ALERT_SCREEN_OFF : A11Y_ALERT_SCREEN_ON);
202 } 204 }
203 205
204 void TabletPowerButtonController::GetInitialBacklightsForcedOff() { 206 void TabletPowerButtonController::GetInitialBacklightsForcedOff() {
205 chromeos::DBusThreadManager::Get() 207 chromeos::DBusThreadManager::Get()
206 ->GetPowerManagerClient() 208 ->GetPowerManagerClient()
207 ->GetBacklightsForcedOff(base::Bind( 209 ->GetBacklightsForcedOff(base::Bind(
208 &TabletPowerButtonController::OnGotInitialBacklightsForcedOff, 210 &TabletPowerButtonController::OnGotInitialBacklightsForcedOff,
209 weak_ptr_factory_.GetWeakPtr())); 211 weak_ptr_factory_.GetWeakPtr()));
210 } 212 }
211 213
(...skipping 17 matching lines...) Expand all
229 WmShell::Get()->GetSessionStateDelegate(); 231 WmShell::Get()->GetSessionStateDelegate();
230 if (session_state_delegate->ShouldLockScreenAutomatically() && 232 if (session_state_delegate->ShouldLockScreenAutomatically() &&
231 session_state_delegate->CanLockScreen() && 233 session_state_delegate->CanLockScreen() &&
232 !session_state_delegate->IsUserSessionBlocked() && 234 !session_state_delegate->IsUserSessionBlocked() &&
233 !controller_->LockRequested()) { 235 !controller_->LockRequested()) {
234 session_state_delegate->LockScreen(); 236 session_state_delegate->LockScreen();
235 } 237 }
236 } 238 }
237 239
238 } // namespace ash 240 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698