| Index: base/file_util_proxy.cc
|
| diff --git a/base/file_util_proxy.cc b/base/file_util_proxy.cc
|
| index 0873c9ccd5b3f61f7b2e84f75a8ed36c9ac6b1b9..655198f7dc986421e808fca98b4ef288f3fba558 100644
|
| --- a/base/file_util_proxy.cc
|
| +++ b/base/file_util_proxy.cc
|
| @@ -13,89 +13,10 @@ namespace base {
|
|
|
| namespace {
|
|
|
| -// 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),
|
| -// RelayHelper(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);
|
| -};
|
| -
|
| -template <typename R>
|
| -struct ReturnValueTranslator<R, R> {
|
| - static R Value(const R& value) { return value; }
|
| -};
|
| -
|
| -template <>
|
| -struct ReturnValueTranslator<bool, PlatformFileError> {
|
| - static PlatformFileError Value(const bool& value) {
|
| - if (value)
|
| - return PLATFORM_FILE_OK;
|
| - return PLATFORM_FILE_ERROR_FAILED;
|
| - }
|
| -};
|
| -
|
| -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());
|
| -}
|
| -
|
| -template <typename R1, typename R2>
|
| -Closure ReturnAsParam(const Callback<R1(void)>& func, R2* result) {
|
| - DCHECK(result);
|
| - return Bind(&ReturnAsParamAdapter<R1, R2>, func, result);
|
| -}
|
| -
|
| -template <typename R, typename A1>
|
| -void ReturnAsParamAdapter1(const Callback<R(A1)>& func, A1 a1, R* result) {
|
| - if (!func.is_null())
|
| - *result = func.Run(a1);
|
| -}
|
| -
|
| -template <typename R, typename A1>
|
| -Closure ReturnAsParam(const Callback<R(A1)>& func, A1 a1, R* result) {
|
| - DCHECK(result);
|
| - return Bind(&ReturnAsParamAdapter1<R, A1>, func, a1, result);
|
| -}
|
| -
|
| -template <typename R>
|
| -void ReplyAdapter(const Callback<void(R)>& callback, R* result) {
|
| - DCHECK(result);
|
| - if (!callback.is_null())
|
| - callback.Run(*result);
|
| -}
|
| -
|
| -template <typename R, typename OWNED>
|
| -Closure ReplyHelper(const Callback<void(R)>& callback, OWNED result) {
|
| - return Bind(&ReplyAdapter<R>, callback, result);
|
| -}
|
| -
|
| -// 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)));
|
| +void CallWithTranslatedParameter(const FileUtilProxy::StatusCallback& callback,
|
| + bool value) {
|
| + DCHECK(!callback.is_null());
|
| + callback.Run(value ? PLATFORM_FILE_OK : PLATFORM_FILE_ERROR_FAILED);
|
| }
|
|
|
| // Helper classes or routines for individual methods.
|
| @@ -427,11 +348,11 @@ bool FileUtilProxy::Touch(
|
| const Time& last_access_time,
|
| const Time& last_modified_time,
|
| const StatusCallback& callback) {
|
| - return PostTaskAndReplyWithStatus<bool>(
|
| - message_loop_proxy, FROM_HERE,
|
| + return message_loop_proxy->PostTaskAndReplyWithResult(
|
| + FROM_HERE,
|
| Bind(&TouchPlatformFile, file,
|
| - last_access_time, last_modified_time), callback,
|
| - new PlatformFileError);
|
| + last_access_time, last_modified_time),
|
| + Bind(&CallWithTranslatedParameter, callback));
|
| }
|
|
|
| // static
|
| @@ -441,12 +362,11 @@ bool FileUtilProxy::Touch(
|
| const Time& last_access_time,
|
| const Time& last_modified_time,
|
| const StatusCallback& callback) {
|
| - return PostTaskAndReplyWithStatus<bool>(
|
| - message_loop_proxy, FROM_HERE,
|
| + return message_loop_proxy->PostTaskAndReplyWithResult(
|
| + FROM_HERE,
|
| Bind(&file_util::TouchFile, file_path,
|
| last_access_time, last_modified_time),
|
| - callback,
|
| - new PlatformFileError);
|
| + Bind(&CallWithTranslatedParameter, callback));
|
| }
|
|
|
| // static
|
| @@ -455,10 +375,10 @@ bool FileUtilProxy::Truncate(
|
| PlatformFile file,
|
| int64 length,
|
| const StatusCallback& callback) {
|
| - return PostTaskAndReplyWithStatus<bool>(
|
| - message_loop_proxy, FROM_HERE,
|
| - Bind(&TruncatePlatformFile, file, length), callback,
|
| - new PlatformFileError);
|
| + return message_loop_proxy->PostTaskAndReplyWithResult(
|
| + FROM_HERE,
|
| + Bind(&TruncatePlatformFile, file, length),
|
| + Bind(&CallWithTranslatedParameter, callback));
|
| }
|
|
|
| // static
|
| @@ -466,10 +386,10 @@ bool FileUtilProxy::Flush(
|
| scoped_refptr<MessageLoopProxy> message_loop_proxy,
|
| PlatformFile file,
|
| const StatusCallback& callback) {
|
| - return PostTaskAndReplyWithStatus<bool>(
|
| - message_loop_proxy, FROM_HERE,
|
| - Bind(&FlushPlatformFile, file), callback,
|
| - new PlatformFileError);
|
| + return message_loop_proxy->PostTaskAndReplyWithResult(
|
| + FROM_HERE,
|
| + Bind(&FlushPlatformFile, file),
|
| + Bind(&CallWithTranslatedParameter, callback));
|
| }
|
|
|
| // static
|
| @@ -478,11 +398,8 @@ bool FileUtilProxy::RelayFileTask(
|
| const tracked_objects::Location& from_here,
|
| const FileTask& file_task,
|
| const StatusCallback& callback) {
|
| - PlatformFileError* result = new PlatformFileError;
|
| - return message_loop_proxy->PostTaskAndReply(
|
| - from_here,
|
| - ReturnAsParam(file_task, result),
|
| - ReplyHelper(callback, Owned(result)));
|
| + return message_loop_proxy->PostTaskAndReplyWithResult(
|
| + from_here, file_task, callback);
|
| }
|
|
|
| // static
|
| @@ -505,11 +422,8 @@ bool FileUtilProxy::RelayClose(
|
| const CloseTask& close_task,
|
| PlatformFile file_handle,
|
| const StatusCallback& callback) {
|
| - PlatformFileError* result = new PlatformFileError;
|
| - return message_loop_proxy->PostTaskAndReply(
|
| - FROM_HERE,
|
| - ReturnAsParam(close_task, file_handle, result),
|
| - ReplyHelper(callback, Owned(result)));
|
| + return message_loop_proxy->PostTaskAndReplyWithResult(
|
| + FROM_HERE, Bind(close_task, file_handle), callback);
|
| }
|
|
|
| } // namespace base
|
|
|