OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // This file describes a central switchboard for notifications that might | 5 #ifndef CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_ |
6 // happen in various parts of the application, and allows users to register | 6 #define CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_ |
7 // observers for various classes of events that they're interested in. | |
8 | |
9 #ifndef CONTENT_COMMON_NOTIFICATION_SERVICE_H_ | |
10 #define CONTENT_COMMON_NOTIFICATION_SERVICE_H_ | |
11 #pragma once | 7 #pragma once |
12 | 8 |
13 #include <map> | 9 #include <map> |
14 | 10 |
15 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
16 #include "content/common/content_export.h" | 12 #include "content/common/content_export.h" |
17 #include "content/public/browser/notification_details.h" | 13 #include "content/public/browser/notification_service.h" |
18 #include "content/public/browser/notification_source.h" | |
19 | 14 |
20 namespace content { | 15 namespace content { |
21 class NotificationObserver; | 16 class NotificationObserver; |
22 class NotificationRegistrar; | 17 class NotificationRegistrar; |
23 } | 18 } |
24 | 19 |
25 class CONTENT_EXPORT NotificationService { | 20 class NotificationServiceImpl : public content::NotificationService { |
26 public: | 21 public: |
27 // Returns the NotificationService object for the current thread, or NULL if | 22 static NotificationServiceImpl* current(); |
28 // none. | |
29 static NotificationService* current(); | |
30 | 23 |
31 // Normally instantiated when the thread is created. Not all threads have | 24 // Normally instantiated when the thread is created. Not all threads have |
32 // a NotificationService. Only one instance should be created per thread. | 25 // a NotificationService. Only one instance should be created per thread. |
33 NotificationService(); | 26 NotificationServiceImpl(); |
34 ~NotificationService(); | 27 virtual ~NotificationServiceImpl(); |
35 | 28 |
36 // Synchronously posts a notification to all interested observers. | 29 // content::NotificationService |
37 // Source is a reference to a NotificationSource object representing | 30 virtual void Notify(int type, |
38 // the object originating the notification (can be | |
39 // NotificationService::AllSources(), in which case | |
40 // only observers interested in all sources will be notified). | |
41 // Details is a reference to an object containing additional data about | |
42 // the notification. If no additional data is needed, NoDetails() is used. | |
43 // There is no particular order in which the observers will be notified. | |
44 void Notify(int type, | |
45 const content::NotificationSource& source, | 31 const content::NotificationSource& source, |
46 const content::NotificationDetails& details); | 32 const content::NotificationDetails& details); |
47 | 33 |
48 // Returns a NotificationSource that represents all notification sources | |
49 // (for the purpose of registering an observer for events from all sources). | |
50 static content::Source<void> AllSources() { | |
51 return content::Source<void>(NULL); | |
52 } | |
53 | |
54 // Returns the same value as AllSources(). This function has semantic | |
55 // differences to the programmer: We have checked that this AllSources() | |
56 // usage is safe in the face of multiple profiles. Objects that were | |
57 // singletons now will always have multiple instances, one per browser | |
58 // context. | |
59 // | |
60 // Some usage is safe, where the Source is checked to see if it's a member of | |
61 // a container before use. But, we want the number of AllSources() calls to | |
62 // drop to almost nothing, because most usages are not multiprofile safe and | |
63 // were done because it was easier to listen to everything. | |
64 static content::Source<void> AllBrowserContextsAndSources() { | |
65 return content::Source<void>(NULL); | |
66 } | |
67 | |
68 // Returns a NotificationDetails object that represents a lack of details | |
69 // associated with a notification. (This is effectively a null pointer.) | |
70 static content::Details<void> NoDetails() { | |
71 return content::Details<void>(NULL); | |
72 } | |
73 | |
74 private: | 34 private: |
75 friend class content::NotificationRegistrar; | 35 friend class content::NotificationRegistrar; |
76 | 36 |
77 typedef ObserverList<content::NotificationObserver> NotificationObserverList; | 37 typedef ObserverList<content::NotificationObserver> NotificationObserverList; |
78 typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap; | 38 typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap; |
79 typedef std::map<int, NotificationSourceMap> NotificationObserverMap; | 39 typedef std::map<int, NotificationSourceMap> NotificationObserverMap; |
80 typedef std::map<int, int> NotificationObserverCount; | 40 typedef std::map<int, int> NotificationObserverCount; |
81 | 41 |
82 // Convenience function to determine whether a source has a | 42 // Convenience function to determine whether a source has a |
83 // NotificationObserverList in the given map; | 43 // NotificationObserverList in the given map; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 // Until we get a prohibitively large number of notification types, | 79 // Until we get a prohibitively large number of notification types, |
120 // a simple array is probably the fastest way to dispatch. | 80 // a simple array is probably the fastest way to dispatch. |
121 NotificationObserverMap observers_; | 81 NotificationObserverMap observers_; |
122 | 82 |
123 #ifndef NDEBUG | 83 #ifndef NDEBUG |
124 // Used to check to see that AddObserver and RemoveObserver calls are | 84 // Used to check to see that AddObserver and RemoveObserver calls are |
125 // balanced. | 85 // balanced. |
126 NotificationObserverCount observer_counts_; | 86 NotificationObserverCount observer_counts_; |
127 #endif | 87 #endif |
128 | 88 |
129 DISALLOW_COPY_AND_ASSIGN(NotificationService); | 89 DISALLOW_COPY_AND_ASSIGN(NotificationServiceImpl); |
130 }; | 90 }; |
131 | 91 |
132 #endif // CONTENT_COMMON_NOTIFICATION_SERVICE_H_ | 92 #endif // CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_ |
OLD | NEW |