[shill] Use ref-counted pointers more, break unneeded header deps

1) Replace more bare pointers with ref-counted pointers.
2) Move declarations of *RefPtr into a header file with fwd-declared classes
3) Make class headers include that, move inclusion of real headers to .cc files.

BUG=None
TEST=unit tests

Change-Id: I8208c82377c6dbefe6903561a503b8db63f063d6
Reviewed-on: http://gerrit.chromium.org/gerrit/3177
Reviewed-by: Chris Masone <cmasone@chromium.org>
Tested-by: Chris Masone <cmasone@chromium.org>
diff --git a/cellular.cc b/cellular.cc
index 631c008..8740103 100644
--- a/cellular.cc
+++ b/cellular.cc
@@ -68,7 +68,7 @@
 }
 
 void Cellular::Stop() {
-  manager_->DeregisterService(service_.get());
+  manager_->DeregisterService(service_);
   Device::Stop();
 }
 
diff --git a/cellular.h b/cellular.h
index 3eba142..083c8e3 100644
--- a/cellular.h
+++ b/cellular.h
@@ -9,8 +9,8 @@
 
 #include <base/basictypes.h>
 
-#include "shill/cellular_service.h"
 #include "shill/device.h"
+#include "shill/refptr_types.h"
 #include "shill/shill_event.h"
 
 namespace shill {
diff --git a/cellular_service.cc b/cellular_service.cc
index 1b34002..70783c1 100644
--- a/cellular_service.cc
+++ b/cellular_service.cc
@@ -21,9 +21,9 @@
 namespace shill {
 CellularService::CellularService(ControlInterface *control_interface,
                                  EventDispatcher *dispatcher,
-                                 Cellular *device,
+                                 const CellularRefPtr &device,
                                  const string &name)
-    : Service(control_interface, dispatcher, device, name),
+    : Service(control_interface, dispatcher, name),
       cellular_(device),
       strength_(0),
       type_(flimflam::kTypeCellular) {
diff --git a/cellular_service.h b/cellular_service.h
index d98be3f..f748377 100644
--- a/cellular_service.h
+++ b/cellular_service.h
@@ -11,19 +11,17 @@
 #include <base/basictypes.h>
 
 #include "shill/cellular.h"
-#include "shill/device.h"
+#include "shill/refptr_types.h"
 #include "shill/shill_event.h"
 #include "shill/service.h"
 
 namespace shill {
 
-class Cellular;
-
 class CellularService : public Service {
  public:
   CellularService(ControlInterface *control_interface,
                   EventDispatcher *dispatcher,
-                  Cellular *device,
+                  const CellularRefPtr &device,
                   const std::string& name);
   ~CellularService();
   void Connect();
@@ -45,7 +43,7 @@
   std::map<std::string, std::string> last_good_apn_info_;
 
  private:
-  Cellular *cellular_;
+  CellularRefPtr cellular_;
   const std::string type_;
   DISALLOW_COPY_AND_ASSIGN(CellularService);
 };
diff --git a/dbus_adaptor_unittest.cc b/dbus_adaptor_unittest.cc
index e6f74bf..19883cc 100644
--- a/dbus_adaptor_unittest.cc
+++ b/dbus_adaptor_unittest.cc
@@ -56,7 +56,6 @@
                                0)),
         service_(new MockService(&control_interface_,
                                  &dispatcher_,
-                                 device_,
                                  "mock")) {
     ex_stringmap_[ex_string_] = ex_string_;
     stringmap_v_ = DBusAdaptor::StringmapToVariant(ex_stringmap_);
diff --git a/device.cc b/device.cc
index 5437f1f..428f985 100644
--- a/device.cc
+++ b/device.cc
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+#include "shill/device.h"
+
 #include <time.h>
 #include <stdio.h>
 
@@ -14,12 +16,14 @@
 #include <chromeos/dbus/service_constants.h>
 
 #include "shill/control_interface.h"
-#include "shill/device.h"
 #include "shill/device_dbus_adaptor.h"
+#include "shill/dhcp_config.h"
 #include "shill/dhcp_provider.h"
 #include "shill/error.h"
 #include "shill/manager.h"
+#include "shill/refptr_types.h"
 #include "shill/rtnl_handler.h"
+#include "shill/service.h"
 #include "shill/shill_event.h"
 
 using std::string;
@@ -149,7 +153,8 @@
   return ipconfig_->RequestIP();
 }
 
-void Device::IPConfigUpdatedCallback(IPConfigRefPtr ipconfig, bool success) {
+void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig,
+                                     bool success) {
   // TODO(petkov): Use DeviceInfo to configure IP, etc. -- maybe through
   // ConfigIP? Also, maybe allow forwarding the callback to interested listeners
   // (e.g., the Manager).
diff --git a/device.h b/device.h
index b8fa675..ef75711 100644
--- a/device.h
+++ b/device.h
@@ -13,16 +13,14 @@
 #include <base/memory/scoped_ptr.h>
 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
 
-#include "shill/device_config_interface.h"
 #include "shill/ipconfig.h"
 #include "shill/property_store.h"
-#include "shill/service.h"
+#include "shill/refptr_types.h"
 #include "shill/shill_event.h"
 
 namespace shill {
 
 class ControlInterface;
-class Device;
 class DeviceAdaptorInterface;
 class DeviceInfo;
 class Endpoint;
@@ -30,14 +28,9 @@
 class EventDispatcher;
 class Manager;
 
-typedef scoped_refptr<const Device> DeviceConstRefPtr;
-typedef scoped_refptr<Device> DeviceRefPtr;
-
 // Device superclass.  Individual network interfaces types will inherit from
 // this class.
-// DeviceConfigInterface is RefCounted, so this class and derived classes
-// are as well.
-class Device : public DeviceConfigInterface, public PropertyStore {
+class Device : public base::RefCounted<Device>, public PropertyStore {
  public:
   enum Technology {
     kEthernet,
@@ -65,7 +58,6 @@
   virtual void LinkEvent(unsigned flags, unsigned change);
   virtual void Scan();
 
-  // Implementation of DeviceConfigInterface
   virtual void ConfigIP() {}
 
   // Implementation of PropertyStore
@@ -122,7 +114,7 @@
   friend class DeviceAdaptorInterface;
 
   // Callback invoked on every IP configuration update.
-  void IPConfigUpdatedCallback(IPConfigRefPtr ipconfig, bool success);
+  void IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig, bool success);
 
   const std::string link_name_;
   scoped_ptr<DeviceAdaptorInterface> adaptor_;
diff --git a/device_config_interface.h b/device_config_interface.h
deleted file mode 100644
index 5031e71..0000000
--- a/device_config_interface.h
+++ /dev/null
@@ -1,32 +0,0 @@
-// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef SHILL_DEVICE_CONFIG_INTERFACE_
-#define SHILL_DEVICE_CONFIG_INTERFACE_
-
-#include <base/memory/ref_counted.h>
-
-namespace shill {
-class DeviceConfigInterface;
-
-typedef scoped_refptr<DeviceConfigInterface> DeviceConfigInterfaceRefPtr;
-
-// The interface by which outside parties can push configuration information
-// to devices.
-class DeviceConfigInterface : public base::RefCounted<DeviceConfigInterface> {
- public:
-  DeviceConfigInterface() {}
-  virtual ~DeviceConfigInterface() {}
-
-  // Set IP configuration info.
-  // TODO(cmasone): figure out what data needs to be passed in here.
-  virtual void ConfigIP() = 0;
- private:
-  friend class base::RefCounted<DeviceConfigInterface>;
-  DISALLOW_COPY_AND_ASSIGN(DeviceConfigInterface);
-};
-
-}  // namespace shill
-
-#endif  // SHILL_DEVICE_CONFIG_INTERFACE_
diff --git a/dhcp_config.h b/dhcp_config.h
index 9cdf535..e7459d8 100644
--- a/dhcp_config.h
+++ b/dhcp_config.h
@@ -15,13 +15,10 @@
 
 namespace shill {
 
-class DHCPConfig;
 class DHCPProvider;
 class DHCPProxyInterface;
 class GLib;
 
-typedef scoped_refptr<DHCPConfig> DHCPConfigRefPtr;
-
 class DHCPConfig : public IPConfig {
  public:
   typedef std::map<std::string, DBus::Variant> Configuration;
diff --git a/dhcp_config_unittest.cc b/dhcp_config_unittest.cc
index 3edeeae..44d638d 100644
--- a/dhcp_config_unittest.cc
+++ b/dhcp_config_unittest.cc
@@ -111,14 +111,14 @@
 class UpdateCallbackTest {
  public:
   UpdateCallbackTest(const string &message,
-                     IPConfigRefPtr ipconfig,
+                     const IPConfigRefPtr &ipconfig,
                      bool success)
       : message_(message),
         ipconfig_(ipconfig),
         success_(success),
         called_(false) {}
 
-  void Callback(IPConfigRefPtr ipconfig, bool success) {
+  void Callback(const IPConfigRefPtr &ipconfig, bool success) {
     called_ = true;
     EXPECT_EQ(ipconfig_.get(), ipconfig.get()) << message_;
     EXPECT_EQ(success_, success) << message_;
diff --git a/dhcp_provider.cc b/dhcp_provider.cc
index ec72602..ae67ed7 100644
--- a/dhcp_provider.cc
+++ b/dhcp_provider.cc
@@ -6,6 +6,7 @@
 
 #include <base/logging.h>
 
+#include "shill/dhcp_config.h"
 #include "shill/dhcpcd_proxy.h"
 
 using std::string;
@@ -44,7 +45,7 @@
   return it->second;
 }
 
-void DHCPProvider::BindPID(int pid, DHCPConfigRefPtr config) {
+void DHCPProvider::BindPID(int pid, const DHCPConfigRefPtr &config) {
   VLOG(2) << __func__ << " pid: " << pid;
   configs_[pid] = config;
 }
diff --git a/dhcp_provider.h b/dhcp_provider.h
index 6d306de..53375d5 100644
--- a/dhcp_provider.h
+++ b/dhcp_provider.h
@@ -12,11 +12,12 @@
 #include <dbus-c++/connection.h>
 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
 
-#include "shill/dhcp_config.h"
+#include "shill/refptr_types.h"
 
 namespace shill {
 
 class DHCPListenerInterface;
+class GLib;
 
 // DHCPProvider is a singleton providing the main DHCP configuration
 // entrypoint. Once the provider is initialized through its Init method, DHCP
@@ -45,7 +46,7 @@
 
   // Binds a |pid| to a DHCP |config|. When a DHCP config spawns a new DHCP
   // client, it binds itself to that client's |pid|.
-  void BindPID(int pid, DHCPConfigRefPtr config);
+  void BindPID(int pid, const DHCPConfigRefPtr &config);
 
   // Unbinds a |pid|. This method is used by a DHCP config to signal the
   // provider that the DHCP client has been terminated. This may result in
diff --git a/dhcp_provider_unittest.cc b/dhcp_provider_unittest.cc
index 14103df..2dd3314 100644
--- a/dhcp_provider_unittest.cc
+++ b/dhcp_provider_unittest.cc
@@ -3,6 +3,8 @@
 // found in the LICENSE file.
 
 #include "shill/dhcp_provider.h"
+
+#include "shill/dhcp_config.h"
 #include "shill/mock_glib.h"
 
 using testing::Test;
diff --git a/ethernet.h b/ethernet.h
index fbcacd9..95feec0 100644
--- a/ethernet.h
+++ b/ethernet.h
@@ -8,8 +8,8 @@
 #include <string>
 
 #include "shill/device.h"
+#include "shill/refptr_types.h"
 #include "shill/shill_event.h"
-#include "shill/ethernet_service.h"
 
 namespace shill {
 
diff --git a/ethernet_service.cc b/ethernet_service.cc
index fe04655..586dc33 100644
--- a/ethernet_service.cc
+++ b/ethernet_service.cc
@@ -27,9 +27,9 @@
 
 EthernetService::EthernetService(ControlInterface *control_interface,
                                  EventDispatcher *dispatcher,
-                                 Ethernet *device,
+                                 const EthernetRefPtr &device,
                                  const string &name)
-    : Service(control_interface, dispatcher, device, name),
+    : Service(control_interface, dispatcher, name),
       ethernet_(device),
       type_(flimflam::kTypeEthernet) {
   set_auto_connect(true);
diff --git a/ethernet_service.h b/ethernet_service.h
index 1886854..df708ae 100644
--- a/ethernet_service.h
+++ b/ethernet_service.h
@@ -8,20 +8,17 @@
 #include <base/basictypes.h>
 
 #include "shill/ethernet.h"
-#include "shill/device.h"
+#include "shill/refptr_types.h"
 #include "shill/shill_event.h"
 #include "shill/service.h"
 
 namespace shill {
 
-
-class Ethernet;
-
 class EthernetService : public Service {
  public:
   EthernetService(ControlInterface *control_interface,
                   EventDispatcher *dispatcher,
-                  Ethernet *device,
+                  const EthernetRefPtr &device,
                   const std::string& name);
   ~EthernetService();
   void Connect();
@@ -31,7 +28,7 @@
   virtual std::string CalculateState() { return "idle"; }
 
  private:
-  Ethernet *ethernet_;
+  EthernetRefPtr ethernet_;
   const std::string type_;
   DISALLOW_COPY_AND_ASSIGN(EthernetService);
 };
diff --git a/ipconfig.cc b/ipconfig.cc
index fd6da2f..5e42eb2 100644
--- a/ipconfig.cc
+++ b/ipconfig.cc
@@ -38,7 +38,7 @@
 }
 
 void IPConfig::RegisterUpdateCallback(
-    Callback2<IPConfigRefPtr, bool>::Type *callback) {
+    Callback2<const IPConfigRefPtr&, bool>::Type *callback) {
   update_callback_.reset(callback);
 }
 
diff --git a/ipconfig.h b/ipconfig.h
index 6708b12..f6ac640 100644
--- a/ipconfig.h
+++ b/ipconfig.h
@@ -13,12 +13,10 @@
 #include <base/memory/scoped_ptr.h>
 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
 
+#include "shill/refptr_types.h"
+
 namespace shill {
 
-class IPConfig;
-
-typedef scoped_refptr<IPConfig> IPConfigRefPtr;
-
 // IPConfig superclass. Individual IP configuration types will inherit from this
 // class.
 class IPConfig : public base::RefCounted<IPConfig> {
@@ -56,7 +54,8 @@
   // configuration instance allowing clients to more easily manage multiple IP
   // configurations. The callback's second argument is set to false if IP
   // configuration failed.
-  void RegisterUpdateCallback(Callback2<IPConfigRefPtr, bool>::Type *callback);
+  void RegisterUpdateCallback(
+      Callback2<const IPConfigRefPtr&, bool>::Type *callback);
 
   const Properties &properties() const { return properties_; }
 
@@ -80,7 +79,7 @@
 
   const std::string device_name_;
   Properties properties_;
-  scoped_ptr<Callback2<IPConfigRefPtr, bool>::Type> update_callback_;
+  scoped_ptr<Callback2<const IPConfigRefPtr&, bool>::Type> update_callback_;
 
   DISALLOW_COPY_AND_ASSIGN(IPConfig);
 };
diff --git a/ipconfig_unittest.cc b/ipconfig_unittest.cc
index b20cfc1..6e71b2d 100644
--- a/ipconfig_unittest.cc
+++ b/ipconfig_unittest.cc
@@ -70,12 +70,12 @@
 
 class UpdateCallbackTest {
  public:
-  UpdateCallbackTest(IPConfigRefPtr ipconfig, bool success)
+  UpdateCallbackTest(const IPConfigRefPtr &ipconfig, bool success)
       : ipconfig_(ipconfig),
         success_(success),
         called_(false) {}
 
-  void Callback(IPConfigRefPtr ipconfig, bool success) {
+  void Callback(const IPConfigRefPtr &ipconfig, bool success) {
     called_ = true;
     EXPECT_EQ(ipconfig_.get(), ipconfig.get());
     EXPECT_EQ(success_, success);
diff --git a/manager.cc b/manager.cc
index 461ac1c..be39401 100644
--- a/manager.cc
+++ b/manager.cc
@@ -80,7 +80,7 @@
   adaptor_->UpdateRunning();
 }
 
-void Manager::RegisterDevice(DeviceRefPtr to_manage) {
+void Manager::RegisterDevice(const DeviceRefPtr &to_manage) {
   vector<DeviceRefPtr>::iterator it;
   for (it = devices_.begin(); it != devices_.end(); ++it) {
     if (to_manage.get() == it->get())
@@ -93,7 +93,7 @@
     to_manage->Start();
 }
 
-void Manager::DeregisterDevice(DeviceConstRefPtr to_forget) {
+void Manager::DeregisterDevice(const DeviceConstRefPtr &to_forget) {
   vector<DeviceRefPtr>::iterator it;
   for (it = devices_.begin(); it != devices_.end(); ++it) {
     if (to_forget.get() == it->get()) {
@@ -103,7 +103,7 @@
   }
 }
 
-void Manager::RegisterService(ServiceRefPtr to_manage) {
+void Manager::RegisterService(const ServiceRefPtr &to_manage) {
   vector<ServiceRefPtr>::iterator it;
   for (it = services_.begin(); it != services_.end(); ++it) {
     if (to_manage.get() == it->get())
@@ -112,7 +112,7 @@
   services_.push_back(to_manage);
 }
 
-void Manager::DeregisterService(ServiceConstRefPtr to_forget) {
+void Manager::DeregisterService(const ServiceConstRefPtr &to_forget) {
   vector<ServiceRefPtr>::iterator it;
   for (it = services_.begin(); it != services_.end(); ++it) {
     if (to_forget.get() == it->get()) {
diff --git a/manager.h b/manager.h
index bd1c82d..d18d300 100644
--- a/manager.h
+++ b/manager.h
@@ -33,11 +33,11 @@
   void Start();
   void Stop();
 
-  void RegisterDevice(DeviceRefPtr to_manage);
-  void DeregisterDevice(DeviceConstRefPtr to_forget);
+  void RegisterDevice(const DeviceRefPtr &to_manage);
+  void DeregisterDevice(const DeviceConstRefPtr &to_forget);
 
-  void RegisterService(ServiceRefPtr to_manage);
-  void DeregisterService(ServiceConstRefPtr to_forget);
+  void RegisterService(const ServiceRefPtr &to_manage);
+  void DeregisterService(const ServiceConstRefPtr &to_forget);
 
   void FilterByTechnology(Device::Technology tech,
                           std::vector<DeviceRefPtr> *found);
diff --git a/manager_unittest.cc b/manager_unittest.cc
index 75404ad..116ca5c 100644
--- a/manager_unittest.cc
+++ b/manager_unittest.cc
@@ -37,10 +37,10 @@
   ManagerTest() : factory_(this) {}
   virtual ~ManagerTest() {}
 
-  bool IsDeviceRegistered(Device *device, Device::Technology tech) {
+  bool IsDeviceRegistered(const DeviceRefPtr &device, Device::Technology tech) {
     vector<DeviceRefPtr> devices;
     manager_.FilterByTechnology(tech, &devices);
-    return (devices.size() == 1 && devices[0].get() == device);
+    return (devices.size() == 1 && devices[0].get() == device.get());
   }
 
  protected:
@@ -74,8 +74,8 @@
   manager_.RegisterDevice(mock_device.get());
   manager_.RegisterDevice(mock_device2.get());
 
-  EXPECT_TRUE(IsDeviceRegistered(mock_device.get(), Device::kEthernet));
-  EXPECT_TRUE(IsDeviceRegistered(mock_device2.get(), Device::kWifi));
+  EXPECT_TRUE(IsDeviceRegistered(mock_device, Device::kEthernet));
+  EXPECT_TRUE(IsDeviceRegistered(mock_device2, Device::kWifi));
 }
 
 TEST_F(ManagerTest, DeviceDeregistration) {
@@ -100,14 +100,14 @@
   manager_.RegisterDevice(mock_device.get());
   manager_.RegisterDevice(mock_device2.get());
 
-  ASSERT_TRUE(IsDeviceRegistered(mock_device.get(), Device::kEthernet));
-  ASSERT_TRUE(IsDeviceRegistered(mock_device2.get(), Device::kWifi));
+  ASSERT_TRUE(IsDeviceRegistered(mock_device, Device::kEthernet));
+  ASSERT_TRUE(IsDeviceRegistered(mock_device2, Device::kWifi));
 
   manager_.DeregisterDevice(mock_device.get());
-  EXPECT_FALSE(IsDeviceRegistered(mock_device.get(), Device::kEthernet));
+  EXPECT_FALSE(IsDeviceRegistered(mock_device, Device::kEthernet));
 
   manager_.DeregisterDevice(mock_device2.get());
-  EXPECT_FALSE(IsDeviceRegistered(mock_device2.get(), Device::kWifi));
+  EXPECT_FALSE(IsDeviceRegistered(mock_device2, Device::kWifi));
 }
 
 TEST_F(ManagerTest, ServiceRegistration) {
@@ -121,13 +121,11 @@
   scoped_refptr<MockService> mock_service(
       new NiceMock<MockService>(&control_interface_,
                                 &dispatcher_,
-                                device.get(),
                                 kService1));
 
   scoped_refptr<MockService> mock_service2(
       new NiceMock<MockService>(&control_interface_,
                                 &dispatcher_,
-                                device.get(),
                                 kService2));
 
   manager_.RegisterService(mock_service);
diff --git a/mock_service.cc b/mock_service.cc
index 7e8a5ce..a66802a 100644
--- a/mock_service.cc
+++ b/mock_service.cc
@@ -20,9 +20,8 @@
 
 MockService::MockService(ControlInterface *control_interface,
                          EventDispatcher *dispatcher,
-                         DeviceConfigInterfaceRefPtr device,
                          const std::string& name)
-    : Service(control_interface, dispatcher, device, name) {
+    : Service(control_interface, dispatcher, name) {
 }
 
 MockService::~MockService() {}
diff --git a/mock_service.h b/mock_service.h
index 78e766e..f418b81 100644
--- a/mock_service.h
+++ b/mock_service.h
@@ -20,7 +20,6 @@
   // A constructor for the Service object
   MockService(ControlInterface *control_interface,
               EventDispatcher *dispatcher,
-              DeviceConfigInterfaceRefPtr interface,
               const std::string& name);
   virtual ~MockService();
 
diff --git a/refptr_types.h b/refptr_types.h
new file mode 100644
index 0000000..fa03e6e
--- /dev/null
+++ b/refptr_types.h
@@ -0,0 +1,64 @@
+// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SHILL_REFPTR_TYPES_
+#define SHILL_REFPTR_TYPES_
+
+#include <base/memory/ref_counted.h>
+
+namespace shill {
+
+// Device and derived classes.
+class Device;
+typedef scoped_refptr<const Device> DeviceConstRefPtr;
+typedef scoped_refptr<Device> DeviceRefPtr;
+
+class Cellular;
+typedef scoped_refptr<const Cellular> CellularConstRefPtr;
+typedef scoped_refptr<Cellular> CellularRefPtr;
+
+class Ethernet;
+typedef scoped_refptr<const Ethernet> EthernetConstRefPtr;
+typedef scoped_refptr<Ethernet> EthernetRefPtr;
+
+class WiFi;
+typedef scoped_refptr<const WiFi> WiFiConstRefPtr;
+typedef scoped_refptr<WiFi> WiFiRefPtr;
+
+// Endpoint and derived classes.
+class Endpoint;
+typedef scoped_refptr<const Endpoint> EndpointConstRefPtr;
+typedef scoped_refptr<Endpoint> EndpointRefPtr;
+
+class WiFiEndpoint;
+typedef scoped_refptr<const WiFiEndpoint> WiFiEndpointConstRefPtr;
+typedef scoped_refptr<WiFiEndpoint> WiFiEndpointRefPtr;
+
+// Service and derived classes.
+class Service;
+typedef scoped_refptr<const Service> ServiceConstRefPtr;
+typedef scoped_refptr<Service> ServiceRefPtr;
+
+class CellularService;
+typedef scoped_refptr<const CellularService> CellularServiceConstRefPtr;
+typedef scoped_refptr<CellularService> CellularServiceRefPtr;
+
+class EthernetService;
+typedef scoped_refptr<const EthernetService> EthernetServiceConstRefPtr;
+typedef scoped_refptr<EthernetService> EthernetServiceRefPtr;
+
+class WiFiService;
+typedef scoped_refptr<const WiFiService> WiFiServiceConstRefPtr;
+typedef scoped_refptr<WiFiService> WiFiServiceRefPtr;
+
+// IPConfig and derived classes.
+class IPConfig;
+typedef scoped_refptr<IPConfig> IPConfigRefPtr;
+
+class DHCPConfig;
+typedef scoped_refptr<DHCPConfig> DHCPConfigRefPtr;
+
+}  // namespace shill
+
+#endif  // SHILL_REFPTR_TYPES_
diff --git a/service.cc b/service.cc
index 8d8b51b..247a466 100644
--- a/service.cc
+++ b/service.cc
@@ -15,7 +15,6 @@
 #include <chromeos/dbus/service_constants.h>
 
 #include "shill/control_interface.h"
-#include "shill/device_config_interface.h"
 #include "shill/error.h"
 #include "shill/property_accessor.h"
 #include "shill/service.h"
@@ -28,7 +27,6 @@
 namespace shill {
 Service::Service(ControlInterface *control_interface,
                  EventDispatcher *dispatcher,
-                 DeviceConfigInterfaceRefPtr config_interface,
                  const string& name)
     : auto_connect_(false),
       connectable_(false),
@@ -41,7 +39,6 @@
       configured_(false),
       configuration_(NULL),
       connection_(NULL),
-      config_interface_(config_interface),
       adaptor_(control_interface->CreateServiceAdaptor(this)) {
 
   RegisterBool(flimflam::kAutoConnectProperty, &auto_connect_);
diff --git a/service.h b/service.h
index b38faa8..28c5c68 100644
--- a/service.h
+++ b/service.h
@@ -13,7 +13,6 @@
 #include <base/memory/scoped_ptr.h>
 
 #include "shill/accessor_interface.h"
-#include "shill/device_config_interface.h"
 #include "shill/property_store.h"
 
 namespace shill {
@@ -24,12 +23,8 @@
 class Endpoint;
 class Error;
 class EventDispatcher;
-class Service;
 class ServiceAdaptorInterface;
 
-typedef scoped_refptr<const Service> ServiceConstRefPtr;
-typedef scoped_refptr<Service> ServiceRefPtr;
-
 class Service : public base::RefCounted<Service>,
                 public PropertyStore {
  public:
@@ -76,7 +71,6 @@
   // A constructor for the Service object
   Service(ControlInterface *control_interface,
           EventDispatcher *dispatcher,
-          DeviceConfigInterfaceRefPtr config_interface,
           const std::string& name);
   virtual ~Service();
   virtual void Connect() = 0;
@@ -142,8 +136,6 @@
   bool configured_;
   Configuration *configuration_;
   Connection *connection_;
-  DeviceConfigInterfaceRefPtr config_interface_;
-  Endpoint *endpoint_;
   scoped_ptr<ServiceAdaptorInterface> adaptor_;
 
   friend class ServiceAdaptorInterface;
diff --git a/service_unittest.cc b/service_unittest.cc
index 3c79005..974156c 100644
--- a/service_unittest.cc
+++ b/service_unittest.cc
@@ -17,7 +17,6 @@
 #include "shill/ethernet_service.h"
 #include "shill/manager.h"
 #include "shill/mock_control.h"
-#include "shill/mock_device.h"
 #include "shill/mock_service.h"
 #include "shill/property_store_unittest.h"
 #include "shill/shill_event.h"
@@ -38,11 +37,6 @@
   ServiceTest()
       : service_(new MockService(&control_interface_,
                                  &dispatcher_,
-                                 new MockDevice(&control_interface_,
-                                                &dispatcher_,
-                                                &manager_,
-                                                "mock-device",
-                                                0),
                                  "mock-service")) {
   }
 
diff --git a/wifi.cc b/wifi.cc
index b92f2a9..1c968b0 100644
--- a/wifi.cc
+++ b/wifi.cc
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+#include "shill/wifi.h"
+
 #include <time.h>
 #include <stdio.h>
 #include <string.h>
@@ -20,8 +22,6 @@
 #include "shill/wifi_endpoint.h"
 #include "shill/wifi_service.h"
 
-#include "shill/wifi.h"
-
 using std::string;
 
 namespace shill {
@@ -59,16 +59,16 @@
 }
 
 WiFi::SupplicantInterfaceProxy::SupplicantInterfaceProxy(
-    WiFi *wifi,
+    const WiFiRefPtr &wifi,
     DBus::Connection *bus,
     const ::DBus::Path &object_path)
-    : wifi_(*wifi),
+    : wifi_(wifi),
       DBus::ObjectProxy(*bus, object_path, kSupplicantDBusAddr) {}
 
 void WiFi::SupplicantInterfaceProxy::ScanDone(const bool& success) {
   LOG(INFO) << __func__ << " " << success;
   if (success) {
-    wifi_.ScanDone();
+    wifi_->ScanDone();
   }
 }
 
@@ -76,7 +76,7 @@
     const ::DBus::Path &BSS,
     const std::map<string, ::DBus::Variant> &properties) {
   LOG(INFO) << __func__;
-  wifi_.BSSAdded(BSS, properties);
+  wifi_->BSSAdded(BSS, properties);
 }
 
 void WiFi::SupplicantInterfaceProxy::BSSRemoved(const ::DBus::Path &BSS) {
@@ -205,6 +205,8 @@
 void WiFi::Stop() {
   LOG(INFO) << __func__;
   // XXX
+  supplicant_interface_proxy_.reset(NULL);
+  supplicant_process_proxy_.reset(NULL);
   Device::Stop();
 }
 
diff --git a/wifi.h b/wifi.h
index 3d2f2d7..21a66cc 100644
--- a/wifi.h
+++ b/wifi.h
@@ -10,16 +10,14 @@
 #include <vector>
 
 #include "shill/device.h"
+#include "shill/refptr_types.h"
 #include "shill/shill_event.h"
 #include "shill/supplicant-process.h"
 #include "shill/supplicant-interface.h"
 
 namespace shill {
 
-class WiFiEndpoint;
 class WiFiService;
-typedef scoped_refptr<const WiFiEndpoint> WiFiEndpointConstRefPtr;
-typedef scoped_refptr<WiFiEndpoint> WiFiEndpointRefPtr;
 
 // WiFi class. Specialization of Device for WiFi.
 class WiFi : public Device {
@@ -72,7 +70,8 @@
       private ::DBus::ObjectProxy  // used by dbus-c++, not WiFi
   {
    public:
-    SupplicantInterfaceProxy(WiFi *wifi, DBus::Connection *bus,
+    SupplicantInterfaceProxy(const WiFiRefPtr &wifi,
+                             DBus::Connection *bus,
                              const ::DBus::Path &object_path);
 
    private:
@@ -94,7 +93,7 @@
     virtual void PropertiesChanged(const std::map<std::string, ::DBus::Variant>
                                    &properties);
 
-    WiFi &wifi_;
+    WiFiRefPtr wifi_;
   };
 
   typedef std::map<const std::string, WiFiEndpointRefPtr> EndpointMap;
diff --git a/wifi_endpoint.h b/wifi_endpoint.h
index 8a7e15a..260e6a7 100644
--- a/wifi_endpoint.h
+++ b/wifi_endpoint.h
@@ -13,7 +13,6 @@
 #include <dbus-c++/dbus.h>
 
 #include "shill/endpoint.h"
-#include "shill/service.h"
 #include "shill/shill_event.h"
 
 namespace shill {
diff --git a/wifi_service.cc b/wifi_service.cc
index bf3d830..260245f 100644
--- a/wifi_service.cc
+++ b/wifi_service.cc
@@ -20,12 +20,12 @@
 
 WiFiService::WiFiService(ControlInterface *control_interface,
                          EventDispatcher *dispatcher,
-                         WiFi *device,
+                         const WiFiRefPtr &device,
                          const std::vector<uint8_t> ssid,
                          uint32_t mode,
                          const std::string &key_management,
                          const std::string &name)
-    : Service(control_interface, dispatcher, device, name),
+    : Service(control_interface, dispatcher, name),
       task_factory_(this),
       wifi_(device),
       ssid_(ssid),
diff --git a/wifi_service.h b/wifi_service.h
index 2b3e5aa..a5a5c58 100644
--- a/wifi_service.h
+++ b/wifi_service.h
@@ -8,9 +8,9 @@
 #include <string>
 #include <vector>
 
-#include "shill/wifi.h"
-#include "shill/shill_event.h"
+#include "shill/refptr_types.h"
 #include "shill/service.h"
+#include "shill/shill_event.h"
 #include "shill/supplicant-interface.h"
 
 namespace shill {
@@ -19,7 +19,7 @@
  public:
   WiFiService(ControlInterface *control_interface,
               EventDispatcher *dispatcher,
-              WiFi *device,
+              const WiFiRefPtr &device,
               const std::vector<uint8_t> ssid,
               uint32_t mode,
               const std::string &key_management,
@@ -52,7 +52,7 @@
   uint16 hex_ssid_;
 
   ScopedRunnableMethodFactory<WiFiService> task_factory_;
-  WiFi *wifi_;
+  WiFiRefPtr wifi_;
   const std::vector<uint8_t> ssid_;
   const uint32_t mode_;
   DISALLOW_COPY_AND_ASSIGN(WiFiService);