rajendrant | 8d298294 | 2025-02-04 02:44:17 | [diff] [blame] | 1 | // Copyright 2025 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/containers/queue.h" |
| 6 | #include "base/time/time.h" |
| 7 | |
| 8 | namespace contextual_cueing { |
| 9 | |
| 10 | // Keeps track of timestamps of recent nudges for the sake of capping nudge |
| 11 | // count over a period of time. (i.e. x nudges per y hours). This queue is |
| 12 | // maintained such that it only has timestamps necessary to enforce the limits. |
| 13 | // Old timestamps will be trimmed. |
| 14 | class NudgeCapTracker { |
| 15 | public: |
| 16 | NudgeCapTracker(size_t cap_count, base::TimeDelta duration); |
| 17 | |
| 18 | NudgeCapTracker(const NudgeCapTracker&) = delete; |
| 19 | NudgeCapTracker& operator=(const NudgeCapTracker&) = delete; |
| 20 | |
| 21 | NudgeCapTracker(NudgeCapTracker&& source); |
| 22 | ~NudgeCapTracker(); |
| 23 | |
| 24 | // Returns whether the nudge is eligible to be shown. |
| 25 | bool CanShowNudge() const; |
| 26 | |
| 27 | // Notifies that the nudge was shown to user. This tracks the shown timestamps |
| 28 | // for nudge cap calculation. |
| 29 | void CueingNudgeShown(); |
| 30 | |
Zekun Jiang | 5e50f18 | 2025-02-24 19:48:39 | [diff] [blame] | 31 | // Returns the time when the most recent nudge was shown. |
| 32 | std::optional<base::TimeTicks> GetMostRecentNudgeTime() const; |
| 33 | |
rajendrant | 8d298294 | 2025-02-04 02:44:17 | [diff] [blame] | 34 | private: |
| 35 | base::queue<base::TimeTicks> recent_nudge_timestamps_; |
| 36 | |
| 37 | // The nudge can be shown `cap_count_` times over `duration_`. |
| 38 | const size_t cap_count_; |
| 39 | const base::TimeDelta duration_; |
| 40 | }; |
| 41 | |
| 42 | } // namespace contextual_cueing |