Merge pull request #2207 from deepak1556/fetch_job_patch

protocol: create separate request context for fetch job
This commit is contained in:
Cheng Zhao 2015-07-16 15:14:45 -07:00
commit 3145c78b61
8 changed files with 58 additions and 13 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()) {
*out = nullptr;
return true;
}
atom::api::Session* session;
if (!Converter<atom::api::Session*>::FromV8(isolate, val, &session))
return false;
*out = session->browser_context()->GetRequestContext();
return true;
}
};
} // namespace mate
namespace atom {
@ -126,13 +144,16 @@ class CustomProtocolRequestJob : public AdapterRequestJob {
} else if (name == "RequestHttpJob") {
GURL url;
std::string method, referrer;
net::URLRequestContextGetter* getter =
registry_->browser_context()->GetRequestContext();
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(),
registry_->browser_context(), url, method, referrer));
base::Unretained(getter), url, method, referrer));
return;
}
}

View file

@ -31,6 +31,8 @@ class Session: public mate::TrackableObject<Session> {
static mate::Handle<Session> CreateFrom(
v8::Isolate* isolate, AtomBrowserContext* browser_context);
AtomBrowserContext* browser_context() const { return browser_context_; }
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

@ -4,7 +4,6 @@
#include "atom/browser/net/adapter_request_job.h"
#include "atom/browser/atom_browser_context.h"
#include "base/threading/sequenced_worker_pool.h"
#include "atom/browser/net/url_request_buffer_job.h"
#include "atom/browser/net/url_request_fetch_job.h"
@ -115,7 +114,7 @@ void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) {
}
void AdapterRequestJob::CreateHttpJobAndStart(
AtomBrowserContext* browser_context,
net::URLRequestContextGetter* request_context_getter,
const GURL& url,
const std::string& method,
const std::string& referrer) {
@ -124,7 +123,7 @@ void AdapterRequestJob::CreateHttpJobAndStart(
return;
}
real_job_ = new URLRequestFetchJob(browser_context, 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,10 +62,11 @@ class AdapterRequestJob : public net::URLRequestJob {
const std::string& charset,
scoped_refptr<base::RefCountedBytes> data);
void CreateFileJobAndStart(const base::FilePath& path);
void CreateHttpJobAndStart(AtomBrowserContext* browser_context,
const GURL& url,
const std::string& method,
const std::string& referrer);
void CreateHttpJobAndStart(
net::URLRequestContextGetter* request_context_getter,
const GURL& url,
const std::string& method,
const std::string& referrer);
void CreateJobFromProtocolHandlerAndStart();
private:

View file

@ -7,13 +7,14 @@
#include <algorithm>
#include <string>
#include "atom/browser/atom_browser_context.h"
#include "base/strings/string_util.h"
#include "base/thread_task_runner_handle.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_response_writer.h"
#include "net/url_request/url_request_context_builder.h"
#include "net/url_request/url_request_status.h"
namespace atom {
@ -74,7 +75,7 @@ class ResponsePiper : public net::URLFetcherResponseWriter {
} // namespace
URLRequestFetchJob::URLRequestFetchJob(
AtomBrowserContext* browser_context,
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));
fetcher_->SetRequestContext(browser_context->url_request_context_getter());
// 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.
@ -107,6 +113,17 @@ URLRequestFetchJob::URLRequestFetchJob(
}
}
net::URLRequestContextGetter* URLRequestFetchJob::GetRequestContext() {
if (!url_request_context_getter_.get()) {
auto task_runner = base::ThreadTaskRunnerHandle::Get();
net::URLRequestContextBuilder builder;
builder.set_proxy_service(net::ProxyService::CreateDirect());
url_request_context_getter_ =
new net::TrivialURLRequestContextGetter(builder.Build(), task_runner);
}
return url_request_context_getter_.get();
}
void URLRequestFetchJob::HeadersCompleted() {
response_info_.reset(new net::HttpResponseInfo);
response_info_->headers = fetcher_->GetResponseHeaders();

View file

@ -7,6 +7,7 @@
#include <string>
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_job.h"
@ -17,13 +18,14 @@ class AtomBrowserContext;
class URLRequestFetchJob : public net::URLRequestJob,
public net::URLFetcherDelegate {
public:
URLRequestFetchJob(AtomBrowserContext* browser_context,
URLRequestFetchJob(net::URLRequestContextGetter* request_context_getter,
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const GURL& url,
const std::string& method,
const std::string& referrer);
net::URLRequestContextGetter* GetRequestContext();
void HeadersCompleted();
int DataAvailable(net::IOBuffer* buffer, int num_bytes);
@ -41,6 +43,7 @@ class URLRequestFetchJob : public net::URLRequestJob,
void OnURLFetchComplete(const net::URLFetcher* source) override;
private:
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
scoped_ptr<net::URLFetcher> fetcher_;
scoped_refptr<net::IOBuffer> pending_buffer_;
int pending_buffer_size_;

View file

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