OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_ | 5 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_ |
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_ | 6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_ |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/logging.h" |
| 11 #include "base/thread_task_runner_handle.h" |
| 12 |
| 13 // TODO(tzik): Merge this file to media/base/bind_to_current_loop.h. |
11 | 14 |
12 namespace sync_file_system { | 15 namespace sync_file_system { |
13 namespace drive_backend { | 16 namespace drive_backend { |
14 | 17 |
15 namespace internal { | 18 namespace internal { |
16 | 19 |
17 template <typename T> | 20 template <typename T> |
18 typename base::enable_if< | 21 typename base::enable_if< |
19 base::internal::IsMoveOnlyType<T>::value, | 22 base::internal::IsMoveOnlyType<T>::value, |
20 base::internal::PassedWrapper<T> >::type | 23 base::internal::PassedWrapper<T> >::type |
21 RebindForward(T& t) { | 24 RebindForward(T& t) { |
22 return base::Passed(&t); | 25 return base::Passed(&t); |
23 } | 26 } |
24 | 27 |
25 template <typename T> | 28 template <typename T> |
26 typename base::enable_if< | 29 typename base::enable_if< |
27 !base::internal::IsMoveOnlyType<T>::value, | 30 !base::internal::IsMoveOnlyType<T>::value, |
28 T&>::type | 31 T&>::type |
29 RebindForward(T& t) { | 32 RebindForward(T& t) { |
30 return t; | 33 return t; |
31 } | 34 } |
32 | 35 |
33 template <typename T> | 36 template <typename> |
34 void RelayToTaskRunner1(base::SequencedTaskRunner* task_runner, | 37 struct RelayToTaskRunner; |
35 const tracked_objects::Location& from_here, | 38 |
36 const base::Callback<void(T)>& callback, | 39 template <> |
37 T arg) { | 40 struct RelayToTaskRunner<void()> { |
38 task_runner->PostTask( | 41 static void Run(base::TaskRunner* task_runner, |
39 from_here, base::Bind(callback, RebindForward(arg))); | 42 const tracked_objects::Location& from_here, |
40 } | 43 const base::Callback<void()>& callback) { |
| 44 task_runner->PostTask(from_here, base::Bind( |
| 45 callback)); |
| 46 } |
| 47 }; |
| 48 |
| 49 template <typename T1> |
| 50 struct RelayToTaskRunner<void(T1)> { |
| 51 static void Run(base::TaskRunner* task_runner, |
| 52 const tracked_objects::Location& from_here, |
| 53 const base::Callback<void(T1)>& callback, |
| 54 T1 t1) { |
| 55 task_runner->PostTask(from_here, base::Bind( |
| 56 callback, |
| 57 RebindForward(t1))); |
| 58 } |
| 59 }; |
41 | 60 |
42 template <typename T1, typename T2> | 61 template <typename T1, typename T2> |
43 void RelayToTaskRunner2(base::SequencedTaskRunner* task_runner, | 62 struct RelayToTaskRunner<void(T1, T2)> { |
44 const tracked_objects::Location& from_here, | 63 static void Run(base::TaskRunner* task_runner, |
45 const base::Callback<void(T1, T2)>& callback, | 64 const tracked_objects::Location& from_here, |
46 T1 arg1, | 65 const base::Callback<void(T1, T2)>& callback, |
47 T2 arg2) { | 66 T1 t1, |
48 task_runner->PostTask( | 67 T2 t2) { |
49 from_here, base::Bind(callback, | 68 task_runner->PostTask(from_here, base::Bind( |
50 RebindForward(arg1), | 69 callback, |
51 RebindForward(arg2))); | 70 RebindForward(t1), |
52 } | 71 RebindForward(t2))); |
| 72 } |
| 73 }; |
53 | 74 |
54 } // namespace internal | 75 } // namespace internal |
55 | 76 |
56 template <typename T> | 77 template <typename T> |
57 base::Callback<void(T)> CreateRelayedCallback( | 78 base::Callback<T> RelayCallbackToTaskRunner( |
58 const base::Callback<void(T)>& callback) { | 79 base::TaskRunner* task_runner, |
59 return base::Bind(&internal::RelayToTaskRunner1<T>, | 80 const tracked_objects::Location& from_here, |
60 base::MessageLoopProxy::current(), | 81 const base::Callback<T>& callback) { |
61 FROM_HERE, | 82 DCHECK(task_runner->RunsTasksOnCurrentThread()); |
| 83 return base::Bind(&internal::RelayToTaskRunner<T>::Run, |
| 84 make_scoped_refptr(task_runner), from_here, |
62 callback); | 85 callback); |
63 } | 86 } |
64 | 87 |
65 template <typename T1, typename T2> | 88 template <typename T> |
66 base::Callback<void(T1, T2)> CreateRelayedCallback( | 89 base::Callback<T> RelayCallbackToCurrentThread( |
67 const base::Callback<void(T1, T2)>& callback) { | 90 const tracked_objects::Location& from_here, |
68 return base::Bind(&internal::RelayToTaskRunner2<T1, T2>, | 91 const base::Callback<T>& callback) { |
69 base::MessageLoopProxy::current(), | 92 return RelayCallbackToTaskRunner( |
70 FROM_HERE, | 93 base::ThreadTaskRunnerHandle::Get(), |
71 callback); | 94 from_here, callback); |
72 } | 95 } |
73 | 96 |
74 } // namespace drive_backend | 97 } // namespace drive_backend |
75 } // namespace sync_file_system | 98 } // namespace sync_file_system |
76 | 99 |
77 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_ | 100 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_HELPER_H_ |
OLD | NEW |