Index: base/debug/trace_event_argument.cc |
diff --git a/base/debug/trace_event_argument.cc b/base/debug/trace_event_argument.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1514185bcc171402c9115fd04a6ffcf1f057e70b |
--- /dev/null |
+++ b/base/debug/trace_event_argument.cc |
@@ -0,0 +1,148 @@ |
+// Copyright (c) 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/debug/trace_event_argument.h" |
+ |
+#include "base/json/json_writer.h" |
+#include "base/values.h" |
+ |
+namespace base { |
+ |
+namespace { |
+ |
+class ConvertibleToTraceFormatValue |
+ : public base::debug::ConvertableToTraceFormat { |
+ public: |
+ explicit ConvertibleToTraceFormatValue(Value* value) : m_value(value) {} |
+ virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE { |
+ std::string tmp; |
+ base::JSONWriter::Write(m_value.get(), &tmp); |
+ *out += tmp; |
+ } |
+ |
+ private: |
+ virtual ~ConvertibleToTraceFormatValue() {} |
+ |
+ scoped_ptr<Value> m_value; |
+ DISALLOW_COPY_AND_ASSIGN(ConvertibleToTraceFormatValue); |
+}; |
+} |
dsinclair
2014/07/10 13:32:14
Nit: blank line before }
nit: } // namespace
yurys
2014/07/10 15:06:29
Done.
|
+ |
+TracedValueBase::TracedValueBase() { |
+} |
+ |
+TracedValueBase::~TracedValueBase() { |
+} |
+ |
+void TracedValueBase::SetInteger(const char* name, int value) { |
+ GetCurrentDictionary()->SetInteger(name, value); |
+} |
+ |
+void TracedValueBase::SetDouble(const char* name, double value) { |
+ GetCurrentDictionary()->SetDouble(name, value); |
+} |
+ |
+void TracedValueBase::SetBoolean(const char* name, bool value) { |
+ GetCurrentDictionary()->SetBoolean(name, value); |
+} |
+ |
+void TracedValueBase::SetString(const char* name, const std::string& value) { |
+ GetCurrentDictionary()->SetString(name, value); |
+} |
+ |
+void TracedValueBase::BeginDictionaryNamed(const char* name) { |
+ DictionaryValue* dictionary = new DictionaryValue(); |
+ GetCurrentDictionary()->Set(name, dictionary); |
+ m_stack.push_back(dictionary); |
+} |
+ |
+void TracedValueBase::BeginArrayNamed(const char* name) { |
+ ListValue* array = new ListValue(); |
+ GetCurrentDictionary()->Set(name, array); |
+ m_stack.push_back(array); |
+} |
+ |
+void TracedValueBase::EndCurrentDictionary() { |
+ DCHECK(m_stack.size() > 1); |
+ DCHECK(GetCurrentDictionary()); |
+ m_stack.pop_back(); |
+} |
+ |
+void TracedValueBase::PushInteger(int value) { |
+ GetCurrentArray()->AppendInteger(value); |
+} |
+ |
+void TracedValueBase::PushDouble(double value) { |
+ GetCurrentArray()->AppendDouble(value); |
+} |
+ |
+void TracedValueBase::PushBoolean(bool value) { |
+ GetCurrentArray()->AppendBoolean(value); |
+} |
+ |
+void TracedValueBase::PushString(const std::string& value) { |
+ GetCurrentArray()->AppendString(value); |
+} |
+ |
+void TracedValueBase::PushArray() { |
+ ListValue* array = new ListValue(); |
+ GetCurrentArray()->Append(array); |
+ m_stack.push_back(array); |
+} |
+ |
+void TracedValueBase::PushDictionary() { |
+ DictionaryValue* dictionary = new DictionaryValue(); |
+ GetCurrentArray()->Append(dictionary); |
+ m_stack.push_back(dictionary); |
+} |
+ |
+void TracedValueBase::EndCurrentArray() { |
+ DCHECK(m_stack.size() > 1); |
+ DCHECK(GetCurrentArray()); |
+ m_stack.pop_back(); |
+} |
+ |
+DictionaryValue* TracedValueBase::GetCurrentDictionary() const { |
+ DCHECK(!m_stack.empty()); |
+ DictionaryValue* dictionary = NULL; |
+ m_stack.back()->GetAsDictionary(&dictionary); |
+ DCHECK(dictionary); |
+ return dictionary; |
+} |
+ |
+ListValue* TracedValueBase::GetCurrentArray() const { |
+ DCHECK(!m_stack.empty()); |
+ ListValue* list = NULL; |
+ m_stack.back()->GetAsList(&list); |
+ DCHECK(list); |
+ return list; |
+} |
+ |
+TracedValue::TracedValue() { |
+ m_stack.push_back(new DictionaryValue()); |
+} |
+ |
+TracedValue::~TracedValue() { |
+ DCHECK(m_stack.empty()); |
+} |
+ |
+TracedDictionary<TracedValue>& TracedValue::BeginDictionary(const char* name) { |
+ BeginDictionaryNamed(name); |
+ return *reinterpret_cast<TracedDictionary<TracedValue>*>(this); |
+} |
+ |
+TracedArray<TracedValue>& TracedValue::BeginArray(const char* name) { |
+ BeginArrayNamed(name); |
+ return *reinterpret_cast<TracedArray<TracedValue>*>(this); |
+} |
+ |
+scoped_refptr<base::debug::ConvertableToTraceFormat> TracedValue::finish() { |
+ DCHECK(m_stack.size() == 1); |
+ scoped_refptr<ConvertibleToTraceFormatValue> result( |
+ new ConvertibleToTraceFormatValue(GetCurrentDictionary())); |
+ m_stack.clear(); |
+ return result; |
+} |
+ |
+} // namespace base |