protocol: create separate request context for fetch job

This commit is contained in:
Robo 2015-07-13 21:10:44 +05:30
parent 652843f447
commit 8eb87c5d2b
6 changed files with 22 additions and 16 deletions

View file

@ -132,7 +132,7 @@ class CustomProtocolRequestJob : public AdapterRequestJob {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&AdapterRequestJob::CreateHttpJobAndStart, GetWeakPtr(), base::Bind(&AdapterRequestJob::CreateHttpJobAndStart, GetWeakPtr(),
registry_->browser_context(), url, method, referrer)); url, method, referrer));
return; return;
} }
} }
@ -211,8 +211,7 @@ std::string ConvertErrorCode(int error_code) {
} // namespace } // namespace
Protocol::Protocol(AtomBrowserContext* browser_context) Protocol::Protocol(AtomBrowserContext* browser_context)
: browser_context_(browser_context), : job_factory_(browser_context->job_factory()) {
job_factory_(browser_context->job_factory()) {
CHECK(job_factory_); CHECK(job_factory_);
} }

View file

@ -46,8 +46,6 @@ class Protocol : public mate::EventEmitter {
JsProtocolHandler GetProtocolHandler(const std::string& scheme); JsProtocolHandler GetProtocolHandler(const std::string& scheme);
AtomBrowserContext* browser_context() const { return browser_context_; }
protected: protected:
explicit Protocol(AtomBrowserContext* browser_context); explicit Protocol(AtomBrowserContext* browser_context);
@ -94,7 +92,6 @@ class Protocol : public mate::EventEmitter {
const JsProtocolHandler& handler); const JsProtocolHandler& handler);
int UninterceptProtocolInIO(const std::string& scheme); int UninterceptProtocolInIO(const std::string& scheme);
AtomBrowserContext* browser_context_;
AtomURLRequestJobFactory* job_factory_; AtomURLRequestJobFactory* job_factory_;
ProtocolHandlersMap protocol_handlers_; ProtocolHandlersMap protocol_handlers_;

View file

@ -4,7 +4,6 @@
#include "atom/browser/net/adapter_request_job.h" #include "atom/browser/net/adapter_request_job.h"
#include "atom/browser/atom_browser_context.h"
#include "base/threading/sequenced_worker_pool.h" #include "base/threading/sequenced_worker_pool.h"
#include "atom/browser/net/url_request_buffer_job.h" #include "atom/browser/net/url_request_buffer_job.h"
#include "atom/browser/net/url_request_fetch_job.h" #include "atom/browser/net/url_request_fetch_job.h"
@ -115,7 +114,6 @@ void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) {
} }
void AdapterRequestJob::CreateHttpJobAndStart( void AdapterRequestJob::CreateHttpJobAndStart(
AtomBrowserContext* browser_context,
const GURL& url, const GURL& url,
const std::string& method, const std::string& method,
const std::string& referrer) { const std::string& referrer) {
@ -124,7 +122,7 @@ void AdapterRequestJob::CreateHttpJobAndStart(
return; return;
} }
real_job_ = new URLRequestFetchJob(browser_context, request(), real_job_ = new URLRequestFetchJob(request(),
network_delegate(), url, method, referrer); network_delegate(), url, method, referrer);
real_job_->Start(); real_job_->Start();
} }

View file

@ -61,8 +61,7 @@ class AdapterRequestJob : public net::URLRequestJob {
const std::string& charset, const std::string& charset,
scoped_refptr<base::RefCountedBytes> data); scoped_refptr<base::RefCountedBytes> data);
void CreateFileJobAndStart(const base::FilePath& path); void CreateFileJobAndStart(const base::FilePath& path);
void CreateHttpJobAndStart(AtomBrowserContext* browser_context, void CreateHttpJobAndStart(const GURL& url,
const GURL& url,
const std::string& method, const std::string& method,
const std::string& referrer); const std::string& referrer);
void CreateJobFromProtocolHandlerAndStart(); void CreateJobFromProtocolHandlerAndStart();

View file

@ -7,13 +7,14 @@
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include "atom/browser/atom_browser_context.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/thread_task_runner_handle.h"
#include "net/base/io_buffer.h" #include "net/base/io_buffer.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/http/http_response_headers.h" #include "net/http/http_response_headers.h"
#include "net/url_request/url_fetcher.h" #include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_response_writer.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" #include "net/url_request/url_request_status.h"
namespace atom { namespace atom {
@ -74,7 +75,6 @@ class ResponsePiper : public net::URLFetcherResponseWriter {
} // namespace } // namespace
URLRequestFetchJob::URLRequestFetchJob( URLRequestFetchJob::URLRequestFetchJob(
AtomBrowserContext* browser_context,
net::URLRequest* request, net::URLRequest* request,
net::NetworkDelegate* network_delegate, net::NetworkDelegate* network_delegate,
const GURL& url, const GURL& url,
@ -90,7 +90,7 @@ URLRequestFetchJob::URLRequestFetchJob(
request_type = GetRequestType(method); request_type = GetRequestType(method);
fetcher_.reset(net::URLFetcher::Create(url, request_type, this)); fetcher_.reset(net::URLFetcher::Create(url, request_type, this));
fetcher_->SetRequestContext(browser_context->url_request_context_getter()); fetcher_->SetRequestContext(GetRequestContext());
fetcher_->SaveResponseWithWriter(make_scoped_ptr(new ResponsePiper(this))); fetcher_->SaveResponseWithWriter(make_scoped_ptr(new ResponsePiper(this)));
// Use |request|'s referrer if |referrer| is not specified. // Use |request|'s referrer if |referrer| is not specified.
@ -107,6 +107,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() { void URLRequestFetchJob::HeadersCompleted() {
response_info_.reset(new net::HttpResponseInfo); response_info_.reset(new net::HttpResponseInfo);
response_info_->headers = fetcher_->GetResponseHeaders(); response_info_->headers = fetcher_->GetResponseHeaders();

View file

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