1 /* 2 * Copyright (c) 2025 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 IPC_TRANSACTOR_H 17 #define IPC_TRANSACTOR_H 18 19 #include <iremote_broker.h> 20 #include <iremote_stub.h> 21 #include <iremote_proxy.h> 22 #include <functional> 23 #include "common_utils.h" 24 #include "frontend_api_defines.h" 25 26 namespace OHOS::perftest { 27 using namespace std; 28 29 class IApiCaller : public OHOS::IRemoteBroker { 30 public: 31 DECLARE_INTERFACE_DESCRIPTOR(u"ohos.perftest.IApiCaller"); 32 const uint32_t TRANS_ID_CALL = 1; 33 const uint32_t TRANS_ID_SET_BACKCALLER = 2; 34 // call api specified in call and receive result in reply 35 virtual void Call(const ApiCallInfo &call, ApiReplyInfo &result) = 0; 36 // set backcaller for callback usage 37 virtual bool SetBackCaller(const OHOS::sptr<OHOS::IRemoteObject> &caller) = 0; 38 }; 39 40 using ApiCallHandler = function<void(const ApiCallInfo &, ApiReplyInfo &)>; 41 class ApiCallerStub : public OHOS::IRemoteStub<IApiCaller> { 42 public: 43 explicit ApiCallerStub() = default; 44 virtual ~ApiCallerStub() = default; 45 virtual int OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, 46 OHOS::MessageParcel &reply, OHOS::MessageOption &option) override; 47 void Call(const ApiCallInfo &call, ApiReplyInfo &result) override; 48 bool SetBackCaller(const OHOS::sptr<IRemoteObject> &caller) override; 49 // set functions which do api-invocation and backcaller handling 50 void SetCallHandler(ApiCallHandler handler); 51 void SetBackCallerHandler(function<void(OHOS::sptr<OHOS::IRemoteObject>)> handler); 52 53 private: 54 ApiCallHandler handler_ = nullptr; 55 function<void(OHOS::sptr<OHOS::IRemoteObject>)> backcallerHandler_ = nullptr; 56 }; 57 58 class ApiCallerProxy : public OHOS::IRemoteProxy<IApiCaller> { 59 public: 60 explicit ApiCallerProxy(const OHOS::sptr<OHOS::IRemoteObject> &impl); 61 virtual ~ApiCallerProxy() = default; 62 void Call(const ApiCallInfo &call, ApiReplyInfo &result) override; 63 bool SetBackCaller(const OHOS::sptr<IRemoteObject> &caller) override; 64 bool SetRemoteDeathCallback(const sptr<OHOS::IRemoteObject::DeathRecipient> &callback); 65 bool UnsetRemoteDeathCallback(const sptr<OHOS::IRemoteObject::DeathRecipient> &callback); 66 }; 67 68 using RemoteDiedHandler = function<void()>; 69 class DeathRecipientForwarder : public IRemoteObject::DeathRecipient { 70 public: DeathRecipientForwarder(RemoteDiedHandler handler)71 explicit DeathRecipientForwarder(RemoteDiedHandler handler) : handler_(handler) {}; 72 ~DeathRecipientForwarder() = default; 73 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 74 75 private: 76 const RemoteDiedHandler handler_; 77 }; 78 } // namespace OHOS::perftest 79 80 #endif