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 // CancelableTaskTracker posts tasks (in the form of a Closure) to a | 5 // CancelableTaskTracker posts tasks (in the form of a Closure) to a |
6 // TaskRunner, and is able to cancel the task later if it's not needed | 6 // TaskRunner, and is able to cancel the task later if it's not needed |
7 // anymore. On destruction, CancelableTaskTracker will cancel all | 7 // anymore. On destruction, CancelableTaskTracker will cancel all |
8 // tracked tasks. | 8 // tracked tasks. |
9 // | 9 // |
10 // Each cancelable task can be associated with a reply (also a Closure). After | 10 // Each cancelable task can be associated with a reply (also a Closure). After |
(...skipping 19 matching lines...) Expand all Loading... |
30 // tasks. This is commonly used to cancel all outstanding tasks. | 30 // tasks. This is commonly used to cancel all outstanding tasks. |
31 // | 31 // |
32 // 3. Both task and reply are deleted on the originating sequence. | 32 // 3. Both task and reply are deleted on the originating sequence. |
33 // | 33 // |
34 // 4. IsCanceledCallback can be run or deleted on any sequence. | 34 // 4. IsCanceledCallback can be run or deleted on any sequence. |
35 #ifndef BASE_TASK_CANCELABLE_TASK_TRACKER_H_ | 35 #ifndef BASE_TASK_CANCELABLE_TASK_TRACKER_H_ |
36 #define BASE_TASK_CANCELABLE_TASK_TRACKER_H_ | 36 #define BASE_TASK_CANCELABLE_TASK_TRACKER_H_ |
37 | 37 |
38 #include <stdint.h> | 38 #include <stdint.h> |
39 | 39 |
| 40 #include <utility> |
| 41 |
40 #include "base/base_export.h" | 42 #include "base/base_export.h" |
41 #include "base/bind.h" | 43 #include "base/bind.h" |
42 #include "base/callback.h" | 44 #include "base/callback.h" |
43 #include "base/containers/hash_tables.h" | 45 #include "base/containers/hash_tables.h" |
44 #include "base/macros.h" | 46 #include "base/macros.h" |
45 #include "base/memory/weak_ptr.h" | 47 #include "base/memory/weak_ptr.h" |
46 #include "base/post_task_and_reply_with_result_internal.h" | 48 #include "base/post_task_and_reply_with_result_internal.h" |
47 #include "base/sequence_checker.h" | 49 #include "base/sequence_checker.h" |
48 | 50 |
49 namespace tracked_objects { | 51 namespace tracked_objects { |
(...skipping 17 matching lines...) Expand all Loading... |
67 | 69 |
68 // Cancels all tracked tasks. | 70 // Cancels all tracked tasks. |
69 ~CancelableTaskTracker(); | 71 ~CancelableTaskTracker(); |
70 | 72 |
71 TaskId PostTask(base::TaskRunner* task_runner, | 73 TaskId PostTask(base::TaskRunner* task_runner, |
72 const tracked_objects::Location& from_here, | 74 const tracked_objects::Location& from_here, |
73 const base::Closure& task); | 75 const base::Closure& task); |
74 | 76 |
75 TaskId PostTaskAndReply(base::TaskRunner* task_runner, | 77 TaskId PostTaskAndReply(base::TaskRunner* task_runner, |
76 const tracked_objects::Location& from_here, | 78 const tracked_objects::Location& from_here, |
77 const base::Closure& task, | 79 base::Closure task, |
78 const base::Closure& reply); | 80 base::Closure reply); |
79 | 81 |
80 template <typename TaskReturnType, typename ReplyArgType> | 82 template <typename TaskReturnType, typename ReplyArgType> |
81 TaskId PostTaskAndReplyWithResult( | 83 TaskId PostTaskAndReplyWithResult(base::TaskRunner* task_runner, |
82 base::TaskRunner* task_runner, | 84 const tracked_objects::Location& from_here, |
83 const tracked_objects::Location& from_here, | 85 base::Callback<TaskReturnType()> task, |
84 const base::Callback<TaskReturnType(void)>& task, | 86 base::Callback<void(ReplyArgType)> reply) { |
85 const base::Callback<void(ReplyArgType)>& reply) { | |
86 TaskReturnType* result = new TaskReturnType(); | 87 TaskReturnType* result = new TaskReturnType(); |
87 return PostTaskAndReply( | 88 return PostTaskAndReply( |
88 task_runner, | 89 task_runner, from_here, |
89 from_here, | |
90 base::Bind(&base::internal::ReturnAsParamAdapter<TaskReturnType>, | 90 base::Bind(&base::internal::ReturnAsParamAdapter<TaskReturnType>, |
91 task, | 91 std::move(task), base::Unretained(result)), |
92 base::Unretained(result)), | |
93 base::Bind(&base::internal::ReplyAdapter<TaskReturnType, ReplyArgType>, | 92 base::Bind(&base::internal::ReplyAdapter<TaskReturnType, ReplyArgType>, |
94 reply, | 93 std::move(reply), base::Owned(result))); |
95 base::Owned(result))); | |
96 } | 94 } |
97 | 95 |
98 // Creates a tracked TaskId and an associated IsCanceledCallback. Client can | 96 // Creates a tracked TaskId and an associated IsCanceledCallback. Client can |
99 // later call TryCancel() with the returned TaskId, and run |is_canceled_cb| | 97 // later call TryCancel() with the returned TaskId, and run |is_canceled_cb| |
100 // from any thread to check whether the TaskId is canceled. | 98 // from any thread to check whether the TaskId is canceled. |
101 // | 99 // |
102 // The returned task ID is tracked until the last copy of | 100 // The returned task ID is tracked until the last copy of |
103 // |is_canceled_cb| is destroyed. | 101 // |is_canceled_cb| is destroyed. |
104 // | 102 // |
105 // Note. This function is used to address some special cancelation requirement | 103 // Note. This function is used to address some special cancelation requirement |
(...skipping 27 matching lines...) Expand all Loading... |
133 SequenceChecker sequence_checker_; | 131 SequenceChecker sequence_checker_; |
134 | 132 |
135 base::WeakPtrFactory<CancelableTaskTracker> weak_factory_; | 133 base::WeakPtrFactory<CancelableTaskTracker> weak_factory_; |
136 | 134 |
137 DISALLOW_COPY_AND_ASSIGN(CancelableTaskTracker); | 135 DISALLOW_COPY_AND_ASSIGN(CancelableTaskTracker); |
138 }; | 136 }; |
139 | 137 |
140 } // namespace base | 138 } // namespace base |
141 | 139 |
142 #endif // BASE_TASK_CANCELABLE_TASK_TRACKER_H_ | 140 #endif // BASE_TASK_CANCELABLE_TASK_TRACKER_H_ |
OLD | NEW |