[shill] Start replacing bare pointers with scoped_refptrs where appropriate

BUG=chromium-os:16259
TEST=unit tests

Change-Id: I5e1531b726bbb40917d25dc3ae44da9a6a420e00
Reviewed-on: http://gerrit.chromium.org/gerrit/2236
Tested-by: Chris Masone <cmasone@chromium.org>
Reviewed-by: Darin Petkov <petkov@chromium.org>
diff --git a/device.h b/device.h
index 1be39ad..e8105b6 100644
--- a/device.h
+++ b/device.h
@@ -11,21 +11,26 @@
 #include <base/memory/ref_counted.h>
 #include <base/memory/scoped_ptr.h>
 
+#include "shill/device_config_interface.h"
 #include "shill/service.h"
 #include "shill/shill_event.h"
 
 namespace shill {
 
 class ControlInterface;
+class Device;
 class DeviceAdaptorInterface;
+class DeviceInfo;
 class EventDispatcher;
 class Endpoint;
-class DeviceInfo;
 class Manager;
 
+typedef scoped_refptr<const Device> DeviceConstRefPtr;
+typedef scoped_refptr<Device> DeviceRefPtr;
+
 // Device superclass.  Individual network interfaces types will inherit from
 // this class.
-class Device : public base::RefCounted<Device> {
+class Device : public DeviceConfigInterface {
  public:
   enum Technology {
     kEthernet,
@@ -51,12 +56,15 @@
   virtual void LinkEvent(unsigned flags, unsigned change);
   virtual void Scan();
 
+  // Implementation of DeviceConfigInterface
+  virtual void ConfigIP() {}
+
   // Returns a string that is guaranteed to uniquely identify this
   // Device instance.
   const std::string& UniqueName() const;
 
  protected:
-  std::vector<scoped_refptr<Service> > services_;
+  std::vector<ServiceRefPtr> services_;
   std::string link_name_;
   int interface_index_;
   bool running_;
@@ -64,7 +72,6 @@
 
  private:
   scoped_ptr<DeviceAdaptorInterface> adaptor_;
-  friend class base::RefCounted<Device>;
   friend class DeviceAdaptorInterface;
   DISALLOW_COPY_AND_ASSIGN(Device);
 };
diff --git a/device_config_interface.h b/device_config_interface.h
new file mode 100644
index 0000000..5031e71
--- /dev/null
+++ b/device_config_interface.h
@@ -0,0 +1,32 @@
+// 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/device_dbus_adaptor.cc b/device_dbus_adaptor.cc
index 9334278..adade7d 100644
--- a/device_dbus_adaptor.cc
+++ b/device_dbus_adaptor.cc
@@ -20,7 +20,8 @@
 // static
 const char DeviceDBusAdaptor::kPath[] = "/device/";
 
-DeviceDBusAdaptor::DeviceDBusAdaptor(DBus::Connection* conn, Device *device)
+DeviceDBusAdaptor::DeviceDBusAdaptor(DBus::Connection* conn,
+                                     DeviceRefPtr device)
     : DBusAdaptor(conn, kPath + device->UniqueName()),
       device_(device) {
 }
diff --git a/device_dbus_adaptor.h b/device_dbus_adaptor.h
index fd23a80..1e1b522 100644
--- a/device_dbus_adaptor.h
+++ b/device_dbus_adaptor.h
@@ -12,10 +12,10 @@
 
 #include "shill/adaptor_interfaces.h"
 #include "shill/dbus_adaptor.h"
+#include "shill/device.h"
 #include "shill/flimflam-device.h"
 
 namespace shill {
-class Device;
 
 // Subclass of DBusAdaptor for Device objects
 class DeviceDBusAdaptor : public org::chromium::flimflam::Device_adaptor,
@@ -25,7 +25,7 @@
   static const char kInterfaceName[];
   static const char kPath[];
 
-  DeviceDBusAdaptor(DBus::Connection* conn, Device *device);
+  DeviceDBusAdaptor(DBus::Connection* conn, DeviceRefPtr device);
   virtual ~DeviceDBusAdaptor();
 
   // Implementation of DeviceAdaptorInterface.
@@ -44,7 +44,7 @@
   ::DBus::Path AddIPConfig(const std::string& , ::DBus::Error &error);
 
  private:
-  Device *device_;
+  DeviceRefPtr device_;
   DISALLOW_COPY_AND_ASSIGN(DeviceDBusAdaptor);
 };
 
diff --git a/device_info.cc b/device_info.cc
index a804cd5..1495104 100644
--- a/device_info.cc
+++ b/device_info.cc
@@ -119,14 +119,14 @@
 
 void DeviceInfo::AddLinkMsgHandler(struct nlmsghdr *hdr) {
   struct ifinfomsg *msg = reinterpret_cast<struct ifinfomsg *>(NLMSG_DATA(hdr));
-  base::hash_map<int, scoped_refptr<Device> >::iterator ndev =
+  base::hash_map<int, DeviceRefPtr>::iterator ndev =
       devices_.find(msg->ifi_index);
   int bytes = IFLA_PAYLOAD(hdr);
   int dev_index = msg->ifi_index;
   struct rtattr *rta;
   int rta_bytes;
   char *link_name = NULL;
-  scoped_refptr<Device> device;
+  DeviceRefPtr device;
   Device::Technology technology;
   bool is_stub = false;
 
@@ -177,10 +177,10 @@
 
 void DeviceInfo::DelLinkMsgHandler(struct nlmsghdr *hdr) {
   struct ifinfomsg *msg = reinterpret_cast<struct ifinfomsg *>(NLMSG_DATA(hdr));
-  base::hash_map<int, scoped_refptr<Device> >::iterator ndev =
+  base::hash_map<int, DeviceRefPtr>::iterator ndev =
       devices_.find(msg->ifi_index);
   int dev_index = msg->ifi_index;
-  scoped_refptr<Device> device;
+  DeviceRefPtr device;
 
   if (ndev != devices_.end()) {
     device = ndev->second;
diff --git a/device_info.h b/device_info.h
index 9b5a030..d28a398 100644
--- a/device_info.h
+++ b/device_info.h
@@ -43,7 +43,7 @@
   ControlInterface *control_interface_;
   EventDispatcher *dispatcher_;
   Manager *manager_;
-  base::hash_map<int, scoped_refptr<Device> > devices_;
+  base::hash_map<int, DeviceRefPtr> devices_;
   scoped_ptr<Callback1<struct nlmsghdr *>::Type> link_callback_;
   scoped_ptr<RTNLListener> link_listener_;
   friend class DeviceInfoTest;
diff --git a/device_info_unittest.cc b/device_info_unittest.cc
index 74dea95..e66cd30 100644
--- a/device_info_unittest.cc
+++ b/device_info_unittest.cc
@@ -27,7 +27,7 @@
         device_info_(&control_interface_, &dispatcher_, &manager_),
         factory_(this) {
   }
-  base::hash_map<int, scoped_refptr<Device> > *DeviceInfoDevices() {
+  base::hash_map<int, DeviceRefPtr> *DeviceInfoDevices() {
     return &device_info_.devices_;
   }
  protected:
@@ -46,7 +46,7 @@
   RTNLHandler::GetInstance()->Start(&dispatcher_);
 
   // Peek in at the map of devices
-  base::hash_map<int, scoped_refptr<Device> > *device_map = DeviceInfoDevices();
+  base::hash_map<int, DeviceRefPtr> *device_map = DeviceInfoDevices();
 
   // Crank the glib main loop a few times
   for (int main_loop_count = 0;
@@ -70,7 +70,7 @@
   device_info_.Start();
 
   // Peek in at the map of devices
-  base::hash_map<int, scoped_refptr<Device> > *device_map = DeviceInfoDevices();
+  base::hash_map<int, DeviceRefPtr> *device_map = DeviceInfoDevices();
 
   // Crank the glib main loop a few times
   for (int main_loop_count = 0;
diff --git a/dhcp_config.cc b/dhcp_config.cc
index 14bc9bc..9ca6eed 100644
--- a/dhcp_config.cc
+++ b/dhcp_config.cc
@@ -28,7 +28,7 @@
 const char DHCPConfig::kDHCPCDPath[] = "/sbin/dhcpcd";
 
 
-DHCPConfig::DHCPConfig(DHCPProvider *provider, const Device &device)
+DHCPConfig::DHCPConfig(DHCPProvider *provider, DeviceConstRefPtr device)
     : IPConfig(device),
       provider_(provider),
       pid_(0) {
diff --git a/dhcp_config.h b/dhcp_config.h
index 0707774..a70d634 100644
--- a/dhcp_config.h
+++ b/dhcp_config.h
@@ -9,6 +9,7 @@
 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
 #include <dbus-c++/connection.h>
 
+#include "shill/device.h"
 #include "shill/ipconfig.h"
 
 namespace shill {
@@ -32,7 +33,7 @@
   static const char kConfigurationKeyRouters[];
   static const char kConfigurationKeySubnetCIDR[];
 
-  DHCPConfig(DHCPProvider *provider, const Device &device);
+  DHCPConfig(DHCPProvider *provider, DeviceConstRefPtr device);
   virtual ~DHCPConfig();
 
   // Inherited from IPConfig.
diff --git a/dhcp_config_unittest.cc b/dhcp_config_unittest.cc
index aa043e5..117f23c 100644
--- a/dhcp_config_unittest.cc
+++ b/dhcp_config_unittest.cc
@@ -23,7 +23,7 @@
 };
 
 TEST_F(DHCPConfigTest, GetIPv4AddressString) {
-  DHCPConfigRefPtr config(new DHCPConfig(NULL, *device_));
+  DHCPConfigRefPtr config(new DHCPConfig(NULL, device_));
   EXPECT_EQ("255.255.255.255", config->GetIPv4AddressString(0xffffffff));
   EXPECT_EQ("0.0.0.0", config->GetIPv4AddressString(0));
   EXPECT_EQ("1.2.3.4", config->GetIPv4AddressString(0x04030201));
@@ -69,7 +69,7 @@
   conf[DHCPConfig::kConfigurationKeyMTU].writer().append_uint16(600);
   conf["UnknownKey"] = DBus::Variant();
 
-  DHCPConfigRefPtr config(new DHCPConfig(NULL, *device_));
+  DHCPConfigRefPtr config(new DHCPConfig(NULL, device_));
   IPConfig::Properties properties;
   ASSERT_TRUE(config->ParseConfiguration(conf, &properties));
   EXPECT_EQ("4.3.2.1", properties.address);
diff --git a/dhcp_provider.cc b/dhcp_provider.cc
index 35f9ca1..e082d5e 100644
--- a/dhcp_provider.cc
+++ b/dhcp_provider.cc
@@ -27,7 +27,7 @@
   listener_.reset(new DHCPCDListener(this, connection));
 }
 
-DHCPConfigRefPtr DHCPProvider::CreateConfig(const Device &device) {
+DHCPConfigRefPtr DHCPProvider::CreateConfig(DeviceConstRefPtr device) {
   VLOG(2) << __func__;
   return DHCPConfigRefPtr(new DHCPConfig(this, device));
 }
diff --git a/dhcp_provider.h b/dhcp_provider.h
index f7ac4f4..06680fd 100644
--- a/dhcp_provider.h
+++ b/dhcp_provider.h
@@ -11,6 +11,7 @@
 #include <base/memory/singleton.h>
 #include <dbus-c++/connection.h>
 
+#include "shill/device.h"
 #include "shill/dhcp_config.h"
 
 namespace shill {
@@ -37,7 +38,7 @@
   // Creates a new DHCPConfig for |device|. The DHCP configuration for the
   // device can then be initiated through DHCPConfig::Request and
   // DHCPConfig::Renew.
-  DHCPConfigRefPtr CreateConfig(const Device &device);
+  DHCPConfigRefPtr CreateConfig(DeviceConstRefPtr device);
 
   // Returns the DHCP configuration associated with DHCP client |pid|. Return
   // NULL if |pid| is not bound to a configuration.
diff --git a/ethernet.h b/ethernet.h
index 60ea5db..09e0ee5 100644
--- a/ethernet.h
+++ b/ethernet.h
@@ -29,7 +29,7 @@
 
  private:
   bool service_registered_;
-  scoped_refptr<EthernetService> service_;
+  ServiceRefPtr service_;
   DISALLOW_COPY_AND_ASSIGN(Ethernet);
 };
 
diff --git a/glib_io_handler.h b/glib_io_handler.h
index 32c0aae..e31ba12 100644
--- a/glib_io_handler.h
+++ b/glib_io_handler.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
+// 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.
 
diff --git a/io_handler.h b/io_handler.h
index 88f812b..f6fae45 100644
--- a/io_handler.h
+++ b/io_handler.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
+// 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.
 
diff --git a/ipconfig.cc b/ipconfig.cc
index 092e279..b386c46 100644
--- a/ipconfig.cc
+++ b/ipconfig.cc
@@ -12,7 +12,7 @@
 
 namespace shill {
 
-IPConfig::IPConfig(const Device &device) : device_(device) {
+IPConfig::IPConfig(DeviceConstRefPtr device) : device_(device) {
   VLOG(2) << "IPConfig created.";
 }
 
@@ -21,7 +21,7 @@
 }
 
 const string &IPConfig::GetDeviceName() const {
-  return device().UniqueName();
+  return device()->UniqueName();
 }
 
 bool IPConfig::Request() {
diff --git a/ipconfig.h b/ipconfig.h
index bf860d6..78ace70 100644
--- a/ipconfig.h
+++ b/ipconfig.h
@@ -10,9 +10,9 @@
 
 #include <base/memory/ref_counted.h>
 
-namespace shill {
+#include "shill/device.h"
 
-class Device;
+namespace shill {
 
 // IPConfig superclass. Individual IP configuration types will inherit from this
 // class.
@@ -31,10 +31,10 @@
     int mtu;
   };
 
-  explicit IPConfig(const Device &device);
+  explicit IPConfig(DeviceConstRefPtr device);
   virtual ~IPConfig();
 
-  const Device &device() const { return device_; }
+  DeviceConstRefPtr device() const { return device_; }
   const std::string &GetDeviceName() const;
 
   // Updates the IP configuration properties and notifies registered listeners
@@ -50,7 +50,7 @@
   virtual bool Renew();
 
  private:
-  const Device &device_;
+  DeviceConstRefPtr device_;
   Properties properties_;
 
   DISALLOW_COPY_AND_ASSIGN(IPConfig);
diff --git a/ipconfig_unittest.cc b/ipconfig_unittest.cc
index 3906c08..e6854b1 100644
--- a/ipconfig_unittest.cc
+++ b/ipconfig_unittest.cc
@@ -16,11 +16,11 @@
 
  protected:
   MockControl control_interface_;
-  scoped_refptr<MockDevice> device_;
+  scoped_refptr<const MockDevice> device_;
 };
 
 TEST_F(IPConfigTest, GetDeviceNameTest) {
-  scoped_refptr<IPConfig> ipconfig(new IPConfig(*device_));
+  scoped_refptr<IPConfig> ipconfig(new IPConfig(device_));
   EXPECT_EQ("testname", ipconfig->GetDeviceName());
 }
 
diff --git a/manager.cc b/manager.cc
index 215c24e..df60b2b 100644
--- a/manager.cc
+++ b/manager.cc
@@ -46,42 +46,42 @@
   adaptor_->UpdateRunning();
 }
 
-void Manager::RegisterDevice(Device *to_manage) {
-  vector<scoped_refptr<Device> >::iterator it;
+void Manager::RegisterDevice(DeviceRefPtr to_manage) {
+  vector<DeviceRefPtr>::iterator it;
   for (it = devices_.begin(); it != devices_.end(); ++it) {
-    if (to_manage == it->get())
+    if (to_manage.get() == it->get())
       return;
   }
-  devices_.push_back(scoped_refptr<Device>(to_manage));
+  devices_.push_back(to_manage);
 
   // TODO(pstew): Should check configuration
   if (running_)
     to_manage->Start();
 }
 
-void Manager::DeregisterDevice(const Device *to_forget) {
-  vector<scoped_refptr<Device> >::iterator it;
+void Manager::DeregisterDevice(DeviceConstRefPtr to_forget) {
+  vector<DeviceRefPtr>::iterator it;
   for (it = devices_.begin(); it != devices_.end(); ++it) {
-    if (to_forget == it->get()) {
+    if (to_forget.get() == it->get()) {
       devices_.erase(it);
       return;
     }
   }
 }
 
-void Manager::RegisterService(Service *to_manage) {
-  vector<scoped_refptr<Service> >::iterator it;
+void Manager::RegisterService(ServiceRefPtr to_manage) {
+  vector<ServiceRefPtr>::iterator it;
   for (it = services_.begin(); it != services_.end(); ++it) {
-    if (to_manage == it->get())
+    if (to_manage.get() == it->get())
       return;
   }
-  services_.push_back(scoped_refptr<Service>(to_manage));
+  services_.push_back(to_manage);
 }
 
-void Manager::DeregisterService(const Service *to_forget) {
-  vector<scoped_refptr<Service> >::iterator it;
+void Manager::DeregisterService(ServiceConstRefPtr to_forget) {
+  vector<ServiceRefPtr>::iterator it;
   for (it = services_.begin(); it != services_.end(); ++it) {
-    if (to_forget == it->get()) {
+    if (to_forget.get() == it->get()) {
       services_.erase(it);
       return;
     }
@@ -89,9 +89,9 @@
 }
 
 void Manager::FilterByTechnology(Device::Technology tech,
-                                 vector<scoped_refptr<Device> > *found) {
+                                 vector<DeviceRefPtr> *found) {
   CHECK(found);
-  vector<scoped_refptr<Device> >::iterator it;
+  vector<DeviceRefPtr>::iterator it;
   for (it = devices_.begin(); it != devices_.end(); ++it) {
     if ((*it)->TechnologyIs(tech))
       found->push_back(*it);
@@ -99,7 +99,7 @@
 }
 
 Service* Manager::FindService(const std::string& name) {
-  vector<scoped_refptr<Service> >::iterator it;
+  vector<ServiceRefPtr>::iterator it;
   for (it = services_.begin(); it != services_.end(); ++it) {
     if (name == (*it)->UniqueName())
       return it->get();
diff --git a/manager.h b/manager.h
index fdd76e0..8b6ac60 100644
--- a/manager.h
+++ b/manager.h
@@ -31,14 +31,14 @@
   void Start();
   void Stop();
 
-  void RegisterDevice(Device *to_manage);
-  void DeregisterDevice(const Device *to_forget);
+  void RegisterDevice(DeviceRefPtr to_manage);
+  void DeregisterDevice(DeviceConstRefPtr to_forget);
 
-  void RegisterService(Service *to_manage);
-  void DeregisterService(const Service *to_forget);
+  void RegisterService(ServiceRefPtr to_manage);
+  void DeregisterService(ServiceConstRefPtr to_forget);
 
   void FilterByTechnology(Device::Technology tech,
-                          std::vector<scoped_refptr<Device> > *found);
+                          std::vector<DeviceRefPtr> *found);
 
   Service* FindService(const std::string& name);
 
@@ -46,8 +46,8 @@
   scoped_ptr<ManagerAdaptorInterface> adaptor_;
   DeviceInfo device_info_;
   bool running_;
-  std::vector<scoped_refptr<Device> > devices_;
-  std::vector<scoped_refptr<Service> > services_;
+  std::vector<DeviceRefPtr> devices_;
+  std::vector<ServiceRefPtr> services_;
   friend class ManagerAdaptorInterface;
 };
 
diff --git a/manager_unittest.cc b/manager_unittest.cc
index 18041af..97643cf 100644
--- a/manager_unittest.cc
+++ b/manager_unittest.cc
@@ -30,7 +30,7 @@
   }
 
   bool IsDeviceRegistered(Device *device, Device::Technology tech) {
-    vector<scoped_refptr<Device> > devices;
+    vector<DeviceRefPtr> devices;
     manager_.FilterByTechnology(tech, &devices);
     return (devices.size() == 1 && devices[0].get() == device);
   }
diff --git a/mock_service.cc b/mock_service.cc
index 651fe2b..b15a968 100644
--- a/mock_service.cc
+++ b/mock_service.cc
@@ -20,7 +20,7 @@
 
 MockService::MockService(ControlInterface *control_interface,
                          EventDispatcher *dispatcher,
-                         Device *device,
+                         DeviceConfigInterfaceRefPtr device,
                          const std::string& name)
     : Service(control_interface, dispatcher, device, string("mock-" + name)) {
 }
diff --git a/mock_service.h b/mock_service.h
index fd4960a..80cf1d8 100644
--- a/mock_service.h
+++ b/mock_service.h
@@ -20,7 +20,7 @@
   // A constructor for the Service object
   MockService(ControlInterface *control_interface,
               EventDispatcher *dispatcher,
-              Device *interface,
+              DeviceConfigInterfaceRefPtr interface,
               const std::string& name);
   virtual ~MockService();
 
diff --git a/service.cc b/service.cc
index d383c3b..19472f5 100644
--- a/service.cc
+++ b/service.cc
@@ -10,6 +10,7 @@
 #include <base/logging.h>
 
 #include "shill/control_interface.h"
+#include "shill/device_config_interface.h"
 #include "shill/service.h"
 #include "shill/service_dbus_adaptor.h"
 
@@ -18,7 +19,7 @@
 namespace shill {
 Service::Service(ControlInterface *control_interface,
                  EventDispatcher */* dispatcher */,
-                 Device *device,
+                 DeviceConfigInterfaceRefPtr config_interface,
                  const string& name)
     : name_(name),
       available_(false),
@@ -26,7 +27,7 @@
       auto_connect_(false),
       configuration_(NULL),
       connection_(NULL),
-      device_(device),
+      config_interface_(config_interface),
       adaptor_(control_interface->CreateServiceAdaptor(this)) {
   // Initialize Interface montior, so we can detect new interfaces
   VLOG(2) << "Service initialized.";
diff --git a/service.h b/service.h
index 24976fe..016ddc7 100644
--- a/service.h
+++ b/service.h
@@ -10,16 +10,21 @@
 #include <base/memory/ref_counted.h>
 #include <base/memory/scoped_ptr.h>
 
+#include "shill/device_config_interface.h"
+
 namespace shill {
 
 class Connection;
 class Configuration;
 class ControlInterface;
-class Device;
 class Endpoint;
 class EventDispatcher;
+class Service;
 class ServiceAdaptorInterface;
 
+typedef scoped_refptr<const Service> ServiceConstRefPtr;
+typedef scoped_refptr<Service> ServiceRefPtr;
+
 class Service : public base::RefCounted<Service> {
  public:
   enum ConnectFailure {
@@ -47,7 +52,7 @@
   // A constructor for the Service object
   Service(ControlInterface *control_interface,
           EventDispatcher *dispatcher,
-          Device *device,
+          DeviceConfigInterfaceRefPtr config_interface,
           const std::string& name);
   virtual ~Service();
   virtual void Connect() = 0;
@@ -64,7 +69,7 @@
   bool auto_connect_;
   Configuration *configuration_;
   Connection *connection_;
-  Device *device_;
+  DeviceConfigInterfaceRefPtr config_interface_;
   Endpoint *endpoint_;
   scoped_ptr<ServiceAdaptorInterface> adaptor_;
   friend class ServiceAdaptorInterface;
diff --git a/service_dbus_adaptor.cc b/service_dbus_adaptor.cc
index 2eea799..ab50e02 100644
--- a/service_dbus_adaptor.cc
+++ b/service_dbus_adaptor.cc
@@ -19,7 +19,8 @@
 // static
 const char ServiceDBusAdaptor::kPath[] = "/service/";
 
-ServiceDBusAdaptor::ServiceDBusAdaptor(DBus::Connection* conn, Service *service)
+ServiceDBusAdaptor::ServiceDBusAdaptor(DBus::Connection* conn,
+                                       ServiceRefPtr service)
     : DBusAdaptor(conn, kPath + service->UniqueName()),
       service_(service) {}
 ServiceDBusAdaptor::~ServiceDBusAdaptor() {}
diff --git a/service_dbus_adaptor.h b/service_dbus_adaptor.h
index e4fbb81..fe45f87 100644
--- a/service_dbus_adaptor.h
+++ b/service_dbus_adaptor.h
@@ -13,9 +13,9 @@
 #include "shill/adaptor_interfaces.h"
 #include "shill/dbus_adaptor.h"
 #include "shill/flimflam-service.h"
+#include "shill/service.h"
 
 namespace shill {
-class Service;
 
 // Subclass of DBusAdaptor for Service objects
 class ServiceDBusAdaptor : public org::chromium::flimflam::Service_adaptor,
@@ -25,7 +25,7 @@
   static const char kInterfaceName[];
   static const char kPath[];
 
-  ServiceDBusAdaptor(DBus::Connection* conn, Service *service);
+  ServiceDBusAdaptor(DBus::Connection* conn, ServiceRefPtr service);
   virtual ~ServiceDBusAdaptor();
 
   // Implementation of ServiceAdaptorInterface.
@@ -49,7 +49,7 @@
   void ActivateCellularModem(const std::string& , ::DBus::Error &error);
 
  private:
-  Service *service_;
+  ServiceRefPtr service_;
   DISALLOW_COPY_AND_ASSIGN(ServiceDBusAdaptor);
 };