exit_warning_handler.h 2.1 KB

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