session: add webrequest api
This commit is contained in:
parent
d2e63dfc64
commit
13b5cab738
13 changed files with 563 additions and 2 deletions
|
@ -10,6 +10,7 @@
|
|||
#include "atom/browser/api/atom_api_cookies.h"
|
||||
#include "atom/browser/api/atom_api_download_item.h"
|
||||
#include "atom/browser/api/atom_api_web_contents.h"
|
||||
#include "atom/browser/api/atom_api_web_request.h"
|
||||
#include "atom/browser/api/save_page_handler.h"
|
||||
#include "atom/browser/atom_browser_context.h"
|
||||
#include "atom/browser/atom_browser_main_parts.h"
|
||||
|
@ -368,6 +369,14 @@ v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
|
|||
return v8::Local<v8::Value>::New(isolate, cookies_);
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> Session::WebRequest(v8::Isolate* isolate) {
|
||||
if (web_request_.IsEmpty()) {
|
||||
auto handle = atom::api::WebRequest::Create(isolate, browser_context());
|
||||
web_request_.Reset(isolate, handle.ToV8());
|
||||
}
|
||||
return v8::Local<v8::Value>::New(isolate, web_request_);
|
||||
}
|
||||
|
||||
// static
|
||||
mate::Handle<Session> Session::CreateFrom(
|
||||
v8::Isolate* isolate, AtomBrowserContext* browser_context) {
|
||||
|
@ -401,7 +410,8 @@ void Session::BuildPrototype(v8::Isolate* isolate,
|
|||
.SetMethod("enableNetworkEmulation", &Session::EnableNetworkEmulation)
|
||||
.SetMethod("disableNetworkEmulation", &Session::DisableNetworkEmulation)
|
||||
.SetMethod("setCertificateVerifyProc", &Session::SetCertVerifyProc)
|
||||
.SetProperty("cookies", &Session::Cookies);
|
||||
.SetProperty("cookies", &Session::Cookies)
|
||||
.SetProperty("webRequest", &Session::WebRequest);
|
||||
}
|
||||
|
||||
void ClearWrapSession() {
|
||||
|
|
|
@ -70,9 +70,11 @@ class Session: public mate::TrackableObject<Session>,
|
|||
void DisableNetworkEmulation();
|
||||
void SetCertVerifyProc(v8::Local<v8::Value> proc, mate::Arguments* args);
|
||||
v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
|
||||
v8::Local<v8::Value> WebRequest(v8::Isolate* isolate);
|
||||
|
||||
// Cached object for cookies API.
|
||||
// Cached object.
|
||||
v8::Global<v8::Value> cookies_;
|
||||
v8::Global<v8::Value> web_request_;
|
||||
|
||||
scoped_refptr<AtomBrowserContext> browser_context_;
|
||||
|
||||
|
|
116
atom/browser/api/atom_api_web_request.cc
Normal file
116
atom/browser/api/atom_api_web_request.cc
Normal file
|
@ -0,0 +1,116 @@
|
|||
// 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"
|
||||
|
||||
#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;
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
WebRequest::WebRequest(AtomBrowserContext* browser_context)
|
||||
: browser_context_(browser_context) {
|
||||
}
|
||||
|
||||
WebRequest::~WebRequest() {
|
||||
}
|
||||
|
||||
template<AtomNetworkDelegate::EventTypes type>
|
||||
void WebRequest::SetListener(mate::Arguments* args) {
|
||||
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
||||
|
||||
base::DictionaryValue* filter = new base::DictionaryValue();
|
||||
args->GetNext(filter);
|
||||
AtomNetworkDelegate::Listener callback;
|
||||
if (!args->GetNext(&callback)) {
|
||||
args->ThrowError("Must pass null or a function");
|
||||
return;
|
||||
}
|
||||
|
||||
auto delegate = browser_context_->network_delegate();
|
||||
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
||||
base::Bind(&AtomNetworkDelegate::SetListenerInIO,
|
||||
base::Unretained(delegate),
|
||||
type, filter, callback));
|
||||
}
|
||||
|
||||
mate::ObjectTemplateBuilder WebRequest::GetObjectTemplateBuilder(
|
||||
v8::Isolate* isolate) {
|
||||
return mate::ObjectTemplateBuilder(isolate)
|
||||
.SetMethod("onBeforeRequest",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnBeforeRequest>)
|
||||
.SetMethod("onBeforeSendHeaders",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnBeforeSendHeaders>)
|
||||
.SetMethod("onSendHeaders",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnSendHeaders>)
|
||||
.SetMethod("onHeadersReceived",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnHeadersReceived>)
|
||||
.SetMethod("onBeforeRedirect",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnBeforeRedirect>)
|
||||
.SetMethod("onResponseStarted",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnResponseStarted>)
|
||||
.SetMethod("onCompleted",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnCompleted>)
|
||||
.SetMethod("onErrorOccurred",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnErrorOccurred>);
|
||||
}
|
||||
|
||||
// static
|
||||
mate::Handle<WebRequest> WebRequest::Create(
|
||||
v8::Isolate* isolate,
|
||||
AtomBrowserContext* browser_context) {
|
||||
return mate::CreateHandle(isolate, new WebRequest(browser_context));
|
||||
}
|
||||
|
||||
// static
|
||||
void WebRequest::BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Local<v8::ObjectTemplate> prototype) {
|
||||
mate::ObjectTemplateBuilder(isolate, prototype)
|
||||
.SetMethod("onBeforeRequest",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnBeforeRequest>)
|
||||
.SetMethod("onBeforeSendHeaders",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnBeforeSendHeaders>)
|
||||
.SetMethod("onSendHeaders",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnSendHeaders>)
|
||||
.SetMethod("onHeadersReceived",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnHeadersReceived>)
|
||||
.SetMethod("onBeforeRedirect",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnBeforeRedirect>)
|
||||
.SetMethod("onResponseStarted",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnResponseStarted>)
|
||||
.SetMethod("onCompleted",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnCompleted>)
|
||||
.SetMethod("onErrorOccurred",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnErrorOccurred>);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
51
atom/browser/api/atom_api_web_request.h
Normal file
51
atom/browser/api/atom_api_web_request.h
Normal file
|
@ -0,0 +1,51 @@
|
|||
// Copyright (c) 2015 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ATOM_BROWSER_API_ATOM_API_WEB_REQUEST_H_
|
||||
#define ATOM_BROWSER_API_ATOM_API_WEB_REQUEST_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "atom/browser/net/atom_network_delegate.h"
|
||||
#include "base/callback.h"
|
||||
#include "native_mate/arguments.h"
|
||||
#include "native_mate/wrappable.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
class AtomBrowserContext;
|
||||
|
||||
namespace api {
|
||||
|
||||
class WebRequest : public mate::TrackableObject<Cookies> {
|
||||
public:
|
||||
static mate::Handle<WebRequest> Create(v8::Isolate* isolate,
|
||||
AtomBrowserContext* browser_context);
|
||||
|
||||
// mate::TrackableObject:
|
||||
static void BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Local<v8::ObjectTemplate> prototype);
|
||||
|
||||
protected:
|
||||
explicit WebRequest(AtomBrowserContext* browser_context);
|
||||
~WebRequest();
|
||||
|
||||
template<AtomNetworkDelegate::EventTypes Event>
|
||||
void SetListener(mate::Arguments* args);
|
||||
|
||||
// mate::Wrappable:
|
||||
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
||||
v8::Isolate* isolate) override;
|
||||
|
||||
private:
|
||||
scoped_refptr<AtomBrowserContext> browser_context_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(WebRequest);
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_BROWSER_API_ATOM_API_WEB_REQUEST_H_
|
|
@ -8,6 +8,7 @@
|
|||
#include "atom/browser/atom_download_manager_delegate.h"
|
||||
#include "atom/browser/browser.h"
|
||||
#include "atom/browser/net/atom_cert_verifier.h"
|
||||
#include "atom/browser/net/atom_network_delegate.h"
|
||||
#include "atom/browser/net/atom_ssl_config_service.h"
|
||||
#include "atom/browser/net/atom_url_request_job_factory.h"
|
||||
#include "atom/browser/net/asar/asar_protocol_handler.h"
|
||||
|
@ -63,12 +64,17 @@ AtomBrowserContext::AtomBrowserContext(const std::string& partition,
|
|||
: brightray::BrowserContext(partition, in_memory),
|
||||
cert_verifier_(nullptr),
|
||||
job_factory_(new AtomURLRequestJobFactory),
|
||||
network_delegate_(new AtomNetworkDelegate),
|
||||
allow_ntlm_everywhere_(false) {
|
||||
}
|
||||
|
||||
AtomBrowserContext::~AtomBrowserContext() {
|
||||
}
|
||||
|
||||
net::NetworkDelegate* AtomBrowserContext::CreateNetworkDelegate() {
|
||||
return network_delegate_;
|
||||
}
|
||||
|
||||
std::string AtomBrowserContext::GetUserAgent() {
|
||||
Browser* browser = Browser::Get();
|
||||
std::string name = RemoveWhitespace(browser->GetName());
|
||||
|
|
|
@ -13,6 +13,7 @@ namespace atom {
|
|||
|
||||
class AtomDownloadManagerDelegate;
|
||||
class AtomCertVerifier;
|
||||
class AtomNetworkDelegate;
|
||||
class AtomURLRequestJobFactory;
|
||||
class WebViewManager;
|
||||
|
||||
|
@ -22,6 +23,7 @@ class AtomBrowserContext : public brightray::BrowserContext {
|
|||
~AtomBrowserContext() override;
|
||||
|
||||
// brightray::URLRequestContextGetter::Delegate:
|
||||
net::NetworkDelegate* CreateNetworkDelegate() override;
|
||||
std::string GetUserAgent() override;
|
||||
scoped_ptr<net::URLRequestJobFactory> CreateURLRequestJobFactory(
|
||||
content::ProtocolHandlerMap* handlers,
|
||||
|
@ -45,6 +47,8 @@ class AtomBrowserContext : public brightray::BrowserContext {
|
|||
|
||||
AtomURLRequestJobFactory* job_factory() const { return job_factory_; }
|
||||
|
||||
AtomNetworkDelegate* network_delegate() const { return network_delegate_; }
|
||||
|
||||
private:
|
||||
scoped_ptr<AtomDownloadManagerDelegate> download_manager_delegate_;
|
||||
scoped_ptr<WebViewManager> guest_manager_;
|
||||
|
@ -52,6 +56,7 @@ class AtomBrowserContext : public brightray::BrowserContext {
|
|||
// Managed by brightray::BrowserContext.
|
||||
AtomCertVerifier* cert_verifier_;
|
||||
AtomURLRequestJobFactory* job_factory_;
|
||||
AtomNetworkDelegate* network_delegate_;
|
||||
|
||||
bool allow_ntlm_everywhere_;
|
||||
|
||||
|
|
220
atom/browser/net/atom_network_delegate.cc
Normal file
220
atom/browser/net/atom_network_delegate.cc
Normal file
|
@ -0,0 +1,220 @@
|
|||
// 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/net/atom_network_delegate.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "atom/common/native_mate_converters/net_converter.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "content/public/browser/resource_request_info.h"
|
||||
#include "net/base/net_errors.h"
|
||||
#include "net/url_request/url_request.h"
|
||||
|
||||
using content::BrowserThread;
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace {
|
||||
|
||||
base::DictionaryValue* ExtractRequestInfo(net::URLRequest* request) {
|
||||
base::DictionaryValue* dict = new base::DictionaryValue();
|
||||
dict->SetInteger("id", request->identifier());
|
||||
dict->SetString("url", request->url().spec());
|
||||
dict->SetString("method", request->method());
|
||||
content::ResourceType resourceType = content::RESOURCE_TYPE_LAST_TYPE;
|
||||
auto info = content::ResourceRequestInfo::ForRequest(request);
|
||||
if (info)
|
||||
resourceType = info->GetResourceType();
|
||||
dict->SetInteger("resourceType", resourceType);
|
||||
dict->SetDouble("timestamp", base::Time::Now().ToDoubleT() * 1000);
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
||||
base::DictionaryValue* GetRequestHeadersDict(
|
||||
const net::HttpRequestHeaders& headers) {
|
||||
base::DictionaryValue* header_dict = new base::DictionaryValue();
|
||||
net::HttpRequestHeaders::Iterator it(headers);
|
||||
while (it.GetNext())
|
||||
header_dict->SetString(it.name(), it.value());
|
||||
return header_dict;
|
||||
}
|
||||
|
||||
base::DictionaryValue* GetResponseHeadersDict(
|
||||
const net::HttpResponseHeaders* headers) {
|
||||
base::DictionaryValue* header_dict = new base::DictionaryValue();
|
||||
if (headers) {
|
||||
void* iter = nullptr;
|
||||
std::string key;
|
||||
std::string value;
|
||||
while (headers->EnumerateHeaderLines(&iter, &key, &value))
|
||||
header_dict->SetString(key, value);
|
||||
}
|
||||
return header_dict;
|
||||
}
|
||||
|
||||
void OnBeforeURLRequestResponse(
|
||||
const net::CompletionCallback& callback,
|
||||
GURL* new_url,
|
||||
const AtomNetworkDelegate::BlockingResponse& result) {
|
||||
if (!result.redirectURL.is_empty())
|
||||
*new_url = result.redirectURL;
|
||||
callback.Run(result.cancel);
|
||||
}
|
||||
|
||||
void OnBeforeSendHeadersResponse(
|
||||
const net::CompletionCallback& callback,
|
||||
net::HttpRequestHeaders* headers,
|
||||
const AtomNetworkDelegate::BlockingResponse& result) {
|
||||
if (!result.requestHeaders.IsEmpty())
|
||||
*headers = result.requestHeaders;
|
||||
callback.Run(result.cancel);
|
||||
}
|
||||
|
||||
void OnHeadersReceivedResponse(
|
||||
const net::CompletionCallback& callback,
|
||||
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
|
||||
const AtomNetworkDelegate::BlockingResponse& result) {
|
||||
if (result.responseHeaders.get())
|
||||
*override_response_headers = result.responseHeaders;
|
||||
callback.Run(result.cancel);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// static
|
||||
std::map<AtomNetworkDelegate::EventTypes, AtomNetworkDelegate::ListenerInfo>
|
||||
AtomNetworkDelegate::event_listener_map_;
|
||||
|
||||
AtomNetworkDelegate::AtomNetworkDelegate() {
|
||||
}
|
||||
|
||||
AtomNetworkDelegate::~AtomNetworkDelegate() {
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::SetListenerInIO(
|
||||
EventTypes type,
|
||||
const base::DictionaryValue* filter,
|
||||
const Listener& callback) {
|
||||
ListenerInfo info;
|
||||
info.callback = callback;
|
||||
event_listener_map_[type] = info;
|
||||
}
|
||||
|
||||
int AtomNetworkDelegate::OnBeforeURLRequest(
|
||||
net::URLRequest* request,
|
||||
const net::CompletionCallback& callback,
|
||||
GURL* new_url) {
|
||||
brightray::NetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
|
||||
|
||||
auto listener_info = event_listener_map_[kOnBeforeRequest];
|
||||
if (!event_listener_map_.empty() && !listener_info.callback.is_null()) {
|
||||
auto wrapped_callback = listener_info.callback;
|
||||
auto details = ExtractRequestInfo(request);
|
||||
|
||||
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(wrapped_callback, details),
|
||||
base::Bind(&OnBeforeURLRequestResponse,
|
||||
callback, new_url));
|
||||
|
||||
return net::ERR_IO_PENDING;
|
||||
}
|
||||
|
||||
return net::OK;
|
||||
}
|
||||
|
||||
int AtomNetworkDelegate::OnBeforeSendHeaders(
|
||||
net::URLRequest* request,
|
||||
const net::CompletionCallback& callback,
|
||||
net::HttpRequestHeaders* headers) {
|
||||
auto listener_info = event_listener_map_[kOnBeforeSendHeaders];
|
||||
if (!event_listener_map_.empty() && !listener_info.callback.is_null()) {
|
||||
auto wrapped_callback = listener_info.callback;
|
||||
auto details = ExtractRequestInfo(request);
|
||||
details->Set("requestHeaders", GetRequestHeadersDict(*headers));
|
||||
|
||||
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(wrapped_callback, details),
|
||||
base::Bind(&OnBeforeSendHeadersResponse,
|
||||
callback, headers));
|
||||
|
||||
return net::ERR_IO_PENDING;
|
||||
}
|
||||
|
||||
return net::OK;
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnSendHeaders(
|
||||
net::URLRequest* request,
|
||||
const net::HttpRequestHeaders& headers) {
|
||||
auto listener_info = event_listener_map_[kOnSendHeaders];
|
||||
if (!event_listener_map_.empty() && !listener_info.callback.is_null()) {
|
||||
auto wrapped_callback = listener_info.callback;
|
||||
auto details = ExtractRequestInfo(request);
|
||||
details->Set("requestHeaders", GetRequestHeadersDict(headers));
|
||||
|
||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(base::IgnoreResult(wrapped_callback),
|
||||
details));
|
||||
}
|
||||
}
|
||||
|
||||
int AtomNetworkDelegate::OnHeadersReceived(
|
||||
net::URLRequest* request,
|
||||
const net::CompletionCallback& callback,
|
||||
const net::HttpResponseHeaders* original_response_headers,
|
||||
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
|
||||
GURL* allowed_unsafe_redirect_url) {
|
||||
auto listener_info = event_listener_map_[kOnHeadersReceived];
|
||||
if (!event_listener_map_.empty() && !listener_info.callback.is_null()) {
|
||||
auto wrapped_callback = listener_info.callback;
|
||||
auto details = ExtractRequestInfo(request);
|
||||
details->Set("responseHeaders",
|
||||
GetResponseHeadersDict(original_response_headers));
|
||||
|
||||
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(wrapped_callback, details),
|
||||
base::Bind(&OnHeadersReceivedResponse,
|
||||
callback,
|
||||
override_response_headers));
|
||||
|
||||
return net::ERR_IO_PENDING;
|
||||
}
|
||||
|
||||
return net::OK;
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
|
||||
const GURL& new_location) {
|
||||
auto listener_info = event_listener_map_[kOnBeforeRedirect];
|
||||
if (!event_listener_map_.empty() && !listener_info.callback.is_null()) {
|
||||
auto wrapped_callback = listener_info.callback;
|
||||
auto details = ExtractRequestInfo(request);
|
||||
details->SetString("redirectURL", new_location.spec());
|
||||
|
||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(base::IgnoreResult(wrapped_callback),
|
||||
details));
|
||||
}
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
|
||||
auto listener_info = event_listener_map_[kOnResponseStarted];
|
||||
if (!event_listener_map_.empty() && !listener_info.callback.is_null()) {
|
||||
auto wrapped_callback = listener_info.callback;
|
||||
auto details = ExtractRequestInfo(request);
|
||||
details->Set("responseHeaders",
|
||||
GetResponseHeadersDict(request->response_headers()));
|
||||
|
||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(base::IgnoreResult(wrapped_callback),
|
||||
details));
|
||||
}
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnCompleted(net::URLRequest* request, bool started) {
|
||||
}
|
||||
|
||||
} // namespace atom
|
89
atom/browser/net/atom_network_delegate.h
Normal file
89
atom/browser/net/atom_network_delegate.h
Normal file
|
@ -0,0 +1,89 @@
|
|||
// Copyright (c) 2015 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ATOM_BROWSER_NET_ATOM_NETWORK_DELEGATE_H_
|
||||
#define ATOM_BROWSER_NET_ATOM_NETWORK_DELEGATE_H_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "brightray/browser/network_delegate.h"
|
||||
#include "base/callback.h"
|
||||
#include "base/values.h"
|
||||
#include "net/http/http_request_headers.h"
|
||||
#include "net/http/http_response_headers.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
class AtomNetworkDelegate : public brightray::NetworkDelegate {
|
||||
public:
|
||||
enum EventTypes {
|
||||
kInvalidEvent = 0,
|
||||
kOnBeforeRequest = 1 << 0,
|
||||
kOnBeforeSendHeaders = 1 << 1,
|
||||
kOnSendHeaders = 1 << 2,
|
||||
kOnHeadersReceived = 1 << 3,
|
||||
kOnBeforeRedirect = 1 << 4,
|
||||
kOnResponseStarted = 1 << 5,
|
||||
kOnErrorOccurred = 1 << 6,
|
||||
kOnCompleted = 1 << 7,
|
||||
};
|
||||
|
||||
struct BlockingResponse {
|
||||
BlockingResponse() {}
|
||||
~BlockingResponse() {}
|
||||
|
||||
bool cancel;
|
||||
GURL redirectURL;
|
||||
net::HttpRequestHeaders requestHeaders;
|
||||
scoped_refptr<net::HttpResponseHeaders> responseHeaders;
|
||||
};
|
||||
|
||||
using Listener =
|
||||
base::Callback<BlockingResponse(const base::DictionaryValue*)>;
|
||||
|
||||
AtomNetworkDelegate();
|
||||
~AtomNetworkDelegate() override;
|
||||
|
||||
void SetListenerInIO(EventTypes type,
|
||||
const base::DictionaryValue* filter,
|
||||
const Listener& callback);
|
||||
|
||||
protected:
|
||||
// net::NetworkDelegate:
|
||||
int OnBeforeURLRequest(net::URLRequest* request,
|
||||
const net::CompletionCallback& callback,
|
||||
GURL* new_url) override;
|
||||
int OnBeforeSendHeaders(net::URLRequest* request,
|
||||
const net::CompletionCallback& callback,
|
||||
net::HttpRequestHeaders* headers) override;
|
||||
void OnSendHeaders(net::URLRequest* request,
|
||||
const net::HttpRequestHeaders& headers) override;
|
||||
int OnHeadersReceived(
|
||||
net::URLRequest* request,
|
||||
const net::CompletionCallback& callback,
|
||||
const net::HttpResponseHeaders* original_response_headers,
|
||||
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
|
||||
GURL* allowed_unsafe_redirect_url) override;
|
||||
void OnBeforeRedirect(net::URLRequest* request,
|
||||
const GURL& new_location) override;
|
||||
void OnResponseStarted(net::URLRequest* request) override;
|
||||
void OnCompleted(net::URLRequest* request, bool started) override;
|
||||
|
||||
private:
|
||||
struct ListenerInfo {
|
||||
ListenerInfo() {}
|
||||
~ListenerInfo() {}
|
||||
|
||||
AtomNetworkDelegate::Listener callback;
|
||||
};
|
||||
|
||||
static std::map<EventTypes, ListenerInfo> event_listener_map_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(AtomNetworkDelegate);
|
||||
};
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_BROWSER_NET_ATOM_NETWORK_DELEGATE_H_
|
|
@ -7,8 +7,11 @@
|
|||
#include <string>
|
||||
|
||||
#include "atom/common/node_includes.h"
|
||||
#include "atom/common/native_mate_converters/gurl_converter.h"
|
||||
#include "atom/common/native_mate_converters/value_converter.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "net/cert/x509_certificate.h"
|
||||
#include "net/http/http_response_headers.h"
|
||||
#include "net/url_request/url_request.h"
|
||||
|
||||
namespace mate {
|
||||
|
@ -50,4 +53,38 @@ v8::Local<v8::Value> Converter<scoped_refptr<net::X509Certificate>>::ToV8(
|
|||
return dict.GetHandle();
|
||||
}
|
||||
|
||||
// static
|
||||
bool Converter<atom::AtomNetworkDelegate::BlockingResponse>::FromV8(
|
||||
v8::Isolate* isolate, v8::Local<v8::Value> val,
|
||||
atom::AtomNetworkDelegate::BlockingResponse* out) {
|
||||
mate::Dictionary dict;
|
||||
if (!ConvertFromV8(isolate, val, &dict))
|
||||
return false;
|
||||
if (!dict.Get("cancel", &(out->cancel)))
|
||||
return false;
|
||||
dict.Get("redirectURL", &(out->redirectURL));
|
||||
base::DictionaryValue request_headers;
|
||||
if (dict.Get("requestHeaders", &request_headers)) {
|
||||
for (base::DictionaryValue::Iterator it(request_headers);
|
||||
!it.IsAtEnd();
|
||||
it.Advance()) {
|
||||
std::string value;
|
||||
CHECK(it.value().GetAsString(&value));
|
||||
out->requestHeaders.SetHeader(it.key(), value);
|
||||
}
|
||||
}
|
||||
base::DictionaryValue response_headers;
|
||||
if (dict.Get("responseHeaders", &response_headers)) {
|
||||
out->responseHeaders = new net::HttpResponseHeaders("");
|
||||
for (base::DictionaryValue::Iterator it(response_headers);
|
||||
!it.IsAtEnd();
|
||||
it.Advance()) {
|
||||
std::string value;
|
||||
CHECK(it.value().GetAsString(&value));
|
||||
out->responseHeaders->AddHeader(it.key() + " : " + value);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace mate
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_NET_CONVERTER_H_
|
||||
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_NET_CONVERTER_H_
|
||||
|
||||
#include "atom/browser/net/atom_network_delegate.h"
|
||||
#include "base/memory/ref_counted.h"
|
||||
#include "native_mate/converter.h"
|
||||
|
||||
|
@ -34,6 +35,13 @@ struct Converter<scoped_refptr<net::X509Certificate>> {
|
|||
const scoped_refptr<net::X509Certificate>& val);
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Converter<atom::AtomNetworkDelegate::BlockingResponse> {
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
atom::AtomNetworkDelegate::BlockingResponse* out);
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_NET_CONVERTER_H_
|
||||
|
|
|
@ -30,6 +30,13 @@ v8::Local<v8::Value> Converter<base::DictionaryValue>::ToV8(
|
|||
return converter->ToV8Value(&val, isolate->GetCurrentContext());
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> Converter<const base::DictionaryValue*>::ToV8(
|
||||
v8::Isolate* isolate,
|
||||
const base::DictionaryValue* val) {
|
||||
scoped_ptr<atom::V8ValueConverter> converter(new atom::V8ValueConverter);
|
||||
return converter->ToV8Value(val, isolate->GetCurrentContext());
|
||||
}
|
||||
|
||||
bool Converter<base::ListValue>::FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
base::ListValue* out) {
|
||||
|
|
|
@ -23,6 +23,12 @@ struct Converter<base::DictionaryValue> {
|
|||
const base::DictionaryValue& val);
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Converter<const base::DictionaryValue*> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const base::DictionaryValue* val);
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Converter<base::ListValue> {
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
|
|
|
@ -110,6 +110,8 @@
|
|||
'atom/browser/api/atom_api_tray.h',
|
||||
'atom/browser/api/atom_api_web_contents.cc',
|
||||
'atom/browser/api/atom_api_web_contents.h',
|
||||
'atom/browser/api/atom_api_web_request.cc',
|
||||
'atom/browser/api/atom_api_web_request.h',
|
||||
'atom/browser/api/atom_api_web_view_manager.cc',
|
||||
'atom/browser/api/atom_api_window.cc',
|
||||
'atom/browser/api/atom_api_window.h',
|
||||
|
@ -178,6 +180,8 @@
|
|||
'atom/browser/net/asar/url_request_asar_job.h',
|
||||
'atom/browser/net/atom_cert_verifier.cc',
|
||||
'atom/browser/net/atom_cert_verifier.h',
|
||||
'atom/browser/net/atom_network_delegate.cc',
|
||||
'atom/browser/net/atom_network_delegate.h',
|
||||
'atom/browser/net/atom_ssl_config_service.cc',
|
||||
'atom/browser/net/atom_ssl_config_service.h',
|
||||
'atom/browser/net/atom_url_request_job_factory.cc',
|
||||
|
|
Loading…
Reference in a new issue