Index: base/callback_list.h |
=================================================================== |
--- base/callback_list.h (revision 269778) |
+++ 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,18 @@ |
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; |
+ } |
+ |
+ // Returns true if there are no subscriptions. This is only valid to call when |
+ // not looping through the list. |
+ bool empty() { |
+ DCHECK_EQ(0, active_iterator_count_); |
+ return callbacks_.empty(); |
+ } |
+ |
protected: |
// An iterator class that can be used to access the list of callbacks. |
class Iterator { |
@@ -167,17 +182,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); |
}; |