1b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Use of this source code is governed by a BSD-style license that can be
3b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// found in the LICENSE file.
4b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
5b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// This file specifies a recursive data storage class called Value intended for
6b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// storing settings and other persistable data.
7b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
8b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// A Value represents something that can be stored in JSON or passed to/from
9b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// JavaScript. As such, it is NOT a generalized variant type, since only the
10b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// types supported by JavaScript/JSON are supported.
11b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
120d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko// IN PARTICULAR this means that there is no support for int64_t or unsigned
13b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// numbers. Writing JSON with such types would violate the spec. If you need
14b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// something like this, either use a double or make a string value containing
15b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// the number you want.
16b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
17b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#ifndef BASE_VALUES_H_
18b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#define BASE_VALUES_H_
19b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
20b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <stddef.h>
210d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko#include <stdint.h>
22b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
23b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <iosfwd>
24b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <map>
2594ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez#include <memory>
26b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <string>
27b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <utility>
28b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <vector>
29b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
30b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/base_export.h"
31b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/compiler_specific.h"
320d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko#include "base/macros.h"
3336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe#include "base/memory/manual_constructor.h"
34b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/strings/string16.h"
350d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko#include "base/strings/string_piece.h"
36b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
37b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratnamespace base {
38b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
39b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratclass DictionaryValue;
40b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratclass ListValue;
41b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratclass Value;
4236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abeusing BinaryValue = Value;
43b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
44b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// The Value class is the base class for Values. A Value can be instantiated
45b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// via the Create*Value() factory methods, or by directly creating instances of
46b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// the subclasses.
47b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
48b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// See the file-level comment above for more information.
49b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratclass BASE_EXPORT Value {
50b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat public:
5136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  using DictStorage = std::map<std::string, std::unique_ptr<Value>>;
5236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  using ListStorage = std::vector<std::unique_ptr<Value>>;
5336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
5436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  enum class Type {
5536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    NONE = 0,
5636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    BOOLEAN,
5736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    INTEGER,
5836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    DOUBLE,
5936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    STRING,
6036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    BINARY,
6136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    DICTIONARY,
6236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    LIST
63b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    // Note: Do not add more types. See the file-level comment above for why.
64b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  };
65b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
66e5b2c6fa6f923f3a2f66346c2f169d9f0fceb3dcLuis Hector Chavez  static std::unique_ptr<Value> CreateNullValue();
670601274935e7f632eb0d6ce0fd223b744349d20bJay Civelli
6836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // For situations where you want to keep ownership of your buffer, this
6936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // factory method creates a new BinaryValue by copying the contents of the
7036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // buffer that's passed in.
7136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // DEPRECATED, use MakeUnique<Value>(const std::vector<char>&) instead.
7236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // TODO(crbug.com/646113): Delete this and migrate callsites.
7336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  static std::unique_ptr<BinaryValue> CreateWithCopiedBuffer(const char* buffer,
7436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe                                                             size_t size);
7536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
7636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  Value(const Value& that);
77319afc59a539d6261307aadbdab4d4ee93eaf1ffJakub Pawlowski  Value(Value&& that) noexcept;
78f810b5921dde57180956b9eadf39a3a2b8cb5855Hidehiko Abe  Value() noexcept;  // A null value.
7936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(Type type);
8036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(bool in_bool);
8136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(int in_int);
8236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(double in_double);
8336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
8436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // Value(const char*) and Value(const char16*) are required despite
8536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // Value(const std::string&) and Value(const string16&) because otherwise the
8636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // compiler will choose the Value(bool) constructor for these arguments.
8736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // Value(std::string&&) allow for efficient move construction.
8836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // Value(StringPiece) exists due to many callsites passing StringPieces as
8936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // arguments.
9036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(const char* in_string);
9136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(const std::string& in_string);
92f810b5921dde57180956b9eadf39a3a2b8cb5855Hidehiko Abe  explicit Value(std::string&& in_string) noexcept;
9336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(const char16* in_string);
9436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(const string16& in_string);
9536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(StringPiece in_string);
9636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
9736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(const std::vector<char>& in_blob);
98f810b5921dde57180956b9eadf39a3a2b8cb5855Hidehiko Abe  explicit Value(std::vector<char>&& in_blob) noexcept;
9936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
10036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  Value& operator=(const Value& that);
101f810b5921dde57180956b9eadf39a3a2b8cb5855Hidehiko Abe  Value& operator=(Value&& that) noexcept;
10236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
10336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  ~Value();
10436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
10536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // Returns the name for a given |type|.
10636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  static const char* GetTypeName(Type type);
10736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
108b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns the type of the value stored by the current Value object.
109b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Each type will be implemented by only one subclass of Value, so it's
110b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // safe to use the Type to determine whether you can cast from
111b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Value* to (Implementing Class)*.  Also, a Value object never changes
112b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // its type after construction.
11336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  Type GetType() const { return type_; }  // DEPRECATED, use type().
11436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  Type type() const { return type_; }
115b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
116b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns true if the current object represents a given type.
117b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool IsType(Type type) const { return type == type_; }
11836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool is_bool() const { return type() == Type::BOOLEAN; }
11936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool is_int() const { return type() == Type::INTEGER; }
12036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool is_double() const { return type() == Type::DOUBLE; }
12136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool is_string() const { return type() == Type::STRING; }
12236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool is_blob() const { return type() == Type::BINARY; }
12336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool is_dict() const { return type() == Type::DICTIONARY; }
12436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool is_list() const { return type() == Type::LIST; }
12536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
12636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // These will all fatally assert if the type doesn't match.
12736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetBool() const;
12836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  int GetInt() const;
12936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  double GetDouble() const;  // Implicitly converts from int if necessary.
13036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  const std::string& GetString() const;
13136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  const std::vector<char>& GetBlob() const;
13236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
13336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  size_t GetSize() const;         // DEPRECATED, use GetBlob().size() instead.
13436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  const char* GetBuffer() const;  // DEPRECATED, use GetBlob().data() instead.
135b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
136b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // These methods allow the convenient retrieval of the contents of the Value.
137b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // If the current object can be converted into the given type, the value is
138b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // returned through the |out_value| parameter and true is returned;
139b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // otherwise, false is returned and |out_value| is unchanged.
14036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsBoolean(bool* out_value) const;
14136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsInteger(int* out_value) const;
14236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsDouble(double* out_value) const;
14336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsString(std::string* out_value) const;
14436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsString(string16* out_value) const;
14536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsString(const Value** out_value) const;
14636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsString(StringPiece* out_value) const;
14736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsBinary(const BinaryValue** out_value) const;
14836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // ListValue::From is the equivalent for std::unique_ptr conversions.
14936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsList(ListValue** out_value);
15036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsList(const ListValue** out_value) const;
15136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // DictionaryValue::From is the equivalent for std::unique_ptr conversions.
15236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsDictionary(DictionaryValue** out_value);
15336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsDictionary(const DictionaryValue** out_value) const;
154b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Note: Do not add more types. See the file-level comment above for why.
155b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
156b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // This creates a deep copy of the entire Value tree, and returns a pointer
15736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // to the copy. The caller gets ownership of the copy, of course.
158b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Subclasses return their own type directly in their overrides;
159b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // this works because C++ supports covariant return types.
160f810b5921dde57180956b9eadf39a3a2b8cb5855Hidehiko Abe  // DEPRECATED, use Value's copy constructor instead.
161f810b5921dde57180956b9eadf39a3a2b8cb5855Hidehiko Abe  // TODO(crbug.com/646113): Delete this and migrate callsites.
16236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  Value* DeepCopy() const;
163b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Preferred version of DeepCopy. TODO(estade): remove the above.
16494ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  std::unique_ptr<Value> CreateDeepCopy() const;
165b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
166319afc59a539d6261307aadbdab4d4ee93eaf1ffJakub Pawlowski  // Comparison operators so that Values can easily be used with standard
167319afc59a539d6261307aadbdab4d4ee93eaf1ffJakub Pawlowski  // library algorithms and associative containers.
168319afc59a539d6261307aadbdab4d4ee93eaf1ffJakub Pawlowski  BASE_EXPORT friend bool operator==(const Value& lhs, const Value& rhs);
169319afc59a539d6261307aadbdab4d4ee93eaf1ffJakub Pawlowski  BASE_EXPORT friend bool operator!=(const Value& lhs, const Value& rhs);
170319afc59a539d6261307aadbdab4d4ee93eaf1ffJakub Pawlowski  BASE_EXPORT friend bool operator<(const Value& lhs, const Value& rhs);
171319afc59a539d6261307aadbdab4d4ee93eaf1ffJakub Pawlowski  BASE_EXPORT friend bool operator>(const Value& lhs, const Value& rhs);
172319afc59a539d6261307aadbdab4d4ee93eaf1ffJakub Pawlowski  BASE_EXPORT friend bool operator<=(const Value& lhs, const Value& rhs);
173319afc59a539d6261307aadbdab4d4ee93eaf1ffJakub Pawlowski  BASE_EXPORT friend bool operator>=(const Value& lhs, const Value& rhs);
174319afc59a539d6261307aadbdab4d4ee93eaf1ffJakub Pawlowski
175b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Compares if two Value objects have equal contents.
176319afc59a539d6261307aadbdab4d4ee93eaf1ffJakub Pawlowski  // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead.
177319afc59a539d6261307aadbdab4d4ee93eaf1ffJakub Pawlowski  // TODO(crbug.com/646113): Delete this and migrate callsites.
17836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool Equals(const Value* other) const;
179b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
180b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Compares if two Value objects have equal contents. Can handle NULLs.
181b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // NULLs are considered equal but different from Value::CreateNullValue().
182319afc59a539d6261307aadbdab4d4ee93eaf1ffJakub Pawlowski  // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead.
183319afc59a539d6261307aadbdab4d4ee93eaf1ffJakub Pawlowski  // TODO(crbug.com/646113): Delete this and migrate callsites.
184b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  static bool Equals(const Value* a, const Value* b);
185b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
186b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat protected:
18736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // TODO(crbug.com/646113): Make these private once DictionaryValue and
18836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // ListValue are properly inlined.
189b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  Type type_;
190b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
191b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  union {
19236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    bool bool_value_;
19336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    int int_value_;
194b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    double double_value_;
19536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    ManualConstructor<std::string> string_value_;
19636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    ManualConstructor<std::vector<char>> binary_value_;
19736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    // For current gcc and clang sizeof(DictStorage) = 48, which would result
19836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    // in sizeof(Value) = 56 if DictStorage was stack allocated. Allocating it
19936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    // on the heap results in sizeof(Value) = 40 for all of gcc, clang and MSVC.
20036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    ManualConstructor<std::unique_ptr<DictStorage>> dict_ptr_;
20136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    ManualConstructor<ListStorage> list_;
202b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  };
203b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
204b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat private:
20536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void InternalCopyFundamentalValue(const Value& that);
20636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void InternalCopyConstructFrom(const Value& that);
20736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void InternalMoveConstructFrom(Value&& that);
20836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void InternalCopyAssignFromSameType(const Value& that);
20936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void InternalCleanup();
210b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat};
211b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
212b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// DictionaryValue provides a key-value dictionary with (optional) "path"
213b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// parsing for recursive access; see the comment at the top of the file. Keys
214b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// are |std::string|s and should be UTF-8 encoded.
215b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratclass BASE_EXPORT DictionaryValue : public Value {
216b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat public:
2170d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko  // Returns |value| if it is a dictionary, nullptr otherwise.
21894ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value);
2190d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko
220b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  DictionaryValue();
221b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
222b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns true if the current dictionary has a value for the given key.
22336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool HasKey(StringPiece key) const;
224b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
225b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns the number of Values in this dictionary.
22636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  size_t size() const { return (*dict_ptr_)->size(); }
227b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
228b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns whether the dictionary is empty.
22936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool empty() const { return (*dict_ptr_)->empty(); }
230b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
231b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Clears any current contents of this dictionary.
232b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void Clear();
233b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
234b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Sets the Value associated with the given path starting from this object.
235b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
236b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // into the next DictionaryValue down.  Obviously, "." can't be used
237b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // within a key, but there are no other restrictions on keys.
238b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // If the key at any step of the way doesn't exist, or exists but isn't
239b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // a DictionaryValue, a new DictionaryValue will be created and attached
240b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // to the path in that location. |in_value| must be non-null.
24136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void Set(StringPiece path, std::unique_ptr<Value> in_value);
242b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Deprecated version of the above. TODO(estade): remove.
24336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void Set(StringPiece path, Value* in_value);
244b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
245b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Convenience forms of Set().  These methods will replace any existing
246b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // value at that path, even if it has a different type.
24736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetBoolean(StringPiece path, bool in_value);
24836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetInteger(StringPiece path, int in_value);
24936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetDouble(StringPiece path, double in_value);
25036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetString(StringPiece path, StringPiece in_value);
25136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetString(StringPiece path, const string16& in_value);
252b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
253b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Like Set(), but without special treatment of '.'.  This allows e.g. URLs to
254b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // be used as paths.
25536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetWithoutPathExpansion(StringPiece key,
25694ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez                               std::unique_ptr<Value> in_value);
257b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Deprecated version of the above. TODO(estade): remove.
25836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetWithoutPathExpansion(StringPiece key, Value* in_value);
259b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
260b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Convenience forms of SetWithoutPathExpansion().
26136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetBooleanWithoutPathExpansion(StringPiece path, bool in_value);
26236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetIntegerWithoutPathExpansion(StringPiece path, int in_value);
26336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetDoubleWithoutPathExpansion(StringPiece path, double in_value);
26436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetStringWithoutPathExpansion(StringPiece path, StringPiece in_value);
26536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetStringWithoutPathExpansion(StringPiece path,
266b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                     const string16& in_value);
267b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
268b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Gets the Value associated with the given path starting from this object.
269b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
270b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // into the next DictionaryValue down.  If the path can be resolved
271b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // successfully, the value for the last key in the path will be returned
272b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // through the |out_value| parameter, and the function will return true.
273b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Otherwise, it will return false and |out_value| will be untouched.
274b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Note that the dictionary always owns the value that's returned.
275b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // |out_value| is optional and will only be set if non-NULL.
2760d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko  bool Get(StringPiece path, const Value** out_value) const;
2770d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko  bool Get(StringPiece path, Value** out_value);
278b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
279b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // These are convenience forms of Get().  The value will be retrieved
280b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // and the return value will be true if the path is valid and the value at
281b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // the end of the path can be returned in the form specified.
282b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // |out_value| is optional and will only be set if non-NULL.
28336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetBoolean(StringPiece path, bool* out_value) const;
28436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetInteger(StringPiece path, int* out_value) const;
28536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
286b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // doubles.
28736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetDouble(StringPiece path, double* out_value) const;
28836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetString(StringPiece path, std::string* out_value) const;
28936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetString(StringPiece path, string16* out_value) const;
29036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetStringASCII(StringPiece path, std::string* out_value) const;
29136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetBinary(StringPiece path, const BinaryValue** out_value) const;
29236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetBinary(StringPiece path, BinaryValue** out_value);
2930d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko  bool GetDictionary(StringPiece path,
294b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                     const DictionaryValue** out_value) const;
2950d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko  bool GetDictionary(StringPiece path, DictionaryValue** out_value);
29636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetList(StringPiece path, const ListValue** out_value) const;
29736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetList(StringPiece path, ListValue** out_value);
298b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
299b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Like Get(), but without special treatment of '.'.  This allows e.g. URLs to
300b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // be used as paths.
30136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const;
30236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetWithoutPathExpansion(StringPiece key, Value** out_value);
30336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const;
30436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const;
30536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const;
30636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetStringWithoutPathExpansion(StringPiece key,
307b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                     std::string* out_value) const;
30836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetStringWithoutPathExpansion(StringPiece key,
309b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                     string16* out_value) const;
310b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetDictionaryWithoutPathExpansion(
31136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe      StringPiece key,
312b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      const DictionaryValue** out_value) const;
31336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetDictionaryWithoutPathExpansion(StringPiece key,
314b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                         DictionaryValue** out_value);
31536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetListWithoutPathExpansion(StringPiece key,
316b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                   const ListValue** out_value) const;
31736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value);
318b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
319b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Removes the Value with the specified path from this dictionary (or one
320b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // of its child dictionaries, if the path is more than just a local key).
321b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // If |out_value| is non-NULL, the removed Value will be passed out via
322b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // |out_value|.  If |out_value| is NULL, the removed value will be deleted.
323b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // This method returns true if |path| is a valid path; otherwise it will
324b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // return false and the DictionaryValue object will be unchanged.
32536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool Remove(StringPiece path, std::unique_ptr<Value>* out_value);
326b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
327b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Like Remove(), but without special treatment of '.'.  This allows e.g. URLs
328b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // to be used as paths.
32936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool RemoveWithoutPathExpansion(StringPiece key,
33036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe                                  std::unique_ptr<Value>* out_value);
331b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
332b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Removes a path, clearing out all dictionaries on |path| that remain empty
333b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // after removing the value at |path|.
33436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value);
335b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
336b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Makes a copy of |this| but doesn't include empty dictionaries and lists in
337b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // the copy.  This never returns NULL, even if |this| itself is empty.
33894ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const;
339b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
340b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Merge |dictionary| into this dictionary. This is done recursively, i.e. any
341b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // sub-dictionaries will be merged as well. In case of key collisions, the
342b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // passed in dictionary takes precedence and data already present will be
343b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // replaced. Values within |dictionary| are deep-copied, so |dictionary| may
344b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // be freed any time after this call.
345b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void MergeDictionary(const DictionaryValue* dictionary);
346b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
347b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Swaps contents with the |other| dictionary.
34836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void Swap(DictionaryValue* other);
349b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
350b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // This class provides an iterator over both keys and values in the
351b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // dictionary.  It can't be used to modify the dictionary.
352b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  class BASE_EXPORT Iterator {
353b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat   public:
354b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    explicit Iterator(const DictionaryValue& target);
35545779228f8c9e40851cfd23f727e2bd8ffdd4714Alex Vakulenko    Iterator(const Iterator& other);
356b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    ~Iterator();
357b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
35836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    bool IsAtEnd() const { return it_ == (*target_.dict_ptr_)->end(); }
359b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    void Advance() { ++it_; }
360b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
361b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    const std::string& key() const { return it_->first; }
362b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    const Value& value() const { return *it_->second; }
363b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
364b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat   private:
365b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    const DictionaryValue& target_;
36636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    DictStorage::const_iterator it_;
367b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  };
368b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
369f810b5921dde57180956b9eadf39a3a2b8cb5855Hidehiko Abe  // DEPRECATED, use DictionaryValue's copy constructor instead.
370f810b5921dde57180956b9eadf39a3a2b8cb5855Hidehiko Abe  // TODO(crbug.com/646113): Delete this and migrate callsites.
37136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  DictionaryValue* DeepCopy() const;
372b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Preferred version of DeepCopy. TODO(estade): remove the above.
37394ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  std::unique_ptr<DictionaryValue> CreateDeepCopy() const;
374b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat};
375b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
376b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// This type of Value represents a list of other Value values.
377b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratclass BASE_EXPORT ListValue : public Value {
378b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat public:
37936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  using const_iterator = ListStorage::const_iterator;
38036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  using iterator = ListStorage::iterator;
381b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
3820d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko  // Returns |value| if it is a list, nullptr otherwise.
38394ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value);
3840d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko
385b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  ListValue();
386b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
387b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Clears the contents of this ListValue
388b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void Clear();
389b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
390b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns the number of Values in this list.
39136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  size_t GetSize() const { return list_->size(); }
392b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
393b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns whether the list is empty.
39436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool empty() const { return list_->empty(); }
395b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
396b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Sets the list item at the given index to be the Value specified by
397b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // the value given.  If the index beyond the current end of the list, null
398b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Values will be used to pad out the list.
399b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns true if successful, or false if the index was negative or
400b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // the value is a null pointer.
401b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool Set(size_t index, Value* in_value);
402b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Preferred version of the above. TODO(estade): remove the above.
40394ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  bool Set(size_t index, std::unique_ptr<Value> in_value);
404b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
405b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Gets the Value at the given index.  Modifies |out_value| (and returns true)
406b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // only if the index falls within the current list range.
407b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Note that the list always owns the Value passed out via |out_value|.
408b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // |out_value| is optional and will only be set if non-NULL.
409b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool Get(size_t index, const Value** out_value) const;
410b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool Get(size_t index, Value** out_value);
411b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
412b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Convenience forms of Get().  Modifies |out_value| (and returns true)
413b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // only if the index is valid and the Value at that index can be returned
414b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // in the specified form.
415b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // |out_value| is optional and will only be set if non-NULL.
416b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetBoolean(size_t index, bool* out_value) const;
417b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetInteger(size_t index, int* out_value) const;
41836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
419b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // doubles.
420b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetDouble(size_t index, double* out_value) const;
421b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetString(size_t index, std::string* out_value) const;
422b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetString(size_t index, string16* out_value) const;
423b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetBinary(size_t index, const BinaryValue** out_value) const;
424b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetBinary(size_t index, BinaryValue** out_value);
425b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetDictionary(size_t index, const DictionaryValue** out_value) const;
426b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetDictionary(size_t index, DictionaryValue** out_value);
427b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetList(size_t index, const ListValue** out_value) const;
428b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetList(size_t index, ListValue** out_value);
429b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
430b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Removes the Value with the specified index from this list.
431b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
432b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // passed out via |out_value|.  If |out_value| is NULL, the removed value will
433b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // be deleted.  This method returns true if |index| is valid; otherwise
434b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // it will return false and the ListValue object will be unchanged.
43536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool Remove(size_t index, std::unique_ptr<Value>* out_value);
436b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
437b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Removes the first instance of |value| found in the list, if any, and
438b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // deletes it. |index| is the location where |value| was found. Returns false
439b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // if not found.
440b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool Remove(const Value& value, size_t* index);
441b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
442b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Removes the element at |iter|. If |out_value| is NULL, the value will be
443b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // deleted, otherwise ownership of the value is passed back to the caller.
444b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns an iterator pointing to the location of the element that
445b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // followed the erased element.
44694ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  iterator Erase(iterator iter, std::unique_ptr<Value>* out_value);
447b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
448b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Appends a Value to the end of the list.
44994ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  void Append(std::unique_ptr<Value> in_value);
45036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe#if !defined(OS_LINUX)
451b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Deprecated version of the above. TODO(estade): remove.
452b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void Append(Value* in_value);
45336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe#endif
454b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
455b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Convenience forms of Append.
456b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void AppendBoolean(bool in_value);
457b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void AppendInteger(int in_value);
458b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void AppendDouble(double in_value);
45936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void AppendString(StringPiece in_value);
460b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void AppendString(const string16& in_value);
461b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void AppendStrings(const std::vector<std::string>& in_values);
462b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void AppendStrings(const std::vector<string16>& in_values);
463b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
46436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // Appends a Value if it's not already present. Returns true if successful,
46536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // or false if the value was already
46636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool AppendIfNotPresent(std::unique_ptr<Value> in_value);
467b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
468b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Insert a Value at index.
469b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns true if successful, or false if the index was out of range.
47036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool Insert(size_t index, std::unique_ptr<Value> in_value);
471b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
472b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Searches for the first instance of |value| in the list using the Equals
473b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // method of the Value type.
474b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns a const_iterator to the found item or to end() if none exists.
475b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  const_iterator Find(const Value& value) const;
476b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
477b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Swaps contents with the |other| list.
47836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void Swap(ListValue* other);
479b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
480b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Iteration.
48136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  iterator begin() { return list_->begin(); }
48236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  iterator end() { return list_->end(); }
483b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
48436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  const_iterator begin() const { return list_->begin(); }
48536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  const_iterator end() const { return list_->end(); }
486b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
487f810b5921dde57180956b9eadf39a3a2b8cb5855Hidehiko Abe  // DEPRECATED, use ListValue's copy constructor instead.
488f810b5921dde57180956b9eadf39a3a2b8cb5855Hidehiko Abe  // TODO(crbug.com/646113): Delete this and migrate callsites.
48936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  ListValue* DeepCopy() const;
490b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Preferred version of DeepCopy. TODO(estade): remove DeepCopy.
49194ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  std::unique_ptr<ListValue> CreateDeepCopy() const;
492b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat};
493b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
494b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// This interface is implemented by classes that know how to serialize
495b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Value objects.
496b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratclass BASE_EXPORT ValueSerializer {
497b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat public:
498b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  virtual ~ValueSerializer();
499b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
500b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  virtual bool Serialize(const Value& root) = 0;
501b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat};
502b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
503b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// This interface is implemented by classes that know how to deserialize Value
504b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// objects.
505b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratclass BASE_EXPORT ValueDeserializer {
506b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat public:
507b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  virtual ~ValueDeserializer();
508b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
509b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // This method deserializes the subclass-specific format into a Value object.
510b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // If the return value is non-NULL, the caller takes ownership of returned
511b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Value. If the return value is NULL, and if error_code is non-NULL,
512b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // error_code will be set with the underlying error.
513b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // If |error_message| is non-null, it will be filled in with a formatted
514b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // error message including the location of the error if appropriate.
51594ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  virtual std::unique_ptr<Value> Deserialize(int* error_code,
51694ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez                                             std::string* error_str) = 0;
517b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat};
518b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
519b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Stream operator so Values can be used in assertion statements.  In order that
520b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// gtest uses this operator to print readable output on test failures, we must
521b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// override each specific type. Otherwise, the default template implementation
522b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// is preferred over an upcast.
523b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
524b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
525b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
526b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                            const DictionaryValue& value) {
527b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return out << static_cast<const Value&>(value);
528b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
529b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
530b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
531b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                            const ListValue& value) {
532b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return out << static_cast<const Value&>(value);
533b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
534b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
53536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe// Stream operator so that enum class Types can be used in log statements.
53636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko AbeBASE_EXPORT std::ostream& operator<<(std::ostream& out,
53736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe                                     const Value::Type& type);
53836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
539b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}  // namespace base
540b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
541b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#endif  // BASE_VALUES_H_
542