summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
Diffstat (limited to 'net')
-rw-r--r--net/base/network_change_notifier_linux.cc26
-rw-r--r--net/base/network_change_notifier_linux.h13
2 files changed, 31 insertions, 8 deletions
diff --git a/net/base/network_change_notifier_linux.cc b/net/base/network_change_notifier_linux.cc
index e6f5453..f5f9bf5 100644
--- a/net/base/network_change_notifier_linux.cc
+++ b/net/base/network_change_notifier_linux.cc
@@ -8,6 +8,7 @@
#include <sys/socket.h>
#include "base/basictypes.h"
+#include "base/eintr_wrapper.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "net/base/net_errors.h"
@@ -31,15 +32,14 @@ NetworkChangeNotifierLinux::NetworkChangeNotifierLinux()
}
ListenForNotifications();
+ loop_->AddDestructionObserver(this);
}
NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() {
- if (netlink_fd_ != kInvalidSocket) {
- if (close(netlink_fd_) != 0)
- PLOG(ERROR) << "Failed to close socket";
- netlink_fd_ = kInvalidSocket;
- netlink_watcher_.StopWatchingFileDescriptor();
- }
+ StopWatching();
+
+ if (loop_)
+ loop_->RemoveDestructionObserver(this);
}
void NetworkChangeNotifierLinux::OnFileCanReadWithoutBlocking(int fd) {
@@ -52,6 +52,11 @@ void NetworkChangeNotifierLinux::OnFileCanWriteWithoutBlocking(int /* fd */) {
NOTREACHED();
}
+void NetworkChangeNotifierLinux::WillDestroyCurrentMessageLoop() {
+ StopWatching();
+ loop_ = NULL;
+}
+
void NetworkChangeNotifierLinux::ListenForNotifications() {
char buf[4096];
int rv = ReadNotificationMessage(buf, arraysize(buf));
@@ -88,4 +93,13 @@ int NetworkChangeNotifierLinux::ReadNotificationMessage(char* buf, size_t len) {
}
}
+void NetworkChangeNotifierLinux::StopWatching() {
+ if (netlink_fd_ != kInvalidSocket) {
+ if (HANDLE_EINTR(close(netlink_fd_)) != 0)
+ PLOG(ERROR) << "Failed to close socket";
+ netlink_fd_ = kInvalidSocket;
+ netlink_watcher_.StopWatchingFileDescriptor();
+ }
+}
+
} // namespace net
diff --git a/net/base/network_change_notifier_linux.h b/net/base/network_change_notifier_linux.h
index 83e63a0..e12a73c 100644
--- a/net/base/network_change_notifier_linux.h
+++ b/net/base/network_change_notifier_linux.h
@@ -13,7 +13,9 @@
namespace net {
class NetworkChangeNotifierLinux
- : public NetworkChangeNotifier, public MessageLoopForIO::Watcher {
+ : public NetworkChangeNotifier,
+ public MessageLoopForIO::Watcher,
+ public MessageLoop::DestructionObserver {
public:
NetworkChangeNotifierLinux();
@@ -32,6 +34,10 @@ class NetworkChangeNotifierLinux
virtual void OnFileCanReadWithoutBlocking(int fd);
virtual void OnFileCanWriteWithoutBlocking(int /* fd */);
+ // MessageLoop::DestructionObserver methods:
+
+ virtual void WillDestroyCurrentMessageLoop();
+
private:
virtual ~NetworkChangeNotifierLinux();
@@ -44,10 +50,13 @@ class NetworkChangeNotifierLinux
// recv() would block. Otherwise, it returns a net error code.
int ReadNotificationMessage(char* buf, size_t len);
+ // Stops watching the netlink file descriptor.
+ void StopWatching();
+
ObserverList<Observer, true> observers_;
int netlink_fd_; // This is the netlink socket descriptor.
- MessageLoopForIO* const loop_;
+ MessageLoopForIO* loop_;
MessageLoopForIO::FileDescriptorWatcher netlink_watcher_;
DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierLinux);