• 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 #ifndef COMPONENTS_METRICS_STRUCTURED_STORAGE_MANAGER_H_
6 #define COMPONENTS_METRICS_STRUCTURED_STORAGE_MANAGER_H_
7 
8 #include "components/metrics/structured/lib/event_buffer.h"
9 #include "components/metrics/structured/lib/event_storage.h"
10 #include "third_party/metrics_proto/structured_data.pb.h"
11 
12 namespace metrics::structured {
13 
14 // Enum representing why events were deleted.
15 enum class DeleteReason {
16   // The events have been uploaded. The events were not lost.
17   kUploaded,
18   // The events were deleted because we are consuming to much disk space. Events
19   // are lost.
20   kExceededQuota,
21 };
22 
23 // The Storage Manager is responsible for storing and managing Structured
24 // Metrics events.
25 //
26 // The Storage Manager is responsible for the following:
27 //   * Flushing events to a local file when memory is exceeded.
28 //   * Dropping events when storage is exceeded.
29 //   * Retrieving events for upload.
30 //
31 // The Storage Manager is not thread safe, with the exception of the
32 // StorageService interface. Therefore, construction and destruction must be
33 // done on the same sequence. This class exists to provide an API to
34 // StructuredMetricsService which only exists in //components.
35 class StorageManager : public EventStorage<StructuredEventProto> {
36  public:
37   // An object that is using the Storage Manager for storing events.
38   //
39   // This interface is used to by the Storage Manager to provide information
40   // externally.
41   class StorageDelegate {
42    public:
43     StorageDelegate() = default;
44     virtual ~StorageDelegate() = default;
45 
46     // Called when a buffer has been flushed to disk.
47     virtual void OnFlushed(const FlushedKey& key) = 0;
48 
49     // Called when flushed events have been deleted.
50     virtual void OnDeleted(const FlushedKey& key, DeleteReason reason) = 0;
51   };
52 
53   // Ideally, this would take a StorageDelegate as a parameter but because of
54   // how the StructuredMetricsService needs to be constructed, this isn't
55   // possible right now.
56   StorageManager();
57 
58   ~StorageManager() override;
59 
60   // Should only be called by the owner of |this|.
set_delegate(StorageDelegate * delegate)61   void set_delegate(StorageDelegate* delegate) {
62     CHECK(delegate);
63     delegate_ = delegate;
64   }
65 
unset_delegate(StorageDelegate * delegate)66   void unset_delegate(StorageDelegate* delegate) {
67     if (delegate_ == delegate) {
68       delegate_ = nullptr;
69     }
70   }
71 
72  protected:
73   void NotifyOnFlushed(const FlushedKey& key);
74 
75   void NotifyOnDeleted(const FlushedKey& key, DeleteReason reason);
76 
77   // The owner of |this|.
78   raw_ptr<StorageDelegate> delegate_ = nullptr;
79 };
80 
81 }  // namespace metrics::structured
82 
83 #endif  // COMPONENTS_METRICS_STRUCTURED_STORAGE_MANAGER_H_
84