This source file includes following definitions.
- offset_
- callback
- file
- Read
- Write
- PostRead
- PostWrite
- OnOperationComplete
- GetFileInFlightIO
- DeleteFileInFlightIO
- base_file_
- Init
- IsValid
- Read
- Write
- Read
- Write
- SetLength
- GetLength
- WaitForPendingIO
- DropPendingIO
- platform_file
- AsyncWrite
#include "net/disk_cache/blockfile/file.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/threading/worker_pool.h"
#include "net/base/net_errors.h"
#include "net/disk_cache/blockfile/in_flight_io.h"
#include "net/disk_cache/disk_cache.h"
namespace {
class FileBackgroundIO : public disk_cache::BackgroundIO {
public:
FileBackgroundIO(disk_cache::File* file, const void* buf, size_t buf_len,
size_t offset, disk_cache::FileIOCallback* callback,
disk_cache::InFlightIO* controller)
: disk_cache::BackgroundIO(controller), callback_(callback), file_(file),
buf_(buf), buf_len_(buf_len), offset_(offset) {
}
disk_cache::FileIOCallback* callback() {
return callback_;
}
disk_cache::File* file() {
return file_;
}
void Read();
void Write();
private:
virtual ~FileBackgroundIO() {}
disk_cache::FileIOCallback* callback_;
disk_cache::File* file_;
const void* buf_;
size_t buf_len_;
size_t offset_;
DISALLOW_COPY_AND_ASSIGN(FileBackgroundIO);
};
class FileInFlightIO : public disk_cache::InFlightIO {
public:
FileInFlightIO() {}
virtual ~FileInFlightIO() {}
void PostRead(disk_cache::File* file, void* buf, size_t buf_len,
size_t offset, disk_cache::FileIOCallback* callback);
void PostWrite(disk_cache::File* file, const void* buf, size_t buf_len,
size_t offset, disk_cache::FileIOCallback* callback);
protected:
virtual void OnOperationComplete(disk_cache::BackgroundIO* operation,
bool cancel) OVERRIDE;
private:
DISALLOW_COPY_AND_ASSIGN(FileInFlightIO);
};
void FileBackgroundIO::Read() {
if (file_->Read(const_cast<void*>(buf_), buf_len_, offset_)) {
result_ = static_cast<int>(buf_len_);
} else {
result_ = net::ERR_CACHE_READ_FAILURE;
}
NotifyController();
}
void FileBackgroundIO::Write() {
bool rv = file_->Write(buf_, buf_len_, offset_);
result_ = rv ? static_cast<int>(buf_len_) : net::ERR_CACHE_WRITE_FAILURE;
NotifyController();
}
void FileInFlightIO::PostRead(disk_cache::File *file, void* buf, size_t buf_len,
size_t offset, disk_cache::FileIOCallback *callback) {
scoped_refptr<FileBackgroundIO> operation(
new FileBackgroundIO(file, buf, buf_len, offset, callback, this));
file->AddRef();
base::WorkerPool::PostTask(FROM_HERE,
base::Bind(&FileBackgroundIO::Read, operation.get()), true);
OnOperationPosted(operation.get());
}
void FileInFlightIO::PostWrite(disk_cache::File* file, const void* buf,
size_t buf_len, size_t offset,
disk_cache::FileIOCallback* callback) {
scoped_refptr<FileBackgroundIO> operation(
new FileBackgroundIO(file, buf, buf_len, offset, callback, this));
file->AddRef();
base::WorkerPool::PostTask(FROM_HERE,
base::Bind(&FileBackgroundIO::Write, operation.get()), true);
OnOperationPosted(operation.get());
}
void FileInFlightIO::OnOperationComplete(disk_cache::BackgroundIO* operation,
bool cancel) {
FileBackgroundIO* op = static_cast<FileBackgroundIO*>(operation);
disk_cache::FileIOCallback* callback = op->callback();
int bytes = operation->result();
op->file()->Release();
callback->OnFileIOComplete(bytes);
}
FileInFlightIO* s_file_operations = NULL;
FileInFlightIO* GetFileInFlightIO() {
if (!s_file_operations) {
s_file_operations = new FileInFlightIO;
}
return s_file_operations;
}
void DeleteFileInFlightIO() {
DCHECK(s_file_operations);
delete s_file_operations;
s_file_operations = NULL;
}
}
namespace disk_cache {
File::File(base::File file)
: init_(true),
mixed_(true),
base_file_(file.Pass()) {
}
bool File::Init(const base::FilePath& name) {
if (base_file_.IsValid())
return false;
int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
base::File::FLAG_WRITE;
base_file_.Initialize(name, flags);
return base_file_.IsValid();
}
bool File::IsValid() const {
return base_file_.IsValid();
}
bool File::Read(void* buffer, size_t buffer_len, size_t offset) {
DCHECK(base_file_.IsValid());
if (buffer_len > static_cast<size_t>(kint32max) ||
offset > static_cast<size_t>(kint32max)) {
return false;
}
int ret = base_file_.Read(offset, static_cast<char*>(buffer), buffer_len);
return (static_cast<size_t>(ret) == buffer_len);
}
bool File::Write(const void* buffer, size_t buffer_len, size_t offset) {
DCHECK(base_file_.IsValid());
if (buffer_len > static_cast<size_t>(kint32max) ||
offset > static_cast<size_t>(kint32max)) {
return false;
}
int ret = base_file_.Write(offset, static_cast<const char*>(buffer),
buffer_len);
return (static_cast<size_t>(ret) == buffer_len);
}
bool File::Read(void* buffer, size_t buffer_len, size_t offset,
FileIOCallback* callback, bool* completed) {
DCHECK(base_file_.IsValid());
if (!callback) {
if (completed)
*completed = true;
return Read(buffer, buffer_len, offset);
}
if (buffer_len > ULONG_MAX || offset > ULONG_MAX)
return false;
GetFileInFlightIO()->PostRead(this, buffer, buffer_len, offset, callback);
*completed = false;
return true;
}
bool File::Write(const void* buffer, size_t buffer_len, size_t offset,
FileIOCallback* callback, bool* completed) {
DCHECK(base_file_.IsValid());
if (!callback) {
if (completed)
*completed = true;
return Write(buffer, buffer_len, offset);
}
return AsyncWrite(buffer, buffer_len, offset, callback, completed);
}
bool File::SetLength(size_t length) {
DCHECK(base_file_.IsValid());
if (length > kuint32max)
return false;
return base_file_.SetLength(length);
}
size_t File::GetLength() {
DCHECK(base_file_.IsValid());
int64 len = base_file_.GetLength();
if (len > static_cast<int64>(kuint32max))
return kuint32max;
return static_cast<size_t>(len);
}
void File::WaitForPendingIO(int* num_pending_io) {
GetFileInFlightIO()->WaitForPendingIO();
DeleteFileInFlightIO();
}
void File::DropPendingIO() {
GetFileInFlightIO()->DropPendingIO();
DeleteFileInFlightIO();
}
File::~File() {
}
base::PlatformFile File::platform_file() const {
return base_file_.GetPlatformFile();
}
bool File::AsyncWrite(const void* buffer, size_t buffer_len, size_t offset,
FileIOCallback* callback, bool* completed) {
DCHECK(base_file_.IsValid());
if (buffer_len > ULONG_MAX || offset > ULONG_MAX)
return false;
GetFileInFlightIO()->PostWrite(this, buffer, buffer_len, offset, callback);
if (completed)
*completed = false;
return true;
}
}