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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//------------------------------------------------------------------------------
// Usage example:
//
// At metrics collection site:
// dwa::builders::MyEvent(source_id)
// .SetMyMetric(metric_value)
// .Record(dwa_recorder.get());
//------------------------------------------------------------------------------
#ifndef COMPONENTS_METRICS_DWA_DWA_RECORDER_H_
#define COMPONENTS_METRICS_DWA_DWA_RECORDER_H_
#include <string>
#include <vector>
#include "base/component_export.h"
#include "base/feature_list.h"
#include "base/sequence_checker.h"
#include "components/metrics/dwa/mojom/dwa_interface.mojom.h"
#include "third_party/metrics_proto/dwa/deidentified_web_analytics.pb.h"
namespace metrics::dwa {
// Enables DWA recording.
COMPONENT_EXPORT(DWA_RECORDER) BASE_DECLARE_FEATURE(kDwaFeature);
class COMPONENT_EXPORT(DWA_RECORDER) DwaRecorder {
public:
DwaRecorder();
DwaRecorder(const DwaRecorder&) = delete;
DwaRecorder& operator=(const DwaRecorder&) = delete;
~DwaRecorder();
void EnableRecording();
void DisableRecording();
// Deletes all unsent entries and page load events.
void Purge();
// Returns whether this DwaRecorder is enabled.
bool IsEnabled();
// Provides access to a global DwaRecorder instance for recording metrics.
// This is typically passed to the Record() method of an entry object from
// dwa_builders.h.
static DwaRecorder* Get();
// Saves all entries into a page load event on every page load. This method is
// called once per page load. The purpose this needs to be called once per
// page load is because the dwa proto collects aggregates events in terms of
// "page load events".
// TODO(b/369473036): Bind OnPageLoad method to call on every page load
void OnPageLoad();
// Adds an entry to the DwaEntry list.
void AddEntry(metrics::dwa::mojom::DwaEntryPtr entry);
// Returns true if DwaEntry list contains entries.
bool HasEntries();
// Takes all existing |page_load_events_| out from DwaRecorder and returns it.
std::vector<::dwa::PageLoadEvents> TakePageLoadEvents();
// Returns true if |page_load_events_| is non-empty.
bool HasPageLoadEvents();
// Returns a vector to internal list of DwaEntryPtr for testing.
const std::vector<metrics::dwa::mojom::DwaEntryPtr>& GetEntriesForTesting()
const;
private:
SEQUENCE_CHECKER(sequence_checker_);
// Local storage for the list of entries.
std::vector<::metrics::dwa::mojom::DwaEntryPtr> entries_;
// Local storage for the entries for page load events.
std::vector<::dwa::PageLoadEvents> page_load_events_;
bool recorder_enabled_ = false;
};
} // namespace metrics::dwa
#endif // COMPONENTS_METRICS_DWA_DWA_RECORDER_H_
|