| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/values.h" | 5 #include "base/values.h" |
| 6 | 6 |
| 7 #include "base/float_util.h" | 7 #include "base/float_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 // Make a deep copy of |node|, but don't include empty lists or dictionaries | 14 // Make a deep copy of |node|, but don't include empty lists or dictionaries |
| 15 // in the copy. It's possible for this function to return NULL and it | 15 // in the copy. It's possible for this function to return NULL and it |
| 16 // expects |node| to always be non-NULL. | 16 // expects |node| to always be non-NULL. |
| 17 Value* CopyWithoutEmptyChildren(Value* node) { | 17 Value* CopyWithoutEmptyChildren(Value* node) { |
| 18 DCHECK(node); | 18 DCHECK(node); |
| 19 switch (node->GetType()) { | 19 switch (node->GetType()) { |
| 20 case Value::TYPE_LIST: { | 20 case Value::TYPE_LIST: { |
| 21 ListValue* list = static_cast<ListValue*>(node); | 21 ListValue* list = node->AsList(); |
| 22 ListValue* copy = new ListValue; | 22 ListValue* copy = new ListValue; |
| 23 for (ListValue::const_iterator it = list->begin(); it != list->end(); | 23 for (ListValue::const_iterator it = list->begin(); it != list->end(); |
| 24 ++it) { | 24 ++it) { |
| 25 Value* child_copy = CopyWithoutEmptyChildren(*it); | 25 Value* child_copy = CopyWithoutEmptyChildren(*it); |
| 26 if (child_copy) | 26 if (child_copy) |
| 27 copy->Append(child_copy); | 27 copy->Append(child_copy); |
| 28 } | 28 } |
| 29 if (!copy->empty()) | 29 if (!copy->empty()) |
| 30 return copy; | 30 return copy; |
| 31 | 31 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 // static | 90 // static |
| 91 StringValue* Value::CreateStringValue(const std::string& in_value) { | 91 StringValue* Value::CreateStringValue(const std::string& in_value) { |
| 92 return new StringValue(in_value); | 92 return new StringValue(in_value); |
| 93 } | 93 } |
| 94 | 94 |
| 95 // static | 95 // static |
| 96 StringValue* Value::CreateStringValue(const string16& in_value) { | 96 StringValue* Value::CreateStringValue(const string16& in_value) { |
| 97 return new StringValue(in_value); | 97 return new StringValue(in_value); |
| 98 } | 98 } |
| 99 | 99 |
| 100 BinaryValue* Value::AsBinary() { |
| 101 return NULL; |
| 102 } |
| 103 |
| 100 ListValue* Value::AsList() { | 104 ListValue* Value::AsList() { |
| 101 return NULL; | 105 return NULL; |
| 102 } | 106 } |
| 103 | 107 |
| 104 bool Value::GetAsBoolean(bool* out_value) const { | 108 bool Value::GetAsBoolean(bool* out_value) const { |
| 105 return false; | 109 return false; |
| 106 } | 110 } |
| 107 | 111 |
| 108 bool Value::GetAsInteger(int* out_value) const { | 112 bool Value::GetAsInteger(int* out_value) const { |
| 109 return false; | 113 return false; |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, | 298 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, |
| 295 size_t size) { | 299 size_t size) { |
| 296 if (!buffer) | 300 if (!buffer) |
| 297 return NULL; | 301 return NULL; |
| 298 | 302 |
| 299 char* buffer_copy = new char[size]; | 303 char* buffer_copy = new char[size]; |
| 300 memcpy(buffer_copy, buffer, size); | 304 memcpy(buffer_copy, buffer, size); |
| 301 return new BinaryValue(buffer_copy, size); | 305 return new BinaryValue(buffer_copy, size); |
| 302 } | 306 } |
| 303 | 307 |
| 308 BinaryValue* BinaryValue::AsBinary() { |
| 309 return this; |
| 310 } |
| 311 |
| 304 BinaryValue* BinaryValue::DeepCopy() const { | 312 BinaryValue* BinaryValue::DeepCopy() const { |
| 305 return CreateWithCopiedBuffer(buffer_, size_); | 313 return CreateWithCopiedBuffer(buffer_, size_); |
| 306 } | 314 } |
| 307 | 315 |
| 308 bool BinaryValue::Equals(const Value* other) const { | 316 bool BinaryValue::Equals(const Value* other) const { |
| 309 if (other->GetType() != GetType()) | 317 if (other->GetType() != GetType()) |
| 310 return false; | 318 return false; |
| 311 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other); | 319 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other); |
| 312 if (other_binary->size_ != size_) | 320 if (other_binary->size_ != size_) |
| 313 return false; | 321 return false; |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 } | 490 } |
| 483 | 491 |
| 484 out_value->assign(out); | 492 out_value->assign(out); |
| 485 return true; | 493 return true; |
| 486 } | 494 } |
| 487 | 495 |
| 488 bool DictionaryValue::GetBinary(const std::string& path, | 496 bool DictionaryValue::GetBinary(const std::string& path, |
| 489 BinaryValue** out_value) const { | 497 BinaryValue** out_value) const { |
| 490 Value* value; | 498 Value* value; |
| 491 bool result = Get(path, &value); | 499 bool result = Get(path, &value); |
| 492 if (!result || !value->IsType(TYPE_BINARY)) | 500 if (!result || !value->AsBinary()) |
| 493 return false; | 501 return false; |
| 494 | 502 |
| 495 if (out_value) | 503 if (out_value) |
| 496 *out_value = static_cast<BinaryValue*>(value); | 504 *out_value = value->AsBinary(); |
| 497 | 505 |
| 498 return true; | 506 return true; |
| 499 } | 507 } |
| 500 | 508 |
| 501 bool DictionaryValue::GetDictionary(const std::string& path, | 509 bool DictionaryValue::GetDictionary(const std::string& path, |
| 502 DictionaryValue** out_value) const { | 510 DictionaryValue** out_value) const { |
| 503 Value* value; | 511 Value* value; |
| 504 bool result = Get(path, &value); | 512 bool result = Get(path, &value); |
| 505 if (!result || !value->IsType(TYPE_DICTIONARY)) | 513 if (!result || !value->IsType(TYPE_DICTIONARY)) |
| 506 return false; | 514 return false; |
| 507 | 515 |
| 508 if (out_value) | 516 if (out_value) |
| 509 *out_value = static_cast<DictionaryValue*>(value); | 517 *out_value = static_cast<DictionaryValue*>(value); |
| 510 | 518 |
| 511 return true; | 519 return true; |
| 512 } | 520 } |
| 513 | 521 |
| 514 bool DictionaryValue::GetList(const std::string& path, | 522 bool DictionaryValue::GetList(const std::string& path, |
| 515 ListValue** out_value) const { | 523 ListValue** out_value) const { |
| 516 Value* value; | 524 Value* value; |
| 517 bool result = Get(path, &value); | 525 bool result = Get(path, &value); |
| 518 if (!result || !value->AsList()) | 526 if (!result || !value->AsList()) |
| 519 return false; | 527 return false; |
| 520 | 528 |
| 521 if (out_value) | 529 if (out_value) |
| 522 *out_value = static_cast<ListValue*>(value); | 530 *out_value = value->AsList(); |
| 523 | 531 |
| 524 return true; | 532 return true; |
| 525 } | 533 } |
| 526 | 534 |
| 527 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, | 535 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, |
| 528 Value** out_value) const { | 536 Value** out_value) const { |
| 529 DCHECK(IsStringUTF8(key)); | 537 DCHECK(IsStringUTF8(key)); |
| 530 ValueMap::const_iterator entry_iterator = dictionary_.find(key); | 538 ValueMap::const_iterator entry_iterator = dictionary_.find(key); |
| 531 if (entry_iterator == dictionary_.end()) | 539 if (entry_iterator == dictionary_.end()) |
| 532 return false; | 540 return false; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 590 } | 598 } |
| 591 | 599 |
| 592 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key, | 600 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key, |
| 593 ListValue** out_value) const { | 601 ListValue** out_value) const { |
| 594 Value* value; | 602 Value* value; |
| 595 bool result = GetWithoutPathExpansion(key, &value); | 603 bool result = GetWithoutPathExpansion(key, &value); |
| 596 if (!result || !value->AsList()) | 604 if (!result || !value->AsList()) |
| 597 return false; | 605 return false; |
| 598 | 606 |
| 599 if (out_value) | 607 if (out_value) |
| 600 *out_value = static_cast<ListValue*>(value); | 608 *out_value = value->AsList(); |
| 601 | 609 |
| 602 return true; | 610 return true; |
| 603 } | 611 } |
| 604 | 612 |
| 605 bool DictionaryValue::Remove(const std::string& path, Value** out_value) { | 613 bool DictionaryValue::Remove(const std::string& path, Value** out_value) { |
| 606 DCHECK(IsStringUTF8(path)); | 614 DCHECK(IsStringUTF8(path)); |
| 607 std::string current_path(path); | 615 std::string current_path(path); |
| 608 DictionaryValue* current_dictionary = this; | 616 DictionaryValue* current_dictionary = this; |
| 609 size_t delimiter_position = current_path.rfind('.'); | 617 size_t delimiter_position = current_path.rfind('.'); |
| 610 if (delimiter_position != std::string::npos) { | 618 if (delimiter_position != std::string::npos) { |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 803 return true; | 811 return true; |
| 804 } | 812 } |
| 805 | 813 |
| 806 bool ListValue::GetList(size_t index, ListValue** out_value) const { | 814 bool ListValue::GetList(size_t index, ListValue** out_value) const { |
| 807 Value* value; | 815 Value* value; |
| 808 bool result = Get(index, &value); | 816 bool result = Get(index, &value); |
| 809 if (!result || !value->AsList()) | 817 if (!result || !value->AsList()) |
| 810 return false; | 818 return false; |
| 811 | 819 |
| 812 if (out_value) | 820 if (out_value) |
| 813 *out_value = static_cast<ListValue*>(value); | 821 *out_value = value->AsList(); |
| 814 | 822 |
| 815 return true; | 823 return true; |
| 816 } | 824 } |
| 817 | 825 |
| 818 bool ListValue::Remove(size_t index, Value** out_value) { | 826 bool ListValue::Remove(size_t index, Value** out_value) { |
| 819 if (index >= list_.size()) | 827 if (index >= list_.size()) |
| 820 return false; | 828 return false; |
| 821 | 829 |
| 822 if (out_value) | 830 if (out_value) |
| 823 *out_value = list_[index]; | 831 *out_value = list_[index]; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 910 if (lhs_it != end() || rhs_it != other_list->end()) | 918 if (lhs_it != end() || rhs_it != other_list->end()) |
| 911 return false; | 919 return false; |
| 912 | 920 |
| 913 return true; | 921 return true; |
| 914 } | 922 } |
| 915 | 923 |
| 916 ValueSerializer::~ValueSerializer() { | 924 ValueSerializer::~ValueSerializer() { |
| 917 } | 925 } |
| 918 | 926 |
| 919 } // namespace base | 927 } // namespace base |
| OLD | NEW |