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 API_CALLER_CLIENT_H 17 #define API_CALLER_CLIENT_H 18 19 #include <string> 20 #include <string_view> 21 #include <functional> 22 #include <common_event_subscribe_info.h> 23 #include <common_event_manager.h> 24 #include "ipc_transactor.h" 25 26 27 namespace OHOS::perftest { 28 using namespace std; 29 using namespace OHOS::AAFwk; 30 using namespace OHOS::EventFwk; 31 32 /**Represents the api transaction participant client.*/ 33 class ApiCallerClient { 34 public: ApiCallerClient()35 ApiCallerClient() {}; 36 ~ApiCallerClient(); 37 bool InitAndConnectPeer(string_view token, ApiCallHandler handler); 38 void Transact(const ApiCallInfo &call, ApiReplyInfo &reply); 39 void SetDeathCallback(function<void()> callback); 40 ConnectionStat GetConnectionStat() const; 41 private: 42 sptr<IRemoteObject> WaitForPublishedCaller(string_view token); 43 void OnPeerDeath(); 44 45 private: 46 ConnectionStat connectState_ = UNINIT; 47 // for concurrent invocation detect 48 string processingApi_ = ""; 49 // ipc objects 50 sptr<ApiCallerStub> caller_ = nullptr; 51 sptr<ApiCallerProxy> remoteCaller_ = nullptr; 52 sptr<OHOS::IRemoteObject::DeathRecipient> peerDeathCallback_ = nullptr; 53 function<void()> onDeathCallback_ = nullptr; 54 }; 55 56 using CommonEventHandler = function<void(const CommonEventData &)>; 57 class CommonEventForwarder : public CommonEventSubscriber { 58 public: CommonEventForwarder(const CommonEventSubscribeInfo & info,CommonEventHandler handler)59 explicit CommonEventForwarder(const CommonEventSubscribeInfo &info, CommonEventHandler handler) 60 : CommonEventSubscriber(info), handler_(handler) {} ~CommonEventForwarder()61 virtual ~CommonEventForwarder() {} 62 void UpdateHandler(CommonEventHandler handler); 63 void OnReceiveEvent(const CommonEventData &data) override; 64 65 private: 66 CommonEventHandler handler_ = nullptr; 67 }; 68 } // namespace OHOS::perftest 69 70 #endif