Commit 2d548886 authored by Karel Král's avatar Karel Král Committed by Commit Bot

TracedValue: container scopes with copied name

Implement Array/Dictionary scopes that create the container with a
copied name. This provides an auto-closing equivalent to
|TracedValue::Begin{Dictionary,Array}[WithCopiedName]| API.

Bug: 1043616
Change-Id: I26640d39f8dcd65c049f0f961c95782b5c21d089
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2374208
Commit-Queue: Karel Král <karelkral@google.com>
Reviewed-by: default avatarAlexander Timin <altimin@chromium.org>
Reviewed-by: default avatarEric Seckler <eseckler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#802186}
parent 0389c24d
...@@ -882,49 +882,50 @@ std::string TracedValueJSON::ToFormattedJSON() const { ...@@ -882,49 +882,50 @@ std::string TracedValueJSON::ToFormattedJSON() const {
return str; return str;
} }
TracedValue::ArrayScope::ArrayScope(TracedValue* value) : value_(value) { TracedValue::ArrayScope::ArrayScope(TracedValue* value) : value_(value) {}
value_->BeginArray();
}
TracedValue::ArrayScope::ArrayScope(TracedValue* value, const char* name)
: value_(value) {
value_->BeginArray(name);
}
TracedValue::ArrayScope::~ArrayScope() { TracedValue::ArrayScope::~ArrayScope() {
value_->EndArray(); value_->EndArray();
} }
TracedValue::ArrayScope TracedValue::AppendArrayScoped() { TracedValue::ArrayScope TracedValue::AppendArrayScoped() {
BeginArray();
return TracedValue::ArrayScope(this); return TracedValue::ArrayScope(this);
} }
TracedValue::ArrayScope TracedValue::BeginArrayScoped(const char* name) { TracedValue::ArrayScope TracedValue::BeginArrayScoped(const char* name) {
return TracedValue::ArrayScope(this, name); BeginArray(name);
return TracedValue::ArrayScope(this);
} }
TracedValue::DictionaryScope::DictionaryScope(TracedValue* value) TracedValue::ArrayScope TracedValue::BeginArrayScopedWithCopiedName(
: value_(value) { base::StringPiece name) {
value_->BeginDictionary(); BeginArrayWithCopiedName(name);
return TracedValue::ArrayScope(this);
} }
TracedValue::DictionaryScope::DictionaryScope(TracedValue* value, TracedValue::DictionaryScope::DictionaryScope(TracedValue* value)
const char* name) : value_(value) {}
: value_(value) {
value_->BeginDictionary(name);
}
TracedValue::DictionaryScope::~DictionaryScope() { TracedValue::DictionaryScope::~DictionaryScope() {
value_->EndDictionary(); value_->EndDictionary();
} }
TracedValue::DictionaryScope TracedValue::AppendDictionaryScoped() { TracedValue::DictionaryScope TracedValue::AppendDictionaryScoped() {
BeginDictionary();
return TracedValue::DictionaryScope(this); return TracedValue::DictionaryScope(this);
} }
TracedValue::DictionaryScope TracedValue::BeginDictionaryScoped( TracedValue::DictionaryScope TracedValue::BeginDictionaryScoped(
const char* name) { const char* name) {
return TracedValue::DictionaryScope(this, name); BeginDictionary(name);
return TracedValue::DictionaryScope(this);
}
TracedValue::DictionaryScope TracedValue::BeginDictionaryScopedWithCopiedName(
base::StringPiece name) {
BeginDictionaryWithCopiedName(name);
return TracedValue::DictionaryScope(this);
} }
} // namespace trace_event } // namespace trace_event
......
...@@ -67,14 +67,17 @@ class BASE_EXPORT TracedValue : public ConvertableToTraceFormat { ...@@ -67,14 +67,17 @@ class BASE_EXPORT TracedValue : public ConvertableToTraceFormat {
void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead) override; void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead) override;
// Helper to open / close an array. The ctor of |ArrayScope| opens the array, // Helper to auto-close an array. The call to |ArrayScope::~ArrayScope| closes
// the dtor closes. To be used via |TracedValue::AppendArrayScoped| and // the array.
// |TracedValue::BeginArrayScoped|. //
// To be constructed using:
// |TracedValue::AppendArrayScoped|
// |TracedValue::BeginArrayScoped|
// |TracedValue::BeginArrayScopedWithCopiedName|
// //
// |ArrayScope| holds a |TracedValue| pointer which should remain a valid // |ArrayScope| holds a |TracedValue| pointer which should remain a valid
// pointer at the time |ArrayScope::~ArrayScope| is called. // pointer until |ArrayScope::~ArrayScope| is called.
// //
// |ArrayScope::ArrayScope| calls |TracedValue::BeginArray|
// |ArrayScope::~ArrayScope| calls |TracedValue::EndArray| (which checks if // |ArrayScope::~ArrayScope| calls |TracedValue::EndArray| (which checks if
// the held |TracedValue*| is in array state). // the held |TracedValue*| is in array state).
// //
...@@ -86,29 +89,37 @@ class BASE_EXPORT TracedValue : public ConvertableToTraceFormat { ...@@ -86,29 +89,37 @@ class BASE_EXPORT TracedValue : public ConvertableToTraceFormat {
// } // }
class BASE_EXPORT ArrayScope { class BASE_EXPORT ArrayScope {
public: public:
ArrayScope(const ArrayScope&) = delete;
ArrayScope(ArrayScope&&) = default;
ArrayScope& operator=(const ArrayScope&) = delete;
ArrayScope& operator=(ArrayScope&&) = default;
~ArrayScope(); ~ArrayScope();
private: private:
explicit ArrayScope(TracedValue* value); explicit ArrayScope(TracedValue* value);
explicit ArrayScope(TracedValue* value, const char* name);
TracedValue* value_; TracedValue* value_;
friend class TracedValue; friend class TracedValue;
}; };
// Call |BeginArray| or |BeginArrayWithCopiedName| with no / the same
// parameter and return an |ArrayScope| holding |this|.
ArrayScope AppendArrayScoped(); ArrayScope AppendArrayScoped();
ArrayScope BeginArrayScoped(const char* name); ArrayScope BeginArrayScoped(const char* name);
ArrayScope BeginArrayScopedWithCopiedName(base::StringPiece name);
// Helper to open / close a dictionary. The ctor of |DictionaryScope| opens // Helper to auto-close a dictionary. The call to
// the dictionary, the dtor closes. To be used via // |DictionaryScope::~DictionaryScope| closes the dictionary.
// |TracedValue::AppendDictionaryScoped| and //
// |TracedValue::BeginDictionaryScoped|. // To be constructed using:
// |TracedValue::AppendDictionaryScoped|
// |TracedValue::BeginDictionaryScoped|
// |TracedValue::BeginDictionaryScopedWithCopiedName|
// //
// |DictionaryScope| holds a |TracedValue| pointer which should remain a valid // |DictionaryScope| holds a |TracedValue| pointer which should remain a valid
// pointer at the time |DictionaryScope::~DictionaryScope| is called. // pointer until |DictionaryScope::~DictionaryScope| is called.
// //
// |DictionaryScope::DictionaryScope| calls |TracedValue::BeginDictionary|
// |DictionaryScope::~DictionaryScope| calls |TracedValue::EndDictionary| // |DictionaryScope::~DictionaryScope| calls |TracedValue::EndDictionary|
// (which checks if the held |TracedValue*| is in dictionary state). // (which checks if the held |TracedValue*| is in dictionary state).
// //
...@@ -120,19 +131,25 @@ class BASE_EXPORT TracedValue : public ConvertableToTraceFormat { ...@@ -120,19 +131,25 @@ class BASE_EXPORT TracedValue : public ConvertableToTraceFormat {
// } // }
class BASE_EXPORT DictionaryScope { class BASE_EXPORT DictionaryScope {
public: public:
DictionaryScope(const DictionaryScope&) = delete;
DictionaryScope(DictionaryScope&&) = default;
DictionaryScope& operator=(const DictionaryScope&) = delete;
DictionaryScope& operator=(DictionaryScope&&) = default;
~DictionaryScope(); ~DictionaryScope();
private: private:
explicit DictionaryScope(TracedValue* value); explicit DictionaryScope(TracedValue* value);
explicit DictionaryScope(TracedValue* value, const char* name);
TracedValue* value_; TracedValue* value_;
friend class TracedValue; friend class TracedValue;
}; };
// Call |BeginDictionary| or |BeginDictionaryWithCopiedName| with no / the
// same parameter and return a |DictionaryScope| holding |this|.
DictionaryScope AppendDictionaryScoped(); DictionaryScope AppendDictionaryScoped();
DictionaryScope BeginDictionaryScoped(const char* name); DictionaryScope BeginDictionaryScoped(const char* name);
DictionaryScope BeginDictionaryScopedWithCopiedName(base::StringPiece name);
class BASE_EXPORT Array; class BASE_EXPORT Array;
class BASE_EXPORT Dictionary; class BASE_EXPORT Dictionary;
......
...@@ -66,7 +66,7 @@ TEST(TraceEventArgumentTest, ArrayAndDictionaryScope) { ...@@ -66,7 +66,7 @@ TEST(TraceEventArgumentTest, ArrayAndDictionaryScope) {
{ {
auto surround_dictionary = auto surround_dictionary =
value->BeginDictionaryScoped("outside_dictionary"); value->BeginDictionaryScoped("outside_dictionary");
value->SetBoolean("my_bool:", true); value->SetBoolean("my_bool", true);
{ {
auto inside_array = value->BeginArrayScoped("inside_array"); auto inside_array = value->BeginArrayScoped("inside_array");
value->AppendBoolean(false); value->AppendBoolean(false);
...@@ -81,22 +81,32 @@ TEST(TraceEventArgumentTest, ArrayAndDictionaryScope) { ...@@ -81,22 +81,32 @@ TEST(TraceEventArgumentTest, ArrayAndDictionaryScope) {
value->AppendBoolean(false); value->AppendBoolean(false);
{ {
auto inside_dictionary = value->AppendDictionaryScoped(); auto inside_dictionary = value->AppendDictionaryScoped();
value->SetBoolean("my_bool:", true); value->SetBoolean("my_bool", true);
} }
{ {
auto inside_array = value->AppendArrayScoped(); auto inside_array = value->AppendArrayScoped();
value->AppendBoolean(false); value->AppendBoolean(false);
} }
} }
{
auto dictionary = value->BeginDictionaryScopedWithCopiedName(
std::string("wonderful_") + std::string("world"));
}
{
auto array = value->BeginArrayScopedWithCopiedName(
std::string("wonderful_") + std::string("array"));
}
std::string json; std::string json;
value->AppendAsTraceFormat(&json); value->AppendAsTraceFormat(&json);
EXPECT_EQ( EXPECT_EQ(
"{" "{"
"\"dictionary_name\":{\"my_int\":1}," "\"dictionary_name\":{\"my_int\":1},"
"\"array_name\":[2]," "\"array_name\":[2],"
"\"outside_dictionary\":{\"my_bool:\":true,\"inside_array\":[false]," "\"outside_dictionary\":{\"my_bool\":true,\"inside_array\":[false],"
"\"inside_dictionary\":{\"inner_bool\":false}}," "\"inside_dictionary\":{\"inner_bool\":false}},"
"\"outside_array\":[false,{\"my_bool:\":true},[false]]" "\"outside_array\":[false,{\"my_bool\":true},[false]],"
"\"wonderful_world\":{},"
"\"wonderful_array\":[]"
"}", "}",
json); json);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment