Make the webRequest listener asynchronous
This commit is contained in:
parent
d3c8363450
commit
fed94aada0
7 changed files with 197 additions and 180 deletions
|
@ -5,6 +5,7 @@
|
|||
#include "atom/browser/api/atom_api_web_contents.h"
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
#include "atom/browser/api/atom_api_session.h"
|
||||
#include "atom/browser/api/atom_api_window.h"
|
||||
|
|
|
@ -41,8 +41,20 @@ WebRequest::WebRequest(AtomBrowserContext* browser_context)
|
|||
WebRequest::~WebRequest() {
|
||||
}
|
||||
|
||||
template<AtomNetworkDelegate::EventType type>
|
||||
void WebRequest::SetListener(mate::Arguments* args) {
|
||||
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) {
|
||||
// { urls }.
|
||||
URLPatterns patterns;
|
||||
mate::Dictionary dict;
|
||||
|
@ -50,8 +62,8 @@ void WebRequest::SetListener(mate::Arguments* args) {
|
|||
|
||||
// Function or null.
|
||||
v8::Local<v8::Value> value;
|
||||
AtomNetworkDelegate::Listener callback;
|
||||
if (!args->GetNext(&callback) &&
|
||||
Listener listener;
|
||||
if (!args->GetNext(&listener) &&
|
||||
!(args->GetNext(&value) && value->IsNull())) {
|
||||
args->ThrowError("Must pass null or a Function");
|
||||
return;
|
||||
|
@ -59,9 +71,8 @@ void WebRequest::SetListener(mate::Arguments* args) {
|
|||
|
||||
auto delegate = browser_context_->network_delegate();
|
||||
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
||||
base::Bind(&AtomNetworkDelegate::SetListenerInIO,
|
||||
base::Unretained(delegate),
|
||||
type, patterns, callback));
|
||||
base::Bind(method, base::Unretained(delegate), type,
|
||||
patterns, listener));
|
||||
}
|
||||
|
||||
// static
|
||||
|
@ -76,28 +87,28 @@ void WebRequest::BuildPrototype(v8::Isolate* isolate,
|
|||
v8::Local<v8::ObjectTemplate> prototype) {
|
||||
mate::ObjectTemplateBuilder(isolate, prototype)
|
||||
.SetMethod("onBeforeRequest",
|
||||
&WebRequest::SetListener<
|
||||
&WebRequest::SetResponseListener<
|
||||
AtomNetworkDelegate::kOnBeforeRequest>)
|
||||
.SetMethod("onBeforeSendHeaders",
|
||||
&WebRequest::SetListener<
|
||||
&WebRequest::SetResponseListener<
|
||||
AtomNetworkDelegate::kOnBeforeSendHeaders>)
|
||||
.SetMethod("onSendHeaders",
|
||||
&WebRequest::SetListener<
|
||||
AtomNetworkDelegate::kOnSendHeaders>)
|
||||
.SetMethod("onHeadersReceived",
|
||||
&WebRequest::SetListener<
|
||||
&WebRequest::SetResponseListener<
|
||||
AtomNetworkDelegate::kOnHeadersReceived>)
|
||||
.SetMethod("onSendHeaders",
|
||||
&WebRequest::SetSimpleListener<
|
||||
AtomNetworkDelegate::kOnSendHeaders>)
|
||||
.SetMethod("onBeforeRedirect",
|
||||
&WebRequest::SetListener<
|
||||
&WebRequest::SetSimpleListener<
|
||||
AtomNetworkDelegate::kOnBeforeRedirect>)
|
||||
.SetMethod("onResponseStarted",
|
||||
&WebRequest::SetListener<
|
||||
&WebRequest::SetSimpleListener<
|
||||
AtomNetworkDelegate::kOnResponseStarted>)
|
||||
.SetMethod("onCompleted",
|
||||
&WebRequest::SetListener<
|
||||
&WebRequest::SetSimpleListener<
|
||||
AtomNetworkDelegate::kOnCompleted>)
|
||||
.SetMethod("onErrorOccurred",
|
||||
&WebRequest::SetListener<
|
||||
&WebRequest::SetSimpleListener<
|
||||
AtomNetworkDelegate::kOnErrorOccurred>);
|
||||
}
|
||||
|
||||
|
|
|
@ -29,8 +29,13 @@ class WebRequest : public mate::TrackableObject<WebRequest> {
|
|||
explicit WebRequest(AtomBrowserContext* browser_context);
|
||||
~WebRequest();
|
||||
|
||||
template<AtomNetworkDelegate::EventType Event>
|
||||
void SetListener(mate::Arguments* args);
|
||||
// C++ can not distinguish overloaded member function.
|
||||
template<AtomNetworkDelegate::SimpleEvent type>
|
||||
void SetSimpleListener(mate::Arguments* args);
|
||||
template<AtomNetworkDelegate::ResponseEvent type>
|
||||
void SetResponseListener(mate::Arguments* args);
|
||||
template<typename Listener, typename Method, typename Event>
|
||||
void SetListener(Method method, Event type, mate::Arguments* args);
|
||||
|
||||
private:
|
||||
scoped_refptr<AtomBrowserContext> browser_context_;
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
#include "atom/browser/net/atom_network_delegate.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "atom/common/native_mate_converters/net_converter.h"
|
||||
#include "base/stl_util.h"
|
||||
#include "base/strings/string_util.h"
|
||||
|
@ -38,12 +40,19 @@ const char* ResourceTypeToString(content::ResourceType type) {
|
|||
}
|
||||
}
|
||||
|
||||
AtomNetworkDelegate::BlockingResponse RunListener(
|
||||
const AtomNetworkDelegate::Listener& callback,
|
||||
void RunSimpleListener(const AtomNetworkDelegate::SimpleListener& listener,
|
||||
scoped_ptr<base::DictionaryValue> details) {
|
||||
return callback.Run(*(details.get()));
|
||||
return listener.Run(*(details.get()));
|
||||
}
|
||||
|
||||
void RunResponseListener(
|
||||
const AtomNetworkDelegate::ResponseListener& listener,
|
||||
scoped_ptr<base::DictionaryValue> details,
|
||||
const AtomNetworkDelegate::ResponseCallback& callback) {
|
||||
return listener.Run(*(details.get()), callback);
|
||||
}
|
||||
|
||||
// Test whether the URL of |request| matches |patterns|.
|
||||
bool MatchesFilterCondition(net::URLRequest* request,
|
||||
const URLPatterns& patterns) {
|
||||
if (patterns.empty())
|
||||
|
@ -56,6 +65,7 @@ bool MatchesFilterCondition(net::URLRequest* request,
|
|||
return false;
|
||||
}
|
||||
|
||||
// Overloaded by multiple types to fill the |details| object.
|
||||
void FillDetailsObject(base::DictionaryValue* details,
|
||||
net::URLRequest* request) {
|
||||
details->SetInteger("id", request->identifier());
|
||||
|
@ -122,41 +132,65 @@ void FillDetailsObject(base::DictionaryValue* details,
|
|||
details->SetString("error", net::ErrorToString(status.error()));
|
||||
}
|
||||
|
||||
void OnBeforeURLRequestResponse(
|
||||
const net::CompletionCallback& callback,
|
||||
GURL* new_url,
|
||||
const AtomNetworkDelegate::BlockingResponse& result) {
|
||||
if (!result.redirect_url.is_empty())
|
||||
*new_url = result.redirect_url;
|
||||
callback.Run(result.code());
|
||||
// Fill the native types with the result from the response object.
|
||||
void ReadFromResponseObject(const base::DictionaryValue& response,
|
||||
GURL* new_location) {
|
||||
std::string url;
|
||||
if (response.GetString("redirectURL", &url))
|
||||
*new_location = GURL(url);
|
||||
}
|
||||
|
||||
void OnBeforeSendHeadersResponse(
|
||||
const net::CompletionCallback& callback,
|
||||
net::HttpRequestHeaders* headers,
|
||||
const AtomNetworkDelegate::BlockingResponse& result) {
|
||||
if (!result.request_headers.IsEmpty())
|
||||
*headers = result.request_headers;
|
||||
callback.Run(result.code());
|
||||
}
|
||||
|
||||
void OnHeadersReceivedResponse(
|
||||
const net::CompletionCallback& callback,
|
||||
const net::HttpResponseHeaders* original_response_headers,
|
||||
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
|
||||
const AtomNetworkDelegate::BlockingResponse& result) {
|
||||
if (result.response_headers.get()) {
|
||||
*override_response_headers = new net::HttpResponseHeaders(
|
||||
original_response_headers->raw_headers());
|
||||
void* iter = nullptr;
|
||||
std::string key;
|
||||
void ReadFromResponseObject(const base::DictionaryValue& response,
|
||||
net::HttpRequestHeaders* headers) {
|
||||
const base::DictionaryValue* dict;
|
||||
if (response.GetDictionary("requestHeaders", &dict)) {
|
||||
for (base::DictionaryValue::Iterator it(*dict);
|
||||
!it.IsAtEnd();
|
||||
it.Advance()) {
|
||||
std::string value;
|
||||
while (result.response_headers->EnumerateHeaderLines(&iter, &key, &value)) {
|
||||
(*override_response_headers)->RemoveHeader(key);
|
||||
(*override_response_headers)->AddHeader(key + ": " + value);
|
||||
if (it.value().GetAsString(&value))
|
||||
headers->SetHeader(it.key(), value);
|
||||
}
|
||||
}
|
||||
callback.Run(result.code());
|
||||
}
|
||||
|
||||
void ReadFromResponseObject(const base::DictionaryValue& response,
|
||||
scoped_refptr<net::HttpResponseHeaders>* headers) {
|
||||
const base::DictionaryValue* dict;
|
||||
if (response.GetDictionary("responseHeaders", &dict)) {
|
||||
*headers = new net::HttpResponseHeaders("");
|
||||
for (base::DictionaryValue::Iterator it(*dict);
|
||||
!it.IsAtEnd();
|
||||
it.Advance()) {
|
||||
std::string value;
|
||||
if (it.value().GetAsString(&value)) {
|
||||
(*headers)->RemoveHeader(it.key());
|
||||
(*headers)->AddHeader(it.key() + " : " + value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Deal with the results of Listener.
|
||||
template<typename T>
|
||||
void OnListenerResultInIO(const net::CompletionCallback& callback,
|
||||
T out,
|
||||
scoped_ptr<base::DictionaryValue> response) {
|
||||
ReadFromResponseObject(*response.get(), out);
|
||||
|
||||
bool cancel = false;
|
||||
response->GetBoolean("cancel", &cancel);
|
||||
callback.Run(cancel ? net::ERR_BLOCKED_BY_CLIENT : net::OK);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void OnListenerResultInUI(const net::CompletionCallback& callback,
|
||||
T out,
|
||||
const base::DictionaryValue& response) {
|
||||
scoped_ptr<base::DictionaryValue> copy = response.CreateDeepCopy();
|
||||
BrowserThread::PostTask(
|
||||
BrowserThread::IO, FROM_HERE,
|
||||
base::Bind(OnListenerResultInIO<T>, callback, out, base::Passed(©)));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
@ -167,36 +201,47 @@ AtomNetworkDelegate::AtomNetworkDelegate() {
|
|||
AtomNetworkDelegate::~AtomNetworkDelegate() {
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::SetListenerInIO(EventType type,
|
||||
void AtomNetworkDelegate::SetSimpleListenerInIO(
|
||||
SimpleEvent type,
|
||||
const URLPatterns& patterns,
|
||||
const Listener& callback) {
|
||||
if (callback.is_null()) {
|
||||
event_listener_map_.erase(type);
|
||||
return;
|
||||
const SimpleListener& callback) {
|
||||
if (callback.is_null())
|
||||
simple_listeners_.erase(type);
|
||||
else
|
||||
simple_listeners_[type] = { patterns, callback };
|
||||
}
|
||||
|
||||
event_listener_map_[type] = { patterns, callback };
|
||||
void AtomNetworkDelegate::SetResponseListenerInIO(
|
||||
ResponseEvent type,
|
||||
const URLPatterns& patterns,
|
||||
const ResponseListener& callback) {
|
||||
if (callback.is_null())
|
||||
response_listeners_.erase(type);
|
||||
else
|
||||
response_listeners_[type] = { patterns, callback };
|
||||
}
|
||||
|
||||
int AtomNetworkDelegate::OnBeforeURLRequest(
|
||||
net::URLRequest* request,
|
||||
const net::CompletionCallback& callback,
|
||||
GURL* new_url) {
|
||||
if (!ContainsKey(event_listener_map_, kOnBeforeRequest))
|
||||
if (!ContainsKey(response_listeners_, kOnBeforeRequest))
|
||||
return brightray::NetworkDelegate::OnBeforeURLRequest(
|
||||
request, callback, new_url);
|
||||
|
||||
const ListenerInfo& info = event_listener_map_[kOnBeforeRequest];
|
||||
const auto& info = response_listeners_[kOnBeforeRequest];
|
||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
||||
return net::OK;
|
||||
|
||||
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
||||
FillDetailsObject(details.get(), request);
|
||||
|
||||
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(&RunListener, info.callback, base::Passed(&details)),
|
||||
base::Bind(&OnBeforeURLRequestResponse,
|
||||
callback, new_url));
|
||||
ResponseCallback response =
|
||||
base::Bind(OnListenerResultInUI<GURL*>, callback, new_url);
|
||||
BrowserThread::PostTask(
|
||||
BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(RunResponseListener, info.listener, base::Passed(&details),
|
||||
response));
|
||||
return net::ERR_IO_PENDING;
|
||||
}
|
||||
|
||||
|
@ -204,11 +249,11 @@ int AtomNetworkDelegate::OnBeforeSendHeaders(
|
|||
net::URLRequest* request,
|
||||
const net::CompletionCallback& callback,
|
||||
net::HttpRequestHeaders* headers) {
|
||||
if (!ContainsKey(event_listener_map_, kOnBeforeSendHeaders))
|
||||
if (!ContainsKey(response_listeners_, kOnBeforeSendHeaders))
|
||||
return brightray::NetworkDelegate::OnBeforeSendHeaders(
|
||||
request, callback, headers);
|
||||
|
||||
const ListenerInfo& info = event_listener_map_[kOnBeforeSendHeaders];
|
||||
const auto& info = response_listeners_[kOnBeforeSendHeaders];
|
||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
||||
return net::OK;
|
||||
|
||||
|
@ -216,22 +261,25 @@ int AtomNetworkDelegate::OnBeforeSendHeaders(
|
|||
FillDetailsObject(details.get(), request);
|
||||
FillDetailsObject(details.get(), *headers);
|
||||
|
||||
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(&RunListener, info.callback, base::Passed(&details)),
|
||||
base::Bind(&OnBeforeSendHeadersResponse,
|
||||
callback, headers));
|
||||
ResponseCallback response =
|
||||
base::Bind(OnListenerResultInUI<net::HttpRequestHeaders*>,
|
||||
callback, headers);
|
||||
BrowserThread::PostTask(
|
||||
BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(RunResponseListener, info.listener, base::Passed(&details),
|
||||
response));
|
||||
return net::ERR_IO_PENDING;
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnSendHeaders(
|
||||
net::URLRequest* request,
|
||||
const net::HttpRequestHeaders& headers) {
|
||||
if (!ContainsKey(event_listener_map_, kOnSendHeaders)) {
|
||||
if (!ContainsKey(simple_listeners_, kOnSendHeaders)) {
|
||||
brightray::NetworkDelegate::OnSendHeaders(request, headers);
|
||||
return;
|
||||
}
|
||||
|
||||
const ListenerInfo& info = event_listener_map_[kOnSendHeaders];
|
||||
const auto& info = simple_listeners_[kOnSendHeaders];
|
||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
||||
return;
|
||||
|
||||
|
@ -239,10 +287,9 @@ void AtomNetworkDelegate::OnSendHeaders(
|
|||
FillDetailsObject(details.get(), request);
|
||||
FillDetailsObject(details.get(), headers);
|
||||
|
||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(base::IgnoreResult(&RunListener),
|
||||
info.callback,
|
||||
base::Passed(&details)));
|
||||
BrowserThread::PostTask(
|
||||
BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(&RunSimpleListener, info.listener, base::Passed(&details)));
|
||||
}
|
||||
|
||||
int AtomNetworkDelegate::OnHeadersReceived(
|
||||
|
@ -251,12 +298,12 @@ int AtomNetworkDelegate::OnHeadersReceived(
|
|||
const net::HttpResponseHeaders* original_response_headers,
|
||||
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
|
||||
GURL* allowed_unsafe_redirect_url) {
|
||||
if (!ContainsKey(event_listener_map_, kOnHeadersReceived))
|
||||
if (!ContainsKey(response_listeners_, kOnHeadersReceived))
|
||||
return brightray::NetworkDelegate::OnHeadersReceived(
|
||||
request, callback, original_response_headers, override_response_headers,
|
||||
allowed_unsafe_redirect_url);
|
||||
|
||||
const ListenerInfo& info = event_listener_map_[kOnHeadersReceived];
|
||||
const auto& info = response_listeners_[kOnHeadersReceived];
|
||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
||||
return net::OK;
|
||||
|
||||
|
@ -264,23 +311,24 @@ int AtomNetworkDelegate::OnHeadersReceived(
|
|||
FillDetailsObject(details.get(), request);
|
||||
FillDetailsObject(details.get(), original_response_headers);
|
||||
|
||||
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(&RunListener, info.callback, base::Passed(&details)),
|
||||
base::Bind(&OnHeadersReceivedResponse,
|
||||
callback,
|
||||
original_response_headers,
|
||||
override_response_headers));
|
||||
ResponseCallback response =
|
||||
base::Bind(OnListenerResultInUI<scoped_refptr<net::HttpResponseHeaders>*>,
|
||||
callback, override_response_headers);
|
||||
BrowserThread::PostTask(
|
||||
BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(RunResponseListener, info.listener, base::Passed(&details),
|
||||
response));
|
||||
return net::ERR_IO_PENDING;
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
|
||||
const GURL& new_location) {
|
||||
if (!ContainsKey(event_listener_map_, kOnBeforeRedirect)) {
|
||||
if (!ContainsKey(simple_listeners_, kOnBeforeRedirect)) {
|
||||
brightray::NetworkDelegate::OnBeforeRedirect(request, new_location);
|
||||
return;
|
||||
}
|
||||
|
||||
const ListenerInfo& info = event_listener_map_[kOnBeforeRedirect];
|
||||
const auto& info = simple_listeners_[kOnBeforeRedirect];
|
||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
||||
return;
|
||||
|
||||
|
@ -291,14 +339,13 @@ void AtomNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
|
|||
FillDetailsObject(details.get(), request->GetSocketAddress());
|
||||
FillDetailsObject(details.get(), request->was_cached());
|
||||
|
||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(base::IgnoreResult(&RunListener),
|
||||
info.callback,
|
||||
base::Passed(&details)));
|
||||
BrowserThread::PostTask(
|
||||
BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(&RunSimpleListener, info.listener, base::Passed(&details)));
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
|
||||
if (!ContainsKey(event_listener_map_, kOnResponseStarted)) {
|
||||
if (!ContainsKey(simple_listeners_, kOnResponseStarted)) {
|
||||
brightray::NetworkDelegate::OnResponseStarted(request);
|
||||
return;
|
||||
}
|
||||
|
@ -306,7 +353,7 @@ void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
|
|||
if (request->status().status() != net::URLRequestStatus::SUCCESS)
|
||||
return;
|
||||
|
||||
const ListenerInfo& info = event_listener_map_[kOnResponseStarted];
|
||||
const auto& info = simple_listeners_[kOnResponseStarted];
|
||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
||||
return;
|
||||
|
||||
|
@ -315,17 +362,16 @@ void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
|
|||
FillDetailsObject(details.get(), request->response_headers());
|
||||
FillDetailsObject(details.get(), request->was_cached());
|
||||
|
||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(base::IgnoreResult(&RunListener),
|
||||
info.callback,
|
||||
base::Passed(&details)));
|
||||
BrowserThread::PostTask(
|
||||
BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(RunSimpleListener, info.listener, base::Passed(&details)));
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnCompleted(net::URLRequest* request, bool started) {
|
||||
if (request->status().status() == net::URLRequestStatus::FAILED ||
|
||||
request->status().status() == net::URLRequestStatus::CANCELED) {
|
||||
// Error event.
|
||||
if (ContainsKey(event_listener_map_, kOnErrorOccurred))
|
||||
if (ContainsKey(simple_listeners_, kOnErrorOccurred))
|
||||
OnErrorOccurred(request);
|
||||
else
|
||||
brightray::NetworkDelegate::OnCompleted(request, started);
|
||||
|
@ -338,12 +384,12 @@ void AtomNetworkDelegate::OnCompleted(net::URLRequest* request, bool started) {
|
|||
return;
|
||||
}
|
||||
|
||||
if (!ContainsKey(event_listener_map_, kOnCompleted)) {
|
||||
if (!ContainsKey(simple_listeners_, kOnCompleted)) {
|
||||
brightray::NetworkDelegate::OnCompleted(request, started);
|
||||
return;
|
||||
}
|
||||
|
||||
const ListenerInfo& info = event_listener_map_[kOnCompleted];
|
||||
const auto& info = simple_listeners_[kOnCompleted];
|
||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
||||
return;
|
||||
|
||||
|
@ -352,15 +398,13 @@ void AtomNetworkDelegate::OnCompleted(net::URLRequest* request, bool started) {
|
|||
FillDetailsObject(details.get(), request->response_headers());
|
||||
FillDetailsObject(details.get(), request->was_cached());
|
||||
|
||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(base::IgnoreResult(&RunListener),
|
||||
info.callback,
|
||||
base::Passed(&details)));
|
||||
BrowserThread::PostTask(
|
||||
BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(RunSimpleListener, info.listener, base::Passed(&details)));
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnErrorOccurred(net::URLRequest* request) {
|
||||
|
||||
const ListenerInfo& info = event_listener_map_[kOnErrorOccurred];
|
||||
const auto& info = simple_listeners_[kOnErrorOccurred];
|
||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
||||
return;
|
||||
|
||||
|
@ -369,10 +413,9 @@ void AtomNetworkDelegate::OnErrorOccurred(net::URLRequest* request) {
|
|||
FillDetailsObject(details.get(), request->was_cached());
|
||||
FillDetailsObject(details.get(), request->status());
|
||||
|
||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(base::IgnoreResult(&RunListener),
|
||||
info.callback,
|
||||
base::Passed(&details)));
|
||||
BrowserThread::PostTask(
|
||||
BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(RunSimpleListener, info.listener, base::Passed(&details)));
|
||||
}
|
||||
|
||||
} // namespace atom
|
||||
|
|
|
@ -26,46 +26,44 @@ using URLPatterns = std::set<extensions::URLPattern>;
|
|||
|
||||
class AtomNetworkDelegate : public brightray::NetworkDelegate {
|
||||
public:
|
||||
struct BlockingResponse;
|
||||
using Listener =
|
||||
base::Callback<BlockingResponse(const base::DictionaryValue&)>;
|
||||
using ResponseCallback = base::Callback<void(const base::DictionaryValue&)>;
|
||||
using SimpleListener = base::Callback<void(const base::DictionaryValue&)>;
|
||||
using ResponseListener = base::Callback<void(const base::DictionaryValue&,
|
||||
const ResponseCallback&)>;
|
||||
|
||||
enum EventType {
|
||||
kOnBeforeRequest,
|
||||
kOnBeforeSendHeaders,
|
||||
enum SimpleEvent {
|
||||
kOnSendHeaders,
|
||||
kOnHeadersReceived,
|
||||
kOnBeforeRedirect,
|
||||
kOnResponseStarted,
|
||||
kOnCompleted,
|
||||
kOnErrorOccurred,
|
||||
};
|
||||
|
||||
struct ListenerInfo {
|
||||
URLPatterns url_patterns;
|
||||
AtomNetworkDelegate::Listener callback;
|
||||
enum ResponseEvent {
|
||||
kOnBeforeRequest,
|
||||
kOnBeforeSendHeaders,
|
||||
kOnHeadersReceived,
|
||||
};
|
||||
|
||||
struct BlockingResponse {
|
||||
BlockingResponse() : cancel(false) {}
|
||||
~BlockingResponse() {}
|
||||
struct SimpleListenerInfo {
|
||||
URLPatterns url_patterns;
|
||||
SimpleListener listener;
|
||||
};
|
||||
|
||||
int code() const {
|
||||
return cancel ? net::ERR_BLOCKED_BY_CLIENT : net::OK;
|
||||
}
|
||||
|
||||
bool cancel;
|
||||
GURL redirect_url;
|
||||
net::HttpRequestHeaders request_headers;
|
||||
scoped_refptr<net::HttpResponseHeaders> response_headers;
|
||||
struct ResponseListenerInfo {
|
||||
URLPatterns url_patterns;
|
||||
ResponseListener listener;
|
||||
};
|
||||
|
||||
AtomNetworkDelegate();
|
||||
~AtomNetworkDelegate() override;
|
||||
|
||||
void SetListenerInIO(EventType type,
|
||||
void SetSimpleListenerInIO(SimpleEvent type,
|
||||
const URLPatterns& patterns,
|
||||
const Listener& callback);
|
||||
const SimpleListener& callback);
|
||||
void SetResponseListenerInIO(ResponseEvent type,
|
||||
const URLPatterns& patterns,
|
||||
const ResponseListener& callback);
|
||||
|
||||
protected:
|
||||
// net::NetworkDelegate:
|
||||
|
@ -91,7 +89,8 @@ class AtomNetworkDelegate : public brightray::NetworkDelegate {
|
|||
void OnErrorOccurred(net::URLRequest* request);
|
||||
|
||||
private:
|
||||
std::map<EventType, ListenerInfo> event_listener_map_;
|
||||
std::map<SimpleEvent, SimpleListenerInfo> simple_listeners_;
|
||||
std::map<ResponseEvent, ResponseListenerInfo> response_listeners_;;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(AtomNetworkDelegate);
|
||||
};
|
||||
|
|
|
@ -82,38 +82,4 @@ 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->redirect_url));
|
||||
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->request_headers.SetHeader(it.key(), value);
|
||||
}
|
||||
}
|
||||
base::DictionaryValue response_headers;
|
||||
if (dict.Get("responseHeaders", &response_headers)) {
|
||||
out->response_headers = new net::HttpResponseHeaders("");
|
||||
for (base::DictionaryValue::Iterator it(response_headers);
|
||||
!it.IsAtEnd();
|
||||
it.Advance()) {
|
||||
std::string value;
|
||||
CHECK(it.value().GetAsString(&value));
|
||||
out->response_headers->AddHeader(it.key() + " : " + value);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace mate
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
#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"
|
||||
|
||||
|
@ -35,13 +34,6 @@ 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_
|
||||
|
|
Loading…
Reference in a new issue