Revert "Revert "Allow proxies to be compared and identified.""

This reverts commit a8559fcb28b854ef4f1b1896292a46b9329d2a40.

Now that branches are synced up again.

(originally reverted due to merge conflicts)

Bug: 32172906
Test: hidl_test

Change-Id: I62e53c89cc86cafe63a6ea90309fd6e4bf44b300
diff --git a/Interface.cpp b/Interface.cpp
index 26bdf3c..3a0390d 100644
--- a/Interface.cpp
+++ b/Interface.cpp
@@ -698,8 +698,6 @@
             << "::android::hardware::toBinder<\n";
         out.indent(2, [&] {
             out << fqName().cppName()
-                << ", "
-                << getProxyFqName().cppName()
                 << ">("
                 << name
                 << ");\n";
diff --git a/generateCpp.cpp b/generateCpp.cpp
index 5dde1e6..b2a4a73 100644
--- a/generateCpp.cpp
+++ b/generateCpp.cpp
@@ -886,6 +886,7 @@
         << " const std::string& HidlInstrumentor_package,"
         << " const std::string& HidlInstrumentor_interface);"
         << "\n\n";
+    out << "virtual ~" << klassName << "();\n\n";
     out << "::android::status_t onTransact(\n";
     out.indent();
     out.indent();
@@ -1511,6 +1512,11 @@
         out << "}\n\n";
     }
 
+    out << klassName << "::~" << klassName << "() ";
+    out.block([&]() {
+        out << "::android::hardware::details::gBnMap.erase(_hidl_mImpl.get());\n";
+    }).endl().endl();
+
     status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
         if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
             return OK;
@@ -1962,8 +1968,7 @@
             out << "return ::android::hardware::details::castInterface<";
             out << iface->localName() << ", "
                 << superType->fqName().cppName() << ", "
-                << iface->getProxyName() << ", "
-                << superType->getProxyFqName().cppName()
+                << iface->getProxyName()
                 << ">(\n";
             out.indent();
             out.indent();
diff --git a/test/hidl_test_client.cpp b/test/hidl_test_client.cpp
index 4859e68..07c3ba7 100644
--- a/test/hidl_test_client.cpp
+++ b/test/hidl_test_client.cpp
@@ -722,6 +722,8 @@
 }
 
 TEST_F(HidlTest, TestToken) {
+    using android::hardware::interfacesEqual;
+
     Return<void> ret = tokenManager->createToken(manager, [&] (const hidl_vec<uint8_t> &token) {
         Return<sp<IBase>> retService = tokenManager->get(token);
         EXPECT_OK(retService);
@@ -730,10 +732,7 @@
             EXPECT_NE(nullptr, service.get());
             sp<IServiceManager> retManager = IServiceManager::castFrom(service);
 
-            // TODO(b/33818800): should have only one Bp per process
-            // EXPECT_EQ(manager, retManager);
-
-            EXPECT_NE(nullptr, retManager.get());
+            EXPECT_TRUE(interfacesEqual(manager, retManager));
         }
 
         Return<bool> unregisterRet = tokenManager->unregister(token);
@@ -1621,7 +1620,7 @@
 
     if (mode == BINDERIZED) {
         EXPECT_TRUE(bar->isRemote());
-        binder = ::android::hardware::toBinder<IBar, BpHwBar>(bar);
+        binder = ::android::hardware::toBinder<IBar>(bar);
     } else {
         // For a local test, just wrap the implementation with a BnHwBar
         binder = new BnHwBar(bar);