| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/common/cancelable_task_tracker.h" | 5 #include "chrome/common/cancelable_task_tracker.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 } | 77 } |
| 78 | 78 |
| 79 CancelableTaskTracker::TaskId CancelableTaskTracker::PostTaskAndReply( | 79 CancelableTaskTracker::TaskId CancelableTaskTracker::PostTaskAndReply( |
| 80 TaskRunner* task_runner, | 80 TaskRunner* task_runner, |
| 81 const tracked_objects::Location& from_here, | 81 const tracked_objects::Location& from_here, |
| 82 const Closure& task, | 82 const Closure& task, |
| 83 const Closure& reply) { | 83 const Closure& reply) { |
| 84 DCHECK(thread_checker_.CalledOnValidThread()); | 84 DCHECK(thread_checker_.CalledOnValidThread()); |
| 85 | 85 |
| 86 // We need a MessageLoop to run reply. | 86 // We need a MessageLoop to run reply. |
| 87 DCHECK(base::MessageLoopProxy::current()); | 87 DCHECK(base::MessageLoopProxy::current().get()); |
| 88 | 88 |
| 89 // Owned by reply callback below. | 89 // Owned by reply callback below. |
| 90 CancellationFlag* flag = new CancellationFlag(); | 90 CancellationFlag* flag = new CancellationFlag(); |
| 91 | 91 |
| 92 TaskId id = next_id_; | 92 TaskId id = next_id_; |
| 93 next_id_++; // int64 is big enough that we ignore the potential overflow. | 93 next_id_++; // int64 is big enough that we ignore the potential overflow. |
| 94 | 94 |
| 95 const Closure& untrack_closure = Bind( | 95 const Closure& untrack_closure = Bind( |
| 96 &CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id); | 96 &CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id); |
| 97 bool success = task_runner->PostTaskAndReply( | 97 bool success = task_runner->PostTaskAndReply( |
| 98 from_here, | 98 from_here, |
| 99 Bind(&RunIfNotCanceled, flag, task), | 99 Bind(&RunIfNotCanceled, flag, task), |
| 100 Bind(&RunIfNotCanceledThenUntrack, | 100 Bind(&RunIfNotCanceledThenUntrack, |
| 101 base::Owned(flag), reply, untrack_closure)); | 101 base::Owned(flag), reply, untrack_closure)); |
| 102 | 102 |
| 103 if (!success) | 103 if (!success) |
| 104 return kBadTaskId; | 104 return kBadTaskId; |
| 105 | 105 |
| 106 Track(id, flag); | 106 Track(id, flag); |
| 107 return id; | 107 return id; |
| 108 } | 108 } |
| 109 | 109 |
| 110 CancelableTaskTracker::TaskId CancelableTaskTracker::NewTrackedTaskId( | 110 CancelableTaskTracker::TaskId CancelableTaskTracker::NewTrackedTaskId( |
| 111 IsCanceledCallback* is_canceled_cb) { | 111 IsCanceledCallback* is_canceled_cb) { |
| 112 DCHECK(thread_checker_.CalledOnValidThread()); | 112 DCHECK(thread_checker_.CalledOnValidThread()); |
| 113 DCHECK(base::MessageLoopProxy::current()); | 113 DCHECK(base::MessageLoopProxy::current().get()); |
| 114 | 114 |
| 115 TaskId id = next_id_; | 115 TaskId id = next_id_; |
| 116 next_id_++; // int64 is big enough that we ignore the potential overflow. | 116 next_id_++; // int64 is big enough that we ignore the potential overflow. |
| 117 | 117 |
| 118 // Will be deleted by |untrack_and_delete_flag| after Untrack(). | 118 // Will be deleted by |untrack_and_delete_flag| after Untrack(). |
| 119 CancellationFlag* flag = new CancellationFlag(); | 119 CancellationFlag* flag = new CancellationFlag(); |
| 120 | 120 |
| 121 Closure untrack_and_delete_flag = Bind( | 121 Closure untrack_and_delete_flag = Bind( |
| 122 &RunAndDeleteFlag, | 122 &RunAndDeleteFlag, |
| 123 Bind(&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id), | 123 Bind(&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id), |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 | 174 |
| 175 bool success = task_flags_.insert(std::make_pair(id, flag)).second; | 175 bool success = task_flags_.insert(std::make_pair(id, flag)).second; |
| 176 DCHECK(success); | 176 DCHECK(success); |
| 177 } | 177 } |
| 178 | 178 |
| 179 void CancelableTaskTracker::Untrack(TaskId id) { | 179 void CancelableTaskTracker::Untrack(TaskId id) { |
| 180 DCHECK(thread_checker_.CalledOnValidThread()); | 180 DCHECK(thread_checker_.CalledOnValidThread()); |
| 181 size_t num = task_flags_.erase(id); | 181 size_t num = task_flags_.erase(id); |
| 182 DCHECK_EQ(1u, num); | 182 DCHECK_EQ(1u, num); |
| 183 } | 183 } |
| OLD | NEW |