move net converter to gin (#19734)

This commit is contained in:
Micha Hanselmann 2019-08-13 22:15:34 -07:00 committed by Cheng Zhao
parent 5d892a557f
commit 4c54cfc692
8 changed files with 169 additions and 102 deletions

View file

@ -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",

View file

@ -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"

View file

@ -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 <memory>
#include <string>
@ -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<v8::Value> Converter<net::AuthChallengeInfo>::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<uint32_t>(val.challenger.port()));
dict.Set("realm", val.realm);
return mate::ConvertToV8(isolate, dict);
return gin::ConvertToV8(isolate, dict);
}
// static
v8::Local<v8::Value> Converter<scoped_refptr<net::X509Certificate>>::ToV8(
v8::Isolate* isolate,
const scoped_refptr<net::X509Certificate>& 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<v8::Value> Converter<scoped_refptr<net::X509Certificate>>::ToV8(
dict.Set("issuerCert", issuer_cert);
}
return dict.GetHandle();
return ConvertToV8(isolate, dict);
}
bool Converter<scoped_refptr<net::X509Certificate>>::FromV8(
v8::Isolate* isolate,
v8::Local<v8::Value> val,
scoped_refptr<net::X509Certificate>* out) {
mate::Dictionary dict;
gin::Dictionary dict(nullptr);
if (!ConvertFromV8(isolate, val, &dict))
return false;
@ -137,7 +137,7 @@ bool Converter<scoped_refptr<net::X509Certificate>>::FromV8(
v8::Local<v8::Value> Converter<net::CertPrincipal>::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<v8::Value> Converter<net::CertPrincipal>::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<net::HttpResponseHeaders*>::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<net::HttpResponseHeaders*>::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<net::HttpResponseHeaders*>::FromV8(
v8::Local<v8::Value> Converter<network::ResourceRequest>::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<v8::Value> Converter<network::ResourceRequest>::ToV8(
v8::Local<v8::Array> 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<v8::Value> Converter<network::ResourceRequest>::ToV8(
NOTREACHED() << "Found unsupported data element";
}
arr->Set(isolate->GetCurrentContext(), static_cast<uint32_t>(i),
upload_data.GetHandle())
ConvertToV8(isolate, upload_data))
.Check();
}
dict.Set("uploadData", arr);
}
return dict.GetHandle();
return ConvertToV8(isolate, dict);
}
// static
v8::Local<v8::Value> Converter<electron::VerifyRequestParams>::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 {

View file

@ -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<net::AuthChallengeInfo> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const net::AuthChallengeInfo& val);
};
template <>
struct Converter<scoped_refptr<net::X509Certificate>> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
const scoped_refptr<net::X509Certificate>& val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
scoped_refptr<net::X509Certificate>* out);
};
template <>
struct Converter<net::CertPrincipal> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const net::CertPrincipal& val);
};
template <>
struct Converter<net::HttpResponseHeaders*> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
net::HttpResponseHeaders* headers);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
net::HttpResponseHeaders* out);
};
template <>
struct Converter<network::ResourceRequest> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const network::ResourceRequest& val);
};
template <>
struct Converter<electron::VerifyRequestParams> {
static v8::Local<v8::Value> 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_

View file

@ -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<scoped_refptr<net::X509Certificate>> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
const scoped_refptr<net::X509Certificate>& val) {
return mate::ConvertToV8(isolate, val);
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
scoped_refptr<net::X509Certificate>* out) {
return mate::ConvertFromV8(isolate, val, out);
}
};
} // namespace gin
#endif // SHELL_COMMON_GIN_CONVERTERS_NET_CONVERTER_GIN_ADAPTER_H_

View file

@ -11,6 +11,22 @@
namespace gin {
template <>
struct Converter<v8::Local<v8::Array>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::Array> val) {
return val;
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::Array>* out) {
if (!val->IsArray())
return false;
*out = v8::Local<v8::Array>::Cast(val);
return true;
}
};
template <typename T>
struct Converter<std::set<T>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,

View file

@ -12,6 +12,19 @@
namespace gin {
template <>
struct Converter<base::DictionaryValue> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
base::DictionaryValue* out) {
return mate::ConvertFromV8(isolate, val, out);
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::DictionaryValue& val) {
return mate::ConvertToV8(isolate, val);
}
};
template <>
struct Converter<base::Value> {
static bool FromV8(v8::Isolate* isolate,

View file

@ -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<net::AuthChallengeInfo> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const net::AuthChallengeInfo& val);
const net::AuthChallengeInfo& val) {
return gin::ConvertToV8(isolate, val);
}
};
template <>
struct Converter<scoped_refptr<net::X509Certificate>> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
const scoped_refptr<net::X509Certificate>& val);
const scoped_refptr<net::X509Certificate>& val) {
return gin::ConvertToV8(isolate, val);
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
scoped_refptr<net::X509Certificate>* out);
scoped_refptr<net::X509Certificate>* out) {
return gin::ConvertFromV8(isolate, val, out);
}
};
template <>
struct Converter<net::CertPrincipal> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const net::CertPrincipal& val);
const net::CertPrincipal& val) {
return gin::ConvertToV8(isolate, val);
}
};
template <>
struct Converter<net::HttpResponseHeaders*> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
net::HttpResponseHeaders* headers);
net::HttpResponseHeaders* headers) {
return gin::ConvertToV8(isolate, headers);
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
net::HttpResponseHeaders* out);
net::HttpResponseHeaders* out) {
return gin::Converter<net::HttpResponseHeaders*>::FromV8(isolate, val, out);
}
};
template <>
struct Converter<network::ResourceRequest> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const network::ResourceRequest& val);
const network::ResourceRequest& val) {
return gin::ConvertToV8(isolate, val);
}
};
template <>
struct Converter<electron::VerifyRequestParams> {
static v8::Local<v8::Value> 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_