15f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Copyright (c) 2014 The Chromium Authors. All rights reserved.
25f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// found in the LICENSE file.
45f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/debug/trace_event_argument.h"
65f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
75f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/json/json_writer.h"
85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/values.h"
95f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)namespace base {
115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)namespace debug {
125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)TracedValue::TracedValue() : root_(new DictionaryValue()) {
145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  stack_.push_back(root_.get());
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)TracedValue::~TracedValue() {
185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK_EQ(1u, stack_.size());
195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void TracedValue::SetInteger(const char* name, int value) {
225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  GetCurrentDictionary()->SetInteger(name, value);
235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void TracedValue::SetDouble(const char* name, double value) {
265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  GetCurrentDictionary()->SetDouble(name, value);
275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void TracedValue::SetBoolean(const char* name, bool value) {
305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  GetCurrentDictionary()->SetBoolean(name, value);
315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void TracedValue::SetString(const char* name, const std::string& value) {
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  GetCurrentDictionary()->SetString(name, value);
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void TracedValue::SetValue(const char* name, Value* value) {
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  GetCurrentDictionary()->Set(name, value);
395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void TracedValue::BeginDictionary(const char* name) {
425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DictionaryValue* dictionary = new DictionaryValue();
435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  GetCurrentDictionary()->Set(name, dictionary);
445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  stack_.push_back(dictionary);
455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void TracedValue::BeginArray(const char* name) {
485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ListValue* array = new ListValue();
495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  GetCurrentDictionary()->Set(name, array);
505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  stack_.push_back(array);
515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void TracedValue::EndDictionary() {
545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK_GT(stack_.size(), 1u);
555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(GetCurrentDictionary());
565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  stack_.pop_back();
575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void TracedValue::AppendInteger(int value) {
605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  GetCurrentArray()->AppendInteger(value);
615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void TracedValue::AppendDouble(double value) {
645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  GetCurrentArray()->AppendDouble(value);
655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void TracedValue::AppendBoolean(bool value) {
685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  GetCurrentArray()->AppendBoolean(value);
695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void TracedValue::AppendString(const std::string& value) {
725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  GetCurrentArray()->AppendString(value);
735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void TracedValue::BeginArray() {
765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ListValue* array = new ListValue();
775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  GetCurrentArray()->Append(array);
785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  stack_.push_back(array);
795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void TracedValue::BeginDictionary() {
825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DictionaryValue* dictionary = new DictionaryValue();
835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  GetCurrentArray()->Append(dictionary);
845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  stack_.push_back(dictionary);
855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void TracedValue::EndArray() {
885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK_GT(stack_.size(), 1u);
895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(GetCurrentArray());
905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  stack_.pop_back();
915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)DictionaryValue* TracedValue::GetCurrentDictionary() {
945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(!stack_.empty());
955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DictionaryValue* dictionary = NULL;
965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  stack_.back()->GetAsDictionary(&dictionary);
975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(dictionary);
985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return dictionary;
995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)ListValue* TracedValue::GetCurrentArray() {
1025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(!stack_.empty());
1035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ListValue* list = NULL;
1045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  stack_.back()->GetAsList(&list);
1055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(list);
1065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return list;
1075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void TracedValue::AppendAsTraceFormat(std::string* out) const {
1105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  std::string tmp;
1115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  JSONWriter::Write(stack_.front(), &tmp);
1125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  *out += tmp;
1135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK_EQ(1u, stack_.size()) << tmp;
1145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}  // namespace debug
1175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}  // namespace base
118