Commit b7617c74 authored by rvargas@chromium.org's avatar rvargas@chromium.org

Remove some PlatformFile instances from ChromeOS Drive.

BUG=322664

Review URL: https://codereview.chromium.org/211483004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@260326 0039d316-1c4b-4281-b951-d872f2087c98
parent faf59c40
...@@ -113,6 +113,9 @@ int LocalReaderProxy::Read(net::IOBuffer* buffer, int buffer_length, ...@@ -113,6 +113,9 @@ int LocalReaderProxy::Read(net::IOBuffer* buffer, int buffer_length,
buffer_length = static_cast<int>(remaining_length_); buffer_length = static_cast<int>(remaining_length_);
} }
if (!buffer_length)
return 0;
file_reader_->Read(buffer, buffer_length, file_reader_->Read(buffer, buffer_length,
base::Bind(&LocalReaderProxy::OnReadCompleted, base::Bind(&LocalReaderProxy::OnReadCompleted,
weak_ptr_factory_.GetWeakPtr(), callback)); weak_ptr_factory_.GetWeakPtr(), callback));
......
...@@ -6,11 +6,10 @@ ...@@ -6,11 +6,10 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/callback_helpers.h" #include "base/callback_helpers.h"
#include "base/files/file.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/files/scoped_platform_file_closer.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/message_loop/message_loop_proxy.h" #include "base/message_loop/message_loop_proxy.h"
#include "base/platform_file.h"
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
#include "base/task_runner_util.h" #include "base/task_runner_util.h"
#include "chrome/browser/chromeos/drive/drive.pb.h" #include "chrome/browser/chromeos/drive/drive.pb.h"
...@@ -42,19 +41,12 @@ FileError TruncateOnBlockingPool(internal::ResourceMetadata* metadata, ...@@ -42,19 +41,12 @@ FileError TruncateOnBlockingPool(internal::ResourceMetadata* metadata,
if (error != FILE_ERROR_OK) if (error != FILE_ERROR_OK)
return error; return error;
base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; base::File file(local_cache_path,
base::PlatformFile file = base::CreatePlatformFile( base::File::FLAG_OPEN | base::File::FLAG_WRITE);
local_cache_path, if (!file.IsValid())
base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
NULL,
&result);
if (result != base::PLATFORM_FILE_OK)
return FILE_ERROR_FAILED; return FILE_ERROR_FAILED;
DCHECK_NE(base::kInvalidPlatformFileValue, file); if (!file.SetLength(length))
base::ScopedPlatformFileCloser platform_file_closer(&file);
if (!base::TruncatePlatformFile(file, length))
return FILE_ERROR_FAILED; return FILE_ERROR_FAILED;
return FILE_ERROR_OK; return FILE_ERROR_OK;
......
...@@ -4,15 +4,9 @@ ...@@ -4,15 +4,9 @@
#include "chrome/browser/chromeos/drive/local_file_reader.h" #include "chrome/browser/chromeos/drive/local_file_reader.h"
#include <errno.h>
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/location.h"
#include "base/platform_file.h"
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
#include "base/task_runner_util.h"
#include "net/base/completion_callback.h" #include "net/base/completion_callback.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"
...@@ -20,157 +14,55 @@ ...@@ -20,157 +14,55 @@
namespace drive { namespace drive {
namespace util { namespace util {
namespace {
// Opens the file at |file_path| and seeks to the |offset| from begin.
// Returns the net::Error code. If succeeded, |platform_file| is set to point
// the opened file.
// This function should run on the blocking pool.
int OpenAndSeekOnBlockingPool(const base::FilePath& file_path,
int64 offset,
base::PlatformFile* platform_file) {
DCHECK(platform_file);
DCHECK_EQ(base::kInvalidPlatformFileValue, *platform_file);
// First of all, open the file.
const int open_flags = base::PLATFORM_FILE_OPEN |
base::PLATFORM_FILE_READ |
base::PLATFORM_FILE_ASYNC;
base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED;
base::PlatformFile file =
base::CreatePlatformFile(file_path, open_flags, NULL, &error);
if (file == base::kInvalidPlatformFileValue) {
DCHECK_NE(base::PLATFORM_FILE_OK, error);
return net::FileErrorToNetError(static_cast<base::File::Error>(error));
}
// If succeeded, seek to the |offset| from begin.
int64 position = base::SeekPlatformFile(
file, base::PLATFORM_FILE_FROM_BEGIN, offset);
if (position < 0) {
// If failed, close the file and return an error.
base::ClosePlatformFile(file);
return net::ERR_FAILED;
}
*platform_file = file;
return net::OK;
}
// Reads the data from the |platform_file| and copies it to the |buffer| at
// most |buffer_length| size. Returns the number of copied bytes if succeeded,
// or the net::Error code.
// This function should run on the blocking pool.
int ReadOnBlockingPool(base::PlatformFile platform_file,
scoped_refptr<net::IOBuffer> buffer,
int buffer_length) {
DCHECK_NE(base::kInvalidPlatformFileValue, platform_file);
int result = base::ReadPlatformFileCurPosNoBestEffort(
platform_file, buffer->data(), buffer_length);
return result < 0 ? net::MapSystemError(errno) : result;
}
// Posts a task to close the |platform_file| into |task_runner|.
// Or, if |platform_file| is kInvalidPlatformFileValue, does nothing.
void PostCloseIfNeeded(base::TaskRunner* task_runner,
base::PlatformFile platform_file) {
DCHECK(task_runner);
if (platform_file != base::kInvalidPlatformFileValue) {
task_runner->PostTask(
FROM_HERE,
base::Bind(
base::IgnoreResult(&base::ClosePlatformFile), platform_file));
}
}
} // namespace
class LocalFileReader::ScopedPlatformFile {
public:
explicit ScopedPlatformFile(base::TaskRunner* task_runner)
: task_runner_(task_runner),
platform_file_(base::kInvalidPlatformFileValue) {
DCHECK(task_runner);
}
~ScopedPlatformFile() {
PostCloseIfNeeded(task_runner_.get(), platform_file_);
}
base::PlatformFile* ptr() { return &platform_file_; }
base::PlatformFile release() {
base::PlatformFile result = platform_file_;
platform_file_ = base::kInvalidPlatformFileValue;
return result;
}
private:
scoped_refptr<base::TaskRunner> task_runner_;
base::PlatformFile platform_file_;
DISALLOW_COPY_AND_ASSIGN(ScopedPlatformFile);
};
LocalFileReader::LocalFileReader( LocalFileReader::LocalFileReader(
base::SequencedTaskRunner* sequenced_task_runner) base::SequencedTaskRunner* sequenced_task_runner)
: sequenced_task_runner_(sequenced_task_runner), : file_stream_(NULL, sequenced_task_runner),
platform_file_(base::kInvalidPlatformFileValue),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
DCHECK(sequenced_task_runner_.get());
} }
LocalFileReader::~LocalFileReader() { LocalFileReader::~LocalFileReader() {
PostCloseIfNeeded(sequenced_task_runner_.get(), platform_file_);
} }
void LocalFileReader::Open(const base::FilePath& file_path, void LocalFileReader::Open(const base::FilePath& file_path,
int64 offset, int64 offset,
const net::CompletionCallback& callback) { const net::CompletionCallback& callback) {
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
DCHECK_EQ(base::kInvalidPlatformFileValue, platform_file_); int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
base::PLATFORM_FILE_ASYNC;
ScopedPlatformFile* platform_file =
new ScopedPlatformFile(sequenced_task_runner_.get()); int rv = file_stream_.Open(file_path, flags,
base::PostTaskAndReplyWithResult( Bind(&LocalFileReader::DidOpen,
sequenced_task_runner_.get(), weak_ptr_factory_.GetWeakPtr(),
FROM_HERE, callback, offset));
base::Bind( DCHECK_EQ(rv, net::ERR_IO_PENDING);
&OpenAndSeekOnBlockingPool, file_path, offset, platform_file->ptr()),
base::Bind(&LocalFileReader::OpenAfterBlockingPoolTask,
weak_ptr_factory_.GetWeakPtr(),
callback,
base::Owned(platform_file)));
} }
void LocalFileReader::Read(net::IOBuffer* in_buffer, void LocalFileReader::Read(net::IOBuffer* in_buffer,
int buffer_length, int buffer_length,
const net::CompletionCallback& callback) { const net::CompletionCallback& callback) {
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
DCHECK_NE(base::kInvalidPlatformFileValue, platform_file_); DCHECK(file_stream_.IsOpen());
int rv = file_stream_.Read(in_buffer, buffer_length, callback);
scoped_refptr<net::IOBuffer> buffer(in_buffer); DCHECK_EQ(rv, net::ERR_IO_PENDING);
base::PostTaskAndReplyWithResult(
sequenced_task_runner_.get(),
FROM_HERE,
base::Bind(&ReadOnBlockingPool, platform_file_, buffer, buffer_length),
callback);
} }
void LocalFileReader::OpenAfterBlockingPoolTask( void LocalFileReader::DidOpen(const net::CompletionCallback& callback,
const net::CompletionCallback& callback, int64 offset,
ScopedPlatformFile* platform_file, int error) {
int open_result) { if (error != net::OK)
DCHECK(!callback.is_null()); return callback.Run(error);
DCHECK(platform_file);
DCHECK_EQ(base::kInvalidPlatformFileValue, platform_file_); int rv = file_stream_.Seek(net::FROM_BEGIN, offset,
Bind(&LocalFileReader::DidSeek,
if (open_result == net::OK) { weak_ptr_factory_.GetWeakPtr(),
DCHECK_NE(base::kInvalidPlatformFileValue, *platform_file->ptr()); callback, offset));
platform_file_ = platform_file->release(); DCHECK_EQ(rv, net::ERR_IO_PENDING);
} }
callback.Run(open_result); void LocalFileReader::DidSeek(const net::CompletionCallback& callback,
int64 offset,
int64 error) {
callback.Run(error == offset ? net::OK : net::ERR_FAILED);
} }
} // namespace util } // namespace util
......
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/platform_file.h"
#include "net/base/completion_callback.h" #include "net/base/completion_callback.h"
#include "net/base/file_stream.h"
namespace base { namespace base {
class FilePath; class FilePath;
...@@ -18,7 +18,7 @@ class SequencedTaskRunner; ...@@ -18,7 +18,7 @@ class SequencedTaskRunner;
namespace net { namespace net {
class IOBuffer; class IOBuffer;
} // namespace } // namespace net
namespace drive { namespace drive {
namespace util { namespace util {
...@@ -49,18 +49,14 @@ class LocalFileReader { ...@@ -49,18 +49,14 @@ class LocalFileReader {
const net::CompletionCallback& callback); const net::CompletionCallback& callback);
private: private:
// The thin wrapper for the platform file to handle closing correctly. void DidOpen(const net::CompletionCallback& callback,
class ScopedPlatformFile; int64 offset,
int error);
void DidSeek(const net::CompletionCallback& callback,
int64 offset,
int64 error);
// Part of Open(). Called after the open() operation task running net::FileStream file_stream_;
// on blocking pool.
void OpenAfterBlockingPoolTask(
const net::CompletionCallback& callback,
ScopedPlatformFile* result_platform_file,
int open_result);
scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
base::PlatformFile platform_file_;
// Note: This should remain the last member so it'll be destroyed and // Note: This should remain the last member so it'll be destroyed and
// invalidate the weak pointers before any other members are destroyed. // invalidate the weak pointers before any other members are destroyed.
......
...@@ -274,21 +274,16 @@ void FileStream::Context::CloseAndDelete() { ...@@ -274,21 +274,16 @@ void FileStream::Context::CloseAndDelete() {
DCHECK(!async_in_progress_); DCHECK(!async_in_progress_);
if (file_.IsValid()) { if (file_.IsValid()) {
bool posted = task_runner_.get()->PostTaskAndReply( bool posted = task_runner_.get()->PostTask(
FROM_HERE, FROM_HERE,
base::Bind(base::IgnoreResult(&Context::CloseFileImpl), base::Bind(base::IgnoreResult(&Context::CloseFileImpl),
base::Unretained(this)), base::Owned(this)));
base::Bind(&Context::OnCloseCompleted, base::Unretained(this)));
DCHECK(posted); DCHECK(posted);
} else { } else {
delete this; delete this;
} }
} }
void FileStream::Context::OnCloseCompleted() {
delete this;
}
Int64CompletionCallback FileStream::Context::IntToInt64( Int64CompletionCallback FileStream::Context::IntToInt64(
const CompletionCallback& callback) { const CompletionCallback& callback) {
return base::Bind(&CallInt64ToInt, callback); return base::Bind(&CallInt64ToInt, callback);
......
...@@ -168,7 +168,6 @@ class FileStream::Context { ...@@ -168,7 +168,6 @@ class FileStream::Context {
OpenResult open_result); OpenResult open_result);
void CloseAndDelete(); void CloseAndDelete();
void OnCloseCompleted();
Int64CompletionCallback IntToInt64(const CompletionCallback& callback); Int64CompletionCallback IntToInt64(const CompletionCallback& callback);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment