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

[DevTools] Roll inspector_protocol (Chromium) (strtod)

New revision: 7a44a37f66b58358dd8ab85ccde1998fafa95e53

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

Previously, we used a wrapper routines, e.g. those in
content/browser/devtools/devtools_protocol_encoding.cc to use
this library, by using dependency injection to provide the
string<->number conversion routines. With this change, the
third_party library depends on the routines in //base via a
custom, Chrome-specific .cc file, json_platform_chromium.cc.
This does away with the indirection for the library usage.

Change-Id: I963b0f962473ce911bfbdfb848863d253bc7dbf0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1922594
Commit-Queue: Johannes Henkel <johannes@chromium.org>
Reviewed-by: default avatarAndrey Kosyakov <caseq@chromium.org>
Reviewed-by: default avatarMax Moroz <mmoroz@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarLeonard Grey <lgrey@chromium.org>
Reviewed-by: default avatarRobert Sesek <rsesek@chromium.org>
Cr-Commit-Position: refs/heads/master@{#718396}
parent b2d02e7a
......@@ -20,35 +20,6 @@
#include "chrome/browser/devtools/protocol/window_manager_handler.h"
#endif
namespace {
// TODO(johannes): This is very similar to the code in
// content/browser/devtools/devtools_protocol_encoding. Once we have
// the error / status propagation story settled, move the common parts
// into a content public API.
// Platform allows us to inject the string<->double conversion
// routines from base:: into the inspector_protocol JSON parser / serializer.
class Platform : public crdtp::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;
}
};
crdtp::Status ConvertCBORToJSON(crdtp::span<uint8_t> cbor, std::string* json) {
Platform platform;
return ConvertCBORToJSON(platform, cbor, json);
}
} // namespace
ChromeDevToolsSession::ChromeDevToolsSession(
content::DevToolsAgentHost* agent_host,
content::DevToolsAgentHostClient* client)
......@@ -112,7 +83,8 @@ static void SendProtocolResponseOrNotification(
return;
}
std::string json;
crdtp::Status status = ConvertCBORToJSON(crdtp::SpanFrom(cbor), &json);
crdtp::Status status =
crdtp::json::ConvertCBORToJSON(crdtp::SpanFrom(cbor), &json);
LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
client->DispatchProtocolMessage(agent_host, json);
}
......
......@@ -107,12 +107,12 @@ component("ui_devtools") {
}
deps = [
":devtools_protocol_encoding",
":protocol_generated_sources",
"//base",
"//mojo/public/cpp/bindings",
"//mojo/public/cpp/system",
"//net",
"//third_party/inspector_protocol:crdtp",
"//ui/gfx",
]
......@@ -134,19 +134,8 @@ static_library("test_support") {
"ui_devtools_unittest_utils.h",
]
public_deps = [
":devtools_protocol_encoding",
":ui_devtools",
"//testing/gmock",
]
}
static_library("devtools_protocol_encoding") {
sources = [
"devtools_protocol_encoding.cc",
"devtools_protocol_encoding.h",
]
deps = [
"//base",
"//third_party/inspector_protocol:crdtp",
]
}
......
......@@ -4,8 +4,8 @@
#include "components/ui_devtools/devtools_client.h"
#include "components/ui_devtools/devtools_protocol_encoding.h"
#include "components/ui_devtools/devtools_server.h"
#include "third_party/inspector_protocol/crdtp/json.h"
namespace ui_devtools {
......@@ -61,7 +61,8 @@ namespace {
std::string SerializeToJSON(std::unique_ptr<protocol::Serializable> message) {
std::vector<uint8_t> cbor = std::move(*message).TakeSerialized();
std::string json;
crdtp::Status status = ConvertCBORToJSON(crdtp::SpanFrom(cbor), &json);
crdtp::Status status =
crdtp::json::ConvertCBORToJSON(crdtp::SpanFrom(cbor), &json);
LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
return json;
}
......
// 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 <string>
#include <vector>
#include "base/strings/string_number_conversions.h"
namespace ui_devtools {
namespace {
// Platform allows us to inject the string<->double conversion
// routines from base:: into the inspector_protocol JSON parser / serializer.
class Platform : public crdtp::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
crdtp::Status ConvertCBORToJSON(crdtp::span<uint8_t> cbor, std::string* json) {
Platform platform;
return crdtp::json::ConvertCBORToJSON(platform, cbor, json);
}
crdtp::Status ConvertJSONToCBOR(crdtp::span<uint8_t> json, std::string* cbor) {
Platform platform;
return ConvertJSONToCBOR(platform, json, cbor);
}
crdtp::Status ConvertJSONToCBOR(crdtp::span<uint8_t> json,
std::vector<uint8_t>* cbor) {
Platform platform;
return ConvertJSONToCBOR(platform, json, cbor);
}
} // 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 <string>
#include <vector>
#include "third_party/inspector_protocol/crdtp/json.h"
// Convenience adaptation of the conversion function
// crdtp::json::ConvertCBORToJSON, crdtp::json::ConvertJSONToCBOR.
// using an implementation of crdtp::json::Platform that
// delegates to base/strings/string_number_conversions.h.
namespace ui_devtools {
crdtp::Status ConvertCBORToJSON(crdtp::span<uint8_t> cbor, std::string* json);
crdtp::Status ConvertJSONToCBOR(crdtp::span<uint8_t> json, std::string* cbor);
crdtp::Status ConvertJSONToCBOR(crdtp::span<uint8_t> json,
std::vector<uint8_t>* cbor);
} // namespace ui_devtools
#endif // COMPONENTS_UI_DEVTOOLS_DEVTOOLS_PROTOCOL_ENCODING_H_
......@@ -16,7 +16,6 @@
#include "base/trace_event/traced_value.h"
#include "components/ui_devtools/connector_delegate.h"
#include "components/ui_devtools/devtools_base_agent.h"
#include "components/ui_devtools/devtools_protocol_encoding.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/system/data_pipe_drainer.h"
......@@ -24,6 +23,7 @@
#include "services/tracing/public/cpp/tracing_features.h"
#include "services/tracing/public/mojom/constants.mojom.h"
#include "services/tracing/public/mojom/perfetto_service.mojom.h"
#include "third_party/inspector_protocol/crdtp/json.h"
namespace ui_devtools {
......@@ -311,7 +311,8 @@ void TracingAgent::OnTraceDataCollected(
trace_data_buffer_state_.offset);
message += "] } }";
std::vector<uint8_t> cbor;
crdtp::Status status = ConvertJSONToCBOR(crdtp::SpanFrom(message), &cbor);
crdtp::Status status =
crdtp::json::ConvertJSONToCBOR(crdtp::SpanFrom(message), &cbor);
LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
frontend()->sendRawCBORNotification(std::move(cbor));
......
......@@ -5,7 +5,7 @@
#include "components/ui_devtools/ui_devtools_unittest_utils.h"
#include "base/strings/string_util.h"
#include "components/ui_devtools/devtools_protocol_encoding.h"
#include "third_party/inspector_protocol/crdtp/json.h"
namespace ui_devtools {
......@@ -34,7 +34,8 @@ namespace {
std::string SerializeToJSON(std::unique_ptr<protocol::Serializable> message) {
std::vector<uint8_t> cbor = std::move(*message).TakeSerialized();
std::string json;
crdtp::Status status = ConvertCBORToJSON(crdtp::SpanFrom(cbor), &json);
crdtp::Status status =
crdtp::json::ConvertCBORToJSON(crdtp::SpanFrom(cbor), &json);
DCHECK(status.ok()) << status.ToASCIIString();
return json;
}
......
......@@ -700,8 +700,6 @@ jumbo_source_set("browser") {
"devtools/devtools_manager.h",
"devtools/devtools_pipe_handler.cc",
"devtools/devtools_pipe_handler.h",
"devtools/devtools_protocol_encoding.cc",
"devtools/devtools_protocol_encoding.h",
"devtools/devtools_renderer_channel.cc",
"devtools/devtools_renderer_channel.h",
"devtools/devtools_session.cc",
......
......@@ -122,22 +122,3 @@ proto_library("devtools_background_services_proto") {
"devtools_background_services.proto",
]
}
source_set("inspector_protocol_crdtp_test") {
testonly = true
sources = [
"../../../third_party/inspector_protocol/crdtp/cbor_test.cc",
"../../../third_party/inspector_protocol/crdtp/glue_test.cc",
"../../../third_party/inspector_protocol/crdtp/json_test.cc",
"../../../third_party/inspector_protocol/crdtp/span_test.cc",
"../../../third_party/inspector_protocol/crdtp/status_test.cc",
"../../../third_party/inspector_protocol/crdtp/test_platform.h",
"../../../third_party/inspector_protocol/crdtp/test_platform.cc",
]
deps = [
"//base",
"//testing/gmock",
"//testing/gtest",
"//third_party/inspector_protocol:crdtp",
]
}
......@@ -44,7 +44,7 @@ const int kWriteFD = 4;
// entire remaining message. Thereby, the length of the byte string
// also tells us the message size on the wire.
// The details of the encoding are implemented in
// third_party/inspector_protocol/crdtp/encoding.h.
// third_party/inspector_protocol/crdtp/cbor.h.
namespace content {
class PipeReaderBase {
......
// 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 "content/browser/devtools/devtools_protocol_encoding.h"
#include "base/strings/string_number_conversions.h"
namespace content {
namespace {
// Platform allows us to inject the string<->double conversion
// routines from base:: into the inspector_protocol JSON parser / serializer.
class Platform : public crdtp::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
crdtp::Status ConvertCBORToJSON(crdtp::span<uint8_t> cbor, std::string* json) {
Platform platform;
return crdtp::json::ConvertCBORToJSON(platform, cbor, json);
}
crdtp::Status ConvertJSONToCBOR(crdtp::span<uint8_t> json, std::string* cbor) {
Platform platform;
return crdtp::json::ConvertJSONToCBOR(platform, json, cbor);
}
crdtp::Status ConvertJSONToCBOR(crdtp::span<uint8_t> json,
std::vector<uint8_t>* cbor) {
Platform platform;
return crdtp::json::ConvertJSONToCBOR(platform, json, cbor);
}
} // namespace content
// 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 CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_ENCODING_H_
#define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_ENCODING_H_
#include <memory>
#include "third_party/inspector_protocol/crdtp/json.h"
// Convenience adaptations of the conversion functions
// crdtp::json::ConvertCBORToJSON crdtp::json::ConvertJDONToCBOR.
// These adaptations use an implementation of crdtp::json::Platform that
// uses base/strings/string_number_conversions.h.
namespace content {
crdtp::Status ConvertCBORToJSON(crdtp::span<uint8_t> cbor, std::string* json);
crdtp::Status ConvertJSONToCBOR(crdtp::span<uint8_t> json, std::string* cbor);
crdtp::Status ConvertJSONToCBOR(crdtp::span<uint8_t> json,
std::vector<uint8_t>* cbor);
} // namespace content
#endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_ENCODING_H_
......@@ -2,10 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/devtools/devtools_protocol_encoding.h"
#include <string>
#include "third_party/inspector_protocol/crdtp/cbor.h"
#include "third_party/inspector_protocol/crdtp/json.h"
// A corpus for this fuzzer is located in
// devtools_protocol_encoding_cbor_fuzzer_corpus.
......@@ -16,7 +13,7 @@
namespace content {
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
std::string json;
content::ConvertCBORToJSON(crdtp::span<uint8_t>(data, size), &json);
crdtp::json::ConvertCBORToJSON(crdtp::span<uint8_t>(data, size), &json);
return 0;
}
} // namespace content
......@@ -2,16 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/devtools/devtools_protocol_encoding.h"
#include <string>
#include "third_party/inspector_protocol/crdtp/json.h"
namespace content {
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
std::string cbor;
content::ConvertJSONToCBOR(crdtp::span<uint8_t>(data, size), &cbor);
crdtp::json::ConvertJSONToCBOR(crdtp::span<uint8_t>(data, size), &cbor);
return 0;
}
} // namespace content
......@@ -10,7 +10,6 @@
#include "base/bind.h"
#include "content/browser/devtools/devtools_manager.h"
#include "content/browser/devtools/devtools_protocol_encoding.h"
#include "content/browser/devtools/protocol/devtools_domain_handler.h"
#include "content/browser/devtools/protocol/protocol.h"
#include "content/browser/devtools/render_frame_devtools_agent_host.h"
......@@ -18,6 +17,7 @@
#include "content/public/browser/devtools_external_agent_proxy_delegate.h"
#include "content/public/browser/devtools_manager_delegate.h"
#include "third_party/inspector_protocol/crdtp/cbor.h"
#include "third_party/inspector_protocol/crdtp/json.h"
namespace content {
namespace {
......@@ -188,7 +188,8 @@ bool DevToolsSession::DispatchProtocolMessage(const std::string& message) {
if (client_->UsesBinaryProtocol()) {
DCHECK(crdtp::cbor::IsCBORMessage(crdtp::SpanFrom(message)));
std::string json;
crdtp::Status status = ConvertCBORToJSON(crdtp::SpanFrom(message), &json);
crdtp::Status status =
crdtp::json::ConvertCBORToJSON(crdtp::SpanFrom(message), &json);
LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
proxy_delegate_->SendMessageToBackend(this, json);
return true;
......@@ -203,8 +204,8 @@ bool DevToolsSession::DispatchProtocolMessage(const std::string& message) {
// CBOR (it comes from the client).
DCHECK(crdtp::cbor::IsCBORMessage(crdtp::SpanFrom(message)));
} else {
crdtp::Status status =
ConvertJSONToCBOR(crdtp::SpanFrom(message), &converted_cbor_message);
crdtp::Status status = crdtp::json::ConvertJSONToCBOR(
crdtp::SpanFrom(message), &converted_cbor_message);
LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
message_to_send = &converted_cbor_message;
}
......@@ -327,7 +328,8 @@ static void SendProtocolResponseOrNotification(
return;
}
std::string json;
crdtp::Status status = ConvertCBORToJSON(crdtp::SpanFrom(cbor), &json);
crdtp::Status status =
crdtp::json::ConvertCBORToJSON(crdtp::SpanFrom(cbor), &json);
LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
client->DispatchProtocolMessage(agent_host, json);
}
......@@ -397,9 +399,10 @@ void DevToolsSession::DispatchOnClientHost(const std::string& message) {
return;
}
std::string converted;
crdtp::Status status = client_->UsesBinaryProtocol()
? ConvertJSONToCBOR(bytes, &converted)
: ConvertCBORToJSON(bytes, &converted);
crdtp::Status status =
client_->UsesBinaryProtocol()
? crdtp::json::ConvertJSONToCBOR(bytes, &converted)
: crdtp::json::ConvertCBORToJSON(bytes, &converted);
LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
client_->DispatchProtocolMessage(agent_host_, converted);
// |this| may be deleted at this point.
......@@ -463,7 +466,7 @@ void DevToolsSession::SendMessageFromChildSession(const std::string& session_id,
return;
}
std::string json;
status = ConvertCBORToJSON(crdtp::SpanFrom(patched), &json);
status = crdtp::json::ConvertCBORToJSON(crdtp::SpanFrom(patched), &json);
LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
client_->DispatchProtocolMessage(agent_host_, json);
// |this| may be deleted at this point.
......
......@@ -28,7 +28,6 @@
#include "components/tracing/common/trace_startup_config.h"
#include "content/browser/devtools/devtools_agent_host_impl.h"
#include "content/browser/devtools/devtools_io_context.h"
#include "content/browser/devtools/devtools_protocol_encoding.h"
#include "content/browser/devtools/devtools_stream_file.h"
#include "content/browser/devtools/devtools_traceable_screenshot.h"
#include "content/browser/devtools/devtools_video_consumer.h"
......@@ -591,7 +590,8 @@ void TracingHandler::OnTraceDataCollected(
trace_data_buffer_state_.offset);
message += "] } }";
std::vector<uint8_t> cbor;
crdtp::Status status = ConvertJSONToCBOR(crdtp::SpanFrom(message), &cbor);
crdtp::Status status =
crdtp::json::ConvertJSONToCBOR(crdtp::SpanFrom(message), &cbor);
LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
frontend_->sendRawCBORNotification(std::move(cbor));
}
......
......@@ -1948,7 +1948,6 @@ test("content_unittests") {
"//content/browser/background_fetch:background_fetch_proto",
"//content/browser/cache_storage:cache_storage_proto",
"//content/browser/devtools:devtools_background_services_proto",
"//content/browser/devtools:inspector_protocol_crdtp_test",
"//content/browser/dom_storage:local_storage_proto",
"//content/browser/notifications:notification_proto",
"//content/browser/service_worker:service_worker_proto",
......@@ -2015,6 +2014,7 @@ test("content_unittests") {
"//testing/gtest",
"//third_party/blink/public:blink",
"//third_party/icu",
"//third_party/inspector_protocol:crdtp_test",
"//third_party/leveldatabase",
"//third_party/libyuv",
"//third_party/metrics_proto",
......
......@@ -104,8 +104,6 @@ fuzzer_test("signed_exchange_envelope_fuzzer") {
fuzzer_test("devtools_protocol_encoding_cbor_fuzzer") {
sources = [
"../../browser/devtools/devtools_protocol_encoding.cc",
"../../browser/devtools/devtools_protocol_encoding.h",
"../../browser/devtools/devtools_protocol_encoding_cbor_fuzzer.cc",
]
deps = [
......@@ -118,8 +116,6 @@ fuzzer_test("devtools_protocol_encoding_cbor_fuzzer") {
fuzzer_test("devtools_protocol_encoding_json_fuzzer") {
sources = [
"../../browser/devtools/devtools_protocol_encoding.cc",
"../../browser/devtools/devtools_protocol_encoding.h",
"../../browser/devtools/devtools_protocol_encoding_json_fuzzer.cc",
]
deps = [
......
......@@ -13,35 +13,6 @@
#include "headless/lib/browser/protocol/target_handler.h"
#include "third_party/inspector_protocol/crdtp/json.h"
namespace {
// TODO(johannes): This is very similar to the code in
// content/browser/devtools/devtools_protocol_encoding. Once we have
// the error / status propagation story settled, move the common parts
// into a content public API.
// Platform allows us to inject the string<->double conversion
// routines from base:: into the inspector_protocol JSON parser / serializer.
class Platform : public crdtp::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;
}
};
crdtp::Status ConvertCBORToJSON(crdtp::span<uint8_t> cbor, std::string* json) {
Platform platform;
return crdtp::json::ConvertCBORToJSON(platform, cbor, json);
}
} // namespace
namespace headless {
namespace protocol {
HeadlessDevToolsSession::HeadlessDevToolsSession(
......@@ -109,7 +80,8 @@ static void SendProtocolResponseOrNotification(
return;
}
std::string json;
crdtp::Status status = ConvertCBORToJSON(crdtp::SpanFrom(cbor), &json);
crdtp::Status status =
crdtp::json::ConvertCBORToJSON(crdtp::SpanFrom(cbor), &json);
LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
client->DispatchProtocolMessage(agent_host, json);
}
......
......@@ -50,34 +50,6 @@ Vector<uint8_t> UnwrapMessage(const mojom::blink::DevToolsMessagePtr& message) {
return unwrap_message;
}
// Platform allows us to inject the string<->double conversion
// routines from Blink into the inspector_protocol JSON parser / serializer.
class JsonPlatform : public crdtp::json::Platform {
public:
bool StrToD(const char* str, double* result) const override {
bool ok;
*result = String(str).ToDouble(&ok);
return ok;
}
// Prints |value| in a format suitable for JSON.
std::unique_ptr<char[]> DToStr(double value) const override {
String str = String::NumberToStringECMAScript(value);
DCHECK(str.Is8Bit());
std::unique_ptr<char[]> result(new char[str.length() + 1]);
memcpy(result.get(), str.Characters8(), str.length());
result.get()[str.length()] = '\0';
return result;
}
};
crdtp::Status ConvertCBORToJSON(crdtp::span<uint8_t> cbor,
std::vector<uint8_t>* json) {
DCHECK(crdtp::cbor::IsCBORMessage(cbor));
JsonPlatform platform;
return ConvertCBORToJSON(platform, cbor, json);
}
std::vector<uint8_t> Get8BitStringFrom(v8_inspector::StringBuffer* msg) {
const v8_inspector::StringView& s = msg->string();
DCHECK(s.is8Bit());
......@@ -359,7 +331,7 @@ blink::mojom::blink::DevToolsMessagePtr DevToolsSession::FinalizeMessage(
if (!client_expects_binary_responses_) {
std::vector<uint8_t> json;
crdtp::Status status =
ConvertCBORToJSON(crdtp::SpanFrom(message_to_send), &json);
crdtp::json::ConvertCBORToJSON(crdtp::SpanFrom(message_to_send), &json);
CHECK(status.ok()) << status.ToASCIIString();
message_to_send = std::move(json);
}
......
......@@ -11,7 +11,7 @@
#include "third_party/blink/public/platform/web_vector.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/inspector_protocol/crdtp/json.h"
#include "third_party/inspector_protocol/crdtp/span.h"
namespace blink {
class InspectorAgentState;
......
......@@ -6,18 +6,16 @@ import("//build/config/jumbo.gni")
jumbo_component("crdtp") {
sources = [
"crdtp/cbor.cc",
"crdtp/cbor.h",
"crdtp/export.h",
"crdtp/glue.h",
"crdtp/status.h",
"crdtp/json.cc",
"crdtp/json.h",
"crdtp/json_platform.h",
"crdtp/parser_handler.h",
"crdtp/span.h",
"crdtp/status.h",
"crdtp/cbor.cc",
"crdtp/json.cc",
"crdtp/status.cc",
"crdtp/status.h",
]
defines = [ "CRDTP_IMPLEMENTATION" ]
......@@ -26,8 +24,49 @@ jumbo_component("crdtp") {
configs -= [ "//build/config/compiler:default_optimization" ]
configs += [ "//build/config/compiler:optimize_max" ]
}
deps = [
":crdtp_platform",
]
}
# A small adapter library which only :crdtp may depend on.
source_set("crdtp_platform") {
visibility = [ ":crdtp" ]
sources = [
"crdtp/chromium/json_platform_chromium.cc",
"crdtp/json_platform.h",
]
deps = [
"//base",
]
}
# These tests are linked into //content/test:content_unittests.
source_set("crdtp_test") {
testonly = true
sources = [
"crdtp/cbor_test.cc",
"crdtp/glue_test.cc",
"crdtp/json_test.cc",
"crdtp/span_test.cc",
"crdtp/status_test.cc",
]
deps = [
":crdtp_test_platform",
]
}
# A small adapter library which only :crdtp_test may depend on.
source_set("crdtp_test_platform") {
testonly = true
sources = [
"crdtp/test_platform.cc",
"crdtp/test_platform.h",
]
public_deps = [
"//base",
"//testing/gmock",
"//testing/gtest",
"//third_party/inspector_protocol:crdtp",
]
}
# encoding/encoding_test.cc:
# There's no target for this file here, instead it is included
# in //content/test:content_unittest
# via //content/browser/devtools:inspector_protocol_encoding_test.
# The tests in the encoding/ and bindings/ directories are setup to use
# The tests in the crdtp/ directory are setup to use
# base from mini_chromium, as well as gmock / gtest.
# Within the Chromium tree, we can use these dependencies
# directly but this requires an exception here.
......
......@@ -2,7 +2,7 @@ Name: inspector protocol
Short Name: inspector_protocol
URL: https://chromium.googlesource.com/deps/inspector_protocol/
Version: 0
Revision: 4c2a3acaea9f2e7958081dd361f81e20e9eff5e7
Revision: 7a44a37f66b58358dd8ab85ccde1998fafa95e53
License: BSD
License File: LICENSE
Security Critical: yes
......@@ -12,7 +12,7 @@ WebKit/core/inspector uses these scripts to generate handlers from protocol
description.
Local modifications:
- This only includes the lib/, templates/, encoding/ directories, scripts, build
- This only includes the lib/, templates/, crdtp/ directories, scripts, build
and the LICENSE files.
- DEPS file to allow the base/gmock/gtest includes from encoding/ tests.
- DEPS files to allow dependencies on base and gtest, for test_platform.{h,cc}
- Chromium specific files in crdtp/chromium.
......@@ -14,21 +14,5 @@ https://cs.chromium.org/chromium/src/v8/third_party/inspector_protocol/
See also [Contributing to Chrome Devtools Protocol](https://docs.google.com/document/d/1c-COD2kaK__5iMM5SEx-PzNA7HFmgttcYfOHHX0HaOM/edit).
We're working on enabling standalone builds for parts of this package for
testing and development.
If you're familiar with
[Chromium's development process](https://www.chromium.org/developers/contributing-code)
and have the depot_tools installed, you may use these commands
to fetch the package (and dependencies) and build and run the tests:
fetch inspector_protocol
cd src
gn gen out/Release
ninja -C out/Release encoding_test bindings_test
out/Release/encoding_test
out/Release/bindings_test
You'll probably also need to install g++, since Clang uses this to find the
standard C++ headers. E.g.,
sudo apt-get install g++-8
To build and run the tests of the crdtp library, see
[CRDTP - Chrome DevTools Protocol](crdtp/README.md).
// 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.
// This file is Chromium specific. It's not rolled from the upstream project.
#include "base/strings/string_number_conversions.h"
namespace crdtp {
namespace json {
namespace platform {
// Parses |str| into |result|. Returns false iff there are
// leftover characters or parsing errors.
bool StrToD(const char* str, double* result) {
return base::StringToDouble(str, result);
}
// Prints |value| in a format suitable for JSON.
std::string DToStr(double value) {
return base::NumberToString(value);
}
} // namespace platform
} // namespace json
} // namespace crdtp
......@@ -12,10 +12,10 @@
#include <stack>
#include "cbor.h"
#include "json_platform.h"
namespace crdtp {
namespace json {
// =============================================================================
// json::NewJSONEncoder - for encoding streaming parser events as JSON
// =============================================================================
......@@ -104,8 +104,7 @@ void Base64Encode(const span<uint8_t>& in, C* out) {
template <typename C>
class JSONEncoder : public ParserHandler {
public:
JSONEncoder(const Platform* platform, C* out, Status* status)
: platform_(platform), out_(out), status_(status) {
JSONEncoder(C* out, Status* status) : out_(out), status_(status) {
*status_ = Status();
state_.emplace(Container::NONE);
}
......@@ -283,14 +282,14 @@ class JSONEncoder : public ParserHandler {
Emit("null");
return;
}
std::unique_ptr<char[]> str_value = platform_->DToStr(value);
std::string str_value = json::platform::DToStr(value);
// DToStr may fail to emit a 0 before the decimal dot. E.g. this is
// the case in base::NumberToString in Chromium (which is based on
// dmg_fp). So, much like
// https://cs.chromium.org/chromium/src/base/json/json_writer.cc
// we probe for this and emit the leading 0 anyway if necessary.
const char* chars = str_value.get();
const char* chars = str_value.c_str();
if (chars[0] == '.') {
Emit('0');
} else if (chars[0] == '-' && chars[1] == '.') {
......@@ -336,25 +335,22 @@ class JSONEncoder : public ParserHandler {
out_->insert(out_->end(), str.begin(), str.end());
}
const Platform* platform_;
C* out_;
Status* status_;
std::stack<State> state_;
};
} // namespace
std::unique_ptr<ParserHandler> NewJSONEncoder(const Platform* platform,
std::vector<uint8_t>* out,
std::unique_ptr<ParserHandler> NewJSONEncoder(std::vector<uint8_t>* out,
Status* status) {
return std::unique_ptr<ParserHandler>(
new JSONEncoder<std::vector<uint8_t>>(platform, out, status));
new JSONEncoder<std::vector<uint8_t>>(out, status));
}
std::unique_ptr<ParserHandler> NewJSONEncoder(const Platform* platform,
std::string* out,
std::unique_ptr<ParserHandler> NewJSONEncoder(std::string* out,
Status* status) {
return std::unique_ptr<ParserHandler>(
new JSONEncoder<std::string>(platform, out, status));
new JSONEncoder<std::string>(out, status));
}
// =============================================================================
......@@ -387,8 +383,7 @@ const char* const kFalseString = "false";
template <typename Char>
class JsonParser {
public:
JsonParser(const Platform* platform, ParserHandler* handler)
: platform_(platform), handler_(handler) {}
explicit JsonParser(ParserHandler* handler) : handler_(handler) {}
void Parse(const Char* start, size_t length) {
start_pos_ = start;
......@@ -412,12 +407,12 @@ class JsonParser {
return false;
buffer.push_back(static_cast<char>(chars[ii]));
}
return platform_->StrToD(buffer.c_str(), result);
return platform::StrToD(buffer.c_str(), result);
}
bool CharsToDouble(const uint8_t* chars, size_t length, double* result) {
std::string buffer(reinterpret_cast<const char*>(chars), length);
return platform_->StrToD(buffer.c_str(), result);
return platform::StrToD(buffer.c_str(), result);
}
static bool ParseConstToken(const Char* start,
......@@ -966,22 +961,17 @@ class JsonParser {
const Char* start_pos_ = nullptr;
bool error_ = false;
const Platform* platform_;
ParserHandler* handler_;
};
} // namespace
void ParseJSON(const Platform& platform,
span<uint8_t> chars,
ParserHandler* handler) {
JsonParser<uint8_t> parser(&platform, handler);
void ParseJSON(span<uint8_t> chars, ParserHandler* handler) {
JsonParser<uint8_t> parser(handler);
parser.Parse(chars.data(), chars.size());
}
void ParseJSON(const Platform& platform,
span<uint16_t> chars,
ParserHandler* handler) {
JsonParser<uint16_t> parser(&platform, handler);
void ParseJSON(span<uint16_t> chars, ParserHandler* handler) {
JsonParser<uint16_t> parser(handler);
parser.Parse(chars.data(), chars.size());
}
......@@ -989,58 +979,43 @@ void ParseJSON(const Platform& platform,
// json::ConvertCBORToJSON, json::ConvertJSONToCBOR - for transcoding
// =============================================================================
template <typename C>
Status ConvertCBORToJSONTmpl(const Platform& platform,
span<uint8_t> cbor,
C* json) {
Status ConvertCBORToJSONTmpl(span<uint8_t> cbor, C* json) {
Status status;
std::unique_ptr<ParserHandler> json_writer =
NewJSONEncoder(&platform, json, &status);
std::unique_ptr<ParserHandler> json_writer = NewJSONEncoder(json, &status);
cbor::ParseCBOR(cbor, json_writer.get());
return status;
}
Status ConvertCBORToJSON(const Platform& platform,
span<uint8_t> cbor,
std::vector<uint8_t>* json) {
return ConvertCBORToJSONTmpl(platform, cbor, json);
Status ConvertCBORToJSON(span<uint8_t> cbor, std::vector<uint8_t>* json) {
return ConvertCBORToJSONTmpl(cbor, json);
}
Status ConvertCBORToJSON(const Platform& platform,
span<uint8_t> cbor,
std::string* json) {
return ConvertCBORToJSONTmpl(platform, cbor, json);
Status ConvertCBORToJSON(span<uint8_t> cbor, std::string* json) {
return ConvertCBORToJSONTmpl(cbor, json);
}
template <typename T, typename C>
Status ConvertJSONToCBORTmpl(const Platform& platform, span<T> json, C* cbor) {
Status ConvertJSONToCBORTmpl(span<T> json, C* cbor) {
Status status;
std::unique_ptr<ParserHandler> encoder = cbor::NewCBOREncoder(cbor, &status);
ParseJSON(platform, json, encoder.get());
ParseJSON(json, encoder.get());
return status;
}
Status ConvertJSONToCBOR(const Platform& platform,
span<uint8_t> json,
std::string* cbor) {
return ConvertJSONToCBORTmpl(platform, json, cbor);
Status ConvertJSONToCBOR(span<uint8_t> json, std::string* cbor) {
return ConvertJSONToCBORTmpl(json, cbor);
}
Status ConvertJSONToCBOR(const Platform& platform,
span<uint16_t> json,
std::string* cbor) {
return ConvertJSONToCBORTmpl(platform, json, cbor);
Status ConvertJSONToCBOR(span<uint16_t> json, std::string* cbor) {
return ConvertJSONToCBORTmpl(json, cbor);
}
Status ConvertJSONToCBOR(const Platform& platform,
span<uint8_t> json,
std::vector<uint8_t>* cbor) {
return ConvertJSONToCBORTmpl(platform, json, cbor);
Status ConvertJSONToCBOR(span<uint8_t> json, std::vector<uint8_t>* cbor) {
return ConvertJSONToCBORTmpl(json, cbor);
}
Status ConvertJSONToCBOR(const Platform& platform,
span<uint16_t> json,
std::vector<uint8_t>* cbor) {
return ConvertJSONToCBORTmpl(platform, json, cbor);
Status ConvertJSONToCBOR(span<uint16_t> json, std::vector<uint8_t>* cbor) {
return ConvertJSONToCBORTmpl(json, cbor);
}
} // namespace json
} // namespace crdtp
......@@ -6,9 +6,7 @@
#define CRDTP_JSON_H_
#include <memory>
#include "export.h"
#include "json_platform.h"
#include "parser_handler.h"
namespace crdtp {
......@@ -24,44 +22,38 @@ namespace json {
// code must call the Handle* methods in an order in which they'd occur
// in valid JSON; otherwise we may crash (the code uses assert).
CRDTP_EXPORT std::unique_ptr<ParserHandler> NewJSONEncoder(
const Platform* platform,
std::vector<uint8_t>* out,
Status* status);
CRDTP_EXPORT std::unique_ptr<ParserHandler>
NewJSONEncoder(const Platform* platform, std::string* out, Status* status);
CRDTP_EXPORT std::unique_ptr<ParserHandler> NewJSONEncoder(std::string* out,
Status* status);
// =============================================================================
// json::ParseJSON - for receiving streaming parser events for JSON
// =============================================================================
CRDTP_EXPORT void ParseJSON(const Platform& platform,
span<uint8_t> chars,
ParserHandler* handler);
CRDTP_EXPORT void ParseJSON(const Platform& platform,
span<uint16_t> chars,
ParserHandler* handler);
CRDTP_EXPORT void ParseJSON(span<uint8_t> chars, ParserHandler* handler);
CRDTP_EXPORT void ParseJSON(span<uint16_t> chars, ParserHandler* handler);
// =============================================================================
// json::ConvertCBORToJSON, json::ConvertJSONToCBOR - for transcoding
// =============================================================================
CRDTP_EXPORT Status ConvertCBORToJSON(const Platform& platform,
span<uint8_t> cbor,
std::string* json);
CRDTP_EXPORT Status ConvertCBORToJSON(const Platform& platform,
span<uint8_t> cbor,
CRDTP_EXPORT Status ConvertCBORToJSON(span<uint8_t> cbor, std::string* json);
CRDTP_EXPORT Status ConvertCBORToJSON(span<uint8_t> cbor,
std::vector<uint8_t>* json);
CRDTP_EXPORT Status ConvertJSONToCBOR(const Platform& platform,
span<uint8_t> json,
CRDTP_EXPORT Status ConvertJSONToCBOR(span<uint8_t> json,
std::vector<uint8_t>* cbor);
CRDTP_EXPORT Status ConvertJSONToCBOR(const Platform& platform,
span<uint16_t> json,
CRDTP_EXPORT Status ConvertJSONToCBOR(span<uint16_t> json,
std::vector<uint8_t>* cbor);
CRDTP_EXPORT Status ConvertJSONToCBOR(const Platform& platform,
span<uint8_t> json,
std::string* cbor);
CRDTP_EXPORT Status ConvertJSONToCBOR(const Platform& platform,
span<uint16_t> json,
std::string* cbor);
CRDTP_EXPORT Status ConvertJSONToCBOR(span<uint8_t> json, std::string* cbor);
CRDTP_EXPORT Status ConvertJSONToCBOR(span<uint16_t> json, std::string* cbor);
} // namespace json
} // namespace crdtp
......
......@@ -5,23 +5,21 @@
#ifndef CRDTP_JSON_PLATFORM_H_
#define CRDTP_JSON_PLATFORM_H_
#include <memory>
#include "export.h"
#include <string>
namespace crdtp {
namespace json {
// Client code must provide an instance. Implementation should delegate
// to whatever is appropriate.
class CRDTP_EXPORT Platform {
public:
virtual ~Platform() = default;
// Parses |str| into |result|. Returns false iff there are
// leftover characters or parsing errors.
virtual bool StrToD(const char* str, double* result) const = 0;
// These routines are implemented in json_platform.cc, or in a
// platform-dependent (code-base dependent) custom replacement.
// E.g., json_platform_chromium.cc, json_platform_v8.cc.
namespace platform {
// Parses |str| into |result|. Returns false iff there are
// leftover characters or parsing errors.
bool StrToD(const char* str, double* result);
// Prints |value| in a format suitable for JSON.
virtual std::unique_ptr<char[]> DToStr(double value) const = 0;
};
// Prints |value| in a format suitable for JSON.
std::string DToStr(double value);
} // namespace platform
} // namespace json
} // namespace crdtp
......
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