| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/network_change_notifier_linux.h" | 5 #include "net/base/network_change_notifier_linux.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <sys/socket.h> | 8 #include <sys/socket.h> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/eintr_wrapper.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 13 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 14 #include "net/base/network_change_notifier_netlink_linux.h" | 15 #include "net/base/network_change_notifier_netlink_linux.h" |
| 15 | 16 |
| 16 namespace net { | 17 namespace net { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 const int kInvalidSocket = -1; | 21 const int kInvalidSocket = -1; |
| 21 | 22 |
| 22 } // namespace | 23 } // namespace |
| 23 | 24 |
| 24 NetworkChangeNotifierLinux::NetworkChangeNotifierLinux() | 25 NetworkChangeNotifierLinux::NetworkChangeNotifierLinux() |
| 25 : netlink_fd_(kInvalidSocket), | 26 : netlink_fd_(kInvalidSocket), |
| 26 loop_(MessageLoopForIO::current()) { | 27 loop_(MessageLoopForIO::current()) { |
| 27 netlink_fd_ = InitializeNetlinkSocket(); | 28 netlink_fd_ = InitializeNetlinkSocket(); |
| 28 if (netlink_fd_ < 0) { | 29 if (netlink_fd_ < 0) { |
| 29 netlink_fd_ = kInvalidSocket; | 30 netlink_fd_ = kInvalidSocket; |
| 30 return; | 31 return; |
| 31 } | 32 } |
| 32 | 33 |
| 33 ListenForNotifications(); | 34 ListenForNotifications(); |
| 35 loop_->AddDestructionObserver(this); |
| 34 } | 36 } |
| 35 | 37 |
| 36 NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() { | 38 NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() { |
| 37 if (netlink_fd_ != kInvalidSocket) { | 39 StopWatching(); |
| 38 if (close(netlink_fd_) != 0) | 40 |
| 39 PLOG(ERROR) << "Failed to close socket"; | 41 if (loop_) |
| 40 netlink_fd_ = kInvalidSocket; | 42 loop_->RemoveDestructionObserver(this); |
| 41 netlink_watcher_.StopWatchingFileDescriptor(); | |
| 42 } | |
| 43 } | 43 } |
| 44 | 44 |
| 45 void NetworkChangeNotifierLinux::OnFileCanReadWithoutBlocking(int fd) { | 45 void NetworkChangeNotifierLinux::OnFileCanReadWithoutBlocking(int fd) { |
| 46 DCHECK_EQ(fd, netlink_fd_); | 46 DCHECK_EQ(fd, netlink_fd_); |
| 47 | 47 |
| 48 ListenForNotifications(); | 48 ListenForNotifications(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 void NetworkChangeNotifierLinux::OnFileCanWriteWithoutBlocking(int /* fd */) { | 51 void NetworkChangeNotifierLinux::OnFileCanWriteWithoutBlocking(int /* fd */) { |
| 52 NOTREACHED(); | 52 NOTREACHED(); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void NetworkChangeNotifierLinux::WillDestroyCurrentMessageLoop() { |
| 56 StopWatching(); |
| 57 loop_ = NULL; |
| 58 } |
| 59 |
| 55 void NetworkChangeNotifierLinux::ListenForNotifications() { | 60 void NetworkChangeNotifierLinux::ListenForNotifications() { |
| 56 char buf[4096]; | 61 char buf[4096]; |
| 57 int rv = ReadNotificationMessage(buf, arraysize(buf)); | 62 int rv = ReadNotificationMessage(buf, arraysize(buf)); |
| 58 while (rv > 0 ) { | 63 while (rv > 0 ) { |
| 59 if (HandleNetlinkMessage(buf, rv)) | 64 if (HandleNetlinkMessage(buf, rv)) |
| 60 FOR_EACH_OBSERVER(Observer, observers_, OnIPAddressChanged()); | 65 FOR_EACH_OBSERVER(Observer, observers_, OnIPAddressChanged()); |
| 61 rv = ReadNotificationMessage(buf, arraysize(buf)); | 66 rv = ReadNotificationMessage(buf, arraysize(buf)); |
| 62 } | 67 } |
| 63 | 68 |
| 64 if (rv == ERR_IO_PENDING) { | 69 if (rv == ERR_IO_PENDING) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 81 DCHECK_NE(rv, 0); | 86 DCHECK_NE(rv, 0); |
| 82 if (errno != EAGAIN && errno != EWOULDBLOCK) { | 87 if (errno != EAGAIN && errno != EWOULDBLOCK) { |
| 83 PLOG(DFATAL) << "recv"; | 88 PLOG(DFATAL) << "recv"; |
| 84 return ERR_FAILED; | 89 return ERR_FAILED; |
| 85 } | 90 } |
| 86 | 91 |
| 87 return ERR_IO_PENDING; | 92 return ERR_IO_PENDING; |
| 88 } | 93 } |
| 89 } | 94 } |
| 90 | 95 |
| 96 void NetworkChangeNotifierLinux::StopWatching() { |
| 97 if (netlink_fd_ != kInvalidSocket) { |
| 98 if (HANDLE_EINTR(close(netlink_fd_)) != 0) |
| 99 PLOG(ERROR) << "Failed to close socket"; |
| 100 netlink_fd_ = kInvalidSocket; |
| 101 netlink_watcher_.StopWatchingFileDescriptor(); |
| 102 } |
| 103 } |
| 104 |
| 91 } // namespace net | 105 } // namespace net |
| OLD | NEW |