michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "chrome/common/notification_service.h" michael@0: michael@0: #include "base/lazy_instance.h" michael@0: #include "base/thread_local.h" michael@0: michael@0: static base::LazyInstance > michael@0: lazy_tls_ptr(base::LINKER_INITIALIZED); michael@0: michael@0: // static michael@0: NotificationService* NotificationService::current() { michael@0: return lazy_tls_ptr.Pointer()->Get(); michael@0: } michael@0: michael@0: // static michael@0: bool NotificationService::HasKey(const NotificationSourceMap& map, michael@0: const NotificationSource& source) { michael@0: return map.find(source.map_key()) != map.end(); michael@0: } michael@0: michael@0: NotificationService::NotificationService() { michael@0: DCHECK(current() == NULL); michael@0: #ifndef NDEBUG michael@0: memset(observer_counts_, 0, sizeof(observer_counts_)); michael@0: #endif michael@0: michael@0: lazy_tls_ptr.Pointer()->Set(this); michael@0: } michael@0: michael@0: void NotificationService::AddObserver(NotificationObserver* observer, michael@0: NotificationType type, michael@0: const NotificationSource& source) { michael@0: DCHECK(type.value < NotificationType::NOTIFICATION_TYPE_COUNT); michael@0: michael@0: // We have gotten some crashes where the observer pointer is NULL. The problem michael@0: // is that this happens when we actually execute a notification, so have no michael@0: // way of knowing who the bad observer was. We want to know when this happens michael@0: // in release mode so we know what code to blame the crash on (since this is michael@0: // guaranteed to crash later). michael@0: CHECK(observer); michael@0: michael@0: NotificationObserverList* observer_list; michael@0: if (HasKey(observers_[type.value], source)) { michael@0: observer_list = observers_[type.value][source.map_key()]; michael@0: } else { michael@0: observer_list = new NotificationObserverList; michael@0: observers_[type.value][source.map_key()] = observer_list; michael@0: } michael@0: michael@0: observer_list->AddObserver(observer); michael@0: #ifndef NDEBUG michael@0: ++observer_counts_[type.value]; michael@0: #endif michael@0: } michael@0: michael@0: void NotificationService::RemoveObserver(NotificationObserver* observer, michael@0: NotificationType type, michael@0: const NotificationSource& source) { michael@0: DCHECK(type.value < NotificationType::NOTIFICATION_TYPE_COUNT); michael@0: DCHECK(HasKey(observers_[type.value], source)); michael@0: michael@0: NotificationObserverList* observer_list = michael@0: observers_[type.value][source.map_key()]; michael@0: if (observer_list) { michael@0: observer_list->RemoveObserver(observer); michael@0: #ifndef NDEBUG michael@0: --observer_counts_[type.value]; michael@0: #endif michael@0: } michael@0: michael@0: // TODO(jhughes): Remove observer list from map if empty? michael@0: } michael@0: michael@0: void NotificationService::Notify(NotificationType type, michael@0: const NotificationSource& source, michael@0: const NotificationDetails& details) { michael@0: DCHECK(type.value > NotificationType::ALL) << michael@0: "Allowed for observing, but not posting."; michael@0: DCHECK(type.value < NotificationType::NOTIFICATION_TYPE_COUNT); michael@0: michael@0: // There's no particular reason for the order in which the different michael@0: // classes of observers get notified here. michael@0: michael@0: // Notify observers of all types and all sources michael@0: if (HasKey(observers_[NotificationType::ALL], AllSources()) && michael@0: source != AllSources()) { michael@0: FOR_EACH_OBSERVER(NotificationObserver, michael@0: *observers_[NotificationType::ALL][AllSources().map_key()], michael@0: Observe(type, source, details)); michael@0: } michael@0: michael@0: // Notify observers of all types and the given source michael@0: if (HasKey(observers_[NotificationType::ALL], source)) { michael@0: FOR_EACH_OBSERVER(NotificationObserver, michael@0: *observers_[NotificationType::ALL][source.map_key()], michael@0: Observe(type, source, details)); michael@0: } michael@0: michael@0: // Notify observers of the given type and all sources michael@0: if (HasKey(observers_[type.value], AllSources()) && michael@0: source != AllSources()) { michael@0: FOR_EACH_OBSERVER(NotificationObserver, michael@0: *observers_[type.value][AllSources().map_key()], michael@0: Observe(type, source, details)); michael@0: } michael@0: michael@0: // Notify observers of the given type and the given source michael@0: if (HasKey(observers_[type.value], source)) { michael@0: FOR_EACH_OBSERVER(NotificationObserver, michael@0: *observers_[type.value][source.map_key()], michael@0: Observe(type, source, details)); michael@0: } michael@0: } michael@0: michael@0: michael@0: NotificationService::~NotificationService() { michael@0: lazy_tls_ptr.Pointer()->Set(NULL); michael@0: michael@0: #ifndef NDEBUG michael@0: for (int i = 0; i < NotificationType::NOTIFICATION_TYPE_COUNT; i++) { michael@0: if (observer_counts_[i] > 0) { michael@0: // This may not be completely fixable -- see michael@0: // http://code.google.com/p/chromium/issues/detail?id=11010 . michael@0: // But any new leaks should be fixed. michael@0: CHROMIUM_LOG(WARNING) << observer_counts_[i] << " notification observer(s) leaked" michael@0: << " of notification type " << i; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: for (int i = 0; i < NotificationType::NOTIFICATION_TYPE_COUNT; i++) { michael@0: NotificationSourceMap omap = observers_[i]; michael@0: for (NotificationSourceMap::iterator it = omap.begin(); michael@0: it != omap.end(); ++it) { michael@0: delete it->second; michael@0: } michael@0: } michael@0: } michael@0: michael@0: NotificationObserver::~NotificationObserver() {}