1 /* 2 * Copyright (c) 2021-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 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 <string> 23 #include <string_view> 24 #include <functional> 25 #include <future> 26 #include "frontend_api_defines.h" 27 28 namespace OHOS::uitest { 29 class IApiCaller : public OHOS::IRemoteBroker { 30 public: 31 DECLARE_INTERFACE_DESCRIPTOR(u"ohos.uitest.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 = std::function<void(const ApiCallInfo &, ApiReplyInfo &)>; 41 class ApiCaller : public OHOS::IRemoteStub<IApiCaller> { 42 public: 43 explicit ApiCaller() = default; 44 virtual ~ApiCaller() = 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(std::function<void(OHOS::sptr<OHOS::IRemoteObject>)> handler); 52 53 private: 54 ApiCallHandler handler_ = nullptr; 55 std::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 private: 68 static inline OHOS::BrokerDelegator<ApiCallerProxy> delegator_; 69 }; 70 71 /**Represents the api transaction participant(client/server).*/ 72 enum ConnectionStat : uint8_t { UNINIT, CONNECTED, DISCONNECTED }; 73 class ApiTransactor { 74 public: 75 ApiTransactor() = delete; 76 explicit ApiTransactor(bool asServer); 77 ~ApiTransactor(); 78 bool InitAndConnectPeer(std::string_view token, ApiCallHandler handler); 79 void Finalize(); 80 void Transact(const ApiCallInfo &call, ApiReplyInfo &reply); 81 void SetDeathCallback(std::function<void()> callback); 82 ConnectionStat GetConnectionStat() const; 83 84 private: 85 const bool asServer_ = false; 86 ConnectionStat connectState_ = UNINIT; 87 // for concurrent invocation detect 88 std::string processingApi_ = ""; 89 sptr<ApiCaller> caller_ = nullptr; 90 // ipc objects 91 sptr<ApiCallerProxy> remoteCaller_ = nullptr; 92 sptr<OHOS::IRemoteObject::DeathRecipient> peerDeathCallback_ = nullptr; 93 std::function<void()> onDeathCallback_ = nullptr; 94 void OnPeerDeath(); 95 }; 96 } // namespace OHOS::uitest 97 98 #endif