Implement Tier 2 of Cookie handling for Mirror on WKBling.
[chromium-blink-merge.git] / base / values.cc
blobc829a7f2f884f30d6aa76477dff94cbf76ecd708
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 scoped_ptr<Value> CopyWithoutEmptyChildren(const Value& node);
25 // Make a deep copy of |node|, but don't include empty lists or dictionaries
26 // in the copy. It's possible for this function to return NULL and it
27 // expects |node| to always be non-NULL.
28 scoped_ptr<ListValue> CopyListWithoutEmptyChildren(const ListValue& list) {
29 scoped_ptr<ListValue> copy;
30 for (ListValue::const_iterator it = list.begin(); it != list.end(); ++it) {
31 scoped_ptr<Value> child_copy = CopyWithoutEmptyChildren(**it);
32 if (child_copy) {
33 if (!copy)
34 copy.reset(new ListValue);
35 copy->Append(child_copy.Pass());
38 return copy;
41 scoped_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren(
42 const DictionaryValue& dict) {
43 scoped_ptr<DictionaryValue> copy;
44 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
45 scoped_ptr<Value> child_copy = CopyWithoutEmptyChildren(it.value());
46 if (child_copy) {
47 if (!copy)
48 copy.reset(new DictionaryValue);
49 copy->SetWithoutPathExpansion(it.key(), child_copy.Pass());
52 return copy;
55 scoped_ptr<Value> CopyWithoutEmptyChildren(const Value& node) {
56 switch (node.GetType()) {
57 case Value::TYPE_LIST:
58 return CopyListWithoutEmptyChildren(static_cast<const ListValue&>(node));
60 case Value::TYPE_DICTIONARY:
61 return CopyDictionaryWithoutEmptyChildren(
62 static_cast<const DictionaryValue&>(node));
64 default:
65 return node.CreateDeepCopy();
69 // A small functor for comparing Values for std::find_if and similar.
70 class ValueEquals {
71 public:
72 // Pass the value against which all consecutive calls of the () operator will
73 // compare their argument to. This Value object must not be destroyed while
74 // the ValueEquals is in use.
75 explicit ValueEquals(const Value* first) : first_(first) { }
77 bool operator ()(const Value* second) const {
78 return first_->Equals(second);
81 private:
82 const Value* first_;
85 } // namespace
87 Value::~Value() {
90 // static
91 scoped_ptr<Value> Value::CreateNullValue() {
92 return make_scoped_ptr(new Value(TYPE_NULL));
95 bool Value::GetAsBinary(const BinaryValue** out_value) const {
96 return false;
99 bool Value::GetAsBoolean(bool* out_value) const {
100 return false;
103 bool Value::GetAsInteger(int* out_value) const {
104 return false;
107 bool Value::GetAsDouble(double* out_value) const {
108 return false;
111 bool Value::GetAsString(std::string* out_value) const {
112 return false;
115 bool Value::GetAsString(string16* out_value) const {
116 return false;
119 bool Value::GetAsString(const StringValue** out_value) const {
120 return false;
123 bool Value::GetAsList(ListValue** out_value) {
124 return false;
127 bool Value::GetAsList(const ListValue** out_value) const {
128 return false;
131 bool Value::GetAsDictionary(DictionaryValue** out_value) {
132 return false;
135 bool Value::GetAsDictionary(const DictionaryValue** out_value) const {
136 return false;
139 Value* Value::DeepCopy() const {
140 // This method should only be getting called for null Values--all subclasses
141 // need to provide their own implementation;.
142 DCHECK(IsType(TYPE_NULL));
143 return CreateNullValue().release();
146 scoped_ptr<Value> Value::CreateDeepCopy() const {
147 return make_scoped_ptr(DeepCopy());
150 bool Value::Equals(const Value* other) const {
151 // This method should only be getting called for null Values--all subclasses
152 // need to provide their own implementation;.
153 DCHECK(IsType(TYPE_NULL));
154 return other->IsType(TYPE_NULL);
157 // static
158 bool Value::Equals(const Value* a, const Value* b) {
159 if ((a == NULL) && (b == NULL)) return true;
160 if ((a == NULL) ^ (b == NULL)) return false;
161 return a->Equals(b);
164 Value::Value(Type type) : type_(type) {}
166 Value::Value(const Value& that) : type_(that.type_) {}
168 Value& Value::operator=(const Value& that) {
169 type_ = that.type_;
170 return *this;
173 ///////////////////// FundamentalValue ////////////////////
175 FundamentalValue::FundamentalValue(bool in_value)
176 : Value(TYPE_BOOLEAN), boolean_value_(in_value) {
179 FundamentalValue::FundamentalValue(int in_value)
180 : Value(TYPE_INTEGER), integer_value_(in_value) {
183 FundamentalValue::FundamentalValue(double in_value)
184 : Value(TYPE_DOUBLE), double_value_(in_value) {
185 if (!std::isfinite(double_value_)) {
186 NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
187 << "values cannot be represented in JSON";
188 double_value_ = 0.0;
192 FundamentalValue::~FundamentalValue() {
195 bool FundamentalValue::GetAsBoolean(bool* out_value) const {
196 if (out_value && IsType(TYPE_BOOLEAN))
197 *out_value = boolean_value_;
198 return (IsType(TYPE_BOOLEAN));
201 bool FundamentalValue::GetAsInteger(int* out_value) const {
202 if (out_value && IsType(TYPE_INTEGER))
203 *out_value = integer_value_;
204 return (IsType(TYPE_INTEGER));
207 bool FundamentalValue::GetAsDouble(double* out_value) const {
208 if (out_value && IsType(TYPE_DOUBLE))
209 *out_value = double_value_;
210 else if (out_value && IsType(TYPE_INTEGER))
211 *out_value = integer_value_;
212 return (IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER));
215 FundamentalValue* FundamentalValue::DeepCopy() const {
216 switch (GetType()) {
217 case TYPE_BOOLEAN:
218 return new FundamentalValue(boolean_value_);
220 case TYPE_INTEGER:
221 return new FundamentalValue(integer_value_);
223 case TYPE_DOUBLE:
224 return new FundamentalValue(double_value_);
226 default:
227 NOTREACHED();
228 return NULL;
232 bool FundamentalValue::Equals(const Value* other) const {
233 if (other->GetType() != GetType())
234 return false;
236 switch (GetType()) {
237 case TYPE_BOOLEAN: {
238 bool lhs, rhs;
239 return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs;
241 case TYPE_INTEGER: {
242 int lhs, rhs;
243 return GetAsInteger(&lhs) && other->GetAsInteger(&rhs) && lhs == rhs;
245 case TYPE_DOUBLE: {
246 double lhs, rhs;
247 return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs;
249 default:
250 NOTREACHED();
251 return false;
255 ///////////////////// StringValue ////////////////////
257 StringValue::StringValue(const std::string& in_value)
258 : Value(TYPE_STRING),
259 value_(in_value) {
260 DCHECK(IsStringUTF8(in_value));
263 StringValue::StringValue(const string16& in_value)
264 : Value(TYPE_STRING),
265 value_(UTF16ToUTF8(in_value)) {
268 StringValue::~StringValue() {
271 std::string* StringValue::GetString() {
272 return &value_;
275 const std::string& StringValue::GetString() const {
276 return value_;
279 bool StringValue::GetAsString(std::string* out_value) const {
280 if (out_value)
281 *out_value = value_;
282 return true;
285 bool StringValue::GetAsString(string16* out_value) const {
286 if (out_value)
287 *out_value = UTF8ToUTF16(value_);
288 return true;
291 bool StringValue::GetAsString(const StringValue** out_value) const {
292 if (out_value)
293 *out_value = this;
294 return true;
297 StringValue* StringValue::DeepCopy() const {
298 return new StringValue(value_);
301 bool StringValue::Equals(const Value* other) const {
302 if (other->GetType() != GetType())
303 return false;
304 std::string lhs, rhs;
305 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
308 ///////////////////// BinaryValue ////////////////////
310 BinaryValue::BinaryValue()
311 : Value(TYPE_BINARY),
312 size_(0) {
315 BinaryValue::BinaryValue(scoped_ptr<char[]> buffer, size_t size)
316 : Value(TYPE_BINARY),
317 buffer_(buffer.Pass()),
318 size_(size) {
321 BinaryValue::~BinaryValue() {
324 // static
325 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer,
326 size_t size) {
327 char* buffer_copy = new char[size];
328 memcpy(buffer_copy, buffer, size);
329 scoped_ptr<char[]> scoped_buffer_copy(buffer_copy);
330 return new BinaryValue(scoped_buffer_copy.Pass(), size);
333 bool BinaryValue::GetAsBinary(const BinaryValue** out_value) const {
334 if (out_value)
335 *out_value = this;
336 return true;
339 BinaryValue* BinaryValue::DeepCopy() const {
340 return CreateWithCopiedBuffer(buffer_.get(), size_);
343 bool BinaryValue::Equals(const Value* other) const {
344 if (other->GetType() != GetType())
345 return false;
346 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other);
347 if (other_binary->size_ != size_)
348 return false;
349 return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_);
352 ///////////////////// DictionaryValue ////////////////////
354 DictionaryValue::DictionaryValue()
355 : Value(TYPE_DICTIONARY) {
358 DictionaryValue::~DictionaryValue() {
359 Clear();
362 bool DictionaryValue::GetAsDictionary(DictionaryValue** out_value) {
363 if (out_value)
364 *out_value = this;
365 return true;
368 bool DictionaryValue::GetAsDictionary(const DictionaryValue** out_value) const {
369 if (out_value)
370 *out_value = this;
371 return true;
374 bool DictionaryValue::HasKey(const std::string& key) const {
375 DCHECK(IsStringUTF8(key));
376 ValueMap::const_iterator current_entry = dictionary_.find(key);
377 DCHECK((current_entry == dictionary_.end()) || current_entry->second);
378 return current_entry != dictionary_.end();
381 void DictionaryValue::Clear() {
382 ValueMap::iterator dict_iterator = dictionary_.begin();
383 while (dict_iterator != dictionary_.end()) {
384 delete dict_iterator->second;
385 ++dict_iterator;
388 dictionary_.clear();
391 void DictionaryValue::Set(const std::string& path, scoped_ptr<Value> in_value) {
392 DCHECK(IsStringUTF8(path));
393 DCHECK(in_value);
395 std::string current_path(path);
396 DictionaryValue* current_dictionary = this;
397 for (size_t delimiter_position = current_path.find('.');
398 delimiter_position != std::string::npos;
399 delimiter_position = current_path.find('.')) {
400 // Assume that we're indexing into a dictionary.
401 std::string key(current_path, 0, delimiter_position);
402 DictionaryValue* child_dictionary = NULL;
403 if (!current_dictionary->GetDictionary(key, &child_dictionary)) {
404 child_dictionary = new DictionaryValue;
405 current_dictionary->SetWithoutPathExpansion(key, child_dictionary);
408 current_dictionary = child_dictionary;
409 current_path.erase(0, delimiter_position + 1);
412 current_dictionary->SetWithoutPathExpansion(current_path, in_value.Pass());
415 void DictionaryValue::Set(const std::string& path, Value* in_value) {
416 Set(path, make_scoped_ptr(in_value));
419 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) {
420 Set(path, new FundamentalValue(in_value));
423 void DictionaryValue::SetInteger(const std::string& path, int in_value) {
424 Set(path, new FundamentalValue(in_value));
427 void DictionaryValue::SetDouble(const std::string& path, double in_value) {
428 Set(path, new FundamentalValue(in_value));
431 void DictionaryValue::SetString(const std::string& path,
432 const std::string& in_value) {
433 Set(path, new StringValue(in_value));
436 void DictionaryValue::SetString(const std::string& path,
437 const string16& in_value) {
438 Set(path, new StringValue(in_value));
441 void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
442 scoped_ptr<Value> in_value) {
443 Value* bare_ptr = in_value.release();
444 // If there's an existing value here, we need to delete it, because
445 // we own all our children.
446 std::pair<ValueMap::iterator, bool> ins_res =
447 dictionary_.insert(std::make_pair(key, bare_ptr));
448 if (!ins_res.second) {
449 DCHECK_NE(ins_res.first->second, bare_ptr); // This would be bogus
450 delete ins_res.first->second;
451 ins_res.first->second = bare_ptr;
455 void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
456 Value* in_value) {
457 SetWithoutPathExpansion(key, make_scoped_ptr(in_value));
460 void DictionaryValue::SetBooleanWithoutPathExpansion(
461 const std::string& path, bool in_value) {
462 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
465 void DictionaryValue::SetIntegerWithoutPathExpansion(
466 const std::string& path, int in_value) {
467 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
470 void DictionaryValue::SetDoubleWithoutPathExpansion(
471 const std::string& path, double in_value) {
472 SetWithoutPathExpansion(path, new FundamentalValue(in_value));
475 void DictionaryValue::SetStringWithoutPathExpansion(
476 const std::string& path, const std::string& in_value) {
477 SetWithoutPathExpansion(path, new StringValue(in_value));
480 void DictionaryValue::SetStringWithoutPathExpansion(
481 const std::string& path, const string16& in_value) {
482 SetWithoutPathExpansion(path, new StringValue(in_value));
485 bool DictionaryValue::Get(StringPiece path,
486 const Value** out_value) const {
487 DCHECK(IsStringUTF8(path));
488 StringPiece current_path(path);
489 const DictionaryValue* current_dictionary = this;
490 for (size_t delimiter_position = current_path.find('.');
491 delimiter_position != std::string::npos;
492 delimiter_position = current_path.find('.')) {
493 const DictionaryValue* child_dictionary = NULL;
494 if (!current_dictionary->GetDictionaryWithoutPathExpansion(
495 current_path.substr(0, delimiter_position).as_string(),
496 &child_dictionary)) {
497 return false;
500 current_dictionary = child_dictionary;
501 current_path = current_path.substr(delimiter_position + 1);
504 return current_dictionary->GetWithoutPathExpansion(current_path.as_string(),
505 out_value);
508 bool DictionaryValue::Get(StringPiece path, Value** out_value) {
509 return static_cast<const DictionaryValue&>(*this).Get(
510 path,
511 const_cast<const Value**>(out_value));
514 bool DictionaryValue::GetBoolean(const std::string& path,
515 bool* bool_value) const {
516 const Value* value;
517 if (!Get(path, &value))
518 return false;
520 return value->GetAsBoolean(bool_value);
523 bool DictionaryValue::GetInteger(const std::string& path,
524 int* out_value) const {
525 const Value* value;
526 if (!Get(path, &value))
527 return false;
529 return value->GetAsInteger(out_value);
532 bool DictionaryValue::GetDouble(const std::string& path,
533 double* out_value) const {
534 const Value* value;
535 if (!Get(path, &value))
536 return false;
538 return value->GetAsDouble(out_value);
541 bool DictionaryValue::GetString(const std::string& path,
542 std::string* out_value) const {
543 const Value* value;
544 if (!Get(path, &value))
545 return false;
547 return value->GetAsString(out_value);
550 bool DictionaryValue::GetString(const std::string& path,
551 string16* out_value) const {
552 const Value* value;
553 if (!Get(path, &value))
554 return false;
556 return value->GetAsString(out_value);
559 bool DictionaryValue::GetStringASCII(const std::string& path,
560 std::string* out_value) const {
561 std::string out;
562 if (!GetString(path, &out))
563 return false;
565 if (!IsStringASCII(out)) {
566 NOTREACHED();
567 return false;
570 out_value->assign(out);
571 return true;
574 bool DictionaryValue::GetBinary(const std::string& path,
575 const BinaryValue** out_value) const {
576 const Value* value;
577 bool result = Get(path, &value);
578 if (!result || !value->IsType(TYPE_BINARY))
579 return false;
581 if (out_value)
582 *out_value = static_cast<const BinaryValue*>(value);
584 return true;
587 bool DictionaryValue::GetBinary(const std::string& path,
588 BinaryValue** out_value) {
589 return static_cast<const DictionaryValue&>(*this).GetBinary(
590 path,
591 const_cast<const BinaryValue**>(out_value));
594 bool DictionaryValue::GetDictionary(StringPiece path,
595 const DictionaryValue** out_value) const {
596 const Value* value;
597 bool result = Get(path, &value);
598 if (!result || !value->IsType(TYPE_DICTIONARY))
599 return false;
601 if (out_value)
602 *out_value = static_cast<const DictionaryValue*>(value);
604 return true;
607 bool DictionaryValue::GetDictionary(StringPiece path,
608 DictionaryValue** out_value) {
609 return static_cast<const DictionaryValue&>(*this).GetDictionary(
610 path,
611 const_cast<const DictionaryValue**>(out_value));
614 bool DictionaryValue::GetList(const std::string& path,
615 const ListValue** out_value) const {
616 const Value* value;
617 bool result = Get(path, &value);
618 if (!result || !value->IsType(TYPE_LIST))
619 return false;
621 if (out_value)
622 *out_value = static_cast<const ListValue*>(value);
624 return true;
627 bool DictionaryValue::GetList(const std::string& path, ListValue** out_value) {
628 return static_cast<const DictionaryValue&>(*this).GetList(
629 path,
630 const_cast<const ListValue**>(out_value));
633 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
634 const Value** out_value) const {
635 DCHECK(IsStringUTF8(key));
636 ValueMap::const_iterator entry_iterator = dictionary_.find(key);
637 if (entry_iterator == dictionary_.end())
638 return false;
640 const Value* entry = entry_iterator->second;
641 if (out_value)
642 *out_value = entry;
643 return true;
646 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
647 Value** out_value) {
648 return static_cast<const DictionaryValue&>(*this).GetWithoutPathExpansion(
649 key,
650 const_cast<const Value**>(out_value));
653 bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string& key,
654 bool* out_value) const {
655 const Value* value;
656 if (!GetWithoutPathExpansion(key, &value))
657 return false;
659 return value->GetAsBoolean(out_value);
662 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string& key,
663 int* out_value) const {
664 const Value* value;
665 if (!GetWithoutPathExpansion(key, &value))
666 return false;
668 return value->GetAsInteger(out_value);
671 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string& key,
672 double* out_value) const {
673 const Value* value;
674 if (!GetWithoutPathExpansion(key, &value))
675 return false;
677 return value->GetAsDouble(out_value);
680 bool DictionaryValue::GetStringWithoutPathExpansion(
681 const std::string& key,
682 std::string* out_value) const {
683 const Value* value;
684 if (!GetWithoutPathExpansion(key, &value))
685 return false;
687 return value->GetAsString(out_value);
690 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string& key,
691 string16* out_value) const {
692 const Value* value;
693 if (!GetWithoutPathExpansion(key, &value))
694 return false;
696 return value->GetAsString(out_value);
699 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
700 const std::string& key,
701 const DictionaryValue** out_value) const {
702 const Value* value;
703 bool result = GetWithoutPathExpansion(key, &value);
704 if (!result || !value->IsType(TYPE_DICTIONARY))
705 return false;
707 if (out_value)
708 *out_value = static_cast<const DictionaryValue*>(value);
710 return true;
713 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
714 const std::string& key,
715 DictionaryValue** out_value) {
716 const DictionaryValue& const_this =
717 static_cast<const DictionaryValue&>(*this);
718 return const_this.GetDictionaryWithoutPathExpansion(
719 key,
720 const_cast<const DictionaryValue**>(out_value));
723 bool DictionaryValue::GetListWithoutPathExpansion(
724 const std::string& key,
725 const ListValue** out_value) const {
726 const Value* value;
727 bool result = GetWithoutPathExpansion(key, &value);
728 if (!result || !value->IsType(TYPE_LIST))
729 return false;
731 if (out_value)
732 *out_value = static_cast<const ListValue*>(value);
734 return true;
737 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key,
738 ListValue** out_value) {
739 return
740 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion(
741 key,
742 const_cast<const ListValue**>(out_value));
745 bool DictionaryValue::Remove(const std::string& path,
746 scoped_ptr<Value>* out_value) {
747 DCHECK(IsStringUTF8(path));
748 std::string current_path(path);
749 DictionaryValue* current_dictionary = this;
750 size_t delimiter_position = current_path.rfind('.');
751 if (delimiter_position != std::string::npos) {
752 if (!GetDictionary(current_path.substr(0, delimiter_position),
753 &current_dictionary))
754 return false;
755 current_path.erase(0, delimiter_position + 1);
758 return current_dictionary->RemoveWithoutPathExpansion(current_path,
759 out_value);
762 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string& key,
763 scoped_ptr<Value>* out_value) {
764 DCHECK(IsStringUTF8(key));
765 ValueMap::iterator entry_iterator = dictionary_.find(key);
766 if (entry_iterator == dictionary_.end())
767 return false;
769 Value* entry = entry_iterator->second;
770 if (out_value)
771 out_value->reset(entry);
772 else
773 delete entry;
774 dictionary_.erase(entry_iterator);
775 return true;
778 bool DictionaryValue::RemovePath(const std::string& path,
779 scoped_ptr<Value>* out_value) {
780 bool result = false;
781 size_t delimiter_position = path.find('.');
783 if (delimiter_position == std::string::npos)
784 return RemoveWithoutPathExpansion(path, out_value);
786 const std::string subdict_path = path.substr(0, delimiter_position);
787 DictionaryValue* subdict = NULL;
788 if (!GetDictionary(subdict_path, &subdict))
789 return false;
790 result = subdict->RemovePath(path.substr(delimiter_position + 1),
791 out_value);
792 if (result && subdict->empty())
793 RemoveWithoutPathExpansion(subdict_path, NULL);
795 return result;
798 scoped_ptr<DictionaryValue> DictionaryValue::DeepCopyWithoutEmptyChildren()
799 const {
800 scoped_ptr<DictionaryValue> copy = CopyDictionaryWithoutEmptyChildren(*this);
801 if (!copy)
802 copy.reset(new DictionaryValue);
803 return copy;
806 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) {
807 for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) {
808 const Value* merge_value = &it.value();
809 // Check whether we have to merge dictionaries.
810 if (merge_value->IsType(Value::TYPE_DICTIONARY)) {
811 DictionaryValue* sub_dict;
812 if (GetDictionaryWithoutPathExpansion(it.key(), &sub_dict)) {
813 sub_dict->MergeDictionary(
814 static_cast<const DictionaryValue*>(merge_value));
815 continue;
818 // All other cases: Make a copy and hook it up.
819 SetWithoutPathExpansion(it.key(), merge_value->DeepCopy());
823 void DictionaryValue::Swap(DictionaryValue* other) {
824 dictionary_.swap(other->dictionary_);
827 DictionaryValue::Iterator::Iterator(const DictionaryValue& target)
828 : target_(target),
829 it_(target.dictionary_.begin()) {}
831 DictionaryValue::Iterator::~Iterator() {}
833 DictionaryValue* DictionaryValue::DeepCopy() const {
834 DictionaryValue* result = new DictionaryValue;
836 for (ValueMap::const_iterator current_entry(dictionary_.begin());
837 current_entry != dictionary_.end(); ++current_entry) {
838 result->SetWithoutPathExpansion(current_entry->first,
839 current_entry->second->DeepCopy());
842 return result;
845 scoped_ptr<DictionaryValue> DictionaryValue::CreateDeepCopy() const {
846 return make_scoped_ptr(DeepCopy());
849 bool DictionaryValue::Equals(const Value* other) const {
850 if (other->GetType() != GetType())
851 return false;
853 const DictionaryValue* other_dict =
854 static_cast<const DictionaryValue*>(other);
855 Iterator lhs_it(*this);
856 Iterator rhs_it(*other_dict);
857 while (!lhs_it.IsAtEnd() && !rhs_it.IsAtEnd()) {
858 if (lhs_it.key() != rhs_it.key() ||
859 !lhs_it.value().Equals(&rhs_it.value())) {
860 return false;
862 lhs_it.Advance();
863 rhs_it.Advance();
865 if (!lhs_it.IsAtEnd() || !rhs_it.IsAtEnd())
866 return false;
868 return true;
871 ///////////////////// ListValue ////////////////////
873 ListValue::ListValue() : Value(TYPE_LIST) {
876 ListValue::~ListValue() {
877 Clear();
880 void ListValue::Clear() {
881 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i)
882 delete *i;
883 list_.clear();
886 bool ListValue::Set(size_t index, Value* in_value) {
887 if (!in_value)
888 return false;
890 if (index >= list_.size()) {
891 // Pad out any intermediate indexes with null settings
892 while (index > list_.size())
893 Append(CreateNullValue());
894 Append(in_value);
895 } else {
896 DCHECK(list_[index] != in_value);
897 delete list_[index];
898 list_[index] = in_value;
900 return true;
903 bool ListValue::Set(size_t index, scoped_ptr<Value> in_value) {
904 return Set(index, in_value.release());
907 bool ListValue::Get(size_t index, const Value** out_value) const {
908 if (index >= list_.size())
909 return false;
911 if (out_value)
912 *out_value = list_[index];
914 return true;
917 bool ListValue::Get(size_t index, Value** out_value) {
918 return static_cast<const ListValue&>(*this).Get(
919 index,
920 const_cast<const Value**>(out_value));
923 bool ListValue::GetBoolean(size_t index, bool* bool_value) const {
924 const Value* value;
925 if (!Get(index, &value))
926 return false;
928 return value->GetAsBoolean(bool_value);
931 bool ListValue::GetInteger(size_t index, int* out_value) const {
932 const Value* value;
933 if (!Get(index, &value))
934 return false;
936 return value->GetAsInteger(out_value);
939 bool ListValue::GetDouble(size_t index, double* out_value) const {
940 const Value* value;
941 if (!Get(index, &value))
942 return false;
944 return value->GetAsDouble(out_value);
947 bool ListValue::GetString(size_t index, std::string* out_value) const {
948 const Value* value;
949 if (!Get(index, &value))
950 return false;
952 return value->GetAsString(out_value);
955 bool ListValue::GetString(size_t index, string16* out_value) const {
956 const Value* value;
957 if (!Get(index, &value))
958 return false;
960 return value->GetAsString(out_value);
963 bool ListValue::GetBinary(size_t index, const BinaryValue** out_value) const {
964 const Value* value;
965 bool result = Get(index, &value);
966 if (!result || !value->IsType(TYPE_BINARY))
967 return false;
969 if (out_value)
970 *out_value = static_cast<const BinaryValue*>(value);
972 return true;
975 bool ListValue::GetBinary(size_t index, BinaryValue** out_value) {
976 return static_cast<const ListValue&>(*this).GetBinary(
977 index,
978 const_cast<const BinaryValue**>(out_value));
981 bool ListValue::GetDictionary(size_t index,
982 const DictionaryValue** out_value) const {
983 const Value* value;
984 bool result = Get(index, &value);
985 if (!result || !value->IsType(TYPE_DICTIONARY))
986 return false;
988 if (out_value)
989 *out_value = static_cast<const DictionaryValue*>(value);
991 return true;
994 bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) {
995 return static_cast<const ListValue&>(*this).GetDictionary(
996 index,
997 const_cast<const DictionaryValue**>(out_value));
1000 bool ListValue::GetList(size_t index, const ListValue** out_value) const {
1001 const Value* value;
1002 bool result = Get(index, &value);
1003 if (!result || !value->IsType(TYPE_LIST))
1004 return false;
1006 if (out_value)
1007 *out_value = static_cast<const ListValue*>(value);
1009 return true;
1012 bool ListValue::GetList(size_t index, ListValue** out_value) {
1013 return static_cast<const ListValue&>(*this).GetList(
1014 index,
1015 const_cast<const ListValue**>(out_value));
1018 bool ListValue::Remove(size_t index, scoped_ptr<Value>* out_value) {
1019 if (index >= list_.size())
1020 return false;
1022 if (out_value)
1023 out_value->reset(list_[index]);
1024 else
1025 delete list_[index];
1027 list_.erase(list_.begin() + index);
1028 return true;
1031 bool ListValue::Remove(const Value& value, size_t* index) {
1032 for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i) {
1033 if ((*i)->Equals(&value)) {
1034 size_t previous_index = i - list_.begin();
1035 delete *i;
1036 list_.erase(i);
1038 if (index)
1039 *index = previous_index;
1040 return true;
1043 return false;
1046 ListValue::iterator ListValue::Erase(iterator iter,
1047 scoped_ptr<Value>* out_value) {
1048 if (out_value)
1049 out_value->reset(*iter);
1050 else
1051 delete *iter;
1053 return list_.erase(iter);
1056 void ListValue::Append(scoped_ptr<Value> in_value) {
1057 Append(in_value.release());
1060 void ListValue::Append(Value* in_value) {
1061 DCHECK(in_value);
1062 list_.push_back(in_value);
1065 void ListValue::AppendBoolean(bool in_value) {
1066 Append(new FundamentalValue(in_value));
1069 void ListValue::AppendInteger(int in_value) {
1070 Append(new FundamentalValue(in_value));
1073 void ListValue::AppendDouble(double in_value) {
1074 Append(new FundamentalValue(in_value));
1077 void ListValue::AppendString(const std::string& in_value) {
1078 Append(new StringValue(in_value));
1081 void ListValue::AppendString(const string16& in_value) {
1082 Append(new StringValue(in_value));
1085 void ListValue::AppendStrings(const std::vector<std::string>& in_values) {
1086 for (std::vector<std::string>::const_iterator it = in_values.begin();
1087 it != in_values.end(); ++it) {
1088 AppendString(*it);
1092 void ListValue::AppendStrings(const std::vector<string16>& in_values) {
1093 for (std::vector<string16>::const_iterator it = in_values.begin();
1094 it != in_values.end(); ++it) {
1095 AppendString(*it);
1099 bool ListValue::AppendIfNotPresent(Value* in_value) {
1100 DCHECK(in_value);
1101 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) {
1102 if ((*i)->Equals(in_value)) {
1103 delete in_value;
1104 return false;
1107 list_.push_back(in_value);
1108 return true;
1111 bool ListValue::Insert(size_t index, Value* in_value) {
1112 DCHECK(in_value);
1113 if (index > list_.size())
1114 return false;
1116 list_.insert(list_.begin() + index, in_value);
1117 return true;
1120 ListValue::const_iterator ListValue::Find(const Value& value) const {
1121 return std::find_if(list_.begin(), list_.end(), ValueEquals(&value));
1124 void ListValue::Swap(ListValue* other) {
1125 list_.swap(other->list_);
1128 bool ListValue::GetAsList(ListValue** out_value) {
1129 if (out_value)
1130 *out_value = this;
1131 return true;
1134 bool ListValue::GetAsList(const ListValue** out_value) const {
1135 if (out_value)
1136 *out_value = this;
1137 return true;
1140 ListValue* ListValue::DeepCopy() const {
1141 ListValue* result = new ListValue;
1143 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i)
1144 result->Append((*i)->DeepCopy());
1146 return result;
1149 scoped_ptr<ListValue> ListValue::CreateDeepCopy() const {
1150 return make_scoped_ptr(DeepCopy());
1153 bool ListValue::Equals(const Value* other) const {
1154 if (other->GetType() != GetType())
1155 return false;
1157 const ListValue* other_list =
1158 static_cast<const ListValue*>(other);
1159 const_iterator lhs_it, rhs_it;
1160 for (lhs_it = begin(), rhs_it = other_list->begin();
1161 lhs_it != end() && rhs_it != other_list->end();
1162 ++lhs_it, ++rhs_it) {
1163 if (!(*lhs_it)->Equals(*rhs_it))
1164 return false;
1166 if (lhs_it != end() || rhs_it != other_list->end())
1167 return false;
1169 return true;
1172 ValueSerializer::~ValueSerializer() {
1175 ValueDeserializer::~ValueDeserializer() {
1178 std::ostream& operator<<(std::ostream& out, const Value& value) {
1179 std::string json;
1180 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json);
1181 return out << json;
1184 } // namespace base