From d9ad8baa4e77fd602f87881db9b7432206a7143f Mon Sep 17 00:00:00 2001 From: "pkasting@chromium.org" Date: Thu, 21 May 2009 00:34:44 +0000 Subject: 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 --- chrome/common/notification_registrar.cc | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'chrome/common/notification_registrar.cc') 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(); +} -- cgit v1.1