This source file includes following definitions.
- is_directory
- weak_ptr_factory_
- Start
- Kill
- ReadRawData
- IsRedirectResponse
- SetupFilter
- GetMimeType
- SetExtraRequestHeaders
- FetchMetaInfo
- DidFetchMetaInfo
- DidOpen
- DidSeek
- DidRead
#include "net/url_request/url_request_file_job.h"
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/file_util.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_util.h"
#include "base/synchronization/lock.h"
#include "base/task_runner.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "net/base/file_stream.h"
#include "net/base/io_buffer.h"
#include "net/base/load_flags.h"
#include "net/base/mime_util.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "net/filter/filter.h"
#include "net/http/http_util.h"
#include "net/url_request/url_request_error_job.h"
#include "net/url_request/url_request_file_dir_job.h"
#include "url/gurl.h"
#if defined(OS_WIN)
#include "base/win/shortcut.h"
#endif
namespace net {
URLRequestFileJob::FileMetaInfo::FileMetaInfo()
: file_size(0),
mime_type_result(false),
file_exists(false),
is_directory(false) {
}
URLRequestFileJob::URLRequestFileJob(
URLRequest* request,
NetworkDelegate* network_delegate,
const base::FilePath& file_path,
const scoped_refptr<base::TaskRunner>& file_task_runner)
: URLRequestJob(request, network_delegate),
file_path_(file_path),
stream_(new FileStream(NULL, file_task_runner)),
file_task_runner_(file_task_runner),
remaining_bytes_(0),
weak_ptr_factory_(this) {}
void URLRequestFileJob::Start() {
FileMetaInfo* meta_info = new FileMetaInfo();
file_task_runner_->PostTaskAndReply(
FROM_HERE,
base::Bind(&URLRequestFileJob::FetchMetaInfo, file_path_,
base::Unretained(meta_info)),
base::Bind(&URLRequestFileJob::DidFetchMetaInfo,
weak_ptr_factory_.GetWeakPtr(),
base::Owned(meta_info)));
}
void URLRequestFileJob::Kill() {
stream_.reset();
weak_ptr_factory_.InvalidateWeakPtrs();
URLRequestJob::Kill();
}
bool URLRequestFileJob::ReadRawData(IOBuffer* dest, int dest_size,
int *bytes_read) {
DCHECK_NE(dest_size, 0);
DCHECK(bytes_read);
DCHECK_GE(remaining_bytes_, 0);
if (remaining_bytes_ < dest_size)
dest_size = static_cast<int>(remaining_bytes_);
if (!dest_size) {
*bytes_read = 0;
return true;
}
int rv = stream_->Read(dest, dest_size,
base::Bind(&URLRequestFileJob::DidRead,
weak_ptr_factory_.GetWeakPtr()));
if (rv >= 0) {
*bytes_read = rv;
remaining_bytes_ -= rv;
DCHECK_GE(remaining_bytes_, 0);
return true;
}
if (rv == ERR_IO_PENDING) {
SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
} else {
NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
}
return false;
}
bool URLRequestFileJob::IsRedirectResponse(GURL* location,
int* http_status_code) {
if (meta_info_.is_directory) {
std::string new_path = request_->url().path();
new_path.push_back('/');
GURL::Replacements replacements;
replacements.SetPathStr(new_path);
*location = request_->url().ReplaceComponents(replacements);
*http_status_code = 301;
return true;
}
#if defined(OS_WIN)
if (!LowerCaseEqualsASCII(file_path_.Extension(), ".lnk"))
return false;
base::FilePath new_path = file_path_;
bool resolved;
resolved = base::win::ResolveShortcut(new_path, &new_path, NULL);
if (!resolved)
return false;
*location = FilePathToFileURL(new_path);
*http_status_code = 301;
return true;
#else
return false;
#endif
}
Filter* URLRequestFileJob::SetupFilter() const {
return LowerCaseEqualsASCII(file_path_.Extension(), ".svgz")
? Filter::GZipFactory() : NULL;
}
bool URLRequestFileJob::GetMimeType(std::string* mime_type) const {
DCHECK(request_);
if (meta_info_.mime_type_result) {
*mime_type = meta_info_.mime_type;
return true;
}
return false;
}
void URLRequestFileJob::SetExtraRequestHeaders(
const HttpRequestHeaders& headers) {
std::string range_header;
if (headers.GetHeader(HttpRequestHeaders::kRange, &range_header)) {
std::vector<HttpByteRange> ranges;
if (HttpUtil::ParseRangeHeader(range_header, &ranges)) {
if (ranges.size() == 1) {
byte_range_ = ranges[0];
} else {
NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
ERR_REQUEST_RANGE_NOT_SATISFIABLE));
}
}
}
}
URLRequestFileJob::~URLRequestFileJob() {
}
void URLRequestFileJob::FetchMetaInfo(const base::FilePath& file_path,
FileMetaInfo* meta_info) {
base::File::Info file_info;
meta_info->file_exists = base::GetFileInfo(file_path, &file_info);
if (meta_info->file_exists) {
meta_info->file_size = file_info.size;
meta_info->is_directory = file_info.is_directory;
}
meta_info->mime_type_result = GetMimeTypeFromFile(file_path,
&meta_info->mime_type);
}
void URLRequestFileJob::DidFetchMetaInfo(const FileMetaInfo* meta_info) {
meta_info_ = *meta_info;
if (!meta_info_.file_exists) {
DidOpen(ERR_FILE_NOT_FOUND);
return;
}
if (meta_info_.is_directory) {
DidOpen(OK);
return;
}
int flags = base::File::FLAG_OPEN |
base::File::FLAG_READ |
base::File::FLAG_ASYNC;
int rv = stream_->Open(file_path_, flags,
base::Bind(&URLRequestFileJob::DidOpen,
weak_ptr_factory_.GetWeakPtr()));
if (rv != ERR_IO_PENDING)
DidOpen(rv);
}
void URLRequestFileJob::DidOpen(int result) {
if (result != OK) {
NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result));
return;
}
if (!byte_range_.ComputeBounds(meta_info_.file_size)) {
NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
ERR_REQUEST_RANGE_NOT_SATISFIABLE));
return;
}
remaining_bytes_ = byte_range_.last_byte_position() -
byte_range_.first_byte_position() + 1;
DCHECK_GE(remaining_bytes_, 0);
if (remaining_bytes_ > 0 && byte_range_.first_byte_position() != 0) {
int rv = stream_->Seek(FROM_BEGIN, byte_range_.first_byte_position(),
base::Bind(&URLRequestFileJob::DidSeek,
weak_ptr_factory_.GetWeakPtr()));
if (rv != ERR_IO_PENDING) {
DidSeek(-1);
}
} else {
DidSeek(byte_range_.first_byte_position());
}
}
void URLRequestFileJob::DidSeek(int64 result) {
if (result != byte_range_.first_byte_position()) {
NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
ERR_REQUEST_RANGE_NOT_SATISFIABLE));
return;
}
set_expected_content_size(remaining_bytes_);
NotifyHeadersComplete();
}
void URLRequestFileJob::DidRead(int result) {
if (result > 0) {
SetStatus(URLRequestStatus());
} else if (result == 0) {
NotifyDone(URLRequestStatus());
} else {
NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result));
}
remaining_bytes_ -= result;
DCHECK_GE(remaining_bytes_, 0);
NotifyReadComplete(result);
}
}