OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 // This file describes a central switchboard for notifications that might | |
6 // happen in various parts of the application, and allows users to register | |
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 | |
12 | |
13 #include <map> | |
14 | |
15 #include "base/observer_list.h" | |
16 #include "content/common/content_export.h" | |
17 #include "content/public/browser/notification_details.h" | |
18 #include "content/public/browser/notification_source.h" | |
19 | |
20 namespace content { | |
21 class NotificationObserver; | |
22 class NotificationRegistrar; | |
23 } | |
24 | |
25 class CONTENT_EXPORT NotificationService { | |
26 public: | |
27 // Returns the NotificationService object for the current thread, or NULL if | |
28 // none. | |
29 static NotificationService* current(); | |
30 | |
31 // Normally instantiated when the thread is created. Not all threads have | |
32 // a NotificationService. Only one instance should be created per thread. | |
33 NotificationService(); | |
34 ~NotificationService(); | |
35 | |
36 // Synchronously posts a notification to all interested observers. | |
37 // Source is a reference to a NotificationSource object representing | |
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, | |
46 const content::NotificationDetails& details); | |
47 | |
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: | |
75 friend class content::NotificationRegistrar; | |
76 | |
77 typedef ObserverList<content::NotificationObserver> NotificationObserverList; | |
78 typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap; | |
79 typedef std::map<int, NotificationSourceMap> NotificationObserverMap; | |
80 typedef std::map<int, int> NotificationObserverCount; | |
81 | |
82 // Convenience function to determine whether a source has a | |
83 // NotificationObserverList in the given map; | |
84 static bool HasKey(const NotificationSourceMap& map, | |
85 const content::NotificationSource& source); | |
86 | |
87 // NOTE: Rather than using this directly, you should use a | |
88 // NotificationRegistrar. | |
89 // | |
90 // Registers a NotificationObserver to be called whenever a matching | |
91 // notification is posted. Observer is a pointer to an object subclassing | |
92 // NotificationObserver to be notified when an event matching the other two | |
93 // parameters is posted to this service. Type is the type of events to be | |
94 // notified about (or content::NOTIFICATION_ALL to receive events of all | |
95 // types). | |
96 // Source is a NotificationSource object (created using | |
97 // "Source<classname>(pointer)"), if this observer only wants to | |
98 // receive events from that object, or NotificationService::AllSources() | |
99 // to receive events from all sources. | |
100 // | |
101 // A given observer can be registered only once for each combination of | |
102 // type and source. If the same object is registered more than once, | |
103 // it must be removed for each of those combinations of type and source later. | |
104 // | |
105 // The caller retains ownership of the object pointed to by observer. | |
106 void AddObserver(content::NotificationObserver* observer, | |
107 int type, const content::NotificationSource& source); | |
108 | |
109 // NOTE: Rather than using this directly, you should use a | |
110 // NotificationRegistrar. | |
111 // | |
112 // Removes the object pointed to by observer from receiving notifications | |
113 // that match type and source. If no object matching the parameters is | |
114 // currently registered, this method is a no-op. | |
115 void RemoveObserver(content::NotificationObserver* observer, | |
116 int type, const content::NotificationSource& source); | |
117 | |
118 // Keeps track of the observers for each type of notification. | |
119 // Until we get a prohibitively large number of notification types, | |
120 // a simple array is probably the fastest way to dispatch. | |
121 NotificationObserverMap observers_; | |
122 | |
123 #ifndef NDEBUG | |
124 // Used to check to see that AddObserver and RemoveObserver calls are | |
125 // balanced. | |
126 NotificationObserverCount observer_counts_; | |
127 #endif | |
128 | |
129 DISALLOW_COPY_AND_ASSIGN(NotificationService); | |
130 }; | |
131 | |
132 #endif // CONTENT_COMMON_NOTIFICATION_SERVICE_H_ | |
OLD | NEW |