Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2289)

Unified Diff: Source/platform/TracedValue.cpp

Issue 357703002: Introduce builders for tracing event arguments (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« Source/platform/TracedValue.h ('K') | « Source/platform/TracedValue.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/platform/TracedValue.cpp
diff --git a/Source/platform/TracedValue.cpp b/Source/platform/TracedValue.cpp
index b8b3697d0a526c1ad5fc476abdc695cfefcc2f9c..0474cd2a982336703bb3011a7fa0eb513c079204 100644
--- a/Source/platform/TracedValue.cpp
+++ b/Source/platform/TracedValue.cpp
@@ -10,18 +10,178 @@
namespace WebCore {
-TracedValue::TracedValue(PassRefPtr<JSONValue> value)
- : m_value(value)
+namespace {
+
+class ConvertibleToTraceFormatJSONValue : public TraceEvent::ConvertableToTraceFormat {
+ WTF_MAKE_NONCOPYABLE(ConvertibleToTraceFormatJSONValue);
+public:
+ explicit ConvertibleToTraceFormatJSONValue(PassRefPtr<JSONValue> value) : m_value(value) { }
+ virtual String asTraceFormat() const OVERRIDE
+ {
+ return m_value->toJSONString();
+ }
+
+private:
+ virtual ~ConvertibleToTraceFormatJSONValue() { }
+
+ RefPtr<JSONValue> m_value;
+};
+
+String threadSafeCopy(const String& string)
+{
+ RefPtr<StringImpl> copy(string.impl());
+ if (string.isSafeToSendToAnotherThread())
+ return string;
+ return string.isolatedCopy();
+}
+
+#ifdef ASSERT_ENABLED
+bool isJSONObject(JSONValue* value)
caseq 2014/06/25 14:27:25 I think explicitly comparing against value->type()
yurys 2014/06/25 14:57:40 Good point. I forgot that we have type getter.
+{
+ RefPtr<JSONObject> object;
+ return value->asObject(&object);
+}
+bool isJSONArray(JSONValue* value)
+{
+ RefPtr<JSONArray> array;
+ return value->asArray(&array);
+}
+#endif
+
+}
+
+TracedValueBase::TracedValueBase()
+{
+}
+
+TracedValueBase::~TracedValueBase()
+{
+}
+
+void TracedValueBase::setInteger(const char* name, int value)
+{
+ currentDictionary()->setNumber(name, value);
+}
+
+void TracedValueBase::setDouble(const char* name, double value)
+{
+ currentDictionary()->setNumber(name, value);
+}
+
+void TracedValueBase::setBoolean(const char* name, bool value)
+{
+ currentDictionary()->setBoolean(name, value);
+}
+
+void TracedValueBase::setString(const char* name, const String& value)
+{
+ currentDictionary()->setString(name, threadSafeCopy(value));
+}
+
+void TracedValueBase::beginDictionaryNamed(const char* name)
+{
+ RefPtr<JSONObject> dictionary = JSONObject::create();
+ currentDictionary()->setObject(name, dictionary);
+ m_stack.append(dictionary);
+}
+
+void TracedValueBase::beginArrayNamed(const char* name)
+{
+ RefPtr<JSONArray> array = JSONArray::create();
+ currentDictionary()->setArray(name, array);
+ m_stack.append(array);
+}
+
+void TracedValueBase::endCurrentDictionary()
+{
+ ASSERT(m_stack.size() > 1);
+ ASSERT(currentDictionary());
+ m_stack.removeLast();
+}
+
+void TracedValueBase::pushInteger(int value)
+{
+ currentArray()->pushInt(value);
+}
+
+void TracedValueBase::pushDouble(double value)
+{
+ currentArray()->pushNumber(value);
+}
+
+void TracedValueBase::pushBoolean(bool value)
+{
+ currentArray()->pushBoolean(value);
+}
+
+void TracedValueBase::pushString(const String& value)
+{
+ currentArray()->pushString(threadSafeCopy(value));
+}
+
+void TracedValueBase::pushArray()
+{
+ RefPtr<JSONArray> array = JSONArray::create();
+ currentArray()->pushArray(array);
+ m_stack.append(array);
+}
+
+void TracedValueBase::pushDictionary()
{
+ RefPtr<JSONObject> dictionary = JSONObject::create();
+ currentArray()->pushObject(dictionary);
+ m_stack.append(dictionary);
}
-String TracedValue::asTraceFormat() const
+void TracedValueBase::endCurrentArray()
{
- return m_value->toJSONString();
+ ASSERT(m_stack.size() > 1);
+ ASSERT(currentArray());
+ m_stack.removeLast();
+}
+
+JSONObject* TracedValueBase::currentDictionary() const
+{
+ ASSERT(!m_stack.isEmpty());
+ ASSERT(isJSONObject(m_stack.last().get()));
+ return static_cast<JSONObject*>(m_stack.last().get());
+}
+
+JSONArray* TracedValueBase::currentArray() const
+{
+ ASSERT(!m_stack.isEmpty());
+ ASSERT(isJSONArray(m_stack.last().get()));
+ return static_cast<JSONArray*>(m_stack.last().get());
+}
+
+TracedValue::TracedValue()
+{
+ m_stack.append(JSONObject::create());
}
TracedValue::~TracedValue()
{
+ ASSERT(m_stack.isEmpty());
+}
+
+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);
+}
+
+PassRefPtr<TraceEvent::ConvertableToTraceFormat> TracedValue::finish()
+{
+ ASSERT(m_stack.size() == 1);
+ RefPtr<JSONValue> value(currentDictionary());
+ m_stack.clear();
+ return adoptRef(new ConvertibleToTraceFormatJSONValue(value));
}
}
« Source/platform/TracedValue.h ('K') | « Source/platform/TracedValue.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698