Print Preview: Refactoring print/cancel button and print summary.
[chromium-blink-merge.git] / content / common / notification_service.cc
blob2089d9f343f90346578478eb6b9c517f8f16c77a
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);
14 // static
15 NotificationService* NotificationService::current() {
16 return lazy_tls_ptr.Pointer()->Get();
19 // static
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,
31 int type,
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).
38 CHECK(observer);
40 NotificationObserverList* observer_list;
41 if (HasKey(observers_[type], source)) {
42 observer_list = observers_[type][source.map_key()];
43 } else {
44 observer_list = new NotificationObserverList;
45 observers_[type][source.map_key()] = observer_list;
48 observer_list->AddObserver(observer);
49 #ifndef NDEBUG
50 ++observer_counts_[type];
51 #endif
54 void NotificationService::RemoveObserver(NotificationObserver* observer,
55 int type,
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
60 // future.
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()];
67 if (observer_list) {
68 observer_list->RemoveObserver(observer);
69 #ifndef NDEBUG
70 --observer_counts_[type];
71 #endif
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);
121 #ifndef NDEBUG
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;
130 #endif
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)
136 delete it->second;
140 NotificationObserver::NotificationObserver() {}
142 NotificationObserver::~NotificationObserver() {}