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 "inter_ipc_client_stub.h"
17 #include "ipc_skeleton.h"
18
19 namespace OHOS {
20 namespace Sharing {
21
InterIpcClientStub()22 InterIpcClientStub::InterIpcClientStub()
23 {
24 SHARING_LOGD("trace.");
25 }
26
~InterIpcClientStub()27 InterIpcClientStub::~InterIpcClientStub()
28 {
29 SHARING_LOGD("trace.");
30 }
31
SetListenerObject(std::string key,const sptr<IRemoteObject> & object)32 int32_t InterIpcClientStub::SetListenerObject(std::string key, const sptr<IRemoteObject> &object)
33 {
34 SHARING_LOGD("trace.");
35 if (object == nullptr) {
36 SHARING_LOGE("domain rpc null listener object.");
37 return -1;
38 }
39
40 sptr<IInterIpc> peerProxy = iface_cast<IInterIpc>(object);
41 if (peerProxy == nullptr) {
42 SHARING_LOGE("convert null.");
43 return -1;
44 }
45
46 std::unique_lock<std::mutex> lock(mutex_);
47 peerProxys_[key] = peerProxy;
48 lock.unlock();
49 SHARING_LOGD("peer key: %{public}s, listener num: %{public}zu.", key.c_str(), peerProxys_.size());
50
51 auto deathRecipient = new (std::nothrow) InterIpcDeathRecipient(key);
52 if (deathRecipient == nullptr) {
53 SHARING_LOGE("deathRecipient create failed.");
54 return -1;
55 }
56 deathRecipients_[key] = deathRecipient;
57 CreateDeathListener(key);
58
59 if (peerProxy->AsObject() != nullptr) {
60 if (!peerProxy->AsObject()->AddDeathRecipient(deathRecipient)) {
61 SHARING_LOGE("add death recipient failed.");
62 return -1;
63 }
64 }
65
66 return ERR_NONE;
67 }
68
DoIpcCommand(std::shared_ptr<BaseMsg> msg,std::shared_ptr<BaseMsg> & replyMsg)69 int32_t InterIpcClientStub::DoIpcCommand(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &replyMsg)
70 {
71 SHARING_LOGD("traces.");
72 auto listener = stubListener_.lock();
73 if (listener) {
74 listener->OnIpcRequest(msg, replyMsg);
75 } else {
76 SHARING_LOGE("stub listener is null!");
77 }
78
79 return 0;
80 }
81
OnRemoteDied()82 void InterIpcClientStub::OnRemoteDied()
83 {
84 SHARING_LOGD("trace.");
85 auto listener = stubListener_.lock();
86 if (listener) {
87 listener->OnRemoteDied();
88 }
89 }
90
91 } // namespace Sharing
92 } // namespace OHOS