From 4c54cfc692e4a2f7e7d6e0dfece9cf1cd0e7c5e2 Mon Sep 17 00:00:00 2001 From: Micha Hanselmann Date: Tue, 13 Aug 2019 22:15:34 -0700 Subject: [PATCH] move net converter to gin (#19734) --- filenames.gni | 4 +- shell/browser/api/atom_api_dialog.cc | 2 +- .../net_converter.cc | 50 +++++------ shell/common/gin_converters/net_converter.h | 87 +++++++++++++++++++ .../net_converter_gin_adapter.h | 34 -------- shell/common/gin_converters/std_converter.h | 16 ++++ .../value_converter_gin_adapter.h | 13 +++ .../native_mate_converters/net_converter.h | 65 ++++++-------- 8 files changed, 169 insertions(+), 102 deletions(-) rename shell/common/{native_mate_converters => gin_converters}/net_converter.cc (89%) create mode 100644 shell/common/gin_converters/net_converter.h delete mode 100644 shell/common/gin_converters/net_converter_gin_adapter.h diff --git a/filenames.gni b/filenames.gni index 589f6b886682..b3b601f6b69d 100644 --- a/filenames.gni +++ b/filenames.gni @@ -474,7 +474,8 @@ filenames = { "shell/common/gin_converters/message_box_converter.cc", "shell/common/gin_converters/message_box_converter.h", "shell/common/gin_converters/native_window_converter.h", - "shell/common/gin_converters/net_converter_gin_adapter.h", + "shell/common/gin_converters/net_converter.cc", + "shell/common/gin_converters/net_converter.h", "shell/common/gin_converters/std_converter.h", "shell/common/gin_converters/string16_converter.h", "shell/common/gin_converters/value_converter_gin_adapter.h", @@ -507,7 +508,6 @@ filenames = { "shell/common/native_mate_converters/image_converter.h", "shell/common/native_mate_converters/map_converter.h", "shell/common/native_mate_converters/native_window_converter.h", - "shell/common/native_mate_converters/net_converter.cc", "shell/common/native_mate_converters/net_converter.h", "shell/common/native_mate_converters/network_converter.cc", "shell/common/native_mate_converters/network_converter.h", diff --git a/shell/browser/api/atom_api_dialog.cc b/shell/browser/api/atom_api_dialog.cc index 3025278e2fd4..6c3a194936c3 100644 --- a/shell/browser/api/atom_api_dialog.cc +++ b/shell/browser/api/atom_api_dialog.cc @@ -14,7 +14,7 @@ #include "shell/common/gin_converters/file_dialog_converter.h" #include "shell/common/gin_converters/message_box_converter.h" #include "shell/common/gin_converters/native_window_converter.h" -#include "shell/common/gin_converters/net_converter_gin_adapter.h" +#include "shell/common/gin_converters/net_converter.h" #include "shell/common/node_includes.h" #include "shell/common/promise_util.h" diff --git a/shell/common/native_mate_converters/net_converter.cc b/shell/common/gin_converters/net_converter.cc similarity index 89% rename from shell/common/native_mate_converters/net_converter.cc rename to shell/common/gin_converters/net_converter.cc index f48f64bda153..1172af0024da 100644 --- a/shell/common/native_mate_converters/net_converter.cc +++ b/shell/common/gin_converters/net_converter.cc @@ -1,8 +1,8 @@ -// Copyright (c) 2015 GitHub, Inc. +// Copyright (c) 2019 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. -#include "shell/common/native_mate_converters/net_converter.h" +#include "shell/common/gin_converters/net_converter.h" #include #include @@ -12,7 +12,7 @@ #include "base/strings/string_number_conversions.h" #include "base/strings/string_util.h" #include "base/values.h" -#include "native_mate/dictionary.h" +#include "gin/dictionary.h" #include "net/base/upload_bytes_element_reader.h" #include "net/base/upload_data_stream.h" #include "net/base/upload_element_reader.h" @@ -21,13 +21,13 @@ #include "net/cert/x509_util.h" #include "net/http/http_response_headers.h" #include "services/network/public/cpp/resource_request.h" -#include "shell/browser/net/cert_verifier_client.h" -#include "shell/common/native_mate_converters/gurl_converter.h" -#include "shell/common/native_mate_converters/string16_converter.h" -#include "shell/common/native_mate_converters/value_converter.h" +#include "shell/common/gin_converters/gurl_converter.h" +#include "shell/common/gin_converters/std_converter.h" +#include "shell/common/gin_converters/string16_converter.h" +#include "shell/common/gin_converters/value_converter_gin_adapter.h" #include "shell/common/node_includes.h" -namespace mate { +namespace gin { namespace { @@ -54,20 +54,20 @@ bool CertFromData(const std::string& data, v8::Local Converter::ToV8( v8::Isolate* isolate, const net::AuthChallengeInfo& val) { - mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + gin::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); dict.Set("isProxy", val.is_proxy); dict.Set("scheme", val.scheme); dict.Set("host", val.challenger.host()); dict.Set("port", static_cast(val.challenger.port())); dict.Set("realm", val.realm); - return mate::ConvertToV8(isolate, dict); + return gin::ConvertToV8(isolate, dict); } // static v8::Local Converter>::ToV8( v8::Isolate* isolate, const scoped_refptr& val) { - mate::Dictionary dict(isolate, v8::Object::New(isolate)); + gin::Dictionary dict(isolate, v8::Object::New(isolate)); std::string encoded_data; net::X509Certificate::GetPEMEncoded(val->cert_buffer(), &encoded_data); @@ -99,14 +99,14 @@ v8::Local Converter>::ToV8( dict.Set("issuerCert", issuer_cert); } - return dict.GetHandle(); + return ConvertToV8(isolate, dict); } bool Converter>::FromV8( v8::Isolate* isolate, v8::Local val, scoped_refptr* out) { - mate::Dictionary dict; + gin::Dictionary dict(nullptr); if (!ConvertFromV8(isolate, val, &dict)) return false; @@ -137,7 +137,7 @@ bool Converter>::FromV8( v8::Local Converter::ToV8( v8::Isolate* isolate, const net::CertPrincipal& val) { - mate::Dictionary dict(isolate, v8::Object::New(isolate)); + gin::Dictionary dict(isolate, v8::Object::New(isolate)); dict.Set("commonName", val.common_name); dict.Set("organizations", val.organization_names); @@ -146,7 +146,7 @@ v8::Local Converter::ToV8( dict.Set("state", val.state_or_province_name); dict.Set("country", val.country_name); - return dict.GetHandle(); + return ConvertToV8(isolate, dict); } // static @@ -191,7 +191,7 @@ bool Converter::FromV8( return false; } std::string value; - mate::ConvertFromV8(isolate, localStrVal, &value); + gin::ConvertFromV8(isolate, localStrVal, &value); out->AddHeader(key + ": " + value); return true; }; @@ -205,7 +205,7 @@ bool Converter::FromV8( return false; } std::string key; - mate::ConvertFromV8(isolate, keyVal, &key); + gin::ConvertFromV8(isolate, keyVal, &key); auto localVal = headers->Get(context, keyVal).ToLocalChecked(); if (localVal->IsArray()) { @@ -229,11 +229,11 @@ bool Converter::FromV8( v8::Local Converter::ToV8( v8::Isolate* isolate, const network::ResourceRequest& val) { - mate::Dictionary dict(isolate, v8::Object::New(isolate)); + gin::Dictionary dict(isolate, v8::Object::New(isolate)); dict.Set("method", val.method); dict.Set("url", val.url.spec()); dict.Set("referrer", val.referrer.spec()); - mate::Dictionary headers(isolate, v8::Object::New(isolate)); + gin::Dictionary headers(isolate, v8::Object::New(isolate)); for (net::HttpRequestHeaders::Iterator it(val.headers); it.GetNext();) headers.Set(it.name(), it.value()); dict.Set("headers", headers); @@ -242,7 +242,7 @@ v8::Local Converter::ToV8( v8::Local arr = v8::Array::New(isolate, elements.size()); for (size_t i = 0; i < elements.size(); ++i) { const auto& element = elements[i]; - mate::Dictionary upload_data(isolate, v8::Object::New(isolate)); + gin::Dictionary upload_data(isolate, v8::Object::New(isolate)); switch (element.type()) { case network::mojom::DataElementType::kFile: upload_data.Set("file", element.path().value()); @@ -259,27 +259,27 @@ v8::Local Converter::ToV8( NOTREACHED() << "Found unsupported data element"; } arr->Set(isolate->GetCurrentContext(), static_cast(i), - upload_data.GetHandle()) + ConvertToV8(isolate, upload_data)) .Check(); } dict.Set("uploadData", arr); } - return dict.GetHandle(); + return ConvertToV8(isolate, dict); } // static v8::Local Converter::ToV8( v8::Isolate* isolate, electron::VerifyRequestParams val) { - mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + gin::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); dict.Set("hostname", val.hostname); dict.Set("certificate", val.certificate); dict.Set("verificationResult", val.default_result); dict.Set("errorCode", val.error_code); - return dict.GetHandle(); + return ConvertToV8(isolate, dict); } -} // namespace mate +} // namespace gin namespace electron { diff --git a/shell/common/gin_converters/net_converter.h b/shell/common/gin_converters/net_converter.h new file mode 100644 index 000000000000..b8038790718a --- /dev/null +++ b/shell/common/gin_converters/net_converter.h @@ -0,0 +1,87 @@ +// Copyright (c) 2019 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef SHELL_COMMON_GIN_CONVERTERS_NET_CONVERTER_H_ +#define SHELL_COMMON_GIN_CONVERTERS_NET_CONVERTER_H_ + +#include "base/memory/ref_counted.h" +#include "gin/converter.h" +#include "shell/browser/net/cert_verifier_client.h" + +namespace base { +class DictionaryValue; +class ListValue; +} // namespace base + +namespace net { +class AuthChallengeInfo; +class URLRequest; +class X509Certificate; +class HttpResponseHeaders; +struct CertPrincipal; +} // namespace net + +namespace network { +struct ResourceRequest; +} + +namespace gin { + +template <> +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, + const net::AuthChallengeInfo& val); +}; + +template <> +struct Converter> { + static v8::Local ToV8( + v8::Isolate* isolate, + const scoped_refptr& val); + + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + scoped_refptr* out); +}; + +template <> +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, + const net::CertPrincipal& val); +}; + +template <> +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, + net::HttpResponseHeaders* headers); + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + net::HttpResponseHeaders* out); +}; + +template <> +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, + const network::ResourceRequest& val); +}; + +template <> +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, + electron::VerifyRequestParams val); +}; + +} // namespace gin + +namespace electron { + +void FillRequestDetails(base::DictionaryValue* details, + const net::URLRequest* request); + +void GetUploadData(base::ListValue* upload_data_list, + const net::URLRequest* request); + +} // namespace electron + +#endif // SHELL_COMMON_GIN_CONVERTERS_NET_CONVERTER_H_ diff --git a/shell/common/gin_converters/net_converter_gin_adapter.h b/shell/common/gin_converters/net_converter_gin_adapter.h deleted file mode 100644 index 8e490f230890..000000000000 --- a/shell/common/gin_converters/net_converter_gin_adapter.h +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) 2019 GitHub, Inc. -// Use of this source code is governed by the MIT license that can be -// found in the LICENSE file. - -#ifndef SHELL_COMMON_GIN_CONVERTERS_NET_CONVERTER_GIN_ADAPTER_H_ -#define SHELL_COMMON_GIN_CONVERTERS_NET_CONVERTER_GIN_ADAPTER_H_ - -#include "gin/converter.h" -#include "shell/common/native_mate_converters/net_converter.h" - -// TODO(deermichel): replace adapter with real implementation after removing -// mate -// -- this adapter forwards all conversions to the existing mate converter -- -// (other direction might be preferred, but this is safer for now :D) - -namespace gin { - -template <> -struct Converter> { - static v8::Local ToV8( - v8::Isolate* isolate, - const scoped_refptr& val) { - return mate::ConvertToV8(isolate, val); - } - static bool FromV8(v8::Isolate* isolate, - v8::Local val, - scoped_refptr* out) { - return mate::ConvertFromV8(isolate, val, out); - } -}; - -} // namespace gin - -#endif // SHELL_COMMON_GIN_CONVERTERS_NET_CONVERTER_GIN_ADAPTER_H_ diff --git a/shell/common/gin_converters/std_converter.h b/shell/common/gin_converters/std_converter.h index 2710ab3bbe73..f9a66a0b9fc6 100644 --- a/shell/common/gin_converters/std_converter.h +++ b/shell/common/gin_converters/std_converter.h @@ -11,6 +11,22 @@ namespace gin { +template <> +struct Converter> { + static v8::Local ToV8(v8::Isolate* isolate, + v8::Local val) { + return val; + } + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + v8::Local* out) { + if (!val->IsArray()) + return false; + *out = v8::Local::Cast(val); + return true; + } +}; + template struct Converter> { static v8::Local ToV8(v8::Isolate* isolate, diff --git a/shell/common/gin_converters/value_converter_gin_adapter.h b/shell/common/gin_converters/value_converter_gin_adapter.h index 79b1489246a2..04d1e96912da 100644 --- a/shell/common/gin_converters/value_converter_gin_adapter.h +++ b/shell/common/gin_converters/value_converter_gin_adapter.h @@ -12,6 +12,19 @@ namespace gin { +template <> +struct Converter { + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + base::DictionaryValue* out) { + return mate::ConvertFromV8(isolate, val, out); + } + static v8::Local ToV8(v8::Isolate* isolate, + const base::DictionaryValue& val) { + return mate::ConvertToV8(isolate, val); + } +}; + template <> struct Converter { static bool FromV8(v8::Isolate* isolate, diff --git a/shell/common/native_mate_converters/net_converter.h b/shell/common/native_mate_converters/net_converter.h index 52b13e271642..5a7c2eecab22 100644 --- a/shell/common/native_mate_converters/net_converter.h +++ b/shell/common/native_mate_converters/net_converter.h @@ -5,86 +5,71 @@ #ifndef SHELL_COMMON_NATIVE_MATE_CONVERTERS_NET_CONVERTER_H_ #define SHELL_COMMON_NATIVE_MATE_CONVERTERS_NET_CONVERTER_H_ -#include "base/memory/ref_counted.h" #include "native_mate/converter.h" - -namespace base { -class DictionaryValue; -class ListValue; -} // namespace base - -namespace electron { -struct VerifyRequestParams; -} - -namespace net { -class AuthChallengeInfo; -class URLRequest; -class X509Certificate; -class HttpResponseHeaders; -struct CertPrincipal; -} // namespace net - -namespace network { -struct ResourceRequest; -} +#include "shell/common/gin_converters/net_converter.h" namespace mate { template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, - const net::AuthChallengeInfo& val); + const net::AuthChallengeInfo& val) { + return gin::ConvertToV8(isolate, val); + } }; template <> struct Converter> { static v8::Local ToV8( v8::Isolate* isolate, - const scoped_refptr& val); + const scoped_refptr& val) { + return gin::ConvertToV8(isolate, val); + } static bool FromV8(v8::Isolate* isolate, v8::Local val, - scoped_refptr* out); + scoped_refptr* out) { + return gin::ConvertFromV8(isolate, val, out); + } }; template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, - const net::CertPrincipal& val); + const net::CertPrincipal& val) { + return gin::ConvertToV8(isolate, val); + } }; template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, - net::HttpResponseHeaders* headers); + net::HttpResponseHeaders* headers) { + return gin::ConvertToV8(isolate, headers); + } static bool FromV8(v8::Isolate* isolate, v8::Local val, - net::HttpResponseHeaders* out); + net::HttpResponseHeaders* out) { + return gin::Converter::FromV8(isolate, val, out); + } }; template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, - const network::ResourceRequest& val); + const network::ResourceRequest& val) { + return gin::ConvertToV8(isolate, val); + } }; template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, - electron::VerifyRequestParams val); + electron::VerifyRequestParams val) { + return gin::ConvertToV8(isolate, val); + } }; } // namespace mate -namespace electron { - -void FillRequestDetails(base::DictionaryValue* details, - const net::URLRequest* request); - -void GetUploadData(base::ListValue* upload_data_list, - const net::URLRequest* request); - -} // namespace electron - #endif // SHELL_COMMON_NATIVE_MATE_CONVERTERS_NET_CONVERTER_H_