• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 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 //------------------------------------------------------------------------------
6 // Usage example:
7 //
8 // At metrics collection site:
9 // dwa::builders::MyEvent(source_id)
10 //    .SetMyMetric(metric_value)
11 //    .Record(dwa_recorder.get());
12 //------------------------------------------------------------------------------
13 
14 #ifndef COMPONENTS_METRICS_DWA_DWA_RECORDER_H_
15 #define COMPONENTS_METRICS_DWA_DWA_RECORDER_H_
16 
17 #include <string>
18 #include <vector>
19 
20 #include "base/component_export.h"
21 #include "base/feature_list.h"
22 #include "base/sequence_checker.h"
23 #include "components/metrics/dwa/mojom/dwa_interface.mojom.h"
24 #include "third_party/metrics_proto/dwa/deidentified_web_analytics.pb.h"
25 
26 namespace metrics::dwa {
27 
28 // Enables DWA recording.
29 COMPONENT_EXPORT(DWA) BASE_DECLARE_FEATURE(kDwaFeature);
30 
COMPONENT_EXPORT(DWA)31 class COMPONENT_EXPORT(DWA) DwaRecorder {
32  public:
33   DwaRecorder();
34 
35   DwaRecorder(const DwaRecorder&) = delete;
36   DwaRecorder& operator=(const DwaRecorder&) = delete;
37 
38   ~DwaRecorder();
39 
40   void EnableRecording();
41   void DisableRecording();
42 
43   // Deletes all unsent entries and page load events.
44   void Purge();
45 
46   // Returns whether this DwaRecorder is enabled.
47   bool IsEnabled();
48 
49   // Provides access to a global DwaRecorder instance for recording metrics.
50   // This is typically passed to the Record() method of an entry object from
51   // dwa_builders.h.
52   static DwaRecorder* Get();
53 
54   // Saves all entries into a page load event on every page load. This method is
55   // called once per page load. The purpose this needs to be called once per
56   // page load is because the dwa proto collects aggregates events in terms of
57   // "page load events".
58   // TODO(b/369473036): Bind OnPageLoad method to call on every page load
59   void OnPageLoad();
60 
61   // Adds an entry to the DwaEntry list.
62   void AddEntry(metrics::dwa::mojom::DwaEntryPtr entry);
63 
64   // Returns true if DwaEntry list contains entries.
65   bool HasEntries();
66 
67   // Takes all existing |page_load_events_| out from DwaRecorder and returns it.
68   std::vector<::dwa::PageLoadEvents> TakePageLoadEvents();
69 
70   // Returns true if |page_load_events_| is non-empty.
71   bool HasPageLoadEvents();
72 
73  private:
74   SEQUENCE_CHECKER(sequence_checker_);
75 
76   // Local storage for the list of entries.
77   std::vector<::metrics::dwa::mojom::DwaEntryPtr> entries_;
78 
79   // Local storage for the entries for page load events.
80   std::vector<::dwa::PageLoadEvents> page_load_events_;
81 
82   bool recorder_enabled_ = false;
83 };
84 
85 }  // namespace metrics::dwa
86 
87 #endif  // COMPONENTS_METRICS_DWA_DWA_RECORDER_H_
88