shill: clear more state on calls to Device::Stop and WiFi::Stop

BUG=chromium-os:20333
TEST=unittests, WiFiManager/000_SSID_Length_Limit

in addition to the main change, this CL
- moves some common code out of derived classes into Device
- adds some debugging messages (at VLOG(3)) to track down
  unexpected live references
- eliminates TestEventDispatcher (in cellular_unittest.cc),
  in favor of using the MockRTNLHandler

note that the 000_SSID_Length_Limit test was run with the
"Test MAX+1 ssid length" step disabled, because that requires
support for Manager.GetService.

Change-Id: I852d984a3af92e2c18e4a6e8d1dd5e4714069fdc
Reviewed-on: http://gerrit.chromium.org/gerrit/7734
Reviewed-by: mukesh agrawal <quiche@chromium.org>
Tested-by: mukesh agrawal <quiche@chromium.org>
diff --git a/device.h b/device.h
index 5e9e068..22dd9a1 100644
--- a/device.h
+++ b/device.h
@@ -28,6 +28,7 @@
 class Error;
 class EventDispatcher;
 class Manager;
+class RTNLHandler;
 
 // Device superclass.  Individual network interfaces types will inherit from
 // this class.
@@ -52,6 +53,10 @@
   virtual ~Device();
 
   virtual void Start();
+
+  // Clear running state, especially any fields that hold a reference back
+  // to us. After a call to Stop(), the Device may be restarted (with a call
+  // to Start()), or destroyed (if its refcount falls to zero).
   virtual void Stop();
 
   // Base method always returns false.
@@ -89,6 +94,7 @@
 
   PropertyStore *mutable_store() { return &store_; }
   const PropertyStore &store() const { return store_; }
+  RTNLHandler *rtnl_handler() { return rtnl_handler_; }
 
   bool Load(StoreInterface *storage);
   bool Save(StoreInterface *storage);
@@ -102,6 +108,7 @@
   FRIEND_TEST(DeviceTest, GetProperties);
   FRIEND_TEST(DeviceTest, Save);
   FRIEND_TEST(DeviceTest, SelectedService);
+  FRIEND_TEST(DeviceTest, Stop);
   FRIEND_TEST(WiFiMainTest, Connect);
 
   // If there's an IP configuration in |ipconfig_|, releases the IP address and
@@ -143,6 +150,9 @@
 
  private:
   friend class DeviceAdaptorInterface;
+  friend class DeviceTest;
+  friend class CellularTest;
+  friend class WiFiMainTest;
 
   static const char kStoragePowered[];
   static const char kStorageIPConfigs[];
@@ -181,8 +191,9 @@
   // Maintain a reference to the connected / connecting service
   ServiceRefPtr selected_service_;
 
-  // Cache singleton pointer for performance and test purposes.
+  // Cache singleton pointers for performance and test purposes.
   DHCPProvider *dhcp_provider_;
+  RTNLHandler *rtnl_handler_;
 
   DISALLOW_COPY_AND_ASSIGN(Device);
 };