feat: migrate webRequest module to NetworkService (Part 4) (#19679)

* chore: use gin in WebRequest

* Add stubs for APIs
This commit is contained in:
Cheng Zhao 2019-08-11 13:24:51 +09:00 committed by GitHub
parent bc0a2d1b28
commit 2dffc9f6eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 255 additions and 23 deletions

View file

@ -465,6 +465,7 @@ filenames = {
"shell/common/crash_reporter/linux/crash_dump_handler.h",
"shell/common/crash_reporter/win/crash_service_main.cc",
"shell/common/crash_reporter/win/crash_service_main.h",
"shell/common/gin_converters/callback_converter_gin_adapter.h",
"shell/common/gin_converters/file_dialog_converter_gin_adapter.h",
"shell/common/gin_converters/file_path_converter.h",
"shell/common/gin_converters/gurl_converter.h",
@ -473,7 +474,9 @@ filenames = {
"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/std_converter.h",
"shell/common/gin_converters/string16_converter.h",
"shell/common/gin_converters/value_converter_gin_adapter.h",
"shell/common/gin_util.h",
"shell/common/heap_snapshot.cc",
"shell/common/heap_snapshot.h",

View file

@ -575,9 +575,8 @@ v8::Local<v8::Value> Session::Protocol(v8::Isolate* isolate) {
v8::Local<v8::Value> Session::WebRequest(v8::Isolate* isolate) {
if (web_request_.IsEmpty()) {
v8::Local<v8::Value> handle;
handle = WebRequestNS::Create(isolate, browser_context()).ToV8();
web_request_.Reset(isolate, handle);
auto handle = WebRequestNS::Create(isolate, browser_context());
web_request_.Reset(isolate, handle.ToV8());
}
return v8::Local<v8::Value>::New(isolate, web_request_);
}

View file

@ -4,33 +4,105 @@
#include "shell/browser/api/atom_api_web_request_ns.h"
#include <set>
#include <string>
#include "extensions/common/url_pattern.h"
#include "gin/converter.h"
#include "gin/dictionary.h"
#include "gin/object_template_builder.h"
#include "shell/browser/atom_browser_context.h"
#include "shell/common/gin_converters/callback_converter_gin_adapter.h"
#include "shell/common/gin_converters/std_converter.h"
#include "shell/common/gin_converters/value_converter_gin_adapter.h"
namespace gin {
template <>
struct Converter<URLPattern> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
URLPattern* out) {
std::string pattern;
if (!ConvertFromV8(isolate, val, &pattern))
return false;
*out = URLPattern(URLPattern::SCHEME_ALL);
return out->Parse(pattern) == URLPattern::ParseResult::kSuccess;
}
};
} // namespace gin
namespace electron {
namespace api {
gin::WrapperInfo WebRequestNS::kWrapperInfo = {gin::kEmbedderNativeGin};
WebRequestNS::WebRequestNS(v8::Isolate* isolate,
AtomBrowserContext* browser_context) {
Init(isolate);
AttachAsUserData(browser_context);
}
AtomBrowserContext* browser_context) {}
WebRequestNS::~WebRequestNS() = default;
// static
mate::Handle<WebRequestNS> WebRequestNS::Create(
v8::Isolate* isolate,
AtomBrowserContext* browser_context) {
return mate::CreateHandle(isolate,
new WebRequestNS(isolate, browser_context));
gin::ObjectTemplateBuilder WebRequestNS::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return gin::Wrappable<WebRequestNS>::GetObjectTemplateBuilder(isolate)
.SetMethod("onBeforeRequest",
&WebRequestNS::SetResponseListener<kOnBeforeRequest>)
.SetMethod("onBeforeSendHeaders",
&WebRequestNS::SetResponseListener<kOnBeforeSendHeaders>)
.SetMethod("onHeadersReceived",
&WebRequestNS::SetResponseListener<kOnHeadersReceived>)
.SetMethod("onSendHeaders",
&WebRequestNS::SetSimpleListener<kOnSendHeaders>)
.SetMethod("onBeforeRedirect",
&WebRequestNS::SetSimpleListener<kOnBeforeRedirect>)
.SetMethod("onResponseStarted",
&WebRequestNS::SetSimpleListener<kOnResponseStarted>)
.SetMethod("onErrorOccurred",
&WebRequestNS::SetSimpleListener<kOnErrorOccurred>)
.SetMethod("onCompleted", &WebRequestNS::SetSimpleListener<kOnCompleted>);
}
const char* WebRequestNS::GetTypeName() {
return "WebRequest";
}
template <WebRequestNS::SimpleEvent event>
void WebRequestNS::SetSimpleListener(gin::Arguments* args) {
SetListener<SimpleListener, SimpleEvent>(event, args);
}
template <WebRequestNS::ResponseEvent event>
void WebRequestNS::SetResponseListener(gin::Arguments* args) {
SetListener<ResponseListener, ResponseEvent>(event, args);
}
template <typename Listener, typename Event>
void WebRequestNS::SetListener(Event event, gin::Arguments* args) {
// { urls }.
std::set<URLPattern> patterns;
gin::Dictionary dict(args->isolate());
args->GetNext(&dict) && dict.Get("urls", &patterns);
// Function or null.
v8::Local<v8::Value> value;
Listener listener;
if (!args->GetNext(&listener) &&
!(args->GetNext(&value) && value->IsNull())) {
args->ThrowTypeError("Must pass null or a Function");
return;
}
// TODO(zcbenz): Actually set the listeners.
args->ThrowTypeError("This API is not implemented yet");
}
// static
void WebRequestNS::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "WebRequest"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate());
gin::Handle<WebRequestNS> WebRequestNS::Create(
v8::Isolate* isolate,
AtomBrowserContext* browser_context) {
return gin::CreateHandle(isolate, new WebRequestNS(isolate, browser_context));
}
} // namespace api

View file

@ -5,9 +5,12 @@
#ifndef SHELL_BROWSER_API_ATOM_API_WEB_REQUEST_NS_H_
#define SHELL_BROWSER_API_ATOM_API_WEB_REQUEST_NS_H_
#include "base/values.h"
#include "gin/arguments.h"
#include "gin/handle.h"
#include "gin/wrappable.h"
#include "native_mate/dictionary.h"
#include "native_mate/handle.h"
#include "shell/browser/api/trackable_object.h"
namespace electron {
@ -15,17 +18,46 @@ class AtomBrowserContext;
namespace api {
class WebRequestNS : public mate::TrackableObject<WebRequestNS> {
class WebRequestNS : public gin::Wrappable<WebRequestNS> {
public:
static mate::Handle<WebRequestNS> Create(v8::Isolate* isolate,
AtomBrowserContext* browser_context);
static gin::WrapperInfo kWrapperInfo;
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
static gin::Handle<WebRequestNS> Create(v8::Isolate* isolate,
AtomBrowserContext* browser_context);
// gin::Wrappable:
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
const char* GetTypeName() override;
private:
WebRequestNS(v8::Isolate* isolate, AtomBrowserContext* browser_context);
~WebRequestNS() override;
enum SimpleEvent {
kOnSendHeaders,
kOnBeforeRedirect,
kOnResponseStarted,
kOnCompleted,
kOnErrorOccurred,
};
enum ResponseEvent {
kOnBeforeRequest,
kOnBeforeSendHeaders,
kOnHeadersReceived,
};
using SimpleListener = base::RepeatingCallback<void(const base::Value&)>;
using ResponseCallback = base::OnceCallback<void(const base::Value&)>;
using ResponseListener =
base::RepeatingCallback<void(const base::Value&, ResponseCallback)>;
template <SimpleEvent event>
void SetSimpleListener(gin::Arguments* args);
template <ResponseEvent event>
void SetResponseListener(gin::Arguments* args);
template <typename Listener, typename Event>
void SetListener(Event event, gin::Arguments* args);
};
} // namespace api

View file

@ -0,0 +1,44 @@
// 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_CALLBACK_CONVERTER_GIN_ADAPTER_H_
#define SHELL_COMMON_GIN_CONVERTERS_CALLBACK_CONVERTER_GIN_ADAPTER_H_
#include "gin/converter.h"
#include "shell/common/native_mate_converters/once_callback.h"
// TODO(zcbenz): Move the implementations from native_mate_converters to here.
namespace gin {
template <typename Sig>
struct Converter<base::RepeatingCallback<Sig>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::RepeatingCallback<Sig>& in) {
return mate::ConvertToV8(isolate, in);
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
base::RepeatingCallback<Sig>* out) {
return mate::ConvertFromV8(isolate, val, out);
}
};
template <typename Sig>
struct Converter<base::OnceCallback<Sig>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
base::OnceCallback<Sig> in) {
return mate::ConvertToV8(isolate, in);
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
base::OnceCallback<Sig>* out) {
return mate::ConvertFromV8(isolate, val, out);
}
};
} // namespace gin
#endif // SHELL_COMMON_GIN_CONVERTERS_CALLBACK_CONVERTER_GIN_ADAPTER_H_

View file

@ -0,0 +1,52 @@
// 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_STD_CONVERTER_H_
#define SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_
#include <set>
#include "gin/converter.h"
namespace gin {
template <typename T>
struct Converter<std::set<T>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const std::set<T>& val) {
v8::Local<v8::Array> result(
v8::Array::New(isolate, static_cast<int>(val.size())));
auto context = isolate->GetCurrentContext();
typename std::set<T>::const_iterator it;
int i;
for (i = 0, it = val.begin(); it != val.end(); ++it, ++i)
result->Set(context, i, Converter<T>::ToV8(isolate, *it)).Check();
return result;
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
std::set<T>* out) {
if (!val->IsArray())
return false;
auto context = isolate->GetCurrentContext();
std::set<T> result;
v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val));
uint32_t length = array->Length();
for (uint32_t i = 0; i < length; ++i) {
T item;
if (!Converter<T>::FromV8(isolate,
array->Get(context, i).ToLocalChecked(), &item))
return false;
result.insert(item);
}
out->swap(result);
return true;
}
};
} // namespace gin
#endif // SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_

View file

@ -0,0 +1,30 @@
// 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_VALUE_CONVERTER_GIN_ADAPTER_H_
#define SHELL_COMMON_GIN_CONVERTERS_VALUE_CONVERTER_GIN_ADAPTER_H_
#include "gin/converter.h"
#include "shell/common/native_mate_converters/value_converter.h"
// TODO(zcbenz): Move the implementations from native_mate_converters to here.
namespace gin {
template <>
struct Converter<base::Value> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
base::Value* out) {
return mate::ConvertFromV8(isolate, val, out);
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::Value& in) {
return mate::ConvertToV8(isolate, in);
}
};
} // namespace gin
#endif // SHELL_COMMON_GIN_CONVERTERS_VALUE_CONVERTER_GIN_ADAPTER_H_