1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <hidl/HidlPassthroughSupport.h>
18 
19 #include "InternalStatic.h"  // TODO(b/69122224): remove this include, for tryWrap
20 
21 #include <hidl/HidlTransportUtils.h>
22 #include <hidl/Static.h>
23 
24 using ::android::hidl::base::V1_0::IBase;
25 
26 namespace android {
27 namespace hardware {
28 namespace details {
29 
tryWrap(const std::string & descriptor,sp<IBase> iface)30 static sp<IBase> tryWrap(const std::string& descriptor, sp<IBase> iface) {
31     auto func = getBsConstructorMap().get(descriptor, nullptr);
32     if (func) {
33         return func(static_cast<void*>(iface.get()));
34     }
35     return nullptr;
36 }
37 
wrapPassthroughInternal(sp<IBase> iface)38 sp<IBase> wrapPassthroughInternal(sp<IBase> iface) {
39     if (iface == nullptr || iface->isRemote()) {
40         // doesn't know how to handle it.
41         return iface;
42     }
43 
44     // Consider the case when an AOSP interface is extended by partners.
45     // Then the partner's HAL interface library is loaded only in the vndk
46     // linker namespace, but not in the default linker namespace, where
47     // this code runs. As a result, BsConstructorMap in the latter does not
48     // have the entry for the descriptor name.
49     //
50     // Therefore, we try to wrap using the descript names of the parent
51     // types along the interface chain, instead of always using the descriptor
52     // name of the current interface.
53     sp<IBase> base;
54     auto ret = iface->interfaceChain([&](const auto& types) {
55         for (const std::string& descriptor : types) {
56             base = tryWrap(descriptor, iface);
57             if (base != nullptr) {
58                 break;  // wrap is successful. no need to lookup further.
59             }
60         }
61     });
62 
63     if (!ret.isOk()) {
64         return nullptr;
65     }
66 
67     // It is ensured that if this function is called with an instance of IType
68     // then the corresponding descriptor would be in the BsConstructorMap.
69     // This is because referencing IType implies that the interface library
70     // defining the type has already been loaded into the current linker
71     // namespace, and thus the library should have added an entry into the
72     // BsConstructorMap while executing the library's constructor.
73     return base;
74 }
75 
76 }  // namespace details
77 }  // namespace hardware
78 }  // namespace android
79