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 "domain_rpc_service_stub.h"
17 #include "common/sharing_log.h"
18 #include "interaction/domain/domain_def.h"
19 #include "interaction/domain/domain_manager.h"
20 #include "interaction/domain/rpc/domain_rpc_manager.h"
21 #include "ipc_msg_decoder.h"
22 #include "ipc_skeleton.h"
23
24 namespace OHOS {
25 namespace Sharing {
26
~DomainRpcServiceStub()27 DomainRpcServiceStub::~DomainRpcServiceStub()
28 {
29 SHARING_LOGD("trace.");
30 for (auto deathRecipient : deathRecipients_) {
31 if (deathRecipient.second != nullptr) {
32 deathRecipient.second->SetDeathListener(nullptr);
33 deathRecipient.second = nullptr;
34 }
35 }
36 }
37
SetStubListener(std::weak_ptr<IDomainStubListener> listener)38 void DomainRpcServiceStub::SetStubListener(std::weak_ptr<IDomainStubListener> listener)
39 {
40 SHARING_LOGD("trace.");
41 stubListener_ = listener;
42 }
43
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)44 int DomainRpcServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
45 MessageOption &option)
46 {
47 SHARING_LOGD("trace.");
48 switch (code) {
49 case DomainServiceMsg::DOMAIN_MSG: {
50 SHARING_LOGD("DOMAIN_MSG.");
51 DoRpcCommand(data, reply);
52 break;
53 }
54 case DomainServiceMsg::SET_LISTENER_OBJ:
55 SHARING_LOGD("SET_LISTENER_OBJ.");
56 SetListenerObject(data, reply);
57 break;
58 case DomainServiceMsg::GET_SUBSYSTEM:
59 SHARING_LOGD("GET_SUBSYSTEM.");
60 GetSystemAbility(data, reply);
61 break;
62 default:
63 SHARING_LOGI("none process case.");
64 break;
65 }
66
67 return 0;
68 }
69
SetListenerObject(MessageParcel & data,MessageParcel & reply)70 int32_t DomainRpcServiceStub::SetListenerObject(MessageParcel &data, MessageParcel &reply)
71 {
72 SHARING_LOGD("trace.");
73 sptr<IRemoteObject> object = data.ReadRemoteObject();
74 (void)reply.WriteInt32(SetListenerObject(object));
75 return ERR_NONE;
76 }
77
SetListenerObject(const sptr<IRemoteObject> & object)78 int32_t DomainRpcServiceStub::SetListenerObject(const sptr<IRemoteObject> &object)
79 {
80 SHARING_LOGD("trace.");
81 if (object == nullptr) {
82 SHARING_LOGE("domain rpc null listener object.");
83 return -1;
84 }
85
86 sptr<IDomainRpcService> peerProxy = iface_cast<IDomainRpcService>(object);
87 if (peerProxy == nullptr) {
88 SHARING_LOGE("convert null.");
89 return -1;
90 }
91
92 std::string peerDevId = IPCSkeleton::GetCallingDeviceID();
93 std::unique_lock<std::mutex> lock(mutex_);
94 peerProxys_[peerDevId] = peerProxy;
95 lock.unlock();
96 SHARING_LOGD("peer deviceId: %{public}s, listener num: %{public}zu.", GetAnonymousDeviceId(peerDevId).c_str(),
97 peerProxys_.size());
98
99 auto deathRecipient = new (std::nothrow) DomainRpcDeathRecipient(peerDevId);
100 if (deathRecipient == nullptr) {
101 SHARING_LOGE("deathRecipient create failed.");
102 return -1;
103 }
104 deathRecipients_[peerDevId] = deathRecipient;
105 CreateDeathListener(peerDevId);
106
107 if (peerProxy->AsObject() != nullptr) {
108 if (!peerProxy->AsObject()->AddDeathRecipient(deathRecipient)) {
109 SHARING_LOGE("add death recipient failed.");
110 return -1;
111 }
112 }
113
114 DomainRpcManager::GetInstance()->AddRpcClient(peerDevId, peerProxy);
115 return ERR_NONE;
116 }
117
DoRpcCommand(MessageParcel & data,MessageParcel & reply)118 int32_t DomainRpcServiceStub::DoRpcCommand(MessageParcel &data, MessageParcel &reply)
119 {
120 SHARING_LOGD("trace.");
121 std::shared_ptr<BaseDomainMsg> msg = nullptr;
122 IpcMsgDecoder::GetInstance().DomainMsgDecode(msg, data);
123 if (msg == nullptr) {
124 SHARING_LOGE("msg null.");
125 return -1;
126 }
127
128 std::shared_ptr<BaseDomainMsg> replyMsg = std::make_shared<BaseDomainMsg>();
129 DoRpcCommand(msg, replyMsg);
130 return 0;
131 }
132
GetSystemAbility(MessageParcel & data,MessageParcel & reply)133 int32_t DomainRpcServiceStub::GetSystemAbility(MessageParcel &data, MessageParcel &reply)
134 {
135 SHARING_LOGD("trace.");
136 int32_t subtype = data.ReadInt32();
137 SHARING_LOGD("subtype: %{public}d.", subtype);
138 (void)reply.WriteRemoteObject(GetSubSystemAbility(subtype));
139
140 return ERR_NONE;
141 }
142
DoRpcCommand(std::shared_ptr<BaseDomainMsg> msg,std::shared_ptr<BaseDomainMsg> replyMsg)143 int32_t DomainRpcServiceStub::DoRpcCommand(std::shared_ptr<BaseDomainMsg> msg, std::shared_ptr<BaseDomainMsg> replyMsg)
144 {
145 SHARING_LOGD("traces.");
146 auto listener = stubListener_.lock();
147 if (listener) {
148 listener->OnDomainRequest(msg);
149 } else {
150 SHARING_LOGE("stub listener is null.");
151 }
152
153 return 0;
154 }
155
GetSubSystemAbility(int32_t type)156 sptr<IRemoteObject> DomainRpcServiceStub::GetSubSystemAbility(int32_t type)
157 {
158 SHARING_LOGD("trace.");
159 return nullptr;
160 }
161
CreateDeathListener(std::string key)162 void DomainRpcServiceStub::CreateDeathListener(std::string key)
163 {
164 SHARING_LOGD("trace.");
165 }
166
167 } // namespace Sharing
168 } // namespace OHOS
169