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 #include "ipc_msg_adapter.h"
17
18 namespace OHOS {
19 namespace Sharing {
20
~IpcMsgAdapter()21 IpcMsgAdapter::~IpcMsgAdapter()
22 {
23 SHARING_LOGD("trace.");
24 peerProxy_ = nullptr;
25 localStub_ = nullptr;
26 }
27
SendRequest(std::shared_ptr<BaseMsg> msg,std::shared_ptr<BaseMsg> & reply)28 int32_t IpcMsgAdapter::SendRequest(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply)
29 {
30 SHARING_LOGD("trace.");
31 if (peerProxy_ != nullptr) {
32 peerProxy_->DoIpcCommand(msg, reply);
33 } else {
34 SHARING_LOGE("peerProxy_ is nullptr.");
35 }
36
37 return 0;
38 }
39
OnRequest(std::shared_ptr<BaseMsg> msg,std::shared_ptr<BaseMsg> & reply)40 int32_t IpcMsgAdapter::OnRequest(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply)
41 {
42 SHARING_LOGD("trace.");
43
44 auto listener = listener_.lock();
45 if (listener) {
46 listener->OnRequest(msg, reply);
47 } else {
48 SHARING_LOGE("listener_ is nullptr.");
49 }
50
51 return 0;
52 }
53
SetLocalStub(sptr<InterIpcStub> stub)54 void IpcMsgAdapter::SetLocalStub(sptr<InterIpcStub> stub)
55 {
56 SHARING_LOGD("trace.");
57 localStub_ = stub;
58 localStub_->SetStubListener(shared_from_this());
59 }
60
SetPeerProxy(sptr<IInterIpc> proxy)61 void IpcMsgAdapter::SetPeerProxy(sptr<IInterIpc> proxy)
62 {
63 SHARING_LOGD("trace.");
64 peerProxy_ = proxy;
65 }
66
OnIpcRequest(std::shared_ptr<BaseMsg> msg,std::shared_ptr<BaseMsg> & reply)67 void IpcMsgAdapter::OnIpcRequest(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply)
68 {
69 SHARING_LOGD("trace.");
70 OnRequest(msg, reply);
71 }
72
OnRemoteDied()73 void IpcMsgAdapter::OnRemoteDied()
74 {
75 SHARING_LOGD("on Adapter IpcStub remote died!");
76 auto listener = listener_.lock();
77 if (listener) {
78 listener->OnRemoteDied();
79 }
80 }
81
SetCallingKey(std::string key)82 void IpcMsgAdapter::SetCallingKey(std::string key)
83 {
84 SHARING_LOGD("trace.");
85 key_ = key;
86 }
87
GetCallingKey()88 std::string IpcMsgAdapter::GetCallingKey()
89 {
90 SHARING_LOGD("trace.");
91 return key_;
92 }
93
SetPeerPid(int32_t pid)94 void IpcMsgAdapter::SetPeerPid(int32_t pid)
95 {
96 SHARING_LOGD("trace.");
97 peerPid_ = pid;
98 }
99
GetPeerPid()100 int32_t IpcMsgAdapter::GetPeerPid()
101 {
102 SHARING_LOGD("trace.");
103 return peerPid_;
104 }
105
106 } // namespace Sharing
107 } // namespace OHOS