• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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_EVENT_VALIDATOR_H_
6 #define COMPONENTS_METRICS_STRUCTURED_EVENT_VALIDATOR_H_
7 
8 #include <cstdint>
9 #include <string>
10 
11 #include "components/metrics/structured/enums.h"
12 #include "components/metrics/structured/event.h"
13 #include "third_party/abseil-cpp/absl/types/optional.h"
14 
15 namespace metrics {
16 namespace structured {
17 
18 // Interface to be implemented by codegen for every event to validate
19 // messages received by the structured metric service.
20 class EventValidator {
21  public:
22   // Metadata about a registered metric.
23   struct MetricMetadata {
24     Event::MetricType metric_type;
25     uint64_t metric_name_hash;
26   };
27 
28   // Should not be copied or moved.
29   EventValidator(const EventValidator&) = delete;
30   EventValidator& operator=(const EventValidator& other) = delete;
31 
32   virtual ~EventValidator();
33 
34   // Returns the event validator if |metric_name| is a valid metric for this
35   // event. This method is virtual because a static constexpr map will be
36   // defined within each event validator implementation.
37   virtual absl::optional<MetricMetadata> GetMetricMetadata(
38       const std::string& metric_name) const = 0;
39 
40   uint64_t event_hash() const;
41 
42  protected:
43   // Should not be constructed directly.
44   explicit EventValidator(uint64_t event_hash);
45 
46  private:
47   uint64_t event_hash_;
48 };
49 
50 }  // namespace structured
51 }  // namespace metrics
52 
53 #endif  // COMPONENTS_METRICS_STRUCTURED_EVENT_VALIDATOR_H_
54