traced_value_support.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2021 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 BASE_TRACE_EVENT_TRACED_VALUE_SUPPORT_H_
  5. #define BASE_TRACE_EVENT_TRACED_VALUE_SUPPORT_H_
  6. #include "base/memory/scoped_refptr.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/strings/string_piece.h"
  9. #include "base/strings/utf_string_conversions.h"
  10. #include "base/time/time.h"
  11. #include "base/unguessable_token.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. #include "third_party/perfetto/include/perfetto/tracing/traced_proto.h"
  14. #include "third_party/perfetto/include/perfetto/tracing/traced_value.h"
  15. // This file contains specialisations for trace serialisation for key
  16. // widely-used //base classes. As these specialisations require full definition
  17. // of perfetto::TracedValue and almost every source unit in Chromium requires
  18. // one of these //base concepts, include specialiazations here and expose them
  19. // to the users including trace_event.h, rather than adding a dependency from
  20. // scoped_refptr.h et al on traced_value.h.
  21. namespace perfetto {
  22. // If T is serialisable into a trace, scoped_refptr<T> is serialisable as well.
  23. template <class T>
  24. struct TraceFormatTraits<scoped_refptr<T>,
  25. perfetto::check_traced_value_support_t<T>> {
  26. static void WriteIntoTrace(perfetto::TracedValue context,
  27. const scoped_refptr<T>& value) {
  28. if (!value) {
  29. std::move(context).WritePointer(nullptr);
  30. return;
  31. }
  32. perfetto::WriteIntoTracedValue(std::move(context), *value);
  33. }
  34. template <class MessageType>
  35. static void WriteIntoTrace(perfetto::TracedProto<MessageType> context,
  36. const scoped_refptr<T>& value) {
  37. if (value) {
  38. // Proto message without any fields is treated as nullptr.
  39. return;
  40. }
  41. perfetto::WriteIntoTracedProto(std::move(context), *value);
  42. }
  43. };
  44. // If T is serialisable into a trace, base::WeakPtr<T> is serialisable as well.
  45. template <class T>
  46. struct TraceFormatTraits<::base::WeakPtr<T>,
  47. perfetto::check_traced_value_support_t<T>> {
  48. static void WriteIntoTrace(perfetto::TracedValue context,
  49. const ::base::WeakPtr<T>& value) {
  50. if (!value) {
  51. std::move(context).WritePointer(nullptr);
  52. return;
  53. }
  54. perfetto::WriteIntoTracedValue(std::move(context), *value);
  55. }
  56. };
  57. // If T is serialisable into a trace, absl::optional<T> is serialisable as well.
  58. // Note that we need definitions for both absl::optional<T>& and
  59. // const absl::optional<T>& (unlike scoped_refptr and WeakPtr above), as
  60. // dereferencing const scoped_refptr<T>& gives you T, while dereferencing const
  61. // absl::optional<T>& gives you const T&.
  62. template <class T>
  63. struct TraceFormatTraits<::absl::optional<T>,
  64. perfetto::check_traced_value_support_t<T>> {
  65. static void WriteIntoTrace(perfetto::TracedValue context,
  66. const ::absl::optional<T>& value) {
  67. if (!value) {
  68. std::move(context).WritePointer(nullptr);
  69. return;
  70. }
  71. perfetto::WriteIntoTracedValue(std::move(context), *value);
  72. }
  73. static void WriteIntoTrace(perfetto::TracedValue context,
  74. ::absl::optional<T>& value) {
  75. if (!value) {
  76. std::move(context).WritePointer(nullptr);
  77. return;
  78. }
  79. perfetto::WriteIntoTracedValue(std::move(context), *value);
  80. }
  81. };
  82. // Time-related classes.
  83. // TODO(altimin): Make them first-class primitives in TracedValue and Perfetto
  84. // UI.
  85. template <>
  86. struct TraceFormatTraits<::base::TimeDelta> {
  87. static void WriteIntoTrace(perfetto::TracedValue context,
  88. const ::base::TimeDelta& value) {
  89. std::move(context).WriteInt64(value.InMicroseconds());
  90. }
  91. };
  92. template <>
  93. struct TraceFormatTraits<::base::TimeTicks> {
  94. static void WriteIntoTrace(perfetto::TracedValue context,
  95. const ::base::TimeTicks& value) {
  96. perfetto::WriteIntoTracedValue(std::move(context), value.since_origin());
  97. }
  98. };
  99. template <>
  100. struct TraceFormatTraits<::base::Time> {
  101. static void WriteIntoTrace(perfetto::TracedValue context,
  102. const ::base::Time& value) {
  103. perfetto::WriteIntoTracedValue(std::move(context), value.since_origin());
  104. }
  105. };
  106. // base::UnguessableToken.
  107. // TODO(altimin): Add first-class primitive, which will allow to show a
  108. // human-comprehensible alias for all unguessable tokens instead.
  109. template <>
  110. struct TraceFormatTraits<::base::UnguessableToken> {
  111. static void WriteIntoTrace(perfetto::TracedValue context,
  112. const ::base::UnguessableToken& value) {
  113. return std::move(context).WriteString(value.ToString());
  114. }
  115. };
  116. // UTF-16 string support.
  117. template <>
  118. struct TraceFormatTraits<std::u16string> {
  119. static void WriteIntoTrace(perfetto::TracedValue context,
  120. const std::u16string& value) {
  121. return std::move(context).WriteString(::base::UTF16ToUTF8(value));
  122. }
  123. };
  124. template <size_t N>
  125. struct TraceFormatTraits<char16_t[N]> {
  126. static void WriteIntoTrace(perfetto::TracedValue context,
  127. const char16_t value[N]) {
  128. return std::move(context).WriteString(
  129. ::base::UTF16ToUTF8(::base::StringPiece16(value)));
  130. }
  131. };
  132. template <>
  133. struct TraceFormatTraits<const char16_t*> {
  134. static void WriteIntoTrace(perfetto::TracedValue context,
  135. const char16_t* value) {
  136. return std::move(context).WriteString(
  137. ::base::UTF16ToUTF8(::base::StringPiece16(value)));
  138. }
  139. };
  140. // Wide string support.
  141. template <>
  142. struct TraceFormatTraits<std::wstring> {
  143. static void WriteIntoTrace(perfetto::TracedValue context,
  144. const std::wstring& value) {
  145. return std::move(context).WriteString(::base::WideToUTF8(value));
  146. }
  147. };
  148. template <size_t N>
  149. struct TraceFormatTraits<wchar_t[N]> {
  150. static void WriteIntoTrace(perfetto::TracedValue context,
  151. const wchar_t value[N]) {
  152. return std::move(context).WriteString(
  153. ::base::WideToUTF8(::base::WStringPiece(value)));
  154. }
  155. };
  156. template <>
  157. struct TraceFormatTraits<const wchar_t*> {
  158. static void WriteIntoTrace(perfetto::TracedValue context,
  159. const wchar_t* value) {
  160. return std::move(context).WriteString(
  161. ::base::WideToUTF8(::base::WStringPiece(value)));
  162. }
  163. };
  164. // base::StringPiece support.
  165. template <>
  166. struct TraceFormatTraits<::base::StringPiece> {
  167. static void WriteIntoTrace(perfetto::TracedValue context,
  168. ::base::StringPiece value) {
  169. return std::move(context).WriteString(value.data(), value.length());
  170. }
  171. };
  172. template <>
  173. struct TraceFormatTraits<::base::StringPiece16> {
  174. static void WriteIntoTrace(perfetto::TracedValue context,
  175. ::base::StringPiece16 value) {
  176. return std::move(context).WriteString(::base::UTF16ToUTF8(value));
  177. }
  178. };
  179. template <>
  180. struct TraceFormatTraits<::base::WStringPiece> {
  181. static void WriteIntoTrace(perfetto::TracedValue context,
  182. ::base::WStringPiece value) {
  183. return std::move(context).WriteString(::base::WideToUTF8(value));
  184. }
  185. };
  186. } // namespace perfetto
  187. #endif // BASE_TRACE_EVENT_TRACED_VALUE_SUPPORT_H_