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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« Source/platform/TracedValue.h ('K') | « Source/platform/TracedValue.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 6
7 #include "platform/TracedValue.h" 7 #include "platform/TracedValue.h"
8 8
9 #include "platform/JSONValues.h" 9 #include "platform/JSONValues.h"
10 10
11 namespace WebCore { 11 namespace WebCore {
12 12
13 TracedValue::TracedValue(PassRefPtr<JSONValue> value) 13 namespace {
14 : m_value(value) 14
15 class ConvertibleToTraceFormatJSONValue : public TraceEvent::ConvertableToTraceF ormat {
16 WTF_MAKE_NONCOPYABLE(ConvertibleToTraceFormatJSONValue);
17 public:
18 explicit ConvertibleToTraceFormatJSONValue(PassRefPtr<JSONValue> value) : m_ value(value) { }
19 virtual String asTraceFormat() const OVERRIDE
20 {
21 return m_value->toJSONString();
22 }
23
24 private:
25 virtual ~ConvertibleToTraceFormatJSONValue() { }
26
27 RefPtr<JSONValue> m_value;
28 };
29
30 String threadSafeCopy(const String& string)
31 {
32 RefPtr<StringImpl> copy(string.impl());
33 if (string.isSafeToSendToAnotherThread())
34 return string;
35 return string.isolatedCopy();
36 }
37
38 #ifdef ASSERT_ENABLED
39 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.
40 {
41 RefPtr<JSONObject> object;
42 return value->asObject(&object);
43 }
44 bool isJSONArray(JSONValue* value)
45 {
46 RefPtr<JSONArray> array;
47 return value->asArray(&array);
48 }
49 #endif
50
51 }
52
53 TracedValueBase::TracedValueBase()
15 { 54 {
16 } 55 }
17 56
18 String TracedValue::asTraceFormat() const 57 TracedValueBase::~TracedValueBase()
19 { 58 {
20 return m_value->toJSONString(); 59 }
60
61 void TracedValueBase::setInteger(const char* name, int value)
62 {
63 currentDictionary()->setNumber(name, value);
64 }
65
66 void TracedValueBase::setDouble(const char* name, double value)
67 {
68 currentDictionary()->setNumber(name, value);
69 }
70
71 void TracedValueBase::setBoolean(const char* name, bool value)
72 {
73 currentDictionary()->setBoolean(name, value);
74 }
75
76 void TracedValueBase::setString(const char* name, const String& value)
77 {
78 currentDictionary()->setString(name, threadSafeCopy(value));
79 }
80
81 void TracedValueBase::beginDictionaryNamed(const char* name)
82 {
83 RefPtr<JSONObject> dictionary = JSONObject::create();
84 currentDictionary()->setObject(name, dictionary);
85 m_stack.append(dictionary);
86 }
87
88 void TracedValueBase::beginArrayNamed(const char* name)
89 {
90 RefPtr<JSONArray> array = JSONArray::create();
91 currentDictionary()->setArray(name, array);
92 m_stack.append(array);
93 }
94
95 void TracedValueBase::endCurrentDictionary()
96 {
97 ASSERT(m_stack.size() > 1);
98 ASSERT(currentDictionary());
99 m_stack.removeLast();
100 }
101
102 void TracedValueBase::pushInteger(int value)
103 {
104 currentArray()->pushInt(value);
105 }
106
107 void TracedValueBase::pushDouble(double value)
108 {
109 currentArray()->pushNumber(value);
110 }
111
112 void TracedValueBase::pushBoolean(bool value)
113 {
114 currentArray()->pushBoolean(value);
115 }
116
117 void TracedValueBase::pushString(const String& value)
118 {
119 currentArray()->pushString(threadSafeCopy(value));
120 }
121
122 void TracedValueBase::pushArray()
123 {
124 RefPtr<JSONArray> array = JSONArray::create();
125 currentArray()->pushArray(array);
126 m_stack.append(array);
127 }
128
129 void TracedValueBase::pushDictionary()
130 {
131 RefPtr<JSONObject> dictionary = JSONObject::create();
132 currentArray()->pushObject(dictionary);
133 m_stack.append(dictionary);
134 }
135
136 void TracedValueBase::endCurrentArray()
137 {
138 ASSERT(m_stack.size() > 1);
139 ASSERT(currentArray());
140 m_stack.removeLast();
141 }
142
143 JSONObject* TracedValueBase::currentDictionary() const
144 {
145 ASSERT(!m_stack.isEmpty());
146 ASSERT(isJSONObject(m_stack.last().get()));
147 return static_cast<JSONObject*>(m_stack.last().get());
148 }
149
150 JSONArray* TracedValueBase::currentArray() const
151 {
152 ASSERT(!m_stack.isEmpty());
153 ASSERT(isJSONArray(m_stack.last().get()));
154 return static_cast<JSONArray*>(m_stack.last().get());
155 }
156
157 TracedValue::TracedValue()
158 {
159 m_stack.append(JSONObject::create());
21 } 160 }
22 161
23 TracedValue::~TracedValue() 162 TracedValue::~TracedValue()
24 { 163 {
164 ASSERT(m_stack.isEmpty());
165 }
166
167 TracedDictionary<TracedValue>& TracedValue::beginDictionary(const char* name)
168 {
169 beginDictionaryNamed(name);
170 return *reinterpret_cast<TracedDictionary<TracedValue>* >(this);
171 }
172
173 TracedArray<TracedValue>& TracedValue::beginArray(const char* name)
174 {
175 beginArrayNamed(name);
176 return *reinterpret_cast<TracedArray<TracedValue>* >(this);
177 }
178
179 PassRefPtr<TraceEvent::ConvertableToTraceFormat> TracedValue::finish()
180 {
181 ASSERT(m_stack.size() == 1);
182 RefPtr<JSONValue> value(currentDictionary());
183 m_stack.clear();
184 return adoptRef(new ConvertibleToTraceFormatJSONValue(value));
25 } 185 }
26 186
27 } 187 }
OLDNEW
« 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