diff options
author | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-24 22:53:51 +0000 |
---|---|---|
committer | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-24 22:53:51 +0000 |
commit | 6f0479fa6d498b8ba48c8b7cbcd507534fda610e (patch) | |
tree | ccd432f1d28d3535ec23c9d6330afbaede7420b6 /net/base | |
parent | 9a0d91236e8557d37498f002254a6a5427ebf842 (diff) | |
download | chromium_src-6f0479fa6d498b8ba48c8b7cbcd507534fda610e.zip chromium_src-6f0479fa6d498b8ba48c8b7cbcd507534fda610e.tar.gz chromium_src-6f0479fa6d498b8ba48c8b7cbcd507534fda610e.tar.bz2 |
Revert 39942 - Switch NetworkChangeNotifier implementations to use ObserverList.
Fix up observer list so we can use FOR_EACH_OBSERVER when check_empty is set.
Clean up the ObserverList API a bit, replacing GetElementAt() with HasObserver() and Clear().
BUG=36590
Review URL: http://codereview.chromium.org/652205
TBR=willchan@chromium.org
Review URL: http://codereview.chromium.org/661029
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@39945 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r-- | net/base/network_change_notifier_helper.cc | 45 | ||||
-rw-r--r-- | net/base/network_change_notifier_helper.h | 44 | ||||
-rw-r--r-- | net/base/network_change_notifier_linux.cc | 2 | ||||
-rw-r--r-- | net/base/network_change_notifier_linux.h | 9 | ||||
-rw-r--r-- | net/base/network_change_notifier_mac.h | 14 | ||||
-rw-r--r-- | net/base/network_change_notifier_win.cc | 2 | ||||
-rw-r--r-- | net/base/network_change_notifier_win.h | 16 |
7 files changed, 104 insertions, 28 deletions
diff --git a/net/base/network_change_notifier_helper.cc b/net/base/network_change_notifier_helper.cc new file mode 100644 index 0000000..052e3ef8 --- /dev/null +++ b/net/base/network_change_notifier_helper.cc @@ -0,0 +1,45 @@ +// Copyright (c) 2009 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 "net/base/network_change_notifier_helper.h" +#include <algorithm> +#include "base/logging.h" + +namespace net { + +namespace internal { + +NetworkChangeNotifierHelper::NetworkChangeNotifierHelper() + : is_notifying_observers_(false) {} + +NetworkChangeNotifierHelper::~NetworkChangeNotifierHelper() { + // TODO(willchan): Re-enable this DCHECK after fixing http://crbug.com/34391 + // since we're leaking URLRequestContextGetters that cause this DCHECK to + // fire. + // DCHECK(observers_.empty()); +} + +void NetworkChangeNotifierHelper::AddObserver(Observer* observer) { + DCHECK(!is_notifying_observers_); + observers_.push_back(observer); +} + +void NetworkChangeNotifierHelper::RemoveObserver(Observer* observer) { + DCHECK(!is_notifying_observers_); + observers_.erase(std::remove(observers_.begin(), observers_.end(), observer)); +} + +void NetworkChangeNotifierHelper::OnIPAddressChanged() { + DCHECK(!is_notifying_observers_); + is_notifying_observers_ = true; + for (std::vector<Observer*>::iterator it = observers_.begin(); + it != observers_.end(); ++it) { + (*it)->OnIPAddressChanged(); + } + is_notifying_observers_ = false; +} + +} // namespace internal + +} // namespace net diff --git a/net/base/network_change_notifier_helper.h b/net/base/network_change_notifier_helper.h new file mode 100644 index 0000000..f860c45 --- /dev/null +++ b/net/base/network_change_notifier_helper.h @@ -0,0 +1,44 @@ +// Copyright (c) 2009 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. + +// NetworkChangeNotifierHelper is a helper class that assists in implementing +// base functionality for a NetworkChangeNotifier implementation. In +// particular, it manages adding/removing observers and sending them +// notifications of event changes. + +#ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_HELPER_H_ +#define NET_BASE_NETWORK_CHANGE_NOTIFIER_HELPER_H_ + +#include <vector> +#include "base/basictypes.h" +#include "net/base/network_change_notifier.h" + +namespace net { + +namespace internal { + +class NetworkChangeNotifierHelper { + public: + typedef NetworkChangeNotifier::Observer Observer; + + NetworkChangeNotifierHelper(); + ~NetworkChangeNotifierHelper(); + + void AddObserver(Observer* observer); + void RemoveObserver(Observer* observer); + + void OnIPAddressChanged(); + + private: + bool is_notifying_observers_; + std::vector<Observer*> observers_; + + DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierHelper); +}; + +} // namespace internal + +} // namespace net + +#endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_HELPER_H_ diff --git a/net/base/network_change_notifier_linux.cc b/net/base/network_change_notifier_linux.cc index e6f5453..2a85c15 100644 --- a/net/base/network_change_notifier_linux.cc +++ b/net/base/network_change_notifier_linux.cc @@ -57,7 +57,7 @@ void NetworkChangeNotifierLinux::ListenForNotifications() { int rv = ReadNotificationMessage(buf, arraysize(buf)); while (rv > 0 ) { if (HandleNetlinkMessage(buf, rv)) - FOR_EACH_OBSERVER(Observer, observers_, OnIPAddressChanged()); + helper_.OnIPAddressChanged(); rv = ReadNotificationMessage(buf, arraysize(buf)); } diff --git a/net/base/network_change_notifier_linux.h b/net/base/network_change_notifier_linux.h index 91d73e8..323ea55 100644 --- a/net/base/network_change_notifier_linux.h +++ b/net/base/network_change_notifier_linux.h @@ -7,8 +7,7 @@ #include "base/basictypes.h" #include "base/message_loop.h" -#include "base/observer_list.h" -#include "net/base/network_change_notifier.h" +#include "net/base/network_change_notifier_helper.h" namespace net { @@ -20,11 +19,11 @@ class NetworkChangeNotifierLinux // NetworkChangeNotifier methods: virtual void AddObserver(Observer* observer) { - observers_.AddObserver(observer); + helper_.AddObserver(observer); } virtual void RemoveObserver(Observer* observer) { - observers_.RemoveObserver(observer); + helper_.RemoveObserver(observer); } // MessageLoopForIO::Watcher methods: @@ -47,7 +46,7 @@ class NetworkChangeNotifierLinux // Handles the netlink message and notifies the observers. void HandleNotifications(const char* buf, size_t len); - ObserverList<Observer, true> observers_; + internal::NetworkChangeNotifierHelper helper_; int netlink_fd_; // This is the netlink socket descriptor. MessageLoopForIO* const loop_; diff --git a/net/base/network_change_notifier_mac.h b/net/base/network_change_notifier_mac.h index 44d4806..5e777f4 100644 --- a/net/base/network_change_notifier_mac.h +++ b/net/base/network_change_notifier_mac.h @@ -6,11 +6,11 @@ #define NET_BASE_NETWORK_CHANGE_NOTIFIER_MAC_H_ #include "base/basictypes.h" -#include "base/observer_list.h" #include "base/ref_counted.h" #include "base/scoped_ptr.h" #include "base/task.h" #include "net/base/network_change_notifier.h" +#include "net/base/network_change_notifier_helper.h" class MessageLoop; namespace base { @@ -23,18 +23,16 @@ class NetworkChangeNotifierMac : public NetworkChangeNotifier { public: NetworkChangeNotifierMac(); - void OnIPAddressChanged() { - FOR_EACH_OBSERVER(Observer, observers_, OnIPAddressChanged()); - } + void OnIPAddressChanged() { helper_.OnIPAddressChanged(); } // NetworkChangeNotifier methods: virtual void AddObserver(Observer* observer) { - observers_.AddObserver(observer); + helper_.AddObserver(observer); } virtual void RemoveObserver(Observer* observer) { - observers_.RemoveObserver(observer); + helper_.RemoveObserver(observer); } private: @@ -51,9 +49,7 @@ class NetworkChangeNotifierMac : public NetworkChangeNotifier { // Receives the OS X network change notifications on this thread. scoped_ptr<base::Thread> notifier_thread_; - // TODO(willchan): Fix the URLRequestContextGetter leaks and flip the false to - // true so we assert that all observers have been removed. - ObserverList<Observer, false> observers_; + internal::NetworkChangeNotifierHelper helper_; // Used to initialize the notifier thread. ScopedRunnableMethodFactory<NetworkChangeNotifierMac> method_factory_; diff --git a/net/base/network_change_notifier_win.cc b/net/base/network_change_notifier_win.cc index d139eb0f..e52bd46 100644 --- a/net/base/network_change_notifier_win.cc +++ b/net/base/network_change_notifier_win.cc @@ -3,11 +3,9 @@ // found in the LICENSE file. #include "net/base/network_change_notifier_win.h" - #include <iphlpapi.h> #include <windows.h> #include <winsock2.h> - #include "base/basictypes.h" #include "base/compiler_specific.h" #include "base/logging.h" diff --git a/net/base/network_change_notifier_win.h b/net/base/network_change_notifier_win.h index 0f1f8d4..b11ec0d 100644 --- a/net/base/network_change_notifier_win.h +++ b/net/base/network_change_notifier_win.h @@ -7,8 +7,7 @@ #include "base/basictypes.h" #include "base/object_watcher.h" -#include "base/observer_list.h" -#include "net/base/network_change_notifier.h" +#include "net/base/network_change_notifier_helper.h" namespace net { @@ -17,19 +16,16 @@ class NetworkChangeNotifierWin : public NetworkChangeNotifier { NetworkChangeNotifierWin(); // Called by NetworkChangeNotifierWin::Impl. - void OnIPAddressChanged() { - FOR_EACH_OBSERVER(Observer, observers_, OnIPAddressChanged()); - } - + void OnIPAddressChanged() { helper_.OnIPAddressChanged(); } // NetworkChangeNotifier methods: virtual void AddObserver(Observer* observer) { - observers_.AddObserver(observer); + helper_.AddObserver(observer); } virtual void RemoveObserver(Observer* observer) { - observers_.RemoveObserver(observer); + helper_.RemoveObserver(observer); } private: @@ -37,9 +33,7 @@ class NetworkChangeNotifierWin : public NetworkChangeNotifier { virtual ~NetworkChangeNotifierWin(); - // TODO(willchan): Fix the URLRequestContextGetter leaks and flip the false to - // true so we assert that all observers have been removed. - ObserverList<Observer, false> observers_; + internal::NetworkChangeNotifierHelper helper_; scoped_ptr<Impl> impl_; DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierWin); |