From f6a76fad93bc2ec872a74b2b74fde8e06623c5ed Mon Sep 17 00:00:00 2001 From: Johannes Henkel Date: Wed, 20 Nov 2019 15:00:57 -0800 Subject: [PATCH] [DevTools] Roll inspector protocol (Cleanup) (V8) New revision: 4c2a3acaea9f2e7958081dd361f81e20e9eff5e7 This cleanup cl does not change any behavior, it just cleans up some headers and does a class rename (StreamingParserHandler->ParserHandler). It was reviewed upstream https://chromium-review.googlesource.com/c/deps/inspector_protocol/+/1924792 https://chromium-review.googlesource.com/c/deps/inspector_protocol/+/1925679 and does not touch V8 code. Would like to get this in to make it easier to review subsequent changes. Thanks! Change-Id: Ie9fe1434bafeb4f5090244f823d1e482ff805dd0 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1925721 Auto-Submit: Johannes Henkel Reviewed-by: Yang Guo Commit-Queue: Johannes Henkel Cr-Commit-Position: refs/heads/master@{#65106} --- third_party/inspector_protocol/README.v8 | 2 +- .../inspector_protocol/code_generator.py | 2 +- third_party/inspector_protocol/crdtp/cbor.cc | 41 +++-- third_party/inspector_protocol/crdtp/cbor.h | 13 +- .../inspector_protocol/crdtp/cbor_test.cc | 142 +++++++++--------- third_party/inspector_protocol/crdtp/json.cc | 32 ++-- third_party/inspector_protocol/crdtp/json.h | 24 +-- .../inspector_protocol/crdtp/json_platform.h | 8 - .../inspector_protocol/crdtp/json_test.cc | 28 ++-- .../inspector_protocol/crdtp/parser_handler.h | 13 +- third_party/inspector_protocol/crdtp/span.h | 5 - .../inspector_protocol/crdtp/span_test.cc | 12 +- .../inspector_protocol/crdtp/status.cc | 7 - third_party/inspector_protocol/crdtp/status.h | 12 +- .../inspector_protocol/crdtp/status_test.cc | 13 -- .../lib/DispatcherBase_cpp.template | 5 +- 16 files changed, 137 insertions(+), 222 deletions(-) diff --git a/third_party/inspector_protocol/README.v8 b/third_party/inspector_protocol/README.v8 index 1682803eea..a6e75e142c 100644 --- a/third_party/inspector_protocol/README.v8 +++ b/third_party/inspector_protocol/README.v8 @@ -2,7 +2,7 @@ Name: inspector protocol Short Name: inspector_protocol URL: https://chromium.googlesource.com/deps/inspector_protocol/ Version: 0 -Revision: d020a9e614d4a5116a7c71f288c0340e282e1a6e +Revision: 4c2a3acaea9f2e7958081dd361f81e20e9eff5e7 License: BSD License File: LICENSE Security Critical: no diff --git a/third_party/inspector_protocol/code_generator.py b/third_party/inspector_protocol/code_generator.py index 45afa03fc6..4260dbe5a4 100755 --- a/third_party/inspector_protocol/code_generator.py +++ b/third_party/inspector_protocol/code_generator.py @@ -114,7 +114,7 @@ def read_config(): ".lib.export_header": False, ".crdtp": False, ".crdtp.dir": os.path.join(inspector_protocol_dir, "crdtp"), - ".crdtp.namespace": "inspector_protocol", + ".crdtp.namespace": "crdtp", } for key_value in config_values: parts = key_value.split("=") diff --git a/third_party/inspector_protocol/crdtp/cbor.cc b/third_party/inspector_protocol/crdtp/cbor.cc index 3e88aaeb14..8c90d60741 100644 --- a/third_party/inspector_protocol/crdtp/cbor.cc +++ b/third_party/inspector_protocol/crdtp/cbor.cc @@ -451,7 +451,7 @@ bool EnvelopeEncoder::EncodeStop(std::string* out) { namespace { template -class CBOREncoder : public StreamingParserHandler { +class CBOREncoder : public ParserHandler { public: CBOREncoder(C* out, Status* status) : out_(out), status_(status) { *status_ = Status(); @@ -557,16 +557,14 @@ class CBOREncoder : public StreamingParserHandler { }; } // namespace -std::unique_ptr NewCBOREncoder( - std::vector* out, - Status* status) { - return std::unique_ptr( +std::unique_ptr NewCBOREncoder(std::vector* out, + Status* status) { + return std::unique_ptr( new CBOREncoder>(out, status)); } - -std::unique_ptr NewCBOREncoder(std::string* out, - Status* status) { - return std::unique_ptr( +std::unique_ptr NewCBOREncoder(std::string* out, + Status* status) { + return std::unique_ptr( new CBOREncoder(out, status)); } @@ -870,21 +868,18 @@ static constexpr int kStackLimit = 300; // to roundtrip JSON messages. bool ParseMap(int32_t stack_depth, CBORTokenizer* tokenizer, - StreamingParserHandler* out); - + ParserHandler* out); bool ParseArray(int32_t stack_depth, CBORTokenizer* tokenizer, - StreamingParserHandler* out); - + ParserHandler* out); bool ParseValue(int32_t stack_depth, CBORTokenizer* tokenizer, - StreamingParserHandler* out); - + ParserHandler* out); bool ParseEnvelope(int32_t stack_depth, CBORTokenizer* tokenizer, - StreamingParserHandler* out); + ParserHandler* out); -void ParseUTF16String(CBORTokenizer* tokenizer, StreamingParserHandler* out) { +void ParseUTF16String(CBORTokenizer* tokenizer, ParserHandler* out) { std::vector value; span rep = tokenizer->GetString16WireRep(); for (size_t ii = 0; ii < rep.size(); ii += 2) @@ -893,7 +888,7 @@ void ParseUTF16String(CBORTokenizer* tokenizer, StreamingParserHandler* out) { tokenizer->Next(); } -bool ParseUTF8String(CBORTokenizer* tokenizer, StreamingParserHandler* out) { +bool ParseUTF8String(CBORTokenizer* tokenizer, ParserHandler* out) { assert(tokenizer->TokenTag() == CBORTokenTag::STRING8); out->HandleString8(tokenizer->GetString8()); tokenizer->Next(); @@ -902,7 +897,7 @@ bool ParseUTF8String(CBORTokenizer* tokenizer, StreamingParserHandler* out) { bool ParseEnvelope(int32_t stack_depth, CBORTokenizer* tokenizer, - StreamingParserHandler* out) { + ParserHandler* out) { assert(tokenizer->TokenTag() == CBORTokenTag::ENVELOPE); // Before we enter the envelope, we save the position that we // expect to see after we're done parsing the envelope contents. @@ -948,7 +943,7 @@ bool ParseEnvelope(int32_t stack_depth, bool ParseValue(int32_t stack_depth, CBORTokenizer* tokenizer, - StreamingParserHandler* out) { + ParserHandler* out) { if (stack_depth > kStackLimit) { out->HandleError( Status{Error::CBOR_STACK_LIMIT_EXCEEDED, tokenizer->Status().pos}); @@ -1010,7 +1005,7 @@ bool ParseValue(int32_t stack_depth, // detected. bool ParseArray(int32_t stack_depth, CBORTokenizer* tokenizer, - StreamingParserHandler* out) { + ParserHandler* out) { assert(tokenizer->TokenTag() == CBORTokenTag::ARRAY_START); tokenizer->Next(); out->HandleArrayBegin(); @@ -1038,7 +1033,7 @@ bool ParseArray(int32_t stack_depth, // detected. bool ParseMap(int32_t stack_depth, CBORTokenizer* tokenizer, - StreamingParserHandler* out) { + ParserHandler* out) { assert(tokenizer->TokenTag() == CBORTokenTag::MAP_START); out->HandleMapBegin(); tokenizer->Next(); @@ -1073,7 +1068,7 @@ bool ParseMap(int32_t stack_depth, } } // namespace -void ParseCBOR(span bytes, StreamingParserHandler* out) { +void ParseCBOR(span bytes, ParserHandler* out) { if (bytes.empty()) { out->HandleError(Status{Error::CBOR_NO_INPUT, 0}); return; diff --git a/third_party/inspector_protocol/crdtp/cbor.h b/third_party/inspector_protocol/crdtp/cbor.h index ac49ad8ec7..e168d310a4 100644 --- a/third_party/inspector_protocol/crdtp/cbor.h +++ b/third_party/inspector_protocol/crdtp/cbor.h @@ -5,11 +5,8 @@ #ifndef V8_CRDTP_CBOR_H_ #define V8_CRDTP_CBOR_H_ -#include #include #include -#include -#include #include #include #include @@ -140,11 +137,9 @@ class EnvelopeEncoder { // that drives it. The handler will encode into |out|, and iff an error occurs // it will set |status| to an error and clear |out|. Otherwise, |status.ok()| // will be |true|. -std::unique_ptr NewCBOREncoder( - std::vector* out, - Status* status); -std::unique_ptr NewCBOREncoder(std::string* out, - Status* status); +std::unique_ptr NewCBOREncoder(std::vector* out, + Status* status); +std::unique_ptr NewCBOREncoder(std::string* out, Status* status); // ============================================================================= // cbor::CBORTokenizer - for parsing individual CBOR items @@ -283,7 +278,7 @@ class CBORTokenizer { // |out|. If an error occurs, sends |out->HandleError|, and parsing stops. // The client is responsible for discarding the already received information in // that case. -void ParseCBOR(span bytes, StreamingParserHandler* out); +void ParseCBOR(span bytes, ParserHandler* out); // ============================================================================= // cbor::AppendString8EntryToMap - for limited in-place editing of messages diff --git a/third_party/inspector_protocol/crdtp/cbor_test.cc b/third_party/inspector_protocol/crdtp/cbor_test.cc index 2c0eaceadd..a24609a4ac 100644 --- a/third_party/inspector_protocol/crdtp/cbor_test.cc +++ b/third_party/inspector_protocol/crdtp/cbor_test.cc @@ -13,8 +13,10 @@ #include #include #include - #include "json.h" +#include "parser_handler.h" +#include "span.h" +#include "status.h" #include "test_platform.h" using testing::ElementsAreArray; @@ -57,7 +59,6 @@ const json::Platform& GetTestPlatform() { } // namespace namespace cbor { - // ============================================================================= // Detecting CBOR content // ============================================================================= @@ -657,17 +658,13 @@ TEST(EncodeDecodeEnvelopesTest, MessageWithNestingAndEnvelopeContentsAccess) { // cbor::NewCBOREncoder - for encoding from a streaming parser // ============================================================================= -void EncodeUTF8ForTest(const std::string& key, std::vector* out) { - EncodeString8(SpanFrom(key), out); -} TEST(JSONToCBOREncoderTest, SevenBitStrings) { // When a string can be represented as 7 bit ASCII, the encoder will use the // STRING (major Type 3) type, so the actual characters end up as bytes on the // wire. std::vector encoded; Status status; - std::unique_ptr encoder = - NewCBOREncoder(&encoded, &status); + std::unique_ptr encoder = NewCBOREncoder(&encoded, &status); std::vector utf16 = {'f', 'o', 'o'}; encoder->HandleString16(span(utf16.data(), utf16.size())); EXPECT_EQ(Error::OK, status.error); @@ -679,7 +676,7 @@ TEST(JSONToCBOREncoderTest, SevenBitStrings) { } TEST(JsonCborRoundtrip, EncodingDecoding) { - // Hits all the cases except binary and error in StreamingParserHandler, first + // Hits all the cases except binary and error in ParserHandler, first // parsing a JSON message into CBOR, then parsing it back from CBOR into JSON. std::string json = "{" @@ -693,8 +690,7 @@ TEST(JsonCborRoundtrip, EncodingDecoding) { "}"; std::vector encoded; Status status; - std::unique_ptr encoder = - NewCBOREncoder(&encoded, &status); + std::unique_ptr encoder = NewCBOREncoder(&encoded, &status); span ascii_in = SpanFrom(json); json::ParseJSON(GetTestPlatform(), ascii_in, encoder.get()); std::vector expected = { @@ -739,8 +735,8 @@ TEST(JsonCborRoundtrip, EncodingDecoding) { // And now we roundtrip, decoding the message we just encoded. std::string decoded; - std::unique_ptr json_encoder = - NewJSONEncoder(&GetTestPlatform(), &decoded, &status); + std::unique_ptr json_encoder = + json::NewJSONEncoder(&GetTestPlatform(), &decoded, &status); ParseCBOR(span(encoded.data(), encoded.size()), json_encoder.get()); EXPECT_EQ(Error::OK, status.error); EXPECT_EQ(json, decoded); @@ -755,13 +751,12 @@ TEST(JsonCborRoundtrip, MoreRoundtripExamples) { SCOPED_TRACE(std::string("example: ") + json); std::vector encoded; Status status; - std::unique_ptr encoder = - NewCBOREncoder(&encoded, &status); + std::unique_ptr encoder = NewCBOREncoder(&encoded, &status); span ascii_in = SpanFrom(json); - ParseJSON(GetTestPlatform(), ascii_in, encoder.get()); + json::ParseJSON(GetTestPlatform(), ascii_in, encoder.get()); std::string decoded; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &decoded, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &decoded, &status); ParseCBOR(span(encoded.data(), encoded.size()), json_writer.get()); EXPECT_EQ(Error::OK, status.error); EXPECT_EQ(json, decoded); @@ -769,7 +764,7 @@ TEST(JsonCborRoundtrip, MoreRoundtripExamples) { } TEST(JSONToCBOREncoderTest, HelloWorldBinary_WithTripToJson) { - // The StreamingParserHandler::HandleBinary is a special case: The JSON parser + // The ParserHandler::HandleBinary is a special case: The JSON parser // will never call this method, because JSON does not natively support the // binary type. So, we can't fully roundtrip. However, the other direction // works: binary will be rendered in JSON, as a base64 string. So, we make @@ -778,8 +773,7 @@ TEST(JSONToCBOREncoderTest, HelloWorldBinary_WithTripToJson) { // containing "Hello, world.". std::vector encoded; Status status; - std::unique_ptr encoder = - NewCBOREncoder(&encoded, &status); + std::unique_ptr encoder = NewCBOREncoder(&encoded, &status); encoder->HandleMapBegin(); // Emit a key. std::vector key = {'f', 'o', 'o'}; @@ -793,8 +787,8 @@ TEST(JSONToCBOREncoderTest, HelloWorldBinary_WithTripToJson) { // Now drive the json writer via the CBOR decoder. std::string decoded; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &decoded, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &decoded, &status); ParseCBOR(SpanFrom(encoded), json_writer.get()); EXPECT_EQ(Error::OK, status.error); EXPECT_EQ(Status::npos(), status.pos); @@ -813,8 +807,8 @@ TEST(ParseCBORTest, ParseEmptyCBORMessage) { std::vector in = {0xd8, 0x5a, 0, 0, 0, 2, 0xbf, 0xff}; std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(in.data(), in.size()), json_writer.get()); EXPECT_EQ(Error::OK, status.error); EXPECT_EQ("{}", out); @@ -837,8 +831,8 @@ TEST(ParseCBORTest, ParseCBORHelloWorld) { std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::OK, status.error); EXPECT_EQ("{\"msg\":\"Hello, \\ud83c\\udf0e.\"}", out); @@ -862,8 +856,8 @@ TEST(ParseCBORTest, UTF8IsSupportedInKeys) { std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::OK, status.error); EXPECT_EQ("{\"\\ud83c\\udf0e\":\"\\u263e\"}", out); @@ -873,8 +867,8 @@ TEST(ParseCBORTest, NoInputError) { std::vector in = {}; std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(in.data(), in.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_NO_INPUT, status.error); EXPECT_EQ("", out); @@ -887,8 +881,8 @@ TEST(ParseCBORTest, InvalidStartByteError) { std::string json = "{\"msg\": \"Hello, world.\"}"; std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(SpanFrom(json), json_writer.get()); EXPECT_EQ(Error::CBOR_INVALID_START_BYTE, status.error); EXPECT_EQ("", out); @@ -903,8 +897,8 @@ TEST(ParseCBORTest, UnexpectedEofExpectedValueError) { EXPECT_EQ(kPayloadLen, bytes.size() - 6); std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_UNEXPECTED_EOF_EXPECTED_VALUE, status.error); EXPECT_EQ(bytes.size(), status.pos); @@ -921,8 +915,8 @@ TEST(ParseCBORTest, UnexpectedEofInArrayError) { EXPECT_EQ(kPayloadLen, bytes.size() - 6); std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_UNEXPECTED_EOF_IN_ARRAY, status.error); EXPECT_EQ(bytes.size(), status.pos); @@ -936,8 +930,8 @@ TEST(ParseCBORTest, UnexpectedEofInMapError) { EXPECT_EQ(kPayloadLen, bytes.size() - 6); std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_UNEXPECTED_EOF_IN_MAP, status.error); EXPECT_EQ(7u, status.pos); @@ -950,8 +944,8 @@ TEST(ParseCBORTest, TopLevelCantBeEmptyEnvelope) { std::vector bytes = {0xd8, 0x5a, 0, 0, 0, 0}; // envelope std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_MAP_START_EXPECTED, status.error); EXPECT_EQ(bytes.size(), status.pos); @@ -972,8 +966,8 @@ TEST(ParseCBORTest, MapStartExpectedAtTopLevel) { EXPECT_EQ(kPayloadLen, bytes.size() - 6); std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_MAP_START_EXPECTED, status.error); EXPECT_EQ(6u, status.pos); @@ -1002,8 +996,8 @@ TEST(ParseCBORTest, OnlyMapsAndArraysSupportedInsideEnvelopes) { std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_MAP_OR_ARRAY_EXPECTED_IN_ENVELOPE, status.error); EXPECT_EQ(error_pos, status.pos); @@ -1019,8 +1013,8 @@ TEST(ParseCBORTest, InvalidMapKeyError) { EXPECT_EQ(kPayloadLen, bytes.size() - 6); std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_INVALID_MAP_KEY, status.error); EXPECT_EQ(7u, status.pos); @@ -1050,8 +1044,8 @@ TEST(ParseCBORTest, StackLimitExceededError) { std::vector bytes = MakeNestedCBOR(3); std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::OK, status.error); EXPECT_EQ(Status::npos(), status.pos); @@ -1061,8 +1055,8 @@ TEST(ParseCBORTest, StackLimitExceededError) { std::vector bytes = MakeNestedCBOR(300); std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::OK, status.error); EXPECT_EQ(Status::npos(), status.pos); @@ -1081,8 +1075,8 @@ TEST(ParseCBORTest, StackLimitExceededError) { std::vector bytes = MakeNestedCBOR(301); std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_STACK_LIMIT_EXCEEDED, status.error); EXPECT_EQ(opening_segment_size * 301, status.pos); @@ -1091,8 +1085,8 @@ TEST(ParseCBORTest, StackLimitExceededError) { std::vector bytes = MakeNestedCBOR(320); std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_STACK_LIMIT_EXCEEDED, status.error); EXPECT_EQ(opening_segment_size * 301, status.pos); @@ -1110,8 +1104,8 @@ TEST(ParseCBORTest, UnsupportedValueError) { std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_UNSUPPORTED_VALUE, status.error); EXPECT_EQ(error_pos, status.pos); @@ -1133,8 +1127,8 @@ TEST(ParseCBORTest, InvalidString16Error) { EXPECT_EQ(kPayloadLen, bytes.size() - 6); std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_INVALID_STRING16, status.error); EXPECT_EQ(error_pos, status.pos); @@ -1153,8 +1147,8 @@ TEST(ParseCBORTest, InvalidString8Error) { EXPECT_EQ(kPayloadLen, bytes.size() - 6); std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_INVALID_STRING8, status.error); EXPECT_EQ(error_pos, status.pos); @@ -1175,8 +1169,8 @@ TEST(ParseCBORTest, InvalidBinaryError) { EXPECT_EQ(kPayloadLen, bytes.size() - 6); std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_INVALID_BINARY, status.error); EXPECT_EQ(error_pos, status.pos); @@ -1196,8 +1190,8 @@ TEST(ParseCBORTest, InvalidDoubleError) { EXPECT_EQ(kPayloadLen, bytes.size() - 6); std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_INVALID_DOUBLE, status.error); EXPECT_EQ(error_pos, status.pos); @@ -1217,8 +1211,8 @@ TEST(ParseCBORTest, InvalidSignedError) { EXPECT_EQ(kPayloadLen, bytes.size() - 6); std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_INVALID_INT32, status.error); EXPECT_EQ(error_pos, status.pos); @@ -1240,8 +1234,8 @@ TEST(ParseCBORTest, TrailingJunk) { std::numeric_limits::max(), &bytes); std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_TRAILING_JUNK, status.error); EXPECT_EQ(error_pos, status.pos); @@ -1263,8 +1257,8 @@ TEST(ParseCBORTest, EnvelopeContentsLengthMismatch) { std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(span(bytes.data(), bytes.size()), json_writer.get()); EXPECT_EQ(Error::CBOR_ENVELOPE_CONTENTS_LENGTH_MISMATCH, status.error); EXPECT_EQ(bytes.size(), status.pos); @@ -1298,8 +1292,8 @@ TYPED_TEST(AppendString8EntryToMapTest, AppendsEntrySuccessfully) { EXPECT_EQ(Error::OK, status.error); EXPECT_EQ(Status::npos(), status.pos); std::string out; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(SpanFrom(msg), json_writer.get()); EXPECT_EQ("{\"key\":\"value\",\"foo\":\"bar\"}", out); EXPECT_EQ(Error::OK, status.error); @@ -1321,8 +1315,8 @@ TYPED_TEST(AppendString8EntryToMapTest, AppendThreeEntries) { TypeParam msg(encoded.begin(), encoded.end()); std::string out; Status status; - std::unique_ptr json_writer = - NewJSONEncoder(&GetTestPlatform(), &out, &status); + std::unique_ptr json_writer = + json::NewJSONEncoder(&GetTestPlatform(), &out, &status); ParseCBOR(SpanFrom(msg), json_writer.get()); EXPECT_EQ("{\"key\":\"value\",\"key1\":\"value1\",\"key2\":\"value2\"}", out); EXPECT_EQ(Error::OK, status.error); diff --git a/third_party/inspector_protocol/crdtp/json.cc b/third_party/inspector_protocol/crdtp/json.cc index 3df9c7b962..31c0b6533a 100644 --- a/third_party/inspector_protocol/crdtp/json.cc +++ b/third_party/inspector_protocol/crdtp/json.cc @@ -102,7 +102,7 @@ void Base64Encode(const span& in, C* out) { // Implements a handler for JSON parser events to emit a JSON string. template -class JSONEncoder : public StreamingParserHandler { +class JSONEncoder : public ParserHandler { public: JSONEncoder(const Platform* platform, C* out, Status* status) : platform_(platform), out_(out), status_(status) { @@ -343,18 +343,17 @@ class JSONEncoder : public StreamingParserHandler { }; } // namespace -std::unique_ptr NewJSONEncoder( - const Platform* platform, - std::vector* out, - Status* status) { - return std::unique_ptr( +std::unique_ptr NewJSONEncoder(const Platform* platform, + std::vector* out, + Status* status) { + return std::unique_ptr( new JSONEncoder>(platform, out, status)); } -std::unique_ptr NewJSONEncoder(const Platform* platform, - std::string* out, - Status* status) { - return std::unique_ptr( +std::unique_ptr NewJSONEncoder(const Platform* platform, + std::string* out, + Status* status) { + return std::unique_ptr( new JSONEncoder(platform, out, status)); } @@ -388,7 +387,7 @@ const char* const kFalseString = "false"; template class JsonParser { public: - JsonParser(const Platform* platform, StreamingParserHandler* handler) + JsonParser(const Platform* platform, ParserHandler* handler) : platform_(platform), handler_(handler) {} void Parse(const Char* start, size_t length) { @@ -968,20 +967,20 @@ class JsonParser { const Char* start_pos_ = nullptr; bool error_ = false; const Platform* platform_; - StreamingParserHandler* handler_; + ParserHandler* handler_; }; } // namespace void ParseJSON(const Platform& platform, span chars, - StreamingParserHandler* handler) { + ParserHandler* handler) { JsonParser parser(&platform, handler); parser.Parse(chars.data(), chars.size()); } void ParseJSON(const Platform& platform, span chars, - StreamingParserHandler* handler) { + ParserHandler* handler) { JsonParser parser(&platform, handler); parser.Parse(chars.data(), chars.size()); } @@ -994,7 +993,7 @@ Status ConvertCBORToJSONTmpl(const Platform& platform, span cbor, C* json) { Status status; - std::unique_ptr json_writer = + std::unique_ptr json_writer = NewJSONEncoder(&platform, json, &status); cbor::ParseCBOR(cbor, json_writer.get()); return status; @@ -1015,8 +1014,7 @@ Status ConvertCBORToJSON(const Platform& platform, template Status ConvertJSONToCBORTmpl(const Platform& platform, span json, C* cbor) { Status status; - std::unique_ptr encoder = - cbor::NewCBOREncoder(cbor, &status); + std::unique_ptr encoder = cbor::NewCBOREncoder(cbor, &status); ParseJSON(platform, json, encoder.get()); return status; } diff --git a/third_party/inspector_protocol/crdtp/json.h b/third_party/inspector_protocol/crdtp/json.h index 37670deb91..da1a7fbe25 100644 --- a/third_party/inspector_protocol/crdtp/json.h +++ b/third_party/inspector_protocol/crdtp/json.h @@ -5,14 +5,7 @@ #ifndef V8_CRDTP_JSON_H_ #define V8_CRDTP_JSON_H_ -#include -#include -#include -#include -#include #include -#include -#include #include "export.h" #include "json_platform.h" @@ -30,13 +23,12 @@ namespace json { // Except for calling the HandleError routine at any time, the client // code must call the Handle* methods in an order in which they'd occur // in valid JSON; otherwise we may crash (the code uses assert). -std::unique_ptr NewJSONEncoder( - const Platform* platform, - std::vector* out, - Status* status); -std::unique_ptr NewJSONEncoder(const Platform* platform, - std::string* out, - Status* status); +std::unique_ptr NewJSONEncoder(const Platform* platform, + std::vector* out, + Status* status); +std::unique_ptr NewJSONEncoder(const Platform* platform, + std::string* out, + Status* status); // ============================================================================= // json::ParseJSON - for receiving streaming parser events for JSON @@ -44,10 +36,10 @@ std::unique_ptr NewJSONEncoder(const Platform* platform, void ParseJSON(const Platform& platform, span chars, - StreamingParserHandler* handler); + ParserHandler* handler); void ParseJSON(const Platform& platform, span chars, - StreamingParserHandler* handler); + ParserHandler* handler); // ============================================================================= // json::ConvertCBORToJSON, json::ConvertJSONToCBOR - for transcoding diff --git a/third_party/inspector_protocol/crdtp/json_platform.h b/third_party/inspector_protocol/crdtp/json_platform.h index 8d5babb949..7e17de9995 100644 --- a/third_party/inspector_protocol/crdtp/json_platform.h +++ b/third_party/inspector_protocol/crdtp/json_platform.h @@ -5,15 +5,7 @@ #ifndef V8_CRDTP_JSON_PLATFORM_H_ #define V8_CRDTP_JSON_PLATFORM_H_ -#include -#include -#include -#include -#include #include -#include -#include - #include "export.h" namespace v8_crdtp { diff --git a/third_party/inspector_protocol/crdtp/json_test.cc b/third_party/inspector_protocol/crdtp/json_test.cc index b0ad30fa59..c6281944a0 100644 --- a/third_party/inspector_protocol/crdtp/json_test.cc +++ b/third_party/inspector_protocol/crdtp/json_test.cc @@ -16,8 +16,6 @@ #include "test_platform.h" -using testing::ElementsAreArray; - namespace v8_crdtp { namespace { class TestPlatform : public json::Platform { @@ -61,14 +59,14 @@ namespace json { // json::NewJSONEncoder - for encoding streaming parser events as JSON // ============================================================================= -void WriteUTF8AsUTF16(StreamingParserHandler* writer, const std::string& utf8) { +void WriteUTF8AsUTF16(ParserHandler* writer, const std::string& utf8) { writer->HandleString16(SpanFrom(UTF8ToUTF16(SpanFrom(utf8)))); } TEST(JsonEncoder, OverlongEncodings) { std::string out; Status status; - std::unique_ptr writer = + std::unique_ptr writer = NewJSONEncoder(&GetTestPlatform(), &out, &status); // We encode 0x7f, which is the DEL ascii character, as a 4 byte UTF8 @@ -87,7 +85,7 @@ TEST(JsonEncoder, OverlongEncodings) { TEST(JsonEncoder, IncompleteUtf8Sequence) { std::string out; Status status; - std::unique_ptr writer = + std::unique_ptr writer = NewJSONEncoder(&GetTestPlatform(), &out, &status); writer->HandleArrayBegin(); // This emits [, which starts an array. @@ -113,7 +111,7 @@ TEST(JsonEncoder, IncompleteUtf8Sequence) { TEST(JsonStdStringWriterTest, HelloWorld) { std::string out; Status status; - std::unique_ptr writer = + std::unique_ptr writer = NewJSONEncoder(&GetTestPlatform(), &out, &status); writer->HandleMapBegin(); WriteUTF8AsUTF16(writer.get(), "msg1"); @@ -157,7 +155,7 @@ TEST(JsonStdStringWriterTest, RepresentingNonFiniteValuesAsNull) { // So in practice it's mapped to null. std::string out; Status status; - std::unique_ptr writer = + std::unique_ptr writer = NewJSONEncoder(&GetTestPlatform(), &out, &status); writer->HandleMapBegin(); writer->HandleString8(SpanFrom("Infinity")); @@ -172,13 +170,13 @@ TEST(JsonStdStringWriterTest, RepresentingNonFiniteValuesAsNull) { } TEST(JsonStdStringWriterTest, BinaryEncodedAsJsonString) { - // The encoder emits binary submitted to StreamingParserHandler::HandleBinary + // The encoder emits binary submitted to ParserHandler::HandleBinary // as base64. The following three examples are taken from // https://en.wikipedia.org/wiki/Base64. { std::string out; Status status; - std::unique_ptr writer = + std::unique_ptr writer = NewJSONEncoder(&GetTestPlatform(), &out, &status); writer->HandleBinary(SpanFrom(std::vector({'M', 'a', 'n'}))); EXPECT_TRUE(status.ok()); @@ -187,7 +185,7 @@ TEST(JsonStdStringWriterTest, BinaryEncodedAsJsonString) { { std::string out; Status status; - std::unique_ptr writer = + std::unique_ptr writer = NewJSONEncoder(&GetTestPlatform(), &out, &status); writer->HandleBinary(SpanFrom(std::vector({'M', 'a'}))); EXPECT_TRUE(status.ok()); @@ -196,7 +194,7 @@ TEST(JsonStdStringWriterTest, BinaryEncodedAsJsonString) { { std::string out; Status status; - std::unique_ptr writer = + std::unique_ptr writer = NewJSONEncoder(&GetTestPlatform(), &out, &status); writer->HandleBinary(SpanFrom(std::vector({'M'}))); EXPECT_TRUE(status.ok()); @@ -205,7 +203,7 @@ TEST(JsonStdStringWriterTest, BinaryEncodedAsJsonString) { { // "Hello, world.", verified with base64decode.org. std::string out; Status status; - std::unique_ptr writer = + std::unique_ptr writer = NewJSONEncoder(&GetTestPlatform(), &out, &status); writer->HandleBinary(SpanFrom(std::vector( {'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '.'}))); @@ -219,7 +217,7 @@ TEST(JsonStdStringWriterTest, HandlesErrors) { // status and clears the output. std::string out; Status status; - std::unique_ptr writer = + std::unique_ptr writer = NewJSONEncoder(&GetTestPlatform(), &out, &status); writer->HandleMapBegin(); WriteUTF8AsUTF16(writer.get(), "msg1"); @@ -257,7 +255,7 @@ TEST(JsonStdStringWriterTest, DoubleToString) { std::string out; Status status; - std::unique_ptr writer = + std::unique_ptr writer = NewJSONEncoder(&platform, &out, &status); writer->HandleArrayBegin(); writer->HandleDouble(.1); @@ -270,7 +268,7 @@ TEST(JsonStdStringWriterTest, DoubleToString) { // json::ParseJSON - for receiving streaming parser events for JSON // ============================================================================= -class Log : public StreamingParserHandler { +class Log : public ParserHandler { public: void HandleMapBegin() override { log_ << "map begin\n"; } diff --git a/third_party/inspector_protocol/crdtp/parser_handler.h b/third_party/inspector_protocol/crdtp/parser_handler.h index aaa1d14af7..47bbe6d5e8 100644 --- a/third_party/inspector_protocol/crdtp/parser_handler.h +++ b/third_party/inspector_protocol/crdtp/parser_handler.h @@ -5,16 +5,7 @@ #ifndef V8_CRDTP_PARSER_HANDLER_H_ #define V8_CRDTP_PARSER_HANDLER_H_ -#include -#include #include -#include -#include -#include -#include -#include - -#include "export.h" #include "span.h" #include "status.h" @@ -22,9 +13,9 @@ namespace v8_crdtp { // Handler interface for parser events emitted by a streaming parser. // See cbor::NewCBOREncoder, cbor::ParseCBOR, json::NewJSONEncoder, // json::ParseJSON. -class StreamingParserHandler { +class ParserHandler { public: - virtual ~StreamingParserHandler() = default; + virtual ~ParserHandler() = default; virtual void HandleMapBegin() = 0; virtual void HandleMapEnd() = 0; virtual void HandleArrayBegin() = 0; diff --git a/third_party/inspector_protocol/crdtp/span.h b/third_party/inspector_protocol/crdtp/span.h index 0d391a13fc..e8d012a24d 100644 --- a/third_party/inspector_protocol/crdtp/span.h +++ b/third_party/inspector_protocol/crdtp/span.h @@ -6,16 +6,11 @@ #define V8_CRDTP_SPAN_H_ #include -#include #include #include -#include -#include #include #include -#include "export.h" - namespace v8_crdtp { // ============================================================================= // span - sequence of bytes diff --git a/third_party/inspector_protocol/crdtp/span_test.cc b/third_party/inspector_protocol/crdtp/span_test.cc index 2f8154c41e..3e8256d152 100644 --- a/third_party/inspector_protocol/crdtp/span_test.cc +++ b/third_party/inspector_protocol/crdtp/span_test.cc @@ -2,22 +2,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "span.h" - -#include -#include -#include #include -#include -#include -#include -#include #include +#include "span.h" #include "test_platform.h" -using testing::ElementsAreArray; - namespace v8_crdtp { // ============================================================================= // span - sequence of bytes diff --git a/third_party/inspector_protocol/crdtp/status.cc b/third_party/inspector_protocol/crdtp/status.cc index 05a296f79d..57cb795985 100644 --- a/third_party/inspector_protocol/crdtp/status.cc +++ b/third_party/inspector_protocol/crdtp/status.cc @@ -4,13 +4,6 @@ #include "status.h" -#include -#include -#include -#include -#include -#include - namespace v8_crdtp { // ============================================================================= // Status and Error codes diff --git a/third_party/inspector_protocol/crdtp/status.h b/third_party/inspector_protocol/crdtp/status.h index 30d7d7d7f8..bf75615c09 100644 --- a/third_party/inspector_protocol/crdtp/status.h +++ b/third_party/inspector_protocol/crdtp/status.h @@ -2,17 +2,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef V8_CRDTP_ENCODING_H_ -#define V8_CRDTP_ENCODING_H_ +#ifndef V8_CRDTP_STATUS_H_ +#define V8_CRDTP_STATUS_H_ -#include #include -#include -#include #include -#include #include -#include #include "export.h" @@ -20,6 +15,7 @@ namespace v8_crdtp { // ============================================================================= // Status and Error codes // ============================================================================= + enum class Error { OK = 0, // JSON parsing errors - json_parser.{h,cc}. @@ -89,4 +85,4 @@ struct Status { }; } // namespace v8_crdtp -#endif // V8_CRDTP_ENCODING_H_ +#endif // V8_CRDTP_STATUS_H_ diff --git a/third_party/inspector_protocol/crdtp/status_test.cc b/third_party/inspector_protocol/crdtp/status_test.cc index fc81590ff3..4b81dd8cb1 100644 --- a/third_party/inspector_protocol/crdtp/status_test.cc +++ b/third_party/inspector_protocol/crdtp/status_test.cc @@ -3,21 +3,8 @@ // found in the LICENSE file. #include "status.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - #include "test_platform.h" -using testing::ElementsAreArray; - namespace v8_crdtp { // ============================================================================= // Status and Error codes diff --git a/third_party/inspector_protocol/lib/DispatcherBase_cpp.template b/third_party/inspector_protocol/lib/DispatcherBase_cpp.template index 0b18529162..22fabf9bfb 100644 --- a/third_party/inspector_protocol/lib/DispatcherBase_cpp.template +++ b/third_party/inspector_protocol/lib/DispatcherBase_cpp.template @@ -317,13 +317,12 @@ std::unique_ptr InternalResponse::createErrorResponse(int callId, void InternalResponse::AppendSerialized(std::vector* out) const { using {{config.crdtp.namespace}}::cbor::NewCBOREncoder; - using {{config.crdtp.namespace}}::StreamingParserHandler; + using {{config.crdtp.namespace}}::ParserHandler; using {{config.crdtp.namespace}}::Status; using {{config.crdtp.namespace}}::SpanFrom; Status status; - std::unique_ptr encoder = - NewCBOREncoder(out, &status); + std::unique_ptr encoder = NewCBOREncoder(out, &status); encoder->HandleMapBegin(); if (m_method) { encoder->HandleString8(SpanFrom("method"));