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