From 1832865cbca0e8da0e00934cf5f57674d4631832 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Fri, 8 Feb 2019 04:39:46 +0000 Subject: [PATCH] Revert "DevTools: roll third_party/inspector_protocol" This reverts commit 84b0216acfa753d91e4d41ebec8aa0e57cc216f2. Reason for revert: Original change's description: > DevTools: roll third_party/inspector_protocol > > Bug: 929862 > > Change-Id: I3c12584095a1d2a45a3b6181bece071a2cd34dae > Reviewed-on: https://chromium-review.googlesource.com/c/1460186 > Commit-Queue: Pavel Feldman > Reviewed-by: Dmitry Gozman > Cr-Commit-Position: refs/heads/master@{#630185} TBR=dgozman@chromium.org,pfeldman@chromium.org Change-Id: I250784ec2a83b539b640a14188e17980c7a9e9ad No-Presubmit: true No-Tree-Checks: true No-Try: true Bug: 929862 Reviewed-on: https://chromium-review.googlesource.com/c/1460485 Reviewed-by: Pavel Feldman Commit-Queue: Pavel Feldman Cr-Commit-Position: refs/heads/master@{#630188} --- chrome/browser/devtools/BUILD.gn | 4 +- .../devtools/inspector_protocol_config.json | 2 +- chrome/browser/devtools/protocol_string.cc | 205 ++++++++++++++++++ chrome/browser/devtools/protocol_string.h | 115 ++++++++++ content/browser/BUILD.gn | 4 +- content/browser/devtools/BUILD.gn | 4 +- content/browser/devtools/protocol_config.json | 2 +- .../browser/devtools/protocol_string.cc | 44 ++-- content/browser/devtools/protocol_string.h | 118 ++++++++++ content/browser/devtools/protocol_unittest.cc | 2 +- headless/BUILD.gn | 5 +- .../browser/devtools_api/domain_h.template | 2 +- .../devtools_api/domain_types_h.template | 2 +- .../lib/browser/protocol/protocol_string.cc | 202 +++++++++++++++++ .../lib/browser/protocol/protocol_string.h | 37 ++-- .../lib/browser/protocol/protocol_unittest.cc | 59 +++++ headless/protocol_config.json | 2 +- headless/public/internal/value_conversions.h | 2 +- third_party/inspector_protocol/BUILD.gn | 2 - third_party/inspector_protocol/DEPS | 71 +----- .../inspector_protocol/README.chromium | 2 +- third_party/inspector_protocol/README.md | 5 - .../inspector_protocol/code_generator.py | 18 +- .../inspector_protocol/encoding/cbor.cc | 42 +--- .../inspector_protocol/encoding/cbor.h | 18 +- .../inspector_protocol/encoding/cbor_test.cc | 6 +- .../inspector_protocol/inspector_protocol.gni | 2 - 27 files changed, 782 insertions(+), 195 deletions(-) create mode 100644 chrome/browser/devtools/protocol_string.cc create mode 100644 chrome/browser/devtools/protocol_string.h rename third_party/inspector_protocol/lib/base_string_adapter_cc.template => content/browser/devtools/protocol_string.cc (84%) create mode 100644 content/browser/devtools/protocol_string.h create mode 100644 headless/lib/browser/protocol/protocol_string.cc rename third_party/inspector_protocol/lib/base_string_adapter_h.template => headless/lib/browser/protocol/protocol_string.h (74%) create mode 100644 headless/lib/browser/protocol/protocol_unittest.cc diff --git a/chrome/browser/devtools/BUILD.gn b/chrome/browser/devtools/BUILD.gn index a6962666f4721..aedd2ea930053 100644 --- a/chrome/browser/devtools/BUILD.gn +++ b/chrome/browser/devtools/BUILD.gn @@ -15,8 +15,6 @@ if (!is_android) { import("$_inspector_protocol/inspector_protocol.gni") _protocol_generated = [ - "protocol/base_string_adapter.cc", - "protocol/base_string_adapter.h", "protocol/browser.cc", "protocol/browser.h", "protocol/cast.cc", @@ -208,6 +206,8 @@ static_library("devtools") { "protocol/page_handler.h", "protocol/target_handler.cc", "protocol/target_handler.h", + "protocol_string.cc", + "protocol_string.h", ] if (is_chromeos) { sources += [ diff --git a/chrome/browser/devtools/inspector_protocol_config.json b/chrome/browser/devtools/inspector_protocol_config.json index 6e98d2516f10e..e5838358271cf 100644 --- a/chrome/browser/devtools/inspector_protocol_config.json +++ b/chrome/browser/devtools/inspector_protocol_config.json @@ -33,6 +33,6 @@ "lib": { "package": "chrome/browser/devtools/protocol", "output": "protocol", - "string_header": "chrome/browser/devtools/protocol/base_string_adapter.h" + "string_header": "chrome/browser/devtools/protocol_string.h" } } diff --git a/chrome/browser/devtools/protocol_string.cc b/chrome/browser/devtools/protocol_string.cc new file mode 100644 index 0000000000000..db3a78ebcf663 --- /dev/null +++ b/chrome/browser/devtools/protocol_string.cc @@ -0,0 +1,205 @@ +// Copyright 2016 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 "chrome/browser/devtools/protocol_string.h" + +#include +#include "base/base64.h" +#include "base/json/json_reader.h" +#include "base/memory/ptr_util.h" +#include "base/strings/string16.h" +#include "base/strings/utf_string_conversions.h" +#include "base/values.h" +#include "chrome/browser/devtools/protocol/protocol.h" + +namespace protocol { + +std::unique_ptr toProtocolValue(const base::Value* value, + int depth) { + if (!value || !depth) + return nullptr; + if (value->is_none()) + return protocol::Value::null(); + if (value->is_bool()) { + bool inner; + value->GetAsBoolean(&inner); + return protocol::FundamentalValue::create(inner); + } + if (value->is_int()) { + int inner; + value->GetAsInteger(&inner); + return protocol::FundamentalValue::create(inner); + } + if (value->is_double()) { + double inner; + value->GetAsDouble(&inner); + return protocol::FundamentalValue::create(inner); + } + if (value->is_string()) { + std::string inner; + value->GetAsString(&inner); + return protocol::StringValue::create(inner); + } + if (value->is_list()) { + const base::ListValue* list = nullptr; + value->GetAsList(&list); + std::unique_ptr result = protocol::ListValue::create(); + for (size_t i = 0; i < list->GetSize(); i++) { + const base::Value* item = nullptr; + list->Get(i, &item); + std::unique_ptr converted = + toProtocolValue(item, depth - 1); + if (converted) + result->pushValue(std::move(converted)); + } + return std::move(result); + } + if (value->is_dict()) { + const base::DictionaryValue* dictionary = nullptr; + value->GetAsDictionary(&dictionary); + std::unique_ptr result = + protocol::DictionaryValue::create(); + for (base::DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); + it.Advance()) { + std::unique_ptr converted = + toProtocolValue(&it.value(), depth - 1); + if (converted) + result->setValue(it.key(), std::move(converted)); + } + return std::move(result); + } + return nullptr; +} + +std::unique_ptr toBaseValue(protocol::Value* value, int depth) { + if (!value || !depth) + return nullptr; + if (value->type() == protocol::Value::TypeNull) + return std::make_unique(); + if (value->type() == protocol::Value::TypeBoolean) { + bool inner; + value->asBoolean(&inner); + return base::WrapUnique(new base::Value(inner)); + } + if (value->type() == protocol::Value::TypeInteger) { + int inner; + value->asInteger(&inner); + return base::WrapUnique(new base::Value(inner)); + } + if (value->type() == protocol::Value::TypeDouble) { + double inner; + value->asDouble(&inner); + return base::WrapUnique(new base::Value(inner)); + } + if (value->type() == protocol::Value::TypeString) { + std::string inner; + value->asString(&inner); + return base::WrapUnique(new base::Value(inner)); + } + if (value->type() == protocol::Value::TypeArray) { + protocol::ListValue* list = protocol::ListValue::cast(value); + std::unique_ptr result(new base::ListValue()); + for (size_t i = 0; i < list->size(); i++) { + std::unique_ptr converted = + toBaseValue(list->at(i), depth - 1); + if (converted) + result->Append(std::move(converted)); + } + return std::move(result); + } + if (value->type() == protocol::Value::TypeObject) { + protocol::DictionaryValue* dict = protocol::DictionaryValue::cast(value); + std::unique_ptr result(new base::DictionaryValue()); + for (size_t i = 0; i < dict->size(); i++) { + protocol::DictionaryValue::Entry entry = dict->at(i); + std::unique_ptr converted = + toBaseValue(entry.second, depth - 1); + if (converted) + result->SetWithoutPathExpansion(entry.first, std::move(converted)); + } + return std::move(result); + } + return nullptr; +} + +// static +std::unique_ptr StringUtil::parseJSON( + const std::string& json) { + std::unique_ptr value = base::JSONReader::Read(json); + return toProtocolValue(value.get(), 1000); +} + +StringBuilder::StringBuilder() {} + +StringBuilder::~StringBuilder() {} + +void StringBuilder::append(const std::string& s) { + string_ += s; +} + +void StringBuilder::append(char c) { + string_ += c; +} + +void StringBuilder::append(const char* characters, size_t length) { + string_.append(characters, length); +} + +// static +void StringUtil::builderAppendQuotedString(StringBuilder& builder, + const String& str) { + builder.append('"'); + base::string16 str16 = base::UTF8ToUTF16(str); + escapeWideStringForJSON(reinterpret_cast(&str16[0]), + str16.length(), &builder); + builder.append('"'); +} + +std::string StringBuilder::toString() { + return string_; +} + +void StringBuilder::reserveCapacity(size_t capacity) { + string_.reserve(capacity); +} + +Binary::Binary() : bytes_(new base::RefCountedBytes) {} +Binary::Binary(const Binary& binary) : bytes_(binary.bytes_) {} +Binary::Binary(scoped_refptr bytes) : bytes_(bytes) {} +Binary::~Binary() {} + +String Binary::toBase64() const { + std::string encoded; + base::Base64Encode( + base::StringPiece(reinterpret_cast(bytes_->front()), + bytes_->size()), + &encoded); + return encoded; +} + +// static +Binary Binary::fromBase64(const String& base64, bool* success) { + std::string decoded; + *success = base::Base64Decode(base::StringPiece(base64), &decoded); + if (*success) { + return Binary::fromString(std::move(decoded)); + } + return Binary(); +} + +// static +Binary Binary::fromRefCounted(scoped_refptr memory) { + return Binary(memory); +} + +// static +Binary Binary::fromVector(std::vector data) { + return Binary(base::RefCountedBytes::TakeVector(&data)); +} + +// static +Binary Binary::fromString(std::string data) { + return Binary(base::RefCountedString::TakeString(&data)); +} +} // namespace protocol diff --git a/chrome/browser/devtools/protocol_string.h b/chrome/browser/devtools/protocol_string.h new file mode 100644 index 0000000000000..b7eea7c8234e4 --- /dev/null +++ b/chrome/browser/devtools/protocol_string.h @@ -0,0 +1,115 @@ +// Copyright 2017 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 CHROME_BROWSER_DEVTOOLS_PROTOCOL_STRING_H_ +#define CHROME_BROWSER_DEVTOOLS_PROTOCOL_STRING_H_ + +#include +#include +#include + +#include "base/logging.h" +#include "base/macros.h" +#include "base/memory/ref_counted_memory.h" +#include "base/strings/string_number_conversions.h" + +namespace base { +class Value; +} + +namespace protocol { + +class Value; + +using String = std::string; + +class StringBuilder { + public: + StringBuilder(); + ~StringBuilder(); + void append(const String&); + void append(char); + void append(const char*, size_t); + String toString(); + void reserveCapacity(size_t); + + private: + std::string string_; +}; + +class StringUtil { + public: + static String substring(const String& s, unsigned pos, unsigned len) { + return s.substr(pos, len); + } + static String fromInteger(int number) { return base::NumberToString(number); } + static String fromDouble(double number) { + String s = base::NumberToString(number); + if (!s.empty() && s[0] == '.') + s = "0" + s; + return s; + } + static double toDouble(const char* s, size_t len, bool* ok) { + double v = 0.0; + *ok = base::StringToDouble(std::string(s, len), &v); + return *ok ? v : 0.0; + } + static size_t find(const String& s, const char* needle) { + return s.find(needle); + } + static size_t find(const String& s, const String& needle) { + return s.find(needle); + } + static const size_t kNotFound = static_cast(-1); + static void builderAppend(StringBuilder& builder, const String& s) { + builder.append(s); + } + static void builderAppend(StringBuilder& builder, char c) { + builder.append(c); + } + static void builderAppend(StringBuilder& builder, const char* s, size_t len) { + builder.append(s, len); + } + static void builderAppendQuotedString(StringBuilder& builder, + const String& str); + static void builderReserve(StringBuilder& builder, unsigned capacity) { + builder.reserveCapacity(capacity); + } + static String builderToString(StringBuilder& builder) { + return builder.toString(); + } + + static std::unique_ptr parseJSON(const String&); +}; + +// A read-only sequence of uninterpreted bytes with reference-counted storage. +class Binary { + public: + Binary(const Binary&); + Binary(); + ~Binary(); + + const uint8_t* data() const { return bytes_->front(); } + size_t size() const { return bytes_->size(); } + scoped_refptr bytes() const { return bytes_; } + + String toBase64() const; + + static Binary fromBase64(const String& base64, bool* success); + static Binary fromRefCounted(scoped_refptr memory); + static Binary fromVector(std::vector data); + static Binary fromString(std::string data); + + private: + explicit Binary(scoped_refptr bytes); + scoped_refptr bytes_; +}; + +std::unique_ptr toProtocolValue(const base::Value* value, + int depth); +std::unique_ptr toBaseValue(protocol::Value* value, int depth); + +} // namespace protocol + +#endif // CHROME_BROWSER_DEVTOOLS_PROTOCOL_STRING_H_ diff --git a/content/browser/BUILD.gn b/content/browser/BUILD.gn index 6818a89fdb780..eaeb87e8aa5fe 100644 --- a/content/browser/BUILD.gn +++ b/content/browser/BUILD.gn @@ -232,8 +232,6 @@ jumbo_source_set("browser") { ] sources = [ - "$target_gen_dir/devtools/protocol/base_string_adapter.cc", - "$target_gen_dir/devtools/protocol/base_string_adapter.h", "$target_gen_dir/devtools/protocol/browser.cc", "$target_gen_dir/devtools/protocol/browser.h", "$target_gen_dir/devtools/protocol/dom.cc", @@ -722,6 +720,8 @@ jumbo_source_set("browser") { "devtools/protocol/tethering_handler.h", "devtools/protocol/tracing_handler.cc", "devtools/protocol/tracing_handler.h", + "devtools/protocol_string.cc", + "devtools/protocol_string.h", "devtools/render_frame_devtools_agent_host.cc", "devtools/render_frame_devtools_agent_host.h", "devtools/service_worker_devtools_agent_host.cc", diff --git a/content/browser/devtools/BUILD.gn b/content/browser/devtools/BUILD.gn index ec74261ad46e5..bb850cc737764 100644 --- a/content/browser/devtools/BUILD.gn +++ b/content/browser/devtools/BUILD.gn @@ -2,9 +2,9 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import("//tools/grit/grit_rule.gni") import("//third_party/brotli/brotli.gni") import("//third_party/inspector_protocol/inspector_protocol.gni") -import("//tools/grit/grit_rule.gni") group("resources") { if (!is_android) { @@ -75,8 +75,6 @@ inspector_protocol_generate("protocol_sources") { # These are relative to $target_gen_dir. outputs = [ - "protocol/base_string_adapter.cc", - "protocol/base_string_adapter.h", "protocol/browser.cc", "protocol/browser.h", "protocol/dom.cc", diff --git a/content/browser/devtools/protocol_config.json b/content/browser/devtools/protocol_config.json index c8ad5d48bd694..f98d48916ae1e 100644 --- a/content/browser/devtools/protocol_config.json +++ b/content/browser/devtools/protocol_config.json @@ -98,7 +98,7 @@ "lib": { "package": "content/browser/devtools/protocol", "output": "protocol", - "string_header": "content/browser/devtools/protocol/base_string_adapter.h", + "string_header": "content/browser/devtools/protocol_string.h", "export_macro": "CONTENT_EXPORT", "export_header": "content/common/content_export.h" } diff --git a/third_party/inspector_protocol/lib/base_string_adapter_cc.template b/content/browser/devtools/protocol_string.cc similarity index 84% rename from third_party/inspector_protocol/lib/base_string_adapter_cc.template rename to content/browser/devtools/protocol_string.cc index 5ce0f6882d504..0b92afa952a0c 100644 --- a/third_party/inspector_protocol/lib/base_string_adapter_cc.template +++ b/content/browser/devtools/protocol_string.cc @@ -1,11 +1,8 @@ -// This file is generated by DispatcherBase_cpp.template. - -// Copyright 2019 The Chromium Authors. All rights reserved. +// Copyright 2016 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 {{format_include(config.protocol.package, "base_string_adapter")}} -#include {{format_include(config.protocol.package, "Protocol")}} +#include "content/browser/devtools/protocol_string.h" #include #include "base/base64.h" @@ -14,10 +11,10 @@ #include "base/strings/string16.h" #include "base/strings/utf_string_conversions.h" #include "base/values.h" +#include "content/browser/devtools/protocol/protocol.h" -{% for namespace in config.protocol.namespace %} -namespace {{namespace}} { -{% endfor %} +namespace content { +namespace protocol { std::unique_ptr toProtocolValue( const base::Value* value, int depth) { @@ -76,33 +73,34 @@ std::unique_ptr toProtocolValue( return nullptr; } -std::unique_ptr toBaseValue(Value* value, int depth) { +std::unique_ptr toBaseValue( + protocol::Value* value, int depth) { if (!value || !depth) return nullptr; - if (value->type() == Value::TypeNull) + if (value->type() == protocol::Value::TypeNull) return std::make_unique(); - if (value->type() == Value::TypeBoolean) { + if (value->type() == protocol::Value::TypeBoolean) { bool inner; value->asBoolean(&inner); return base::WrapUnique(new base::Value(inner)); } - if (value->type() == Value::TypeInteger) { + if (value->type() == protocol::Value::TypeInteger) { int inner; value->asInteger(&inner); return base::WrapUnique(new base::Value(inner)); } - if (value->type() == Value::TypeDouble) { + if (value->type() == protocol::Value::TypeDouble) { double inner; value->asDouble(&inner); return base::WrapUnique(new base::Value(inner)); } - if (value->type() == Value::TypeString) { + if (value->type() == protocol::Value::TypeString) { std::string inner; value->asString(&inner); return base::WrapUnique(new base::Value(inner)); } - if (value->type() == Value::TypeArray) { - ListValue* list = ListValue::cast(value); + if (value->type() == protocol::Value::TypeArray) { + protocol::ListValue* list = protocol::ListValue::cast(value); std::unique_ptr result(new base::ListValue()); for (size_t i = 0; i < list->size(); i++) { std::unique_ptr converted = @@ -112,11 +110,11 @@ std::unique_ptr toBaseValue(Value* value, int depth) { } return std::move(result); } - if (value->type() == Value::TypeObject) { - DictionaryValue* dict = DictionaryValue::cast(value); + if (value->type() == protocol::Value::TypeObject) { + protocol::DictionaryValue* dict = protocol::DictionaryValue::cast(value); std::unique_ptr result(new base::DictionaryValue()); for (size_t i = 0; i < dict->size(); i++) { - DictionaryValue::Entry entry = dict->at(i); + protocol::DictionaryValue::Entry entry = dict->at(i); std::unique_ptr converted = toBaseValue(entry.second, depth - 1); if (converted) @@ -128,7 +126,7 @@ std::unique_ptr toBaseValue(Value* value, int depth) { } // static -std::unique_ptr StringUtil::parseJSON( +std::unique_ptr StringUtil::parseJSON( const std::string& json) { std::unique_ptr value = base::JSONReader::Read(json); return toProtocolValue(value.get(), 1000); @@ -206,7 +204,5 @@ Binary Binary::fromVector(std::vector data) { Binary Binary::fromString(std::string data) { return Binary(base::RefCountedString::TakeString(&data)); } - -{% for namespace in config.protocol.namespace %} -} // namespace {{namespace}} -{% endfor %} +} // namespace protocol +} // namespace content diff --git a/content/browser/devtools/protocol_string.h b/content/browser/devtools/protocol_string.h new file mode 100644 index 0000000000000..83cf9755af30d --- /dev/null +++ b/content/browser/devtools/protocol_string.h @@ -0,0 +1,118 @@ +// Copyright 2016 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_PROTOCOL_STRING_H_ +#define CONTENT_BROWSER_DEVTOOLS_PROTOCOL_STRING_H_ + +#include +#include +#include + +#include "base/logging.h" +#include "base/macros.h" +#include "base/memory/ref_counted_memory.h" +#include "base/strings/string_number_conversions.h" +#include "content/common/content_export.h" + +namespace base { +class Value; +} + +namespace content { +namespace protocol { + +class Value; + +using String = std::string; + +class CONTENT_EXPORT StringBuilder { + public: + StringBuilder(); + ~StringBuilder(); + void append(const String&); + void append(char); + void append(const char*, size_t); + String toString(); + void reserveCapacity(size_t); + + private: + std::string string_; +}; + +class CONTENT_EXPORT StringUtil { + public: + static String substring(const String& s, unsigned pos, unsigned len) { + return s.substr(pos, len); + } + static String fromInteger(int number) { return base::NumberToString(number); } + static String fromDouble(double number) { + String s = base::NumberToString(number); + if (!s.empty() && s[0] == '.') + s = "0" + s; + return s; + } + static double toDouble(const char* s, size_t len, bool* ok) { + double v = 0.0; + *ok = base::StringToDouble(std::string(s, len), &v); + return *ok ? v : 0.0; + } + static size_t find(const String& s, const char* needle) { + return s.find(needle); + } + static size_t find(const String& s, const String& needle) { + return s.find(needle); + } + static const size_t kNotFound = static_cast(-1); + static void builderAppend(StringBuilder& builder, const String& s) { + builder.append(s); + } + static void builderAppend(StringBuilder& builder, char c) { + builder.append(c); + } + static void builderAppend(StringBuilder& builder, const char* s, size_t len) { + builder.append(s, len); + } + static void builderAppendQuotedString(StringBuilder& builder, + const String& str); + static void builderReserve(StringBuilder& builder, unsigned capacity) { + builder.reserveCapacity(capacity); + } + static String builderToString(StringBuilder& builder) { + return builder.toString(); + } + + static std::unique_ptr parseJSON(const String&); +}; + +// A read-only sequence of uninterpreted bytes with reference-counted storage. +class CONTENT_EXPORT Binary { + public: + Binary(const Binary&); + Binary(); + ~Binary(); + + const uint8_t* data() const { return bytes_->front(); } + size_t size() const { return bytes_->size(); } + scoped_refptr bytes() const { return bytes_; } + + String toBase64() const; + + static Binary fromBase64(const String& base64, bool* success); + static Binary fromRefCounted(scoped_refptr memory); + static Binary fromVector(std::vector data); + static Binary fromString(std::string data); + + private: + explicit Binary(scoped_refptr bytes); + scoped_refptr bytes_; +}; + +std::unique_ptr toProtocolValue( + const base::Value* value, int depth); +std::unique_ptr toBaseValue(protocol::Value* value, int depth); + +} // namespace protocol +} // namespace content + +#endif // CONTENT_BROWSER_DEVTOOLS_PROTOCOL_STRING_H_ diff --git a/content/browser/devtools/protocol_unittest.cc b/content/browser/devtools/protocol_unittest.cc index 204b53a2c11db..e3801e3f55158 100644 --- a/content/browser/devtools/protocol_unittest.cc +++ b/content/browser/devtools/protocol_unittest.cc @@ -2,7 +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/protocol/base_string_adapter.h" +#include "content/browser/devtools/protocol_string.h" #include #include "testing/gtest/include/gtest/gtest.h" diff --git a/headless/BUILD.gn b/headless/BUILD.gn index e27f6a516fcb7..3bfc5cce3371d 100644 --- a/headless/BUILD.gn +++ b/headless/BUILD.gn @@ -248,8 +248,6 @@ inspector_protocol_generate("protocol_sources") { # These are relative to $target_gen_dir. outputs = [ - "lib/browser/protocol/base_string_adapter.cc", - "lib/browser/protocol/base_string_adapter.h", "lib/browser/protocol/dp_browser.cc", "lib/browser/protocol/dp_browser.h", "lib/browser/protocol/dp_headless_experimental.cc", @@ -307,6 +305,8 @@ jumbo_component("headless") { "lib/browser/protocol/headless_handler.h", "lib/browser/protocol/page_handler.cc", "lib/browser/protocol/page_handler.h", + "lib/browser/protocol/protocol_string.cc", + "lib/browser/protocol/protocol_string.h", "lib/browser/protocol/target_handler.cc", "lib/browser/protocol/target_handler.h", "lib/headless_content_client.cc", @@ -552,6 +552,7 @@ group("headless_tests") { test("headless_unittests") { sources = [ + "lib/browser/protocol/protocol_unittest.cc", "public/domains/types_unittest.cc", "public/util/error_reporter_unittest.cc", ] diff --git a/headless/lib/browser/devtools_api/domain_h.template b/headless/lib/browser/devtools_api/domain_h.template index a070791da8a6e..a113383c95169 100644 --- a/headless/lib/browser/devtools_api/domain_h.template +++ b/headless/lib/browser/devtools_api/domain_h.template @@ -10,7 +10,7 @@ #include "base/callback.h" #include "base/observer_list.h" #include "base/values.h" -#include "headless/lib/browser/protocol/base_string_adapter.h" +#include "headless/lib/browser/protocol/protocol_string.h" {% for domain_name in domain.dependencies %} #include "headless/public/devtools/domains/types_{{domain_name | camelcase_to_hacker_style}}.h" {% endfor %} diff --git a/headless/lib/browser/devtools_api/domain_types_h.template b/headless/lib/browser/devtools_api/domain_types_h.template index dd85de1ee41d2..1ad48132f966d 100644 --- a/headless/lib/browser/devtools_api/domain_types_h.template +++ b/headless/lib/browser/devtools_api/domain_types_h.template @@ -9,7 +9,7 @@ #include "base/optional.h" #include "base/values.h" -#include "headless/lib/browser/protocol/base_string_adapter.h" +#include "headless/lib/browser/protocol/protocol_string.h" {% for domain_name in domain.dependencies %} #include "headless/public/devtools/internal/types_forward_declarations_{{domain_name | camelcase_to_hacker_style}}.h" {% endfor %} diff --git a/headless/lib/browser/protocol/protocol_string.cc b/headless/lib/browser/protocol/protocol_string.cc new file mode 100644 index 0000000000000..b6b108546f855 --- /dev/null +++ b/headless/lib/browser/protocol/protocol_string.cc @@ -0,0 +1,202 @@ +// Copyright 2018 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 "headless/lib/browser/protocol/protocol_string.h" + +#include +#include "base/base64.h" +#include "base/json/json_reader.h" +#include "base/memory/ptr_util.h" +#include "base/strings/string16.h" +#include "base/strings/utf_string_conversions.h" +#include "base/values.h" +#include "headless/lib/browser/protocol/protocol.h" + +namespace headless { +namespace protocol { + +std::unique_ptr toProtocolValue(const base::Value* value, int depth) { + if (!value || !depth) + return nullptr; + if (value->is_none()) + return Value::null(); + if (value->is_bool()) { + bool inner; + value->GetAsBoolean(&inner); + return FundamentalValue::create(inner); + } + if (value->is_int()) { + int inner; + value->GetAsInteger(&inner); + return FundamentalValue::create(inner); + } + if (value->is_double()) { + double inner; + value->GetAsDouble(&inner); + return FundamentalValue::create(inner); + } + if (value->is_string()) { + std::string inner; + value->GetAsString(&inner); + return StringValue::create(inner); + } + if (value->is_list()) { + const base::ListValue* list = nullptr; + value->GetAsList(&list); + std::unique_ptr result = ListValue::create(); + for (size_t i = 0; i < list->GetSize(); i++) { + const base::Value* item = nullptr; + list->Get(i, &item); + std::unique_ptr converted = toProtocolValue(item, depth - 1); + if (converted) + result->pushValue(std::move(converted)); + } + return std::move(result); + } + if (value->is_dict()) { + const base::DictionaryValue* dictionary = nullptr; + value->GetAsDictionary(&dictionary); + std::unique_ptr result = DictionaryValue::create(); + for (base::DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd(); + it.Advance()) { + std::unique_ptr converted = + toProtocolValue(&it.value(), depth - 1); + if (converted) + result->setValue(it.key(), std::move(converted)); + } + return std::move(result); + } + return nullptr; +} + +std::unique_ptr toBaseValue(Value* value, int depth) { + if (!value || !depth) + return nullptr; + if (value->type() == Value::TypeNull) + return std::make_unique(); + if (value->type() == Value::TypeBoolean) { + bool inner; + value->asBoolean(&inner); + return base::WrapUnique(new base::Value(inner)); + } + if (value->type() == Value::TypeInteger) { + int inner; + value->asInteger(&inner); + return base::WrapUnique(new base::Value(inner)); + } + if (value->type() == Value::TypeDouble) { + double inner; + value->asDouble(&inner); + return base::WrapUnique(new base::Value(inner)); + } + if (value->type() == Value::TypeString) { + std::string inner; + value->asString(&inner); + return base::WrapUnique(new base::Value(inner)); + } + if (value->type() == Value::TypeArray) { + ListValue* list = ListValue::cast(value); + std::unique_ptr result(new base::ListValue()); + for (size_t i = 0; i < list->size(); i++) { + std::unique_ptr converted = + toBaseValue(list->at(i), depth - 1); + if (converted) + result->Append(std::move(converted)); + } + return std::move(result); + } + if (value->type() == Value::TypeObject) { + DictionaryValue* dict = DictionaryValue::cast(value); + std::unique_ptr result(new base::DictionaryValue()); + for (size_t i = 0; i < dict->size(); i++) { + DictionaryValue::Entry entry = dict->at(i); + std::unique_ptr converted = + toBaseValue(entry.second, depth - 1); + if (converted) + result->SetWithoutPathExpansion(entry.first, std::move(converted)); + } + return std::move(result); + } + return nullptr; +} + +// static +std::unique_ptr StringUtil::parseJSON(const std::string& json) { + std::unique_ptr value = base::JSONReader::Read(json); + return toProtocolValue(value.get(), 1000); +} + +StringBuilder::StringBuilder() {} + +StringBuilder::~StringBuilder() {} + +void StringBuilder::append(const std::string& s) { + string_ += s; +} + +void StringBuilder::append(char c) { + string_ += c; +} + +void StringBuilder::append(const char* characters, size_t length) { + string_.append(characters, length); +} + +// static +void StringUtil::builderAppendQuotedString(StringBuilder& builder, + const String& str) { + builder.append('"'); + base::string16 str16 = base::UTF8ToUTF16(str); + escapeWideStringForJSON(reinterpret_cast(&str16[0]), + str16.length(), &builder); + builder.append('"'); +} + +std::string StringBuilder::toString() { + return string_; +} + +void StringBuilder::reserveCapacity(size_t capacity) { + string_.reserve(capacity); +} + +Binary::Binary() : bytes_(new base::RefCountedBytes) {} +Binary::Binary(const Binary& binary) : bytes_(binary.bytes_) {} +Binary::Binary(scoped_refptr bytes) : bytes_(bytes) {} +Binary::~Binary() {} + +String Binary::toBase64() const { + std::string encoded; + base::Base64Encode( + base::StringPiece(reinterpret_cast(data()), size()), + &encoded); + return encoded; +} + +// static +Binary Binary::fromBase64(const String& base64, bool* success) { + std::string decoded; + *success = base::Base64Decode(base::StringPiece(base64), &decoded); + if (*success) { + return Binary::fromString(std::move(decoded)); + } + return Binary(); +} + +// static +Binary Binary::fromRefCounted(scoped_refptr memory) { + return Binary(memory); +} + +// static +Binary Binary::fromVector(std::vector data) { + return Binary(base::RefCountedBytes::TakeVector(&data)); +} + +// static +Binary Binary::fromString(std::string data) { + return Binary(base::RefCountedString::TakeString(&data)); +} +} // namespace protocol +} // namespace headless diff --git a/third_party/inspector_protocol/lib/base_string_adapter_h.template b/headless/lib/browser/protocol/protocol_string.h similarity index 74% rename from third_party/inspector_protocol/lib/base_string_adapter_h.template rename to headless/lib/browser/protocol/protocol_string.h index bf860b60083a0..3f0bf133f363c 100644 --- a/third_party/inspector_protocol/lib/base_string_adapter_h.template +++ b/headless/lib/browser/protocol/protocol_string.h @@ -1,11 +1,9 @@ -// This file is generated by Parser_h.template. - -// Copyright 2019 The Chromium Authors. All rights reserved. +// Copyright 2018 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 {{"_".join(config.protocol.namespace)}}_Parser_h -#define {{"_".join(config.protocol.namespace)}}_Parser_h +#ifndef HEADLESS_LIB_BROWSER_PROTOCOL_PROTOCOL_STRING_H_ +#define HEADLESS_LIB_BROWSER_PROTOCOL_PROTOCOL_STRING_H_ #include #include @@ -15,23 +13,20 @@ #include "base/macros.h" #include "base/memory/ref_counted_memory.h" #include "base/strings/string_number_conversions.h" -{% if config.lib.export_header %} -#include "{{config.lib.export_header}}" -{% endif %} +#include "headless/public/headless_export.h" namespace base { class Value; } -{% for namespace in config.protocol.namespace %} -namespace {{namespace}} { -{% endfor %} +namespace headless { +namespace protocol { class Value; using String = std::string; -class {{config.lib.export_macro}} StringBuilder { +class HEADLESS_EXPORT StringBuilder { public: StringBuilder(); ~StringBuilder(); @@ -45,12 +40,12 @@ class {{config.lib.export_macro}} StringBuilder { std::string string_; }; -class {{config.lib.export_macro}} StringUtil { +class HEADLESS_EXPORT StringUtil { public: static String substring(const String& s, unsigned pos, unsigned len) { return s.substr(pos, len); } - static String fromInteger(int number) { return base::NumberToString(number); } + static String fromInteger(int number) { return base::IntToString(number); } static String fromDouble(double number) { String s = base::NumberToString(number); if (!s.empty() && s[0] == '.') @@ -86,15 +81,12 @@ class {{config.lib.export_macro}} StringUtil { static String builderToString(StringBuilder& builder) { return builder.toString(); } - static std::vector utf8data(const String& str) { - return std::vector(str.begin(), str.end()); - } static std::unique_ptr parseJSON(const String&); }; // A read-only sequence of uninterpreted bytes with reference-counted storage. -class {{config.lib.export_macro}} Binary { +class HEADLESS_EXPORT Binary { public: Binary(const Binary&); Binary(); @@ -102,10 +94,8 @@ class {{config.lib.export_macro}} Binary { const uint8_t* data() const { return bytes_->front(); } size_t size() const { return bytes_->size(); } - scoped_refptr bytes() const { return bytes_; } String toBase64() const; - static Binary fromBase64(const String& base64, bool* success); static Binary fromRefCounted(scoped_refptr memory); static Binary fromVector(std::vector data); @@ -119,8 +109,7 @@ class {{config.lib.export_macro}} Binary { std::unique_ptr toProtocolValue(const base::Value* value, int depth); std::unique_ptr toBaseValue(Value* value, int depth); -{% for namespace in config.protocol.namespace %} -} // namespace {{namespace}} -{% endfor %} +} // namespace protocol +} // namespace headless -#endif // !defined({{"_".join(config.protocol.namespace)}}_Parser_h) +#endif // HEADLESS_LIB_BROWSER_PROTOCOL_PROTOCOL_STRING_H_ diff --git a/headless/lib/browser/protocol/protocol_unittest.cc b/headless/lib/browser/protocol/protocol_unittest.cc new file mode 100644 index 0000000000000..3d5419f9e327d --- /dev/null +++ b/headless/lib/browser/protocol/protocol_unittest.cc @@ -0,0 +1,59 @@ +// Copyright 2018 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 "headless/lib/browser/protocol/protocol_string.h" + +#include +#include "testing/gtest/include/gtest/gtest.h" + +namespace headless { +namespace protocol { +namespace { +TEST(ProtocolBinaryTest, base64EmptyArgs) { + EXPECT_EQ(protocol::String(), Binary().toBase64()); + + bool success = false; + Binary decoded = Binary::fromBase64("", &success); + EXPECT_TRUE(success); + EXPECT_EQ( + std::vector(), + std::vector(decoded.data(), decoded.data() + decoded.size())); +} + +TEST(ProtocolStringTest, AllBytesBase64Roundtrip) { + std::vector all_bytes; + for (int ii = 0; ii < 255; ++ii) + all_bytes.push_back(ii); + Binary binary = Binary::fromVector(all_bytes); + bool success = false; + Binary decoded = Binary::fromBase64(binary.toBase64(), &success); + EXPECT_TRUE(success); + std::vector decoded_bytes(decoded.data(), + decoded.data() + decoded.size()); + EXPECT_EQ(all_bytes, decoded_bytes); +} + +TEST(ProtocolStringTest, HelloWorldBase64Roundtrip) { + const char* kMsg = "Hello, world."; + std::vector msg(kMsg, kMsg + strlen(kMsg)); + EXPECT_EQ(strlen(kMsg), msg.size()); + + protocol::String encoded = Binary::fromVector(msg).toBase64(); + EXPECT_EQ("SGVsbG8sIHdvcmxkLg==", encoded); + bool success = false; + Binary decoded_binary = Binary::fromBase64(encoded, &success); + EXPECT_TRUE(success); + std::vector decoded(decoded_binary.data(), + decoded_binary.data() + decoded_binary.size()); + EXPECT_EQ(msg, decoded); +} + +TEST(ProtocolBinaryTest, InvalidBase64Decode) { + bool success = true; + Binary binary = Binary::fromBase64("This is not base64.", &success); + EXPECT_FALSE(success); +} +} // namespace +} // namespace protocol +} // namespace headless diff --git a/headless/protocol_config.json b/headless/protocol_config.json index 2a035a04a3aa8..b2511d389aa30 100644 --- a/headless/protocol_config.json +++ b/headless/protocol_config.json @@ -36,7 +36,7 @@ "lib": { "package": "headless/lib/browser/protocol", "output": "lib/browser/protocol", - "string_header": "headless/lib/browser/protocol/base_string_adapter.h", + "string_header": "headless/lib/browser/protocol/protocol_string.h", "export_macro": "HEADLESS_EXPORT", "export_header": "headless/public/headless_export.h" } diff --git a/headless/public/internal/value_conversions.h b/headless/public/internal/value_conversions.h index 126d1efd1d584..9f18f32983a48 100644 --- a/headless/public/internal/value_conversions.h +++ b/headless/public/internal/value_conversions.h @@ -7,7 +7,7 @@ #include -#include "headless/lib/browser/protocol/base_string_adapter.h" +#include "headless/lib/browser/protocol/protocol_string.h" #include "headless/public/util/error_reporter.h" namespace headless { diff --git a/third_party/inspector_protocol/BUILD.gn b/third_party/inspector_protocol/BUILD.gn index 6b82c845fab29..9631371579589 100644 --- a/third_party/inspector_protocol/BUILD.gn +++ b/third_party/inspector_protocol/BUILD.gn @@ -15,8 +15,6 @@ static_library("json_parser") { "encoding/platform.h", "encoding/span.h", "encoding/status.h", - "encoding/str_util.cc", - "encoding/str_util.h", ] } diff --git a/third_party/inspector_protocol/DEPS b/third_party/inspector_protocol/DEPS index 0e646e40a203b..81c9b08282b17 100644 --- a/third_party/inspector_protocol/DEPS +++ b/third_party/inspector_protocol/DEPS @@ -1,61 +1,12 @@ -# Copyright 2018 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 configures gclient, a tool that installs dependencies -# at particular versions into this source tree. See -# https://chromium.googlesource.com/chromium/tools/depot_tools.git -# To fetch these dependencies, run "gclient sync". The fetch -# command (from depot_tools) will also run gclient sync. - -vars = { - 'chromium_git': 'https://chromium.googlesource.com', +# The tests in the encoding/ 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. + +specific_include_rules = { + '^.*_test\.cc$': [ + '+base', + '+gmock', + '+gtest', + ], } - -# The keys in this dictionary define where the external dependencies (values) -# will be mapped into this gclient. The root of the gclient will -# be the parent directory of the directory in which this DEPS file is. -deps = { - # gn (the build tool) and clang-format. - 'buildtools': - Var('chromium_git') + '/chromium/buildtools.git@' + - '6fe4a3251488f7af86d64fc25cf442e817cf6133', - # The toolchain definitions (clang C++ compiler etc.) - 'src/third_party/mini_chromium/mini_chromium': - Var('chromium_git') + '/chromium/mini_chromium@' + - '737433ebade4d446643c6c07daae02a67e8decca', - # For writing unittests. - 'src/third_party/gtest/gtest': - Var('chromium_git') + '/external/github.com/google/googletest@' + - 'c091b0469ab4c04ee9411ef770f32360945f4c53', -} - -hooks = [ - { - 'name': 'clang_format_linux', - 'pattern': '.', - 'condition': 'host_os == "linux"', - 'action': [ - 'download_from_google_storage', - '--no_resume', - '--no_auth', - '--bucket=chromium-clang-format', - '--sha1_file', - 'buildtools/linux64/clang-format.sha1', - ], - }, - { - 'name': 'gn_linux', - 'pattern': '.', - 'condition': 'host_os == "linux"', - 'action': [ - 'download_from_google_storage', - '--no_resume', - '--no_auth', - '--bucket=chromium-gn', - '--sha1_file', - 'buildtools/linux64/gn.sha1', - ], - }, -] -recursedeps = ['buildtools'] diff --git a/third_party/inspector_protocol/README.chromium b/third_party/inspector_protocol/README.chromium index b7d510e7a6b6b..606a31ffddbbc 100644 --- a/third_party/inspector_protocol/README.chromium +++ b/third_party/inspector_protocol/README.chromium @@ -2,7 +2,7 @@ Name: inspector protocol Short Name: inspector_protocol URL: https://chromium.googlesource.com/deps/inspector_protocol/ Version: 0 -Revision: dfaf19b7350529d9d454ae79b6747ee57cd1c6d4 +Revision: 8dfd1432319592c9ed70676b6cc166ee98025664 License: BSD License File: LICENSE Security Critical: no diff --git a/third_party/inspector_protocol/README.md b/third_party/inspector_protocol/README.md index da3f93f3f3b49..4eff4338ffd1a 100644 --- a/third_party/inspector_protocol/README.md +++ b/third_party/inspector_protocol/README.md @@ -26,8 +26,3 @@ to fetch the package (and dependencies) and build and run the tests: gn gen out/Release ninja -C out/Release json_parser_test out/Release/json_parser_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 diff --git a/third_party/inspector_protocol/code_generator.py b/third_party/inspector_protocol/code_generator.py index 0ba7276623363..bee9ccecb1241 100755 --- a/third_party/inspector_protocol/code_generator.py +++ b/third_party/inspector_protocol/code_generator.py @@ -623,7 +623,7 @@ def main(): lib_templates_dir = os.path.join(module_path, "lib") # Note these should be sorted in the right order. # TODO(dgozman): sort them programmatically based on commented includes. - protocol_h_templates = [ + lib_h_templates = [ "ErrorSupport_h.template", "Values_h.template", "Object_h.template", @@ -634,7 +634,7 @@ def main(): "Parser_h.template", ] - protocol_cpp_templates = [ + lib_cpp_templates = [ "Protocol_cpp.template", "ErrorSupport_cpp.template", "Values_cpp.template", @@ -649,14 +649,6 @@ def main(): "FrontendChannel_h.template", ] - base_string_adapter_h_templates = [ - "base_string_adapter_h.template", - ] - - base_string_adapter_cc_templates = [ - "base_string_adapter_cc.template", - ] - def generate_lib_file(file_name, template_files): parts = [] for template_file in template_files: @@ -666,10 +658,8 @@ def generate_lib_file(file_name, template_files): outputs[file_name] = "\n\n".join(parts) generate_lib_file(os.path.join(config.lib.output, to_file_name(config, "Forward.h")), forward_h_templates) - generate_lib_file(os.path.join(config.lib.output, to_file_name(config, "Protocol.h")), protocol_h_templates) - generate_lib_file(os.path.join(config.lib.output, to_file_name(config, "Protocol.cpp")), protocol_cpp_templates) - generate_lib_file(os.path.join(config.lib.output, to_file_name(config, "base_string_adapter.h")), base_string_adapter_h_templates) - generate_lib_file(os.path.join(config.lib.output, to_file_name(config, "base_string_adapter.cc")), base_string_adapter_cc_templates) + generate_lib_file(os.path.join(config.lib.output, to_file_name(config, "Protocol.h")), lib_h_templates) + generate_lib_file(os.path.join(config.lib.output, to_file_name(config, "Protocol.cpp")), lib_cpp_templates) # Make gyp / make generatos happy, otherwise make rebuilds world. inputs_ts = max(map(os.path.getmtime, inputs)) diff --git a/third_party/inspector_protocol/encoding/cbor.cc b/third_party/inspector_protocol/encoding/cbor.cc index 2f1efaedc019c..105ccf31f133c 100644 --- a/third_party/inspector_protocol/encoding/cbor.cc +++ b/third_party/inspector_protocol/encoding/cbor.cc @@ -47,13 +47,7 @@ static constexpr uint8_t kEncodedNull = EncodeInitialByte(MajorType::SIMPLE_VALUE, 22); static constexpr uint8_t kInitialByteForDouble = EncodeInitialByte(MajorType::SIMPLE_VALUE, 27); -} // namespace - -uint8_t EncodeTrue() { return kEncodedTrue; } -uint8_t EncodeFalse() { return kEncodedFalse; } -uint8_t EncodeNull() { return kEncodedNull; } -namespace { // TAG 24 indicates that what follows is a byte string which is // encoded in CBOR format. We use this as a wrapper for // maps and arrays, allowing us to skip them, because the @@ -77,19 +71,7 @@ static constexpr uint8_t kInitialByteIndefiniteLengthMap = // length maps / arrays. static constexpr uint8_t kStopByte = EncodeInitialByte(MajorType::SIMPLE_VALUE, 31); -} // namespace - -uint8_t EncodeIndefiniteLengthArrayStart() { - return kInitialByteIndefiniteLengthArray; -} - -uint8_t EncodeIndefiniteLengthMapStart() { - return kInitialByteIndefiniteLengthMap; -} -uint8_t EncodeStop() { return kStopByte; } - -namespace { // See RFC 7049 Table 3 and Section 2.4.4.2. This is used as a prefix for // arbitrary binary data encoded as BYTE_STRING. static constexpr uint8_t kExpectedConversionToBase64Tag = @@ -134,8 +116,7 @@ void WriteTokenStart(MajorType type, uint64_t value, if (value <= std::numeric_limits::max()) { // 32 bit uint: 1 initial byte + 4 bytes payload. encoded->push_back(EncodeInitialByte(type, kAdditionalInformation4Bytes)); - WriteBytesMostSignificantByteFirst(static_cast(value), - encoded); + WriteBytesMostSignificantByteFirst(value, encoded); return; } // 64 bit uint: 1 initial byte + 8 bytes payload. @@ -419,7 +400,7 @@ bool ParseValue(int32_t stack_depth, CBORTokenizer* tokenizer, if (tokenizer->TokenTag() == CBORTokenTag::ENVELOPE) tokenizer->EnterEnvelope(); switch (tokenizer->TokenTag()) { - case CBORTokenTag::ERROR_VALUE: + case CBORTokenTag::ERROR: out->HandleError(tokenizer->Status()); return false; case CBORTokenTag::DONE: @@ -482,7 +463,7 @@ bool ParseArray(int32_t stack_depth, CBORTokenizer* tokenizer, Status{Error::CBOR_UNEXPECTED_EOF_IN_ARRAY, tokenizer->Status().pos}); return false; } - if (tokenizer->TokenTag() == CBORTokenTag::ERROR_VALUE) { + if (tokenizer->TokenTag() == CBORTokenTag::ERROR) { out->HandleError(tokenizer->Status()); return false; } @@ -508,7 +489,7 @@ bool ParseMap(int32_t stack_depth, CBORTokenizer* tokenizer, Status{Error::CBOR_UNEXPECTED_EOF_IN_MAP, tokenizer->Status().pos}); return false; } - if (tokenizer->TokenTag() == CBORTokenTag::ERROR_VALUE) { + if (tokenizer->TokenTag() == CBORTokenTag::ERROR) { out->HandleError(tokenizer->Status()); return false; } @@ -541,7 +522,7 @@ void ParseCBOR(span bytes, JSONParserHandler* json_out) { return; } CBORTokenizer tokenizer(bytes); - if (tokenizer.TokenTag() == CBORTokenTag::ERROR_VALUE) { + if (tokenizer.TokenTag() == CBORTokenTag::ERROR) { json_out->HandleError(tokenizer.Status()); return; } @@ -556,7 +537,7 @@ void ParseCBOR(span bytes, JSONParserHandler* json_out) { } if (!ParseMap(/*stack_depth=*/1, &tokenizer, json_out)) return; if (tokenizer.TokenTag() == CBORTokenTag::DONE) return; - if (tokenizer.TokenTag() == CBORTokenTag::ERROR_VALUE) { + if (tokenizer.TokenTag() == CBORTokenTag::ERROR) { json_out->HandleError(tokenizer.Status()); return; } @@ -572,7 +553,7 @@ CBORTokenizer::~CBORTokenizer() {} CBORTokenTag CBORTokenizer::TokenTag() const { return token_tag_; } void CBORTokenizer::Next() { - if (token_tag_ == CBORTokenTag::ERROR_VALUE || token_tag_ == CBORTokenTag::DONE) + if (token_tag_ == CBORTokenTag::ERROR || token_tag_ == CBORTokenTag::DONE) return; ReadNextToken(/*enter_envelope=*/false); } @@ -587,10 +568,9 @@ Status CBORTokenizer::Status() const { return status_; } int32_t CBORTokenizer::GetInt32() const { assert(token_tag_ == CBORTokenTag::INT32); // The range checks happen in ::ReadNextToken(). - return static_cast( - token_start_type_ == MajorType::UNSIGNED - ? token_start_internal_value_ - : -static_cast(token_start_internal_value_) - 1); + return token_start_type_ == MajorType::UNSIGNED + ? token_start_internal_value_ + : -static_cast(token_start_internal_value_) - 1; } double CBORTokenizer::GetDouble() const { @@ -764,7 +744,7 @@ void CBORTokenizer::SetToken(CBORTokenTag token_tag, } void CBORTokenizer::SetError(Error error) { - token_tag_ = CBORTokenTag::ERROR_VALUE; + token_tag_ = CBORTokenTag::ERROR; status_.error = error; } diff --git a/third_party/inspector_protocol/encoding/cbor.h b/third_party/inspector_protocol/encoding/cbor.h index e0cf395c459ed..bf92a5970a22c 100644 --- a/third_party/inspector_protocol/encoding/cbor.h +++ b/third_party/inspector_protocol/encoding/cbor.h @@ -56,14 +56,6 @@ void EncodeBinary(span in, std::vector* out); // with additional info = 27, followed by 8 bytes in big endian. void EncodeDouble(double value, std::vector* out); -// Some constants for CBOR tokens that only take a single byte on the wire. -uint8_t EncodeTrue(); -uint8_t EncodeFalse(); -uint8_t EncodeNull(); -uint8_t EncodeIndefiniteLengthArrayStart(); -uint8_t EncodeIndefiniteLengthMapStart(); -uint8_t EncodeStop(); - // An envelope indicates the byte length of a wrapped item. // We use this for maps and array, which allows the decoder // to skip such (nested) values whole sale. @@ -105,7 +97,7 @@ void ParseCBOR(span bytes, JSONParserHandler* json_out); enum class CBORTokenTag { // Encountered an error in the structure of the message. Consult // status() for details. - ERROR_VALUE, + ERROR, // Booleans and NULL. TRUE_VALUE, FALSE_VALUE, @@ -148,7 +140,7 @@ class CBORTokenizer { ~CBORTokenizer(); // Identifies the current token that we're looking at, - // or ERROR_VALUE (in which ase ::Status() has details) + // or ERROR (in which ase ::Status() has details) // or DONE (if we're past the last token). CBORTokenTag TokenTag() const; @@ -160,10 +152,10 @@ class CBORTokenizer { // letting the client explore the nested structure. void EnterEnvelope(); - // If TokenTag() is CBORTokenTag::ERROR_VALUE, then Status().error describes + // If TokenTag() is CBORTokenTag::ERROR, then Status().error describes // the error more precisely; otherwise it'll be set to Error::OK. // In either case, Status().pos is the current position. - struct Status Status() const; + inspector_protocol::Status Status() const; // The following methods retrieve the token values. They can only // be called if TokenTag() matches. @@ -191,7 +183,7 @@ class CBORTokenizer { span bytes_; CBORTokenTag token_tag_; - struct Status status_; + inspector_protocol::Status status_; int64_t token_byte_length_; cbor_internals::MajorType token_start_type_; uint64_t token_start_internal_value_; diff --git a/third_party/inspector_protocol/encoding/cbor_test.cc b/third_party/inspector_protocol/encoding/cbor_test.cc index a71daada31aa1..1b928f1345d4d 100644 --- a/third_party/inspector_protocol/encoding/cbor_test.cc +++ b/third_party/inspector_protocol/encoding/cbor_test.cc @@ -113,7 +113,7 @@ TEST(EncodeDecodeInt32Test, CantRoundtripUint32) { // Now try to decode; we treat this as an invalid INT32. CBORTokenizer tokenizer(span(&encoded[0], encoded.size())); // 0xdeadbeef is > std::numerical_limits::max(). - EXPECT_EQ(CBORTokenTag::ERROR_VALUE, tokenizer.TokenTag()); + EXPECT_EQ(CBORTokenTag::ERROR, tokenizer.TokenTag()); EXPECT_EQ(Error::CBOR_INVALID_INT32, tokenizer.Status().error); } @@ -136,7 +136,7 @@ TEST(EncodeDecodeInt32Test, DecodeErrorCases) { span encoded_bytes(&test.data[0], test.data.size()); CBORTokenizer tokenizer( span(&encoded_bytes[0], encoded_bytes.size())); - EXPECT_EQ(CBORTokenTag::ERROR_VALUE, tokenizer.TokenTag()); + EXPECT_EQ(CBORTokenTag::ERROR, tokenizer.TokenTag()); EXPECT_EQ(Error::CBOR_INVALID_INT32, tokenizer.Status().error); } } @@ -292,7 +292,7 @@ TEST(EncodeDecodeString16Test, ErrorCases) { for (const TestCase& test : tests) { SCOPED_TRACE(test.msg); CBORTokenizer tokenizer(span(&test.data[0], test.data.size())); - EXPECT_EQ(CBORTokenTag::ERROR_VALUE, tokenizer.TokenTag()); + EXPECT_EQ(CBORTokenTag::ERROR, tokenizer.TokenTag()); EXPECT_EQ(Error::CBOR_INVALID_STRING16, tokenizer.Status().error); } } diff --git a/third_party/inspector_protocol/inspector_protocol.gni b/third_party/inspector_protocol/inspector_protocol.gni index b4acdf0b7b062..af2f21691cd75 100644 --- a/third_party/inspector_protocol/inspector_protocol.gni +++ b/third_party/inspector_protocol/inspector_protocol.gni @@ -31,8 +31,6 @@ template("inspector_protocol_generate") { inputs = [ invoker.config_file, - "$inspector_protocol_dir/lib/base_string_adapter_cc.template", - "$inspector_protocol_dir/lib/base_string_adapter_h.template", "$inspector_protocol_dir/lib/Allocator_h.template", "$inspector_protocol_dir/lib/Array_h.template", "$inspector_protocol_dir/lib/DispatcherBase_cpp.template",