[DevTools] Detect if the state of the inspector session was encoded as CBOR.

In the longer run we only want the CBOR code path, for now we need to handle
JSON as well. So we convert if possible.

Change-Id: I726b737f4cd2602d4fb676ce7cf996fcd1ba33e9
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1598254
Reviewed-by: Alexei Filippov <alph@chromium.org>
Commit-Queue: Johannes Henkel <johannes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61424}
This commit is contained in:
Johannes Henkel 2019-05-09 15:21:40 -07:00 committed by Commit Bot
parent 02c1900095
commit 402570546f
3 changed files with 40 additions and 12 deletions

View File

@ -43,4 +43,9 @@ IPEStatus ConvertJSONToCBOR(span<uint8_t> json, std::vector<uint8_t>* cbor) {
return ConvertJSONToCBOR(platform, json, cbor);
}
IPEStatus ConvertJSONToCBOR(span<uint16_t> json, std::vector<uint8_t>* cbor) {
Platform platform;
return ConvertJSONToCBOR(platform, json, cbor);
}
} // namespace v8_inspector

View File

@ -17,6 +17,10 @@ namespace v8_inspector {
::v8_inspector_protocol_encoding::span<uint8_t> json,
std::vector<uint8_t>* cbor);
::v8_inspector_protocol_encoding::Status ConvertJSONToCBOR(
::v8_inspector_protocol_encoding::span<uint16_t> json,
std::vector<uint8_t>* cbor);
} // namespace v8_inspector
#endif // V8_INSPECTOR_V8_INSPECTOR_PROTOCOL_ENCODING_H_

View File

@ -24,8 +24,37 @@
namespace v8_inspector {
namespace {
using ::v8_inspector_protocol_encoding::span;
using ::v8_inspector_protocol_encoding::SpanFrom;
using IPEStatus = ::v8_inspector_protocol_encoding::Status;
bool IsCBORMessage(const StringView& msg) {
return msg.is8Bit() && msg.length() >= 2 && msg.characters8()[0] == 0xd8 &&
msg.characters8()[1] == 0x5a;
}
IPEStatus ConvertToCBOR(const StringView& state, std::vector<uint8_t>* cbor) {
return state.is8Bit()
? ConvertJSONToCBOR(
span<uint8_t>(state.characters8(), state.length()), cbor)
: ConvertJSONToCBOR(
span<uint16_t>(state.characters16(), state.length()), cbor);
}
std::unique_ptr<protocol::DictionaryValue> ParseState(const StringView& state) {
std::vector<uint8_t> converted;
span<uint8_t> cbor;
if (IsCBORMessage(state))
cbor = span<uint8_t>(state.characters8(), state.length());
else if (ConvertToCBOR(state, &converted).ok())
cbor = SpanFrom(converted);
if (!cbor.empty()) {
std::unique_ptr<protocol::Value> value =
protocol::Value::parseBinary(cbor.data(), cbor.size());
if (value) return protocol::DictionaryValue::cast(std::move(value));
}
return protocol::DictionaryValue::create();
}
} // namespace
// static
@ -67,22 +96,13 @@ V8InspectorSessionImpl::V8InspectorSessionImpl(V8InspectorImpl* inspector,
m_channel(channel),
m_customObjectFormatterEnabled(false),
m_dispatcher(this),
m_state(nullptr),
m_state(ParseState(savedState)),
m_runtimeAgent(nullptr),
m_debuggerAgent(nullptr),
m_heapProfilerAgent(nullptr),
m_profilerAgent(nullptr),
m_consoleAgent(nullptr),
m_schemaAgent(nullptr) {
if (savedState.length()) {
std::unique_ptr<protocol::Value> state =
protocol::StringUtil::parseJSON(toString16(savedState));
if (state) m_state = protocol::DictionaryValue::cast(std::move(state));
if (!m_state) m_state = protocol::DictionaryValue::create();
} else {
m_state = protocol::DictionaryValue::create();
}
m_state->getBoolean("use_binary_protocol", &use_binary_protocol_);
m_runtimeAgent.reset(new V8RuntimeAgentImpl(
@ -337,8 +357,7 @@ void V8InspectorSessionImpl::reportAllContexts(V8RuntimeAgentImpl* agent) {
void V8InspectorSessionImpl::dispatchProtocolMessage(
const StringView& message) {
bool binary_protocol =
message.is8Bit() && message.length() && message.characters8()[0] == 0xD8;
bool binary_protocol = IsCBORMessage(message);
if (binary_protocol) {
use_binary_protocol_ = true;
m_state->setBoolean("use_binary_protocol", true);