fix pointer usage

This commit is contained in:
Robo 2015-12-10 00:46:03 +05:30
parent 461ee49988
commit 658accab94
9 changed files with 150 additions and 151 deletions

View file

@ -30,8 +30,8 @@ template<AtomNetworkDelegate::EventTypes type>
void WebRequest::SetListener(mate::Arguments* args) { void WebRequest::SetListener(mate::Arguments* args) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::DictionaryValue* filter = new base::DictionaryValue(); scoped_ptr<base::DictionaryValue> filter(new base::DictionaryValue());
args->GetNext(filter); args->GetNext(filter.get());
AtomNetworkDelegate::Listener callback; AtomNetworkDelegate::Listener callback;
if (!args->GetNext(&callback)) { if (!args->GetNext(&callback)) {
args->ThrowError("Must pass null or a function"); args->ThrowError("Must pass null or a function");
@ -42,7 +42,7 @@ void WebRequest::SetListener(mate::Arguments* args) {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&AtomNetworkDelegate::SetListenerInIO, base::Bind(&AtomNetworkDelegate::SetListenerInIO,
base::Unretained(delegate), base::Unretained(delegate),
type, filter, callback)); type, base::Passed(&filter), callback));
} }
// static // static
@ -54,7 +54,7 @@ mate::Handle<WebRequest> WebRequest::Create(
// static // static
void WebRequest::BuildPrototype(v8::Isolate* isolate, void WebRequest::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> prototype) { v8::Local<v8::ObjectTemplate> prototype) {
mate::ObjectTemplateBuilder(isolate, prototype) mate::ObjectTemplateBuilder(isolate, prototype)
.SetMethod("onBeforeRequest", .SetMethod("onBeforeRequest",
&WebRequest::SetListener< &WebRequest::SetListener<

View file

@ -9,7 +9,6 @@
#include "atom/browser/api/trackable_object.h" #include "atom/browser/api/trackable_object.h"
#include "atom/browser/net/atom_network_delegate.h" #include "atom/browser/net/atom_network_delegate.h"
#include "base/callback.h"
#include "native_mate/arguments.h" #include "native_mate/arguments.h"
#include "native_mate/handle.h" #include "native_mate/handle.h"
@ -22,7 +21,7 @@ namespace api {
class WebRequest : public mate::TrackableObject<WebRequest> { class WebRequest : public mate::TrackableObject<WebRequest> {
public: public:
static mate::Handle<WebRequest> Create(v8::Isolate* isolate, static mate::Handle<WebRequest> Create(v8::Isolate* isolate,
AtomBrowserContext* browser_context); AtomBrowserContext* browser_context);
// mate::TrackableObject: // mate::TrackableObject:
static void BuildPrototype(v8::Isolate* isolate, static void BuildPrototype(v8::Isolate* isolate,

View file

@ -18,9 +18,9 @@ namespace {
std::string ResourceTypeToString(content::ResourceType type) { std::string ResourceTypeToString(content::ResourceType type) {
switch (type) { switch (type) {
case content::RESOURCE_TYPE_MAIN_FRAME: case content::RESOURCE_TYPE_MAIN_FRAME:
return "main_frame"; return "mainFrame";
case content::RESOURCE_TYPE_SUB_FRAME: case content::RESOURCE_TYPE_SUB_FRAME:
return "sub_frame"; return "subFrame";
case content::RESOURCE_TYPE_STYLESHEET: case content::RESOURCE_TYPE_STYLESHEET:
return "stylesheet"; return "stylesheet";
case content::RESOURCE_TYPE_SCRIPT: case content::RESOURCE_TYPE_SCRIPT:
@ -30,12 +30,18 @@ std::string ResourceTypeToString(content::ResourceType type) {
case content::RESOURCE_TYPE_OBJECT: case content::RESOURCE_TYPE_OBJECT:
return "object"; return "object";
case content::RESOURCE_TYPE_XHR: case content::RESOURCE_TYPE_XHR:
return "xmlhttprequest"; return "xhr";
default: default:
return "other"; return "other";
} }
} }
AtomNetworkDelegate::BlockingResponse RunListener(
const AtomNetworkDelegate::Listener& callback,
scoped_ptr<base::DictionaryValue> details) {
return callback.Run(*(details.get()));
}
bool MatchesFilterCondition( bool MatchesFilterCondition(
net::URLRequest* request, net::URLRequest* request,
const AtomNetworkDelegate::ListenerInfo& info) { const AtomNetworkDelegate::ListenerInfo& info) {
@ -50,8 +56,8 @@ bool MatchesFilterCondition(
return true; return true;
} }
base::DictionaryValue* ExtractRequestInfo(net::URLRequest* request) { scoped_ptr<base::DictionaryValue> ExtractRequestInfo(net::URLRequest* request) {
base::DictionaryValue* dict = new base::DictionaryValue(); scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
dict->SetInteger("id", request->identifier()); dict->SetInteger("id", request->identifier());
dict->SetString("url", request->url().spec()); dict->SetString("url", request->url().spec());
dict->SetString("method", request->method()); dict->SetString("method", request->method());
@ -62,21 +68,21 @@ base::DictionaryValue* ExtractRequestInfo(net::URLRequest* request) {
dict->SetString("resourceType", ResourceTypeToString(resourceType)); dict->SetString("resourceType", ResourceTypeToString(resourceType));
dict->SetDouble("timestamp", base::Time::Now().ToDoubleT() * 1000); dict->SetDouble("timestamp", base::Time::Now().ToDoubleT() * 1000);
return dict; return dict.Pass();
} }
base::DictionaryValue* GetRequestHeadersDict( scoped_ptr<base::DictionaryValue> GetRequestHeadersDict(
const net::HttpRequestHeaders& headers) { const net::HttpRequestHeaders& headers) {
base::DictionaryValue* header_dict = new base::DictionaryValue(); scoped_ptr<base::DictionaryValue> header_dict(new base::DictionaryValue());
net::HttpRequestHeaders::Iterator it(headers); net::HttpRequestHeaders::Iterator it(headers);
while (it.GetNext()) while (it.GetNext())
header_dict->SetString(it.name(), it.value()); header_dict->SetString(it.name(), it.value());
return header_dict; return header_dict.Pass();
} }
base::DictionaryValue* GetResponseHeadersDict( scoped_ptr<base::DictionaryValue> GetResponseHeadersDict(
const net::HttpResponseHeaders* headers) { const net::HttpResponseHeaders* headers) {
base::DictionaryValue* header_dict = new base::DictionaryValue(); scoped_ptr<base::DictionaryValue> header_dict(new base::DictionaryValue());
if (headers) { if (headers) {
void* iter = nullptr; void* iter = nullptr;
std::string key; std::string key;
@ -84,25 +90,25 @@ base::DictionaryValue* GetResponseHeadersDict(
while (headers->EnumerateHeaderLines(&iter, &key, &value)) while (headers->EnumerateHeaderLines(&iter, &key, &value))
header_dict->SetString(key, value); header_dict->SetString(key, value);
} }
return header_dict; return header_dict.Pass();
} }
void OnBeforeURLRequestResponse( void OnBeforeURLRequestResponse(
const net::CompletionCallback& callback, const net::CompletionCallback& callback,
GURL* new_url, GURL* new_url,
const AtomNetworkDelegate::BlockingResponse& result) { const AtomNetworkDelegate::BlockingResponse& result) {
if (!result.redirectURL.is_empty()) if (!result.redirect_url.is_empty())
*new_url = result.redirectURL; *new_url = result.redirect_url;
callback.Run(result.Cancel()); callback.Run(result.Code());
} }
void OnBeforeSendHeadersResponse( void OnBeforeSendHeadersResponse(
const net::CompletionCallback& callback, const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers, net::HttpRequestHeaders* headers,
const AtomNetworkDelegate::BlockingResponse& result) { const AtomNetworkDelegate::BlockingResponse& result) {
if (!result.requestHeaders.IsEmpty()) if (!result.request_headers.IsEmpty())
*headers = result.requestHeaders; *headers = result.request_headers;
callback.Run(result.Cancel()); callback.Run(result.Code());
} }
void OnHeadersReceivedResponse( void OnHeadersReceivedResponse(
@ -110,26 +116,22 @@ void OnHeadersReceivedResponse(
const net::HttpResponseHeaders* original_response_headers, const net::HttpResponseHeaders* original_response_headers,
scoped_refptr<net::HttpResponseHeaders>* override_response_headers, scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
const AtomNetworkDelegate::BlockingResponse& result) { const AtomNetworkDelegate::BlockingResponse& result) {
if (result.responseHeaders.get()) { if (result.response_headers.get()) {
*override_response_headers = new net::HttpResponseHeaders( *override_response_headers = new net::HttpResponseHeaders(
original_response_headers->raw_headers()); original_response_headers->raw_headers());
void* iter = nullptr; void* iter = nullptr;
std::string key; std::string key;
std::string value; std::string value;
while (result.responseHeaders->EnumerateHeaderLines(&iter, &key, &value)) { while (result.response_headers->EnumerateHeaderLines(&iter, &key, &value)) {
(*override_response_headers)->RemoveHeader(key); (*override_response_headers)->RemoveHeader(key);
(*override_response_headers)->AddHeader(key + ": " + value); (*override_response_headers)->AddHeader(key + ": " + value);
} }
} }
callback.Run(result.Cancel()); callback.Run(result.Code());
} }
} // namespace } // namespace
// static
std::map<AtomNetworkDelegate::EventTypes, AtomNetworkDelegate::ListenerInfo>
AtomNetworkDelegate::event_listener_map_;
AtomNetworkDelegate::AtomNetworkDelegate() { AtomNetworkDelegate::AtomNetworkDelegate() {
} }
@ -138,8 +140,13 @@ AtomNetworkDelegate::~AtomNetworkDelegate() {
void AtomNetworkDelegate::SetListenerInIO( void AtomNetworkDelegate::SetListenerInIO(
EventTypes type, EventTypes type,
const base::DictionaryValue* filter, scoped_ptr<base::DictionaryValue> filter,
const Listener& callback) { const Listener& callback) {
if (callback.is_null()) {
event_listener_map_.erase(type);
return;
}
ListenerInfo info; ListenerInfo info;
info.callback = callback; info.callback = callback;
@ -162,69 +169,71 @@ int AtomNetworkDelegate::OnBeforeURLRequest(
net::URLRequest* request, net::URLRequest* request,
const net::CompletionCallback& callback, const net::CompletionCallback& callback,
GURL* new_url) { GURL* new_url) {
brightray::NetworkDelegate::OnBeforeURLRequest(request, callback, new_url); auto listener_info = event_listener_map_.find(kOnBeforeRequest);
if (listener_info != event_listener_map_.end()) {
if (!MatchesFilterCondition(request, listener_info->second))
return net::OK;
auto listener_info = event_listener_map_[kOnBeforeRequest]; auto wrapped_callback = listener_info->second.callback;
if (!MatchesFilterCondition(request, listener_info))
return net::OK;
if (!listener_info.callback.is_null()) {
auto wrapped_callback = listener_info.callback;
auto details = ExtractRequestInfo(request); auto details = ExtractRequestInfo(request);
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE, BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
base::Bind(wrapped_callback, details), base::Bind(&RunListener, wrapped_callback, base::Passed(&details)),
base::Bind(&OnBeforeURLRequestResponse, base::Bind(&OnBeforeURLRequestResponse,
callback, new_url)); callback, new_url));
return net::ERR_IO_PENDING; return net::ERR_IO_PENDING;
} }
return net::OK; return brightray::NetworkDelegate::OnBeforeURLRequest(request,
callback,
new_url);
} }
int AtomNetworkDelegate::OnBeforeSendHeaders( int AtomNetworkDelegate::OnBeforeSendHeaders(
net::URLRequest* request, net::URLRequest* request,
const net::CompletionCallback& callback, const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) { net::HttpRequestHeaders* headers) {
auto listener_info = event_listener_map_[kOnBeforeSendHeaders]; auto listener_info = event_listener_map_.find(kOnBeforeSendHeaders);
if (listener_info != event_listener_map_.end()) {
if (!MatchesFilterCondition(request, listener_info->second))
return net::OK;
if (!MatchesFilterCondition(request, listener_info)) auto wrapped_callback = listener_info->second.callback;
return net::OK;
if (!listener_info.callback.is_null()) {
auto wrapped_callback = listener_info.callback;
auto details = ExtractRequestInfo(request); auto details = ExtractRequestInfo(request);
details->Set("requestHeaders", GetRequestHeadersDict(*headers)); details->Set("requestHeaders", GetRequestHeadersDict(*headers).release());
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE, BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
base::Bind(wrapped_callback, details), base::Bind(&RunListener, wrapped_callback, base::Passed(&details)),
base::Bind(&OnBeforeSendHeadersResponse, base::Bind(&OnBeforeSendHeadersResponse,
callback, headers)); callback, headers));
return net::ERR_IO_PENDING; return net::ERR_IO_PENDING;
} }
return net::OK; return brightray::NetworkDelegate::OnBeforeSendHeaders(request,
callback,
headers);
} }
void AtomNetworkDelegate::OnSendHeaders( void AtomNetworkDelegate::OnSendHeaders(
net::URLRequest* request, net::URLRequest* request,
const net::HttpRequestHeaders& headers) { const net::HttpRequestHeaders& headers) {
auto listener_info = event_listener_map_[kOnSendHeaders]; auto listener_info = event_listener_map_.find(kOnSendHeaders);
if (listener_info != event_listener_map_.end()) {
if (!MatchesFilterCondition(request, listener_info->second))
return;
if (!MatchesFilterCondition(request, listener_info)) auto wrapped_callback = listener_info->second.callback;
return;
if (!listener_info.callback.is_null()) {
auto wrapped_callback = listener_info.callback;
auto details = ExtractRequestInfo(request); auto details = ExtractRequestInfo(request);
details->Set("requestHeaders", GetRequestHeadersDict(headers)); details->Set("requestHeaders", GetRequestHeadersDict(headers).release());
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(base::IgnoreResult(wrapped_callback), base::Bind(base::IgnoreResult(&RunListener),
details)); wrapped_callback,
base::Passed(&details)));
} else {
brightray::NetworkDelegate::OnSendHeaders(request, headers);
} }
} }
@ -234,23 +243,22 @@ int AtomNetworkDelegate::OnHeadersReceived(
const net::HttpResponseHeaders* original_response_headers, const net::HttpResponseHeaders* original_response_headers,
scoped_refptr<net::HttpResponseHeaders>* override_response_headers, scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
GURL* allowed_unsafe_redirect_url) { GURL* allowed_unsafe_redirect_url) {
auto listener_info = event_listener_map_[kOnHeadersReceived]; auto listener_info = event_listener_map_.find(kOnHeadersReceived);
if (listener_info != event_listener_map_.end()) {
if (!MatchesFilterCondition(request, listener_info->second))
return net::OK;
if (!MatchesFilterCondition(request, listener_info)) auto wrapped_callback = listener_info->second.callback;
return net::OK;
if (!listener_info.callback.is_null()) {
auto wrapped_callback = listener_info.callback;
auto details = ExtractRequestInfo(request); auto details = ExtractRequestInfo(request);
details->SetString("statusLine", details->SetString("statusLine",
original_response_headers->GetStatusLine()); original_response_headers->GetStatusLine());
details->SetInteger("statusCode", details->SetInteger("statusCode",
original_response_headers->response_code()); original_response_headers->response_code());
details->Set("responseHeaders", details->Set("responseHeaders",
GetResponseHeadersDict(original_response_headers)); GetResponseHeadersDict(original_response_headers).release());
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE, BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
base::Bind(wrapped_callback, details), base::Bind(&RunListener, wrapped_callback, base::Passed(&details)),
base::Bind(&OnHeadersReceivedResponse, base::Bind(&OnHeadersReceivedResponse,
callback, callback,
original_response_headers, original_response_headers,
@ -259,18 +267,19 @@ int AtomNetworkDelegate::OnHeadersReceived(
return net::ERR_IO_PENDING; return net::ERR_IO_PENDING;
} }
return net::OK; return brightray::NetworkDelegate::OnHeadersReceived(
request, callback, original_response_headers, override_response_headers,
allowed_unsafe_redirect_url);
} }
void AtomNetworkDelegate::OnBeforeRedirect(net::URLRequest* request, void AtomNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
const GURL& new_location) { const GURL& new_location) {
auto listener_info = event_listener_map_[kOnBeforeRedirect]; auto listener_info = event_listener_map_.find(kOnBeforeRedirect);
if (listener_info != event_listener_map_.end()) {
if (!MatchesFilterCondition(request, listener_info->second))
return;
if (!MatchesFilterCondition(request, listener_info)) auto wrapped_callback = listener_info->second.callback;
return;
if (!listener_info.callback.is_null()) {
auto wrapped_callback = listener_info.callback;
auto details = ExtractRequestInfo(request); auto details = ExtractRequestInfo(request);
details->SetString("redirectURL", new_location.spec()); details->SetString("redirectURL", new_location.spec());
details->SetInteger("statusCode", request->GetResponseCode()); details->SetInteger("statusCode", request->GetResponseCode());
@ -279,28 +288,30 @@ void AtomNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
details->SetString("ip", ip); details->SetString("ip", ip);
details->SetBoolean("fromCache", request->was_cached()); details->SetBoolean("fromCache", request->was_cached());
details->Set("responseHeaders", details->Set("responseHeaders",
GetResponseHeadersDict(request->response_headers())); GetResponseHeadersDict(request->response_headers()).release());
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(base::IgnoreResult(wrapped_callback), base::Bind(base::IgnoreResult(&RunListener),
details)); wrapped_callback,
base::Passed(&details)));
} else {
brightray::NetworkDelegate::OnBeforeRedirect(request, new_location);
} }
} }
void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request) { void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
if (request->status().status() != net::URLRequestStatus::SUCCESS) auto listener_info = event_listener_map_.find(kOnResponseStarted);
return; if (listener_info != event_listener_map_.end()) {
if (request->status().status() != net::URLRequestStatus::SUCCESS)
return;
auto listener_info = event_listener_map_[kOnResponseStarted]; if (!MatchesFilterCondition(request, listener_info->second))
return;
if (!MatchesFilterCondition(request, listener_info)) auto wrapped_callback = listener_info->second.callback;
return;
if (!listener_info.callback.is_null()) {
auto wrapped_callback = listener_info.callback;
auto details = ExtractRequestInfo(request); auto details = ExtractRequestInfo(request);
details->Set("responseHeaders", details->Set("responseHeaders",
GetResponseHeadersDict(request->response_headers())); GetResponseHeadersDict(request->response_headers()).release());
details->SetBoolean("fromCache", request->was_cached()); details->SetBoolean("fromCache", request->was_cached());
auto response_headers = request->response_headers(); auto response_headers = request->response_headers();
@ -312,34 +323,36 @@ void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
response_headers->GetStatusLine() : std::string()); response_headers->GetStatusLine() : std::string());
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(base::IgnoreResult(wrapped_callback), base::Bind(base::IgnoreResult(&RunListener),
details)); wrapped_callback,
base::Passed(&details)));
} else {
brightray::NetworkDelegate::OnResponseStarted(request);
} }
} }
void AtomNetworkDelegate::OnCompleted(net::URLRequest* request, bool started) { void AtomNetworkDelegate::OnCompleted(net::URLRequest* request, bool started) {
if (request->status().status() == net::URLRequestStatus::FAILED || auto listener_info = event_listener_map_.find(kOnCompleted);
request->status().status() == net::URLRequestStatus::CANCELED) { if (listener_info != event_listener_map_.end()) {
OnErrorOccurred(request); if (request->status().status() == net::URLRequestStatus::FAILED ||
return; request->status().status() == net::URLRequestStatus::CANCELED) {
} else { OnErrorOccurred(request);
bool is_redirect = request->response_headers() &&
net::HttpResponseHeaders::IsRedirectResponseCode(
request->response_headers()->response_code());
if (is_redirect)
return; return;
} } else {
bool is_redirect = request->response_headers() &&
net::HttpResponseHeaders::IsRedirectResponseCode(
request->response_headers()->response_code());
if (is_redirect)
return;
}
auto listener_info = event_listener_map_[kOnCompleted]; if (!MatchesFilterCondition(request, listener_info->second))
return;
if (!MatchesFilterCondition(request, listener_info)) auto wrapped_callback = listener_info->second.callback;
return;
if (!listener_info.callback.is_null()) {
auto wrapped_callback = listener_info.callback;
auto details = ExtractRequestInfo(request); auto details = ExtractRequestInfo(request);
details->Set("responseHeaders", details->Set("responseHeaders",
GetResponseHeadersDict(request->response_headers())); GetResponseHeadersDict(request->response_headers()).release());
details->SetBoolean("fromCache", request->was_cached()); details->SetBoolean("fromCache", request->was_cached());
auto response_headers = request->response_headers(); auto response_headers = request->response_headers();
@ -351,26 +364,29 @@ void AtomNetworkDelegate::OnCompleted(net::URLRequest* request, bool started) {
response_headers->GetStatusLine() : std::string()); response_headers->GetStatusLine() : std::string());
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(base::IgnoreResult(wrapped_callback), base::Bind(base::IgnoreResult(&RunListener),
details)); wrapped_callback,
base::Passed(&details)));
} else {
brightray::NetworkDelegate::OnCompleted(request, started);
} }
} }
void AtomNetworkDelegate::OnErrorOccurred(net::URLRequest* request) { void AtomNetworkDelegate::OnErrorOccurred(net::URLRequest* request) {
auto listener_info = event_listener_map_[kOnErrorOccurred]; auto listener_info = event_listener_map_.find(kOnErrorOccurred);
if (listener_info != event_listener_map_.end()) {
if (!MatchesFilterCondition(request, listener_info->second))
return;
if (!MatchesFilterCondition(request, listener_info)) auto wrapped_callback = listener_info->second.callback;
return;
if (!listener_info.callback.is_null()) {
auto wrapped_callback = listener_info.callback;
auto details = ExtractRequestInfo(request); auto details = ExtractRequestInfo(request);
details->SetBoolean("fromCache", request->was_cached()); details->SetBoolean("fromCache", request->was_cached());
details->SetString("error", net::ErrorToString(request->status().error())); details->SetString("error", net::ErrorToString(request->status().error()));
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(base::IgnoreResult(wrapped_callback), base::Bind(base::IgnoreResult(&RunListener),
details)); wrapped_callback,
base::Passed(&details)));
} }
} }

View file

@ -27,7 +27,7 @@ class AtomNetworkDelegate : public brightray::NetworkDelegate {
public: public:
struct BlockingResponse; struct BlockingResponse;
using Listener = using Listener =
base::Callback<BlockingResponse(const base::DictionaryValue*)>; base::Callback<BlockingResponse(const base::DictionaryValue&)>;
enum EventTypes { enum EventTypes {
kInvalidEvent = 0, kInvalidEvent = 0,
@ -42,35 +42,34 @@ class AtomNetworkDelegate : public brightray::NetworkDelegate {
}; };
struct ListenerInfo { struct ListenerInfo {
ListenerInfo() {}
~ListenerInfo() {}
std::set<extensions::URLPattern> url_patterns; std::set<extensions::URLPattern> url_patterns;
AtomNetworkDelegate::Listener callback; AtomNetworkDelegate::Listener callback;
}; };
struct BlockingResponse { struct BlockingResponse {
BlockingResponse() {} BlockingResponse() : cancel(false) {}
~BlockingResponse() {} ~BlockingResponse() {}
int Cancel() const { int Code() const {
return cancel ? net::ERR_BLOCKED_BY_CLIENT : net::OK; return cancel ? net::ERR_BLOCKED_BY_CLIENT : net::OK;
} }
bool cancel; bool cancel;
GURL redirectURL; GURL redirect_url;
net::HttpRequestHeaders requestHeaders; net::HttpRequestHeaders request_headers;
scoped_refptr<net::HttpResponseHeaders> responseHeaders; scoped_refptr<net::HttpResponseHeaders> response_headers;
}; };
AtomNetworkDelegate(); AtomNetworkDelegate();
~AtomNetworkDelegate() override; ~AtomNetworkDelegate() override;
void SetListenerInIO(EventTypes type, void SetListenerInIO(EventTypes type,
const base::DictionaryValue* filter, scoped_ptr<base::DictionaryValue> filter,
const Listener& callback); const Listener& callback);
protected: protected:
void OnErrorOccurred(net::URLRequest* request);
// net::NetworkDelegate: // net::NetworkDelegate:
int OnBeforeURLRequest(net::URLRequest* request, int OnBeforeURLRequest(net::URLRequest* request,
const net::CompletionCallback& callback, const net::CompletionCallback& callback,
@ -91,10 +90,8 @@ class AtomNetworkDelegate : public brightray::NetworkDelegate {
void OnResponseStarted(net::URLRequest* request) override; void OnResponseStarted(net::URLRequest* request) override;
void OnCompleted(net::URLRequest* request, bool started) override; void OnCompleted(net::URLRequest* request, bool started) override;
void OnErrorOccurred(net::URLRequest* request);
private: private:
static std::map<EventTypes, ListenerInfo> event_listener_map_; std::map<EventTypes, ListenerInfo> event_listener_map_;
DISALLOW_COPY_AND_ASSIGN(AtomNetworkDelegate); DISALLOW_COPY_AND_ASSIGN(AtomNetworkDelegate);
}; };

View file

@ -62,7 +62,7 @@ bool Converter<atom::AtomNetworkDelegate::BlockingResponse>::FromV8(
return false; return false;
if (!dict.Get("cancel", &(out->cancel))) if (!dict.Get("cancel", &(out->cancel)))
return false; return false;
dict.Get("redirectURL", &(out->redirectURL)); dict.Get("redirectURL", &(out->redirect_url));
base::DictionaryValue request_headers; base::DictionaryValue request_headers;
if (dict.Get("requestHeaders", &request_headers)) { if (dict.Get("requestHeaders", &request_headers)) {
for (base::DictionaryValue::Iterator it(request_headers); for (base::DictionaryValue::Iterator it(request_headers);
@ -70,18 +70,18 @@ bool Converter<atom::AtomNetworkDelegate::BlockingResponse>::FromV8(
it.Advance()) { it.Advance()) {
std::string value; std::string value;
CHECK(it.value().GetAsString(&value)); CHECK(it.value().GetAsString(&value));
out->requestHeaders.SetHeader(it.key(), value); out->request_headers.SetHeader(it.key(), value);
} }
} }
base::DictionaryValue response_headers; base::DictionaryValue response_headers;
if (dict.Get("responseHeaders", &response_headers)) { if (dict.Get("responseHeaders", &response_headers)) {
out->responseHeaders = new net::HttpResponseHeaders(""); out->response_headers = new net::HttpResponseHeaders("");
for (base::DictionaryValue::Iterator it(response_headers); for (base::DictionaryValue::Iterator it(response_headers);
!it.IsAtEnd(); !it.IsAtEnd();
it.Advance()) { it.Advance()) {
std::string value; std::string value;
CHECK(it.value().GetAsString(&value)); CHECK(it.value().GetAsString(&value));
out->responseHeaders->AddHeader(it.key() + " : " + value); out->response_headers->AddHeader(it.key() + " : " + value);
} }
} }
return true; return true;

View file

@ -30,13 +30,6 @@ v8::Local<v8::Value> Converter<base::DictionaryValue>::ToV8(
return converter->ToV8Value(&val, isolate->GetCurrentContext()); 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, bool Converter<base::ListValue>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val, v8::Local<v8::Value> val,
base::ListValue* out) { base::ListValue* out) {

View file

@ -23,12 +23,6 @@ struct Converter<base::DictionaryValue> {
const base::DictionaryValue& val); const base::DictionaryValue& val);
}; };
template<>
struct Converter<const base::DictionaryValue*> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::DictionaryValue* val);
};
template<> template<>
struct Converter<base::ListValue> { struct Converter<base::ListValue> {
static bool FromV8(v8::Isolate* isolate, static bool FromV8(v8::Isolate* isolate,

View file

@ -250,7 +250,7 @@ URLPattern::ParseResult URLPattern::Parse(const std::string& pattern) {
host_components.erase(host_components.begin(), host_components.erase(host_components.begin(),
host_components.begin() + 1); host_components.begin() + 1);
} }
host_ = JoinString(host_components, "."); host_ = base::JoinString(host_components, ".");
path_start_pos = host_end_pos; path_start_pos = host_end_pos;
} }

View file

@ -367,7 +367,7 @@ Fired just before a request is going to be sent to the server, modifications of
* `filter` Object * `filter` Object
* `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs * `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs
will be filtered out. will be filtered out.
* `listener` Function * `listener` Function
* `details` Object * `details` Object
* `id` String - Request id. * `id` String - Request id.
@ -390,7 +390,7 @@ Fired when HTTP response headers of a request have been received. Should return
* `filter` Object * `filter` Object
* `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs * `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs
will be filtered out. will be filtered out.
* `listener` Function * `listener` Function
* `details` Object * `details` Object
* `id` String - Request id. * `id` String - Request id.
@ -410,7 +410,7 @@ status line and response headers are available.
* `filter` Object * `filter` Object
* `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs * `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs
will be filtered out. will be filtered out.
* `listener` Function * `listener` Function
* `details` Object * `details` Object
* `id` String - Request id. * `id` String - Request id.
@ -420,7 +420,7 @@ status line and response headers are available.
* `timestamp` Double * `timestamp` Double
* `redirectURL` String * `redirectURL` String
* `statusCode` Integer * `statusCode` Integer
* `ip` String - The server IP address that the request was actually sent to. * `ip` String **optional** - The server IP address that the request was actually sent to.
* `fromCache` Boolean * `fromCache` Boolean
* `responseHeaders` Object * `responseHeaders` Object