| 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 <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cmath> | 10 #include <cmath> |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs; | 244 return GetAsDouble(&lhs) && other->GetAsDouble(&rhs) && lhs == rhs; |
| 245 } | 245 } |
| 246 default: | 246 default: |
| 247 NOTREACHED(); | 247 NOTREACHED(); |
| 248 return false; | 248 return false; |
| 249 } | 249 } |
| 250 } | 250 } |
| 251 | 251 |
| 252 ///////////////////// StringValue //////////////////// | 252 ///////////////////// StringValue //////////////////// |
| 253 | 253 |
| 254 StringValue::StringValue(const std::string& in_value) | 254 StringValue::StringValue(StringPiece in_value) |
| 255 : Value(TYPE_STRING), | 255 : Value(TYPE_STRING), value_(in_value.as_string()) { |
| 256 value_(in_value) { | |
| 257 DCHECK(IsStringUTF8(in_value)); | 256 DCHECK(IsStringUTF8(in_value)); |
| 258 } | 257 } |
| 259 | 258 |
| 260 StringValue::StringValue(const string16& in_value) | 259 StringValue::StringValue(const string16& in_value) |
| 261 : Value(TYPE_STRING), | 260 : Value(TYPE_STRING), |
| 262 value_(UTF16ToUTF8(in_value)) { | 261 value_(UTF16ToUTF8(in_value)) { |
| 263 } | 262 } |
| 264 | 263 |
| 265 StringValue::~StringValue() { | 264 StringValue::~StringValue() { |
| 266 } | 265 } |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 *out_value = this; | 368 *out_value = this; |
| 370 return true; | 369 return true; |
| 371 } | 370 } |
| 372 | 371 |
| 373 bool DictionaryValue::GetAsDictionary(const DictionaryValue** out_value) const { | 372 bool DictionaryValue::GetAsDictionary(const DictionaryValue** out_value) const { |
| 374 if (out_value) | 373 if (out_value) |
| 375 *out_value = this; | 374 *out_value = this; |
| 376 return true; | 375 return true; |
| 377 } | 376 } |
| 378 | 377 |
| 379 bool DictionaryValue::HasKey(const std::string& key) const { | 378 bool DictionaryValue::HasKey(StringPiece key) const { |
| 380 DCHECK(IsStringUTF8(key)); | 379 DCHECK(IsStringUTF8(key)); |
| 381 auto current_entry = dictionary_.find(key); | 380 auto current_entry = dictionary_.find(key.as_string()); |
| 382 DCHECK((current_entry == dictionary_.end()) || current_entry->second); | 381 DCHECK((current_entry == dictionary_.end()) || current_entry->second); |
| 383 return current_entry != dictionary_.end(); | 382 return current_entry != dictionary_.end(); |
| 384 } | 383 } |
| 385 | 384 |
| 386 void DictionaryValue::Clear() { | 385 void DictionaryValue::Clear() { |
| 387 dictionary_.clear(); | 386 dictionary_.clear(); |
| 388 } | 387 } |
| 389 | 388 |
| 390 void DictionaryValue::Set(const std::string& path, | 389 void DictionaryValue::Set(StringPiece path, std::unique_ptr<Value> in_value) { |
| 391 std::unique_ptr<Value> in_value) { | |
| 392 DCHECK(IsStringUTF8(path)); | 390 DCHECK(IsStringUTF8(path)); |
| 393 DCHECK(in_value); | 391 DCHECK(in_value); |
| 394 | 392 |
| 395 std::string current_path(path); | 393 std::string current_path(path.as_string()); |
| 396 DictionaryValue* current_dictionary = this; | 394 DictionaryValue* current_dictionary = this; |
| 397 for (size_t delimiter_position = current_path.find('.'); | 395 for (size_t delimiter_position = current_path.find('.'); |
| 398 delimiter_position != std::string::npos; | 396 delimiter_position != std::string::npos; |
| 399 delimiter_position = current_path.find('.')) { | 397 delimiter_position = current_path.find('.')) { |
| 400 // Assume that we're indexing into a dictionary. | 398 // Assume that we're indexing into a dictionary. |
| 401 std::string key(current_path, 0, delimiter_position); | 399 std::string key(current_path, 0, delimiter_position); |
| 402 DictionaryValue* child_dictionary = NULL; | 400 DictionaryValue* child_dictionary = NULL; |
| 403 if (!current_dictionary->GetDictionary(key, &child_dictionary)) { | 401 if (!current_dictionary->GetDictionary(key, &child_dictionary)) { |
| 404 child_dictionary = new DictionaryValue; | 402 child_dictionary = new DictionaryValue; |
| 405 current_dictionary->SetWithoutPathExpansion(key, child_dictionary); | 403 current_dictionary->SetWithoutPathExpansion(key, child_dictionary); |
| 406 } | 404 } |
| 407 | 405 |
| 408 current_dictionary = child_dictionary; | 406 current_dictionary = child_dictionary; |
| 409 current_path.erase(0, delimiter_position + 1); | 407 current_path.erase(0, delimiter_position + 1); |
| 410 } | 408 } |
| 411 | 409 |
| 412 current_dictionary->SetWithoutPathExpansion(current_path, | 410 current_dictionary->SetWithoutPathExpansion(current_path, |
| 413 std::move(in_value)); | 411 std::move(in_value)); |
| 414 } | 412 } |
| 415 | 413 |
| 416 void DictionaryValue::Set(const std::string& path, Value* in_value) { | 414 void DictionaryValue::Set(StringPiece path, Value* in_value) { |
| 417 Set(path, WrapUnique(in_value)); | 415 Set(path, WrapUnique(in_value)); |
| 418 } | 416 } |
| 419 | 417 |
| 420 void DictionaryValue::SetBoolean(const std::string& path, bool in_value) { | 418 void DictionaryValue::SetBoolean(StringPiece path, bool in_value) { |
| 421 Set(path, new FundamentalValue(in_value)); | 419 Set(path, new FundamentalValue(in_value)); |
| 422 } | 420 } |
| 423 | 421 |
| 424 void DictionaryValue::SetInteger(const std::string& path, int in_value) { | 422 void DictionaryValue::SetInteger(StringPiece path, int in_value) { |
| 425 Set(path, new FundamentalValue(in_value)); | 423 Set(path, new FundamentalValue(in_value)); |
| 426 } | 424 } |
| 427 | 425 |
| 428 void DictionaryValue::SetDouble(const std::string& path, double in_value) { | 426 void DictionaryValue::SetDouble(StringPiece path, double in_value) { |
| 429 Set(path, new FundamentalValue(in_value)); | 427 Set(path, new FundamentalValue(in_value)); |
| 430 } | 428 } |
| 431 | 429 |
| 432 void DictionaryValue::SetString(const std::string& path, | 430 void DictionaryValue::SetString(StringPiece path, StringPiece in_value) { |
| 433 const std::string& in_value) { | 431 Set(path, new StringValue(in_value.as_string())); |
| 432 } |
| 433 |
| 434 void DictionaryValue::SetString(StringPiece path, const string16& in_value) { |
| 434 Set(path, new StringValue(in_value)); | 435 Set(path, new StringValue(in_value)); |
| 435 } | 436 } |
| 436 | 437 |
| 437 void DictionaryValue::SetString(const std::string& path, | 438 void DictionaryValue::SetWithoutPathExpansion(StringPiece key, |
| 438 const string16& in_value) { | 439 std::unique_ptr<Value> in_value) { |
| 439 Set(path, new StringValue(in_value)); | 440 dictionary_[key.as_string()] = std::move(in_value); |
| 440 } | 441 } |
| 441 | 442 |
| 442 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, | 443 void DictionaryValue::SetWithoutPathExpansion(StringPiece key, |
| 443 std::unique_ptr<Value> in_value) { | |
| 444 dictionary_[key] = std::move(in_value); | |
| 445 } | |
| 446 | |
| 447 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, | |
| 448 Value* in_value) { | 444 Value* in_value) { |
| 449 SetWithoutPathExpansion(key, WrapUnique(in_value)); | 445 SetWithoutPathExpansion(key, WrapUnique(in_value)); |
| 450 } | 446 } |
| 451 | 447 |
| 452 void DictionaryValue::SetBooleanWithoutPathExpansion( | 448 void DictionaryValue::SetBooleanWithoutPathExpansion(StringPiece path, |
| 453 const std::string& path, bool in_value) { | 449 bool in_value) { |
| 454 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); | 450 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); |
| 455 } | 451 } |
| 456 | 452 |
| 457 void DictionaryValue::SetIntegerWithoutPathExpansion( | 453 void DictionaryValue::SetIntegerWithoutPathExpansion(StringPiece path, |
| 458 const std::string& path, int in_value) { | 454 int in_value) { |
| 459 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); | 455 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); |
| 460 } | 456 } |
| 461 | 457 |
| 462 void DictionaryValue::SetDoubleWithoutPathExpansion( | 458 void DictionaryValue::SetDoubleWithoutPathExpansion(StringPiece path, |
| 463 const std::string& path, double in_value) { | 459 double in_value) { |
| 464 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); | 460 SetWithoutPathExpansion(path, new FundamentalValue(in_value)); |
| 465 } | 461 } |
| 466 | 462 |
| 467 void DictionaryValue::SetStringWithoutPathExpansion( | 463 void DictionaryValue::SetStringWithoutPathExpansion(StringPiece path, |
| 468 const std::string& path, const std::string& in_value) { | 464 StringPiece in_value) { |
| 465 SetWithoutPathExpansion(path, new StringValue(in_value.as_string())); |
| 466 } |
| 467 |
| 468 void DictionaryValue::SetStringWithoutPathExpansion(StringPiece path, |
| 469 const string16& in_value) { |
| 469 SetWithoutPathExpansion(path, new StringValue(in_value)); | 470 SetWithoutPathExpansion(path, new StringValue(in_value)); |
| 470 } | 471 } |
| 471 | 472 |
| 472 void DictionaryValue::SetStringWithoutPathExpansion( | |
| 473 const std::string& path, const string16& in_value) { | |
| 474 SetWithoutPathExpansion(path, new StringValue(in_value)); | |
| 475 } | |
| 476 | |
| 477 bool DictionaryValue::Get(StringPiece path, | 473 bool DictionaryValue::Get(StringPiece path, |
| 478 const Value** out_value) const { | 474 const Value** out_value) const { |
| 479 DCHECK(IsStringUTF8(path)); | 475 DCHECK(IsStringUTF8(path)); |
| 480 StringPiece current_path(path); | 476 StringPiece current_path(path); |
| 481 const DictionaryValue* current_dictionary = this; | 477 const DictionaryValue* current_dictionary = this; |
| 482 for (size_t delimiter_position = current_path.find('.'); | 478 for (size_t delimiter_position = current_path.find('.'); |
| 483 delimiter_position != std::string::npos; | 479 delimiter_position != std::string::npos; |
| 484 delimiter_position = current_path.find('.')) { | 480 delimiter_position = current_path.find('.')) { |
| 485 const DictionaryValue* child_dictionary = NULL; | 481 const DictionaryValue* child_dictionary = NULL; |
| 486 if (!current_dictionary->GetDictionaryWithoutPathExpansion( | 482 if (!current_dictionary->GetDictionaryWithoutPathExpansion( |
| 487 current_path.substr(0, delimiter_position).as_string(), | 483 current_path.substr(0, delimiter_position).as_string(), |
| 488 &child_dictionary)) { | 484 &child_dictionary)) { |
| 489 return false; | 485 return false; |
| 490 } | 486 } |
| 491 | 487 |
| 492 current_dictionary = child_dictionary; | 488 current_dictionary = child_dictionary; |
| 493 current_path = current_path.substr(delimiter_position + 1); | 489 current_path = current_path.substr(delimiter_position + 1); |
| 494 } | 490 } |
| 495 | 491 |
| 496 return current_dictionary->GetWithoutPathExpansion(current_path.as_string(), | 492 return current_dictionary->GetWithoutPathExpansion(current_path.as_string(), |
| 497 out_value); | 493 out_value); |
| 498 } | 494 } |
| 499 | 495 |
| 500 bool DictionaryValue::Get(StringPiece path, Value** out_value) { | 496 bool DictionaryValue::Get(StringPiece path, Value** out_value) { |
| 501 return static_cast<const DictionaryValue&>(*this).Get( | 497 return static_cast<const DictionaryValue&>(*this).Get( |
| 502 path, | 498 path, |
| 503 const_cast<const Value**>(out_value)); | 499 const_cast<const Value**>(out_value)); |
| 504 } | 500 } |
| 505 | 501 |
| 506 bool DictionaryValue::GetBoolean(const std::string& path, | 502 bool DictionaryValue::GetBoolean(StringPiece path, bool* bool_value) const { |
| 507 bool* bool_value) const { | |
| 508 const Value* value; | 503 const Value* value; |
| 509 if (!Get(path, &value)) | 504 if (!Get(path, &value)) |
| 510 return false; | 505 return false; |
| 511 | 506 |
| 512 return value->GetAsBoolean(bool_value); | 507 return value->GetAsBoolean(bool_value); |
| 513 } | 508 } |
| 514 | 509 |
| 515 bool DictionaryValue::GetInteger(const std::string& path, | 510 bool DictionaryValue::GetInteger(StringPiece path, int* out_value) const { |
| 516 int* out_value) const { | |
| 517 const Value* value; | 511 const Value* value; |
| 518 if (!Get(path, &value)) | 512 if (!Get(path, &value)) |
| 519 return false; | 513 return false; |
| 520 | 514 |
| 521 return value->GetAsInteger(out_value); | 515 return value->GetAsInteger(out_value); |
| 522 } | 516 } |
| 523 | 517 |
| 524 bool DictionaryValue::GetDouble(const std::string& path, | 518 bool DictionaryValue::GetDouble(StringPiece path, double* out_value) const { |
| 525 double* out_value) const { | |
| 526 const Value* value; | 519 const Value* value; |
| 527 if (!Get(path, &value)) | 520 if (!Get(path, &value)) |
| 528 return false; | 521 return false; |
| 529 | 522 |
| 530 return value->GetAsDouble(out_value); | 523 return value->GetAsDouble(out_value); |
| 531 } | 524 } |
| 532 | 525 |
| 533 bool DictionaryValue::GetString(const std::string& path, | 526 bool DictionaryValue::GetString(StringPiece path, |
| 534 std::string* out_value) const { | 527 std::string* out_value) const { |
| 535 const Value* value; | 528 const Value* value; |
| 536 if (!Get(path, &value)) | 529 if (!Get(path, &value)) |
| 537 return false; | 530 return false; |
| 538 | 531 |
| 539 return value->GetAsString(out_value); | 532 return value->GetAsString(out_value); |
| 540 } | 533 } |
| 541 | 534 |
| 542 bool DictionaryValue::GetString(const std::string& path, | 535 bool DictionaryValue::GetString(StringPiece path, string16* out_value) const { |
| 543 string16* out_value) const { | |
| 544 const Value* value; | 536 const Value* value; |
| 545 if (!Get(path, &value)) | 537 if (!Get(path, &value)) |
| 546 return false; | 538 return false; |
| 547 | 539 |
| 548 return value->GetAsString(out_value); | 540 return value->GetAsString(out_value); |
| 549 } | 541 } |
| 550 | 542 |
| 551 bool DictionaryValue::GetStringASCII(const std::string& path, | 543 bool DictionaryValue::GetStringASCII(StringPiece path, |
| 552 std::string* out_value) const { | 544 std::string* out_value) const { |
| 553 std::string out; | 545 std::string out; |
| 554 if (!GetString(path, &out)) | 546 if (!GetString(path, &out)) |
| 555 return false; | 547 return false; |
| 556 | 548 |
| 557 if (!IsStringASCII(out)) { | 549 if (!IsStringASCII(out)) { |
| 558 NOTREACHED(); | 550 NOTREACHED(); |
| 559 return false; | 551 return false; |
| 560 } | 552 } |
| 561 | 553 |
| 562 out_value->assign(out); | 554 out_value->assign(out); |
| 563 return true; | 555 return true; |
| 564 } | 556 } |
| 565 | 557 |
| 566 bool DictionaryValue::GetBinary(const std::string& path, | 558 bool DictionaryValue::GetBinary(StringPiece path, |
| 567 const BinaryValue** out_value) const { | 559 const BinaryValue** out_value) const { |
| 568 const Value* value; | 560 const Value* value; |
| 569 bool result = Get(path, &value); | 561 bool result = Get(path, &value); |
| 570 if (!result || !value->IsType(TYPE_BINARY)) | 562 if (!result || !value->IsType(TYPE_BINARY)) |
| 571 return false; | 563 return false; |
| 572 | 564 |
| 573 if (out_value) | 565 if (out_value) |
| 574 *out_value = static_cast<const BinaryValue*>(value); | 566 *out_value = static_cast<const BinaryValue*>(value); |
| 575 | 567 |
| 576 return true; | 568 return true; |
| 577 } | 569 } |
| 578 | 570 |
| 579 bool DictionaryValue::GetBinary(const std::string& path, | 571 bool DictionaryValue::GetBinary(StringPiece path, BinaryValue** out_value) { |
| 580 BinaryValue** out_value) { | |
| 581 return static_cast<const DictionaryValue&>(*this).GetBinary( | 572 return static_cast<const DictionaryValue&>(*this).GetBinary( |
| 582 path, | 573 path, |
| 583 const_cast<const BinaryValue**>(out_value)); | 574 const_cast<const BinaryValue**>(out_value)); |
| 584 } | 575 } |
| 585 | 576 |
| 586 bool DictionaryValue::GetDictionary(StringPiece path, | 577 bool DictionaryValue::GetDictionary(StringPiece path, |
| 587 const DictionaryValue** out_value) const { | 578 const DictionaryValue** out_value) const { |
| 588 const Value* value; | 579 const Value* value; |
| 589 bool result = Get(path, &value); | 580 bool result = Get(path, &value); |
| 590 if (!result || !value->IsType(TYPE_DICTIONARY)) | 581 if (!result || !value->IsType(TYPE_DICTIONARY)) |
| 591 return false; | 582 return false; |
| 592 | 583 |
| 593 if (out_value) | 584 if (out_value) |
| 594 *out_value = static_cast<const DictionaryValue*>(value); | 585 *out_value = static_cast<const DictionaryValue*>(value); |
| 595 | 586 |
| 596 return true; | 587 return true; |
| 597 } | 588 } |
| 598 | 589 |
| 599 bool DictionaryValue::GetDictionary(StringPiece path, | 590 bool DictionaryValue::GetDictionary(StringPiece path, |
| 600 DictionaryValue** out_value) { | 591 DictionaryValue** out_value) { |
| 601 return static_cast<const DictionaryValue&>(*this).GetDictionary( | 592 return static_cast<const DictionaryValue&>(*this).GetDictionary( |
| 602 path, | 593 path, |
| 603 const_cast<const DictionaryValue**>(out_value)); | 594 const_cast<const DictionaryValue**>(out_value)); |
| 604 } | 595 } |
| 605 | 596 |
| 606 bool DictionaryValue::GetList(const std::string& path, | 597 bool DictionaryValue::GetList(StringPiece path, |
| 607 const ListValue** out_value) const { | 598 const ListValue** out_value) const { |
| 608 const Value* value; | 599 const Value* value; |
| 609 bool result = Get(path, &value); | 600 bool result = Get(path, &value); |
| 610 if (!result || !value->IsType(TYPE_LIST)) | 601 if (!result || !value->IsType(TYPE_LIST)) |
| 611 return false; | 602 return false; |
| 612 | 603 |
| 613 if (out_value) | 604 if (out_value) |
| 614 *out_value = static_cast<const ListValue*>(value); | 605 *out_value = static_cast<const ListValue*>(value); |
| 615 | 606 |
| 616 return true; | 607 return true; |
| 617 } | 608 } |
| 618 | 609 |
| 619 bool DictionaryValue::GetList(const std::string& path, ListValue** out_value) { | 610 bool DictionaryValue::GetList(StringPiece path, ListValue** out_value) { |
| 620 return static_cast<const DictionaryValue&>(*this).GetList( | 611 return static_cast<const DictionaryValue&>(*this).GetList( |
| 621 path, | 612 path, |
| 622 const_cast<const ListValue**>(out_value)); | 613 const_cast<const ListValue**>(out_value)); |
| 623 } | 614 } |
| 624 | 615 |
| 625 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, | 616 bool DictionaryValue::GetWithoutPathExpansion(StringPiece key, |
| 626 const Value** out_value) const { | 617 const Value** out_value) const { |
| 627 DCHECK(IsStringUTF8(key)); | 618 DCHECK(IsStringUTF8(key)); |
| 628 auto entry_iterator = dictionary_.find(key); | 619 auto entry_iterator = dictionary_.find(key.as_string()); |
| 629 if (entry_iterator == dictionary_.end()) | 620 if (entry_iterator == dictionary_.end()) |
| 630 return false; | 621 return false; |
| 631 | 622 |
| 632 if (out_value) | 623 if (out_value) |
| 633 *out_value = entry_iterator->second.get(); | 624 *out_value = entry_iterator->second.get(); |
| 634 return true; | 625 return true; |
| 635 } | 626 } |
| 636 | 627 |
| 637 bool DictionaryValue::GetWithoutPathExpansion(const std::string& key, | 628 bool DictionaryValue::GetWithoutPathExpansion(StringPiece key, |
| 638 Value** out_value) { | 629 Value** out_value) { |
| 639 return static_cast<const DictionaryValue&>(*this).GetWithoutPathExpansion( | 630 return static_cast<const DictionaryValue&>(*this).GetWithoutPathExpansion( |
| 640 key, | 631 key, |
| 641 const_cast<const Value**>(out_value)); | 632 const_cast<const Value**>(out_value)); |
| 642 } | 633 } |
| 643 | 634 |
| 644 bool DictionaryValue::GetBooleanWithoutPathExpansion(const std::string& key, | 635 bool DictionaryValue::GetBooleanWithoutPathExpansion(StringPiece key, |
| 645 bool* out_value) const { | 636 bool* out_value) const { |
| 646 const Value* value; | 637 const Value* value; |
| 647 if (!GetWithoutPathExpansion(key, &value)) | 638 if (!GetWithoutPathExpansion(key, &value)) |
| 648 return false; | 639 return false; |
| 649 | 640 |
| 650 return value->GetAsBoolean(out_value); | 641 return value->GetAsBoolean(out_value); |
| 651 } | 642 } |
| 652 | 643 |
| 653 bool DictionaryValue::GetIntegerWithoutPathExpansion(const std::string& key, | 644 bool DictionaryValue::GetIntegerWithoutPathExpansion(StringPiece key, |
| 654 int* out_value) const { | 645 int* out_value) const { |
| 655 const Value* value; | 646 const Value* value; |
| 656 if (!GetWithoutPathExpansion(key, &value)) | 647 if (!GetWithoutPathExpansion(key, &value)) |
| 657 return false; | 648 return false; |
| 658 | 649 |
| 659 return value->GetAsInteger(out_value); | 650 return value->GetAsInteger(out_value); |
| 660 } | 651 } |
| 661 | 652 |
| 662 bool DictionaryValue::GetDoubleWithoutPathExpansion(const std::string& key, | 653 bool DictionaryValue::GetDoubleWithoutPathExpansion(StringPiece key, |
| 663 double* out_value) const { | 654 double* out_value) const { |
| 664 const Value* value; | 655 const Value* value; |
| 665 if (!GetWithoutPathExpansion(key, &value)) | 656 if (!GetWithoutPathExpansion(key, &value)) |
| 666 return false; | 657 return false; |
| 667 | 658 |
| 668 return value->GetAsDouble(out_value); | 659 return value->GetAsDouble(out_value); |
| 669 } | 660 } |
| 670 | 661 |
| 671 bool DictionaryValue::GetStringWithoutPathExpansion( | 662 bool DictionaryValue::GetStringWithoutPathExpansion( |
| 672 const std::string& key, | 663 StringPiece key, |
| 673 std::string* out_value) const { | 664 std::string* out_value) const { |
| 674 const Value* value; | 665 const Value* value; |
| 675 if (!GetWithoutPathExpansion(key, &value)) | 666 if (!GetWithoutPathExpansion(key, &value)) |
| 676 return false; | 667 return false; |
| 677 | 668 |
| 678 return value->GetAsString(out_value); | 669 return value->GetAsString(out_value); |
| 679 } | 670 } |
| 680 | 671 |
| 681 bool DictionaryValue::GetStringWithoutPathExpansion(const std::string& key, | 672 bool DictionaryValue::GetStringWithoutPathExpansion(StringPiece key, |
| 682 string16* out_value) const { | 673 string16* out_value) const { |
| 683 const Value* value; | 674 const Value* value; |
| 684 if (!GetWithoutPathExpansion(key, &value)) | 675 if (!GetWithoutPathExpansion(key, &value)) |
| 685 return false; | 676 return false; |
| 686 | 677 |
| 687 return value->GetAsString(out_value); | 678 return value->GetAsString(out_value); |
| 688 } | 679 } |
| 689 | 680 |
| 690 bool DictionaryValue::GetDictionaryWithoutPathExpansion( | 681 bool DictionaryValue::GetDictionaryWithoutPathExpansion( |
| 691 const std::string& key, | 682 StringPiece key, |
| 692 const DictionaryValue** out_value) const { | 683 const DictionaryValue** out_value) const { |
| 693 const Value* value; | 684 const Value* value; |
| 694 bool result = GetWithoutPathExpansion(key, &value); | 685 bool result = GetWithoutPathExpansion(key, &value); |
| 695 if (!result || !value->IsType(TYPE_DICTIONARY)) | 686 if (!result || !value->IsType(TYPE_DICTIONARY)) |
| 696 return false; | 687 return false; |
| 697 | 688 |
| 698 if (out_value) | 689 if (out_value) |
| 699 *out_value = static_cast<const DictionaryValue*>(value); | 690 *out_value = static_cast<const DictionaryValue*>(value); |
| 700 | 691 |
| 701 return true; | 692 return true; |
| 702 } | 693 } |
| 703 | 694 |
| 704 bool DictionaryValue::GetDictionaryWithoutPathExpansion( | 695 bool DictionaryValue::GetDictionaryWithoutPathExpansion( |
| 705 const std::string& key, | 696 StringPiece key, |
| 706 DictionaryValue** out_value) { | 697 DictionaryValue** out_value) { |
| 707 const DictionaryValue& const_this = | 698 const DictionaryValue& const_this = |
| 708 static_cast<const DictionaryValue&>(*this); | 699 static_cast<const DictionaryValue&>(*this); |
| 709 return const_this.GetDictionaryWithoutPathExpansion( | 700 return const_this.GetDictionaryWithoutPathExpansion( |
| 710 key, | 701 key, |
| 711 const_cast<const DictionaryValue**>(out_value)); | 702 const_cast<const DictionaryValue**>(out_value)); |
| 712 } | 703 } |
| 713 | 704 |
| 714 bool DictionaryValue::GetListWithoutPathExpansion( | 705 bool DictionaryValue::GetListWithoutPathExpansion( |
| 715 const std::string& key, | 706 StringPiece key, |
| 716 const ListValue** out_value) const { | 707 const ListValue** out_value) const { |
| 717 const Value* value; | 708 const Value* value; |
| 718 bool result = GetWithoutPathExpansion(key, &value); | 709 bool result = GetWithoutPathExpansion(key, &value); |
| 719 if (!result || !value->IsType(TYPE_LIST)) | 710 if (!result || !value->IsType(TYPE_LIST)) |
| 720 return false; | 711 return false; |
| 721 | 712 |
| 722 if (out_value) | 713 if (out_value) |
| 723 *out_value = static_cast<const ListValue*>(value); | 714 *out_value = static_cast<const ListValue*>(value); |
| 724 | 715 |
| 725 return true; | 716 return true; |
| 726 } | 717 } |
| 727 | 718 |
| 728 bool DictionaryValue::GetListWithoutPathExpansion(const std::string& key, | 719 bool DictionaryValue::GetListWithoutPathExpansion(StringPiece key, |
| 729 ListValue** out_value) { | 720 ListValue** out_value) { |
| 730 return | 721 return |
| 731 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion( | 722 static_cast<const DictionaryValue&>(*this).GetListWithoutPathExpansion( |
| 732 key, | 723 key, |
| 733 const_cast<const ListValue**>(out_value)); | 724 const_cast<const ListValue**>(out_value)); |
| 734 } | 725 } |
| 735 | 726 |
| 736 bool DictionaryValue::Remove(const std::string& path, | 727 bool DictionaryValue::Remove(StringPiece path, |
| 737 std::unique_ptr<Value>* out_value) { | 728 std::unique_ptr<Value>* out_value) { |
| 738 DCHECK(IsStringUTF8(path)); | 729 DCHECK(IsStringUTF8(path)); |
| 739 std::string current_path(path); | 730 std::string current_path(path.as_string()); |
| 740 DictionaryValue* current_dictionary = this; | 731 DictionaryValue* current_dictionary = this; |
| 741 size_t delimiter_position = current_path.rfind('.'); | 732 size_t delimiter_position = current_path.rfind('.'); |
| 742 if (delimiter_position != std::string::npos) { | 733 if (delimiter_position != std::string::npos) { |
| 743 if (!GetDictionary(current_path.substr(0, delimiter_position), | 734 if (!GetDictionary(current_path.substr(0, delimiter_position), |
| 744 ¤t_dictionary)) | 735 ¤t_dictionary)) |
| 745 return false; | 736 return false; |
| 746 current_path.erase(0, delimiter_position + 1); | 737 current_path.erase(0, delimiter_position + 1); |
| 747 } | 738 } |
| 748 | 739 |
| 749 return current_dictionary->RemoveWithoutPathExpansion(current_path, | 740 return current_dictionary->RemoveWithoutPathExpansion(current_path, |
| 750 out_value); | 741 out_value); |
| 751 } | 742 } |
| 752 | 743 |
| 753 bool DictionaryValue::RemoveWithoutPathExpansion( | 744 bool DictionaryValue::RemoveWithoutPathExpansion( |
| 754 const std::string& key, | 745 StringPiece key, |
| 755 std::unique_ptr<Value>* out_value) { | 746 std::unique_ptr<Value>* out_value) { |
| 756 DCHECK(IsStringUTF8(key)); | 747 DCHECK(IsStringUTF8(key)); |
| 757 auto entry_iterator = dictionary_.find(key); | 748 auto entry_iterator = dictionary_.find(key.as_string()); |
| 758 if (entry_iterator == dictionary_.end()) | 749 if (entry_iterator == dictionary_.end()) |
| 759 return false; | 750 return false; |
| 760 | 751 |
| 761 if (out_value) | 752 if (out_value) |
| 762 *out_value = std::move(entry_iterator->second); | 753 *out_value = std::move(entry_iterator->second); |
| 763 dictionary_.erase(entry_iterator); | 754 dictionary_.erase(entry_iterator); |
| 764 return true; | 755 return true; |
| 765 } | 756 } |
| 766 | 757 |
| 767 bool DictionaryValue::RemovePath(const std::string& path, | 758 bool DictionaryValue::RemovePath(StringPiece path, |
| 768 std::unique_ptr<Value>* out_value) { | 759 std::unique_ptr<Value>* out_value) { |
| 769 bool result = false; | 760 bool result = false; |
| 770 size_t delimiter_position = path.find('.'); | 761 size_t delimiter_position = path.find('.'); |
| 771 | 762 |
| 772 if (delimiter_position == std::string::npos) | 763 if (delimiter_position == std::string::npos) |
| 773 return RemoveWithoutPathExpansion(path, out_value); | 764 return RemoveWithoutPathExpansion(path, out_value); |
| 774 | 765 |
| 775 const std::string subdict_path = path.substr(0, delimiter_position); | 766 StringPiece subdict_path = path.substr(0, delimiter_position); |
| 776 DictionaryValue* subdict = NULL; | 767 DictionaryValue* subdict = NULL; |
| 777 if (!GetDictionary(subdict_path, &subdict)) | 768 if (!GetDictionary(subdict_path, &subdict)) |
| 778 return false; | 769 return false; |
| 779 result = subdict->RemovePath(path.substr(delimiter_position + 1), | 770 result = subdict->RemovePath(path.substr(delimiter_position + 1), |
| 780 out_value); | 771 out_value); |
| 781 if (result && subdict->empty()) | 772 if (result && subdict->empty()) |
| 782 RemoveWithoutPathExpansion(subdict_path, NULL); | 773 RemoveWithoutPathExpansion(subdict_path, NULL); |
| 783 | 774 |
| 784 return result; | 775 return result; |
| 785 } | 776 } |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1061 } | 1052 } |
| 1062 | 1053 |
| 1063 void ListValue::AppendInteger(int in_value) { | 1054 void ListValue::AppendInteger(int in_value) { |
| 1064 Append(new FundamentalValue(in_value)); | 1055 Append(new FundamentalValue(in_value)); |
| 1065 } | 1056 } |
| 1066 | 1057 |
| 1067 void ListValue::AppendDouble(double in_value) { | 1058 void ListValue::AppendDouble(double in_value) { |
| 1068 Append(new FundamentalValue(in_value)); | 1059 Append(new FundamentalValue(in_value)); |
| 1069 } | 1060 } |
| 1070 | 1061 |
| 1071 void ListValue::AppendString(const std::string& in_value) { | 1062 void ListValue::AppendString(StringPiece in_value) { |
| 1072 Append(new StringValue(in_value)); | 1063 Append(new StringValue(in_value.as_string())); |
| 1073 } | 1064 } |
| 1074 | 1065 |
| 1075 void ListValue::AppendString(const string16& in_value) { | 1066 void ListValue::AppendString(const string16& in_value) { |
| 1076 Append(new StringValue(in_value)); | 1067 Append(new StringValue(in_value)); |
| 1077 } | 1068 } |
| 1078 | 1069 |
| 1079 void ListValue::AppendStrings(const std::vector<std::string>& in_values) { | 1070 void ListValue::AppendStrings(const std::vector<std::string>& in_values) { |
| 1080 for (std::vector<std::string>::const_iterator it = in_values.begin(); | 1071 for (std::vector<std::string>::const_iterator it = in_values.begin(); |
| 1081 it != in_values.end(); ++it) { | 1072 it != in_values.end(); ++it) { |
| 1082 AppendString(*it); | 1073 AppendString(*it); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1172 ValueDeserializer::~ValueDeserializer() { | 1163 ValueDeserializer::~ValueDeserializer() { |
| 1173 } | 1164 } |
| 1174 | 1165 |
| 1175 std::ostream& operator<<(std::ostream& out, const Value& value) { | 1166 std::ostream& operator<<(std::ostream& out, const Value& value) { |
| 1176 std::string json; | 1167 std::string json; |
| 1177 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); | 1168 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
| 1178 return out << json; | 1169 return out << json; |
| 1179 } | 1170 } |
| 1180 | 1171 |
| 1181 } // namespace base | 1172 } // namespace base |
| OLD | NEW |