| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ | 5 #ifndef BASE_BIND_TO_CURRENT_LOOP_H_ |
| 6 #define MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ | 6 #define BASE_BIND_TO_CURRENT_LOOP_H_ |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 | 12 |
| 13 // This is a helper utility for base::Bind()ing callbacks to the current | 13 // This is a helper utility for base::Bind()ing callbacks to the current |
| 14 // MessageLoop. The typical use is when |a| (of class |A|) wants to hand a | 14 // MessageLoop. The typical use is when |a| (of class |A|) wants to hand a |
| 15 // callback such as base::Bind(&A::AMethod, a) to |b|, but needs to ensure that | 15 // callback such as base::Bind(&A::AMethod, a) to |b|, but needs to ensure that |
| 16 // when |b| executes the callback, it does so on |a|'s current MessageLoop. | 16 // when |b| executes the callback, it does so on |a|'s current MessageLoop. |
| 17 // | 17 // |
| 18 // Typical usage: request to be called back on the current thread: | 18 // Typical usage: request to be called back on the current thread: |
| 19 // other->StartAsyncProcessAndCallMeBack( | 19 // other->StartAsyncProcessAndCallMeBack( |
| 20 // media::BindToCurrentLoop(base::Bind(&MyClass::MyMethod, this))); | 20 // base::BindToCurrentLoop(base::Bind(&MyClass::MyMethod, this))); |
| 21 // | 21 // |
| 22 // Note that like base::Bind(), BindToCurrentLoop() can't bind non-constant | 22 // Note that like base::Bind(), base::BindToCurrentLoop() can't bind |
| 23 // references, and that *unlike* base::Bind(), BindToCurrentLoop() makes copies | 23 // non-constant references, and that *unlike* base::Bind(), |
| 24 // of its arguments, and thus can't be used with arrays. | 24 // base::BindToCurrentLoop() makes copies of its arguments, and thus can't be |
| 25 // used with arrays. |
| 25 | 26 |
| 26 namespace media { | 27 namespace base { |
| 28 |
| 29 namespace internal { |
| 27 | 30 |
| 28 // Mimic base::internal::CallbackForward, replacing p.Pass() with | 31 // Mimic base::internal::CallbackForward, replacing p.Pass() with |
| 29 // base::Passed(&p) to account for the extra layer of indirection. | 32 // base::Passed(&p) to account for the extra layer of indirection. |
| 30 namespace internal { | |
| 31 template <typename T> | 33 template <typename T> |
| 32 T& TrampolineForward(T& t) { return t; } | 34 T& TrampolineForward(T& t) { return t; } |
| 33 | 35 |
| 34 template <typename T, typename R> | 36 template <typename T, typename R> |
| 35 base::internal::PassedWrapper<scoped_ptr<T, R> > TrampolineForward( | 37 PassedWrapper<scoped_ptr<T, R> > TrampolineForward(scoped_ptr<T, R>& p) { |
| 36 scoped_ptr<T, R>& p) { return base::Passed(&p); } | 38 return Passed(&p); |
| 39 } |
| 37 | 40 |
| 38 template <typename T> | 41 template <typename T> |
| 39 base::internal::PassedWrapper<ScopedVector<T> > TrampolineForward( | 42 PassedWrapper<ScopedVector<T> > TrampolineForward(ScopedVector<T>& p) { |
| 40 ScopedVector<T>& p) { return base::Passed(&p); } | 43 return Passed(&p); |
| 44 } |
| 41 | 45 |
| 42 // First, tell the compiler TrampolineHelper is a struct template with one | 46 // First, tell the compiler TrampolineHelper is a struct template with one |
| 43 // type parameter. Then define specializations where the type is a function | 47 // type parameter. Then define specializations where the type is a function |
| 44 // returning void and taking zero or more arguments. | 48 // returning void and taking zero or more arguments. |
| 45 template <typename Sig> struct TrampolineHelper; | 49 template <typename Sig> struct TrampolineHelper; |
| 46 | 50 |
| 47 template <typename... Args> | 51 template <typename... Args> |
| 48 struct TrampolineHelper<void(Args...)> { | 52 struct TrampolineHelper<void(Args...)> { |
| 49 static void Run( | 53 static void Run(const scoped_refptr<SingleThreadTaskRunner>& task_runner, |
| 50 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 54 const Callback<void(Args...)>& cb, |
| 51 const base::Callback<void(Args...)>& cb, | 55 Args... args) { |
| 52 Args... args) { | |
| 53 task_runner->PostTask(FROM_HERE, | 56 task_runner->PostTask(FROM_HERE, |
| 54 base::Bind(cb, TrampolineForward(args)...)); | 57 Bind(cb, TrampolineForward(args)...)); |
| 55 } | 58 } |
| 56 }; | 59 }; |
| 57 | 60 |
| 58 } // namespace internal | 61 } // namespace internal |
| 59 | 62 |
| 60 template<typename T> | 63 template<typename T> |
| 61 static base::Callback<T> BindToCurrentLoop( | 64 static Callback<T> BindToCurrentLoop(const Callback<T>& cb) { |
| 62 const base::Callback<T>& cb) { | 65 return Bind(&internal::TrampolineHelper<T>::Run, |
| 63 return base::Bind(&internal::TrampolineHelper<T>::Run, | 66 MessageLoop::current()->task_runner(), cb); |
| 64 base::MessageLoopProxy::current(), cb); | |
| 65 } | 67 } |
| 66 | 68 |
| 67 } // namespace media | 69 } // namespace base |
| 68 | 70 |
| 69 #endif // MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ | 71 #endif // BASE_BIND_TO_CURRENT_LOOP_H_ |
| OLD | NEW |