Don't fall-back into AP mode after connecting to WiFi network

Probably the code was written with assumption that
On*PropertyChangeRegistration will be called asynchronously.
These function access data which will be initialized immediately after
RegisterPropertyChangedSignalHandler return.
This is not true with the current state of the code. Functions are being
called from RegisterPropertyChangedSignalHandler before data is read.
This effectively disabled most of OnPropertyChange logic. Probably the
logic of DBUS proxies was changed.

This fix initializes required data before calling
RegisterPropertyChangedSignalHandler.

Also fixed call to Init(), but seems does nothing important.

BUG:25531670
Change-Id: I7e1580c165d618b3f9bd763d06f909d9c35f87c3
diff --git a/buffet/shill_client.cc b/buffet/shill_client.cc
index 8ee5bca..654fe4f 100644
--- a/buffet/shill_client.cc
+++ b/buffet/shill_client.cc
@@ -115,6 +115,8 @@
                                      weak_factory_.GetWeakPtr());
   bus_->GetObjectProxy(shill::kFlimflamServiceName, ObjectPath{"/"})
       ->SetNameOwnerChangedCallback(owner_changed_cb);
+
+  Init();
 }
 
 ShillClient::~ShillClient() {}
@@ -266,6 +268,7 @@
   if (property_name != shill::kDevicesProperty) {
     return;
   }
+  bool update_connectivity = false;
   VLOG(3) << "Manager's device list has changed.";
   // We're going to remove every device we haven't seen in the update.
   set<ObjectPath> device_paths_to_remove;
@@ -288,21 +291,23 @@
     if (!IsMonitoredDevice(device.get())) {
       continue;
     }
-    device->RegisterPropertyChangedSignalHandler(
+    VLOG(3) << "Creating device proxy at " << device_path.value();
+    devices_[device_path].device = std::move(device);
+    update_connectivity = true;
+    devices_[device_path].device->RegisterPropertyChangedSignalHandler(
         base::Bind(&ShillClient::OnDevicePropertyChange,
                    weak_factory_.GetWeakPtr(), device_path),
         base::Bind(&ShillClient::OnDevicePropertyChangeRegistration,
                    weak_factory_.GetWeakPtr(), device_path));
-    VLOG(3) << "Creating device proxy at " << device_path.value();
-    devices_[device_path].device = std::move(device);
   }
   // Clean up devices/services related to removed devices.
-  if (!device_paths_to_remove.empty()) {
-    for (const ObjectPath& device_path : device_paths_to_remove) {
-      devices_.erase(device_path);
-    }
-    UpdateConnectivityState();
+  for (const ObjectPath& device_path : device_paths_to_remove) {
+    devices_.erase(device_path);
+    update_connectivity = true;
   }
+
+  if (update_connectivity)
+    UpdateConnectivityState();
 }
 
 void ShillClient::OnDevicePropertyChangeRegistration(
@@ -361,18 +366,17 @@
     device_state.service_state = Network::State::kOffline;
     removed_old_service = true;
   }
-  std::shared_ptr<ServiceProxy> new_service;
   const bool reuse_connecting_service =
       service_path.value() != "/" && connecting_service_ &&
       connecting_service_->GetObjectPath() == service_path;
   if (reuse_connecting_service) {
-    new_service = connecting_service_;
+    device_state.selected_service = connecting_service_;
     // When we reuse the connecting service, we need to make sure that our
     // cached state is correct.  Normally, we do this by relying reading the
     // state when our signal handlers finish registering, but this may have
     // happened long in the past for the connecting service.
     string state;
-    if (GetStateForService(new_service.get(), &state)) {
+    if (GetStateForService(connecting_service_.get(), &state)) {
       device_state.service_state = ShillServiceStateToNetworkState(state);
     } else {
       LOG(WARNING) << "Failed to read properties from existing service "
@@ -380,14 +384,15 @@
     }
   } else if (service_path.value() != "/") {
     // The device has selected a new service we haven't see before.
-    new_service.reset(new ServiceProxy{bus_, service_path});
-    new_service->RegisterPropertyChangedSignalHandler(
+    device_state.selected_service =
+        std::make_shared<ServiceProxy>(bus_, service_path);
+    device_state.selected_service->RegisterPropertyChangedSignalHandler(
         base::Bind(&ShillClient::OnServicePropertyChange,
                    weak_factory_.GetWeakPtr(), service_path),
         base::Bind(&ShillClient::OnServicePropertyChangeRegistration,
                    weak_factory_.GetWeakPtr(), service_path));
   }
-  device_state.selected_service = new_service;
+
   if (reuse_connecting_service || removed_old_service) {
     UpdateConnectivityState();
   }
diff --git a/buffet/shill_client.h b/buffet/shill_client.h
index 20bfbf6..015db7b 100644
--- a/buffet/shill_client.h
+++ b/buffet/shill_client.h
@@ -42,8 +42,6 @@
               bool disable_xmpp);
   ~ShillClient();
 
-  void Init();
-
   // NetworkProvider implementation.
   void AddConnectionChangedCallback(
       const ConnectionChangedCallback& listener) override;
@@ -70,6 +68,8 @@
     State service_state{State::kOffline};
   };
 
+  void Init();
+
   bool IsMonitoredDevice(org::chromium::flimflam::DeviceProxy* device);
   void OnShillServiceOwnerChange(const std::string& old_owner,
                                  const std::string& new_owner);