shill: vpn: Support Service.Name property change.

BUG=chromium:221577
TEST=Unit tests; Tested on device through configure-vpn; Also, tested
on device through corp policy and checked that the service got
renamed.

Change-Id: I936f85573e8d0820e2baa2361f94619e0e655f8f
Reviewed-on: https://gerrit.chromium.org/gerrit/46514
Reviewed-by: Darin Petkov <petkov@chromium.org>
Tested-by: Darin Petkov <petkov@chromium.org>
Commit-Queue: Darin Petkov <petkov@chromium.org>
diff --git a/vpn_provider.cc b/vpn_provider.cc
index dbadcbc..be8530e 100644
--- a/vpn_provider.cc
+++ b/vpn_provider.cc
@@ -49,23 +49,28 @@
     return NULL;
   }
 
+  string host = args.LookupString(flimflam::kProviderHostProperty, "");
+  if (host.empty()) {
+    Error::PopulateAndLog(
+        error, Error::kNotSupported, "Missing VPN host property.");
+    return NULL;
+  }
+
   string storage_id = VPNService::CreateStorageIdentifier(args, error);
   if (storage_id.empty()) {
     return NULL;
   }
 
-  // Find a service in the provider list which matches these parameters.
-  VPNServiceRefPtr service = FindService(type, storage_id);
-
-  if (service == NULL) {
-    // Create a service, using the name and type arguments passed in.
-    string name = args.LookupString(flimflam::kProviderNameProperty, "");
-    if (name.empty()) {
-      name = args.LookupString(flimflam::kNameProperty, "");
-    }
-    service = CreateService(type, name, storage_id, error);
+  string name = args.LookupString(flimflam::kProviderNameProperty, "");
+  if (name.empty()) {
+    name = args.LookupString(flimflam::kNameProperty, "");
   }
 
+  // Find a service in the provider list which matches these parameters.
+  VPNServiceRefPtr service = FindService(type, name, host);
+  if (service == NULL) {
+    service = CreateService(type, name, storage_id, error);
+  }
   return service;
 }
 
@@ -115,7 +120,14 @@
       continue;
     }
 
-    VPNServiceRefPtr service = FindService(type, *it);
+    string host;
+    if (!storage->GetString(*it, flimflam::kProviderHostProperty, &host)) {
+      LOG(ERROR) << "Group " << *it << " is missing the "
+                 << flimflam::kProviderHostProperty << " property.";
+      continue;
+    }
+
+    VPNServiceRefPtr service = FindService(type, name, host);
     if (service != NULL) {
       // If the service already exists, it does not need to be configured,
       // since PushProfile would have already called ConfigureService on it.
@@ -181,17 +193,18 @@
 #endif  // DISABLE_VPN
 }
 
-VPNServiceRefPtr VPNProvider::FindService(const std::string &type,
-                                          const std::string &storage_id) {
+VPNServiceRefPtr VPNProvider::FindService(const string &type,
+                                          const string &name,
+                                          const string &host) {
   for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin();
        it != services_.end();
        ++it) {
     if (type == (*it)->driver()->GetProviderType() &&
-        storage_id == (*it)->GetStorageIdentifier()) {
+        name == (*it)->friendly_name() &&
+        host == (*it)->driver()->GetHost()) {
       return *it;
     }
   }
-
   return NULL;
 }