| 6 |
| 7 | #include <string.h> |
| 8 |
| 9 | #include <algorithm> |
| 10 | #include <cmath> |
| 11 | #include <new> |
| 12 | #include <ostream> |
| 13 | #include <utility> |
| 14 |
| 15 | #include "base/bit_cast.h" |
| 16 | #include "base/containers/checked_iterators.h" |
| 17 | #include "base/json/json_writer.h" |
| 18 | #include "base/logging.h" |
| 19 | #include "base/memory/ptr_util.h" |
| 20 | #include "base/stl_util.h" |
| 21 | #include "base/strings/string_util.h" |
| 22 | #include "base/strings/utf_string_conversions.h" |
| 23 | #include "base/trace_event/memory_usage_estimator.h" |
| 24 |
| 25 | namespace base { |
| 26 |
| 27 | // base::Value must be standard layout to guarantee that writing to |
| 28 | // |bool_type_| then reading |type_| is defined behaviour. See: |
| 29 | // |
| 30 | // [class.union]: |
| 31 | // If a standard-layout union contains several standard-layout structs that |
| 32 | // share a common initial sequence (9.2), and if an object of this |
| 33 | // standard-layout union type contains one of the standard-layout structs, |
| 34 | // it is permitted to inspect the common initial sequence of any of |
| 35 | // standard-layout struct members; |
| 36 | // |
| 37 | static_assert(std::is_standard_layout<Value>::value, |
| 38 | "base::Value should be a standard-layout C++ class in order " |
| 39 | "to avoid undefined behaviour in its implementation!"); |
| 40 |
| 41 | static_assert(sizeof(Value::DoubleStorage) == sizeof(double), |
| 42 | "The double and DoubleStorage types should have the same size"); |
| 43 |
| 44 | namespace { |
| 45 |
| 46 | const char* const kTypeNames[] = {"null", "boolean", "integer", "double", |
| 47 | "string", "binary", "dictionary", "list"}; |
| 48 | static_assert(base::size(kTypeNames) == |
| 49 | static_cast<size_t>(Value::Type::LIST) + 1, |
| 50 | "kTypeNames Has Wrong Size"); |
| 51 |
| 52 | std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node); |
| 53 |
| 54 | // Make a deep copy of |node|, but don't include empty lists or dictionaries |
| 55 | // in the copy. It's possible for this function to return NULL and it |
| 56 | // expects |node| to always be non-NULL. |
| 57 | std::unique_ptr<Value> CopyListWithoutEmptyChildren(const Value& list) { |
| 58 | Value copy(Value::Type::LIST); |
| 59 | for (const auto& entry : list.GetList()) { |
| 60 | std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(entry); |
| 61 | if (child_copy) |
| 62 | copy.Append(std::move(*child_copy)); |
| 63 | } |
| 64 | return copy.GetList().empty() ? nullptr |
| 65 | : std::make_unique<Value>(std::move(copy)); |
| 66 | } |
| 67 |
| 68 | std::unique_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren( |
| 69 | const DictionaryValue& dict) { |
| 70 | std::unique_ptr<DictionaryValue> copy; |
| 71 | for (const auto& it : dict.DictItems()) { |
| 72 | std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(it.second); |
| 73 | if (child_copy) { |
| 74 | if (!copy) |
| 75 | copy = std::make_unique<DictionaryValue>(); |
| 76 | copy->SetKey(it.first, std::move(*child_copy)); |
| 77 | } |
| 78 | } |
| 79 | return copy; |
| 80 | } |
| 81 |
| 82 | std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node) { |
| 83 | switch (node.type()) { |
| 84 | case Value::Type::LIST: |
| 85 | return CopyListWithoutEmptyChildren(static_cast<const ListValue&>(node)); |
| 86 |
| 87 | case Value::Type::DICTIONARY: |
| 88 | return CopyDictionaryWithoutEmptyChildren( |
| 89 | static_cast<const DictionaryValue&>(node)); |
| 90 |
| 91 | default: |
| 92 | return std::make_unique<Value>(node.Clone()); |
| 93 | } |
| 94 | } |
| 95 |
| 96 | // Helper class to enumerate the path components from a StringPiece |
| 97 | // without performing heap allocations. Components are simply separated |
| 98 | // by single dots (e.g. "foo.bar.baz" -> ["foo", "bar", "baz"]). |
| 99 | // |
| 100 | // Usage example: |
| 101 | // PathSplitter splitter(some_path); |
| 102 | // while (splitter.HasNext()) { |
| 103 | // StringPiece component = splitter.Next(); |
| 104 | // ... |
| 105 | // } |
| 106 | // |
| 107 | class PathSplitter { |
| 108 | public: |
| 109 | explicit PathSplitter(StringPiece path) : path_(path) {} |
| 110 |
| 111 | bool HasNext() const { return pos_ < path_.size(); } |
| 112 |
| 113 | StringPiece Next() { |
| 114 | DCHECK(HasNext()); |
| 115 | size_t start = pos_; |
| 116 | size_t pos = path_.find('.', start); |
| 117 | size_t end; |
| 118 | if (pos == path_.npos) { |
| 119 | end = path_.size(); |
| 120 | pos_ = end; |
| 121 | } else { |
| 122 | end = pos; |
| 123 | pos_ = pos + 1; |
| 124 | } |
| 125 | return path_.substr(start, end - start); |
| 126 | } |
| 127 |
| 128 | private: |
| 129 | StringPiece path_; |
| 130 | size_t pos_ = 0; |
| 131 | }; |
| 132 |
| 133 | } // namespace |
| 134 |
| 135 | // static |
| 136 | std::unique_ptr<Value> Value::CreateWithCopiedBuffer(const char* buffer, |
| 137 | size_t size) { |
| 138 | return std::make_unique<Value>(BlobStorage(buffer, buffer + size)); |
| 139 | } |
| 140 |
| 141 | // static |
| 142 | Value Value::FromUniquePtrValue(std::unique_ptr<Value> val) { |
| 143 | return std::move(*val); |
| 144 | } |
| 145 |
| 146 | // static |
| 147 | std::unique_ptr<Value> Value::ToUniquePtrValue(Value val) { |
| 148 | return std::make_unique<Value>(std::move(val)); |
| 149 | } |
| 150 |
| 151 | // static |
| 152 | const DictionaryValue& Value::AsDictionaryValue(const Value& val) { |
| 153 | CHECK(val.is_dict()); |
| 154 | return static_cast<const DictionaryValue&>(val); |
| 155 | } |
| 156 |
| 157 | // static |
| 158 | const ListValue& Value::AsListValue(const Value& val) { |
| 159 | CHECK(val.is_list()); |
| 160 | return static_cast<const ListValue&>(val); |
| 161 | } |
| 162 |
| 163 | Value::Value(Value&& that) noexcept { |
| 164 | InternalMoveConstructFrom(std::move(that)); |
| 165 | } |
| 166 |
| 167 | Value::Value(Type type) : type_(type) { |
| 168 | // Initialize with the default value. |
| 169 | switch (type_) { |
| 170 | case Type::NONE: |
| 171 | return; |
| 172 |
| 173 | case Type::BOOLEAN: |
| 174 | bool_value_ = false; |
| 175 | return; |
| 176 | case Type::INTEGER: |
| 177 | int_value_ = 0; |
| 178 | return; |
| 179 | case Type::DOUBLE: |
| 180 | double_value_ = bit_cast<DoubleStorage>(0.0); |
| 181 | return; |
| 182 | case Type::STRING: |
| 183 | new (&string_value_) std::string(); |
| 184 | return; |
| 185 | case Type::BINARY: |
| 186 | new (&binary_value_) BlobStorage(); |
| 187 | return; |
| 188 | case Type::DICTIONARY: |
| 189 | new (&dict_) DictStorage(); |
| 190 | return; |
| 191 | case Type::LIST: |
| 192 | new (&list_) ListStorage(); |
| 193 | return; |
| 194 | // TODO(crbug.com/859477): Remove after root cause is found. |
| 195 | case Type::DEAD: |
| 196 | CHECK(false); |
| 197 | return; |
| 198 | } |
| 199 |
| 200 | // TODO(crbug.com/859477): Revert to NOTREACHED() after root cause is found. |
| 201 | CHECK(false); |
| 202 | } |
| 203 |
| 204 | Value::Value(bool in_bool) : type_(Type::BOOLEAN), bool_value_(in_bool) {} |
| 205 |
| 206 | Value::Value(int in_int) : type_(Type::INTEGER), int_value_(in_int) {} |
| 207 |
| 208 | Value::Value(double in_double) |
| 209 | : type_(Type::DOUBLE), double_value_(bit_cast<DoubleStorage>(in_double)) { |
| 210 | if (!std::isfinite(in_double)) { |
| 211 | NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) " |
| 212 | << "values cannot be represented in JSON"; |
| 213 | double_value_ = bit_cast<DoubleStorage>(0.0); |
| 214 | } |
| 215 | } |
| 216 |
| 217 | Value::Value(const char* in_string) : Value(std::string(in_string)) {} |
| 218 |
| 219 | Value::Value(StringPiece in_string) : Value(std::string(in_string)) {} |
| 220 |
| 221 | Value::Value(std::string&& in_string) noexcept |
| 222 | : type_(Type::STRING), string_value_(std::move(in_string)) { |
| 223 | DCHECK(IsStringUTF8AllowingNoncharacters(string_value_)); |
| 224 | } |
| 225 |
| 226 | Value::Value(const char16* in_string16) : Value(StringPiece16(in_string16)) {} |
| 227 |
| 228 | Value::Value(StringPiece16 in_string16) : Value(UTF16ToUTF8(in_string16)) {} |
| 229 |
| 230 | Value::Value(const std::vector<char>& in_blob) |
| 231 | : type_(Type::BINARY), binary_value_(in_blob.begin(), in_blob.end()) {} |
| 232 |
| 233 | Value::Value(base::span<const uint8_t> in_blob) |
| 234 | : type_(Type::BINARY), binary_value_(in_blob.begin(), in_blob.end()) {} |
| 235 |
| 236 | Value::Value(BlobStorage&& in_blob) noexcept |
| 237 | : type_(Type::BINARY), binary_value_(std::move(in_blob)) {} |
| 238 |
| 239 | Value::Value(const DictStorage& in_dict) : type_(Type::DICTIONARY), dict_() { |
| 240 | dict_.reserve(in_dict.size()); |
| 241 | for (const auto& it : in_dict) { |
| 242 | dict_.try_emplace(dict_.end(), it.first, |
| 243 | std::make_unique<Value>(it.second->Clone())); |
| 244 | } |
| 245 | } |
| 246 |
| 247 | Value::Value(DictStorage&& in_dict) noexcept |
| 248 | : type_(Type::DICTIONARY), dict_(std::move(in_dict)) {} |
| 249 |
| 250 | Value::Value(span<const Value> in_list) : type_(Type::LIST), list_() { |
| 251 | list_.reserve(in_list.size()); |
| 252 | for (const auto& val : in_list) |
| 253 | list_.emplace_back(val.Clone()); |
| 254 | } |
| 255 |
| 256 | Value::Value(ListStorage&& in_list) noexcept |
| 257 | : type_(Type::LIST), list_(std::move(in_list)) {} |
| 258 |
| 259 | Value& Value::operator=(Value&& that) noexcept { |
| 260 | InternalCleanup(); |
| 261 | InternalMoveConstructFrom(std::move(that)); |
| 262 |
| 263 | return *this; |
| 264 | } |
| 265 |
| 266 | double Value::AsDoubleInternal() const { |
| 267 | return bit_cast<double>(double_value_); |
| 268 | } |
| 269 |
| 270 | Value Value::Clone() const { |
| 271 | switch (type_) { |
| 272 | case Type::NONE: |
| 273 | return Value(); |
| 274 | case Type::BOOLEAN: |
| 275 | return Value(bool_value_); |
| 276 | case Type::INTEGER: |
| 277 | return Value(int_value_); |
| 278 | case Type::DOUBLE: |
| 279 | return Value(AsDoubleInternal()); |
| 280 | case Type::STRING: |
| 281 | return Value(string_value_); |
| 282 | case Type::BINARY: |
| 283 | return Value(binary_value_); |
| 284 | case Type::DICTIONARY: |
| 285 | return Value(dict_); |
| 286 | case Type::LIST: |
| 287 | return Value(list_); |
| 288 | // TODO(crbug.com/859477): Remove after root cause is found. |
| 289 | case Type::DEAD: |
| 290 | CHECK(false); |
| 291 | return Value(); |
| 292 | } |
| 293 |
| 294 | // TODO(crbug.com/859477): Revert to NOTREACHED() after root cause is found. |
| 295 | CHECK(false); |
| 296 | return Value(); |
| 297 | } |
| 298 |
| 299 | Value::~Value() { |
| 300 | InternalCleanup(); |
| 301 | // TODO(crbug.com/859477): Remove after root cause is found. |
| 302 | type_ = Type::DEAD; |
| 303 | } |
| 304 |
| 305 | // static |
| 306 | const char* Value::GetTypeName(Value::Type type) { |
| 307 | DCHECK_GE(static_cast<int>(type), 0); |
| 308 | DCHECK_LT(static_cast<size_t>(type), base::size(kTypeNames)); |
| 309 | return kTypeNames[static_cast<size_t>(type)]; |
| 310 | } |
| 311 |
| 312 | bool Value::GetBool() const { |
| 313 | CHECK(is_bool()); |
| 314 | return bool_value_; |
| 315 | } |
| 316 |
| 317 | int Value::GetInt() const { |
| 318 | CHECK(is_int()); |
| 319 | return int_value_; |
| 320 | } |
| 321 |
| 322 | double Value::GetDouble() const { |
| 323 | if (is_double()) |
| 324 | return AsDoubleInternal(); |
| 325 | if (is_int()) |
| 326 | return int_value_; |
| 327 | CHECK(false); |
| 328 | return 0.0; |
| 329 | } |
| 330 |
| 331 | const std::string& Value::GetString() const { |
| 332 | CHECK(is_string()); |
| 333 | return string_value_; |
| 334 | } |
| 335 |
| 336 | std::string& Value::GetString() { |
| 337 | CHECK(is_string()); |
| 338 | return string_value_; |
| 339 | } |
| 340 |
| 341 | const Value::BlobStorage& Value::GetBlob() const { |
| 342 | CHECK(is_blob()); |
| 343 | return binary_value_; |
| 344 | } |
| 345 |
| 346 | Value::ListView Value::GetList() { |
| 347 | CHECK(is_list()); |
| 348 | return list_; |
| 349 | } |
| 350 |
| 351 | Value::ConstListView Value::GetList() const { |
| 352 | CHECK(is_list()); |
| 353 | return list_; |
| 354 | } |
| 355 |
| 356 | Value::ListStorage Value::TakeList() { |
| 357 | CHECK(is_list()); |
| 358 | return std::exchange(list_, ListStorage()); |
| 359 | } |
| 360 |
| 361 | void Value::Append(bool value) { |
| 362 | CHECK(is_list()); |
| 363 | list_.emplace_back(value); |
| 364 | } |
| 365 |
| 366 | void Value::Append(int value) { |
| 367 | CHECK(is_list()); |
| 368 | list_.emplace_back(value); |
| 369 | } |
| 370 |
| 371 | void Value::Append(double value) { |
| 372 | CHECK(is_list()); |
| 373 | list_.emplace_back(value); |
| 374 | } |
| 375 |
| 376 | void Value::Append(const char* value) { |
| 377 | CHECK(is_list()); |
| 378 | list_.emplace_back(value); |
| 379 | } |
| 380 |
| 381 | void Value::Append(StringPiece value) { |
| 382 | CHECK(is_list()); |
| 383 | list_.emplace_back(value); |
| 384 | } |
| 385 |
| 386 | void Value::Append(std::string&& value) { |
| 387 | CHECK(is_list()); |
| 388 | list_.emplace_back(std::move(value)); |
| 389 | } |
| 390 |
| 391 | void Value::Append(const char16* value) { |
| 392 | CHECK(is_list()); |
| 393 | list_.emplace_back(value); |
| 394 | } |
| 395 |
| 396 | void Value::Append(StringPiece16 value) { |
| 397 | CHECK(is_list()); |
| 398 | list_.emplace_back(value); |
| 399 | } |
| 400 |
| 401 | void Value::Append(Value&& value) { |
| 402 | CHECK(is_list()); |
| 403 | list_.emplace_back(std::move(value)); |
| 404 | } |
| 405 |
| 406 | CheckedContiguousIterator<Value> Value::Insert( |
| 407 | CheckedContiguousConstIterator<Value> pos, |
| 408 | Value&& value) { |
| 409 | CHECK(is_list()); |
| 410 | const auto offset = pos - make_span(list_).begin(); |
| 411 | list_.insert(list_.begin() + offset, std::move(value)); |
| 412 | return make_span(list_).begin() + offset; |
| 413 | } |
| 414 |
| 415 | bool Value::EraseListIter(CheckedContiguousConstIterator<Value> iter) { |
| 416 | CHECK(is_list()); |
| 417 | const auto offset = iter - ListView(list_).begin(); |
| 418 | auto list_iter = list_.begin() + offset; |
| 419 | if (list_iter == list_.end()) |
| 420 | return false; |
| 421 |
| 422 | list_.erase(list_iter); |
| 423 | return true; |
| 424 | } |
| 425 |
| 426 | size_t Value::EraseListValue(const Value& val) { |
| 427 | return EraseListValueIf([&val](const Value& other) { return val == other; }); |
| 428 | } |
| 429 |
| 430 | void Value::ClearList() { |
| 431 | CHECK(is_list()); |
| 432 | list_.clear(); |
| 433 | } |
| 434 |
| 435 | Value* Value::FindKey(StringPiece key) { |
| 436 | return const_cast<Value*>(as_const(*this).FindKey(key)); |
| 437 | } |
| 438 |
| 439 | const Value* Value::FindKey(StringPiece key) const { |
| 440 | CHECK(is_dict()); |
| 441 | auto found = dict_.find(key); |
| 442 | if (found == dict_.end()) |
| 443 | return nullptr; |
| 444 | return found->second.get(); |
| 445 | } |
| 446 |
| 447 | Value* Value::FindKeyOfType(StringPiece key, Type type) { |
| 448 | return const_cast<Value*>(as_const(*this).FindKeyOfType(key, type)); |
| 449 | } |
| 450 |
| 451 | const Value* Value::FindKeyOfType(StringPiece key, Type type) const { |
| 452 | const Value* result = FindKey(key); |
| 453 | if (!result || result->type() != type) |
| 454 | return nullptr; |
| 455 | return result; |
| 456 | } |
| 457 |
| 458 | base::Optional<bool> Value::FindBoolKey(StringPiece key) const { |
| 459 | const Value* result = FindKeyOfType(key, Type::BOOLEAN); |
| 460 | return result ? base::make_optional(result->bool_value_) : base::nullopt; |
| 461 | } |
| 462 |
| 463 | base::Optional<int> Value::FindIntKey(StringPiece key) const { |
| 464 | const Value* result = FindKeyOfType(key, Type::INTEGER); |
| 465 | return result ? base::make_optional(result->int_value_) : base::nullopt; |
| 466 | } |
| 467 |
| 468 | base::Optional<double> Value::FindDoubleKey(StringPiece key) const { |
| 469 | const Value* result = FindKey(key); |
| 470 | if (result) { |
| 471 | if (result->is_int()) |
| 472 | return static_cast<double>(result->int_value_); |
| 473 | if (result->is_double()) { |
| 474 | return result->AsDoubleInternal(); |
| 475 | } |
| 476 | } |
| 477 | return base::nullopt; |
| 478 | } |
| 479 |
| 480 | const std::string* Value::FindStringKey(StringPiece key) const { |
| 481 | const Value* result = FindKeyOfType(key, Type::STRING); |
| 482 | return result ? &result->string_value_ : nullptr; |
| 483 | } |
| 484 |
| 485 | std::string* Value::FindStringKey(StringPiece key) { |
| 486 | Value* result = FindKeyOfType(key, Type::STRING); |
| 487 | return result ? &result->string_value_ : nullptr; |
| 488 | } |
| 489 |
| 490 | const Value::BlobStorage* Value::FindBlobKey(StringPiece key) const { |
| 491 | const Value* value = FindKeyOfType(key, Type::BINARY); |
| 492 | return value ? &value->binary_value_ : nullptr; |
| 493 | } |
| 494 |
| 495 | const Value* Value::FindDictKey(StringPiece key) const { |
| 496 | return FindKeyOfType(key, Type::DICTIONARY); |
| 497 | } |
| 498 |
| 499 | Value* Value::FindDictKey(StringPiece key) { |
| 500 | return FindKeyOfType(key, Type::DICTIONARY); |
| 501 | } |
| 502 |
| 503 | const Value* Value::FindListKey(StringPiece key) const { |
| 504 | return FindKeyOfType(key, Type::LIST); |
| 505 | } |
| 506 |
| 507 | Value* Value::FindListKey(StringPiece key) { |
| 508 | return FindKeyOfType(key, Type::LIST); |
| 509 | } |
| 510 |
| 511 | Value* Value::SetKey(StringPiece key, Value&& value) { |
| 512 | return SetKeyInternal(key, std::make_unique<Value>(std::move(value))); |
| 513 | } |
| 514 |
| 515 | Value* Value::SetKey(std::string&& key, Value&& value) { |
| 516 | CHECK(is_dict()); |
| 517 | return dict_ |
| 518 | .insert_or_assign(std::move(key), |
| 519 | std::make_unique<Value>(std::move(value))) |
| 520 | .first->second.get(); |
| 521 | } |
| 522 |
| 523 | Value* Value::SetKey(const char* key, Value&& value) { |
| 524 | return SetKeyInternal(key, std::make_unique<Value>(std::move(value))); |
| 525 | } |
| 526 |
| 527 | Value* Value::SetBoolKey(StringPiece key, bool value) { |
| 528 | return SetKeyInternal(key, std::make_unique<Value>(value)); |
| 529 | } |
| 530 |
| 531 | Value* Value::SetIntKey(StringPiece key, int value) { |
| 532 | return SetKeyInternal(key, std::make_unique<Value>(value)); |
| 533 | } |
| 534 |
| 535 | Value* Value::SetDoubleKey(StringPiece key, double value) { |
| 536 | return SetKeyInternal(key, std::make_unique<Value>(value)); |
| 537 | } |
| 538 |
| 539 | Value* Value::SetStringKey(StringPiece key, StringPiece value) { |
| 540 | return SetKeyInternal(key, std::make_unique<Value>(value)); |
| 541 | } |
| 542 |
| 543 | Value* Value::SetStringKey(StringPiece key, const char* value) { |
| 544 | return SetKeyInternal(key, std::make_unique<Value>(value)); |
| 545 | } |
| 546 |
| 547 | Value* Value::SetStringKey(StringPiece key, std::string&& value) { |
| 548 | return SetKeyInternal(key, std::make_unique<Value>(std::move(value))); |
| 549 | } |
| 550 |
| 551 | Value* Value::SetStringKey(StringPiece key, StringPiece16 value) { |
| 552 | return SetKeyInternal(key, std::make_unique<Value>(value)); |
| 553 | } |
| 554 |
| 555 | bool Value::RemoveKey(StringPiece key) { |
| 556 | CHECK(is_dict()); |
| 557 | return dict_.erase(key) != 0; |
| 558 | } |
| 559 |
| 560 | Optional<Value> Value::ExtractKey(StringPiece key) { |
| 561 | CHECK(is_dict()); |
| 562 | auto found = dict_.find(key); |
| 563 | if (found == dict_.end()) |
| 564 | return nullopt; |
| 565 |
| 566 | Value value = std::move(*found->second); |
| 567 | dict_.erase(found); |
| 568 | return std::move(value); |
| 569 | } |
| 570 |
| 571 | Value* Value::FindPath(StringPiece path) { |
| 572 | return const_cast<Value*>(as_const(*this).FindPath(path)); |
| 573 | } |
| 574 |
| 575 | const Value* Value::FindPath(StringPiece path) const { |
| 576 | CHECK(is_dict()); |
| 577 | const Value* cur = this; |
| 578 | PathSplitter splitter(path); |
| 579 | while (splitter.HasNext()) { |
| 580 | if (!cur->is_dict() || (cur = cur->FindKey(splitter.Next())) == nullptr) |
| 581 | return nullptr; |
| 582 | } |
| 583 | return cur; |
| 584 | } |
| 585 |
| 586 | Value* Value::FindPathOfType(StringPiece path, Type type) { |
| 587 | return const_cast<Value*>(as_const(*this).FindPathOfType(path, type)); |
| 588 | } |
| 589 |
| 590 | const Value* Value::FindPathOfType(StringPiece path, Type type) const { |
| 591 | const Value* cur = FindPath(path); |
| 592 | if (!cur || cur->type() != type) |
| 593 | return nullptr; |
| 594 | return cur; |
| 595 | } |
| 596 |
| 597 | base::Optional<bool> Value::FindBoolPath(StringPiece path) const { |
| 598 | const Value* cur = FindPath(path); |
| 599 | if (!cur || !cur->is_bool()) |
| 600 | return base::nullopt; |
| 601 | return cur->bool_value_; |
| 602 | } |
| 603 |
| 604 | base::Optional<int> Value::FindIntPath(StringPiece path) const { |
| 605 | const Value* cur = FindPath(path); |
| 606 | if (!cur || !cur->is_int()) |
| 607 | return base::nullopt; |
| 608 | return cur->int_value_; |
| 609 | } |
| 610 |
| 611 | base::Optional<double> Value::FindDoublePath(StringPiece path) const { |
| 612 | const Value* cur = FindPath(path); |
| 613 | if (cur) { |
| 614 | if (cur->is_int()) |
| 615 | return static_cast<double>(cur->int_value_); |
| 616 | if (cur->is_double()) |
| 617 | return cur->AsDoubleInternal(); |
| 618 | } |
| 619 | return base::nullopt; |
| 620 | } |
| 621 |
| 622 | const std::string* Value::FindStringPath(StringPiece path) const { |
| 623 | const Value* cur = FindPath(path); |
| 624 | if (!cur || !cur->is_string()) |
| 625 | return nullptr; |
| 626 | return &cur->string_value_; |
| 627 | } |
| 628 |
| 629 | std::string* Value::FindStringPath(StringPiece path) { |
| 630 | return const_cast<std::string*>(as_const(*this).FindStringPath(path)); |
| 631 | } |
| 632 |
| 633 | const Value::BlobStorage* Value::FindBlobPath(StringPiece path) const { |
| 634 | const Value* cur = FindPath(path); |
| 635 | if (!cur || !cur->is_blob()) |
| 636 | return nullptr; |
| 637 | return &cur->binary_value_; |
| 638 | } |
| 639 |
| 640 | const Value* Value::FindDictPath(StringPiece path) const { |
| 641 | return FindPathOfType(path, Type::DICTIONARY); |
| 642 | } |
| 643 |
| 644 | Value* Value::FindDictPath(StringPiece path) { |
| 645 | return FindPathOfType(path, Type::DICTIONARY); |
| 646 | } |
| 647 |
| 648 | const Value* Value::FindListPath(StringPiece path) const { |
| 649 | return FindPathOfType(path, Type::LIST); |
| 650 | } |
| 651 |
| 652 | Value* Value::FindListPath(StringPiece path) { |
| 653 | return FindPathOfType(path, Type::LIST); |
| 654 | } |
| 655 |
| 656 | Value* Value::SetPath(StringPiece path, Value&& value) { |
| 657 | return SetPathInternal(path, std::make_unique<Value>(std::move(value))); |
| 658 | } |
| 659 |
| 660 | Value* Value::SetBoolPath(StringPiece path, bool value) { |
| 661 | return SetPathInternal(path, std::make_unique<Value>(value)); |
| 662 | } |
| 663 |
| 664 | Value* Value::SetIntPath(StringPiece path, int value) { |
| 665 | return SetPathInternal(path, std::make_unique<Value>(value)); |
| 666 | } |
| 667 |
| 668 | Value* Value::SetDoublePath(StringPiece path, double value) { |
| 669 | return SetPathInternal(path, std::make_unique<Value>(value)); |
| 670 | } |
| 671 |
| 672 | Value* Value::SetStringPath(StringPiece path, StringPiece value) { |
| 673 | return SetPathInternal(path, std::make_unique<Value>(value)); |
| 674 | } |
| 675 |
| 676 | Value* Value::SetStringPath(StringPiece path, std::string&& value) { |
| 677 | return SetPathInternal(path, std::make_unique<Value>(std::move(value))); |
| 678 | } |
| 679 |
| 680 | Value* Value::SetStringPath(StringPiece path, const char* value) { |
| 681 | return SetPathInternal(path, std::make_unique<Value>(value)); |
| 682 | } |
| 683 |
| 684 | Value* Value::SetStringPath(StringPiece path, StringPiece16 value) { |
| 685 | return SetPathInternal(path, std::make_unique<Value>(value)); |
| 686 | } |
| 687 |
| 688 | bool Value::RemovePath(StringPiece path) { |
| 689 | return ExtractPath(path).has_value(); |
| 690 | } |
| 691 |
| 692 | Optional<Value> Value::ExtractPath(StringPiece path) { |
| 693 | if (!is_dict() || path.empty()) |
| 694 | return nullopt; |
| 695 |
| 696 | // NOTE: PathSplitter is not being used here because recursion is used to |
| 697 | // ensure that dictionaries that become empty due to this operation are |
| 698 | // removed automatically. |
| 699 | size_t pos = path.find('.'); |
| 700 | if (pos == path.npos) |
| 701 | return ExtractKey(path); |
| 702 |
| 703 | auto found = dict_.find(path.substr(0, pos)); |
| 704 | if (found == dict_.end() || !found->second->is_dict()) |
| 705 | return nullopt; |
| 706 |
| 707 | Optional<Value> extracted = found->second->ExtractPath(path.substr(pos + 1)); |
| 708 | if (extracted && found->second->dict_.empty()) |
| 709 | dict_.erase(found); |
| 710 |
| 711 | return extracted; |
| 712 | } |
| 713 |
| 714 | // DEPRECATED METHODS |
| 715 | Value* Value::FindPath(std::initializer_list<StringPiece> path) { |
| 716 | return const_cast<Value*>(as_const(*this).FindPath(path)); |
| 717 | } |
| 718 |
| 719 | Value* Value::FindPath(span<const StringPiece> path) { |
| 720 | return const_cast<Value*>(as_const(*this).FindPath(path)); |
| 721 | } |
| 722 |
| 723 | const Value* Value::FindPath(std::initializer_list<StringPiece> path) const { |
| 724 | DCHECK_GE(path.size(), 2u) << "Use FindKey() for a path of length 1."; |
| 725 | return FindPath(make_span(path.begin(), path.size())); |
| 726 | } |
| 727 |
| 728 | const Value* Value::FindPath(span<const StringPiece> path) const { |
| 729 | const Value* cur = this; |
| 730 | for (const StringPiece component : path) { |
| 731 | if (!cur->is_dict() || (cur = cur->FindKey(component)) == nullptr) |
| 732 | return nullptr; |
| 733 | } |
| 734 | return cur; |
| 735 | } |
| 736 |
| 737 | Value* Value::FindPathOfType(std::initializer_list<StringPiece> path, |
| 738 | Type type) { |
| 739 | return const_cast<Value*>(as_const(*this).FindPathOfType(path, type)); |
| 740 | } |
| 741 |
| 742 | Value* Value::FindPathOfType(span<const StringPiece> path, Type type) { |
| 743 | return const_cast<Value*>(as_const(*this).FindPathOfType(path, type)); |
| 744 | } |
| 745 |
| 746 | const Value* Value::FindPathOfType(std::initializer_list<StringPiece> path, |
| 747 | Type type) const { |
| 748 | DCHECK_GE(path.size(), 2u) << "Use FindKeyOfType() for a path of length 1."; |
| 749 | return FindPathOfType(make_span(path.begin(), path.size()), type); |
| 750 | } |
| 751 |
| 752 | const Value* Value::FindPathOfType(span<const StringPiece> path, |
| 753 | Type type) const { |
| 754 | const Value* result = FindPath(path); |
| 755 | if (!result || result->type() != type) |
| 756 | return nullptr; |
| 757 | return result; |
| 758 | } |
| 759 |
| 760 | Value* Value::SetPath(std::initializer_list<StringPiece> path, Value&& value) { |
| 761 | DCHECK_GE(path.size(), 2u) << "Use SetKey() for a path of length 1."; |
| 762 | return SetPath(make_span(path.begin(), path.size()), std::move(value)); |
| 763 | } |
| 764 |
| 765 | Value* Value::SetPath(span<const StringPiece> path, Value&& value) { |
| 766 | DCHECK(path.begin() != path.end()); // Can't be empty path. |
| 767 |
| 768 | // Walk/construct intermediate dictionaries. The last element requires |
| 769 | // special handling so skip it in this loop. |
| 770 | Value* cur = this; |
| 771 | auto cur_path = path.begin(); |
| 772 | for (; (cur_path + 1) < path.end(); ++cur_path) { |
| 773 | if (!cur->is_dict()) |
| 774 | return nullptr; |
| 775 |
| 776 | // Use lower_bound to avoid doing the search twice for missing keys. |
| 777 | const StringPiece path_component = *cur_path; |
| 778 | auto found = cur->dict_.lower_bound(path_component); |
| 779 | if (found == cur->dict_.end() || found->first != path_component) { |
| 780 | // No key found, insert one. |
| 781 | auto inserted = cur->dict_.try_emplace( |
| 782 | found, path_component, std::make_unique<Value>(Type::DICTIONARY)); |
| 783 | cur = inserted->second.get(); |
| 784 | } else { |
| 785 | cur = found->second.get(); |
| 786 | } |
| 787 | } |
| 788 |
| 789 | // "cur" will now contain the last dictionary to insert or replace into. |
| 790 | if (!cur->is_dict()) |
| 791 | return nullptr; |
| 792 | return cur->SetKey(*cur_path, std::move(value)); |
| 793 | } |
| 794 |
| 795 | bool Value::RemovePath(std::initializer_list<StringPiece> path) { |
| 796 | DCHECK_GE(path.size(), 2u) << "Use RemoveKey() for a path of length 1."; |
| 797 | return RemovePath(make_span(path.begin(), path.size())); |
| 798 | } |
| 799 |
| 800 | bool Value::RemovePath(span<const StringPiece> path) { |
| 801 | if (!is_dict() || path.empty()) |
| 802 | return false; |
| 803 |
| 804 | if (path.size() == 1) |
| 805 | return RemoveKey(path[0]); |
| 806 |
| 807 | auto found = dict_.find(path[0]); |
| 808 | if (found == dict_.end() || !found->second->is_dict()) |
| 809 | return false; |
| 810 |
| 811 | bool removed = found->second->RemovePath(path.subspan(1)); |
| 812 | if (removed && found->second->dict_.empty()) |
| 813 | dict_.erase(found); |
| 814 |
| 815 | return removed; |
| 816 | } |
| 817 |
| 818 | Value::dict_iterator_proxy Value::DictItems() { |
| 819 | CHECK(is_dict()); |
| 820 | return dict_iterator_proxy(&dict_); |
| 821 | } |
| 822 |
| 823 | Value::const_dict_iterator_proxy Value::DictItems() const { |
| 824 | CHECK(is_dict()); |
| 825 | return const_dict_iterator_proxy(&dict_); |
| 826 | } |
| 827 |
| 828 | size_t Value::DictSize() const { |
| 829 | CHECK(is_dict()); |
| 830 | return dict_.size(); |
| 831 | } |
| 832 |
| 833 | bool Value::DictEmpty() const { |
| 834 | CHECK(is_dict()); |
| 835 | return dict_.empty(); |
| 836 | } |
| 837 |
| 838 | void Value::MergeDictionary(const Value* dictionary) { |
| 839 | CHECK(is_dict()); |
| 840 | CHECK(dictionary->is_dict()); |
| 841 | for (const auto& pair : dictionary->dict_) { |
| 842 | const auto& key = pair.first; |
| 843 | const auto& val = pair.second; |
| 844 | // Check whether we have to merge dictionaries. |
| 845 | if (val->is_dict()) { |
| 846 | auto found = dict_.find(key); |
| 847 | if (found != dict_.end() && found->second->is_dict()) { |
| 848 | found->second->MergeDictionary(val.get()); |
| 849 | continue; |
| 850 | } |
| 851 | } |
| 852 |
| 853 | // All other cases: Make a copy and hook it up. |
| 854 | SetKey(key, val->Clone()); |
| 855 | } |
| 856 | } |
| 857 |
| 858 | bool Value::GetAsBoolean(bool* out_value) const { |
| 859 | if (out_value && is_bool()) { |
| 860 | *out_value = bool_value_; |
| 861 | return true; |
| 862 | } |
| 863 | return is_bool(); |
| 864 | } |
| 865 |
| 866 | bool Value::GetAsInteger(int* out_value) const { |
| 867 | if (out_value && is_int()) { |
| 868 | *out_value = int_value_; |
| 869 | return true; |
| 870 | } |
| 871 | return is_int(); |
| 872 | } |
| 873 |
| 874 | bool Value::GetAsDouble(double* out_value) const { |
| 875 | if (out_value && is_double()) { |
| 876 | *out_value = AsDoubleInternal(); |
| 877 | return true; |
| 878 | } |
| 879 | if (out_value && is_int()) { |
| 880 | // Allow promotion from int to double. |
| 881 | *out_value = int_value_; |
| 882 | return true; |
| 883 | } |
| 884 | return is_double() || is_int(); |
| 885 | } |
| 886 |
| 887 | bool Value::GetAsString(std::string* out_value) const { |
| 888 | if (out_value && is_string()) { |
| 889 | *out_value = string_value_; |
| 890 | return true; |
| 891 | } |
| 892 | return is_string(); |
| 893 | } |
| 894 |
| 895 | bool Value::GetAsString(string16* out_value) const { |
| 896 | if (out_value && is_string()) { |
| 897 | *out_value = UTF8ToUTF16(string_value_); |
| 898 | return true; |
| 899 | } |
| 900 | return is_string(); |
| 901 | } |
| 902 |
| 903 | bool Value::GetAsString(const Value** out_value) const { |
| 904 | if (out_value && is_string()) { |
| 905 | *out_value = this; |
| 906 | return true; |
| 907 | } |
| 908 | return is_string(); |
| 909 | } |
| 910 |
| 911 | bool Value::GetAsString(StringPiece* out_value) const { |
| 912 | if (out_value && is_string()) { |
| 913 | *out_value = string_value_; |
| 914 | return true; |
| 915 | } |
| 916 | return is_string(); |
| 917 | } |
| 918 |
| 919 | bool Value::GetAsList(ListValue** out_value) { |
| 920 | if (out_value && is_list()) { |
| 921 | *out_value = static_cast<ListValue*>(this); |
| 922 | return true; |
| 923 | } |
| 924 | return is_list(); |
| 925 | } |
| 926 |
| 927 | bool Value::GetAsList(const ListValue** out_value) const { |
| 928 | if (out_value && is_list()) { |
| 929 | *out_value = static_cast<const ListValue*>(this); |
| 930 | return true; |
| 931 | } |
| 932 | return is_list(); |
| 933 | } |
| 934 |
| 935 | bool Value::GetAsDictionary(DictionaryValue** out_value) { |
| 936 | if (out_value && is_dict()) { |
| 937 | *out_value = static_cast<DictionaryValue*>(this); |
| 938 | return true; |
| 939 | } |
| 940 | return is_dict(); |
| 941 | } |
| 942 |
| 943 | bool Value::GetAsDictionary(const DictionaryValue** out_value) const { |
| 944 | if (out_value && is_dict()) { |
| 945 | *out_value = static_cast<const DictionaryValue*>(this); |
| 946 | return true; |
| 947 | } |
| 948 | return is_dict(); |
| 949 | } |
| 950 |
| 951 | Value* Value::DeepCopy() const { |
| 952 | return new Value(Clone()); |
| 953 | } |
| 954 |
| 955 | std::unique_ptr<Value> Value::CreateDeepCopy() const { |
| 956 | return std::make_unique<Value>(Clone()); |
| 957 | } |
| 958 |
| 959 | bool operator==(const Value& lhs, const Value& rhs) { |
| 960 | if (lhs.type_ != rhs.type_) |
| 961 | return false; |
| 962 |
| 963 | switch (lhs.type_) { |
| 964 | case Value::Type::NONE: |
| 965 | return true; |
| 966 | case Value::Type::BOOLEAN: |
| 967 | return lhs.bool_value_ == rhs.bool_value_; |
| 968 | case Value::Type::INTEGER: |
| 969 | return lhs.int_value_ == rhs.int_value_; |
| 970 | case Value::Type::DOUBLE: |
| 971 | return lhs.AsDoubleInternal() == rhs.AsDoubleInternal(); |
| 972 | case Value::Type::STRING: |
| 973 | return lhs.string_value_ == rhs.string_value_; |
| 974 | case Value::Type::BINARY: |
| 975 | return lhs.binary_value_ == rhs.binary_value_; |
| 976 | // TODO(crbug.com/646113): Clean this up when DictionaryValue and ListValue |
| 977 | // are completely inlined. |
| 978 | case Value::Type::DICTIONARY: |
| 979 | if (lhs.dict_.size() != rhs.dict_.size()) |
| 980 | return false; |
| 981 | return std::equal(std::begin(lhs.dict_), std::end(lhs.dict_), |
| 982 | std::begin(rhs.dict_), |
| 983 | [](const auto& u, const auto& v) { |
| 984 | return std::tie(u.first, *u.second) == |
| 985 | std::tie(v.first, *v.second); |
| 986 | }); |
| 987 | case Value::Type::LIST: |
| 988 | return lhs.list_ == rhs.list_; |
| 989 | // TODO(crbug.com/859477): Remove after root cause is found. |
| 990 | case Value::Type::DEAD: |
| 991 | CHECK(false); |
| 992 | return false; |
| 993 | } |
| 994 |
| 995 | // TODO(crbug.com/859477): Revert to NOTREACHED() after root cause is found. |
| 996 | CHECK(false); |
| 997 | return false; |
| 998 | } |
| 999 |
| 1000 | bool operator!=(const Value& lhs, const Value& rhs) { |
| 1001 | return !(lhs == rhs); |
| 1002 | } |
| 1003 |
| 1004 | bool operator<(const Value& lhs, const Value& rhs) { |
| 1005 | if (lhs.type_ != rhs.type_) |
| 1006 | return lhs.type_ < rhs.type_; |
| 1007 |
| 1008 | switch (lhs.type_) { |
| 1009 | case Value::Type::NONE: |
| 1010 | return false; |
| 1011 | case Value::Type::BOOLEAN: |
| 1012 | return lhs.bool_value_ < rhs.bool_value_; |
| 1013 | case Value::Type::INTEGER: |
| 1014 | return lhs.int_value_ < rhs.int_value_; |
| 1015 | case Value::Type::DOUBLE: |
| 1016 | return lhs.AsDoubleInternal() < rhs.AsDoubleInternal(); |
| 1017 | case Value::Type::STRING: |
| 1018 | return lhs.string_value_ < rhs.string_value_; |
| 1019 | case Value::Type::BINARY: |
| 1020 | return lhs.binary_value_ < rhs.binary_value_; |
| 1021 | // TODO(crbug.com/646113): Clean this up when DictionaryValue and ListValue |
| 1022 | // are completely inlined. |
| 1023 | case Value::Type::DICTIONARY: |
| 1024 | return std::lexicographical_compare( |
| 1025 | std::begin(lhs.dict_), std::end(lhs.dict_), std::begin(rhs.dict_), |
| 1026 | std::end(rhs.dict_), |
| 1027 | [](const Value::DictStorage::value_type& u, |
| 1028 | const Value::DictStorage::value_type& v) { |
| 1029 | return std::tie(u.first, *u.second) < std::tie(v.first, *v.second); |
| 1030 | }); |
| 1031 | case Value::Type::LIST: |
| 1032 | return lhs.list_ < rhs.list_; |
| 1033 | // TODO(crbug.com/859477): Remove after root cause is found. |
| 1034 | case Value::Type::DEAD: |
| 1035 | CHECK(false); |
| 1036 | return false; |
| 1037 | } |
| 1038 |
| 1039 | // TODO(crbug.com/859477): Revert to NOTREACHED() after root cause is found. |
| 1040 | CHECK(false); |
| 1041 | return false; |
| 1042 | } |
| 1043 |
| 1044 | bool operator>(const Value& lhs, const Value& rhs) { |
| 1045 | return rhs < lhs; |
| 1046 | } |
| 1047 |
| 1048 | bool operator<=(const Value& lhs, const Value& rhs) { |
| 1049 | return !(rhs < lhs); |
| 1050 | } |
| 1051 |
| 1052 | bool operator>=(const Value& lhs, const Value& rhs) { |
| 1053 | return !(lhs < rhs); |
| 1054 | } |
| 1055 |
| 1056 | bool Value::Equals(const Value* other) const { |
| 1057 | DCHECK(other); |
| 1058 | return *this == *other; |
| 1059 | } |
| 1060 |
| 1061 | size_t Value::EstimateMemoryUsage() const { |
| 1062 | switch (type_) { |
| 1063 | case Type::STRING: |
| 1064 | return base::trace_event::EstimateMemoryUsage(string_value_); |
| 1065 | case Type::BINARY: |
| 1066 | return base::trace_event::EstimateMemoryUsage(binary_value_); |
| 1067 | case Type::DICTIONARY: |
| 1068 | return base::trace_event::EstimateMemoryUsage(dict_); |
| 1069 | case Type::LIST: |
| 1070 | return base::trace_event::EstimateMemoryUsage(list_); |
| 1071 | default: |
| 1072 | return 0; |
| 1073 | } |
| 1074 | } |
| 1075 |
| 1076 | void Value::InternalMoveConstructFrom(Value&& that) { |
| 1077 | type_ = that.type_; |
| 1078 |
| 1079 | switch (type_) { |
| 1080 | case Type::NONE: |
| 1081 | return; |
| 1082 | case Type::BOOLEAN: |
| 1083 | bool_value_ = that.bool_value_; |
| 1084 | return; |
| 1085 | case Type::INTEGER: |
| 1086 | int_value_ = that.int_value_; |
| 1087 | return; |
| 1088 | case Type::DOUBLE: |
| 1089 | double_value_ = that.double_value_; |
| 1090 | return; |
| 1091 | case Type::STRING: |
| 1092 | new (&string_value_) std::string(std::move(that.string_value_)); |
| 1093 | return; |
| 1094 | case Type::BINARY: |
| 1095 | new (&binary_value_) BlobStorage(std::move(that.binary_value_)); |
| 1096 | return; |
| 1097 | case Type::DICTIONARY: |
| 1098 | new (&dict_) DictStorage(std::move(that.dict_)); |
| 1099 | return; |
| 1100 | case Type::LIST: |
| 1101 | new (&list_) ListStorage(std::move(that.list_)); |
| 1102 | return; |
| 1103 | // TODO(crbug.com/859477): Remove after root cause is found. |
| 1104 | case Type::DEAD: |
| 1105 | CHECK(false); |
| 1106 | return; |
| 1107 | } |
| 1108 |
| 1109 | // TODO(crbug.com/859477): Revert to NOTREACHED() after root cause is found. |
| 1110 | CHECK(false); |
| 1111 | } |
| 1112 |
| 1113 | void Value::InternalCleanup() { |
| 1114 | switch (type_) { |
| 1115 | case Type::NONE: |
| 1116 | case Type::BOOLEAN: |
| 1117 | case Type::INTEGER: |
| 1118 | case Type::DOUBLE: |
| 1119 | // Nothing to do |
| 1120 | return; |
| 1121 |
| 1122 | case Type::STRING: |
| 1123 | string_value_.~basic_string(); |
| 1124 | return; |
| 1125 | case Type::BINARY: |
| 1126 | binary_value_.~BlobStorage(); |
| 1127 | return; |
| 1128 | case Type::DICTIONARY: |
| 1129 | dict_.~DictStorage(); |
| 1130 | return; |
| 1131 | case Type::LIST: |
| 1132 | list_.~ListStorage(); |
| 1133 | return; |
| 1134 | // TODO(crbug.com/859477): Remove after root cause is found. |
| 1135 | case Type::DEAD: |
| 1136 | CHECK(false); |
| 1137 | return; |
| 1138 | } |
| 1139 |
| 1140 | // TODO(crbug.com/859477): Revert to NOTREACHED() after root cause is found. |
| 1141 | CHECK(false); |
| 1142 | } |
| 1143 |
| 1144 | Value* Value::SetKeyInternal(StringPiece key, |
| 1145 | std::unique_ptr<Value>&& val_ptr) { |
| 1146 | CHECK(is_dict()); |
| 1147 | // NOTE: We can't use |insert_or_assign| here, as only |try_emplace| does |
| 1148 | // an explicit conversion from StringPiece to std::string if necessary. |
| 1149 | auto result = dict_.try_emplace(key, std::move(val_ptr)); |
| 1150 | if (!result.second) { |
| 1151 | // val_ptr is guaranteed to be still intact at this point. |
| 1152 | result.first->second = std::move(val_ptr); |
| 1153 | } |
| 1154 | return result.first->second.get(); |
| 1155 | } |
| 1156 |
| 1157 | Value* Value::SetPathInternal(StringPiece path, |
| 1158 | std::unique_ptr<Value>&& value_ptr) { |
| 1159 | PathSplitter splitter(path); |
| 1160 | DCHECK(splitter.HasNext()) << "Cannot call SetPath() with empty path"; |
| 1161 | // Walk/construct intermediate dictionaries. The last element requires |
| 1162 | // special handling so skip it in this loop. |
| 1163 | Value* cur = this; |
| 1164 | StringPiece path_component = splitter.Next(); |
| 1165 | while (splitter.HasNext()) { |
| 1166 | if (!cur->is_dict()) |
| 1167 | return nullptr; |
| 1168 |
| 1169 | // Use lower_bound to avoid doing the search twice for missing keys. |
| 1170 | auto found = cur->dict_.lower_bound(path_component); |
| 1171 | if (found == cur->dict_.end() || found->first != path_component) { |
| 1172 | // No key found, insert one. |
| 1173 | auto inserted = cur->dict_.try_emplace( |
| 1174 | found, path_component, std::make_unique<Value>(Type::DICTIONARY)); |
| 1175 | cur = inserted->second.get(); |
| 1176 | } else { |
| 1177 | cur = found->second.get(); |
| 1178 | } |
| 1179 | path_component = splitter.Next(); |
| 1180 | } |
| 1181 |
| 1182 | // "cur" will now contain the last dictionary to insert or replace into. |
| 1183 | if (!cur->is_dict()) |
| 1184 | return nullptr; |
| 1185 | return cur->SetKeyInternal(path_component, std::move(value_ptr)); |
| 1186 | } |
| 1187 |
| 1188 | ///////////////////// DictionaryValue //////////////////// |
| 1189 |
| 1190 | // static |
| 1191 | std::unique_ptr<DictionaryValue> DictionaryValue::From( |
| 1192 | std::unique_ptr<Value> value) { |
| 1193 | DictionaryValue* out; |
| 1194 | if (value && value->GetAsDictionary(&out)) { |
| 1195 | ignore_result(value.release()); |
| 1196 | return WrapUnique(out); |
| 1197 | } |
| 1198 | return nullptr; |
| 1199 | } |
| 1200 |
| 1201 | DictionaryValue::DictionaryValue() : Value(Type::DICTIONARY) {} |
| 1202 | DictionaryValue::DictionaryValue(const DictStorage& in_dict) : Value(in_dict) {} |
| 1203 | DictionaryValue::DictionaryValue(DictStorage&& in_dict) noexcept |
| 1204 | : Value(std::move(in_dict)) {} |
| 1205 |
| 1206 | bool DictionaryValue::HasKey(StringPiece key) const { |
| 1207 | DCHECK(IsStringUTF8AllowingNoncharacters(key)); |
| 1208 | auto current_entry = dict_.find(key); |
| 1209 | DCHECK((current_entry == dict_.end()) || current_entry->second); |
| 1210 | return current_entry != dict_.end(); |
| 1211 | } |
| 1212 |
| 1213 | void DictionaryValue::Clear() { |
| 1214 | dict_.clear(); |
| 1215 | } |
| 1216 |
| 1217 | Value* DictionaryValue::Set(StringPiece path, std::unique_ptr<Value> in_value) { |
| 1218 | DCHECK(IsStringUTF8AllowingNoncharacters(path)); |
| 1219 | DCHECK(in_value); |
| 1220 |
| 1221 | // IMPORTANT NOTE: Do not replace with SetPathInternal() yet, because the |
| 1222 | // latter fails when over-writing a non-dict intermediate node, while this |
| 1223 | // method just replaces it with one. This difference makes some tests actually |
| 1224 | // fail (http://crbug.com/949461). |
| 1225 | StringPiece current_path(path); |
| 1226 | Value* current_dictionary = this; |
| 1227 | for (size_t delimiter_position = current_path.find('.'); |
| 1228 | delimiter_position != StringPiece::npos; |
| 1229 | delimiter_position = current_path.find('.')) { |
| 1230 | // Assume that we're indexing into a dictionary. |
| 1231 | StringPiece key = current_path.substr(0, delimiter_position); |
| 1232 | Value* child_dictionary = |
| 1233 | current_dictionary->FindKeyOfType(key, Type::DICTIONARY); |
| 1234 | if (!child_dictionary) { |
| 1235 | child_dictionary = |
| 1236 | current_dictionary->SetKey(key, Value(Type::DICTIONARY)); |
| 1237 | } |
| 1238 |
| 1239 | current_dictionary = child_dictionary; |
| 1240 | current_path = current_path.substr(delimiter_position + 1); |
| 1241 | } |
| 1242 |
| 1243 | return static_cast<DictionaryValue*>(current_dictionary) |
| 1244 | ->SetWithoutPathExpansion(current_path, std::move(in_value)); |
| 1245 | } |
| 1246 |
| 1247 | Value* DictionaryValue::SetBoolean(StringPiece path, bool in_value) { |
| 1248 | return Set(path, std::make_unique<Value>(in_value)); |
| 1249 | } |
| 1250 |
| 1251 | Value* DictionaryValue::SetInteger(StringPiece path, int in_value) { |
| 1252 | return Set(path, std::make_unique<Value>(in_value)); |
| 1253 | } |
| 1254 |
| 1255 | Value* DictionaryValue::SetDouble(StringPiece path, double in_value) { |
| 1256 | return Set(path, std::make_unique<Value>(in_value)); |
| 1257 | } |
| 1258 |
| 1259 | Value* DictionaryValue::SetString(StringPiece path, StringPiece in_value) { |
| 1260 | return Set(path, std::make_unique<Value>(in_value)); |
| 1261 | } |
| 1262 |
| 1263 | Value* DictionaryValue::SetString(StringPiece path, const string16& in_value) { |
| 1264 | return Set(path, std::make_unique<Value>(in_value)); |
| 1265 | } |
| 1266 |
| 1267 | DictionaryValue* DictionaryValue::SetDictionary( |
| 1268 | StringPiece path, |
| 1269 | std::unique_ptr<DictionaryValue> in_value) { |
| 1270 | return static_cast<DictionaryValue*>(Set(path, std::move(in_value))); |
| 1271 | } |
| 1272 |
| 1273 | ListValue* DictionaryValue::SetList(StringPiece path, |
| 1274 | std::unique_ptr<ListValue> in_value) { |
| 1275 | return static_cast<ListValue*>(Set(path, std::move(in_value))); |
| 1276 | } |
| 1277 |
| 1278 | Value* DictionaryValue::SetWithoutPathExpansion( |
| 1279 | StringPiece key, |
| 1280 | std::unique_ptr<Value> in_value) { |
| 1281 | // NOTE: We can't use |insert_or_assign| here, as only |try_emplace| does |
| 1282 | // an explicit conversion from StringPiece to std::string if necessary. |
| 1283 | auto result = dict_.try_emplace(key, std::move(in_value)); |
| 1284 | if (!result.second) { |
| 1285 | // in_value is guaranteed to be still intact at this point. |
| 1286 | result.first->second = std::move(in_value); |
| 1287 | } |
| 1288 | return result.first->second.get(); |
| 1289 | } |
| 1290 |
| 1291 | bool DictionaryValue::Get(StringPiece path, |
| 1292 | const Value** out_value) const { |
| 1293 | DCHECK(IsStringUTF8AllowingNoncharacters(path)); |
| 1294 | const Value* value = FindPath(path); |
| 1295 | if (!value) |
| 1296 | return false; |
| 1297 | if (out_value) |
| 1298 | *out_value = value; |
| 1299 | return true; |
| 1300 | } |
| 1301 |
| 1302 | bool DictionaryValue::Get(StringPiece path, Value** out_value) { |
| 1303 | return as_const(*this).Get(path, const_cast<const Value**>(out_value)); |
| 1304 | } |
| 1305 |
| 1306 | bool DictionaryValue::GetBoolean(StringPiece path, bool* bool_value) const { |
| 1307 | const Value* value; |
| 1308 | if (!Get(path, &value)) |
| 1309 | return false; |
| 1310 |
| 1311 | return value->GetAsBoolean(bool_value); |
| 1312 | } |
| 1313 |
| 1314 | bool DictionaryValue::GetInteger(StringPiece path, int* out_value) const { |
| 1315 | const Value* value; |
| 1316 | if (!Get(path, &value)) |
| 1317 | return false; |
| 1318 |
| 1319 | return value->GetAsInteger(out_value); |
| 1320 | } |
| 1321 |
| 1322 | bool DictionaryValue::GetDouble(StringPiece path, double* out_value) const { |
| 1323 | const Value* value; |
| 1324 | if (!Get(path, &value)) |
| 1325 | return false; |
| 1326 |
| 1327 | return value->GetAsDouble(out_value); |
| 1328 | } |
| 1329 |
| 1330 | bool DictionaryValue::GetString(StringPiece path, |
| 1331 | std::string* out_value) const { |
| 1332 | const Value* value; |
| 1333 | if (!Get(path, &value)) |
| 1334 | return false; |
| 1335 |
| 1336 | return value->GetAsString(out_value); |
| 1337 | } |
| 1338 |
| 1339 | bool DictionaryValue::GetString(StringPiece path, string16* out_value) const { |
| 1340 | const Value* value; |
| 1341 | if (!Get(path, &value)) |
| 1342 | return false; |
| 1343 |
| 1344 | return value->GetAsString(out_value); |
| 1345 | } |
| 1346 |
| 1347 | bool DictionaryValue::GetStringASCII(StringPiece path, |
| 1348 | std::string* out_value) const { |
| 1349 | std::string out; |
| 1350 | if (!GetString(path, &out)) |
| 1351 | return false; |
| 1352 |
| 1353 | if (!IsStringASCII(out)) { |
| 1354 | NOTREACHED(); |
| 1355 | return false; |
| 1356 | } |
| 1357 |
| 1358 | out_value->assign(out); |
| 1359 | return true; |
| 1360 | } |
| 1361 |
| 1362 | bool DictionaryValue::GetBinary(StringPiece path, |
| 1363 | const Value** out_value) const { |
| 1364 | const Value* value; |
| 1365 | bool result = Get(path, &value); |
| 1366 | if (!result || !value->is_blob()) |
| 1367 | return false; |
| 1368 |
| 1369 | if (out_value) |
| 1370 | *out_value = value; |
| 1371 |
| 1372 | return true; |
| 1373 | } |
| 1374 |
| 1375 | bool DictionaryValue::GetBinary(StringPiece path, Value** out_value) { |
| 1376 | return as_const(*this).GetBinary(path, const_cast<const Value**>(out_value)); |
| 1377 | } |
| 1378 |
| 1379 | bool DictionaryValue::GetDictionary(StringPiece path, |
| 1380 | const DictionaryValue** out_value) const { |
| 1381 | const Value* value; |
| 1382 | bool result = Get(path, &value); |
| 1383 | if (!result || !value->is_dict()) |
| 1384 | return false; |
| 1385 |
| 1386 | if (out_value) |
| 1387 | *out_value = static_cast<const DictionaryValue*>(value); |
| 1388 |
| 1389 | return true; |
| 1390 | } |
| 1391 |
| 1392 | bool DictionaryValue::GetDictionary(StringPiece path, |
| 1393 | DictionaryValue** out_value) { |
| 1394 | return as_const(*this).GetDictionary( |
| 1395 | path, const_cast<const DictionaryValue**>(out_value)); |
| 1396 | } |
| 1397 |
| 1398 | bool DictionaryValue::GetList(StringPiece path, |
| 1399 | const ListValue** out_value) const { |
| 1400 | const Value* value; |
| 1401 | bool result = Get(path, &value); |
| 1402 | if (!result || !value->is_list()) |
| 1403 | return false; |
| 1404 |
| 1405 | if (out_value) |
| 1406 | *out_value = static_cast<const ListValue*>(value); |
| 1407 |
| 1408 | return true; |
| 1409 | } |
| 1410 |
| 1411 | bool DictionaryValue::GetList(StringPiece path, ListValue** out_value) { |
| 1412 | return as_const(*this).GetList(path, |
| 1413 | const_cast<const ListValue**>(out_value)); |
| 1414 | } |
| 1415 |
| 1416 | bool DictionaryValue::GetWithoutPathExpansion(StringPiece key, |
| 1417 | const Value** out_value) const { |
| 1418 | DCHECK(IsStringUTF8AllowingNoncharacters(key)); |
| 1419 | auto entry_iterator = dict_.find(key); |
| 1420 | if (entry_iterator == dict_.end()) |
| 1421 | return false; |
| 1422 |
| 1423 | if (out_value) |
| 1424 | *out_value = entry_iterator->second.get(); |
| 1425 | return true; |
| 1426 | } |
| 1427 |
| 1428 | bool DictionaryValue::GetWithoutPathExpansion(StringPiece key, |
| 1429 | Value** out_value) { |
| 1430 | return as_const(*this).GetWithoutPathExpansion( |
| 1431 | key, const_cast<const Value**>(out_value)); |
| 1432 | } |
| 1433 |
| 1434 | bool DictionaryValue::GetBooleanWithoutPathExpansion(StringPiece key, |
| 1435 | bool* out_value) const { |
| 1436 | const Value* value; |
| 1437 | if (!GetWithoutPathExpansion(key, &value)) |
| 1438 | return false; |
| 1439 |
| 1440 | return value->GetAsBoolean(out_value); |
| 1441 | } |
| 1442 |
| 1443 | bool DictionaryValue::GetIntegerWithoutPathExpansion(StringPiece key, |
| 1444 | int* out_value) const { |
| 1445 | const Value* value; |
| 1446 | if (!GetWithoutPathExpansion(key, &value)) |
| 1447 | return false; |
| 1448 |
| 1449 | return value->GetAsInteger(out_value); |
| 1450 | } |
| 1451 |
| 1452 | bool DictionaryValue::GetDoubleWithoutPathExpansion(StringPiece key, |
| 1453 | double* out_value) const { |
| 1454 | const Value* value; |
| 1455 | if (!GetWithoutPathExpansion(key, &value)) |
| 1456 | return false; |
| 1457 |
| 1458 | return value->GetAsDouble(out_value); |
| 1459 | } |
| 1460 |
| 1461 | bool DictionaryValue::GetStringWithoutPathExpansion( |
| 1462 | StringPiece key, |
| 1463 | std::string* out_value) const { |
| 1464 | const Value* value; |
| 1465 | if (!GetWithoutPathExpansion(key, &value)) |
| 1466 | return false; |
| 1467 |
| 1468 | return value->GetAsString(out_value); |
| 1469 | } |
| 1470 |
| 1471 | bool DictionaryValue::GetStringWithoutPathExpansion(StringPiece key, |
| 1472 | string16* out_value) const { |
| 1473 | const Value* value; |
| 1474 | if (!GetWithoutPathExpansion(key, &value)) |
| 1475 | return false; |
| 1476 |
| 1477 | return value->GetAsString(out_value); |
| 1478 | } |
| 1479 |
| 1480 | bool DictionaryValue::GetDictionaryWithoutPathExpansion( |
| 1481 | StringPiece key, |
| 1482 | const DictionaryValue** out_value) const { |
| 1483 | const Value* value; |
| 1484 | bool result = GetWithoutPathExpansion(key, &value); |
| 1485 | if (!result || !value->is_dict()) |
| 1486 | return false; |
| 1487 |
| 1488 | if (out_value) |
| 1489 | *out_value = static_cast<const DictionaryValue*>(value); |
| 1490 |
| 1491 | return true; |
| 1492 | } |
| 1493 |
| 1494 | bool DictionaryValue::GetDictionaryWithoutPathExpansion( |
| 1495 | StringPiece key, |
| 1496 | DictionaryValue** out_value) { |
| 1497 | return as_const(*this).GetDictionaryWithoutPathExpansion( |
| 1498 | key, const_cast<const DictionaryValue**>(out_value)); |
| 1499 | } |
| 1500 |
| 1501 | bool DictionaryValue::GetListWithoutPathExpansion( |
| 1502 | StringPiece key, |
| 1503 | const ListValue** out_value) const { |
| 1504 | const Value* value; |
| 1505 | bool result = GetWithoutPathExpansion(key, &value); |
| 1506 | if (!result || !value->is_list()) |
| 1507 | return false; |
| 1508 |
| 1509 | if (out_value) |
| 1510 | *out_value = static_cast<const ListValue*>(value); |
| 1511 |
| 1512 | return true; |
| 1513 | } |
| 1514 |
| 1515 | bool DictionaryValue::GetListWithoutPathExpansion(StringPiece key, |
| 1516 | ListValue** out_value) { |
| 1517 | return as_const(*this).GetListWithoutPathExpansion( |
| 1518 | key, const_cast<const ListValue**>(out_value)); |
| 1519 | } |
| 1520 |
| 1521 | bool DictionaryValue::Remove(StringPiece path, |
| 1522 | std::unique_ptr<Value>* out_value) { |
| 1523 | DCHECK(IsStringUTF8AllowingNoncharacters(path)); |
| 1524 | StringPiece current_path(path); |
| 1525 | DictionaryValue* current_dictionary = this; |
| 1526 | size_t delimiter_position = current_path.rfind('.'); |
| 1527 | if (delimiter_position != StringPiece::npos) { |
| 1528 | if (!GetDictionary(current_path.substr(0, delimiter_position), |
| 1529 | ¤t_dictionary)) |
| 1530 | return false; |
| 1531 | current_path = current_path.substr(delimiter_position + 1); |
| 1532 | } |
| 1533 |
| 1534 | return current_dictionary->RemoveWithoutPathExpansion(current_path, |
| 1535 | out_value); |
| 1536 | } |
| 1537 |
| 1538 | bool DictionaryValue::RemoveWithoutPathExpansion( |
| 1539 | StringPiece key, |
| 1540 | std::unique_ptr<Value>* out_value) { |
| 1541 | DCHECK(IsStringUTF8AllowingNoncharacters(key)); |
| 1542 | auto entry_iterator = dict_.find(key); |
| 1543 | if (entry_iterator == dict_.end()) |
| 1544 | return false; |
| 1545 |
| 1546 | if (out_value) |
| 1547 | *out_value = std::move(entry_iterator->second); |
| 1548 | dict_.erase(entry_iterator); |
| 1549 | return true; |
| 1550 | } |
| 1551 |
| 1552 | bool DictionaryValue::RemovePath(StringPiece path, |
| 1553 | std::unique_ptr<Value>* out_value) { |
| 1554 | bool result = false; |
| 1555 | size_t delimiter_position = path.find('.'); |
| 1556 |
| 1557 | if (delimiter_position == std::string::npos) |
| 1558 | return RemoveWithoutPathExpansion(path, out_value); |
| 1559 |
| 1560 | StringPiece subdict_path = path.substr(0, delimiter_position); |
| 1561 | DictionaryValue* subdict = nullptr; |
| 1562 | if (!GetDictionary(subdict_path, &subdict)) |
| 1563 | return false; |
| 1564 | result = subdict->RemovePath(path.substr(delimiter_position + 1), |
| 1565 | out_value); |
| 1566 | if (result && subdict->empty()) |
| 1567 | RemoveWithoutPathExpansion(subdict_path, nullptr); |
| 1568 |
| 1569 | return result; |
| 1570 | } |
| 1571 |
| 1572 | std::unique_ptr<DictionaryValue> DictionaryValue::DeepCopyWithoutEmptyChildren() |
| 1573 | const { |
| 1574 | std::unique_ptr<DictionaryValue> copy = |
| 1575 | CopyDictionaryWithoutEmptyChildren(*this); |
| 1576 | if (!copy) |
| 1577 | copy = std::make_unique<DictionaryValue>(); |
| 1578 | return copy; |
| 1579 | } |
| 1580 |
| 1581 | void DictionaryValue::Swap(DictionaryValue* other) { |
| 1582 | CHECK(other->is_dict()); |
| 1583 | dict_.swap(other->dict_); |
| 1584 | } |
| 1585 |
| 1586 | DictionaryValue::Iterator::Iterator(const DictionaryValue& target) |
| 1587 | : target_(target), it_(target.dict_.begin()) {} |
| 1588 |
| 1589 | DictionaryValue::Iterator::Iterator(const Iterator& other) = default; |
| 1590 |
| 1591 | DictionaryValue::Iterator::~Iterator() = default; |
| 1592 |
| 1593 | DictionaryValue* DictionaryValue::DeepCopy() const { |
| 1594 | return new DictionaryValue(dict_); |
| 1595 | } |
| 1596 |
| 1597 | std::unique_ptr<DictionaryValue> DictionaryValue::CreateDeepCopy() const { |
| 1598 | return std::make_unique<DictionaryValue>(dict_); |
| 1599 | } |
| 1600 |
| 1601 | ///////////////////// ListValue //////////////////// |
| 1602 |
| 1603 | // static |
| 1604 | std::unique_ptr<ListValue> ListValue::From(std::unique_ptr<Value> value) { |
| 1605 | ListValue* out; |
| 1606 | if (value && value->GetAsList(&out)) { |
| 1607 | ignore_result(value.release()); |
| 1608 | return WrapUnique(out); |
| 1609 | } |
| 1610 | return nullptr; |
| 1611 | } |
| 1612 |
| 1613 | ListValue::ListValue() : Value(Type::LIST) {} |
| 1614 | ListValue::ListValue(span<const Value> in_list) : Value(in_list) {} |
| 1615 | ListValue::ListValue(ListStorage&& in_list) noexcept |
| 1616 | : Value(std::move(in_list)) {} |
| 1617 |
| 1618 | void ListValue::Clear() { |
| 1619 | list_.clear(); |
| 1620 | } |
| 1621 |
| 1622 | void ListValue::Reserve(size_t n) { |
| 1623 | list_.reserve(n); |
| 1624 | } |
| 1625 |
| 1626 | bool ListValue::Set(size_t index, std::unique_ptr<Value> in_value) { |
| 1627 | if (!in_value) |
| 1628 | return false; |
| 1629 |
| 1630 | if (index >= list_.size()) |
| 1631 | list_.resize(index + 1); |
| 1632 |
| 1633 | list_[index] = std::move(*in_value); |
| 1634 | return true; |
| 1635 | } |
| 1636 |
| 1637 | bool ListValue::Get(size_t index, const Value** out_value) const { |
| 1638 | if (index >= list_.size()) |
| 1639 | return false; |
| 1640 |
| 1641 | if (out_value) |
| 1642 | *out_value = &list_[index]; |
| 1643 |
| 1644 | return true; |
| 1645 | } |
| 1646 |
| 1647 | bool ListValue::Get(size_t index, Value** out_value) { |
| 1648 | return as_const(*this).Get(index, const_cast<const Value**>(out_value)); |
| 1649 | } |
| 1650 |
| 1651 | bool ListValue::GetBoolean(size_t index, bool* bool_value) const { |
| 1652 | const Value* value; |
| 1653 | if (!Get(index, &value)) |
| 1654 | return false; |
| 1655 |
| 1656 | return value->GetAsBoolean(bool_value); |
| 1657 | } |
| 1658 |
| 1659 | bool ListValue::GetInteger(size_t index, int* out_value) const { |
| 1660 | const Value* value; |
| 1661 | if (!Get(index, &value)) |
| 1662 | return false; |
| 1663 |
| 1664 | return value->GetAsInteger(out_value); |
| 1665 | } |
| 1666 |
| 1667 | bool ListValue::GetDouble(size_t index, double* out_value) const { |
| 1668 | const Value* value; |
| 1669 | if (!Get(index, &value)) |
| 1670 | return false; |
| 1671 |
| 1672 | return value->GetAsDouble(out_value); |
| 1673 | } |
| 1674 |
| 1675 | bool ListValue::GetString(size_t index, std::string* out_value) const { |
| 1676 | const Value* value; |
| 1677 | if (!Get(index, &value)) |
| 1678 | return false; |
| 1679 |
| 1680 | return value->GetAsString(out_value); |
| 1681 | } |
| 1682 |
| 1683 | bool ListValue::GetString(size_t index, string16* out_value) const { |
| 1684 | const Value* value; |
| 1685 | if (!Get(index, &value)) |
| 1686 | return false; |
| 1687 |
| 1688 | return value->GetAsString(out_value); |
| 1689 | } |
| 1690 |
| 1691 | bool ListValue::GetDictionary(size_t index, |
| 1692 | const DictionaryValue** out_value) const { |
| 1693 | const Value* value; |
| 1694 | bool result = Get(index, &value); |
| 1695 | if (!result || !value->is_dict()) |
| 1696 | return false; |
| 1697 |
| 1698 | if (out_value) |
| 1699 | *out_value = static_cast<const DictionaryValue*>(value); |
| 1700 |
| 1701 | return true; |
| 1702 | } |
| 1703 |
| 1704 | bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) { |
| 1705 | return as_const(*this).GetDictionary( |
| 1706 | index, const_cast<const DictionaryValue**>(out_value)); |
| 1707 | } |
| 1708 |
| 1709 | bool ListValue::GetList(size_t index, const ListValue** out_value) const { |
| 1710 | const Value* value; |
| 1711 | bool result = Get(index, &value); |
| 1712 | if (!result || !value->is_list()) |
| 1713 | return false; |
| 1714 |
| 1715 | if (out_value) |
| 1716 | *out_value = static_cast<const ListValue*>(value); |
| 1717 |
| 1718 | return true; |
| 1719 | } |
| 1720 |
| 1721 | bool ListValue::GetList(size_t index, ListValue** out_value) { |
| 1722 | return as_const(*this).GetList(index, |
| 1723 | const_cast<const ListValue**>(out_value)); |
| 1724 | } |
| 1725 |
| 1726 | bool ListValue::Remove(size_t index, std::unique_ptr<Value>* out_value) { |
| 1727 | if (index >= list_.size()) |
| 1728 | return false; |
| 1729 |
| 1730 | if (out_value) |
| 1731 | *out_value = std::make_unique<Value>(std::move(list_[index])); |
| 1732 |
| 1733 | list_.erase(list_.begin() + index); |
| 1734 | return true; |
| 1735 | } |
| 1736 |
| 1737 | bool ListValue::Remove(const Value& value, size_t* index) { |
| 1738 | auto it = std::find(list_.begin(), list_.end(), value); |
| 1739 |
| 1740 | if (it == list_.end()) |
| 1741 | return false; |
| 1742 |
| 1743 | if (index) |
| 1744 | *index = std::distance(list_.begin(), it); |
| 1745 |
| 1746 | list_.erase(it); |
| 1747 | return true; |
| 1748 | } |
| 1749 |
| 1750 | ListValue::iterator ListValue::Erase(iterator iter, |
| 1751 | std::unique_ptr<Value>* out_value) { |
| 1752 | if (out_value) |
| 1753 | *out_value = std::make_unique<Value>(std::move(*iter)); |
| 1754 |
| 1755 | auto list_iter = list_.begin() + (iter - GetList().begin()); |
| 1756 | CHECK(list_iter != list_.end()); |
| 1757 | list_iter = list_.erase(list_iter); |
| 1758 | return GetList().begin() + (list_iter - list_.begin()); |
| 1759 | } |
| 1760 |
| 1761 | void ListValue::Append(std::unique_ptr<Value> in_value) { |
| 1762 | list_.push_back(std::move(*in_value)); |
| 1763 | } |
| 1764 |
| 1765 | void ListValue::AppendBoolean(bool in_value) { |
| 1766 | list_.emplace_back(in_value); |
| 1767 | } |
| 1768 |
| 1769 | void ListValue::AppendInteger(int in_value) { |
| 1770 | list_.emplace_back(in_value); |
| 1771 | } |
| 1772 |
| 1773 | void ListValue::AppendDouble(double in_value) { |
| 1774 | list_.emplace_back(in_value); |
| 1775 | } |
| 1776 |
| 1777 | void ListValue::AppendString(StringPiece in_value) { |
| 1778 | list_.emplace_back(in_value); |
| 1779 | } |
| 1780 |
| 1781 | void ListValue::AppendString(const string16& in_value) { |
| 1782 | list_.emplace_back(in_value); |
| 1783 | } |
| 1784 |
| 1785 | void ListValue::AppendStrings(const std::vector<std::string>& in_values) { |
| 1786 | list_.reserve(list_.size() + in_values.size()); |
| 1787 | for (const auto& in_value : in_values) |
| 1788 | list_.emplace_back(in_value); |
| 1789 | } |
| 1790 |
| 1791 | void ListValue::AppendStrings(const std::vector<string16>& in_values) { |
| 1792 | list_.reserve(list_.size() + in_values.size()); |
| 1793 | for (const auto& in_value : in_values) |
| 1794 | list_.emplace_back(in_value); |
| 1795 | } |
| 1796 |
| 1797 | bool ListValue::AppendIfNotPresent(std::unique_ptr<Value> in_value) { |
| 1798 | DCHECK(in_value); |
| 1799 | if (Contains(list_, *in_value)) |
| 1800 | return false; |
| 1801 |
| 1802 | list_.push_back(std::move(*in_value)); |
| 1803 | return true; |
| 1804 | } |
| 1805 |
| 1806 | bool ListValue::Insert(size_t index, std::unique_ptr<Value> in_value) { |
| 1807 | DCHECK(in_value); |
| 1808 | if (index > list_.size()) |
| 1809 | return false; |
| 1810 |
| 1811 | list_.insert(list_.begin() + index, std::move(*in_value)); |
| 1812 | return true; |
| 1813 | } |
| 1814 |
| 1815 | ListValue::const_iterator ListValue::Find(const Value& value) const { |
| 1816 | return std::find(GetList().begin(), GetList().end(), value); |
| 1817 | } |
| 1818 |
| 1819 | void ListValue::Swap(ListValue* other) { |
| 1820 | CHECK(other->is_list()); |
| 1821 | list_.swap(other->list_); |
| 1822 | } |
| 1823 |
| 1824 | ListValue* ListValue::DeepCopy() const { |
| 1825 | return new ListValue(list_); |
| 1826 | } |
| 1827 |
| 1828 | std::unique_ptr<ListValue> ListValue::CreateDeepCopy() const { |
| 1829 | return std::make_unique<ListValue>(list_); |
| 1830 | } |
| 1831 |
| 1832 | ValueSerializer::~ValueSerializer() = default; |
| 1833 |
| 1834 | ValueDeserializer::~ValueDeserializer() = default; |
| 1835 |
| 1836 | std::ostream& operator<<(std::ostream& out, const Value& value) { |
| 1837 | std::string json; |
| 1838 | JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
| 1839 | return out << json; |
| 1840 | } |
| 1841 |
| 1842 | std::ostream& operator<<(std::ostream& out, const Value::Type& type) { |
| 1843 | if (static_cast<int>(type) < 0 || |
| 1844 | static_cast<size_t>(type) >= base::size(kTypeNames)) |
| 1845 | return out << "Invalid Type (index = " << static_cast<int>(type) << ")"; |
| 1846 | return out << Value::GetTypeName(type); |
| 1847 | } |
| 1848 |
| 1849 | } // namespace base |
| 1850 |