File: browsing_data_counter.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (237 lines) | stat: -rw-r--r-- 8,541 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_BROWSING_DATA_CORE_COUNTERS_BROWSING_DATA_COUNTER_H_
#define COMPONENTS_BROWSING_DATA_CORE_COUNTERS_BROWSING_DATA_COUNTER_H_

#include <stdint.h>

#include <memory>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "components/browsing_data/core/clear_browsing_data_tab.h"
#include "components/prefs/pref_member.h"

class PrefService;

namespace browsing_data {

class BrowsingDataCounter {
 public:
  typedef int64_t ResultInt;

  // Base class of results returned by BrowsingDataCounter. When the computation
  // has started, an instance is returned to represent a pending result.
  class Result {
   public:
    explicit Result(const BrowsingDataCounter* source);

    Result(const Result&) = delete;
    Result& operator=(const Result&) = delete;

    virtual ~Result();

    const BrowsingDataCounter* source() const { return source_; }
    virtual bool Finished() const;

   private:
    raw_ptr<const BrowsingDataCounter, DanglingUntriaged> source_;
  };

  // A subclass of Result returned when the computation has finished. The result
  // value can be retrieved by calling |Value()|. Some BrowsingDataCounter
  // subclasses might use a subclass of FinishedResult to provide more complex
  // results.
  class FinishedResult : public Result {
   public:
    FinishedResult(const BrowsingDataCounter* source, ResultInt value);

    FinishedResult(const FinishedResult&) = delete;
    FinishedResult& operator=(const FinishedResult&) = delete;

    ~FinishedResult() override;

    // Result:
    bool Finished() const override;

    ResultInt Value() const;

   private:
    ResultInt value_;
  };

  // A subclass of FinishedResult that besides |Value()| also stores whether
  // the datatype is synced.
  class SyncResult : public FinishedResult {
   public:
    SyncResult(const BrowsingDataCounter* source,
               ResultInt value,
               bool sync_enabled);

    SyncResult(const SyncResult&) = delete;
    SyncResult& operator=(const SyncResult&) = delete;

    ~SyncResult() override;

    bool is_sync_enabled() const { return sync_enabled_; }

   private:
    bool sync_enabled_;
  };

  typedef base::RepeatingCallback<void(std::unique_ptr<Result>)> ResultCallback;

  // Every calculation progresses through a state machine. At initialization,
  // the counter is IDLE. If a result is calculated within a given time
  // interval, it is immediately reported and the counter is again IDLE.
  // Otherwise, the counter instructs the UI to show a "Calculating..."
  // message and transitions to the SHOW_CALCULATING state. The counter stays
  // in this state for a given amount of time. If a result is calculated at
  // this time, it is stored, but not immediately reported. After the timer
  // elapses, we check if a result has been reported in the meantime. If yes,
  // we transition to REPORT_STAGED_RESULT, report the result, and then return
  // to IDLE. If not, we transition to READY_TO_REPORT_RESULT. In this
  // state, we wait for the calculation to finish. When that happens, we show
  // the result, and  return to the IDLE state.
  enum class State {
    IDLE,
    RESTARTED,
    SHOW_CALCULATING,
    REPORT_STAGED_RESULT,
    READY_TO_REPORT_RESULT,
  };

  BrowsingDataCounter();
  virtual ~BrowsingDataCounter();

  // Should be called once to initialize this class.
  //
  // TODO(crbug.com/331925113): Since the Clear Browsing Data dialog no longer
  // updates preferences in real time, this method should be deprecated in favor
  // of |InitWithoutPeriodPref()| and |InitWithoutPref()|. Counters should
  // be explicitly restarted from the UI when needed instead of observing
  // preference changes.
  void Init(PrefService* pref_service,
            ClearBrowsingDataTab clear_browsing_data_tab,
            ResultCallback callback);

  // Can be called instead of |Init()|, to create a counter that doesn't
  // observe pref changes for the time range period - instead, the period is
  // specified explicitly through |begin_time|.
  void InitWithoutPeriodPref(PrefService* pref_service,
                             ClearBrowsingDataTab clear_browsing_data_tab,
                             base::Time begin_time,
                             ResultCallback callback);

  // Can be called instead of |Init()|, to create a counter that doesn't
  // observe pref changes for the time range period - instead, the period is
  // specified explicitly through |begin_time|. Additionally, this counter is
  // also not associated with any datatype preference of the Clear Browsing
  // Data dialog.
  //
  // This mode doesn't use delayed responses.
  void InitWithoutPref(base::Time begin_time, ResultCallback callback);

  // Name of the preference associated with this counter.
  virtual const char* GetPrefName() const = 0;

  // Restarts the counter. Will be called automatically if the counting needs
  // to be restarted, e.g. when the deletion preference changes state or when
  // we are notified of data changes.
  void Restart();

  // Changes the |begin_time| for this counter. May only be used if the counter
  // is not associated with a time period pref, i.e. if it was initialized
  // through |InitWithoutPeriodPref()| or |InitWithoutPeriodPref()|.
  //
  // This forces a restart, as changing the time range while the counter is
  // running could cause the already running calculation to start using the
  // new time.
  virtual void SetBeginTime(base::Time begin_time);

  // Returns the state transition of this counter since past restart.
  // Used only for testing.
  const std::vector<State>& GetStateTransitionsForTesting();

 protected:
  // Should be called from |Count| by any overriding class to indicate that
  // counting is finished and report |value| as the result.
  void ReportResult(ResultInt value);

  // A convenience overload of the previous method that allows subclasses to
  // provide a custom |result|.
  void ReportResult(std::unique_ptr<Result> result);

  // A synchronous implementation of ReportResult(). Called immediately in the
  // RESTARTED and READY_TO_REPORT_RESULT states, called later if the counter is
  // in the SHOW_CALCULATING stage. This method is made virtual to be overriden
  // in tests.
  virtual void DoReportResult(std::unique_ptr<Result> result);

  // Calculates the beginning of the counting period as |period_| before now.
  base::Time GetPeriodStart();

  // Calculates the ending of the counting period.
  base::Time GetPeriodEnd();

  // Returns if this counter belongs to a preference on the default, basic or
  // advanced CBD tab.
  ClearBrowsingDataTab GetTab() const;

 private:
  // Called after the class is initialized by calling |Init|.
  virtual void OnInitialized();

  // Count the data. Call ReportResult() when finished. Tasks that are still
  // running should be cancelled to avoid reporting old results.
  virtual void Count() = 0;

  // State transition methods.
  void TransitionToShowCalculating();
  void TransitionToReadyToReportResult();

  // Indicates if this counter belongs to a preference on the basic CBD tab.
  ClearBrowsingDataTab clear_browsing_data_tab_;

  // The callback that will be called when the UI should be updated with a new
  // counter value.
  ResultCallback callback_;

  // The boolean preference indicating whether this data type is to be deleted.
  BooleanPrefMember pref_;

  // The integer preference describing the time period for which this data type
  // is to be deleted.
  IntegerPrefMember period_;

  // This time period is used when |period_| is not initialized.
  base::Time begin_time_;

  // Whether this class was properly initialized by calling |Init|.
  bool initialized_;

  // Whether to introduce a delayed response to avoid flickering.
  bool use_delay_;

  // State of the counter.
  State state_;

  // State transitions since the last restart.
  std::vector<State> state_transitions_;

  // A result is staged if it arrives during the SHOW_CALCULATING state.
  std::unique_ptr<Result> staged_result_;

  // A timer to time the RESTARTED->SHOW_CALCULATION and
  // SHOW_CALCULATION->READY_TO_REPORT_RESULT state transitions.
  base::OneShotTimer timer_;
};

}  // namespace browsing_data

#endif  // COMPONENTS_BROWSING_DATA_CORE_COUNTERS_BROWSING_DATA_COUNTER_H_