1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/values.h"
7 #include "base/logging.h"
8 #include "base/string_util.h"
9 #include "base/utf_string_conversions.h"
13 // Make a deep copy of |node|, but don't include empty lists or dictionaries
14 // in the copy. It's possible for this function to return NULL and it
15 // expects |node| to always be non-NULL.
16 Value
* CopyWithoutEmptyChildren(Value
* node
) {
18 switch (node
->GetType()) {
19 case Value::TYPE_LIST
: {
20 ListValue
* list
= static_cast<ListValue
*>(node
);
21 ListValue
* copy
= new ListValue
;
22 for (ListValue::const_iterator it
= list
->begin(); it
!= list
->end();
24 Value
* child_copy
= CopyWithoutEmptyChildren(*it
);
26 copy
->Append(child_copy
);
35 case Value::TYPE_DICTIONARY
: {
36 DictionaryValue
* dict
= static_cast<DictionaryValue
*>(node
);
37 DictionaryValue
* copy
= new DictionaryValue
;
38 for (DictionaryValue::key_iterator it
= dict
->begin_keys();
39 it
!= dict
->end_keys(); ++it
) {
41 bool rv
= dict
->GetWithoutPathExpansion(*it
, &child
);
43 Value
* child_copy
= CopyWithoutEmptyChildren(child
);
45 copy
->SetWithoutPathExpansion(*it
, child_copy
);
55 // For everything else, just make a copy.
56 return node
->DeepCopy();
62 ///////////////////// Value ////////////////////
68 Value
* Value::CreateNullValue() {
69 return new Value(TYPE_NULL
);
73 FundamentalValue
* Value::CreateBooleanValue(bool in_value
) {
74 return new FundamentalValue(in_value
);
78 FundamentalValue
* Value::CreateIntegerValue(int in_value
) {
79 return new FundamentalValue(in_value
);
83 FundamentalValue
* Value::CreateDoubleValue(double in_value
) {
84 return new FundamentalValue(in_value
);
88 StringValue
* Value::CreateStringValue(const std::string
& in_value
) {
89 return new StringValue(in_value
);
93 StringValue
* Value::CreateStringValue(const string16
& in_value
) {
94 return new StringValue(in_value
);
98 BinaryValue
* Value::CreateBinaryValue(char* buffer
, size_t size
) {
99 return BinaryValue::Create(buffer
, size
);
102 bool Value::GetAsBoolean(bool* out_value
) const {
106 bool Value::GetAsInteger(int* out_value
) const {
110 bool Value::GetAsDouble(double* out_value
) const {
114 bool Value::GetAsString(std::string
* out_value
) const {
118 bool Value::GetAsString(string16
* out_value
) const {
122 bool Value::GetAsList(ListValue
** out_value
) {
126 Value
* Value::DeepCopy() const {
127 // This method should only be getting called for null Values--all subclasses
128 // need to provide their own implementation;.
129 DCHECK(IsType(TYPE_NULL
));
130 return CreateNullValue();
133 bool Value::Equals(const Value
* other
) const {
134 // This method should only be getting called for null Values--all subclasses
135 // need to provide their own implementation;.
136 DCHECK(IsType(TYPE_NULL
));
137 return other
->IsType(TYPE_NULL
);
141 bool Value::Equals(const Value
* a
, const Value
* b
) {
142 if ((a
== NULL
) && (b
== NULL
)) return true;
143 if ((a
== NULL
) ^ (b
== NULL
)) return false;
147 Value::Value(ValueType type
) : type_(type
) {
150 ///////////////////// FundamentalValue ////////////////////
152 FundamentalValue::FundamentalValue(bool in_value
)
153 : Value(TYPE_BOOLEAN
), boolean_value_(in_value
) {
156 FundamentalValue::FundamentalValue(int in_value
)
157 : Value(TYPE_INTEGER
), integer_value_(in_value
) {
160 FundamentalValue::FundamentalValue(double in_value
)
161 : Value(TYPE_DOUBLE
), double_value_(in_value
) {
164 FundamentalValue::~FundamentalValue() {
167 bool FundamentalValue::GetAsBoolean(bool* out_value
) const {
168 if (out_value
&& IsType(TYPE_BOOLEAN
))
169 *out_value
= boolean_value_
;
170 return (IsType(TYPE_BOOLEAN
));
173 bool FundamentalValue::GetAsInteger(int* out_value
) const {
174 if (out_value
&& IsType(TYPE_INTEGER
))
175 *out_value
= integer_value_
;
176 return (IsType(TYPE_INTEGER
));
179 bool FundamentalValue::GetAsDouble(double* out_value
) const {
180 if (out_value
&& IsType(TYPE_DOUBLE
))
181 *out_value
= double_value_
;
182 else if (out_value
&& IsType(TYPE_INTEGER
))
183 *out_value
= integer_value_
;
184 return (IsType(TYPE_DOUBLE
) || IsType(TYPE_INTEGER
));
187 FundamentalValue
* FundamentalValue::DeepCopy() const {
190 return CreateBooleanValue(boolean_value_
);
193 return CreateIntegerValue(integer_value_
);
196 return CreateDoubleValue(double_value_
);
204 bool FundamentalValue::Equals(const Value
* other
) const {
205 if (other
->GetType() != GetType())
211 return GetAsBoolean(&lhs
) && other
->GetAsBoolean(&rhs
) && lhs
== rhs
;
215 return GetAsInteger(&lhs
) && other
->GetAsInteger(&rhs
) && lhs
== rhs
;
219 return GetAsDouble(&lhs
) && other
->GetAsDouble(&rhs
) && lhs
== rhs
;
227 ///////////////////// StringValue ////////////////////
229 StringValue::StringValue(const std::string
& in_value
)
230 : Value(TYPE_STRING
),
232 DCHECK(IsStringUTF8(in_value
));
235 StringValue::StringValue(const string16
& in_value
)
236 : Value(TYPE_STRING
),
237 value_(UTF16ToUTF8(in_value
)) {
240 StringValue::~StringValue() {
243 bool StringValue::GetAsString(std::string
* out_value
) const {
249 bool StringValue::GetAsString(string16
* out_value
) const {
251 *out_value
= UTF8ToUTF16(value_
);
255 StringValue
* StringValue::DeepCopy() const {
256 return CreateStringValue(value_
);
259 bool StringValue::Equals(const Value
* other
) const {
260 if (other
->GetType() != GetType())
262 std::string lhs
, rhs
;
263 return GetAsString(&lhs
) && other
->GetAsString(&rhs
) && lhs
== rhs
;
266 ///////////////////// BinaryValue ////////////////////
268 BinaryValue::~BinaryValue() {
275 BinaryValue
* BinaryValue::Create(char* buffer
, size_t size
) {
279 return new BinaryValue(buffer
, size
);
283 BinaryValue
* BinaryValue::CreateWithCopiedBuffer(const char* buffer
,
288 char* buffer_copy
= new char[size
];
289 memcpy(buffer_copy
, buffer
, size
);
290 return new BinaryValue(buffer_copy
, size
);
293 BinaryValue
* BinaryValue::DeepCopy() const {
294 return CreateWithCopiedBuffer(buffer_
, size_
);
297 bool BinaryValue::Equals(const Value
* other
) const {
298 if (other
->GetType() != GetType())
300 const BinaryValue
* other_binary
= static_cast<const BinaryValue
*>(other
);
301 if (other_binary
->size_
!= size_
)
303 return !memcmp(buffer_
, other_binary
->buffer_
, size_
);
306 BinaryValue::BinaryValue(char* buffer
, size_t size
)
307 : Value(TYPE_BINARY
),
313 ///////////////////// DictionaryValue ////////////////////
315 DictionaryValue::DictionaryValue()
316 : Value(TYPE_DICTIONARY
) {
319 DictionaryValue::~DictionaryValue() {
323 bool DictionaryValue::HasKey(const std::string
& key
) const {
324 DCHECK(IsStringUTF8(key
));
325 ValueMap::const_iterator current_entry
= dictionary_
.find(key
);
326 DCHECK((current_entry
== dictionary_
.end()) || current_entry
->second
);
327 return current_entry
!= dictionary_
.end();
330 void DictionaryValue::Clear() {
331 ValueMap::iterator dict_iterator
= dictionary_
.begin();
332 while (dict_iterator
!= dictionary_
.end()) {
333 delete dict_iterator
->second
;
340 void DictionaryValue::Set(const std::string
& path
, Value
* in_value
) {
341 DCHECK(IsStringUTF8(path
));
344 std::string
current_path(path
);
345 DictionaryValue
* current_dictionary
= this;
346 for (size_t delimiter_position
= current_path
.find('.');
347 delimiter_position
!= std::string::npos
;
348 delimiter_position
= current_path
.find('.')) {
349 // Assume that we're indexing into a dictionary.
350 std::string
key(current_path
, 0, delimiter_position
);
351 DictionaryValue
* child_dictionary
= NULL
;
352 if (!current_dictionary
->GetDictionary(key
, &child_dictionary
)) {
353 child_dictionary
= new DictionaryValue
;
354 current_dictionary
->SetWithoutPathExpansion(key
, child_dictionary
);
357 current_dictionary
= child_dictionary
;
358 current_path
.erase(0, delimiter_position
+ 1);
361 current_dictionary
->SetWithoutPathExpansion(current_path
, in_value
);
364 void DictionaryValue::SetBoolean(const std::string
& path
, bool in_value
) {
365 Set(path
, CreateBooleanValue(in_value
));
368 void DictionaryValue::SetInteger(const std::string
& path
, int in_value
) {
369 Set(path
, CreateIntegerValue(in_value
));
372 void DictionaryValue::SetDouble(const std::string
& path
, double in_value
) {
373 Set(path
, CreateDoubleValue(in_value
));
376 void DictionaryValue::SetString(const std::string
& path
,
377 const std::string
& in_value
) {
378 Set(path
, CreateStringValue(in_value
));
381 void DictionaryValue::SetString(const std::string
& path
,
382 const string16
& in_value
) {
383 Set(path
, CreateStringValue(in_value
));
386 void DictionaryValue::SetWithoutPathExpansion(const std::string
& key
,
388 // If there's an existing value here, we need to delete it, because
389 // we own all our children.
391 DCHECK(dictionary_
[key
] != in_value
); // This would be bogus
392 delete dictionary_
[key
];
395 dictionary_
[key
] = in_value
;
398 bool DictionaryValue::Get(const std::string
& path
, Value
** out_value
) const {
399 DCHECK(IsStringUTF8(path
));
400 std::string
current_path(path
);
401 const DictionaryValue
* current_dictionary
= this;
402 for (size_t delimiter_position
= current_path
.find('.');
403 delimiter_position
!= std::string::npos
;
404 delimiter_position
= current_path
.find('.')) {
405 DictionaryValue
* child_dictionary
= NULL
;
406 if (!current_dictionary
->GetDictionary(
407 current_path
.substr(0, delimiter_position
), &child_dictionary
))
410 current_dictionary
= child_dictionary
;
411 current_path
.erase(0, delimiter_position
+ 1);
414 return current_dictionary
->GetWithoutPathExpansion(current_path
, out_value
);
417 bool DictionaryValue::GetBoolean(const std::string
& path
,
418 bool* bool_value
) const {
420 if (!Get(path
, &value
))
423 return value
->GetAsBoolean(bool_value
);
426 bool DictionaryValue::GetInteger(const std::string
& path
,
427 int* out_value
) const {
429 if (!Get(path
, &value
))
432 return value
->GetAsInteger(out_value
);
435 bool DictionaryValue::GetDouble(const std::string
& path
,
436 double* out_value
) const {
438 if (!Get(path
, &value
))
441 return value
->GetAsDouble(out_value
);
444 bool DictionaryValue::GetString(const std::string
& path
,
445 std::string
* out_value
) const {
447 if (!Get(path
, &value
))
450 return value
->GetAsString(out_value
);
453 bool DictionaryValue::GetString(const std::string
& path
,
454 string16
* out_value
) const {
456 if (!Get(path
, &value
))
459 return value
->GetAsString(out_value
);
462 bool DictionaryValue::GetStringASCII(const std::string
& path
,
463 std::string
* out_value
) const {
465 if (!GetString(path
, &out
))
468 if (!IsStringASCII(out
)) {
473 out_value
->assign(out
);
477 bool DictionaryValue::GetBinary(const std::string
& path
,
478 BinaryValue
** out_value
) const {
480 bool result
= Get(path
, &value
);
481 if (!result
|| !value
->IsType(TYPE_BINARY
))
485 *out_value
= static_cast<BinaryValue
*>(value
);
490 bool DictionaryValue::GetDictionary(const std::string
& path
,
491 DictionaryValue
** out_value
) const {
493 bool result
= Get(path
, &value
);
494 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
498 *out_value
= static_cast<DictionaryValue
*>(value
);
503 bool DictionaryValue::GetList(const std::string
& path
,
504 ListValue
** out_value
) const {
506 bool result
= Get(path
, &value
);
507 if (!result
|| !value
->IsType(TYPE_LIST
))
511 *out_value
= static_cast<ListValue
*>(value
);
516 bool DictionaryValue::GetWithoutPathExpansion(const std::string
& key
,
517 Value
** out_value
) const {
518 DCHECK(IsStringUTF8(key
));
519 ValueMap::const_iterator entry_iterator
= dictionary_
.find(key
);
520 if (entry_iterator
== dictionary_
.end())
523 Value
* entry
= entry_iterator
->second
;
529 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string
& key
,
530 int* out_value
) const {
532 if (!GetWithoutPathExpansion(key
, &value
))
535 return value
->GetAsInteger(out_value
);
538 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string
& key
,
539 double* out_value
) const {
541 if (!GetWithoutPathExpansion(key
, &value
))
544 return value
->GetAsDouble(out_value
);
547 bool DictionaryValue::GetStringWithoutPathExpansion(
548 const std::string
& key
,
549 std::string
* out_value
) const {
551 if (!GetWithoutPathExpansion(key
, &value
))
554 return value
->GetAsString(out_value
);
557 bool DictionaryValue::GetStringWithoutPathExpansion(
558 const std::string
& key
,
559 string16
* out_value
) const {
561 if (!GetWithoutPathExpansion(key
, &value
))
564 return value
->GetAsString(out_value
);
567 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
568 const std::string
& key
,
569 DictionaryValue
** out_value
) const {
571 bool result
= GetWithoutPathExpansion(key
, &value
);
572 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
576 *out_value
= static_cast<DictionaryValue
*>(value
);
581 bool DictionaryValue::GetListWithoutPathExpansion(const std::string
& key
,
582 ListValue
** out_value
) const {
584 bool result
= GetWithoutPathExpansion(key
, &value
);
585 if (!result
|| !value
->IsType(TYPE_LIST
))
589 *out_value
= static_cast<ListValue
*>(value
);
594 bool DictionaryValue::Remove(const std::string
& path
, Value
** out_value
) {
595 DCHECK(IsStringUTF8(path
));
596 std::string
current_path(path
);
597 DictionaryValue
* current_dictionary
= this;
598 size_t delimiter_position
= current_path
.rfind('.');
599 if (delimiter_position
!= std::string::npos
) {
600 if (!GetDictionary(current_path
.substr(0, delimiter_position
),
601 ¤t_dictionary
))
603 current_path
.erase(0, delimiter_position
+ 1);
606 return current_dictionary
->RemoveWithoutPathExpansion(current_path
,
610 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string
& key
,
612 DCHECK(IsStringUTF8(key
));
613 ValueMap::iterator entry_iterator
= dictionary_
.find(key
);
614 if (entry_iterator
== dictionary_
.end())
617 Value
* entry
= entry_iterator
->second
;
622 dictionary_
.erase(entry_iterator
);
626 DictionaryValue
* DictionaryValue::DeepCopyWithoutEmptyChildren() {
627 Value
* copy
= CopyWithoutEmptyChildren(this);
628 return copy
? static_cast<DictionaryValue
*>(copy
) : new DictionaryValue
;
631 void DictionaryValue::MergeDictionary(const DictionaryValue
* dictionary
) {
632 for (DictionaryValue::key_iterator
key(dictionary
->begin_keys());
633 key
!= dictionary
->end_keys(); ++key
) {
635 if (dictionary
->GetWithoutPathExpansion(*key
, &merge_value
)) {
636 // Check whether we have to merge dictionaries.
637 if (merge_value
->IsType(Value::TYPE_DICTIONARY
)) {
638 DictionaryValue
* sub_dict
;
639 if (GetDictionaryWithoutPathExpansion(*key
, &sub_dict
)) {
640 sub_dict
->MergeDictionary(
641 static_cast<const DictionaryValue
*>(merge_value
));
645 // All other cases: Make a copy and hook it up.
646 SetWithoutPathExpansion(*key
, merge_value
->DeepCopy());
651 DictionaryValue
* DictionaryValue::DeepCopy() const {
652 DictionaryValue
* result
= new DictionaryValue
;
654 for (ValueMap::const_iterator
current_entry(dictionary_
.begin());
655 current_entry
!= dictionary_
.end(); ++current_entry
) {
656 result
->SetWithoutPathExpansion(current_entry
->first
,
657 current_entry
->second
->DeepCopy());
663 bool DictionaryValue::Equals(const Value
* other
) const {
664 if (other
->GetType() != GetType())
667 const DictionaryValue
* other_dict
=
668 static_cast<const DictionaryValue
*>(other
);
669 key_iterator
lhs_it(begin_keys());
670 key_iterator
rhs_it(other_dict
->begin_keys());
671 while (lhs_it
!= end_keys() && rhs_it
!= other_dict
->end_keys()) {
674 if (*lhs_it
!= *rhs_it
||
675 !GetWithoutPathExpansion(*lhs_it
, &lhs
) ||
676 !other_dict
->GetWithoutPathExpansion(*rhs_it
, &rhs
) ||
683 if (lhs_it
!= end_keys() || rhs_it
!= other_dict
->end_keys())
689 ///////////////////// ListValue ////////////////////
691 ListValue::ListValue() : Value(TYPE_LIST
) {
694 ListValue::~ListValue() {
698 void ListValue::Clear() {
699 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
704 bool ListValue::Set(size_t index
, Value
* in_value
) {
708 if (index
>= list_
.size()) {
709 // Pad out any intermediate indexes with null settings
710 while (index
> list_
.size())
711 Append(CreateNullValue());
714 DCHECK(list_
[index
] != in_value
);
716 list_
[index
] = in_value
;
721 bool ListValue::Get(size_t index
, Value
** out_value
) const {
722 if (index
>= list_
.size())
726 *out_value
= list_
[index
];
731 bool ListValue::GetBoolean(size_t index
, bool* bool_value
) const {
733 if (!Get(index
, &value
))
736 return value
->GetAsBoolean(bool_value
);
739 bool ListValue::GetInteger(size_t index
, int* out_value
) const {
741 if (!Get(index
, &value
))
744 return value
->GetAsInteger(out_value
);
747 bool ListValue::GetDouble(size_t index
, double* out_value
) const {
749 if (!Get(index
, &value
))
752 return value
->GetAsDouble(out_value
);
755 bool ListValue::GetString(size_t index
, std::string
* out_value
) const {
757 if (!Get(index
, &value
))
760 return value
->GetAsString(out_value
);
763 bool ListValue::GetString(size_t index
, string16
* out_value
) const {
765 if (!Get(index
, &value
))
768 return value
->GetAsString(out_value
);
771 bool ListValue::GetBinary(size_t index
, BinaryValue
** out_value
) const {
773 bool result
= Get(index
, &value
);
774 if (!result
|| !value
->IsType(TYPE_BINARY
))
778 *out_value
= static_cast<BinaryValue
*>(value
);
783 bool ListValue::GetDictionary(size_t index
, DictionaryValue
** out_value
) const {
785 bool result
= Get(index
, &value
);
786 if (!result
|| !value
->IsType(TYPE_DICTIONARY
))
790 *out_value
= static_cast<DictionaryValue
*>(value
);
795 bool ListValue::GetList(size_t index
, ListValue
** out_value
) const {
797 bool result
= Get(index
, &value
);
798 if (!result
|| !value
->IsType(TYPE_LIST
))
802 *out_value
= static_cast<ListValue
*>(value
);
807 bool ListValue::Remove(size_t index
, Value
** out_value
) {
808 if (index
>= list_
.size())
812 *out_value
= list_
[index
];
816 list_
.erase(list_
.begin() + index
);
820 int ListValue::Remove(const Value
& value
) {
821 for (ValueVector::iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
822 if ((*i
)->Equals(&value
)) {
823 size_t index
= i
- list_
.begin();
827 // TODO(anyone): Returning a signed int type here is just wrong.
828 // Change this interface to return a size_t.
829 DCHECK(index
<= INT_MAX
);
830 int return_index
= static_cast<int>(index
);
837 void ListValue::Append(Value
* in_value
) {
839 list_
.push_back(in_value
);
842 bool ListValue::AppendIfNotPresent(Value
* in_value
) {
844 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
) {
845 if ((*i
)->Equals(in_value
)) {
850 list_
.push_back(in_value
);
854 bool ListValue::Insert(size_t index
, Value
* in_value
) {
856 if (index
> list_
.size())
859 list_
.insert(list_
.begin() + index
, in_value
);
863 bool ListValue::GetAsList(ListValue
** out_value
) {
869 ListValue
* ListValue::DeepCopy() const {
870 ListValue
* result
= new ListValue
;
872 for (ValueVector::const_iterator
i(list_
.begin()); i
!= list_
.end(); ++i
)
873 result
->Append((*i
)->DeepCopy());
878 bool ListValue::Equals(const Value
* other
) const {
879 if (other
->GetType() != GetType())
882 const ListValue
* other_list
=
883 static_cast<const ListValue
*>(other
);
884 const_iterator lhs_it
, rhs_it
;
885 for (lhs_it
= begin(), rhs_it
= other_list
->begin();
886 lhs_it
!= end() && rhs_it
!= other_list
->end();
887 ++lhs_it
, ++rhs_it
) {
888 if (!(*lhs_it
)->Equals(*rhs_it
))
891 if (lhs_it
!= end() || rhs_it
!= other_list
->end())
897 ValueSerializer::~ValueSerializer() {