OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/logging.h" | 7 #include "base/logging.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 | 10 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 Value* Value::CreateRealValue(double in_value) { | 83 Value* Value::CreateRealValue(double in_value) { |
84 return new FundamentalValue(in_value); | 84 return new FundamentalValue(in_value); |
85 } | 85 } |
86 | 86 |
87 // static | 87 // static |
88 Value* Value::CreateStringValue(const std::string& in_value) { | 88 Value* Value::CreateStringValue(const std::string& in_value) { |
89 return new StringValue(in_value); | 89 return new StringValue(in_value); |
90 } | 90 } |
91 | 91 |
92 // static | 92 // static |
| 93 Value* Value::CreateStringValue(const string16& in_value) { |
| 94 return new StringValue(in_value); |
| 95 } |
| 96 |
| 97 #if !defined(WCHAR_T_IS_UTF16) |
| 98 // TODO(viettrungluu): Deprecated and to be removed: |
| 99 // static |
93 Value* Value::CreateStringValue(const std::wstring& in_value) { | 100 Value* Value::CreateStringValue(const std::wstring& in_value) { |
94 return new StringValue(in_value); | 101 return new StringValue(in_value); |
95 } | 102 } |
96 | 103 #endif |
97 // static | |
98 Value* Value::CreateStringValueFromUTF16(const string16& in_value) { | |
99 return new StringValue(in_value); | |
100 } | |
101 | 104 |
102 // static | 105 // static |
103 BinaryValue* Value::CreateBinaryValue(char* buffer, size_t size) { | 106 BinaryValue* Value::CreateBinaryValue(char* buffer, size_t size) { |
104 return BinaryValue::Create(buffer, size); | 107 return BinaryValue::Create(buffer, size); |
105 } | 108 } |
106 | 109 |
107 bool Value::GetAsBoolean(bool* in_value) const { | 110 bool Value::GetAsBoolean(bool* out_value) const { |
108 return false; | 111 return false; |
109 } | 112 } |
110 | 113 |
111 bool Value::GetAsInteger(int* in_value) const { | 114 bool Value::GetAsInteger(int* out_value) const { |
112 return false; | 115 return false; |
113 } | 116 } |
114 | 117 |
115 bool Value::GetAsReal(double* in_value) const { | 118 bool Value::GetAsReal(double* out_value) const { |
116 return false; | 119 return false; |
117 } | 120 } |
118 | 121 |
119 bool Value::GetAsString(std::string* in_value) const { | 122 bool Value::GetAsString(std::string* out_value) const { |
120 return false; | 123 return false; |
121 } | 124 } |
122 | 125 |
123 bool Value::GetAsString(std::wstring* in_value) const { | 126 bool Value::GetAsString(string16* out_value) const { |
124 return false; | 127 return false; |
125 } | 128 } |
126 | 129 |
127 bool Value::GetAsUTF16(string16* out_value) const { | 130 #if !defined(WCHAR_T_IS_UTF16) |
| 131 // TODO(viettrungluu): Deprecated and to be removed: |
| 132 bool Value::GetAsString(std::wstring* out_value) const { |
128 return false; | 133 return false; |
129 } | 134 } |
| 135 #endif |
130 | 136 |
131 Value* Value::DeepCopy() const { | 137 Value* Value::DeepCopy() const { |
132 // This method should only be getting called for null Values--all subclasses | 138 // This method should only be getting called for null Values--all subclasses |
133 // need to provide their own implementation;. | 139 // need to provide their own implementation;. |
134 DCHECK(IsType(TYPE_NULL)); | 140 DCHECK(IsType(TYPE_NULL)); |
135 return CreateNullValue(); | 141 return CreateNullValue(); |
136 } | 142 } |
137 | 143 |
138 bool Value::Equals(const Value* other) const { | 144 bool Value::Equals(const Value* other) const { |
139 // This method should only be getting called for null Values--all subclasses | 145 // This method should only be getting called for null Values--all subclasses |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 } | 227 } |
222 | 228 |
223 ///////////////////// StringValue //////////////////// | 229 ///////////////////// StringValue //////////////////// |
224 | 230 |
225 StringValue::StringValue(const std::string& in_value) | 231 StringValue::StringValue(const std::string& in_value) |
226 : Value(TYPE_STRING), | 232 : Value(TYPE_STRING), |
227 value_(in_value) { | 233 value_(in_value) { |
228 DCHECK(IsStringUTF8(in_value)); | 234 DCHECK(IsStringUTF8(in_value)); |
229 } | 235 } |
230 | 236 |
| 237 StringValue::StringValue(const string16& in_value) |
| 238 : Value(TYPE_STRING), |
| 239 value_(UTF16ToUTF8(in_value)) { |
| 240 } |
| 241 |
| 242 #if !defined(WCHAR_T_IS_UTF16) |
| 243 // TODO(viettrungluu): Deprecated and to be removed: |
231 StringValue::StringValue(const std::wstring& in_value) | 244 StringValue::StringValue(const std::wstring& in_value) |
232 : Value(TYPE_STRING), | 245 : Value(TYPE_STRING), |
233 value_(WideToUTF8(in_value)) { | 246 value_(WideToUTF8(in_value)) { |
234 } | 247 } |
235 | |
236 #if !defined(WCHAR_T_IS_UTF16) | |
237 StringValue::StringValue(const string16& in_value) | |
238 : Value(TYPE_STRING), | |
239 value_(UTF16ToUTF8(in_value)) { | |
240 } | |
241 #endif | 248 #endif |
242 | 249 |
243 StringValue::~StringValue() { | 250 StringValue::~StringValue() { |
244 } | 251 } |
245 | 252 |
246 bool StringValue::GetAsString(std::string* out_value) const { | 253 bool StringValue::GetAsString(std::string* out_value) const { |
247 if (out_value) | 254 if (out_value) |
248 *out_value = value_; | 255 *out_value = value_; |
249 return true; | 256 return true; |
250 } | 257 } |
251 | 258 |
| 259 bool StringValue::GetAsString(string16* out_value) const { |
| 260 if (out_value) |
| 261 *out_value = UTF8ToUTF16(value_); |
| 262 return true; |
| 263 } |
| 264 |
| 265 #if !defined(WCHAR_T_IS_UTF16) |
| 266 // TODO(viettrungluu): Deprecated and to be removed: |
252 bool StringValue::GetAsString(std::wstring* out_value) const { | 267 bool StringValue::GetAsString(std::wstring* out_value) const { |
253 if (out_value) | 268 if (out_value) |
254 *out_value = UTF8ToWide(value_); | 269 *out_value = UTF8ToWide(value_); |
255 return true; | 270 return true; |
256 } | 271 } |
257 | 272 #endif |
258 bool StringValue::GetAsUTF16(string16* out_value) const { | |
259 if (out_value) | |
260 *out_value = UTF8ToUTF16(value_); | |
261 return true; | |
262 } | |
263 | 273 |
264 Value* StringValue::DeepCopy() const { | 274 Value* StringValue::DeepCopy() const { |
265 return CreateStringValue(value_); | 275 return CreateStringValue(value_); |
266 } | 276 } |
267 | 277 |
268 bool StringValue::Equals(const Value* other) const { | 278 bool StringValue::Equals(const Value* other) const { |
269 if (other->GetType() != GetType()) | 279 if (other->GetType() != GetType()) |
270 return false; | 280 return false; |
271 std::string lhs, rhs; | 281 std::string lhs, rhs; |
272 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; | 282 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 Set(path, CreateRealValue(in_value)); | 443 Set(path, CreateRealValue(in_value)); |
434 } | 444 } |
435 | 445 |
436 void DictionaryValue::SetString(const std::string& path, | 446 void DictionaryValue::SetString(const std::string& path, |
437 const std::string& in_value) { | 447 const std::string& in_value) { |
438 Set(path, CreateStringValue(in_value)); | 448 Set(path, CreateStringValue(in_value)); |
439 } | 449 } |
440 | 450 |
441 void DictionaryValue::SetStringFromUTF16(const std::string& path, | 451 void DictionaryValue::SetStringFromUTF16(const std::string& path, |
442 const string16& in_value) { | 452 const string16& in_value) { |
443 Set(path, CreateStringValueFromUTF16(in_value)); | 453 Set(path, CreateStringValue(in_value)); |
444 } | 454 } |
445 | 455 |
446 // TODO(viettrungluu): Deprecated and to be removed: | 456 // TODO(viettrungluu): Deprecated and to be removed: |
447 void DictionaryValue::SetBoolean(const std::wstring& path, bool in_value) { | 457 void DictionaryValue::SetBoolean(const std::wstring& path, bool in_value) { |
448 Set(path, CreateBooleanValue(in_value)); | 458 Set(path, CreateBooleanValue(in_value)); |
449 } | 459 } |
450 | 460 |
451 // TODO(viettrungluu): Deprecated and to be removed: | 461 // TODO(viettrungluu): Deprecated and to be removed: |
452 void DictionaryValue::SetInteger(const std::wstring& path, int in_value) { | 462 void DictionaryValue::SetInteger(const std::wstring& path, int in_value) { |
453 Set(path, CreateIntegerValue(in_value)); | 463 Set(path, CreateIntegerValue(in_value)); |
(...skipping 12 matching lines...) Expand all Loading... |
466 | 476 |
467 // TODO(viettrungluu): Deprecated and to be removed: | 477 // TODO(viettrungluu): Deprecated and to be removed: |
468 void DictionaryValue::SetString(const std::wstring& path, | 478 void DictionaryValue::SetString(const std::wstring& path, |
469 const std::wstring& in_value) { | 479 const std::wstring& in_value) { |
470 Set(path, CreateStringValue(in_value)); | 480 Set(path, CreateStringValue(in_value)); |
471 } | 481 } |
472 | 482 |
473 // TODO(viettrungluu): Deprecated and to be removed: | 483 // TODO(viettrungluu): Deprecated and to be removed: |
474 void DictionaryValue::SetStringFromUTF16(const std::wstring& path, | 484 void DictionaryValue::SetStringFromUTF16(const std::wstring& path, |
475 const string16& in_value) { | 485 const string16& in_value) { |
476 Set(path, CreateStringValueFromUTF16(in_value)); | 486 Set(path, CreateStringValue(in_value)); |
477 } | 487 } |
478 | 488 |
479 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, | 489 void DictionaryValue::SetWithoutPathExpansion(const std::string& key, |
480 Value* in_value) { | 490 Value* in_value) { |
481 // If there's an existing value here, we need to delete it, because | 491 // If there's an existing value here, we need to delete it, because |
482 // we own all our children. | 492 // we own all our children. |
483 if (HasKey(key)) { | 493 if (HasKey(key)) { |
484 DCHECK(dictionary_[key] != in_value); // This would be bogus | 494 DCHECK(dictionary_[key] != in_value); // This would be bogus |
485 delete dictionary_[key]; | 495 delete dictionary_[key]; |
486 } | 496 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 | 562 |
553 return value->GetAsString(out_value); | 563 return value->GetAsString(out_value); |
554 } | 564 } |
555 | 565 |
556 bool DictionaryValue::GetStringAsUTF16(const std::string& path, | 566 bool DictionaryValue::GetStringAsUTF16(const std::string& path, |
557 string16* out_value) const { | 567 string16* out_value) const { |
558 Value* value; | 568 Value* value; |
559 if (!Get(path, &value)) | 569 if (!Get(path, &value)) |
560 return false; | 570 return false; |
561 | 571 |
562 return value->GetAsUTF16(out_value); | 572 return value->GetAsString(out_value); |
563 } | 573 } |
564 | 574 |
565 bool DictionaryValue::GetStringASCII(const std::string& path, | 575 bool DictionaryValue::GetStringASCII(const std::string& path, |
566 std::string* out_value) const { | 576 std::string* out_value) const { |
567 std::string out; | 577 std::string out; |
568 if (!GetString(path, &out)) | 578 if (!GetString(path, &out)) |
569 return false; | 579 return false; |
570 | 580 |
571 if (!IsStringASCII(out)) { | 581 if (!IsStringASCII(out)) { |
572 NOTREACHED(); | 582 NOTREACHED(); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
711 return value->GetAsString(out_value); | 721 return value->GetAsString(out_value); |
712 } | 722 } |
713 | 723 |
714 bool DictionaryValue::GetStringAsUTF16WithoutPathExpansion( | 724 bool DictionaryValue::GetStringAsUTF16WithoutPathExpansion( |
715 const std::string& key, | 725 const std::string& key, |
716 string16* out_value) const { | 726 string16* out_value) const { |
717 Value* value; | 727 Value* value; |
718 if (!GetWithoutPathExpansion(key, &value)) | 728 if (!GetWithoutPathExpansion(key, &value)) |
719 return false; | 729 return false; |
720 | 730 |
721 return value->GetAsUTF16(out_value); | 731 return value->GetAsString(out_value); |
722 } | 732 } |
723 | 733 |
724 bool DictionaryValue::GetDictionaryWithoutPathExpansion( | 734 bool DictionaryValue::GetDictionaryWithoutPathExpansion( |
725 const std::string& key, | 735 const std::string& key, |
726 DictionaryValue** out_value) const { | 736 DictionaryValue** out_value) const { |
727 Value* value; | 737 Value* value; |
728 bool result = GetWithoutPathExpansion(key, &value); | 738 bool result = GetWithoutPathExpansion(key, &value); |
729 if (!result || !value->IsType(TYPE_DICTIONARY)) | 739 if (!result || !value->IsType(TYPE_DICTIONARY)) |
730 return false; | 740 return false; |
731 | 741 |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
944 return false; | 954 return false; |
945 | 955 |
946 return value->GetAsString(out_value); | 956 return value->GetAsString(out_value); |
947 } | 957 } |
948 | 958 |
949 bool ListValue::GetStringAsUTF16(size_t index, string16* out_value) const { | 959 bool ListValue::GetStringAsUTF16(size_t index, string16* out_value) const { |
950 Value* value; | 960 Value* value; |
951 if (!Get(index, &value)) | 961 if (!Get(index, &value)) |
952 return false; | 962 return false; |
953 | 963 |
954 return value->GetAsUTF16(out_value); | 964 return value->GetAsString(out_value); |
955 } | 965 } |
956 | 966 |
957 bool ListValue::GetBinary(size_t index, BinaryValue** out_value) const { | 967 bool ListValue::GetBinary(size_t index, BinaryValue** out_value) const { |
958 Value* value; | 968 Value* value; |
959 bool result = Get(index, &value); | 969 bool result = Get(index, &value); |
960 if (!result || !value->IsType(TYPE_BINARY)) | 970 if (!result || !value->IsType(TYPE_BINARY)) |
961 return false; | 971 return false; |
962 | 972 |
963 if (out_value) | 973 if (out_value) |
964 *out_value = static_cast<BinaryValue*>(value); | 974 *out_value = static_cast<BinaryValue*>(value); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1067 return false; | 1077 return false; |
1068 } | 1078 } |
1069 if (lhs_it != end() || rhs_it != other_list->end()) | 1079 if (lhs_it != end() || rhs_it != other_list->end()) |
1070 return false; | 1080 return false; |
1071 | 1081 |
1072 return true; | 1082 return true; |
1073 } | 1083 } |
1074 | 1084 |
1075 ValueSerializer::~ValueSerializer() { | 1085 ValueSerializer::~ValueSerializer() { |
1076 } | 1086 } |
OLD | NEW |