Commit a16b2c7e authored by Johannes Henkel's avatar Johannes Henkel Committed by Commit Bot

[DevTools] Roll inspector_protocol (V8)

New revision: f5a3199a3f37c7e48a9ffdbee04aa5c8f38d2889

Use crdtp::span<uint8_t> instead of const ProtocolMessage&. (V8)
Also includes a PR which moves Serializable into the crdtp library
and adds a simple test. Hadn't rolled this yet.

Upstream review:
https://chromium-review.googlesource.com/c/deps/inspector_protocol/+/1952196

Change-Id: If78bc8f11be8fb248dd66babc0190870312f7ec4
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1952158
Commit-Queue: Johannes Henkel <johannes@chromium.org>
Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#65356}
parent cf7646c5
...@@ -22,7 +22,6 @@ class Value; ...@@ -22,7 +22,6 @@ class Value;
using String = v8_inspector::String16; using String = v8_inspector::String16;
using StringBuilder = v8_inspector::String16Builder; using StringBuilder = v8_inspector::String16Builder;
using ProtocolMessage = std::vector<uint8_t>;
class StringUtil { class StringUtil {
public: public:
......
...@@ -188,9 +188,8 @@ void V8InspectorSessionImpl::sendProtocolNotification( ...@@ -188,9 +188,8 @@ void V8InspectorSessionImpl::sendProtocolNotification(
m_channel->sendNotification(serializeForFrontend(std::move(message))); m_channel->sendNotification(serializeForFrontend(std::move(message)));
} }
void V8InspectorSessionImpl::fallThrough( void V8InspectorSessionImpl::fallThrough(int callId, const String16& method,
int callId, const String16& method, v8_crdtp::span<uint8_t> message) {
const protocol::ProtocolMessage& message) {
// There's no other layer to handle the command. // There's no other layer to handle the command.
UNREACHABLE(); UNREACHABLE();
} }
...@@ -364,7 +363,7 @@ void V8InspectorSessionImpl::dispatchProtocolMessage( ...@@ -364,7 +363,7 @@ void V8InspectorSessionImpl::dispatchProtocolMessage(
// 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.
// We're allowed to do so because fall-through is not implemented. // We're allowed to do so because fall-through is not implemented.
m_dispatcher.dispatch(callId, method, std::move(parsed_message), m_dispatcher.dispatch(callId, method, std::move(parsed_message),
protocol::ProtocolMessage()); v8_crdtp::span<uint8_t>());
} }
} }
......
...@@ -103,7 +103,7 @@ class V8InspectorSessionImpl : public V8InspectorSession, ...@@ -103,7 +103,7 @@ class V8InspectorSessionImpl : public V8InspectorSession,
void sendProtocolNotification( void sendProtocolNotification(
std::unique_ptr<protocol::Serializable> message) override; std::unique_ptr<protocol::Serializable> message) override;
void fallThrough(int callId, const String16& method, void fallThrough(int callId, const String16& method,
const protocol::ProtocolMessage& message) override; v8_crdtp::span<uint8_t> message) override;
void flushProtocolNotifications() override; void flushProtocolNotifications() override;
std::unique_ptr<StringBuffer> serializeForFrontend( std::unique_ptr<StringBuffer> serializeForFrontend(
......
...@@ -19,6 +19,8 @@ v8_source_set("crdtp") { ...@@ -19,6 +19,8 @@ v8_source_set("crdtp") {
"crdtp/json.cc", "crdtp/json.cc",
"crdtp/json.h", "crdtp/json.h",
"crdtp/parser_handler.h", "crdtp/parser_handler.h",
"crdtp/serializable.cc",
"crdtp/serializable.h",
"crdtp/span.h", "crdtp/span.h",
"crdtp/status.cc", "crdtp/status.cc",
"crdtp/status.h", "crdtp/status.h",
...@@ -48,6 +50,7 @@ v8_source_set("crdtp_test") { ...@@ -48,6 +50,7 @@ v8_source_set("crdtp_test") {
"crdtp/cbor_test.cc", "crdtp/cbor_test.cc",
"crdtp/glue_test.cc", "crdtp/glue_test.cc",
"crdtp/json_test.cc", "crdtp/json_test.cc",
"crdtp/serializable_test.cc",
"crdtp/span_test.cc", "crdtp/span_test.cc",
"crdtp/status_test.cc", "crdtp/status_test.cc",
] ]
......
...@@ -2,7 +2,7 @@ Name: inspector protocol ...@@ -2,7 +2,7 @@ Name: inspector protocol
Short Name: inspector_protocol Short Name: inspector_protocol
URL: https://chromium.googlesource.com/deps/inspector_protocol/ URL: https://chromium.googlesource.com/deps/inspector_protocol/
Version: 0 Version: 0
Revision: 7a44a37f66b58358dd8ab85ccde1998fafa95e53 Revision: f5a3199a3f37c7e48a9ffdbee04aa5c8f38d2889
License: BSD License: BSD
License File: LICENSE License File: LICENSE
Security Critical: no Security Critical: no
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "serializable.h"
namespace v8_crdtp {
// =============================================================================
// Serializable - An object to be emitted as a sequence of bytes.
// =============================================================================
std::vector<uint8_t> Serializable::TakeSerialized() && {
std::vector<uint8_t> out;
AppendSerialized(&out);
return out;
}
} // namespace v8_crdtp
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_CRDTP_SERIALIZABLE_H_
#define V8_CRDTP_SERIALIZABLE_H_
#include <cstdint>
#include <vector>
#include "export.h"
namespace v8_crdtp {
// =============================================================================
// Serializable - An object to be emitted as a sequence of bytes.
// =============================================================================
class Serializable {
public:
// The default implementation invokes AppendSerialized with an empty vector
// and returns it; some subclasses may override and move out internal state
// instead to avoid copying.
virtual std::vector<uint8_t> TakeSerialized() &&;
virtual void AppendSerialized(std::vector<uint8_t>* out) const = 0;
virtual ~Serializable() = default;
};
} // namespace v8_crdtp
#endif // V8_CRDTP_SERIALIZABLE_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <cstdlib>
#include <string>
#include "serializable.h"
#include "test_platform.h"
namespace v8_crdtp {
// =============================================================================
// Serializable - An object to be emitted as a sequence of bytes.
// =============================================================================
namespace {
// Tests the default behavior for ::TakeSerialized (to invoke
// ::AppendSerialized).
class SimpleExample : public Serializable {
public:
explicit SimpleExample(const std::vector<uint8_t>& contents)
: contents_(contents) {}
void AppendSerialized(std::vector<uint8_t>* out) const override {
out->insert(out->end(), contents_.begin(), contents_.end());
}
private:
std::vector<uint8_t> contents_;
};
} // namespace
TEST(SerializableTest, YieldsContents) {
std::vector<uint8_t> contents = {1, 2, 3};
SimpleExample foo(contents);
foo.AppendSerialized(&contents); // Yields contents by appending.
EXPECT_THAT(contents, testing::ElementsAre(1, 2, 3, 1, 2, 3));
// Yields contents by returning.
EXPECT_THAT(std::move(foo).TakeSerialized(), testing::ElementsAre(1, 2, 3));
}
} // namespace v8_crdtp
...@@ -22,8 +22,8 @@ class span { ...@@ -22,8 +22,8 @@ class span {
public: public:
using index_type = size_t; using index_type = size_t;
span() : data_(nullptr), size_(0) {} constexpr span() : data_(nullptr), size_(0) {}
span(const T* data, index_type size) : data_(data), size_(size) {} constexpr span(const T* data, index_type size) : data_(data), size_(size) {}
const T* data() const { return data_; } const T* data() const { return data_; }
...@@ -51,7 +51,7 @@ class span { ...@@ -51,7 +51,7 @@ class span {
}; };
template <typename T> template <typename T>
span<T> SpanFrom(const std::vector<T>& v) { constexpr span<T> SpanFrom(const std::vector<T>& v) {
return span<T>(v.data(), v.size()); return span<T>(v.data(), v.size());
} }
......
...@@ -70,11 +70,11 @@ DispatcherBase::WeakPtr::~WeakPtr() ...@@ -70,11 +70,11 @@ DispatcherBase::WeakPtr::~WeakPtr()
m_dispatcher->m_weakPtrs.erase(this); m_dispatcher->m_weakPtrs.erase(this);
} }
DispatcherBase::Callback::Callback(std::unique_ptr<DispatcherBase::WeakPtr> backendImpl, int callId, const String& method, const ProtocolMessage& message) DispatcherBase::Callback::Callback(std::unique_ptr<DispatcherBase::WeakPtr> backendImpl, int callId, const String& method, {{config.crdtp.namespace}}::span<uint8_t> message)
: m_backendImpl(std::move(backendImpl)) : m_backendImpl(std::move(backendImpl))
, m_callId(callId) , m_callId(callId)
, m_method(method) , m_method(method)
, m_message(message) { } , m_message(message.begin(), message.end()) { }
DispatcherBase::Callback::~Callback() = default; DispatcherBase::Callback::~Callback() = default;
...@@ -95,7 +95,7 @@ void DispatcherBase::Callback::fallThroughIfActive() ...@@ -95,7 +95,7 @@ void DispatcherBase::Callback::fallThroughIfActive()
{ {
if (!m_backendImpl || !m_backendImpl->get()) if (!m_backendImpl || !m_backendImpl->get())
return; return;
m_backendImpl->get()->channel()->fallThrough(m_callId, m_method, m_message); m_backendImpl->get()->channel()->fallThrough(m_callId, m_method, {{config.crdtp.namespace}}::SpanFrom(m_message));
m_backendImpl = nullptr; m_backendImpl = nullptr;
} }
...@@ -279,7 +279,7 @@ bool UberDispatcher::canDispatch(const String& in_method) ...@@ -279,7 +279,7 @@ bool UberDispatcher::canDispatch(const String& in_method)
return !!findDispatcher(method); return !!findDispatcher(method);
} }
void UberDispatcher::dispatch(int callId, const String& in_method, std::unique_ptr<Value> parsedMessage, const ProtocolMessage& rawMessage) void UberDispatcher::dispatch(int callId, const String& in_method, std::unique_ptr<Value> parsedMessage, {{config.crdtp.namespace}}::span<uint8_t> rawMessage)
{ {
String method = in_method; String method = in_method;
auto redirectIt = m_redirects.find(method); auto redirectIt = m_redirects.find(method);
......
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
//#include "ErrorSupport.h" //#include "ErrorSupport.h"
//#include "Values.h" //#include "Values.h"
#include "{{config.crdtp.dir}}/span.h"
{% for namespace in config.protocol.namespace %} {% for namespace in config.protocol.namespace %}
namespace {{namespace}} { namespace {{namespace}} {
{% endfor %} {% endfor %}
...@@ -71,7 +73,7 @@ public: ...@@ -71,7 +73,7 @@ public:
class {{config.lib.export_macro}} Callback { class {{config.lib.export_macro}} Callback {
public: public:
Callback(std::unique_ptr<WeakPtr> backendImpl, int callId, const String& method, const ProtocolMessage& message); Callback(std::unique_ptr<WeakPtr> backendImpl, int callId, const String& method, {{config.crdtp.namespace}}::span<uint8_t> message);
virtual ~Callback(); virtual ~Callback();
void dispose(); void dispose();
...@@ -83,14 +85,14 @@ public: ...@@ -83,14 +85,14 @@ public:
std::unique_ptr<WeakPtr> m_backendImpl; std::unique_ptr<WeakPtr> m_backendImpl;
int m_callId; int m_callId;
String m_method; String m_method;
ProtocolMessage m_message; std::vector<uint8_t> m_message;
}; };
explicit DispatcherBase(FrontendChannel*); explicit DispatcherBase(FrontendChannel*);
virtual ~DispatcherBase(); virtual ~DispatcherBase();
virtual bool canDispatch(const String& method) = 0; virtual bool canDispatch(const String& method) = 0;
virtual void dispatch(int callId, const String& method, const ProtocolMessage& rawMessage, std::unique_ptr<protocol::DictionaryValue> messageObject) = 0; virtual void dispatch(int callId, const String& method, {{config.crdtp.namespace}}::span<uint8_t> rawMessage, std::unique_ptr<protocol::DictionaryValue> messageObject) = 0;
FrontendChannel* channel() { return m_frontendChannel; } FrontendChannel* channel() { return m_frontendChannel; }
void sendResponse(int callId, const DispatchResponse&, std::unique_ptr<protocol::DictionaryValue> result); void sendResponse(int callId, const DispatchResponse&, std::unique_ptr<protocol::DictionaryValue> result);
...@@ -114,7 +116,7 @@ public: ...@@ -114,7 +116,7 @@ public:
void setupRedirects(const std::unordered_map<String, String>&); void setupRedirects(const std::unordered_map<String, String>&);
bool parseCommand(Value* message, int* callId, String* method); bool parseCommand(Value* message, int* callId, String* method);
bool canDispatch(const String& method); bool canDispatch(const String& method);
void dispatch(int callId, const String& method, std::unique_ptr<Value> message, const ProtocolMessage& rawMessage); void dispatch(int callId, const String& method, std::unique_ptr<Value> message, {{config.crdtp.namespace}}::span<uint8_t> rawMessage);
FrontendChannel* channel() { return m_frontendChannel; } FrontendChannel* channel() { return m_frontendChannel; }
virtual ~UberDispatcher(); virtual ~UberDispatcher();
......
...@@ -7,27 +7,22 @@ ...@@ -7,27 +7,22 @@
#ifndef {{"_".join(config.protocol.namespace)}}_FrontendChannel_h #ifndef {{"_".join(config.protocol.namespace)}}_FrontendChannel_h
#define {{"_".join(config.protocol.namespace)}}_FrontendChannel_h #define {{"_".join(config.protocol.namespace)}}_FrontendChannel_h
#include "{{config.crdtp.dir}}/serializable.h"
#include "{{config.crdtp.dir}}/span.h"
{% for namespace in config.protocol.namespace %} {% for namespace in config.protocol.namespace %}
namespace {{namespace}} { namespace {{namespace}} {
{% endfor %} {% endfor %}
class {{config.lib.export_macro}} Serializable { using {{config.crdtp.namespace}}::Serializable;
public:
virtual std::vector<uint8_t> TakeSerialized() && {
std::vector<uint8_t> out;
AppendSerialized(&out);
return out;
}
virtual void AppendSerialized(std::vector<uint8_t>* out) const = 0;
virtual ~Serializable() = default;
};
class {{config.lib.export_macro}} FrontendChannel { class {{config.lib.export_macro}} FrontendChannel {
public: public:
virtual ~FrontendChannel() { } virtual ~FrontendChannel() { }
virtual void sendProtocolResponse(int callId, std::unique_ptr<Serializable> message) = 0; virtual void sendProtocolResponse(int callId, std::unique_ptr<Serializable> message) = 0;
virtual void sendProtocolNotification(std::unique_ptr<Serializable> message) = 0; virtual void sendProtocolNotification(std::unique_ptr<Serializable> message) = 0;
virtual void fallThrough(int callId, const String& method, const ProtocolMessage& message) = 0;
virtual void fallThrough(int callId, const String& method, {{config.crdtp.namespace}}::span<uint8_t> message) = 0;
virtual void flushProtocolNotifications() = 0; virtual void flushProtocolNotifications() = 0;
}; };
......
...@@ -140,17 +140,6 @@ std::unique_ptr<Value> StringUtil::parseMessage( ...@@ -140,17 +140,6 @@ std::unique_ptr<Value> StringUtil::parseMessage(
return toProtocolValue(value.get(), 1000); return toProtocolValue(value.get(), 1000);
} }
// static
ProtocolMessage StringUtil::jsonToMessage(String message) {
return message;
}
// static
ProtocolMessage StringUtil::binaryToMessage(std::vector<uint8_t> message) {
// TODO(pfeldman): figure out what to do with this copy.
return std::string(reinterpret_cast<const char*>(message.data()), message.size());
}
StringBuilder::StringBuilder() {} StringBuilder::StringBuilder() {}
StringBuilder::~StringBuilder() {} StringBuilder::~StringBuilder() {}
......
...@@ -30,7 +30,6 @@ namespace {{namespace}} { ...@@ -30,7 +30,6 @@ namespace {{namespace}} {
class Value; class Value;
using String = std::string; using String = std::string;
using ProtocolMessage = std::string;
class {{config.lib.export_macro}} StringBuilder { class {{config.lib.export_macro}} StringBuilder {
public: public:
...@@ -93,8 +92,6 @@ class {{config.lib.export_macro}} StringUtil { ...@@ -93,8 +92,6 @@ class {{config.lib.export_macro}} StringUtil {
} }
static std::unique_ptr<Value> parseMessage(const std::string& message, bool binary); static std::unique_ptr<Value> parseMessage(const std::string& message, bool binary);
static ProtocolMessage jsonToMessage(String message);
static ProtocolMessage binaryToMessage(std::vector<uint8_t> message);
static String fromUTF8(const uint8_t* data, size_t length) { static String fromUTF8(const uint8_t* data, size_t length) {
return std::string(reinterpret_cast<const char*>(data), length); return std::string(reinterpret_cast<const char*>(data), length);
......
...@@ -28,6 +28,9 @@ FILES_TO_SYNC = [ ...@@ -28,6 +28,9 @@ FILES_TO_SYNC = [
'crdtp/json_platform.h', 'crdtp/json_platform.h',
'crdtp/json_test.cc', 'crdtp/json_test.cc',
'crdtp/parser_handler.h', 'crdtp/parser_handler.h',
'crdtp/serializable.h',
'crdtp/serializable.cc',
'crdtp/serializable_test.cc',
'crdtp/span.h', 'crdtp/span.h',
'crdtp/span_test.cc', 'crdtp/span_test.cc',
'crdtp/status.cc', 'crdtp/status.cc',
......
...@@ -226,11 +226,11 @@ public: ...@@ -226,11 +226,11 @@ public:
} }
~DispatcherImpl() override { } ~DispatcherImpl() override { }
bool canDispatch(const String& method) override; bool canDispatch(const String& method) override;
void dispatch(int callId, const String& method, const ProtocolMessage& message, std::unique_ptr<protocol::DictionaryValue> messageObject) override; void dispatch(int callId, const String& method, {{config.crdtp.namespace}}::span<uint8_t> message, std::unique_ptr<protocol::DictionaryValue> messageObject) override;
std::unordered_map<String, String>& redirects() { return m_redirects; } std::unordered_map<String, String>& redirects() { return m_redirects; }
protected: protected:
using CallHandler = void (DispatcherImpl::*)(int callId, const String& method, const ProtocolMessage& message, std::unique_ptr<DictionaryValue> messageObject, ErrorSupport* errors); using CallHandler = void (DispatcherImpl::*)(int callId, const String& method, {{config.crdtp.namespace}}::span<uint8_t> message, std::unique_ptr<DictionaryValue> messageObject, ErrorSupport* errors);
using DispatchMap = std::unordered_map<String, CallHandler>; using DispatchMap = std::unordered_map<String, CallHandler>;
DispatchMap m_dispatchMap; DispatchMap m_dispatchMap;
std::unordered_map<String, String> m_redirects; std::unordered_map<String, String> m_redirects;
...@@ -238,7 +238,7 @@ protected: ...@@ -238,7 +238,7 @@ protected:
{% for command in domain.commands %} {% for command in domain.commands %}
{% if "redirect" in command %}{% continue %}{% endif %} {% if "redirect" in command %}{% continue %}{% endif %}
{% if not protocol.generate_command(domain.domain, command.name) %}{% continue %}{% endif %} {% if not protocol.generate_command(domain.domain, command.name) %}{% continue %}{% endif %}
void {{command.name}}(int callId, const String& method, const ProtocolMessage& message, std::unique_ptr<DictionaryValue> requestMessageObject, ErrorSupport*); void {{command.name}}(int callId, const String& method, {{config.crdtp.namespace}}::span<uint8_t> message, std::unique_ptr<DictionaryValue> requestMessageObject, ErrorSupport*);
{% endfor %} {% endfor %}
Backend* m_backend; Backend* m_backend;
...@@ -248,7 +248,7 @@ bool DispatcherImpl::canDispatch(const String& method) { ...@@ -248,7 +248,7 @@ bool DispatcherImpl::canDispatch(const String& method) {
return m_dispatchMap.find(method) != m_dispatchMap.end(); return m_dispatchMap.find(method) != m_dispatchMap.end();
} }
void DispatcherImpl::dispatch(int callId, const String& method, const ProtocolMessage& message, std::unique_ptr<protocol::DictionaryValue> messageObject) void DispatcherImpl::dispatch(int callId, const String& method, {{config.crdtp.namespace}}::span<uint8_t> message, std::unique_ptr<protocol::DictionaryValue> messageObject)
{ {
std::unordered_map<String, CallHandler>::iterator it = m_dispatchMap.find(method); std::unordered_map<String, CallHandler>::iterator it = m_dispatchMap.find(method);
DCHECK(it != m_dispatchMap.end()); DCHECK(it != m_dispatchMap.end());
...@@ -264,7 +264,7 @@ void DispatcherImpl::dispatch(int callId, const String& method, const ProtocolMe ...@@ -264,7 +264,7 @@ void DispatcherImpl::dispatch(int callId, const String& method, const ProtocolMe
class {{command_name_title}}CallbackImpl : public Backend::{{command_name_title}}Callback, public DispatcherBase::Callback { class {{command_name_title}}CallbackImpl : public Backend::{{command_name_title}}Callback, public DispatcherBase::Callback {
public: public:
{{command_name_title}}CallbackImpl(std::unique_ptr<DispatcherBase::WeakPtr> backendImpl, int callId, const String& method, const ProtocolMessage& message) {{command_name_title}}CallbackImpl(std::unique_ptr<DispatcherBase::WeakPtr> backendImpl, int callId, const String& method, {{config.crdtp.namespace}}::span<uint8_t> message)
: DispatcherBase::Callback(std::move(backendImpl), callId, method, message) { } : DispatcherBase::Callback(std::move(backendImpl), callId, method, message) { }
void sendSuccess( void sendSuccess(
...@@ -302,7 +302,7 @@ public: ...@@ -302,7 +302,7 @@ public:
}; };
{% endif %} {% endif %}
void DispatcherImpl::{{command.name}}(int callId, const String& method, const ProtocolMessage& message, std::unique_ptr<DictionaryValue> requestMessageObject, ErrorSupport* errors) void DispatcherImpl::{{command.name}}(int callId, const String& method, {{config.crdtp.namespace}}::span<uint8_t> message, std::unique_ptr<DictionaryValue> requestMessageObject, ErrorSupport* errors)
{ {
{% if "parameters" in command %} {% if "parameters" in command %}
// Prepare input parameters. // Prepare input parameters.
......
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