xref: /aosp_15_r20/external/cronet/components/metrics/structured/project_validator.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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_PROJECT_VALIDATOR_H_
6 #define COMPONENTS_METRICS_STRUCTURED_PROJECT_VALIDATOR_H_
7 
8 #include <cstdint>
9 #include <optional>
10 #include <string>
11 
12 #include "components/metrics/structured/enums.h"
13 #include "components/metrics/structured/event_validator.h"
14 #include "third_party/metrics_proto/structured_data.pb.h"
15 
16 namespace metrics::structured {
17 
18 using EventType = StructuredEventProto_EventType;
19 
20 // Interface to be implemented by codegen for every project to validate
21 // messages received by the structured metric service.
22 class ProjectValidator {
23  public:
24   // Should not be copied or moved.
25   ProjectValidator(const ProjectValidator&) = delete;
26   ProjectValidator& operator=(const ProjectValidator& other) = delete;
27 
28   virtual ~ProjectValidator();
29 
30   // Returns the event validator if |event_name| is a valid event for this
31   // project.
32   const EventValidator* GetEventValidator(base::StringPiece event_name) const;
33 
34   std::optional<base::StringPiece> GetEventName(uint64_t event_name_hash) const;
35 
project_hash()36   uint64_t project_hash() const { return project_hash_; }
id_type()37   IdType id_type() const { return id_type_; }
id_scope()38   IdScope id_scope() const { return id_scope_; }
event_type()39   EventType event_type() const { return event_type_; }
key_rotation_period()40   int key_rotation_period() const { return key_rotation_period_; }
41 
42  protected:
43   // Should not be constructed directly.
44   ProjectValidator(uint64_t project_hash,
45                    IdType id_type,
46                    IdScope id_scope,
47                    EventType event_type,
48                    int key_rotation_period);
49 
50   std::unordered_map<base::StringPiece, std::unique_ptr<EventValidator>>
51       event_validators_;
52 
53   std::unordered_map<uint64_t, base::StringPiece> event_name_map_;
54 
55  private:
56   const uint64_t project_hash_;
57   const IdType id_type_;
58   const IdScope id_scope_;
59   const EventType event_type_;
60   const int key_rotation_period_;
61 };
62 
63 }  // namespace metrics::structured
64 
65 #endif  // COMPONENTS_METRICS_STRUCTURED_PROJECT_VALIDATOR_H_
66