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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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