1886da342Sopenharmony_ci/*
2886da342Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3886da342Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4886da342Sopenharmony_ci * you may not use this file except in compliance with the License.
5886da342Sopenharmony_ci * You may obtain a copy of the License at
6886da342Sopenharmony_ci *
7886da342Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8886da342Sopenharmony_ci *
9886da342Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10886da342Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11886da342Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12886da342Sopenharmony_ci * See the License for the specific language governing permissions and
13886da342Sopenharmony_ci * limitations under the License.
14886da342Sopenharmony_ci */
15886da342Sopenharmony_ci
16886da342Sopenharmony_ci#ifndef IPC_TRANSACTOR_H
17886da342Sopenharmony_ci#define IPC_TRANSACTOR_H
18886da342Sopenharmony_ci
19886da342Sopenharmony_ci#include <iremote_broker.h>
20886da342Sopenharmony_ci#include <iremote_stub.h>
21886da342Sopenharmony_ci#include <iremote_proxy.h>
22886da342Sopenharmony_ci#include <want.h>
23886da342Sopenharmony_ci#include <string>
24886da342Sopenharmony_ci#include <string_view>
25886da342Sopenharmony_ci#include <functional>
26886da342Sopenharmony_ci#include <future>
27886da342Sopenharmony_ci#include "frontend_api_defines.h"
28886da342Sopenharmony_ci
29886da342Sopenharmony_cinamespace OHOS::uitest {
30886da342Sopenharmony_ci    class IApiCaller : public OHOS::IRemoteBroker {
31886da342Sopenharmony_ci    public:
32886da342Sopenharmony_ci        DECLARE_INTERFACE_DESCRIPTOR(u"ohos.uitest.IApiCaller");
33886da342Sopenharmony_ci        const uint32_t TRANS_ID_CALL = 1;
34886da342Sopenharmony_ci        const uint32_t TRANS_ID_SET_BACKCALLER = 2;
35886da342Sopenharmony_ci        // call api specified in call and receive result in reply
36886da342Sopenharmony_ci        virtual void Call(const ApiCallInfo &call, ApiReplyInfo &result) = 0;
37886da342Sopenharmony_ci        // set backcaller for callback usage
38886da342Sopenharmony_ci        virtual bool SetBackCaller(const OHOS::sptr<OHOS::IRemoteObject> &caller) = 0;
39886da342Sopenharmony_ci    };
40886da342Sopenharmony_ci
41886da342Sopenharmony_ci    using ApiCallHandler = std::function<void(const ApiCallInfo &, ApiReplyInfo &)>;
42886da342Sopenharmony_ci    class ApiCaller : public OHOS::IRemoteStub<IApiCaller> {
43886da342Sopenharmony_ci    public:
44886da342Sopenharmony_ci        explicit ApiCaller() = default;
45886da342Sopenharmony_ci        virtual ~ApiCaller() = default;
46886da342Sopenharmony_ci        virtual int OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data,
47886da342Sopenharmony_ci                    OHOS::MessageParcel &reply, OHOS::MessageOption &option) override;
48886da342Sopenharmony_ci        void Call(const ApiCallInfo &call, ApiReplyInfo &result) override;
49886da342Sopenharmony_ci        bool SetBackCaller(const OHOS::sptr<IRemoteObject> &caller) override;
50886da342Sopenharmony_ci        // set functions which do api-invocation and backcaller handling
51886da342Sopenharmony_ci        void SetCallHandler(ApiCallHandler handler);
52886da342Sopenharmony_ci        void SetBackCallerHandler(std::function<void(OHOS::sptr<OHOS::IRemoteObject>)> handler);
53886da342Sopenharmony_ci
54886da342Sopenharmony_ci    private:
55886da342Sopenharmony_ci        ApiCallHandler handler_ = nullptr;
56886da342Sopenharmony_ci        std::function<void(OHOS::sptr<OHOS::IRemoteObject>)> backcallerHandler_ = nullptr;
57886da342Sopenharmony_ci    };
58886da342Sopenharmony_ci
59886da342Sopenharmony_ci    class ApiCallerProxy : public OHOS::IRemoteProxy<IApiCaller> {
60886da342Sopenharmony_ci    public:
61886da342Sopenharmony_ci        explicit ApiCallerProxy(const OHOS::sptr<OHOS::IRemoteObject> &impl);
62886da342Sopenharmony_ci        virtual ~ApiCallerProxy()  = default;
63886da342Sopenharmony_ci        void Call(const ApiCallInfo &call, ApiReplyInfo &result) override;
64886da342Sopenharmony_ci        bool SetBackCaller(const OHOS::sptr<IRemoteObject> &caller) override;
65886da342Sopenharmony_ci        bool SetRemoteDeathCallback(const sptr<OHOS::IRemoteObject::DeathRecipient> &callback);
66886da342Sopenharmony_ci        bool UnsetRemoteDeathCallback(const sptr<OHOS::IRemoteObject::DeathRecipient> &callback);
67886da342Sopenharmony_ci
68886da342Sopenharmony_ci    private:
69886da342Sopenharmony_ci        static inline OHOS::BrokerDelegator<ApiCallerProxy> delegator_;
70886da342Sopenharmony_ci    };
71886da342Sopenharmony_ci
72886da342Sopenharmony_ci    /**Represents the api transaction participant(client/server).*/
73886da342Sopenharmony_ci    enum ConnectionStat : uint8_t { UNINIT, CONNECTED, DISCONNECTED };
74886da342Sopenharmony_ci    using BroadcastCommandHandler = std::function<void(const OHOS::AAFwk::Want &cmd, ApiCallErr &err)>;
75886da342Sopenharmony_ci    class ApiTransactor {
76886da342Sopenharmony_ci    public:
77886da342Sopenharmony_ci        ApiTransactor() = delete;
78886da342Sopenharmony_ci        explicit ApiTransactor(bool asServer);
79886da342Sopenharmony_ci        ~ApiTransactor();
80886da342Sopenharmony_ci        bool InitAndConnectPeer(std::string_view token, ApiCallHandler handler);
81886da342Sopenharmony_ci        void Finalize();
82886da342Sopenharmony_ci        void Transact(const ApiCallInfo &call, ApiReplyInfo &reply);
83886da342Sopenharmony_ci        void SetDeathCallback(std::function<void()> callback);
84886da342Sopenharmony_ci        ConnectionStat GetConnectionStat() const;
85886da342Sopenharmony_ci        // functions for sending/handling broadcast commands
86886da342Sopenharmony_ci        static void SendBroadcastCommand(const OHOS::AAFwk::Want &cmd, ApiCallErr &err);
87886da342Sopenharmony_ci        static void SetBroadcastCommandHandler(BroadcastCommandHandler handler);
88886da342Sopenharmony_ci        static void UnsetBroadcastCommandHandler();
89886da342Sopenharmony_ci
90886da342Sopenharmony_ci    private:
91886da342Sopenharmony_ci        const bool asServer_ = false;
92886da342Sopenharmony_ci        ConnectionStat connectState_ = UNINIT;
93886da342Sopenharmony_ci        bool singlenessMode_ = false;
94886da342Sopenharmony_ci        // for concurrent invocation detect
95886da342Sopenharmony_ci        std::string processingApi_ = "";
96886da342Sopenharmony_ci        sptr<ApiCaller> caller_ = nullptr;
97886da342Sopenharmony_ci        // ipc objects
98886da342Sopenharmony_ci        sptr<ApiCallerProxy> remoteCaller_ = nullptr;
99886da342Sopenharmony_ci        sptr<OHOS::IRemoteObject::DeathRecipient> peerDeathCallback_ = nullptr;
100886da342Sopenharmony_ci        std::function<void()> onDeathCallback_ = nullptr;
101886da342Sopenharmony_ci        void OnPeerDeath();
102886da342Sopenharmony_ci    };
103886da342Sopenharmony_ci} // namespace OHOS::uitest
104886da342Sopenharmony_ci
105886da342Sopenharmony_ci#endif