diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-21 00:34:44 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-21 00:34:44 +0000 |
commit | d9ad8baa4e77fd602f87881db9b7432206a7143f (patch) | |
tree | 52d0e758bc65e4b5a090ece741bf6d5ba505a155 /chrome/common/notification_registrar.cc | |
parent | 7a70481028d8e7c40796c79d70d8f98604e9d152 (diff) | |
download | chromium_src-d9ad8baa4e77fd602f87881db9b7432206a7143f.zip chromium_src-d9ad8baa4e77fd602f87881db9b7432206a7143f.tar.gz chromium_src-d9ad8baa4e77fd602f87881db9b7432206a7143f.tar.bz2 |
Make the NotificationRegistrar safe for use in Singletons, which may outlive the NotificationService instances, by checking whether the service exists before calling RemoveObserver() on it.
Also add comments in NotificationService telling people to use NotificationRegistrar.
BUG=2381
Review URL: http://codereview.chromium.org/115601
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16554 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/notification_registrar.cc')
-rw-r--r-- | chrome/common/notification_registrar.cc | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/chrome/common/notification_registrar.cc b/chrome/common/notification_registrar.cc index b9ea3d7..73635b5 100644 --- a/chrome/common/notification_registrar.cc +++ b/chrome/common/notification_registrar.cc @@ -57,15 +57,27 @@ void NotificationRegistrar::Remove(NotificationObserver* observer, NOTREACHED(); } - NotificationService::current()->RemoveObserver(observer, type, source); + // This can be NULL if our owner outlives the NotificationService, e.g. if our + // owner is a Singleton. + NotificationService* service = NotificationService::current(); + if (service) + service->RemoveObserver(observer, type, source); } void NotificationRegistrar::RemoveAll() { + // This can be NULL if our owner outlives the NotificationService, e.g. if our + // owner is a Singleton. NotificationService* service = NotificationService::current(); - for (size_t i = 0; i < registered_.size(); i++) { - service->RemoveObserver(registered_[i].observer, - registered_[i].type, - registered_[i].source); + if (service) { + for (size_t i = 0; i < registered_.size(); i++) { + service->RemoveObserver(registered_[i].observer, + registered_[i].type, + registered_[i].source); + } } registered_.clear(); } + +bool NotificationRegistrar::IsEmpty() const { + return registered_.empty(); +} |