1 // Copyright 2023 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 #ifndef COMPONENTS_METRICS_STRUCTURED_LIB_EVENT_STORAGE_H_ 6 #define COMPONENTS_METRICS_STRUCTURED_LIB_EVENT_STORAGE_H_ 7 8 #include "third_party/protobuf/src/google/protobuf/message_lite.h" 9 10 namespace metrics { 11 class StructuredEventProto; 12 } // namespace metrics 13 14 namespace metrics::structured { 15 16 class EventsProto; 17 18 // Abstraction for how events are stored in Structured Metrics. 19 template <typename T, 20 template <class> class Container = 21 ::google::protobuf::RepeatedPtrField> 22 class EventStorage { 23 public: 24 EventStorage() = default; 25 26 virtual ~EventStorage() = default; 27 28 EventStorage(const EventStorage&) = delete; 29 EventStorage& operator=(const EventStorage&) = delete; 30 IsReady()31 virtual bool IsReady() { return true; } 32 33 // A callback to be run when the storage is ready. OnReady()34 virtual void OnReady() {} 35 36 // Add a new StructuredEventProto to be stored. 37 virtual void AddEvent(T event) = 0; 38 39 // External API for removing events from the storage. 40 // Intended to be used with a Swap for improved performance. 41 virtual Container<T> TakeEvents() = 0; 42 43 // The number of events that have been recorded. 44 virtual int RecordedEventsCount() const = 0; 45 46 // Checks if events have been stored. HasEvents()47 bool HasEvents() const { return RecordedEventsCount() > 0; } 48 49 // Delete all events. 50 virtual void Purge() = 0; 51 52 // Copies the events out of the event storage. CopyEvents(EventsProto * events_proto)53 virtual void CopyEvents(EventsProto* events_proto) const {} 54 55 // Temporary API for external metrics. AddBatchEvents(const Container<T> & events)56 virtual void AddBatchEvents(const Container<T>& events) {} 57 }; 58 59 } // namespace metrics::structured 60 61 #endif // COMPONENTS_METRICS_STRUCTURED_LIB_EVENT_STORAGE_H_ 62