clipboard_nudge_controller.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2020 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_CLIPBOARD_CLIPBOARD_NUDGE_CONTROLLER_H_
  5. #define ASH_CLIPBOARD_CLIPBOARD_NUDGE_CONTROLLER_H_
  6. #include "ash/ash_export.h"
  7. #include "ash/clipboard/clipboard_history.h"
  8. #include "ash/clipboard/clipboard_history_controller_impl.h"
  9. #include "ash/clipboard/clipboard_nudge.h"
  10. #include "ash/clipboard/clipboard_nudge_constants.h"
  11. #include "ash/public/cpp/clipboard_history_controller.h"
  12. #include "ash/public/cpp/session/session_observer.h"
  13. #include "ash/system/tray/system_nudge_controller.h"
  14. #include "base/time/clock.h"
  15. #include "base/time/time.h"
  16. #include "chromeos/crosapi/mojom/clipboard_history.mojom.h"
  17. #include "ui/base/clipboard/clipboard_observer.h"
  18. class PrefService;
  19. class PrefRegistrySimple;
  20. class ClipboardHistoryItem;
  21. namespace ash {
  22. // The clipboard contextual nudge will be shown after 4 user actions that must
  23. // happen in sequence. The user must perform copy, paste, copy and paste in
  24. // sequence to activate the nudge.
  25. enum class ClipboardState {
  26. kInit = 0,
  27. kFirstCopy = 1,
  28. kFirstPaste = 2,
  29. kSecondCopy = 3,
  30. kShouldShowNudge = 4,
  31. };
  32. class ASH_EXPORT ClipboardNudgeController
  33. : public SystemNudgeController,
  34. public ClipboardHistory::Observer,
  35. public ui::ClipboardObserver,
  36. public SessionObserver,
  37. public ClipboardHistoryController::Observer {
  38. public:
  39. class TimeMetricHelper {
  40. public:
  41. TimeMetricHelper() = default;
  42. TimeMetricHelper(const TimeMetricHelper&) = delete;
  43. TimeMetricHelper& operator=(const TimeMetricHelper&) = delete;
  44. ~TimeMetricHelper() = default;
  45. bool ShouldLogFeatureUsedTime() const;
  46. bool ShouldLogFeatureOpenTime() const;
  47. base::TimeDelta GetTimeSinceShown(base::Time current_time) const;
  48. void ResetTime();
  49. void set_was_logged_as_used() { was_logged_as_used_ = true; }
  50. void set_was_logged_as_opened() { was_logged_as_opened_ = true; }
  51. private:
  52. base::Time last_shown_time_;
  53. bool was_logged_as_used_ = false;
  54. bool was_logged_as_opened_ = false;
  55. };
  56. ClipboardNudgeController(
  57. ClipboardHistory* clipboard_history,
  58. ClipboardHistoryControllerImpl* clipboard_history_controller);
  59. ClipboardNudgeController(const ClipboardNudgeController&) = delete;
  60. ClipboardNudgeController& operator=(const ClipboardNudgeController&) = delete;
  61. ~ClipboardNudgeController() override;
  62. // Registers profile prefs.
  63. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  64. // ui::ClipboardHistory::Observer:
  65. void OnClipboardHistoryItemAdded(const ClipboardHistoryItem& item,
  66. bool is_duplicate = false) override;
  67. // ui::ClipboardObserver:
  68. void OnClipboardDataRead() override;
  69. // SessionObserver:
  70. void OnActiveUserPrefServiceChanged(PrefService* prefs) override;
  71. // Resets nudge state and show nudge timer.
  72. void HandleNudgeShown();
  73. // Checks whether we should show the context menu 'new' badge.
  74. bool ShouldShowNewFeatureBadge();
  75. // Increment the 'new' feature badge shown count.
  76. void MarkNewFeatureBadgeShown();
  77. // Increment the screenshot notification shown count.
  78. void MarkScreenshotNotificationShown();
  79. // ClipboardHistoryControllerImpl:
  80. void OnClipboardHistoryMenuShown(
  81. crosapi::mojom::ClipboardHistoryControllerShowSource show_source)
  82. override;
  83. void OnClipboardHistoryPasted() override;
  84. // Shows the nudge widget.
  85. void ShowNudge(ClipboardNudgeType nudge_type);
  86. // Test methods for overriding and resetting the clock used by GetTime.
  87. void OverrideClockForTesting(base::Clock* test_clock);
  88. void ClearClockOverrideForTesting();
  89. const ClipboardState& GetClipboardStateForTesting();
  90. protected:
  91. // SystemNudgeController:
  92. std::unique_ptr<SystemNudge> CreateSystemNudge() override;
  93. private:
  94. // Gets the number of times the nudge has been shown.
  95. int GetShownCount(PrefService* prefs);
  96. // Gets the last time the nudge was shown.
  97. base::Time GetLastShownTime(PrefService* prefs);
  98. // Gets the number of times the context menu 'new' badge has been shown.
  99. int GetNewFeatureBadgeShownCount(PrefService* prefs);
  100. // Checks whether another nudge can be shown.
  101. bool ShouldShowNudge(PrefService* prefs);
  102. // Gets the current time. Can be overridden for testing.
  103. base::Time GetTime();
  104. // Time the nudge was last shown.
  105. TimeMetricHelper last_shown_time_;
  106. // Time the zero state nudge was last shown.
  107. TimeMetricHelper zero_state_last_shown_time_;
  108. // Time the new feature badge was last shown.
  109. TimeMetricHelper new_feature_last_shown_time_;
  110. // Time the screenshot notification nudge was last shown.
  111. TimeMetricHelper screenshot_notification_last_shown_time_;
  112. // Owned by ClipboardHistoryController.
  113. const ClipboardHistory* clipboard_history_;
  114. // Owned by ash/Shell.
  115. ClipboardHistoryControllerImpl* const clipboard_history_controller_;
  116. // Current clipboard state.
  117. ClipboardState clipboard_state_ = ClipboardState::kInit;
  118. // The timestamp of the most recent paste.
  119. base::Time last_paste_timestamp_;
  120. // The current nudge type being shown from ShowNudge().
  121. ClipboardNudgeType current_nudge_type_;
  122. };
  123. } // namespace ash
  124. #endif // ASH_CLIPBOARD_CLIPBOARD_NUDGE_CONTROLLER_H_