summaryrefslogtreecommitdiffstats
path: root/chrome/common/notification_registrar.h
diff options
context:
space:
mode:
authorbrettw@google.com <brettw@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-16 20:16:08 +0000
committerbrettw@google.com <brettw@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-16 20:16:08 +0000
commitd778c4701b5e5ffec54eee51d2427836f4f5c0b1 (patch)
tree850ae017300e5a83d4a4775451051ffc2c9187bd /chrome/common/notification_registrar.h
parent90e2b34d20d3cf5662ea8776580b9e437b4698a8 (diff)
downloadchromium_src-d778c4701b5e5ffec54eee51d2427836f4f5c0b1.zip
chromium_src-d778c4701b5e5ffec54eee51d2427836f4f5c0b1.tar.gz
chromium_src-d778c4701b5e5ffec54eee51d2427836f4f5c0b1.tar.bz2
Remove an explicit call from the NavigationController to the alternate URL
fetcher since there is already a notification that does this. I created a class that will automatically unregister for notifications when it goes out of scope and used it here. I think it will be useful for most consumers of notifications. Review URL: http://codereview.chromium.org/2895 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2276 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/notification_registrar.h')
-rw-r--r--chrome/common/notification_registrar.h63
1 files changed, 63 insertions, 0 deletions
diff --git a/chrome/common/notification_registrar.h b/chrome/common/notification_registrar.h
new file mode 100644
index 0000000..e253eb4
--- /dev/null
+++ b/chrome/common/notification_registrar.h
@@ -0,0 +1,63 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_COMMON_NOTIFICATION_REGISTRAR_H_
+#define CHROME_COMMON_NOTIFICATION_REGISTRAR_H_
+
+#include <vector>
+
+#include "base/basictypes.h"
+#include "chrome/common/notification_service.h"
+
+// Aids in registering for notifications and ensures that all registered
+// notifications are unregistered when the class is destroyed.
+//
+// The intended use is that you make a NotificationRegistrar member in your
+// class and use it to register your notifications instead of going through the
+// notification service directly. It will automatically unregister them for
+// you.
+class NotificationRegistrar {
+ public:
+ // This class must not be derived from (we don't have a virtual destructor so
+ // it won't work). Instead, use it as a member in your class.
+ NotificationRegistrar();
+ ~NotificationRegistrar();
+
+ // Wrappers around NotificationService::[Add|Remove]Observer.
+ void Add(NotificationObserver* observer,
+ NotificationType type,
+ NotificationSource source);
+ void Remove(NotificationObserver* observer,
+ NotificationType type,
+ NotificationSource source);
+
+ // Unregisters all notifications.
+ void RemoveAll();
+
+ private:
+ struct Record {
+ bool operator==(const Record& other) const {
+ return observer == other.observer &&
+ type == other.type &&
+ source == other.source;
+ }
+
+ NotificationObserver* observer;
+ NotificationType type;
+ NotificationSource source;
+ };
+
+ // We keep registered notifications in a simple vector. This means we'll do
+ // brute-force searches when removing them individually, but individual
+ // removal is uncommon, and there will typically only be a couple of
+ // notifications anyway.
+ typedef std::vector<Record> RecordVector;
+
+ // Lists all notifications we're currently registered for.
+ RecordVector registered_;
+
+ DISALLOW_COPY_AND_ASSIGN(NotificationRegistrar);
+};
+
+#endif // CHROME_COMMON_NOTIFICATION_REGISTRAR_H_