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

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

Issue 2712603002: Destroy bound callback to BindToCurrentLoop on the target task runner (2) (Closed)
Patch Set: +test Created 3 years, 9 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/scoped_vector.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)));
24 //
25 // Note that like base::Bind(), BindToCurrentLoop() can't bind non-constant
26 // references, and that *unlike* base::Bind(), BindToCurrentLoop() makes copies
27 // of its arguments, and thus can't be used with arrays.
28 24
29 namespace media { 25 namespace media {
30
31 // Mimic base::internal::CallbackForward, replacing std::move(p) with
32 // base::Passed(&p) to account for the extra layer of indirection.
33 namespace internal { 26 namespace internal {
34 template <typename T>
35 T& TrampolineForward(T& t) { return t; }
36
37 template <typename T, typename R>
38 base::internal::PassedWrapper<std::unique_ptr<T, R>> TrampolineForward(
39 std::unique_ptr<T, R>& p) {
40 return base::Passed(&p);
41 }
42
43 template <typename T>
44 base::internal::PassedWrapper<ScopedVector<T> > TrampolineForward(
45 ScopedVector<T>& p) { return base::Passed(&p); }
46 27
47 // First, tell the compiler TrampolineHelper is a struct template with one 28 // First, tell the compiler TrampolineHelper is a struct template with one
48 // type parameter. Then define specializations where the type is a function 29 // type parameter. Then define specializations where the type is a function
49 // returning void and taking zero or more arguments. 30 // returning void and taking zero or more arguments.
50 template <typename Sig> struct TrampolineHelper; 31 template <typename Signature>
32 class TrampolineHelper;
33
34 template <typename... Args>
35 class TrampolineHelper<void(Args...)> {
36 public:
37 using CallbackType = base::Callback<void(Args...)>;
38
39 TrampolineHelper(const tracked_objects::Location& posted_from,
40 scoped_refptr<base::SequencedTaskRunner> task_runner,
41 CallbackType callback)
42 : posted_from_(posted_from),
43 task_runner_(std::move(task_runner)),
44 callback_(std::move(callback)) {
45 DCHECK(task_runner_);
46 DCHECK(callback_);
47 }
48
49 inline void Run(Args... args);
50
51 ~TrampolineHelper() {
52 task_runner_->PostTask(
53 posted_from_,
54 base::Bind(&TrampolineHelper::ClearCallbackOnTargetTaskRunner,
55 base::Passed(&callback_)));
56 }
57
58 private:
59 static void ClearCallbackOnTargetTaskRunner(const CallbackType&) {}
danakj 2017/02/28 18:28:04 This should receive by value so the Callback can m
tzik 2017/03/01 05:51:49 Hm, yes, this should be passed by value, though ba
60 static void RunOnceClosure(base::OnceClosure cb) { std::move(cb).Run(); }
61
62 tracked_objects::Location posted_from_;
63 scoped_refptr<base::SequencedTaskRunner> task_runner_;
64 CallbackType callback_;
65 };
51 66
52 template <> 67 template <>
53 struct TrampolineHelper<void()> { 68 inline void TrampolineHelper<void()>::Run() {
54 static void Run( 69 task_runner_->PostTask(posted_from_, callback_);
55 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 70 }
56 const base::Closure& cb) {
57 task_runner->PostTask(FROM_HERE, cb);
58 }
59 };
60 71
61 template <typename... Args> 72 template <typename... Args>
62 struct TrampolineHelper<void(Args...)> { 73 inline void TrampolineHelper<void(Args...)>::Run(Args... args) {
63 static void Run( 74 // TODO(tzik): Use OnceCallback directly without RunOnceClosure, once
64 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 75 // TaskRunner::PostTask migrates to OnceClosure.
65 const base::Callback<void(Args...)>& cb, 76 base::OnceClosure cb = base::BindOnce(callback_, std::forward<Args>(args)...);
66 Args... args) { 77 task_runner_->PostTask(
67 task_runner->PostTask(FROM_HERE, 78 posted_from_,
68 base::Bind(cb, TrampolineForward(args)...)); 79 base::Bind(&TrampolineHelper::RunOnceClosure, base::Passed(&cb)));
69 } 80 }
70 };
71 81
72 } // namespace internal 82 } // namespace internal
73 83
74 template<typename T> 84 template <typename T>
75 static base::Callback<T> BindToCurrentLoop( 85 base::Callback<T> BindToCurrentLoop(base::Callback<T> cb) {
76 const base::Callback<T>& cb) { 86 return base::Bind(
77 return base::Bind(&internal::TrampolineHelper<T>::Run, 87 &internal::TrampolineHelper<T>::Run,
78 base::ThreadTaskRunnerHandle::Get(), cb); 88 base::MakeUnique<internal::TrampolineHelper<T>>(
89 FROM_HERE, // TODO(tzik): Propagate FROM_HERE from the caller.
90 base::ThreadTaskRunnerHandle::Get(), std::move(cb)));
79 } 91 }
80 92
81 } // namespace media 93 } // namespace media
82 94
83 #endif // MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ 95 #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