1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef HDI_PROXY_BROKER_H 17 #define HDI_PROXY_BROKER_H 18 19 #include <iremote_broker.h> 20 #include <peer_holder.h> 21 #include <refbase.h> 22 23 namespace OHOS { 24 namespace HDI { 25 template <typename INTERFACE> 26 class IProxyBroker : public OHOS::PeerHolder, public OHOS::IRemoteBroker, public INTERFACE { 27 public: 28 explicit IProxyBroker(const sptr<IRemoteObject> &object); 29 virtual ~IProxyBroker() = default; 30 virtual sptr<INTERFACE> AsInterface(); 31 sptr<IRemoteObject> AsObject() override; 32 }; 33 34 template <typename INTERFACE> IProxyBroker(const sptr<IRemoteObject> & object)35IProxyBroker<INTERFACE>::IProxyBroker(const sptr<IRemoteObject> &object) : PeerHolder(object) 36 { 37 } 38 39 template <typename INTERFACE> AsInterface()40sptr<INTERFACE> IProxyBroker<INTERFACE>::AsInterface() 41 { 42 return this; 43 } 44 45 template <typename INTERFACE> AsObject()46sptr<IRemoteObject> IProxyBroker<INTERFACE>::AsObject() 47 { 48 return Remote(); 49 } 50 51 template <typename INTERFACE> hdi_facecast(const sptr<IRemoteObject> & object)52inline sptr<INTERFACE> hdi_facecast(const sptr<IRemoteObject> &object) 53 { 54 const std::u16string descriptor = INTERFACE::GetDescriptor(); 55 BrokerRegistration ®istration = BrokerRegistration::Get(); 56 sptr<IRemoteBroker> broker = registration.NewInstance(descriptor, object); 57 INTERFACE *proxyBroker = static_cast<IProxyBroker<INTERFACE> *>(broker.GetRefPtr()); 58 return static_cast<INTERFACE *>(proxyBroker); 59 } 60 61 template <typename INTERFACE> hdi_objcast(const sptr<INTERFACE> & iface)62inline sptr<IRemoteObject> hdi_objcast(const sptr<INTERFACE> &iface) 63 { 64 IProxyBroker<INTERFACE> *brokerObject = static_cast<IProxyBroker<INTERFACE> *>(iface.GetRefPtr()); 65 return brokerObject->AsObject(); 66 } 67 } // namespace HDI 68 } // namespace OHOS 69 70 #endif // HDI_PROXY_BROKER_H 71