blob: b5d084028e96024366c5a3d530882c7e57c9a19d [file] [log] [blame]
[email protected]13502562012-05-09 21:54:271// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]9e4cda7332010-07-31 04:56:145// This file specifies a recursive data storage class called Value intended for
[email protected]2f03f532013-07-17 08:43:336// storing settings and other persistable data.
initial.commitd7cae122008-07-26 21:49:387//
[email protected]2f03f532013-07-17 08:43:338// A Value represents something that can be stored in JSON or passed to/from
9// JavaScript. As such, it is NOT a generalized variant type, since only the
10// types supported by JavaScript/JSON are supported.
initial.commitd7cae122008-07-26 21:49:3811//
avi9b6f42932015-12-26 22:15:1412// IN PARTICULAR this means that there is no support for int64_t or unsigned
[email protected]2f03f532013-07-17 08:43:3313// numbers. Writing JSON with such types would violate the spec. If you need
14// something like this, either use a double or make a string value containing
15// the number you want.
Daniel Cheng9d949902017-09-26 00:52:4316//
17// NOTE: A Value parameter that is always a Value::STRING should just be passed
18// as a std::string. Similarly for Values that are always Value::DICTIONARY
19// (should be flat_map), Value::LIST (should be std::vector), et cetera.
initial.commitd7cae122008-07-26 21:49:3820
[email protected]101d5422008-09-26 20:22:4221#ifndef BASE_VALUES_H_
22#define BASE_VALUES_H_
initial.commitd7cae122008-07-26 21:49:3823
[email protected]c014f2b32013-09-03 23:29:1224#include <stddef.h>
avi9b6f42932015-12-26 22:15:1425#include <stdint.h>
[email protected]c014f2b32013-09-03 23:29:1226
27#include <iosfwd>
initial.commitd7cae122008-07-26 21:49:3828#include <map>
dcheng093de9b2016-04-04 21:25:5129#include <memory>
[email protected]8e50b602009-03-03 22:59:4330#include <string>
[email protected]c014f2b32013-09-03 23:29:1231#include <utility>
initial.commitd7cae122008-07-26 21:49:3832#include <vector>
33
[email protected]0bea7252011-08-05 15:34:0034#include "base/base_export.h"
Jan Wilken Dörrie7e7a9792019-10-15 14:42:0535#include "base/containers/checked_iterators.h"
Jan Wilken Dörrie8d9034f12019-11-28 14:48:5736#include "base/containers/checked_range.h"
mkwstcd8067b2017-04-11 06:52:2137#include "base/containers/flat_map.h"
jdoerriecd022242017-08-23 08:38:2738#include "base/containers/span.h"
asvitkinedbd26533e2015-06-23 18:22:5239#include "base/strings/string_piece.h"
Alexander Timine68aeb32021-04-11 23:06:2140#include "base/trace_event/base_tracing_forward.h"
jdoerrie44efa9d2017-07-14 14:47:2041#include "base/value_iterators.h"
Jan Wilken Dörrie79d022142020-08-19 18:18:3242#include "third_party/abseil-cpp/absl/types/variant.h"
initial.commitd7cae122008-07-26 21:49:3843
[email protected]f3a1c642011-07-12 19:15:0344namespace base {
45
initial.commitd7cae122008-07-26 21:49:3846class DictionaryValue;
47class ListValue;
[email protected]68d9d352011-02-21 16:35:0448class Value;
initial.commitd7cae122008-07-26 21:49:3849
[email protected]b59ea312011-08-05 18:20:0550// The Value class is the base class for Values. A Value can be instantiated
jdoerrie43ab3c02017-08-24 20:44:3651// via passing the appropriate type or backing storage to the constructor.
[email protected]2f03f532013-07-17 08:43:3352//
53// See the file-level comment above for more information.
Brett Wilson4bef8ee2017-09-01 20:11:4954//
55// base::Value is currently in the process of being refactored. Design doc:
56// https://docs.google.com/document/d/1uDLu5uTRlCWePxQUEHc8yNQdEoE1BDISYdpggWEABnw
57//
58// Previously (which is how most code that currently exists is written), Value
59// used derived types to implement the individual data types, and base::Value
60// was just a base class to refer to them. This required everything be heap
61// allocated.
62//
63// OLD WAY:
64//
65// std::unique_ptr<base::Value> GetFoo() {
66// std::unique_ptr<DictionaryValue> dict;
67// dict->SetString("mykey", foo);
68// return dict;
69// }
70//
71// The new design makes base::Value a variant type that holds everything in
72// a union. It is now recommended to pass by value with std::move rather than
73// use heap allocated values. The DictionaryValue and ListValue subclasses
74// exist only as a compatibility shim that we're in the process of removing.
75//
76// NEW WAY:
77//
78// base::Value GetFoo() {
79// base::Value dict(base::Value::Type::DICTIONARY);
80// dict.SetKey("mykey", base::Value(foo));
81// return dict;
82// }
Jan Wilken Dörriecf4ce5522020-10-27 14:41:0483//
84// The new design tries to avoid losing type information. Thus when migrating
85// off deprecated types, existing usages of base::ListValue should be replaced
Jan Wilken Dörrie2a06d6e92020-11-09 09:32:4986// by std::vector<base::Value>, and existing usages of base::DictionaryValue
87// should be replaced with base::flat_map<std::string, base::Value>.
Jan Wilken Dörriecf4ce5522020-10-27 14:41:0488//
Jan Wilken Dörrie2a06d6e92020-11-09 09:32:4989// OLD WAY:
90//
91// void AlwaysTakesList(std::unique_ptr<base::ListValue> list);
92// void AlwaysTakesDict(std::unique_ptr<base::DictionaryValue> dict);
93//
94// NEW WAY:
95//
96// void AlwaysTakesList(std::vector<base::Value> list);
97// void AlwaysTakesDict(base::flat_map<std::string, base::Value> dict);
98//
99// Migrating code will require conversions on API boundaries. This can be done
100// cheaply by making use of overloaded base::Value constructors and the
Gabriel Charetteb49d73a2021-05-05 20:05:59101// `Value::TakeList()` and `Value::TakeDict()` APIs.
[email protected]0bea7252011-08-05 15:34:00102class BASE_EXPORT Value {
initial.commitd7cae122008-07-26 21:49:38103 public:
jdoerrie9970f20e2018-07-20 21:41:18104 using BlobStorage = std::vector<uint8_t>;
jdoerriea5676c62017-04-11 18:09:14105 using ListStorage = std::vector<Value>;
Jan Wilken Dörrief961a372020-11-02 20:30:34106 using DictStorage = flat_map<std::string, Value>;
107
108 // Like `DictStorage`, but with std::unique_ptr in the mapped type. This is
109 // due to legacy reasons, and should be removed once no caller relies on
110 // stability of pointers anymore.
111 using LegacyDictStorage = flat_map<std::string, std::unique_ptr<Value>>;
Jan Wilken Dörrie8d9034f12019-11-28 14:48:57112
113 using ListView = CheckedContiguousRange<ListStorage>;
114 using ConstListView = CheckedContiguousConstRange<ListStorage>;
115
Jose Dapena Paz7685422a2019-04-03 18:35:04116 enum class Type : unsigned char {
jdoerriedc72ee942016-12-07 15:43:28117 NONE = 0,
118 BOOLEAN,
119 INTEGER,
120 DOUBLE,
121 STRING,
122 BINARY,
123 DICTIONARY,
jdoerriee1b1f3a2019-03-16 04:08:01124 LIST,
[email protected]2f03f532013-07-17 08:43:33125 // Note: Do not add more types. See the file-level comment above for why.
[email protected]a502bbe72011-01-07 18:06:45126 };
127
Lei Zhang30895d52017-10-23 19:14:46128 // Adaptors for converting from the old way to the new way and vice versa.
129 static Value FromUniquePtrValue(std::unique_ptr<Value> val);
130 static std::unique_ptr<Value> ToUniquePtrValue(Value val);
Lei Zhang8c1432b2019-10-08 18:48:54131 static const DictionaryValue& AsDictionaryValue(const Value& val);
132 static const ListValue& AsListValue(const Value& val);
Lei Zhang30895d52017-10-23 19:14:46133
Jan Wilken Dörrie79d022142020-08-19 18:18:32134 Value() noexcept;
brettwf78cc272017-03-24 16:36:42135 Value(Value&& that) noexcept;
jdoerriecc9f5732017-08-23 14:12:30136
137 // Value's copy constructor and copy assignment operator are deleted. Use this
138 // to obtain a deep copy explicitly.
139 Value Clone() const;
140
jdoerrie05eb3162017-02-01 10:36:56141 explicit Value(Type type);
142 explicit Value(bool in_bool);
143 explicit Value(int in_int);
144 explicit Value(double in_double);
145
Jan Wilken Dörrie677e0c872021-03-10 10:04:38146 // Value(const char*) and Value(const char16_t*) are required despite
jdoerrie9f90ad72017-09-11 17:23:26147 // Value(StringPiece) and Value(StringPiece16) because otherwise the
jdoerrief38f37b2017-02-01 14:38:32148 // compiler will choose the Value(bool) constructor for these arguments.
149 // Value(std::string&&) allow for efficient move construction.
jdoerrief38f37b2017-02-01 14:38:32150 explicit Value(const char* in_string);
jdoerrief38f37b2017-02-01 14:38:32151 explicit Value(StringPiece in_string);
jdoerrie9f90ad72017-09-11 17:23:26152 explicit Value(std::string&& in_string) noexcept;
Jan Wilken Dörrie677e0c872021-03-10 10:04:38153 explicit Value(const char16_t* in_string16);
jdoerrie9f90ad72017-09-11 17:23:26154 explicit Value(StringPiece16 in_string16);
jdoerrief38f37b2017-02-01 14:38:32155
Jan Wilken Dörrie1f00b012021-03-22 15:10:00156 // Disable constructions from other pointers, so that there is no silent
157 // conversion to bool.
158 template <typename T,
159 typename = std::enable_if_t<
160 !std::is_convertible<T*, std::string>::value &&
161 !std::is_convertible<T*, std::u16string>::value>>
162 explicit Value(T* ptr) = delete;
163
jdoerrie9970f20e2018-07-20 21:41:18164 explicit Value(const std::vector<char>& in_blob);
165 explicit Value(base::span<const uint8_t> in_blob);
jdoerrie5f12b6272017-04-18 10:22:41166 explicit Value(BlobStorage&& in_blob) noexcept;
jdoerriee03e80f2017-02-15 08:42:14167
jdoerriecc9f5732017-08-23 14:12:30168 explicit Value(const DictStorage& in_dict);
mkwstcd8067b2017-04-11 06:52:21169 explicit Value(DictStorage&& in_dict) noexcept;
170
Jan Wilken Dörrie53e009b2019-09-09 14:17:41171 explicit Value(span<const Value> in_list);
jdoerrie2b7d0fcd2017-04-19 07:15:38172 explicit Value(ListStorage&& in_list) noexcept;
173
jdoerrie17e71cc2017-03-30 06:40:29174 Value& operator=(Value&& that) noexcept;
David Bienvenu5f4d4f02020-09-27 16:55:03175 Value(const Value&) = delete;
176 Value& operator=(const Value&) = delete;
jdoerrie05eb3162017-02-01 10:36:56177
jdoerrie8e945542017-02-17 13:54:49178 ~Value();
jdoerrie05eb3162017-02-01 10:36:56179
Gabriel Charetteb49d73a2021-05-05 20:05:59180 // Returns the name for a given `type`.
thestig61709242016-07-19 00:39:30181 static const char* GetTypeName(Type type);
182
initial.commitd7cae122008-07-26 21:49:38183 // Returns the type of the value stored by the current Value object.
Jan Wilken Dörrie79d022142020-08-19 18:18:32184 Type type() const { return static_cast<Type>(data_.index()); }
initial.commitd7cae122008-07-26 21:49:38185
186 // Returns true if the current object represents a given type.
jdoerriecc9f5732017-08-23 14:12:30187 bool is_none() const { return type() == Type::NONE; }
jdoerrie05eb3162017-02-01 10:36:56188 bool is_bool() const { return type() == Type::BOOLEAN; }
189 bool is_int() const { return type() == Type::INTEGER; }
190 bool is_double() const { return type() == Type::DOUBLE; }
191 bool is_string() const { return type() == Type::STRING; }
192 bool is_blob() const { return type() == Type::BINARY; }
193 bool is_dict() const { return type() == Type::DICTIONARY; }
194 bool is_list() const { return type() == Type::LIST; }
195
Jan Wilken Dörrie2e125622021-02-17 10:52:53196 // These will return nullopt / nullptr if the type does not match.
197 Optional<bool> GetIfBool() const;
198 Optional<int> GetIfInt() const;
199 // Implicitly converts from int if necessary.
200 Optional<double> GetIfDouble() const;
201 const std::string* GetIfString() const;
202 const BlobStorage* GetIfBlob() const;
203
Alexander Hendriche86ee512019-06-12 16:01:57204 // These will all CHECK that the type matches.
jdoerrie05eb3162017-02-01 10:36:56205 bool GetBool() const;
206 int GetInt() const;
207 double GetDouble() const; // Implicitly converts from int if necessary.
jdoerrief38f37b2017-02-01 14:38:32208 const std::string& GetString() const;
Dominic Battre08cbe972019-07-31 03:57:19209 std::string& GetString();
jdoerrie5f12b6272017-04-18 10:22:41210 const BlobStorage& GetBlob() const;
jdoerriee03e80f2017-02-15 08:42:14211
Jan Wilken Dörrie69a65a3a2020-01-16 15:03:30212 // Returns the Values in a list as a view. The mutable overload allows for
213 // modification of the underlying values, but does not allow changing the
Gabriel Charetteb49d73a2021-05-05 20:05:59214 // structure of the list. If this is desired, use `TakeList()`, perform the
Jan Wilken Dörrie69a65a3a2020-01-16 15:03:30215 // operations, and return the list back to the Value via move assignment.
216 ListView GetList();
Jan Wilken Dörrie8d9034f12019-11-28 14:48:57217 ConstListView GetList() const;
jdoerrie2b7d0fcd2017-04-19 07:15:38218
Lei Zhang20b21af82020-08-10 18:31:58219 // Transfers ownership of the underlying list to the caller. Subsequent
Gabriel Charetteb49d73a2021-05-05 20:05:59220 // calls to `GetList()` will return an empty list.
221 // Note: This requires that `type()` is Type::LIST.
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05222 ListStorage TakeList();
223
Gabriel Charetteb49d73a2021-05-05 20:05:59224 // Appends `value` to the end of the list.
225 // Note: These CHECK that `type()` is Type::LIST.
Jan Wilken Dörrie55b0b2b2019-09-10 05:40:24226 void Append(bool value);
227 void Append(int value);
228 void Append(double value);
Jan Wilken Dörrie1f00b012021-03-22 15:10:00229
Jan Wilken Dörrie55b0b2b2019-09-10 05:40:24230 void Append(const char* value);
231 void Append(StringPiece value);
232 void Append(std::string&& value);
Jan Wilken Dörrie677e0c872021-03-10 10:04:38233 void Append(const char16_t* value);
Jan Wilken Dörrie1f00b012021-03-22 15:10:00234
Gabriel Charetteb49d73a2021-05-05 20:05:59235 // Disable `Append()` from other pointers, so that there is no silent
Jan Wilken Dörrie1f00b012021-03-22 15:10:00236 // conversion to bool.
237 template <typename T,
238 typename = std::enable_if_t<
239 !std::is_convertible<T*, std::string>::value &&
240 !std::is_convertible<T*, std::u16string>::value>>
241 void Append(T* ptr) = delete;
242
Jan Wilken Dörrie55b0b2b2019-09-10 05:40:24243 void Append(StringPiece16 value);
244 void Append(Value&& value);
245
Gabriel Charetteb49d73a2021-05-05 20:05:59246 // Inserts `value` before `pos`.
247 // Note: This CHECK that `type()` is Type::LIST.
Jan Wilken Dörrie9065545e12019-10-30 10:44:51248 CheckedContiguousIterator<Value> Insert(
249 CheckedContiguousConstIterator<Value> pos,
250 Value&& value);
251
Gabriel Charetteb49d73a2021-05-05 20:05:59252 // Erases the Value pointed to by `iter`. Returns false if `iter` is out of
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05253 // bounds.
Gabriel Charetteb49d73a2021-05-05 20:05:59254 // Note: This requires that `type()` is Type::LIST.
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05255 bool EraseListIter(CheckedContiguousConstIterator<Value> iter);
256
Gabriel Charetteb49d73a2021-05-05 20:05:59257 // Erases all Values that compare equal to `val`. Returns the number of
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05258 // deleted Values.
Gabriel Charetteb49d73a2021-05-05 20:05:59259 // Note: This requires that `type()` is Type::LIST.
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05260 size_t EraseListValue(const Value& val);
261
Gabriel Charetteb49d73a2021-05-05 20:05:59262 // Erases all Values for which `pred` returns true. Returns the number of
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05263 // deleted Values.
Gabriel Charetteb49d73a2021-05-05 20:05:59264 // Note: This requires that `type()` is Type::LIST.
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05265 template <typename Predicate>
266 size_t EraseListValueIf(Predicate pred) {
Jan Wilken Dörrie79d022142020-08-19 18:18:32267 return base::EraseIf(list(), pred);
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05268 }
269
Jan Wilken Dörrie02577a22019-12-04 14:27:39270 // Erases all Values from the list.
Gabriel Charetteb49d73a2021-05-05 20:05:59271 // Note: This requires that `type()` is Type::LIST.
Jan Wilken Dörrie02577a22019-12-04 14:27:39272 void ClearList();
273
Gabriel Charetteb49d73a2021-05-05 20:05:59274 // `FindKey` looks up `key` in the underlying dictionary. If found, it returns
jdoerrie78ab7a22017-08-17 19:04:45275 // a pointer to the element. Otherwise it returns nullptr.
276 // returned. Callers are expected to perform a check against null before using
277 // the pointer.
Gabriel Charetteb49d73a2021-05-05 20:05:59278 // Note: This requires that `type()` is Type::DICTIONARY.
jdoerrie78ab7a22017-08-17 19:04:45279 //
280 // Example:
281 // auto* found = FindKey("foo");
282 Value* FindKey(StringPiece key);
283 const Value* FindKey(StringPiece key) const;
jdoerrie44efa9d2017-07-14 14:47:20284
Gabriel Charetteb49d73a2021-05-05 20:05:59285 // `FindKeyOfType` is similar to `FindKey`, but it also requires the found
286 // value to have type `type`. If no type is found, or the found value is of a
jdoerrie78ab7a22017-08-17 19:04:45287 // different type nullptr is returned.
288 // Callers are expected to perform a check against null before using the
289 // pointer.
Gabriel Charetteb49d73a2021-05-05 20:05:59290 // Note: This requires that `type()` is Type::DICTIONARY.
jdoerrie78ab7a22017-08-17 19:04:45291 //
292 // Example:
293 // auto* found = FindKey("foo", Type::DOUBLE);
294 Value* FindKeyOfType(StringPiece key, Type type);
295 const Value* FindKeyOfType(StringPiece key, Type type) const;
jdoerrie44efa9d2017-07-14 14:47:20296
Gabriel Charetteb49d73a2021-05-05 20:05:59297 // These are convenience forms of `FindKey`. They return `base`:nullopt| if
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16298 // the value is not found or doesn't have the type specified in the
299 // function's name.
300 base::Optional<bool> FindBoolKey(StringPiece key) const;
301 base::Optional<int> FindIntKey(StringPiece key) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59302 // Note `FindDoubleKey()` will auto-convert INTEGER keys to their double
303 // value, for consistency with `GetDouble()`.
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16304 base::Optional<double> FindDoubleKey(StringPiece key) const;
305
Gabriel Charetteb49d73a2021-05-05 20:05:59306 // `FindStringKey` returns `nullptr` if value is not found or not a string.
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16307 const std::string* FindStringKey(StringPiece key) const;
Dominic Battre08cbe972019-07-31 03:57:19308 std::string* FindStringKey(StringPiece key);
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16309
David 'Digit' Turnerfca8c4b52019-03-26 11:14:06310 // Returns nullptr is value is not found or not a binary.
311 const BlobStorage* FindBlobKey(StringPiece key) const;
312
313 // Returns nullptr if value is not found or not a dictionary.
314 const Value* FindDictKey(StringPiece key) const;
315 Value* FindDictKey(StringPiece key);
316
317 // Returns nullptr if value is not found or not a list.
318 const Value* FindListKey(StringPiece key) const;
319 Value* FindListKey(StringPiece key);
320
Gabriel Charetteb49d73a2021-05-05 20:05:59321 // `SetKey` looks up `key` in the underlying dictionary and sets the mapped
322 // value to `value`. If `key` could not be found, a new element is inserted.
jdoerrie78ab7a22017-08-17 19:04:45323 // A pointer to the modified item is returned.
Gabriel Charetteb49d73a2021-05-05 20:05:59324 // Note: This requires that `type()` is Type::DICTIONARY.
325 // Note: Prefer `Set<Type>Key()` for simple values.
jdoerrie78ab7a22017-08-17 19:04:45326 //
327 // Example:
328 // SetKey("foo", std::move(myvalue));
Sergey Abbakumov0e1215882019-03-15 22:32:16329 Value* SetKey(StringPiece key, Value&& value);
jdoerrie78ab7a22017-08-17 19:04:45330 // This overload results in a performance improvement for std::string&&.
Sergey Abbakumov0e1215882019-03-15 22:32:16331 Value* SetKey(std::string&& key, Value&& value);
jdoerrie46349472017-08-02 02:20:32332 // This overload is necessary to avoid ambiguity for const char* arguments.
Sergey Abbakumov0e1215882019-03-15 22:32:16333 Value* SetKey(const char* key, Value&& value);
jdoerrie44efa9d2017-07-14 14:47:20334
Gabriel Charetteb49d73a2021-05-05 20:05:59335 // `Set`Type>Key` looks up `key` in the underlying dictionary and associates a
336 // corresponding Value() constructed from the second parameter. Compared to
337 // `SetKey()`, this avoids un-necessary temporary `Value()` creation, as well
David 'Digit' Turnere169e6c2019-03-28 22:06:29338 // ambiguities in the value type.
339 Value* SetBoolKey(StringPiece key, bool val);
340 Value* SetIntKey(StringPiece key, int val);
341 Value* SetDoubleKey(StringPiece key, double val);
342 Value* SetStringKey(StringPiece key, StringPiece val);
Jan Wilken Dörrie293405a2020-05-12 19:45:11343 Value* SetStringKey(StringPiece key, StringPiece16 val);
344 // NOTE: The following two overloads are provided as performance / code
345 // generation optimizations.
David 'Digit' Turnere169e6c2019-03-28 22:06:29346 Value* SetStringKey(StringPiece key, const char* val);
347 Value* SetStringKey(StringPiece key, std::string&& val);
David 'Digit' Turnere169e6c2019-03-28 22:06:29348
Gabriel Charetteb49d73a2021-05-05 20:05:59349 // This attempts to remove the value associated with `key`. In case of
jdoerriec209c7d2019-04-05 09:51:46350 // failure, e.g. the key does not exist, false is returned and the underlying
Gabriel Charetteb49d73a2021-05-05 20:05:59351 // dictionary is not changed. In case of success, `key` is deleted from the
jdoerriec209c7d2019-04-05 09:51:46352 // dictionary and the method returns true.
Gabriel Charetteb49d73a2021-05-05 20:05:59353 // Note: This requires that `type()` is Type::DICTIONARY.
jdoerrie64783162017-09-04 16:33:32354 //
355 // Example:
jdoerriec209c7d2019-04-05 09:51:46356 // bool success = dict.RemoveKey("foo");
jdoerrie64783162017-09-04 16:33:32357 bool RemoveKey(StringPiece key);
358
Gabriel Charetteb49d73a2021-05-05 20:05:59359 // This attempts to extract the value associated with `key`. In case of
jdoerriec209c7d2019-04-05 09:51:46360 // failure, e.g. the key does not exist, nullopt is returned and the
Gabriel Charetteb49d73a2021-05-05 20:05:59361 // underlying dictionary is not changed. In case of success, `key` is deleted
jdoerriec209c7d2019-04-05 09:51:46362 // from the dictionary and the method returns the extracted Value.
Gabriel Charetteb49d73a2021-05-05 20:05:59363 // Note: This requires that `type()` is Type::DICTIONARY.
jdoerriec209c7d2019-04-05 09:51:46364 //
365 // Example:
366 // Optional<Value> maybe_value = dict.ExtractKey("foo");
367 Optional<Value> ExtractKey(StringPiece key);
368
Brett Wilsond16cf4ee2017-08-03 00:08:27369 // Searches a hierarchy of dictionary values for a given value. If a path
370 // of dictionaries exist, returns the item at that path. If any of the path
371 // components do not exist or if any but the last path components are not
372 // dictionaries, returns nullptr.
373 //
374 // The type of the leaf Value is not checked.
375 //
376 // Implementation note: This can't return an iterator because the iterator
377 // will actually be into another Value, so it can't be compared to iterators
jdoerrie78ab7a22017-08-17 19:04:45378 // from this one (in particular, the DictItems().end() iterator).
Brett Wilsond16cf4ee2017-08-03 00:08:27379 //
David 'Digit' Turner43ce6492019-04-04 16:04:44380 // This version takes a StringPiece for the path, using dots as separators.
Brett Wilsond16cf4ee2017-08-03 00:08:27381 // Example:
David 'Digit' Turner43ce6492019-04-04 16:04:44382 // auto* found = FindPath("foo.bar");
383 Value* FindPath(StringPiece path);
384 const Value* FindPath(StringPiece path) const;
385
386 // There are also deprecated versions that take the path parameter
387 // as either a std::initializer_list<StringPiece> or a
388 // span<const StringPiece>. The latter is useful to use a
389 // std::vector<std::string> as a parameter but creates huge dynamic
jdoerriec209c7d2019-04-05 09:51:46390 // allocations and should be avoided!
Gabriel Charetteb49d73a2021-05-05 20:05:59391 // Note: If there is only one component in the path, use `FindKey()` instead.
jdoerriec209c7d2019-04-05 09:51:46392 //
David 'Digit' Turner43ce6492019-04-04 16:04:44393 // Example:
jdoerriecd022242017-08-23 08:38:27394 // std::vector<StringPiece> components = ...
395 // auto* found = FindPath(components);
396 Value* FindPath(std::initializer_list<StringPiece> path);
397 Value* FindPath(span<const StringPiece> path);
398 const Value* FindPath(std::initializer_list<StringPiece> path) const;
399 const Value* FindPath(span<const StringPiece> path) const;
Brett Wilsond16cf4ee2017-08-03 00:08:27400
Lei Zhange0fc6f02017-10-27 00:27:23401 // Like FindPath() but will only return the value if the leaf Value type
Brett Wilsond16cf4ee2017-08-03 00:08:27402 // matches the given type. Will return nullptr otherwise.
Gabriel Charetteb49d73a2021-05-05 20:05:59403 // Note: Prefer `Find<Type>Path()` for simple values.
Lei Zhange0fc6f02017-10-27 00:27:23404 //
Gabriel Charetteb49d73a2021-05-05 20:05:59405 // Note: If there is only one component in the path, use `FindKeyOfType()`
David 'Digit' Turner43ce6492019-04-04 16:04:44406 // instead for slightly better performance.
407 Value* FindPathOfType(StringPiece path, Type type);
408 const Value* FindPathOfType(StringPiece path, Type type) const;
409
410 // Convenience accessors used when the expected type of a value is known.
411 // Similar to Find<Type>Key() but accepts paths instead of keys.
412 base::Optional<bool> FindBoolPath(StringPiece path) const;
413 base::Optional<int> FindIntPath(StringPiece path) const;
414 base::Optional<double> FindDoublePath(StringPiece path) const;
415 const std::string* FindStringPath(StringPiece path) const;
Dominic Battre08cbe972019-07-31 03:57:19416 std::string* FindStringPath(StringPiece path);
David 'Digit' Turner43ce6492019-04-04 16:04:44417 const BlobStorage* FindBlobPath(StringPiece path) const;
418 Value* FindDictPath(StringPiece path);
419 const Value* FindDictPath(StringPiece path) const;
420 Value* FindListPath(StringPiece path);
421 const Value* FindListPath(StringPiece path) const;
422
423 // The following forms are deprecated too, use the ones that take the path
424 // as a single StringPiece instead.
jdoerriecd022242017-08-23 08:38:27425 Value* FindPathOfType(std::initializer_list<StringPiece> path, Type type);
426 Value* FindPathOfType(span<const StringPiece> path, Type type);
427 const Value* FindPathOfType(std::initializer_list<StringPiece> path,
Brett Wilsond16cf4ee2017-08-03 00:08:27428 Type type) const;
jdoerriecd022242017-08-23 08:38:27429 const Value* FindPathOfType(span<const StringPiece> path, Type type) const;
Brett Wilsond16cf4ee2017-08-03 00:08:27430
431 // Sets the given path, expanding and creating dictionary keys as necessary.
432 //
jdoerrie64783162017-09-04 16:33:32433 // If the current value is not a dictionary, the function returns nullptr. If
434 // path components do not exist, they will be created. If any but the last
435 // components matches a value that is not a dictionary, the function will fail
436 // (it will not overwrite the value) and return nullptr. The last path
437 // component will be unconditionally overwritten if it exists, and created if
438 // it doesn't.
Brett Wilsond16cf4ee2017-08-03 00:08:27439 //
440 // Example:
David 'Digit' Turner43ce6492019-04-04 16:04:44441 // value.SetPath("foo.bar", std::move(myvalue));
Lei Zhange0fc6f02017-10-27 00:27:23442 //
Gabriel Charetteb49d73a2021-05-05 20:05:59443 // Note: If there is only one component in the path, use `SetKey()` instead.
444 // Note: Using `Set<Type>Path()` might be more convenient and efficient.
David 'Digit' Turner43ce6492019-04-04 16:04:44445 Value* SetPath(StringPiece path, Value&& value);
446
447 // These setters are more convenient and efficient than the corresponding
448 // SetPath(...) call.
449 Value* SetBoolPath(StringPiece path, bool value);
450 Value* SetIntPath(StringPiece path, int value);
451 Value* SetDoublePath(StringPiece path, double value);
452 Value* SetStringPath(StringPiece path, StringPiece value);
453 Value* SetStringPath(StringPiece path, const char* value);
454 Value* SetStringPath(StringPiece path, std::string&& value);
455 Value* SetStringPath(StringPiece path, StringPiece16 value);
456
Gabriel Charetteb49d73a2021-05-05 20:05:59457 // Deprecated: use the `SetPath(StringPiece, ...)` methods above instead.
Sergey Abbakumov0e1215882019-03-15 22:32:16458 Value* SetPath(std::initializer_list<StringPiece> path, Value&& value);
459 Value* SetPath(span<const StringPiece> path, Value&& value);
Brett Wilsond16cf4ee2017-08-03 00:08:27460
jdoerrie64783162017-09-04 16:33:32461 // Tries to remove a Value at the given path.
462 //
jdoerriec209c7d2019-04-05 09:51:46463 // If the current value is not a dictionary or any path component does not
jdoerrie64783162017-09-04 16:33:32464 // exist, this operation fails, leaves underlying Values untouched and returns
Gabriel Charetteb49d73a2021-05-05 20:05:59465 // `false`. In case intermediate dictionaries become empty as a result of this
jdoerrie64783162017-09-04 16:33:32466 // path removal, they will be removed as well.
Gabriel Charetteb49d73a2021-05-05 20:05:59467 // Note: If there is only one component in the path, use `ExtractKey()`
468 // instead.
jdoerrie64783162017-09-04 16:33:32469 //
470 // Example:
David 'Digit' Turner43ce6492019-04-04 16:04:44471 // bool success = value.RemovePath("foo.bar");
David 'Digit' Turner43ce6492019-04-04 16:04:44472 bool RemovePath(StringPiece path);
473
jdoerriec209c7d2019-04-05 09:51:46474 // Tries to extract a Value at the given path.
475 //
476 // If the current value is not a dictionary or any path component does not
477 // exist, this operation fails, leaves underlying Values untouched and returns
478 // nullopt. In case intermediate dictionaries become empty as a result of this
479 // path removal, they will be removed as well. Returns the extracted value on
480 // success.
Gabriel Charetteb49d73a2021-05-05 20:05:59481 // Note: If there is only one component in the path, use `ExtractKey()`
482 // instead.
jdoerriec209c7d2019-04-05 09:51:46483 //
484 // Example:
485 // Optional<Value> maybe_value = value.ExtractPath("foo.bar");
486 Optional<Value> ExtractPath(StringPiece path);
487
jdoerrie78ab7a22017-08-17 19:04:45488 using dict_iterator_proxy = detail::dict_iterator_proxy;
489 using const_dict_iterator_proxy = detail::const_dict_iterator_proxy;
jdoerrie44efa9d2017-07-14 14:47:20490
Gabriel Charetteb49d73a2021-05-05 20:05:59491 // `DictItems` returns a proxy object that exposes iterators to the underlying
jdoerrie44efa9d2017-07-14 14:47:20492 // dictionary. These are intended for iteration over all items in the
493 // dictionary and are compatible with for-each loops and standard library
494 // algorithms.
David Van Cleve373381d2020-01-07 18:16:13495 //
Gabriel Charetteb49d73a2021-05-05 20:05:59496 // Unlike with std::map, a range-for over the non-const version of
497 // `DictItems()` will range over items of type
498 // `pair<const std::string&, Value&>`, so code of the form
David Van Cleve373381d2020-01-07 18:16:13499 // for (auto kv : my_value.DictItems())
500 // Mutate(kv.second);
Gabriel Charetteb49d73a2021-05-05 20:05:59501 // will actually alter `my_value` in place (if it isn't const).
David Van Cleve373381d2020-01-07 18:16:13502 //
Gabriel Charetteb49d73a2021-05-05 20:05:59503 // Note: These CHECK that `type()` is Type::DICTIONARY.
jdoerrie44efa9d2017-07-14 14:47:20504 dict_iterator_proxy DictItems();
505 const_dict_iterator_proxy DictItems() const;
506
Jan Wilken Dörrief961a372020-11-02 20:30:34507 // Transfers ownership of the underlying dict to the caller. Subsequent
508 // calls to DictItems() will return an empty dict.
Gabriel Charetteb49d73a2021-05-05 20:05:59509 // Note: This requires that `type()` is Type::DICTIONARY.
Jan Wilken Dörrief961a372020-11-02 20:30:34510 DictStorage TakeDict();
511
Panos Astithas0532a862020-10-29 04:15:07512 // Returns the size of the dictionary, if the dictionary is empty, and clears
Gabriel Charetteb49d73a2021-05-05 20:05:59513 // the dictionary. Note: These CHECK that `type()` is Type::DICTIONARY.
Lei Zhange823c512018-05-07 20:27:30514 size_t DictSize() const;
515 bool DictEmpty() const;
Panos Astithas0532a862020-10-29 04:15:07516 void DictClear();
Lei Zhange823c512018-05-07 20:27:30517
Gabriel Charetteb49d73a2021-05-05 20:05:59518 // Merge `dictionary` into this value. This is done recursively, i.e. any
jdoerriec1091282018-08-01 17:28:12519 // sub-dictionaries will be merged as well. In case of key collisions, the
520 // passed in dictionary takes precedence and data already present will be
Gabriel Charetteb49d73a2021-05-05 20:05:59521 // replaced. Values within `dictionary` are deep-copied, so `dictionary` may
jdoerriec1091282018-08-01 17:28:12522 // be freed any time after this call.
Gabriel Charetteb49d73a2021-05-05 20:05:59523 // Note: This requires that `type()` and `dictionary->type()` is
524 // Type::DICTIONARY.
jdoerriec1091282018-08-01 17:28:12525 void MergeDictionary(const Value* dictionary);
526
[email protected]2f03f532013-07-17 08:43:33527 // These methods allow the convenient retrieval of the contents of the Value.
528 // If the current object can be converted into the given type, the value is
Gabriel Charetteb49d73a2021-05-05 20:05:59529 // returned through the `out_value` parameter and true is returned;
530 // otherwise, false is returned and `out_value` is unchanged.
531 // DEPRECATED, use `GetIfBool()` instead.
jdoerrie8e945542017-02-17 13:54:49532 bool GetAsBoolean(bool* out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59533 // DEPRECATED, use `GetIfInt()` instead.
jdoerrie8e945542017-02-17 13:54:49534 bool GetAsInteger(int* out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59535 // DEPRECATED, use `GetIfDouble()` instead.
jdoerrie8e945542017-02-17 13:54:49536 bool GetAsDouble(double* out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59537 // DEPRECATED, use `GetIfString()` instead.
jdoerrie8e945542017-02-17 13:54:49538 bool GetAsString(std::string* out_value) const;
Jan Wilken Dörrie085b2aa2021-03-12 16:26:57539 bool GetAsString(std::u16string* out_value) const;
jdoerrie122c4da2017-03-06 11:12:04540 bool GetAsString(const Value** out_value) const;
jdoerrie8e945542017-02-17 13:54:49541 bool GetAsString(StringPiece* out_value) const;
lukaszad1485da72016-11-01 21:49:46542 // ListValue::From is the equivalent for std::unique_ptr conversions.
Gabriel Charetteb49d73a2021-05-05 20:05:59543 // DEPRECATED, use `is_list()` instead.
jdoerrie8e945542017-02-17 13:54:49544 bool GetAsList(ListValue** out_value);
545 bool GetAsList(const ListValue** out_value) const;
lukaszad1485da72016-11-01 21:49:46546 // DictionaryValue::From is the equivalent for std::unique_ptr conversions.
jdoerrie8e945542017-02-17 13:54:49547 bool GetAsDictionary(DictionaryValue** out_value);
548 bool GetAsDictionary(const DictionaryValue** out_value) const;
[email protected]2f03f532013-07-17 08:43:33549 // Note: Do not add more types. See the file-level comment above for why.
initial.commitd7cae122008-07-26 21:49:38550
551 // This creates a deep copy of the entire Value tree, and returns a pointer
jdoerrie05eb3162017-02-01 10:36:56552 // to the copy. The caller gets ownership of the copy, of course.
[email protected]16f47e082011-01-18 02:16:59553 // Subclasses return their own type directly in their overrides;
554 // this works because C++ supports covariant return types.
Gabriel Charetteb49d73a2021-05-05 20:05:59555 // DEPRECATED, use `Value::Clone()` instead.
jdoerriee964d9a2017-04-05 06:44:17556 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49557 Value* DeepCopy() const;
Gabriel Charetteb49d73a2021-05-05 20:05:59558 // DEPRECATED, use `Value::Clone()` instead.
jdoerried4b852612017-06-06 11:48:43559 // TODO(crbug.com/646113): Delete this and migrate callsites.
dcheng093de9b2016-04-04 21:25:51560 std::unique_ptr<Value> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:38561
jdoerrie5c1cee112017-03-28 17:52:00562 // Comparison operators so that Values can easily be used with standard
563 // library algorithms and associative containers.
564 BASE_EXPORT friend bool operator==(const Value& lhs, const Value& rhs);
565 BASE_EXPORT friend bool operator!=(const Value& lhs, const Value& rhs);
566 BASE_EXPORT friend bool operator<(const Value& lhs, const Value& rhs);
567 BASE_EXPORT friend bool operator>(const Value& lhs, const Value& rhs);
568 BASE_EXPORT friend bool operator<=(const Value& lhs, const Value& rhs);
569 BASE_EXPORT friend bool operator>=(const Value& lhs, const Value& rhs);
570
initial.commitd7cae122008-07-26 21:49:38571 // Compares if two Value objects have equal contents.
Gabriel Charetteb49d73a2021-05-05 20:05:59572 // DEPRECATED, use `operator==(const Value& lhs, const Value& rhs)` instead.
jdoerrie5c1cee112017-03-28 17:52:00573 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49574 bool Equals(const Value* other) const;
initial.commitd7cae122008-07-26 21:49:38575
Eric Secklerf6c544f2020-06-02 10:49:21576 // Estimates dynamic memory usage. Requires tracing support
577 // (enable_base_tracing gn flag), otherwise always returns 0. See
578 // base/trace_event/memory_usage_estimator.h for more info.
Alexander Yashkinab504e72017-11-30 11:54:45579 size_t EstimateMemoryUsage() const;
580
Alan Cutter2dd83032020-12-08 21:55:00581 // Serializes to a string for logging and debug purposes.
582 std::string DebugString() const;
583
Peter Kotwicz83a231372021-04-13 17:42:12584#if BUILDFLAG(ENABLE_BASE_TRACING)
Alexander Timine68aeb32021-04-11 23:06:21585 // Write this object into a trace.
Alexander Timinbebb2002021-04-20 15:42:24586 void WriteIntoTrace(perfetto::TracedValue) const;
Peter Kotwicz83a231372021-04-13 17:42:12587#endif // BUILDFLAG(ENABLE_BASE_TRACING)
Alexander Timine68aeb32021-04-11 23:06:21588
jdoerrie8e945542017-02-17 13:54:49589 protected:
Jan Wilken Dörrie79d022142020-08-19 18:18:32590 // Checked convenience accessors for dict and list.
Jan Wilken Dörrief961a372020-11-02 20:30:34591 const LegacyDictStorage& dict() const {
592 return absl::get<LegacyDictStorage>(data_);
593 }
594 LegacyDictStorage& dict() { return absl::get<LegacyDictStorage>(data_); }
Jan Wilken Dörrie79d022142020-08-19 18:18:32595 const ListStorage& list() const { return absl::get<ListStorage>(data_); }
596 ListStorage& list() { return absl::get<ListStorage>(data_); }
597
Jan Wilken Dörrief961a372020-11-02 20:30:34598 // Internal constructors, allowing the simplify the implementation of Clone().
599 explicit Value(const LegacyDictStorage& storage);
600 explicit Value(LegacyDictStorage&& storage) noexcept;
601
Jan Wilken Dörrie79d022142020-08-19 18:18:32602 private:
David 'Digit' Turner2f287312019-04-03 14:32:09603 // Special case for doubles, which are aligned to 8 bytes on some
604 // 32-bit architectures. In this case, a simple declaration as a
605 // double member would make the whole union 8 byte-aligned, which
606 // would also force 4 bytes of wasted padding space before it in
607 // the Value layout.
David 'Digit' Turner806dedb82019-03-06 17:43:11608 //
David 'Digit' Turner2f287312019-04-03 14:32:09609 // To override this, store the value as an array of 32-bit integers, and
610 // perform the appropriate bit casts when reading / writing to it.
Jan Wilken Dörrie79d022142020-08-19 18:18:32611 using DoubleStorage = struct { alignas(4) char v[sizeof(double)]; };
David 'Digit' Turner2f287312019-04-03 14:32:09612
Jan Wilken Dörrie79d022142020-08-19 18:18:32613 // Internal constructors, allowing the simplify the implementation of Clone().
614 explicit Value(absl::monostate);
615 explicit Value(DoubleStorage storage);
jdoerrie8e945542017-02-17 13:54:49616
David 'Digit' Turner806dedb82019-03-06 17:43:11617 friend class ValuesTest_SizeOfValue_Test;
David 'Digit' Turner2f287312019-04-03 14:32:09618 double AsDoubleInternal() const;
jdoerriecc9f5732017-08-23 14:12:30619
David 'Digit' Turnere169e6c2019-03-28 22:06:29620 // NOTE: Using a movable reference here is done for performance (it avoids
621 // creating + moving + destroying a temporary unique ptr).
622 Value* SetKeyInternal(StringPiece key, std::unique_ptr<Value>&& val_ptr);
David 'Digit' Turner43ce6492019-04-04 16:04:44623 Value* SetPathInternal(StringPiece path, std::unique_ptr<Value>&& value_ptr);
David 'Digit' Turnere169e6c2019-03-28 22:06:29624
Jan Wilken Dörrie79d022142020-08-19 18:18:32625 absl::variant<absl::monostate,
626 bool,
627 int,
628 DoubleStorage,
629 std::string,
630 BlobStorage,
Jan Wilken Dörrief961a372020-11-02 20:30:34631 LegacyDictStorage,
Jan Wilken Dörrie79d022142020-08-19 18:18:32632 ListStorage>
633 data_;
initial.commitd7cae122008-07-26 21:49:38634};
635
[email protected]9e4cda7332010-07-31 04:56:14636// DictionaryValue provides a key-value dictionary with (optional) "path"
637// parsing for recursive access; see the comment at the top of the file. Keys
Gabriel Charetteb49d73a2021-05-05 20:05:59638// are `std`:string|s and should be UTF-8 encoded.
[email protected]0bea7252011-08-05 15:34:00639class BASE_EXPORT DictionaryValue : public Value {
initial.commitd7cae122008-07-26 21:49:38640 public:
Gabriel Charetteb49d73a2021-05-05 20:05:59641 // Returns `value` if it is a dictionary, nullptr otherwise.
dcheng093de9b2016-04-04 21:25:51642 static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value);
reillyg259c0a32015-09-11 00:25:54643
[email protected]3a3d47472010-07-15 21:03:54644 DictionaryValue();
Jan Wilken Dörrief961a372020-11-02 20:30:34645 explicit DictionaryValue(const LegacyDictStorage& in_dict);
646 explicit DictionaryValue(LegacyDictStorage&& in_dict) noexcept;
[email protected]5cf906f82011-11-26 01:11:44647
initial.commitd7cae122008-07-26 21:49:38648 // Returns true if the current dictionary has a value for the given key.
Gabriel Charetteb49d73a2021-05-05 20:05:59649 // DEPRECATED, use `Value::FindKey(key)` instead.
dcheng16d6f532016-08-25 16:07:11650 bool HasKey(StringPiece key) const;
initial.commitd7cae122008-07-26 21:49:38651
[email protected]fb804132c2009-04-15 00:17:53652 // Returns the number of Values in this dictionary.
Song Fangzhen771e71302021-05-07 06:10:19653 // DEPRECATED, use `Value::DictSize()` instead.
Jan Wilken Dörrie79d022142020-08-19 18:18:32654 size_t size() const { return dict().size(); }
[email protected]4dad9ad82009-11-25 20:47:52655
656 // Returns whether the dictionary is empty.
Song Fangzhen954ab3882021-05-13 19:20:47657 // DEPRECATED, use `Value::DictEmpty()` instead.
Jan Wilken Dörrie79d022142020-08-19 18:18:32658 bool empty() const { return dict().empty(); }
[email protected]fb804132c2009-04-15 00:17:53659
initial.commitd7cae122008-07-26 21:49:38660 // Clears any current contents of this dictionary.
Gabriel Charetteb49d73a2021-05-05 20:05:59661 // DEPRECATED, use `Value::DictClear()` instead.
[email protected]af5ed4a2008-08-04 13:56:25662 void Clear();
initial.commitd7cae122008-07-26 21:49:38663
664 // Sets the Value associated with the given path starting from this object.
665 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
666 // into the next DictionaryValue down. Obviously, "." can't be used
667 // within a key, but there are no other restrictions on keys.
668 // If the key at any step of the way doesn't exist, or exists but isn't
669 // a DictionaryValue, a new DictionaryValue will be created and attached
Gabriel Charetteb49d73a2021-05-05 20:05:59670 // to the path in that location. `in_value` must be non-null.
jdoerrieb94e5422017-04-28 21:52:58671 // Returns a pointer to the inserted value.
Gabriel Charetteb49d73a2021-05-05 20:05:59672 // DEPRECATED, use `Value::SetPath(path, value)` instead.
jdoerrieb94e5422017-04-28 21:52:58673 Value* Set(StringPiece path, std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38674
675 // Convenience forms of Set(). These methods will replace any existing
676 // value at that path, even if it has a different type.
Gabriel Charetteb49d73a2021-05-05 20:05:59677 // DEPRECATED, use `Value::SetBoolKey()` or `Value::SetBoolPath()`.
jdoerrieb94e5422017-04-28 21:52:58678 Value* SetBoolean(StringPiece path, bool in_value);
Gabriel Charetteb49d73a2021-05-05 20:05:59679 // DEPRECATED, use `Value::SetIntPath()`.
jdoerrieb94e5422017-04-28 21:52:58680 Value* SetInteger(StringPiece path, int in_value);
Gabriel Charetteb49d73a2021-05-05 20:05:59681 // DEPRECATED, use `Value::SetDoublePath()`.
jdoerrieb94e5422017-04-28 21:52:58682 Value* SetDouble(StringPiece path, double in_value);
Gabriel Charetteb49d73a2021-05-05 20:05:59683 // DEPRECATED, use `Value::SetStringPath()`.
jdoerrieb94e5422017-04-28 21:52:58684 Value* SetString(StringPiece path, StringPiece in_value);
Gabriel Charetteb49d73a2021-05-05 20:05:59685 // DEPRECATED, use `Value::SetStringPath()`.
Jan Wilken Dörrie085b2aa2021-03-12 16:26:57686 Value* SetString(StringPiece path, const std::u16string& in_value);
Gabriel Charetteb49d73a2021-05-05 20:05:59687 // DEPRECATED, use `Value::SetPath()`.
jdoerrieb94e5422017-04-28 21:52:58688 DictionaryValue* SetDictionary(StringPiece path,
689 std::unique_ptr<DictionaryValue> in_value);
Gabriel Charetteb49d73a2021-05-05 20:05:59690 // DEPRECATED, use `Value::SetPath()`.
jdoerrieb94e5422017-04-28 21:52:58691 ListValue* SetList(StringPiece path, std::unique_ptr<ListValue> in_value);
[email protected]4dad9ad82009-11-25 20:47:52692
693 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
694 // be used as paths.
Gabriel Charetteb49d73a2021-05-05 20:05:59695 // DEPRECATED, use `Value::SetKey(key, value)` instead.
jdoerrieb94e5422017-04-28 21:52:58696 Value* SetWithoutPathExpansion(StringPiece key,
697 std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38698
699 // Gets the Value associated with the given path starting from this object.
700 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
701 // into the next DictionaryValue down. If the path can be resolved
702 // successfully, the value for the last key in the path will be returned
Gabriel Charetteb49d73a2021-05-05 20:05:59703 // through the `out_value` parameter, and the function will return true.
704 // Otherwise, it will return false and `out_value` will be untouched.
initial.commitd7cae122008-07-26 21:49:38705 // Note that the dictionary always owns the value that's returned.
Gabriel Charetteb49d73a2021-05-05 20:05:59706 // `out_value` is optional and will only be set if non-NULL.
707 // DEPRECATED, use `Value::FindPath(path)` instead.
asvitkinedbd26533e2015-06-23 18:22:52708 bool Get(StringPiece path, const Value** out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59709 // DEPRECATED, use `Value::FindPath(path)` instead.
asvitkinedbd26533e2015-06-23 18:22:52710 bool Get(StringPiece path, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38711
Gabriel Charetteb49d73a2021-05-05 20:05:59712 // These are convenience forms of `Get()`. The value will be retrieved
initial.commitd7cae122008-07-26 21:49:38713 // and the return value will be true if the path is valid and the value at
714 // the end of the path can be returned in the form specified.
Gabriel Charetteb49d73a2021-05-05 20:05:59715 // `out_value` is optional and will only be set if non-NULL.
716 // DEPRECATED, use `Value::FindBoolPath(path)` instead.
dcheng16d6f532016-08-25 16:07:11717 bool GetBoolean(StringPiece path, bool* out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59718 // DEPRECATED, use `Value::FindIntPath(path)` instead.
dcheng16d6f532016-08-25 16:07:11719 bool GetInteger(StringPiece path, int* out_value) const;
jdoerriedc72ee942016-12-07 15:43:28720 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
[email protected]78c03a42014-03-09 07:13:23721 // doubles.
Gabriel Charetteb49d73a2021-05-05 20:05:59722 // DEPRECATED, use `Value::FindDoublePath(path)`.
dcheng16d6f532016-08-25 16:07:11723 bool GetDouble(StringPiece path, double* out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59724 // DEPRECATED, use `Value::FindStringPath(path)` instead.
dcheng16d6f532016-08-25 16:07:11725 bool GetString(StringPiece path, std::string* out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59726 // DEPRECATED, use `Value::FindStringPath(path)` instead.
Jan Wilken Dörrie085b2aa2021-03-12 16:26:57727 bool GetString(StringPiece path, std::u16string* out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59728 // DEPRECATED, use `Value::FindString(path)` and `IsAsciiString()` instead.
dcheng16d6f532016-08-25 16:07:11729 bool GetStringASCII(StringPiece path, std::string* out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59730 // DEPRECATED, use `Value::FindBlobPath(path)` instead.
jdoerrie14b25da2017-04-11 07:45:50731 bool GetBinary(StringPiece path, const Value** out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59732 // DEPRECATED, use `Value::FindBlobPath(path)` instead.
jdoerrie14b25da2017-04-11 07:45:50733 bool GetBinary(StringPiece path, Value** out_value);
Gabriel Charetteb49d73a2021-05-05 20:05:59734 // DEPRECATED, use `Value::FindPath(path)` and Value's Dictionary API
735 // instead.
Jan Wilken Dörrie79d022142020-08-19 18:18:32736 bool GetDictionary(StringPiece path, const DictionaryValue** out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59737 // DEPRECATED, use `Value::FindPath(path)` and Value's Dictionary API
738 // instead.
asvitkinedbd26533e2015-06-23 18:22:52739 bool GetDictionary(StringPiece path, DictionaryValue** out_value);
Gabriel Charetteb49d73a2021-05-05 20:05:59740 // DEPRECATED, use `Value::FindPath(path)` and `Value::GetList()` instead.
dcheng16d6f532016-08-25 16:07:11741 bool GetList(StringPiece path, const ListValue** out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59742 // DEPRECATED, use `Value::FindPath(path)` and `Value::GetList()` instead.
dcheng16d6f532016-08-25 16:07:11743 bool GetList(StringPiece path, ListValue** out_value);
initial.commitd7cae122008-07-26 21:49:38744
Gabriel Charetteb49d73a2021-05-05 20:05:59745 // Like `Get()`, but without special treatment of '.'. This allows e.g. URLs
746 // to be used as paths.
747 // DEPRECATED, use `Value::FindKey(key)` instead.
dcheng16d6f532016-08-25 16:07:11748 bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59749 // DEPRECATED, use `Value::FindKey(key)` instead.
dcheng16d6f532016-08-25 16:07:11750 bool GetWithoutPathExpansion(StringPiece key, Value** out_value);
Gabriel Charetteb49d73a2021-05-05 20:05:59751 // DEPRECATED, use `Value::FindBoolKey(key)` instead.
dcheng16d6f532016-08-25 16:07:11752 bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59753 // DEPRECATED, use `Value::FindIntKey(key)` instead.
dcheng16d6f532016-08-25 16:07:11754 bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59755 // DEPRECATED, use `Value::FindDoubleKey(key)` instead.
dcheng16d6f532016-08-25 16:07:11756 bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59757 // DEPRECATED, use `Value::FindStringKey(key)` instead.
dcheng16d6f532016-08-25 16:07:11758 bool GetStringWithoutPathExpansion(StringPiece key,
[email protected]4dad9ad82009-11-25 20:47:52759 std::string* out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59760 // DEPRECATED, use `Value::FindStringKey(key)` and UTF8ToUTF16() instead.
dcheng16d6f532016-08-25 16:07:11761 bool GetStringWithoutPathExpansion(StringPiece key,
Jan Wilken Dörrie085b2aa2021-03-12 16:26:57762 std::u16string* out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59763 // DEPRECATED, use `Value::FindDictKey(key)` instead.
[email protected]a61890e2012-07-27 22:27:11764 bool GetDictionaryWithoutPathExpansion(
dcheng16d6f532016-08-25 16:07:11765 StringPiece key,
[email protected]a61890e2012-07-27 22:27:11766 const DictionaryValue** out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59767 // DEPRECATED, use `Value::FindDictKey(key)` instead.
dcheng16d6f532016-08-25 16:07:11768 bool GetDictionaryWithoutPathExpansion(StringPiece key,
[email protected]a61890e2012-07-27 22:27:11769 DictionaryValue** out_value);
Gabriel Charetteb49d73a2021-05-05 20:05:59770 // DEPRECATED, use `Value::FindListKey(key)` instead.
dcheng16d6f532016-08-25 16:07:11771 bool GetListWithoutPathExpansion(StringPiece key,
[email protected]a61890e2012-07-27 22:27:11772 const ListValue** out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59773 // DEPRECATED, use `Value::FindListKey(key)` instead.
dcheng16d6f532016-08-25 16:07:11774 bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value);
[email protected]4dad9ad82009-11-25 20:47:52775
initial.commitd7cae122008-07-26 21:49:38776 // Removes the Value with the specified path from this dictionary (or one
777 // of its child dictionaries, if the path is more than just a local key).
Gabriel Charetteb49d73a2021-05-05 20:05:59778 // If `out_value` is non-NULL, the removed Value will be passed out via
779 // `out_value`. If `out_value` is NULL, the removed value will be deleted.
780 // This method returns true if `path` is a valid path; otherwise it will
[email protected]d814a8852013-08-06 13:33:04781 // return false and the DictionaryValue object will be unchanged.
Gabriel Charetteb49d73a2021-05-05 20:05:59782 // DEPRECATED, use `Value::RemovePath(path)` or `Value::ExtractPath(path)`
jdoerriec209c7d2019-04-05 09:51:46783 // instead.
dcheng5f9cf762016-11-29 05:30:31784 bool Remove(StringPiece path, std::unique_ptr<Value>* out_value);
initial.commitd7cae122008-07-26 21:49:38785
[email protected]4dad9ad82009-11-25 20:47:52786 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
787 // to be used as paths.
Gabriel Charetteb49d73a2021-05-05 20:05:59788 // DEPRECATED, use `Value::RemoveKey(key)` or `Value::ExtractKey(key)`
789 // instead.
jdoerrie8e945542017-02-17 13:54:49790 bool RemoveWithoutPathExpansion(StringPiece key,
791 std::unique_ptr<Value>* out_value);
[email protected]4dad9ad82009-11-25 20:47:52792
Gabriel Charetteb49d73a2021-05-05 20:05:59793 // Removes a path, clearing out all dictionaries on `path` that remain empty
794 // after removing the value at `path`.
795 // DEPRECATED, use `Value::RemovePath(path)` or `Value::ExtractPath(path)`
jdoerriec209c7d2019-04-05 09:51:46796 // instead.
dcheng5f9cf762016-11-29 05:30:31797 bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value);
[email protected]aa3283392013-11-27 01:38:24798
jdoerrie64783162017-09-04 16:33:32799 using Value::RemovePath; // DictionaryValue::RemovePath shadows otherwise.
800
Gabriel Charetteb49d73a2021-05-05 20:05:59801 // Makes a copy of `this` but doesn't include empty dictionaries and lists in
802 // the copy. This never returns NULL, even if `this` itself is empty.
dcheng093de9b2016-04-04 21:25:51803 std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const;
[email protected]ec330b52009-12-02 00:20:32804
Gabriel Charetteb49d73a2021-05-05 20:05:59805 // Swaps contents with the `other` dictionary.
jdoerrie8e945542017-02-17 13:54:49806 void Swap(DictionaryValue* other);
[email protected]ec5263a2011-05-10 09:23:39807
[email protected]32c0e002011-11-08 21:26:41808 // This class provides an iterator over both keys and values in the
809 // dictionary. It can't be used to modify the dictionary.
Gabriel Charetteb49d73a2021-05-05 20:05:59810 // DEPRECATED, use `Value::DictItems()` instead.
[email protected]a34cc092012-08-10 12:45:35811 class BASE_EXPORT Iterator {
[email protected]32c0e002011-11-08 21:26:41812 public:
[email protected]a34cc092012-08-10 12:45:35813 explicit Iterator(const DictionaryValue& target);
vmpstre65942b2016-02-25 00:50:31814 Iterator(const Iterator& other);
[email protected]a8d94b42013-12-10 18:52:22815 ~Iterator();
[email protected]32c0e002011-11-08 21:26:41816
Lei Zhang10fce02f2021-05-14 18:45:08817 bool IsAtEnd() const { return it_ == target_.DictItems().end(); }
[email protected]32c0e002011-11-08 21:26:41818 void Advance() { ++it_; }
819
820 const std::string& key() const { return it_->first; }
Lei Zhang10fce02f2021-05-14 18:45:08821 const Value& value() const { return it_->second; }
[email protected]32c0e002011-11-08 21:26:41822
823 private:
824 const DictionaryValue& target_;
Lei Zhang10fce02f2021-05-14 18:45:08825 detail::const_dict_iterator it_;
[email protected]32c0e002011-11-08 21:26:41826 };
827
Gabriel Charetteb49d73a2021-05-05 20:05:59828 // DEPRECATED, use `Value::Clone()` instead.
jdoerriee964d9a2017-04-05 06:44:17829 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49830 DictionaryValue* DeepCopy() const;
Gabriel Charetteb49d73a2021-05-05 20:05:59831 // DEPRECATED, use `Value::Clone()` instead.
jdoerried4b852612017-06-06 11:48:43832 // TODO(crbug.com/646113): Delete this and migrate callsites.
dcheng093de9b2016-04-04 21:25:51833 std::unique_ptr<DictionaryValue> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:38834};
835
836// This type of Value represents a list of other Value values.
Jan Wilken Dörriecf4ce5522020-10-27 14:41:04837// DEPRECATED: Use std::vector<base::Value> instead.
[email protected]0bea7252011-08-05 15:34:00838class BASE_EXPORT ListValue : public Value {
initial.commitd7cae122008-07-26 21:49:38839 public:
Jan Wilken Dörrie46992f022020-02-20 11:25:47840 using const_iterator = ListView::const_iterator;
841 using iterator = ListView::iterator;
[email protected]a502bbe72011-01-07 18:06:45842
Gabriel Charetteb49d73a2021-05-05 20:05:59843 // Returns `value` if it is a list, nullptr otherwise.
dcheng093de9b2016-04-04 21:25:51844 static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value);
reillyg259c0a32015-09-11 00:25:54845
[email protected]3a3d47472010-07-15 21:03:54846 ListValue();
Jan Wilken Dörrie53e009b2019-09-09 14:17:41847 explicit ListValue(span<const Value> in_list);
jdoerrie52939ed2017-04-26 18:19:42848 explicit ListValue(ListStorage&& in_list) noexcept;
initial.commitd7cae122008-07-26 21:49:38849
initial.commitd7cae122008-07-26 21:49:38850 // Clears the contents of this ListValue
Gabriel Charetteb49d73a2021-05-05 20:05:59851 // DEPRECATED, use `ClearList()` instead.
initial.commitd7cae122008-07-26 21:49:38852 void Clear();
853
854 // Returns the number of Values in this list.
Gabriel Charetteb49d73a2021-05-05 20:05:59855 // DEPRECATED, use `GetList()::size()` instead.
Jan Wilken Dörrie79d022142020-08-19 18:18:32856 size_t GetSize() const { return list().size(); }
initial.commitd7cae122008-07-26 21:49:38857
[email protected]ec330b52009-12-02 00:20:32858 // Returns whether the list is empty.
Gabriel Charetteb49d73a2021-05-05 20:05:59859 // DEPRECATED, use `GetList()::empty()` instead.
Jan Wilken Dörrie79d022142020-08-19 18:18:32860 bool empty() const { return list().empty(); }
[email protected]ec330b52009-12-02 00:20:32861
initial.commitd7cae122008-07-26 21:49:38862 // Sets the list item at the given index to be the Value specified by
863 // the value given. If the index beyond the current end of the list, null
864 // Values will be used to pad out the list.
865 // Returns true if successful, or false if the index was negative or
866 // the value is a null pointer.
Gabriel Charetteb49d73a2021-05-05 20:05:59867 // DEPRECATED, use `GetList()::operator[] instead.
dcheng093de9b2016-04-04 21:25:51868 bool Set(size_t index, std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38869
Gabriel Charetteb49d73a2021-05-05 20:05:59870 // Gets the Value at the given index. Modifies `out_value` (and returns true)
initial.commitd7cae122008-07-26 21:49:38871 // only if the index falls within the current list range.
Gabriel Charetteb49d73a2021-05-05 20:05:59872 // Note that the list always owns the Value passed out via `out_value`.
873 // `out_value` is optional and will only be set if non-NULL.
874 // DEPRECATED, use `GetList()::operator[] instead.
[email protected]5d30f92bf2012-08-03 08:43:37875 bool Get(size_t index, const Value** out_value) const;
876 bool Get(size_t index, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38877
Gabriel Charetteb49d73a2021-05-05 20:05:59878 // Convenience forms of `Get()`. Modifies `out_value` (and returns true)
[email protected]35213ce92010-04-08 19:06:15879 // only if the index is valid and the Value at that index can be returned
880 // in the specified form.
Gabriel Charetteb49d73a2021-05-05 20:05:59881 // `out_value` is optional and will only be set if non-NULL.
882 // DEPRECATED, use `GetList()::operator[]::GetBool()` instead.
[email protected]f82fb4952009-01-20 21:05:32883 bool GetBoolean(size_t index, bool* out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59884 // DEPRECATED, use `GetList()::operator[]::GetInt()` instead.
[email protected]f82fb4952009-01-20 21:05:32885 bool GetInteger(size_t index, int* out_value) const;
jdoerriedc72ee942016-12-07 15:43:28886 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
[email protected]78c03a42014-03-09 07:13:23887 // doubles.
Gabriel Charetteb49d73a2021-05-05 20:05:59888 // DEPRECATED, use `GetList()::operator[]::GetDouble()` instead.
[email protected]fb534c92011-02-01 01:02:07889 bool GetDouble(size_t index, double* out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:59890 // DEPRECATED, use `GetList()::operator[]::GetString()` instead.
[email protected]f82fb4952009-01-20 21:05:32891 bool GetString(size_t index, std::string* out_value) const;
Jan Wilken Dörrie085b2aa2021-03-12 16:26:57892 bool GetString(size_t index, std::u16string* out_value) const;
jdoerried4b852612017-06-06 11:48:43893
[email protected]5d30f92bf2012-08-03 08:43:37894 bool GetDictionary(size_t index, const DictionaryValue** out_value) const;
895 bool GetDictionary(size_t index, DictionaryValue** out_value);
jdoerrie52939ed2017-04-26 18:19:42896
897 using Value::GetList;
Gabriel Charetteb49d73a2021-05-05 20:05:59898 // DEPRECATED, use `GetList()::operator[]::GetList()` instead.
[email protected]5d30f92bf2012-08-03 08:43:37899 bool GetList(size_t index, const ListValue** out_value) const;
900 bool GetList(size_t index, ListValue** out_value);
initial.commitd7cae122008-07-26 21:49:38901
902 // Removes the Value with the specified index from this list.
Gabriel Charetteb49d73a2021-05-05 20:05:59903 // If `out_value` is non-NULL, the removed Value AND ITS OWNERSHIP will be
904 // passed out via `out_value`. If `out_value` is NULL, the removed value will
905 // be deleted. This method returns true if `index` is valid; otherwise
initial.commitd7cae122008-07-26 21:49:38906 // it will return false and the ListValue object will be unchanged.
Gabriel Charetteb49d73a2021-05-05 20:05:59907 // DEPRECATED, use `GetList()::erase()` instead.
jdoerrie8e945542017-02-17 13:54:49908 bool Remove(size_t index, std::unique_ptr<Value>* out_value);
initial.commitd7cae122008-07-26 21:49:38909
Gabriel Charetteb49d73a2021-05-05 20:05:59910 // Removes the first instance of `value` found in the list, if any, and
911 // deletes it. `index` is the location where `value` was found. Returns false
[email protected]4fc3c5642011-08-13 17:34:31912 // if not found.
Gabriel Charetteb49d73a2021-05-05 20:05:59913 // DEPRECATED, use `GetList()::erase()` instead.
[email protected]4fc3c5642011-08-13 17:34:31914 bool Remove(const Value& value, size_t* index);
[email protected]e7f5c6f2009-05-09 00:33:04915
Gabriel Charetteb49d73a2021-05-05 20:05:59916 // Removes the element at `iter`. If `out_value` is NULL, the value will be
[email protected]3cbe0812012-07-03 02:51:43917 // deleted, otherwise ownership of the value is passed back to the caller.
[email protected]a8d379cc2013-02-18 16:31:45918 // Returns an iterator pointing to the location of the element that
919 // followed the erased element.
Gabriel Charetteb49d73a2021-05-05 20:05:59920 // DEPRECATED, use `GetList()::erase()` instead.
dcheng093de9b2016-04-04 21:25:51921 iterator Erase(iterator iter, std::unique_ptr<Value>* out_value);
[email protected]3cbe0812012-07-03 02:51:43922
Jan Wilken Dörrie55b0b2b2019-09-10 05:40:24923 using Value::Append;
initial.commitd7cae122008-07-26 21:49:38924 // Appends a Value to the end of the list.
Gabriel Charetteb49d73a2021-05-05 20:05:59925 // DEPRECATED, use `Value::Append()` instead.
dcheng093de9b2016-04-04 21:25:51926 void Append(std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38927
[email protected]095812b2012-09-14 02:14:01928 // Convenience forms of Append.
Gabriel Charetteb49d73a2021-05-05 20:05:59929 // DEPRECATED, use `Value::Append()` instead.
[email protected]095812b2012-09-14 02:14:01930 void AppendBoolean(bool in_value);
931 void AppendInteger(int in_value);
932 void AppendDouble(double in_value);
dcheng16d6f532016-08-25 16:07:11933 void AppendString(StringPiece in_value);
Jan Wilken Dörrie085b2aa2021-03-12 16:26:57934 void AppendString(const std::u16string& in_value);
Gabriel Charetteb49d73a2021-05-05 20:05:59935 // DEPRECATED, use `Value::Append()` in a loop instead.
[email protected]095812b2012-09-14 02:14:01936 void AppendStrings(const std::vector<std::string>& in_values);
[email protected]095812b2012-09-14 02:14:01937
Jan Wilken Dörrie9065545e12019-10-30 10:44:51938 using Value::Insert;
[email protected]86c008e82009-08-28 20:26:05939 // Insert a Value at index.
940 // Returns true if successful, or false if the index was out of range.
Gabriel Charetteb49d73a2021-05-05 20:05:59941 // DEPRECATED, use `Value::Insert()` instead.
dcheng66c7a4c2016-09-14 05:49:58942 bool Insert(size_t index, std::unique_ptr<Value> in_value);
[email protected]86c008e82009-08-28 20:26:05943
Gabriel Charetteb49d73a2021-05-05 20:05:59944 // Searches for the first instance of `value` in the list using the Equals
[email protected]5fb35372011-09-19 15:23:10945 // method of the Value type.
946 // Returns a const_iterator to the found item or to end() if none exists.
Gabriel Charetteb49d73a2021-05-05 20:05:59947 // DEPRECATED, use `std::find()` instead.
[email protected]5fb35372011-09-19 15:23:10948 const_iterator Find(const Value& value) const;
949
Gabriel Charetteb49d73a2021-05-05 20:05:59950 // Swaps contents with the `other` list.
951 // DEPRECATED, use `GetList()::swap()` instead.
jdoerrie8e945542017-02-17 13:54:49952 void Swap(ListValue* other);
[email protected]8b8e7c92010-08-19 18:05:56953
[email protected]e8789192011-08-11 20:56:32954 // Iteration.
David Bertonife592842021-05-13 22:46:56955 //
956 // ListValue no longer supports iteration. Instead, use GetList() to get the
957 // underlying list:
958 //
959 // for (const auto& entry : list_value.GetList()) {
960 // ...
961 //
962 // for (auto it = list_value.GetList().begin();
963 // it != list_value.GetList().end(); ++it) {
964 // ...
initial.commitd7cae122008-07-26 21:49:38965
Gabriel Charetteb49d73a2021-05-05 20:05:59966 // DEPRECATED, use `Value::Clone()` instead.
jdoerriee964d9a2017-04-05 06:44:17967 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49968 ListValue* DeepCopy() const;
Gabriel Charetteb49d73a2021-05-05 20:05:59969 // DEPRECATED, use `Value::Clone()` instead.
jdoerried4b852612017-06-06 11:48:43970 // TODO(crbug.com/646113): Delete this and migrate callsites.
dcheng093de9b2016-04-04 21:25:51971 std::unique_ptr<ListValue> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:38972};
973
prashhir54a994502015-03-05 09:30:57974// This interface is implemented by classes that know how to serialize
975// Value objects.
[email protected]0bea7252011-08-05 15:34:00976class BASE_EXPORT ValueSerializer {
initial.commitd7cae122008-07-26 21:49:38977 public:
[email protected]3a3d47472010-07-15 21:03:54978 virtual ~ValueSerializer();
[email protected]abb9d0c2008-08-06 15:46:59979
initial.commitd7cae122008-07-26 21:49:38980 virtual bool Serialize(const Value& root) = 0;
prashhir54a994502015-03-05 09:30:57981};
982
983// This interface is implemented by classes that know how to deserialize Value
984// objects.
985class BASE_EXPORT ValueDeserializer {
986 public:
987 virtual ~ValueDeserializer();
initial.commitd7cae122008-07-26 21:49:38988
989 // This method deserializes the subclass-specific format into a Value object.
[email protected]b4cebf82008-12-29 19:59:08990 // If the return value is non-NULL, the caller takes ownership of returned
Nigel Tao410788e2020-06-24 07:12:27991 // Value.
992 //
Gabriel Charetteb49d73a2021-05-05 20:05:59993 // If the return value is nullptr, and if `error_code` is non-nullptr,
994 // `*error_code` will be set to an integer value representing the underlying
Nigel Tao410788e2020-06-24 07:12:27995 // error. See "enum ErrorCode" below for more detail about the integer value.
996 //
Gabriel Charetteb49d73a2021-05-05 20:05:59997 // If `error_message` is non-nullptr, it will be filled in with a formatted
[email protected]ba399672010-04-06 15:42:39998 // error message including the location of the error if appropriate.
dcheng093de9b2016-04-04 21:25:51999 virtual std::unique_ptr<Value> Deserialize(int* error_code,
Nigel Tao410788e2020-06-24 07:12:271000 std::string* error_message) = 0;
1001
1002 // The integer-valued error codes form four groups:
1003 // - The value 0 means no error.
1004 // - Values between 1 and 999 inclusive mean an error in the data (i.e.
1005 // content). The bytes being deserialized are not in the right format.
1006 // - Values 1000 and above mean an error in the metadata (i.e. context). The
1007 // file could not be read, the network is down, etc.
1008 // - Negative values are reserved.
1009 enum ErrorCode {
1010 kErrorCodeNoError = 0,
1011 // kErrorCodeInvalidFormat is a generic error code for "the data is not in
1012 // the right format". Subclasses of ValueDeserializer may return other
1013 // values for more specific errors.
1014 kErrorCodeInvalidFormat = 1,
1015 // kErrorCodeFirstMetadataError is the minimum value (inclusive) of the
1016 // range of metadata errors.
1017 kErrorCodeFirstMetadataError = 1000,
1018 };
1019
Gabriel Charetteb49d73a2021-05-05 20:05:591020 // The `error_code` argument can be one of the ErrorCode values, but it is
Nigel Tao410788e2020-06-24 07:12:271021 // not restricted to only being 0, 1 or 1000. Subclasses of ValueDeserializer
1022 // can define their own error code values.
1023 static inline bool ErrorCodeIsDataError(int error_code) {
1024 return (kErrorCodeInvalidFormat <= error_code) &&
1025 (error_code < kErrorCodeFirstMetadataError);
1026 }
initial.commitd7cae122008-07-26 21:49:381027};
1028
[email protected]ea0ec052013-04-16 09:04:021029// Stream operator so Values can be used in assertion statements. In order that
1030// gtest uses this operator to print readable output on test failures, we must
1031// override each specific type. Otherwise, the default template implementation
1032// is preferred over an upcast.
[email protected]e4ef8312012-09-12 03:39:351033BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
1034
[email protected]ea0ec052013-04-16 09:04:021035BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
[email protected]ea0ec052013-04-16 09:04:021036 const DictionaryValue& value) {
1037 return out << static_cast<const Value&>(value);
1038}
1039
1040BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
1041 const ListValue& value) {
1042 return out << static_cast<const Value&>(value);
1043}
1044
jdoerriedc72ee942016-12-07 15:43:281045// Stream operator so that enum class Types can be used in log statements.
1046BASE_EXPORT std::ostream& operator<<(std::ostream& out,
1047 const Value::Type& type);
1048
[email protected]f3a1c642011-07-12 19:15:031049} // namespace base
1050
[email protected]101d5422008-09-26 20:22:421051#endif // BASE_VALUES_H_