Commit 4d5dd3db authored by Johannes Henkel's avatar Johannes Henkel Committed by Commit Bot

In inspector session, convert incoming messages to cbor and

serialize outgoing messages via cbor to json if needed.

Change-Id: I6d0300ddc27e365b16671d19922f467e09adcc54
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1636681Reviewed-by: 's avatarAlexei Filippov <alph@chromium.org>
Commit-Queue: Johannes Henkel <johannes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61973}
parent 8dcf591f
...@@ -161,53 +161,28 @@ protocol::DictionaryValue* V8InspectorSessionImpl::agentState( ...@@ -161,53 +161,28 @@ protocol::DictionaryValue* V8InspectorSessionImpl::agentState(
return state; return state;
} }
namespace { std::unique_ptr<StringBuffer> V8InspectorSessionImpl::serializeForFrontend(
std::unique_ptr<protocol::Serializable> message) {
class MessageBuffer : public StringBuffer { std::vector<uint8_t> cbor = message->serializeToBinary();
public: if (use_binary_protocol_)
static std::unique_ptr<MessageBuffer> create( return std::unique_ptr<StringBuffer>(
std::unique_ptr<protocol::Serializable> message, bool binary) { new BinaryStringBuffer(std::move(cbor)));
return std::unique_ptr<MessageBuffer>( std::vector<uint8_t> json;
new MessageBuffer(std::move(message), binary)); IPEStatus status = ConvertCBORToJSON(SpanFrom(cbor), &json);
} DCHECK(status.ok());
USE(status);
const StringView& string() override { String16 string16(reinterpret_cast<const char*>(json.data()), json.size());
if (!m_serialized) { return StringBufferImpl::adopt(string16);
if (m_binary) { }
// Encode binary response as an 8bit string buffer.
m_serialized.reset(
new BinaryStringBuffer(m_message->serializeToBinary()));
} else {
m_serialized =
StringBuffer::create(toStringView(m_message->serializeToJSON()));
}
m_message.reset(nullptr);
}
return m_serialized->string();
}
private:
explicit MessageBuffer(std::unique_ptr<protocol::Serializable> message,
bool binary)
: m_message(std::move(message)), m_binary(binary) {}
std::unique_ptr<protocol::Serializable> m_message;
std::unique_ptr<StringBuffer> m_serialized;
bool m_binary;
};
} // namespace
void V8InspectorSessionImpl::sendProtocolResponse( void V8InspectorSessionImpl::sendProtocolResponse(
int callId, std::unique_ptr<protocol::Serializable> message) { int callId, std::unique_ptr<protocol::Serializable> message) {
m_channel->sendResponse( m_channel->sendResponse(callId, serializeForFrontend(std::move(message)));
callId, MessageBuffer::create(std::move(message), use_binary_protocol_));
} }
void V8InspectorSessionImpl::sendProtocolNotification( void V8InspectorSessionImpl::sendProtocolNotification(
std::unique_ptr<protocol::Serializable> message) { std::unique_ptr<protocol::Serializable> message) {
m_channel->sendNotification( m_channel->sendNotification(serializeForFrontend(std::move(message)));
MessageBuffer::create(std::move(message), use_binary_protocol_));
} }
void V8InspectorSessionImpl::fallThrough( void V8InspectorSessionImpl::fallThrough(
...@@ -357,20 +332,30 @@ void V8InspectorSessionImpl::reportAllContexts(V8RuntimeAgentImpl* agent) { ...@@ -357,20 +332,30 @@ void V8InspectorSessionImpl::reportAllContexts(V8RuntimeAgentImpl* agent) {
void V8InspectorSessionImpl::dispatchProtocolMessage( void V8InspectorSessionImpl::dispatchProtocolMessage(
const StringView& message) { const StringView& message) {
bool binary_protocol = IsCBORMessage(message); using ::v8_inspector_protocol_encoding::span;
if (binary_protocol) { using ::v8_inspector_protocol_encoding::SpanFrom;
span<uint8_t> cbor;
std::vector<uint8_t> converted_cbor;
if (IsCBORMessage(message)) {
use_binary_protocol_ = true; use_binary_protocol_ = true;
m_state->setBoolean("use_binary_protocol", true); m_state->setBoolean("use_binary_protocol", true);
} cbor = span<uint8_t>(message.characters8(), message.length());
} else {
int callId; if (message.is8Bit()) {
std::unique_ptr<protocol::Value> parsed_message; // We're ignoring the return value of these conversion functions
if (binary_protocol) { // intentionally. It means the |parsed_message| below will be nullptr.
parsed_message = protocol::Value::parseBinary( ConvertJSONToCBOR(span<uint8_t>(message.characters8(), message.length()),
message.characters8(), static_cast<unsigned>(message.length())); &converted_cbor);
} else { } else {
parsed_message = protocol::StringUtil::parseJSON(message); ConvertJSONToCBOR(
span<uint16_t>(message.characters16(), message.length()),
&converted_cbor);
} }
cbor = SpanFrom(converted_cbor);
}
int callId;
std::unique_ptr<protocol::Value> parsed_message =
protocol::Value::parseBinary(cbor.data(), cbor.size());
String16 method; String16 method;
if (m_dispatcher.parseCommand(parsed_message.get(), &callId, &method)) { if (m_dispatcher.parseCommand(parsed_message.get(), &callId, &method)) {
// Pass empty string instead of the actual message to save on a conversion. // Pass empty string instead of the actual message to save on a conversion.
......
...@@ -106,6 +106,8 @@ class V8InspectorSessionImpl : public V8InspectorSession, ...@@ -106,6 +106,8 @@ class V8InspectorSessionImpl : public V8InspectorSession,
const protocol::ProtocolMessage& message) override; const protocol::ProtocolMessage& message) override;
void flushProtocolNotifications() override; void flushProtocolNotifications() override;
std::unique_ptr<StringBuffer> serializeForFrontend(
std::unique_ptr<protocol::Serializable> message);
int m_contextGroupId; int m_contextGroupId;
int m_sessionId; int m_sessionId;
V8InspectorImpl* m_inspector; V8InspectorImpl* m_inspector;
......
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