Chromium Code Reviews| 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 } | |
|
dsinclair
2014/07/10 13:32:14
Nit: blank line before }
nit: } // namespace
yurys
2014/07/10 15:06:29
Done.
| |
| 31 | |
| 32 TracedValueBase::TracedValueBase() { | |
| 33 } | |
| 34 | |
| 35 TracedValueBase::~TracedValueBase() { | |
| 36 } | |
| 37 | |
| 38 void TracedValueBase::SetInteger(const char* name, int value) { | |
| 39 GetCurrentDictionary()->SetInteger(name, value); | |
| 40 } | |
| 41 | |
| 42 void TracedValueBase::SetDouble(const char* name, double value) { | |
| 43 GetCurrentDictionary()->SetDouble(name, value); | |
| 44 } | |
| 45 | |
| 46 void TracedValueBase::SetBoolean(const char* name, bool value) { | |
| 47 GetCurrentDictionary()->SetBoolean(name, value); | |
| 48 } | |
| 49 | |
| 50 void TracedValueBase::SetString(const char* name, const std::string& value) { | |
| 51 GetCurrentDictionary()->SetString(name, value); | |
| 52 } | |
| 53 | |
| 54 void TracedValueBase::BeginDictionaryNamed(const char* name) { | |
| 55 DictionaryValue* dictionary = new DictionaryValue(); | |
| 56 GetCurrentDictionary()->Set(name, dictionary); | |
| 57 m_stack.push_back(dictionary); | |
| 58 } | |
| 59 | |
| 60 void TracedValueBase::BeginArrayNamed(const char* name) { | |
| 61 ListValue* array = new ListValue(); | |
| 62 GetCurrentDictionary()->Set(name, array); | |
| 63 m_stack.push_back(array); | |
| 64 } | |
| 65 | |
| 66 void TracedValueBase::EndCurrentDictionary() { | |
| 67 DCHECK(m_stack.size() > 1); | |
| 68 DCHECK(GetCurrentDictionary()); | |
| 69 m_stack.pop_back(); | |
| 70 } | |
| 71 | |
| 72 void TracedValueBase::PushInteger(int value) { | |
| 73 GetCurrentArray()->AppendInteger(value); | |
| 74 } | |
| 75 | |
| 76 void TracedValueBase::PushDouble(double value) { | |
| 77 GetCurrentArray()->AppendDouble(value); | |
| 78 } | |
| 79 | |
| 80 void TracedValueBase::PushBoolean(bool value) { | |
| 81 GetCurrentArray()->AppendBoolean(value); | |
| 82 } | |
| 83 | |
| 84 void TracedValueBase::PushString(const std::string& value) { | |
| 85 GetCurrentArray()->AppendString(value); | |
| 86 } | |
| 87 | |
| 88 void TracedValueBase::PushArray() { | |
| 89 ListValue* array = new ListValue(); | |
| 90 GetCurrentArray()->Append(array); | |
| 91 m_stack.push_back(array); | |
| 92 } | |
| 93 | |
| 94 void TracedValueBase::PushDictionary() { | |
| 95 DictionaryValue* dictionary = new DictionaryValue(); | |
| 96 GetCurrentArray()->Append(dictionary); | |
| 97 m_stack.push_back(dictionary); | |
| 98 } | |
| 99 | |
| 100 void TracedValueBase::EndCurrentArray() { | |
| 101 DCHECK(m_stack.size() > 1); | |
| 102 DCHECK(GetCurrentArray()); | |
| 103 m_stack.pop_back(); | |
| 104 } | |
| 105 | |
| 106 DictionaryValue* TracedValueBase::GetCurrentDictionary() const { | |
| 107 DCHECK(!m_stack.empty()); | |
| 108 DictionaryValue* dictionary = NULL; | |
| 109 m_stack.back()->GetAsDictionary(&dictionary); | |
| 110 DCHECK(dictionary); | |
| 111 return dictionary; | |
| 112 } | |
| 113 | |
| 114 ListValue* TracedValueBase::GetCurrentArray() const { | |
| 115 DCHECK(!m_stack.empty()); | |
| 116 ListValue* list = NULL; | |
| 117 m_stack.back()->GetAsList(&list); | |
| 118 DCHECK(list); | |
| 119 return list; | |
| 120 } | |
| 121 | |
| 122 TracedValue::TracedValue() { | |
| 123 m_stack.push_back(new DictionaryValue()); | |
| 124 } | |
| 125 | |
| 126 TracedValue::~TracedValue() { | |
| 127 DCHECK(m_stack.empty()); | |
| 128 } | |
| 129 | |
| 130 TracedDictionary<TracedValue>& TracedValue::BeginDictionary(const char* name) { | |
| 131 BeginDictionaryNamed(name); | |
| 132 return *reinterpret_cast<TracedDictionary<TracedValue>*>(this); | |
| 133 } | |
| 134 | |
| 135 TracedArray<TracedValue>& TracedValue::BeginArray(const char* name) { | |
| 136 BeginArrayNamed(name); | |
| 137 return *reinterpret_cast<TracedArray<TracedValue>*>(this); | |
| 138 } | |
| 139 | |
| 140 scoped_refptr<base::debug::ConvertableToTraceFormat> TracedValue::finish() { | |
| 141 DCHECK(m_stack.size() == 1); | |
| 142 scoped_refptr<ConvertibleToTraceFormatValue> result( | |
| 143 new ConvertibleToTraceFormatValue(GetCurrentDictionary())); | |
| 144 m_stack.clear(); | |
| 145 return result; | |
| 146 } | |
| 147 | |
| 148 } // namespace base | |
| OLD | NEW |