logout_confirmation_controller.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2014 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_SYSTEM_SESSION_LOGOUT_CONFIRMATION_CONTROLLER_H_
  5. #define ASH_SYSTEM_SESSION_LOGOUT_CONFIRMATION_CONTROLLER_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ash/public/cpp/session/session_observer.h"
  9. #include "base/callback_forward.h"
  10. #include "base/time/time.h"
  11. #include "base/timer/timer.h"
  12. class PrefRegistrySimple;
  13. namespace base {
  14. class TickClock;
  15. }
  16. namespace ash {
  17. class LogoutConfirmationDialog;
  18. // This class shows a dialog asking the user to confirm or deny logout and
  19. // terminates the session if the user either confirms or allows the countdown
  20. // shown in the dialog to expire.
  21. //
  22. // It is guaranteed that no more than one confirmation dialog will be visible at
  23. // any given time. If there are multiple requests to show a confirmation dialog
  24. // at the same time, the dialog whose countdown expires first is shown.
  25. //
  26. // In public sessions, asks the user to end the session when the last window is
  27. // closed.
  28. class ASH_EXPORT LogoutConfirmationController : public SessionObserver {
  29. public:
  30. enum class Source { kShelfExitButton, kCloseAllWindows };
  31. LogoutConfirmationController();
  32. LogoutConfirmationController(const LogoutConfirmationController&) = delete;
  33. LogoutConfirmationController& operator=(const LogoutConfirmationController&) =
  34. delete;
  35. ~LogoutConfirmationController() override;
  36. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  37. const base::TickClock* clock() const { return clock_; }
  38. // Shows a LogoutConfirmationDialog. If a confirmation dialog is already being
  39. // shown, it is closed and a new one opened if |logout_time| is earlier than
  40. // the current dialog's |logout_time_|.
  41. void ConfirmLogout(base::TimeTicks logout_time, Source source);
  42. // SessionObserver:
  43. void OnLoginStatusChanged(LoginStatus login_status) override;
  44. void OnLockStateChanged(bool locked) override;
  45. // Called by the |dialog_| when the user confirms logout.
  46. void OnLogoutConfirmed();
  47. // Called by the |dialog_| when it is closed.
  48. void OnDialogClosed();
  49. // Overrides the internal clock for testing. This doesn't take the ownership
  50. // of the clock. |clock| must outlive the LogoutConfirmationController
  51. // instance.
  52. void SetClockForTesting(const base::TickClock* clock);
  53. void SetLogoutCallbackForTesting(
  54. const base::RepeatingCallback<void(Source)>& logout_callback);
  55. LogoutConfirmationDialog* dialog_for_testing() const { return dialog_; }
  56. int confirm_logout_count_for_test() const {
  57. return confirm_logout_count_for_test_;
  58. }
  59. private:
  60. class LastWindowClosedObserver;
  61. std::unique_ptr<LastWindowClosedObserver> last_window_closed_observer_;
  62. const base::TickClock* clock_;
  63. base::RepeatingCallback<void(Source)> logout_callback_;
  64. Source source_;
  65. base::TimeTicks logout_time_;
  66. LogoutConfirmationDialog* dialog_ = nullptr; // Owned by the Views hierarchy.
  67. base::OneShotTimer logout_timer_;
  68. int confirm_logout_count_for_test_ = 0;
  69. };
  70. } // namespace ash
  71. #endif // ASH_SYSTEM_SESSION_LOGOUT_CONFIRMATION_CONTROLLER_H_