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

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

Issue 2620383003: ash: Add one second grace period For LockScreenIfRequired (Closed)
Patch Set: based on ps2's comments Created 3 years, 11 months 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 = 500; 26 constexpr int kShutdownTimeoutMs = 500;
27 27
28 // Amount of time that locking screen needs to be delayed after setting display
29 // forced off.
Daniel Erat 2017/01/12 01:40:46 thanks, i understand now. maybe: // Amount of tim
Qiang(Joe) Xu 2017/01/12 15:05:55 Done.
30 constexpr int kLockScreenTimeoutMs = 1000;
31
28 // Amount of time since last SuspendDone() that power button event needs to be 32 // Amount of time since last SuspendDone() that power button event needs to be
29 // ignored. 33 // ignored.
30 constexpr int kIgnorePowerButtonAfterResumeMs = 2000; 34 constexpr int kIgnorePowerButtonAfterResumeMs = 2000;
31 35
32 // Returns true if device is a convertible/tablet device or has 36 // Returns true if device is a convertible/tablet device or has
33 // kAshEnableTouchViewTesting in test, otherwise false. 37 // kAshEnableTouchViewTesting in test, otherwise false.
34 bool IsTabletModeSupported() { 38 bool IsTabletModeSupported() {
35 MaximizeModeController* maximize_mode_controller = 39 MaximizeModeController* maximize_mode_controller =
36 WmShell::Get()->maximize_mode_controller(); 40 WmShell::Get()->maximize_mode_controller();
37 return maximize_mode_controller && 41 return maximize_mode_controller &&
(...skipping 19 matching lines...) Expand all
57 bool TabletPowerButtonController::TestApi::ShutdownTimerIsRunning() const { 61 bool TabletPowerButtonController::TestApi::ShutdownTimerIsRunning() const {
58 return controller_->shutdown_timer_.IsRunning(); 62 return controller_->shutdown_timer_.IsRunning();
59 } 63 }
60 64
61 void TabletPowerButtonController::TestApi::TriggerShutdownTimeout() { 65 void TabletPowerButtonController::TestApi::TriggerShutdownTimeout() {
62 DCHECK(ShutdownTimerIsRunning()); 66 DCHECK(ShutdownTimerIsRunning());
63 controller_->OnShutdownTimeout(); 67 controller_->OnShutdownTimeout();
64 controller_->shutdown_timer_.Stop(); 68 controller_->shutdown_timer_.Stop();
65 } 69 }
66 70
71 bool TabletPowerButtonController::TestApi::LockScreenTimerIsRunning() const {
72 return controller_->lock_screen_timer_.IsRunning();
73 }
74
75 void TabletPowerButtonController::TestApi::TriggerLockScreenTimeout() {
76 DCHECK(LockScreenTimerIsRunning());
77 controller_->OnLockScreenTimeout();
78 controller_->lock_screen_timer_.Stop();
79 }
80
67 TabletPowerButtonController::TabletPowerButtonController( 81 TabletPowerButtonController::TabletPowerButtonController(
68 LockStateController* controller) 82 LockStateController* controller)
69 : tick_clock_(new base::DefaultTickClock()), 83 : tick_clock_(new base::DefaultTickClock()),
70 last_resume_time_(base::TimeTicks()), 84 last_resume_time_(base::TimeTicks()),
71 force_off_on_button_up_(true), 85 force_off_on_button_up_(true),
72 controller_(controller), 86 controller_(controller),
73 weak_ptr_factory_(this) { 87 weak_ptr_factory_(this) {
74 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( 88 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
75 this); 89 this);
76 WmShell::Get()->AddShellObserver(this); 90 WmShell::Get()->AddShellObserver(this);
(...skipping 24 matching lines...) Expand all
101 // Chrome receives powerd's SuspendDone signal and notification that the 115 // Chrome receives powerd's SuspendDone signal and notification that the
102 // backlight has been turned back on before seeing the power button events 116 // backlight has been turned back on before seeing the power button events
103 // that woke the system. Avoid forcing off display just after resuming to 117 // that woke the system. Avoid forcing off display just after resuming to
104 // ensure that we don't turn the display off in response to the events. 118 // ensure that we don't turn the display off in response to the events.
105 if (timestamp - last_resume_time_ <= 119 if (timestamp - last_resume_time_ <=
106 base::TimeDelta::FromMilliseconds(kIgnorePowerButtonAfterResumeMs)) { 120 base::TimeDelta::FromMilliseconds(kIgnorePowerButtonAfterResumeMs)) {
107 force_off_on_button_up_ = false; 121 force_off_on_button_up_ = false;
108 } 122 }
109 screen_off_when_power_button_down_ = brightness_level_is_zero_; 123 screen_off_when_power_button_down_ = brightness_level_is_zero_;
110 SetDisplayForcedOff(false); 124 SetDisplayForcedOff(false);
125 lock_screen_timer_.Stop();
111 StartShutdownTimer(); 126 StartShutdownTimer();
112 } else { 127 } else {
113 if (shutdown_timer_.IsRunning()) { 128 if (shutdown_timer_.IsRunning()) {
114 shutdown_timer_.Stop(); 129 shutdown_timer_.Stop();
115 if (!screen_off_when_power_button_down_ && force_off_on_button_up_) { 130 if (!screen_off_when_power_button_down_ && force_off_on_button_up_) {
116 SetDisplayForcedOff(true); 131 SetDisplayForcedOff(true);
117 LockScreenIfRequired(); 132 LockScreenIfRequired();
118 } 133 }
119 } 134 }
120 135
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 if (controller_->CanCancelShutdownAnimation()) 167 if (controller_->CanCancelShutdownAnimation())
153 controller_->CancelShutdownAnimation(); 168 controller_->CancelShutdownAnimation();
154 } 169 }
155 170
156 void TabletPowerButtonController::OnKeyEvent(ui::KeyEvent* event) { 171 void TabletPowerButtonController::OnKeyEvent(ui::KeyEvent* event) {
157 // Ignore key events generated by the power button since power button activity 172 // Ignore key events generated by the power button since power button activity
158 // is already handled by OnPowerButtonEvent(). 173 // is already handled by OnPowerButtonEvent().
159 if (event->key_code() == ui::VKEY_POWER) 174 if (event->key_code() == ui::VKEY_POWER)
160 return; 175 return;
161 176
162 if (!IsTabletModeActive() && backlights_forced_off_) 177 if (!IsTabletModeActive() && backlights_forced_off_) {
163 SetDisplayForcedOff(false); 178 SetDisplayForcedOff(false);
179 lock_screen_timer_.Stop();
180 }
164 } 181 }
165 182
166 void TabletPowerButtonController::OnMouseEvent(ui::MouseEvent* event) { 183 void TabletPowerButtonController::OnMouseEvent(ui::MouseEvent* event) {
167 ui::EventPointerType pointer_type = event->pointer_details().pointer_type; 184 ui::EventPointerType pointer_type = event->pointer_details().pointer_type;
168 185
169 if (pointer_type != ui::EventPointerType::POINTER_TYPE_MOUSE || 186 if (pointer_type != ui::EventPointerType::POINTER_TYPE_MOUSE ||
170 (event->flags() & ui::EF_IS_SYNTHESIZED)) { 187 (event->flags() & ui::EF_IS_SYNTHESIZED)) {
171 return; 188 return;
172 } 189 }
173 190
174 if (!IsTabletModeActive() && backlights_forced_off_) 191 if (!IsTabletModeActive() && backlights_forced_off_) {
175 SetDisplayForcedOff(false); 192 SetDisplayForcedOff(false);
193 lock_screen_timer_.Stop();
194 }
176 } 195 }
177 196
178 void TabletPowerButtonController::OnStylusStateChanged(ui::StylusState state) { 197 void TabletPowerButtonController::OnStylusStateChanged(ui::StylusState state) {
179 if (IsTabletModeSupported() && state == ui::StylusState::REMOVED && 198 if (IsTabletModeSupported() && state == ui::StylusState::REMOVED &&
180 backlights_forced_off_) { 199 backlights_forced_off_) {
181 SetDisplayForcedOff(false); 200 SetDisplayForcedOff(false);
201 lock_screen_timer_.Stop();
182 } 202 }
183 } 203 }
184 204
185 void TabletPowerButtonController::SetTickClockForTesting( 205 void TabletPowerButtonController::SetTickClockForTesting(
186 std::unique_ptr<base::TickClock> tick_clock) { 206 std::unique_ptr<base::TickClock> tick_clock) {
187 DCHECK(tick_clock); 207 DCHECK(tick_clock);
188 tick_clock_ = std::move(tick_clock); 208 tick_clock_ = std::move(tick_clock);
189 } 209 }
190 210
191 void TabletPowerButtonController::SetDisplayForcedOff(bool forced_off) { 211 void TabletPowerButtonController::SetDisplayForcedOff(bool forced_off) {
192 if (backlights_forced_off_ == forced_off) 212 if (backlights_forced_off_ == forced_off)
193 return; 213 return;
194 214
195 // Set the display and keyboard backlights (if present) to |forced_off|. 215 // Set the display and keyboard backlights (if present) to |forced_off|.
196 chromeos::DBusThreadManager::Get() 216 chromeos::DBusThreadManager::Get()
197 ->GetPowerManagerClient() 217 ->GetPowerManagerClient()
198 ->SetBacklightsForcedOff(forced_off); 218 ->SetBacklightsForcedOff(forced_off);
199 backlights_forced_off_ = forced_off; 219 backlights_forced_off_ = forced_off;
200 220
201 ShellDelegate* delegate = WmShell::Get()->delegate(); 221 ShellDelegate* delegate = WmShell::Get()->delegate();
202 delegate->SetTouchscreenEnabledInPrefs(!forced_off, 222 delegate->SetTouchscreenEnabledInPrefs(!forced_off,
203 true /* use_local_state */); 223 true /* use_local_state */);
204 delegate->UpdateTouchscreenStatusFromPrefs(); 224 delegate->UpdateTouchscreenStatusFromPrefs();
205 225
206 // Send an a11y alert. 226 // Send an a11y alert.
207 WmShell::Get()->accessibility_delegate()->TriggerAccessibilityAlert( 227 WmShell::Get()->accessibility_delegate()->TriggerAccessibilityAlert(
208 forced_off ? A11Y_ALERT_SCREEN_OFF : A11Y_ALERT_SCREEN_ON); 228 forced_off ? A11Y_ALERT_SCREEN_OFF : A11Y_ALERT_SCREEN_ON);
229
230 // Reset |lock_screen_timer_| when display is not set to forced off since
231 // under this case, |lock_screen_timer_| should not timeout.
232 if (!forced_off)
233 lock_screen_timer_.Stop();
Daniel Erat 2017/01/12 01:40:46 did you mean to remove this now? aren't you alread
Qiang(Joe) Xu 2017/01/12 15:05:55 oops, yeh, sorry about this.
209 } 234 }
210 235
211 void TabletPowerButtonController::GetInitialBacklightsForcedOff() { 236 void TabletPowerButtonController::GetInitialBacklightsForcedOff() {
212 chromeos::DBusThreadManager::Get() 237 chromeos::DBusThreadManager::Get()
213 ->GetPowerManagerClient() 238 ->GetPowerManagerClient()
214 ->GetBacklightsForcedOff(base::Bind( 239 ->GetBacklightsForcedOff(base::Bind(
215 &TabletPowerButtonController::OnGotInitialBacklightsForcedOff, 240 &TabletPowerButtonController::OnGotInitialBacklightsForcedOff,
216 weak_ptr_factory_.GetWeakPtr())); 241 weak_ptr_factory_.GetWeakPtr()));
217 } 242 }
218 243
(...skipping 12 matching lines...) Expand all
231 controller_->StartShutdownAnimation(); 256 controller_->StartShutdownAnimation();
232 } 257 }
233 258
234 void TabletPowerButtonController::LockScreenIfRequired() { 259 void TabletPowerButtonController::LockScreenIfRequired() {
235 SessionStateDelegate* session_state_delegate = 260 SessionStateDelegate* session_state_delegate =
236 WmShell::Get()->GetSessionStateDelegate(); 261 WmShell::Get()->GetSessionStateDelegate();
237 if (session_state_delegate->ShouldLockScreenAutomatically() && 262 if (session_state_delegate->ShouldLockScreenAutomatically() &&
238 session_state_delegate->CanLockScreen() && 263 session_state_delegate->CanLockScreen() &&
239 !session_state_delegate->IsUserSessionBlocked() && 264 !session_state_delegate->IsUserSessionBlocked() &&
240 !controller_->LockRequested()) { 265 !controller_->LockRequested()) {
241 session_state_delegate->LockScreen(); 266 lock_screen_timer_.Start(
267 FROM_HERE, base::TimeDelta::FromMilliseconds(kLockScreenTimeoutMs),
268 this, &TabletPowerButtonController::OnLockScreenTimeout);
242 } 269 }
243 } 270 }
244 271
272 void TabletPowerButtonController::OnLockScreenTimeout() {
273 WmShell::Get()->GetSessionStateDelegate()->LockScreen();
274 }
275
245 } // namespace ash 276 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698