blob: 30c438869a6af5e8a66c2abb125fdd939135ec6e [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]101d5422008-09-26 20:22:425#ifndef BASE_VALUES_H_
6#define BASE_VALUES_H_
initial.commitd7cae122008-07-26 21:49:387
[email protected]c014f2b32013-09-03 23:29:128#include <stddef.h>
avi9b6f42932015-12-26 22:15:149#include <stdint.h>
[email protected]c014f2b32013-09-03 23:29:1210
Daniel Cheng782d2ba32022-02-16 19:40:2911#include <array>
Lei Zhang935738a2021-05-17 21:35:5312#include <initializer_list>
[email protected]c014f2b32013-09-03 23:29:1213#include <iosfwd>
dcheng093de9b2016-04-04 21:25:5114#include <memory>
[email protected]8e50b602009-03-03 22:59:4315#include <string>
[email protected]c014f2b32013-09-03 23:29:1216#include <utility>
initial.commitd7cae122008-07-26 21:49:3817#include <vector>
18
[email protected]0bea7252011-08-05 15:34:0019#include "base/base_export.h"
Daniel Cheng782d2ba32022-02-16 19:40:2920#include "base/bit_cast.h"
Daniel Cheng8ac305b2022-02-17 00:05:1121#include "base/compiler_specific.h"
Jan Wilken Dörrie7e7a9792019-10-15 14:42:0522#include "base/containers/checked_iterators.h"
Jan Wilken Dörrie8d9034f12019-11-28 14:48:5723#include "base/containers/checked_range.h"
Lei Zhanga1209af2021-06-26 03:26:2424#include "base/containers/cxx20_erase_vector.h"
mkwstcd8067b2017-04-11 06:52:2125#include "base/containers/flat_map.h"
jdoerriecd022242017-08-23 08:38:2726#include "base/containers/span.h"
asvitkinedbd26533e2015-06-23 18:22:5227#include "base/strings/string_piece.h"
Alexander Timine68aeb32021-04-11 23:06:2128#include "base/trace_event/base_tracing_forward.h"
jdoerrie44efa9d2017-07-14 14:47:2029#include "base/value_iterators.h"
Anton Bikineev7dd58ad2021-05-18 01:01:3930#include "third_party/abseil-cpp/absl/types/optional.h"
Jan Wilken Dörrie79d022142020-08-19 18:18:3231#include "third_party/abseil-cpp/absl/types/variant.h"
initial.commitd7cae122008-07-26 21:49:3832
[email protected]f3a1c642011-07-12 19:15:0333namespace base {
34
initial.commitd7cae122008-07-26 21:49:3835class DictionaryValue;
36class ListValue;
37
Daniel Chenga367fe52022-02-15 18:08:4838// The `Value` class is a variant type can hold one of the following types:
39// - null
40// - bool
41// - int
42// - double
43// - string (internally UTF8-encoded)
44// - binary data (i.e. a blob)
45// - dictionary of string keys to `Value`s
46// - list of `Value`s
[email protected]2f03f532013-07-17 08:43:3347//
Daniel Chenga367fe52022-02-15 18:08:4848// With the exception of binary blobs, `Value` is intended to be the C++ version
49// of data types that can be represented in JSON.
Brett Wilson4bef8ee2017-09-01 20:11:4950//
Daniel Chenga367fe52022-02-15 18:08:4851// Warning: blob support may be removed in the future.
52//
53// ## Usage
54//
55// Do not use `Value` if a more specific type would be more appropriate. For
56// example, a function that only accepts dictionary values should have a
57// `base::Value::Dict` parameter, not a `base::Value` parameter.
58//
59// Construction:
60//
61// `Value` is directly constructible from `bool`, `int`, `double`, binary blobs
62// (`std::vector<uint8_t>`), `base::StringPiece`, `base::StringPiece16`,
63// `Value::Dict`, and `Value::List`.
64//
65// Copying:
66//
67// `Value` does not support C++ copy semantics to make it harder to accidentally
68// copy large values. Instead, use `Clone()` to manually create a deep copy.
69//
70// Reading:
71//
72// `GetBool()`, GetInt()`, et cetera `CHECK()` that the `Value` has the correct
73// subtype before returning the contained value. `bool`, `int`, `double` are
74// returned by value. Binary blobs, `std::string`, `Value::Dict`, `Value::List`
75// are returned by reference.
76//
77// `GetIfBool()`, `GetIfInt()`, et cetera return `absl::nullopt`/`nullptr` if
78// the `Value` does not have the correct subtype; otherwise, returns the value
79// wrapped in an `absl::optional` (for `bool`, `int`, `double`) or by pointer
80// (for binary blobs, `std::string`, `Value::Dict`, `Value::List`).
81//
82// Note: both `GetDouble()` and `GetIfDouble()` still return a non-null result
83// when the subtype is `Value::Type::INT`. In that case, the stored value is
84// coerced to a double before being returned.
85//
86// Assignment:
87//
88// It is not possible to directly assign `bool`, `int`, et cetera to a `Value`.
89// Instead, wrap the underlying type in `Value` before assigning.
90//
91// ## Dictionaries and Lists
92//
93// `Value` provides the `Value::Dict` and `Value::List` container types for
94// working with dictionaries and lists of values respectively, rather than
95// exposing the underlying container types directly. This allows the types to
96// provide convenient helpers for dictionaries and lists, as well as giving
97// greater flexibility for changing implementation details in the future.
98//
99// Both container types support enough STL-isms to be usable in range-based for
100// loops and generic operations such as those from <algorithm>.
101//
102// Dictionaries support:
103// - `empty()`, `size()`, `begin()`, `end()`, `cbegin()`, `cend()`,
104// `contains()`, `clear()`, `erase()`: Identical to the STL container
105// equivalents, with additional safety checks, e.g. iterators will
106// `CHECK()` if `end()` is dereferenced.
107//
108// - `Clone()`: Create a deep copy.
109// - `Merge()`: Merge another dictionary into this dictionary.
110// - `Find()`: Find a value by `StringPiece` key, returning nullptr if the key
111// is not present.
112// - `FindBool()`, `FindInt()`, ...: Similar to `Find()`, but ensures that the
113// `Value` also has the correct subtype. Same return semantics as
114// `GetIfBool()`, `GetIfInt()`, et cetera, returning `absl::nullopt` or
115// `nullptr` if the key is not present or the value has the wrong subtype.
116// - `Set()`: Associate a value with a `StringPiece` key. Accepts `Value` or any
117// of the subtypes that `Value` can hold.
118// - `Remove()`: Remove the key from this dictionary, if present.
119// - `Extract()`: If the key is present in the dictionary, removes the key from
120// the dictionary and transfers ownership of `Value` to the caller.
121// Otherwise, returns `absl::nullopt`.
122//
123// Dictionaries also support an additional set of helper methods that operate on
124// "paths": `FindByDottedPath()`, `SetByDottedPath()`, `RemoveByDottedPath()`,
125// and `ExtractByDottedPath()`. Dotted paths are a convenience method of naming
126// intermediate nested dictionaries, separating the components of the path using
127// '.' characters. For example, finding a string path on a `Value::Dict` using
128// the dotted path:
129//
130// "aaa.bbb.ccc"
131//
132// Will first look for a `Value::Type::DICT` associated with the key "aaa", then
133// another `Value::Type::DICT` under the "aaa" dict associated with the
134// key "bbb", and then a `Value::Type::STRING` under the "bbb" dict associated
135// with the key "ccc".
136//
Daniel Cheng619653b2022-02-17 18:33:12137// If a path only has one component (i.e. has no dots), please use the regular,
138// non-path APIs.
139//
Daniel Chenga367fe52022-02-15 18:08:48140// Lists support:
141// - `empty()`, `size()`, `begin()`, `end()`, `cbegin()`, `cend()`,
Daniel Chengc9ab0ef2022-02-18 02:34:07142// `front()`, `back()`, `reserve()`, `operator[]`, `clear()`, `erase()`:
143// Identical to the STL container equivalents, with additional safety
144// checks, e.g. `operator[]` will `CHECK()` if the index is out of range.
Daniel Chenga367fe52022-02-15 18:08:48145// - `Clone()`: Create a deep copy.
146// - `Append()`: Append a value to the end of the list. Accepts `Value` or any
147// of the subtypes that `Value` can hold.
148// - `Insert()`: Insert a `Value` at a specified point in the list.
149// - `EraseValue()`: Erases all matching `Value`s from the list.
150// - `EraseIf()`: Erase all `Value`s matching an arbitrary predicate from the
151// list.
152//
153// ## Refactoring Notes
154//
155// `Value` was originally implemented as a class hierarchy, with a `Value` base
156// class, and a leaf class for each of the different types of `Value` subtypes.
Brett Wilson4bef8ee2017-09-01 20:11:49157// https://docs.google.com/document/d/1uDLu5uTRlCWePxQUEHc8yNQdEoE1BDISYdpggWEABnw
Daniel Chenga367fe52022-02-15 18:08:48158// proposed an overhaul of the `Value` API that has now largely been
159// implemented, though there remains a significant amount of legacy code that is
160// still being migrated as part of the code health migration.
Brett Wilson4bef8ee2017-09-01 20:11:49161//
162// OLD WAY:
163//
164// std::unique_ptr<base::Value> GetFoo() {
165// std::unique_ptr<DictionaryValue> dict;
Daniel Chenga367fe52022-02-15 18:08:48166// dict->SetString("mykey", "foo");
Brett Wilson4bef8ee2017-09-01 20:11:49167// return dict;
168// }
169//
Brett Wilson4bef8ee2017-09-01 20:11:49170// NEW WAY:
171//
172// base::Value GetFoo() {
Daniel Chenga367fe52022-02-15 18:08:48173// base::Value::Dict dict;
Scott Haseley5fd36262022-03-04 19:35:11174// dict.Set("mykey", "abc");
Daniel Chenga367fe52022-02-15 18:08:48175// return base::Value(std::move(dict));
Brett Wilson4bef8ee2017-09-01 20:11:49176// }
Jan Wilken Dörriecf4ce5522020-10-27 14:41:04177//
Daniel Chenga367fe52022-02-15 18:08:48178// To avoid losing type information with the new variant-based design, migration
179// off the deprecated types should use more specific subtypes where possible:
Jan Wilken Dörriecf4ce5522020-10-27 14:41:04180//
Jan Wilken Dörrie2a06d6e92020-11-09 09:32:49181// OLD WAY:
182//
183// void AlwaysTakesList(std::unique_ptr<base::ListValue> list);
184// void AlwaysTakesDict(std::unique_ptr<base::DictionaryValue> dict);
185//
Daniel Chenga367fe52022-02-15 18:08:48186// DEPRECATED (PREVIOUS) WAY:
Jan Wilken Dörrie2a06d6e92020-11-09 09:32:49187//
188// void AlwaysTakesList(std::vector<base::Value> list);
Daniel Chenga367fe52022-02-15 18:08:48189// void AlwaysTakesListAlternative1(base::Value::ConstListView list);
190// void AlwaysTakesListAlternative2(base::Value::ListView& list);
191// void AlwaysTakesListAlterantive3(base::Value::ListStorage);
Jan Wilken Dörrie2a06d6e92020-11-09 09:32:49192// void AlwaysTakesDict(base::flat_map<std::string, base::Value> dict);
Daniel Chenga367fe52022-02-15 18:08:48193// void AlwaysTakesDictAlternative(base::Value::DictStorage);
Daniel Cheng60e6b2d2022-02-05 01:08:46194//
195// NEW WAY:
196//
Daniel Chenga367fe52022-02-15 18:08:48197// void AlwaysTakesList(base::Value::List list);
198// void AlwaysTakesDict(base::Value::Dict dict);
Daniel Cheng60e6b2d2022-02-05 01:08:46199//
Daniel Chenga367fe52022-02-15 18:08:48200// Migrating code may require conversions on API boundaries. If something seems
201// awkward/inefficient, please reach out to #code-health-rotation on Slack for
202// consultation: it is entirely possible that certain classes of APIs may be
203// missing due to an unrealized need.
Daniel Cheng8ac305b2022-02-17 00:05:11204class BASE_EXPORT GSL_OWNER Value {
initial.commitd7cae122008-07-26 21:49:38205 public:
jdoerrie9970f20e2018-07-20 21:41:18206 using BlobStorage = std::vector<uint8_t>;
Daniel Cheng773ce4502022-01-28 15:25:06207
208 using DeprecatedListStorage = std::vector<Value>;
209 using DeprecatedDictStorage = flat_map<std::string, Value>;
210 // TODO(https://crbug.com/1291666): Make these private.
211 using ListStorage = DeprecatedListStorage;
212 using DictStorage = DeprecatedDictStorage;
Jan Wilken Dörrief961a372020-11-02 20:30:34213
214 // Like `DictStorage`, but with std::unique_ptr in the mapped type. This is
215 // due to legacy reasons, and should be removed once no caller relies on
216 // stability of pointers anymore.
217 using LegacyDictStorage = flat_map<std::string, std::unique_ptr<Value>>;
Jan Wilken Dörrie8d9034f12019-11-28 14:48:57218
Daniel Cheng773ce4502022-01-28 15:25:06219 using DeprecatedListView = CheckedContiguousRange<ListStorage>;
220 using DeprecatedConstListView = CheckedContiguousConstRange<ListStorage>;
221 // TODO(https://crbug.com/1291666): Make these private.
222 using ListView = DeprecatedListView;
223 using ConstListView = DeprecatedConstListView;
Jan Wilken Dörrie8d9034f12019-11-28 14:48:57224
Daniel Chenga367fe52022-02-15 18:08:48225 class Dict;
226 class List;
227
Jose Dapena Paz7685422a2019-04-03 18:35:04228 enum class Type : unsigned char {
jdoerriedc72ee942016-12-07 15:43:28229 NONE = 0,
230 BOOLEAN,
231 INTEGER,
232 DOUBLE,
233 STRING,
234 BINARY,
Daniel Chenga367fe52022-02-15 18:08:48235 DICT,
236 // TODO(https://crbug.com/1291670): Deprecated and will be removed.
237 DICTIONARY = DICT,
jdoerriee1b1f3a2019-03-16 04:08:01238 LIST,
[email protected]2f03f532013-07-17 08:43:33239 // Note: Do not add more types. See the file-level comment above for why.
[email protected]a502bbe72011-01-07 18:06:45240 };
241
Lei Zhang30895d52017-10-23 19:14:46242 // Adaptors for converting from the old way to the new way and vice versa.
243 static Value FromUniquePtrValue(std::unique_ptr<Value> val);
244 static std::unique_ptr<Value> ToUniquePtrValue(Value val);
Lei Zhang8c1432b2019-10-08 18:48:54245 static const DictionaryValue& AsDictionaryValue(const Value& val);
246 static const ListValue& AsListValue(const Value& val);
Lei Zhang30895d52017-10-23 19:14:46247
Jan Wilken Dörrie79d022142020-08-19 18:18:32248 Value() noexcept;
jdoerriecc9f5732017-08-23 14:12:30249
Daniel Chenga367fe52022-02-15 18:08:48250 Value(Value&&) noexcept;
251 Value& operator=(Value&&) noexcept;
jdoerriecc9f5732017-08-23 14:12:30252
Daniel Chenga367fe52022-02-15 18:08:48253 // Deleted to prevent accidental copying.
David Bienvenu5f4d4f02020-09-27 16:55:03254 Value(const Value&) = delete;
255 Value& operator=(const Value&) = delete;
jdoerrie05eb3162017-02-01 10:36:56256
Daniel Chenga367fe52022-02-15 18:08:48257 // Creates a deep copy of this value.
258 Value Clone() const;
259
260 // Creates a `Value` of `type`. The data of the corresponding type will be
261 // default constructed.
262 explicit Value(Type type);
263
264 // Constructor for `Value::Type::BOOLEAN`.
265 explicit Value(bool value);
266
267 // Prevent pointers from implicitly converting to bool. Another way to write
268 // this would be to template the bool constructor and use SFINAE to only allow
269 // use if `std::is_same_v<T, bool>` is true, but this has surprising behavior
270 // with range-based for loops over a `std::vector<bool>` (which will
271 // unintuitively match the int overload instead).
272 //
273 // The `const` is load-bearing; otherwise, a `char*` argument would prefer the
274 // deleted overload due to requiring a qualification conversion.
275 template <typename T>
276 explicit Value(const T*) = delete;
277
278 // Constructor for `Value::Type::INT`.
279 explicit Value(int value);
280
281 // Constructor for `Value::Type::DOUBLE`.
282 explicit Value(double value);
283
284 // Constructors for `Value::Type::STRING`.
285 explicit Value(StringPiece value);
286 explicit Value(StringPiece16 value);
287 // `char*` and `char16_t*` are needed to provide a more specific overload than
288 // the deleted `const T*` overload above.
289 explicit Value(const char* value);
290 explicit Value(const char16_t* value);
291 // `std::string&&` allows for efficient move construction.
292 explicit Value(std::string&& value) noexcept;
293
294 // Constructors for `Value::Type::BINARY`.
295 explicit Value(const std::vector<char>& value);
296 explicit Value(base::span<const uint8_t> value);
297 explicit Value(BlobStorage&& value) noexcept;
298
299 // Constructor for `Value::Type::DICT`.
300 explicit Value(Dict&& value) noexcept;
301
302 // Constructor for `Value::Type::LIST`.
303 explicit Value(List&& value) noexcept;
304
Daniel Cheng619653b2022-02-17 18:33:12305 // DEPRECATED: prefer `Value(Dict&&)`.
Daniel Chenga367fe52022-02-15 18:08:48306 explicit Value(const DictStorage& value);
307 explicit Value(DictStorage&& value);
308
Daniel Cheng619653b2022-02-17 18:33:12309 // DEPRECATED: prefer `Value(List&&)`.
Daniel Chenga367fe52022-02-15 18:08:48310 explicit Value(span<const Value> value);
311 explicit Value(ListStorage&& value) noexcept;
312
jdoerrie8e945542017-02-17 13:54:49313 ~Value();
jdoerrie05eb3162017-02-01 10:36:56314
Gabriel Charetteb49d73a2021-05-05 20:05:59315 // Returns the name for a given `type`.
thestig61709242016-07-19 00:39:30316 static const char* GetTypeName(Type type);
317
initial.commitd7cae122008-07-26 21:49:38318 // Returns the type of the value stored by the current Value object.
Jan Wilken Dörrie79d022142020-08-19 18:18:32319 Type type() const { return static_cast<Type>(data_.index()); }
initial.commitd7cae122008-07-26 21:49:38320
321 // Returns true if the current object represents a given type.
jdoerriecc9f5732017-08-23 14:12:30322 bool is_none() const { return type() == Type::NONE; }
jdoerrie05eb3162017-02-01 10:36:56323 bool is_bool() const { return type() == Type::BOOLEAN; }
324 bool is_int() const { return type() == Type::INTEGER; }
325 bool is_double() const { return type() == Type::DOUBLE; }
326 bool is_string() const { return type() == Type::STRING; }
327 bool is_blob() const { return type() == Type::BINARY; }
Daniel Chenga367fe52022-02-15 18:08:48328 bool is_dict() const { return type() == Type::DICT; }
jdoerrie05eb3162017-02-01 10:36:56329 bool is_list() const { return type() == Type::LIST; }
330
Daniel Chenga367fe52022-02-15 18:08:48331 // Returns the stored data if the type matches, or `absl::nullopt`/`nullptr`
332 // otherwise. `bool`, `int`, and `double` are returned in a wrapped
333 // `absl::optional`; blobs, `Value::Dict`, and `Value::List` are returned by
334 // pointer.
Anton Bikineev7dd58ad2021-05-18 01:01:39335 absl::optional<bool> GetIfBool() const;
336 absl::optional<int> GetIfInt() const;
Daniel Chenga367fe52022-02-15 18:08:48337 // Returns a non-null value for both `Value::Type::DOUBLE` and
338 // `Value::Type::INT`, converting the latter to a double.
Anton Bikineev7dd58ad2021-05-18 01:01:39339 absl::optional<double> GetIfDouble() const;
Jan Wilken Dörrie2e125622021-02-17 10:52:53340 const std::string* GetIfString() const;
Daniel Chenga367fe52022-02-15 18:08:48341 std::string* GetIfString();
Jan Wilken Dörrie2e125622021-02-17 10:52:53342 const BlobStorage* GetIfBlob() const;
Daniel Chenga367fe52022-02-15 18:08:48343 const Dict* GetIfDict() const;
344 Dict* GetIfDict();
345 const List* GetIfList() const;
346 List* GetIfList();
Jan Wilken Dörrie2e125622021-02-17 10:52:53347
Daniel Chenga367fe52022-02-15 18:08:48348 // Similar to the `GetIf...()` variants above, but fails with a `CHECK()` on a
349 // type mismatch. `bool`, `int`, and `double` are returned by value; blobs,
350 // `Value::Dict`, and `Value::List` are returned by reference.
jdoerrie05eb3162017-02-01 10:36:56351 bool GetBool() const;
352 int GetInt() const;
Daniel Chenga367fe52022-02-15 18:08:48353 // Returns a value for both `Value::Type::DOUBLE` and `Value::Type::INT`,
354 // converting the latter to a double.
355 double GetDouble() const;
jdoerrief38f37b2017-02-01 14:38:32356 const std::string& GetString() const;
Dominic Battre08cbe972019-07-31 03:57:19357 std::string& GetString();
jdoerrie5f12b6272017-04-18 10:22:41358 const BlobStorage& GetBlob() const;
Daniel Chenga367fe52022-02-15 18:08:48359 const Dict& GetDict() const;
360 Dict& GetDict();
361 const List& GetList() const;
362 List& GetList();
363
364 // Represents a dictionary of string keys to Values.
Daniel Cheng8ac305b2022-02-17 00:05:11365 class BASE_EXPORT GSL_OWNER Dict {
Daniel Chenga367fe52022-02-15 18:08:48366 public:
367 using iterator = detail::dict_iterator;
368 using const_iterator = detail::const_dict_iterator;
369
370 Dict();
371
372 Dict(Dict&&) noexcept;
373 Dict& operator=(Dict&&) noexcept;
374
375 // Deleted to prevent accidental copying.
376 Dict(const Dict&) = delete;
377 Dict& operator=(const Dict&) = delete;
378
379 ~Dict();
380
381 // TODO(dcheng): Probably need to allow construction from a pair of
382 // iterators for now due to the prevalence of DictStorage.
383
384 // Returns true if there are no entries in this dictionary and false
385 // otherwise.
386 bool empty() const;
387
388 // Returns the number of entries in this dictionary.
389 size_t size() const;
390
391 // Returns an iterator to the first entry in this dictionary.
392 iterator begin();
393 const_iterator begin() const;
394 const_iterator cbegin() const;
395
396 // Returns an iterator following the last entry in this dictionary. May not
397 // be dereferenced.
398 iterator end();
399 const_iterator end() const;
400 const_iterator cend() const;
401
402 // Returns true if `key` is an entry in this dictionary.
403 bool contains(base::StringPiece key) const;
404
405 // Removes all entries from this dictionary.
406 void clear();
407
408 // Removes the entry referenced by `pos` in this dictionary and returns an
409 // iterator to the entry following the removed entry.
410 iterator erase(iterator pos);
411 iterator erase(const_iterator pos);
412
413 // Creates a deep copy of this dictionary.
414 Dict Clone() const;
415
416 // Merges the entries from `dict` into this dictionary. If an entry with the
417 // same key exists in this dictionary and `dict`:
418 // - if both entries are dictionaries, they will be recursively merged
419 // - otherwise, the already-existing entry in this dictionary will be
420 // overwritten with the entry from `dict`.
421 void Merge(const Dict& dict);
422
423 // Finds the entry corresponding to `key` in this dictionary. Returns
424 // nullptr if there is no such entry.
425 const Value* Find(StringPiece key) const;
426 Value* Find(StringPiece key);
427
428 // Similar to `Find()` above, but returns `absl::nullopt`/`nullptr` if the
429 // type of the entry does not match. `bool`, `int`, and `double` are
430 // returned in a wrapped `absl::optional`; blobs, `Value::Dict`, and
431 // `Value::List` are returned by pointer.
432 absl::optional<bool> FindBool(StringPiece key) const;
433 absl::optional<int> FindInt(StringPiece key) const;
434 // Returns a non-null value for both `Value::Type::DOUBLE` and
435 // `Value::Type::INT`, converting the latter to a double.
436 absl::optional<double> FindDouble(StringPiece key) const;
437 const std::string* FindString(StringPiece key) const;
438 std::string* FindString(StringPiece key);
439 const BlobStorage* FindBlob(StringPiece key) const;
440 const Dict* FindDict(StringPiece key) const;
441 Dict* FindDict(StringPiece key);
442 const List* FindList(StringPiece key) const;
443 List* FindList(StringPiece key);
444
445 // Sets an entry with `key` and `value` in this dictionary, overwriting any
446 // existing entry with the same `key`. Returns a pointer to the set `value`.
447 Value* Set(StringPiece key, Value&& value);
448 Value* Set(StringPiece key, bool value);
449 template <typename T>
450 Value* Set(StringPiece, const T*) = delete;
451 Value* Set(StringPiece key, int value);
452 Value* Set(StringPiece key, double value);
453 Value* Set(StringPiece key, StringPiece value);
454 Value* Set(StringPiece key, StringPiece16 value);
455 Value* Set(StringPiece key, const char* value);
456 Value* Set(StringPiece key, const char16_t* value);
457 Value* Set(StringPiece key, std::string&& value);
458 Value* Set(StringPiece key, BlobStorage&& value);
459 Value* Set(StringPiece key, Dict&& value);
460 Value* Set(StringPiece key, List&& value);
461
462 // Removes the entry corresponding to `key` from this dictionary. Returns
463 // true if an entry was removed or false otherwise.
464 bool Remove(StringPiece key);
465
466 // Similar to `Remove()`, but returns the value corresponding to the removed
467 // entry or `absl::nullopt` otherwise.
468 absl::optional<Value> Extract(StringPiece key);
469
470 // Equivalent to the above methods but operating on paths instead of keys.
471 // A path is shorthand syntax for referring to a key nested inside
472 // intermediate dictionaries, with components delimited by ".". Paths may
473 // not be empty.
474 //
Daniel Cheng619653b2022-02-17 18:33:12475 // Prefer the non-path methods above when possible. Paths that have only one
476 // component (i.e. no dots in the path) should never use the path-based
477 // methods.
478 //
479 // Originally, the path-based APIs were the only way of specifying a key, so
480 // there are likely to be many legacy (and unnecessary) uses of the path
481 // APIs that do not actually require traversing nested dictionaries.
Daniel Chenga367fe52022-02-15 18:08:48482 const Value* FindByDottedPath(StringPiece path) const;
483 Value* FindByDottedPath(StringPiece path);
484
485 absl::optional<bool> FindBoolByDottedPath(StringPiece path) const;
486 absl::optional<int> FindIntByDottedPath(StringPiece path) const;
487 // Returns a non-null value for both `Value::Type::DOUBLE` and
488 // `Value::Type::INT`, converting the latter to a double.
489 absl::optional<double> FindDoubleByDottedPath(StringPiece path) const;
490 const std::string* FindStringByDottedPath(StringPiece path) const;
491 std::string* FindStringByDottedPath(StringPiece path);
492 const BlobStorage* FindBlobByDottedPath(StringPiece path) const;
493 const Dict* FindDictByDottedPath(StringPiece path) const;
494 Dict* FindDictByDottedPath(StringPiece path);
495 const List* FindListByDottedPath(StringPiece path) const;
496 List* FindListByDottedPath(StringPiece path);
497
Daniel Cheng619653b2022-02-17 18:33:12498 // Creates a new entry with a dictionary for any non-last component that is
499 // missing an entry while performing the path traversal. Will fail if any
500 // non-last component of the path refers to an already-existing entry that
501 // is not a dictionary. Returns `nullptr` on failure.
Daniel Chenga367fe52022-02-15 18:08:48502 Value* SetByDottedPath(StringPiece path, Value&& value);
503 Value* SetByDottedPath(StringPiece path, bool value);
504 template <typename T>
505 Value* SetByDottedPath(StringPiece, const T*) = delete;
506 Value* SetByDottedPath(StringPiece path, int value);
507 Value* SetByDottedPath(StringPiece path, double value);
508 Value* SetByDottedPath(StringPiece path, StringPiece value);
509 Value* SetByDottedPath(StringPiece path, StringPiece16 value);
510 Value* SetByDottedPath(StringPiece path, const char* value);
511 Value* SetByDottedPath(StringPiece path, const char16_t* value);
512 Value* SetByDottedPath(StringPiece path, std::string&& value);
513 Value* SetByDottedPath(StringPiece path, BlobStorage&& value);
514 Value* SetByDottedPath(StringPiece path, Dict&& value);
515 Value* SetByDottedPath(StringPiece path, List&& value);
516
517 bool RemoveByDottedPath(StringPiece path);
518
519 absl::optional<Value> ExtractByDottedPath(StringPiece path);
520
521 private:
522 BASE_EXPORT friend bool operator==(const Dict& lhs, const Dict& rhs);
523 BASE_EXPORT friend bool operator!=(const Dict& lhs, const Dict& rhs);
524 BASE_EXPORT friend bool operator<(const Dict& lhs, const Dict& rhs);
525 BASE_EXPORT friend bool operator>(const Dict& lhs, const Dict& rhs);
526 BASE_EXPORT friend bool operator<=(const Dict& lhs, const Dict& rhs);
527 BASE_EXPORT friend bool operator>=(const Dict& lhs, const Dict& rhs);
528
529 // For legacy access to the internal storage type.
530 friend Value;
531
532 explicit Dict(const flat_map<std::string, std::unique_ptr<Value>>& storage);
533
534 flat_map<std::string, std::unique_ptr<Value>> storage_;
535 };
536
537 // Represents a list of Values.
Daniel Cheng8ac305b2022-02-17 00:05:11538 class BASE_EXPORT GSL_OWNER List {
Daniel Chenga367fe52022-02-15 18:08:48539 public:
540 using iterator = CheckedContiguousIterator<Value>;
541 using const_iterator = CheckedContiguousConstIterator<Value>;
542
543 List();
544
545 List(List&&) noexcept;
546 List& operator=(List&&) noexcept;
547
548 // Deleted to prevent accidental copying.
549 List(const List&) = delete;
550 List& operator=(const List&) = delete;
551
552 ~List();
553
554 // TODO(dcheng): Probably need to allow construction from a pair of
555 // iterators for now due to the prevalence of ListStorage now.
556
557 // Returns true if there are no values in this list and false otherwise.
558 bool empty() const;
559
560 // Returns the number of values in this list.
561 size_t size() const;
562
563 // Returns an iterator to the first value in this list.
564 iterator begin();
565 const_iterator begin() const;
566 const_iterator cbegin() const;
567
568 // Returns an iterator following the last value in this list. May not be
569 // dereferenced.
570 iterator end();
571 const_iterator end() const;
572 const_iterator cend() const;
573
Daniel Chengc9ab0ef2022-02-18 02:34:07574 // Returns a reference to the first value in the container. Fails with
575 // `CHECK()` if the list is empty.
576 const Value& front() const;
577 Value& front();
578
579 // Returns a reference to the last value in the container. Fails with
580 // `CHECK()` if the list is empty.
581 const Value& back() const;
582 Value& back();
583
584 // Increase the capacity of the backing container, but does not change
585 // the size. Assume all existing iterators will be invalidated.
586 void reserve(size_t capacity);
587
Daniel Chenga367fe52022-02-15 18:08:48588 // Returns a reference to the value at `index` in this list. Fails with a
589 // `CHECK()` if `index >= size()`.
590 const Value& operator[](size_t index) const;
591 Value& operator[](size_t index);
592
593 // Removes all value from this list.
594 void clear();
595
Daniel Chengc9ab0ef2022-02-18 02:34:07596 // Removes the value referenced by `pos` in this list and returns an
597 // iterator to the value following the removed value.
Daniel Chenga367fe52022-02-15 18:08:48598 iterator erase(iterator pos);
599 const_iterator erase(const_iterator pos);
600
601 // Creates a deep copy of this dictionary.
602 List Clone() const;
603
604 // Appends `value` to the end of this list.
605 void Append(Value&& value);
606 void Append(bool value);
607 template <typename T>
608 void Append(const T*) = delete;
609 void Append(int value);
610 void Append(double value);
611 void Append(StringPiece value);
612 void Append(StringPiece16 value);
613 void Append(const char* value);
614 void Append(const char16_t* value);
615 void Append(std::string&& value);
616 void Append(BlobStorage&& value);
617 void Append(Dict&& value);
618 void Append(List&& value);
619
620 // Inserts `value` before `pos` in this list. Returns an iterator to the
621 // inserted value.
622 // TODO(dcheng): Should this provide the same set of overloads that Append()
623 // does?
624 iterator Insert(const_iterator pos, Value&& value);
625
626 // Erases all values equal to `value` from this list.
627 size_t EraseValue(const Value& value);
628
629 // Erases all values for which `predicate` evaluates to true from this list.
630 template <typename Predicate>
631 size_t EraseIf(Predicate predicate) {
632 return base::EraseIf(storage_, predicate);
633 }
634
635 private:
636 BASE_EXPORT friend bool operator==(const List& lhs, const List& rhs);
637 BASE_EXPORT friend bool operator!=(const List& lhs, const List& rhs);
638 BASE_EXPORT friend bool operator<(const List& lhs, const List& rhs);
639 BASE_EXPORT friend bool operator>(const List& lhs, const List& rhs);
640 BASE_EXPORT friend bool operator<=(const List& lhs, const List& rhs);
641 BASE_EXPORT friend bool operator>=(const List& lhs, const List& rhs);
642
643 // For legacy access to the internal storage type.
644 friend Value;
645
646 explicit List(const std::vector<Value>& storage);
647
648 std::vector<Value> storage_;
649 };
jdoerriee03e80f2017-02-15 08:42:14650
Daniel Cheng619653b2022-02-17 18:33:12651 // ===== DEPRECATED methods that require `type() == Type::LIST` =====
652
Jan Wilken Dörrie69a65a3a2020-01-16 15:03:30653 // Returns the Values in a list as a view. The mutable overload allows for
654 // modification of the underlying values, but does not allow changing the
Daniel Cheng60e6b2d2022-02-05 01:08:46655 // structure of the list. If this is desired, use `TakeListDeprecated()`,
656 // perform the operations, and return the list back to the Value via move
657 // assignment.
Daniel Cheng619653b2022-02-17 18:33:12658 //
659 // DEPRECATED: prefer direct use `base::Value::List` where possible, or
660 // `GetList()` otherwise.
Daniel Cheng773ce4502022-01-28 15:25:06661 DeprecatedListView GetListDeprecated();
662 DeprecatedConstListView GetListDeprecated() const;
jdoerrie2b7d0fcd2017-04-19 07:15:38663
Lei Zhang20b21af82020-08-10 18:31:58664 // Transfers ownership of the underlying list to the caller. Subsequent
Daniel Cheng60e6b2d2022-02-05 01:08:46665 // calls to `GetListDeprecated()` will return an empty list.
Daniel Cheng619653b2022-02-17 18:33:12666 //
667 // DEPRECATED: prefer direct use of `base::Value::List` where possible, or
668 // `std::move(value.GetList())` otherwise.
Daniel Cheng773ce4502022-01-28 15:25:06669 DeprecatedListStorage TakeListDeprecated() &&;
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05670
Gabriel Charetteb49d73a2021-05-05 20:05:59671 // Appends `value` to the end of the list.
Daniel Cheng619653b2022-02-17 18:33:12672 //
673 // DEPRECATED: prefer `Value::List::Append()`.
Jan Wilken Dörrie55b0b2b2019-09-10 05:40:24674 void Append(Value&& value);
Daniel Cheng619653b2022-02-17 18:33:12675 // DEPRECATED: prefer `Value::List::Append()`.
676 void Append(bool value);
677 template <typename T>
678 void Append(const T* ptr) = delete;
679 // DEPRECATED: prefer `Value::List::Append()`.
680 void Append(int value);
681 // DEPRECATED: prefer `Value::List::Append()`.
682 void Append(double value);
683 // DEPRECATED: prefer `Value::List::Append()`.
684 void Append(StringPiece value);
685 // DEPRECATED: prefer `Value::List::Append()`.
686 void Append(StringPiece16 value);
687 // DEPRECATED: prefer `Value::List::Append()`.
688 void Append(const char* value);
689 // DEPRECATED: prefer `Value::List::Append()`.
690 void Append(const char16_t* value);
691 // DEPRECATED: prefer `Value::List::Append()`.
692 void Append(std::string&& value);
Jan Wilken Dörrie55b0b2b2019-09-10 05:40:24693
Gabriel Charetteb49d73a2021-05-05 20:05:59694 // Inserts `value` before `pos`.
Daniel Cheng619653b2022-02-17 18:33:12695 //
696 // DEPRECATED: prefer `Value::List::Insert()`.
Jan Wilken Dörrie9065545e12019-10-30 10:44:51697 CheckedContiguousIterator<Value> Insert(
698 CheckedContiguousConstIterator<Value> pos,
699 Value&& value);
700
Gabriel Charetteb49d73a2021-05-05 20:05:59701 // Erases the Value pointed to by `iter`. Returns false if `iter` is out of
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05702 // bounds.
Daniel Cheng619653b2022-02-17 18:33:12703 //
704 // DEPRECATED: prefer `Value::List::erase(iter)`.
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05705 bool EraseListIter(CheckedContiguousConstIterator<Value> iter);
706
Gabriel Charetteb49d73a2021-05-05 20:05:59707 // Erases all Values that compare equal to `val`. Returns the number of
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05708 // deleted Values.
Daniel Cheng619653b2022-02-17 18:33:12709 //
710 // DEPRECATED: prefer `Value::List::EraseValue(val)`.
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05711 size_t EraseListValue(const Value& val);
712
Gabriel Charetteb49d73a2021-05-05 20:05:59713 // Erases all Values for which `pred` returns true. Returns the number of
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05714 // deleted Values.
Daniel Cheng619653b2022-02-17 18:33:12715 //
716 // DEPRECATED: prefer `Value::List::EraseIf(pred)`.
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05717 template <typename Predicate>
718 size_t EraseListValueIf(Predicate pred) {
Jan Wilken Dörrie79d022142020-08-19 18:18:32719 return base::EraseIf(list(), pred);
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05720 }
721
Jan Wilken Dörrie02577a22019-12-04 14:27:39722 // Erases all Values from the list.
Daniel Cheng619653b2022-02-17 18:33:12723 //
724 // DEPRECATED: prefer `Value::List::clear()`.
Jan Wilken Dörrie02577a22019-12-04 14:27:39725 void ClearList();
726
Daniel Cheng619653b2022-02-17 18:33:12727 // ===== DEPRECATED methods that require `type() == Type::DICT` =====
728
Gabriel Charetteb49d73a2021-05-05 20:05:59729 // `FindKey` looks up `key` in the underlying dictionary. If found, it returns
jdoerrie78ab7a22017-08-17 19:04:45730 // a pointer to the element. Otherwise it returns nullptr.
jdoerrie78ab7a22017-08-17 19:04:45731 //
Daniel Cheng619653b2022-02-17 18:33:12732 // DEPRECATED: prefer `Value::Dict::Find()`.
jdoerrie78ab7a22017-08-17 19:04:45733 Value* FindKey(StringPiece key);
734 const Value* FindKey(StringPiece key) const;
jdoerrie44efa9d2017-07-14 14:47:20735
Gabriel Charetteb49d73a2021-05-05 20:05:59736 // `FindKeyOfType` is similar to `FindKey`, but it also requires the found
737 // value to have type `type`. If no type is found, or the found value is of a
jdoerrie78ab7a22017-08-17 19:04:45738 // different type nullptr is returned.
jdoerrie78ab7a22017-08-17 19:04:45739 //
Daniel Cheng619653b2022-02-17 18:33:12740 // DEPRECATED: prefer `Value::Dict::FindBool()`, `Value::Dict::FindInt()`, et
741 // cetera.
jdoerrie78ab7a22017-08-17 19:04:45742 Value* FindKeyOfType(StringPiece key, Type type);
743 const Value* FindKeyOfType(StringPiece key, Type type) const;
jdoerrie44efa9d2017-07-14 14:47:20744
Daniel Cheng619653b2022-02-17 18:33:12745 // These are convenience forms of `FindKey`. They return `absl::nullopt` or
746 // `nullptr` if the value is not found or doesn't have the type specified in
747 // the function's name.
748 //
749 // DEPRECATED: prefer `Value::Dict::FindBool()`.
Anton Bikineev7dd58ad2021-05-18 01:01:39750 absl::optional<bool> FindBoolKey(StringPiece key) const;
Daniel Cheng619653b2022-02-17 18:33:12751 // DEPRECATED: prefer `Value::Dict::FindInt()`.
Anton Bikineev7dd58ad2021-05-18 01:01:39752 absl::optional<int> FindIntKey(StringPiece key) const;
Daniel Cheng619653b2022-02-17 18:33:12753 // Returns a non-null value for both `Value::Type::DOUBLE` and
754 // `Value::Type::INT`, converting the latter to a double.
755 //
756 // DEPRECATED: prefer `Value::Dict::FindDouble()`.
Anton Bikineev7dd58ad2021-05-18 01:01:39757 absl::optional<double> FindDoubleKey(StringPiece key) const;
Daniel Cheng619653b2022-02-17 18:33:12758 // DEPRECATED: prefer `Value::Dict::FindString()`.
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16759 const std::string* FindStringKey(StringPiece key) const;
Dominic Battre08cbe972019-07-31 03:57:19760 std::string* FindStringKey(StringPiece key);
Daniel Cheng619653b2022-02-17 18:33:12761 // DEPRECATED: prefer `Value::Dict::FindBlob()`.
David 'Digit' Turnerfca8c4b52019-03-26 11:14:06762 const BlobStorage* FindBlobKey(StringPiece key) const;
Daniel Cheng619653b2022-02-17 18:33:12763 // DEPRECATED: prefer `Value::Dict::FindDict()`.
David 'Digit' Turnerfca8c4b52019-03-26 11:14:06764 const Value* FindDictKey(StringPiece key) const;
765 Value* FindDictKey(StringPiece key);
Daniel Cheng619653b2022-02-17 18:33:12766 // DEPRECATED: prefer `Value::Dict::FindList()`.
David 'Digit' Turnerfca8c4b52019-03-26 11:14:06767 const Value* FindListKey(StringPiece key) const;
768 Value* FindListKey(StringPiece key);
769
Gabriel Charetteb49d73a2021-05-05 20:05:59770 // `SetKey` looks up `key` in the underlying dictionary and sets the mapped
771 // value to `value`. If `key` could not be found, a new element is inserted.
jdoerrie78ab7a22017-08-17 19:04:45772 // A pointer to the modified item is returned.
jdoerrie78ab7a22017-08-17 19:04:45773 //
Daniel Cheng619653b2022-02-17 18:33:12774 // Note: Prefer `Set<Type>Key()` if the input is not already a `Value`.
775 //
776 // DEPRECATED: Prefer `Value::Dict::Set()`.
Sergey Abbakumov0e1215882019-03-15 22:32:16777 Value* SetKey(StringPiece key, Value&& value);
jdoerrie44efa9d2017-07-14 14:47:20778
Gabriel Charetteb49d73a2021-05-05 20:05:59779 // `Set`Type>Key` looks up `key` in the underlying dictionary and associates a
780 // corresponding Value() constructed from the second parameter. Compared to
781 // `SetKey()`, this avoids un-necessary temporary `Value()` creation, as well
David 'Digit' Turnere169e6c2019-03-28 22:06:29782 // ambiguities in the value type.
Daniel Cheng619653b2022-02-17 18:33:12783 //
784 // DEPRECATED: Prefer `Value::Dict::Set()`.
David 'Digit' Turnere169e6c2019-03-28 22:06:29785 Value* SetBoolKey(StringPiece key, bool val);
Daniel Cheng619653b2022-02-17 18:33:12786 // DEPRECATED: Prefer `Value::Dict::Set()`.
David 'Digit' Turnere169e6c2019-03-28 22:06:29787 Value* SetIntKey(StringPiece key, int val);
Daniel Cheng619653b2022-02-17 18:33:12788 // DEPRECATED: Prefer `Value::Dict::Set()`.
David 'Digit' Turnere169e6c2019-03-28 22:06:29789 Value* SetDoubleKey(StringPiece key, double val);
Daniel Cheng619653b2022-02-17 18:33:12790 // DEPRECATED: Prefer `Value::Dict::Set()`.
David 'Digit' Turnere169e6c2019-03-28 22:06:29791 Value* SetStringKey(StringPiece key, StringPiece val);
Daniel Cheng619653b2022-02-17 18:33:12792 // DEPRECATED: Prefer `Value::Dict::Set()`.
Jan Wilken Dörrie293405a2020-05-12 19:45:11793 Value* SetStringKey(StringPiece key, StringPiece16 val);
Daniel Cheng619653b2022-02-17 18:33:12794 // DEPRECATED: Prefer `Value::Dict::Set()`.
David 'Digit' Turnere169e6c2019-03-28 22:06:29795 Value* SetStringKey(StringPiece key, const char* val);
Daniel Cheng619653b2022-02-17 18:33:12796 // DEPRECATED: Prefer `Value::Dict::Set()`.
David 'Digit' Turnere169e6c2019-03-28 22:06:29797 Value* SetStringKey(StringPiece key, std::string&& val);
David 'Digit' Turnere169e6c2019-03-28 22:06:29798
Gabriel Charetteb49d73a2021-05-05 20:05:59799 // This attempts to remove the value associated with `key`. In case of
jdoerriec209c7d2019-04-05 09:51:46800 // failure, e.g. the key does not exist, false is returned and the underlying
Gabriel Charetteb49d73a2021-05-05 20:05:59801 // dictionary is not changed. In case of success, `key` is deleted from the
jdoerriec209c7d2019-04-05 09:51:46802 // dictionary and the method returns true.
jdoerrie64783162017-09-04 16:33:32803 //
Daniel Cheng619653b2022-02-17 18:33:12804 // Deprecated: Prefer `Value::Dict::Remove()`.
jdoerrie64783162017-09-04 16:33:32805 bool RemoveKey(StringPiece key);
806
Gabriel Charetteb49d73a2021-05-05 20:05:59807 // This attempts to extract the value associated with `key`. In case of
jdoerriec209c7d2019-04-05 09:51:46808 // failure, e.g. the key does not exist, nullopt is returned and the
Gabriel Charetteb49d73a2021-05-05 20:05:59809 // underlying dictionary is not changed. In case of success, `key` is deleted
jdoerriec209c7d2019-04-05 09:51:46810 // from the dictionary and the method returns the extracted Value.
jdoerriec209c7d2019-04-05 09:51:46811 //
Daniel Cheng619653b2022-02-17 18:33:12812 // DEPRECATED: Prefer `Value::Dict::Extract()`.
Anton Bikineev7dd58ad2021-05-18 01:01:39813 absl::optional<Value> ExtractKey(StringPiece key);
jdoerriec209c7d2019-04-05 09:51:46814
Brett Wilsond16cf4ee2017-08-03 00:08:27815 // Searches a hierarchy of dictionary values for a given value. If a path
816 // of dictionaries exist, returns the item at that path. If any of the path
817 // components do not exist or if any but the last path components are not
Daniel Cheng619653b2022-02-17 18:33:12818 // dictionaries, returns nullptr. The type of the leaf Value is not checked.
Brett Wilsond16cf4ee2017-08-03 00:08:27819 //
David 'Digit' Turner43ce6492019-04-04 16:04:44820 // This version takes a StringPiece for the path, using dots as separators.
Daniel Cheng619653b2022-02-17 18:33:12821 //
822 // DEPRECATED: Prefer `Value::Dict::FindByDottedPath()`.
David 'Digit' Turner43ce6492019-04-04 16:04:44823 Value* FindPath(StringPiece path);
824 const Value* FindPath(StringPiece path) const;
825
826 // There are also deprecated versions that take the path parameter
827 // as either a std::initializer_list<StringPiece> or a
828 // span<const StringPiece>. The latter is useful to use a
829 // std::vector<std::string> as a parameter but creates huge dynamic
jdoerriec209c7d2019-04-05 09:51:46830 // allocations and should be avoided!
Gabriel Charetteb49d73a2021-05-05 20:05:59831 // Note: If there is only one component in the path, use `FindKey()` instead.
jdoerriec209c7d2019-04-05 09:51:46832 //
David 'Digit' Turner43ce6492019-04-04 16:04:44833 // Example:
jdoerriecd022242017-08-23 08:38:27834 // std::vector<StringPiece> components = ...
835 // auto* found = FindPath(components);
Daniel Cheng619653b2022-02-17 18:33:12836 //
837 // DEPRECATED: These are not common, and there is no currently planned
838 // replacement.
jdoerriecd022242017-08-23 08:38:27839 Value* FindPath(std::initializer_list<StringPiece> path);
840 Value* FindPath(span<const StringPiece> path);
841 const Value* FindPath(std::initializer_list<StringPiece> path) const;
842 const Value* FindPath(span<const StringPiece> path) const;
Brett Wilsond16cf4ee2017-08-03 00:08:27843
Lei Zhange0fc6f02017-10-27 00:27:23844 // Like FindPath() but will only return the value if the leaf Value type
Brett Wilsond16cf4ee2017-08-03 00:08:27845 // matches the given type. Will return nullptr otherwise.
Gabriel Charetteb49d73a2021-05-05 20:05:59846 // Note: Prefer `Find<Type>Path()` for simple values.
Lei Zhange0fc6f02017-10-27 00:27:23847 //
Gabriel Charetteb49d73a2021-05-05 20:05:59848 // Note: If there is only one component in the path, use `FindKeyOfType()`
David 'Digit' Turner43ce6492019-04-04 16:04:44849 // instead for slightly better performance.
Daniel Cheng619653b2022-02-17 18:33:12850 //
851 // DEPRECATED: Use `Value::Dict::FindBoolByDottedPath()`,
852 // `Value::Dict::FindIntByDottedPath()`, et cetera.
David 'Digit' Turner43ce6492019-04-04 16:04:44853 Value* FindPathOfType(StringPiece path, Type type);
854 const Value* FindPathOfType(StringPiece path, Type type) const;
855
856 // Convenience accessors used when the expected type of a value is known.
857 // Similar to Find<Type>Key() but accepts paths instead of keys.
Daniel Cheng619653b2022-02-17 18:33:12858 //
859 // DEPRECATED: Use `Value::Dict::FindBoolByDottedPath()`, or
860 // `Value::Dict::FindBool()` if the path only has one component, i.e. has no
861 // dots.
Anton Bikineev7dd58ad2021-05-18 01:01:39862 absl::optional<bool> FindBoolPath(StringPiece path) const;
Daniel Cheng619653b2022-02-17 18:33:12863 // DEPRECATED: Use `Value::Dict::FindIntByDottedPath()`, or
864 // `Value::Dict::FindInt()` if the path only has one component, i.e. has no
865 // dots.
Anton Bikineev7dd58ad2021-05-18 01:01:39866 absl::optional<int> FindIntPath(StringPiece path) const;
Daniel Cheng619653b2022-02-17 18:33:12867 // DEPRECATED: Use `Value::Dict::FindDoubleByDottedPath()`, or
868 // `Value::Dict::FindDouble()` if the path only has one component, i.e. has no
869 // dots.
Anton Bikineev7dd58ad2021-05-18 01:01:39870 absl::optional<double> FindDoublePath(StringPiece path) const;
Daniel Cheng619653b2022-02-17 18:33:12871 // DEPRECATED: Use `Value::Dict::FindStringByDottedPath()`, or
872 // `Value::Dict::FindString()` if the path only has one component, i.e. has no
873 // dots.
David 'Digit' Turner43ce6492019-04-04 16:04:44874 const std::string* FindStringPath(StringPiece path) const;
Dominic Battre08cbe972019-07-31 03:57:19875 std::string* FindStringPath(StringPiece path);
Daniel Cheng619653b2022-02-17 18:33:12876 // DEPRECATED: Use `Value::Dict::FindBlobByDottedPath()`, or
877 // `Value::Dict::FindBlob()` if the path only has one component, i.e. has no
878 // dots.
David 'Digit' Turner43ce6492019-04-04 16:04:44879 const BlobStorage* FindBlobPath(StringPiece path) const;
Daniel Cheng619653b2022-02-17 18:33:12880 // DEPRECATED: Use `Value::Dict::FindDictByDottedPath()`, or
881 // `Value::Dict::FindDict()` if the path only has one component, i.e. has no
882 // dots.
David 'Digit' Turner43ce6492019-04-04 16:04:44883 Value* FindDictPath(StringPiece path);
884 const Value* FindDictPath(StringPiece path) const;
Daniel Cheng619653b2022-02-17 18:33:12885 // DEPRECATED: Use `Value::Dict::FindListByDottedPath()`, or
886 // `Value::Dict::FindList()` if the path only has one component, i.e. has no
887 // dots.
David 'Digit' Turner43ce6492019-04-04 16:04:44888 Value* FindListPath(StringPiece path);
889 const Value* FindListPath(StringPiece path) const;
890
891 // The following forms are deprecated too, use the ones that take the path
892 // as a single StringPiece instead.
Daniel Cheng619653b2022-02-17 18:33:12893 //
894 // DEPRECATED: These are not common, and there is no currently planned
895 // replacement.
jdoerriecd022242017-08-23 08:38:27896 Value* FindPathOfType(std::initializer_list<StringPiece> path, Type type);
897 Value* FindPathOfType(span<const StringPiece> path, Type type);
898 const Value* FindPathOfType(std::initializer_list<StringPiece> path,
Brett Wilsond16cf4ee2017-08-03 00:08:27899 Type type) const;
jdoerriecd022242017-08-23 08:38:27900 const Value* FindPathOfType(span<const StringPiece> path, Type type) const;
Brett Wilsond16cf4ee2017-08-03 00:08:27901
902 // Sets the given path, expanding and creating dictionary keys as necessary.
903 //
jdoerrie64783162017-09-04 16:33:32904 // If the current value is not a dictionary, the function returns nullptr. If
905 // path components do not exist, they will be created. If any but the last
906 // components matches a value that is not a dictionary, the function will fail
907 // (it will not overwrite the value) and return nullptr. The last path
908 // component will be unconditionally overwritten if it exists, and created if
909 // it doesn't.
Brett Wilsond16cf4ee2017-08-03 00:08:27910 //
Gabriel Charetteb49d73a2021-05-05 20:05:59911 // Note: If there is only one component in the path, use `SetKey()` instead.
912 // Note: Using `Set<Type>Path()` might be more convenient and efficient.
Daniel Cheng619653b2022-02-17 18:33:12913 //
914 // DEPRECATED: Use `Value::Dict::SetByDottedPath()`.
David 'Digit' Turner43ce6492019-04-04 16:04:44915 Value* SetPath(StringPiece path, Value&& value);
916
917 // These setters are more convenient and efficient than the corresponding
918 // SetPath(...) call.
Daniel Cheng619653b2022-02-17 18:33:12919 //
920 // DEPRECATED: Use `Value::Dict::SetByDottedPath()`.
David 'Digit' Turner43ce6492019-04-04 16:04:44921 Value* SetBoolPath(StringPiece path, bool value);
Daniel Cheng619653b2022-02-17 18:33:12922 // DEPRECATED: Use `Value::Dict::SetByDottedPath()`.
David 'Digit' Turner43ce6492019-04-04 16:04:44923 Value* SetIntPath(StringPiece path, int value);
Daniel Cheng619653b2022-02-17 18:33:12924 // DEPRECATED: Use `Value::Dict::SetByDottedPath()`.
David 'Digit' Turner43ce6492019-04-04 16:04:44925 Value* SetDoublePath(StringPiece path, double value);
Daniel Cheng619653b2022-02-17 18:33:12926 // DEPRECATED: Use `Value::Dict::SetByDottedPath()`.
David 'Digit' Turner43ce6492019-04-04 16:04:44927 Value* SetStringPath(StringPiece path, StringPiece value);
Daniel Cheng619653b2022-02-17 18:33:12928 // DEPRECATED: Use `Value::Dict::SetByDottedPath()`.
David 'Digit' Turner43ce6492019-04-04 16:04:44929 Value* SetStringPath(StringPiece path, const char* value);
Daniel Cheng619653b2022-02-17 18:33:12930 // DEPRECATED: Use `Value::Dict::SetByDottedPath()`.
David 'Digit' Turner43ce6492019-04-04 16:04:44931 Value* SetStringPath(StringPiece path, std::string&& value);
Daniel Cheng619653b2022-02-17 18:33:12932 // DEPRECATED: Use `Value::Dict::SetByDottedPath()`.
David 'Digit' Turner43ce6492019-04-04 16:04:44933 Value* SetStringPath(StringPiece path, StringPiece16 value);
934
Daniel Cheng619653b2022-02-17 18:33:12935 // DEPRECATED: Use `Value::Dict::SetByDottedPath()`.
Sergey Abbakumov0e1215882019-03-15 22:32:16936 Value* SetPath(std::initializer_list<StringPiece> path, Value&& value);
937 Value* SetPath(span<const StringPiece> path, Value&& value);
Brett Wilsond16cf4ee2017-08-03 00:08:27938
jdoerrie64783162017-09-04 16:33:32939 // Tries to remove a Value at the given path.
940 //
jdoerriec209c7d2019-04-05 09:51:46941 // If the current value is not a dictionary or any path component does not
jdoerrie64783162017-09-04 16:33:32942 // exist, this operation fails, leaves underlying Values untouched and returns
Gabriel Charetteb49d73a2021-05-05 20:05:59943 // `false`. In case intermediate dictionaries become empty as a result of this
jdoerrie64783162017-09-04 16:33:32944 // path removal, they will be removed as well.
Song Fangzhen09e06912021-07-21 15:01:28945 // Note: If there is only one component in the path, use `RemoveKey()`
Gabriel Charetteb49d73a2021-05-05 20:05:59946 // instead.
jdoerrie64783162017-09-04 16:33:32947 //
Daniel Cheng619653b2022-02-17 18:33:12948 // DEPRECATED: Use `Value::Dict::RemoveByDottedPath()`.
David 'Digit' Turner43ce6492019-04-04 16:04:44949 bool RemovePath(StringPiece path);
950
jdoerriec209c7d2019-04-05 09:51:46951 // Tries to extract a Value at the given path.
952 //
953 // If the current value is not a dictionary or any path component does not
954 // exist, this operation fails, leaves underlying Values untouched and returns
955 // nullopt. In case intermediate dictionaries become empty as a result of this
956 // path removal, they will be removed as well. Returns the extracted value on
957 // success.
Gabriel Charetteb49d73a2021-05-05 20:05:59958 // Note: If there is only one component in the path, use `ExtractKey()`
959 // instead.
jdoerriec209c7d2019-04-05 09:51:46960 //
Daniel Cheng619653b2022-02-17 18:33:12961 // DEPRECATED: Use `Value::Dict::ExtractByDottedPath()`.
Anton Bikineev7dd58ad2021-05-18 01:01:39962 absl::optional<Value> ExtractPath(StringPiece path);
jdoerriec209c7d2019-04-05 09:51:46963
jdoerrie78ab7a22017-08-17 19:04:45964 using dict_iterator_proxy = detail::dict_iterator_proxy;
965 using const_dict_iterator_proxy = detail::const_dict_iterator_proxy;
jdoerrie44efa9d2017-07-14 14:47:20966
Gabriel Charetteb49d73a2021-05-05 20:05:59967 // `DictItems` returns a proxy object that exposes iterators to the underlying
jdoerrie44efa9d2017-07-14 14:47:20968 // dictionary. These are intended for iteration over all items in the
969 // dictionary and are compatible with for-each loops and standard library
970 // algorithms.
David Van Cleve373381d2020-01-07 18:16:13971 //
Gabriel Charetteb49d73a2021-05-05 20:05:59972 // Unlike with std::map, a range-for over the non-const version of
973 // `DictItems()` will range over items of type
974 // `pair<const std::string&, Value&>`, so code of the form
David Van Cleve373381d2020-01-07 18:16:13975 // for (auto kv : my_value.DictItems())
976 // Mutate(kv.second);
Gabriel Charetteb49d73a2021-05-05 20:05:59977 // will actually alter `my_value` in place (if it isn't const).
David Van Cleve373381d2020-01-07 18:16:13978 //
Daniel Cheng619653b2022-02-17 18:33:12979 // DEPRECATED: Use a range-based for loop over `base::Value::Dict` directly
980 // instead.
jdoerrie44efa9d2017-07-14 14:47:20981 dict_iterator_proxy DictItems();
982 const_dict_iterator_proxy DictItems() const;
983
Jan Wilken Dörrief961a372020-11-02 20:30:34984 // Transfers ownership of the underlying dict to the caller. Subsequent
985 // calls to DictItems() will return an empty dict.
Daniel Cheng619653b2022-02-17 18:33:12986 //
987 // DEPRECATED: prefer direct use of `base::Value::Dict` where possible, or
988 // `std::move(value.GetDict())` otherwise.
Daniel Cheng773ce4502022-01-28 15:25:06989 DeprecatedDictStorage TakeDictDeprecated() &&;
Jan Wilken Dörrief961a372020-11-02 20:30:34990
Daniel Cheng619653b2022-02-17 18:33:12991 // DEPRECATED: prefer `Value::Dict::size()`.
Lei Zhange823c512018-05-07 20:27:30992 size_t DictSize() const;
Daniel Cheng619653b2022-02-17 18:33:12993
994 // DEPRECATED: prefer `Value::Dict::empty()`.
Lei Zhange823c512018-05-07 20:27:30995 bool DictEmpty() const;
Daniel Cheng619653b2022-02-17 18:33:12996
997 // DEPRECATED: prefer `Value::Dict::clear()`.
Panos Astithas0532a862020-10-29 04:15:07998 void DictClear();
Lei Zhange823c512018-05-07 20:27:30999
Gabriel Charetteb49d73a2021-05-05 20:05:591000 // Merge `dictionary` into this value. This is done recursively, i.e. any
jdoerriec1091282018-08-01 17:28:121001 // sub-dictionaries will be merged as well. In case of key collisions, the
1002 // passed in dictionary takes precedence and data already present will be
Gabriel Charetteb49d73a2021-05-05 20:05:591003 // replaced. Values within `dictionary` are deep-copied, so `dictionary` may
jdoerriec1091282018-08-01 17:28:121004 // be freed any time after this call.
Gabriel Charetteb49d73a2021-05-05 20:05:591005 // Note: This requires that `type()` and `dictionary->type()` is
Daniel Chenga367fe52022-02-15 18:08:481006 // Type::DICT.
Daniel Cheng619653b2022-02-17 18:33:121007 //
1008 // DEPRECATED: prefer `Value::Dict::Merge()`.
jdoerriec1091282018-08-01 17:28:121009 void MergeDictionary(const Value* dictionary);
1010
[email protected]2f03f532013-07-17 08:43:331011 // These methods allow the convenient retrieval of the contents of the Value.
1012 // If the current object can be converted into the given type, the value is
Gabriel Charetteb49d73a2021-05-05 20:05:591013 // returned through the `out_value` parameter and true is returned;
1014 // otherwise, false is returned and `out_value` is unchanged.
lukaszad1485da72016-11-01 21:49:461015 // ListValue::From is the equivalent for std::unique_ptr conversions.
Daniel Cheng619653b2022-02-17 18:33:121016 //
1017 // DEPRECATED: prefer direct use `base::Value::List` where possible, or
1018 // `GetIfList()` otherwise.
jdoerrie8e945542017-02-17 13:54:491019 bool GetAsList(ListValue** out_value);
1020 bool GetAsList(const ListValue** out_value) const;
lukaszad1485da72016-11-01 21:49:461021 // DictionaryValue::From is the equivalent for std::unique_ptr conversions.
Daniel Cheng619653b2022-02-17 18:33:121022 //
1023 // DEPRECATED: prefer direct use `base::Value::Dict` where possible, or
1024 // `GetIfDict()` otherwise.
jdoerrie8e945542017-02-17 13:54:491025 bool GetAsDictionary(DictionaryValue** out_value);
1026 bool GetAsDictionary(const DictionaryValue** out_value) const;
[email protected]2f03f532013-07-17 08:43:331027 // Note: Do not add more types. See the file-level comment above for why.
initial.commitd7cae122008-07-26 21:49:381028
1029 // This creates a deep copy of the entire Value tree, and returns a pointer
jdoerrie05eb3162017-02-01 10:36:561030 // to the copy. The caller gets ownership of the copy, of course.
[email protected]16f47e082011-01-18 02:16:591031 // Subclasses return their own type directly in their overrides;
1032 // this works because C++ supports covariant return types.
jdoerriee964d9a2017-04-05 06:44:171033 // TODO(crbug.com/646113): Delete this and migrate callsites.
Daniel Cheng619653b2022-02-17 18:33:121034 //
1035 // DEPRECATED: prefer `Value::Clone()`.
dcheng093de9b2016-04-04 21:25:511036 std::unique_ptr<Value> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:381037
jdoerrie5c1cee112017-03-28 17:52:001038 // Comparison operators so that Values can easily be used with standard
1039 // library algorithms and associative containers.
1040 BASE_EXPORT friend bool operator==(const Value& lhs, const Value& rhs);
1041 BASE_EXPORT friend bool operator!=(const Value& lhs, const Value& rhs);
1042 BASE_EXPORT friend bool operator<(const Value& lhs, const Value& rhs);
1043 BASE_EXPORT friend bool operator>(const Value& lhs, const Value& rhs);
1044 BASE_EXPORT friend bool operator<=(const Value& lhs, const Value& rhs);
1045 BASE_EXPORT friend bool operator>=(const Value& lhs, const Value& rhs);
1046
initial.commitd7cae122008-07-26 21:49:381047 // Compares if two Value objects have equal contents.
Gabriel Charetteb49d73a2021-05-05 20:05:591048 // DEPRECATED, use `operator==(const Value& lhs, const Value& rhs)` instead.
jdoerrie5c1cee112017-03-28 17:52:001049 // TODO(crbug.com/646113): Delete this and migrate callsites.
Daniel Cheng619653b2022-02-17 18:33:121050 //
1051 // DEPRECATED: prefer direct use of the equality operator instead.
jdoerrie8e945542017-02-17 13:54:491052 bool Equals(const Value* other) const;
initial.commitd7cae122008-07-26 21:49:381053
Eric Secklerf6c544f2020-06-02 10:49:211054 // Estimates dynamic memory usage. Requires tracing support
1055 // (enable_base_tracing gn flag), otherwise always returns 0. See
1056 // base/trace_event/memory_usage_estimator.h for more info.
Alexander Yashkinab504e72017-11-30 11:54:451057 size_t EstimateMemoryUsage() const;
1058
Alan Cutter2dd83032020-12-08 21:55:001059 // Serializes to a string for logging and debug purposes.
1060 std::string DebugString() const;
1061
Peter Kotwicz83a231372021-04-13 17:42:121062#if BUILDFLAG(ENABLE_BASE_TRACING)
Alexander Timine68aeb32021-04-11 23:06:211063 // Write this object into a trace.
Alexander Timinbebb2002021-04-20 15:42:241064 void WriteIntoTrace(perfetto::TracedValue) const;
Peter Kotwicz83a231372021-04-13 17:42:121065#endif // BUILDFLAG(ENABLE_BASE_TRACING)
Alexander Timine68aeb32021-04-11 23:06:211066
Daniel Cheng8ac305b2022-02-17 00:05:111067 template <typename Visitor>
1068 auto Visit(Visitor&& visitor) const {
1069 return absl::visit(std::forward<Visitor>(visitor), data_);
1070 }
1071
jdoerrie8e945542017-02-17 13:54:491072 protected:
Jan Wilken Dörrie79d022142020-08-19 18:18:321073 // Checked convenience accessors for dict and list.
Daniel Chenga367fe52022-02-15 18:08:481074 const LegacyDictStorage& dict() const { return GetDict().storage_; }
1075 LegacyDictStorage& dict() { return GetDict().storage_; }
1076 const ListStorage& list() const { return GetList().storage_; }
1077 ListStorage& list() { return GetList().storage_; }
Jan Wilken Dörrie79d022142020-08-19 18:18:321078
Jan Wilken Dörrief961a372020-11-02 20:30:341079 // Internal constructors, allowing the simplify the implementation of Clone().
1080 explicit Value(const LegacyDictStorage& storage);
1081 explicit Value(LegacyDictStorage&& storage) noexcept;
1082
Jan Wilken Dörrie79d022142020-08-19 18:18:321083 private:
Daniel Cheng8ac305b2022-02-17 00:05:111084 // For access to DoubleStorage.
1085 friend class ValueView;
1086
David 'Digit' Turner2f287312019-04-03 14:32:091087 // Special case for doubles, which are aligned to 8 bytes on some
1088 // 32-bit architectures. In this case, a simple declaration as a
1089 // double member would make the whole union 8 byte-aligned, which
1090 // would also force 4 bytes of wasted padding space before it in
1091 // the Value layout.
David 'Digit' Turner806dedb82019-03-06 17:43:111092 //
David 'Digit' Turner2f287312019-04-03 14:32:091093 // To override this, store the value as an array of 32-bit integers, and
1094 // perform the appropriate bit casts when reading / writing to it.
Daniel Cheng782d2ba32022-02-16 19:40:291095 class DoubleStorage {
1096 public:
1097 explicit DoubleStorage(double v);
1098 DoubleStorage(const DoubleStorage&) = default;
1099 DoubleStorage& operator=(const DoubleStorage&) = default;
1100
Daniel Cheng8ac305b2022-02-17 00:05:111101 // Provide an implicit conversion to double to simplify the use of visitors
1102 // with `Value::Visit()`. Otherwise, visitors would need a branch for
1103 // handling `DoubleStorage` like:
1104 //
1105 // value.Visit([] (const auto& member) {
1106 // using T = std::decay_t<decltype(member)>;
1107 // if constexpr (std::is_same_v<T, Value::DoubleStorage>) {
1108 // SomeFunction(double{member});
1109 // } else {
1110 // SomeFunction(member);
1111 // }
1112 // });
1113 operator double() const { return bit_cast<double>(v_); }
Daniel Cheng782d2ba32022-02-16 19:40:291114
1115 private:
1116 friend bool operator==(const DoubleStorage& lhs, const DoubleStorage& rhs) {
Daniel Cheng8ac305b2022-02-17 00:05:111117 return double{lhs} == double{rhs};
Daniel Cheng782d2ba32022-02-16 19:40:291118 }
1119
1120 friend bool operator!=(const DoubleStorage& lhs, const DoubleStorage& rhs) {
1121 return !(lhs == rhs);
1122 }
1123
1124 friend bool operator<(const DoubleStorage& lhs, const DoubleStorage& rhs) {
Daniel Cheng8ac305b2022-02-17 00:05:111125 return double{lhs} < double{rhs};
Daniel Cheng782d2ba32022-02-16 19:40:291126 }
1127
1128 friend bool operator>(const DoubleStorage& lhs, const DoubleStorage& rhs) {
1129 return rhs < lhs;
1130 }
1131
1132 friend bool operator<=(const DoubleStorage& lhs, const DoubleStorage& rhs) {
1133 return !(rhs < lhs);
1134 }
1135
1136 friend bool operator>=(const DoubleStorage& lhs, const DoubleStorage& rhs) {
1137 return !(lhs < rhs);
1138 }
1139
1140 alignas(4) std::array<char, sizeof(double)> v_;
1141 };
David 'Digit' Turner2f287312019-04-03 14:32:091142
Jan Wilken Dörrie79d022142020-08-19 18:18:321143 // Internal constructors, allowing the simplify the implementation of Clone().
1144 explicit Value(absl::monostate);
1145 explicit Value(DoubleStorage storage);
jdoerrie8e945542017-02-17 13:54:491146
Jan Wilken Dörrie79d022142020-08-19 18:18:321147 absl::variant<absl::monostate,
1148 bool,
1149 int,
1150 DoubleStorage,
1151 std::string,
1152 BlobStorage,
Daniel Chenga367fe52022-02-15 18:08:481153 Dict,
1154 List>
Jan Wilken Dörrie79d022142020-08-19 18:18:321155 data_;
initial.commitd7cae122008-07-26 21:49:381156};
1157
[email protected]9e4cda7332010-07-31 04:56:141158// DictionaryValue provides a key-value dictionary with (optional) "path"
1159// parsing for recursive access; see the comment at the top of the file. Keys
Gabriel Charetted7c00cbe2021-05-14 20:05:531160// are std::string's and should be UTF-8 encoded.
Daniel Cheng619653b2022-02-17 18:33:121161//
1162// DEPRECATED: prefer `Value::Dict`.
[email protected]0bea7252011-08-05 15:34:001163class BASE_EXPORT DictionaryValue : public Value {
initial.commitd7cae122008-07-26 21:49:381164 public:
Gabriel Charetteb49d73a2021-05-05 20:05:591165 // Returns `value` if it is a dictionary, nullptr otherwise.
dcheng093de9b2016-04-04 21:25:511166 static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value);
reillyg259c0a32015-09-11 00:25:541167
[email protected]3a3d47472010-07-15 21:03:541168 DictionaryValue();
Jan Wilken Dörrief961a372020-11-02 20:30:341169 explicit DictionaryValue(const LegacyDictStorage& in_dict);
1170 explicit DictionaryValue(LegacyDictStorage&& in_dict) noexcept;
[email protected]5cf906f82011-11-26 01:11:441171
initial.commitd7cae122008-07-26 21:49:381172 // Returns true if the current dictionary has a value for the given key.
Daniel Cheng619653b2022-02-17 18:33:121173 //
1174 // DEPRECATED: prefer `Value::Dict::contains()`.
dcheng16d6f532016-08-25 16:07:111175 bool HasKey(StringPiece key) const;
initial.commitd7cae122008-07-26 21:49:381176
initial.commitd7cae122008-07-26 21:49:381177 // Sets the Value associated with the given path starting from this object.
1178 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
1179 // into the next DictionaryValue down. Obviously, "." can't be used
1180 // within a key, but there are no other restrictions on keys.
1181 // If the key at any step of the way doesn't exist, or exists but isn't
1182 // a DictionaryValue, a new DictionaryValue will be created and attached
Gabriel Charetteb49d73a2021-05-05 20:05:591183 // to the path in that location. `in_value` must be non-null.
jdoerrieb94e5422017-04-28 21:52:581184 // Returns a pointer to the inserted value.
Daniel Cheng619653b2022-02-17 18:33:121185 //
1186 // DEPRECATED: prefer `Value::Dict::Set()` (if the path only has one
1187 // component, i.e. has no dots), or `Value::Dict::SetByDottedPath()`
1188 // otherwise.
jdoerrieb94e5422017-04-28 21:52:581189 Value* Set(StringPiece path, std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:381190
1191 // Convenience forms of Set(). These methods will replace any existing
1192 // value at that path, even if it has a different type.
Daniel Cheng619653b2022-02-17 18:33:121193 //
1194 // DEPRECATED: prefer `Value::Dict::Set()` (if the path only has one
1195 // component, i.e. has no dots), or `Value::Dict::SetByDottedPath()`
1196 // otherwise.
jdoerrieb94e5422017-04-28 21:52:581197 Value* SetBoolean(StringPiece path, bool in_value);
Daniel Cheng619653b2022-02-17 18:33:121198 // DEPRECATED: prefer `Value::Dict::Set()` (if the path only has one
1199 // component, i.e. has no dots), or `Value::Dict::SetByDottedPath()`
1200 // otherwise.
jdoerrieb94e5422017-04-28 21:52:581201 Value* SetInteger(StringPiece path, int in_value);
Daniel Cheng619653b2022-02-17 18:33:121202 // DEPRECATED: prefer `Value::Dict::Set()` (if the path only has one
1203 // component, i.e. has no dots), or `Value::Dict::SetByDottedPath()`
1204 // otherwise.
jdoerrieb94e5422017-04-28 21:52:581205 Value* SetDouble(StringPiece path, double in_value);
Daniel Cheng619653b2022-02-17 18:33:121206 // DEPRECATED: prefer `Value::Dict::Set()` (if the path only has one
1207 // component, i.e. has no dots), or `Value::Dict::SetByDottedPath()`
1208 // otherwise.
jdoerrieb94e5422017-04-28 21:52:581209 Value* SetString(StringPiece path, StringPiece in_value);
Daniel Cheng619653b2022-02-17 18:33:121210 // DEPRECATED: prefer `Value::Dict::Set()` (if the path only has one
1211 // component, i.e. has no dots), or `Value::Dict::SetByDottedPath()`
1212 // otherwise.
Jan Wilken Dörrie085b2aa2021-03-12 16:26:571213 Value* SetString(StringPiece path, const std::u16string& in_value);
Daniel Cheng619653b2022-02-17 18:33:121214 // DEPRECATED: prefer `Value::Dict::Set()` (if the path only has one
1215 // component, i.e. has no dots), or `Value::Dict::SetByDottedPath()`
1216 // otherwise.
jdoerrieb94e5422017-04-28 21:52:581217 ListValue* SetList(StringPiece path, std::unique_ptr<ListValue> in_value);
[email protected]4dad9ad82009-11-25 20:47:521218
1219 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
1220 // be used as paths.
Daniel Cheng619653b2022-02-17 18:33:121221 //
1222 // DEPRECATED: prefer `Value::Dict::Set()`.
jdoerrieb94e5422017-04-28 21:52:581223 Value* SetWithoutPathExpansion(StringPiece key,
1224 std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:381225
1226 // Gets the Value associated with the given path starting from this object.
1227 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
1228 // into the next DictionaryValue down. If the path can be resolved
1229 // successfully, the value for the last key in the path will be returned
Gabriel Charetteb49d73a2021-05-05 20:05:591230 // through the `out_value` parameter, and the function will return true.
1231 // Otherwise, it will return false and `out_value` will be untouched.
initial.commitd7cae122008-07-26 21:49:381232 // Note that the dictionary always owns the value that's returned.
Gabriel Charetteb49d73a2021-05-05 20:05:591233 // `out_value` is optional and will only be set if non-NULL.
Daniel Cheng619653b2022-02-17 18:33:121234 //
1235 // DEPRECATED: prefer `Value::Dict::Find()` (if the path only has one
1236 // component, i.e. has no dots), or `Value::Dict::FindByDottedPath()`
1237 // otherwise.
asvitkinedbd26533e2015-06-23 18:22:521238 bool Get(StringPiece path, const Value** out_value) const;
1239 bool Get(StringPiece path, Value** out_value);
initial.commitd7cae122008-07-26 21:49:381240
Gabriel Charetteb49d73a2021-05-05 20:05:591241 // These are convenience forms of `Get()`. The value will be retrieved
initial.commitd7cae122008-07-26 21:49:381242 // and the return value will be true if the path is valid and the value at
1243 // the end of the path can be returned in the form specified.
Gabriel Charetteb49d73a2021-05-05 20:05:591244 // `out_value` is optional and will only be set if non-NULL.
Daniel Cheng619653b2022-02-17 18:33:121245 //
1246 // DEPRECATED: prefer `Value::Dict::FindInt()` (if the path only has one
1247 // component, i.e. has no dots), or `Value::Dict::FindIntByDottedPath()`
1248 // otherwise.
dcheng16d6f532016-08-25 16:07:111249 bool GetInteger(StringPiece path, int* out_value) const;
Daniel Cheng619653b2022-02-17 18:33:121250 // DEPRECATED: prefer `Value::Dict::FindString()` (if the path only has one
1251 // component, i.e. has no dots), or `Value::Dict::FindStringByDottedPath()`
1252 // otherwise.
dcheng16d6f532016-08-25 16:07:111253 bool GetString(StringPiece path, std::string* out_value) const;
Jan Wilken Dörrie085b2aa2021-03-12 16:26:571254 bool GetString(StringPiece path, std::u16string* out_value) const;
Daniel Cheng619653b2022-02-17 18:33:121255 // DEPRECATED: prefer `Value::Dict::FindDict()` (if the path only has one
1256 // component, i.e. has no dots), or `Value::Dict::FindDictByDottedPath()`
1257 // otherwise.
Jan Wilken Dörrie79d022142020-08-19 18:18:321258 bool GetDictionary(StringPiece path, const DictionaryValue** out_value) const;
asvitkinedbd26533e2015-06-23 18:22:521259 bool GetDictionary(StringPiece path, DictionaryValue** out_value);
Daniel Cheng619653b2022-02-17 18:33:121260 // DEPRECATED: prefer `Value::Dict::FindList()` (if the path only has one
1261 // component, i.e. has no dots), or `Value::Dict::FindListByDottedPath()`
1262 // otherwise.
dcheng16d6f532016-08-25 16:07:111263 bool GetList(StringPiece path, const ListValue** out_value) const;
1264 bool GetList(StringPiece path, ListValue** out_value);
initial.commitd7cae122008-07-26 21:49:381265
Gabriel Charetteb49d73a2021-05-05 20:05:591266 // Like `Get()`, but without special treatment of '.'. This allows e.g. URLs
1267 // to be used as paths.
Gabriel Charetteb49d73a2021-05-05 20:05:591268 // DEPRECATED, use `Value::FindDictKey(key)` instead.
[email protected]a61890e2012-07-27 22:27:111269 bool GetDictionaryWithoutPathExpansion(
dcheng16d6f532016-08-25 16:07:111270 StringPiece key,
[email protected]a61890e2012-07-27 22:27:111271 const DictionaryValue** out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:591272 // DEPRECATED, use `Value::FindDictKey(key)` instead.
dcheng16d6f532016-08-25 16:07:111273 bool GetDictionaryWithoutPathExpansion(StringPiece key,
[email protected]a61890e2012-07-27 22:27:111274 DictionaryValue** out_value);
Gabriel Charetteb49d73a2021-05-05 20:05:591275 // DEPRECATED, use `Value::FindListKey(key)` instead.
dcheng16d6f532016-08-25 16:07:111276 bool GetListWithoutPathExpansion(StringPiece key,
[email protected]a61890e2012-07-27 22:27:111277 const ListValue** out_value) const;
Gabriel Charetteb49d73a2021-05-05 20:05:591278 // DEPRECATED, use `Value::FindListKey(key)` instead.
dcheng16d6f532016-08-25 16:07:111279 bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value);
[email protected]4dad9ad82009-11-25 20:47:521280
Gabriel Charetteb49d73a2021-05-05 20:05:591281 // Makes a copy of `this` but doesn't include empty dictionaries and lists in
1282 // the copy. This never returns NULL, even if `this` itself is empty.
dcheng093de9b2016-04-04 21:25:511283 std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const;
[email protected]ec330b52009-12-02 00:20:321284
Gabriel Charetteb49d73a2021-05-05 20:05:591285 // Swaps contents with the `other` dictionary.
jdoerrie8e945542017-02-17 13:54:491286 void Swap(DictionaryValue* other);
[email protected]ec5263a2011-05-10 09:23:391287
[email protected]32c0e002011-11-08 21:26:411288 // This class provides an iterator over both keys and values in the
1289 // dictionary. It can't be used to modify the dictionary.
Daniel Cheng619653b2022-02-17 18:33:121290 //
1291 // DEPRECATED: Use a range-based for loop over `base::Value::Dict` directly
1292 // instead.
[email protected]a34cc092012-08-10 12:45:351293 class BASE_EXPORT Iterator {
[email protected]32c0e002011-11-08 21:26:411294 public:
[email protected]a34cc092012-08-10 12:45:351295 explicit Iterator(const DictionaryValue& target);
vmpstre65942b2016-02-25 00:50:311296 Iterator(const Iterator& other);
[email protected]a8d94b42013-12-10 18:52:221297 ~Iterator();
[email protected]32c0e002011-11-08 21:26:411298
Lei Zhang10fce02f2021-05-14 18:45:081299 bool IsAtEnd() const { return it_ == target_.DictItems().end(); }
[email protected]32c0e002011-11-08 21:26:411300 void Advance() { ++it_; }
1301
1302 const std::string& key() const { return it_->first; }
Lei Zhang10fce02f2021-05-14 18:45:081303 const Value& value() const { return it_->second; }
[email protected]32c0e002011-11-08 21:26:411304
1305 private:
1306 const DictionaryValue& target_;
Lei Zhang10fce02f2021-05-14 18:45:081307 detail::const_dict_iterator it_;
[email protected]32c0e002011-11-08 21:26:411308 };
1309
Daniel Cheng619653b2022-02-17 18:33:121310 // DEPRECATED, use `Value::Dict::Clone()` instead.
jdoerriee964d9a2017-04-05 06:44:171311 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:491312 DictionaryValue* DeepCopy() const;
Daniel Cheng619653b2022-02-17 18:33:121313 // DEPRECATED, use `Value::Dict::Clone()` instead.
jdoerried4b852612017-06-06 11:48:431314 // TODO(crbug.com/646113): Delete this and migrate callsites.
dcheng093de9b2016-04-04 21:25:511315 std::unique_ptr<DictionaryValue> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:381316};
1317
1318// This type of Value represents a list of other Value values.
Daniel Cheng619653b2022-02-17 18:33:121319//
1320// DEPRECATED: prefer `base::Value::List`.
[email protected]0bea7252011-08-05 15:34:001321class BASE_EXPORT ListValue : public Value {
initial.commitd7cae122008-07-26 21:49:381322 public:
Jan Wilken Dörrie46992f022020-02-20 11:25:471323 using const_iterator = ListView::const_iterator;
1324 using iterator = ListView::iterator;
[email protected]a502bbe72011-01-07 18:06:451325
Gabriel Charetteb49d73a2021-05-05 20:05:591326 // Returns `value` if it is a list, nullptr otherwise.
dcheng093de9b2016-04-04 21:25:511327 static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value);
reillyg259c0a32015-09-11 00:25:541328
[email protected]3a3d47472010-07-15 21:03:541329 ListValue();
Jan Wilken Dörrie53e009b2019-09-09 14:17:411330 explicit ListValue(span<const Value> in_list);
jdoerrie52939ed2017-04-26 18:19:421331 explicit ListValue(ListStorage&& in_list) noexcept;
initial.commitd7cae122008-07-26 21:49:381332
Gabriel Charetteb49d73a2021-05-05 20:05:591333 // Convenience forms of `Get()`. Modifies `out_value` (and returns true)
[email protected]35213ce92010-04-08 19:06:151334 // only if the index is valid and the Value at that index can be returned
1335 // in the specified form.
Gabriel Charetteb49d73a2021-05-05 20:05:591336 // `out_value` is optional and will only be set if non-NULL.
Daniel Cheng619653b2022-02-17 18:33:121337 //
1338 // DEPRECATED: prefer `Value::List::operator[]` + `GetIfDict()`.
[email protected]5d30f92bf2012-08-03 08:43:371339 bool GetDictionary(size_t index, const DictionaryValue** out_value) const;
1340 bool GetDictionary(size_t index, DictionaryValue** out_value);
jdoerrie52939ed2017-04-26 18:19:421341
initial.commitd7cae122008-07-26 21:49:381342 // Appends a Value to the end of the list.
Daniel Cheng619653b2022-02-17 18:33:121343 // DEPRECATED: prefer `Value::List::Append()`.
1344 using Value::Append;
1345 // DEPRECATED: prefer `Value::List::Append()`.
dcheng093de9b2016-04-04 21:25:511346 void Append(std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:381347
Gabriel Charetteb49d73a2021-05-05 20:05:591348 // Swaps contents with the `other` list.
Daniel Cheng619653b2022-02-17 18:33:121349 //
1350 // DEPRECATED: prefer `base::Value::List` + `std::swap()`.
jdoerrie8e945542017-02-17 13:54:491351 void Swap(ListValue* other);
[email protected]8b8e7c92010-08-19 18:05:561352
Daniel Cheng619653b2022-02-17 18:33:121353 // Iteration: Use a range-based for loop over `base::Value::List` directly
1354 // instead.
initial.commitd7cae122008-07-26 21:49:381355};
1356
Daniel Cheng8ac305b2022-02-17 00:05:111357// Adapter so `Value::Dict` or `Value::List` can be directly passed to JSON
1358// serialization methods without having to clone the contents and transfer
1359// ownership of the clone to a `Value` wrapper object.
1360//
1361// Like `StringPiece` and `span<T>`, this adapter does NOT retain ownership. Any
1362// underlying object that is passed by reference (i.e. `std::string`,
1363// `Value::BlobStorage`, `Value::Dict`, `Value::List`, or `Value`) MUST remain
1364// live as long as there is a `ValueView` referencing it.
1365//
1366// While it might be nice to just use the `absl::variant` type directly, the
1367// need to use `std::reference_wrapper` makes it clunky. `absl::variant` and
1368// `std::reference_wrapper` both support implicit construction, but C++ only
1369// allows at most one user-defined conversion in an implicit conversion
1370// sequence. If this adapter and its implicit constructors did not exist,
1371// callers would need to use `std::ref` or `std::cref` to pass `Value::Dict` or
1372// `Value::List` to a function with a `ValueView` parameter.
1373class BASE_EXPORT GSL_POINTER ValueView {
1374 public:
1375 ValueView(bool value) : data_view_(value) {}
1376 ValueView(int value) : data_view_(value) {}
1377 ValueView(double value)
1378 : data_view_(absl::in_place_type_t<Value::DoubleStorage>(), value) {}
1379 ValueView(const std::string& value) : data_view_(value) {}
1380 ValueView(const Value::BlobStorage& value) : data_view_(value) {}
1381 ValueView(const Value::Dict& value) : data_view_(value) {}
1382 ValueView(const Value::List& value) : data_view_(value) {}
1383 ValueView(const Value& value);
1384
1385 template <typename Visitor>
1386 auto Visit(Visitor&& visitor) const {
1387 return absl::visit(std::forward<Visitor>(visitor), data_view_);
1388 }
1389
1390 private:
1391 using ViewType =
1392 absl::variant<absl::monostate,
1393 bool,
1394 int,
1395 Value::DoubleStorage,
1396 std::reference_wrapper<const std::string>,
1397 std::reference_wrapper<const Value::BlobStorage>,
1398 std::reference_wrapper<const Value::Dict>,
1399 std::reference_wrapper<const Value::List>>;
1400
1401 ViewType data_view_;
1402};
1403
prashhir54a994502015-03-05 09:30:571404// This interface is implemented by classes that know how to serialize
1405// Value objects.
[email protected]0bea7252011-08-05 15:34:001406class BASE_EXPORT ValueSerializer {
initial.commitd7cae122008-07-26 21:49:381407 public:
[email protected]3a3d47472010-07-15 21:03:541408 virtual ~ValueSerializer();
[email protected]abb9d0c2008-08-06 15:46:591409
Daniel Cheng8ac305b2022-02-17 00:05:111410 virtual bool Serialize(ValueView root) = 0;
prashhir54a994502015-03-05 09:30:571411};
1412
1413// This interface is implemented by classes that know how to deserialize Value
1414// objects.
1415class BASE_EXPORT ValueDeserializer {
1416 public:
1417 virtual ~ValueDeserializer();
initial.commitd7cae122008-07-26 21:49:381418
1419 // This method deserializes the subclass-specific format into a Value object.
[email protected]b4cebf82008-12-29 19:59:081420 // If the return value is non-NULL, the caller takes ownership of returned
Nigel Tao410788e2020-06-24 07:12:271421 // Value.
1422 //
Gabriel Charetteb49d73a2021-05-05 20:05:591423 // If the return value is nullptr, and if `error_code` is non-nullptr,
1424 // `*error_code` will be set to an integer value representing the underlying
Nigel Tao410788e2020-06-24 07:12:271425 // error. See "enum ErrorCode" below for more detail about the integer value.
1426 //
Gabriel Charetteb49d73a2021-05-05 20:05:591427 // If `error_message` is non-nullptr, it will be filled in with a formatted
[email protected]ba399672010-04-06 15:42:391428 // error message including the location of the error if appropriate.
dcheng093de9b2016-04-04 21:25:511429 virtual std::unique_ptr<Value> Deserialize(int* error_code,
Nigel Tao410788e2020-06-24 07:12:271430 std::string* error_message) = 0;
1431
1432 // The integer-valued error codes form four groups:
1433 // - The value 0 means no error.
1434 // - Values between 1 and 999 inclusive mean an error in the data (i.e.
1435 // content). The bytes being deserialized are not in the right format.
1436 // - Values 1000 and above mean an error in the metadata (i.e. context). The
1437 // file could not be read, the network is down, etc.
1438 // - Negative values are reserved.
Caitlin Fischeraac06dc2021-12-17 00:21:321439 //
1440 // These values are persisted to logs. Entries should not be renumbered and
1441 // numeric values should never be reused.
Nigel Tao410788e2020-06-24 07:12:271442 enum ErrorCode {
1443 kErrorCodeNoError = 0,
1444 // kErrorCodeInvalidFormat is a generic error code for "the data is not in
1445 // the right format". Subclasses of ValueDeserializer may return other
1446 // values for more specific errors.
1447 kErrorCodeInvalidFormat = 1,
1448 // kErrorCodeFirstMetadataError is the minimum value (inclusive) of the
1449 // range of metadata errors.
1450 kErrorCodeFirstMetadataError = 1000,
1451 };
1452
Gabriel Charetteb49d73a2021-05-05 20:05:591453 // The `error_code` argument can be one of the ErrorCode values, but it is
Nigel Tao410788e2020-06-24 07:12:271454 // not restricted to only being 0, 1 or 1000. Subclasses of ValueDeserializer
1455 // can define their own error code values.
1456 static inline bool ErrorCodeIsDataError(int error_code) {
1457 return (kErrorCodeInvalidFormat <= error_code) &&
1458 (error_code < kErrorCodeFirstMetadataError);
1459 }
initial.commitd7cae122008-07-26 21:49:381460};
1461
Daniel Chenga367fe52022-02-15 18:08:481462// Stream operator so Values can be pretty printed by gtest.
[email protected]e4ef8312012-09-12 03:39:351463BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
Daniel Chenga367fe52022-02-15 18:08:481464BASE_EXPORT std::ostream& operator<<(std::ostream& out,
1465 const Value::Dict& dict);
1466BASE_EXPORT std::ostream& operator<<(std::ostream& out,
1467 const Value::List& list);
[email protected]e4ef8312012-09-12 03:39:351468
Daniel Chenga367fe52022-02-15 18:08:481469// Hints for DictionaryValue and ListValue; otherwise, gtest tends to prefer the
1470// default template implementation over an upcast to Value.
[email protected]ea0ec052013-04-16 09:04:021471BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
[email protected]ea0ec052013-04-16 09:04:021472 const DictionaryValue& value) {
1473 return out << static_cast<const Value&>(value);
1474}
1475
1476BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
1477 const ListValue& value) {
1478 return out << static_cast<const Value&>(value);
1479}
1480
jdoerriedc72ee942016-12-07 15:43:281481// Stream operator so that enum class Types can be used in log statements.
1482BASE_EXPORT std::ostream& operator<<(std::ostream& out,
1483 const Value::Type& type);
1484
[email protected]f3a1c642011-07-12 19:15:031485} // namespace base
1486
[email protected]101d5422008-09-26 20:22:421487#endif // BASE_VALUES_H_