2015-12-01 04:52:22 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/api/atom_api_web_request.h"
|
|
|
|
|
2015-12-12 05:49:58 +00:00
|
|
|
#include <string>
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <utility>
|
2015-12-12 05:49:58 +00:00
|
|
|
|
2015-12-01 04:52:22 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
|
|
|
#include "atom/browser/net/atom_network_delegate.h"
|
|
|
|
#include "atom/common/native_mate_converters/callback.h"
|
|
|
|
#include "atom/common/native_mate_converters/net_converter.h"
|
|
|
|
#include "atom/common/native_mate_converters/value_converter.h"
|
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "native_mate/dictionary.h"
|
|
|
|
#include "native_mate/object_template_builder.h"
|
|
|
|
|
|
|
|
using content::BrowserThread;
|
|
|
|
|
2015-12-11 13:14:13 +00:00
|
|
|
namespace mate {
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
template <>
|
2017-01-24 08:29:35 +00:00
|
|
|
struct Converter<URLPattern> {
|
2018-04-18 01:55:30 +00:00
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
2017-01-24 08:29:35 +00:00
|
|
|
URLPattern* out) {
|
2015-12-11 13:14:13 +00:00
|
|
|
std::string pattern;
|
|
|
|
if (!ConvertFromV8(isolate, val, &pattern))
|
|
|
|
return false;
|
2017-02-01 06:06:40 +00:00
|
|
|
*out = URLPattern(URLPattern::SCHEME_ALL);
|
2017-01-24 08:29:35 +00:00
|
|
|
return out->Parse(pattern) == URLPattern::PARSE_SUCCESS;
|
2015-12-11 13:14:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mate
|
|
|
|
|
2015-12-01 04:52:22 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2018-02-26 14:24:00 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
template <typename Method, typename Event, typename Listener>
|
2018-02-26 14:24:00 +00:00
|
|
|
void CallNetworkDelegateMethod(
|
2018-10-04 18:08:56 +00:00
|
|
|
URLRequestContextGetter* url_request_context_getter,
|
2018-02-26 14:24:00 +00:00
|
|
|
Method method,
|
|
|
|
Event type,
|
|
|
|
URLPatterns patterns,
|
|
|
|
Listener listener) {
|
|
|
|
// Force creating network delegate.
|
2018-10-08 22:04:45 +00:00
|
|
|
url_request_context_getter->GetURLRequestContext();
|
2018-02-26 14:24:00 +00:00
|
|
|
// Then call the method.
|
2018-10-08 22:04:45 +00:00
|
|
|
auto* network_delegate = url_request_context_getter->network_delegate();
|
2018-02-26 14:24:00 +00:00
|
|
|
(network_delegate->*method)(type, std::move(patterns), std::move(listener));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
WebRequest::WebRequest(v8::Isolate* isolate,
|
|
|
|
AtomBrowserContext* browser_context)
|
2015-12-01 04:52:22 +00:00
|
|
|
: browser_context_(browser_context) {
|
2016-04-25 01:17:54 +00:00
|
|
|
Init(isolate);
|
2015-12-01 04:52:22 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
WebRequest::~WebRequest() {}
|
2015-12-01 04:52:22 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
template <AtomNetworkDelegate::SimpleEvent type>
|
2015-12-11 15:54:32 +00:00
|
|
|
void WebRequest::SetSimpleListener(mate::Arguments* args) {
|
|
|
|
SetListener<AtomNetworkDelegate::SimpleListener>(
|
|
|
|
&AtomNetworkDelegate::SetSimpleListenerInIO, type, args);
|
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
template <AtomNetworkDelegate::ResponseEvent type>
|
2015-12-11 15:54:32 +00:00
|
|
|
void WebRequest::SetResponseListener(mate::Arguments* args) {
|
|
|
|
SetListener<AtomNetworkDelegate::ResponseListener>(
|
|
|
|
&AtomNetworkDelegate::SetResponseListenerInIO, type, args);
|
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
template <typename Listener, typename Method, typename Event>
|
2015-12-11 15:54:32 +00:00
|
|
|
void WebRequest::SetListener(Method method, Event type, mate::Arguments* args) {
|
2015-12-11 13:14:13 +00:00
|
|
|
// { urls }.
|
|
|
|
URLPatterns patterns;
|
|
|
|
mate::Dictionary dict;
|
|
|
|
args->GetNext(&dict) && dict.Get("urls", &patterns);
|
2015-12-11 11:11:09 +00:00
|
|
|
|
2015-12-11 13:14:13 +00:00
|
|
|
// Function or null.
|
2015-12-11 11:11:09 +00:00
|
|
|
v8::Local<v8::Value> value;
|
2015-12-11 15:54:32 +00:00
|
|
|
Listener listener;
|
|
|
|
if (!args->GetNext(&listener) &&
|
2015-12-11 11:11:09 +00:00
|
|
|
!(args->GetNext(&value) && value->IsNull())) {
|
|
|
|
args->ThrowError("Must pass null or a Function");
|
2015-12-01 04:52:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
auto* url_request_context_getter = static_cast<URLRequestContextGetter*>(
|
|
|
|
browser_context_->GetRequestContext());
|
2017-12-15 17:32:33 +00:00
|
|
|
if (!url_request_context_getter)
|
|
|
|
return;
|
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::IO, FROM_HERE,
|
2018-04-20 10:55:05 +00:00
|
|
|
base::BindOnce(&CallNetworkDelegateMethod<Method, Event, Listener>,
|
|
|
|
base::RetainedRef(url_request_context_getter), method,
|
|
|
|
type, std::move(patterns), std::move(listener)));
|
2015-12-01 04:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
mate::Handle<WebRequest> WebRequest::Create(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
AtomBrowserContext* browser_context) {
|
2016-04-25 01:17:54 +00:00
|
|
|
return mate::CreateHandle(isolate, new WebRequest(isolate, browser_context));
|
2015-12-01 04:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void WebRequest::BuildPrototype(v8::Isolate* isolate,
|
2016-08-02 09:08:12 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> prototype) {
|
2016-08-02 10:28:12 +00:00
|
|
|
prototype->SetClassName(mate::StringToV8(isolate, "WebRequest"));
|
2016-08-02 09:08:12 +00:00
|
|
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
2018-04-18 01:55:30 +00:00
|
|
|
.SetMethod("onBeforeRequest", &WebRequest::SetResponseListener<
|
|
|
|
AtomNetworkDelegate::kOnBeforeRequest>)
|
2015-12-01 04:52:22 +00:00
|
|
|
.SetMethod("onBeforeSendHeaders",
|
2015-12-11 15:54:32 +00:00
|
|
|
&WebRequest::SetResponseListener<
|
2018-04-18 01:55:30 +00:00
|
|
|
AtomNetworkDelegate::kOnBeforeSendHeaders>)
|
2015-12-01 04:52:22 +00:00
|
|
|
.SetMethod("onHeadersReceived",
|
2015-12-11 15:54:32 +00:00
|
|
|
&WebRequest::SetResponseListener<
|
2018-04-18 01:55:30 +00:00
|
|
|
AtomNetworkDelegate::kOnHeadersReceived>)
|
|
|
|
.SetMethod(
|
|
|
|
"onSendHeaders",
|
|
|
|
&WebRequest::SetSimpleListener<AtomNetworkDelegate::kOnSendHeaders>)
|
2015-12-01 04:52:22 +00:00
|
|
|
.SetMethod("onBeforeRedirect",
|
2015-12-11 15:54:32 +00:00
|
|
|
&WebRequest::SetSimpleListener<
|
2018-04-18 01:55:30 +00:00
|
|
|
AtomNetworkDelegate::kOnBeforeRedirect>)
|
2015-12-01 04:52:22 +00:00
|
|
|
.SetMethod("onResponseStarted",
|
2015-12-11 15:54:32 +00:00
|
|
|
&WebRequest::SetSimpleListener<
|
2018-04-18 01:55:30 +00:00
|
|
|
AtomNetworkDelegate::kOnResponseStarted>)
|
|
|
|
.SetMethod(
|
|
|
|
"onCompleted",
|
|
|
|
&WebRequest::SetSimpleListener<AtomNetworkDelegate::kOnCompleted>)
|
|
|
|
.SetMethod("onErrorOccurred", &WebRequest::SetSimpleListener<
|
|
|
|
AtomNetworkDelegate::kOnErrorOccurred>);
|
2015-12-01 04:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|