OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MEDIA_BASE_CALLBACK_WRAPPER_H_ | |
6 #define MEDIA_BASE_CALLBACK_WRAPPER_H_ | |
7 | |
8 #include "base/callback.h" | |
9 #include "base/message_loop_proxy.h" | |
10 | |
11 // This is a helper utility for running callbacks on the current message loop as | |
12 // a freshly posted task as opposed to executing on the current stack. | |
13 // | |
14 // Typical usage is when implementing an asynchronous callback-accepting | |
15 // interface in a synchronous fashion. Running the callback as a posted task | |
16 // prevents reentrancy into the calling code. | |
17 // | |
18 // void MyClass::DoSomethingAsync(const base::Closure& closure) { | |
19 // DoWorkSynchronously(); | |
20 // WrapCB(closure).Run(); | |
21 // } | |
22 | |
23 namespace media { | |
24 | |
25 namespace internal { | |
26 void RunWrappedCB( | |
scherkus (not reviewing)
2012/12/06 21:57:57
I just realized I was completely mistaken about Bi
| |
27 const scoped_refptr<base::MessageLoopProxy>& message_loop, | |
28 const base::Closure& cb) { | |
29 message_loop->PostTask(FROM_HERE, cb); | |
30 } | |
31 | |
32 template<typename A1> | |
33 void RunWrappedCBWithArgs( | |
34 const scoped_refptr<base::MessageLoopProxy>& message_loop, | |
35 const base::Callback<void(A1)>& cb, | |
36 A1 a1) { | |
37 RunWrappedCB(message_loop, base::Bind(cb, a1)); | |
38 } | |
39 | |
40 template<typename A1, typename A2> | |
41 void RunWrappedCBWithArgs( | |
42 const scoped_refptr<base::MessageLoopProxy>& message_loop, | |
43 const base::Callback<void(A1, A2)>& cb, | |
44 A1 a1, A2 a2) { | |
45 RunWrappedCB(message_loop, base::Bind(cb, a1, a2)); | |
46 } | |
47 } // namespace internal | |
48 | |
49 base::Closure WrapCB(const base::Closure& cb) { | |
50 return base::Bind( | |
51 &internal::RunWrappedCB, | |
52 base::MessageLoopProxy::current(), cb); | |
53 } | |
54 | |
55 template<typename A1> | |
56 base::Callback<void(A1)> WrapCB(const base::Callback<void(A1)>& cb) { | |
57 return base::Bind( | |
58 &internal::RunWrappedCBWithArgs<A1>, | |
59 base::MessageLoopProxy::current(), cb); | |
60 } | |
61 | |
62 template<typename A1, typename A2> | |
63 base::Callback<void(A1, A2)> WrapCB(const base::Callback<void(A1, A2)>& cb) { | |
64 return base::Bind( | |
65 &internal::RunWrappedCBWithArgs<A1, A2>, | |
66 base::MessageLoopProxy::current(), cb); | |
67 } | |
68 | |
69 } // namespace media | |
70 | |
71 #endif // MEDIA_BASE_CALLBACK_WRAPPER_H_ | |
OLD | NEW |