Commit 580c1164 authored by Johannes Henkel's avatar Johannes Henkel Committed by Commit Bot

[DevTools] Reduce duplication in blink DevToolsSession.

Adds a DevToolsSession::FinalizeMessage method which is called
just before the message is put on the wire, converting the
message to JSON if needed, and sticking it inside a Mojo object.

Change-Id: Ie9005a79e3dabf65810ebd727a06460dcf79e9ee
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1846248Reviewed-by: default avatarAndrey Kosyakov <caseq@chromium.org>
Commit-Queue: Johannes Henkel <johannes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#703833}
parent 3bf76b27
...@@ -56,20 +56,6 @@ Vector<uint8_t> UnwrapMessage(const mojom::blink::DevToolsMessagePtr& message) { ...@@ -56,20 +56,6 @@ Vector<uint8_t> UnwrapMessage(const mojom::blink::DevToolsMessagePtr& message) {
return unwrap_message; return unwrap_message;
} }
mojom::blink::DevToolsMessagePtr WrapMessage(
protocol::ProtocolMessage message) {
auto result = mojom::blink::DevToolsMessage::New();
if (message.json.IsEmpty()) {
result->data = message.binary.ReleaseVector();
} else {
WTF::StringUTF8Adaptor adaptor(message.json);
result->data =
mojo_base::BigBuffer(base::as_bytes(base::make_span(adaptor)));
}
return result;
}
protocol::ProtocolMessage ToProtocolMessage( protocol::ProtocolMessage ToProtocolMessage(
std::unique_ptr<v8_inspector::StringBuffer> buffer) { std::unique_ptr<v8_inspector::StringBuffer> buffer) {
protocol::ProtocolMessage message; protocol::ProtocolMessage message;
...@@ -331,15 +317,7 @@ void DevToolsSession::SendProtocolResponse( ...@@ -331,15 +317,7 @@ void DevToolsSession::SendProtocolResponse(
if (WebTestSupport::IsRunningWebTest()) if (WebTestSupport::IsRunningWebTest())
agent_->FlushProtocolNotifications(); agent_->FlushProtocolNotifications();
mojom::blink::DevToolsMessagePtr serialized = WrapMessage(message); host_remote_->DispatchProtocolResponse(FinalizeMessage(message), call_id,
if (!client_expects_binary_responses_) {
std::vector<uint8_t> json;
IPEStatus status = ConvertCBORToJSON(
span<uint8_t>(serialized->data.data(), serialized->data.size()), &json);
CHECK(status.ok()) << status.ToASCIIString();
serialized->data = mojo_base::BigBuffer(json);
}
host_remote_->DispatchProtocolResponse(std::move(serialized), call_id,
session_state_.TakeUpdates()); session_state_.TakeUpdates());
} }
...@@ -364,16 +342,16 @@ class DevToolsSession::Notification { ...@@ -364,16 +342,16 @@ class DevToolsSession::Notification {
std::unique_ptr<v8_inspector::StringBuffer> notification) std::unique_ptr<v8_inspector::StringBuffer> notification)
: v8_notification_(std::move(notification)) {} : v8_notification_(std::move(notification)) {}
mojom::blink::DevToolsMessagePtr Serialize() { protocol::ProtocolMessage Serialize() {
protocol::ProtocolMessage serialized; protocol::ProtocolMessage result;
if (blink_notification_) { if (blink_notification_) {
serialized = blink_notification_->serialize(/*binary=*/true); result = blink_notification_->serialize(/*binary=*/true);
blink_notification_.reset(); blink_notification_.reset();
} else if (v8_notification_) { } else if (v8_notification_) {
serialized = ToProtocolMessage(std::move(v8_notification_)); result = ToProtocolMessage(std::move(v8_notification_));
v8_notification_.reset(); v8_notification_.reset();
} }
return WrapMessage(std::move(serialized)); return result;
} }
private: private:
...@@ -407,18 +385,9 @@ void DevToolsSession::flushProtocolNotifications() { ...@@ -407,18 +385,9 @@ void DevToolsSession::flushProtocolNotifications() {
if (v8_session_) if (v8_session_)
v8_session_state_cbor_.Set(v8_session_->state()); v8_session_state_cbor_.Set(v8_session_->state());
for (wtf_size_t i = 0; i < notification_queue_.size(); ++i) { for (wtf_size_t i = 0; i < notification_queue_.size(); ++i) {
mojom::blink::DevToolsMessagePtr serialized = host_remote_->DispatchProtocolNotification(
notification_queue_[i]->Serialize(); FinalizeMessage(notification_queue_[i]->Serialize()),
if (!client_expects_binary_responses_) { session_state_.TakeUpdates());
std::vector<uint8_t> json;
IPEStatus status = ConvertCBORToJSON(
span<uint8_t>(serialized->data.data(), serialized->data.size()),
&json);
CHECK(status.ok()) << status.ToASCIIString();
serialized->data = mojo_base::BigBuffer(json);
}
host_remote_->DispatchProtocolNotification(std::move(serialized),
session_state_.TakeUpdates());
} }
notification_queue_.clear(); notification_queue_.clear();
} }
...@@ -428,4 +397,18 @@ void DevToolsSession::Trace(blink::Visitor* visitor) { ...@@ -428,4 +397,18 @@ void DevToolsSession::Trace(blink::Visitor* visitor) {
visitor->Trace(agents_); visitor->Trace(agents_);
} }
blink::mojom::blink::DevToolsMessagePtr DevToolsSession::FinalizeMessage(
protocol::ProtocolMessage message) {
std::vector<uint8_t> message_to_send = message.binary.ReleaseVector();
if (!client_expects_binary_responses_) {
std::vector<uint8_t> json;
IPEStatus status = ConvertCBORToJSON(SpanFrom(message_to_send), &json);
CHECK(status.ok()) << status.ToASCIIString();
message_to_send = std::move(json);
}
auto mojo_msg = mojom::blink::DevToolsMessage::New();
mojo_msg->data = std::move(message_to_send);
return mojo_msg;
}
} // namespace blink } // namespace blink
...@@ -93,6 +93,10 @@ class CORE_EXPORT DevToolsSession : public GarbageCollected<DevToolsSession>, ...@@ -93,6 +93,10 @@ class CORE_EXPORT DevToolsSession : public GarbageCollected<DevToolsSession>,
void SendProtocolResponse(int call_id, void SendProtocolResponse(int call_id,
const protocol::ProtocolMessage& message); const protocol::ProtocolMessage& message);
// Converts to JSON if requested by the client.
blink::mojom::blink::DevToolsMessagePtr FinalizeMessage(
protocol::ProtocolMessage message);
Member<DevToolsAgent> agent_; Member<DevToolsAgent> agent_;
mojo::AssociatedReceiver<mojom::blink::DevToolsSession> receiver_; mojo::AssociatedReceiver<mojom::blink::DevToolsSession> receiver_;
mojo::AssociatedRemote<mojom::blink::DevToolsSessionHost> host_remote_; mojo::AssociatedRemote<mojom::blink::DevToolsSessionHost> host_remote_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment