media_log_type_enforcement.h 3.0 KB

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