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

View file

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

View file

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

View file

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

View file

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

View file

@ -30,13 +30,6 @@ 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) {

View file

@ -23,12 +23,6 @@ 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,

View file

@ -250,7 +250,7 @@ URLPattern::ParseResult URLPattern::Parse(const std::string& pattern) {
host_components.erase(host_components.begin(),
host_components.begin() + 1);
}
host_ = JoinString(host_components, ".");
host_ = base::JoinString(host_components, ".");
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
* `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
* `details` Object
* `id` String - Request id.
@ -390,7 +390,7 @@ Fired when HTTP response headers of a request have been received. Should return
* `filter` Object
* `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
* `details` Object
* `id` String - Request id.
@ -410,7 +410,7 @@ status line and response headers are available.
* `filter` Object
* `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
* `details` Object
* `id` String - Request id.
@ -420,7 +420,7 @@ status line and response headers are available.
* `timestamp` Double
* `redirectURL` String
* `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
* `responseHeaders` Object