diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2022-05-12 15:59:20 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2022-05-25 06:57:22 +0000 |
commit | f7eaed5286974984ba5f9e3189d8f49d03e99f81 (patch) | |
tree | caed19b2af2024f35449fb0b781d0a25e09d4f8f /chromium/third_party/inspector_protocol | |
parent | 9729c4479fe23554eae6e6dd1f30ff488f470c84 (diff) | |
download | qtwebengine-chromium-f7eaed5286974984ba5f9e3189d8f49d03e99f81.tar.gz |
BASELINE: Update Chromium to 100.0.4896.167
Change-Id: I98cbeb5d7543d966ffe04d8cefded0c493a11333
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/third_party/inspector_protocol')
17 files changed, 468 insertions, 236 deletions
diff --git a/chromium/third_party/inspector_protocol/BUILD.gn b/chromium/third_party/inspector_protocol/BUILD.gn index 34261a73c73..bcd256e6152 100644 --- a/chromium/third_party/inspector_protocol/BUILD.gn +++ b/chromium/third_party/inspector_protocol/BUILD.gn @@ -57,6 +57,7 @@ source_set("crdtp_test") { testonly = true sources = [ "crdtp/cbor_test.cc", + "crdtp/chromium/protocol_traits_test.cc", "crdtp/dispatch_test.cc", "crdtp/error_support_test.cc", "crdtp/find_by_first_test.cc", @@ -69,7 +70,10 @@ source_set("crdtp_test") { "crdtp/status_test_support.cc", "crdtp/status_test_support.h", ] - deps = [ ":crdtp_test_platform" ] + deps = [ + ":crdtp_test_platform", + "//base/test:test_support", + ] } # A small adapter library which only :crdtp_test may depend on. diff --git a/chromium/third_party/inspector_protocol/README.chromium b/chromium/third_party/inspector_protocol/README.chromium index 45b128e4531..7ce2bf83766 100644 --- a/chromium/third_party/inspector_protocol/README.chromium +++ b/chromium/third_party/inspector_protocol/README.chromium @@ -2,7 +2,7 @@ Name: inspector protocol Short Name: inspector_protocol URL: https://chromium.googlesource.com/deps/inspector_protocol/ Version: 0 -Revision: ef5648d5b28cc90afd32ca8f0576cd98b5c040bc +Revision: 201d4b51ed2ab22839939236aa909c6d7c0f5147 License: BSD License File: LICENSE Security Critical: yes diff --git a/chromium/third_party/inspector_protocol/code_generator.py b/chromium/third_party/inspector_protocol/code_generator.py index 11a93e76226..df76716f365 100755 --- a/chromium/third_party/inspector_protocol/code_generator.py +++ b/chromium/third_party/inspector_protocol/code_generator.py @@ -97,6 +97,7 @@ def read_config(): defaults = { ".use_snake_file_names": False, ".use_title_case_methods": False, + ".use_embedder_types": False, ".imported": False, ".imported.export_macro": "", ".imported.export_header": False, @@ -638,31 +639,32 @@ def main(): lib_templates_dir = os.path.join(module_path, "lib") # Note these should be sorted in the right order. - # TODO(dgozman): sort them programmatically based on commented includes. - protocol_h_templates = [ - "Values_h.template", - "Object_h.template", - "ValueConversions_h.template", - ] - protocol_cpp_templates = [ - "Protocol_cpp.template", - "Values_cpp.template", - "Object_cpp.template", - "ValueConversions_cpp.template", - ] + # TODO(dgozman): sort them programmatically based on commented includes. forward_h_templates = [ "Forward_h.template", ] - base_string_adapter_h_templates = [ - "base_string_adapter_h.template", - ] - - base_string_adapter_cc_templates = [ - "base_string_adapter_cc.template", - ] + protocol_h_templates = [] + protocol_cpp_templates = [] + + if not config.use_embedder_types: + protocol_h_templates += [ + "Values_h.template", + "Object_h.template", + "ValueConversions_h.template", + ] + protocol_cpp_templates += [ + "Protocol_cpp.template", + "Values_cpp.template", + "Object_cpp.template", + "ValueConversions_cpp.template", + ] + else: + protocol_h_templates += [ + "Forward_h.template", + ] def generate_lib_file(file_name, template_files): parts = [] @@ -676,12 +678,10 @@ def main(): config, "Forward.h")), forward_h_templates) generate_lib_file(os.path.join(config.lib.output, to_file_name( config, "Protocol.h")), protocol_h_templates) - generate_lib_file(os.path.join(config.lib.output, to_file_name( - config, "Protocol.cpp")), protocol_cpp_templates) - generate_lib_file(os.path.join(config.lib.output, to_file_name( - config, "base_string_adapter.h")), base_string_adapter_h_templates) - generate_lib_file(os.path.join(config.lib.output, to_file_name( - config, "base_string_adapter.cc")), base_string_adapter_cc_templates) + + if not config.use_embedder_types: + generate_lib_file(os.path.join(config.lib.output, to_file_name( + config, "Protocol.cpp")), protocol_cpp_templates) # Make gyp / make generatos happy, otherwise make rebuilds world. inputs_ts = max(map(os.path.getmtime, inputs)) diff --git a/chromium/third_party/inspector_protocol/convert_protocol_to_json.py b/chromium/third_party/inspector_protocol/convert_protocol_to_json.py index f98bebcd5e6..7070a80a460 100755 --- a/chromium/third_party/inspector_protocol/convert_protocol_to_json.py +++ b/chromium/third_party/inspector_protocol/convert_protocol_to_json.py @@ -10,6 +10,13 @@ import sys import pdl +def open_to_write(path): + if sys.version_info >= (3,0): + return open(path, 'w', encoding='utf-8') + else: + return open(path, 'wb') + + def main(argv): parser = argparse.ArgumentParser(description=( "Converts from .pdl to .json by invoking the pdl Python module.")) @@ -25,8 +32,7 @@ def main(argv): pdl_string = input_file.read() protocol = pdl.loads(pdl_string, file_name, args.map_binary_to_string) input_file.close() - - output_file = open(os.path.normpath(args.json_file), 'wb') + output_file = open_to_write(os.path.normpath(args.json_file)) json.dump(protocol, output_file, indent=4, separators=(',', ': ')) output_file.close() diff --git a/chromium/third_party/inspector_protocol/crdtp/README.md b/chromium/third_party/inspector_protocol/crdtp/README.md index 81957e1692e..a9bbc9e8865 100644 --- a/chromium/third_party/inspector_protocol/crdtp/README.md +++ b/chromium/third_party/inspector_protocol/crdtp/README.md @@ -1,6 +1,6 @@ # CRDTP - Chrome DevTools Protocol Library. -[Canonical location for this library.](https://chromium.googlesource.com/deps/inspector_protocol/+/refs/heads/master) +[Canonical location for this library.](https://chromium.googlesource.com/deps/inspector_protocol/+/refs/heads/main) This is a support library for the Chrome DevTools protocol implementation. diff --git a/chromium/third_party/inspector_protocol/crdtp/cbor.cc b/chromium/third_party/inspector_protocol/crdtp/cbor.cc index c384364dfde..794e9474746 100644 --- a/chromium/third_party/inspector_protocol/crdtp/cbor.cc +++ b/chromium/third_party/inspector_protocol/crdtp/cbor.cc @@ -595,7 +595,7 @@ span<uint8_t> CBORTokenizer::GetEnvelopeContents() const { // and then checking whether the sum went past it. // // See also -// https://chromium.googlesource.com/chromium/src/+/master/docs/security/integer-semantics.md +// https://chromium.googlesource.com/chromium/src/+/main/docs/security/integer-semantics.md static const uint64_t kMaxValidLength = std::min<uint64_t>(std::numeric_limits<uint64_t>::max() >> 2, std::numeric_limits<size_t>::max()); diff --git a/chromium/third_party/inspector_protocol/crdtp/chromium/protocol_traits.cc b/chromium/third_party/inspector_protocol/crdtp/chromium/protocol_traits.cc index 3f9e4f55772..8beafae4d05 100644 --- a/chromium/third_party/inspector_protocol/crdtp/chromium/protocol_traits.cc +++ b/chromium/third_party/inspector_protocol/crdtp/chromium/protocol_traits.cc @@ -3,6 +3,7 @@ #include <utility> #include "base/base64.h" #include "base/memory/ptr_util.h" +#include "base/notreached.h" #include "base/strings/stringprintf.h" #include "base/strings/utf_string_conversions.h" #include "third_party/inspector_protocol/crdtp/cbor.h" @@ -104,12 +105,185 @@ bool ProtocolTypeTraits<std::string>::Deserialize(DeserializerState* state, return false; } +namespace { + +template <typename Iterable> +void SerializeDict(const Iterable& iterable, std::vector<uint8_t>* bytes) { + ContainerSerializer serializer(bytes, cbor::EncodeIndefiniteLengthMapStart()); + for (const auto& kv : iterable) { + serializer.AddField(span<char>(kv.first.data(), kv.first.size()), + kv.second); + } + serializer.EncodeStop(); +} + +bool DeserializeDict(DeserializerState* state, + base::flat_map<std::string, base::Value>* value) { + cbor::CBORTokenizer* tokenizer = state->tokenizer(); + if (tokenizer->TokenTag() == cbor::CBORTokenTag::ENVELOPE) + tokenizer->EnterEnvelope(); + if (tokenizer->TokenTag() != cbor::CBORTokenTag::MAP_START) { + state->RegisterError(Error::CBOR_MAP_START_EXPECTED); + return false; + } + tokenizer->Next(); + for (; tokenizer->TokenTag() != cbor::CBORTokenTag::STOP; tokenizer->Next()) { + if (tokenizer->TokenTag() != cbor::CBORTokenTag::STRING8) { + state->RegisterError(Error::CBOR_INVALID_MAP_KEY); + return false; + } + auto key = tokenizer->GetString8(); + std::string name(reinterpret_cast<const char*>(key.begin()), key.size()); + tokenizer->Next(); + base::Value dict_value; + if (!ProtocolTypeTraits<base::Value>::Deserialize(state, &dict_value)) + return false; + value->insert(std::make_pair(std::move(name), std::move(dict_value))); + } + return true; +} + +} // namespace + // static void ProtocolTypeTraits<std::string>::Serialize(const std::string& str, std::vector<uint8_t>* bytes) { - cbor::EncodeString8( - span<uint8_t>(reinterpret_cast<const uint8_t*>(str.data()), str.size()), - bytes); + cbor::EncodeString8(SpanFrom(str), bytes); +} + +// static +bool ProtocolTypeTraits<base::Value>::Deserialize(DeserializerState* state, + base::Value* value) { + cbor::CBORTokenizer* tokenizer = state->tokenizer(); + switch (tokenizer->TokenTag()) { + case cbor::CBORTokenTag::TRUE_VALUE: + *value = base::Value(true); + break; + case cbor::CBORTokenTag::FALSE_VALUE: + *value = base::Value(false); + break; + case cbor::CBORTokenTag::NULL_VALUE: + *value = base::Value(); + break; + case cbor::CBORTokenTag::INT32: + *value = base::Value(tokenizer->GetInt32()); + break; + case cbor::CBORTokenTag::DOUBLE: + *value = base::Value(tokenizer->GetDouble()); + break; + case cbor::CBORTokenTag::STRING8: { + const auto str = tokenizer->GetString8(); + *value = base::Value(base::StringPiece( + reinterpret_cast<const char*>(str.data()), str.size())); + break; + } + case cbor::CBORTokenTag::STRING16: { + const auto str = tokenizer->GetString16WireRep(); + // TODO(caseq): support big-endian architectures when needed. + *value = base::Value(base::StringPiece16( + reinterpret_cast<const char16_t*>(str.data()), str.size() / 2)); + break; + } + + case cbor::CBORTokenTag::ENVELOPE: + tokenizer->EnterEnvelope(); + if (tokenizer->TokenTag() != cbor::CBORTokenTag::ARRAY_START && + tokenizer->TokenTag() != cbor::CBORTokenTag::MAP_START) { + state->RegisterError(Error::CBOR_MAP_OR_ARRAY_EXPECTED_IN_ENVELOPE); + return false; + } + return ProtocolTypeTraits<base::Value>::Deserialize(state, value); + + case cbor::CBORTokenTag::MAP_START: { + base::flat_map<std::string, base::Value> dict; + if (!DeserializeDict(state, &dict)) + return false; + *value = base::Value(std::move(dict)); + break; + } + + case cbor::CBORTokenTag::ARRAY_START: { + std::vector<base::Value> values; + if (!ProtocolTypeTraits<std::vector<base::Value>>::Deserialize(state, + &values)) { + return false; + } + *value = base::Value(std::move(values)); + break; + } + + // Intentionally not supported. + case cbor::CBORTokenTag::BINARY: + // Should not occur at this level. + case cbor::CBORTokenTag::STOP: + case cbor::CBORTokenTag::DONE: + state->RegisterError(Error::CBOR_UNSUPPORTED_VALUE); + return false; + + case cbor::CBORTokenTag::ERROR_VALUE: + return false; + } + return true; +} + +// static +void ProtocolTypeTraits<base::Value>::Serialize(const base::Value& value, + std::vector<uint8_t>* bytes) { + switch (value.type()) { + case base::Value::Type::NONE: + bytes->push_back(cbor::EncodeNull()); + return; + case base::Value::Type::BOOLEAN: + bytes->push_back(value.GetBool() ? cbor::EncodeTrue() + : cbor::EncodeFalse()); + return; + case base::Value::Type::INTEGER: { + // Truncate, but DCHECK() the actual value was within int32_t range. + // TODO(caseq): consider if we need to convert ints outside of int32_t to + // double automatically. Right now, we maintain historic behavior where we + // didn't. + int32_t i = static_cast<int32_t>(value.GetInt()); + DCHECK_EQ(static_cast<int>(i), value.GetInt()); + cbor::EncodeInt32(i, bytes); + return; + } + case base::Value::Type::DOUBLE: + cbor::EncodeDouble(value.GetDouble(), bytes); + return; + case base::Value::Type::STRING: { + cbor::EncodeString8(SpanFrom(value.GetString()), bytes); + return; + } + case base::Value::Type::BINARY: + // TODO(caseq): support this? + NOTREACHED(); + return; + case base::Value::Type::DICTIONARY: + SerializeDict(value.DictItems(), bytes); + return; + case base::Value::Type::LIST: { + ContainerSerializer serializer(bytes, + cbor::EncodeIndefiniteLengthArrayStart()); + for (const auto& item : value.GetListDeprecated()) + ProtocolTypeTraits<base::Value>::Serialize(item, bytes); + serializer.EncodeStop(); + return; + } + } +} + +// static +bool ProtocolTypeTraits<traits::DictionaryValue>::Deserialize( + DeserializerState* state, + traits::DictionaryValue* value) { + return DeserializeDict(state, value); +} + +// static +void ProtocolTypeTraits<traits::DictionaryValue>::Serialize( + const traits::DictionaryValue& value, + std::vector<uint8_t>* bytes) { + SerializeDict(value, bytes); } } // namespace crdtp diff --git a/chromium/third_party/inspector_protocol/crdtp/chromium/protocol_traits.h b/chromium/third_party/inspector_protocol/crdtp/chromium/protocol_traits.h index 4f9386210db..a4e9b96e70c 100644 --- a/chromium/third_party/inspector_protocol/crdtp/chromium/protocol_traits.h +++ b/chromium/third_party/inspector_protocol/crdtp/chromium/protocol_traits.h @@ -5,6 +5,7 @@ #include "base/memory/ref_counted_memory.h" #include "base/memory/scoped_refptr.h" +#include "base/values.h" #include "third_party/inspector_protocol/crdtp/protocol_core.h" #include "third_party/inspector_protocol/crdtp/serializable.h" @@ -15,12 +16,63 @@ class Value; namespace crdtp { class Serializable; +namespace traits { + +// TODO(caseq): these should eventually be replaced with configurable +// types in protocol_config.json, so we can generate code with direct +// types rather than aliases, but for the time being, this is our way +// to specify the type mapping to the rest of the generated code. +using String = std::string; +using Value = base::Value; +using DictionaryValue = base::flat_map<std::string, base::Value>; +using ListValue = std::vector<base::Value>; + +} // namespace traits + template <> struct CRDTP_EXPORT ProtocolTypeTraits<std::string> { static bool Deserialize(DeserializerState* state, std::string* value); static void Serialize(const std::string& value, std::vector<uint8_t>* bytes); }; +template <> +struct CRDTP_EXPORT ProtocolTypeTraits<base::Value> { + static bool Deserialize(DeserializerState* state, base::Value* value); + static void Serialize(const base::Value& value, std::vector<uint8_t>* bytes); +}; + +template <> +struct CRDTP_EXPORT ProtocolTypeTraits<traits::DictionaryValue> { + static bool Deserialize(DeserializerState* state, + traits::DictionaryValue* value); + static void Serialize(const traits::DictionaryValue& value, + std::vector<uint8_t>* bytes); +}; + +template <typename T> +struct UniquePtrTraitsHelper { + static bool Deserialize(DeserializerState* state, std::unique_ptr<T>* value) { + auto res = std::make_unique<T>(); + if (!ProtocolTypeTraits<T>::Deserialize(state, res.get())) + return false; + *value = std::move(res); + return true; + } + static void Serialize(const std::unique_ptr<T>& value, + std::vector<uint8_t>* bytes) { + ProtocolTypeTraits<T>::Serialize(*value, bytes); + } +}; + +template <> +struct CRDTP_EXPORT ProtocolTypeTraits<std::unique_ptr<traits::DictionaryValue>> + : public UniquePtrTraitsHelper<traits::DictionaryValue> {}; + +// TODO(caseq): get rid of this, make sure Value is stored directly. +template <> +struct CRDTP_EXPORT ProtocolTypeTraits<std::unique_ptr<base::Value>> + : public UniquePtrTraitsHelper<base::Value> {}; + // A read-only sequence of uninterpreted bytes with reference-counted storage. class CRDTP_EXPORT Binary : public Serializable { public: diff --git a/chromium/third_party/inspector_protocol/crdtp/chromium/protocol_traits_test.cc b/chromium/third_party/inspector_protocol/crdtp/chromium/protocol_traits_test.cc new file mode 100644 index 00000000000..55a664caae0 --- /dev/null +++ b/chromium/third_party/inspector_protocol/crdtp/chromium/protocol_traits_test.cc @@ -0,0 +1,139 @@ +#include "third_party/inspector_protocol/crdtp/test_platform.h" + +#include "base/test/values_test_util.h" +#include "third_party/inspector_protocol/crdtp/chromium/protocol_traits.h" + +namespace crdtp { + +namespace { + +using base::test::IsJson; +using testing::Eq; + +template <typename T, typename F> +T ConvertTo(const F& from) { + std::vector<uint8_t> bytes; + ProtocolTypeTraits<F>::Serialize(from, &bytes); + DeserializerState deserializer(std::move(bytes)); + T to; + bool rc = ProtocolTypeTraits<T>::Deserialize(&deserializer, &to); + EXPECT_TRUE(rc) << deserializer.ErrorMessage( + crdtp::MakeSpan("Error deserializing")); + return to; +} + +template <typename T> +T RoundTrip(const T& from) { + return ConvertTo<T>(from); +} + +TEST(ProtocolTraits, String) { + const std::string kEmpty = ""; + EXPECT_THAT(RoundTrip(kEmpty), Eq(kEmpty)); + + const std::string kHelloWorld = "Hello, world!"; + EXPECT_THAT(RoundTrip(kHelloWorld), Eq(kHelloWorld)); + + std::string all_values(256, ' '); + for (size_t i = 0; i < all_values.size(); ++i) + all_values[i] = i; + EXPECT_THAT(RoundTrip(all_values), Eq(all_values)); +} + +std::vector<uint8_t> MakeVector(const Binary& b) { + return std::vector<uint8_t>(b.data(), b.data() + b.size()); +} + +TEST(ProtocolTraits, BinaryBasic) { + const Binary empty; + EXPECT_THAT(empty.size(), Eq(0UL)); + + constexpr uint8_t data[] = {'H', 'e', 'l', 'l', 'o', ',', 0, + 'w', 'o', 'r', 'l', 'd', '!', 0x80}; + const std::vector<uint8_t> data_vec(std::cbegin(data), std::cend(data)); + Binary binary = Binary::fromSpan(data, sizeof data); + EXPECT_THAT(binary.toBase64(), Eq("SGVsbG8sAHdvcmxkIYA=")); + EXPECT_THAT(MakeVector(binary), Eq(data_vec)); + EXPECT_THAT(MakeVector(Binary::fromVector(data_vec)), Eq(data_vec)); + EXPECT_THAT(MakeVector(Binary::fromString(std::string( + reinterpret_cast<const char*>(data), sizeof data))), + Eq(data_vec)); + bool success = false; + EXPECT_THAT(MakeVector(Binary::fromBase64("SGVsbG8sAHdvcmxkIYA=", &success)), + Eq(data_vec)); + Binary::fromBase64("SGVsbG8sAHdvcmxkIYA", &success); + EXPECT_FALSE(success); +} + +TEST(ProtocolTraits, BinarySerialization) { + constexpr uint8_t data[] = {'H', 'e', 'l', 'l', 'o', ',', 0, + 'w', 'o', 'r', 'l', 'd', '!', 0x80}; + Binary binary = Binary::fromSpan(data, sizeof data); + + EXPECT_THAT(MakeVector(RoundTrip(binary)), Eq(MakeVector(binary))); +} + +TEST(ProtocolTraits, PrimitiveValueSerialization) { + EXPECT_THAT(RoundTrip(base::Value()), IsJson(base::Value())); + + EXPECT_THAT(ConvertTo<bool>(base::Value(false)), Eq(false)); + EXPECT_THAT(ConvertTo<bool>(base::Value(true)), Eq(true)); + EXPECT_THAT(RoundTrip(base::Value(false)), IsJson(base::Value(false))); + EXPECT_THAT(RoundTrip(base::Value(true)), IsJson(base::Value(true))); + + EXPECT_THAT(ConvertTo<int>(base::Value(42)), 42); + EXPECT_THAT(RoundTrip(base::Value(42)), IsJson(base::Value(42))); + + constexpr char kMessage[] = "We apologise for the inconvenience"; + EXPECT_THAT(ConvertTo<std::string>(base::Value(kMessage)), Eq(kMessage)); + EXPECT_THAT(RoundTrip(base::Value(kMessage)), IsJson(base::Value(kMessage))); + + EXPECT_THAT(ConvertTo<double>(base::Value(6.62607015e-34)), + Eq(6.62607015e-34)); + EXPECT_THAT(RoundTrip(base::Value(6.62607015e-34)), + IsJson(base::Value(6.62607015e-34))); +} + +template <typename... Args> +std::vector<base::Value> MakeList(Args&&... args) { + std::vector<base::Value> res; + (res.push_back(base::Value(std::forward<Args>(args))), ...); + return res; +} + +TEST(ProtocolTraits, ListValueSerialization) { + EXPECT_THAT( + ConvertTo<std::vector<int>>(base::Value(std::vector<base::Value>())), + Eq(std::vector<int>())); + EXPECT_THAT(RoundTrip(base::Value(std::vector<base::Value>())), + IsJson(base::Value(base::Value::Type::LIST))); + + std::vector<base::Value> list = MakeList(2, 3, 5); + EXPECT_THAT(ConvertTo<std::vector<int>>(base::Value(list)), + Eq(std::vector<int>{2, 3, 5})); + EXPECT_THAT(ConvertTo<base::Value>(list), IsJson(base::Value(list))); + EXPECT_THAT(RoundTrip(base::Value(list)), IsJson(base::Value(list))); + + std::vector<base::Value> list_of_lists = + MakeList(list, MakeList("foo", "bar", "bazz"), MakeList(base::Value())); + EXPECT_THAT(RoundTrip(base::Value(list_of_lists)), + IsJson(base::Value(list_of_lists))); +} + +TEST(ProtocolTraits, DictValueSerialization) { + base::flat_map<std::string, base::Value> dict; + EXPECT_THAT(RoundTrip(base::Value(dict)), + IsJson(base::Value(base::Value::Type::DICTIONARY))); + dict.insert(std::make_pair("int", base::Value(42))); + dict.insert(std::make_pair("double", base::Value(2.718281828459045))); + dict.insert(std::make_pair("string", base::Value("foo"))); + dict.insert(std::make_pair("list", base::Value(MakeList("bar", 42)))); + dict.insert(std::make_pair("null", base::Value())); + dict.insert(std::make_pair("dict", base::Value(dict))); + EXPECT_THAT(ConvertTo<base::Value>(dict), IsJson(base::Value(dict))); + EXPECT_THAT(RoundTrip(base::Value(dict)), IsJson(base::Value(dict))); +} + +} // namespace + +} // namespace crdtp diff --git a/chromium/third_party/inspector_protocol/crdtp/dispatch.h b/chromium/third_party/inspector_protocol/crdtp/dispatch.h index 8aeec309da1..fe115c6e8be 100644 --- a/chromium/third_party/inspector_protocol/crdtp/dispatch.h +++ b/chromium/third_party/inspector_protocol/crdtp/dispatch.h @@ -31,7 +31,7 @@ enum class DispatchCode { FALL_THROUGH = 2, // For historical reasons, these error codes correspond to commonly used // XMLRPC codes (e.g. see METHOD_NOT_FOUND in - // https://github.com/python/cpython/blob/master/Lib/xmlrpc/client.py). + // https://github.com/python/cpython/blob/main/Lib/xmlrpc/client.py). PARSE_ERROR = -32700, INVALID_REQUEST = -32600, METHOD_NOT_FOUND = -32601, diff --git a/chromium/third_party/inspector_protocol/crdtp/protocol_core.h b/chromium/third_party/inspector_protocol/crdtp/protocol_core.h index b36ace2615c..6bf006a3fa4 100644 --- a/chromium/third_party/inspector_protocol/crdtp/protocol_core.h +++ b/chromium/third_party/inspector_protocol/crdtp/protocol_core.h @@ -348,6 +348,16 @@ struct ProtocolTypeTraits< } }; +template <typename T, typename F> +bool ConvertProtocolValue(const F& from, T* to) { + std::vector<uint8_t> bytes; + ProtocolTypeTraits<F>::Serialize(from, &bytes); + auto deserializer = + DeferredMessage::FromSpan(span<uint8_t>(bytes.data(), bytes.size())) + ->MakeDeserializer(); + return ProtocolTypeTraits<T>::Deserialize(&deserializer, to); +} + #define DECLARE_DESERIALIZATION_SUPPORT() \ friend DeserializableBase<ProtocolType>; \ static const DeserializerDescriptorType& deserializer_descriptor() diff --git a/chromium/third_party/inspector_protocol/inspector_protocol.gni b/chromium/third_party/inspector_protocol/inspector_protocol.gni index 2d241d6546e..1e2a30cf5b7 100644 --- a/chromium/third_party/inspector_protocol/inspector_protocol.gni +++ b/chromium/third_party/inspector_protocol/inspector_protocol.gni @@ -25,22 +25,14 @@ template("inspector_protocol_generate") { assert(defined(invoker.outputs)) assert(defined(invoker.inspector_protocol_dir)) inspector_protocol_dir = invoker.inspector_protocol_dir - + use_embedder_types = + defined(invoker.use_embedder_types) && invoker.use_embedder_types action(target_name) { script = "$inspector_protocol_dir/code_generator.py" inputs = [ invoker.config_file, - "$inspector_protocol_dir/lib/base_string_adapter_cc.template", - "$inspector_protocol_dir/lib/base_string_adapter_h.template", "$inspector_protocol_dir/lib/Forward_h.template", - "$inspector_protocol_dir/lib/Object_cpp.template", - "$inspector_protocol_dir/lib/Object_h.template", - "$inspector_protocol_dir/lib/Protocol_cpp.template", - "$inspector_protocol_dir/lib/ValueConversions_cpp.template", - "$inspector_protocol_dir/lib/ValueConversions_h.template", - "$inspector_protocol_dir/lib/Values_cpp.template", - "$inspector_protocol_dir/lib/Values_h.template", "$inspector_protocol_dir/templates/Exported_h.template", "$inspector_protocol_dir/templates/Imported_h.template", "$inspector_protocol_dir/templates/TypeBuilder_cpp.template", @@ -49,12 +41,21 @@ template("inspector_protocol_generate") { if (defined(invoker.inputs)) { inputs += invoker.inputs } + if (!use_embedder_types) { + inputs += [ + "$inspector_protocol_dir/lib/ValueConversions_cpp.template", + "$inspector_protocol_dir/lib/ValueConversions_h.template", + "$inspector_protocol_dir/lib/Values_cpp.template", + "$inspector_protocol_dir/lib/Values_h.template", + "$inspector_protocol_dir/lib/Object_cpp.template", + "$inspector_protocol_dir/lib/Object_h.template", + ] + } args = [ "--jinja_dir", rebase_path("//third_party/", root_build_dir), # jinja is in chromium's # third_party - "--output_base", rebase_path(invoker.out_dir, root_build_dir), "--config", @@ -62,7 +63,12 @@ template("inspector_protocol_generate") { "--inspector_protocol_dir", "$inspector_protocol_dir", ] - + if (use_embedder_types) { + args += [ + "--config_value", + "use_embedder_types=true", + ] + } if (defined(invoker.config_values)) { foreach(value, invoker.config_values) { args += [ diff --git a/chromium/third_party/inspector_protocol/lib/Forward_h.template b/chromium/third_party/inspector_protocol/lib/Forward_h.template index 54f25e1c9c0..4a1a6d707da 100644 --- a/chromium/third_party/inspector_protocol/lib/Forward_h.template +++ b/chromium/third_party/inspector_protocol/lib/Forward_h.template @@ -10,37 +10,49 @@ {% if config.lib.export_header %} #include {{format_include(config.lib.export_header)}} {% endif %} -#include {{format_include(config.lib.string_header)}} -#include <cstddef> #include <memory> #include <vector> -#include <unordered_map> -#include <unordered_set> #include "{{config.crdtp.dir}}/error_support.h" #include "{{config.crdtp.dir}}/dispatch.h" #include "{{config.crdtp.dir}}/frontend_channel.h" #include "{{config.crdtp.dir}}/protocol_core.h" +{% if config.use_embedder_types %} +#include {{format_include(config.lib.protocol_traits)}} +{% else %} +#include {{format_include(config.lib.string_header)}} +{% endif %} + {% for namespace in config.protocol.namespace %} namespace {{namespace}} { {% endfor %} -class DictionaryValue; using DispatchResponse = {{config.crdtp.namespace}}::DispatchResponse; using ErrorSupport = {{config.crdtp.namespace}}::ErrorSupport; using Serializable = {{config.crdtp.namespace}}::Serializable; using FrontendChannel = {{config.crdtp.namespace}}::FrontendChannel; using DomainDispatcher = {{config.crdtp.namespace}}::DomainDispatcher; using UberDispatcher = {{config.crdtp.namespace}}::UberDispatcher; +using Response = DispatchResponse; + +{% if config.use_embedder_types %} +using DictionaryValue = crdtp::traits::DictionaryValue; +using Object = crdtp::traits::DictionaryValue; +using ListValue = crdtp::traits::ListValue; +using Value = crdtp::traits::Value; +using String = crdtp::traits::String; +using Binary = crdtp::Binary; +{% else %} +class DictionaryValue; class FundamentalValue; class ListValue; class Object; -using Response = DispatchResponse; class SerializedValue; class StringValue; class Value; +{% endif %} using {{config.crdtp.namespace}}::detail::PtrMaybe; using {{config.crdtp.namespace}}::detail::ValueMaybe; diff --git a/chromium/third_party/inspector_protocol/lib/ValueConversions_cpp.template b/chromium/third_party/inspector_protocol/lib/ValueConversions_cpp.template index a16b522c38a..998808be6c6 100644 --- a/chromium/third_party/inspector_protocol/lib/ValueConversions_cpp.template +++ b/chromium/third_party/inspector_protocol/lib/ValueConversions_cpp.template @@ -18,7 +18,7 @@ namespace {{namespace}} { {% endfor %} {% for namespace in config.protocol.namespace %} -} // namespce +} // namespace {% endfor %} diff --git a/chromium/third_party/inspector_protocol/lib/Values_h.template b/chromium/third_party/inspector_protocol/lib/Values_h.template index 82080090098..53087c914ca 100644 --- a/chromium/third_party/inspector_protocol/lib/Values_h.template +++ b/chromium/third_party/inspector_protocol/lib/Values_h.template @@ -10,6 +10,11 @@ //#include "Allocator.h" //#include "Forward.h" +#include <memory> +#include <unordered_map> +#include <utility> +#include <vector> + #include {{format_include(config.protocol.package, "Forward")}} {% for namespace in config.protocol.namespace %} @@ -165,7 +170,9 @@ public: static std::unique_ptr<DictionaryValue> cast(std::unique_ptr<Value> value) { - return std::unique_ptr<DictionaryValue>(DictionaryValue::cast(value.release())); + DictionaryValue* dictionaryValue = cast(value.get()); + if (dictionaryValue) value.release(); + return std::unique_ptr<DictionaryValue>(dictionaryValue); } void AppendSerialized(std::vector<uint8_t>* bytes) const override; @@ -231,7 +238,9 @@ public: static std::unique_ptr<ListValue> cast(std::unique_ptr<Value> value) { - return std::unique_ptr<ListValue>(ListValue::cast(value.release())); + ListValue* listValue = cast(value.get()); + if (listValue) value.release(); + return std::unique_ptr<ListValue>(listValue); } ~ListValue() override; diff --git a/chromium/third_party/inspector_protocol/lib/base_string_adapter_cc.template b/chromium/third_party/inspector_protocol/lib/base_string_adapter_cc.template deleted file mode 100644 index 628bd6fb2f7..00000000000 --- a/chromium/third_party/inspector_protocol/lib/base_string_adapter_cc.template +++ /dev/null @@ -1,132 +0,0 @@ -// This file is generated by base_string_adapter_cc.template. - -// Copyright 2019 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 {{format_include(config.protocol.package, "base_string_adapter")}} -#include {{format_include(config.protocol.package, "Protocol")}} - -#include <utility> -#include "base/base64.h" -#include "base/json/json_reader.h" -#include "base/memory/ptr_util.h" -#include "base/strings/stringprintf.h" -#include "base/strings/utf_string_conversions.h" -#include "base/values.h" -#include "{{config.crdtp.dir}}/cbor.h" -#include "{{config.crdtp.dir}}/protocol_core.h" - -using namespace {{config.crdtp.namespace}}; - -using {{"::".join(config.protocol.namespace)}}::Binary; -using {{"::".join(config.protocol.namespace)}}::String; -using {{"::".join(config.protocol.namespace)}}::StringUtil; - -{% for namespace in config.protocol.namespace %} -namespace {{namespace}} { -{% endfor %} - -// In Chromium, we do not support big endian architectures, so no conversion is needed -// to interpret UTF16LE. -// static -String StringUtil::fromUTF16LE(const uint16_t* data, size_t length) { - std::string utf8; - base::UTF16ToUTF8(reinterpret_cast<const char16_t*>(data), length, &utf8); - return utf8; -} - -std::unique_ptr<protocol::Value> toProtocolValue( - const base::Value* value, int depth) { - if (!value || !depth) - return nullptr; - if (value->is_none()) - return protocol::Value::null(); - if (value->is_bool()) - return protocol::FundamentalValue::create(value->GetBool()); - if (value->is_int()) - return protocol::FundamentalValue::create(value->GetInt()); - if (value->is_double()) - return protocol::FundamentalValue::create(value->GetDouble()); - if (value->is_string()) - return protocol::StringValue::create(value->GetString()); - if (value->is_list()) { - std::unique_ptr<protocol::ListValue> result = protocol::ListValue::create(); - for (const base::Value& item : value->GetList()) { - std::unique_ptr<protocol::Value> converted = - toProtocolValue(&item, depth - 1); - if (converted) - result->pushValue(std::move(converted)); - } - return result; - } - if (value->is_dict()) { - std::unique_ptr<protocol::DictionaryValue> result = - protocol::DictionaryValue::create(); - for (const auto it : value->DictItems()) { - std::unique_ptr<protocol::Value> converted = - toProtocolValue(&it.second, depth - 1); - if (converted) - result->setValue(it.first, std::move(converted)); - } - return result; - } - return nullptr; -} - -std::unique_ptr<base::Value> toBaseValue(Value* value, int depth) { - if (!value || !depth) - return nullptr; - if (value->type() == Value::TypeNull) - return std::make_unique<base::Value>(); - if (value->type() == Value::TypeBoolean) { - bool inner; - value->asBoolean(&inner); - return base::WrapUnique(new base::Value(inner)); - } - if (value->type() == Value::TypeInteger) { - int inner; - value->asInteger(&inner); - return base::WrapUnique(new base::Value(inner)); - } - if (value->type() == Value::TypeDouble) { - double inner; - value->asDouble(&inner); - return base::WrapUnique(new base::Value(inner)); - } - if (value->type() == Value::TypeString) { - std::string inner; - value->asString(&inner); - return base::WrapUnique(new base::Value(inner)); - } - if (value->type() == Value::TypeArray) { - ListValue* list = ListValue::cast(value); - std::unique_ptr<base::Value> result(new base::Value( - base::Value::Type::LIST)); - for (size_t i = 0; i < list->size(); i++) { - std::unique_ptr<base::Value> converted = - toBaseValue(list->at(i), depth - 1); - if (converted) - result->Append(std::move(*converted)); - } - return result; - } - if (value->type() == Value::TypeObject) { - DictionaryValue* dict = DictionaryValue::cast(value); - std::unique_ptr<base::Value> result(new base::Value( - base::Value::Type::DICTIONARY)); - for (size_t i = 0; i < dict->size(); i++) { - DictionaryValue::Entry entry = dict->at(i); - std::unique_ptr<base::Value> converted = - toBaseValue(entry.second, depth - 1); - if (converted) - result->SetKey(entry.first, std::move(*converted)); - } - return result; - } - return nullptr; -} - -{% for namespace in config.protocol.namespace %} -} // namespace {{namespace}} { -{% endfor %} diff --git a/chromium/third_party/inspector_protocol/lib/base_string_adapter_h.template b/chromium/third_party/inspector_protocol/lib/base_string_adapter_h.template deleted file mode 100644 index f39b13e925e..00000000000 --- a/chromium/third_party/inspector_protocol/lib/base_string_adapter_h.template +++ /dev/null @@ -1,48 +0,0 @@ -// This file is generated by base_string_adapter_h.template. - -// Copyright 2019 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. - -#ifndef {{"_".join(config.protocol.namespace)}}_BASE_STRING_ADAPTER_H -#define {{"_".join(config.protocol.namespace)}}_BASE_STRING_ADAPTER_H - -#include "{{config.crdtp.dir}}/chromium/protocol_traits.h" - -{% if config.lib.export_header %} -#include "{{config.lib.export_header}}" -{% endif %} - -{% for namespace in config.protocol.namespace %} -namespace {{namespace}} { -{% endfor %} - -class Value; - -using String = std::string; -using Binary = crdtp::Binary; - -class {{config.lib.export_macro}} StringUtil { - public: - static String fromUTF8(const uint8_t* data, size_t length) { - return std::string(reinterpret_cast<const char*>(data), length); - } - - static String fromUTF16LE(const uint16_t* data, size_t length); - - static const uint8_t* CharactersLatin1(const String& s) { return nullptr; } - static const uint8_t* CharactersUTF8(const String& s) { - return reinterpret_cast<const uint8_t*>(s.data()); - } - static const uint16_t* CharactersUTF16(const String& s) { return nullptr; } - static size_t CharacterCount(const String& s) { return s.size(); } -}; - -std::unique_ptr<Value> toProtocolValue(const base::Value* value, int depth); -std::unique_ptr<base::Value> toBaseValue(Value* value, int depth); - -{% for namespace in config.protocol.namespace %} -} // namespace {{namespace}} -{% endfor %} - -#endif // !defined({{"_".join(config.protocol.namespace)}}_BASE_STRING_ADAPTER_H) |