Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: content/common/notification_service.h

Issue 7327007: Moving notification types which are chrome specific to a new header file chrome_notification_type... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include <vector>
darin (slow to review) 2011/07/09 04:07:44 nit: this include is not needed?
14 15
15 #include "base/observer_list.h" 16 #include "base/observer_list.h"
17 #include "content/common/content_notification_types.h"
16 #include "content/common/notification_details.h" 18 #include "content/common/notification_details.h"
17 #include "content/common/notification_source.h" 19 #include "content/common/notification_source.h"
18 #include "content/common/notification_type.h"
19 20
20 class NotificationObserver; 21 class NotificationObserver;
21 22
22 class NotificationService { 23 class NotificationService {
23 public: 24 public:
24 // Returns the NotificationService object for the current thread, or NULL if 25 // Returns the NotificationService object for the current thread, or NULL if
25 // none. 26 // none.
26 static NotificationService* current(); 27 static NotificationService* current();
27 28
28 // Normally instantiated when the thread is created. Not all threads have 29 // Normally instantiated when the thread is created. Not all threads have
29 // a NotificationService. Only one instance should be created per thread. 30 // a NotificationService. Only one instance should be created per thread.
30 NotificationService(); 31 NotificationService();
31 ~NotificationService(); 32 ~NotificationService();
32 33
33 // Synchronously posts a notification to all interested observers. 34 // Synchronously posts a notification to all interested observers.
34 // Source is a reference to a NotificationSource object representing 35 // Source is a reference to a NotificationSource object representing
35 // the object originating the notification (can be 36 // the object originating the notification (can be
36 // NotificationService::AllSources(), in which case 37 // NotificationService::AllSources(), in which case
37 // only observers interested in all sources will be notified). 38 // only observers interested in all sources will be notified).
38 // Details is a reference to an object containing additional data about 39 // Details is a reference to an object containing additional data about
39 // the notification. If no additional data is needed, NoDetails() is used. 40 // the notification. If no additional data is needed, NoDetails() is used.
40 // There is no particular order in which the observers will be notified. 41 // There is no particular order in which the observers will be notified.
41 void Notify(NotificationType type, 42 void Notify(int type,
42 const NotificationSource& source, 43 const NotificationSource& source,
43 const NotificationDetails& details); 44 const NotificationDetails& details);
44 45
45 // Returns a NotificationSource that represents all notification sources 46 // Returns a NotificationSource that represents all notification sources
46 // (for the purpose of registering an observer for events from all sources). 47 // (for the purpose of registering an observer for events from all sources).
47 static Source<void> AllSources() { return Source<void>(NULL); } 48 static Source<void> AllSources() { return Source<void>(NULL); }
48 49
49 // Returns a NotificationDetails object that represents a lack of details 50 // Returns a NotificationDetails object that represents a lack of details
50 // associated with a notification. (This is effectively a null pointer.) 51 // associated with a notification. (This is effectively a null pointer.)
51 static Details<void> NoDetails() { return Details<void>(NULL); } 52 static Details<void> NoDetails() { return Details<void>(NULL); }
52 53
53 private: 54 private:
54 friend class NotificationRegistrar; 55 friend class NotificationRegistrar;
55 56
56 typedef ObserverList<NotificationObserver> NotificationObserverList; 57 typedef ObserverList<NotificationObserver> NotificationObserverList;
57 typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap; 58 typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap;
59 typedef std::map<int, NotificationSourceMap> NotificationObserverMap;
60 typedef std::map<int, int> NotificationObserverCount;
58 61
59 // Convenience function to determine whether a source has a 62 // Convenience function to determine whether a source has a
60 // NotificationObserverList in the given map; 63 // NotificationObserverList in the given map;
61 static bool HasKey(const NotificationSourceMap& map, 64 static bool HasKey(const NotificationSourceMap& map,
62 const NotificationSource& source); 65 const NotificationSource& source);
63 66
64 // NOTE: Rather than using this directly, you should use a 67 // NOTE: Rather than using this directly, you should use a
65 // NotificationRegistrar. 68 // NotificationRegistrar.
66 // 69 //
67 // Registers a NotificationObserver to be called whenever a matching 70 // Registers a NotificationObserver to be called whenever a matching
68 // notification is posted. Observer is a pointer to an object subclassing 71 // notification is posted. Observer is a pointer to an object subclassing
69 // NotificationObserver to be notified when an event matching the other two 72 // 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 73 // 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). 74 // notified about (or content::NOTIFICATION_ALL to receive events of all
75 // types).
72 // Source is a NotificationSource object (created using 76 // Source is a NotificationSource object (created using
73 // "Source<classname>(pointer)"), if this observer only wants to 77 // "Source<classname>(pointer)"), if this observer only wants to
74 // receive events from that object, or NotificationService::AllSources() 78 // receive events from that object, or NotificationService::AllSources()
75 // to receive events from all sources. 79 // to receive events from all sources.
76 // 80 //
77 // A given observer can be registered only once for each combination of 81 // 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, 82 // 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. 83 // it must be removed for each of those combinations of type and source later.
80 // 84 //
81 // The caller retains ownership of the object pointed to by observer. 85 // The caller retains ownership of the object pointed to by observer.
82 void AddObserver(NotificationObserver* observer, 86 void AddObserver(NotificationObserver* observer,
83 NotificationType type, const NotificationSource& source); 87 int type, const NotificationSource& source);
84 88
85 // NOTE: Rather than using this directly, you should use a 89 // NOTE: Rather than using this directly, you should use a
86 // NotificationRegistrar. 90 // NotificationRegistrar.
87 // 91 //
88 // Removes the object pointed to by observer from receiving notifications 92 // Removes the object pointed to by observer from receiving notifications
89 // that match type and source. If no object matching the parameters is 93 // that match type and source. If no object matching the parameters is
90 // currently registered, this method is a no-op. 94 // currently registered, this method is a no-op.
91 void RemoveObserver(NotificationObserver* observer, 95 void RemoveObserver(NotificationObserver* observer,
92 NotificationType type, const NotificationSource& source); 96 int type, const NotificationSource& source);
93 97
94 // Keeps track of the observers for each type of notification. 98 // Keeps track of the observers for each type of notification.
95 // Until we get a prohibitively large number of notification types, 99 // Until we get a prohibitively large number of notification types,
96 // a simple array is probably the fastest way to dispatch. 100 // a simple array is probably the fastest way to dispatch.
97 NotificationSourceMap observers_[NotificationType::NOTIFICATION_TYPE_COUNT]; 101 NotificationObserverMap observers_;
98 102
99 #ifndef NDEBUG 103 #ifndef NDEBUG
100 // Used to check to see that AddObserver and RemoveObserver calls are 104 // Used to check to see that AddObserver and RemoveObserver calls are
101 // balanced. 105 // balanced.
102 int observer_counts_[NotificationType::NOTIFICATION_TYPE_COUNT]; 106 NotificationObserverCount observer_counts_;
103 #endif 107 #endif
104 108
105 DISALLOW_COPY_AND_ASSIGN(NotificationService); 109 DISALLOW_COPY_AND_ASSIGN(NotificationService);
106 }; 110 };
107 111
108 #endif // CONTENT_COMMON_NOTIFICATION_SERVICE_H_ 112 #endif // CONTENT_COMMON_NOTIFICATION_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698