OLD | NEW |
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 } |
| 39 |
| 40 TracedValueBase::TracedValueBase() |
15 { | 41 { |
16 } | 42 } |
17 | 43 |
18 String TracedValue::asTraceFormat() const | 44 TracedValueBase::~TracedValueBase() |
19 { | 45 { |
20 return m_value->toJSONString(); | 46 } |
| 47 |
| 48 void TracedValueBase::setInteger(const char* name, int value) |
| 49 { |
| 50 currentDictionary()->setNumber(name, value); |
| 51 } |
| 52 |
| 53 void TracedValueBase::setDouble(const char* name, double value) |
| 54 { |
| 55 currentDictionary()->setNumber(name, value); |
| 56 } |
| 57 |
| 58 void TracedValueBase::setBoolean(const char* name, bool value) |
| 59 { |
| 60 currentDictionary()->setBoolean(name, value); |
| 61 } |
| 62 |
| 63 void TracedValueBase::setString(const char* name, const String& value) |
| 64 { |
| 65 currentDictionary()->setString(name, threadSafeCopy(value)); |
| 66 } |
| 67 |
| 68 void TracedValueBase::beginDictionaryNamed(const char* name) |
| 69 { |
| 70 RefPtr<JSONObject> dictionary = JSONObject::create(); |
| 71 currentDictionary()->setObject(name, dictionary); |
| 72 m_stack.append(dictionary); |
| 73 } |
| 74 |
| 75 void TracedValueBase::beginArrayNamed(const char* name) |
| 76 { |
| 77 RefPtr<JSONArray> array = JSONArray::create(); |
| 78 currentDictionary()->setArray(name, array); |
| 79 m_stack.append(array); |
| 80 } |
| 81 |
| 82 void TracedValueBase::endCurrentDictionary() |
| 83 { |
| 84 ASSERT(m_stack.size() > 1); |
| 85 ASSERT(currentDictionary()); |
| 86 m_stack.removeLast(); |
| 87 } |
| 88 |
| 89 void TracedValueBase::pushInteger(int value) |
| 90 { |
| 91 currentArray()->pushInt(value); |
| 92 } |
| 93 |
| 94 void TracedValueBase::pushDouble(double value) |
| 95 { |
| 96 currentArray()->pushNumber(value); |
| 97 } |
| 98 |
| 99 void TracedValueBase::pushBoolean(bool value) |
| 100 { |
| 101 currentArray()->pushBoolean(value); |
| 102 } |
| 103 |
| 104 void TracedValueBase::pushString(const String& value) |
| 105 { |
| 106 currentArray()->pushString(threadSafeCopy(value)); |
| 107 } |
| 108 |
| 109 void TracedValueBase::pushArray() |
| 110 { |
| 111 RefPtr<JSONArray> array = JSONArray::create(); |
| 112 currentArray()->pushArray(array); |
| 113 m_stack.append(array); |
| 114 } |
| 115 |
| 116 void TracedValueBase::pushDictionary() |
| 117 { |
| 118 RefPtr<JSONObject> dictionary = JSONObject::create(); |
| 119 currentArray()->pushObject(dictionary); |
| 120 m_stack.append(dictionary); |
| 121 } |
| 122 |
| 123 void TracedValueBase::endCurrentArray() |
| 124 { |
| 125 ASSERT(m_stack.size() > 1); |
| 126 ASSERT(currentArray()); |
| 127 m_stack.removeLast(); |
| 128 } |
| 129 |
| 130 JSONObject* TracedValueBase::currentDictionary() const |
| 131 { |
| 132 ASSERT(!m_stack.isEmpty()); |
| 133 ASSERT(m_stack.last()->type() == JSONValue::TypeObject); |
| 134 return static_cast<JSONObject*>(m_stack.last().get()); |
| 135 } |
| 136 |
| 137 JSONArray* TracedValueBase::currentArray() const |
| 138 { |
| 139 ASSERT(!m_stack.isEmpty()); |
| 140 ASSERT(m_stack.last()->type() == JSONValue::TypeArray); |
| 141 return static_cast<JSONArray*>(m_stack.last().get()); |
| 142 } |
| 143 |
| 144 TracedValue::TracedValue() |
| 145 { |
| 146 m_stack.append(JSONObject::create()); |
21 } | 147 } |
22 | 148 |
23 TracedValue::~TracedValue() | 149 TracedValue::~TracedValue() |
24 { | 150 { |
| 151 ASSERT(m_stack.isEmpty()); |
| 152 } |
| 153 |
| 154 TracedDictionary<TracedValue>& TracedValue::beginDictionary(const char* name) |
| 155 { |
| 156 beginDictionaryNamed(name); |
| 157 return *reinterpret_cast<TracedDictionary<TracedValue>* >(this); |
| 158 } |
| 159 |
| 160 TracedArray<TracedValue>& TracedValue::beginArray(const char* name) |
| 161 { |
| 162 beginArrayNamed(name); |
| 163 return *reinterpret_cast<TracedArray<TracedValue>* >(this); |
| 164 } |
| 165 |
| 166 PassRefPtr<TraceEvent::ConvertableToTraceFormat> TracedValue::finish() |
| 167 { |
| 168 ASSERT(m_stack.size() == 1); |
| 169 RefPtr<JSONValue> value(currentDictionary()); |
| 170 m_stack.clear(); |
| 171 return adoptRef(new ConvertibleToTraceFormatJSONValue(value)); |
25 } | 172 } |
26 | 173 |
27 } | 174 } |
OLD | NEW |