chore: remove native_mate (Part 7) (#20561)

* refactor: use gin converters in api::Protocol

* refactor: convert JS constructor impl to gin

* refactor: use InitWithArgs helper

* fix: gin_helper::Dictionary should behave the same with mate

* fix cpplint warnings

* refactor: no more need to patch gin/dictionary.h
This commit is contained in:
Cheng Zhao 2019-10-15 10:15:23 +09:00 committed by GitHub
parent 6c6bff81ac
commit 1ecfcc8c70
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
43 changed files with 274 additions and 368 deletions

View file

@ -23,16 +23,16 @@
#include "shell/browser/net/node_stream_loader.h"
#include "shell/browser/net/url_pipe_loader.h"
#include "shell/common/atom_constants.h"
#include "shell/common/native_mate_converters/file_path_converter.h"
#include "shell/common/native_mate_converters/gurl_converter.h"
#include "shell/common/native_mate_converters/net_converter.h"
#include "shell/common/native_mate_converters/value_converter.h"
#include "shell/common/gin_converters/file_path_converter.h"
#include "shell/common/gin_converters/gurl_converter.h"
#include "shell/common/gin_converters/net_converter.h"
#include "shell/common/gin_converters/value_converter_gin_adapter.h"
#include "shell/common/node_includes.h"
using content::BrowserThread;
namespace mate {
namespace gin {
template <>
struct Converter<electron::ProtocolType> {
@ -58,7 +58,7 @@ struct Converter<electron::ProtocolType> {
}
};
} // namespace mate
} // namespace gin
namespace electron {
@ -77,17 +77,18 @@ bool ResponseMustBeObject(ProtocolType type) {
}
// Helper to convert value to Dictionary.
mate::Dictionary ToDict(v8::Isolate* isolate, v8::Local<v8::Value> value) {
gin::Dictionary ToDict(v8::Isolate* isolate, v8::Local<v8::Value> value) {
if (!value->IsFunction() && value->IsObject())
return mate::Dictionary(
return gin::Dictionary(
isolate,
value->ToObject(isolate->GetCurrentContext()).ToLocalChecked());
else
return mate::Dictionary();
return gin::Dictionary(isolate);
}
// Parse headers from response object.
network::ResourceResponseHead ToResponseHead(const mate::Dictionary& dict) {
network::ResourceResponseHead ToResponseHead(
const gin_helper::Dictionary& dict) {
network::ResourceResponseHead head;
head.mime_type = "text/html";
head.charset = "utf-8";
@ -197,7 +198,7 @@ void AtomURLLoaderFactory::StartLoading(
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
network::mojom::URLLoaderFactory* proxy_factory,
ProtocolType type,
mate::Arguments* args) {
gin::Arguments* args) {
// Send network error when there is no argument passed.
//
// Note that we should not throw JS error in the callback no matter what is
@ -210,7 +211,7 @@ void AtomURLLoaderFactory::StartLoading(
}
// Parse {error} object.
mate::Dictionary dict = ToDict(args->isolate(), response);
gin_helper::Dictionary dict = ToDict(args->isolate(), response);
if (!dict.IsEmpty()) {
int error_code;
if (dict.Get("error", &error_code)) {
@ -251,7 +252,7 @@ void AtomURLLoaderFactory::StartLoading(
} else {
StartLoadingHttp(std::move(loader), new_request, std::move(client),
traffic_annotation,
mate::Dictionary::CreateEmpty(args->isolate()));
gin::Dictionary::CreateEmpty(args->isolate()));
}
return;
}
@ -285,7 +286,7 @@ void AtomURLLoaderFactory::StartLoading(
break;
case ProtocolType::kFree:
ProtocolType type;
if (!mate::ConvertFromV8(args->isolate(), response, &type)) {
if (!gin::ConvertFromV8(args->isolate(), response, &type)) {
client->OnComplete(network::URLLoaderCompletionStatus(net::ERR_FAILED));
return;
}
@ -300,7 +301,7 @@ void AtomURLLoaderFactory::StartLoading(
void AtomURLLoaderFactory::StartLoadingBuffer(
network::mojom::URLLoaderClientPtr client,
network::ResourceResponseHead head,
const mate::Dictionary& dict) {
const gin_helper::Dictionary& dict) {
v8::Local<v8::Value> buffer = dict.GetHandle();
dict.Get("data", &buffer);
if (!node::Buffer::HasInstance(buffer)) {
@ -317,7 +318,7 @@ void AtomURLLoaderFactory::StartLoadingBuffer(
void AtomURLLoaderFactory::StartLoadingString(
network::mojom::URLLoaderClientPtr client,
network::ResourceResponseHead head,
const mate::Dictionary& dict,
const gin_helper::Dictionary& dict,
v8::Isolate* isolate,
v8::Local<v8::Value> response) {
std::string contents;
@ -339,11 +340,11 @@ void AtomURLLoaderFactory::StartLoadingFile(
network::ResourceRequest request,
network::mojom::URLLoaderClientPtr client,
network::ResourceResponseHead head,
const mate::Dictionary& dict,
const gin_helper::Dictionary& dict,
v8::Isolate* isolate,
v8::Local<v8::Value> response) {
base::FilePath path;
if (mate::ConvertFromV8(isolate, response, &path)) {
if (gin::ConvertFromV8(isolate, response, &path)) {
request.url = net::FilePathToFileURL(path);
} else if (!dict.IsEmpty()) {
dict.Get("referrer", &request.referrer);
@ -366,7 +367,7 @@ void AtomURLLoaderFactory::StartLoadingHttp(
const network::ResourceRequest& original_request,
network::mojom::URLLoaderClientPtr client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
const mate::Dictionary& dict) {
const gin_helper::Dictionary& dict) {
auto request = std::make_unique<network::ResourceRequest>();
request->headers = original_request.headers;
request->cors_exempt_headers = original_request.cors_exempt_headers;
@ -407,7 +408,7 @@ void AtomURLLoaderFactory::StartLoadingStream(
network::mojom::URLLoaderRequest loader,
network::mojom::URLLoaderClientPtr client,
network::ResourceResponseHead head,
const mate::Dictionary& dict) {
const gin_helper::Dictionary& dict) {
v8::Local<v8::Value> stream;
if (!dict.Get("data", &stream)) {
// Assume the opts is already a stream.
@ -435,7 +436,7 @@ void AtomURLLoaderFactory::StartLoadingStream(
return;
}
mate::Dictionary data = ToDict(dict.isolate(), stream);
gin_helper::Dictionary data = ToDict(dict.isolate(), stream);
v8::Local<v8::Value> method;
if (!data.Get("on", &method) || !method->IsFunction() ||
!data.Get("removeListener", &method) || !method->IsFunction()) {

View file

@ -10,10 +10,10 @@
#include <utility>
#include "mojo/public/cpp/bindings/binding_set.h"
#include "native_mate/dictionary.h"
#include "net/url_request/url_request_job_factory.h"
#include "services/network/public/cpp/resource_response.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
#include "shell/common/gin_helper/dictionary.h"
namespace electron {
@ -27,7 +27,7 @@ enum class ProtocolType {
kFree, // special type for returning arbitrary type of response.
};
using StartLoadingCallback = base::OnceCallback<void(mate::Arguments*)>;
using StartLoadingCallback = base::OnceCallback<void(gin::Arguments*)>;
using ProtocolHandler =
base::Callback<void(const network::ResourceRequest&, StartLoadingCallback)>;
@ -62,22 +62,22 @@ class AtomURLLoaderFactory : public network::mojom::URLLoaderFactory {
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
network::mojom::URLLoaderFactory* proxy_factory,
ProtocolType type,
mate::Arguments* args);
gin::Arguments* args);
private:
static void StartLoadingBuffer(network::mojom::URLLoaderClientPtr client,
network::ResourceResponseHead head,
const mate::Dictionary& dict);
const gin_helper::Dictionary& dict);
static void StartLoadingString(network::mojom::URLLoaderClientPtr client,
network::ResourceResponseHead head,
const mate::Dictionary& dict,
const gin_helper::Dictionary& dict,
v8::Isolate* isolate,
v8::Local<v8::Value> response);
static void StartLoadingFile(network::mojom::URLLoaderRequest loader,
network::ResourceRequest request,
network::mojom::URLLoaderClientPtr client,
network::ResourceResponseHead head,
const mate::Dictionary& dict,
const gin_helper::Dictionary& dict,
v8::Isolate* isolate,
v8::Local<v8::Value> response);
static void StartLoadingHttp(
@ -85,11 +85,11 @@ class AtomURLLoaderFactory : public network::mojom::URLLoaderFactory {
const network::ResourceRequest& original_request,
network::mojom::URLLoaderClientPtr client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
const mate::Dictionary& dict);
const gin_helper::Dictionary& dict);
static void StartLoadingStream(network::mojom::URLLoaderRequest loader,
network::mojom::URLLoaderClientPtr client,
network::ResourceResponseHead head,
const mate::Dictionary& dict);
const gin_helper::Dictionary& dict);
// Helper to send string as response.
static void SendContents(network::mojom::URLLoaderClientPtr client,