Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
1 // Copyright (c) 2006-2008 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 "chrome/common/notification_service.h"
7 #include "base/lazy_instance.h"
8 #include "base/thread_local.h"
10 static base::LazyInstance<base::ThreadLocalPointer<NotificationService> >
11 lazy_tls_ptr(base::LINKER_INITIALIZED);
13 // static
14 NotificationService* NotificationService::current() {
15 return lazy_tls_ptr.Pointer()->Get();
16 }
18 // static
19 bool NotificationService::HasKey(const NotificationSourceMap& map,
20 const NotificationSource& source) {
21 return map.find(source.map_key()) != map.end();
22 }
24 NotificationService::NotificationService() {
25 DCHECK(current() == NULL);
26 #ifndef NDEBUG
27 memset(observer_counts_, 0, sizeof(observer_counts_));
28 #endif
30 lazy_tls_ptr.Pointer()->Set(this);
31 }
33 void NotificationService::AddObserver(NotificationObserver* observer,
34 NotificationType type,
35 const NotificationSource& source) {
36 DCHECK(type.value < NotificationType::NOTIFICATION_TYPE_COUNT);
38 // We have gotten some crashes where the observer pointer is NULL. The problem
39 // is that this happens when we actually execute a notification, so have no
40 // way of knowing who the bad observer was. We want to know when this happens
41 // in release mode so we know what code to blame the crash on (since this is
42 // guaranteed to crash later).
43 CHECK(observer);
45 NotificationObserverList* observer_list;
46 if (HasKey(observers_[type.value], source)) {
47 observer_list = observers_[type.value][source.map_key()];
48 } else {
49 observer_list = new NotificationObserverList;
50 observers_[type.value][source.map_key()] = observer_list;
51 }
53 observer_list->AddObserver(observer);
54 #ifndef NDEBUG
55 ++observer_counts_[type.value];
56 #endif
57 }
59 void NotificationService::RemoveObserver(NotificationObserver* observer,
60 NotificationType type,
61 const NotificationSource& source) {
62 DCHECK(type.value < NotificationType::NOTIFICATION_TYPE_COUNT);
63 DCHECK(HasKey(observers_[type.value], source));
65 NotificationObserverList* observer_list =
66 observers_[type.value][source.map_key()];
67 if (observer_list) {
68 observer_list->RemoveObserver(observer);
69 #ifndef NDEBUG
70 --observer_counts_[type.value];
71 #endif
72 }
74 // TODO(jhughes): Remove observer list from map if empty?
75 }
77 void NotificationService::Notify(NotificationType type,
78 const NotificationSource& source,
79 const NotificationDetails& details) {
80 DCHECK(type.value > NotificationType::ALL) <<
81 "Allowed for observing, but not posting.";
82 DCHECK(type.value < NotificationType::NOTIFICATION_TYPE_COUNT);
84 // There's no particular reason for the order in which the different
85 // classes of observers get notified here.
87 // Notify observers of all types and all sources
88 if (HasKey(observers_[NotificationType::ALL], AllSources()) &&
89 source != AllSources()) {
90 FOR_EACH_OBSERVER(NotificationObserver,
91 *observers_[NotificationType::ALL][AllSources().map_key()],
92 Observe(type, source, details));
93 }
95 // Notify observers of all types and the given source
96 if (HasKey(observers_[NotificationType::ALL], source)) {
97 FOR_EACH_OBSERVER(NotificationObserver,
98 *observers_[NotificationType::ALL][source.map_key()],
99 Observe(type, source, details));
100 }
102 // Notify observers of the given type and all sources
103 if (HasKey(observers_[type.value], AllSources()) &&
104 source != AllSources()) {
105 FOR_EACH_OBSERVER(NotificationObserver,
106 *observers_[type.value][AllSources().map_key()],
107 Observe(type, source, details));
108 }
110 // Notify observers of the given type and the given source
111 if (HasKey(observers_[type.value], source)) {
112 FOR_EACH_OBSERVER(NotificationObserver,
113 *observers_[type.value][source.map_key()],
114 Observe(type, source, details));
115 }
116 }
119 NotificationService::~NotificationService() {
120 lazy_tls_ptr.Pointer()->Set(NULL);
122 #ifndef NDEBUG
123 for (int i = 0; i < NotificationType::NOTIFICATION_TYPE_COUNT; i++) {
124 if (observer_counts_[i] > 0) {
125 // This may not be completely fixable -- see
126 // http://code.google.com/p/chromium/issues/detail?id=11010 .
127 // But any new leaks should be fixed.
128 CHROMIUM_LOG(WARNING) << observer_counts_[i] << " notification observer(s) leaked"
129 << " of notification type " << i;
130 }
131 }
132 #endif
134 for (int i = 0; i < NotificationType::NOTIFICATION_TYPE_COUNT; i++) {
135 NotificationSourceMap omap = observers_[i];
136 for (NotificationSourceMap::iterator it = omap.begin();
137 it != omap.end(); ++it) {
138 delete it->second;
139 }
140 }
141 }
143 NotificationObserver::~NotificationObserver() {}