File: report_queue_configuration.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (164 lines) | stat: -rw-r--r-- 6,718 bytes parent folder | download | duplicates (6)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2020 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_REPORTING_CLIENT_REPORT_QUEUE_CONFIGURATION_H_
#define COMPONENTS_REPORTING_CLIENT_REPORT_QUEUE_CONFIGURATION_H_

#include <memory>
#include <optional>
#include <string>
#include <utility>

#include "base/functional/callback.h"
#include "components/reporting/proto/synced/record.pb.h"
#include "components/reporting/proto/synced/record_constants.pb.h"
#include "components/reporting/util/rate_limiter_interface.h"
#include "components/reporting/util/status.h"
#include "components/reporting/util/statusor.h"
#include "components/reporting/util/wrapped_rate_limiter.h"

namespace reporting {

// |EventType| enum is used to distinguish between user and device event types,
// and inherently determine the type of DM tokens (user vs device) generated.
enum class EventType { kDevice, kUser };

// ReportQueueConfiguration configures a report queue.
// |dm_token| if set will be attached to all records generated with this queue.
// Pass user DM tokens where applicable so the server can associate these events
// with the user. |event_type| describes the event type being reported and is
// indirectly used to retrieve DM tokens for downstream processing. Please use
// |EventType::kUser| for events that need to be associated with the current
// user. |destination| indicates what server side handler will be handling the
// records that are generated by the ReportQueueImpl. |policy_check_callback_|
// is a RepeatingCallback that verifies the specific report queue is allowed.
class ReportQueueConfiguration {
 public:
  // PolicyCheckCallbacks should return error::UNAUTHENTICATED if a policy check
  // fails due to policies, any other error as appropriate, and OK if a policy
  // check is successful.
  using PolicyCheckCallback = base::RepeatingCallback<Status(void)>;

  // Transient settings used by `ReportQueueConfiguration` instantiation.
  struct Settings {
    EventType event_type = EventType::kDevice;
    Destination destination = Destination::UNDEFINED_DESTINATION;
    int64_t reserved_space = 0L;
  };

  // Transient moveable helper class for composing `ReportQueueConfiguration`
  // out of settings and then allowing to set non-trivial fields.
  class Builder {
   public:
    explicit Builder(const Settings& settings);
    Builder(Builder&&);
    Builder& operator=(const Builder&) = delete;
    ~Builder();

    // Modifiers for non-trivial fields.
    Builder SetPolicyCheckCallback(PolicyCheckCallback policy_check_callback);
    Builder SetRateLimiter(std::unique_ptr<RateLimiterInterface> rate_limiter);
    Builder SetDMToken(std::string_view dm_token);
    Builder SetSourceInfo(std::optional<SourceInfo> source_info);

    // Finalizes the builder (no modifications are accepted after that) and
    // outputs the final `ReportQueueConfiguration` or status.
    StatusOr<std::unique_ptr<ReportQueueConfiguration>> Build();

   private:
    StatusOr<std::unique_ptr<ReportQueueConfiguration>> final_value_;
  };

  ~ReportQueueConfiguration();
  ReportQueueConfiguration(const ReportQueueConfiguration& other) = delete;
  ReportQueueConfiguration& operator=(const ReportQueueConfiguration& other) =
      delete;

  // Factory for generating a `ReportQueueConfiguration`.
  // The factory produces `Builder` thus allowing to set non-trivial fields.
  // Once everything is set, `Builder` can be assigned (once!) using
  // cast operator to `StatusOr<std::unique_ptr<ReportQueueConfiguration>>()`.
  //
  // Example usage:
  //   StatusOr<reporting::ReportQueueConfiguration> config_result =
  //       reporting::ReportQueueConfiguration::Create(
  //           {.event_type = EventType::kUser,
  //            .destination = Destination::HEART_BEAT})
  //           .SetRateLimiter(std::make_unique<RateLimiterTokenBucket>(
  //               /*max_level=*/1024,
  //               /*filling_time=*/base::Minutes(10)))
  //           .Build();
  //   if (!config_result.has_value()) {
  //     return config_result.error();
  //   }
  //   auto config = config_result.value();
  static Builder Create(const Settings& settings);

  // Deprecated and should not be used. Use `Create({settings})` instead.
  //
  // Factory for generating a ReportQueueConfiguration.
  // If any of the parameters are invalid, will return error::INVALID_ARGUMENT.
  // `dm_token` is valid when dm_token.is_valid() is true.
  // `destination` is valid when it is any value other than
  // Destination::UNDEFINED_DESTINATION.
  // If `reserved_space` > 0, underlying Storage would only accept enqueue
  // request if after it remaining amount of disk space will not drop below
  // `reserved_space`. Intended for use by opportunistic queue.
  static StatusOr<std::unique_ptr<ReportQueueConfiguration>> Create(
      std::string_view dm_token,
      Destination destination,
      PolicyCheckCallback policy_check_callback,
      std::unique_ptr<RateLimiterInterface> rate_limiter = nullptr,
      int64_t reserved_space = 0L);

  Destination destination() const { return destination_; }

  std::string dm_token() { return dm_token_; }

  EventType event_type() const { return event_type_; }

  // Returns a repeating callback that checks whether an event of the given size
  // is allowed by the attached rate limiter. If there is no rate limiter,
  // allowed always.
  WrappedRateLimiter::AsyncAcquireCb is_event_allowed_cb() const {
    return is_event_allowed_cb_;
  }

  int64_t reserved_space() const { return reserved_space_; }

  std::optional<SourceInfo> source_info() const { return source_info_; }

  Status SetDMToken(std::string_view dm_token);

  Status CheckPolicy() const;

 private:
  friend class Builder;

  ReportQueueConfiguration();

  Status SetEventType(EventType event_type);
  Status SetDestination(Destination destination);
  Status SetReservedSpace(int64_t reserved_space);
  Status SetPolicyCheckCallback(PolicyCheckCallback policy_check_callback);
  Status SetRateLimiter(std::unique_ptr<RateLimiterInterface> rate_limiter);
  Status SetSourceInfo(std::optional<SourceInfo> source_info);

  std::string dm_token_;
  EventType event_type_;
  Destination destination_;

  PolicyCheckCallback policy_check_callback_;

  WrappedRateLimiter::SmartPtr wrapped_rate_limiter_{
      nullptr, base::OnTaskRunnerDeleter(nullptr)};
  WrappedRateLimiter::AsyncAcquireCb is_event_allowed_cb_;

  int64_t reserved_space_ = 0L;  // By default queues are not opportunistic.
  std::optional<SourceInfo> source_info_ = std::nullopt;
};

}  // namespace reporting

#endif  // COMPONENTS_REPORTING_CLIENT_REPORT_QUEUE_CONFIGURATION_H_