Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(321)

Side by Side Diff: content/browser/notification_service_impl.cc

Issue 11273049: Revert 164120 - content/browser: Move more files into the content namespace. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/notification_service_impl.h" 5 #include "content/browser/notification_service_impl.h"
6 6
7 #include "base/lazy_instance.h" 7 #include "base/lazy_instance.h"
8 #include "base/threading/thread_local.h" 8 #include "base/threading/thread_local.h"
9 #include "content/public/browser/notification_observer.h" 9 #include "content/public/browser/notification_observer.h"
10 #include "content/public/browser/notification_types.h" 10 #include "content/public/browser/notification_types.h"
11 11
12 namespace content {
13
14 static base::LazyInstance<base::ThreadLocalPointer<NotificationServiceImpl> > 12 static base::LazyInstance<base::ThreadLocalPointer<NotificationServiceImpl> >
15 lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; 13 lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER;
16 14
17 // static 15 // static
18 NotificationServiceImpl* NotificationServiceImpl::current() { 16 NotificationServiceImpl* NotificationServiceImpl::current() {
19 return lazy_tls_ptr.Pointer()->Get(); 17 return lazy_tls_ptr.Pointer()->Get();
20 } 18 }
21 19
22 // static 20 // static
23 NotificationService* NotificationService::current() { 21 content::NotificationService* content::NotificationService::current() {
24 return NotificationServiceImpl::current(); 22 return NotificationServiceImpl::current();
25 } 23 }
26 24
27 // static 25 // static
28 NotificationService* NotificationService::Create() { 26 content::NotificationService* content::NotificationService::Create() {
29 return new NotificationServiceImpl; 27 return new NotificationServiceImpl;
30 } 28 }
31 29
32 // static 30 // static
33 bool NotificationServiceImpl::HasKey(const NotificationSourceMap& map, 31 bool NotificationServiceImpl::HasKey(const NotificationSourceMap& map,
34 const NotificationSource& source) { 32 const content::NotificationSource& source) {
35 return map.find(source.map_key()) != map.end(); 33 return map.find(source.map_key()) != map.end();
36 } 34 }
37 35
38 NotificationServiceImpl::NotificationServiceImpl() { 36 NotificationServiceImpl::NotificationServiceImpl() {
39 DCHECK(current() == NULL); 37 DCHECK(current() == NULL);
40 lazy_tls_ptr.Pointer()->Set(this); 38 lazy_tls_ptr.Pointer()->Set(this);
41 } 39 }
42 40
43 void NotificationServiceImpl::AddObserver(NotificationObserver* observer, 41 void NotificationServiceImpl::AddObserver(
44 int type, 42 content::NotificationObserver* observer,
45 const NotificationSource& source) { 43 int type,
44 const content::NotificationSource& source) {
46 // We have gotten some crashes where the observer pointer is NULL. The problem 45 // We have gotten some crashes where the observer pointer is NULL. The problem
47 // is that this happens when we actually execute a notification, so have no 46 // is that this happens when we actually execute a notification, so have no
48 // way of knowing who the bad observer was. We want to know when this happens 47 // way of knowing who the bad observer was. We want to know when this happens
49 // in release mode so we know what code to blame the crash on (since this is 48 // in release mode so we know what code to blame the crash on (since this is
50 // guaranteed to crash later). 49 // guaranteed to crash later).
51 CHECK(observer); 50 CHECK(observer);
52 51
53 NotificationObserverList* observer_list; 52 NotificationObserverList* observer_list;
54 if (HasKey(observers_[type], source)) { 53 if (HasKey(observers_[type], source)) {
55 observer_list = observers_[type][source.map_key()]; 54 observer_list = observers_[type][source.map_key()];
56 } else { 55 } else {
57 observer_list = new NotificationObserverList; 56 observer_list = new NotificationObserverList;
58 observers_[type][source.map_key()] = observer_list; 57 observers_[type][source.map_key()] = observer_list;
59 } 58 }
60 59
61 observer_list->AddObserver(observer); 60 observer_list->AddObserver(observer);
62 #ifndef NDEBUG 61 #ifndef NDEBUG
63 ++observer_counts_[type]; 62 ++observer_counts_[type];
64 #endif 63 #endif
65 } 64 }
66 65
67 void NotificationServiceImpl::RemoveObserver(NotificationObserver* observer, 66 void NotificationServiceImpl::RemoveObserver(
68 int type, 67 content::NotificationObserver* observer,
69 const NotificationSource& source) { 68 int type,
69 const content::NotificationSource& source) {
70 // This is a very serious bug. An object is most likely being deleted on 70 // This is a very serious bug. An object is most likely being deleted on
71 // the wrong thread, and as a result another thread's NotificationServiceImpl 71 // the wrong thread, and as a result another thread's NotificationServiceImpl
72 // has its deleted pointer in its map. A garbge object will be called in the 72 // has its deleted pointer in its map. A garbge object will be called in the
73 // future. 73 // future.
74 // NOTE: when this check shows crashes, use BrowserThread::DeleteOnIOThread or 74 // NOTE: when this check shows crashes, use BrowserThread::DeleteOnIOThread or
75 // other variants as the trait on the object. 75 // other variants as the trait on the object.
76 CHECK(HasKey(observers_[type], source)); 76 CHECK(HasKey(observers_[type], source));
77 77
78 NotificationObserverList* observer_list = 78 NotificationObserverList* observer_list =
79 observers_[type][source.map_key()]; 79 observers_[type][source.map_key()];
80 if (observer_list) { 80 if (observer_list) {
81 observer_list->RemoveObserver(observer); 81 observer_list->RemoveObserver(observer);
82 if (!observer_list->size()) { 82 if (!observer_list->size()) {
83 observers_[type].erase(source.map_key()); 83 observers_[type].erase(source.map_key());
84 delete observer_list; 84 delete observer_list;
85 } 85 }
86 #ifndef NDEBUG 86 #ifndef NDEBUG
87 --observer_counts_[type]; 87 --observer_counts_[type];
88 #endif 88 #endif
89 } 89 }
90 } 90 }
91 91
92 void NotificationServiceImpl::Notify(int type, 92 void NotificationServiceImpl::Notify(
93 const NotificationSource& source, 93 int type,
94 const NotificationDetails& details) { 94 const content::NotificationSource& source,
95 DCHECK(type > NOTIFICATION_ALL) << 95 const content::NotificationDetails& details) {
96 DCHECK(type > content::NOTIFICATION_ALL) <<
96 "Allowed for observing, but not posting."; 97 "Allowed for observing, but not posting.";
97 98
98 // There's no particular reason for the order in which the different 99 // There's no particular reason for the order in which the different
99 // classes of observers get notified here. 100 // classes of observers get notified here.
100 101
101 // Notify observers of all types and all sources 102 // Notify observers of all types and all sources
102 if (HasKey(observers_[NOTIFICATION_ALL], AllSources()) && 103 if (HasKey(observers_[content::NOTIFICATION_ALL], AllSources()) &&
103 source != AllSources()) { 104 source != AllSources()) {
104 FOR_EACH_OBSERVER(NotificationObserver, 105 FOR_EACH_OBSERVER(content::NotificationObserver,
105 *observers_[NOTIFICATION_ALL][AllSources().map_key()], 106 *observers_[content::NOTIFICATION_ALL][AllSources().map_key()],
106 Observe(type, source, details)); 107 Observe(type, source, details));
107 } 108 }
108 109
109 // Notify observers of all types and the given source 110 // Notify observers of all types and the given source
110 if (HasKey(observers_[NOTIFICATION_ALL], source)) { 111 if (HasKey(observers_[content::NOTIFICATION_ALL], source)) {
111 FOR_EACH_OBSERVER(NotificationObserver, 112 FOR_EACH_OBSERVER(content::NotificationObserver,
112 *observers_[NOTIFICATION_ALL][source.map_key()], 113 *observers_[content::NOTIFICATION_ALL][source.map_key()],
113 Observe(type, source, details)); 114 Observe(type, source, details));
114 } 115 }
115 116
116 // Notify observers of the given type and all sources 117 // Notify observers of the given type and all sources
117 if (HasKey(observers_[type], AllSources()) && 118 if (HasKey(observers_[type], AllSources()) &&
118 source != AllSources()) { 119 source != AllSources()) {
119 FOR_EACH_OBSERVER(NotificationObserver, 120 FOR_EACH_OBSERVER(content::NotificationObserver,
120 *observers_[type][AllSources().map_key()], 121 *observers_[type][AllSources().map_key()],
121 Observe(type, source, details)); 122 Observe(type, source, details));
122 } 123 }
123 124
124 // Notify observers of the given type and the given source 125 // Notify observers of the given type and the given source
125 if (HasKey(observers_[type], source)) { 126 if (HasKey(observers_[type], source)) {
126 FOR_EACH_OBSERVER(NotificationObserver, 127 FOR_EACH_OBSERVER(content::NotificationObserver,
127 *observers_[type][source.map_key()], 128 *observers_[type][source.map_key()],
128 Observe(type, source, details)); 129 Observe(type, source, details));
129 } 130 }
130 } 131 }
131 132
132 133
133 NotificationServiceImpl::~NotificationServiceImpl() { 134 NotificationServiceImpl::~NotificationServiceImpl() {
134 lazy_tls_ptr.Pointer()->Set(NULL); 135 lazy_tls_ptr.Pointer()->Set(NULL);
135 136
136 #ifndef NDEBUG 137 #ifndef NDEBUG
137 for (int i = 0; i < static_cast<int>(observer_counts_.size()); i++) { 138 for (int i = 0; i < static_cast<int>(observer_counts_.size()); i++) {
138 if (observer_counts_[i] > 0) { 139 if (observer_counts_[i] > 0) {
139 // This may not be completely fixable -- see 140 // This may not be completely fixable -- see
140 // http://code.google.com/p/chromium/issues/detail?id=11010 . 141 // http://code.google.com/p/chromium/issues/detail?id=11010 .
141 VLOG(1) << observer_counts_[i] << " notification observer(s) leaked " 142 VLOG(1) << observer_counts_[i] << " notification observer(s) leaked "
142 "of notification type " << i; 143 "of notification type " << i;
143 } 144 }
144 } 145 }
145 #endif 146 #endif
146 147
147 for (int i = 0; i < static_cast<int>(observers_.size()); i++) { 148 for (int i = 0; i < static_cast<int>(observers_.size()); i++) {
148 NotificationSourceMap omap = observers_[i]; 149 NotificationSourceMap omap = observers_[i];
149 for (NotificationSourceMap::iterator it = omap.begin(); 150 for (NotificationSourceMap::iterator it = omap.begin();
150 it != omap.end(); ++it) 151 it != omap.end(); ++it)
151 delete it->second; 152 delete it->second;
152 } 153 }
153 } 154 }
154
155 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/notification_service_impl.h ('k') | content/browser/notification_service_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698