OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 CHROME_COMMON_NOTIFICATION_SERVICE_H__ | 9 #ifndef CHROME_COMMON_NOTIFICATION_SERVICE_H__ |
10 #define CHROME_COMMON_NOTIFICATION_SERVICE_H__ | 10 #define CHROME_COMMON_NOTIFICATION_SERVICE_H__ |
11 | 11 |
12 #include <map> | 12 #include <map> |
13 | 13 |
14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
15 #include "base/thread_local_storage.h" | |
16 #include "base/values.h" | 15 #include "base/values.h" |
17 #include "chrome/common/notification_details.h" | 16 #include "chrome/common/notification_details.h" |
18 #include "chrome/common/notification_source.h" | 17 #include "chrome/common/notification_source.h" |
19 #include "chrome/common/notification_types.h" | 18 #include "chrome/common/notification_types.h" |
20 | 19 |
21 class NotificationObserver; | 20 class NotificationObserver; |
22 | 21 |
23 class NotificationService { | 22 class NotificationService { |
24 public: | 23 public: |
25 // Returns the NotificationService object for the current thread, or NULL if | 24 // Returns the NotificationService object for the current thread, or NULL if |
26 // none. | 25 // none. |
27 static NotificationService* current() { | 26 static NotificationService* current(); |
28 return static_cast<NotificationService *>(tls_index_.Get()); | |
29 } | |
30 | 27 |
31 // Normally instantiated when the thread is created. Not all threads have | 28 // Normally instantiated when the thread is created. Not all threads have |
32 // a NotificationService. Only one instance should be created per thread. | 29 // a NotificationService. Only one instance should be created per thread. |
33 NotificationService(); | 30 NotificationService(); |
34 ~NotificationService(); | 31 ~NotificationService(); |
35 | 32 |
36 // Registers a NotificationObserver to be called whenever a matching | 33 // Registers a NotificationObserver to be called whenever a matching |
37 // notification is posted. Observer is a pointer to an object subclassing | 34 // notification is posted. Observer is a pointer to an object subclassing |
38 // NotificationObserver to be notified when an event matching the other two | 35 // NotificationObserver to be notified when an event matching the other two |
39 // parameters is posted to this service. Type is the type of events to | 36 // parameters is posted to this service. Type is the type of events to |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 // Convenience function to determine whether a source has a | 81 // Convenience function to determine whether a source has a |
85 // NotificationObserverList in the given map; | 82 // NotificationObserverList in the given map; |
86 static bool HasKey(const NotificationSourceMap& map, | 83 static bool HasKey(const NotificationSourceMap& map, |
87 const NotificationSource& source); | 84 const NotificationSource& source); |
88 | 85 |
89 // Keeps track of the observers for each type of notification. | 86 // Keeps track of the observers for each type of notification. |
90 // Until we get a prohibitively large number of notification types, | 87 // Until we get a prohibitively large number of notification types, |
91 // a simple array is probably the fastest way to dispatch. | 88 // a simple array is probably the fastest way to dispatch. |
92 NotificationSourceMap observers_[NOTIFICATION_TYPE_COUNT]; | 89 NotificationSourceMap observers_[NOTIFICATION_TYPE_COUNT]; |
93 | 90 |
94 // The thread local storage index, used for getting the current thread's | |
95 // instance. | |
96 static TLSSlot tls_index_; | |
97 | |
98 #ifndef NDEBUG | 91 #ifndef NDEBUG |
99 // Used to check to see that AddObserver and RemoveObserver calls are | 92 // Used to check to see that AddObserver and RemoveObserver calls are |
100 // balanced. | 93 // balanced. |
101 int observer_counts_[NOTIFICATION_TYPE_COUNT]; | 94 int observer_counts_[NOTIFICATION_TYPE_COUNT]; |
102 #endif | 95 #endif |
103 | 96 |
104 DISALLOW_EVIL_CONSTRUCTORS(NotificationService); | 97 DISALLOW_EVIL_CONSTRUCTORS(NotificationService); |
105 }; | 98 }; |
106 | 99 |
107 // This is the base class for notification observers. When a matching | 100 // This is the base class for notification observers. When a matching |
108 // notification is posted to the notification service, Observe is called. | 101 // notification is posted to the notification service, Observe is called. |
109 class NotificationObserver { | 102 class NotificationObserver { |
110 public: | 103 public: |
111 virtual ~NotificationObserver(); | 104 virtual ~NotificationObserver(); |
112 | 105 |
113 virtual void Observe(NotificationType type, | 106 virtual void Observe(NotificationType type, |
114 const NotificationSource& source, | 107 const NotificationSource& source, |
115 const NotificationDetails& details) = 0; | 108 const NotificationDetails& details) = 0; |
116 }; | 109 }; |
117 | 110 |
118 #endif // CHROME_COMMON_NOTIFICATION_SERVICE_H__ | 111 #endif // CHROME_COMMON_NOTIFICATION_SERVICE_H__ |
119 | 112 |
OLD | NEW |