| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 TOOLS_ANDROID_FORWARDER2_SELF_DELETER_HELPER_H_ | 5 #ifndef TOOLS_ANDROID_FORWARDER2_SELF_DELETER_HELPER_H_ |
| 6 #define TOOLS_ANDROID_FORWARDER2_SELF_DELETER_HELPER_H_ | 6 #define TOOLS_ANDROID_FORWARDER2_SELF_DELETER_HELPER_H_ |
| 7 | 7 |
| 8 #include <memory> |
| 9 |
| 8 #include "base/bind.h" | 10 #include "base/bind.h" |
| 9 #include "base/callback.h" | 11 #include "base/callback.h" |
| 10 #include "base/location.h" | 12 #include "base/location.h" |
| 11 #include "base/logging.h" | 13 #include "base/logging.h" |
| 12 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 16 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
| 17 | 19 |
| 18 namespace base { | 20 namespace base { |
| 19 | 21 |
| 20 class SingleThreadTaskRunner; | 22 class SingleThreadTaskRunner; |
| 21 | 23 |
| 22 } // namespace base | 24 } // namespace base |
| 23 | 25 |
| 24 namespace forwarder2 { | 26 namespace forwarder2 { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 35 // 2) In the internal thread, to trigger self-deletion, call the | 37 // 2) In the internal thread, to trigger self-deletion, call the |
| 36 // MaybeDeleteSoon() method on this member. | 38 // MaybeDeleteSoon() method on this member. |
| 37 // | 39 // |
| 38 // MaybeDeleteSoon() posts a task on the message loop where the T instance was | 40 // MaybeDeleteSoon() posts a task on the message loop where the T instance was |
| 39 // created to delete it. The task will be safely ignored if the instance is | 41 // created to delete it. The task will be safely ignored if the instance is |
| 40 // otherwise deleted. | 42 // otherwise deleted. |
| 41 // | 43 // |
| 42 // Usage example: | 44 // Usage example: |
| 43 // class Object { | 45 // class Object { |
| 44 // public: | 46 // public: |
| 45 // typedef base::Callback<void (scoped_ptr<Object>)> ErrorCallback; | 47 // typedef base::Callback<void (std::unique_ptr<Object>)> ErrorCallback; |
| 46 // | 48 // |
| 47 // Object(const ErrorCallback& error_callback) | 49 // Object(const ErrorCallback& error_callback) |
| 48 // : self_deleter_helper_(this, error_callback) { | 50 // : self_deleter_helper_(this, error_callback) { |
| 49 // } | 51 // } |
| 50 // | 52 // |
| 51 // void StartWork() { | 53 // void StartWork() { |
| 52 // // Post a callback to DoSomethingOnWorkerThread() below to another | 54 // // Post a callback to DoSomethingOnWorkerThread() below to another |
| 53 // // thread. | 55 // // thread. |
| 54 // } | 56 // } |
| 55 // | 57 // |
| (...skipping 17 matching lines...) Expand all Loading... |
| 73 // // that the ObjectOwner instance is still alive when | 75 // // that the ObjectOwner instance is still alive when |
| 74 // // DeleteObjectOnError() gets called below. This can be achieved by | 76 // // DeleteObjectOnError() gets called below. This can be achieved by |
| 75 // // using a WeakPtr<ObjectOwner> for instance. | 77 // // using a WeakPtr<ObjectOwner> for instance. |
| 76 // } | 78 // } |
| 77 // | 79 // |
| 78 // void StartWork() { | 80 // void StartWork() { |
| 79 // object_->StartWork(); | 81 // object_->StartWork(); |
| 80 // } | 82 // } |
| 81 // | 83 // |
| 82 // private: | 84 // private: |
| 83 // void DeleteObjectOnError(scoped_ptr<Object> object) { | 85 // void DeleteObjectOnError(std::unique_ptr<Object> object) { |
| 84 // DCHECK(thread_checker_.CalledOnValidThread()); | 86 // DCHECK(thread_checker_.CalledOnValidThread()); |
| 85 // DCHECK_EQ(object_, object); | 87 // DCHECK_EQ(object_, object); |
| 86 // // Do some extra work with |object| before it gets deleted... | 88 // // Do some extra work with |object| before it gets deleted... |
| 87 // object_.reset(); | 89 // object_.reset(); |
| 88 // ignore_result(object.release()); | 90 // ignore_result(object.release()); |
| 89 // } | 91 // } |
| 90 // | 92 // |
| 91 // base::ThreadChecker thread_checker_; | 93 // base::ThreadChecker thread_checker_; |
| 92 // scoped_ptr<Object> object_; | 94 // std::unique_ptr<Object> object_; |
| 93 // }; | 95 // }; |
| 94 // | 96 // |
| 95 template <typename T> | 97 template <typename T> |
| 96 class SelfDeleterHelper { | 98 class SelfDeleterHelper { |
| 97 public: | 99 public: |
| 98 typedef base::Callback<void (scoped_ptr<T>)> DeletionCallback; | 100 typedef base::Callback<void(std::unique_ptr<T>)> DeletionCallback; |
| 99 | 101 |
| 100 SelfDeleterHelper(T* self_deleting_object, | 102 SelfDeleterHelper(T* self_deleting_object, |
| 101 const DeletionCallback& deletion_callback) | 103 const DeletionCallback& deletion_callback) |
| 102 : construction_runner_(base::ThreadTaskRunnerHandle::Get()), | 104 : construction_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 103 self_deleting_object_(self_deleting_object), | 105 self_deleting_object_(self_deleting_object), |
| 104 deletion_callback_(deletion_callback), | 106 deletion_callback_(deletion_callback), |
| 105 weak_ptr_factory_(this) {} | 107 weak_ptr_factory_(this) {} |
| 106 | 108 |
| 107 ~SelfDeleterHelper() { | 109 ~SelfDeleterHelper() { |
| 108 DCHECK(construction_runner_->RunsTasksOnCurrentThread()); | 110 DCHECK(construction_runner_->RunsTasksOnCurrentThread()); |
| 109 } | 111 } |
| 110 | 112 |
| 111 void MaybeSelfDeleteSoon() { | 113 void MaybeSelfDeleteSoon() { |
| 112 DCHECK(!construction_runner_->RunsTasksOnCurrentThread()); | 114 DCHECK(!construction_runner_->RunsTasksOnCurrentThread()); |
| 113 construction_runner_->PostTask( | 115 construction_runner_->PostTask( |
| 114 FROM_HERE, | 116 FROM_HERE, |
| 115 base::Bind(&SelfDeleterHelper::SelfDelete, | 117 base::Bind(&SelfDeleterHelper::SelfDelete, |
| 116 weak_ptr_factory_.GetWeakPtr())); | 118 weak_ptr_factory_.GetWeakPtr())); |
| 117 } | 119 } |
| 118 | 120 |
| 119 private: | 121 private: |
| 120 void SelfDelete() { | 122 void SelfDelete() { |
| 121 DCHECK(construction_runner_->RunsTasksOnCurrentThread()); | 123 DCHECK(construction_runner_->RunsTasksOnCurrentThread()); |
| 122 deletion_callback_.Run(make_scoped_ptr(self_deleting_object_)); | 124 deletion_callback_.Run(base::WrapUnique(self_deleting_object_)); |
| 123 } | 125 } |
| 124 | 126 |
| 125 const scoped_refptr<base::SingleThreadTaskRunner> construction_runner_; | 127 const scoped_refptr<base::SingleThreadTaskRunner> construction_runner_; |
| 126 T* const self_deleting_object_; | 128 T* const self_deleting_object_; |
| 127 const DeletionCallback deletion_callback_; | 129 const DeletionCallback deletion_callback_; |
| 128 | 130 |
| 129 //WeakPtrFactory's documentation says: | 131 //WeakPtrFactory's documentation says: |
| 130 // Member variables should appear before the WeakPtrFactory, to ensure | 132 // Member variables should appear before the WeakPtrFactory, to ensure |
| 131 // that any WeakPtrs to Controller are invalidated before its members | 133 // that any WeakPtrs to Controller are invalidated before its members |
| 132 // variable's destructors are executed, rendering them invalid. | 134 // variable's destructors are executed, rendering them invalid. |
| 133 base::WeakPtrFactory<SelfDeleterHelper<T> > weak_ptr_factory_; | 135 base::WeakPtrFactory<SelfDeleterHelper<T> > weak_ptr_factory_; |
| 134 | 136 |
| 135 DISALLOW_COPY_AND_ASSIGN(SelfDeleterHelper); | 137 DISALLOW_COPY_AND_ASSIGN(SelfDeleterHelper); |
| 136 }; | 138 }; |
| 137 | 139 |
| 138 } // namespace forwarder2 | 140 } // namespace forwarder2 |
| 139 | 141 |
| 140 #endif // TOOLS_ANDROID_FORWARDER2_SELF_DELETER_HELPER_H_ | 142 #endif // TOOLS_ANDROID_FORWARDER2_SELF_DELETER_HELPER_H_ |
| OLD | NEW |