Commit 69fcc619 authored by Johannes Henkel's avatar Johannes Henkel Committed by Commit Bot

UiDevToolsClient: Convert incoming messages to CBOR.

We'll be doing away with StringUtil::parseMessage(..., false)
which invokes a JSON parser. So, in preparation, I'd like to
convert the incoming traffic to CBOR. StringUtil::parseMessage(..., true)
will then still make a protocol::Value but using a CBOR parser.

Later on, we will be able to extract call_id / method
with a shallow parse. So the plan is to have the raw messages
be in CBOR format.

I did a small amount of cleanup as well to make this easier to
read (inlining the conversion, adding a method applying to
both sending response and notification).

Change-Id: I6659c46f8e59dc15442b08fbf9f9999a2b353986
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1954385Reviewed-by: default avatarLeonard Grey <lgrey@chromium.org>
Commit-Queue: Johannes Henkel <johannes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#722612}
parent bf75ce08
......@@ -30,14 +30,19 @@ void UiDevToolsClient::Disconnect() {
DisableAllAgents();
}
void UiDevToolsClient::Dispatch(const std::string& data) {
void UiDevToolsClient::Dispatch(const std::string& json) {
std::string cbor;
crdtp::Status status =
crdtp::json::ConvertJSONToCBOR(crdtp::SpanFrom(json), &cbor);
LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
int call_id;
std::string method;
std::unique_ptr<protocol::Value> protocolCommand =
protocol::StringUtil::parseMessage(data, false);
protocol::StringUtil::parseMessage(cbor, true);
if (dispatcher_.parseCommand(protocolCommand.get(), &call_id, &method)) {
dispatcher_.dispatch(call_id, method, std::move(protocolCommand),
crdtp::SpanFrom(data));
crdtp::SpanFrom(cbor));
}
}
......@@ -58,32 +63,28 @@ void UiDevToolsClient::DisableAllAgents() {
agent->Disable();
}
namespace {
std::string SerializeToJSON(std::unique_ptr<protocol::Serializable> message) {
void UiDevToolsClient::MaybeSendProtocolResponseOrNotification(
std::unique_ptr<protocol::Serializable> message) {
if (!connected())
return;
std::vector<uint8_t> cbor = std::move(*message).TakeSerialized();
std::string json;
crdtp::Status status =
crdtp::json::ConvertCBORToJSON(crdtp::SpanFrom(cbor), &json);
LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
return json;
server_->SendOverWebSocket(connection_id_, base::StringPiece(json));
}
} // namespace
void UiDevToolsClient::sendProtocolResponse(
int callId,
std::unique_ptr<protocol::Serializable> message) {
if (connected()) {
server_->SendOverWebSocket(
connection_id_, base::StringPiece(SerializeToJSON(std::move(message))));
}
MaybeSendProtocolResponseOrNotification(std::move(message));
}
void UiDevToolsClient::sendProtocolNotification(
std::unique_ptr<protocol::Serializable> message) {
if (connected()) {
server_->SendOverWebSocket(
connection_id_, base::StringPiece(SerializeToJSON(std::move(message))));
}
MaybeSendProtocolResponseOrNotification(std::move(message));
}
void UiDevToolsClient::flushProtocolNotifications() {
......
......@@ -30,7 +30,7 @@ class UI_DEVTOOLS_EXPORT UiDevToolsClient : public protocol::FrontendChannel {
void AddAgent(std::unique_ptr<UiDevToolsAgent> agent);
void Disconnect();
void Dispatch(const std::string& data);
void Dispatch(const std::string& json);
bool connected() const;
void set_connection_id(int connection_id);
......@@ -38,6 +38,8 @@ class UI_DEVTOOLS_EXPORT UiDevToolsClient : public protocol::FrontendChannel {
private:
void DisableAllAgents();
void MaybeSendProtocolResponseOrNotification(
std::unique_ptr<protocol::Serializable> message);
// protocol::FrontendChannel
void sendProtocolResponse(
......
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