1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/public/browser/notification_registrar.h"
9 #include "base/logging.h"
10 #include "content/browser/notification_service_impl.h"
14 struct NotificationRegistrar::Record
{
15 bool operator==(const Record
& other
) const;
17 NotificationObserver
* observer
;
19 NotificationSource source
;
22 bool NotificationRegistrar::Record::operator==(const Record
& other
) const {
23 return observer
== other
.observer
&&
25 source
== other
.source
;
28 NotificationRegistrar::NotificationRegistrar() {
29 // Force the NotificationService to be constructed (if it isn't already).
30 // This ensures the NotificationService will be registered on the
31 // AtExitManager before any objects which access it via NotificationRegistrar.
32 // This in turn means it will be destroyed after these objects, so they will
33 // never try to access the NotificationService after it's been destroyed.
34 NotificationServiceImpl::current();
35 // It is OK to create a NotificationRegistrar instance on one thread and then
36 // use it (exclusively) on another, so we detach from the initial thread.
40 NotificationRegistrar::~NotificationRegistrar() {
44 void NotificationRegistrar::Add(NotificationObserver
* observer
,
46 const NotificationSource
& source
) {
47 DCHECK(CalledOnValidThread());
48 DCHECK(!IsRegistered(observer
, type
, source
)) << "Duplicate registration.";
50 Record record
= { observer
, type
, source
};
51 registered_
.push_back(record
);
53 NotificationServiceImpl::current()->AddObserver(observer
, type
, source
);
56 void NotificationRegistrar::Remove(NotificationObserver
* observer
,
58 const NotificationSource
& source
) {
59 DCHECK(CalledOnValidThread());
61 Record record
= { observer
, type
, source
};
62 RecordVector::iterator found
= std::find(
63 registered_
.begin(), registered_
.end(), record
);
64 if (found
== registered_
.end()) {
65 NOTREACHED() << "Trying to remove unregistered observer of type " <<
66 type
<< " from list of size " << registered_
.size() << ".";
69 registered_
.erase(found
);
71 // This can be NULL if our owner outlives the NotificationService, e.g. if our
72 // owner is a Singleton.
73 NotificationServiceImpl
* service
= NotificationServiceImpl::current();
75 service
->RemoveObserver(observer
, type
, source
);
78 void NotificationRegistrar::RemoveAll() {
79 CHECK(CalledOnValidThread());
80 // Early-exit if no registrations, to avoid calling
81 // NotificationService::current. If we've constructed an object with a
82 // NotificationRegistrar member, but haven't actually used the notification
83 // service, and we reach prgram exit, then calling current() below could try
84 // to initialize the service's lazy TLS pointer during exit, which throws
85 // wrenches at things.
86 if (registered_
.empty())
89 // This can be NULL if our owner outlives the NotificationService, e.g. if our
90 // owner is a Singleton.
91 NotificationServiceImpl
* service
= NotificationServiceImpl::current();
93 for (size_t i
= 0; i
< registered_
.size(); i
++) {
94 service
->RemoveObserver(registered_
[i
].observer
,
96 registered_
[i
].source
);
102 bool NotificationRegistrar::IsEmpty() const {
103 DCHECK(CalledOnValidThread());
104 return registered_
.empty();
107 bool NotificationRegistrar::IsRegistered(NotificationObserver
* observer
,
109 const NotificationSource
& source
) {
110 DCHECK(CalledOnValidThread());
111 Record record
= { observer
, type
, source
};
112 return std::find(registered_
.begin(), registered_
.end(), record
) !=
116 } // namespace content