OLD | NEW |
1 $$ This is a pump file for generating file templates. Pump is a python | 1 $$ This is a pump file for generating file templates. Pump is a python |
2 $$ script that is part of the Google Test suite of utilities. Description | 2 $$ script that is part of the Google Test suite of utilities. Description |
3 $$ can be found here: | 3 $$ can be found here: |
4 $$ | 4 $$ |
5 $$ http://code.google.com/p/googletest/wiki/PumpManual | 5 $$ http://code.google.com/p/googletest/wiki/PumpManual |
6 $$ | 6 $$ |
7 $$ See comment for MAX_ARITY in base/bind.h.pump. | 7 $$ See comment for MAX_ARITY in base/bind.h.pump. |
8 $var MAX_ARITY = 7 | 8 $var MAX_ARITY = 7 |
9 | 9 |
10 // Copyright 2014 The Chromium Authors. All rights reserved. | 10 // Copyright 2014 The Chromium Authors. All rights reserved. |
11 // Use of this source code is governed by a BSD-style license that can be | 11 // Use of this source code is governed by a BSD-style license that can be |
12 // found in the LICENSE file. | 12 // found in the LICENSE file. |
13 | 13 |
14 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_ | 14 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_ |
15 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_ | 15 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_ |
16 | 16 |
17 #include "base/bind.h" | 17 #include "base/bind.h" |
18 #include "base/location.h" | 18 #include "base/location.h" |
19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/sequenced_task_runner.h" |
20 #include "base/thread_task_runner_handle.h" | 21 #include "base/thread_task_runner_handle.h" |
21 | 22 |
22 // TODO(tzik): Merge this file to media/base/bind_to_current_loop.h. | 23 // TODO(tzik): Merge this file to media/base/bind_to_current_loop.h. |
23 | 24 |
24 namespace sync_file_system { | 25 namespace sync_file_system { |
25 namespace drive_backend { | 26 namespace drive_backend { |
26 | 27 |
27 namespace internal { | 28 namespace internal { |
28 | 29 |
29 template <typename T> | 30 template <typename T> |
30 typename base::enable_if< | 31 typename base::enable_if< |
31 base::internal::IsMoveOnlyType<T>::value, | 32 base::internal::IsMoveOnlyType<T>::value, |
32 base::internal::PassedWrapper<T> >::type | 33 base::internal::PassedWrapper<T> >::type |
33 RebindForward(T& t) { | 34 RebindForward(T& t) { |
34 return base::Passed(&t); | 35 return base::Passed(&t); |
35 } | 36 } |
36 | 37 |
37 template <typename T> | 38 template <typename T> |
38 typename base::enable_if< | 39 typename base::enable_if< |
39 !base::internal::IsMoveOnlyType<T>::value, | 40 !base::internal::IsMoveOnlyType<T>::value, |
40 T&>::type | 41 T&>::type |
41 RebindForward(T& t) { | 42 RebindForward(T& t) { |
42 return t; | 43 return t; |
43 } | 44 } |
44 | 45 |
| 46 template <typename T> |
| 47 class CallbackHolder { |
| 48 public: |
| 49 CallbackHolder(base::SequencedTaskRunner* task_runner, |
| 50 const tracked_objects::Location& from_here, |
| 51 const base::Callback<T>& callback) |
| 52 : task_runner_(task_runner), |
| 53 from_here_(from_here), |
| 54 callback_(new base::Callback<T>(callback)) { |
| 55 DCHECK(task_runner_); |
| 56 } |
| 57 |
| 58 ~CallbackHolder() { |
| 59 base::Callback<T>* callback = callback_.release(); |
| 60 if (!task_runner_->DeleteSoon(from_here_, callback)) |
| 61 delete callback; |
| 62 } |
| 63 |
| 64 base::SequencedTaskRunner* task_runner() const { return task_runner_; } |
| 65 const tracked_objects::Location& from_here() const { return from_here_; } |
| 66 const base::Callback<T>& callback() const { return *callback_; } |
| 67 |
| 68 private: |
| 69 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 70 const tracked_objects::Location from_here_; |
| 71 scoped_ptr<base::Callback<T> > callback_; |
| 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(CallbackHolder); |
| 74 }; |
| 75 |
45 template <typename> | 76 template <typename> |
46 struct RelayToTaskRunnerHelper; | 77 struct RelayToTaskRunnerHelper; |
47 | 78 |
48 $range ARITY 0..MAX_ARITY | 79 $range ARITY 0..MAX_ARITY |
49 $for ARITY [[ | 80 $for ARITY [[ |
50 $range ARG 1..ARITY | 81 $range ARG 1..ARITY |
51 | 82 |
52 template <$for ARG , [[typename A$(ARG)]]> | 83 template <$for ARG , [[typename A$(ARG)]]> |
53 struct RelayToTaskRunnerHelper<void($for ARG , [[A$(ARG)]])> { | 84 struct RelayToTaskRunnerHelper<void($for ARG , [[A$(ARG)]])> { |
54 static void Run(base::TaskRunner* task_runner, | 85 static void Run(CallbackHolder<void($for ARG , [[A$(ARG)]])>* holder |
55 const tracked_objects::Location& from_here, | |
56 const base::Callback<void($for ARG , [[A$(ARG)]])>& callback | |
57 $if ARITY != 0 [[, ]] | 86 $if ARITY != 0 [[, ]] |
58 $for ARG , [[A$(ARG) a$(ARG)]] | 87 $for ARG , [[A$(ARG) a$(ARG)]] |
59 ) { | 88 ) { |
60 task_runner->PostTask(from_here, base::Bind(callback | 89 holder->task_runner()->PostTask( |
| 90 holder->from_here(), base::Bind(holder->callback() |
61 $if ARITY != 0 [[, ]] | 91 $if ARITY != 0 [[, ]] |
62 $for ARG , [[RebindForward(a$(ARG))]])); | 92 $for ARG , [[RebindForward(a$(ARG))]])); |
63 } | 93 } |
64 }; | 94 }; |
65 | 95 |
66 ]] $$ for ARITY | 96 ]] $$ for ARITY |
67 | 97 |
68 } // namespace internal | 98 } // namespace internal |
69 | 99 |
70 template <typename T> | 100 template <typename T> |
71 base::Callback<T> RelayCallbackToTaskRunner( | 101 base::Callback<T> RelayCallbackToTaskRunner( |
72 base::TaskRunner* task_runner, | 102 base::SequencedTaskRunner* task_runner, |
73 const tracked_objects::Location& from_here, | 103 const tracked_objects::Location& from_here, |
74 const base::Callback<T>& callback) { | 104 const base::Callback<T>& callback) { |
75 DCHECK(task_runner->RunsTasksOnCurrentThread()); | 105 DCHECK(task_runner->RunsTasksOnCurrentThread()); |
76 | 106 |
77 if (callback.is_null()) | 107 if (callback.is_null()) |
78 return base::Callback<T>(); | 108 return base::Callback<T>(); |
79 | 109 |
80 return base::Bind(&internal::RelayToTaskRunnerHelper<T>::Run, | 110 return base::Bind(&internal::RelayToTaskRunnerHelper<T>::Run, |
81 make_scoped_refptr(task_runner), from_here, | 111 base::Owned(new internal::CallbackHolder<T>( |
82 callback); | 112 task_runner, from_here, callback))); |
83 } | 113 } |
84 | 114 |
85 template <typename T> | 115 template <typename T> |
86 base::Callback<T> RelayCallbackToCurrentThread( | 116 base::Callback<T> RelayCallbackToCurrentThread( |
87 const tracked_objects::Location& from_here, | 117 const tracked_objects::Location& from_here, |
88 const base::Callback<T>& callback) { | 118 const base::Callback<T>& callback) { |
89 return RelayCallbackToTaskRunner( | 119 return RelayCallbackToTaskRunner( |
90 base::ThreadTaskRunnerHandle::Get(), | 120 base::ThreadTaskRunnerHandle::Get(), |
91 from_here, callback); | 121 from_here, callback); |
92 } | 122 } |
93 | 123 |
94 } // namespace drive_backend | 124 } // namespace drive_backend |
95 } // namespace sync_file_system | 125 } // namespace sync_file_system |
96 | 126 |
97 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_ | 127 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_ |
OLD | NEW |