2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-08-30 12:02:17 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/net/adapter_request_job.h"
|
2013-08-30 12:02:17 +00:00
|
|
|
|
2013-12-11 07:48:19 +00:00
|
|
|
#include "base/threading/sequenced_worker_pool.h"
|
2015-05-07 07:42:35 +00:00
|
|
|
#include "atom/browser/net/url_request_buffer_job.h"
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/net/url_request_string_job.h"
|
2015-03-04 05:22:51 +00:00
|
|
|
#include "atom/browser/net/asar/url_request_asar_job.h"
|
|
|
|
#include "atom/common/asar/asar_util.h"
|
2013-08-30 12:02:17 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "net/base/net_errors.h"
|
|
|
|
#include "net/url_request/url_request_error_job.h"
|
|
|
|
#include "net/url_request/url_request_file_job.h"
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
AdapterRequestJob::AdapterRequestJob(ProtocolHandler* protocol_handler,
|
|
|
|
net::URLRequest* request,
|
|
|
|
net::NetworkDelegate* network_delegate)
|
|
|
|
: URLRequestJob(request, network_delegate),
|
|
|
|
protocol_handler_(protocol_handler),
|
|
|
|
weak_factory_(this) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdapterRequestJob::Start() {
|
2014-12-07 15:43:26 +00:00
|
|
|
DCHECK(!real_job_.get());
|
2013-08-30 12:02:17 +00:00
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::UI,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&AdapterRequestJob::GetJobTypeInUI,
|
|
|
|
weak_factory_.GetWeakPtr()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdapterRequestJob::Kill() {
|
2014-12-07 15:43:26 +00:00
|
|
|
if (real_job_.get()) // Kill could happen when real_job_ is created.
|
2013-10-22 01:33:38 +00:00
|
|
|
real_job_->Kill();
|
2013-08-30 12:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AdapterRequestJob::ReadRawData(net::IOBuffer* buf,
|
|
|
|
int buf_size,
|
|
|
|
int *bytes_read) {
|
2014-12-07 15:43:26 +00:00
|
|
|
DCHECK(!real_job_.get());
|
2013-08-30 12:38:06 +00:00
|
|
|
return real_job_->ReadRawData(buf, buf_size, bytes_read);
|
2013-08-30 12:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AdapterRequestJob::IsRedirectResponse(GURL* location,
|
|
|
|
int* http_status_code) {
|
2014-12-07 15:43:26 +00:00
|
|
|
DCHECK(!real_job_.get());
|
2013-08-30 12:02:17 +00:00
|
|
|
return real_job_->IsRedirectResponse(location, http_status_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
net::Filter* AdapterRequestJob::SetupFilter() const {
|
2014-12-07 15:43:26 +00:00
|
|
|
DCHECK(!real_job_.get());
|
2013-08-30 12:02:17 +00:00
|
|
|
return real_job_->SetupFilter();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AdapterRequestJob::GetMimeType(std::string* mime_type) const {
|
2014-12-07 15:43:26 +00:00
|
|
|
DCHECK(!real_job_.get());
|
2013-08-30 12:02:17 +00:00
|
|
|
return real_job_->GetMimeType(mime_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AdapterRequestJob::GetCharset(std::string* charset) {
|
2014-12-07 15:43:26 +00:00
|
|
|
DCHECK(!real_job_.get());
|
2013-08-30 12:02:17 +00:00
|
|
|
return real_job_->GetCharset(charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
base::WeakPtr<AdapterRequestJob> AdapterRequestJob::GetWeakPtr() {
|
|
|
|
return weak_factory_.GetWeakPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdapterRequestJob::CreateErrorJobAndStart(int error_code) {
|
|
|
|
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
|
|
|
|
|
|
|
real_job_ = new net::URLRequestErrorJob(
|
|
|
|
request(), network_delegate(), error_code);
|
|
|
|
real_job_->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdapterRequestJob::CreateStringJobAndStart(const std::string& mime_type,
|
|
|
|
const std::string& charset,
|
|
|
|
const std::string& data) {
|
|
|
|
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
|
|
|
|
|
|
|
real_job_ = new URLRequestStringJob(
|
|
|
|
request(), network_delegate(), mime_type, charset, data);
|
|
|
|
real_job_->Start();
|
|
|
|
}
|
|
|
|
|
2015-03-16 12:53:45 +00:00
|
|
|
void AdapterRequestJob::CreateBufferJobAndStart(const std::string& mime_type,
|
|
|
|
const std::string& charset,
|
|
|
|
v8::Local<v8::Object> buffer) {
|
|
|
|
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
|
|
|
|
|
|
|
real_job_ = new URLRequestBufferJob(
|
|
|
|
request(), network_delegate(), mime_type, charset, buffer);
|
|
|
|
real_job_->Start();
|
|
|
|
}
|
|
|
|
|
2013-08-30 12:02:17 +00:00
|
|
|
void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) {
|
|
|
|
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
2015-03-20 12:34:58 +00:00
|
|
|
real_job_ = asar::CreateJobFromPath(
|
|
|
|
path,
|
|
|
|
request(),
|
|
|
|
network_delegate(),
|
|
|
|
content::BrowserThread::GetBlockingPool()->
|
|
|
|
GetTaskRunnerWithShutdownBehavior(
|
|
|
|
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
|
2013-08-30 12:02:17 +00:00
|
|
|
real_job_->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdapterRequestJob::CreateJobFromProtocolHandlerAndStart() {
|
|
|
|
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
|
|
|
DCHECK(protocol_handler_);
|
|
|
|
real_job_ = protocol_handler_->MaybeCreateJob(request(),
|
|
|
|
network_delegate());
|
2013-09-03 08:50:10 +00:00
|
|
|
if (!real_job_.get())
|
2013-08-30 12:02:17 +00:00
|
|
|
CreateErrorJobAndStart(net::ERR_NOT_IMPLEMENTED);
|
|
|
|
else
|
|
|
|
real_job_->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|