- add sources.
[platform/framework/web/crosswalk.git] / src / base / values.cc
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.
4
5 #include "base/values.h"
6
7 #include <string.h>
8
9 #include <algorithm>
10 #include <ostream>
11
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"
18
19 namespace base {
20
21 namespace {
22
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);
37       }
38       if (!copy->empty())
39         return copy;
40
41       delete copy;
42       return NULL;
43     }
44
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);
52       }
53       if (!copy->empty())
54         return copy;
55
56       delete copy;
57       return NULL;
58     }
59
60     default:
61       // For everything else, just make a copy.
62       return node->DeepCopy();
63   }
64 }
65
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) { }
73
74   bool operator ()(const Value* second) const {
75     return first_->Equals(second);
76   }
77
78  private:
79   const Value* first_;
80 };
81
82 }  // namespace
83
84 Value::~Value() {
85 }
86
87 // static
88 Value* Value::CreateNullValue() {
89   return new Value(TYPE_NULL);
90 }
91
92 // static
93 FundamentalValue* Value::CreateBooleanValue(bool in_value) {
94   return new FundamentalValue(in_value);
95 }
96
97 // static
98 FundamentalValue* Value::CreateIntegerValue(int in_value) {
99   return new FundamentalValue(in_value);
100 }
101
102 // static
103 FundamentalValue* Value::CreateDoubleValue(double in_value) {
104   return new FundamentalValue(in_value);
105 }
106
107 // static
108 StringValue* Value::CreateStringValue(const std::string& in_value) {
109   return new StringValue(in_value);
110 }
111
112 // static
113 StringValue* Value::CreateStringValue(const string16& in_value) {
114   return new StringValue(in_value);
115 }
116
117 bool Value::GetAsBoolean(bool* out_value) const {
118   return false;
119 }
120
121 bool Value::GetAsInteger(int* out_value) const {
122   return false;
123 }
124
125 bool Value::GetAsDouble(double* out_value) const {
126   return false;
127 }
128
129 bool Value::GetAsString(std::string* out_value) const {
130   return false;
131 }
132
133 bool Value::GetAsString(string16* out_value) const {
134   return false;
135 }
136
137 bool Value::GetAsList(ListValue** out_value) {
138   return false;
139 }
140
141 bool Value::GetAsList(const ListValue** out_value) const {
142   return false;
143 }
144
145 bool Value::GetAsDictionary(DictionaryValue** out_value) {
146   return false;
147 }
148
149 bool Value::GetAsDictionary(const DictionaryValue** out_value) const {
150   return false;
151 }
152
153 Value* Value::DeepCopy() const {
154   // This method should only be getting called for null Values--all subclasses
155   // need to provide their own implementation;.
156   DCHECK(IsType(TYPE_NULL));
157   return CreateNullValue();
158 }
159
160 bool Value::Equals(const Value* other) const {
161   // This method should only be getting called for null Values--all subclasses
162   // need to provide their own implementation;.
163   DCHECK(IsType(TYPE_NULL));
164   return other->IsType(TYPE_NULL);
165 }
166
167 // static
168 bool Value::Equals(const Value* a, const Value* b) {
169   if ((a == NULL) && (b == NULL)) return true;
170   if ((a == NULL) ^  (b == NULL)) return false;
171   return a->Equals(b);
172 }
173
174 Value::Value(Type type) : type_(type) {}
175
176 Value::Value(const Value& that) : type_(that.type_) {}
177
178 Value& Value::operator=(const Value& that) {
179   type_ = that.type_;
180   return *this;
181 }
182
183 ///////////////////// FundamentalValue ////////////////////
184
185 FundamentalValue::FundamentalValue(bool in_value)
186     : Value(TYPE_BOOLEAN), boolean_value_(in_value) {
187 }
188
189 FundamentalValue::FundamentalValue(int in_value)
190     : Value(TYPE_INTEGER), integer_value_(in_value) {
191 }
192
193 FundamentalValue::FundamentalValue(double in_value)
194     : Value(TYPE_DOUBLE), double_value_(in_value) {
195   if (!IsFinite(double_value_)) {
196     NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
197                  << "values cannot be represented in JSON";
198     double_value_ = 0.0;
199   }
200 }
201
202 FundamentalValue::~FundamentalValue() {
203 }
204
205 bool FundamentalValue::GetAsBoolean(bool* out_value) const {
206   if (out_value && IsType(TYPE_BOOLEAN))
207     *out_value = boolean_value_;
208   return (IsType(TYPE_BOOLEAN));
209 }
210
211 bool FundamentalValue::GetAsInteger(int* out_value) const {
212   if (out_value && IsType(TYPE_INTEGER))
213     *out_value = integer_value_;
214   return (IsType(TYPE_INTEGER));
215 }
216
217 bool FundamentalValue::GetAsDouble(double* out_value) const {
218   if (out_value && IsType(TYPE_DOUBLE))
219     *out_value = double_value_;
220   else if (out_value && IsType(TYPE_INTEGER))
221     *out_value = integer_value_;
222   return (IsType(TYPE_DOUBLE) || IsType(TYPE_INTEGER));
223 }
224
225 FundamentalValue* FundamentalValue::DeepCopy() const {
226   switch (GetType()) {
227     case TYPE_BOOLEAN:
228       return CreateBooleanValue(boolean_value_);
229
230     case TYPE_INTEGER:
231       return CreateIntegerValue(integer_value_);
232
233     case TYPE_DOUBLE:
234       return CreateDoubleValue(double_value_);
235
236     default:
237       NOTREACHED();
238       return NULL;
239   }
240 }
241
242 bool FundamentalValue::Equals(const Value* other) const {
243   if (other->GetType() != GetType())
244     return false;
245
246   switch (GetType()) {
247     case TYPE_BOOLEAN: {
248       bool lhs, rhs;
249       return GetAsBoolean(&lhs) && other->GetAsBoolean(&rhs) && lhs == rhs;
250     }
251     case TYPE_INTEGER: {
252       int lhs, rhs;
253       return GetAsInteger(&lhs) && other->GetAsInteger(&rhs) && lhs == rhs;
254     }
255     case TYPE_DOUBLE: {
256       double lhs, rhs;
257       return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs;
258     }
259     default:
260       NOTREACHED();
261       return false;
262   }
263 }
264
265 ///////////////////// StringValue ////////////////////
266
267 StringValue::StringValue(const std::string& in_value)
268     : Value(TYPE_STRING),
269       value_(in_value) {
270   DCHECK(IsStringUTF8(in_value));
271 }
272
273 StringValue::StringValue(const string16& in_value)
274     : Value(TYPE_STRING),
275       value_(UTF16ToUTF8(in_value)) {
276 }
277
278 StringValue::~StringValue() {
279 }
280
281 bool StringValue::GetAsString(std::string* out_value) const {
282   if (out_value)
283     *out_value = value_;
284   return true;
285 }
286
287 bool StringValue::GetAsString(string16* out_value) const {
288   if (out_value)
289     *out_value = UTF8ToUTF16(value_);
290   return true;
291 }
292
293 StringValue* StringValue::DeepCopy() const {
294   return CreateStringValue(value_);
295 }
296
297 bool StringValue::Equals(const Value* other) const {
298   if (other->GetType() != GetType())
299     return false;
300   std::string lhs, rhs;
301   return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
302 }
303
304 ///////////////////// BinaryValue ////////////////////
305
306 BinaryValue::BinaryValue()
307     : Value(TYPE_BINARY),
308       size_(0) {
309 }
310
311 BinaryValue::BinaryValue(scoped_ptr<char[]> buffer, size_t size)
312     : Value(TYPE_BINARY),
313       buffer_(buffer.Pass()),
314       size_(size) {
315 }
316
317 BinaryValue::~BinaryValue() {
318 }
319
320 // static
321 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer,
322                                                  size_t size) {
323   char* buffer_copy = new char[size];
324   memcpy(buffer_copy, buffer, size);
325   scoped_ptr<char[]> scoped_buffer_copy(buffer_copy);
326   return new BinaryValue(scoped_buffer_copy.Pass(), size);
327 }
328
329 BinaryValue* BinaryValue::DeepCopy() const {
330   return CreateWithCopiedBuffer(buffer_.get(), size_);
331 }
332
333 bool BinaryValue::Equals(const Value* other) const {
334   if (other->GetType() != GetType())
335     return false;
336   const BinaryValue* other_binary = static_cast<const BinaryValue*>(other);
337   if (other_binary->size_ != size_)
338     return false;
339   return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_);
340 }
341
342 ///////////////////// DictionaryValue ////////////////////
343
344 DictionaryValue::DictionaryValue()
345     : Value(TYPE_DICTIONARY) {
346 }
347
348 DictionaryValue::~DictionaryValue() {
349   Clear();
350 }
351
352 bool DictionaryValue::GetAsDictionary(DictionaryValue** out_value) {
353   if (out_value)
354     *out_value = this;
355   return true;
356 }
357
358 bool DictionaryValue::GetAsDictionary(const DictionaryValue** out_value) const {
359   if (out_value)
360     *out_value = this;
361   return true;
362 }
363
364 bool DictionaryValue::HasKey(const std::string& key) const {
365   DCHECK(IsStringUTF8(key));
366   ValueMap::const_iterator current_entry = dictionary_.find(key);
367   DCHECK((current_entry == dictionary_.end()) || current_entry->second);
368   return current_entry != dictionary_.end();
369 }
370
371 void DictionaryValue::Clear() {
372   ValueMap::iterator dict_iterator = dictionary_.begin();
373   while (dict_iterator != dictionary_.end()) {
374     delete dict_iterator->second;
375     ++dict_iterator;
376   }
377
378   dictionary_.clear();
379 }
380
381 void DictionaryValue::Set(const std::string& path, Value* in_value) {
382   DCHECK(IsStringUTF8(path));
383   DCHECK(in_value);
384
385   std::string current_path(path);
386   DictionaryValue* current_dictionary = this;
387   for (size_t delimiter_position = current_path.find('.');
388        delimiter_position != std::string::npos;
389        delimiter_position = current_path.find('.')) {
390     // Assume that we're indexing into a dictionary.
391     std::string key(current_path, 0, delimiter_position);
392     DictionaryValue* child_dictionary = NULL;
393     if (!current_dictionary->GetDictionary(key, &child_dictionary)) {
394       child_dictionary = new DictionaryValue;
395       current_dictionary->SetWithoutPathExpansion(key, child_dictionary);
396     }
397
398     current_dictionary = child_dictionary;
399     current_path.erase(0, delimiter_position + 1);
400   }
401
402   current_dictionary->SetWithoutPathExpansion(current_path, in_value);
403 }
404
405 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) {
406   Set(path, CreateBooleanValue(in_value));
407 }
408
409 void DictionaryValue::SetInteger(const std::string& path, int in_value) {
410   Set(path, CreateIntegerValue(in_value));
411 }
412
413 void DictionaryValue::SetDouble(const std::string& path, double in_value) {
414   Set(path, CreateDoubleValue(in_value));
415 }
416
417 void DictionaryValue::SetString(const std::string& path,
418                                 const std::string& in_value) {
419   Set(path, CreateStringValue(in_value));
420 }
421
422 void DictionaryValue::SetString(const std::string& path,
423                                 const string16& in_value) {
424   Set(path, CreateStringValue(in_value));
425 }
426
427 void DictionaryValue::SetWithoutPathExpansion(const std::string& key,
428                                               Value* in_value) {
429   // If there's an existing value here, we need to delete it, because
430   // we own all our children.
431   std::pair<ValueMap::iterator, bool> ins_res =
432       dictionary_.insert(std::make_pair(key, in_value));
433   if (!ins_res.second) {
434     DCHECK_NE(ins_res.first->second, in_value);  // This would be bogus
435     delete ins_res.first->second;
436     ins_res.first->second = in_value;
437   }
438 }
439
440 void DictionaryValue::SetBooleanWithoutPathExpansion(
441     const std::string& path, bool in_value) {
442   SetWithoutPathExpansion(path, CreateBooleanValue(in_value));
443 }
444
445 void DictionaryValue::SetIntegerWithoutPathExpansion(
446     const std::string& path, int in_value) {
447   SetWithoutPathExpansion(path, CreateIntegerValue(in_value));
448 }
449
450 void DictionaryValue::SetDoubleWithoutPathExpansion(
451     const std::string& path, double in_value) {
452   SetWithoutPathExpansion(path, CreateDoubleValue(in_value));
453 }
454
455 void DictionaryValue::SetStringWithoutPathExpansion(
456     const std::string& path, const std::string& in_value) {
457   SetWithoutPathExpansion(path, CreateStringValue(in_value));
458 }
459
460 void DictionaryValue::SetStringWithoutPathExpansion(
461     const std::string& path, const string16& in_value) {
462   SetWithoutPathExpansion(path, CreateStringValue(in_value));
463 }
464
465 bool DictionaryValue::Get(
466     const std::string& path, const Value** out_value) const {
467   DCHECK(IsStringUTF8(path));
468 //  LOG(WARNING) << "\n1\n";
469   std::string current_path(path);
470   const DictionaryValue* current_dictionary = this;
471 //  LOG(WARNING) << "\n2\n";
472   for (size_t delimiter_position = current_path.find('.');
473        delimiter_position != std::string::npos;
474        delimiter_position = current_path.find('.')) {
475     const DictionaryValue* child_dictionary = NULL;
476     if (!current_dictionary->GetDictionary(
477             current_path.substr(0, delimiter_position), &child_dictionary))
478       return false;
479
480     current_dictionary = child_dictionary;
481     current_path.erase(0, delimiter_position + 1);
482   }
483 //  LOG(WARNING) << "\n3\n";
484
485   return current_dictionary->GetWithoutPathExpansion(current_path, out_value);
486 }
487
488 bool DictionaryValue::Get(const std::string& path, Value** out_value)  {
489   return static_cast<const DictionaryValue&>(*this).Get(
490       path,
491       const_cast<const Value**>(out_value));
492 }
493
494 bool DictionaryValue::GetBoolean(const std::string& path,
495                                  bool* bool_value) const {
496   const Value* value;
497   if (!Get(path, &value))
498     return false;
499
500   return value->GetAsBoolean(bool_value);
501 }
502
503 bool DictionaryValue::GetInteger(const std::string& path,
504                                  int* out_value) const {
505   const Value* value;
506   if (!Get(path, &value))
507     return false;
508
509   return value->GetAsInteger(out_value);
510 }
511
512 bool DictionaryValue::GetDouble(const std::string& path,
513                                 double* out_value) const {
514   const Value* value;
515   if (!Get(path, &value))
516     return false;
517
518   return value->GetAsDouble(out_value);
519 }
520
521 bool DictionaryValue::GetString(const std::string& path,
522                                 std::string* out_value) const {
523   const Value* value;
524   if (!Get(path, &value))
525     return false;
526
527   return value->GetAsString(out_value);
528 }
529
530 bool DictionaryValue::GetString(const std::string& path,
531                                 string16* out_value) const {
532   const Value* value;
533   if (!Get(path, &value))
534     return false;
535
536   return value->GetAsString(out_value);
537 }
538
539 bool DictionaryValue::GetStringASCII(const std::string& path,
540                                      std::string* out_value) const {
541   std::string out;
542   if (!GetString(path, &out))
543     return false;
544
545   if (!IsStringASCII(out)) {
546     NOTREACHED();
547     return false;
548   }
549
550   out_value->assign(out);
551   return true;
552 }
553
554 bool DictionaryValue::GetBinary(const std::string& path,
555                                 const BinaryValue** out_value) const {
556   const Value* value;
557   bool result = Get(path, &value);
558   if (!result || !value->IsType(TYPE_BINARY))
559     return false;
560
561   if (out_value)
562     *out_value = static_cast<const BinaryValue*>(value);
563
564   return true;
565 }
566
567 bool DictionaryValue::GetBinary(const std::string& path,
568                                 BinaryValue** out_value) {
569   return static_cast<const DictionaryValue&>(*this).GetBinary(
570       path,
571       const_cast<const BinaryValue**>(out_value));
572 }
573
574 bool DictionaryValue::GetDictionary(const std::string& path,
575                                     const DictionaryValue** out_value) const {
576   const Value* value;
577   bool result = Get(path, &value);
578   if (!result || !value->IsType(TYPE_DICTIONARY))
579     return false;
580
581   if (out_value)
582     *out_value = static_cast<const DictionaryValue*>(value);
583
584   return true;
585 }
586
587 bool DictionaryValue::GetDictionary(const std::string& path,
588                                     DictionaryValue** out_value) {
589   return static_cast<const DictionaryValue&>(*this).GetDictionary(
590       path,
591       const_cast<const DictionaryValue**>(out_value));
592 }
593
594 bool DictionaryValue::GetList(const std::string& path,
595                               const ListValue** out_value) const {
596   const Value* value;
597   bool result = Get(path, &value);
598   if (!result || !value->IsType(TYPE_LIST))
599     return false;
600
601   if (out_value)
602     *out_value = static_cast<const ListValue*>(value);
603
604   return true;
605 }
606
607 bool DictionaryValue::GetList(const std::string& path, ListValue** out_value) {
608   return static_cast<const DictionaryValue&>(*this).GetList(
609       path,
610       const_cast<const ListValue**>(out_value));
611 }
612
613 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
614                                               const Value** out_value) const {
615   DCHECK(IsStringUTF8(key));
616   ValueMap::const_iterator entry_iterator = dictionary_.find(key);
617   if (entry_iterator == dictionary_.end())
618     return false;
619
620   const Value* entry = entry_iterator->second;
621   if (out_value)
622     *out_value = entry;
623   return true;
624 }
625
626 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key,
627                                               Value** out_value) {
628   return static_cast<const DictionaryValue&>(*this).GetWithoutPathExpansion(
629       key,
630       const_cast<const Value**>(out_value));
631 }
632
633 bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string& key,
634                                                      bool* out_value) const {
635   const Value* value;
636   if (!GetWithoutPathExpansion(key, &value))
637     return false;
638
639   return value->GetAsBoolean(out_value);
640 }
641
642 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string& key,
643                                                      int* out_value) const {
644   const Value* value;
645   if (!GetWithoutPathExpansion(key, &value))
646     return false;
647
648   return value->GetAsInteger(out_value);
649 }
650
651 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string& key,
652                                                     double* out_value) const {
653   const Value* value;
654   if (!GetWithoutPathExpansion(key, &value))
655     return false;
656
657   return value->GetAsDouble(out_value);
658 }
659
660 bool DictionaryValue::GetStringWithoutPathExpansion(
661     const std::string& key,
662     std::string* out_value) const {
663   const Value* value;
664   if (!GetWithoutPathExpansion(key, &value))
665     return false;
666
667   return value->GetAsString(out_value);
668 }
669
670 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string& key,
671                                                     string16* out_value) const {
672   const Value* value;
673   if (!GetWithoutPathExpansion(key, &value))
674     return false;
675
676   return value->GetAsString(out_value);
677 }
678
679 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
680     const std::string& key,
681     const DictionaryValue** out_value) const {
682   const Value* value;
683   bool result = GetWithoutPathExpansion(key, &value);
684   if (!result || !value->IsType(TYPE_DICTIONARY))
685     return false;
686
687   if (out_value)
688     *out_value = static_cast<const DictionaryValue*>(value);
689
690   return true;
691 }
692
693 bool DictionaryValue::GetDictionaryWithoutPathExpansion(
694     const std::string& key,
695     DictionaryValue** out_value) {
696   const DictionaryValue& const_this =
697       static_cast<const DictionaryValue&>(*this);
698   return const_this.GetDictionaryWithoutPathExpansion(
699           key,
700           const_cast<const DictionaryValue**>(out_value));
701 }
702
703 bool DictionaryValue::GetListWithoutPathExpansion(
704     const std::string& key,
705     const ListValue** out_value) const {
706   const Value* value;
707   bool result = GetWithoutPathExpansion(key, &value);
708   if (!result || !value->IsType(TYPE_LIST))
709     return false;
710
711   if (out_value)
712     *out_value = static_cast<const ListValue*>(value);
713
714   return true;
715 }
716
717 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key,
718                                                   ListValue** out_value) {
719   return
720       static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion(
721           key,
722           const_cast<const ListValue**>(out_value));
723 }
724
725 bool DictionaryValue::Remove(const std::string& path,
726                              scoped_ptr<Value>* out_value) {
727   DCHECK(IsStringUTF8(path));
728   std::string current_path(path);
729   DictionaryValue* current_dictionary = this;
730   size_t delimiter_position = current_path.rfind('.');
731   if (delimiter_position != std::string::npos) {
732     if (!GetDictionary(current_path.substr(0, delimiter_position),
733                        &current_dictionary))
734       return false;
735     current_path.erase(0, delimiter_position + 1);
736   }
737
738   return current_dictionary->RemoveWithoutPathExpansion(current_path,
739                                                         out_value);
740 }
741
742 bool DictionaryValue::RemoveWithoutPathExpansion(const std::string& key,
743                                                  scoped_ptr<Value>* out_value) {
744   DCHECK(IsStringUTF8(key));
745   ValueMap::iterator entry_iterator = dictionary_.find(key);
746   if (entry_iterator == dictionary_.end())
747     return false;
748
749   Value* entry = entry_iterator->second;
750   if (out_value)
751     out_value->reset(entry);
752   else
753     delete entry;
754   dictionary_.erase(entry_iterator);
755   return true;
756 }
757
758 DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() const {
759   Value* copy = CopyWithoutEmptyChildren(this);
760   return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue;
761 }
762
763 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) {
764   for (DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); it.Advance()) {
765     const Value* merge_value = &it.value();
766     // Check whether we have to merge dictionaries.
767     if (merge_value->IsType(Value::TYPE_DICTIONARY)) {
768       DictionaryValue* sub_dict;
769       if (GetDictionaryWithoutPathExpansion(it.key(), &sub_dict)) {
770         sub_dict->MergeDictionary(
771             static_cast<const DictionaryValue*>(merge_value));
772         continue;
773       }
774     }
775     // All other cases: Make a copy and hook it up.
776     SetWithoutPathExpansion(it.key(), merge_value->DeepCopy());
777   }
778 }
779
780 void DictionaryValue::Swap(DictionaryValue* other) {
781   dictionary_.swap(other->dictionary_);
782 }
783
784 DictionaryValue::Iterator::Iterator(const DictionaryValue& target)
785     : target_(target),
786       it_(target.dictionary_.begin()) {}
787
788 DictionaryValue* DictionaryValue::DeepCopy() const {
789   DictionaryValue* result = new DictionaryValue;
790
791   for (ValueMap::const_iterator current_entry(dictionary_.begin());
792        current_entry != dictionary_.end(); ++current_entry) {
793     result->SetWithoutPathExpansion(current_entry->first,
794                                     current_entry->second->DeepCopy());
795   }
796
797   return result;
798 }
799
800 bool DictionaryValue::Equals(const Value* other) const {
801   if (other->GetType() != GetType())
802     return false;
803
804   const DictionaryValue* other_dict =
805       static_cast<const DictionaryValue*>(other);
806   Iterator lhs_it(*this);
807   Iterator rhs_it(*other_dict);
808   while (!lhs_it.IsAtEnd() && !rhs_it.IsAtEnd()) {
809     if (lhs_it.key() != rhs_it.key() ||
810         !lhs_it.value().Equals(&rhs_it.value())) {
811       return false;
812     }
813     lhs_it.Advance();
814     rhs_it.Advance();
815   }
816   if (!lhs_it.IsAtEnd() || !rhs_it.IsAtEnd())
817     return false;
818
819   return true;
820 }
821
822 ///////////////////// ListValue ////////////////////
823
824 ListValue::ListValue() : Value(TYPE_LIST) {
825 }
826
827 ListValue::~ListValue() {
828   Clear();
829 }
830
831 void ListValue::Clear() {
832   for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i)
833     delete *i;
834   list_.clear();
835 }
836
837 bool ListValue::Set(size_t index, Value* in_value) {
838   if (!in_value)
839     return false;
840
841   if (index >= list_.size()) {
842     // Pad out any intermediate indexes with null settings
843     while (index > list_.size())
844       Append(CreateNullValue());
845     Append(in_value);
846   } else {
847     DCHECK(list_[index] != in_value);
848     delete list_[index];
849     list_[index] = in_value;
850   }
851   return true;
852 }
853
854 bool ListValue::Get(size_t index, const Value** out_value) const {
855   if (index >= list_.size())
856     return false;
857
858   if (out_value)
859     *out_value = list_[index];
860
861   return true;
862 }
863
864 bool ListValue::Get(size_t index, Value** out_value) {
865   return static_cast<const ListValue&>(*this).Get(
866       index,
867       const_cast<const Value**>(out_value));
868 }
869
870 bool ListValue::GetBoolean(size_t index, bool* bool_value) const {
871   const Value* value;
872   if (!Get(index, &value))
873     return false;
874
875   return value->GetAsBoolean(bool_value);
876 }
877
878 bool ListValue::GetInteger(size_t index, int* out_value) const {
879   const Value* value;
880   if (!Get(index, &value))
881     return false;
882
883   return value->GetAsInteger(out_value);
884 }
885
886 bool ListValue::GetDouble(size_t index, double* out_value) const {
887   const Value* value;
888   if (!Get(index, &value))
889     return false;
890
891   return value->GetAsDouble(out_value);
892 }
893
894 bool ListValue::GetString(size_t index, std::string* out_value) const {
895   const Value* value;
896   if (!Get(index, &value))
897     return false;
898
899   return value->GetAsString(out_value);
900 }
901
902 bool ListValue::GetString(size_t index, string16* out_value) const {
903   const Value* value;
904   if (!Get(index, &value))
905     return false;
906
907   return value->GetAsString(out_value);
908 }
909
910 bool ListValue::GetBinary(size_t index, const BinaryValue** out_value) const {
911   const Value* value;
912   bool result = Get(index, &value);
913   if (!result || !value->IsType(TYPE_BINARY))
914     return false;
915
916   if (out_value)
917     *out_value = static_cast<const BinaryValue*>(value);
918
919   return true;
920 }
921
922 bool ListValue::GetBinary(size_t index, BinaryValue** out_value) {
923   return static_cast<const ListValue&>(*this).GetBinary(
924       index,
925       const_cast<const BinaryValue**>(out_value));
926 }
927
928 bool ListValue::GetDictionary(size_t index,
929                               const DictionaryValue** out_value) const {
930   const Value* value;
931   bool result = Get(index, &value);
932   if (!result || !value->IsType(TYPE_DICTIONARY))
933     return false;
934
935   if (out_value)
936     *out_value = static_cast<const DictionaryValue*>(value);
937
938   return true;
939 }
940
941 bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) {
942   return static_cast<const ListValue&>(*this).GetDictionary(
943       index,
944       const_cast<const DictionaryValue**>(out_value));
945 }
946
947 bool ListValue::GetList(size_t index, const ListValue** out_value) const {
948   const Value* value;
949   bool result = Get(index, &value);
950   if (!result || !value->IsType(TYPE_LIST))
951     return false;
952
953   if (out_value)
954     *out_value = static_cast<const ListValue*>(value);
955
956   return true;
957 }
958
959 bool ListValue::GetList(size_t index, ListValue** out_value) {
960   return static_cast<const ListValue&>(*this).GetList(
961       index,
962       const_cast<const ListValue**>(out_value));
963 }
964
965 bool ListValue::Remove(size_t index, scoped_ptr<Value>* out_value) {
966   if (index >= list_.size())
967     return false;
968
969   if (out_value)
970     out_value->reset(list_[index]);
971   else
972     delete list_[index];
973
974   list_.erase(list_.begin() + index);
975   return true;
976 }
977
978 bool ListValue::Remove(const Value& value, size_t* index) {
979   for (ValueVector::iterator i(list_.begin()); i != list_.end(); ++i) {
980     if ((*i)->Equals(&value)) {
981       size_t previous_index = i - list_.begin();
982       delete *i;
983       list_.erase(i);
984
985       if (index)
986         *index = previous_index;
987       return true;
988     }
989   }
990   return false;
991 }
992
993 ListValue::iterator ListValue::Erase(iterator iter,
994                                      scoped_ptr<Value>* out_value) {
995   if (out_value)
996     out_value->reset(*iter);
997   else
998     delete *iter;
999
1000   return list_.erase(iter);
1001 }
1002
1003 void ListValue::Append(Value* in_value) {
1004   DCHECK(in_value);
1005   list_.push_back(in_value);
1006 }
1007
1008 void ListValue::AppendBoolean(bool in_value) {
1009   Append(CreateBooleanValue(in_value));
1010 }
1011
1012 void ListValue::AppendInteger(int in_value) {
1013   Append(CreateIntegerValue(in_value));
1014 }
1015
1016 void ListValue::AppendDouble(double in_value) {
1017   Append(CreateDoubleValue(in_value));
1018 }
1019
1020 void ListValue::AppendString(const std::string& in_value) {
1021   Append(CreateStringValue(in_value));
1022 }
1023
1024 void ListValue::AppendString(const string16& in_value) {
1025   Append(CreateStringValue(in_value));
1026 }
1027
1028 void ListValue::AppendStrings(const std::vector<std::string>& in_values) {
1029   for (std::vector<std::string>::const_iterator it = in_values.begin();
1030        it != in_values.end(); ++it) {
1031     AppendString(*it);
1032   }
1033 }
1034
1035 void ListValue::AppendStrings(const std::vector<string16>& in_values) {
1036   for (std::vector<string16>::const_iterator it = in_values.begin();
1037        it != in_values.end(); ++it) {
1038     AppendString(*it);
1039   }
1040 }
1041
1042 bool ListValue::AppendIfNotPresent(Value* in_value) {
1043   DCHECK(in_value);
1044   for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) {
1045     if ((*i)->Equals(in_value)) {
1046       delete in_value;
1047       return false;
1048     }
1049   }
1050   list_.push_back(in_value);
1051   return true;
1052 }
1053
1054 bool ListValue::Insert(size_t index, Value* in_value) {
1055   DCHECK(in_value);
1056   if (index > list_.size())
1057     return false;
1058
1059   list_.insert(list_.begin() + index, in_value);
1060   return true;
1061 }
1062
1063 ListValue::const_iterator ListValue::Find(const Value& value) const {
1064   return std::find_if(list_.begin(), list_.end(), ValueEquals(&value));
1065 }
1066
1067 void ListValue::Swap(ListValue* other) {
1068   list_.swap(other->list_);
1069 }
1070
1071 bool ListValue::GetAsList(ListValue** out_value) {
1072   if (out_value)
1073     *out_value = this;
1074   return true;
1075 }
1076
1077 bool ListValue::GetAsList(const ListValue** out_value) const {
1078   if (out_value)
1079     *out_value = this;
1080   return true;
1081 }
1082
1083 ListValue* ListValue::DeepCopy() const {
1084   ListValue* result = new ListValue;
1085
1086   for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i)
1087     result->Append((*i)->DeepCopy());
1088
1089   return result;
1090 }
1091
1092 bool ListValue::Equals(const Value* other) const {
1093   if (other->GetType() != GetType())
1094     return false;
1095
1096   const ListValue* other_list =
1097       static_cast<const ListValue*>(other);
1098   const_iterator lhs_it, rhs_it;
1099   for (lhs_it = begin(), rhs_it = other_list->begin();
1100        lhs_it != end() && rhs_it != other_list->end();
1101        ++lhs_it, ++rhs_it) {
1102     if (!(*lhs_it)->Equals(*rhs_it))
1103       return false;
1104   }
1105   if (lhs_it != end() || rhs_it != other_list->end())
1106     return false;
1107
1108   return true;
1109 }
1110
1111 ValueSerializer::~ValueSerializer() {
1112 }
1113
1114 std::ostream& operator<<(std::ostream& out, const Value& value) {
1115   std::string json;
1116   JSONWriter::WriteWithOptions(&value,
1117                                JSONWriter::OPTIONS_PRETTY_PRINT,
1118                                &json);
1119   return out << json;
1120 }
1121
1122 }  // namespace base