xref: /aosp_15_r20/external/cronet/components/metrics/structured/mojom/event_mojom_traits.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_MOJOM_EVENT_MOJOM_TRAITS_H_
6 #define COMPONENTS_METRICS_STRUCTURED_MOJOM_EVENT_MOJOM_TRAITS_H_
7 
8 #include <memory>
9 #include <optional>
10 
11 #include "base/strings/string_number_conversions.h"
12 #include "components/metrics/structured/event.h"
13 #include "components/metrics/structured/mojom/event.mojom.h"
14 #include "mojo/public/cpp/bindings/struct_traits.h"
15 #include "mojo/public/cpp/bindings/union_traits.h"
16 
17 namespace mojo {
18 
19 // Converts MetricValue into the union mojom::MetricValue and vice versa.
20 template <>
21 struct UnionTraits<metrics::structured::mojom::MetricValueDataView,
22                    metrics::structured::Event::MetricValue> {
23   static metrics::structured::mojom::MetricValueDataView::Tag GetTag(
24       const metrics::structured::Event::MetricValue& metric_value);
25 
26   static const std::string& hmac_value(
27       const metrics::structured::Event::MetricValue& metric_value) {
28     return metric_value.value.GetString();
29   }
30 
31   static int64_t long_value(
32       const metrics::structured::Event::MetricValue& metric_value) {
33     int64_t long_value;
34     base::StringToInt64(metric_value.value.GetString(), &long_value);
35     return long_value;
36   }
37 
38   static int32_t int_value(
39       const metrics::structured::Event::MetricValue& metric_value) {
40     return metric_value.value.GetInt();
41   }
42 
43   static double double_value(
44       const metrics::structured::Event::MetricValue& metric_value) {
45     return metric_value.value.GetDouble();
46   }
47 
48   static const std::string& raw_str_value(
49       const metrics::structured::Event::MetricValue& metric_value) {
50     return metric_value.value.GetString();
51   }
52 
53   static bool bool_value(
54       const metrics::structured::Event::MetricValue& metric_value) {
55     return metric_value.value.GetBool();
56   }
57 
58   static bool Read(metrics::structured::mojom::MetricValueDataView metric,
59                    metrics::structured::Event::MetricValue* out);
60 };
61 
62 // Converts mojom::Event to/from Event, so that Event can be used throughout the
63 // codebase without any direct reference to mojom::Event.
64 template <>
65 class StructTraits<metrics::structured::mojom::EventDataView,
66                    metrics::structured::Event> {
67  public:
68   static const std::string& project_name(
69       const metrics::structured::Event& event) {
70     return event.project_name();
71   }
72 
73   static const std::string& event_name(
74       const metrics::structured::Event& event) {
75     return event.event_name();
76   }
77 
78   static const std::map<std::string, metrics::structured::Event::MetricValue>&
79   metrics(const metrics::structured::Event& event) {
80     return event.metric_values();
81   }
82 
83   static std::optional<base::TimeDelta> system_uptime(
84       const metrics::structured::Event& event);
85 
86   static bool is_event_sequence(const metrics::structured::Event& event) {
87     return event.IsEventSequenceType();
88   }
89 
90   static bool Read(metrics::structured::mojom::EventDataView event,
91                    metrics::structured::Event* out);
92 };
93 
94 }  // namespace mojo
95 
96 #endif  // COMPONENTS_METRICS_STRUCTURED_MOJOM_EVENT_MOJOM_TRAITS_H_
97