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