Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / net / base / address_tracker_linux.h
index e5ab692..ec52b60 100644 (file)
@@ -20,6 +20,7 @@
 #include "base/message_loop/message_loop.h"
 #include "base/synchronization/condition_variable.h"
 #include "base/synchronization/lock.h"
+#include "base/threading/thread_checker.h"
 #include "net/base/net_util.h"
 #include "net/base/network_change_notifier.h"
 
@@ -33,18 +34,33 @@ class NET_EXPORT_PRIVATE AddressTrackerLinux :
  public:
   typedef std::map<IPAddressNumber, struct ifaddrmsg> AddressMap;
 
-  // Will run |address_callback| when the AddressMap changes and will run
-  // |link_callback| when the list of online links changes.
+  // Non-tracking version constructor: it takes a snapshot of the
+  // current system configuration. Once Init() returns, the
+  // configuration is available through GetOnlineLinks() and
+  // GetAddressMap().
+  AddressTrackerLinux();
+
+  // Tracking version constructor: it will run |address_callback| when
+  // the AddressMap changes, |link_callback| when the list of online
+  // links changes, and |tunnel_callback| when the list of online
+  // tunnels changes.
   AddressTrackerLinux(const base::Closure& address_callback,
-                      const base::Closure& link_callback);
+                      const base::Closure& link_callback,
+                      const base::Closure& tunnel_callback);
   virtual ~AddressTrackerLinux();
 
-  // Starts watching system configuration for changes. The current thread must
-  // have a MessageLoopForIO.
+  // In tracking mode, it starts watching the system configuration for
+  // changes. The current thread must have a MessageLoopForIO. In
+  // non-tracking mode, once Init() returns, a snapshot of the system
+  // configuration is available through GetOnlineLinks() and
+  // GetAddressMap().
   void Init();
 
   AddressMap GetAddressMap() const;
 
+  // Returns set of interface indicies for online interfaces.
+  base::hash_set<int> GetOnlineLinks() const;
+
   // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType().
   // Safe to call from any thread, but will block until Init() has completed.
   NetworkChangeNotifier::ConnectionType GetCurrentConnectionType();
@@ -52,18 +68,41 @@ class NET_EXPORT_PRIVATE AddressTrackerLinux :
  private:
   friend class AddressTrackerLinuxTest;
 
+  // In tracking mode, holds |lock| while alive. In non-tracking mode,
+  // enforces single-threaded access.
+  class AddressTrackerAutoLock {
+   public:
+    AddressTrackerAutoLock(const AddressTrackerLinux& tracker,
+                           base::Lock& lock);
+    ~AddressTrackerAutoLock();
+
+   private:
+    const AddressTrackerLinux& tracker_;
+    base::Lock& lock_;
+    DISALLOW_COPY_AND_ASSIGN(AddressTrackerAutoLock);
+  };
+
+  // A function that returns the name of an interface given the interface index
+  // in |interface_index|.
+  typedef const char* (*GetInterfaceNameFunction)(int interface_index);
+
   // Sets |*address_changed| to indicate whether |address_map_| changed and
-  // sets |*link_changed| to indicate if |online_links_| changed while reading
-  // messages from |netlink_fd_|.
-  void ReadMessages(bool* address_changed, bool* link_changed);
+  // sets |*link_changed| to indicate if |online_links_| changed and sets
+  // |*tunnel_changed| to indicate if |online_links_| changed with regards to a
+  // tunnel interface while reading messages from |netlink_fd_|.
+  void ReadMessages(bool* address_changed,
+                    bool* link_changed,
+                    bool* tunnel_changed);
 
   // Sets |*address_changed| to true if |address_map_| changed, sets
-  // |*link_changed| to true if |online_links_| changed while reading the
-  // message from |buffer|.
-  void HandleMessage(const char* buffer,
+  // |*link_changed| to true if |online_links_| changed, sets |*tunnel_changed|
+  // to true if |online_links_| changed with regards to a tunnel interface while
+  // reading the message from |buffer|.
+  void HandleMessage(char* buffer,
                      size_t length,
                      bool* address_changed,
-                     bool* link_changed);
+                     bool* link_changed,
+                     bool* tunnel_changed);
 
   // Call when some part of initialization failed; forces online and unblocks.
   void AbortAndForceOnline();
@@ -75,8 +114,17 @@ class NET_EXPORT_PRIVATE AddressTrackerLinux :
   // Close |netlink_fd_|
   void CloseSocket();
 
+  // Does |msg| refer to a tunnel interface?
+  bool IsTunnelInterface(const struct ifinfomsg* msg) const;
+
+  // Gets the name of an interface given the interface index |interface_index|.
+  // May return empty string if it fails but should not return NULL. This is
+  // overridden by tests.
+  GetInterfaceNameFunction get_interface_name_;
+
   base::Closure address_callback_;
   base::Closure link_callback_;
+  base::Closure tunnel_callback_;
 
   int netlink_fd_;
   base::MessageLoopForIO::FileDescriptorWatcher watcher_;
@@ -85,12 +133,17 @@ class NET_EXPORT_PRIVATE AddressTrackerLinux :
   AddressMap address_map_;
 
   // Set of interface indices for links that are currently online.
+  mutable base::Lock online_links_lock_;
   base::hash_set<int> online_links_;
 
   base::Lock is_offline_lock_;
   bool is_offline_;
   bool is_offline_initialized_;
   base::ConditionVariable is_offline_initialized_cv_;
+  bool tracking_;
+
+  // Used to verify single-threaded access in non-tracking mode.
+  base::ThreadChecker thread_checker_;
 };
 
 }  // namespace internal