1 // Copyright 2019 The Chromium Authors. All rights reserved.
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 MEDIA_BASE_MEDIA_LOG_TYPE_ENFORCEMENT_H_
6 #define MEDIA_BASE_MEDIA_LOG_TYPE_ENFORCEMENT_H_
7 
8 #include "media/base/media_serializers.h"
9 
10 namespace media {
11 
12 namespace internal {
13 enum class UnmatchableType {};
14 }  // namespace internal
15 
16 // Forward declare the enums.
17 enum class MediaLogProperty;
18 enum class MediaLogEvent;
19 
20 // Allow only specific types for an individual property.
21 template <MediaLogProperty PROP, typename T>
22 struct MediaLogPropertyTypeSupport {};
23 
24 // Allow only specific types for an individual event.
25 // However unlike Property, T is not required, so we default it to some
26 // unmatchable type that will never be passed as an argument accidentally.
27 template <MediaLogEvent EVENT, typename T = internal::UnmatchableType>
28 struct MediaLogEventTypeSupport {};
29 
30 // Lets us define the supported type in a single line in media_log_properties.h.
31 #define MEDIA_LOG_PROPERTY_SUPPORTS_TYPE(PROPERTY, TYPE)                 \
32   template <>                                                            \
33   struct MediaLogPropertyTypeSupport<MediaLogProperty::PROPERTY, TYPE> { \
34     static base::Value Convert(const TYPE& type) {                       \
35       return MediaSerialize<TYPE>(type);                                 \
36     }                                                                    \
37   }
38 
39 #define MEDIA_LOG_EVENT_NAMED_DATA(EVENT, TYPE, DISPLAY)           \
40   template <>                                                      \
41   struct MediaLogEventTypeSupport<MediaLogEvent::EVENT, TYPE> {    \
42     static void AddExtraData(base::Value* params, const TYPE& t) { \
43       DCHECK(params);                                              \
44       params->SetKey(DISPLAY, MediaSerialize<TYPE>(t));            \
45     }                                                              \
46     static std::string TypeName() { return #EVENT; }               \
47   }
48 
49 #define MEDIA_LOG_EVENT_NAMED_DATA_OP(EVENT, TYPE, DISPLAY, OP)    \
50   template <>                                                      \
51   struct MediaLogEventTypeSupport<MediaLogEvent::EVENT, TYPE> {    \
52     static void AddExtraData(base::Value* params, const TYPE& t) { \
53       DCHECK(params);                                              \
54       params->SetKey(DISPLAY, MediaSerialize<TYPE>(OP(t)));        \
55     }                                                              \
56     static std::string TypeName() { return #EVENT; }               \
57   }
58 
59 // Specifically do not create the Convert or DisplayName methods
60 #define MEDIA_LOG_EVENT_TYPELESS(EVENT)                   \
61   template <>                                             \
62   struct MediaLogEventTypeSupport<MediaLogEvent::EVENT> { \
63     static std::string TypeName() { return #EVENT; }      \
64     static void AddExtraData(base::Value* params) {}      \
65   }
66 
67 }  // namespace media
68 
69 #endif  // MEDIA_BASE_MEDIA_LOG_TYPE_ENFORCEMENT_H_
70