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;
using String = v8_inspector::String16;
using StringBuilder = v8_inspector::String16Builder;
using ProtocolMessage = std::vector<uint8_t>;
class StringUtil {
public:
......
......@@ -188,9 +188,8 @@ void V8InspectorSessionImpl::sendProtocolNotification(
m_channel->sendNotification(serializeForFrontend(std::move(message)));
}
void V8InspectorSessionImpl::fallThrough(
int callId, const String16& method,
const protocol::ProtocolMessage& message) {
void V8InspectorSessionImpl::fallThrough(int callId, const String16& method,
v8_crdtp::span<uint8_t> message) {
// There's no other layer to handle the command.
UNREACHABLE();
}
......@@ -364,7 +363,7 @@ void V8InspectorSessionImpl::dispatchProtocolMessage(
// 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.
m_dispatcher.dispatch(callId, method, std::move(parsed_message),
protocol::ProtocolMessage());
v8_crdtp::span<uint8_t>());
}
}
......
......@@ -103,7 +103,7 @@ class V8InspectorSessionImpl : public V8InspectorSession,
void sendProtocolNotification(
std::unique_ptr<protocol::Serializable> message) override;
void fallThrough(int callId, const String16& method,
const protocol::ProtocolMessage& message) override;
v8_crdtp::span<uint8_t> message) override;
void flushProtocolNotifications() override;
std::unique_ptr<StringBuffer> serializeForFrontend(
......
......@@ -19,6 +19,8 @@ v8_source_set("crdtp") {
"crdtp/json.cc",
"crdtp/json.h",
"crdtp/parser_handler.h",
"crdtp/serializable.cc",
"crdtp/serializable.h",
"crdtp/span.h",
"crdtp/status.cc",
"crdtp/status.h",
......@@ -48,6 +50,7 @@ v8_source_set("crdtp_test") {
"crdtp/cbor_test.cc",
"crdtp/glue_test.cc",
"crdtp/json_test.cc",
"crdtp/serializable_test.cc",
"crdtp/span_test.cc",
"crdtp/status_test.cc",
]
......
......@@ -2,7 +2,7 @@ Name: inspector protocol
Short Name: inspector_protocol
URL: https://chromium.googlesource.com/deps/inspector_protocol/
Version: 0
Revision: 7a44a37f66b58358dd8ab85ccde1998fafa95e53
Revision: f5a3199a3f37c7e48a9ffdbee04aa5c8f38d2889
License: BSD
License File: LICENSE
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 {
public:
using index_type = size_t;
span() : data_(nullptr), size_(0) {}
span(const T* data, index_type size) : data_(data), size_(size) {}
constexpr span() : data_(nullptr), size_(0) {}
constexpr span(const T* data, index_type size) : data_(data), size_(size) {}
const T* data() const { return data_; }
......@@ -51,7 +51,7 @@ class span {
};
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());
}
......
......@@ -70,11 +70,11 @@ DispatcherBase::WeakPtr::~WeakPtr()
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_callId(callId)
, m_method(method)
, m_message(message) { }
, m_message(message.begin(), message.end()) { }
DispatcherBase::Callback::~Callback() = default;
......@@ -95,7 +95,7 @@ void DispatcherBase::Callback::fallThroughIfActive()
{
if (!m_backendImpl || !m_backendImpl->get())
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;
}
......@@ -279,7 +279,7 @@ bool UberDispatcher::canDispatch(const String& in_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;
auto redirectIt = m_redirects.find(method);
......
......@@ -11,6 +11,8 @@
//#include "ErrorSupport.h"
//#include "Values.h"
#include "{{config.crdtp.dir}}/span.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
......@@ -71,7 +73,7 @@ public:
class {{config.lib.export_macro}} Callback {
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();
void dispose();
......@@ -83,14 +85,14 @@ public:
std::unique_ptr<WeakPtr> m_backendImpl;
int m_callId;
String m_method;
ProtocolMessage m_message;
std::vector<uint8_t> m_message;
};
explicit DispatcherBase(FrontendChannel*);
virtual ~DispatcherBase();
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; }
void sendResponse(int callId, const DispatchResponse&, std::unique_ptr<protocol::DictionaryValue> result);
......@@ -114,7 +116,7 @@ public:
void setupRedirects(const std::unordered_map<String, String>&);
bool parseCommand(Value* message, int* callId, 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; }
virtual ~UberDispatcher();
......
......@@ -7,27 +7,22 @@
#ifndef {{"_".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 %}
namespace {{namespace}} {
{% endfor %}
class {{config.lib.export_macro}} 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;
};
using {{config.crdtp.namespace}}::Serializable;
class {{config.lib.export_macro}} FrontendChannel {
public:
virtual ~FrontendChannel() { }
virtual void sendProtocolResponse(int callId, 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;
};
......
......@@ -140,17 +140,6 @@ std::unique_ptr<Value> StringUtil::parseMessage(
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() {}
......
......@@ -30,7 +30,6 @@ namespace {{namespace}} {
class Value;
using String = std::string;
using ProtocolMessage = std::string;
class {{config.lib.export_macro}} StringBuilder {
public:
......@@ -93,8 +92,6 @@ class {{config.lib.export_macro}} StringUtil {
}
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) {
return std::string(reinterpret_cast<const char*>(data), length);
......
......@@ -28,6 +28,9 @@ FILES_TO_SYNC = [
'crdtp/json_platform.h',
'crdtp/json_test.cc',
'crdtp/parser_handler.h',
'crdtp/serializable.h',
'crdtp/serializable.cc',
'crdtp/serializable_test.cc',
'crdtp/span.h',
'crdtp/span_test.cc',
'crdtp/status.cc',
......
......@@ -226,11 +226,11 @@ public:
}
~DispatcherImpl() 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; }
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>;
DispatchMap m_dispatchMap;
std::unordered_map<String, String> m_redirects;
......@@ -238,7 +238,7 @@ protected:
{% for command in domain.commands %}
{% if "redirect" in command %}{% 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 %}
Backend* m_backend;
......@@ -248,7 +248,7 @@ bool DispatcherImpl::canDispatch(const String& method) {
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);
DCHECK(it != m_dispatchMap.end());
......@@ -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 {
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) { }
void sendSuccess(
......@@ -302,7 +302,7 @@ public:
};
{% 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 %}
// 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