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 } |
17 | 18 |
18 class CONTENT_EXPORT NotificationServiceImpl : public NotificationService { | 19 class CONTENT_EXPORT NotificationServiceImpl |
| 20 : public content::NotificationService { |
19 public: | 21 public: |
20 static NotificationServiceImpl* current(); | 22 static NotificationServiceImpl* current(); |
21 | 23 |
22 // Normally instantiated when the thread is created. Not all threads have | 24 // Normally instantiated when the thread is created. Not all threads have |
23 // a NotificationService. Only one instance should be created per thread. | 25 // a NotificationService. Only one instance should be created per thread. |
24 NotificationServiceImpl(); | 26 NotificationServiceImpl(); |
25 virtual ~NotificationServiceImpl(); | 27 virtual ~NotificationServiceImpl(); |
26 | 28 |
27 // NotificationService: | 29 // content::NotificationService |
28 virtual void Notify(int type, | 30 virtual void Notify(int type, |
29 const NotificationSource& source, | 31 const content::NotificationSource& source, |
30 const NotificationDetails& details) OVERRIDE; | 32 const content::NotificationDetails& details) OVERRIDE; |
31 | 33 |
32 private: | 34 private: |
33 friend class NotificationRegistrar; | 35 friend class content::NotificationRegistrar; |
34 | 36 |
35 typedef ObserverList<NotificationObserver> NotificationObserverList; | 37 typedef ObserverList<content::NotificationObserver> NotificationObserverList; |
36 typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap; | 38 typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap; |
37 typedef std::map<int, NotificationSourceMap> NotificationObserverMap; | 39 typedef std::map<int, NotificationSourceMap> NotificationObserverMap; |
38 typedef std::map<int, int> NotificationObserverCount; | 40 typedef std::map<int, int> NotificationObserverCount; |
39 | 41 |
40 // Convenience function to determine whether a source has a | 42 // Convenience function to determine whether a source has a |
41 // NotificationObserverList in the given map; | 43 // NotificationObserverList in the given map; |
42 static bool HasKey(const NotificationSourceMap& map, | 44 static bool HasKey(const NotificationSourceMap& map, |
43 const NotificationSource& source); | 45 const content::NotificationSource& source); |
44 | 46 |
45 // NOTE: Rather than using this directly, you should use a | 47 // NOTE: Rather than using this directly, you should use a |
46 // NotificationRegistrar. | 48 // NotificationRegistrar. |
47 // | 49 // |
48 // Registers a NotificationObserver to be called whenever a matching | 50 // Registers a NotificationObserver to be called whenever a matching |
49 // notification is posted. Observer is a pointer to an object subclassing | 51 // notification is posted. Observer is a pointer to an object subclassing |
50 // NotificationObserver to be notified when an event matching the other two | 52 // NotificationObserver to be notified when an event matching the other two |
51 // parameters is posted to this service. Type is the type of events to be | 53 // parameters is posted to this service. Type is the type of events to be |
52 // notified about (or NOTIFICATION_ALL to receive events of all | 54 // notified about (or content::NOTIFICATION_ALL to receive events of all |
53 // types). | 55 // types). |
54 // Source is a NotificationSource object (created using | 56 // Source is a NotificationSource object (created using |
55 // "Source<classname>(pointer)"), if this observer only wants to | 57 // "Source<classname>(pointer)"), if this observer only wants to |
56 // receive events from that object, or NotificationService::AllSources() | 58 // receive events from that object, or NotificationService::AllSources() |
57 // to receive events from all sources. | 59 // to receive events from all sources. |
58 // | 60 // |
59 // A given observer can be registered only once for each combination of | 61 // A given observer can be registered only once for each combination of |
60 // type and source. If the same object is registered more than once, | 62 // type and source. If the same object is registered more than once, |
61 // it must be removed for each of those combinations of type and source later. | 63 // it must be removed for each of those combinations of type and source later. |
62 // | 64 // |
63 // The caller retains ownership of the object pointed to by observer. | 65 // The caller retains ownership of the object pointed to by observer. |
64 void AddObserver(NotificationObserver* observer, | 66 void AddObserver(content::NotificationObserver* observer, |
65 int type, | 67 int type, const content::NotificationSource& source); |
66 const NotificationSource& source); | |
67 | 68 |
68 // NOTE: Rather than using this directly, you should use a | 69 // NOTE: Rather than using this directly, you should use a |
69 // NotificationRegistrar. | 70 // NotificationRegistrar. |
70 // | 71 // |
71 // Removes the object pointed to by observer from receiving notifications | 72 // Removes the object pointed to by observer from receiving notifications |
72 // that match type and source. If no object matching the parameters is | 73 // that match type and source. If no object matching the parameters is |
73 // currently registered, this method is a no-op. | 74 // currently registered, this method is a no-op. |
74 void RemoveObserver(NotificationObserver* observer, | 75 void RemoveObserver(content::NotificationObserver* observer, |
75 int type, | 76 int type, const content::NotificationSource& source); |
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 | |
94 #endif // CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_ | 92 #endif // CONTENT_PUBLIC_BROWSER_NOTIFICATION_SERVICE_IMPL_H_ |
OLD | NEW |