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 deathRecipients_[key] = deathRecipient;
53 CreateDeathListener(key);
54
55 if (peerProxy->AsObject() != nullptr) {
56 if (!peerProxy->AsObject()->AddDeathRecipient(deathRecipient)) {
57 SHARING_LOGE("add death recipient failed.");
58 return -1;
59 }
60 }
61
62 return ERR_NONE;
63 }
64
DoIpcCommand(std::shared_ptr<BaseMsg> msg,std::shared_ptr<BaseMsg> & replyMsg)65 int32_t InterIpcClientStub::DoIpcCommand(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &replyMsg)
66 {
67 SHARING_LOGD("traces.");
68 auto listener = stubListener_.lock();
69 if (listener) {
70 listener->OnIpcRequest(msg, replyMsg);
71 } else {
72 SHARING_LOGE("stub listener is null!");
73 }
74
75 return 0;
76 }
77
OnRemoteDied()78 void InterIpcClientStub::OnRemoteDied()
79 {
80 SHARING_LOGD("trace.");
81 auto listener = stubListener_.lock();
82 if (listener) {
83 listener->OnRemoteDied();
84 }
85 }
86
87 } // namespace Sharing
88 } // namespace OHOS