1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
base / trace_event / traced_value_support.h [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_TRACE_EVENT_TRACED_VALUE_SUPPORT_H_
#define BASE_TRACE_EVENT_TRACED_VALUE_SUPPORT_H_
#include <optional>
#include <string_view>
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/unguessable_token.h"
#include "third_party/perfetto/include/perfetto/tracing/traced_proto.h"
#include "third_party/perfetto/include/perfetto/tracing/traced_value.h"
// This file contains specialisations for trace serialisation for key
// widely-used //base classes. As these specialisations require full definition
// of perfetto::TracedValue and almost every source unit in Chromium requires
// one of these //base concepts, include specialiazations here and expose them
// to the users including trace_event.h, rather than adding a dependency from
// scoped_refptr.h et al on traced_value.h.
namespace perfetto {
// If T is serialisable into a trace, scoped_refptr<T> is serialisable as well.
template <class T>
struct TraceFormatTraits<scoped_refptr<T>,
perfetto::check_traced_value_support_t<T>> {
static void WriteIntoTrace(perfetto::TracedValue context,
const scoped_refptr<T>& value) {
if (!value) {
std::move(context).WritePointer(nullptr);
return;
}
perfetto::WriteIntoTracedValue(std::move(context), *value);
}
template <class MessageType>
static void WriteIntoTrace(perfetto::TracedProto<MessageType> context,
const scoped_refptr<T>& value) {
if (value) {
// Proto message without any fields is treated as nullptr.
return;
}
perfetto::WriteIntoTracedProto(std::move(context), *value);
}
};
// If T is serialisable into a trace, base::WeakPtr<T> is serialisable as well.
template <class T>
struct TraceFormatTraits<::base::WeakPtr<T>,
perfetto::check_traced_value_support_t<T>> {
static void WriteIntoTrace(perfetto::TracedValue context,
const ::base::WeakPtr<T>& value) {
if (!value) {
std::move(context).WritePointer(nullptr);
return;
}
perfetto::WriteIntoTracedValue(std::move(context), *value);
}
};
// If T is serialisable into a trace, std::optional<T> is serialisable as well.
// Note that we need definitions for both std::optional<T>& and
// const std::optional<T>& (unlike scoped_refptr and WeakPtr above), as
// dereferencing const scoped_refptr<T>& gives you T, while dereferencing const
// std::optional<T>& gives you const T&.
template <class T>
struct TraceFormatTraits<::std::optional<T>,
perfetto::check_traced_value_support_t<T>> {
static void WriteIntoTrace(perfetto::TracedValue context,
const ::std::optional<T>& value) {
if (!value) {
std::move(context).WritePointer(nullptr);
return;
}
perfetto::WriteIntoTracedValue(std::move(context), *value);
}
static void WriteIntoTrace(perfetto::TracedValue context,
::std::optional<T>& value) {
if (!value) {
std::move(context).WritePointer(nullptr);
return;
}
perfetto::WriteIntoTracedValue(std::move(context), *value);
}
};
// If T is serialisable into a trace, raw_ptr<T> is serialisable as well.
template <class T, ::base::RawPtrTraits Traits>
struct TraceFormatTraits<::base::raw_ptr<T, Traits>,
perfetto::check_traced_value_support_t<T>> {
static void WriteIntoTrace(perfetto::TracedValue context,
const ::base::raw_ptr<T, Traits>& value) {
perfetto::WriteIntoTracedValue(std::move(context), value.get());
}
static void WriteIntoTrace(perfetto::TracedValue context,
::base::raw_ptr<T, Traits>& value) {
perfetto::WriteIntoTracedValue(std::move(context), value.get());
}
};
// If T is serialisable into a trace, raw_ref<T> is serialisable as well.
template <class T, ::base::RawPtrTraits Traits>
struct TraceFormatTraits<::base::raw_ref<T, Traits>,
perfetto::check_traced_value_support_t<T>> {
static void WriteIntoTrace(perfetto::TracedValue context,
const ::base::raw_ref<T, Traits>& value) {
perfetto::WriteIntoTracedValue(std::move(context), value.get());
}
static void WriteIntoTrace(perfetto::TracedValue context,
::base::raw_ref<T, Traits>& value) {
perfetto::WriteIntoTracedValue(std::move(context), value.get());
}
};
// Time-related classes.
// TODO(altimin): Make them first-class primitives in TracedValue and Perfetto
// UI.
template <>
struct TraceFormatTraits<::base::TimeDelta> {
static void WriteIntoTrace(perfetto::TracedValue context,
const ::base::TimeDelta& value) {
std::move(context).WriteInt64(value.InMicroseconds());
}
};
template <>
struct TraceFormatTraits<::base::TimeTicks> {
static void WriteIntoTrace(perfetto::TracedValue context,
const ::base::TimeTicks& value) {
perfetto::WriteIntoTracedValue(std::move(context), value.since_origin());
}
};
template <>
struct TraceFormatTraits<::base::Time> {
static void WriteIntoTrace(perfetto::TracedValue context,
const ::base::Time& value) {
perfetto::WriteIntoTracedValue(std::move(context), value.since_origin());
}
};
// base::UnguessableToken.
// TODO(altimin): Add first-class primitive, which will allow to show a
// human-comprehensible alias for all unguessable tokens instead.
template <>
struct TraceFormatTraits<::base::UnguessableToken> {
static void WriteIntoTrace(perfetto::TracedValue context,
const ::base::UnguessableToken& value) {
return std::move(context).WriteString(value.ToString());
}
};
// UTF-16 string support.
template <>
struct TraceFormatTraits<std::u16string> {
static void WriteIntoTrace(perfetto::TracedValue context,
const std::u16string& value) {
return std::move(context).WriteString(::base::UTF16ToUTF8(value));
}
};
template <size_t N>
struct TraceFormatTraits<char16_t[N]> {
static void WriteIntoTrace(perfetto::TracedValue context,
const char16_t value[N]) {
return std::move(context).WriteString(
::base::UTF16ToUTF8(::std::u16string_view(value)));
}
};
template <>
struct TraceFormatTraits<const char16_t*> {
static void WriteIntoTrace(perfetto::TracedValue context,
const char16_t* value) {
return std::move(context).WriteString(
::base::UTF16ToUTF8(::std::u16string_view(value)));
}
};
// Wide string support.
template <>
struct TraceFormatTraits<std::wstring> {
static void WriteIntoTrace(perfetto::TracedValue context,
const std::wstring& value) {
return std::move(context).WriteString(::base::WideToUTF8(value));
}
};
template <size_t N>
struct TraceFormatTraits<wchar_t[N]> {
static void WriteIntoTrace(perfetto::TracedValue context,
const wchar_t value[N]) {
return std::move(context).WriteString(
::base::WideToUTF8(::std::wstring_view(value)));
}
};
template <>
struct TraceFormatTraits<const wchar_t*> {
static void WriteIntoTrace(perfetto::TracedValue context,
const wchar_t* value) {
return std::move(context).WriteString(
::base::WideToUTF8(::std::wstring_view(value)));
}
};
// std::string_view support.
template <>
struct TraceFormatTraits<::std::u16string_view> {
static void WriteIntoTrace(perfetto::TracedValue context,
::std::u16string_view value) {
return std::move(context).WriteString(::base::UTF16ToUTF8(value));
}
};
template <>
struct TraceFormatTraits<::std::wstring_view> {
static void WriteIntoTrace(perfetto::TracedValue context,
::std::wstring_view value) {
return std::move(context).WriteString(::base::WideToUTF8(value));
}
};
} // namespace perfetto
#endif // BASE_TRACE_EVENT_TRACED_VALUE_SUPPORT_H_