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(
return state;
}
namespace {
class MessageBuffer : public StringBuffer {
public:
static std::unique_ptr<MessageBuffer> create(
std::unique_ptr<protocol::Serializable> message, bool binary) {
return std::unique_ptr<MessageBuffer>(
new MessageBuffer(std::move(message), binary));
}
const StringView& string() override {
if (!m_serialized) {
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
std::unique_ptr<StringBuffer> V8InspectorSessionImpl::serializeForFrontend(
std::unique_ptr<protocol::Serializable> message) {
std::vector<uint8_t> cbor = message->serializeToBinary();
if (use_binary_protocol_)
return std::unique_ptr<StringBuffer>(
new BinaryStringBuffer(std::move(cbor)));
std::vector<uint8_t> json;
IPEStatus status = ConvertCBORToJSON(SpanFrom(cbor), &json);
DCHECK(status.ok());
USE(status);
String16 string16(reinterpret_cast<const char*>(json.data()), json.size());
return StringBufferImpl::adopt(string16);
}
void V8InspectorSessionImpl::sendProtocolResponse(
int callId, std::unique_ptr<protocol::Serializable> message) {
m_channel->sendResponse(
callId, MessageBuffer::create(std::move(message), use_binary_protocol_));
m_channel->sendResponse(callId, serializeForFrontend(std::move(message)));
}
void V8InspectorSessionImpl::sendProtocolNotification(
std::unique_ptr<protocol::Serializable> message) {
m_channel->sendNotification(
MessageBuffer::create(std::move(message), use_binary_protocol_));
m_channel->sendNotification(serializeForFrontend(std::move(message)));
}
void V8InspectorSessionImpl::fallThrough(
......@@ -357,20 +332,30 @@ void V8InspectorSessionImpl::reportAllContexts(V8RuntimeAgentImpl* agent) {
void V8InspectorSessionImpl::dispatchProtocolMessage(
const StringView& message) {
bool binary_protocol = IsCBORMessage(message);
if (binary_protocol) {
using ::v8_inspector_protocol_encoding::span;
using ::v8_inspector_protocol_encoding::SpanFrom;
span<uint8_t> cbor;
std::vector<uint8_t> converted_cbor;
if (IsCBORMessage(message)) {
use_binary_protocol_ = true;
m_state->setBoolean("use_binary_protocol", true);
}
int callId;
std::unique_ptr<protocol::Value> parsed_message;
if (binary_protocol) {
parsed_message = protocol::Value::parseBinary(
message.characters8(), static_cast<unsigned>(message.length()));
cbor = span<uint8_t>(message.characters8(), message.length());
} else {
parsed_message = protocol::StringUtil::parseJSON(message);
if (message.is8Bit()) {
// We're ignoring the return value of these conversion functions
// intentionally. It means the |parsed_message| below will be nullptr.
ConvertJSONToCBOR(span<uint8_t>(message.characters8(), message.length()),
&converted_cbor);
} else {
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;
if (m_dispatcher.parseCommand(parsed_message.get(), &callId, &method)) {
// Pass empty string instead of the actual message to save on a conversion.
......
......@@ -106,6 +106,8 @@ class V8InspectorSessionImpl : public V8InspectorSession,
const protocol::ProtocolMessage& message) override;
void flushProtocolNotifications() override;
std::unique_ptr<StringBuffer> serializeForFrontend(
std::unique_ptr<protocol::Serializable> message);
int m_contextGroupId;
int m_sessionId;
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