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_IPC_MSG_ADAPTER_DEF_H 17 #define OHOS_SHARING_IPC_MSG_ADAPTER_DEF_H 18 19 #include "inter_ipc_stub.h" 20 21 namespace OHOS { 22 namespace Sharing { 23 24 class MsgAdapterListener { 25 public: 26 using Ptr = std::shared_ptr<MsgAdapterListener>; 27 28 MsgAdapterListener() = default; 29 virtual ~MsgAdapterListener() = default; 30 31 virtual void OnRemoteDied() = 0; 32 virtual void OnRequest(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply) = 0; 33 }; 34 35 class MsgAdapter { 36 public: 37 using Ptr = std::shared_ptr<MsgAdapter>; 38 39 MsgAdapter() = default; 40 virtual ~MsgAdapter() = default; 41 SetListener(const std::weak_ptr<MsgAdapterListener> &listener)42 int32_t SetListener(const std::weak_ptr<MsgAdapterListener> &listener) 43 { 44 SHARING_LOGD("trace."); 45 listener_ = listener; 46 return 0; 47 } 48 49 public: 50 virtual int32_t OnRequest(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply) = 0; 51 virtual int32_t SendRequest(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply) = 0; 52 53 protected: 54 std::weak_ptr<MsgAdapterListener> listener_; 55 }; 56 57 class IpcMsgAdapter : public MsgAdapter, 58 public IInterIpcStubListener, 59 public std::enable_shared_from_this<IpcMsgAdapter> { 60 public: 61 using Ptr = std::shared_ptr<IpcMsgAdapter>; 62 63 IpcMsgAdapter() = default; 64 ~IpcMsgAdapter() override; 65 66 int32_t OnRequest(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply) override; 67 int32_t SendRequest(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply) override; 68 69 void OnRemoteDied() override; 70 void OnIpcRequest(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply) override; 71 72 void SetPeerProxy(sptr<IInterIpc> proxy); 73 void SetLocalStub(sptr<InterIpcStub> stub); 74 75 std::string GetCallingKey(); 76 void SetCallingKey(std::string key); 77 78 int32_t GetPeerPid(); 79 void SetPeerPid(int32_t pid); 80 81 protected: 82 std::string key_; 83 int32_t peerPid_ = 0; 84 sptr<IInterIpc> peerProxy_ = nullptr; 85 sptr<InterIpcStub> localStub_ = nullptr; 86 }; 87 88 } // namespace Sharing 89 } // namespace OHOS 90 91 #endif