launcher_nudge_controller.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2021 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_SHELF_LAUNCHER_NUDGE_CONTROLLER_H_
  5. #define ASH_SHELF_LAUNCHER_NUDGE_CONTROLLER_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ash/public/cpp/app_list/app_list_controller_observer.h"
  9. #include "ash/public/cpp/session/session_observer.h"
  10. #include "ash/public/cpp/tablet_mode_observer.h"
  11. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/scoped_observation.h"
  14. #include "base/time/time.h"
  15. namespace base {
  16. class Clock;
  17. class TickClock;
  18. class WallClockTimer;
  19. } // namespace base
  20. class PrefRegistrySimple;
  21. class PrefService;
  22. namespace ash {
  23. class HomeButton;
  24. // Controls the launcher nudge which animates the home button to guide users to
  25. // open the launcher. The animation is implemented in HomeButton class.
  26. class ASH_EXPORT LauncherNudgeController : public SessionObserver,
  27. public AppListControllerObserver,
  28. public TabletModeObserver {
  29. public:
  30. // Maximum number of times that the nudge will be shown to the users.
  31. static constexpr int kMaxShownCount = 3;
  32. // To prevent showing the nudge to users right after the home button appears
  33. // (e.g. at the moment when the user logs in or change to the clamshell mode),
  34. // a minimum time delta is used here to guarantee the nudge is shown after a
  35. // certain amount of time since the home button shows up.
  36. static constexpr base::TimeDelta kMinIntervalAfterHomeButtonAppears =
  37. base::Minutes(2);
  38. LauncherNudgeController();
  39. LauncherNudgeController(const LauncherNudgeController&) = delete;
  40. LauncherNudgeController& operator=(const LauncherNudgeController&) = delete;
  41. ~LauncherNudgeController() override;
  42. // Registers profile prefs.
  43. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  44. // Gets the home button on the display with id `display_id`.
  45. static HomeButton* GetHomeButtonForDisplay(int64_t display_id);
  46. // Gets the number of times that the nudge has been shown.
  47. static int GetShownCount(PrefService* prefs);
  48. // Returns the time delta between user's first login and the first time
  49. // showing the nudge if `is_first_time` is true. Otherwise, returns the time
  50. // delta between each time showing the nudge to the user. If the
  51. // `kLauncherNudgeShortInterval` feature is enabled, use a short interval for
  52. // testing.
  53. base::TimeDelta GetNudgeInterval(bool is_first_time) const;
  54. // Sets custom Clock and TickClock for testing. Note that this should be
  55. // called before `show_nudge_timer_` has ever started.
  56. void SetClockForTesting(const base::Clock* clock,
  57. const base::TickClock* timer_clock);
  58. // Checks whether the `show_nudge_timer_` is running.
  59. bool IsRecheckTimerRunningForTesting();
  60. private:
  61. // Checks whether the nudge should be shown. Update the `recheck_time` for
  62. // MaybeShowNudge() to recheck at `recheck_time`.
  63. bool ShouldShowNudge(base::Time& recheck_time) const;
  64. // Updates the user preference data after the nudge is shown.
  65. void HandleNudgeShown();
  66. // Shows the nudge immediately if needed. Schedules a task to recheck
  67. // whether the nudge should be shown at a later time.
  68. void MaybeShowNudge();
  69. // Starts a timer to schedule the next attempt to show nudge.
  70. void ScheduleShowNudgeAttempt(base::Time recheck_time);
  71. // SessionObserver:
  72. void OnActiveUserPrefServiceChanged(PrefService* prefs) override;
  73. // AppListControllerObserver:
  74. void OnAppListVisibilityChanged(bool shown, int64_t display_id) override;
  75. // TabletModeObserver:
  76. void OnTabletModeEnded() override;
  77. void OnTabletControllerDestroyed() override;
  78. // Returns the current time. The clock may be overridden for testing.
  79. base::Time GetNow() const;
  80. const base::Clock* clock_for_test_ = nullptr;
  81. // The timer that keeps track of when should the nudge be shown next time.
  82. std::unique_ptr<base::WallClockTimer> show_nudge_timer_;
  83. // The earliest available time for the nudge to be shown next time. Could be
  84. // null or earlier than GetNow().
  85. base::Time earliest_available_time_;
  86. ScopedSessionObserver session_observer_{this};
  87. base::ScopedObservation<TabletModeController, TabletModeObserver>
  88. tablet_mode_observation_{this};
  89. base::WeakPtrFactory<LauncherNudgeController> weak_ptr_factory_{this};
  90. };
  91. } // namespace ash
  92. #endif // ASH_SHELF_LAUNCHER_NUDGE_CONTROLLER_H_