Move away from Singleton

Move from Singleton to LazyInstance for one-of classes in Shill.
This will allow them to be subclassed (mocked).

BUG=chromium-os:18986
TEST=Re-run unit tests

Change-Id: I83320694db5df1a263332315ad4f6955b107280b
Reviewed-on: http://gerrit.chromium.org/gerrit/5676
Reviewed-by: Darin Petkov <petkov@chromium.org>
Tested-by: Paul Stewart <pstew@chromium.org>
diff --git a/dhcp_provider.cc b/dhcp_provider.cc
index b79a2b2..e632328 100644
--- a/dhcp_provider.cc
+++ b/dhcp_provider.cc
@@ -15,6 +15,9 @@
 
 namespace shill {
 
+static base::LazyInstance<DHCPProvider> g_dhcp_provider(
+    base::LINKER_INITIALIZED);
+
 DHCPProvider::DHCPProvider()
     : control_interface_(NULL),
       dispatcher_(NULL),
@@ -27,7 +30,7 @@
 }
 
 DHCPProvider* DHCPProvider::GetInstance() {
-  return Singleton<DHCPProvider>::get();
+  return g_dhcp_provider.Pointer();
 }
 
 void DHCPProvider::Init(ControlInterface *control_interface,
diff --git a/dhcp_provider.h b/dhcp_provider.h
index fd83029..5d96ee1 100644
--- a/dhcp_provider.h
+++ b/dhcp_provider.h
@@ -8,8 +8,8 @@
 #include <map>
 #include <string>
 
+#include <base/lazy_instance.h>
 #include <base/memory/scoped_ptr.h>
-#include <base/memory/singleton.h>
 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
 
 #include "shill/refptr_types.h"
@@ -29,6 +29,8 @@
 // DHCPProvider::GetInstance()->CreateConfig(device_name)->Request();
 class DHCPProvider {
  public:
+  virtual ~DHCPProvider();
+
   // This is a singleton -- use DHCPProvider::GetInstance()->Foo()
   static DHCPProvider *GetInstance();
 
@@ -57,8 +59,11 @@
   // destruction of the DHCP config instance if its reference count goes to 0.
   void UnbindPID(int pid);
 
+ protected:
+  DHCPProvider();
+
  private:
-  friend struct DefaultSingletonTraits<DHCPProvider>;
+  friend struct base::DefaultLazyInstanceTraits<DHCPProvider>;
   friend class DHCPProviderTest;
   friend class DeviceInfoTest;
   friend class DeviceTest;
@@ -66,10 +71,6 @@
 
   typedef std::map<int, DHCPConfigRefPtr> PIDConfigMap;
 
-  // Private to ensure that this behaves as a singleton.
-  DHCPProvider();
-  virtual ~DHCPProvider();
-
   // A single listener is used to catch signals from all DHCP clients and
   // dispatch them to the appropriate DHCP configuration instance.
   scoped_ptr<DHCPCDListener> listener_;
diff --git a/resolver.cc b/resolver.cc
index 5b4cf5b..16f503c 100644
--- a/resolver.cc
+++ b/resolver.cc
@@ -13,18 +13,21 @@
 
 #include "shill/ipconfig.h"
 
-namespace shill {
-
 using base::StringPrintf;
 using std::string;
 using std::vector;
 
+namespace shill {
+
+static base::LazyInstance<Resolver> g_resolver(
+    base::LINKER_INITIALIZED);
+
 Resolver::Resolver() {}
 
 Resolver::~Resolver() {}
 
 Resolver* Resolver::GetInstance() {
-  return Singleton<Resolver>::get();
+  return g_resolver.Pointer();
 }
 
 bool Resolver::SetDNS(const IPConfigRefPtr &ipconfig) {
diff --git a/resolver.h b/resolver.h
index bbf188f..34b43a7 100644
--- a/resolver.h
+++ b/resolver.h
@@ -6,8 +6,8 @@
 #define SHILL_RESOLVER_
 
 #include <base/file_path.h>
+#include <base/lazy_instance.h>
 #include <base/memory/ref_counted.h>
-#include <base/memory/singleton.h>
 
 #include "shill/refptr_types.h"
 
@@ -17,6 +17,8 @@
 // of an ipconfig into a "resolv.conf" formatted file.
 class Resolver {
  public:
+  virtual ~Resolver();
+
   // Since this is a singleton, use Resolver::GetInstance()->Foo()
   static Resolver *GetInstance();
 
@@ -28,13 +30,12 @@
   // Remove any created domain name service file
   bool ClearDNS();
 
- private:
-  friend struct DefaultSingletonTraits<Resolver>;
-  friend class ResolverTest;
-
-  // Don't allow this object to be created
+ protected:
   Resolver();
-  ~Resolver();
+
+ private:
+  friend struct base::DefaultLazyInstanceTraits<Resolver>;
+  friend class ResolverTest;
 
   FilePath path_;
 
diff --git a/routing_table.cc b/routing_table.cc
index 7b337d3..dc78f4b 100644
--- a/routing_table.cc
+++ b/routing_table.cc
@@ -38,8 +38,12 @@
 
 namespace shill {
 
+static base::LazyInstance<RoutingTable> g_routing_table(
+    base::LINKER_INITIALIZED);
+
 // static
 const char RoutingTable::kRouteFlushPath4[] = "/proc/sys/net/ipv4/route/flush";
+// static
 const char RoutingTable::kRouteFlushPath6[] = "/proc/sys/net/ipv6/route/flush";
 
 RoutingTable::RoutingTable()
@@ -51,7 +55,7 @@
 RoutingTable::~RoutingTable() {}
 
 RoutingTable* RoutingTable::GetInstance() {
-  return Singleton<RoutingTable>::get();
+  return g_routing_table.Pointer();
 }
 
 void RoutingTable::Start() {
diff --git a/routing_table.h b/routing_table.h
index f0a2cc9..4a09406 100644
--- a/routing_table.h
+++ b/routing_table.h
@@ -9,8 +9,8 @@
 
 #include <base/callback_old.h>
 #include <base/hash_tables.h>
+#include <base/lazy_instance.h>
 #include <base/memory/ref_counted.h>
-#include <base/memory/singleton.h>
 #include <base/memory/scoped_ptr.h>
 
 #include "shill/ip_address.h"
@@ -28,7 +28,8 @@
 // default route for an interface or modifying its metric (priority).
 class RoutingTable {
  public:
-  // Since this is a singleton, use RoutingTable::GetInstance()->Foo()
+  virtual ~RoutingTable();
+
   static RoutingTable *GetInstance();
 
   void Start();
@@ -56,13 +57,12 @@
   // Set the metric (priority) on existing default routes for an interface
   void SetDefaultMetric(int interface_index, uint32 metric);
 
- private:
-  friend struct DefaultSingletonTraits<RoutingTable>;
-  friend class RoutingTableTest;
-
-  // Constructor and destructor are private since this is a singleton
+ protected:
   RoutingTable();
-  ~RoutingTable();
+
+ private:
+  friend struct base::DefaultLazyInstanceTraits<RoutingTable>;
+  friend class RoutingTableTest;
 
   void RouteMsgHandler(const RTNLMessage &msg);
   bool ApplyRoute(uint32 interface_index,
diff --git a/rtnl_handler.cc b/rtnl_handler.cc
index 185275f..d9d8024 100644
--- a/rtnl_handler.cc
+++ b/rtnl_handler.cc
@@ -31,6 +31,9 @@
 
 namespace shill {
 
+static base::LazyInstance<RTNLHandler> g_rtnl_handler(
+    base::LINKER_INITIALIZED);
+
 RTNLHandler::RTNLHandler()
     : sockets_(NULL),
       in_request_(false),
@@ -47,7 +50,7 @@
 }
 
 RTNLHandler* RTNLHandler::GetInstance() {
-  return Singleton<RTNLHandler>::get();
+  return g_rtnl_handler.Pointer();
 }
 
 void RTNLHandler::Start(EventDispatcher *dispatcher, Sockets *sockets) {
diff --git a/rtnl_handler.h b/rtnl_handler.h
index 7f42d33..2dd119c 100644
--- a/rtnl_handler.h
+++ b/rtnl_handler.h
@@ -10,9 +10,9 @@
 
 #include <base/callback_old.h>
 #include <base/hash_tables.h>
+#include <base/lazy_instance.h>
 #include <base/memory/ref_counted.h>
 #include <base/memory/scoped_ptr.h>
-#include <base/memory/singleton.h>
 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
 
 #include "shill/device.h"
@@ -46,6 +46,8 @@
   static const int kRequestAddr6 = 8;
   static const int kRequestRoute6 = 16;
 
+  virtual ~RTNLHandler();
+
   // Since this is a singleton, use RTNHandler::GetInstance()->Foo()
   static RTNLHandler *GetInstance();
 
@@ -88,19 +90,19 @@
   // Send a formatted RTNL message.  The sequence number in the message is set.
   bool SendMessage(RTNLMessage *message);
 
+ protected:
+  RTNLHandler();
+
  private:
+  friend struct base::DefaultLazyInstanceTraits<RTNLHandler>;
   friend class DeviceInfoTest;
   friend class ModemTest;
   friend class RTNLHandlerTest;
   friend class RoutingTableTest;
-  friend struct DefaultSingletonTraits<RTNLHandler>;
+
   FRIEND_TEST(RTNLListenerTest, NoRun);
   FRIEND_TEST(RTNLListenerTest, Run);
 
-  // Private to ensure that this behaves as a singleton.
-  RTNLHandler();
-  virtual ~RTNLHandler();
-
   // This stops the event-monitoring function of the RTNL handler -- it is
   // private since it will never happen in normal running, but is useful for
   // tests.