Commit 99cc4c98 authored by Johannes Henkel's avatar Johannes Henkel Committed by Commit Bot

[DevTools] Serialize to JSON via CBOR.

I'm planning to remove protocol::Serializable::serialize(bool),
removing the ability there to make a ProtocolMessage
that carries JSON. This is the last usage of such messages
in both Chromium and V8; so I've put a bit of conversion
boilerplate code which is borrowed from
content/browser/devtools/devtools_protocol_encoding.cc.
Sadly it's longer, but it is a price to pay for simplifying
the API. Thanks much for considering.

Change-Id: I24a01e1ed6134f58fa65f03d29506afa33a7ff01
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1641551
Commit-Queue: Johannes Henkel <johannes@chromium.org>
Reviewed-by: default avatarLeonard Grey <lgrey@chromium.org>
Cr-Commit-Position: refs/heads/master@{#666095}
parent c25d14f9
...@@ -88,11 +88,11 @@ component("ui_devtools") { ...@@ -88,11 +88,11 @@ component("ui_devtools") {
} }
deps = [ deps = [
":devtools_protocol_encoding",
":protocol_generated_sources", ":protocol_generated_sources",
"//base", "//base",
"//net", "//net",
"//ui/gfx", "//ui/gfx",
"//third_party/inspector_protocol:encoding",
] ]
public_deps = [ public_deps = [
...@@ -108,11 +108,23 @@ static_library("test_support") { ...@@ -108,11 +108,23 @@ static_library("test_support") {
"ui_devtools_unittest_utils.h", "ui_devtools_unittest_utils.h",
] ]
public_deps = [ public_deps = [
":devtools_protocol_encoding",
":ui_devtools", ":ui_devtools",
"//testing/gmock", "//testing/gmock",
] ]
} }
static_library("devtools_protocol_encoding") {
sources = [
"devtools_protocol_encoding.cc",
"devtools_protocol_encoding.h",
]
deps = [
"//base",
"//third_party/inspector_protocol:encoding",
]
}
source_set("unit_tests") { source_set("unit_tests") {
testonly = true testonly = true
......
...@@ -4,6 +4,7 @@ include_rules = [ ...@@ -4,6 +4,7 @@ include_rules = [
"+services/network/public/cpp", "+services/network/public/cpp",
"+services/network/public/mojom", "+services/network/public/mojom",
"+third_party/blink/renderer/platform/inspector_protocol", "+third_party/blink/renderer/platform/inspector_protocol",
"+third_party/inspector_protocol",
"+ui/gfx", "+ui/gfx",
] ]
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "components/ui_devtools/devtools_client.h" #include "components/ui_devtools/devtools_client.h"
#include "components/ui_devtools/devtools_protocol_encoding.h"
#include "components/ui_devtools/devtools_server.h" #include "components/ui_devtools/devtools_server.h"
namespace ui_devtools { namespace ui_devtools {
...@@ -56,17 +57,32 @@ void UiDevToolsClient::DisableAllAgents() { ...@@ -56,17 +57,32 @@ void UiDevToolsClient::DisableAllAgents() {
agent->Disable(); agent->Disable();
} }
namespace {
std::string SerializeToJSON(std::unique_ptr<protocol::Serializable> message) {
std::vector<uint8_t> cbor = message->serializeToBinary();
std::string json;
::inspector_protocol_encoding::Status status =
ConvertCBORToJSON(::inspector_protocol_encoding::SpanFrom(cbor), &json);
LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
return json;
}
} // namespace
void UiDevToolsClient::sendProtocolResponse( void UiDevToolsClient::sendProtocolResponse(
int callId, int callId,
std::unique_ptr<protocol::Serializable> message) { std::unique_ptr<protocol::Serializable> message) {
if (connected()) if (connected()) {
server_->SendOverWebSocket(connection_id_, message->serialize(false)); server_->SendOverWebSocket(
connection_id_, base::StringPiece(SerializeToJSON(std::move(message))));
}
} }
void UiDevToolsClient::sendProtocolNotification( void UiDevToolsClient::sendProtocolNotification(
std::unique_ptr<protocol::Serializable> message) { std::unique_ptr<protocol::Serializable> message) {
if (connected()) if (connected()) {
server_->SendOverWebSocket(connection_id_, message->serialize(false)); server_->SendOverWebSocket(
connection_id_, base::StringPiece(SerializeToJSON(std::move(message))));
}
} }
void UiDevToolsClient::flushProtocolNotifications() { void UiDevToolsClient::flushProtocolNotifications() {
......
// 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 "components/ui_devtools/devtools_protocol_encoding.h"
#include <memory>
#include "base/strings/string_number_conversions.h"
namespace ui_devtools {
namespace {
using ::inspector_protocol_encoding::span;
using IPEStatus = ::inspector_protocol_encoding::Status;
// Platform allows us to inject the string<->double conversion
// routines from base:: into the inspector_protocol JSON parser / serializer.
class Platform : public ::inspector_protocol_encoding::json::Platform {
public:
bool StrToD(const char* str, double* result) const override {
return base::StringToDouble(str, result);
}
// Prints |value| in a format suitable for JSON.
std::unique_ptr<char[]> DToStr(double value) const override {
std::string str = base::NumberToString(value);
std::unique_ptr<char[]> result(new char[str.size() + 1]);
memcpy(result.get(), str.c_str(), str.size() + 1);
return result;
}
};
} // namespace
IPEStatus ConvertCBORToJSON(span<uint8_t> cbor, std::string* json) {
Platform platform;
return ::inspector_protocol_encoding::json::ConvertCBORToJSON(platform, cbor,
json);
}
} // namespace ui_devtools
// 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 COMPONENTS_UI_DEVTOOLS_DEVTOOLS_PROTOCOL_ENCODING_H_
#define COMPONENTS_UI_DEVTOOLS_DEVTOOLS_PROTOCOL_ENCODING_H_
#include "third_party/inspector_protocol/encoding/encoding.h"
// Convenience adaptation of the conversion function
// ::inspector_protocol_encoding::json::ConvertCBORToJSON,
// using an implementation of
// ::inspector_protocol_encoding::json::Platform that
// delegates to base/strings/string_number_conversions.h.
namespace ui_devtools {
::inspector_protocol_encoding::Status ConvertCBORToJSON(
::inspector_protocol_encoding::span<uint8_t> cbor,
std::string* json);
} // namespace ui_devtools
#endif // COMPONENTS_UI_DEVTOOLS_DEVTOOLS_PROTOCOL_ENCODING_H_
...@@ -168,7 +168,7 @@ void UiDevToolsServer::AttachClient(std::unique_ptr<UiDevToolsClient> client) { ...@@ -168,7 +168,7 @@ void UiDevToolsServer::AttachClient(std::unique_ptr<UiDevToolsClient> client) {
} }
void UiDevToolsServer::SendOverWebSocket(int connection_id, void UiDevToolsServer::SendOverWebSocket(int connection_id,
const protocol::String& message) { base::StringPiece message) {
DCHECK_CALLED_ON_VALID_SEQUENCE(devtools_server_sequence_); DCHECK_CALLED_ON_VALID_SEQUENCE(devtools_server_sequence_);
server_->SendOverWebSocket(connection_id, message, tag_); server_->SendOverWebSocket(connection_id, message, tag_);
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/strings/string_piece_forward.h"
#include "base/threading/thread.h" #include "base/threading/thread.h"
#include "components/ui_devtools/DOM.h" #include "components/ui_devtools/DOM.h"
#include "components/ui_devtools/Forward.h" #include "components/ui_devtools/Forward.h"
...@@ -62,7 +63,7 @@ class UI_DEVTOOLS_EXPORT UiDevToolsServer ...@@ -62,7 +63,7 @@ class UI_DEVTOOLS_EXPORT UiDevToolsServer
int default_port); int default_port);
void AttachClient(std::unique_ptr<UiDevToolsClient> client); void AttachClient(std::unique_ptr<UiDevToolsClient> client);
void SendOverWebSocket(int connection_id, const protocol::String& message); void SendOverWebSocket(int connection_id, base::StringPiece message);
int port() const { return port_; } int port() const { return port_; }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "components/ui_devtools/ui_devtools_unittest_utils.h" #include "components/ui_devtools/ui_devtools_unittest_utils.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "components/ui_devtools/devtools_protocol_encoding.h"
namespace ui_devtools { namespace ui_devtools {
...@@ -29,10 +30,22 @@ int FakeFrontendChannel::CountProtocolNotificationMessage( ...@@ -29,10 +30,22 @@ int FakeFrontendChannel::CountProtocolNotificationMessage(
protocol_notification_messages_.end(), message); protocol_notification_messages_.end(), message);
} }
namespace {
std::string SerializeToJSON(std::unique_ptr<protocol::Serializable> message) {
std::vector<uint8_t> cbor = message->serializeToBinary();
std::string json;
::inspector_protocol_encoding::Status status =
ConvertCBORToJSON(::inspector_protocol_encoding::SpanFrom(cbor), &json);
DCHECK(status.ok()) << status.ToASCIIString();
return json;
}
} // namespace
void FakeFrontendChannel::sendProtocolNotification( void FakeFrontendChannel::sendProtocolNotification(
std::unique_ptr<protocol::Serializable> message) { std::unique_ptr<protocol::Serializable> message) {
EXPECT_TRUE(allow_notifications_); EXPECT_TRUE(allow_notifications_);
protocol_notification_messages_.push_back(message->serialize(false)); protocol_notification_messages_.push_back(
SerializeToJSON(std::move(message)));
} }
} // namespace ui_devtools } // namespace ui_devtools
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