OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/debug/trace_event_argument.h" |
| 6 |
| 7 #include "base/json/json_writer.h" |
| 8 #include "base/values.h" |
| 9 |
| 10 namespace base { |
| 11 |
| 12 namespace { |
| 13 |
| 14 class ConvertibleToTraceFormatValue |
| 15 : public base::debug::ConvertableToTraceFormat { |
| 16 public: |
| 17 explicit ConvertibleToTraceFormatValue(Value* value) : m_value(value) {} |
| 18 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE { |
| 19 std::string tmp; |
| 20 base::JSONWriter::Write(m_value.get(), &tmp); |
| 21 *out += tmp; |
| 22 } |
| 23 |
| 24 private: |
| 25 virtual ~ConvertibleToTraceFormatValue() {} |
| 26 |
| 27 scoped_ptr<Value> m_value; |
| 28 DISALLOW_COPY_AND_ASSIGN(ConvertibleToTraceFormatValue); |
| 29 }; |
| 30 |
| 31 } |
| 32 |
| 33 TracedValueBase::TracedValueBase() { |
| 34 } |
| 35 |
| 36 TracedValueBase::~TracedValueBase() { |
| 37 } |
| 38 |
| 39 void TracedValueBase::SetInteger(const char* name, int value) { |
| 40 GetCurrentDictionary()->SetInteger(name, value); |
| 41 } |
| 42 |
| 43 void TracedValueBase::SetDouble(const char* name, double value) { |
| 44 GetCurrentDictionary()->SetDouble(name, value); |
| 45 } |
| 46 |
| 47 void TracedValueBase::SetBoolean(const char* name, bool value) { |
| 48 GetCurrentDictionary()->SetBoolean(name, value); |
| 49 } |
| 50 |
| 51 void TracedValueBase::SetString(const char* name, const std::string& value) { |
| 52 GetCurrentDictionary()->SetString(name, value); |
| 53 } |
| 54 |
| 55 void TracedValueBase::BeginDictionaryNamed(const char* name) { |
| 56 DictionaryValue* dictionary = new DictionaryValue(); |
| 57 GetCurrentDictionary()->Set(name, dictionary); |
| 58 m_stack.push_back(dictionary); |
| 59 } |
| 60 |
| 61 void TracedValueBase::BeginArrayNamed(const char* name) { |
| 62 ListValue* array = new ListValue(); |
| 63 GetCurrentDictionary()->Set(name, array); |
| 64 m_stack.push_back(array); |
| 65 } |
| 66 |
| 67 void TracedValueBase::EndCurrentDictionary() { |
| 68 DCHECK(m_stack.size() > 1); |
| 69 DCHECK(GetCurrentDictionary()); |
| 70 m_stack.pop_back(); |
| 71 } |
| 72 |
| 73 void TracedValueBase::PushInteger(int value) { |
| 74 GetCurrentArray()->AppendInteger(value); |
| 75 } |
| 76 |
| 77 void TracedValueBase::PushDouble(double value) { |
| 78 GetCurrentArray()->AppendDouble(value); |
| 79 } |
| 80 |
| 81 void TracedValueBase::PushBoolean(bool value) { |
| 82 GetCurrentArray()->AppendBoolean(value); |
| 83 } |
| 84 |
| 85 void TracedValueBase::PushString(const std::string& value) { |
| 86 GetCurrentArray()->AppendString(value); |
| 87 } |
| 88 |
| 89 void TracedValueBase::PushArray() { |
| 90 ListValue* array = new ListValue(); |
| 91 GetCurrentArray()->Append(array); |
| 92 m_stack.push_back(array); |
| 93 } |
| 94 |
| 95 void TracedValueBase::PushDictionary() { |
| 96 DictionaryValue* dictionary = new DictionaryValue(); |
| 97 GetCurrentArray()->Append(dictionary); |
| 98 m_stack.push_back(dictionary); |
| 99 } |
| 100 |
| 101 void TracedValueBase::EndCurrentArray() { |
| 102 DCHECK(m_stack.size() > 1); |
| 103 DCHECK(GetCurrentArray()); |
| 104 m_stack.pop_back(); |
| 105 } |
| 106 |
| 107 DictionaryValue* TracedValueBase::GetCurrentDictionary() const { |
| 108 DCHECK(!m_stack.empty()); |
| 109 DictionaryValue* dictionary = NULL; |
| 110 m_stack.back()->GetAsDictionary(&dictionary); |
| 111 DCHECK(dictionary); |
| 112 return dictionary; |
| 113 } |
| 114 |
| 115 ListValue* TracedValueBase::GetCurrentArray() const { |
| 116 DCHECK(!m_stack.empty()); |
| 117 ListValue* list = NULL; |
| 118 m_stack.back()->GetAsList(&list); |
| 119 DCHECK(list); |
| 120 return list; |
| 121 } |
| 122 |
| 123 TracedValue::TracedValue() { |
| 124 m_stack.push_back(new DictionaryValue()); |
| 125 } |
| 126 |
| 127 TracedValue::~TracedValue() { |
| 128 DCHECK(m_stack.empty()); |
| 129 } |
| 130 |
| 131 TracedDictionary<TracedValue>& TracedValue::BeginDictionary(const char* name) { |
| 132 BeginDictionaryNamed(name); |
| 133 return *reinterpret_cast<TracedDictionary<TracedValue>*>(this); |
| 134 } |
| 135 |
| 136 TracedArray<TracedValue>& TracedValue::BeginArray(const char* name) { |
| 137 BeginArrayNamed(name); |
| 138 return *reinterpret_cast<TracedArray<TracedValue>*>(this); |
| 139 } |
| 140 |
| 141 scoped_refptr<base::debug::ConvertableToTraceFormat> TracedValue::finish() { |
| 142 DCHECK(m_stack.size() == 1); |
| 143 scoped_refptr<ConvertibleToTraceFormatValue> result( |
| 144 new ConvertibleToTraceFormatValue(GetCurrentDictionary())); |
| 145 m_stack.clear(); |
| 146 return result; |
| 147 } |
| 148 |
| 149 } // namespace base |
OLD | NEW |