1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 OHOS_SHARING_INTER_IPC_INTERFACE_STUB_H
17 #define OHOS_SHARING_INTER_IPC_INTERFACE_STUB_H
18 
19 #include <unordered_map>
20 #include <vector>
21 #include "i_inter_ipc.h"
22 #include "inter_ipc_death_recipient.h"
23 
24 namespace OHOS {
25 namespace Sharing {
26 
27 class IInterIpcStubListener {
28 public:
29     virtual ~IInterIpcStubListener() = default;
30 
31     virtual void OnRemoteDied() = 0;
32     virtual void OnIpcRequest(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply) = 0;
33 };
34 
35 class InterIpcStubDeathListener;
36 class InterIpcStub : public IRemoteStub<IInterIpc>,
37                      public NoCopyable {
38 public:
39     using Ptr = std::shared_ptr<InterIpcStub>;
40     InterIpcStub();
41     virtual ~InterIpcStub();
42 
43     virtual void OnRemoteDied();
44     virtual void SetStubListener(std::weak_ptr<IInterIpcStubListener> listener);
45     virtual int32_t SendIpcRequest(std::string tokenId, std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply);
46 
47     int32_t SetListenerObject(std::string key, const sptr<IRemoteObject> &object) override;
48     int32_t DoIpcCommand(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &replyMsg) override;
49     int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
50     sptr<IRemoteObject> GetSubSystemAbility(std::string key, std::string className) override;
51 
52 protected:
53     virtual void DelPeerProxy(std::string key);
54     virtual void CreateDeathListener(std::string key);
55 
56     int32_t DoIpcCommand(MessageParcel &data, MessageParcel &reply);
57     int32_t GetSystemAbility(MessageParcel &data, MessageParcel &reply);
58     int32_t SetListenerObject(MessageParcel &data, MessageParcel &reply);
59 
60 protected:
61     std::mutex mutex_;
62     Ptr sharedFromThis_ = nullptr;
63     std::weak_ptr<IInterIpcStubListener> stubListener_;
64     std::unordered_map<std::string, sptr<IInterIpc>> peerProxys_;
65     std::unordered_map<std::string, sptr<InterIpcDeathRecipient>> deathRecipients_;
66 };
67 
68 } // namespace Sharing
69 } // namespace OHOS
70 #endif