OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_util_proxy.h" | 5 #include "base/file_util_proxy.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
| 11 #include "base/task_runner_helpers.h" |
11 | 12 |
12 namespace base { | 13 namespace base { |
13 | 14 |
14 namespace { | 15 // Translates a boolean return value into a PlatformFileError for callbacks in |
15 | 16 // the ReturnAsParam helper. |
16 // Helper templates to call file_util or base::PlatformFile methods | |
17 // and reply with the returned value. | |
18 // | |
19 // Typically when you have these methods: | |
20 // R DoWorkAndReturn(); | |
21 // void Callback(R& result); | |
22 // | |
23 // You can pass the result of DoWorkAndReturn to the Callback by: | |
24 // | |
25 // R* result = new R; | |
26 // message_loop_proxy->PostTaskAndReply( | |
27 // from_here, | |
28 // ReturnAsParam<R>(Bind(&DoWorkAndReturn), result), | |
29 // RelayHelper(Bind(&Callback), Owned(result))); | |
30 // | |
31 // Or just use PostTaskAndReplyWithStatus helper template (see the code below). | |
32 template <typename R1, typename R2> | |
33 struct ReturnValueTranslator { | |
34 static R2 Value(const R1& value); | |
35 }; | |
36 | |
37 template <typename R> | |
38 struct ReturnValueTranslator<R, R> { | |
39 static R Value(const R& value) { return value; } | |
40 }; | |
41 | |
42 template <> | 17 template <> |
43 struct ReturnValueTranslator<bool, PlatformFileError> { | 18 struct ReturnValueTranslator<bool, PlatformFileError> { |
44 static PlatformFileError Value(const bool& value) { | 19 static PlatformFileError Value(const bool& value) { |
45 if (value) | 20 if (value) |
46 return PLATFORM_FILE_OK; | 21 return PLATFORM_FILE_OK; |
47 return PLATFORM_FILE_ERROR_FAILED; | 22 return PLATFORM_FILE_ERROR_FAILED; |
48 } | 23 } |
49 }; | 24 }; |
50 | 25 |
51 template <typename R1, typename R2> | 26 namespace { |
52 void ReturnAsParamAdapter(const Callback<R1(void)>& func, R2* result) { | |
53 if (!func.is_null()) | |
54 *result = ReturnValueTranslator<R1, R2>::Value(func.Run()); | |
55 } | |
56 | |
57 template <typename R1, typename R2> | |
58 Closure ReturnAsParam(const Callback<R1(void)>& func, R2* result) { | |
59 DCHECK(result); | |
60 return Bind(&ReturnAsParamAdapter<R1, R2>, func, result); | |
61 } | |
62 | |
63 template <typename R, typename A1> | |
64 void ReturnAsParamAdapter1(const Callback<R(A1)>& func, A1 a1, R* result) { | |
65 if (!func.is_null()) | |
66 *result = func.Run(a1); | |
67 } | |
68 | |
69 template <typename R, typename A1> | |
70 Closure ReturnAsParam(const Callback<R(A1)>& func, A1 a1, R* result) { | |
71 DCHECK(result); | |
72 return Bind(&ReturnAsParamAdapter1<R, A1>, func, a1, result); | |
73 } | |
74 | |
75 template <typename R> | |
76 void ReplyAdapter(const Callback<void(R)>& callback, R* result) { | |
77 DCHECK(result); | |
78 if (!callback.is_null()) | |
79 callback.Run(*result); | |
80 } | |
81 | |
82 template <typename R, typename OWNED> | |
83 Closure ReplyHelper(const Callback<void(R)>& callback, OWNED result) { | |
84 return Bind(&ReplyAdapter<R>, callback, result); | |
85 } | |
86 | |
87 // Putting everything together. | |
88 template <typename R1, typename R2> | |
89 bool PostTaskAndReplyWithStatus( | |
90 const scoped_refptr<MessageLoopProxy>& message_loop_proxy, | |
91 const tracked_objects::Location& from_here, | |
92 const Callback<R1(void)>& file_util_work, | |
93 const Callback<void(R2)>& callback, | |
94 R2* result) { | |
95 return message_loop_proxy->PostTaskAndReply( | |
96 from_here, | |
97 ReturnAsParam<R1>(file_util_work, result), | |
98 ReplyHelper(callback, Owned(result))); | |
99 } | |
100 | 27 |
101 // Helper classes or routines for individual methods. | 28 // Helper classes or routines for individual methods. |
102 class CreateOrOpenHelper { | 29 class CreateOrOpenHelper { |
103 public: | 30 public: |
104 CreateOrOpenHelper(MessageLoopProxy* message_loop_proxy, | 31 CreateOrOpenHelper(MessageLoopProxy* message_loop_proxy, |
105 const FileUtilProxy::CloseTask& close_task) | 32 const FileUtilProxy::CloseTask& close_task) |
106 : message_loop_proxy_(message_loop_proxy), | 33 : message_loop_proxy_(message_loop_proxy), |
107 close_task_(close_task), | 34 close_task_(close_task), |
108 file_handle_(kInvalidPlatformFileValue), | 35 file_handle_(kInvalidPlatformFileValue), |
109 created_(false), | 36 created_(false), |
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 PlatformFile file_handle, | 433 PlatformFile file_handle, |
507 const StatusCallback& callback) { | 434 const StatusCallback& callback) { |
508 PlatformFileError* result = new PlatformFileError; | 435 PlatformFileError* result = new PlatformFileError; |
509 return message_loop_proxy->PostTaskAndReply( | 436 return message_loop_proxy->PostTaskAndReply( |
510 FROM_HERE, | 437 FROM_HERE, |
511 ReturnAsParam(close_task, file_handle, result), | 438 ReturnAsParam(close_task, file_handle, result), |
512 ReplyHelper(callback, Owned(result))); | 439 ReplyHelper(callback, Owned(result))); |
513 } | 440 } |
514 | 441 |
515 } // namespace base | 442 } // namespace base |
OLD | NEW |