use request context from session if provided

This commit is contained in:
Robo 2015-07-15 18:05:38 +05:30
parent 8eb87c5d2b
commit 57244e4718
9 changed files with 46 additions and 8 deletions

View file

@ -7,6 +7,7 @@
#include "atom/browser/atom_browser_client.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/api/atom_api_session.h"
#include "atom/browser/net/adapter_request_job.h"
#include "atom/browser/net/atom_url_request_job_factory.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
@ -34,6 +35,23 @@ struct Converter<const net::URLRequest*> {
}
};
template<>
struct Converter<net::URLRequestContextGetter*> {
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
net::URLRequestContextGetter** out) {
if (val->IsNull() || val->IsUndefined()) {
*out = nullptr;
return true;
}
atom::api::Session* session;
if (!Converter<atom::api::Session*>::FromV8(isolate, val, &session))
return false;
*out = session->GetBrowserContext()->GetRequestContext();
return true;
}
};
} // namespace mate
namespace atom {
@ -126,13 +144,15 @@ class CustomProtocolRequestJob : public AdapterRequestJob {
} else if (name == "RequestHttpJob") {
GURL url;
std::string method, referrer;
net::URLRequestContextGetter* getter;
dict.Get("url", &url);
dict.Get("method", &method);
dict.Get("referrer", &referrer);
dict.Get("session", &getter);
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&AdapterRequestJob::CreateHttpJobAndStart, GetWeakPtr(),
url, method, referrer));
base::Unretained(getter), url, method, referrer));
return;
}
}

View file

@ -203,6 +203,10 @@ Session::Session(AtomBrowserContext* browser_context)
Session::~Session() {
}
AtomBrowserContext* Session::GetBrowserContext() const {
return browser_context_;
}
void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
new ResolveProxyHelper(browser_context_, url, callback);
}

View file

@ -31,6 +31,8 @@ class Session: public mate::TrackableObject<Session> {
static mate::Handle<Session> CreateFrom(
v8::Isolate* isolate, AtomBrowserContext* browser_context);
AtomBrowserContext* GetBrowserContext() const;
protected:
explicit Session(AtomBrowserContext* browser_context);
~Session();

View file

@ -59,6 +59,6 @@ class RequestErrorJob
protocol.RequestHttpJob =
class RequestHttpJob
constructor: ({@url, @method, @referrer}) ->
constructor: ({@session, @url, @method, @referrer}) ->
module.exports = protocol

View file

@ -114,6 +114,7 @@ void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) {
}
void AdapterRequestJob::CreateHttpJobAndStart(
net::URLRequestContextGetter* request_context_getter,
const GURL& url,
const std::string& method,
const std::string& referrer) {
@ -122,7 +123,7 @@ void AdapterRequestJob::CreateHttpJobAndStart(
return;
}
real_job_ = new URLRequestFetchJob(request(),
real_job_ = new URLRequestFetchJob(request_context_getter, request(),
network_delegate(), url, method, referrer);
real_job_->Start();
}

View file

@ -10,6 +10,7 @@
#include "base/memory/ref_counted_memory.h"
#include "base/memory/weak_ptr.h"
#include "net/url_request/url_request_context.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_factory.h"
#include "v8/include/v8.h"
@ -61,7 +62,9 @@ class AdapterRequestJob : public net::URLRequestJob {
const std::string& charset,
scoped_refptr<base::RefCountedBytes> data);
void CreateFileJobAndStart(const base::FilePath& path);
void CreateHttpJobAndStart(const GURL& url,
void CreateHttpJobAndStart(
net::URLRequestContextGetter* request_context_getter,
const GURL& url,
const std::string& method,
const std::string& referrer);
void CreateJobFromProtocolHandlerAndStart();

View file

@ -75,6 +75,7 @@ class ResponsePiper : public net::URLFetcherResponseWriter {
} // namespace
URLRequestFetchJob::URLRequestFetchJob(
net::URLRequestContextGetter* request_context_getter,
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const GURL& url,
@ -90,7 +91,12 @@ URLRequestFetchJob::URLRequestFetchJob(
request_type = GetRequestType(method);
fetcher_.reset(net::URLFetcher::Create(url, request_type, this));
// Use request context if provided else create one.
if (request_context_getter)
fetcher_->SetRequestContext(request_context_getter);
else
fetcher_->SetRequestContext(GetRequestContext());
fetcher_->SaveResponseWithWriter(make_scoped_ptr(new ResponsePiper(this)));
// Use |request|'s referrer if |referrer| is not specified.

View file

@ -18,7 +18,8 @@ class AtomBrowserContext;
class URLRequestFetchJob : public net::URLRequestJob,
public net::URLFetcherDelegate {
public:
URLRequestFetchJob(net::URLRequest* request,
URLRequestFetchJob(net::URLRequestContextGetter* request_context_getter,
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const GURL& url,
const std::string& method,

View file

@ -101,6 +101,7 @@ Create a request job which sends a buffer as response.
## Class: protocol.RequestHttpJob(options)
* `options` Object
* `session` [Session](browser-window.md#class-session)
* `url` String
* `method` String - Default is `GET`
* `referrer` String