Update comments of TabObserver#onLoadStarted and rename onContentChanged
[chromium-blink-merge.git] / base / values.cc
blobebd7157d3c9cc352511f2b7c54521ad79b140fe2
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 <cmath>
11 #include <ostream>
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 scoped_ptr<Value> Value::CreateNullValue() {
89 return make_scoped_ptr(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().release();
143 scoped_ptr<Value> Value::CreateDeepCopy() const {
144 return make_scoped_ptr(DeepCopy());
147 bool Value::Equals(const Value* other) const {
148 // This method should only be getting called for null Values--all subclasses
149 // need to provide their own implementation;.
150 DCHECK(IsType(TYPE_NULL));
151 return other->IsType(TYPE_NULL);
154 // static
155 bool Value::Equals(const Value* a, const Value* b) {
156 if ((a == NULL) && (b == NULL)) return true;
157 if ((a == NULL) ^ (b == NULL)) return false;
158 return a->Equals(b);
161 Value::Value(Type type) : type_(type) {}
163 Value::Value(const Value& that) : type_(that.type_) {}
165 Value& Value::operator=(const Value& that) {
166 type_ = that.type_;
167 return *this;
170 ///////////////////// FundamentalValue ////////////////////
172 FundamentalValue::FundamentalValue(bool in_value)
173 : Value(TYPE_BOOLEAN), boolean_value_(in_value) {
176 FundamentalValue::FundamentalValue(int in_value)
177 : Value(TYPE_INTEGER), integer_value_(in_value) {
180 FundamentalValue::FundamentalValue(double in_value)
181 : Value(TYPE_DOUBLE), double_value_(in_value) {
182 if (!std::isfinite(double_value_)) {
183 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
184 << "values cannot be represented in JSON";
185 double_value_ = 0.0;
189 FundamentalValue::~FundamentalValue() {
192 bool FundamentalValue::GetAsBoolean(bool* out_value) const {
193 if (out_value && IsType(TYPE_BOOLEAN))
194 *out_value = boolean_value_;
195 return (IsType(TYPE_BOOLEAN));
198 bool FundamentalValue::GetAsInteger(int* out_value) const {
199 if (out_value && IsType(TYPE_INTEGER))
200 *out_value = integer_value_;
201 return (IsType(TYPE_INTEGER));
204 bool FundamentalValue::GetAsDouble(double* out_value) const {
205 if (out_value && IsType(TYPE_DOUBLE))
206 *out_value = double_value_;
207 else if (out_value && IsType(TYPE_INTEGER))
208 *out_value = integer_value_;
209 return (IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER));
212 FundamentalValue* FundamentalValue::DeepCopy() const {
213 switch (GetType()) {
214 case TYPE_BOOLEAN:
215 return new FundamentalValue(boolean_value_);
217 case TYPE_INTEGER:
218 return new FundamentalValue(integer_value_);
220 case TYPE_DOUBLE:
221 return new FundamentalValue(double_value_);
223 default:
224 NOTREACHED();
225 return NULL;
229 bool FundamentalValue::Equals(const Value* other) const {
230 if (other->GetType() != GetType())
231 return false;
233 switch (GetType()) {
234 case TYPE_BOOLEAN: {
235 bool lhs, rhs;
236 return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs;
238 case TYPE_INTEGER: {
239 int lhs, rhs;
240 return GetAsInteger(&lhs) && other->GetAsInteger(&rhs) && lhs == rhs;
242 case TYPE_DOUBLE: {
243 double lhs, rhs;
244 return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs;
246 default:
247 NOTREACHED();
248 return false;
252 ///////////////////// StringValue ////////////////////
254 StringValue::StringValue(const std::string& in_value)
255 : Value(TYPE_STRING),
256 value_(in_value) {
257 DCHECK(IsStringUTF8(in_value));
260 StringValue::StringValue(const string16& in_value)
261 : Value(TYPE_STRING),
262 value_(UTF16ToUTF8(in_value)) {
265 StringValue::~StringValue() {
268 std::string* StringValue::GetString() {
269 return &value_;
272 const std::string& StringValue::GetString() const {
273 return value_;
276 bool StringValue::GetAsString(std::string* out_value) const {
277 if (out_value)
278 *out_value = value_;
279 return true;
282 bool StringValue::GetAsString(string16* out_value) const {
283 if (out_value)
284 *out_value = UTF8ToUTF16(value_);
285 return true;
288 bool StringValue::GetAsString(const StringValue** out_value) const {
289 if (out_value)
290 *out_value = this;
291 return true;
294 StringValue* StringValue::DeepCopy() const {
295 return new StringValue(value_);
298 bool StringValue::Equals(const Value* other) const {
299 if (other->GetType() != GetType())
300 return false;
301 std::string lhs, rhs;
302 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
305 ///////////////////// BinaryValue ////////////////////
307 BinaryValue::BinaryValue()
308 : Value(TYPE_BINARY),
309 size_(0) {
312 BinaryValue::BinaryValue(scoped_ptr<char[]> buffer, size_t size)
313 : Value(TYPE_BINARY),
314 buffer_(buffer.Pass()),
315 size_(size) {
318 BinaryValue::~BinaryValue() {
321 // static
322 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer,
323 size_t size) {
324 char* buffer_copy = new char[size];
325 memcpy(buffer_copy, buffer, size);
326 scoped_ptr<char[]> scoped_buffer_copy(buffer_copy);
327 return new BinaryValue(scoped_buffer_copy.Pass(), size);
330 bool BinaryValue::GetAsBinary(const BinaryValue** out_value) const {
331 if (out_value)
332 *out_value = this;
333 return true;
336 BinaryValue* BinaryValue::DeepCopy() const {
337 return CreateWithCopiedBuffer(buffer_.get(), size_);
340 bool BinaryValue::Equals(const Value* other) const {
341 if (other->GetType() != GetType())
342 return false;
343 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other);
344 if (other_binary->size_ != size_)
345 return false;
346 return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_);
349 ///////////////////// DictionaryValue ////////////////////
351 DictionaryValue::DictionaryValue()
352 : Value(TYPE_DICTIONARY) {
355 DictionaryValue::~DictionaryValue() {
356 Clear();
359 bool DictionaryValue::GetAsDictionary(DictionaryValue** out_value) {
360 if (out_value)
361 *out_value = this;
362 return true;
365 bool DictionaryValue::GetAsDictionary(const DictionaryValue** out_value) const {
366 if (out_value)
367 *out_value = this;
368 return true;
371 bool DictionaryValue::HasKey(const std::string& key) const {
372 DCHECK(IsStringUTF8(key));
373 ValueMap::const_iterator current_entry = dictionary_.find(key);
374 DCHECK((current_entry == dictionary_.end()) || current_entry->second);
375 return current_entry != dictionary_.end();
378 void DictionaryValue::Clear() {
379 ValueMap::iterator dict_iterator = dictionary_.begin();
380 while (dict_iterator != dictionary_.end()) {
381 delete dict_iterator->second;
382 ++dict_iterator;
385 dictionary_.clear();
388 void DictionaryValue::Set(const std::string& path, scoped_ptr<Value> in_value) {
389 DCHECK(IsStringUTF8(path));
390 DCHECK(in_value);
392 std::string current_path(path);
393 DictionaryValue* current_dictionary = this;
394 for (size_t delimiter_position = current_path.find('.');
395 delimiter_position != std::string::npos;
396 delimiter_position = current_path.find('.')) {
397 // Assume that we're indexing into a dictionary.
398 std::string key(current_path, 0, delimiter_position);
399 DictionaryValue* child_dictionary = NULL;
400 if (!current_dictionary->GetDictionary(key, &child_dictionary)) {
401 child_dictionary = new DictionaryValue;
402 current_dictionary->SetWithoutPathExpansion(key, child_dictionary);
405 current_dictionary = child_dictionary;
406 current_path.erase(0, delimiter_position + 1);
409 current_dictionary->SetWithoutPathExpansion(current_path, in_value.Pass());
412 void DictionaryValue::Set(const std::string& path, Value* in_value) {
413 Set(path, make_scoped_ptr(in_value));
416 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) {
417 Set(path, new FundamentalValue(in_value));
420 void DictionaryValue::SetInteger(const std::string& path, int in_value) {
421 Set(path, new FundamentalValue(in_value));
424 void DictionaryValue::SetDouble(const std::string& path, double in_value) {
425 Set(path, new FundamentalValue(in_value));
428 void DictionaryValue::SetString(const std::string& path,
429 const std::string& in_value) {
430 Set(path, new StringValue(in_value));
433 void DictionaryValue::SetString(const std::string& path,
434 const string16& in_value) {
435 Set(path, new StringValue(in_value));
438 void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
439 scoped_ptr<Value> in_value) {
440 Value* bare_ptr = in_value.release();
441 // If there's an existing value here, we need to delete it, because
442 // we own all our children.
443 std::pair<ValueMap::iterator, bool> ins_res =
444 dictionary_.insert(std::make_pair(key, bare_ptr));
445 if (!ins_res.second) {
446 DCHECK_NE(ins_res.first->second, bare_ptr); // This would be bogus
447 delete ins_res.first->second;
448 ins_res.first->second = bare_ptr;
452 void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
453 Value* in_value) {
454 SetWithoutPathExpansion(key, make_scoped_ptr(in_value));
457 void DictionaryValue::SetBooleanWithoutPathExpansion(
458 const std::string& path, bool in_value) {
459 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
462 void DictionaryValue::SetIntegerWithoutPathExpansion(
463 const std::string& path, int in_value) {
464 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
467 void DictionaryValue::SetDoubleWithoutPathExpansion(
468 const std::string& path, double in_value) {
469 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
472 void DictionaryValue::SetStringWithoutPathExpansion(
473 const std::string& path, const std::string& in_value) {
474 SetWithoutPathExpansion(path, new StringValue(in_value));
477 void DictionaryValue::SetStringWithoutPathExpansion(
478 const std::string& path, const string16& in_value) {
479 SetWithoutPathExpansion(path, new StringValue(in_value));
482 bool DictionaryValue::Get(const std::string& path,
483 const Value** out_value) const {
484 DCHECK(IsStringUTF8(path));
485 std::string current_path(path);
486 const DictionaryValue* current_dictionary = this;
487 for (size_t delimiter_position = current_path.find('.');
488 delimiter_position != std::string::npos;
489 delimiter_position = current_path.find('.')) {
490 const DictionaryValue* child_dictionary = NULL;
491 if (!current_dictionary->GetDictionary(
492 current_path.substr(0, delimiter_position), &child_dictionary))
493 return false;
495 current_dictionary = child_dictionary;
496 current_path.erase(0, delimiter_position + 1);
499 return current_dictionary->GetWithoutPathExpansion(current_path, out_value);
502 bool DictionaryValue::Get(const std::string& path, Value** out_value) {
503 return static_cast<const DictionaryValue&>(*this).Get(
504 path,
505 const_cast<const Value**>(out_value));
508 bool DictionaryValue::GetBoolean(const std::string& path,
509 bool* bool_value) const {
510 const Value* value;
511 if (!Get(path, &value))
512 return false;
514 return value->GetAsBoolean(bool_value);
517 bool DictionaryValue::GetInteger(const std::string& path,
518 int* out_value) const {
519 const Value* value;
520 if (!Get(path, &value))
521 return false;
523 return value->GetAsInteger(out_value);
526 bool DictionaryValue::GetDouble(const std::string& path,
527 double* out_value) const {
528 const Value* value;
529 if (!Get(path, &value))
530 return false;
532 return value->GetAsDouble(out_value);
535 bool DictionaryValue::GetString(const std::string& path,
536 std::string* out_value) const {
537 const Value* value;
538 if (!Get(path, &value))
539 return false;
541 return value->GetAsString(out_value);
544 bool DictionaryValue::GetString(const std::string& path,
545 string16* out_value) const {
546 const Value* value;
547 if (!Get(path, &value))
548 return false;
550 return value->GetAsString(out_value);
553 bool DictionaryValue::GetStringASCII(const std::string& path,
554 std::string* out_value) const {
555 std::string out;
556 if (!GetString(path, &out))
557 return false;
559 if (!IsStringASCII(out)) {
560 NOTREACHED();
561 return false;
564 out_value->assign(out);
565 return true;
568 bool DictionaryValue::GetBinary(const std::string& path,
569 const BinaryValue** out_value) const {
570 const Value* value;
571 bool result = Get(path, &value);
572 if (!result || !value->IsType(TYPE_BINARY))
573 return false;
575 if (out_value)
576 *out_value = static_cast<const BinaryValue*>(value);
578 return true;
581 bool DictionaryValue::GetBinary(const std::string& path,
582 BinaryValue** out_value) {
583 return static_cast<const DictionaryValue&>(*this).GetBinary(
584 path,
585 const_cast<const BinaryValue**>(out_value));
588 bool DictionaryValue::GetDictionary(const std::string& path,
589 const DictionaryValue** out_value) const {
590 const Value* value;
591 bool result = Get(path, &value);
592 if (!result || !value->IsType(TYPE_DICTIONARY))
593 return false;
595 if (out_value)
596 *out_value = static_cast<const DictionaryValue*>(value);
598 return true;
601 bool DictionaryValue::GetDictionary(const std::string& path,
602 DictionaryValue** out_value) {
603 return static_cast<const DictionaryValue&>(*this).GetDictionary(
604 path,
605 const_cast<const DictionaryValue**>(out_value));
608 bool DictionaryValue::GetList(const std::string& path,
609 const ListValue** out_value) const {
610 const Value* value;
611 bool result = Get(path, &value);
612 if (!result || !value->IsType(TYPE_LIST))
613 return false;
615 if (out_value)
616 *out_value = static_cast<const ListValue*>(value);
618 return true;
621 bool DictionaryValue::GetList(const std::string& path, ListValue** out_value) {
622 return static_cast<const DictionaryValue&>(*this).GetList(
623 path,
624 const_cast<const ListValue**>(out_value));
627 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
628 const Value** out_value) const {
629 DCHECK(IsStringUTF8(key));
630 ValueMap::const_iterator entry_iterator = dictionary_.find(key);
631 if (entry_iterator == dictionary_.end())
632 return false;
634 const Value* entry = entry_iterator->second;
635 if (out_value)
636 *out_value = entry;
637 return true;
640 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
641 Value** out_value) {
642 return static_cast<const DictionaryValue&>(*this).GetWithoutPathExpansion(
643 key,
644 const_cast<const Value**>(out_value));
647 bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string& key,
648 bool* out_value) const {
649 const Value* value;
650 if (!GetWithoutPathExpansion(key, &value))
651 return false;
653 return value->GetAsBoolean(out_value);
656 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string& key,
657 int* out_value) const {
658 const Value* value;
659 if (!GetWithoutPathExpansion(key, &value))
660 return false;
662 return value->GetAsInteger(out_value);
665 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string& key,
666 double* out_value) const {
667 const Value* value;
668 if (!GetWithoutPathExpansion(key, &value))
669 return false;
671 return value->GetAsDouble(out_value);
674 bool DictionaryValue::GetStringWithoutPathExpansion(
675 const std::string& key,
676 std::string* out_value) const {
677 const Value* value;
678 if (!GetWithoutPathExpansion(key, &value))
679 return false;
681 return value->GetAsString(out_value);
684 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string& key,
685 string16* out_value) const {
686 const Value* value;
687 if (!GetWithoutPathExpansion(key, &value))
688 return false;
690 return value->GetAsString(out_value);
693 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
694 const std::string& key,
695 const DictionaryValue** out_value) const {
696 const Value* value;
697 bool result = GetWithoutPathExpansion(key, &value);
698 if (!result || !value->IsType(TYPE_DICTIONARY))
699 return false;
701 if (out_value)
702 *out_value = static_cast<const DictionaryValue*>(value);
704 return true;
707 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
708 const std::string& key,
709 DictionaryValue** out_value) {
710 const DictionaryValue& const_this =
711 static_cast<const DictionaryValue&>(*this);
712 return const_this.GetDictionaryWithoutPathExpansion(
713 key,
714 const_cast<const DictionaryValue**>(out_value));
717 bool DictionaryValue::GetListWithoutPathExpansion(
718 const std::string& key,
719 const ListValue** out_value) const {
720 const Value* value;
721 bool result = GetWithoutPathExpansion(key, &value);
722 if (!result || !value->IsType(TYPE_LIST))
723 return false;
725 if (out_value)
726 *out_value = static_cast<const ListValue*>(value);
728 return true;
731 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key,
732 ListValue** out_value) {
733 return
734 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion(
735 key,
736 const_cast<const ListValue**>(out_value));
739 bool DictionaryValue::Remove(const std::string& path,
740 scoped_ptr<Value>* out_value) {
741 DCHECK(IsStringUTF8(path));
742 std::string current_path(path);
743 DictionaryValue* current_dictionary = this;
744 size_t delimiter_position = current_path.rfind('.');
745 if (delimiter_position != std::string::npos) {
746 if (!GetDictionary(current_path.substr(0, delimiter_position),
747 &current_dictionary))
748 return false;
749 current_path.erase(0, delimiter_position + 1);
752 return current_dictionary->RemoveWithoutPathExpansion(current_path,
753 out_value);
756 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string& key,
757 scoped_ptr<Value>* out_value) {
758 DCHECK(IsStringUTF8(key));
759 ValueMap::iterator entry_iterator = dictionary_.find(key);
760 if (entry_iterator == dictionary_.end())
761 return false;
763 Value* entry = entry_iterator->second;
764 if (out_value)
765 out_value->reset(entry);
766 else
767 delete entry;
768 dictionary_.erase(entry_iterator);
769 return true;
772 bool DictionaryValue::RemovePath(const std::string& path,
773 scoped_ptr<Value>* out_value) {
774 bool result = false;
775 size_t delimiter_position = path.find('.');
777 if (delimiter_position == std::string::npos)
778 return RemoveWithoutPathExpansion(path, out_value);
780 const std::string subdict_path = path.substr(0, delimiter_position);
781 DictionaryValue* subdict = NULL;
782 if (!GetDictionary(subdict_path, &subdict))
783 return false;
784 result = subdict->RemovePath(path.substr(delimiter_position + 1),
785 out_value);
786 if (result && subdict->empty())
787 RemoveWithoutPathExpansion(subdict_path, NULL);
789 return result;
792 DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() const {
793 Value* copy = CopyWithoutEmptyChildren(this);
794 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue;
797 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) {
798 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) {
799 const Value* merge_value = &it.value();
800 // Check whether we have to merge dictionaries.
801 if (merge_value->IsType(Value::TYPE_DICTIONARY)) {
802 DictionaryValue* sub_dict;
803 if (GetDictionaryWithoutPathExpansion(it.key(), &sub_dict)) {
804 sub_dict->MergeDictionary(
805 static_cast<const DictionaryValue*>(merge_value));
806 continue;
809 // All other cases: Make a copy and hook it up.
810 SetWithoutPathExpansion(it.key(), merge_value->DeepCopy());
814 void DictionaryValue::Swap(DictionaryValue* other) {
815 dictionary_.swap(other->dictionary_);
818 DictionaryValue::Iterator::Iterator(const DictionaryValue& target)
819 : target_(target),
820 it_(target.dictionary_.begin()) {}
822 DictionaryValue::Iterator::~Iterator() {}
824 DictionaryValue* DictionaryValue::DeepCopy() const {
825 DictionaryValue* result = new DictionaryValue;
827 for (ValueMap::const_iterator current_entry(dictionary_.begin());
828 current_entry != dictionary_.end(); ++current_entry) {
829 result->SetWithoutPathExpansion(current_entry->first,
830 current_entry->second->DeepCopy());
833 return result;
836 scoped_ptr<DictionaryValue> DictionaryValue::CreateDeepCopy() const {
837 return make_scoped_ptr(DeepCopy());
840 bool DictionaryValue::Equals(const Value* other) const {
841 if (other->GetType() != GetType())
842 return false;
844 const DictionaryValue* other_dict =
845 static_cast<const DictionaryValue*>(other);
846 Iterator lhs_it(*this);
847 Iterator rhs_it(*other_dict);
848 while (!lhs_it.IsAtEnd() && !rhs_it.IsAtEnd()) {
849 if (lhs_it.key() != rhs_it.key() ||
850 !lhs_it.value().Equals(&rhs_it.value())) {
851 return false;
853 lhs_it.Advance();
854 rhs_it.Advance();
856 if (!lhs_it.IsAtEnd() || !rhs_it.IsAtEnd())
857 return false;
859 return true;
862 ///////////////////// ListValue ////////////////////
864 ListValue::ListValue() : Value(TYPE_LIST) {
867 ListValue::~ListValue() {
868 Clear();
871 void ListValue::Clear() {
872 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i)
873 delete *i;
874 list_.clear();
877 bool ListValue::Set(size_t index, Value* in_value) {
878 if (!in_value)
879 return false;
881 if (index >= list_.size()) {
882 // Pad out any intermediate indexes with null settings
883 while (index > list_.size())
884 Append(CreateNullValue());
885 Append(in_value);
886 } else {
887 DCHECK(list_[index] != in_value);
888 delete list_[index];
889 list_[index] = in_value;
891 return true;
894 bool ListValue::Set(size_t index, scoped_ptr<Value> in_value) {
895 return Set(index, in_value.release());
898 bool ListValue::Get(size_t index, const Value** out_value) const {
899 if (index >= list_.size())
900 return false;
902 if (out_value)
903 *out_value = list_[index];
905 return true;
908 bool ListValue::Get(size_t index, Value** out_value) {
909 return static_cast<const ListValue&>(*this).Get(
910 index,
911 const_cast<const Value**>(out_value));
914 bool ListValue::GetBoolean(size_t index, bool* bool_value) const {
915 const Value* value;
916 if (!Get(index, &value))
917 return false;
919 return value->GetAsBoolean(bool_value);
922 bool ListValue::GetInteger(size_t index, int* out_value) const {
923 const Value* value;
924 if (!Get(index, &value))
925 return false;
927 return value->GetAsInteger(out_value);
930 bool ListValue::GetDouble(size_t index, double* out_value) const {
931 const Value* value;
932 if (!Get(index, &value))
933 return false;
935 return value->GetAsDouble(out_value);
938 bool ListValue::GetString(size_t index, std::string* out_value) const {
939 const Value* value;
940 if (!Get(index, &value))
941 return false;
943 return value->GetAsString(out_value);
946 bool ListValue::GetString(size_t index, string16* out_value) const {
947 const Value* value;
948 if (!Get(index, &value))
949 return false;
951 return value->GetAsString(out_value);
954 bool ListValue::GetBinary(size_t index, const BinaryValue** out_value) const {
955 const Value* value;
956 bool result = Get(index, &value);
957 if (!result || !value->IsType(TYPE_BINARY))
958 return false;
960 if (out_value)
961 *out_value = static_cast<const BinaryValue*>(value);
963 return true;
966 bool ListValue::GetBinary(size_t index, BinaryValue** out_value) {
967 return static_cast<const ListValue&>(*this).GetBinary(
968 index,
969 const_cast<const BinaryValue**>(out_value));
972 bool ListValue::GetDictionary(size_t index,
973 const DictionaryValue** out_value) const {
974 const Value* value;
975 bool result = Get(index, &value);
976 if (!result || !value->IsType(TYPE_DICTIONARY))
977 return false;
979 if (out_value)
980 *out_value = static_cast<const DictionaryValue*>(value);
982 return true;
985 bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) {
986 return static_cast<const ListValue&>(*this).GetDictionary(
987 index,
988 const_cast<const DictionaryValue**>(out_value));
991 bool ListValue::GetList(size_t index, const ListValue** out_value) const {
992 const Value* value;
993 bool result = Get(index, &value);
994 if (!result || !value->IsType(TYPE_LIST))
995 return false;
997 if (out_value)
998 *out_value = static_cast<const ListValue*>(value);
1000 return true;
1003 bool ListValue::GetList(size_t index, ListValue** out_value) {
1004 return static_cast<const ListValue&>(*this).GetList(
1005 index,
1006 const_cast<const ListValue**>(out_value));
1009 bool ListValue::Remove(size_t index, scoped_ptr<Value>* out_value) {
1010 if (index >= list_.size())
1011 return false;
1013 if (out_value)
1014 out_value->reset(list_[index]);
1015 else
1016 delete list_[index];
1018 list_.erase(list_.begin() + index);
1019 return true;
1022 bool ListValue::Remove(const Value& value, size_t* index) {
1023 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i) {
1024 if ((*i)->Equals(&value)) {
1025 size_t previous_index = i - list_.begin();
1026 delete *i;
1027 list_.erase(i);
1029 if (index)
1030 *index = previous_index;
1031 return true;
1034 return false;
1037 ListValue::iterator ListValue::Erase(iterator iter,
1038 scoped_ptr<Value>* out_value) {
1039 if (out_value)
1040 out_value->reset(*iter);
1041 else
1042 delete *iter;
1044 return list_.erase(iter);
1047 void ListValue::Append(scoped_ptr<Value> in_value) {
1048 Append(in_value.release());
1051 void ListValue::Append(Value* in_value) {
1052 DCHECK(in_value);
1053 list_.push_back(in_value);
1056 void ListValue::AppendBoolean(bool in_value) {
1057 Append(new FundamentalValue(in_value));
1060 void ListValue::AppendInteger(int in_value) {
1061 Append(new FundamentalValue(in_value));
1064 void ListValue::AppendDouble(double in_value) {
1065 Append(new FundamentalValue(in_value));
1068 void ListValue::AppendString(const std::string& in_value) {
1069 Append(new StringValue(in_value));
1072 void ListValue::AppendString(const string16& in_value) {
1073 Append(new StringValue(in_value));
1076 void ListValue::AppendStrings(const std::vector<std::string>& in_values) {
1077 for (std::vector<std::string>::const_iterator it = in_values.begin();
1078 it != in_values.end(); ++it) {
1079 AppendString(*it);
1083 void ListValue::AppendStrings(const std::vector<string16>& in_values) {
1084 for (std::vector<string16>::const_iterator it = in_values.begin();
1085 it != in_values.end(); ++it) {
1086 AppendString(*it);
1090 bool ListValue::AppendIfNotPresent(Value* in_value) {
1091 DCHECK(in_value);
1092 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) {
1093 if ((*i)->Equals(in_value)) {
1094 delete in_value;
1095 return false;
1098 list_.push_back(in_value);
1099 return true;
1102 bool ListValue::Insert(size_t index, Value* in_value) {
1103 DCHECK(in_value);
1104 if (index > list_.size())
1105 return false;
1107 list_.insert(list_.begin() + index, in_value);
1108 return true;
1111 ListValue::const_iterator ListValue::Find(const Value& value) const {
1112 return std::find_if(list_.begin(), list_.end(), ValueEquals(&value));
1115 void ListValue::Swap(ListValue* other) {
1116 list_.swap(other->list_);
1119 bool ListValue::GetAsList(ListValue** out_value) {
1120 if (out_value)
1121 *out_value = this;
1122 return true;
1125 bool ListValue::GetAsList(const ListValue** out_value) const {
1126 if (out_value)
1127 *out_value = this;
1128 return true;
1131 ListValue* ListValue::DeepCopy() const {
1132 ListValue* result = new ListValue;
1134 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i)
1135 result->Append((*i)->DeepCopy());
1137 return result;
1140 scoped_ptr<ListValue> ListValue::CreateDeepCopy() const {
1141 return make_scoped_ptr(DeepCopy());
1144 bool ListValue::Equals(const Value* other) const {
1145 if (other->GetType() != GetType())
1146 return false;
1148 const ListValue* other_list =
1149 static_cast<const ListValue*>(other);
1150 const_iterator lhs_it, rhs_it;
1151 for (lhs_it = begin(), rhs_it = other_list->begin();
1152 lhs_it != end() && rhs_it != other_list->end();
1153 ++lhs_it, ++rhs_it) {
1154 if (!(*lhs_it)->Equals(*rhs_it))
1155 return false;
1157 if (lhs_it != end() || rhs_it != other_list->end())
1158 return false;
1160 return true;
1163 ValueSerializer::~ValueSerializer() {
1166 ValueDeserializer::~ValueDeserializer() {
1169 std::ostream& operator<<(std::ostream& out, const Value& value) {
1170 std::string json;
1171 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json);
1172 return out << json;
1175 } // namespace base