• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <want.h>
23 #include <string>
24 #include <string_view>
25 #include <functional>
26 #include <future>
27 #include "frontend_api_defines.h"
28 
29 namespace OHOS::uitest {
30     class IApiCaller : public OHOS::IRemoteBroker {
31     public:
32         DECLARE_INTERFACE_DESCRIPTOR(u"ohos.uitest.IApiCaller");
33         const uint32_t TRANS_ID_CALL = 1;
34         const uint32_t TRANS_ID_SET_BACKCALLER = 2;
35         // call api specified in call and receive result in reply
36         virtual void Call(const ApiCallInfo &call, ApiReplyInfo &result) = 0;
37         // set backcaller for callback usage
38         virtual bool SetBackCaller(const OHOS::sptr<OHOS::IRemoteObject> &caller) = 0;
39     };
40 
41     using ApiCallHandler = std::function<void(const ApiCallInfo &, ApiReplyInfo &)>;
42     class ApiCaller : public OHOS::IRemoteStub<IApiCaller> {
43     public:
44         explicit ApiCaller() = default;
45         virtual ~ApiCaller() = default;
46         virtual int OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data,
47                     OHOS::MessageParcel &reply, OHOS::MessageOption &option) override;
48         void Call(const ApiCallInfo &call, ApiReplyInfo &result) override;
49         bool SetBackCaller(const OHOS::sptr<IRemoteObject> &caller) override;
50         // set functions which do api-invocation and backcaller handling
51         void SetCallHandler(ApiCallHandler handler);
52         void SetBackCallerHandler(std::function<void(OHOS::sptr<OHOS::IRemoteObject>)> handler);
53 
54     private:
55         ApiCallHandler handler_ = nullptr;
56         std::function<void(OHOS::sptr<OHOS::IRemoteObject>)> backcallerHandler_ = nullptr;
57     };
58 
59     class ApiCallerProxy : public OHOS::IRemoteProxy<IApiCaller> {
60     public:
61         explicit ApiCallerProxy(const OHOS::sptr<OHOS::IRemoteObject> &impl);
62         virtual ~ApiCallerProxy()  = default;
63         void Call(const ApiCallInfo &call, ApiReplyInfo &result) override;
64         bool SetBackCaller(const OHOS::sptr<IRemoteObject> &caller) override;
65         bool SetRemoteDeathCallback(const sptr<OHOS::IRemoteObject::DeathRecipient> &callback);
66         bool UnsetRemoteDeathCallback(const sptr<OHOS::IRemoteObject::DeathRecipient> &callback);
67 
68     private:
69         static inline OHOS::BrokerDelegator<ApiCallerProxy> delegator_;
70     };
71 
72     /**Represents the api transaction participant(client/server).*/
73     enum ConnectionStat : uint8_t { UNINIT, CONNECTED, DISCONNECTED };
74     using BroadcastCommandHandler = std::function<void(const OHOS::AAFwk::Want &cmd, ApiCallErr &err)>;
75     class ApiTransactor {
76     public:
77         ApiTransactor() = delete;
78         explicit ApiTransactor(bool asServer);
79         ~ApiTransactor();
80         bool InitAndConnectPeer(std::string_view token, ApiCallHandler handler);
81         void Finalize();
82         void Transact(const ApiCallInfo &call, ApiReplyInfo &reply);
83         void SetDeathCallback(std::function<void()> callback);
84         ConnectionStat GetConnectionStat() const;
85         // functions for sending/handling broadcast commands
86         static void SendBroadcastCommand(const OHOS::AAFwk::Want &cmd, ApiCallErr &err);
87         static void SetBroadcastCommandHandler(BroadcastCommandHandler handler);
88         static void UnsetBroadcastCommandHandler();
89 
90     private:
91         const bool asServer_ = false;
92         ConnectionStat connectState_ = UNINIT;
93         bool singlenessMode_ = false;
94         // for concurrent invocation detect
95         std::string processingApi_ = "";
96         sptr<ApiCaller> caller_ = nullptr;
97         // ipc objects
98         sptr<ApiCallerProxy> remoteCaller_ = nullptr;
99         sptr<OHOS::IRemoteObject::DeathRecipient> peerDeathCallback_ = nullptr;
100         std::function<void()> onDeathCallback_ = nullptr;
101         void OnPeerDeath();
102     };
103 } // namespace OHOS::uitest
104 
105 #endif