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
|
// 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.
#ifndef COMPONENTS_METRICS_STRUCTURED_LIB_EVENT_BUFFER_H_
#define COMPONENTS_METRICS_STRUCTURED_LIB_EVENT_BUFFER_H_
#include <concepts>
#include <type_traits>
#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/time/time.h"
#include "base/types/expected.h"
#include "components/metrics/structured/lib/resource_info.h"
#include "third_party/protobuf/src/google/protobuf/message_lite.h"
namespace metrics::structured {
// The result of adding an event to a buffer.
enum class Result {
// Event was added successfully.
kOk = 0,
// Event was added but another might not fit.
kShouldFlush = 1,
// Event was not added. Must flush now.
kFull = 2,
// Any error that occurred.
kError = 3,
};
// Errors of flushing a buffer to disk.
enum FlushError {
kQuotaExceeded = 1,
kWriteError = 2,
kDiskFull = 3,
kSerializationFailed = 4,
};
// Represents a flushed buffer.
struct FlushedKey {
// The size of the file flushed.
int64_t size;
// Path of the file.
base::FilePath path;
// When the flushed file was created.
base::Time creation_time;
};
using FlushedCallback =
base::OnceCallback<void(base::expected<FlushedKey, FlushError>)>;
// Abstraction for how in-memory events are managed on device.
template <typename T>
requires(std::derived_from<T, google::protobuf::MessageLite>)
class EventBuffer {
public:
explicit EventBuffer(ResourceInfo info) : resource_info_(info) {}
virtual ~EventBuffer() = default;
// Adds an event to |this| buffer.
virtual Result AddEvent(T event) = 0;
// Clears the content of the buffer.
virtual void Purge() = 0;
// The number of events stored in the buffer.
virtual uint64_t Size() = 0;
// Serialize the contents of |this|.
virtual google::protobuf::RepeatedPtrField<T> Serialize() = 0;
// Flushes the buffer to |path|, once the flush is complete |callback| is
// executed.
virtual void Flush(const base::FilePath& path, FlushedCallback callback) = 0;
const ResourceInfo& resource_info() const { return resource_info_; }
protected:
ResourceInfo resource_info_;
};
} // namespace metrics::structured
#endif // COMPONENTS_METRICS_STRUCTURED_LIB_EVENT_BUFFER_H_
|