Use generic function to handle events
This commit is contained in:
parent
f976e1eda3
commit
9438d42322
3 changed files with 58 additions and 92 deletions
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
#include "atom/browser/api/atom_api_web_request.h"
|
#include "atom/browser/api/atom_api_web_request.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "atom/browser/atom_browser_context.h"
|
#include "atom/browser/atom_browser_context.h"
|
||||||
#include "atom/browser/net/atom_network_delegate.h"
|
#include "atom/browser/net/atom_network_delegate.h"
|
||||||
#include "atom/common/native_mate_converters/callback.h"
|
#include "atom/common/native_mate_converters/callback.h"
|
||||||
|
|
|
@ -243,20 +243,7 @@ int AtomNetworkDelegate::OnBeforeURLRequest(
|
||||||
return brightray::NetworkDelegate::OnBeforeURLRequest(
|
return brightray::NetworkDelegate::OnBeforeURLRequest(
|
||||||
request, callback, new_url);
|
request, callback, new_url);
|
||||||
|
|
||||||
const auto& info = response_listeners_[kOnBeforeRequest];
|
return HandleResponseEvent(kOnBeforeRequest, request, callback, new_url);
|
||||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
|
||||||
return net::OK;
|
|
||||||
|
|
||||||
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
|
||||||
FillDetailsObject(details.get(), request);
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int AtomNetworkDelegate::OnBeforeSendHeaders(
|
int AtomNetworkDelegate::OnBeforeSendHeaders(
|
||||||
|
@ -267,21 +254,8 @@ int AtomNetworkDelegate::OnBeforeSendHeaders(
|
||||||
return brightray::NetworkDelegate::OnBeforeSendHeaders(
|
return brightray::NetworkDelegate::OnBeforeSendHeaders(
|
||||||
request, callback, headers);
|
request, callback, headers);
|
||||||
|
|
||||||
const auto& info = response_listeners_[kOnBeforeSendHeaders];
|
return HandleResponseEvent(
|
||||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
kOnBeforeSendHeaders, request, callback, headers, *headers);
|
||||||
return net::OK;
|
|
||||||
|
|
||||||
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
|
||||||
FillDetailsObject(details.get(), request, *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(
|
void AtomNetworkDelegate::OnSendHeaders(
|
||||||
|
@ -292,44 +266,21 @@ void AtomNetworkDelegate::OnSendHeaders(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto& info = simple_listeners_[kOnSendHeaders];
|
HandleSimpleEvent(kOnSendHeaders, request, headers);
|
||||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
|
||||||
return;
|
|
||||||
|
|
||||||
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
|
||||||
FillDetailsObject(details.get(), request, headers);
|
|
||||||
|
|
||||||
BrowserThread::PostTask(
|
|
||||||
BrowserThread::UI, FROM_HERE,
|
|
||||||
base::Bind(&RunSimpleListener, info.listener, base::Passed(&details)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int AtomNetworkDelegate::OnHeadersReceived(
|
int AtomNetworkDelegate::OnHeadersReceived(
|
||||||
net::URLRequest* request,
|
net::URLRequest* request,
|
||||||
const net::CompletionCallback& callback,
|
const net::CompletionCallback& callback,
|
||||||
const net::HttpResponseHeaders* original_response_headers,
|
const net::HttpResponseHeaders* original,
|
||||||
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
|
scoped_refptr<net::HttpResponseHeaders>* override,
|
||||||
GURL* allowed_unsafe_redirect_url) {
|
GURL* allowed) {
|
||||||
if (!ContainsKey(response_listeners_, kOnHeadersReceived))
|
if (!ContainsKey(response_listeners_, kOnHeadersReceived))
|
||||||
return brightray::NetworkDelegate::OnHeadersReceived(
|
return brightray::NetworkDelegate::OnHeadersReceived(
|
||||||
request, callback, original_response_headers, override_response_headers,
|
request, callback, original, override, allowed);
|
||||||
allowed_unsafe_redirect_url);
|
|
||||||
|
|
||||||
const auto& info = response_listeners_[kOnHeadersReceived];
|
return HandleResponseEvent(
|
||||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
kOnHeadersReceived, request, callback, override, original);
|
||||||
return net::OK;
|
|
||||||
|
|
||||||
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
|
||||||
FillDetailsObject(details.get(), request, original_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,
|
void AtomNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
|
||||||
|
@ -339,18 +290,9 @@ void AtomNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto& info = simple_listeners_[kOnBeforeRedirect];
|
HandleSimpleEvent(kOnBeforeRedirect, request, new_location,
|
||||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
|
||||||
return;
|
|
||||||
|
|
||||||
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
|
||||||
FillDetailsObject(details.get(), request, new_location,
|
|
||||||
request->response_headers(), request->GetSocketAddress(),
|
request->response_headers(), request->GetSocketAddress(),
|
||||||
request->was_cached());
|
request->was_cached());
|
||||||
|
|
||||||
BrowserThread::PostTask(
|
|
||||||
BrowserThread::UI, FROM_HERE,
|
|
||||||
base::Bind(&RunSimpleListener, info.listener, base::Passed(&details)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
|
void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
|
||||||
|
@ -362,17 +304,8 @@ void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
|
||||||
if (request->status().status() != net::URLRequestStatus::SUCCESS)
|
if (request->status().status() != net::URLRequestStatus::SUCCESS)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const auto& info = simple_listeners_[kOnResponseStarted];
|
HandleSimpleEvent(kOnResponseStarted, request, request->response_headers(),
|
||||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
|
||||||
return;
|
|
||||||
|
|
||||||
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
|
||||||
FillDetailsObject(details.get(), request, request->response_headers(),
|
|
||||||
request->was_cached());
|
request->was_cached());
|
||||||
|
|
||||||
BrowserThread::PostTask(
|
|
||||||
BrowserThread::UI, FROM_HERE,
|
|
||||||
base::Bind(RunSimpleListener, info.listener, base::Passed(&details)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AtomNetworkDelegate::OnCompleted(net::URLRequest* request, bool started) {
|
void AtomNetworkDelegate::OnCompleted(net::URLRequest* request, bool started) {
|
||||||
|
@ -397,27 +330,47 @@ void AtomNetworkDelegate::OnCompleted(net::URLRequest* request, bool started) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto& info = simple_listeners_[kOnCompleted];
|
HandleSimpleEvent(kOnCompleted, request, request->response_headers(),
|
||||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
|
||||||
return;
|
|
||||||
|
|
||||||
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
|
||||||
FillDetailsObject(details.get(), request, request->response_headers(),
|
|
||||||
request->was_cached());
|
request->was_cached());
|
||||||
|
|
||||||
BrowserThread::PostTask(
|
|
||||||
BrowserThread::UI, FROM_HERE,
|
|
||||||
base::Bind(RunSimpleListener, info.listener, base::Passed(&details)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AtomNetworkDelegate::OnErrorOccurred(net::URLRequest* request) {
|
void AtomNetworkDelegate::OnErrorOccurred(net::URLRequest* request) {
|
||||||
const auto& info = simple_listeners_[kOnErrorOccurred];
|
HandleSimpleEvent(kOnErrorOccurred, request, request->was_cached(),
|
||||||
|
request->status());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Out, typename... Args>
|
||||||
|
int AtomNetworkDelegate::HandleResponseEvent(
|
||||||
|
ResponseEvent type,
|
||||||
|
net::URLRequest* request,
|
||||||
|
const net::CompletionCallback& callback,
|
||||||
|
Out out,
|
||||||
|
Args... args) {
|
||||||
|
const auto& info = response_listeners_[type];
|
||||||
|
if (!MatchesFilterCondition(request, info.url_patterns))
|
||||||
|
return net::OK;
|
||||||
|
|
||||||
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
||||||
|
FillDetailsObject(details.get(), request, args...);
|
||||||
|
|
||||||
|
ResponseCallback response =
|
||||||
|
base::Bind(OnListenerResultInUI<Out>, callback, out);
|
||||||
|
BrowserThread::PostTask(
|
||||||
|
BrowserThread::UI, FROM_HERE,
|
||||||
|
base::Bind(RunResponseListener, info.listener, base::Passed(&details),
|
||||||
|
response));
|
||||||
|
return net::ERR_IO_PENDING;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename...Args>
|
||||||
|
void AtomNetworkDelegate::HandleSimpleEvent(
|
||||||
|
SimpleEvent type, net::URLRequest* request, Args... args) {
|
||||||
|
const auto& info = simple_listeners_[type];
|
||||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
if (!MatchesFilterCondition(request, info.url_patterns))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue);
|
||||||
FillDetailsObject(details.get(), request, request->was_cached(),
|
FillDetailsObject(details.get(), request, args...);
|
||||||
request->status());
|
|
||||||
|
|
||||||
BrowserThread::PostTask(
|
BrowserThread::PostTask(
|
||||||
BrowserThread::UI, FROM_HERE,
|
BrowserThread::UI, FROM_HERE,
|
||||||
|
|
|
@ -89,6 +89,17 @@ class AtomNetworkDelegate : public brightray::NetworkDelegate {
|
||||||
void OnErrorOccurred(net::URLRequest* request);
|
void OnErrorOccurred(net::URLRequest* request);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
template<typename...Args>
|
||||||
|
void HandleSimpleEvent(SimpleEvent type,
|
||||||
|
net::URLRequest* request,
|
||||||
|
Args... args);
|
||||||
|
template<typename Out, typename... Args>
|
||||||
|
int HandleResponseEvent(ResponseEvent type,
|
||||||
|
net::URLRequest* request,
|
||||||
|
const net::CompletionCallback& callback,
|
||||||
|
Out out,
|
||||||
|
Args... args);
|
||||||
|
|
||||||
std::map<SimpleEvent, SimpleListenerInfo> simple_listeners_;
|
std::map<SimpleEvent, SimpleListenerInfo> simple_listeners_;
|
||||||
std::map<ResponseEvent, ResponseListenerInfo> response_listeners_;;
|
std::map<ResponseEvent, ResponseListenerInfo> response_listeners_;;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue