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_PUBLIC_BROWSER_NOTIFICATION_REGISTRAR_H_
6 #define CONTENT_PUBLIC_BROWSER_NOTIFICATION_REGISTRAR_H_
10 #include "base/basictypes.h"
11 #include "base/threading/non_thread_safe.h"
12 #include "content/common/content_export.h"
16 class NotificationObserver
;
17 class NotificationSource
;
19 // Aids in registering for notifications and ensures that all registered
20 // notifications are unregistered when the class is destroyed.
22 // The intended use is that you make a NotificationRegistrar member in your
23 // class and use it to register your notifications instead of going through the
24 // notification service directly. It will automatically unregister them for
26 class CONTENT_EXPORT NotificationRegistrar
:
27 NON_EXPORTED_BASE(public base::NonThreadSafe
) {
29 // This class must not be derived from (we don't have a virtual destructor so
30 // it won't work). Instead, use it as a member in your class.
31 NotificationRegistrar();
32 ~NotificationRegistrar();
34 // Wrappers around NotificationService::[Add|Remove]Observer.
35 void Add(NotificationObserver
* observer
,
37 const NotificationSource
& source
);
38 void Remove(NotificationObserver
* observer
,
40 const NotificationSource
& source
);
42 // Unregisters all notifications.
45 // Returns true if no notifications are registered.
48 // Returns true if there is already a registered notification with the
50 bool IsRegistered(NotificationObserver
* observer
,
52 const NotificationSource
& source
);
57 // We keep registered notifications in a simple vector. This means we'll do
58 // brute-force searches when removing them individually, but individual
59 // removal is uncommon, and there will typically only be a couple of
60 // notifications anyway.
61 typedef std::vector
<Record
> RecordVector
;
63 // Lists all notifications we're currently registered for.
64 RecordVector registered_
;
66 DISALLOW_COPY_AND_ASSIGN(NotificationRegistrar
);
69 } // namespace content
71 #endif // CONTENT_PUBLIC_BROWSER_NOTIFICATION_REGISTRAR_H_