#include "content/browser/notification_service_impl.h"
#include "base/lazy_instance.h"
#include "base/threading/thread_local.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_types.h"
static base::LazyInstance<base::ThreadLocalPointer<NotificationServiceImpl> >
lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER;
NotificationServiceImpl* NotificationServiceImpl::current() {
return lazy_tls_ptr.Pointer()->Get();
}
content::NotificationService* content::NotificationService::current() {
return NotificationServiceImpl::current();
}
content::NotificationService* content::NotificationService::Create() {
return new NotificationServiceImpl;
}
bool NotificationServiceImpl::HasKey(const NotificationSourceMap& map,
const content::NotificationSource& source) {
return map.find(source.map_key()) != map.end();
}
NotificationServiceImpl::NotificationServiceImpl() {
DCHECK(current() == NULL);
lazy_tls_ptr.Pointer()->Set(this);
}
void NotificationServiceImpl::AddObserver(
content::NotificationObserver* observer,
int type,
const content::NotificationSource& source) {
CHECK(observer);
NotificationObserverList* observer_list;
if (HasKey(observers_[type], source)) {
observer_list = observers_[type][source.map_key()];
} else {
observer_list = new NotificationObserverList;
observers_[type][source.map_key()] = observer_list;
}
observer_list->AddObserver(observer);
#ifndef NDEBUG
++observer_counts_[type];
#endif
}
void NotificationServiceImpl::RemoveObserver(
content::NotificationObserver* observer,
int type,
const content::NotificationSource& source) {
CHECK(HasKey(observers_[type], source));
NotificationObserverList* observer_list =
observers_[type][source.map_key()];
if (observer_list) {
observer_list->RemoveObserver(observer);
if (!observer_list->size()) {
observers_[type].erase(source.map_key());
delete observer_list;
}
#ifndef NDEBUG
--observer_counts_[type];
#endif
}
}
void NotificationServiceImpl::Notify(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK(type > content::NOTIFICATION_ALL) <<
"Allowed for observing, but not posting.";
if (HasKey(observers_[content::NOTIFICATION_ALL], AllSources()) &&
source != AllSources()) {
FOR_EACH_OBSERVER(content::NotificationObserver,
*observers_[content::NOTIFICATION_ALL][AllSources().map_key()],
Observe(type, source, details));
}
if (HasKey(observers_[content::NOTIFICATION_ALL], source)) {
FOR_EACH_OBSERVER(content::NotificationObserver,
*observers_[content::NOTIFICATION_ALL][source.map_key()],
Observe(type, source, details));
}
if (HasKey(observers_[type], AllSources()) &&
source != AllSources()) {
FOR_EACH_OBSERVER(content::NotificationObserver,
*observers_[type][AllSources().map_key()],
Observe(type, source, details));
}
if (HasKey(observers_[type], source)) {
FOR_EACH_OBSERVER(content::NotificationObserver,
*observers_[type][source.map_key()],
Observe(type, source, details));
}
}
NotificationServiceImpl::~NotificationServiceImpl() {
lazy_tls_ptr.Pointer()->Set(NULL);
#ifndef NDEBUG
for (int i = 0; i < static_cast<int>(observer_counts_.size()); i++) {
if (observer_counts_[i] > 0) {
VLOG(1) << observer_counts_[i] << " notification observer(s) leaked "
"of notification type " << i;
}
}
#endif
for (int i = 0; i < static_cast<int>(observers_.size()); i++) {
NotificationSourceMap omap = observers_[i];
for (NotificationSourceMap::iterator it = omap.begin();
it != omap.end(); ++it)
delete it->second;
}
}