Enable base::Value::Dict and base::Value::List to be used with tracing.
Change-Id: Ic0cb35a53cfa179f2fbb5b82819c8fd0fe5d82da Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3696804 Reviewed-by: Matt Menke <mmenke@chromium.org> Commit-Queue: Daniel Cheng <dcheng@chromium.org> Reviewed-by: Alexander Timin <altimin@chromium.org> Cr-Commit-Position: refs/heads/main@{#1012560}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
65c691433c
commit
b5862bc519
@ -789,6 +789,13 @@ std::string Value::Dict::DebugString() const {
|
||||
return DebugStringImpl(*this);
|
||||
}
|
||||
|
||||
void Value::Dict::WriteIntoTrace(perfetto::TracedValue context) const {
|
||||
perfetto::TracedDictionary dict = std::move(context).WriteDictionary();
|
||||
for (auto kv : *this) {
|
||||
dict.Add(perfetto::DynamicString(kv.first), kv.second);
|
||||
}
|
||||
}
|
||||
|
||||
Value::Dict::Dict(
|
||||
const flat_map<std::string, std::unique_ptr<Value>>& storage) {
|
||||
storage_.reserve(storage.size());
|
||||
@ -992,6 +999,13 @@ std::string Value::List::DebugString() const {
|
||||
return DebugStringImpl(*this);
|
||||
}
|
||||
|
||||
void Value::List::WriteIntoTrace(perfetto::TracedValue context) const {
|
||||
perfetto::TracedArray array = std::move(context).WriteArray();
|
||||
for (const auto& item : *this) {
|
||||
array.Append(item);
|
||||
}
|
||||
}
|
||||
|
||||
Value::List::List(const std::vector<Value>& storage) {
|
||||
storage_.reserve(storage.size());
|
||||
for (const auto& value : storage) {
|
||||
@ -1522,38 +1536,26 @@ std::string Value::DebugString() const {
|
||||
|
||||
#if BUILDFLAG(ENABLE_BASE_TRACING)
|
||||
void Value::WriteIntoTrace(perfetto::TracedValue context) const {
|
||||
switch (type()) {
|
||||
case Type::BOOLEAN:
|
||||
std::move(context).WriteBoolean(GetBool());
|
||||
return;
|
||||
case Type::INTEGER:
|
||||
std::move(context).WriteInt64(GetInt());
|
||||
return;
|
||||
case Type::DOUBLE:
|
||||
std::move(context).WriteDouble(GetDouble());
|
||||
return;
|
||||
case Type::STRING:
|
||||
std::move(context).WriteString(GetString());
|
||||
return;
|
||||
case Type::BINARY:
|
||||
std::move(context).WriteString("<binary data not supported>");
|
||||
return;
|
||||
case Type::DICTIONARY: {
|
||||
perfetto::TracedDictionary dict = std::move(context).WriteDictionary();
|
||||
for (auto kv : DictItems())
|
||||
dict.Add(perfetto::DynamicString{kv.first}, kv.second);
|
||||
return;
|
||||
}
|
||||
case Type::LIST: {
|
||||
perfetto::TracedArray array = std::move(context).WriteArray();
|
||||
for (const auto& item : GetListDeprecated())
|
||||
array.Append(item);
|
||||
return;
|
||||
}
|
||||
case Type::NONE:
|
||||
Visit([&](const auto& member) {
|
||||
using T = std::decay_t<decltype(member)>;
|
||||
if constexpr (std::is_same_v<T, absl::monostate>) {
|
||||
std::move(context).WriteString("<none>");
|
||||
return;
|
||||
}
|
||||
} else if constexpr (std::is_same_v<T, bool>) {
|
||||
std::move(context).WriteBoolean(member);
|
||||
} else if constexpr (std::is_same_v<T, int>) {
|
||||
std::move(context).WriteInt64(member);
|
||||
} else if constexpr (std::is_same_v<T, DoubleStorage>) {
|
||||
std::move(context).WriteDouble(member);
|
||||
} else if constexpr (std::is_same_v<T, std::string>) {
|
||||
std::move(context).WriteString(member);
|
||||
} else if constexpr (std::is_same_v<T, BlobStorage>) {
|
||||
std::move(context).WriteString("<binary data not supported>");
|
||||
} else if constexpr (std::is_same_v<T, Dict>) {
|
||||
member.WriteIntoTrace(std::move(context));
|
||||
} else if constexpr (std::is_same_v<T, List>) {
|
||||
member.WriteIntoTrace(std::move(context));
|
||||
}
|
||||
});
|
||||
}
|
||||
#endif // BUILDFLAG(ENABLE_BASE_TRACING)
|
||||
|
||||
|
@ -521,6 +521,11 @@ class BASE_EXPORT GSL_OWNER Value {
|
||||
// Serializes to a string for logging and debug purposes.
|
||||
std::string DebugString() const;
|
||||
|
||||
#if BUILDFLAG(ENABLE_BASE_TRACING)
|
||||
// Write this object into a trace.
|
||||
void WriteIntoTrace(perfetto::TracedValue) const;
|
||||
#endif // BUILDFLAG(ENABLE_BASE_TRACING)
|
||||
|
||||
private:
|
||||
BASE_EXPORT friend bool operator==(const Dict& lhs, const Dict& rhs);
|
||||
BASE_EXPORT friend bool operator!=(const Dict& lhs, const Dict& rhs);
|
||||
@ -638,6 +643,11 @@ class BASE_EXPORT GSL_OWNER Value {
|
||||
// Serializes to a string for logging and debug purposes.
|
||||
std::string DebugString() const;
|
||||
|
||||
#if BUILDFLAG(ENABLE_BASE_TRACING)
|
||||
// Write this object into a trace.
|
||||
void WriteIntoTrace(perfetto::TracedValue) const;
|
||||
#endif // BUILDFLAG(ENABLE_BASE_TRACING)
|
||||
|
||||
private:
|
||||
BASE_EXPORT friend bool operator==(const List& lhs, const List& rhs);
|
||||
BASE_EXPORT friend bool operator!=(const List& lhs, const List& rhs);
|
||||
|
@ -2497,14 +2497,18 @@ TEST(ValuesTest, TracingSupport) {
|
||||
EXPECT_EQ(perfetto::TracedValueToString(Value("value")), "value");
|
||||
EXPECT_EQ(perfetto::TracedValueToString(Value(Value::Type::NONE)), "<none>");
|
||||
{
|
||||
Value::ListStorage list;
|
||||
list.emplace_back(2);
|
||||
list.emplace_back(3);
|
||||
EXPECT_EQ(perfetto::TracedValueToString(Value(list)), "[2,3]");
|
||||
Value::List list;
|
||||
EXPECT_EQ(perfetto::TracedValueToString(list), "{}");
|
||||
list.Append(2);
|
||||
list.Append(3);
|
||||
EXPECT_EQ(perfetto::TracedValueToString(list), "[2,3]");
|
||||
EXPECT_EQ(perfetto::TracedValueToString(Value(std::move(list))), "[2,3]");
|
||||
}
|
||||
{
|
||||
Value::Dict dict;
|
||||
EXPECT_EQ(perfetto::TracedValueToString(dict), "{}");
|
||||
dict.Set("key", "value");
|
||||
EXPECT_EQ(perfetto::TracedValueToString(dict), "{key:value}");
|
||||
EXPECT_EQ(perfetto::TracedValueToString(Value(std::move(dict))),
|
||||
"{key:value}");
|
||||
}
|
||||
|
Reference in New Issue
Block a user