create gBsConstructorMap; add wrapPassthrough

Similar to gBnConstructorMap, this map helps wrapping a local
interface with the smallest BsFoo possible. They are added
in Static.h/cpp.

Added HidlPassthroughSupport.h/cpp to hold wrapPassthrough.

Also fixes a few Return object isOk() checks.

Fix: 33307350

Test: hidl_test

Change-Id: I92a7283699bfe5b022df5ab5a9ead5690477ea97
diff --git a/transport/Android.bp b/transport/Android.bp
index 4f97ff6..5614ffb 100644
--- a/transport/Android.bp
+++ b/transport/Android.bp
@@ -52,6 +52,7 @@
 
     srcs: [
         "HidlBinderSupport.cpp",
+        "HidlPassthroughSupport.cpp",
         "ServiceManagement.cpp",
         "Static.cpp"
     ],
diff --git a/transport/HidlPassthroughSupport.cpp b/transport/HidlPassthroughSupport.cpp
new file mode 100644
index 0000000..9d4e2b4
--- /dev/null
+++ b/transport/HidlPassthroughSupport.cpp
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <hidl/HidlPassthroughSupport.h>
+
+#include <hidl/HidlSupport.h>
+#include <hidl/Static.h>
+#include <hidl/HidlTransportUtils.h>
+
+namespace android {
+namespace hardware {
+
+sp<::android::hidl::base::V1_0::IBase> wrapPassthrough(
+        sp<::android::hidl::base::V1_0::IBase> iface) {
+    if (iface.get() == nullptr || iface->isRemote()) {
+        // doesn't know how to handle it.
+        return iface;
+    }
+    std::string myDescriptor = getDescriptor(iface.get());
+    if (myDescriptor.empty()) {
+        // interfaceChain fails
+        return nullptr;
+    }
+    auto iter = gBsConstructorMap.find(myDescriptor);
+    if (iter == gBsConstructorMap.end()) {
+        return nullptr;
+    }
+    return (iter->second)(reinterpret_cast<void *>(iface.get()));
+}
+
+
+}  // namespace hardware
+}  // namespace android
diff --git a/transport/Static.cpp b/transport/Static.cpp
index ddda712..4a140e5 100644
--- a/transport/Static.cpp
+++ b/transport/Static.cpp
@@ -25,7 +25,11 @@
 Mutex gDefaultServiceManagerLock;
 sp<android::hidl::manager::V1_0::IServiceManager> gDefaultServiceManager;
 
-std::map<std::string, std::function<sp<IBinder>(void*)>> gBnConstructorMap{};
+std::map<std::string, std::function<sp<IBinder>(void *)>>
+        gBnConstructorMap{};
+
+std::map<std::string, std::function<sp<::android::hidl::base::V1_0::IBase>(void *)>>
+        gBsConstructorMap;
 
 }   // namespace hardware
 }   // namespace android
diff --git a/transport/include/hidl/HidlBinderSupport.h b/transport/include/hidl/HidlBinderSupport.h
index 1dfaecd..38ff6c9 100644
--- a/transport/include/hidl/HidlBinderSupport.h
+++ b/transport/include/hidl/HidlBinderSupport.h
@@ -296,12 +296,7 @@
     if (ifacePtr->isRemote()) {
         return ::android::hardware::IInterface::asBinder(static_cast<ProxyType *>(ifacePtr));
     } else {
-        std::string myDescriptor{};
-        ifacePtr->interfaceChain([&](const hidl_vec<hidl_string> &types) {
-            if (types.size() > 0) {
-                myDescriptor = types[0].c_str();
-            }
-        });
+        std::string myDescriptor = getDescriptor(ifacePtr);
         if (myDescriptor.empty()) {
             // interfaceChain fails
             return nullptr;
diff --git a/transport/include/hidl/HidlPassthroughSupport.h b/transport/include/hidl/HidlPassthroughSupport.h
new file mode 100644
index 0000000..2b2f86b
--- /dev/null
+++ b/transport/include/hidl/HidlPassthroughSupport.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_HIDL_PASSTHROUGH_SUPPORT_H
+#define ANDROID_HIDL_PASSTHROUGH_SUPPORT_H
+
+#include <android/hidl/base/1.0/IBase.h>
+
+namespace android {
+namespace hardware {
+
+/*
+ * Wrap the given interface with the smallest BsChild possible. Will return the
+ * argument directly if nullptr or isRemote().
+ */
+sp<::android::hidl::base::V1_0::IBase> wrapPassthrough(
+        sp<::android::hidl::base::V1_0::IBase> iface);
+
+}  // namespace hardware
+}  // namespace android
+
+
+#endif  // ANDROID_HIDL_PASSTHROUGH_SUPPORT_H
diff --git a/transport/include/hidl/HidlTransportUtils.h b/transport/include/hidl/HidlTransportUtils.h
index d890988..dc444dc 100644
--- a/transport/include/hidl/HidlTransportUtils.h
+++ b/transport/include/hidl/HidlTransportUtils.h
@@ -31,7 +31,7 @@
     }
 
     bool canCast = false;
-    interface->interfaceChain([&](const hidl_vec<hidl_string> &types) {
+    auto ret = interface->interfaceChain([&](const hidl_vec<hidl_string> &types) {
         for (size_t i = 0; i < types.size(); i++) {
             if (types[i] == castTo) {
                 canCast = true;
@@ -39,7 +39,17 @@
             }
         }
     });
-    return canCast;
+    return ret.isOk() && canCast;
+}
+
+inline std::string getDescriptor(::android::hidl::base::V1_0::IBase* interface) {
+    std::string myDescriptor{};
+    auto ret = interface->interfaceChain([&](const hidl_vec<hidl_string> &types) {
+        if (types.size() > 0) {
+            myDescriptor = types[0].c_str();
+        }
+    });
+    return ret.isOk() ? myDescriptor : "";
 }
 
 }   // namespace hardware
diff --git a/transport/include/hidl/Static.h b/transport/include/hidl/Static.h
index 2a40125..1f6fa9f 100644
--- a/transport/include/hidl/Static.h
+++ b/transport/include/hidl/Static.h
@@ -32,7 +32,14 @@
 // For HidlBinderSupport
 // value function receives reinterpret_cast<void *>(static_cast<IFoo *>(foo)),
 // returns sp<IBinder>
-extern std::map<std::string, std::function<sp<IBinder>(void*)>> gBnConstructorMap;
+extern std::map<std::string, std::function<sp<IBinder>(void *)>>
+        gBnConstructorMap;
+
+// For HidlPassthroughSupport
+// value function receives reinterpret_cast<void *>(static_cast<IFoo *>(foo)),
+// returns sp<IBase>
+extern std::map<std::string, std::function<sp<::android::hidl::base::V1_0::IBase>(void *)>>
+        gBsConstructorMap;
 
 }   // namespace hardware
 }   // namespace android