blob: 81d8c0172a49ed08e0cb378cb5b43bc77f57b08c [file] [log] [blame]
Scott Graham66962112018-06-08 12:42:08 -07001// Copyright 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#ifndef BASE_TRACE_EVENT_TRACE_EVENT_ARGUMENT_H_
6#define BASE_TRACE_EVENT_TRACE_EVENT_ARGUMENT_H_
7
8#include <stddef.h>
9
10#include <memory>
11#include <string>
12#include <vector>
13
14#include "base/macros.h"
15#include "base/pickle.h"
16#include "base/strings/string_piece.h"
17#include "base/trace_event/trace_event_impl.h"
18
19namespace base {
20
21class Value;
22
23namespace trace_event {
24
25class BASE_EXPORT TracedValue : public ConvertableToTraceFormat {
26 public:
27 TracedValue();
28 explicit TracedValue(size_t capacity);
29 ~TracedValue() override;
30
31 void EndDictionary();
32 void EndArray();
33
34 // These methods assume that |name| is a long lived "quoted" string.
35 void SetInteger(const char* name, int value);
36 void SetDouble(const char* name, double value);
37 void SetBoolean(const char* name, bool value);
38 void SetString(const char* name, base::StringPiece value);
39 void SetValue(const char* name, const TracedValue& value);
40 void BeginDictionary(const char* name);
41 void BeginArray(const char* name);
42
43 // These, instead, can be safely passed a temporary string.
44 void SetIntegerWithCopiedName(base::StringPiece name, int value);
45 void SetDoubleWithCopiedName(base::StringPiece name, double value);
46 void SetBooleanWithCopiedName(base::StringPiece name, bool value);
47 void SetStringWithCopiedName(base::StringPiece name,
48 base::StringPiece value);
49 void SetValueWithCopiedName(base::StringPiece name,
50 const TracedValue& value);
51 void BeginDictionaryWithCopiedName(base::StringPiece name);
52 void BeginArrayWithCopiedName(base::StringPiece name);
53
54 void AppendInteger(int);
55 void AppendDouble(double);
56 void AppendBoolean(bool);
57 void AppendString(base::StringPiece);
58 void BeginArray();
59 void BeginDictionary();
60
61 // ConvertableToTraceFormat implementation.
62 void AppendAsTraceFormat(std::string* out) const override;
63
64 void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead) override;
65
66 // DEPRECATED: do not use, here only for legacy reasons. These methods causes
67 // a copy-and-translation of the base::Value into the equivalent TracedValue.
68 // TODO(primiano): migrate the (three) existing clients to the cheaper
69 // SetValue(TracedValue) API. crbug.com/495628.
70 void SetValue(const char* name, std::unique_ptr<base::Value> value);
71 void SetBaseValueWithCopiedName(base::StringPiece name,
72 const base::Value& value);
73 void AppendBaseValue(const base::Value& value);
74
75 // Public for tests only.
76 std::unique_ptr<base::Value> ToBaseValue() const;
77
78 private:
79 Pickle pickle_;
80
81#ifndef NDEBUG
82 // In debug builds checks the pairings of {Start,End}{Dictionary,Array}
83 std::vector<bool> nesting_stack_;
84#endif
85
86 DISALLOW_COPY_AND_ASSIGN(TracedValue);
87};
88
89} // namespace trace_event
90} // namespace base
91
92#endif // BASE_TRACE_EVENT_TRACE_EVENT_ARGUMENT_H_