Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: media/base/bind_to_current_loop.h

Issue 2869733005: Convert some audio code to OnceCallback. (Closed)
Patch Set: Fix BindToCurrentLoop issues Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include <utility>
9 10
10 #include "base/bind.h" 11 #include "base/bind.h"
11 #include "base/location.h" 12 #include "base/location.h"
12 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
13 #include "base/single_thread_task_runner.h" 14 #include "base/single_thread_task_runner.h"
14 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/thread_task_runner_handle.h"
15 16
16 // This is a helper utility for base::Bind()ing callbacks to the current 17 // 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 18 // 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 19 // callback such as base::Bind(&A::AMethod, a) to |b|, but needs to ensure that
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 51
51 ~TrampolineHelper() { 52 ~TrampolineHelper() {
52 task_runner_->PostTask( 53 task_runner_->PostTask(
53 posted_from_, 54 posted_from_,
54 base::Bind(&TrampolineHelper::ClearCallbackOnTargetTaskRunner, 55 base::Bind(&TrampolineHelper::ClearCallbackOnTargetTaskRunner,
55 base::Passed(&callback_))); 56 base::Passed(&callback_)));
56 } 57 }
57 58
58 private: 59 private:
59 static void ClearCallbackOnTargetTaskRunner(CallbackType) {} 60 static void ClearCallbackOnTargetTaskRunner(CallbackType) {}
60 static void RunOnceClosure(base::OnceClosure cb) { std::move(cb).Run(); }
61 61
62 tracked_objects::Location posted_from_; 62 tracked_objects::Location posted_from_;
63 scoped_refptr<base::SequencedTaskRunner> task_runner_; 63 scoped_refptr<base::SequencedTaskRunner> task_runner_;
64 CallbackType callback_; 64 CallbackType callback_;
65 }; 65 };
66 66
67 template <> 67 template <>
68 inline void TrampolineHelper<void()>::Run() { 68 inline void TrampolineHelper<void()>::Run() {
69 task_runner_->PostTask(posted_from_, callback_); 69 task_runner_->PostTask(posted_from_, callback_);
70 } 70 }
71 71
72 template <typename... Args> 72 template <typename... Args>
73 inline void TrampolineHelper<void(Args...)>::Run(Args... args) { 73 inline void TrampolineHelper<void(Args...)>::Run(Args... args) {
74 // TODO(tzik): Use OnceCallback directly without RunOnceClosure, once
75 // TaskRunner::PostTask migrates to OnceClosure.
76 base::OnceClosure cb = base::BindOnce(callback_, std::forward<Args>(args)...); 74 base::OnceClosure cb = base::BindOnce(callback_, std::forward<Args>(args)...);
77 task_runner_->PostTask( 75 task_runner_->PostTask(posted_from_, std::move(cb));
78 posted_from_, 76 }
79 base::Bind(&TrampolineHelper::RunOnceClosure, base::Passed(&cb))); 77
78 // First, tell the compiler OnceTrampolineHelper is a struct template with one
79 // type parameter. Then define specializations where the type is a function
80 // returning void and taking zero or more arguments.
81 template <typename Signature>
82 class OnceTrampolineHelper;
83
84 template <typename... Args>
85 class OnceTrampolineHelper<void(Args...)> {
86 public:
87 using CallbackType = base::OnceCallback<void(Args...)>;
88
89 OnceTrampolineHelper(const tracked_objects::Location& posted_from,
90 scoped_refptr<base::SequencedTaskRunner> task_runner,
91 CallbackType callback)
92 : posted_from_(posted_from),
93 task_runner_(std::move(task_runner)),
94 callback_(std::move(callback)) {
95 DCHECK(task_runner_);
96 DCHECK(callback_);
97 }
98
99 inline void Run(Args... args);
100
101 ~OnceTrampolineHelper() {
102 if (callback_) {
103 task_runner_->PostTask(
104 posted_from_,
105 base::BindOnce(&OnceTrampolineHelper::ClearCallbackOnTargetTaskRunner,
106 std::move(callback_)));
107 }
108 }
109
110 private:
111 static void ClearCallbackOnTargetTaskRunner(CallbackType) {}
112
113 tracked_objects::Location posted_from_;
114 scoped_refptr<base::SequencedTaskRunner> task_runner_;
115 CallbackType callback_;
116 };
117
118 template <>
119 inline void OnceTrampolineHelper<void()>::Run() {
120 task_runner_->PostTask(posted_from_, std::move(callback_));
121 }
122
123 template <typename... Args>
124 inline void OnceTrampolineHelper<void(Args...)>::Run(Args... args) {
125 base::OnceClosure cb =
126 base::BindOnce(std::move(callback_), std::forward<Args>(args)...);
127 task_runner_->PostTask(posted_from_, std::move(cb));
80 } 128 }
81 129
82 } // namespace internal 130 } // namespace internal
83 131
84 template <typename T> 132 template <typename T>
133 inline base::OnceCallback<T> BindToCurrentLoop(base::OnceCallback<T> cb) {
134 return base::BindOnce(
135 &internal::OnceTrampolineHelper<T>::Run,
136 base::MakeUnique<internal::OnceTrampolineHelper<T>>(
137 FROM_HERE, // TODO(tzik): Propagate FROM_HERE from the caller.
138 base::ThreadTaskRunnerHandle::Get(), std::move(cb)));
139 }
140
141 template <typename T>
85 inline base::Callback<T> BindToCurrentLoop(base::Callback<T> cb) { 142 inline base::Callback<T> BindToCurrentLoop(base::Callback<T> cb) {
86 return base::Bind( 143 return base::Bind(
87 &internal::TrampolineHelper<T>::Run, 144 &internal::TrampolineHelper<T>::Run,
88 base::MakeUnique<internal::TrampolineHelper<T>>( 145 base::MakeUnique<internal::TrampolineHelper<T>>(
89 FROM_HERE, // TODO(tzik): Propagate FROM_HERE from the caller. 146 FROM_HERE, // TODO(tzik): Propagate FROM_HERE from the caller.
90 base::ThreadTaskRunnerHandle::Get(), std::move(cb))); 147 base::ThreadTaskRunnerHandle::Get(), std::move(cb)));
91 } 148 }
92 149
93 } // namespace media 150 } // namespace media
94 151
95 #endif // MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ 152 #endif // MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698