OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_GOOGLE_APIS_TASK_UTIL_H_ | 5 #ifndef CHROME_BROWSER_GOOGLE_APIS_TASK_UTIL_H_ |
6 #define CHROME_BROWSER_GOOGLE_APIS_TASK_UTIL_H_ | 6 #define CHROME_BROWSER_GOOGLE_APIS_TASK_UTIL_H_ |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 | 10 |
11 namespace google_apis { | 11 namespace google_apis { |
12 | 12 |
13 // Runs task on the thread to which |relay_proxy| belongs. | 13 // Runs task on the thread to which |task_runner| belongs. |
14 void RunTaskOnThread(scoped_refptr<base::MessageLoopProxy> relay_proxy, | 14 void RunTaskOnThread(scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
15 const base::Closure& task); | 15 const base::Closure& task); |
16 | 16 |
17 namespace internal { | 17 namespace internal { |
18 | 18 |
19 // Implementation of the composed callback, whose signature is |Sig|. | 19 // Implementation of the composed callback, whose signature is |Sig|. |
20 template<typename Sig> struct ComposedCallback; | 20 template<typename Sig> struct ComposedCallback; |
21 | 21 |
22 // ComposedCallback with no argument. | 22 // ComposedCallback with no argument. |
23 template<> | 23 template<> |
24 struct ComposedCallback<void()> { | 24 struct ComposedCallback<void()> { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 template<typename T1, typename T2, typename D2, typename T3, typename T4> | 99 template<typename T1, typename T2, typename D2, typename T3, typename T4> |
100 struct ComposedCallback<void(T1, scoped_ptr<T2, D2>, T3, T4)> { | 100 struct ComposedCallback<void(T1, scoped_ptr<T2, D2>, T3, T4)> { |
101 static void Run( | 101 static void Run( |
102 const base::Callback<void(const base::Closure&)>& runner, | 102 const base::Callback<void(const base::Closure&)>& runner, |
103 const base::Callback<void(T1, scoped_ptr<T2, D2>, T3, T4)>& callback, | 103 const base::Callback<void(T1, scoped_ptr<T2, D2>, T3, T4)>& callback, |
104 T1 arg1, scoped_ptr<T2, D2> arg2, T3 arg3, T4 arg4) { | 104 T1 arg1, scoped_ptr<T2, D2> arg2, T3 arg3, T4 arg4) { |
105 runner.Run(base::Bind(callback, arg1, base::Passed(&arg2), arg3, arg4)); | 105 runner.Run(base::Bind(callback, arg1, base::Passed(&arg2), arg3, arg4)); |
106 } | 106 } |
107 }; | 107 }; |
108 | 108 |
109 // GetDefaultValue returns a value constructed by the default constructor. | |
110 template<typename T> struct DefaultValueCreator { | |
111 static T GetDefaultValue() { return T(); } | |
112 }; | |
113 template<typename T> struct DefaultValueCreator<const T&> { | |
114 static T GetDefaultValue() { return T(); } | |
115 }; | |
116 | |
117 // Helper of CreateErrorRunCallback implementation. | |
118 // Provides: | |
119 // - ResultType; the type of the Callback which should be returned by | |
120 // CreateErrorRunCallback. | |
121 // - Run(): a static function which takes the original |callback| and |error|, | |
122 // and runs the |callback|.Run() with the error code and default values | |
123 // for remaining arguments. | |
124 template<typename CallbackType> struct CreateErrorRunCallbackHelper; | |
125 | |
126 // CreateErrorRunCallback with two arguments. | |
127 template<typename ErrorType, typename P1> | |
128 struct CreateErrorRunCallbackHelper<void(ErrorType, P1)> { | |
129 typedef base::Callback<void(ErrorType)> ResultType; | |
130 static void Run( | |
131 const base::Callback<void(ErrorType, P1)>& callback, ErrorType error) { | |
132 callback.Run(error, DefaultValueCreator<P1>::GetDefaultValue()); | |
133 } | |
134 }; | |
135 | |
136 } // namespace internal | 109 } // namespace internal |
137 | 110 |
138 // Returns callback that takes arguments (arg1, arg2, ...), create a closure | 111 // Returns callback that takes arguments (arg1, arg2, ...), create a closure |
139 // by binding them to |callback|, and runs |runner| with the closure. | 112 // by binding them to |callback|, and runs |runner| with the closure. |
140 // I.e. the returned callback works as follows: | 113 // I.e. the returned callback works as follows: |
141 // runner.Run(Bind(callback, arg1, arg2, ...)) | 114 // runner.Run(Bind(callback, arg1, arg2, ...)) |
142 template<typename CallbackType> | 115 template<typename CallbackType> |
143 CallbackType CreateComposedCallback( | 116 CallbackType CreateComposedCallback( |
144 const base::Callback<void(const base::Closure&)>& runner, | 117 const base::Callback<void(const base::Closure&)>& runner, |
145 const CallbackType& callback) { | 118 const CallbackType& callback) { |
146 DCHECK(!runner.is_null()); | 119 DCHECK(!runner.is_null()); |
147 DCHECK(!callback.is_null()); | 120 DCHECK(!callback.is_null()); |
148 return base::Bind( | 121 return base::Bind( |
149 &internal::ComposedCallback<typename CallbackType::RunType>::Run, | 122 &internal::ComposedCallback<typename CallbackType::RunType>::Run, |
150 runner, callback); | 123 runner, callback); |
151 } | 124 } |
152 | 125 |
153 // Returns callback which runs the given |callback| on the current thread. | 126 // Returns callback which runs the given |callback| on the current thread. |
154 template<typename CallbackType> | 127 template<typename CallbackType> |
155 CallbackType CreateRelayCallback(const CallbackType& callback) { | 128 CallbackType CreateRelayCallback(const CallbackType& callback) { |
156 return CreateComposedCallback( | 129 return CreateComposedCallback( |
157 base::Bind(&RunTaskOnThread, base::MessageLoopProxy::current()), | 130 base::Bind(&RunTaskOnThread, base::MessageLoopProxy::current()), |
158 callback); | 131 callback); |
159 } | 132 } |
160 | 133 |
161 // Returns a callback with the tail parameter bound to its default value. | |
162 // In other words, returned_callback.Run(error) runs callback.Run(error, T()). | |
163 template<typename CallbackType> | |
164 typename internal::CreateErrorRunCallbackHelper<CallbackType>::ResultType | |
165 CreateErrorRunCallback(const base::Callback<CallbackType>& callback) { | |
166 return base::Bind( | |
167 &internal::CreateErrorRunCallbackHelper<CallbackType>::Run, callback); | |
168 } | |
169 | |
170 } // namespace google_apis | 134 } // namespace google_apis |
171 | 135 |
172 #endif // CHROME_BROWSER_GOOGLE_APIS_TASK_UTIL_H_ | 136 #endif // CHROME_BROWSER_GOOGLE_APIS_TASK_UTIL_H_ |
OLD | NEW |