Make session parameter work with null value

This commit is contained in:
Cheng Zhao 2015-08-12 22:57:25 +08:00
parent 225321b580
commit d9b845fcdf
10 changed files with 55 additions and 52 deletions

View file

@ -62,18 +62,26 @@ class Protocol : public mate::Wrappable {
class CustomProtocolHandler
: public net::URLRequestJobFactory::ProtocolHandler {
public:
CustomProtocolHandler(v8::Isolate* isolate, const Handler& handler)
: isolate_(isolate), handler_(handler) {}
CustomProtocolHandler(
v8::Isolate* isolate,
scoped_refptr<net::URLRequestContextGetter> request_context,
const Handler& handler)
: isolate_(isolate),
request_context_(request_context),
handler_(handler) {}
~CustomProtocolHandler() override {}
net::URLRequestJob* MaybeCreateJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const override {
return new RequestJob(request, network_delegate, isolate_, handler_);
RequestJob* request_job = new RequestJob(request, network_delegate);
request_job->SetHandlerInfo(isolate_, request_context_, handler_);
return request_job;
}
private:
v8::Isolate* isolate_;
scoped_refptr<net::URLRequestContextGetter> request_context_;
Protocol::Handler handler_;
DISALLOW_COPY_AND_ASSIGN(CustomProtocolHandler);
@ -101,7 +109,8 @@ class Protocol : public mate::Wrappable {
if (job_factory_->IsHandledProtocol(scheme))
return PROTOCOL_REGISTERED;
scoped_ptr<CustomProtocolHandler<RequestJob>> protocol_handler(
new CustomProtocolHandler<RequestJob>(isolate(), handler));
new CustomProtocolHandler<RequestJob>(
isolate(), request_context_getter_, handler));
if (job_factory_->SetProtocolHandler(scheme, protocol_handler.Pass()))
return PROTOCOL_OK;
else
@ -135,7 +144,7 @@ class Protocol : public mate::Wrappable {
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
AtomURLRequestJobFactory* job_factory_; // weak ref.
AtomURLRequestJobFactory* job_factory_; // weak ref
DISALLOW_COPY_AND_ASSIGN(Protocol);
};

View file

@ -6,10 +6,12 @@
#define ATOM_BROWSER_NET_JS_ASKER_H_
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/net_errors.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_job.h"
#include "v8/include/v8.h"
@ -37,18 +39,26 @@ bool IsErrorOptions(base::Value* value, int* error);
template<typename RequestJob>
class JsAsker : public RequestJob {
public:
JsAsker(net::URLRequest* request,
net::NetworkDelegate* network_delegate,
v8::Isolate* isolate,
const JavaScriptHandler& handler)
: RequestJob(request, network_delegate),
isolate_(isolate),
handler_(handler),
weak_factory_(this) {}
JsAsker(net::URLRequest* request, net::NetworkDelegate* network_delegate)
: RequestJob(request, network_delegate), weak_factory_(this) {}
// Called by |CustomProtocolHandler| to store handler related information.
void SetHandlerInfo(
v8::Isolate* isolate,
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
const JavaScriptHandler& handler) {
isolate_ = isolate;
request_context_getter_ = request_context_getter;
handler_ = handler;
}
// Subclass should do initailze work here.
virtual void StartAsync(scoped_ptr<base::Value> options) = 0;
net::URLRequestContextGetter* request_context_getter() const {
return request_context_getter_.get();
}
private:
// RequestJob:
void Start() override {
@ -75,7 +85,9 @@ class JsAsker : public RequestJob {
}
v8::Isolate* isolate_;
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
JavaScriptHandler handler_;
base::WeakPtrFactory<JsAsker> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(JsAsker);

View file

@ -8,11 +8,8 @@ namespace atom {
UrlRequestAsyncAsarJob::UrlRequestAsyncAsarJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
v8::Isolate* isolate,
const JavaScriptHandler& handler)
: JsAsker<asar::URLRequestAsarJob>(request, network_delegate, isolate,
handler) {
net::NetworkDelegate* network_delegate)
: JsAsker<asar::URLRequestAsarJob>(request, network_delegate) {
}
void UrlRequestAsyncAsarJob::StartAsync(scoped_ptr<base::Value> options) {

View file

@ -13,10 +13,7 @@ namespace atom {
// Like URLRequestAsarJob, but asks the JavaScript handler for file path.
class UrlRequestAsyncAsarJob : public JsAsker<asar::URLRequestAsarJob> {
public:
UrlRequestAsyncAsarJob(net::URLRequest* request,
net::NetworkDelegate* network_delegate,
v8::Isolate* isolate,
const JavaScriptHandler& handler);
UrlRequestAsyncAsarJob(net::URLRequest*, net::NetworkDelegate*);
// JsAsker:
void StartAsync(scoped_ptr<base::Value> options) override;

View file

@ -11,12 +11,8 @@
namespace atom {
URLRequestBufferJob::URLRequestBufferJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
v8::Isolate* isolate,
const JavaScriptHandler& handler)
: JsAsker<net::URLRequestSimpleJob>(request, network_delegate, isolate,
handler) {
net::URLRequest* request, net::NetworkDelegate* network_delegate)
: JsAsker<net::URLRequestSimpleJob>(request, network_delegate) {
}
void URLRequestBufferJob::StartAsync(scoped_ptr<base::Value> options) {

View file

@ -15,10 +15,7 @@ namespace atom {
class URLRequestBufferJob : public JsAsker<net::URLRequestSimpleJob> {
public:
URLRequestBufferJob(net::URLRequest* request,
net::NetworkDelegate* network_delegate,
v8::Isolate* isolate,
const JavaScriptHandler& handler);
URLRequestBufferJob(net::URLRequest*, net::NetworkDelegate*);
// JsAsker:
void StartAsync(scoped_ptr<base::Value> options) override;

View file

@ -75,11 +75,8 @@ class ResponsePiper : public net::URLFetcherResponseWriter {
} // namespace
URLRequestFetchJob::URLRequestFetchJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
v8::Isolate* isolate,
const JavaScriptHandler& handler)
: JsAsker<net::URLRequestJob>(request, network_delegate, isolate, handler),
net::URLRequest* request, net::NetworkDelegate* network_delegate)
: JsAsker<net::URLRequestJob>(request, network_delegate),
pending_buffer_size_(0) {
}
@ -91,11 +88,13 @@ void URLRequestFetchJob::StartAsync(scoped_ptr<base::Value> options) {
}
std::string url, method, referrer;
base::Value* session = nullptr;
base::DictionaryValue* dict =
static_cast<base::DictionaryValue*>(options.get());
dict->GetString("url", &url);
dict->GetString("method", &method);
dict->GetString("referrer", &referrer);
dict->Get("session", &session);
// Use |request|'s method if |method| is not specified.
net::URLFetcher::RequestType request_type;
@ -105,9 +104,14 @@ void URLRequestFetchJob::StartAsync(scoped_ptr<base::Value> options) {
request_type = GetRequestType(method);
fetcher_ = net::URLFetcher::Create(GURL(url), request_type, this);
fetcher_->SetRequestContext(CreateRequestContext());
fetcher_->SaveResponseWithWriter(make_scoped_ptr(new ResponsePiper(this)));
// When |session| is set to |null| we use a new request context for fetch job.
if (session && session->IsType(base::Value::TYPE_NULL))
fetcher_->SetRequestContext(CreateRequestContext());
else
fetcher_->SetRequestContext(request_context_getter());
// Use |request|'s referrer if |referrer| is not specified.
if (referrer.empty())
fetcher_->SetReferrer(request()->referrer());

View file

@ -19,10 +19,7 @@ class AtomBrowserContext;
class URLRequestFetchJob : public JsAsker<net::URLRequestJob>,
public net::URLFetcherDelegate {
public:
URLRequestFetchJob(net::URLRequest* request,
net::NetworkDelegate* network_delegate,
v8::Isolate* isolate,
const JavaScriptHandler& handler);
URLRequestFetchJob(net::URLRequest*, net::NetworkDelegate*);
// Called by response writer.
void HeadersCompleted();

View file

@ -10,12 +10,9 @@
namespace atom {
URLRequestStringJob::URLRequestStringJob(net::URLRequest* request,
net::NetworkDelegate* network_delegate,
v8::Isolate* isolate,
const JavaScriptHandler& handler)
: JsAsker<net::URLRequestSimpleJob>(request, network_delegate, isolate,
handler) {
URLRequestStringJob::URLRequestStringJob(
net::URLRequest* request, net::NetworkDelegate* network_delegate)
: JsAsker<net::URLRequestSimpleJob>(request, network_delegate) {
}
void URLRequestStringJob::StartAsync(scoped_ptr<base::Value> options) {

View file

@ -14,10 +14,7 @@ namespace atom {
class URLRequestStringJob : public JsAsker<net::URLRequestSimpleJob> {
public:
URLRequestStringJob(net::URLRequest* request,
net::NetworkDelegate* network_delegate,
v8::Isolate* isolate,
const JavaScriptHandler& handler);
URLRequestStringJob(net::URLRequest*, net::NetworkDelegate*);
// JsAsker:
void StartAsync(scoped_ptr<base::Value> options) override;