app_list_nudge_controller.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2022 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_APP_LIST_VIEWS_APP_LIST_NUDGE_CONTROLLER_H_
  5. #define ASH_APP_LIST_VIEWS_APP_LIST_NUDGE_CONTROLLER_H_
  6. #include "ash/ash_export.h"
  7. #include "base/time/time.h"
  8. #include "components/prefs/scoped_user_pref_update.h"
  9. #include "third_party/abseil-cpp/absl/types/optional.h"
  10. class PrefRegistrySimple;
  11. class PrefService;
  12. namespace ash {
  13. enum class AppListSortOrder;
  14. // Controls and coordinates the toast nudge views in app list. Note that it
  15. // currently assumes that at most one nudge could be visible at a time.
  16. class ASH_EXPORT AppListNudgeController {
  17. public:
  18. // The type of nudge that is currently showing.
  19. enum class NudgeType {
  20. // No nudge in app list is showing.
  21. kNone,
  22. // A nudge in continue section which notifies that recommended files are
  23. // going to be shown in continue section.
  24. kPrivacyNotice,
  25. // A nudge in app list that guide users to reorder apps using context menu.
  26. kReorderNudge,
  27. };
  28. AppListNudgeController();
  29. AppListNudgeController(const AppListNudgeController&) = delete;
  30. AppListNudgeController& operator=(const AppListNudgeController&) = delete;
  31. ~AppListNudgeController() = default;
  32. // Registers profile prefs.
  33. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  34. // Clears nudges profile perfs. Used when a new user session is added in the
  35. // app list controller. This feature can be toggled in chrome:://flags with
  36. // launcher-nudge-session-reset flag.
  37. static void ResetPrefsForNewUserSession(PrefService* prefs);
  38. // Gets the number of times that the nudge with type `type` has been shown.
  39. static int GetShownCount(PrefService* prefs, NudgeType type);
  40. // Set the value of global variable `g_reorder_nudge_disabled_for_test` to
  41. // disable showing the nudge.
  42. static void SetReorderNudgeDisabledForTest(bool is_disabled);
  43. // Set the value of global variable `g_privacy_nudge_accepted_for_test` to
  44. // disable showing the nudge.
  45. static void SetPrivacyNoticeAcceptedForTest(bool is_accepted);
  46. // Returns true if the reorder nudge should be shown.
  47. bool ShouldShowReorderNudge() const;
  48. // Called when the app list temporary sort order changes. Null `new_order`
  49. // indicates that the temporary sort order is cleared.
  50. void OnTemporarySortOrderChanged(
  51. const absl::optional<AppListSortOrder>& new_order);
  52. // Updates the privacy notice's accepted pref. The
  53. // caller of this function is responsible for the actual creation and removal
  54. // of the nudge view.
  55. void SetPrivacyNoticeAcceptedPref(bool accepted);
  56. // Updates the privacy notice's shown pref. The
  57. // caller of this function is responsible for the actual creation and removal
  58. // of the nudge view.
  59. void SetPrivacyNoticeShownPref(bool shown);
  60. // Whether the user has already accepted the privacy notice.
  61. bool IsPrivacyNoticeAccepted() const;
  62. // Whether the user has already seen the privacy notice.
  63. bool WasPrivacyNoticeShown() const;
  64. // Updates the nudge type when the privacy notice is showing or hiding. The
  65. // caller of this function is responsible for the actual creation and removal
  66. // of the nudge view.
  67. void SetPrivacyNoticeShown(bool shown);
  68. // Sets the new nudge visible state and updates the prefs. The caller of
  69. // this function is responsible for the actual creation and removal of the
  70. // nudge view.
  71. void SetNudgeVisible(bool is_nudge_visible, NudgeType type);
  72. // Sets the new nudge active state and updates the prefs. The caller of
  73. // this function is responsible for the actual creation and removal of the
  74. // nudge view. Note that inactive nudge state does not necessarily mean that
  75. // the nudge is hidden. A inactive nudge could be visible in the background.
  76. void SetNudgeActive(bool is_nudge_active, NudgeType type);
  77. // Called when the reorder nudge is dismissed. Updates the pref so the reorder
  78. // nudge will not show again.
  79. void OnReorderNudgeConfirmed();
  80. // Updates the the current nudge state in prefs to determine if a nudge should
  81. // be showing.
  82. void UpdateCurrentNudgeStateInPrefs(bool is_visible_updated,
  83. bool is_active_updated);
  84. NudgeType current_nudge() const { return current_nudge_; }
  85. bool is_visible() const { return is_visible_; }
  86. private:
  87. // If the nudge is hidden and the showing duration is long enough, increments
  88. // the shown count in prefs.
  89. void MaybeIncrementShownCountInPrefs(DictionaryPrefUpdate& update,
  90. base::TimeDelta duration);
  91. // The timestamp when the current nudge started showing.
  92. base::Time current_nudge_show_timestamp_;
  93. // Current type of nudge that is showing.
  94. NudgeType current_nudge_ = NudgeType::kNone;
  95. // Records if `current_nudge_` is visible.
  96. bool is_visible_ = false;
  97. // Records if `current_nudge_` is active. The nudge could be visible
  98. // but be shadowed by other views to be inactive.
  99. bool is_active_ = false;
  100. // Caches that the nudge is considered as shown before the next shown count
  101. // update.
  102. bool is_nudge_considered_as_shown_ = false;
  103. };
  104. } // namespace ash
  105. #endif // ASH_APP_LIST_VIEWS_APP_LIST_NUDGE_CONTROLLER_H_