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
10namespace base {
11namespace debug {
12
13TracedValue::TracedValue() : root_(new DictionaryValue()) {
14  stack_.push_back(root_.get());
15}
16
17TracedValue::~TracedValue() {
18  DCHECK_EQ(1u, stack_.size());
19}
20
21void TracedValue::SetInteger(const char* name, int value) {
22  GetCurrentDictionary()->SetInteger(name, value);
23}
24
25void TracedValue::SetDouble(const char* name, double value) {
26  GetCurrentDictionary()->SetDouble(name, value);
27}
28
29void TracedValue::SetBoolean(const char* name, bool value) {
30  GetCurrentDictionary()->SetBoolean(name, value);
31}
32
33void TracedValue::SetString(const char* name, const std::string& value) {
34  GetCurrentDictionary()->SetString(name, value);
35}
36
37void TracedValue::SetValue(const char* name, Value* value) {
38  GetCurrentDictionary()->Set(name, value);
39}
40
41void TracedValue::BeginDictionary(const char* name) {
42  DictionaryValue* dictionary = new DictionaryValue();
43  GetCurrentDictionary()->Set(name, dictionary);
44  stack_.push_back(dictionary);
45}
46
47void TracedValue::BeginArray(const char* name) {
48  ListValue* array = new ListValue();
49  GetCurrentDictionary()->Set(name, array);
50  stack_.push_back(array);
51}
52
53void TracedValue::EndDictionary() {
54  DCHECK_GT(stack_.size(), 1u);
55  DCHECK(GetCurrentDictionary());
56  stack_.pop_back();
57}
58
59void TracedValue::AppendInteger(int value) {
60  GetCurrentArray()->AppendInteger(value);
61}
62
63void TracedValue::AppendDouble(double value) {
64  GetCurrentArray()->AppendDouble(value);
65}
66
67void TracedValue::AppendBoolean(bool value) {
68  GetCurrentArray()->AppendBoolean(value);
69}
70
71void TracedValue::AppendString(const std::string& value) {
72  GetCurrentArray()->AppendString(value);
73}
74
75void TracedValue::BeginArray() {
76  ListValue* array = new ListValue();
77  GetCurrentArray()->Append(array);
78  stack_.push_back(array);
79}
80
81void TracedValue::BeginDictionary() {
82  DictionaryValue* dictionary = new DictionaryValue();
83  GetCurrentArray()->Append(dictionary);
84  stack_.push_back(dictionary);
85}
86
87void TracedValue::EndArray() {
88  DCHECK_GT(stack_.size(), 1u);
89  DCHECK(GetCurrentArray());
90  stack_.pop_back();
91}
92
93DictionaryValue* TracedValue::GetCurrentDictionary() {
94  DCHECK(!stack_.empty());
95  DictionaryValue* dictionary = NULL;
96  stack_.back()->GetAsDictionary(&dictionary);
97  DCHECK(dictionary);
98  return dictionary;
99}
100
101ListValue* TracedValue::GetCurrentArray() {
102  DCHECK(!stack_.empty());
103  ListValue* list = NULL;
104  stack_.back()->GetAsList(&list);
105  DCHECK(list);
106  return list;
107}
108
109void TracedValue::AppendAsTraceFormat(std::string* out) const {
110  std::string tmp;
111  JSONWriter::Write(stack_.front(), &tmp);
112  *out += tmp;
113  DCHECK_EQ(1u, stack_.size()) << tmp;
114}
115
116}  // namespace debug
117}  // namespace base
118