Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / content / browser / notification_service_impl.h
blob8a74c750144836489be6482db44cb5e2a1193f98
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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_NOTIFICATION_SERVICE_IMPL_H_
6 #define CONTENT_BROWSER_NOTIFICATION_SERVICE_IMPL_H_
8 #include <map>
10 #include "base/observer_list.h"
11 #include "content/common/content_export.h"
12 #include "content/public/browser/notification_service.h"
14 namespace content {
16 class NotificationObserver;
17 class NotificationRegistrar;
19 class CONTENT_EXPORT NotificationServiceImpl : public NotificationService {
20 public:
21 static NotificationServiceImpl* current();
23 // Normally instantiated when the thread is created. Not all threads have
24 // a NotificationService. Only one instance should be created per thread.
25 NotificationServiceImpl();
26 ~NotificationServiceImpl() override;
28 // NotificationService:
29 void Notify(int type,
30 const NotificationSource& source,
31 const NotificationDetails& details) override;
33 private:
34 friend class NotificationRegistrar;
36 typedef base::ObserverList<NotificationObserver> NotificationObserverList;
37 typedef std::map<uintptr_t, NotificationObserverList*> NotificationSourceMap;
38 typedef std::map<int, NotificationSourceMap> NotificationObserverMap;
39 typedef std::map<int, int> NotificationObserverCount;
41 // Convenience function to determine whether a source has a
42 // NotificationObserverList in the given map;
43 static bool HasKey(const NotificationSourceMap& map,
44 const NotificationSource& source);
46 // NOTE: Rather than using this directly, you should use a
47 // NotificationRegistrar.
49 // Registers a NotificationObserver to be called whenever a matching
50 // notification is posted. Observer is a pointer to an object subclassing
51 // NotificationObserver to be notified when an event matching the other two
52 // parameters is posted to this service. Type is the type of events to be
53 // notified about (or NOTIFICATION_ALL to receive events of all
54 // types).
55 // Source is a NotificationSource object (created using
56 // "Source<classname>(pointer)"), if this observer only wants to
57 // receive events from that object, or NotificationService::AllSources()
58 // to receive events from all sources.
60 // A given observer can be registered only once for each combination of
61 // type and source. If the same object is registered more than once,
62 // it must be removed for each of those combinations of type and source later.
64 // The caller retains ownership of the object pointed to by observer.
65 void AddObserver(NotificationObserver* observer,
66 int type,
67 const NotificationSource& source);
69 // NOTE: Rather than using this directly, you should use a
70 // NotificationRegistrar.
72 // Removes the object pointed to by observer from receiving notifications
73 // that match type and source. If no object matching the parameters is
74 // currently registered, this method is a no-op.
75 void RemoveObserver(NotificationObserver* observer,
76 int type,
77 const NotificationSource& source);
79 // Keeps track of the observers for each type of notification.
80 // Until we get a prohibitively large number of notification types,
81 // a simple array is probably the fastest way to dispatch.
82 NotificationObserverMap observers_;
84 #ifndef NDEBUG
85 // Used to check to see that AddObserver and RemoveObserver calls are
86 // balanced.
87 NotificationObserverCount observer_counts_;
88 #endif
90 DISALLOW_COPY_AND_ASSIGN(NotificationServiceImpl);
93 } // namespace content
95 #endif // CONTENT_BROWSER_NOTIFICATION_SERVICE_IMPL_H_