OLD | NEW |
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 #ifndef CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_ | 5 #ifndef CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_ |
6 #define CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_ | 6 #define CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
11 #include "content/common/content_export.h" | 11 #include "content/common/content_export.h" |
12 #include "content/public/browser/notification_service.h" | 12 #include "content/public/browser/notification_service.h" |
13 | 13 |
14 namespace content { | 14 namespace content { |
15 class NotificationObserver; | 15 class NotificationObserver; |
16 class NotificationRegistrar; | 16 class NotificationRegistrar; |
17 } | |
18 | 17 |
19 class CONTENT_EXPORT NotificationServiceImpl | 18 class CONTENT_EXPORT NotificationServiceImpl : public NotificationService { |
20 : public content::NotificationService { | |
21 public: | 19 public: |
22 static NotificationServiceImpl* current(); | 20 static NotificationServiceImpl* current(); |
23 | 21 |
24 // Normally instantiated when the thread is created. Not all threads have | 22 // Normally instantiated when the thread is created. Not all threads have |
25 // a NotificationService. Only one instance should be created per thread. | 23 // a NotificationService. Only one instance should be created per thread. |
26 NotificationServiceImpl(); | 24 NotificationServiceImpl(); |
27 virtual ~NotificationServiceImpl(); | 25 virtual ~NotificationServiceImpl(); |
28 | 26 |
29 // content::NotificationService | 27 // NotificationService: |
30 virtual void Notify(int type, | 28 virtual void Notify(int type, |
31 const content::NotificationSource& source, | 29 const NotificationSource& source, |
32 const content::NotificationDetails& details) OVERRIDE; | 30 const NotificationDetails& details) OVERRIDE; |
33 | 31 |
34 private: | 32 private: |
35 friend class content::NotificationRegistrar; | 33 friend class NotificationRegistrar; |
36 | 34 |
37 typedef ObserverList<content::NotificationObserver> NotificationObserverList; | 35 typedef ObserverList<NotificationObserver> NotificationObserverList; |
38 typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap; | 36 typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap; |
39 typedef std::map<int, NotificationSourceMap> NotificationObserverMap; | 37 typedef std::map<int, NotificationSourceMap> NotificationObserverMap; |
40 typedef std::map<int, int> NotificationObserverCount; | 38 typedef std::map<int, int> NotificationObserverCount; |
41 | 39 |
42 // Convenience function to determine whether a source has a | 40 // Convenience function to determine whether a source has a |
43 // NotificationObserverList in the given map; | 41 // NotificationObserverList in the given map; |
44 static bool HasKey(const NotificationSourceMap& map, | 42 static bool HasKey(const NotificationSourceMap& map, |
45 const content::NotificationSource& source); | 43 const NotificationSource& source); |
46 | 44 |
47 // NOTE: Rather than using this directly, you should use a | 45 // NOTE: Rather than using this directly, you should use a |
48 // NotificationRegistrar. | 46 // NotificationRegistrar. |
49 // | 47 // |
50 // Registers a NotificationObserver to be called whenever a matching | 48 // Registers a NotificationObserver to be called whenever a matching |
51 // notification is posted. Observer is a pointer to an object subclassing | 49 // notification is posted. Observer is a pointer to an object subclassing |
52 // NotificationObserver to be notified when an event matching the other two | 50 // NotificationObserver to be notified when an event matching the other two |
53 // parameters is posted to this service. Type is the type of events to be | 51 // parameters is posted to this service. Type is the type of events to be |
54 // notified about (or content::NOTIFICATION_ALL to receive events of all | 52 // notified about (or NOTIFICATION_ALL to receive events of all |
55 // types). | 53 // types). |
56 // Source is a NotificationSource object (created using | 54 // Source is a NotificationSource object (created using |
57 // "Source<classname>(pointer)"), if this observer only wants to | 55 // "Source<classname>(pointer)"), if this observer only wants to |
58 // receive events from that object, or NotificationService::AllSources() | 56 // receive events from that object, or NotificationService::AllSources() |
59 // to receive events from all sources. | 57 // to receive events from all sources. |
60 // | 58 // |
61 // A given observer can be registered only once for each combination of | 59 // A given observer can be registered only once for each combination of |
62 // type and source. If the same object is registered more than once, | 60 // type and source. If the same object is registered more than once, |
63 // it must be removed for each of those combinations of type and source later. | 61 // it must be removed for each of those combinations of type and source later. |
64 // | 62 // |
65 // The caller retains ownership of the object pointed to by observer. | 63 // The caller retains ownership of the object pointed to by observer. |
66 void AddObserver(content::NotificationObserver* observer, | 64 void AddObserver(NotificationObserver* observer, |
67 int type, const content::NotificationSource& source); | 65 int type, |
| 66 const NotificationSource& source); |
68 | 67 |
69 // NOTE: Rather than using this directly, you should use a | 68 // NOTE: Rather than using this directly, you should use a |
70 // NotificationRegistrar. | 69 // NotificationRegistrar. |
71 // | 70 // |
72 // Removes the object pointed to by observer from receiving notifications | 71 // Removes the object pointed to by observer from receiving notifications |
73 // that match type and source. If no object matching the parameters is | 72 // that match type and source. If no object matching the parameters is |
74 // currently registered, this method is a no-op. | 73 // currently registered, this method is a no-op. |
75 void RemoveObserver(content::NotificationObserver* observer, | 74 void RemoveObserver(NotificationObserver* observer, |
76 int type, const content::NotificationSource& source); | 75 int type, |
| 76 const NotificationSource& source); |
77 | 77 |
78 // Keeps track of the observers for each type of notification. | 78 // Keeps track of the observers for each type of notification. |
79 // Until we get a prohibitively large number of notification types, | 79 // Until we get a prohibitively large number of notification types, |
80 // a simple array is probably the fastest way to dispatch. | 80 // a simple array is probably the fastest way to dispatch. |
81 NotificationObserverMap observers_; | 81 NotificationObserverMap observers_; |
82 | 82 |
83 #ifndef NDEBUG | 83 #ifndef NDEBUG |
84 // Used to check to see that AddObserver and RemoveObserver calls are | 84 // Used to check to see that AddObserver and RemoveObserver calls are |
85 // balanced. | 85 // balanced. |
86 NotificationObserverCount observer_counts_; | 86 NotificationObserverCount observer_counts_; |
87 #endif | 87 #endif |
88 | 88 |
89 DISALLOW_COPY_AND_ASSIGN(NotificationServiceImpl); | 89 DISALLOW_COPY_AND_ASSIGN(NotificationServiceImpl); |
90 }; | 90 }; |
91 | 91 |
| 92 } // namespace content |
| 93 |
92 #endif // CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_ | 94 #endif // CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_ |
OLD | NEW |