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

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

Issue 2874003002: Update media::BindToCurrentLoop to support base::OnceCallback (Closed)
Patch Set: mod comments 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
« no previous file with comments | « no previous file | media/base/bind_to_current_loop_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 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::Bind(&MyClass::MyMethod, this)));
xhwang 2017/05/11 18:29:02 nit: Also add an example using BindOnce?
tzik 2017/05/12 04:05:54 Done.
24 24
25 namespace media { 25 namespace media {
26 namespace internal { 26 namespace internal {
27 27
28 // First, tell the compiler TrampolineHelper is a struct template with one 28 inline base::OnceClosure MakeClosure(base::RepeatingClosure* callback) {
29 // type parameter. Then define specializations where the type is a function 29 return *callback;
30 // returning void and taking zero or more arguments. 30 }
31 template <typename Signature>
32 class TrampolineHelper;
33 31
34 template <typename... Args> 32 inline base::OnceClosure MakeClosure(base::OnceClosure* callback) {
35 class TrampolineHelper<void(Args...)> { 33 return std::move(*callback);
34 }
35
36 template <typename Signature, typename... Args>
37 base::OnceClosure MakeClosure(base::RepeatingCallback<Signature>* callback,
38 Args&&... args) {
39 return base::BindOnce(*callback, std::forward<Args>(args)...);
40 }
41
42 template <typename Signature, typename... Args>
43 base::OnceClosure MakeClosure(base::OnceCallback<Signature>* callback,
44 Args&&... args) {
45 return base::BindOnce(std::move(*callback), std::forward<Args>(args)...);
46 }
47
48 template <typename CallbackType>
49 class TrampolineHelper {
36 public: 50 public:
37 using CallbackType = base::Callback<void(Args...)>;
38
39 TrampolineHelper(const tracked_objects::Location& posted_from, 51 TrampolineHelper(const tracked_objects::Location& posted_from,
40 scoped_refptr<base::SequencedTaskRunner> task_runner, 52 scoped_refptr<base::SequencedTaskRunner> task_runner,
41 CallbackType callback) 53 CallbackType callback)
42 : posted_from_(posted_from), 54 : posted_from_(posted_from),
43 task_runner_(std::move(task_runner)), 55 task_runner_(std::move(task_runner)),
44 callback_(std::move(callback)) { 56 callback_(std::move(callback)) {
45 DCHECK(task_runner_); 57 DCHECK(task_runner_);
46 DCHECK(callback_); 58 DCHECK(callback_);
47 } 59 }
48 60
49 inline void Run(Args... args); 61 template <typename... Args>
62 void Run(Args... args) {
63 // MakeClosure consumes |callback_| if it's OnceCallback.
64 task_runner_->PostTask(
65 posted_from_, MakeClosure(&callback_, std::forward<Args>(args)...));
66 }
50 67
51 ~TrampolineHelper() { 68 ~TrampolineHelper() {
52 task_runner_->PostTask( 69 if (callback_) {
53 posted_from_, 70 task_runner_->PostTask(
54 base::Bind(&TrampolineHelper::ClearCallbackOnTargetTaskRunner, 71 posted_from_,
55 base::Passed(&callback_))); 72 base::BindOnce(&TrampolineHelper::ClearCallbackOnTargetTaskRunner,
73 std::move(callback_)));
74 }
56 } 75 }
57 76
58 private: 77 private:
59 static void ClearCallbackOnTargetTaskRunner(CallbackType) {} 78 static void ClearCallbackOnTargetTaskRunner(CallbackType) {}
60 static void RunOnceClosure(base::OnceClosure cb) { std::move(cb).Run(); }
61 79
62 tracked_objects::Location posted_from_; 80 tracked_objects::Location posted_from_;
63 scoped_refptr<base::SequencedTaskRunner> task_runner_; 81 scoped_refptr<base::SequencedTaskRunner> task_runner_;
64 CallbackType callback_; 82 CallbackType callback_;
65 }; 83 };
66 84
67 template <> 85 } // namespace internal
68 inline void TrampolineHelper<void()>::Run() { 86
69 task_runner_->PostTask(posted_from_, callback_); 87 template <typename... Args>
88 inline base::RepeatingCallback<void(Args...)> BindToCurrentLoop(
89 base::RepeatingCallback<void(Args...)> cb) {
90 using CallbackType = base::RepeatingCallback<void(Args...)>;
91 using Helper = internal::TrampolineHelper<CallbackType>;
92 using RunnerType = void (Helper::*)(Args...);
93 RunnerType run = &Helper::Run;
94 return base::BindRepeating(
95 run, base::MakeUnique<Helper>(
96 FROM_HERE, base::ThreadTaskRunnerHandle::Get(), std::move(cb)));
xhwang 2017/05/11 18:29:02 Do we still need the old TODO here? // TODO(tzik)
tzik 2017/05/12 04:05:54 Done.
70 } 97 }
71 98
72 template <typename... Args> 99 template <typename... Args>
73 inline void TrampolineHelper<void(Args...)>::Run(Args... args) { 100 inline base::OnceCallback<void(Args...)> BindToCurrentLoop(
74 // TODO(tzik): Use OnceCallback directly without RunOnceClosure, once 101 base::OnceCallback<void(Args...)> cb) {
75 // TaskRunner::PostTask migrates to OnceClosure. 102 using CallbackType = base::OnceCallback<void(Args...)>;
76 base::OnceClosure cb = base::BindOnce(callback_, std::forward<Args>(args)...); 103 using Helper = internal::TrampolineHelper<CallbackType>;
77 task_runner_->PostTask( 104 using RunnerType = void (Helper::*)(Args...);
78 posted_from_, 105 RunnerType run = &Helper::Run;
79 base::Bind(&TrampolineHelper::RunOnceClosure, base::Passed(&cb))); 106 return base::BindOnce(
80 } 107 run, base::MakeUnique<Helper>(
81 108 FROM_HERE, base::ThreadTaskRunnerHandle::Get(), std::move(cb)));
82 } // namespace internal
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 } 109 }
92 110
93 } // namespace media 111 } // namespace media
94 112
95 #endif // MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ 113 #endif // MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_
OLDNEW
« no previous file with comments | « no previous file | media/base/bind_to_current_loop_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698