shill: Manager: Take over responsibility for device save

Move the responsibility of saving devices from the default
profile to the Manager.  This removes an awkward setup where
the DefaultProfile asks for and iterates the Manager's device
list.  Instead, call UpdateDevice on each device on
Manager::Stop().

This has a positive side effect that newly created default
profiles (such as those created during autotests) do not
end up with device entries for all devices, but only the
ones persisted while the profile is in use.

This also allows the Manager to take charge of persisting
device state to disk while deregistering, so that the most
recent snapshot of the device is saved before it is removed
from memory.

BUG=chromium-os:31584
TEST=Expanded unit tests.

Change-Id: Id2eb916b0b33d038847248a88312fb6899b3f717
Reviewed-on: https://gerrit.chromium.org/gerrit/27299
Reviewed-by: Ben Chan <benchan@chromium.org>
Reviewed-by: Darin Petkov <petkov@chromium.org>
Commit-Ready: Paul Stewart <pstew@chromium.org>
Tested-by: Paul Stewart <pstew@chromium.org>
diff --git a/manager_unittest.cc b/manager_unittest.cc
index a867141..b881e17 100644
--- a/manager_unittest.cc
+++ b/manager_unittest.cc
@@ -292,11 +292,16 @@
   ASSERT_TRUE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
   ASSERT_TRUE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
 
+  MockProfile *profile = new MockProfile(control_interface(), manager(), "");
+  AdoptProfile(manager(), profile);  // Passes ownership.
+
   EXPECT_CALL(*mock_devices_[0].get(), SetEnabled(false));
+  EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[0])));
   manager()->DeregisterDevice(mock_devices_[0]);
   EXPECT_FALSE(IsDeviceRegistered(mock_devices_[0], Technology::kEthernet));
 
   EXPECT_CALL(*mock_devices_[1].get(), SetEnabled(false));
+  EXPECT_CALL(*profile, UpdateDevice(DeviceRefPtr(mock_devices_[1])));
   manager()->DeregisterDevice(mock_devices_[1]);
   EXPECT_FALSE(IsDeviceRegistered(mock_devices_[1], Technology::kWifi));
 }
@@ -1900,14 +1905,22 @@
   EXPECT_THAT(manager()->DefaultTechnology(&error), StrEq(expected_technology));
 }
 
-TEST_F(ManagerTest, DisconnectServicesOnStop) {
-  scoped_refptr<MockService> mock_service(
+TEST_F(ManagerTest, Stop) {
+  scoped_refptr<MockProfile> profile(
+      new NiceMock<MockProfile>(control_interface(), manager(), ""));
+  AdoptProfile(manager(), profile);
+  scoped_refptr<MockService> service(
       new NiceMock<MockService>(control_interface(),
                                 dispatcher(),
                                 metrics(),
                                 manager()));
-  manager()->RegisterService(mock_service);
-  EXPECT_CALL(*mock_service.get(), Disconnect(_)).Times(1);
+  manager()->RegisterService(service);
+  manager()->RegisterDevice(mock_devices_[0]);
+  EXPECT_CALL(*profile.get(),
+              UpdateDevice(DeviceRefPtr(mock_devices_[0].get())))
+      .WillOnce(Return(true));
+  EXPECT_CALL(*profile.get(), Save()).WillOnce(Return(true));
+  EXPECT_CALL(*service.get(), Disconnect(_)).Times(1);
   manager()->Stop();
 }
 
@@ -2339,5 +2352,4 @@
   EXPECT_TRUE(error.IsOngoing());
 }
 
-
 }  // namespace shill