Chromium Code Reviews| Index: base/callback_list.h |
| =================================================================== |
| --- base/callback_list.h (revision 269342) |
| +++ base/callback_list.h (working copy) |
| @@ -89,10 +89,13 @@ |
| } |
| ~Subscription() { |
| - if (list_->active_iterator_count_) |
| + if (list_->active_iterator_count_) { |
| iter_->Reset(); |
| - else |
| + } else { |
| list_->callbacks_.erase(iter_); |
| + if (!list_->removal_callback_.is_null()) |
| + list_->removal_callback_.Run(); |
| + } |
| } |
| private: |
| @@ -111,6 +114,13 @@ |
| new Subscription(this, callbacks_.insert(callbacks_.end(), cb))); |
| } |
| + // Sets a callback which will be run when a subscription list is changed. |
| + void set_removal_callback(const Closure& callback) { |
| + removal_callback_ = callback; |
| + } |
| + |
| + bool empty() { return callbacks_.empty(); } |
|
Cait (Slow)
2014/05/09 20:11:13
Note that this will only return true when the acti
jam
2014/05/09 20:30:52
good point, thanks. i added a dcheck to ensure thi
|
| + |
| protected: |
| // An iterator class that can be used to access the list of callbacks. |
| class Iterator { |
| @@ -167,17 +177,24 @@ |
| // iteration. |
| void Compact() { |
| typename std::list<CallbackType>::iterator it = callbacks_.begin(); |
| + bool updated = false; |
| while (it != callbacks_.end()) { |
| - if ((*it).is_null()) |
| + if ((*it).is_null()) { |
| + updated = true; |
| it = callbacks_.erase(it); |
| - else |
| + } else { |
| ++it; |
| + } |
| + |
| + if (updated && !removal_callback_.is_null()) |
| + removal_callback_.Run(); |
| } |
| } |
| private: |
| std::list<CallbackType> callbacks_; |
| int active_iterator_count_; |
| + Closure removal_callback_; |
| DISALLOW_COPY_AND_ASSIGN(CallbackListBase); |
| }; |