Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
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
3 // found in the LICENSE file.
5 // This file describes a central switchboard for notifications that might
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.
9 #ifndef CHROME_COMMON_NOTIFICATION_SERVICE_H_
10 #define CHROME_COMMON_NOTIFICATION_SERVICE_H_
12 #include <map>
14 #include "base/observer_list.h"
15 #include "chrome/common/notification_details.h"
16 #include "chrome/common/notification_observer.h"
17 #include "chrome/common/notification_source.h"
18 #include "chrome/common/notification_type.h"
20 class NotificationObserver;
22 class NotificationService {
23 public:
24 // Returns the NotificationService object for the current thread, or NULL if
25 // none.
26 static NotificationService* current();
28 // Normally instantiated when the thread is created. Not all threads have
29 // a NotificationService. Only one instance should be created per thread.
30 NotificationService();
31 ~NotificationService();
33 // Registers a NotificationObserver to be called whenever a matching
34 // notification is posted. Observer is a pointer to an object subclassing
35 // NotificationObserver to be notified when an event matching the other two
36 // parameters is posted to this service. Type is the type of events to
37 // be notified about (or NOTIFY_ALL to receive events of all types).
38 // Source is a NotificationSource object (created using
39 // "Source<classname>(pointer)"), if this observer only wants to
40 // receive events from that object, or NotificationService::AllSources()
41 // to receive events from all sources.
42 //
43 // A given observer can be registered only once for each combination of
44 // type and source. If the same object is registered more than once,
45 // it must be removed for each of those combinations of type and source later.
46 //
47 // The caller retains ownership of the object pointed to by observer.
48 void AddObserver(NotificationObserver* observer,
49 NotificationType type, const NotificationSource& source);
51 // Removes the object pointed to by observer from receiving notifications
52 // that match type and source. If no object matching the parameters is
53 // currently registered, this method is a no-op.
54 void RemoveObserver(NotificationObserver* observer,
55 NotificationType type, const NotificationSource& source);
57 // Synchronously posts a notification to all interested observers.
58 // Source is a reference to a NotificationSource object representing
59 // the object originating the notification (can be
60 // NotificationService::AllSources(), in which case
61 // only observers interested in all sources will be notified).
62 // Details is a reference to an object containing additional data about
63 // the notification. If no additional data is needed, NoDetails() is used.
64 // There is no particular order in which the observers will be notified.
65 void Notify(NotificationType type,
66 const NotificationSource& source,
67 const NotificationDetails& details);
69 // Returns a NotificationSource that represents all notification sources
70 // (for the purpose of registering an observer for events from all sources).
71 static Source<void> AllSources() { return Source<void>(NULL); }
73 // Returns a NotificationDetails object that represents a lack of details
74 // associated with a notification. (This is effectively a null pointer.)
75 static Details<void> NoDetails() { return Details<void>(NULL); }
77 private:
78 typedef base::ObserverList<NotificationObserver> NotificationObserverList;
79 typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap;
81 // Convenience function to determine whether a source has a
82 // NotificationObserverList in the given map;
83 static bool HasKey(const NotificationSourceMap& map,
84 const NotificationSource& source);
86 // Keeps track of the observers for each type of notification.
87 // Until we get a prohibitively large number of notification types,
88 // a simple array is probably the fastest way to dispatch.
89 NotificationSourceMap observers_[NotificationType::NOTIFICATION_TYPE_COUNT];
91 #ifndef NDEBUG
92 // Used to check to see that AddObserver and RemoveObserver calls are
93 // balanced.
94 int observer_counts_[NotificationType::NOTIFICATION_TYPE_COUNT];
95 #endif
97 DISALLOW_COPY_AND_ASSIGN(NotificationService);
98 };
100 #endif // CHROME_COMMON_NOTIFICATION_SERVICE_H_