This source file includes following definitions.
- current
- current
- Create
- HasKey
- AddObserver
- RemoveObserver
- Notify
#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"
namespace content {
namespace {
base::LazyInstance<base::ThreadLocalPointer<NotificationServiceImpl> >
lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER;
}
NotificationServiceImpl* NotificationServiceImpl::current() {
return lazy_tls_ptr.Pointer()->Get();
}
NotificationService* NotificationService::current() {
return NotificationServiceImpl::current();
}
NotificationService* NotificationService::Create() {
return new NotificationServiceImpl;
}
bool NotificationServiceImpl::HasKey(const NotificationSourceMap& map,
const NotificationSource& source) {
return map.find(source.map_key()) != map.end();
}
NotificationServiceImpl::NotificationServiceImpl() {
DCHECK(current() == NULL);
lazy_tls_ptr.Pointer()->Set(this);
}
void NotificationServiceImpl::AddObserver(NotificationObserver* observer,
int type,
const 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(NotificationObserver* observer,
int type,
const 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->might_have_observers()) {
observers_[type].erase(source.map_key());
delete observer_list;
}
#ifndef NDEBUG
--observer_counts_[type];
#endif
}
}
void NotificationServiceImpl::Notify(int type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK_GT(type, NOTIFICATION_ALL) <<
"Allowed for observing, but not posting.";
if (HasKey(observers_[NOTIFICATION_ALL], AllSources()) &&
source != AllSources()) {
FOR_EACH_OBSERVER(NotificationObserver,
*observers_[NOTIFICATION_ALL][AllSources().map_key()],
Observe(type, source, details));
}
if (HasKey(observers_[NOTIFICATION_ALL], source)) {
FOR_EACH_OBSERVER(NotificationObserver,
*observers_[NOTIFICATION_ALL][source.map_key()],
Observe(type, source, details));
}
if (HasKey(observers_[type], AllSources()) &&
source != AllSources()) {
FOR_EACH_OBSERVER(NotificationObserver,
*observers_[type][AllSources().map_key()],
Observe(type, source, details));
}
if (HasKey(observers_[type], source)) {
FOR_EACH_OBSERVER(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;
}
}
}