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 MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ |
6 #define MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ | 6 #define MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
15 | 15 |
16 // This is a helper utility for base::Bind()ing callbacks to the current | 16 // This is a helper utility for base::Bind()ing callbacks to the current |
17 // MessageLoop. The typical use is when |a| (of class |A|) wants to hand a | 17 // MessageLoop. The typical use is when |a| (of class |A|) wants to hand a |
18 // callback such as base::Bind(&A::AMethod, a) to |b|, but needs to ensure that | 18 // callback such as base::Bind(&A::AMethod, a) to |b|, but needs to ensure that |
19 // when |b| executes the callback, it does so on |a|'s current MessageLoop. | 19 // when |b| executes the callback, it does so on |a|'s current MessageLoop. |
20 // | 20 // |
21 // Typical usage: request to be called back on the current thread: | 21 // Typical usage: request to be called back on the current thread: |
22 // other->StartAsyncProcessAndCallMeBack( | 22 // other->StartAsyncProcessAndCallMeBack( |
23 // media::BindToCurrentLoop(base::Bind(&MyClass::MyMethod, this))); | 23 // media::BindToCurrentLoop(base::BindOnce(&MyClass::MyMethod, this))); |
| 24 // |
| 25 // media::BindToCurrentLoop returns the same type of callback to the given |
| 26 // callback. I.e. it returns a RepeatingCallback for a given RepeatingCallback, |
| 27 // and returns OnceCallback for a given OnceCallback. |
24 | 28 |
25 namespace media { | 29 namespace media { |
26 namespace internal { | 30 namespace internal { |
27 | 31 |
28 // First, tell the compiler TrampolineHelper is a struct template with one | 32 inline base::OnceClosure MakeClosure(base::RepeatingClosure* callback) { |
29 // type parameter. Then define specializations where the type is a function | 33 return *callback; |
30 // returning void and taking zero or more arguments. | 34 } |
31 template <typename Signature> | |
32 class TrampolineHelper; | |
33 | 35 |
34 template <typename... Args> | 36 inline base::OnceClosure MakeClosure(base::OnceClosure* callback) { |
35 class TrampolineHelper<void(Args...)> { | 37 return std::move(*callback); |
| 38 } |
| 39 |
| 40 template <typename Signature, typename... Args> |
| 41 base::OnceClosure MakeClosure(base::RepeatingCallback<Signature>* callback, |
| 42 Args&&... args) { |
| 43 return base::BindOnce(*callback, std::forward<Args>(args)...); |
| 44 } |
| 45 |
| 46 template <typename Signature, typename... Args> |
| 47 base::OnceClosure MakeClosure(base::OnceCallback<Signature>* callback, |
| 48 Args&&... args) { |
| 49 return base::BindOnce(std::move(*callback), std::forward<Args>(args)...); |
| 50 } |
| 51 |
| 52 template <typename CallbackType> |
| 53 class TrampolineHelper { |
36 public: | 54 public: |
37 using CallbackType = base::Callback<void(Args...)>; | |
38 | |
39 TrampolineHelper(const tracked_objects::Location& posted_from, | 55 TrampolineHelper(const tracked_objects::Location& posted_from, |
40 scoped_refptr<base::SequencedTaskRunner> task_runner, | 56 scoped_refptr<base::SequencedTaskRunner> task_runner, |
41 CallbackType callback) | 57 CallbackType callback) |
42 : posted_from_(posted_from), | 58 : posted_from_(posted_from), |
43 task_runner_(std::move(task_runner)), | 59 task_runner_(std::move(task_runner)), |
44 callback_(std::move(callback)) { | 60 callback_(std::move(callback)) { |
45 DCHECK(task_runner_); | 61 DCHECK(task_runner_); |
46 DCHECK(callback_); | 62 DCHECK(callback_); |
47 } | 63 } |
48 | 64 |
49 inline void Run(Args... args); | 65 template <typename... Args> |
| 66 void Run(Args... args) { |
| 67 // MakeClosure consumes |callback_| if it's OnceCallback. |
| 68 task_runner_->PostTask( |
| 69 posted_from_, MakeClosure(&callback_, std::forward<Args>(args)...)); |
| 70 } |
50 | 71 |
51 ~TrampolineHelper() { | 72 ~TrampolineHelper() { |
52 task_runner_->PostTask( | 73 if (callback_) { |
53 posted_from_, | 74 task_runner_->PostTask( |
54 base::Bind(&TrampolineHelper::ClearCallbackOnTargetTaskRunner, | 75 posted_from_, |
55 base::Passed(&callback_))); | 76 base::BindOnce(&TrampolineHelper::ClearCallbackOnTargetTaskRunner, |
| 77 std::move(callback_))); |
| 78 } |
56 } | 79 } |
57 | 80 |
58 private: | 81 private: |
59 static void ClearCallbackOnTargetTaskRunner(CallbackType) {} | 82 static void ClearCallbackOnTargetTaskRunner(CallbackType) {} |
60 static void RunOnceClosure(base::OnceClosure cb) { std::move(cb).Run(); } | |
61 | 83 |
62 tracked_objects::Location posted_from_; | 84 tracked_objects::Location posted_from_; |
63 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 85 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
64 CallbackType callback_; | 86 CallbackType callback_; |
65 }; | 87 }; |
66 | 88 |
67 template <> | 89 } // namespace internal |
68 inline void TrampolineHelper<void()>::Run() { | 90 |
69 task_runner_->PostTask(posted_from_, callback_); | 91 template <typename... Args> |
| 92 inline base::RepeatingCallback<void(Args...)> BindToCurrentLoop( |
| 93 base::RepeatingCallback<void(Args...)> cb) { |
| 94 using CallbackType = base::RepeatingCallback<void(Args...)>; |
| 95 using Helper = internal::TrampolineHelper<CallbackType>; |
| 96 using RunnerType = void (Helper::*)(Args...); |
| 97 RunnerType run = &Helper::Run; |
| 98 // TODO(tzik): Propagate FROM_HERE from the caller. |
| 99 return base::BindRepeating( |
| 100 run, base::MakeUnique<Helper>( |
| 101 FROM_HERE, base::ThreadTaskRunnerHandle::Get(), std::move(cb))); |
70 } | 102 } |
71 | 103 |
72 template <typename... Args> | 104 template <typename... Args> |
73 inline void TrampolineHelper<void(Args...)>::Run(Args... args) { | 105 inline base::OnceCallback<void(Args...)> BindToCurrentLoop( |
74 // TODO(tzik): Use OnceCallback directly without RunOnceClosure, once | 106 base::OnceCallback<void(Args...)> cb) { |
75 // TaskRunner::PostTask migrates to OnceClosure. | 107 using CallbackType = base::OnceCallback<void(Args...)>; |
76 base::OnceClosure cb = base::BindOnce(callback_, std::forward<Args>(args)...); | 108 using Helper = internal::TrampolineHelper<CallbackType>; |
77 task_runner_->PostTask( | 109 using RunnerType = void (Helper::*)(Args...); |
78 posted_from_, | 110 RunnerType run = &Helper::Run; |
79 base::Bind(&TrampolineHelper::RunOnceClosure, base::Passed(&cb))); | 111 // TODO(tzik): Propagate FROM_HERE from the caller. |
80 } | 112 return base::BindOnce( |
81 | 113 run, base::MakeUnique<Helper>( |
82 } // namespace internal | 114 FROM_HERE, base::ThreadTaskRunnerHandle::Get(), std::move(cb))); |
83 | |
84 template <typename T> | |
85 inline base::Callback<T> BindToCurrentLoop(base::Callback<T> cb) { | |
86 return base::Bind( | |
87 &internal::TrampolineHelper<T>::Run, | |
88 base::MakeUnique<internal::TrampolineHelper<T>>( | |
89 FROM_HERE, // TODO(tzik): Propagate FROM_HERE from the caller. | |
90 base::ThreadTaskRunnerHandle::Get(), std::move(cb))); | |
91 } | 115 } |
92 | 116 |
93 } // namespace media | 117 } // namespace media |
94 | 118 |
95 #endif // MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ | 119 #endif // MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ |
OLD | NEW |