| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/float_util.h" | 9 #include "base/float_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 // we own all our children. | 434 // we own all our children. |
| 435 std::pair<ValueMap::iterator, bool> ins_res = | 435 std::pair<ValueMap::iterator, bool> ins_res = |
| 436 dictionary_.insert(std::make_pair(key, in_value)); | 436 dictionary_.insert(std::make_pair(key, in_value)); |
| 437 if (!ins_res.second) { | 437 if (!ins_res.second) { |
| 438 DCHECK_NE(ins_res.first->second, in_value); // This would be bogus | 438 DCHECK_NE(ins_res.first->second, in_value); // This would be bogus |
| 439 delete ins_res.first->second; | 439 delete ins_res.first->second; |
| 440 ins_res.first->second = in_value; | 440 ins_res.first->second = in_value; |
| 441 } | 441 } |
| 442 } | 442 } |
| 443 | 443 |
| 444 bool DictionaryValue::Get(const std::string& path, Value** out_value) const { | 444 bool DictionaryValue::Get( |
| 445 const std::string& path, const Value** out_value) const { |
| 445 DCHECK(IsStringUTF8(path)); | 446 DCHECK(IsStringUTF8(path)); |
| 446 std::string current_path(path); | 447 std::string current_path(path); |
| 447 const DictionaryValue* current_dictionary = this; | 448 const DictionaryValue* current_dictionary = this; |
| 448 for (size_t delimiter_position = current_path.find('.'); | 449 for (size_t delimiter_position = current_path.find('.'); |
| 449 delimiter_position != std::string::npos; | 450 delimiter_position != std::string::npos; |
| 450 delimiter_position = current_path.find('.')) { | 451 delimiter_position = current_path.find('.')) { |
| 451 DictionaryValue* child_dictionary = NULL; | 452 const DictionaryValue* child_dictionary = NULL; |
| 452 if (!current_dictionary->GetDictionary( | 453 if (!current_dictionary->GetDictionary( |
| 453 current_path.substr(0, delimiter_position), &child_dictionary)) | 454 current_path.substr(0, delimiter_position), &child_dictionary)) |
| 454 return false; | 455 return false; |
| 455 | 456 |
| 456 current_dictionary = child_dictionary; | 457 current_dictionary = child_dictionary; |
| 457 current_path.erase(0, delimiter_position + 1); | 458 current_path.erase(0, delimiter_position + 1); |
| 458 } | 459 } |
| 459 | 460 |
| 460 return current_dictionary->GetWithoutPathExpansion(current_path, out_value); | 461 return current_dictionary->GetWithoutPathExpansion(current_path, out_value); |
| 461 } | 462 } |
| 462 | 463 |
| 464 bool DictionaryValue::Get(const std::string& path, Value** out_value) { |
| 465 return static_cast<const DictionaryValue&>(*this).Get( |
| 466 path, |
| 467 const_cast<const Value**>(out_value)); |
| 468 } |
| 469 |
| 463 bool DictionaryValue::GetBoolean(const std::string& path, | 470 bool DictionaryValue::GetBoolean(const std::string& path, |
| 464 bool* bool_value) const { | 471 bool* bool_value) const { |
| 465 Value* value; | 472 const Value* value; |
| 466 if (!Get(path, &value)) | 473 if (!Get(path, &value)) |
| 467 return false; | 474 return false; |
| 468 | 475 |
| 469 return value->GetAsBoolean(bool_value); | 476 return value->GetAsBoolean(bool_value); |
| 470 } | 477 } |
| 471 | 478 |
| 472 bool DictionaryValue::GetInteger(const std::string& path, | 479 bool DictionaryValue::GetInteger(const std::string& path, |
| 473 int* out_value) const { | 480 int* out_value) const { |
| 474 Value* value; | 481 const Value* value; |
| 475 if (!Get(path, &value)) | 482 if (!Get(path, &value)) |
| 476 return false; | 483 return false; |
| 477 | 484 |
| 478 return value->GetAsInteger(out_value); | 485 return value->GetAsInteger(out_value); |
| 479 } | 486 } |
| 480 | 487 |
| 481 bool DictionaryValue::GetDouble(const std::string& path, | 488 bool DictionaryValue::GetDouble(const std::string& path, |
| 482 double* out_value) const { | 489 double* out_value) const { |
| 483 Value* value; | 490 const Value* value; |
| 484 if (!Get(path, &value)) | 491 if (!Get(path, &value)) |
| 485 return false; | 492 return false; |
| 486 | 493 |
| 487 return value->GetAsDouble(out_value); | 494 return value->GetAsDouble(out_value); |
| 488 } | 495 } |
| 489 | 496 |
| 490 bool DictionaryValue::GetString(const std::string& path, | 497 bool DictionaryValue::GetString(const std::string& path, |
| 491 std::string* out_value) const { | 498 std::string* out_value) const { |
| 492 Value* value; | 499 const Value* value; |
| 493 if (!Get(path, &value)) | 500 if (!Get(path, &value)) |
| 494 return false; | 501 return false; |
| 495 | 502 |
| 496 return value->GetAsString(out_value); | 503 return value->GetAsString(out_value); |
| 497 } | 504 } |
| 498 | 505 |
| 499 bool DictionaryValue::GetString(const std::string& path, | 506 bool DictionaryValue::GetString(const std::string& path, |
| 500 string16* out_value) const { | 507 string16* out_value) const { |
| 501 Value* value; | 508 const Value* value; |
| 502 if (!Get(path, &value)) | 509 if (!Get(path, &value)) |
| 503 return false; | 510 return false; |
| 504 | 511 |
| 505 return value->GetAsString(out_value); | 512 return value->GetAsString(out_value); |
| 506 } | 513 } |
| 507 | 514 |
| 508 bool DictionaryValue::GetStringASCII(const std::string& path, | 515 bool DictionaryValue::GetStringASCII(const std::string& path, |
| 509 std::string* out_value) const { | 516 std::string* out_value) const { |
| 510 std::string out; | 517 std::string out; |
| 511 if (!GetString(path, &out)) | 518 if (!GetString(path, &out)) |
| 512 return false; | 519 return false; |
| 513 | 520 |
| 514 if (!IsStringASCII(out)) { | 521 if (!IsStringASCII(out)) { |
| 515 NOTREACHED(); | 522 NOTREACHED(); |
| 516 return false; | 523 return false; |
| 517 } | 524 } |
| 518 | 525 |
| 519 out_value->assign(out); | 526 out_value->assign(out); |
| 520 return true; | 527 return true; |
| 521 } | 528 } |
| 522 | 529 |
| 523 bool DictionaryValue::GetBinary(const std::string& path, | 530 bool DictionaryValue::GetBinary(const std::string& path, |
| 524 BinaryValue** out_value) const { | 531 const BinaryValue** out_value) const { |
| 525 Value* value; | 532 const Value* value; |
| 526 bool result = Get(path, &value); | 533 bool result = Get(path, &value); |
| 527 if (!result || !value->IsType(TYPE_BINARY)) | 534 if (!result || !value->IsType(TYPE_BINARY)) |
| 528 return false; | 535 return false; |
| 529 | 536 |
| 530 if (out_value) | 537 if (out_value) |
| 531 *out_value = static_cast<BinaryValue*>(value); | 538 *out_value = static_cast<const BinaryValue*>(value); |
| 539 |
| 540 return true; |
| 541 } |
| 542 |
| 543 bool DictionaryValue::GetBinary(const std::string& path, |
| 544 BinaryValue** out_value) { |
| 545 return static_cast<const DictionaryValue&>(*this).GetBinary( |
| 546 path, |
| 547 const_cast<const BinaryValue**>(out_value)); |
| 548 } |
| 549 |
| 550 bool DictionaryValue::GetDictionary(const std::string& path, |
| 551 const DictionaryValue** out_value) const { |
| 552 const Value* value; |
| 553 bool result = Get(path, &value); |
| 554 if (!result || !value->IsType(TYPE_DICTIONARY)) |
| 555 return false; |
| 556 |
| 557 if (out_value) |
| 558 *out_value = static_cast<const DictionaryValue*>(value); |
| 532 | 559 |
| 533 return true; | 560 return true; |
| 534 } | 561 } |
| 535 | 562 |
| 536 bool DictionaryValue::GetDictionary(const std::string& path, | 563 bool DictionaryValue::GetDictionary(const std::string& path, |
| 537 DictionaryValue** out_value) const { | 564 DictionaryValue** out_value) { |
| 538 Value* value; | 565 return static_cast<const DictionaryValue&>(*this).GetDictionary( |
| 539 bool result = Get(path, &value); | 566 path, |
| 540 if (!result || !value->IsType(TYPE_DICTIONARY)) | 567 const_cast<const DictionaryValue**>(out_value)); |
| 541 return false; | |
| 542 | |
| 543 if (out_value) | |
| 544 *out_value = static_cast<DictionaryValue*>(value); | |
| 545 | |
| 546 return true; | |
| 547 } | 568 } |
| 548 | 569 |
| 549 bool DictionaryValue::GetList(const std::string& path, | 570 bool DictionaryValue::GetList(const std::string& path, |
| 550 ListValue** out_value) const { | 571 const ListValue** out_value) const { |
| 551 Value* value; | 572 const Value* value; |
| 552 bool result = Get(path, &value); | 573 bool result = Get(path, &value); |
| 553 if (!result || !value->IsType(TYPE_LIST)) | 574 if (!result || !value->IsType(TYPE_LIST)) |
| 554 return false; | 575 return false; |
| 555 | 576 |
| 556 if (out_value) | 577 if (out_value) |
| 557 *out_value = static_cast<ListValue*>(value); | 578 *out_value = static_cast<const ListValue*>(value); |
| 558 | 579 |
| 559 return true; | 580 return true; |
| 560 } | 581 } |
| 561 | 582 |
| 583 bool DictionaryValue::GetList(const std::string& path, ListValue** out_value) { |
| 584 return static_cast<const DictionaryValue&>(*this).GetList( |
| 585 path, |
| 586 const_cast<const ListValue**>(out_value)); |
| 587 } |
| 588 |
| 562 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, | 589 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, |
| 563 Value** out_value) const { | 590 const Value** out_value) const { |
| 564 DCHECK(IsStringUTF8(key)); | 591 DCHECK(IsStringUTF8(key)); |
| 565 ValueMap::const_iterator entry_iterator = dictionary_.find(key); | 592 ValueMap::const_iterator entry_iterator = dictionary_.find(key); |
| 566 if (entry_iterator == dictionary_.end()) | 593 if (entry_iterator == dictionary_.end()) |
| 567 return false; | 594 return false; |
| 568 | 595 |
| 569 Value* entry = entry_iterator->second; | 596 const Value* entry = entry_iterator->second; |
| 570 if (out_value) | 597 if (out_value) |
| 571 *out_value = entry; | 598 *out_value = entry; |
| 572 return true; | 599 return true; |
| 573 } | 600 } |
| 574 | 601 |
| 602 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, |
| 603 Value** out_value) { |
| 604 return static_cast<const DictionaryValue&>(*this).GetWithoutPathExpansion( |
| 605 key, |
| 606 const_cast<const Value**>(out_value)); |
| 607 } |
| 608 |
| 575 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string& key, | 609 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string& key, |
| 576 int* out_value) const { | 610 int* out_value) const { |
| 577 Value* value; | 611 const Value* value; |
| 578 if (!GetWithoutPathExpansion(key, &value)) | 612 if (!GetWithoutPathExpansion(key, &value)) |
| 579 return false; | 613 return false; |
| 580 | 614 |
| 581 return value->GetAsInteger(out_value); | 615 return value->GetAsInteger(out_value); |
| 582 } | 616 } |
| 583 | 617 |
| 584 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string& key, | 618 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string& key, |
| 585 double* out_value) const { | 619 double* out_value) const { |
| 586 Value* value; | 620 const Value* value; |
| 587 if (!GetWithoutPathExpansion(key, &value)) | 621 if (!GetWithoutPathExpansion(key, &value)) |
| 588 return false; | 622 return false; |
| 589 | 623 |
| 590 return value->GetAsDouble(out_value); | 624 return value->GetAsDouble(out_value); |
| 591 } | 625 } |
| 592 | 626 |
| 593 bool DictionaryValue::GetStringWithoutPathExpansion( | 627 bool DictionaryValue::GetStringWithoutPathExpansion( |
| 594 const std::string& key, | 628 const std::string& key, |
| 595 std::string* out_value) const { | 629 std::string* out_value) const { |
| 596 Value* value; | 630 const Value* value; |
| 597 if (!GetWithoutPathExpansion(key, &value)) | 631 if (!GetWithoutPathExpansion(key, &value)) |
| 598 return false; | 632 return false; |
| 599 | 633 |
| 600 return value->GetAsString(out_value); | 634 return value->GetAsString(out_value); |
| 601 } | 635 } |
| 602 | 636 |
| 603 bool DictionaryValue::GetStringWithoutPathExpansion( | 637 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string& key, |
| 604 const std::string& key, | 638 string16* out_value) const { |
| 605 string16* out_value) const { | 639 const Value* value; |
| 606 Value* value; | |
| 607 if (!GetWithoutPathExpansion(key, &value)) | 640 if (!GetWithoutPathExpansion(key, &value)) |
| 608 return false; | 641 return false; |
| 609 | 642 |
| 610 return value->GetAsString(out_value); | 643 return value->GetAsString(out_value); |
| 611 } | 644 } |
| 612 | 645 |
| 613 bool DictionaryValue::GetDictionaryWithoutPathExpansion( | 646 bool DictionaryValue::GetDictionaryWithoutPathExpansion( |
| 614 const std::string& key, | 647 const std::string& key, |
| 615 DictionaryValue** out_value) const { | 648 const DictionaryValue** out_value) const { |
| 616 Value* value; | 649 const Value* value; |
| 617 bool result = GetWithoutPathExpansion(key, &value); | 650 bool result = GetWithoutPathExpansion(key, &value); |
| 618 if (!result || !value->IsType(TYPE_DICTIONARY)) | 651 if (!result || !value->IsType(TYPE_DICTIONARY)) |
| 619 return false; | 652 return false; |
| 620 | 653 |
| 621 if (out_value) | 654 if (out_value) |
| 622 *out_value = static_cast<DictionaryValue*>(value); | 655 *out_value = static_cast<const DictionaryValue*>(value); |
| 656 |
| 657 return true; |
| 658 } |
| 659 |
| 660 bool DictionaryValue::GetDictionaryWithoutPathExpansion( |
| 661 const std::string& key, |
| 662 DictionaryValue** out_value) { |
| 663 const DictionaryValue& const_this = |
| 664 static_cast<const DictionaryValue&>(*this); |
| 665 return const_this.GetDictionaryWithoutPathExpansion( |
| 666 key, |
| 667 const_cast<const DictionaryValue**>(out_value)); |
| 668 } |
| 669 |
| 670 bool DictionaryValue::GetListWithoutPathExpansion( |
| 671 const std::string& key, |
| 672 const ListValue** out_value) const { |
| 673 const Value* value; |
| 674 bool result = GetWithoutPathExpansion(key, &value); |
| 675 if (!result || !value->IsType(TYPE_LIST)) |
| 676 return false; |
| 677 |
| 678 if (out_value) |
| 679 *out_value = static_cast<const ListValue*>(value); |
| 623 | 680 |
| 624 return true; | 681 return true; |
| 625 } | 682 } |
| 626 | 683 |
| 627 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key, | 684 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key, |
| 628 ListValue** out_value) const { | 685 ListValue** out_value) { |
| 629 Value* value; | 686 return |
| 630 bool result = GetWithoutPathExpansion(key, &value); | 687 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion( |
| 631 if (!result || !value->IsType(TYPE_LIST)) | 688 key, |
| 632 return false; | 689 const_cast<const ListValue**>(out_value)); |
| 633 | |
| 634 if (out_value) | |
| 635 *out_value = static_cast<ListValue*>(value); | |
| 636 | |
| 637 return true; | |
| 638 } | 690 } |
| 639 | 691 |
| 640 bool DictionaryValue::Remove(const std::string& path, Value** out_value) { | 692 bool DictionaryValue::Remove(const std::string& path, Value** out_value) { |
| 641 DCHECK(IsStringUTF8(path)); | 693 DCHECK(IsStringUTF8(path)); |
| 642 std::string current_path(path); | 694 std::string current_path(path); |
| 643 DictionaryValue* current_dictionary = this; | 695 DictionaryValue* current_dictionary = this; |
| 644 size_t delimiter_position = current_path.rfind('.'); | 696 size_t delimiter_position = current_path.rfind('.'); |
| 645 if (delimiter_position != std::string::npos) { | 697 if (delimiter_position != std::string::npos) { |
| 646 if (!GetDictionary(current_path.substr(0, delimiter_position), | 698 if (!GetDictionary(current_path.substr(0, delimiter_position), |
| 647 ¤t_dictionary)) | 699 ¤t_dictionary)) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 670 } | 722 } |
| 671 | 723 |
| 672 DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() { | 724 DictionaryValue* DictionaryValue::DeepCopyWithoutEmptyChildren() { |
| 673 Value* copy = CopyWithoutEmptyChildren(this); | 725 Value* copy = CopyWithoutEmptyChildren(this); |
| 674 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue; | 726 return copy ? static_cast<DictionaryValue*>(copy) : new DictionaryValue; |
| 675 } | 727 } |
| 676 | 728 |
| 677 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { | 729 void DictionaryValue::MergeDictionary(const DictionaryValue* dictionary) { |
| 678 for (DictionaryValue::key_iterator key(dictionary->begin_keys()); | 730 for (DictionaryValue::key_iterator key(dictionary->begin_keys()); |
| 679 key != dictionary->end_keys(); ++key) { | 731 key != dictionary->end_keys(); ++key) { |
| 680 Value* merge_value; | 732 const Value* merge_value; |
| 681 if (dictionary->GetWithoutPathExpansion(*key, &merge_value)) { | 733 if (dictionary->GetWithoutPathExpansion(*key, &merge_value)) { |
| 682 // Check whether we have to merge dictionaries. | 734 // Check whether we have to merge dictionaries. |
| 683 if (merge_value->IsType(Value::TYPE_DICTIONARY)) { | 735 if (merge_value->IsType(Value::TYPE_DICTIONARY)) { |
| 684 DictionaryValue* sub_dict; | 736 DictionaryValue* sub_dict; |
| 685 if (GetDictionaryWithoutPathExpansion(*key, &sub_dict)) { | 737 if (GetDictionaryWithoutPathExpansion(*key, &sub_dict)) { |
| 686 sub_dict->MergeDictionary( | 738 sub_dict->MergeDictionary( |
| 687 static_cast<const DictionaryValue*>(merge_value)); | 739 static_cast<const DictionaryValue*>(merge_value)); |
| 688 continue; | 740 continue; |
| 689 } | 741 } |
| 690 } | 742 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 712 | 764 |
| 713 bool DictionaryValue::Equals(const Value* other) const { | 765 bool DictionaryValue::Equals(const Value* other) const { |
| 714 if (other->GetType() != GetType()) | 766 if (other->GetType() != GetType()) |
| 715 return false; | 767 return false; |
| 716 | 768 |
| 717 const DictionaryValue* other_dict = | 769 const DictionaryValue* other_dict = |
| 718 static_cast<const DictionaryValue*>(other); | 770 static_cast<const DictionaryValue*>(other); |
| 719 key_iterator lhs_it(begin_keys()); | 771 key_iterator lhs_it(begin_keys()); |
| 720 key_iterator rhs_it(other_dict->begin_keys()); | 772 key_iterator rhs_it(other_dict->begin_keys()); |
| 721 while (lhs_it != end_keys() && rhs_it != other_dict->end_keys()) { | 773 while (lhs_it != end_keys() && rhs_it != other_dict->end_keys()) { |
| 722 Value* lhs; | 774 const Value* lhs; |
| 723 Value* rhs; | 775 const Value* rhs; |
| 724 if (*lhs_it != *rhs_it || | 776 if (*lhs_it != *rhs_it || |
| 725 !GetWithoutPathExpansion(*lhs_it, &lhs) || | 777 !GetWithoutPathExpansion(*lhs_it, &lhs) || |
| 726 !other_dict->GetWithoutPathExpansion(*rhs_it, &rhs) || | 778 !other_dict->GetWithoutPathExpansion(*rhs_it, &rhs) || |
| 727 !lhs->Equals(rhs)) { | 779 !lhs->Equals(rhs)) { |
| 728 return false; | 780 return false; |
| 729 } | 781 } |
| 730 ++lhs_it; | 782 ++lhs_it; |
| 731 ++rhs_it; | 783 ++rhs_it; |
| 732 } | 784 } |
| 733 if (lhs_it != end_keys() || rhs_it != other_dict->end_keys()) | 785 if (lhs_it != end_keys() || rhs_it != other_dict->end_keys()) |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 962 if (lhs_it != end() || rhs_it != other_list->end()) | 1014 if (lhs_it != end() || rhs_it != other_list->end()) |
| 963 return false; | 1015 return false; |
| 964 | 1016 |
| 965 return true; | 1017 return true; |
| 966 } | 1018 } |
| 967 | 1019 |
| 968 ValueSerializer::~ValueSerializer() { | 1020 ValueSerializer::~ValueSerializer() { |
| 969 } | 1021 } |
| 970 | 1022 |
| 971 } // namespace base | 1023 } // namespace base |
| OLD | NEW |