123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737 |
- // Copyright (c) 2012 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "base/values.h"
- #include <algorithm>
- #include <cmath>
- #include <ostream>
- #include <tuple>
- #include <utility>
- #include "base/as_const.h"
- #include "base/bit_cast.h"
- #include "base/check.h"
- #include "base/check_op.h"
- #include "base/containers/checked_iterators.h"
- #include "base/containers/cxx20_erase_vector.h"
- #include "base/cxx17_backports.h"
- #include "base/cxx20_to_address.h"
- #include "base/json/json_writer.h"
- #include "base/logging.h"
- #include "base/memory/ptr_util.h"
- #include "base/notreached.h"
- #include "base/ranges/algorithm.h"
- #include "base/strings/string_util.h"
- #include "base/strings/utf_string_conversions.h"
- #include "base/trace_event/base_tracing.h"
- #include "base/tracing_buildflags.h"
- #include "third_party/abseil-cpp/absl/types/optional.h"
- #include "third_party/abseil-cpp/absl/types/variant.h"
- #if BUILDFLAG(ENABLE_BASE_TRACING)
- #include "base/trace_event/memory_usage_estimator.h" // no-presubmit-check
- #endif // BUILDFLAG(ENABLE_BASE_TRACING)
- namespace base {
- namespace {
- const char* const kTypeNames[] = {"null", "boolean", "integer", "double",
- "string", "binary", "dictionary", "list"};
- static_assert(std::size(kTypeNames) ==
- static_cast<size_t>(Value::Type::LIST) + 1,
- "kTypeNames Has Wrong Size");
- // Helper class to enumerate the path components from a StringPiece
- // without performing heap allocations. Components are simply separated
- // by single dots (e.g. "foo.bar.baz" -> ["foo", "bar", "baz"]).
- //
- // Usage example:
- // PathSplitter splitter(some_path);
- // while (splitter.HasNext()) {
- // StringPiece component = splitter.Next();
- // ...
- // }
- //
- class PathSplitter {
- public:
- explicit PathSplitter(StringPiece path) : path_(path) {}
- bool HasNext() const { return pos_ < path_.size(); }
- StringPiece Next() {
- DCHECK(HasNext());
- size_t start = pos_;
- size_t pos = path_.find('.', start);
- size_t end;
- if (pos == path_.npos) {
- end = path_.size();
- pos_ = end;
- } else {
- end = pos;
- pos_ = pos + 1;
- }
- return path_.substr(start, end - start);
- }
- private:
- StringPiece path_;
- size_t pos_ = 0;
- };
- std::string DebugStringImpl(ValueView value) {
- std::string json;
- JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json);
- return json;
- }
- } // namespace
- // A helper used to provide templated functions for cloning to Value, and
- // ValueView. This private class is used so the cloning method may have access
- // to the special private constructors in Value, created specifically for
- // cloning.
- class Value::CloningHelper {
- public:
- // This set of overloads are used to unwrap the reference wrappers, which are
- // presented when cloning a ValueView.
- template <typename T>
- static const T& UnwrapReference(std::reference_wrapper<const T> value) {
- return value.get();
- }
- template <typename T>
- static const T& UnwrapReference(const T& value) {
- return value;
- }
- // Returns a new Value object using the contents of the |storage| variant.
- template <typename Storage>
- static Value Clone(const Storage& storage) {
- return absl::visit(
- [](const auto& member) {
- const auto& value = UnwrapReference(member);
- using T = std::decay_t<decltype(value)>;
- if constexpr (std::is_same_v<T, Value::Dict> ||
- std::is_same_v<T, Value::List>) {
- return Value(value.Clone());
- } else {
- return Value(value);
- }
- },
- storage);
- }
- };
- // static
- Value Value::FromUniquePtrValue(std::unique_ptr<Value> val) {
- return std::move(*val);
- }
- // static
- std::unique_ptr<Value> Value::ToUniquePtrValue(Value val) {
- return std::make_unique<Value>(std::move(val));
- }
- // static
- const DictionaryValue& Value::AsDictionaryValue(const Value& val) {
- CHECK(val.is_dict());
- return static_cast<const DictionaryValue&>(val);
- }
- // static
- const ListValue& Value::AsListValue(const Value& val) {
- CHECK(val.is_list());
- return static_cast<const ListValue&>(val);
- }
- Value::Value() noexcept = default;
- Value::Value(Value&&) noexcept = default;
- Value& Value::operator=(Value&&) noexcept = default;
- Value::Value(Type type) {
- // Initialize with the default value.
- switch (type) {
- case Type::NONE:
- return;
- case Type::BOOLEAN:
- data_.emplace<bool>(false);
- return;
- case Type::INTEGER:
- data_.emplace<int>(0);
- return;
- case Type::DOUBLE:
- data_.emplace<DoubleStorage>(0.0);
- return;
- case Type::STRING:
- data_.emplace<std::string>();
- return;
- case Type::BINARY:
- data_.emplace<BlobStorage>();
- return;
- case Type::DICTIONARY:
- data_.emplace<Dict>();
- return;
- case Type::LIST:
- data_.emplace<List>();
- return;
- }
- CHECK(false);
- }
- Value::Value(bool value) : data_(value) {}
- Value::Value(int value) : data_(value) {}
- Value::Value(double value)
- : data_(absl::in_place_type_t<DoubleStorage>(), value) {}
- Value::Value(StringPiece value) : Value(std::string(value)) {}
- Value::Value(StringPiece16 value) : Value(UTF16ToUTF8(value)) {}
- Value::Value(const char* value) : Value(std::string(value)) {}
- Value::Value(const char16_t* value) : Value(UTF16ToUTF8(value)) {}
- Value::Value(std::string&& value) noexcept : data_(std::move(value)) {
- DCHECK(IsStringUTF8AllowingNoncharacters(GetString()));
- }
- Value::Value(const std::vector<char>& value)
- : data_(absl::in_place_type_t<BlobStorage>(), value.begin(), value.end()) {}
- Value::Value(base::span<const uint8_t> value)
- : data_(absl::in_place_type_t<BlobStorage>(), value.size()) {
- // This is 100x faster than using the "range" constructor for a 512k blob:
- // crbug.com/1343636
- std::copy(value.begin(), value.end(), absl::get<BlobStorage>(data_).data());
- }
- Value::Value(BlobStorage&& value) noexcept : data_(std::move(value)) {}
- Value::Value(Dict&& value) noexcept : data_(std::move(value)) {}
- Value::Value(List&& value) noexcept : data_(std::move(value)) {}
- Value::Value(const LegacyDictStorage& storage)
- : data_(absl::in_place_type_t<Dict>()) {
- dict().reserve(storage.size());
- for (const auto& it : storage) {
- dict().try_emplace(dict().end(), it.first,
- std::make_unique<Value>(it.second->Clone()));
- }
- }
- Value::Value(LegacyDictStorage&& storage) noexcept
- : data_(absl::in_place_type_t<Dict>()) {
- dict() = std::move(storage);
- }
- Value::Value(absl::monostate) {}
- Value::Value(DoubleStorage storage) : data_(std::move(storage)) {}
- Value::DoubleStorage::DoubleStorage(double v) : v_(bit_cast<decltype(v_)>(v)) {
- if (!std::isfinite(v)) {
- NOTREACHED() << "Non-finite (i.e. NaN or positive/negative infinity) "
- << "values cannot be represented in JSON";
- v_ = bit_cast<decltype(v_)>(0.0);
- }
- }
- Value Value::Clone() const {
- return CloningHelper::Clone(data_);
- }
- Value::~Value() = default;
- // static
- const char* Value::GetTypeName(Value::Type type) {
- DCHECK_GE(static_cast<int>(type), 0);
- DCHECK_LT(static_cast<size_t>(type), std::size(kTypeNames));
- return kTypeNames[static_cast<size_t>(type)];
- }
- absl::optional<bool> Value::GetIfBool() const {
- return is_bool() ? absl::make_optional(GetBool()) : absl::nullopt;
- }
- absl::optional<int> Value::GetIfInt() const {
- return is_int() ? absl::make_optional(GetInt()) : absl::nullopt;
- }
- absl::optional<double> Value::GetIfDouble() const {
- return (is_int() || is_double()) ? absl::make_optional(GetDouble())
- : absl::nullopt;
- }
- const std::string* Value::GetIfString() const {
- return absl::get_if<std::string>(&data_);
- }
- std::string* Value::GetIfString() {
- return absl::get_if<std::string>(&data_);
- }
- const Value::BlobStorage* Value::GetIfBlob() const {
- return absl::get_if<BlobStorage>(&data_);
- }
- const Value::Dict* Value::GetIfDict() const {
- return absl::get_if<Dict>(&data_);
- }
- Value::Dict* Value::GetIfDict() {
- return absl::get_if<Dict>(&data_);
- }
- const Value::List* Value::GetIfList() const {
- return absl::get_if<List>(&data_);
- }
- Value::List* Value::GetIfList() {
- return absl::get_if<List>(&data_);
- }
- bool Value::GetBool() const {
- DCHECK(is_bool());
- return absl::get<bool>(data_);
- }
- int Value::GetInt() const {
- DCHECK(is_int());
- return absl::get<int>(data_);
- }
- double Value::GetDouble() const {
- if (is_double())
- return absl::get<DoubleStorage>(data_);
- if (is_int())
- return GetInt();
- CHECK(false);
- return 0.0;
- }
- const std::string& Value::GetString() const {
- DCHECK(is_string());
- return absl::get<std::string>(data_);
- }
- std::string& Value::GetString() {
- DCHECK(is_string());
- return absl::get<std::string>(data_);
- }
- const Value::BlobStorage& Value::GetBlob() const {
- DCHECK(is_blob());
- return absl::get<BlobStorage>(data_);
- }
- const Value::Dict& Value::GetDict() const {
- DCHECK(is_dict());
- return absl::get<Dict>(data_);
- }
- Value::Dict& Value::GetDict() {
- DCHECK(is_dict());
- return absl::get<Dict>(data_);
- }
- const Value::List& Value::GetList() const {
- DCHECK(is_list());
- return absl::get<List>(data_);
- }
- Value::List& Value::GetList() {
- DCHECK(is_list());
- return absl::get<List>(data_);
- }
- Value::Dict::Dict() = default;
- Value::Dict::Dict(Dict&&) noexcept = default;
- Value::Dict& Value::Dict::operator=(Dict&&) noexcept = default;
- Value::Dict::~Dict() = default;
- bool Value::Dict::empty() const {
- return storage_.empty();
- }
- size_t Value::Dict::size() const {
- return storage_.size();
- }
- Value::Dict::iterator Value::Dict::begin() {
- return iterator(storage_.begin());
- }
- Value::Dict::const_iterator Value::Dict::begin() const {
- return const_iterator(storage_.begin());
- }
- Value::Dict::const_iterator Value::Dict::cbegin() const {
- return const_iterator(storage_.cbegin());
- }
- Value::Dict::iterator Value::Dict::end() {
- return iterator(storage_.end());
- }
- Value::Dict::const_iterator Value::Dict::end() const {
- return const_iterator(storage_.end());
- }
- Value::Dict::const_iterator Value::Dict::cend() const {
- return const_iterator(storage_.cend());
- }
- bool Value::Dict::contains(base::StringPiece key) const {
- DCHECK(IsStringUTF8AllowingNoncharacters(key));
- return storage_.contains(key);
- }
- void Value::Dict::clear() {
- return storage_.clear();
- }
- Value::Dict::iterator Value::Dict::erase(iterator pos) {
- return iterator(storage_.erase(pos.GetUnderlyingIteratorDoNotUse()));
- }
- Value::Dict::iterator Value::Dict::erase(const_iterator pos) {
- return iterator(storage_.erase(pos.GetUnderlyingIteratorDoNotUse()));
- }
- Value::Dict Value::Dict::Clone() const {
- return Dict(storage_);
- }
- void Value::Dict::Merge(Dict dict) {
- for (const auto [key, value] : dict) {
- if (Dict* nested_dict = value.GetIfDict()) {
- if (Dict* current_dict = FindDict(key)) {
- // If `key` is a nested dictionary in this dictionary and the dictionary
- // being merged, recursively merge the two dictionaries.
- current_dict->Merge(std::move(*nested_dict));
- continue;
- }
- }
- // Otherwise, unconditionally set the value, overwriting any value that may
- // already be associated with the key.
- Set(key, std::move(value));
- }
- }
- const Value* Value::Dict::Find(StringPiece key) const {
- DCHECK(IsStringUTF8AllowingNoncharacters(key));
- auto it = storage_.find(key);
- return it != storage_.end() ? it->second.get() : nullptr;
- }
- Value* Value::Dict::Find(StringPiece key) {
- auto it = storage_.find(key);
- return it != storage_.end() ? it->second.get() : nullptr;
- }
- absl::optional<bool> Value::Dict::FindBool(StringPiece key) const {
- const Value* v = Find(key);
- return v ? v->GetIfBool() : absl::nullopt;
- }
- absl::optional<int> Value::Dict::FindInt(StringPiece key) const {
- const Value* v = Find(key);
- return v ? v->GetIfInt() : absl::nullopt;
- }
- absl::optional<double> Value::Dict::FindDouble(StringPiece key) const {
- const Value* v = Find(key);
- return v ? v->GetIfDouble() : absl::nullopt;
- }
- const std::string* Value::Dict::FindString(StringPiece key) const {
- const Value* v = Find(key);
- return v ? v->GetIfString() : nullptr;
- }
- std::string* Value::Dict::FindString(StringPiece key) {
- Value* v = Find(key);
- return v ? v->GetIfString() : nullptr;
- }
- const Value::BlobStorage* Value::Dict::FindBlob(StringPiece key) const {
- const Value* v = Find(key);
- return v ? v->GetIfBlob() : nullptr;
- }
- const Value::Dict* Value::Dict::FindDict(StringPiece key) const {
- const Value* v = Find(key);
- return v ? v->GetIfDict() : nullptr;
- }
- Value::Dict* Value::Dict::FindDict(StringPiece key) {
- Value* v = Find(key);
- return v ? v->GetIfDict() : nullptr;
- }
- const Value::List* Value::Dict::FindList(StringPiece key) const {
- const Value* v = Find(key);
- return v ? v->GetIfList() : nullptr;
- }
- Value::List* Value::Dict::FindList(StringPiece key) {
- Value* v = Find(key);
- return v ? v->GetIfList() : nullptr;
- }
- Value::Dict* Value::Dict::EnsureDict(StringPiece key) {
- Value::Dict* dict = FindDict(key);
- if (dict)
- return dict;
- return &Set(key, base::Value::Dict())->GetDict();
- }
- Value::List* Value::Dict::EnsureList(StringPiece key) {
- Value::List* list = FindList(key);
- if (list)
- return list;
- return &Set(key, base::Value::List())->GetList();
- }
- Value* Value::Dict::Set(StringPiece key, Value&& value) {
- DCHECK(IsStringUTF8AllowingNoncharacters(key));
- auto wrapped_value = std::make_unique<Value>(std::move(value));
- auto* raw_value = wrapped_value.get();
- storage_.insert_or_assign(key, std::move(wrapped_value));
- return raw_value;
- }
- Value* Value::Dict::Set(StringPiece key, bool value) {
- return Set(key, Value(value));
- }
- Value* Value::Dict::Set(StringPiece key, int value) {
- return Set(key, Value(value));
- }
- Value* Value::Dict::Set(StringPiece key, double value) {
- return Set(key, Value(value));
- }
- Value* Value::Dict::Set(StringPiece key, StringPiece value) {
- return Set(key, Value(value));
- }
- Value* Value::Dict::Set(StringPiece key, StringPiece16 value) {
- return Set(key, Value(value));
- }
- Value* Value::Dict::Set(StringPiece key, const char* value) {
- return Set(key, Value(value));
- }
- Value* Value::Dict::Set(StringPiece key, const char16_t* value) {
- return Set(key, Value(value));
- }
- Value* Value::Dict::Set(StringPiece key, std::string&& value) {
- return Set(key, Value(std::move(value)));
- }
- Value* Value::Dict::Set(StringPiece key, BlobStorage&& value) {
- return Set(key, Value(std::move(value)));
- }
- Value* Value::Dict::Set(StringPiece key, Dict&& value) {
- return Set(key, Value(std::move(value)));
- }
- Value* Value::Dict::Set(StringPiece key, List&& value) {
- return Set(key, Value(std::move(value)));
- }
- bool Value::Dict::Remove(StringPiece key) {
- DCHECK(IsStringUTF8AllowingNoncharacters(key));
- return storage_.erase(key) > 0;
- }
- absl::optional<Value> Value::Dict::Extract(StringPiece key) {
- DCHECK(IsStringUTF8AllowingNoncharacters(key));
- auto it = storage_.find(key);
- if (it == storage_.end())
- return absl::nullopt;
- Value v = std::move(*it->second);
- storage_.erase(it);
- return v;
- }
- const Value* Value::Dict::FindByDottedPath(StringPiece path) const {
- DCHECK(!path.empty());
- DCHECK(IsStringUTF8AllowingNoncharacters(path));
- const Dict* current_dict = this;
- const Value* current_value = nullptr;
- PathSplitter splitter(path);
- while (true) {
- current_value = current_dict->Find(splitter.Next());
- if (!splitter.HasNext()) {
- return current_value;
- }
- if (!current_value) {
- return nullptr;
- }
- current_dict = current_value->GetIfDict();
- if (!current_dict) {
- return nullptr;
- }
- }
- }
- Value* Value::Dict::FindByDottedPath(StringPiece path) {
- return const_cast<Value*>(as_const(*this).FindByDottedPath(path));
- }
- absl::optional<bool> Value::Dict::FindBoolByDottedPath(StringPiece path) const {
- const Value* v = FindByDottedPath(path);
- return v ? v->GetIfBool() : absl::nullopt;
- }
- absl::optional<int> Value::Dict::FindIntByDottedPath(StringPiece path) const {
- const Value* v = FindByDottedPath(path);
- return v ? v->GetIfInt() : absl::nullopt;
- }
- absl::optional<double> Value::Dict::FindDoubleByDottedPath(
- StringPiece path) const {
- const Value* v = FindByDottedPath(path);
- return v ? v->GetIfDouble() : absl::nullopt;
- }
- const std::string* Value::Dict::FindStringByDottedPath(StringPiece path) const {
- const Value* v = FindByDottedPath(path);
- return v ? v->GetIfString() : nullptr;
- }
- std::string* Value::Dict::FindStringByDottedPath(StringPiece path) {
- Value* v = FindByDottedPath(path);
- return v ? v->GetIfString() : nullptr;
- }
- const Value::BlobStorage* Value::Dict::FindBlobByDottedPath(
- StringPiece path) const {
- const Value* v = FindByDottedPath(path);
- return v ? v->GetIfBlob() : nullptr;
- }
- const Value::Dict* Value::Dict::FindDictByDottedPath(StringPiece path) const {
- const Value* v = FindByDottedPath(path);
- return v ? v->GetIfDict() : nullptr;
- }
- Value::Dict* Value::Dict::FindDictByDottedPath(StringPiece path) {
- Value* v = FindByDottedPath(path);
- return v ? v->GetIfDict() : nullptr;
- }
- const Value::List* Value::Dict::FindListByDottedPath(StringPiece path) const {
- const Value* v = FindByDottedPath(path);
- return v ? v->GetIfList() : nullptr;
- }
- Value::List* Value::Dict::FindListByDottedPath(StringPiece path) {
- Value* v = FindByDottedPath(path);
- return v ? v->GetIfList() : nullptr;
- }
- Value* Value::Dict::SetByDottedPath(StringPiece path, Value&& value) {
- DCHECK(!path.empty());
- DCHECK(IsStringUTF8AllowingNoncharacters(path));
- Dict* current_dict = this;
- Value* current_value = nullptr;
- PathSplitter splitter(path);
- while (true) {
- StringPiece next_key = splitter.Next();
- if (!splitter.HasNext()) {
- return current_dict->Set(next_key, std::move(value));
- }
- // This could be clever to avoid a double-lookup via use of lower_bound(),
- // but for now, just implement it the most straightforward way.
- current_value = current_dict->Find(next_key);
- if (current_value) {
- // Unlike the legacy DictionaryValue API, encountering an intermediate
- // node that is not a `Value::Type::DICT` is an error.
- current_dict = current_value->GetIfDict();
- if (!current_dict) {
- return nullptr;
- }
- } else {
- current_dict = ¤t_dict->Set(next_key, Dict())->GetDict();
- }
- }
- }
- Value* Value::Dict::SetByDottedPath(StringPiece path, bool value) {
- return SetByDottedPath(path, Value(value));
- }
- Value* Value::Dict::SetByDottedPath(StringPiece path, int value) {
- return SetByDottedPath(path, Value(value));
- }
- Value* Value::Dict::SetByDottedPath(StringPiece path, double value) {
- return SetByDottedPath(path, Value(value));
- }
- Value* Value::Dict::SetByDottedPath(StringPiece path, StringPiece value) {
- return SetByDottedPath(path, Value(value));
- }
- Value* Value::Dict::SetByDottedPath(StringPiece path, StringPiece16 value) {
- return SetByDottedPath(path, Value(value));
- }
- Value* Value::Dict::SetByDottedPath(StringPiece path, const char* value) {
- return SetByDottedPath(path, Value(value));
- }
- Value* Value::Dict::SetByDottedPath(StringPiece path, const char16_t* value) {
- return SetByDottedPath(path, Value(value));
- }
- Value* Value::Dict::SetByDottedPath(StringPiece path, std::string&& value) {
- return SetByDottedPath(path, Value(std::move(value)));
- }
- Value* Value::Dict::SetByDottedPath(StringPiece path, BlobStorage&& value) {
- return SetByDottedPath(path, Value(std::move(value)));
- }
- Value* Value::Dict::SetByDottedPath(StringPiece path, Dict&& value) {
- return SetByDottedPath(path, Value(std::move(value)));
- }
- Value* Value::Dict::SetByDottedPath(StringPiece path, List&& value) {
- return SetByDottedPath(path, Value(std::move(value)));
- }
- bool Value::Dict::RemoveByDottedPath(StringPiece path) {
- return ExtractByDottedPath(path).has_value();
- }
- absl::optional<Value> Value::Dict::ExtractByDottedPath(StringPiece path) {
- DCHECK(!path.empty());
- DCHECK(IsStringUTF8AllowingNoncharacters(path));
- // Use recursion instead of PathSplitter here, as it simplifies code for
- // removing dictionaries that become empty if a value matching `path` is
- // extracted.
- size_t dot_index = path.find('.');
- if (dot_index == StringPiece::npos) {
- return Extract(path);
- }
- // This could be clever to avoid a double-lookup by using storage_ directly,
- // but for now, just implement it in the most straightforward way.
- StringPiece next_key = path.substr(0, dot_index);
- auto* next_dict = FindDict(next_key);
- if (!next_dict) {
- return absl::nullopt;
- }
- absl::optional<Value> extracted =
- next_dict->ExtractByDottedPath(path.substr(dot_index + 1));
- if (extracted && next_dict->empty()) {
- Remove(next_key);
- }
- return extracted;
- }
- std::string Value::Dict::DebugString() const {
- return DebugStringImpl(*this);
- }
- #if BUILDFLAG(ENABLE_BASE_TRACING)
- void Value::Dict::WriteIntoTrace(perfetto::TracedValue context) const {
- perfetto::TracedDictionary dict = std::move(context).WriteDictionary();
- for (auto kv : *this) {
- dict.Add(perfetto::DynamicString(kv.first), kv.second);
- }
- }
- #endif // BUILDFLAG(ENABLE_BASE_TRACING)
- Value::Dict::Dict(
- const flat_map<std::string, std::unique_ptr<Value>>& storage) {
- storage_.reserve(storage.size());
- for (const auto& [key, value] : storage) {
- Set(key, value->Clone());
- }
- }
- bool operator==(const Value::Dict& lhs, const Value::Dict& rhs) {
- auto deref_2nd = [](const auto& p) { return std::tie(p.first, *p.second); };
- return ranges::equal(lhs.storage_, rhs.storage_, {}, deref_2nd, deref_2nd);
- }
- bool operator!=(const Value::Dict& lhs, const Value::Dict& rhs) {
- return !(lhs == rhs);
- }
- bool operator<(const Value::Dict& lhs, const Value::Dict& rhs) {
- auto deref_2nd = [](const auto& p) { return std::tie(p.first, *p.second); };
- return ranges::lexicographical_compare(lhs.storage_, rhs.storage_, {},
- deref_2nd, deref_2nd);
- }
- bool operator>(const Value::Dict& lhs, const Value::Dict& rhs) {
- return rhs < lhs;
- }
- bool operator<=(const Value::Dict& lhs, const Value::Dict& rhs) {
- return !(rhs < lhs);
- }
- bool operator>=(const Value::Dict& lhs, const Value::Dict& rhs) {
- return !(lhs < rhs);
- }
- Value::List::List() = default;
- Value::List::List(List&&) noexcept = default;
- Value::List& Value::List::operator=(List&&) noexcept = default;
- Value::List::~List() = default;
- bool Value::List::empty() const {
- return storage_.empty();
- }
- size_t Value::List::size() const {
- return storage_.size();
- }
- Value::List::iterator Value::List::begin() {
- return iterator(base::to_address(storage_.begin()),
- base::to_address(storage_.end()));
- }
- Value::List::const_iterator Value::List::begin() const {
- return const_iterator(base::to_address(storage_.begin()),
- base::to_address(storage_.end()));
- }
- Value::List::const_iterator Value::List::cbegin() const {
- return const_iterator(base::to_address(storage_.cbegin()),
- base::to_address(storage_.cend()));
- }
- Value::List::iterator Value::List::end() {
- return iterator(base::to_address(storage_.begin()),
- base::to_address(storage_.end()),
- base::to_address(storage_.end()));
- }
- Value::List::const_iterator Value::List::end() const {
- return const_iterator(base::to_address(storage_.begin()),
- base::to_address(storage_.end()),
- base::to_address(storage_.end()));
- }
- Value::List::const_iterator Value::List::cend() const {
- return const_iterator(base::to_address(storage_.cbegin()),
- base::to_address(storage_.cend()),
- base::to_address(storage_.cend()));
- }
- const Value& Value::List::front() const {
- CHECK(!storage_.empty());
- return storage_.front();
- }
- Value& Value::List::front() {
- CHECK(!storage_.empty());
- return storage_.front();
- }
- const Value& Value::List::back() const {
- CHECK(!storage_.empty());
- return storage_.back();
- }
- Value& Value::List::back() {
- CHECK(!storage_.empty());
- return storage_.back();
- }
- void Value::List::reserve(size_t capacity) {
- storage_.reserve(capacity);
- }
- const Value& Value::List::operator[](size_t index) const {
- CHECK_LT(index, storage_.size());
- return storage_[index];
- }
- Value& Value::List::operator[](size_t index) {
- CHECK_LT(index, storage_.size());
- return storage_[index];
- }
- void Value::List::clear() {
- storage_.clear();
- }
- Value::List::iterator Value::List::erase(iterator pos) {
- auto next_it = storage_.erase(storage_.begin() + (pos - begin()));
- return iterator(base::to_address(storage_.begin()), base::to_address(next_it),
- base::to_address(storage_.end()));
- }
- Value::List::const_iterator Value::List::erase(const_iterator pos) {
- auto next_it = storage_.erase(storage_.begin() + (pos - begin()));
- return const_iterator(base::to_address(storage_.begin()),
- base::to_address(next_it),
- base::to_address(storage_.end()));
- }
- Value::List::iterator Value::List::erase(iterator first, iterator last) {
- auto next_it = storage_.erase(storage_.begin() + (first - begin()),
- storage_.begin() + (last - begin()));
- return iterator(base::to_address(storage_.begin()), base::to_address(next_it),
- base::to_address(storage_.end()));
- }
- Value::List::const_iterator Value::List::erase(const_iterator first,
- const_iterator last) {
- auto next_it = storage_.erase(storage_.begin() + (first - begin()),
- storage_.begin() + (last - begin()));
- return const_iterator(base::to_address(storage_.begin()),
- base::to_address(next_it),
- base::to_address(storage_.end()));
- }
- Value::List Value::List::Clone() const {
- return List(storage_);
- }
- void Value::List::Append(Value&& value) {
- storage_.emplace_back(std::move(value));
- }
- void Value::List::Append(bool value) {
- storage_.emplace_back(value);
- }
- void Value::List::Append(int value) {
- storage_.emplace_back(value);
- }
- void Value::List::Append(double value) {
- storage_.emplace_back(value);
- }
- void Value::List::Append(StringPiece value) {
- Append(Value(value));
- }
- void Value::List::Append(StringPiece16 value) {
- storage_.emplace_back(value);
- }
- void Value::List::Append(const char* value) {
- storage_.emplace_back(value);
- }
- void Value::List::Append(const char16_t* value) {
- storage_.emplace_back(value);
- }
- void Value::List::Append(std::string&& value) {
- storage_.emplace_back(std::move(value));
- }
- void Value::List::Append(BlobStorage&& value) {
- storage_.emplace_back(std::move(value));
- }
- void Value::List::Append(Dict&& value) {
- storage_.emplace_back(std::move(value));
- }
- void Value::List::Append(List&& value) {
- storage_.emplace_back(std::move(value));
- }
- Value::List::iterator Value::List::Insert(const_iterator pos, Value&& value) {
- auto inserted_it =
- storage_.insert(storage_.begin() + (pos - begin()), std::move(value));
- return iterator(base::to_address(storage_.begin()),
- base::to_address(inserted_it),
- base::to_address(storage_.end()));
- }
- size_t Value::List::EraseValue(const Value& value) {
- return Erase(storage_, value);
- }
- std::string Value::List::DebugString() const {
- return DebugStringImpl(*this);
- }
- #if BUILDFLAG(ENABLE_BASE_TRACING)
- void Value::List::WriteIntoTrace(perfetto::TracedValue context) const {
- perfetto::TracedArray array = std::move(context).WriteArray();
- for (const auto& item : *this) {
- array.Append(item);
- }
- }
- #endif // BUILDFLAG(ENABLE_BASE_TRACING)
- Value::List::List(const std::vector<Value>& storage) {
- storage_.reserve(storage.size());
- for (const auto& value : storage) {
- storage_.push_back(value.Clone());
- }
- }
- bool operator==(const Value::List& lhs, const Value::List& rhs) {
- return lhs.storage_ == rhs.storage_;
- }
- bool operator!=(const Value::List& lhs, const Value::List& rhs) {
- return !(lhs == rhs);
- }
- bool operator<(const Value::List& lhs, const Value::List& rhs) {
- return lhs.storage_ < rhs.storage_;
- }
- bool operator>(const Value::List& lhs, const Value::List& rhs) {
- return rhs < lhs;
- }
- bool operator<=(const Value::List& lhs, const Value::List& rhs) {
- return !(rhs < lhs);
- }
- bool operator>=(const Value::List& lhs, const Value::List& rhs) {
- return !(lhs < rhs);
- }
- Value::ListView Value::GetListDeprecated() {
- return list();
- }
- Value::ConstListView Value::GetListDeprecated() const {
- return list();
- }
- void Value::Append(bool value) {
- GetList().Append(value);
- }
- void Value::Append(int value) {
- GetList().Append(value);
- }
- void Value::Append(double value) {
- GetList().Append(value);
- }
- void Value::Append(const char* value) {
- GetList().Append(value);
- }
- void Value::Append(StringPiece value) {
- GetList().Append(value);
- }
- void Value::Append(std::string&& value) {
- GetList().Append(std::move(value));
- }
- void Value::Append(StringPiece16 value) {
- GetList().Append(value);
- }
- void Value::Append(Value&& value) {
- GetList().Append(std::move(value));
- }
- bool Value::EraseListIter(CheckedContiguousConstIterator<Value> iter) {
- const auto offset = iter - ListView(list()).begin();
- auto list_iter = list().begin() + offset;
- if (list_iter == list().end())
- return false;
- list().erase(list_iter);
- return true;
- }
- size_t Value::EraseListValue(const Value& val) {
- return GetList().EraseValue(val);
- }
- void Value::ClearList() {
- GetList().clear();
- }
- Value* Value::FindKey(StringPiece key) {
- return GetDict().Find(key);
- }
- const Value* Value::FindKey(StringPiece key) const {
- return GetDict().Find(key);
- }
- Value* Value::FindKeyOfType(StringPiece key, Type type) {
- return const_cast<Value*>(as_const(*this).FindKeyOfType(key, type));
- }
- const Value* Value::FindKeyOfType(StringPiece key, Type type) const {
- const Value* result = FindKey(key);
- if (!result || result->type() != type)
- return nullptr;
- return result;
- }
- absl::optional<bool> Value::FindBoolKey(StringPiece key) const {
- return GetDict().FindBool(key);
- }
- absl::optional<int> Value::FindIntKey(StringPiece key) const {
- return GetDict().FindInt(key);
- }
- absl::optional<double> Value::FindDoubleKey(StringPiece key) const {
- return GetDict().FindDouble(key);
- }
- const std::string* Value::FindStringKey(StringPiece key) const {
- return GetDict().FindString(key);
- }
- std::string* Value::FindStringKey(StringPiece key) {
- return GetDict().FindString(key);
- }
- const Value::BlobStorage* Value::FindBlobKey(StringPiece key) const {
- return GetDict().FindBlob(key);
- }
- const Value* Value::FindDictKey(StringPiece key) const {
- return FindKeyOfType(key, Type::DICTIONARY);
- }
- Value* Value::FindDictKey(StringPiece key) {
- return FindKeyOfType(key, Type::DICTIONARY);
- }
- const Value* Value::FindListKey(StringPiece key) const {
- return FindKeyOfType(key, Type::LIST);
- }
- Value* Value::FindListKey(StringPiece key) {
- return FindKeyOfType(key, Type::LIST);
- }
- Value* Value::SetKey(StringPiece key, Value&& value) {
- return GetDict().Set(key, std::move(value));
- }
- Value* Value::SetBoolKey(StringPiece key, bool value) {
- return GetDict().Set(key, value);
- }
- Value* Value::SetIntKey(StringPiece key, int value) {
- return GetDict().Set(key, value);
- }
- Value* Value::SetDoubleKey(StringPiece key, double value) {
- return GetDict().Set(key, value);
- }
- Value* Value::SetStringKey(StringPiece key, StringPiece value) {
- return GetDict().Set(key, value);
- }
- Value* Value::SetStringKey(StringPiece key, StringPiece16 value) {
- return GetDict().Set(key, value);
- }
- Value* Value::SetStringKey(StringPiece key, const char* value) {
- return GetDict().Set(key, value);
- }
- Value* Value::SetStringKey(StringPiece key, std::string&& value) {
- return GetDict().Set(key, std::move(value));
- }
- bool Value::RemoveKey(StringPiece key) {
- return GetDict().Remove(key);
- }
- absl::optional<Value> Value::ExtractKey(StringPiece key) {
- return GetDict().Extract(key);
- }
- Value* Value::FindPath(StringPiece path) {
- return GetDict().FindByDottedPath(path);
- }
- const Value* Value::FindPath(StringPiece path) const {
- return GetDict().FindByDottedPath(path);
- }
- Value* Value::FindPathOfType(StringPiece path, Type type) {
- return const_cast<Value*>(as_const(*this).FindPathOfType(path, type));
- }
- const Value* Value::FindPathOfType(StringPiece path, Type type) const {
- const Value* cur = FindPath(path);
- if (!cur || cur->type() != type)
- return nullptr;
- return cur;
- }
- absl::optional<bool> Value::FindBoolPath(StringPiece path) const {
- return GetDict().FindBoolByDottedPath(path);
- }
- absl::optional<int> Value::FindIntPath(StringPiece path) const {
- return GetDict().FindIntByDottedPath(path);
- }
- absl::optional<double> Value::FindDoublePath(StringPiece path) const {
- return GetDict().FindDoubleByDottedPath(path);
- }
- const std::string* Value::FindStringPath(StringPiece path) const {
- return GetDict().FindStringByDottedPath(path);
- }
- std::string* Value::FindStringPath(StringPiece path) {
- return GetDict().FindStringByDottedPath(path);
- }
- const Value* Value::FindDictPath(StringPiece path) const {
- return FindPathOfType(path, Type::DICTIONARY);
- }
- Value* Value::FindDictPath(StringPiece path) {
- return FindPathOfType(path, Type::DICTIONARY);
- }
- const Value* Value::FindListPath(StringPiece path) const {
- return FindPathOfType(path, Type::LIST);
- }
- Value* Value::FindListPath(StringPiece path) {
- return FindPathOfType(path, Type::LIST);
- }
- Value* Value::SetPath(StringPiece path, Value&& value) {
- return GetDict().SetByDottedPath(path, std::move(value));
- }
- Value* Value::SetBoolPath(StringPiece path, bool value) {
- return GetDict().SetByDottedPath(path, value);
- }
- Value* Value::SetIntPath(StringPiece path, int value) {
- return GetDict().SetByDottedPath(path, value);
- }
- Value* Value::SetDoublePath(StringPiece path, double value) {
- return GetDict().SetByDottedPath(path, value);
- }
- Value* Value::SetStringPath(StringPiece path, StringPiece value) {
- return GetDict().SetByDottedPath(path, value);
- }
- Value* Value::SetStringPath(StringPiece path, std::string&& value) {
- return GetDict().SetByDottedPath(path, std::move(value));
- }
- Value* Value::SetStringPath(StringPiece path, const char* value) {
- return GetDict().SetByDottedPath(path, value);
- }
- Value* Value::SetStringPath(StringPiece path, StringPiece16 value) {
- return GetDict().SetByDottedPath(path, value);
- }
- bool Value::RemovePath(StringPiece path) {
- return GetDict().RemoveByDottedPath(path);
- }
- absl::optional<Value> Value::ExtractPath(StringPiece path) {
- return GetDict().ExtractByDottedPath(path);
- }
- // DEPRECATED METHODS
- Value* Value::FindPath(std::initializer_list<StringPiece> path) {
- return const_cast<Value*>(as_const(*this).FindPath(path));
- }
- Value* Value::FindPath(span<const StringPiece> path) {
- return const_cast<Value*>(as_const(*this).FindPath(path));
- }
- const Value* Value::FindPath(std::initializer_list<StringPiece> path) const {
- DCHECK_GE(path.size(), 2u) << "Use FindKey() for a path of length 1.";
- return FindPath(make_span(path.begin(), path.size()));
- }
- const Value* Value::FindPath(span<const StringPiece> path) const {
- const Value* cur = this;
- for (const StringPiece& component : path) {
- if (!cur->is_dict() || (cur = cur->FindKey(component)) == nullptr)
- return nullptr;
- }
- return cur;
- }
- Value* Value::FindPathOfType(std::initializer_list<StringPiece> path,
- Type type) {
- return const_cast<Value*>(as_const(*this).FindPathOfType(path, type));
- }
- Value* Value::FindPathOfType(span<const StringPiece> path, Type type) {
- return const_cast<Value*>(as_const(*this).FindPathOfType(path, type));
- }
- const Value* Value::FindPathOfType(std::initializer_list<StringPiece> path,
- Type type) const {
- DCHECK_GE(path.size(), 2u) << "Use FindKeyOfType() for a path of length 1.";
- return FindPathOfType(make_span(path.begin(), path.size()), type);
- }
- const Value* Value::FindPathOfType(span<const StringPiece> path,
- Type type) const {
- const Value* result = FindPath(path);
- if (!result || result->type() != type)
- return nullptr;
- return result;
- }
- Value* Value::SetPath(std::initializer_list<StringPiece> path, Value&& value) {
- DCHECK_GE(path.size(), 2u) << "Use SetKey() for a path of length 1.";
- return SetPath(make_span(path.begin(), path.size()), std::move(value));
- }
- Value* Value::SetPath(span<const StringPiece> path, Value&& value) {
- DCHECK(path.begin() != path.end()); // Can't be empty path.
- // Walk/construct intermediate dictionaries. The last element requires
- // special handling so skip it in this loop.
- Value* cur = this;
- auto cur_path = path.begin();
- for (; (cur_path + 1) < path.end(); ++cur_path) {
- if (!cur->is_dict())
- return nullptr;
- // Use lower_bound to avoid doing the search twice for missing keys.
- const StringPiece path_component = *cur_path;
- auto found = cur->dict().lower_bound(path_component);
- if (found == cur->dict().end() || found->first != path_component) {
- // No key found, insert one.
- auto inserted = cur->dict().try_emplace(
- found, path_component, std::make_unique<Value>(Type::DICTIONARY));
- cur = inserted->second.get();
- } else {
- cur = found->second.get();
- }
- }
- // "cur" will now contain the last dictionary to insert or replace into.
- if (!cur->is_dict())
- return nullptr;
- return cur->SetKey(*cur_path, std::move(value));
- }
- Value::dict_iterator_proxy Value::DictItems() {
- return dict_iterator_proxy(&dict());
- }
- Value::const_dict_iterator_proxy Value::DictItems() const {
- return const_dict_iterator_proxy(&dict());
- }
- size_t Value::DictSize() const {
- return GetDict().size();
- }
- bool Value::DictEmpty() const {
- return GetDict().empty();
- }
- void Value::DictClear() {
- GetDict().clear();
- }
- void Value::MergeDictionary(const Value* dictionary) {
- return GetDict().Merge(dictionary->GetDict().Clone());
- }
- bool Value::GetAsDictionary(DictionaryValue** out_value) {
- if (out_value && is_dict()) {
- *out_value = static_cast<DictionaryValue*>(this);
- return true;
- }
- return is_dict();
- }
- bool Value::GetAsDictionary(const DictionaryValue** out_value) const {
- if (out_value && is_dict()) {
- *out_value = static_cast<const DictionaryValue*>(this);
- return true;
- }
- return is_dict();
- }
- std::unique_ptr<Value> Value::CreateDeepCopy() const {
- return std::make_unique<Value>(Clone());
- }
- bool operator==(const Value& lhs, const Value& rhs) {
- return lhs.data_ == rhs.data_;
- }
- bool operator!=(const Value& lhs, const Value& rhs) {
- return !(lhs == rhs);
- }
- bool operator<(const Value& lhs, const Value& rhs) {
- return lhs.data_ < rhs.data_;
- }
- bool operator>(const Value& lhs, const Value& rhs) {
- return rhs < lhs;
- }
- bool operator<=(const Value& lhs, const Value& rhs) {
- return !(rhs < lhs);
- }
- bool operator>=(const Value& lhs, const Value& rhs) {
- return !(lhs < rhs);
- }
- bool operator==(const Value& lhs, bool rhs) {
- return lhs.is_bool() && lhs.GetBool() == rhs;
- }
- bool operator==(const Value& lhs, int rhs) {
- return lhs.is_int() && lhs.GetInt() == rhs;
- }
- bool operator==(const Value& lhs, double rhs) {
- return lhs.is_double() && lhs.GetDouble() == rhs;
- }
- bool operator==(const Value& lhs, StringPiece rhs) {
- return lhs.is_string() && lhs.GetString() == rhs;
- }
- bool operator==(const Value& lhs, const Value::Dict& rhs) {
- return lhs.is_dict() && lhs.GetDict() == rhs;
- }
- bool operator==(const Value& lhs, const Value::List& rhs) {
- return lhs.is_list() && lhs.GetList() == rhs;
- }
- size_t Value::EstimateMemoryUsage() const {
- switch (type()) {
- #if BUILDFLAG(ENABLE_BASE_TRACING)
- case Type::STRING:
- return base::trace_event::EstimateMemoryUsage(GetString());
- case Type::BINARY:
- return base::trace_event::EstimateMemoryUsage(GetBlob());
- case Type::DICTIONARY:
- return base::trace_event::EstimateMemoryUsage(dict());
- case Type::LIST:
- return base::trace_event::EstimateMemoryUsage(list());
- #endif // BUILDFLAG(ENABLE_BASE_TRACING)
- default:
- return 0;
- }
- }
- std::string Value::DebugString() const {
- return DebugStringImpl(*this);
- }
- #if BUILDFLAG(ENABLE_BASE_TRACING)
- void Value::WriteIntoTrace(perfetto::TracedValue context) const {
- Visit([&](const auto& member) {
- using T = std::decay_t<decltype(member)>;
- if constexpr (std::is_same_v<T, absl::monostate>) {
- std::move(context).WriteString("<none>");
- } else if constexpr (std::is_same_v<T, bool>) {
- std::move(context).WriteBoolean(member);
- } else if constexpr (std::is_same_v<T, int>) {
- std::move(context).WriteInt64(member);
- } else if constexpr (std::is_same_v<T, DoubleStorage>) {
- std::move(context).WriteDouble(member);
- } else if constexpr (std::is_same_v<T, std::string>) {
- std::move(context).WriteString(member);
- } else if constexpr (std::is_same_v<T, BlobStorage>) {
- std::move(context).WriteString("<binary data not supported>");
- } else if constexpr (std::is_same_v<T, Dict>) {
- member.WriteIntoTrace(std::move(context));
- } else if constexpr (std::is_same_v<T, List>) {
- member.WriteIntoTrace(std::move(context));
- }
- });
- }
- #endif // BUILDFLAG(ENABLE_BASE_TRACING)
- ///////////////////// DictionaryValue ////////////////////
- // static
- std::unique_ptr<DictionaryValue> DictionaryValue::From(
- std::unique_ptr<Value> value) {
- DictionaryValue* out;
- if (value && value->GetAsDictionary(&out)) {
- std::ignore = value.release();
- return WrapUnique(out);
- }
- return nullptr;
- }
- DictionaryValue::DictionaryValue() : Value(Type::DICTIONARY) {}
- DictionaryValue::DictionaryValue(const LegacyDictStorage& storage)
- : Value(storage) {}
- DictionaryValue::DictionaryValue(LegacyDictStorage&& storage) noexcept
- : Value(std::move(storage)) {}
- Value* DictionaryValue::Set(StringPiece path, std::unique_ptr<Value> in_value) {
- DCHECK(IsStringUTF8AllowingNoncharacters(path));
- DCHECK(in_value);
- // IMPORTANT NOTE: Do not replace with GetDict.SetByDottedPath() yet, because
- // the latter fails when over-writing a non-dict intermediate node, while this
- // method just replaces it with one. This difference makes some tests actually
- // fail (http://crbug.com/949461).
- StringPiece current_path(path);
- Value* current_dictionary = this;
- for (size_t delimiter_position = current_path.find('.');
- delimiter_position != StringPiece::npos;
- delimiter_position = current_path.find('.')) {
- // Assume that we're indexing into a dictionary.
- StringPiece key = current_path.substr(0, delimiter_position);
- Value* child_dictionary =
- current_dictionary->FindKeyOfType(key, Type::DICTIONARY);
- if (!child_dictionary) {
- child_dictionary =
- current_dictionary->SetKey(key, Value(Type::DICTIONARY));
- }
- current_dictionary = child_dictionary;
- current_path = current_path.substr(delimiter_position + 1);
- }
- return static_cast<DictionaryValue*>(current_dictionary)
- ->SetWithoutPathExpansion(current_path, std::move(in_value));
- }
- Value* DictionaryValue::SetBoolean(StringPiece path, bool in_value) {
- return Set(path, std::make_unique<Value>(in_value));
- }
- Value* DictionaryValue::SetInteger(StringPiece path, int in_value) {
- return Set(path, std::make_unique<Value>(in_value));
- }
- Value* DictionaryValue::SetString(StringPiece path, StringPiece in_value) {
- return Set(path, std::make_unique<Value>(in_value));
- }
- Value* DictionaryValue::SetString(StringPiece path,
- const std::u16string& in_value) {
- return Set(path, std::make_unique<Value>(in_value));
- }
- ListValue* DictionaryValue::SetList(StringPiece path,
- std::unique_ptr<ListValue> in_value) {
- return static_cast<ListValue*>(Set(path, std::move(in_value)));
- }
- Value* DictionaryValue::SetWithoutPathExpansion(
- StringPiece key,
- std::unique_ptr<Value> in_value) {
- // NOTE: We can't use |insert_or_assign| here, as only |try_emplace| does
- // an explicit conversion from StringPiece to std::string if necessary.
- auto result = dict().try_emplace(key, std::move(in_value));
- if (!result.second) {
- // in_value is guaranteed to be still intact at this point.
- result.first->second = std::move(in_value);
- }
- return result.first->second.get();
- }
- bool DictionaryValue::Get(StringPiece path, const Value** out_value) const {
- DCHECK(IsStringUTF8AllowingNoncharacters(path));
- const Value* value = FindPath(path);
- if (!value)
- return false;
- if (out_value)
- *out_value = value;
- return true;
- }
- bool DictionaryValue::Get(StringPiece path, Value** out_value) {
- return as_const(*this).Get(path, const_cast<const Value**>(out_value));
- }
- bool DictionaryValue::GetInteger(StringPiece path, int* out_value) const {
- const Value* value;
- if (!Get(path, &value))
- return false;
- bool is_int = value->is_int();
- if (is_int && out_value)
- *out_value = value->GetInt();
- return is_int;
- }
- bool DictionaryValue::GetString(StringPiece path,
- std::string* out_value) const {
- const Value* value;
- if (!Get(path, &value))
- return false;
- const bool is_string = value->is_string();
- if (is_string && out_value)
- *out_value = value->GetString();
- return is_string;
- }
- bool DictionaryValue::GetDictionary(StringPiece path,
- const DictionaryValue** out_value) const {
- const Value* value;
- bool result = Get(path, &value);
- if (!result || !value->is_dict())
- return false;
- if (out_value)
- *out_value = static_cast<const DictionaryValue*>(value);
- return true;
- }
- bool DictionaryValue::GetDictionary(StringPiece path,
- DictionaryValue** out_value) {
- return as_const(*this).GetDictionary(
- path, const_cast<const DictionaryValue**>(out_value));
- }
- bool DictionaryValue::GetList(StringPiece path,
- const ListValue** out_value) const {
- const Value* value;
- bool result = Get(path, &value);
- if (!result || !value->is_list())
- return false;
- if (out_value)
- *out_value = static_cast<const ListValue*>(value);
- return true;
- }
- bool DictionaryValue::GetList(StringPiece path, ListValue** out_value) {
- return as_const(*this).GetList(path,
- const_cast<const ListValue**>(out_value));
- }
- void DictionaryValue::Swap(DictionaryValue* other) {
- CHECK(other->is_dict());
- dict().swap(other->dict());
- }
- DictionaryValue::Iterator::Iterator(const DictionaryValue& target)
- : target_(target), it_(target.DictItems().begin()) {}
- DictionaryValue::Iterator::Iterator(const Iterator& other) = default;
- DictionaryValue::Iterator::~Iterator() = default;
- std::unique_ptr<DictionaryValue> DictionaryValue::CreateDeepCopy() const {
- return std::make_unique<DictionaryValue>(dict());
- }
- ///////////////////// ListValue ////////////////////
- // static
- std::unique_ptr<ListValue> ListValue::From(std::unique_ptr<Value> value) {
- if (value && value->is_list())
- return WrapUnique(static_cast<ListValue*>(value.release()));
- return nullptr;
- }
- ListValue::ListValue() : Value(Type::LIST) {}
- void ListValue::Append(base::Value::Dict in_dict) {
- list().emplace_back(std::move(in_dict));
- }
- void ListValue::Append(base::Value::List in_list) {
- list().emplace_back(std::move(in_list));
- }
- void ListValue::Swap(ListValue* other) {
- CHECK(other->is_list());
- list().swap(other->list());
- }
- ValueView::ValueView(const Value& value)
- : data_view_(
- value.Visit([](const auto& member) { return ViewType(member); })) {}
- Value ValueView::ToValue() const {
- return Value::CloningHelper::Clone(data_view_);
- }
- ValueSerializer::~ValueSerializer() = default;
- ValueDeserializer::~ValueDeserializer() = default;
- std::ostream& operator<<(std::ostream& out, const Value& value) {
- return out << value.DebugString();
- }
- std::ostream& operator<<(std::ostream& out, const Value::Dict& dict) {
- return out << dict.DebugString();
- }
- std::ostream& operator<<(std::ostream& out, const Value::List& list) {
- return out << list.DebugString();
- }
- std::ostream& operator<<(std::ostream& out, const Value::Type& type) {
- if (static_cast<int>(type) < 0 ||
- static_cast<size_t>(type) >= std::size(kTypeNames))
- return out << "Invalid Type (index = " << static_cast<int>(type) << ")";
- return out << Value::GetTypeName(type);
- }
- } // namespace base
|