diff options
author | kinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-25 09:37:19 +0000 |
---|---|---|
committer | kinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-25 09:37:19 +0000 |
commit | 8b2eb4bd4b8da8aa86b89c22f4b4d2b045f28b06 (patch) | |
tree | d44886951197dcf5dc0cf9ca53a3f01ad4213816 /base/file_util_proxy.cc | |
parent | 483623eb5349d9b1ffee623feaaa53c3e2c63732 (diff) | |
download | chromium_src-8b2eb4bd4b8da8aa86b89c22f4b4d2b045f28b06.zip chromium_src-8b2eb4bd4b8da8aa86b89c22f4b4d2b045f28b06.tar.gz chromium_src-8b2eb4bd4b8da8aa86b89c22f4b4d2b045f28b06.tar.bz2 |
Reimplementing FileUtilProxy using PostTaskAndReply
Splitted from http://codereview.chromium.org/8231004/
BUG=none
TEST=existing tests should pass
Review URL: http://codereview.chromium.org/8339012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@107088 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/file_util_proxy.cc')
-rw-r--r-- | base/file_util_proxy.cc | 634 |
1 files changed, 225 insertions, 409 deletions
diff --git a/base/file_util_proxy.cc b/base/file_util_proxy.cc index 3e6a365..4ca68f1 100644 --- a/base/file_util_proxy.cc +++ b/base/file_util_proxy.cc @@ -5,126 +5,142 @@ #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) { - } - - 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)); - } +// 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), Owned(result))); +// +// Or just use PostTaskAndReplyWithStatus helper template (see the code below). +template <typename R1, typename R2> +struct ReturnValueTranslator { + static R2 Value(const R1& value); +}; - protected: - friend class RefCountedThreadSafe<MessageLoopRelay>; - virtual ~MessageLoopRelay() {} +template <typename R> +struct ReturnValueTranslator<R, R> { + static R Value(const R& value) { return value; } +}; - // Called to perform work on the FILE thread. - virtual void RunWork() = 0; +template <> +struct ReturnValueTranslator<bool, PlatformFileError> { + static PlatformFileError Value(const bool& value) { + if (value) + return PLATFORM_FILE_OK; + return PLATFORM_FILE_ERROR_FAILED; + } +}; - // Called to notify the callback on the origin thread. - virtual void RunCallback() = 0; +template <typename R1, typename R2> +void ReturnAsParamAdapter(const Callback<R1(void)>& func, R2* result) { + if (!func.is_null()) + *result = ReturnValueTranslator<R1, R2>::Value(func.Run()); +} - void set_error_code(PlatformFileError error_code) { - error_code_ = error_code; - } +template <typename R1, typename R2> +Closure ReturnAsParam(const Callback<R1(void)>& func, R2* result) { + DCHECK(result); + return Bind(&ReturnAsParamAdapter<R1, R2>, func, result); +} - PlatformFileError error_code() const { - return error_code_; - } +template <typename R> +void ReplyAdapter(const Callback<void(R)>& callback, R* result) { + DCHECK(result); + if (!callback.is_null()) + callback.Run(*result); +} - private: - void ProcessOnTargetThread() { - RunWork(); - origin_message_loop_proxy_->PostTask( - FROM_HERE, Bind(&MessageLoopRelay::RunCallback, this)); - } +template <typename R, typename OWNED> +Closure ReplyHelper(const Callback<void(R)>& callback, OWNED result) { + return Bind(&ReplyAdapter<R>, callback, result); +} - scoped_refptr<MessageLoopProxy> origin_message_loop_proxy_; - PlatformFileError error_code_; -}; +// Putting everything together. +template <typename R1, typename R2> +bool PostTaskAndReplyWithStatus( + const 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) { + return message_loop_proxy->PostTaskAndReply( + from_here, + ReturnAsParam<R1>(file_util_work, result), + ReplyHelper(callback, Owned(result))); +} -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) + CreateOrOpenHelper(MessageLoopProxy* message_loop_proxy) : 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()); - } + created_(false), + error_(PLATFORM_FILE_OK) {} - protected: - virtual ~RelayCreateOrOpen() { - if (file_handle_ != kInvalidPlatformFileValue) + ~CreateOrOpenHelper() { + if (file_handle_ != kInvalidPlatformFileValue) { FileUtilProxy::Close(message_loop_proxy_, file_handle_, - FileUtilProxy::StatusCallback()); + 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); + error_ = PLATFORM_FILE_OK; + file_handle_ = CreatePlatformFile(file_path, file_flags, + &created_, &error_); } - virtual void RunCallback() { - callback_.Run(error_code(), PassPlatformFile(&file_handle_), - created_); + void Reply(const FileUtilProxy::CreateOrOpenCallback& callback) { + DCHECK(!callback.is_null()); + callback.Run(error_, PassPlatformFile(&file_handle_), created_); } private: scoped_refptr<MessageLoopProxy> message_loop_proxy_; - FilePath file_path_; - int file_flags_; - FileUtilProxy::CreateOrOpenCallback callback_; PlatformFile file_handle_; bool created_; + PlatformFileError error_; + DISALLOW_COPY_AND_ASSIGN(CreateOrOpenHelper); }; -class RelayCreateTemporary : public MessageLoopRelay { +class CreateTemporaryHelper { public: - RelayCreateTemporary( - scoped_refptr<MessageLoopProxy> message_loop_proxy, - int additional_file_flags, - const FileUtilProxy::CreateTemporaryCallback& callback) + CreateTemporaryHelper(MessageLoopProxy* message_loop_proxy) : message_loop_proxy_(message_loop_proxy), - additional_file_flags_(additional_file_flags), - callback_(callback), - file_handle_(kInvalidPlatformFileValue) { - DCHECK_EQ(false, callback.is_null()); - } + file_handle_(kInvalidPlatformFileValue), + error_(PLATFORM_FILE_OK) {} - protected: - virtual ~RelayCreateTemporary() { - if (file_handle_ != kInvalidPlatformFileValue) + ~CreateTemporaryHelper() { + if (file_handle_ != kInvalidPlatformFileValue) { FileUtilProxy::Close(message_loop_proxy_, file_handle_, - FileUtilProxy::StatusCallback()); + 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 +149,124 @@ 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(const FileUtilProxy::CreateTemporaryCallback& callback) { + DCHECK(!callback.is_null()); + callback.Run(error_, PassPlatformFile(&file_handle_), file_path_); } private: scoped_refptr<MessageLoopProxy> message_loop_proxy_; - int additional_file_flags_; - FileUtilProxy::CreateTemporaryCallback callback_; PlatformFile file_handle_; FilePath file_path_; + PlatformFileError error_; + DISALLOW_COPY_AND_ASSIGN(CreateTemporaryHelper); }; -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()); +PlatformFileError DeleteHelper(const FilePath& file_path, bool recursive) { + if (!file_util::PathExists(file_path)) { + return PLATFORM_FILE_ERROR_NOT_FOUND; } - - 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) { - } - - 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); - } - - virtual void RunCallback() { - callback_.Run(error_code(), file_info_); - } - - 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()); + if (!file_util::GetFileInfo(file_path, &file_info_)) + error_ = PLATFORM_FILE_ERROR_FAILED; } - protected: - virtual void RunWork() { - if (!GetPlatformFileInfo(file_, &file_info_)) - set_error_code(PLATFORM_FILE_ERROR_FAILED); + void RunWorkForPlatformFile(PlatformFile file) { + if (!GetPlatformFileInfo(file, &file_info_)) + error_ = 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_); + } } private: - FileUtilProxy::GetFileInfoCallback callback_; - PlatformFile file_; + PlatformFileError error_; PlatformFileInfo file_info_; + DISALLOW_COPY_AND_ASSIGN(GetFileInfoHelper); }; -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_); + } } private: - PlatformFile file_; - int64 offset_; scoped_array<char> buffer_; int bytes_to_read_; - FileUtilProxy::ReadCallback callback_; int bytes_read_; + DISALLOW_COPY_AND_ASSIGN(ReadHelper); }; -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_); + } } private: - PlatformFile file_; - int64 offset_; scoped_array<char> buffer_; int bytes_to_write_; - FileUtilProxy::WriteCallback callback_; int bytes_written_; + DISALLOW_COPY_AND_ASSIGN(WriteHelper); }; -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 +274,12 @@ 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(message_loop_proxy); + return message_loop_proxy->PostTaskAndReply( + FROM_HERE, + Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), + file_path, file_flags), + Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback)); } // static @@ -493,18 +287,22 @@ 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(message_loop_proxy); + return message_loop_proxy->PostTaskAndReply( + FROM_HERE, + Bind(&CreateTemporaryHelper::RunWork, Unretained(helper), + additional_file_flags), + Bind(&CreateTemporaryHelper::Reply, Owned(helper), callback)); } // 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 +311,12 @@ 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; + return message_loop_proxy->PostTaskAndReply( + FROM_HERE, + Bind(&GetFileInfoHelper::RunWorkForFilePath, + Unretained(helper), file_path), + Bind(&GetFileInfoHelper::Reply, Owned(helper), callback)); } // static @@ -522,8 +324,12 @@ 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; + return message_loop_proxy->PostTaskAndReply( + FROM_HERE, + Bind(&GetFileInfoHelper::RunWorkForPlatformFile, + Unretained(helper), file), + Bind(&GetFileInfoHelper::Reply, Owned(helper), callback)); } // static @@ -531,8 +337,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 +348,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 +361,14 @@ 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); + return message_loop_proxy->PostTaskAndReply( + FROM_HERE, + Bind(&ReadHelper::RunWork, Unretained(helper), file, offset), + Bind(&ReadHelper::Reply, Owned(helper), callback)); } // static @@ -566,11 +379,14 @@ 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); + return message_loop_proxy->PostTaskAndReply( + FROM_HERE, + Bind(&WriteHelper::RunWork, Unretained(helper), file, offset), + Bind(&WriteHelper::Reply, Owned(helper), callback)); } // static @@ -580,9 +396,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 +410,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 +424,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 +435,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 |