Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef NET_BASE_ADDRESS_TRACKER_LINUX_H_ | 5 #ifndef NET_BASE_ADDRESS_TRACKER_LINUX_H_ |
| 6 #define NET_BASE_ADDRESS_TRACKER_LINUX_H_ | 6 #define NET_BASE_ADDRESS_TRACKER_LINUX_H_ |
| 7 | 7 |
| 8 #include <sys/socket.h> // Needed to include netlink. | 8 #include <sys/socket.h> // Needed to include netlink. |
| 9 // Mask superfluous definition of |struct net|. This is fixed in Linux 2.6.38. | 9 // Mask superfluous definition of |struct net|. This is fixed in Linux 2.6.38. |
| 10 #define net net_kernel | 10 #define net net_kernel |
| 11 #include <linux/rtnetlink.h> | 11 #include <linux/rtnetlink.h> |
| 12 #undef net | 12 #undef net |
| 13 | 13 |
| 14 #include <map> | 14 #include <map> |
| 15 | 15 |
| 16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 17 #include "base/callback.h" | 17 #include "base/callback.h" |
| 18 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
| 19 #include "base/hash_tables.h" | |
| 19 #include "base/message_loop.h" | 20 #include "base/message_loop.h" |
| 20 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
| 22 #include "base/synchronization/waitable_event.h" | |
| 21 #include "net/base/net_util.h" | 23 #include "net/base/net_util.h" |
| 24 #include "net/base/network_change_notifier.h" | |
| 22 | 25 |
| 23 namespace net { | 26 namespace net { |
| 24 namespace internal { | 27 namespace internal { |
| 25 | 28 |
| 26 // Keeps track of network interface addresses using rtnetlink. Used by | 29 // Keeps track of network interface addresses using rtnetlink. Used by |
| 27 // NetworkChangeNotifier to provide signals to registered IPAddressObservers. | 30 // NetworkChangeNotifier to provide signals to registered IPAddressObservers. |
| 28 class NET_EXPORT_PRIVATE AddressTrackerLinux | 31 class NET_EXPORT_PRIVATE AddressTrackerLinux |
| 29 : public MessageLoopForIO::Watcher { | 32 : public MessageLoopForIO::Watcher { |
| 30 public: | 33 public: |
| 31 typedef std::map<IPAddressNumber, struct ifaddrmsg> AddressMap; | 34 typedef std::map<IPAddressNumber, struct ifaddrmsg> AddressMap; |
| 32 | 35 |
| 33 // Will run |callback| when the AddressMap changes. | 36 // Will run |address_callback| when the AddressMap changes and will run |
| 34 explicit AddressTrackerLinux(const base::Closure& callback); | 37 // |link_callback| when the list of online links changes. |
| 38 AddressTrackerLinux(const base::Closure& address_callback, | |
| 39 const base::Closure& link_callback); | |
| 35 virtual ~AddressTrackerLinux(); | 40 virtual ~AddressTrackerLinux(); |
| 36 | 41 |
| 37 // Starts watching system configuration for changes. The current thread must | 42 // Starts watching system configuration for changes. The current thread must |
| 38 // have a MessageLoopForIO. | 43 // have a MessageLoopForIO. |
| 39 void Init(); | 44 void Init(); |
| 40 | 45 |
| 41 AddressMap GetAddressMap() const; | 46 AddressMap GetAddressMap() const; |
| 42 | 47 |
| 48 // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType(). | |
| 49 // Safe to call from any thread, but will block until Init() has completed. | |
| 50 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType(); | |
|
szym
2012/11/11 16:21:55
const?
pauljensen
2012/11/12 16:45:21
This would require making is_offline_lock_ and off
| |
| 51 | |
| 43 private: | 52 private: |
| 44 friend class AddressTrackerLinuxTest; | 53 friend class AddressTrackerLinuxTest; |
| 45 | 54 |
| 46 // Returns true if |map_| changed while reading messages from |netlink_fd_|. | 55 // Sets |*address_changed| to indicate whether |address_map_| changed and |
| 47 bool ReadMessages(); | 56 // sets |*link_changed| to indicate if |online_links_| changed while reading |
| 57 // messages from |netlink_fd_|. | |
| 58 void ReadMessages(bool* address_changed, bool* link_changed); | |
| 48 | 59 |
| 49 // Returns true if |map_| changed while reading the message from |buffer|. | 60 // Sets |*address_changed| to true if |address_map_| changed, sets |
| 50 bool HandleMessage(const char* buffer, size_t length); | 61 // |*link_changed| to true if |online_links_| changed while reading the |
| 62 // message from |buffer|. | |
| 63 void HandleMessage(const char* buffer, | |
| 64 size_t length, | |
| 65 bool* address_changed, | |
| 66 bool* link_changed); | |
| 67 | |
| 68 // Populates |address_map_| and |online_links_|. | |
| 69 void GetInitialSettings(); | |
| 51 | 70 |
| 52 // MessageLoopForIO::Watcher: | 71 // MessageLoopForIO::Watcher: |
| 53 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | 72 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
| 54 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE; | 73 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE; |
| 55 | 74 |
| 56 base::Closure callback_; | 75 base::Closure address_callback_; |
| 76 base::Closure link_callback_; | |
| 57 | 77 |
| 58 int netlink_fd_; | 78 int netlink_fd_; |
| 59 MessageLoopForIO::FileDescriptorWatcher watcher_; | 79 MessageLoopForIO::FileDescriptorWatcher watcher_; |
| 60 | 80 |
| 61 mutable base::Lock lock_; | 81 mutable base::Lock address_map_lock_; |
| 62 AddressMap map_; | 82 AddressMap address_map_; |
| 83 base::hash_set<int> online_links_; | |
| 84 | |
| 85 bool is_offline_; | |
| 86 base::Lock is_offline_lock_; | |
|
szym
2012/11/11 16:21:55
If you make GetCurrentConnectionType const, then t
pauljensen
2012/11/12 16:45:21
I was just moving this from NetworkManagerApi. I'
| |
| 87 base::WaitableEvent offline_state_initialized_; | |
| 63 }; | 88 }; |
| 64 | 89 |
| 65 } // namespace internal | 90 } // namespace internal |
| 66 } // namespace net | 91 } // namespace net |
| 67 | 92 |
| 68 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_ | 93 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_ |
| OLD | NEW |