electron/atom/browser/api/atom_api_web_request.cc

122 lines
4 KiB
C++
Raw Normal View History

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>
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 {
template<>
struct Converter<extensions::URLPattern> {
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
extensions::URLPattern* out) {
std::string pattern;
if (!ConvertFromV8(isolate, val, &pattern))
return false;
return out->Parse(pattern) == extensions::URLPattern::PARSE_SUCCESS;
}
};
} // namespace mate
2015-12-01 04:52:22 +00:00
namespace atom {
namespace api {
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
}
WebRequest::~WebRequest() {
}
template<AtomNetworkDelegate::SimpleEvent type>
void WebRequest::SetSimpleListener(mate::Arguments* args) {
SetListener<AtomNetworkDelegate::SimpleListener>(
&AtomNetworkDelegate::SetSimpleListenerInIO, type, args);
}
template<AtomNetworkDelegate::ResponseEvent type>
void WebRequest::SetResponseListener(mate::Arguments* args) {
SetListener<AtomNetworkDelegate::ResponseListener>(
&AtomNetworkDelegate::SetResponseListenerInIO, type, args);
}
template<typename Listener, typename Method, typename Event>
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;
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;
}
auto delegate = browser_context_->network_delegate();
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(method, base::Unretained(delegate), type,
patterns, 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,
2015-12-09 19:16:03 +00:00
v8::Local<v8::ObjectTemplate> prototype) {
2015-12-01 04:52:22 +00:00
mate::ObjectTemplateBuilder(isolate, prototype)
.SetMethod("onBeforeRequest",
&WebRequest::SetResponseListener<
2015-12-01 04:52:22 +00:00
AtomNetworkDelegate::kOnBeforeRequest>)
.SetMethod("onBeforeSendHeaders",
&WebRequest::SetResponseListener<
2015-12-01 04:52:22 +00:00
AtomNetworkDelegate::kOnBeforeSendHeaders>)
.SetMethod("onHeadersReceived",
&WebRequest::SetResponseListener<
2015-12-01 04:52:22 +00:00
AtomNetworkDelegate::kOnHeadersReceived>)
.SetMethod("onSendHeaders",
&WebRequest::SetSimpleListener<
AtomNetworkDelegate::kOnSendHeaders>)
2015-12-01 04:52:22 +00:00
.SetMethod("onBeforeRedirect",
&WebRequest::SetSimpleListener<
2015-12-01 04:52:22 +00:00
AtomNetworkDelegate::kOnBeforeRedirect>)
.SetMethod("onResponseStarted",
&WebRequest::SetSimpleListener<
2015-12-01 04:52:22 +00:00
AtomNetworkDelegate::kOnResponseStarted>)
.SetMethod("onCompleted",
&WebRequest::SetSimpleListener<
2015-12-01 04:52:22 +00:00
AtomNetworkDelegate::kOnCompleted>)
.SetMethod("onErrorOccurred",
&WebRequest::SetSimpleListener<
2015-12-01 04:52:22 +00:00
AtomNetworkDelegate::kOnErrorOccurred>);
}
} // namespace api
} // namespace atom