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 // This file describes a central switchboard for notifications that might |
6 // happen in various parts of the application, and allows users to register | 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. | 7 // observers for various classes of events that they're interested in. |
8 | 8 |
9 #ifndef CONTENT_COMMON_NOTIFICATION_SERVICE_H_ | 9 #ifndef CONTENT_COMMON_NOTIFICATION_SERVICE_H_ |
10 #define CONTENT_COMMON_NOTIFICATION_SERVICE_H_ | 10 #define CONTENT_COMMON_NOTIFICATION_SERVICE_H_ |
11 #pragma once | 11 #pragma once |
12 | 12 |
13 #include <map> | 13 #include <map> |
14 | 14 |
15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 #include "content/common/content_notification_types.h" |
16 #include "content/common/notification_details.h" | 17 #include "content/common/notification_details.h" |
17 #include "content/common/notification_source.h" | 18 #include "content/common/notification_source.h" |
18 #include "content/common/notification_type.h" | |
19 | 19 |
20 class NotificationObserver; | 20 class NotificationObserver; |
21 | 21 |
22 class NotificationService { | 22 class NotificationService { |
23 public: | 23 public: |
24 // Returns the NotificationService object for the current thread, or NULL if | 24 // Returns the NotificationService object for the current thread, or NULL if |
25 // none. | 25 // none. |
26 static NotificationService* current(); | 26 static NotificationService* current(); |
27 | 27 |
28 // Normally instantiated when the thread is created. Not all threads have | 28 // Normally instantiated when the thread is created. Not all threads have |
29 // a NotificationService. Only one instance should be created per thread. | 29 // a NotificationService. Only one instance should be created per thread. |
30 NotificationService(); | 30 NotificationService(); |
31 ~NotificationService(); | 31 ~NotificationService(); |
32 | 32 |
33 // Synchronously posts a notification to all interested observers. | 33 // Synchronously posts a notification to all interested observers. |
34 // Source is a reference to a NotificationSource object representing | 34 // Source is a reference to a NotificationSource object representing |
35 // the object originating the notification (can be | 35 // the object originating the notification (can be |
36 // NotificationService::AllSources(), in which case | 36 // NotificationService::AllSources(), in which case |
37 // only observers interested in all sources will be notified). | 37 // only observers interested in all sources will be notified). |
38 // Details is a reference to an object containing additional data about | 38 // Details is a reference to an object containing additional data about |
39 // the notification. If no additional data is needed, NoDetails() is used. | 39 // the notification. If no additional data is needed, NoDetails() is used. |
40 // There is no particular order in which the observers will be notified. | 40 // There is no particular order in which the observers will be notified. |
41 void Notify(NotificationType type, | 41 void Notify(int type, |
42 const NotificationSource& source, | 42 const NotificationSource& source, |
43 const NotificationDetails& details); | 43 const NotificationDetails& details); |
44 | 44 |
45 // Returns a NotificationSource that represents all notification sources | 45 // Returns a NotificationSource that represents all notification sources |
46 // (for the purpose of registering an observer for events from all sources). | 46 // (for the purpose of registering an observer for events from all sources). |
47 static Source<void> AllSources() { return Source<void>(NULL); } | 47 static Source<void> AllSources() { return Source<void>(NULL); } |
48 | 48 |
49 // Returns a NotificationDetails object that represents a lack of details | 49 // Returns a NotificationDetails object that represents a lack of details |
50 // associated with a notification. (This is effectively a null pointer.) | 50 // associated with a notification. (This is effectively a null pointer.) |
51 static Details<void> NoDetails() { return Details<void>(NULL); } | 51 static Details<void> NoDetails() { return Details<void>(NULL); } |
52 | 52 |
53 private: | 53 private: |
54 friend class NotificationRegistrar; | 54 friend class NotificationRegistrar; |
55 | 55 |
56 typedef ObserverList<NotificationObserver> NotificationObserverList; | 56 typedef ObserverList<NotificationObserver> NotificationObserverList; |
57 typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap; | 57 typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap; |
| 58 typedef std::map<int, NotificationSourceMap> NotificationObserverMap; |
| 59 typedef std::map<int, int> NotificationObserverCount; |
58 | 60 |
59 // Convenience function to determine whether a source has a | 61 // Convenience function to determine whether a source has a |
60 // NotificationObserverList in the given map; | 62 // NotificationObserverList in the given map; |
61 static bool HasKey(const NotificationSourceMap& map, | 63 static bool HasKey(const NotificationSourceMap& map, |
62 const NotificationSource& source); | 64 const NotificationSource& source); |
63 | 65 |
64 // NOTE: Rather than using this directly, you should use a | 66 // NOTE: Rather than using this directly, you should use a |
65 // NotificationRegistrar. | 67 // NotificationRegistrar. |
66 // | 68 // |
67 // Registers a NotificationObserver to be called whenever a matching | 69 // Registers a NotificationObserver to be called whenever a matching |
68 // notification is posted. Observer is a pointer to an object subclassing | 70 // notification is posted. Observer is a pointer to an object subclassing |
69 // NotificationObserver to be notified when an event matching the other two | 71 // NotificationObserver to be notified when an event matching the other two |
70 // parameters is posted to this service. Type is the type of events to be | 72 // parameters is posted to this service. Type is the type of events to be |
71 // notified about (or NotificationType::ALL to receive events of all types). | 73 // notified about (or content::NOTIFICATION_ALL to receive events of all |
| 74 // types). |
72 // Source is a NotificationSource object (created using | 75 // Source is a NotificationSource object (created using |
73 // "Source<classname>(pointer)"), if this observer only wants to | 76 // "Source<classname>(pointer)"), if this observer only wants to |
74 // receive events from that object, or NotificationService::AllSources() | 77 // receive events from that object, or NotificationService::AllSources() |
75 // to receive events from all sources. | 78 // to receive events from all sources. |
76 // | 79 // |
77 // A given observer can be registered only once for each combination of | 80 // A given observer can be registered only once for each combination of |
78 // type and source. If the same object is registered more than once, | 81 // type and source. If the same object is registered more than once, |
79 // it must be removed for each of those combinations of type and source later. | 82 // it must be removed for each of those combinations of type and source later. |
80 // | 83 // |
81 // The caller retains ownership of the object pointed to by observer. | 84 // The caller retains ownership of the object pointed to by observer. |
82 void AddObserver(NotificationObserver* observer, | 85 void AddObserver(NotificationObserver* observer, |
83 NotificationType type, const NotificationSource& source); | 86 int type, const NotificationSource& source); |
84 | 87 |
85 // NOTE: Rather than using this directly, you should use a | 88 // NOTE: Rather than using this directly, you should use a |
86 // NotificationRegistrar. | 89 // NotificationRegistrar. |
87 // | 90 // |
88 // Removes the object pointed to by observer from receiving notifications | 91 // Removes the object pointed to by observer from receiving notifications |
89 // that match type and source. If no object matching the parameters is | 92 // that match type and source. If no object matching the parameters is |
90 // currently registered, this method is a no-op. | 93 // currently registered, this method is a no-op. |
91 void RemoveObserver(NotificationObserver* observer, | 94 void RemoveObserver(NotificationObserver* observer, |
92 NotificationType type, const NotificationSource& source); | 95 int type, const NotificationSource& source); |
93 | 96 |
94 // Keeps track of the observers for each type of notification. | 97 // Keeps track of the observers for each type of notification. |
95 // Until we get a prohibitively large number of notification types, | 98 // Until we get a prohibitively large number of notification types, |
96 // a simple array is probably the fastest way to dispatch. | 99 // a simple array is probably the fastest way to dispatch. |
97 NotificationSourceMap observers_[NotificationType::NOTIFICATION_TYPE_COUNT]; | 100 NotificationObserverMap observers_; |
98 | 101 |
99 #ifndef NDEBUG | 102 #ifndef NDEBUG |
100 // Used to check to see that AddObserver and RemoveObserver calls are | 103 // Used to check to see that AddObserver and RemoveObserver calls are |
101 // balanced. | 104 // balanced. |
102 int observer_counts_[NotificationType::NOTIFICATION_TYPE_COUNT]; | 105 NotificationObserverCount observer_counts_; |
103 #endif | 106 #endif |
104 | 107 |
105 DISALLOW_COPY_AND_ASSIGN(NotificationService); | 108 DISALLOW_COPY_AND_ASSIGN(NotificationService); |
106 }; | 109 }; |
107 | 110 |
108 #endif // CONTENT_COMMON_NOTIFICATION_SERVICE_H_ | 111 #endif // CONTENT_COMMON_NOTIFICATION_SERVICE_H_ |
OLD | NEW |