Make Enable/Disable work using new callbacks for async support.

Use new-style callbacks to implement the Manager EnableTechnology
and DisableTechnology operations asynchronously. This allows
devices to be enabled and disabled from the UI ,and for the UI
to display available networks once the device is enabled.

Removed the behavior whereby setting the Device.Powered property
had the side effect of enabling or disabling the device. To
replace this, I added new Device.Enable and Device.Disable calls
for enabling and disabling individual devices.

Also separated the in-memory value of the Powered property from
the persisted value. Whenever a client requests that a device
be enabled or disabled, the desired power state is immediately
saved in the profile, but the in-memory value isn't updated until
the operation completes. On startup, shill now automatically
starts any devices for which the persistent Powered property
is set, and does not start devices for which it is not set.

BUG=chromium-os:23319,chromium-os:27814
TEST=Manual testing on device + unit tests passing.

Change-Id: Id676be3fc662cfd5efb730c67687edfd16b2dc6b
Reviewed-on: https://gerrit.chromium.org/gerrit/18123
Commit-Ready: Eric Shienbrood <ers@chromium.org>
Reviewed-by: Eric Shienbrood <ers@chromium.org>
Tested-by: Eric Shienbrood <ers@chromium.org>
diff --git a/device_unittest.cc b/device_unittest.cc
index 2f4dd17..de7a1e6 100644
--- a/device_unittest.cc
+++ b/device_unittest.cc
@@ -53,17 +53,36 @@
 
 namespace shill {
 
+class TestDevice : public Device {
+ public:
+  TestDevice(ControlInterface *control_interface,
+             EventDispatcher *dispatcher,
+             Metrics *metrics,
+             Manager *manager,
+             const std::string &link_name,
+             const std::string &address,
+             int interface_index,
+             Technology::Identifier technology)
+      : Device(control_interface, dispatcher, metrics, manager, link_name,
+               address, interface_index, technology) {}
+  ~TestDevice() {}
+  virtual void Start(Error *error,
+                     const EnabledStateChangedCallback &callback) {}
+  virtual void Stop(Error *error,
+                    const EnabledStateChangedCallback &callback) {}
+};
+
 class DeviceTest : public PropertyStoreTest {
  public:
   DeviceTest()
-      : device_(new Device(control_interface(),
-                           dispatcher(),
-                           NULL,
-                           manager(),
-                           kDeviceName,
-                           kDeviceAddress,
-                           kDeviceInterfaceIndex,
-                           Technology::kUnknown)),
+      : device_(new TestDevice(control_interface(),
+                               dispatcher(),
+                               NULL,
+                               manager(),
+                               kDeviceName,
+                               kDeviceAddress,
+                               kDeviceInterfaceIndex,
+                               Technology::kUnknown)),
         device_info_(control_interface(), NULL, NULL, NULL) {
     DHCPProvider::GetInstance()->glib_ = glib();
     DHCPProvider::GetInstance()->control_interface_ = control_interface();
@@ -113,33 +132,17 @@
 TEST_F(DeviceTest, GetProperties) {
   map<string, ::DBus::Variant> props;
   Error error(Error::kInvalidProperty, "");
-  {
-    ::DBus::Error dbus_error;
-    bool expected = true;
-    device_->mutable_store()->SetBoolProperty(flimflam::kPoweredProperty,
-                                              expected,
-                                              &error);
-    DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error);
-    ASSERT_FALSE(props.find(flimflam::kPoweredProperty) == props.end());
-    EXPECT_EQ(props[flimflam::kPoweredProperty].reader().get_bool(),
-              expected);
-  }
-  {
-    ::DBus::Error dbus_error;
-    DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error);
-    ASSERT_FALSE(props.find(flimflam::kNameProperty) == props.end());
-    EXPECT_EQ(props[flimflam::kNameProperty].reader().get_string(),
-              string(kDeviceName));
-  }
+  ::DBus::Error dbus_error;
+  DBusAdaptor::GetProperties(device_->store(), &props, &dbus_error);
+  ASSERT_FALSE(props.find(flimflam::kNameProperty) == props.end());
+  EXPECT_EQ(props[flimflam::kNameProperty].reader().get_string(),
+            string(kDeviceName));
 }
 
-TEST_F(DeviceTest, SetProperty) {
+// Note: there are currently no writeable Device properties that
+// aren't registered in a subclass.
+TEST_F(DeviceTest, SetReadOnlyProperty) {
   ::DBus::Error error;
-  EXPECT_TRUE(DBusAdaptor::SetProperty(device_->mutable_store(),
-                                       flimflam::kPoweredProperty,
-                                       PropertyStoreTest::kBoolV,
-                                       &error));
-
   // Ensure that an attempt to write a R/O property returns InvalidArgs error.
   EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(),
                                         flimflam::kAddressProperty,
@@ -148,22 +151,6 @@
   EXPECT_EQ(invalid_args(), error.name());
 }
 
-TEST_F(DeviceTest, ClearProperty) {
-  ::DBus::Error error;
-  EXPECT_TRUE(device_->powered());
-
-  EXPECT_TRUE(DBusAdaptor::SetProperty(device_->mutable_store(),
-                                       flimflam::kPoweredProperty,
-                                       PropertyStoreTest::kBoolV,
-                                       &error));
-  EXPECT_FALSE(device_->powered());
-
-  EXPECT_TRUE(DBusAdaptor::ClearProperty(device_->mutable_store(),
-                                         flimflam::kPoweredProperty,
-                                         &error));
-  EXPECT_TRUE(device_->powered());
-}
-
 TEST_F(DeviceTest, ClearReadOnlyProperty) {
   ::DBus::Error error;
   EXPECT_FALSE(DBusAdaptor::SetProperty(device_->mutable_store(),
@@ -311,10 +298,12 @@
 
 TEST_F(DeviceTest, Start) {
   EXPECT_CALL(routing_table_, FlushRoutes(kDeviceInterfaceIndex));
-  device_->Start();
+  device_->SetEnabled(true);
 }
 
 TEST_F(DeviceTest, Stop) {
+  device_->enabled_ = true;
+  device_->enabled_pending_ = true;
   device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName);
   scoped_refptr<MockService> service(
       new NiceMock<MockService>(&control_interface_,
@@ -327,8 +316,11 @@
       WillRepeatedly(Return(Service::kStateConnected));
   EXPECT_CALL(*dynamic_cast<DeviceMockAdaptor *>(device_->adaptor_.get()),
               UpdateEnabled());
+  EXPECT_CALL(*dynamic_cast<DeviceMockAdaptor *>(device_->adaptor_.get()),
+              EmitBoolChanged(flimflam::kPoweredProperty, false));
   EXPECT_CALL(rtnl_handler_, SetInterfaceFlags(_, 0, IFF_UP));
-  device_->Stop();
+  device_->SetEnabled(false);
+  device_->OnEnabledStateChanged(ResultCallback(), Error());
 
   EXPECT_FALSE(device_->ipconfig_.get());
   EXPECT_FALSE(device_->selected_service_.get());