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 "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 // scoped_ptr<Object> object_; | 92 // scoped_ptr<Object> object_; |
93 // }; | 93 // }; |
94 // | 94 // |
95 template <typename T> | 95 template <typename T> |
96 class SelfDeleterHelper { | 96 class SelfDeleterHelper { |
97 public: | 97 public: |
98 typedef base::Callback<void (scoped_ptr<T>)> DeletionCallback; | 98 typedef base::Callback<void (scoped_ptr<T>)> DeletionCallback; |
99 | 99 |
100 SelfDeleterHelper(T* self_deleting_object, | 100 SelfDeleterHelper(T* self_deleting_object, |
101 const DeletionCallback& deletion_callback) | 101 const DeletionCallback& deletion_callback) |
102 : weak_ptr_factory_(this), | 102 : construction_runner_(base::MessageLoopProxy::current()), |
103 construction_runner_(base::MessageLoopProxy::current()), | |
104 self_deleting_object_(self_deleting_object), | 103 self_deleting_object_(self_deleting_object), |
105 deletion_callback_(deletion_callback) { | 104 deletion_callback_(deletion_callback), |
| 105 weak_ptr_factory_(this) { |
106 } | 106 } |
107 | 107 |
108 ~SelfDeleterHelper() { | 108 ~SelfDeleterHelper() { |
109 DCHECK(construction_runner_->RunsTasksOnCurrentThread()); | 109 DCHECK(construction_runner_->RunsTasksOnCurrentThread()); |
110 } | 110 } |
111 | 111 |
112 void MaybeSelfDeleteSoon() { | 112 void MaybeSelfDeleteSoon() { |
113 DCHECK(!construction_runner_->RunsTasksOnCurrentThread()); | 113 DCHECK(!construction_runner_->RunsTasksOnCurrentThread()); |
114 construction_runner_->PostTask( | 114 construction_runner_->PostTask( |
115 FROM_HERE, | 115 FROM_HERE, |
116 base::Bind(&SelfDeleterHelper::SelfDelete, | 116 base::Bind(&SelfDeleterHelper::SelfDelete, |
117 weak_ptr_factory_.GetWeakPtr())); | 117 weak_ptr_factory_.GetWeakPtr())); |
118 } | 118 } |
119 | 119 |
120 private: | 120 private: |
121 void SelfDelete() { | 121 void SelfDelete() { |
122 DCHECK(construction_runner_->RunsTasksOnCurrentThread()); | 122 DCHECK(construction_runner_->RunsTasksOnCurrentThread()); |
123 deletion_callback_.Run(make_scoped_ptr(self_deleting_object_)); | 123 deletion_callback_.Run(make_scoped_ptr(self_deleting_object_)); |
124 } | 124 } |
125 | 125 |
126 base::WeakPtrFactory<SelfDeleterHelper<T> > weak_ptr_factory_; | |
127 const scoped_refptr<base::SingleThreadTaskRunner> construction_runner_; | 126 const scoped_refptr<base::SingleThreadTaskRunner> construction_runner_; |
128 T* const self_deleting_object_; | 127 T* const self_deleting_object_; |
129 const DeletionCallback deletion_callback_; | 128 const DeletionCallback deletion_callback_; |
130 | 129 |
| 130 //WeakPtrFactory's documentation says: |
| 131 // Member variables should appear before the WeakPtrFactory, to ensure |
| 132 // that any WeakPtrs to Controller are invalidated before its members |
| 133 // variable's destructors are executed, rendering them invalid. |
| 134 base::WeakPtrFactory<SelfDeleterHelper<T> > weak_ptr_factory_; |
| 135 |
131 DISALLOW_COPY_AND_ASSIGN(SelfDeleterHelper); | 136 DISALLOW_COPY_AND_ASSIGN(SelfDeleterHelper); |
132 }; | 137 }; |
133 | 138 |
134 } // namespace forwarder2 | 139 } // namespace forwarder2 |
135 | 140 |
136 #endif // TOOLS_ANDROID_FORWARDER2_SELF_DELETER_HELPER_H_ | 141 #endif // TOOLS_ANDROID_FORWARDER2_SELF_DELETER_HELPER_H_ |
OLD | NEW |