1 // Copyright (c) 2010 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/common/notification_service.h"
7 #include "base/lazy_instance.h"
8 #include "base/threading/thread_local.h"
9 #include "content/common/notification_observer.h"
11 static base::LazyInstance
<base::ThreadLocalPointer
<NotificationService
> >
12 lazy_tls_ptr(base::LINKER_INITIALIZED
);
15 NotificationService
* NotificationService::current() {
16 return lazy_tls_ptr
.Pointer()->Get();
20 bool NotificationService::HasKey(const NotificationSourceMap
& map
,
21 const NotificationSource
& source
) {
22 return map
.find(source
.map_key()) != map
.end();
25 NotificationService::NotificationService() {
26 DCHECK(current() == NULL
);
27 lazy_tls_ptr
.Pointer()->Set(this);
30 void NotificationService::AddObserver(NotificationObserver
* observer
,
32 const NotificationSource
& source
) {
33 // We have gotten some crashes where the observer pointer is NULL. The problem
34 // is that this happens when we actually execute a notification, so have no
35 // way of knowing who the bad observer was. We want to know when this happens
36 // in release mode so we know what code to blame the crash on (since this is
37 // guaranteed to crash later).
40 NotificationObserverList
* observer_list
;
41 if (HasKey(observers_
[type
], source
)) {
42 observer_list
= observers_
[type
][source
.map_key()];
44 observer_list
= new NotificationObserverList
;
45 observers_
[type
][source
.map_key()] = observer_list
;
48 observer_list
->AddObserver(observer
);
50 ++observer_counts_
[type
];
54 void NotificationService::RemoveObserver(NotificationObserver
* observer
,
56 const NotificationSource
& source
) {
57 // This is a very serious bug. An object is most likely being deleted on
58 // the wrong thread, and as a result another thread's NotificationService
59 // has its deleted pointer in its map. A garbge object will be called in the
61 // NOTE: when this check shows crashes, use BrowserThread::DeleteOnIOThread or
62 // other variants as the trait on the object.
63 CHECK(HasKey(observers_
[type
], source
));
65 NotificationObserverList
* observer_list
=
66 observers_
[type
][source
.map_key()];
68 observer_list
->RemoveObserver(observer
);
70 --observer_counts_
[type
];
74 // TODO(jhughes): Remove observer list from map if empty?
77 void NotificationService::Notify(int type
,
78 const NotificationSource
& source
,
79 const NotificationDetails
& details
) {
80 DCHECK(type
> content::NOTIFICATION_ALL
) <<
81 "Allowed for observing, but not posting.";
83 // There's no particular reason for the order in which the different
84 // classes of observers get notified here.
86 // Notify observers of all types and all sources
87 if (HasKey(observers_
[content::NOTIFICATION_ALL
], AllSources()) &&
88 source
!= AllSources()) {
89 FOR_EACH_OBSERVER(NotificationObserver
,
90 *observers_
[content::NOTIFICATION_ALL
][AllSources().map_key()],
91 Observe(type
, source
, details
));
94 // Notify observers of all types and the given source
95 if (HasKey(observers_
[content::NOTIFICATION_ALL
], source
)) {
96 FOR_EACH_OBSERVER(NotificationObserver
,
97 *observers_
[content::NOTIFICATION_ALL
][source
.map_key()],
98 Observe(type
, source
, details
));
101 // Notify observers of the given type and all sources
102 if (HasKey(observers_
[type
], AllSources()) &&
103 source
!= AllSources()) {
104 FOR_EACH_OBSERVER(NotificationObserver
,
105 *observers_
[type
][AllSources().map_key()],
106 Observe(type
, source
, details
));
109 // Notify observers of the given type and the given source
110 if (HasKey(observers_
[type
], source
)) {
111 FOR_EACH_OBSERVER(NotificationObserver
,
112 *observers_
[type
][source
.map_key()],
113 Observe(type
, source
, details
));
118 NotificationService::~NotificationService() {
119 lazy_tls_ptr
.Pointer()->Set(NULL
);
122 for (int i
= 0; i
< static_cast<int>(observer_counts_
.size()); i
++) {
123 if (observer_counts_
[i
] > 0) {
124 // This may not be completely fixable -- see
125 // http://code.google.com/p/chromium/issues/detail?id=11010 .
126 VLOG(1) << observer_counts_
[i
] << " notification observer(s) leaked "
127 "of notification type " << i
;
132 for (int i
= 0; i
< static_cast<int>(observers_
.size()); i
++) {
133 NotificationSourceMap omap
= observers_
[i
];
134 for (NotificationSourceMap::iterator it
= omap
.begin();
135 it
!= omap
.end(); ++it
)
140 NotificationObserver::NotificationObserver() {}
142 NotificationObserver::~NotificationObserver() {}