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 #ifndef ASH_ACCELERATORS_EXIT_WARNING_HANDLER_H_ 6 #define ASH_ACCELERATORS_EXIT_WARNING_HANDLER_H_ 7 8 #include "ash/ash_export.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/timer/timer.h" 11 #include "ui/base/accelerators/accelerator.h" 12 13 namespace views { 14 class Widget; 15 } 16 17 namespace ash { 18 19 // In order to avoid accidental exits when the user presses the exit 20 // shortcut by mistake, we require the user press it twice within a 21 // period of time. During that time we show a popup informing the 22 // user of this. 23 // 24 // Notes: 25 // 26 // The corresponding accelerator must be non-repeatable (see 27 // kNonrepeatableActions in accelerator_table.cc). Otherwise the "Double Press 28 // Exit" will be activated just by holding it down, i.e. probably every time. 29 // 30 // State Transition Diagrams: 31 // 32 // IDLE 33 // | Press 34 // WAIT_FOR_DOUBLE_PRESS action: show ui & start timers 35 // | Press (before time limit ) 36 // EXITING action: hide ui, stop timer, exit 37 // 38 // IDLE 39 // | Press 40 // WAIT_FOR_DOUBLE_PRESS action: show ui & start timers 41 // | T timer expires 42 // IDLE action: hide ui 43 // 44 45 class AcceleratorControllerTest; 46 47 class ASH_EXPORT ExitWarningHandler { 48 public: 49 ExitWarningHandler(); 50 51 ~ExitWarningHandler(); 52 53 // Handles accelerator for exit (Ctrl-Shift-Q). 54 void HandleAccelerator(); 55 56 private: 57 friend class AcceleratorControllerTest; 58 59 enum State { 60 IDLE, 61 WAIT_FOR_DOUBLE_PRESS, 62 EXITING 63 }; 64 65 // Performs actions when the time limit is exceeded. 66 void TimerAction(); 67 68 void StartTimer(); 69 void CancelTimer(); 70 71 void Show(); 72 void Hide(); 73 74 State state_; 75 scoped_ptr<views::Widget> widget_; 76 base::OneShotTimer<ExitWarningHandler> timer_; 77 78 // Flag to suppress starting the timer for testing. For test we call 79 // TimerAction() directly to simulate the expiration of the timer. 80 bool stub_timer_for_test_; 81 82 DISALLOW_COPY_AND_ASSIGN(ExitWarningHandler); 83 }; 84 85 } // namespace ash 86 87 #endif // ASH_ACCELERATORS_EXIT_WARNING_HANDLER_H_ 88