Chromium Code Reviews| Index: base/file_util_proxy.cc |
| diff --git a/base/file_util_proxy.cc b/base/file_util_proxy.cc |
| index 3e6a3657f1bc3f4d62865de3774e5d2cc1e505a7..0ef1a857bbf701715d09aa043b0bd99f38985399 100644 |
| --- a/base/file_util_proxy.cc |
| +++ b/base/file_util_proxy.cc |
| @@ -5,126 +5,136 @@ |
| #include "base/file_util_proxy.h" |
| #include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/file_util.h" |
| #include "base/message_loop_proxy.h" |
| namespace base { |
| namespace { |
| -class MessageLoopRelay |
| - : public RefCountedThreadSafe<MessageLoopRelay> { |
| - public: |
| - MessageLoopRelay() |
| - : origin_message_loop_proxy_( |
| - MessageLoopProxy::current()), |
| - error_code_(PLATFORM_FILE_OK) { |
| - } |
| +// Helper templates to call file_util or base::PlatformFile methods |
| +// and reply with the returned value. |
| +// |
| +// Typically when you have these methods: |
| +// R DoWorkAndReturn(); |
| +// void Callback(R& result); |
| +// |
| +// You can pass the result of DoWorkAndReturn to the Callback by: |
| +// |
| +// R* result = new R; |
| +// message_loop_proxy->PostTaskAndReply( |
| +// from_here, |
| +// ReturnAsParam<R>(Bind(&DoWorkAndReturn), result), |
| +// CallbackWithReturn(Bind(&Callback), result)); |
| +// |
| +// Or just use PostTaskAndReplyWithStatus helper method for the combination of |
| +// generic file_util methods (that return bool) and StatusCallback. |
| +template <typename R1, typename R2> |
| +struct ReturnValueTranslator { |
| + static R2 Value(const R1& value); |
| +}; |
| - bool Start(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| - const tracked_objects::Location& from_here) { |
| - return message_loop_proxy->PostTask( |
| - from_here, Bind(&MessageLoopRelay::ProcessOnTargetThread, this)); |
| - } |
| +template <typename R> |
| +struct ReturnValueTranslator<R, R> { |
| + static R Value(const R& value) { return value; } |
| +}; |
| - protected: |
| - friend class RefCountedThreadSafe<MessageLoopRelay>; |
| - virtual ~MessageLoopRelay() {} |
| +template <> |
| +struct ReturnValueTranslator<bool, PlatformFileError> { |
| + static PlatformFileError Value(const bool& value) { |
| + if (value) |
| + return PLATFORM_FILE_OK; |
| + return PLATFORM_FILE_ERROR_FAILED; |
| + } |
| +}; |
| - // Called to perform work on the FILE thread. |
| - virtual void RunWork() = 0; |
| +template <typename R1, typename R2> |
| +void ReturnAsParamAdapter(const Callback<R1(void)>& func, R2* result) { |
| + DCHECK(result); |
|
willchan no longer on Chromium
2011/10/19 00:26:52
Why isn't this DCHECK done earlier, perhaps in Ret
kinuko
2011/10/19 08:42:05
Done. Indeed...
|
| + if (!func.is_null()) |
| + *result = ReturnValueTranslator<R1, R2>::Value(func.Run()); |
| +} |
| - // Called to notify the callback on the origin thread. |
| - virtual void RunCallback() = 0; |
| +template <typename R1, typename R2> |
| +Closure ReturnAsParam(const Callback<R1(void)>& func, R2* result) { |
| + return Bind(&ReturnAsParamAdapter<R1, R2>, func, result); |
| +} |
| - void set_error_code(PlatformFileError error_code) { |
| - error_code_ = error_code; |
| - } |
| +template <typename R> |
| +void ReplyAndCleanupAdapter(const Callback<void(R)>& callback, R* result) { |
| + DCHECK(result); |
| + if (!callback.is_null()) |
| + callback.Run(*result); |
| + delete result; |
| +} |
| - PlatformFileError error_code() const { |
| - return error_code_; |
| - } |
| +template <typename R> |
| +Closure ReplyAndCleanup(const Callback<void(R)>& callback, R* result) { |
| + return Bind(&ReplyAndCleanupAdapter<R>, callback, result); |
| +} |
| - private: |
| - void ProcessOnTargetThread() { |
| - RunWork(); |
| - origin_message_loop_proxy_->PostTask( |
| - FROM_HERE, Bind(&MessageLoopRelay::RunCallback, this)); |
| +// Putting everything together. |
| +template <typename R1, typename R2> |
| +bool PostTaskAndReplyWithStatus( |
| + scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| + const tracked_objects::Location& from_here, |
| + const Callback<R1(void)>& file_util_work, |
| + const Callback<void(R2)>& callback, |
| + R2* result) { |
| + if (!message_loop_proxy->PostTaskAndReply( |
| + from_here, |
| + ReturnAsParam<R1>(file_util_work, result), |
| + ReplyAndCleanup(callback, result))) { |
| + delete result; |
| + return false; |
| } |
| + return true; |
| +} |
| - scoped_refptr<MessageLoopProxy> origin_message_loop_proxy_; |
| - PlatformFileError error_code_; |
| -}; |
| - |
| -class RelayCreateOrOpen : public MessageLoopRelay { |
| +// Helper classes or routines for individual methods. |
| +class CreateOrOpenHelper { |
| public: |
| - RelayCreateOrOpen( |
| - scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| - const FilePath& file_path, |
| - int file_flags, |
| - const FileUtilProxy::CreateOrOpenCallback& callback) |
| - : message_loop_proxy_(message_loop_proxy), |
| - file_path_(file_path), |
| - file_flags_(file_flags), |
| - callback_(callback), |
| - file_handle_(kInvalidPlatformFileValue), |
| - created_(false) { |
| - DCHECK_EQ(false, callback.is_null()); |
| - } |
| + CreateOrOpenHelper() |
| + : file_handle_(kInvalidPlatformFileValue), |
| + created_(false), |
| + error_(PLATFORM_FILE_OK) {} |
| - protected: |
| - virtual ~RelayCreateOrOpen() { |
| - if (file_handle_ != kInvalidPlatformFileValue) |
| - FileUtilProxy::Close(message_loop_proxy_, file_handle_, |
| - FileUtilProxy::StatusCallback()); |
| - } |
| - |
| - virtual void RunWork() { |
| - if (!file_util::DirectoryExists(file_path_.DirName())) { |
| + void RunWork(const FilePath& file_path, int file_flags) { |
| + if (!file_util::DirectoryExists(file_path.DirName())) { |
| // If its parent does not exist, should return NOT_FOUND error. |
| - set_error_code(PLATFORM_FILE_ERROR_NOT_FOUND); |
| + error_ = PLATFORM_FILE_ERROR_NOT_FOUND; |
| return; |
| } |
| - PlatformFileError error_code = PLATFORM_FILE_OK; |
| - file_handle_ = CreatePlatformFile(file_path_, file_flags_, |
| - &created_, &error_code); |
| - set_error_code(error_code); |
| - } |
| - |
| - virtual void RunCallback() { |
| - callback_.Run(error_code(), PassPlatformFile(&file_handle_), |
| - created_); |
| + error_ = PLATFORM_FILE_OK; |
| + file_handle_ = CreatePlatformFile(file_path, file_flags, |
| + &created_, &error_); |
| + } |
| + |
| + void Reply(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| + const FileUtilProxy::CreateOrOpenCallback& callback) { |
| + DCHECK(!callback.is_null()); |
| + callback.Run(error_, PassPlatformFile(&file_handle_), created_); |
| + if (file_handle_ != kInvalidPlatformFileValue) { |
| + FileUtilProxy::Close(message_loop_proxy, file_handle_, |
| + FileUtilProxy::StatusCallback()); |
| + } |
| + delete this; |
|
willchan no longer on Chromium
2011/10/19 00:26:52
I think you should get rid of all these delete thi
kinuko
2011/10/19 08:42:05
Done. Cool.
|
| } |
| private: |
| - scoped_refptr<MessageLoopProxy> message_loop_proxy_; |
| - FilePath file_path_; |
| - int file_flags_; |
| - FileUtilProxy::CreateOrOpenCallback callback_; |
| PlatformFile file_handle_; |
| bool created_; |
| + PlatformFileError error_; |
| }; |
|
willchan no longer on Chromium
2011/10/19 00:26:52
DISALLOW_COPY_AND_ASSIGN
kinuko
2011/10/19 08:42:05
Done.
|
| -class RelayCreateTemporary : public MessageLoopRelay { |
| +class CreateTemporaryHelper { |
| public: |
| - RelayCreateTemporary( |
| - scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| - int additional_file_flags, |
| - const FileUtilProxy::CreateTemporaryCallback& callback) |
| - : message_loop_proxy_(message_loop_proxy), |
| - additional_file_flags_(additional_file_flags), |
| - callback_(callback), |
| - file_handle_(kInvalidPlatformFileValue) { |
| - DCHECK_EQ(false, callback.is_null()); |
| - } |
| + CreateTemporaryHelper() |
| + : file_handle_(kInvalidPlatformFileValue), |
| + error_(PLATFORM_FILE_OK) {} |
| - protected: |
| - virtual ~RelayCreateTemporary() { |
| - if (file_handle_ != kInvalidPlatformFileValue) |
| - FileUtilProxy::Close(message_loop_proxy_, file_handle_, |
| - FileUtilProxy::StatusCallback()); |
| - } |
| - |
| - virtual void RunWork() { |
| + void RunWork(int additional_file_flags) { |
| // TODO(darin): file_util should have a variant of CreateTemporaryFile |
| // that returns a FilePath and a PlatformFile. |
| file_util::CreateTemporaryFile(&file_path_); |
| @@ -133,350 +143,128 @@ class RelayCreateTemporary : public MessageLoopRelay { |
| PLATFORM_FILE_WRITE | |
| PLATFORM_FILE_TEMPORARY | |
| PLATFORM_FILE_CREATE_ALWAYS | |
| - additional_file_flags_; |
| + additional_file_flags; |
| - PlatformFileError error_code = PLATFORM_FILE_OK; |
| - file_handle_ = CreatePlatformFile(file_path_, file_flags, |
| - NULL, &error_code); |
| - set_error_code(error_code); |
| + error_ = PLATFORM_FILE_OK; |
| + file_handle_ = CreatePlatformFile(file_path_, file_flags, NULL, &error_); |
| } |
| - virtual void RunCallback() { |
| - callback_.Run(error_code(), PassPlatformFile(&file_handle_), |
| - file_path_); |
| + void Reply(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
|
willchan no longer on Chromium
2011/10/19 00:26:52
MessageLoopProxy* instead. What's important is the
kinuko
2011/10/19 08:42:05
Done. Actually it seems the Close needs to be cal
|
| + const FileUtilProxy::CreateTemporaryCallback& callback) { |
| + DCHECK(!callback.is_null()); |
| + callback.Run(error_, PassPlatformFile(&file_handle_), file_path_); |
| + if (file_handle_ != kInvalidPlatformFileValue) { |
| + FileUtilProxy::Close(message_loop_proxy, file_handle_, |
| + FileUtilProxy::StatusCallback()); |
| + } |
| + delete this; |
| } |
| private: |
| - scoped_refptr<MessageLoopProxy> message_loop_proxy_; |
| - int additional_file_flags_; |
| - FileUtilProxy::CreateTemporaryCallback callback_; |
| PlatformFile file_handle_; |
| FilePath file_path_; |
| + PlatformFileError error_; |
| }; |
| -class RelayWithStatusCallback : public MessageLoopRelay { |
| - public: |
| - explicit RelayWithStatusCallback( |
| - const FileUtilProxy::StatusCallback& callback) |
| - : callback_(callback) { |
| - // It is OK for callback to be NULL. |
| - } |
| - |
| - protected: |
| - virtual void RunCallback() { |
| - // The caller may not have been interested in the result. |
| - if (!callback_.is_null()) |
| - callback_.Run(error_code()); |
| - } |
| - |
| - private: |
| - FileUtilProxy::StatusCallback callback_; |
| -}; |
| - |
| -class RelayClose : public RelayWithStatusCallback { |
| - public: |
| - RelayClose(PlatformFile file_handle, |
| - const FileUtilProxy::StatusCallback& callback) |
| - : RelayWithStatusCallback(callback), |
| - file_handle_(file_handle) { |
| - } |
| - |
| - protected: |
| - virtual void RunWork() { |
| - if (!ClosePlatformFile(file_handle_)) |
| - set_error_code(PLATFORM_FILE_ERROR_FAILED); |
| - } |
| - |
| - private: |
| - PlatformFile file_handle_; |
| -}; |
| - |
| -class RelayDelete : public RelayWithStatusCallback { |
| - public: |
| - RelayDelete(const FilePath& file_path, |
| - bool recursive, |
| - const FileUtilProxy::StatusCallback& callback) |
| - : RelayWithStatusCallback(callback), |
| - file_path_(file_path), |
| - recursive_(recursive) { |
| +PlatformFileError DeleteHelper(const FilePath& file_path, bool recursive) { |
| + if (!file_util::PathExists(file_path)) { |
| + return PLATFORM_FILE_ERROR_NOT_FOUND; |
| } |
| - |
| - protected: |
| - virtual void RunWork() { |
| - if (!file_util::PathExists(file_path_)) { |
| - set_error_code(PLATFORM_FILE_ERROR_NOT_FOUND); |
| - return; |
| - } |
| - if (!file_util::Delete(file_path_, recursive_)) { |
| - if (!recursive_ && !file_util::IsDirectoryEmpty(file_path_)) { |
| - set_error_code(PLATFORM_FILE_ERROR_NOT_EMPTY); |
| - return; |
| - } |
| - set_error_code(PLATFORM_FILE_ERROR_FAILED); |
| + if (!file_util::Delete(file_path, recursive)) { |
| + if (!recursive && !file_util::IsDirectoryEmpty(file_path)) { |
| + return PLATFORM_FILE_ERROR_NOT_EMPTY; |
| } |
| + return PLATFORM_FILE_ERROR_FAILED; |
| } |
| + return PLATFORM_FILE_OK; |
| +} |
| - private: |
| - FilePath file_path_; |
| - bool recursive_; |
| -}; |
| - |
| -class RelayGetFileInfo : public MessageLoopRelay { |
| +class GetFileInfoHelper { |
| public: |
| - RelayGetFileInfo(const FilePath& file_path, |
| - const FileUtilProxy::GetFileInfoCallback& callback) |
| - : callback_(callback), |
| - file_path_(file_path) { |
| - DCHECK_EQ(false, callback.is_null()); |
| - } |
| + GetFileInfoHelper() |
| + : error_(PLATFORM_FILE_OK) {} |
| - protected: |
| - virtual void RunWork() { |
| - if (!file_util::PathExists(file_path_)) { |
| - set_error_code(PLATFORM_FILE_ERROR_NOT_FOUND); |
| + void RunWorkForFilePath(const FilePath& file_path) { |
| + if (!file_util::PathExists(file_path)) { |
| + error_ = PLATFORM_FILE_ERROR_NOT_FOUND; |
| return; |
| } |
| - if (!file_util::GetFileInfo(file_path_, &file_info_)) |
| - set_error_code(PLATFORM_FILE_ERROR_FAILED); |
| + if (!file_util::GetFileInfo(file_path, &file_info_)) |
| + error_ = PLATFORM_FILE_ERROR_FAILED; |
| } |
| - virtual void RunCallback() { |
| - callback_.Run(error_code(), file_info_); |
| + void RunWorkForPlatformFile(PlatformFile file) { |
| + if (!GetPlatformFileInfo(file, &file_info_)) |
| + error_ = PLATFORM_FILE_ERROR_FAILED; |
| } |
| - private: |
| - FileUtilProxy::GetFileInfoCallback callback_; |
| - FilePath file_path_; |
| - PlatformFileInfo file_info_; |
| -}; |
| - |
| -class RelayGetFileInfoFromPlatformFile : public MessageLoopRelay { |
| - public: |
| - RelayGetFileInfoFromPlatformFile( |
| - PlatformFile file, |
| - const FileUtilProxy::GetFileInfoCallback& callback) |
| - : callback_(callback), |
| - file_(file) { |
| - DCHECK_EQ(false, callback.is_null()); |
| - } |
| - |
| - protected: |
| - virtual void RunWork() { |
| - if (!GetPlatformFileInfo(file_, &file_info_)) |
| - set_error_code(PLATFORM_FILE_ERROR_FAILED); |
| - } |
| - |
| - virtual void RunCallback() { |
| - callback_.Run(error_code(), file_info_); |
| + void Reply(const FileUtilProxy::GetFileInfoCallback& callback) { |
| + if (!callback.is_null()) { |
| + callback.Run(error_, file_info_); |
| + } |
| + delete this; |
| } |
| private: |
| - FileUtilProxy::GetFileInfoCallback callback_; |
| - PlatformFile file_; |
| + PlatformFileError error_; |
| PlatformFileInfo file_info_; |
| }; |
| -class RelayRead : public MessageLoopRelay { |
| +class ReadHelper { |
| public: |
| - RelayRead(PlatformFile file, |
| - int64 offset, |
| - int bytes_to_read, |
| - const FileUtilProxy::ReadCallback& callback) |
| - : file_(file), |
| - offset_(offset), |
| - buffer_(new char[bytes_to_read]), |
| + ReadHelper(int bytes_to_read) |
| + : buffer_(new char[bytes_to_read]), |
| bytes_to_read_(bytes_to_read), |
| - callback_(callback), |
| - bytes_read_(0) { |
| - } |
| + bytes_read_(0) {} |
| - protected: |
| - virtual void RunWork() { |
| - bytes_read_ = ReadPlatformFile(file_, offset_, buffer_.get(), |
| - bytes_to_read_); |
| - if (bytes_read_ < 0) |
| - set_error_code(PLATFORM_FILE_ERROR_FAILED); |
| + void RunWork(PlatformFile file, int64 offset) { |
| + bytes_read_ = ReadPlatformFile(file, offset, buffer_.get(), bytes_to_read_); |
| } |
| - virtual void RunCallback() { |
| - if (!callback_.is_null()) |
| - callback_.Run(error_code(), buffer_.get(), bytes_read_); |
| + void Reply(const FileUtilProxy::ReadCallback& callback) { |
| + if (!callback.is_null()) { |
| + PlatformFileError error = |
| + (bytes_read_ < 0) ? PLATFORM_FILE_ERROR_FAILED : PLATFORM_FILE_OK; |
| + callback.Run(error, buffer_.get(), bytes_read_); |
| + } |
| + delete this; |
| } |
| private: |
| - PlatformFile file_; |
| - int64 offset_; |
| scoped_array<char> buffer_; |
| int bytes_to_read_; |
| - FileUtilProxy::ReadCallback callback_; |
| int bytes_read_; |
| }; |
| -class RelayWrite : public MessageLoopRelay { |
| +class WriteHelper { |
| public: |
| - RelayWrite(PlatformFile file, |
| - int64 offset, |
| - const char* buffer, |
| - int bytes_to_write, |
| - const FileUtilProxy::WriteCallback& callback) |
| - : file_(file), |
| - offset_(offset), |
| - buffer_(new char[bytes_to_write]), |
| + WriteHelper(const char* buffer, int bytes_to_write) |
| + : buffer_(new char[bytes_to_write]), |
| bytes_to_write_(bytes_to_write), |
| - callback_(callback), |
| bytes_written_(0) { |
| memcpy(buffer_.get(), buffer, bytes_to_write); |
| } |
| - protected: |
| - virtual void RunWork() { |
| - bytes_written_ = WritePlatformFile(file_, offset_, buffer_.get(), |
| - bytes_to_write_); |
| - if (bytes_written_ < 0) |
| - set_error_code(PLATFORM_FILE_ERROR_FAILED); |
| + void RunWork(PlatformFile file, int64 offset) { |
| + bytes_written_ = WritePlatformFile(file, offset, buffer_.get(), |
| + bytes_to_write_); |
| } |
| - virtual void RunCallback() { |
| - if (!callback_.is_null()) |
| - callback_.Run(error_code(), bytes_written_); |
| + void Reply(const FileUtilProxy::WriteCallback& callback) { |
| + if (!callback.is_null()) { |
| + PlatformFileError error = |
| + (bytes_written_ < 0) ? PLATFORM_FILE_ERROR_FAILED : PLATFORM_FILE_OK; |
| + callback.Run(error, bytes_written_); |
| + } |
| + delete this; |
| } |
| private: |
| - PlatformFile file_; |
| - int64 offset_; |
| scoped_array<char> buffer_; |
| int bytes_to_write_; |
| - FileUtilProxy::WriteCallback callback_; |
| int bytes_written_; |
| }; |
| -class RelayTouch : public RelayWithStatusCallback { |
| - public: |
| - RelayTouch(PlatformFile file, |
| - const Time& last_access_time, |
| - const Time& last_modified_time, |
| - const FileUtilProxy::StatusCallback& callback) |
| - : RelayWithStatusCallback(callback), |
| - file_(file), |
| - last_access_time_(last_access_time), |
| - last_modified_time_(last_modified_time) { |
| - } |
| - |
| - protected: |
| - virtual void RunWork() { |
| - if (!TouchPlatformFile(file_, last_access_time_, last_modified_time_)) |
| - set_error_code(PLATFORM_FILE_ERROR_FAILED); |
| - } |
| - |
| - private: |
| - PlatformFile file_; |
| - Time last_access_time_; |
| - Time last_modified_time_; |
| -}; |
| - |
| -class RelayTouchFilePath : public RelayWithStatusCallback { |
| - public: |
| - RelayTouchFilePath(const FilePath& file_path, |
| - const Time& last_access_time, |
| - const Time& last_modified_time, |
| - const FileUtilProxy::StatusCallback& callback) |
| - : RelayWithStatusCallback(callback), |
| - file_path_(file_path), |
| - last_access_time_(last_access_time), |
| - last_modified_time_(last_modified_time) { |
| - } |
| - |
| - protected: |
| - virtual void RunWork() { |
| - if (!file_util::TouchFile( |
| - file_path_, last_access_time_, last_modified_time_)) |
| - set_error_code(PLATFORM_FILE_ERROR_FAILED); |
| - } |
| - |
| - private: |
| - FilePath file_path_; |
| - Time last_access_time_; |
| - Time last_modified_time_; |
| -}; |
| - |
| -class RelayTruncatePlatformFile : public RelayWithStatusCallback { |
| - public: |
| - RelayTruncatePlatformFile(PlatformFile file, |
| - int64 length, |
| - const FileUtilProxy::StatusCallback& callback) |
| - : RelayWithStatusCallback(callback), |
| - file_(file), |
| - length_(length) { |
| - } |
| - |
| - protected: |
| - virtual void RunWork() { |
| - if (!TruncatePlatformFile(file_, length_)) |
| - set_error_code(PLATFORM_FILE_ERROR_FAILED); |
| - } |
| - |
| - private: |
| - PlatformFile file_; |
| - int64 length_; |
| -}; |
| - |
| -class RelayTruncate : public RelayWithStatusCallback { |
| - public: |
| - RelayTruncate(const FilePath& path, |
| - int64 length, |
| - const FileUtilProxy::StatusCallback& callback) |
| - : RelayWithStatusCallback(callback), |
| - path_(path), |
| - length_(length) { |
| - } |
| - |
| - protected: |
| - virtual void RunWork() { |
| - PlatformFileError error_code(PLATFORM_FILE_ERROR_FAILED); |
| - PlatformFile file = |
| - CreatePlatformFile( |
| - path_, |
| - PLATFORM_FILE_OPEN | PLATFORM_FILE_WRITE, |
| - NULL, |
| - &error_code); |
| - if (error_code != PLATFORM_FILE_OK) { |
| - set_error_code(error_code); |
| - return; |
| - } |
| - if (!TruncatePlatformFile(file, length_)) |
| - set_error_code(PLATFORM_FILE_ERROR_FAILED); |
| - ClosePlatformFile(file); |
| - } |
| - |
| - private: |
| - FilePath path_; |
| - int64 length_; |
| -}; |
| - |
| -class RelayFlush : public RelayWithStatusCallback { |
| - public: |
| - RelayFlush(PlatformFile file, |
| - const FileUtilProxy::StatusCallback& callback) |
| - : RelayWithStatusCallback(callback), |
| - file_(file) { |
| - } |
| - |
| - protected: |
| - virtual void RunWork() { |
| - if (!FlushPlatformFile(file_)) |
| - set_error_code(PLATFORM_FILE_ERROR_FAILED); |
| - } |
| - |
| - private: |
| - PlatformFile file_; |
| -}; |
| - |
| -bool Start(const tracked_objects::Location& from_here, |
| - scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| - scoped_refptr<MessageLoopRelay> relay) { |
| - return relay->Start(message_loop_proxy, from_here); |
| -} |
| - |
| } // namespace |
| // static |
| @@ -484,8 +272,17 @@ bool FileUtilProxy::CreateOrOpen( |
| scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| const FilePath& file_path, int file_flags, |
| const CreateOrOpenCallback& callback) { |
| - return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( |
| - message_loop_proxy, file_path, file_flags, callback)); |
| + CreateOrOpenHelper* helper = new CreateOrOpenHelper; |
| + if (!message_loop_proxy->PostTaskAndReply( |
| + FROM_HERE, |
| + Bind(&CreateOrOpenHelper::RunWork, base::Unretained(helper), |
| + file_path, file_flags), |
| + Bind(&CreateOrOpenHelper::Reply, base::Unretained(helper), |
| + message_loop_proxy, callback))) { |
| + delete helper; |
| + return false; |
| + } |
| + return true; |
| } |
| // static |
| @@ -493,18 +290,27 @@ bool FileUtilProxy::CreateTemporary( |
| scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| int additional_file_flags, |
| const CreateTemporaryCallback& callback) { |
| - return Start(FROM_HERE, message_loop_proxy, |
| - new RelayCreateTemporary(message_loop_proxy, |
| - additional_file_flags, |
| - callback)); |
| + CreateTemporaryHelper* helper = new CreateTemporaryHelper; |
| + if (!message_loop_proxy->PostTaskAndReply( |
| + FROM_HERE, |
| + Bind(&CreateTemporaryHelper::RunWork, base::Unretained(helper), |
| + additional_file_flags), |
| + Bind(&CreateTemporaryHelper::Reply, base::Unretained(helper), |
| + message_loop_proxy, callback))) { |
| + delete helper; |
| + return false; |
| + } |
| + return true; |
| } |
| // static |
| bool FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| PlatformFile file_handle, |
| const StatusCallback& callback) { |
| - return Start(FROM_HERE, message_loop_proxy, |
| - new RelayClose(file_handle, callback)); |
| + return PostTaskAndReplyWithStatus<bool>( |
| + message_loop_proxy, FROM_HERE, |
| + Bind(&ClosePlatformFile, file_handle), callback, |
| + new PlatformFileError); |
| } |
| // Retrieves the information about a file. It is invalid to pass NULL for the |
| @@ -513,8 +319,16 @@ bool FileUtilProxy::GetFileInfo( |
| scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| const FilePath& file_path, |
| const GetFileInfoCallback& callback) { |
| - return Start(FROM_HERE, message_loop_proxy, new RelayGetFileInfo( |
| - file_path, callback)); |
| + GetFileInfoHelper* helper = new GetFileInfoHelper; |
| + if (!message_loop_proxy->PostTaskAndReply( |
| + FROM_HERE, |
| + Bind(&GetFileInfoHelper::RunWorkForFilePath, |
| + base::Unretained(helper), file_path), |
| + Bind(&GetFileInfoHelper::Reply, base::Unretained(helper), callback))) { |
| + delete helper; |
| + return false; |
| + } |
| + return true; |
| } |
| // static |
| @@ -522,8 +336,16 @@ bool FileUtilProxy::GetFileInfoFromPlatformFile( |
| scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| PlatformFile file, |
| const GetFileInfoCallback& callback) { |
| - return Start(FROM_HERE, message_loop_proxy, |
| - new RelayGetFileInfoFromPlatformFile(file, callback)); |
| + GetFileInfoHelper* helper = new GetFileInfoHelper; |
| + if (!message_loop_proxy->PostTaskAndReply( |
| + FROM_HERE, |
| + Bind(&GetFileInfoHelper::RunWorkForPlatformFile, |
| + base::Unretained(helper), file), |
| + Bind(&GetFileInfoHelper::Reply, base::Unretained(helper), callback))) { |
| + delete helper; |
| + return false; |
| + } |
| + return true; |
| } |
| // static |
| @@ -531,8 +353,10 @@ bool FileUtilProxy::Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| const FilePath& file_path, |
| bool recursive, |
| const StatusCallback& callback) { |
| - return Start(FROM_HERE, message_loop_proxy, |
| - new RelayDelete(file_path, recursive, callback)); |
| + return PostTaskAndReplyWithStatus<PlatformFileError>( |
| + message_loop_proxy, FROM_HERE, |
| + Bind(&DeleteHelper, file_path, recursive), callback, |
| + new PlatformFileError); |
| } |
| // static |
| @@ -540,8 +364,10 @@ bool FileUtilProxy::RecursiveDelete( |
| scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| const FilePath& file_path, |
| const StatusCallback& callback) { |
| - return Start(FROM_HERE, message_loop_proxy, |
| - new RelayDelete(file_path, true, callback)); |
| + return PostTaskAndReplyWithStatus<PlatformFileError>( |
| + message_loop_proxy, FROM_HERE, |
| + Bind(&DeleteHelper, file_path, true /* recursive */), callback, |
| + new PlatformFileError); |
| } |
| // static |
| @@ -551,11 +377,18 @@ bool FileUtilProxy::Read( |
| int64 offset, |
| int bytes_to_read, |
| const ReadCallback& callback) { |
| - if (bytes_to_read < 0) |
| + if (bytes_to_read < 0) { |
| return false; |
| - |
| - return Start(FROM_HERE, message_loop_proxy, |
| - new RelayRead(file, offset, bytes_to_read, callback)); |
| + } |
| + ReadHelper* helper = new ReadHelper(bytes_to_read); |
| + if (!message_loop_proxy->PostTaskAndReply( |
| + FROM_HERE, |
| + Bind(&ReadHelper::RunWork, base::Unretained(helper), file, offset), |
| + Bind(&ReadHelper::Reply, base::Unretained(helper), callback))) { |
| + delete helper; |
| + return false; |
| + } |
| + return true; |
| } |
| // static |
| @@ -566,11 +399,18 @@ bool FileUtilProxy::Write( |
| const char* buffer, |
| int bytes_to_write, |
| const WriteCallback& callback) { |
| - if (bytes_to_write <= 0) |
| + if (bytes_to_write <= 0 || buffer == NULL) { |
| return false; |
| - |
| - return Start(FROM_HERE, message_loop_proxy, |
| - new RelayWrite(file, offset, buffer, bytes_to_write, callback)); |
| + } |
| + WriteHelper* helper = new WriteHelper(buffer, bytes_to_write); |
| + if (!message_loop_proxy->PostTaskAndReply( |
| + FROM_HERE, |
| + Bind(&WriteHelper::RunWork, base::Unretained(helper), file, offset), |
| + Bind(&WriteHelper::Reply, base::Unretained(helper), callback))) { |
| + delete helper; |
| + return false; |
| + } |
| + return true; |
| } |
| // static |
| @@ -580,9 +420,11 @@ bool FileUtilProxy::Touch( |
| const Time& last_access_time, |
| const Time& last_modified_time, |
| const StatusCallback& callback) { |
| - return Start(FROM_HERE, message_loop_proxy, |
| - new RelayTouch(file, last_access_time, last_modified_time, |
| - callback)); |
| + return PostTaskAndReplyWithStatus<bool>( |
| + message_loop_proxy, FROM_HERE, |
| + Bind(&TouchPlatformFile, file, |
| + last_access_time, last_modified_time), callback, |
| + new PlatformFileError); |
| } |
| // static |
| @@ -592,9 +434,12 @@ bool FileUtilProxy::Touch( |
| const Time& last_access_time, |
| const Time& last_modified_time, |
| const StatusCallback& callback) { |
| - return Start(FROM_HERE, message_loop_proxy, |
| - new RelayTouchFilePath(file_path, last_access_time, |
| - last_modified_time, callback)); |
| + return PostTaskAndReplyWithStatus<bool>( |
| + message_loop_proxy, FROM_HERE, |
| + Bind(&file_util::TouchFile, file_path, |
| + last_access_time, last_modified_time), |
| + callback, |
| + new PlatformFileError); |
| } |
| // static |
| @@ -603,18 +448,10 @@ bool FileUtilProxy::Truncate( |
| PlatformFile file, |
| int64 length, |
| const StatusCallback& callback) { |
| - return Start(FROM_HERE, message_loop_proxy, |
| - new RelayTruncatePlatformFile(file, length, callback)); |
| -} |
| - |
| -// static |
| -bool FileUtilProxy::Truncate( |
| - scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| - const FilePath& path, |
| - int64 length, |
| - const StatusCallback& callback) { |
| - return Start(FROM_HERE, message_loop_proxy, |
| - new RelayTruncate(path, length, callback)); |
| + return PostTaskAndReplyWithStatus<bool>( |
| + message_loop_proxy, FROM_HERE, |
| + Bind(&TruncatePlatformFile, file, length), callback, |
| + new PlatformFileError); |
| } |
| // static |
| @@ -622,7 +459,10 @@ bool FileUtilProxy::Flush( |
| scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| PlatformFile file, |
| const StatusCallback& callback) { |
| - return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); |
| + return PostTaskAndReplyWithStatus<bool>( |
| + message_loop_proxy, FROM_HERE, |
| + Bind(&FlushPlatformFile, file), callback, |
| + new PlatformFileError); |
| } |
| } // namespace base |