chromeos-dbus-bindings: Correct ObjectManager without service name

We need to store the passed in service name and pass it to proxies
we create when not configured with a service name in the dbus config.

BUG=brillo:581
TEST=leaderd changes which depend on this compile, unittests.

Change-Id: Id53f3a0c45ac64d477ba1d723952679cc3eb1b29
Reviewed-on: https://chromium-review.googlesource.com/260014
Tested-by: Christopher Wiley <[email protected]>
Reviewed-by: Alex Vakulenko <[email protected]>
Commit-Queue: Christopher Wiley <[email protected]>
diff --git a/chromeos-dbus-bindings/proxy_generator.cc b/chromeos-dbus-bindings/proxy_generator.cc
index 34d9e90..0921ae4 100644
--- a/chromeos-dbus-bindings/proxy_generator.cc
+++ b/chromeos-dbus-bindings/proxy_generator.cc
@@ -837,10 +837,10 @@
   text->AddLineWithOffset("private:", kScopeOffset);
   text->PushOffset(kBlockOffset);
   AddOnPropertyChanged(interfaces, text);
-  AddObjectAdded(interfaces, text);
+  AddObjectAdded(config, interfaces, text);
   AddObjectRemoved(interfaces, text);
   AddCreateProperties(interfaces, class_name, text);
-  AddDataMembers(interfaces, class_name, text);
+  AddDataMembers(config, interfaces, class_name, text);
 
   text->AddLine(StringPrintf("DISALLOW_COPY_AND_ASSIGN(%s);",
                               class_name.c_str()));
@@ -870,6 +870,9 @@
   text->PushOffset(kLineContinuationOffset);
   text->AddLine(": bus_{bus},");
   text->PushOffset(kBlockOffset);
+  if (config.service_name.empty()) {
+    text->AddLine("service_name_{service_name},");
+  }
   text->AddLine("dbus_object_manager_{bus->GetObjectManager(");
   text->PushOffset(kLineContinuationOffset);
   if (config.service_name.empty()) {
@@ -1037,6 +1040,7 @@
 }
 
 void ProxyGenerator::ObjectManager::AddObjectAdded(
+    const ServiceConfig& config,
     const std::vector<Interface>& interfaces,
     IndentedText* text) {
   text->AddLine("void ObjectAdded(");
@@ -1068,6 +1072,9 @@
     text->PushOffset(kBlockOffset);
     string new_instance = StringPrintf("new %s{bus_",
                                        itf_name.MakeProxyName(true).c_str());
+    if (config.service_name.empty()) {
+      new_instance += ", service_name_";
+    }
     if (itf.path.empty())
       new_instance += ", object_path";
     if (!itf.properties.empty())
@@ -1177,10 +1184,14 @@
 }
 
 void ProxyGenerator::ObjectManager::AddDataMembers(
+    const ServiceConfig& config,
     const std::vector<Interface>& interfaces,
     const std::string& class_name,
     IndentedText* text) {
   text->AddLine("scoped_refptr<dbus::Bus> bus_;");
+  if (config.service_name.empty()) {
+    text->AddLine("std::string service_name_;");
+  }
   text->AddLine("dbus::ObjectManager* dbus_object_manager_;");
   for (const auto& itf : interfaces) {
     NameParser itf_name{itf.name};
diff --git a/chromeos-dbus-bindings/proxy_generator.h b/chromeos-dbus-bindings/proxy_generator.h
index aa9e89e..5dc04c9 100644
--- a/chromeos-dbus-bindings/proxy_generator.h
+++ b/chromeos-dbus-bindings/proxy_generator.h
@@ -146,7 +146,8 @@
                                      IndentedText* text);
 
     // Generates ObjectAdded() method.
-    static void AddObjectAdded(const std::vector<Interface>& interfaces,
+    static void AddObjectAdded(const ServiceConfig& config,
+                               const std::vector<Interface>& interfaces,
                                IndentedText* text);
 
     // Generates ObjectRemoved() method.
@@ -159,7 +160,8 @@
                                     IndentedText* text);
 
     // Generates data members of the class.
-    static void AddDataMembers(const std::vector<Interface>& interfaces,
+    static void AddDataMembers(const ServiceConfig& config,
+                               const std::vector<Interface>& interfaces,
                                const std::string& class_name,
                                IndentedText* text);
   };
diff --git a/chromeos-dbus-bindings/proxy_generator_unittest.cc b/chromeos-dbus-bindings/proxy_generator_unittest.cc
index a157656..165d068 100644
--- a/chromeos-dbus-bindings/proxy_generator_unittest.cc
+++ b/chromeos-dbus-bindings/proxy_generator_unittest.cc
@@ -740,6 +740,7 @@
   ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus,
                      const std::string& service_name)
       : bus_{bus},
+        service_name_{service_name},
         dbus_object_manager_{bus->GetObjectManager(
             service_name,
             dbus::ObjectPath{"/org/chromium/Test"})} {
@@ -821,7 +822,7 @@
           static_cast<org::chromium::Itf1Proxy::PropertySet*>(
               dbus_object_manager_->GetProperties(object_path, interface_name));
       std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{
-        new org::chromium::Itf1Proxy{bus_, property_set}
+        new org::chromium::Itf1Proxy{bus_, service_name_, property_set}
       };
       auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy));
       if (!on_itf1_added_.is_null())
@@ -830,7 +831,7 @@
     }
     if (interface_name == "org.chromium.Itf2") {
       std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{
-        new org::chromium::Itf2Proxy{bus_, object_path}
+        new org::chromium::Itf2Proxy{bus_, service_name_, object_path}
       };
       auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy));
       if (!on_itf2_added_.is_null())
@@ -890,6 +891,7 @@
   }
 
   scoped_refptr<dbus::Bus> bus_;
+  std::string service_name_;
   dbus::ObjectManager* dbus_object_manager_;
   std::map<dbus::ObjectPath,
            std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_;