diff options
author | brettw@google.com <brettw@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-16 20:16:08 +0000 |
---|---|---|
committer | brettw@google.com <brettw@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-16 20:16:08 +0000 |
commit | d778c4701b5e5ffec54eee51d2427836f4f5c0b1 (patch) | |
tree | 850ae017300e5a83d4a4775451051ffc2c9187bd /chrome/common/notification_registrar.cc | |
parent | 90e2b34d20d3cf5662ea8776580b9e437b4698a8 (diff) | |
download | chromium_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.cc')
-rw-r--r-- | chrome/common/notification_registrar.cc | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/chrome/common/notification_registrar.cc b/chrome/common/notification_registrar.cc new file mode 100644 index 0000000..fdedde4 --- /dev/null +++ b/chrome/common/notification_registrar.cc @@ -0,0 +1,54 @@ +// 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. + +#include <algorithm> + +#include "chrome/common/notification_registrar.h" + +NotificationRegistrar::NotificationRegistrar() { +} + +NotificationRegistrar::~NotificationRegistrar() { + RemoveAll(); +} + +void NotificationRegistrar::Add(NotificationObserver* observer, + NotificationType type, + NotificationSource source) { + Record record = { observer, type, source }; + DCHECK(std::find(registered_.begin(), registered_.end(), record) == + registered_.end()) << "Duplicate registration."; + registered_.push_back(record); + + NotificationService::current()->AddObserver(observer, type, source); +} + +void NotificationRegistrar::Remove(NotificationObserver* observer, + NotificationType type, + NotificationSource source) { + Record record = { observer, type, source }; + RecordVector::iterator found = std::find( + registered_.begin(), registered_.end(), record); + if (found != registered_.end()) { + registered_.erase(found); + } else { + // Fall through to passing the removal through to the notification service. + // If it really isn't registered, it will also assert and do nothing, but + // we might as well catch the case where the class is trying to unregister + // for something they registered without going through us. + NOTREACHED(); + } + + NotificationService::current()->RemoveObserver(observer, type, source); +} + +void NotificationRegistrar::RemoveAll() { + NotificationService* service = NotificationService::current(); + for (size_t i = 0; i < registered_.size(); i++) { + service->RemoveObserver(registered_[i].observer, + registered_[i].type, + registered_[i].source); + } + registered_.clear(); +} |