13f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be
3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file.
4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
53345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// This file specifies a recursive data storage class called Value intended for
63345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// storing setting and other persistable data.  It includes the ability to
73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// specify (recursive) lists and dictionaries, so it's fairly expressive.
83345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// However, the API is optimized for the common case, namely storing a
93345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// hierarchical tree of simple values.  Given a DictionaryValue root, you can
103345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// easily do things like:
11c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott//
123345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// root->SetString("global.pages.homepage", "http://goateleporter.com");
133345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// std::string homepage = "http://google.com";  // default/fallback value
143345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// root->GetString("global.pages.homepage", &homepage);
15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott//
163345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// where "global" and "pages" are also DictionaryValues, and "homepage" is a
173345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// string setting.  If some elements of the path didn't exist yet, the
183345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// SetString() method would create the missing elements and attach them to root
193345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// before attaching the homepage value.
20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifndef BASE_VALUES_H_
22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#define BASE_VALUES_H_
233345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <iterator>
26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <map>
27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <string>
28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <vector>
29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
30ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/base_api.h"
31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/basictypes.h"
32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/string16.h"
33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "build/build_config.h"
34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass BinaryValue;
36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass DictionaryValue;
37dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenclass FundamentalValue;
38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass ListValue;
39dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenclass StringValue;
40dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenclass Value;
41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttypedef std::vector<Value*> ValueVector;
433345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merricktypedef std::map<std::string, Value*> ValueMap;
44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// The Value class is the base class for Values.  A Value can be
46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// instantiated via the Create*Value() factory methods, or by directly
47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// creating instances of the subclasses.
48ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API Value {
49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
503f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  enum ValueType {
513f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    TYPE_NULL = 0,
523f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    TYPE_BOOLEAN,
533f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    TYPE_INTEGER,
5472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen    TYPE_DOUBLE,
553f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    TYPE_STRING,
563f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    TYPE_BINARY,
573f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    TYPE_DICTIONARY,
583f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    TYPE_LIST
593f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  };
603f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
61c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  virtual ~Value();
62c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
63c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Convenience methods for creating Value objects for various
64c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // kinds of values without thinking about which class implements them.
65c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // These can always be expected to return a valid Value*.
66c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  static Value* CreateNullValue();
6772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  static FundamentalValue* CreateBooleanValue(bool in_value);
6872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  static FundamentalValue* CreateIntegerValue(int in_value);
6972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  static FundamentalValue* CreateDoubleValue(double in_value);
7072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  static StringValue* CreateStringValue(const std::string& in_value);
7172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  static StringValue* CreateStringValue(const string16& in_value);
72c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
73c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // This one can return NULL if the input isn't valid.  If the return value
74c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // is non-null, the new object has taken ownership of the buffer pointer.
75c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  static BinaryValue* CreateBinaryValue(char* buffer, size_t size);
76c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
77c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Returns the type of the value stored by the current Value object.
78c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Each type will be implemented by only one subclass of Value, so it's
79c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // safe to use the ValueType to determine whether you can cast from
80c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Value* to (Implementing Class)*.  Also, a Value object never changes
81c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // its type after construction.
82c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ValueType GetType() const { return type_; }
83c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
84c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Returns true if the current object represents a given type.
85c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool IsType(ValueType type) const { return type == type_; }
86c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
87c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // These methods allow the convenient retrieval of settings.
88c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // If the current setting object can be converted into the given type,
89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the value is returned through the |out_value| parameter and true is
90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // returned;  otherwise, false is returned and |out_value| is unchanged.
91c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  virtual bool GetAsBoolean(bool* out_value) const;
92c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  virtual bool GetAsInteger(int* out_value) const;
9372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual bool GetAsDouble(double* out_value) const;
94c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  virtual bool GetAsString(std::string* out_value) const;
953345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  virtual bool GetAsString(string16* out_value) const;
9621d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen  virtual bool GetAsList(ListValue** out_value);
97c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
98c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // This creates a deep copy of the entire Value tree, and returns a pointer
99c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // to the copy.  The caller gets ownership of the copy, of course.
10072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  //
10172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // Subclasses return their own type directly in their overrides;
10272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // this works because C++ supports covariant return types.
103c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  virtual Value* DeepCopy() const;
104c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
105c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Compares if two Value objects have equal contents.
106c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  virtual bool Equals(const Value* other) const;
107c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
10821d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen  // Compares if two Value objects have equal contents. Can handle NULLs.
10921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen  // NULLs are considered equal but different from Value::CreateNullValue().
11021d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen  static bool Equals(const Value* a, const Value* b);
11121d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen
112c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott protected:
113c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // This isn't safe for end-users (they should use the Create*Value()
114c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // static methods above), but it's useful for subclasses.
115c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  explicit Value(ValueType type);
116c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
117c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
118c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  Value();
119c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
120c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ValueType type_;
121c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
122c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  DISALLOW_COPY_AND_ASSIGN(Value);
123c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
124c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
125c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// FundamentalValue represents the simple fundamental types of values.
126ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API FundamentalValue : public Value {
127c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
128c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  explicit FundamentalValue(bool in_value);
129c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  explicit FundamentalValue(int in_value);
130c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  explicit FundamentalValue(double in_value);
13121d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen  virtual ~FundamentalValue();
132c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
133c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Subclassed methods
134c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  virtual bool GetAsBoolean(bool* out_value) const;
135c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  virtual bool GetAsInteger(int* out_value) const;
13672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual bool GetAsDouble(double* out_value) const;
13772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual FundamentalValue* DeepCopy() const;
138c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  virtual bool Equals(const Value* other) const;
139c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
140c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
141c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  union {
142c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    bool boolean_value_;
143c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    int integer_value_;
14472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen    double double_value_;
145c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  };
146c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
147c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  DISALLOW_COPY_AND_ASSIGN(FundamentalValue);
148c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
149c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
150ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API StringValue : public Value {
151c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
152c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Initializes a StringValue with a UTF-8 narrow character string.
153c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  explicit StringValue(const std::string& in_value);
154c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
155c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Initializes a StringValue with a string16.
156c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  explicit StringValue(const string16& in_value);
157c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
15821d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen  virtual ~StringValue();
159c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
160c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Subclassed methods
16121d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen  virtual bool GetAsString(std::string* out_value) const;
16221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen  virtual bool GetAsString(string16* out_value) const;
16372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual StringValue* DeepCopy() const;
164c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  virtual bool Equals(const Value* other) const;
165c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
166c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
167c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  std::string value_;
168c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
169c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  DISALLOW_COPY_AND_ASSIGN(StringValue);
170c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
171c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
172ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API BinaryValue: public Value {
173c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
1743f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  virtual ~BinaryValue();
1753f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
176c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Creates a Value to represent a binary buffer.  The new object takes
177c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // ownership of the pointer passed in, if successful.
178c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Returns NULL if buffer is NULL.
179c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  static BinaryValue* Create(char* buffer, size_t size);
180c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
181c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // For situations where you want to keep ownership of your buffer, this
182c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // factory method creates a new BinaryValue by copying the contents of the
183c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // buffer that's passed in.
184c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Returns NULL if buffer is NULL.
185c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size);
186c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
187c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  size_t GetSize() const { return size_; }
188c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  char* GetBuffer() { return buffer_; }
189c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  const char* GetBuffer() const { return buffer_; }
190c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
1913f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // Overridden from Value:
19272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual BinaryValue* DeepCopy() const;
1933f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  virtual bool Equals(const Value* other) const;
1943f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
195c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
196c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Constructor is private so that only objects with valid buffer pointers
197c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // and size values can be created.
198c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  BinaryValue(char* buffer, size_t size);
199c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
200c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  char* buffer_;
201c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  size_t size_;
202c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
203c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  DISALLOW_COPY_AND_ASSIGN(BinaryValue);
204c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
205c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
2063345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// DictionaryValue provides a key-value dictionary with (optional) "path"
2073345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// parsing for recursive access; see the comment at the top of the file. Keys
2083345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// are |std::string|s and should be UTF-8 encoded.
209ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API DictionaryValue : public Value {
210c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
211c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  DictionaryValue();
21221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen  virtual ~DictionaryValue();
213c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
214c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Returns true if the current dictionary has a value for the given key.
2153345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool HasKey(const std::string& key) const;
216c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
217c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Returns the number of Values in this dictionary.
218c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  size_t size() const { return dictionary_.size(); }
219c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
220c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Returns whether the dictionary is empty.
221c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool empty() const { return dictionary_.empty(); }
222c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
223c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Clears any current contents of this dictionary.
224c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void Clear();
225c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
226c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Sets the Value associated with the given path starting from this object.
227c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
228c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // into the next DictionaryValue down.  Obviously, "." can't be used
229c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // within a key, but there are no other restrictions on keys.
230c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // If the key at any step of the way doesn't exist, or exists but isn't
231c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // a DictionaryValue, a new DictionaryValue will be created and attached
232c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // to the path in that location.
233c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Note that the dictionary takes ownership of the value referenced by
234c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // |in_value|, and therefore |in_value| must be non-NULL.
2353345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  void Set(const std::string& path, Value* in_value);
236c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
237c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Convenience forms of Set().  These methods will replace any existing
238c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // value at that path, even if it has a different type.
2393345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  void SetBoolean(const std::string& path, bool in_value);
2403345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  void SetInteger(const std::string& path, int in_value);
24172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  void SetDouble(const std::string& path, double in_value);
2423345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  void SetString(const std::string& path, const std::string& in_value);
2433345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  void SetString(const std::string& path, const string16& in_value);
244c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
245c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Like Set(), but without special treatment of '.'.  This allows e.g. URLs to
246c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // be used as paths.
2473345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  void SetWithoutPathExpansion(const std::string& key, Value* in_value);
248c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
249c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Gets the Value associated with the given path starting from this object.
250c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
251c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // into the next DictionaryValue down.  If the path can be resolved
252c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // successfully, the value for the last key in the path will be returned
253c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // through the |out_value| parameter, and the function will return true.
254c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Otherwise, it will return false and |out_value| will be untouched.
255c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Note that the dictionary always owns the value that's returned.
2563345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool Get(const std::string& path, Value** out_value) const;
257c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
258c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // These are convenience forms of Get().  The value will be retrieved
259c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // and the return value will be true if the path is valid and the value at
260c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // the end of the path can be returned in the form specified.
2613345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool GetBoolean(const std::string& path, bool* out_value) const;
2623345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool GetInteger(const std::string& path, int* out_value) const;
26372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  bool GetDouble(const std::string& path, double* out_value) const;
2643345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool GetString(const std::string& path, std::string* out_value) const;
265c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool GetString(const std::string& path, string16* out_value) const;
266c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool GetStringASCII(const std::string& path, std::string* out_value) const;
2673345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool GetBinary(const std::string& path, BinaryValue** out_value) const;
2683345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool GetDictionary(const std::string& path,
269c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                     DictionaryValue** out_value) const;
2703345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool GetList(const std::string& path, ListValue** out_value) const;
271c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
272c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Like Get(), but without special treatment of '.'.  This allows e.g. URLs to
273c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // be used as paths.
2743345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool GetWithoutPathExpansion(const std::string& key,
275c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                               Value** out_value) const;
2763345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool GetIntegerWithoutPathExpansion(const std::string& key,
277c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                                      int* out_value) const;
27872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  bool GetDoubleWithoutPathExpansion(const std::string& key,
279731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick                                   double* out_value) const;
2803345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool GetStringWithoutPathExpansion(const std::string& key,
281c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                                     std::string* out_value) const;
2823345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool GetStringWithoutPathExpansion(const std::string& key,
2833345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick                                     string16* out_value) const;
2843345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool GetDictionaryWithoutPathExpansion(const std::string& key,
285c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                                         DictionaryValue** out_value) const;
2863345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool GetListWithoutPathExpansion(const std::string& key,
287c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                                   ListValue** out_value) const;
288c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
289c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Removes the Value with the specified path from this dictionary (or one
290c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // of its child dictionaries, if the path is more than just a local key).
291c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
292c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // passed out via out_value.  If |out_value| is NULL, the removed value will
293c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // be deleted.  This method returns true if |path| is a valid path; otherwise
294c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // it will return false and the DictionaryValue object will be unchanged.
2953345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool Remove(const std::string& path, Value** out_value);
296c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
297c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Like Remove(), but without special treatment of '.'.  This allows e.g. URLs
298c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // to be used as paths.
2993345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool RemoveWithoutPathExpansion(const std::string& key, Value** out_value);
300c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
301c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Makes a copy of |this| but doesn't include empty dictionaries and lists in
302c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // the copy.  This never returns NULL, even if |this| itself is empty.
303c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  DictionaryValue* DeepCopyWithoutEmptyChildren();
304c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
305c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Merge a given dictionary into this dictionary. This is done recursively,
306c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // i.e. any subdictionaries will be merged as well. In case of key collisions,
307c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the passed in dictionary takes precedence and data already present will be
308c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // replaced.
309c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void MergeDictionary(const DictionaryValue* dictionary);
310c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
311c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // This class provides an iterator for the keys in the dictionary.
312c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // It can't be used to modify the dictionary.
313c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  //
314c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT
315c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // THE NORMAL XXX() APIs.  This makes sure things will work correctly if any
316c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // keys have '.'s in them.
317ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  class BASE_API key_iterator
3183345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick      : private std::iterator<std::input_iterator_tag, const std::string> {
319c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott   public:
320c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; }
321c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    key_iterator operator++() {
322c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      ++itr_;
323c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      return *this;
324c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    }
3253345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick    const std::string& operator*() { return itr_->first; }
326c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    bool operator!=(const key_iterator& other) { return itr_ != other.itr_; }
327c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    bool operator==(const key_iterator& other) { return itr_ == other.itr_; }
328c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
329c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott   private:
330c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    ValueMap::const_iterator itr_;
331c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  };
332c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
333c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); }
334c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  key_iterator end_keys() const { return key_iterator(dictionary_.end()); }
335c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
3363f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // Overridden from Value:
33772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual DictionaryValue* DeepCopy() const;
3383f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  virtual bool Equals(const Value* other) const;
3393f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
340c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
341c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ValueMap dictionary_;
342c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
343c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  DISALLOW_COPY_AND_ASSIGN(DictionaryValue);
344c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
345c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
346c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This type of Value represents a list of other Value values.
347ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API ListValue : public Value {
348c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
3493f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  typedef ValueVector::iterator iterator;
3503f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  typedef ValueVector::const_iterator const_iterator;
3513f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
352c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  ListValue();
353c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ~ListValue();
354c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
355c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Clears the contents of this ListValue
356c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void Clear();
357c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
358c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Returns the number of Values in this list.
359c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  size_t GetSize() const { return list_.size(); }
360c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
361c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Returns whether the list is empty.
362c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool empty() const { return list_.empty(); }
363c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
364c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Sets the list item at the given index to be the Value specified by
365c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // the value given.  If the index beyond the current end of the list, null
366c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Values will be used to pad out the list.
367c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Returns true if successful, or false if the index was negative or
368c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // the value is a null pointer.
369c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool Set(size_t index, Value* in_value);
370c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
371c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Gets the Value at the given index.  Modifies |out_value| (and returns true)
372c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // only if the index falls within the current list range.
373c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Note that the list always owns the Value passed out via |out_value|.
374c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool Get(size_t index, Value** out_value) const;
375c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
376c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Convenience forms of Get().  Modifies |out_value| (and returns true)
377c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // only if the index is valid and the Value at that index can be returned
378c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // in the specified form.
379c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool GetBoolean(size_t index, bool* out_value) const;
380c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool GetInteger(size_t index, int* out_value) const;
38172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  bool GetDouble(size_t index, double* out_value) const;
382c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool GetString(size_t index, std::string* out_value) const;
3833345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool GetString(size_t index, string16* out_value) const;
384c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool GetBinary(size_t index, BinaryValue** out_value) const;
385c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool GetDictionary(size_t index, DictionaryValue** out_value) const;
386c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool GetList(size_t index, ListValue** out_value) const;
387c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
388c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Removes the Value with the specified index from this list.
389c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
390c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // passed out via |out_value|.  If |out_value| is NULL, the removed value will
391c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // be deleted.  This method returns true if |index| is valid; otherwise
392c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // it will return false and the ListValue object will be unchanged.
393c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool Remove(size_t index, Value** out_value);
394c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
395c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Removes the first instance of |value| found in the list, if any, and
396c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // deletes it.  Returns the index that it was located at (-1 for not present).
397c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  int Remove(const Value& value);
398c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
399c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Appends a Value to the end of the list.
400c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void Append(Value* in_value);
401c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
402ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Appends a Value if it's not already present. Takes ownership of the
403ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // |in_value|. Returns true if successful, or false if the value was already
404ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // present. If the value was already present the |in_value| is deleted.
405c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool AppendIfNotPresent(Value* in_value);
406c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
407c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Insert a Value at index.
408c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Returns true if successful, or false if the index was out of range.
409c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool Insert(size_t index, Value* in_value);
410c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
4113345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // Swaps contents with the |other| list.
4123345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  void Swap(ListValue* other) {
4133345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick    list_.swap(other->list_);
4143345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  }
4153345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
416c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Iteration
417c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ListValue::iterator begin() { return list_.begin(); }
418c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ListValue::iterator end() { return list_.end(); }
419c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
420c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ListValue::const_iterator begin() const { return list_.begin(); }
421c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ListValue::const_iterator end() const { return list_.end(); }
422c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
4233f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // Overridden from Value:
4243f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  virtual bool GetAsList(ListValue** out_value);
42572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual ListValue* DeepCopy() const;
4263f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  virtual bool Equals(const Value* other) const;
4273f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
428c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
429c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ValueVector list_;
430c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
431c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  DISALLOW_COPY_AND_ASSIGN(ListValue);
432c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
433c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
434c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This interface is implemented by classes that know how to serialize and
435c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// deserialize Value objects.
436ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API ValueSerializer {
437c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
438c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual ~ValueSerializer();
439c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
440c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  virtual bool Serialize(const Value& root) = 0;
441c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
442c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // This method deserializes the subclass-specific format into a Value object.
443c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // If the return value is non-NULL, the caller takes ownership of returned
444c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Value. If the return value is NULL, and if error_code is non-NULL,
445c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // error_code will be set with the underlying error.
446c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // If |error_message| is non-null, it will be filled in with a formatted
447c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // error message including the location of the error if appropriate.
448c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual Value* Deserialize(int* error_code, std::string* error_str) = 0;
449c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
450c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
451c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif  // BASE_VALUES_H_
452