blob: 810966f082eff0c19647749ef6b13f3a830f7616 [file] [log] [blame]
rajendrant8d2982942025-02-04 02:44:171// 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
8namespace 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.
14class 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 Jiang5e50f182025-02-24 19:48:3931 // Returns the time when the most recent nudge was shown.
32 std::optional<base::TimeTicks> GetMostRecentNudgeTime() const;
33
rajendrant8d2982942025-02-04 02:44:1734 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