This source file includes following definitions.
- Add
- Remove
- RemoveAll
- IsEmpty
- IsRegistered
#include "content/public/browser/notification_registrar.h"
#include <algorithm>
#include "base/logging.h"
#include "content/browser/notification_service_impl.h"
namespace content {
struct NotificationRegistrar::Record {
bool operator==(const Record& other) const;
NotificationObserver* observer;
int type;
NotificationSource source;
};
bool NotificationRegistrar::Record::operator==(const Record& other) const {
return observer == other.observer &&
type == other.type &&
source == other.source;
}
NotificationRegistrar::NotificationRegistrar() {
NotificationServiceImpl::current();
DetachFromThread();
}
NotificationRegistrar::~NotificationRegistrar() {
RemoveAll();
}
void NotificationRegistrar::Add(NotificationObserver* observer,
int type,
const NotificationSource& source) {
DCHECK(CalledOnValidThread());
DCHECK(!IsRegistered(observer, type, source)) << "Duplicate registration.";
Record record = { observer, type, source };
registered_.push_back(record);
NotificationServiceImpl::current()->AddObserver(observer, type, source);
}
void NotificationRegistrar::Remove(NotificationObserver* observer,
int type,
const NotificationSource& source) {
DCHECK(CalledOnValidThread());
Record record = { observer, type, source };
RecordVector::iterator found = std::find(
registered_.begin(), registered_.end(), record);
DCHECK(found != registered_.end());
registered_.erase(found);
NotificationServiceImpl* service = NotificationServiceImpl::current();
if (service)
service->RemoveObserver(observer, type, source);
}
void NotificationRegistrar::RemoveAll() {
CHECK(CalledOnValidThread());
if (registered_.empty())
return;
NotificationServiceImpl* service = NotificationServiceImpl::current();
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 {
DCHECK(CalledOnValidThread());
return registered_.empty();
}
bool NotificationRegistrar::IsRegistered(NotificationObserver* observer,
int type,
const NotificationSource& source) {
DCHECK(CalledOnValidThread());
Record record = { observer, type, source };
return std::find(registered_.begin(), registered_.end(), record) !=
registered_.end();
}
}