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