Skip to content

Commit 1b62408

Browse files
committed
inspector: roll inspector_protocol
Roll the inspector_protocol library to match V8's inspector_protocol revision. Update the node inspector to use the new `crdtp` protocol library.
1 parent 428c4fb commit 1b62408

File tree

86 files changed

+10903
-6213
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+10903
-6213
lines changed

src/inspector/network_agent.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ void NetworkAgent::Wire(UberDispatcher* dispatcher) {
5353

5454
DispatchResponse NetworkAgent::enable() {
5555
inspector_->Enable();
56-
return DispatchResponse::OK();
56+
return DispatchResponse::Success();
5757
}
5858

5959
DispatchResponse NetworkAgent::disable() {
6060
inspector_->Disable();
61-
return DispatchResponse::OK();
61+
return DispatchResponse::Success();
6262
}
6363

6464
void NetworkAgent::requestWillBeSent(
@@ -78,7 +78,7 @@ void NetworkAgent::requestWillBeSent(
7878
ErrorSupport errors;
7979
auto headers =
8080
Network::Headers::fromValue(request->getObject("headers"), &errors);
81-
if (errors.hasErrors()) {
81+
if (!errors.Errors().empty()) {
8282
headers = std::make_unique<Network::Headers>(DictionaryValue::create());
8383
}
8484

@@ -107,7 +107,7 @@ void NetworkAgent::responseReceived(
107107
ErrorSupport errors;
108108
auto headers =
109109
Network::Headers::fromValue(response->getObject("headers"), &errors);
110-
if (errors.hasErrors()) {
110+
if (!errors.Errors().empty()) {
111111
headers = std::make_unique<Network::Headers>(DictionaryValue::create());
112112
}
113113

src/inspector/node_inspector.gypi

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
'src/inspector_socket_server.h',
1616
'src/inspector/main_thread_interface.cc',
1717
'src/inspector/main_thread_interface.h',
18+
'src/inspector/node_json_protocol.cc',
19+
'src/inspector/node_json_protocol.h',
1820
'src/inspector/node_string.cc',
1921
'src/inspector/node_string.h',
2022
'src/inspector/runtime_agent.cc',
@@ -29,6 +31,30 @@
2931
'src/inspector/network_agent.h',
3032
'src/inspector/worker_inspector.cc',
3133
'src/inspector/worker_inspector.h',
34+
35+
'<(protocol_tool_path)/crdtp/cbor.cc',
36+
'<(protocol_tool_path)/crdtp/cbor.h',
37+
'<(protocol_tool_path)/crdtp/dispatch.cc',
38+
'<(protocol_tool_path)/crdtp/dispatch.h',
39+
'<(protocol_tool_path)/crdtp/error_support.cc',
40+
'<(protocol_tool_path)/crdtp/error_support.h',
41+
'<(protocol_tool_path)/crdtp/export.h',
42+
'<(protocol_tool_path)/crdtp/find_by_first.h',
43+
'<(protocol_tool_path)/crdtp/frontend_channel.h',
44+
'<(protocol_tool_path)/crdtp/json.cc',
45+
'<(protocol_tool_path)/crdtp/json.h',
46+
'<(protocol_tool_path)/crdtp/json_platform.cc',
47+
'<(protocol_tool_path)/crdtp/json_platform.h',
48+
'<(protocol_tool_path)/crdtp/maybe.h',
49+
'<(protocol_tool_path)/crdtp/parser_handler.h',
50+
'<(protocol_tool_path)/crdtp/protocol_core.cc',
51+
'<(protocol_tool_path)/crdtp/protocol_core.h',
52+
'<(protocol_tool_path)/crdtp/serializable.cc',
53+
'<(protocol_tool_path)/crdtp/serializable.h',
54+
'<(protocol_tool_path)/crdtp/span.cc',
55+
'<(protocol_tool_path)/crdtp/span.h',
56+
'<(protocol_tool_path)/crdtp/status.cc',
57+
'<(protocol_tool_path)/crdtp/status.h',
3258
],
3359
'node_inspector_generated_sources': [
3460
'<(SHARED_INTERMEDIATE_DIR)/src/node/inspector/protocol/Forward.h',
@@ -44,23 +70,11 @@
4470
'<(SHARED_INTERMEDIATE_DIR)/src/node/inspector/protocol/Network.h',
4571
],
4672
'node_protocol_files': [
47-
'<(protocol_tool_path)/lib/Allocator_h.template',
48-
'<(protocol_tool_path)/lib/base_string_adapter_cc.template',
49-
'<(protocol_tool_path)/lib/base_string_adapter_h.template',
50-
'<(protocol_tool_path)/lib/DispatcherBase_cpp.template',
51-
'<(protocol_tool_path)/lib/DispatcherBase_h.template',
52-
'<(protocol_tool_path)/lib/encoding_cpp.template',
53-
'<(protocol_tool_path)/lib/encoding_h.template',
54-
'<(protocol_tool_path)/lib/ErrorSupport_cpp.template',
55-
'<(protocol_tool_path)/lib/ErrorSupport_h.template',
5673
'<(protocol_tool_path)/lib/Forward_h.template',
57-
'<(protocol_tool_path)/lib/FrontendChannel_h.template',
58-
'<(protocol_tool_path)/lib/Maybe_h.template',
5974
'<(protocol_tool_path)/lib/Object_cpp.template',
6075
'<(protocol_tool_path)/lib/Object_h.template',
61-
'<(protocol_tool_path)/lib/Parser_cpp.template',
62-
'<(protocol_tool_path)/lib/Parser_h.template',
6376
'<(protocol_tool_path)/lib/Protocol_cpp.template',
77+
'<(protocol_tool_path)/lib/ValueConversions_cpp.template',
6478
'<(protocol_tool_path)/lib/ValueConversions_h.template',
6579
'<(protocol_tool_path)/lib/Values_cpp.template',
6680
'<(protocol_tool_path)/lib/Values_h.template',
@@ -77,6 +91,7 @@
7791
'<@(node_inspector_sources)',
7892
],
7993
'include_dirs': [
94+
'<(protocol_tool_path)',
8095
'<(SHARED_INTERMEDIATE_DIR)/include', # for inspector
8196
'<(SHARED_INTERMEDIATE_DIR)',
8297
'<(SHARED_INTERMEDIATE_DIR)/src', # for inspector
@@ -112,6 +127,7 @@
112127
'action': [
113128
'<(python)',
114129
'tools/inspector_protocol/code_generator.py',
130+
'--inspector_protocol_dir', 'tools/inspector_protocol/',
115131
'--jinja_dir', '<@(protocol_tool_path)',
116132
'--output_base', '<(SHARED_INTERMEDIATE_DIR)/src/',
117133
'--config', 'src/inspector/node_protocol_config.json',

src/inspector/node_json_protocol.cc

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#include "node_json_protocol.h"
2+
3+
#include "crdtp/json.h"
4+
5+
namespace node {
6+
namespace inspector {
7+
8+
using crdtp::ParserHandler;
9+
using crdtp::span;
10+
using crdtp::Status;
11+
using crdtp::json::ParseJSON;
12+
using protocol::Binary;
13+
using protocol::BinaryValue;
14+
using protocol::DictionaryValue;
15+
using protocol::FundamentalValue;
16+
using protocol::ListValue;
17+
using protocol::String;
18+
using protocol::StringUtil;
19+
using protocol::StringValue;
20+
using protocol::Value;
21+
22+
namespace {
23+
24+
// Uses the parsing events received from driver of `ParserHandler`
25+
// (e.g. crdtp::json::ParseJSON) into a protocol::Value instance.
26+
class ValueParserHandler : public ParserHandler {
27+
public:
28+
// Provides the parsed protocol::Value.
29+
std::unique_ptr<Value> ReleaseRoot() { return std::move(root_); }
30+
31+
// The first parsing error encountered; `status().ok()` is the default.
32+
Status status() const { return status_; }
33+
34+
private:
35+
// Implementation of `ParserHandler`.
36+
void HandleMapBegin() override {
37+
if (!status_.ok()) return;
38+
std::unique_ptr<DictionaryValue> dict = DictionaryValue::create();
39+
DictionaryValue* dict_ptr = dict.get();
40+
AddValueToParent(std::move(dict));
41+
stack_.emplace_back(dict_ptr);
42+
}
43+
44+
void HandleMapEnd() override {
45+
if (!status_.ok()) return;
46+
DCHECK(!stack_.empty());
47+
DCHECK(stack_.back().is_dict);
48+
stack_.pop_back();
49+
}
50+
51+
void HandleArrayBegin() override {
52+
if (!status_.ok()) return;
53+
std::unique_ptr<ListValue> list = ListValue::create();
54+
ListValue* list_ptr = list.get();
55+
AddValueToParent(std::move(list));
56+
stack_.emplace_back(list_ptr);
57+
}
58+
59+
void HandleArrayEnd() override {
60+
if (!status_.ok()) return;
61+
DCHECK(!stack_.empty());
62+
DCHECK(!stack_.back().is_dict);
63+
stack_.pop_back();
64+
}
65+
66+
void HandleString8(span<uint8_t> chars) override {
67+
AddStringToParent(StringUtil::fromUTF8(chars.data(), chars.size()));
68+
}
69+
70+
void HandleString16(span<uint16_t> chars) override {
71+
AddStringToParent(StringUtil::fromUTF16LE(chars.data(), chars.size()));
72+
}
73+
74+
void HandleBinary(span<uint8_t> bytes) override {
75+
AddValueToParent(
76+
BinaryValue::create(Binary::fromSpan(bytes.data(), bytes.size())));
77+
}
78+
79+
void HandleDouble(double value) override {
80+
AddValueToParent(FundamentalValue::create(value));
81+
}
82+
83+
void HandleInt32(int32_t value) override {
84+
AddValueToParent(FundamentalValue::create(value));
85+
}
86+
87+
void HandleBool(bool value) override {
88+
AddValueToParent(FundamentalValue::create(value));
89+
}
90+
91+
void HandleNull() override { AddValueToParent(Value::null()); }
92+
93+
void HandleError(Status error) override { status_ = error; }
94+
95+
// Adding strings and values to the parent value.
96+
// Strings are handled separately because they can be keys for
97+
// dictionary values.
98+
void AddStringToParent(String str) {
99+
if (!status_.ok()) return;
100+
if (!root_) {
101+
DCHECK(!key_is_pending_);
102+
root_ = StringValue::create(str);
103+
} else if (stack_.back().is_dict) {
104+
// If we already have a pending key, then this is the value of the
105+
// key/value pair. Otherwise, it's the new pending key.
106+
if (key_is_pending_) {
107+
stack_.back().dict->setString(pending_key_, str);
108+
key_is_pending_ = false;
109+
} else {
110+
pending_key_ = std::move(str);
111+
key_is_pending_ = true;
112+
}
113+
} else { // Top of the stack is a list.
114+
DCHECK(!key_is_pending_);
115+
stack_.back().list->pushValue(StringValue::create(str));
116+
}
117+
}
118+
119+
void AddValueToParent(std::unique_ptr<Value> value) {
120+
if (!status_.ok()) return;
121+
if (!root_) {
122+
DCHECK(!key_is_pending_);
123+
root_ = std::move(value);
124+
} else if (stack_.back().is_dict) {
125+
DCHECK(key_is_pending_);
126+
stack_.back().dict->setValue(pending_key_, std::move(value));
127+
key_is_pending_ = false;
128+
} else { // Top of the stack is a list.
129+
DCHECK(!key_is_pending_);
130+
stack_.back().list->pushValue(std::move(value));
131+
}
132+
}
133+
134+
// `status_.ok()` is the default; if we receive an error event
135+
// we keep the first one and stop modifying any other state.
136+
Status status_;
137+
138+
// The root of the parsed protocol::Value tree.
139+
std::unique_ptr<Value> root_;
140+
141+
// If root_ is a list or a dictionary, this stack keeps track of
142+
// the container we're currently parsing as well as its ancestors.
143+
struct ContainerState {
144+
explicit ContainerState(DictionaryValue* dict)
145+
: is_dict(true), dict(dict) {}
146+
explicit ContainerState(ListValue* list) : is_dict(false), list(list) {}
147+
148+
bool is_dict;
149+
union {
150+
DictionaryValue* dict;
151+
ListValue* list;
152+
};
153+
};
154+
std::vector<ContainerState> stack_;
155+
156+
// For maps, keys and values are alternating events, so we keep the
157+
// key around and process it when the value arrives.
158+
bool key_is_pending_ = false;
159+
String pending_key_;
160+
};
161+
} // anonymous namespace
162+
163+
std::unique_ptr<protocol::Value> ProtocolValueParseJSON(const uint8_t* chars,
164+
size_t size) {
165+
ValueParserHandler handler;
166+
ParseJSON(span<uint8_t>(chars, size), &handler);
167+
if (handler.status().ok()) return handler.ReleaseRoot();
168+
return nullptr;
169+
}
170+
171+
} // namespace inspector
172+
} // namespace node

src/inspector/node_json_protocol.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef SRC_INSPECTOR_NODE_JSON_PROTOCOL_H_
2+
#define SRC_INSPECTOR_NODE_JSON_PROTOCOL_H_
3+
4+
#include "node/inspector/protocol/Protocol.h"
5+
6+
namespace node {
7+
namespace inspector {
8+
9+
// Parse s JSON string into protocol::Value.
10+
std::unique_ptr<protocol::Value> ProtocolValueParseJSON(const uint8_t* chars,
11+
size_t size);
12+
13+
} // namespace inspector
14+
} // namespace node
15+
16+
#endif // SRC_INSPECTOR_NODE_JSON_PROTOCOL_H_

src/inspector/node_protocol_config.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"protocol": {
3-
"path": "node_protocol.json",
3+
"path": "node_protocol.pdl",
44
"package": "src/node/inspector/protocol",
55
"output": "node/inspector/protocol",
66
"namespace": ["node", "inspector", "protocol"]
@@ -18,5 +18,8 @@
1818
"package": "src/node/inspector/protocol",
1919
"output": "node/inspector/protocol",
2020
"string_header": "inspector/node_string.h"
21+
},
22+
"crdtp": {
23+
"dir": "crdtp"
2124
}
2225
}

0 commit comments

Comments
 (0)