Allow silent update for default-installed apps.
[chromium-blink-merge.git] / base / values.cc
blob061b7a1a0253454d51a14fb18b5328bcac99cf49
1 // Copyright (c) 2012 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 <string.h>
9 #include <algorithm>
10 #include <ostream>
12 #include "base/float_util.h"
13 #include "base/json/json_writer.h"
14 #include "base/logging.h"
15 #include "base/move.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
19 namespace base {
21 namespace {
23 // Make a deep copy of |node|, but don't include empty lists or dictionaries
24 // in the copy. It's possible for this function to return NULL and it
25 // expects |node| to always be non-NULL.
26 Value* CopyWithoutEmptyChildren(const Value* node) {
27 DCHECK(node);
28 switch (node->GetType()) {
29 case Value::TYPE_LIST: {
30 const ListValue* list = static_cast<const ListValue*>(node);
31 ListValue* copy = new ListValue;
32 for (ListValue::const_iterator it = list->begin(); it != list->end();
33 ++it) {
34 Value* child_copy = CopyWithoutEmptyChildren(*it);
35 if (child_copy)
36 copy->Append(child_copy);
38 if (!copy->empty())
39 return copy;
41 delete copy;
42 return NULL;
45 case Value::TYPE_DICTIONARY: {
46 const DictionaryValue* dict = static_cast<const DictionaryValue*>(node);
47 DictionaryValue* copy = new DictionaryValue;
48 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) {
49 Value* child_copy = CopyWithoutEmptyChildren(&it.value());
50 if (child_copy)
51 copy->SetWithoutPathExpansion(it.key(), child_copy);
53 if (!copy->empty())
54 return copy;
56 delete copy;
57 return NULL;
60 default:
61 // For everything else, just make a copy.
62 return node->DeepCopy();
66 // A small functor for comparing Values for std::find_if and similar.
67 class ValueEquals {
68 public:
69 // Pass the value against which all consecutive calls of the () operator will
70 // compare their argument to. This Value object must not be destroyed while
71 // the ValueEquals is in use.
72 explicit ValueEquals(const Value* first) : first_(first) { }
74 bool operator ()(const Value* second) const {
75 return first_->Equals(second);
78 private:
79 const Value* first_;
82 } // namespace
84 Value::~Value() {
87 // static
88 Value* Value::CreateNullValue() {
89 return new Value(TYPE_NULL);
92 bool Value::GetAsBinary(const BinaryValue** out_value) const {
93 return false;
96 bool Value::GetAsBoolean(bool* out_value) const {
97 return false;
100 bool Value::GetAsInteger(int* out_value) const {
101 return false;
104 bool Value::GetAsDouble(double* out_value) const {
105 return false;
108 bool Value::GetAsString(std::string* out_value) const {
109 return false;
112 bool Value::GetAsString(string16* out_value) const {
113 return false;
116 bool Value::GetAsString(const StringValue** out_value) const {
117 return false;
120 bool Value::GetAsList(ListValue** out_value) {
121 return false;
124 bool Value::GetAsList(const ListValue** out_value) const {
125 return false;
128 bool Value::GetAsDictionary(DictionaryValue** out_value) {
129 return false;
132 bool Value::GetAsDictionary(const DictionaryValue** out_value) const {
133 return false;
136 Value* Value::DeepCopy() const {
137 // This method should only be getting called for null Values--all subclasses
138 // need to provide their own implementation;.
139 DCHECK(IsType(TYPE_NULL));
140 return CreateNullValue();
143 bool Value::Equals(const Value* other) const {
144 // This method should only be getting called for null Values--all subclasses
145 // need to provide their own implementation;.
146 DCHECK(IsType(TYPE_NULL));
147 return other->IsType(TYPE_NULL);
150 // static
151 bool Value::Equals(const Value* a, const Value* b) {
152 if ((a == NULL) && (b == NULL)) return true;
153 if ((a == NULL) ^ (b == NULL)) return false;
154 return a->Equals(b);
157 Value::Value(Type type) : type_(type) {}
159 Value::Value(const Value& that) : type_(that.type_) {}
161 Value& Value::operator=(const Value& that) {
162 type_ = that.type_;
163 return *this;
166 ///////////////////// FundamentalValue ////////////////////
168 FundamentalValue::FundamentalValue(bool in_value)
169 : Value(TYPE_BOOLEAN), boolean_value_(in_value) {
172 FundamentalValue::FundamentalValue(int in_value)
173 : Value(TYPE_INTEGER), integer_value_(in_value) {
176 FundamentalValue::FundamentalValue(double in_value)
177 : Value(TYPE_DOUBLE), double_value_(in_value) {
178 if (!IsFinite(double_value_)) {
179 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
180 << "values cannot be represented in JSON";
181 double_value_ = 0.0;
185 FundamentalValue::~FundamentalValue() {
188 bool FundamentalValue::GetAsBoolean(bool* out_value) const {
189 if (out_value && IsType(TYPE_BOOLEAN))
190 *out_value = boolean_value_;
191 return (IsType(TYPE_BOOLEAN));
194 bool FundamentalValue::GetAsInteger(int* out_value) const {
195 if (out_value && IsType(TYPE_INTEGER))
196 *out_value = integer_value_;
197 return (IsType(TYPE_INTEGER));
200 bool FundamentalValue::GetAsDouble(double* out_value) const {
201 if (out_value && IsType(TYPE_DOUBLE))
202 *out_value = double_value_;
203 else if (out_value && IsType(TYPE_INTEGER))
204 *out_value = integer_value_;
205 return (IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER));
208 FundamentalValue* FundamentalValue::DeepCopy() const {
209 switch (GetType()) {
210 case TYPE_BOOLEAN:
211 return new FundamentalValue(boolean_value_);
213 case TYPE_INTEGER:
214 return new FundamentalValue(integer_value_);
216 case TYPE_DOUBLE:
217 return new FundamentalValue(double_value_);
219 default:
220 NOTREACHED();
221 return NULL;
225 bool FundamentalValue::Equals(const Value* other) const {
226 if (other->GetType() != GetType())
227 return false;
229 switch (GetType()) {
230 case TYPE_BOOLEAN: {
231 bool lhs, rhs;
232 return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs;
234 case TYPE_INTEGER: {
235 int lhs, rhs;
236 return GetAsInteger(&lhs) && other->GetAsInteger(&rhs) && lhs == rhs;
238 case TYPE_DOUBLE: {
239 double lhs, rhs;
240 return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs;
242 default:
243 NOTREACHED();
244 return false;
248 ///////////////////// StringValue ////////////////////
250 StringValue::StringValue(const std::string& in_value)
251 : Value(TYPE_STRING),
252 value_(in_value) {
253 DCHECK(IsStringUTF8(in_value));
256 StringValue::StringValue(const string16& in_value)
257 : Value(TYPE_STRING),
258 value_(UTF16ToUTF8(in_value)) {
261 StringValue::~StringValue() {
264 std::string* StringValue::GetString() {
265 return &value_;
268 const std::string& StringValue::GetString() const {
269 return value_;
272 bool StringValue::GetAsString(std::string* out_value) const {
273 if (out_value)
274 *out_value = value_;
275 return true;
278 bool StringValue::GetAsString(string16* out_value) const {
279 if (out_value)
280 *out_value = UTF8ToUTF16(value_);
281 return true;
284 bool StringValue::GetAsString(const StringValue** out_value) const {
285 if (out_value)
286 *out_value = this;
287 return true;
290 StringValue* StringValue::DeepCopy() const {
291 return new StringValue(value_);
294 bool StringValue::Equals(const Value* other) const {
295 if (other->GetType() != GetType())
296 return false;
297 std::string lhs, rhs;
298 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
301 ///////////////////// BinaryValue ////////////////////
303 BinaryValue::BinaryValue()
304 : Value(TYPE_BINARY),
305 size_(0) {
308 BinaryValue::BinaryValue(scoped_ptr<char[]> buffer, size_t size)
309 : Value(TYPE_BINARY),
310 buffer_(buffer.Pass()),
311 size_(size) {
314 BinaryValue::~BinaryValue() {
317 // static
318 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer,
319 size_t size) {
320 char* buffer_copy = new char[size];
321 memcpy(buffer_copy, buffer, size);
322 scoped_ptr<char[]> scoped_buffer_copy(buffer_copy);
323 return new BinaryValue(scoped_buffer_copy.Pass(), size);
326 bool BinaryValue::GetAsBinary(const BinaryValue** out_value) const {
327 if (out_value)
328 *out_value = this;
329 return true;
332 BinaryValue* BinaryValue::DeepCopy() const {
333 return CreateWithCopiedBuffer(buffer_.get(), size_);
336 bool BinaryValue::Equals(const Value* other) const {
337 if (other->GetType() != GetType())
338 return false;
339 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other);
340 if (other_binary->size_ != size_)
341 return false;
342 return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_);
345 ///////////////////// DictionaryValue ////////////////////
347 DictionaryValue::DictionaryValue()
348 : Value(TYPE_DICTIONARY) {
351 DictionaryValue::~DictionaryValue() {
352 Clear();
355 bool DictionaryValue::GetAsDictionary(DictionaryValue** out_value) {
356 if (out_value)
357 *out_value = this;
358 return true;
361 bool DictionaryValue::GetAsDictionary(const DictionaryValue** out_value) const {
362 if (out_value)
363 *out_value = this;
364 return true;
367 bool DictionaryValue::HasKey(const std::string& key) const {
368 DCHECK(IsStringUTF8(key));
369 ValueMap::const_iterator current_entry = dictionary_.find(key);
370 DCHECK((current_entry == dictionary_.end()) || current_entry->second);
371 return current_entry != dictionary_.end();
374 void DictionaryValue::Clear() {
375 ValueMap::iterator dict_iterator = dictionary_.begin();
376 while (dict_iterator != dictionary_.end()) {
377 delete dict_iterator->second;
378 ++dict_iterator;
381 dictionary_.clear();
384 void DictionaryValue::Set(const std::string& path, scoped_ptr<Value> in_value) {
385 DCHECK(IsStringUTF8(path));
386 DCHECK(in_value);
388 std::string current_path(path);
389 DictionaryValue* current_dictionary = this;
390 for (size_t delimiter_position = current_path.find('.');
391 delimiter_position != std::string::npos;
392 delimiter_position = current_path.find('.')) {
393 // Assume that we're indexing into a dictionary.
394 std::string key(current_path, 0, delimiter_position);
395 DictionaryValue* child_dictionary = NULL;
396 if (!current_dictionary->GetDictionary(key, &child_dictionary)) {
397 child_dictionary = new DictionaryValue;
398 current_dictionary->SetWithoutPathExpansion(key, child_dictionary);
401 current_dictionary = child_dictionary;
402 current_path.erase(0, delimiter_position + 1);
405 current_dictionary->SetWithoutPathExpansion(current_path, in_value.Pass());
408 void DictionaryValue::Set(const std::string& path, Value* in_value) {
409 Set(path, make_scoped_ptr(in_value));
412 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) {
413 Set(path, new FundamentalValue(in_value));
416 void DictionaryValue::SetInteger(const std::string& path, int in_value) {
417 Set(path, new FundamentalValue(in_value));
420 void DictionaryValue::SetDouble(const std::string& path, double in_value) {
421 Set(path, new FundamentalValue(in_value));
424 void DictionaryValue::SetString(const std::string& path,
425 const std::string& in_value) {
426 Set(path, new StringValue(in_value));
429 void DictionaryValue::SetString(const std::string& path,
430 const string16& in_value) {
431 Set(path, new StringValue(in_value));
434 void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
435 scoped_ptr<Value> in_value) {
436 Value* bare_ptr = in_value.release();
437 // If there's an existing value here, we need to delete it, because
438 // we own all our children.
439 std::pair<ValueMap::iterator, bool> ins_res =
440 dictionary_.insert(std::make_pair(key, bare_ptr));
441 if (!ins_res.second) {
442 DCHECK_NE(ins_res.first->second, bare_ptr); // This would be bogus
443 delete ins_res.first->second;
444 ins_res.first->second = bare_ptr;
448 void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
449 Value* in_value) {
450 SetWithoutPathExpansion(key, make_scoped_ptr(in_value));
453 void DictionaryValue::SetBooleanWithoutPathExpansion(
454 const std::string& path, bool in_value) {
455 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
458 void DictionaryValue::SetIntegerWithoutPathExpansion(
459 const std::string& path, int in_value) {
460 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
463 void DictionaryValue::SetDoubleWithoutPathExpansion(
464 const std::string& path, double in_value) {
465 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
468 void DictionaryValue::SetStringWithoutPathExpansion(
469 const std::string& path, const std::string& in_value) {
470 SetWithoutPathExpansion(path, new StringValue(in_value));
473 void DictionaryValue::SetStringWithoutPathExpansion(
474 const std::string& path, const string16& in_value) {
475 SetWithoutPathExpansion(path, new StringValue(in_value));
478 bool DictionaryValue::Get(const std::string& path,
479 const Value** out_value) const {
480 DCHECK(IsStringUTF8(path));
481 std::string current_path(path);
482 const DictionaryValue* current_dictionary = this;
483 for (size_t delimiter_position = current_path.find('.');
484 delimiter_position != std::string::npos;
485 delimiter_position = current_path.find('.')) {
486 const DictionaryValue* child_dictionary = NULL;
487 if (!current_dictionary->GetDictionary(
488 current_path.substr(0, delimiter_position), &child_dictionary))
489 return false;
491 current_dictionary = child_dictionary;
492 current_path.erase(0, delimiter_position + 1);
495 return current_dictionary->GetWithoutPathExpansion(current_path, out_value);
498 bool DictionaryValue::Get(const std::string& path, Value** out_value) {
499 return static_cast<const DictionaryValue&>(*this).Get(
500 path,
501 const_cast<const Value**>(out_value));
504 bool DictionaryValue::GetBoolean(const std::string& path,
505 bool* bool_value) const {
506 const Value* value;
507 if (!Get(path, &value))
508 return false;
510 return value->GetAsBoolean(bool_value);
513 bool DictionaryValue::GetInteger(const std::string& path,
514 int* out_value) const {
515 const Value* value;
516 if (!Get(path, &value))
517 return false;
519 return value->GetAsInteger(out_value);
522 bool DictionaryValue::GetDouble(const std::string& path,
523 double* out_value) const {
524 const Value* value;
525 if (!Get(path, &value))
526 return false;
528 return value->GetAsDouble(out_value);
531 bool DictionaryValue::GetString(const std::string& path,
532 std::string* out_value) const {
533 const Value* value;
534 if (!Get(path, &value))
535 return false;
537 return value->GetAsString(out_value);
540 bool DictionaryValue::GetString(const std::string& path,
541 string16* out_value) const {
542 const Value* value;
543 if (!Get(path, &value))
544 return false;
546 return value->GetAsString(out_value);
549 bool DictionaryValue::GetStringASCII(const std::string& path,
550 std::string* out_value) const {
551 std::string out;
552 if (!GetString(path, &out))
553 return false;
555 if (!IsStringASCII(out)) {
556 NOTREACHED();
557 return false;
560 out_value->assign(out);
561 return true;
564 bool DictionaryValue::GetBinary(const std::string& path,
565 const BinaryValue** out_value) const {
566 const Value* value;
567 bool result = Get(path, &value);
568 if (!result || !value->IsType(TYPE_BINARY))
569 return false;
571 if (out_value)
572 *out_value = static_cast<const BinaryValue*>(value);
574 return true;
577 bool DictionaryValue::GetBinary(const std::string& path,
578 BinaryValue** out_value) {
579 return static_cast<const DictionaryValue&>(*this).GetBinary(
580 path,
581 const_cast<const BinaryValue**>(out_value));
584 bool DictionaryValue::GetDictionary(const std::string& path,
585 const DictionaryValue** out_value) const {
586 const Value* value;
587 bool result = Get(path, &value);
588 if (!result || !value->IsType(TYPE_DICTIONARY))
589 return false;
591 if (out_value)
592 *out_value = static_cast<const DictionaryValue*>(value);
594 return true;
597 bool DictionaryValue::GetDictionary(const std::string& path,
598 DictionaryValue** out_value) {
599 return static_cast<const DictionaryValue&>(*this).GetDictionary(
600 path,
601 const_cast<const DictionaryValue**>(out_value));
604 bool DictionaryValue::GetList(const std::string& path,
605 const ListValue** out_value) const {
606 const Value* value;
607 bool result = Get(path, &value);
608 if (!result || !value->IsType(TYPE_LIST))
609 return false;
611 if (out_value)
612 *out_value = static_cast<const ListValue*>(value);
614 return true;
617 bool DictionaryValue::GetList(const std::string& path, ListValue** out_value) {
618 return static_cast<const DictionaryValue&>(*this).GetList(
619 path,
620 const_cast<const ListValue**>(out_value));
623 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
624 const Value** out_value) const {
625 DCHECK(IsStringUTF8(key));
626 ValueMap::const_iterator entry_iterator = dictionary_.find(key);
627 if (entry_iterator == dictionary_.end())
628 return false;
630 const Value* entry = entry_iterator->second;
631 if (out_value)
632 *out_value = entry;
633 return true;
636 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
637 Value** out_value) {
638 return static_cast<const DictionaryValue&>(*this).GetWithoutPathExpansion(
639 key,
640 const_cast<const Value**>(out_value));
643 bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string& key,
644 bool* out_value) const {
645 const Value* value;
646 if (!GetWithoutPathExpansion(key, &value))
647 return false;
649 return value->GetAsBoolean(out_value);
652 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string& key,
653 int* out_value) const {
654 const Value* value;
655 if (!GetWithoutPathExpansion(key, &value))
656 return false;
658 return value->GetAsInteger(out_value);
661 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string& key,
662 double* out_value) const {
663 const Value* value;
664 if (!GetWithoutPathExpansion(key, &value))
665 return false;
667 return value->GetAsDouble(out_value);
670 bool DictionaryValue::GetStringWithoutPathExpansion(
671 const std::string& key,
672 std::string* out_value) const {
673 const Value* value;
674 if (!GetWithoutPathExpansion(key, &value))
675 return false;
677 return value->GetAsString(out_value);
680 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string& key,
681 string16* out_value) const {
682 const Value* value;
683 if (!GetWithoutPathExpansion(key, &value))
684 return false;
686 return value->GetAsString(out_value);
689 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
690 const std::string& key,
691 const DictionaryValue** out_value) const {
692 const Value* value;
693 bool result = GetWithoutPathExpansion(key, &value);
694 if (!result || !value->IsType(TYPE_DICTIONARY))
695 return false;
697 if (out_value)
698 *out_value = static_cast<const DictionaryValue*>(value);
700 return true;
703 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
704 const std::string& key,
705 DictionaryValue** out_value) {
706 const DictionaryValue& const_this =
707 static_cast<const DictionaryValue&>(*this);
708 return const_this.GetDictionaryWithoutPathExpansion(
709 key,
710 const_cast<const DictionaryValue**>(out_value));
713 bool DictionaryValue::GetListWithoutPathExpansion(
714 const std::string& key,
715 const ListValue** out_value) const {
716 const Value* value;
717 bool result = GetWithoutPathExpansion(key, &value);
718 if (!result || !value->IsType(TYPE_LIST))
719 return false;
721 if (out_value)
722 *out_value = static_cast<const ListValue*>(value);
724 return true;
727 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key,
728 ListValue** out_value) {
729 return
730 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion(
731 key,
732 const_cast<const ListValue**>(out_value));
735 bool DictionaryValue::Remove(const std::string& path,
736 scoped_ptr<Value>* out_value) {
737 DCHECK(IsStringUTF8(path));
738 std::string current_path(path);
739 DictionaryValue* current_dictionary = this;
740 size_t delimiter_position = current_path.rfind('.');
741 if (delimiter_position != std::string::npos) {
742 if (!GetDictionary(current_path.substr(0, delimiter_position),
743 &current_dictionary))
744 return false;
745 current_path.erase(0, delimiter_position + 1);
748 return current_dictionary->RemoveWithoutPathExpansion(current_path,
749 out_value);
752 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string& key,
753 scoped_ptr<Value>* out_value) {
754 DCHECK(IsStringUTF8(key));
755 ValueMap::iterator entry_iterator = dictionary_.find(key);
756 if (entry_iterator == dictionary_.end())
757 return false;
759 Value* entry = entry_iterator->second;
760 if (out_value)
761 out_value->reset(entry);
762 else
763 delete entry;
764 dictionary_.erase(entry_iterator);
765 return true;
768 bool DictionaryValue::RemovePath(const std::string& path,
769 scoped_ptr<Value>* out_value) {
770 bool result = false;
771 size_t delimiter_position = path.find('.');
773 if (delimiter_position == std::string::npos)
774 return RemoveWithoutPathExpansion(path, out_value);
776 const std::string subdict_path = path.substr(0, delimiter_position);
777 DictionaryValue* subdict = NULL;
778 if (!GetDictionary(subdict_path, &subdict))
779 return false;
780 result = subdict->RemovePath(path.substr(delimiter_position + 1),
781 out_value);
782 if (result && subdict->empty())
783 RemoveWithoutPathExpansion(subdict_path, NULL);
785 return result;
788 DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() const {
789 Value* copy = CopyWithoutEmptyChildren(this);
790 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue;
793 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) {
794 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) {
795 const Value* merge_value = &it.value();
796 // Check whether we have to merge dictionaries.
797 if (merge_value->IsType(Value::TYPE_DICTIONARY)) {
798 DictionaryValue* sub_dict;
799 if (GetDictionaryWithoutPathExpansion(it.key(), &sub_dict)) {
800 sub_dict->MergeDictionary(
801 static_cast<const DictionaryValue*>(merge_value));
802 continue;
805 // All other cases: Make a copy and hook it up.
806 SetWithoutPathExpansion(it.key(), merge_value->DeepCopy());
810 void DictionaryValue::Swap(DictionaryValue* other) {
811 dictionary_.swap(other->dictionary_);
814 DictionaryValue::Iterator::Iterator(const DictionaryValue& target)
815 : target_(target),
816 it_(target.dictionary_.begin()) {}
818 DictionaryValue::Iterator::~Iterator() {}
820 DictionaryValue* DictionaryValue::DeepCopy() const {
821 DictionaryValue* result = new DictionaryValue;
823 for (ValueMap::const_iterator current_entry(dictionary_.begin());
824 current_entry != dictionary_.end(); ++current_entry) {
825 result->SetWithoutPathExpansion(current_entry->first,
826 current_entry->second->DeepCopy());
829 return result;
832 bool DictionaryValue::Equals(const Value* other) const {
833 if (other->GetType() != GetType())
834 return false;
836 const DictionaryValue* other_dict =
837 static_cast<const DictionaryValue*>(other);
838 Iterator lhs_it(*this);
839 Iterator rhs_it(*other_dict);
840 while (!lhs_it.IsAtEnd() && !rhs_it.IsAtEnd()) {
841 if (lhs_it.key() != rhs_it.key() ||
842 !lhs_it.value().Equals(&rhs_it.value())) {
843 return false;
845 lhs_it.Advance();
846 rhs_it.Advance();
848 if (!lhs_it.IsAtEnd() || !rhs_it.IsAtEnd())
849 return false;
851 return true;
854 ///////////////////// ListValue ////////////////////
856 ListValue::ListValue() : Value(TYPE_LIST) {
859 ListValue::~ListValue() {
860 Clear();
863 void ListValue::Clear() {
864 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i)
865 delete *i;
866 list_.clear();
869 bool ListValue::Set(size_t index, Value* in_value) {
870 if (!in_value)
871 return false;
873 if (index >= list_.size()) {
874 // Pad out any intermediate indexes with null settings
875 while (index > list_.size())
876 Append(CreateNullValue());
877 Append(in_value);
878 } else {
879 DCHECK(list_[index] != in_value);
880 delete list_[index];
881 list_[index] = in_value;
883 return true;
886 bool ListValue::Get(size_t index, const Value** out_value) const {
887 if (index >= list_.size())
888 return false;
890 if (out_value)
891 *out_value = list_[index];
893 return true;
896 bool ListValue::Get(size_t index, Value** out_value) {
897 return static_cast<const ListValue&>(*this).Get(
898 index,
899 const_cast<const Value**>(out_value));
902 bool ListValue::GetBoolean(size_t index, bool* bool_value) const {
903 const Value* value;
904 if (!Get(index, &value))
905 return false;
907 return value->GetAsBoolean(bool_value);
910 bool ListValue::GetInteger(size_t index, int* out_value) const {
911 const Value* value;
912 if (!Get(index, &value))
913 return false;
915 return value->GetAsInteger(out_value);
918 bool ListValue::GetDouble(size_t index, double* out_value) const {
919 const Value* value;
920 if (!Get(index, &value))
921 return false;
923 return value->GetAsDouble(out_value);
926 bool ListValue::GetString(size_t index, std::string* out_value) const {
927 const Value* value;
928 if (!Get(index, &value))
929 return false;
931 return value->GetAsString(out_value);
934 bool ListValue::GetString(size_t index, string16* out_value) const {
935 const Value* value;
936 if (!Get(index, &value))
937 return false;
939 return value->GetAsString(out_value);
942 bool ListValue::GetBinary(size_t index, const BinaryValue** out_value) const {
943 const Value* value;
944 bool result = Get(index, &value);
945 if (!result || !value->IsType(TYPE_BINARY))
946 return false;
948 if (out_value)
949 *out_value = static_cast<const BinaryValue*>(value);
951 return true;
954 bool ListValue::GetBinary(size_t index, BinaryValue** out_value) {
955 return static_cast<const ListValue&>(*this).GetBinary(
956 index,
957 const_cast<const BinaryValue**>(out_value));
960 bool ListValue::GetDictionary(size_t index,
961 const DictionaryValue** out_value) const {
962 const Value* value;
963 bool result = Get(index, &value);
964 if (!result || !value->IsType(TYPE_DICTIONARY))
965 return false;
967 if (out_value)
968 *out_value = static_cast<const DictionaryValue*>(value);
970 return true;
973 bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) {
974 return static_cast<const ListValue&>(*this).GetDictionary(
975 index,
976 const_cast<const DictionaryValue**>(out_value));
979 bool ListValue::GetList(size_t index, const ListValue** out_value) const {
980 const Value* value;
981 bool result = Get(index, &value);
982 if (!result || !value->IsType(TYPE_LIST))
983 return false;
985 if (out_value)
986 *out_value = static_cast<const ListValue*>(value);
988 return true;
991 bool ListValue::GetList(size_t index, ListValue** out_value) {
992 return static_cast<const ListValue&>(*this).GetList(
993 index,
994 const_cast<const ListValue**>(out_value));
997 bool ListValue::Remove(size_t index, scoped_ptr<Value>* out_value) {
998 if (index >= list_.size())
999 return false;
1001 if (out_value)
1002 out_value->reset(list_[index]);
1003 else
1004 delete list_[index];
1006 list_.erase(list_.begin() + index);
1007 return true;
1010 bool ListValue::Remove(const Value& value, size_t* index) {
1011 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i) {
1012 if ((*i)->Equals(&value)) {
1013 size_t previous_index = i - list_.begin();
1014 delete *i;
1015 list_.erase(i);
1017 if (index)
1018 *index = previous_index;
1019 return true;
1022 return false;
1025 ListValue::iterator ListValue::Erase(iterator iter,
1026 scoped_ptr<Value>* out_value) {
1027 if (out_value)
1028 out_value->reset(*iter);
1029 else
1030 delete *iter;
1032 return list_.erase(iter);
1035 void ListValue::Append(Value* in_value) {
1036 DCHECK(in_value);
1037 list_.push_back(in_value);
1040 void ListValue::AppendBoolean(bool in_value) {
1041 Append(new FundamentalValue(in_value));
1044 void ListValue::AppendInteger(int in_value) {
1045 Append(new FundamentalValue(in_value));
1048 void ListValue::AppendDouble(double in_value) {
1049 Append(new FundamentalValue(in_value));
1052 void ListValue::AppendString(const std::string& in_value) {
1053 Append(new StringValue(in_value));
1056 void ListValue::AppendString(const string16& in_value) {
1057 Append(new StringValue(in_value));
1060 void ListValue::AppendStrings(const std::vector<std::string>& in_values) {
1061 for (std::vector<std::string>::const_iterator it = in_values.begin();
1062 it != in_values.end(); ++it) {
1063 AppendString(*it);
1067 void ListValue::AppendStrings(const std::vector<string16>& in_values) {
1068 for (std::vector<string16>::const_iterator it = in_values.begin();
1069 it != in_values.end(); ++it) {
1070 AppendString(*it);
1074 bool ListValue::AppendIfNotPresent(Value* in_value) {
1075 DCHECK(in_value);
1076 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) {
1077 if ((*i)->Equals(in_value)) {
1078 delete in_value;
1079 return false;
1082 list_.push_back(in_value);
1083 return true;
1086 bool ListValue::Insert(size_t index, Value* in_value) {
1087 DCHECK(in_value);
1088 if (index > list_.size())
1089 return false;
1091 list_.insert(list_.begin() + index, in_value);
1092 return true;
1095 ListValue::const_iterator ListValue::Find(const Value& value) const {
1096 return std::find_if(list_.begin(), list_.end(), ValueEquals(&value));
1099 void ListValue::Swap(ListValue* other) {
1100 list_.swap(other->list_);
1103 bool ListValue::GetAsList(ListValue** out_value) {
1104 if (out_value)
1105 *out_value = this;
1106 return true;
1109 bool ListValue::GetAsList(const ListValue** out_value) const {
1110 if (out_value)
1111 *out_value = this;
1112 return true;
1115 ListValue* ListValue::DeepCopy() const {
1116 ListValue* result = new ListValue;
1118 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i)
1119 result->Append((*i)->DeepCopy());
1121 return result;
1124 bool ListValue::Equals(const Value* other) const {
1125 if (other->GetType() != GetType())
1126 return false;
1128 const ListValue* other_list =
1129 static_cast<const ListValue*>(other);
1130 const_iterator lhs_it, rhs_it;
1131 for (lhs_it = begin(), rhs_it = other_list->begin();
1132 lhs_it != end() && rhs_it != other_list->end();
1133 ++lhs_it, ++rhs_it) {
1134 if (!(*lhs_it)->Equals(*rhs_it))
1135 return false;
1137 if (lhs_it != end() || rhs_it != other_list->end())
1138 return false;
1140 return true;
1143 ValueSerializer::~ValueSerializer() {
1146 std::ostream& operator<<(std::ostream& out, const Value& value) {
1147 std::string json;
1148 JSONWriter::WriteWithOptions(&value,
1149 JSONWriter::OPTIONS_PRETTY_PRINT,
1150 &json);
1151 return out << json;
1154 } // namespace base