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.", GetAnonyString(peerDevId).c_str(),
97 peerProxys_.size());
98
99 auto deathRecipient = new (std::nothrow) DomainRpcDeathRecipient(peerDevId);
100 deathRecipients_[peerDevId] = deathRecipient;
101 CreateDeathListener(peerDevId);
102
103 if (peerProxy->AsObject() != nullptr) {
104 if (!peerProxy->AsObject()->AddDeathRecipient(deathRecipient)) {
105 SHARING_LOGE("add death recipient failed.");
106 return -1;
107 }
108 }
109
110 DomainRpcManager::GetInstance()->AddRpcClient(peerDevId, peerProxy);
111 return ERR_NONE;
112 }
113
DoRpcCommand(MessageParcel & data,MessageParcel & reply)114 int32_t DomainRpcServiceStub::DoRpcCommand(MessageParcel &data, MessageParcel &reply)
115 {
116 SHARING_LOGD("trace.");
117 std::shared_ptr<BaseDomainMsg> msg = nullptr;
118 IpcMsgDecoder::GetInstance().DomainMsgDecode(msg, data);
119 if (msg == nullptr) {
120 SHARING_LOGE("msg null.");
121 return -1;
122 }
123
124 std::shared_ptr<BaseDomainMsg> replyMsg = std::make_shared<BaseDomainMsg>();
125 DoRpcCommand(msg, replyMsg);
126 return 0;
127 }
128
GetSystemAbility(MessageParcel & data,MessageParcel & reply)129 int32_t DomainRpcServiceStub::GetSystemAbility(MessageParcel &data, MessageParcel &reply)
130 {
131 SHARING_LOGD("trace.");
132 int32_t subtype = data.ReadInt32();
133 SHARING_LOGD("subtype: %{public}d.", subtype);
134 (void)reply.WriteRemoteObject(GetSubSystemAbility(subtype));
135
136 return ERR_NONE;
137 }
138
DoRpcCommand(std::shared_ptr<BaseDomainMsg> msg,std::shared_ptr<BaseDomainMsg> replyMsg)139 int32_t DomainRpcServiceStub::DoRpcCommand(std::shared_ptr<BaseDomainMsg> msg, std::shared_ptr<BaseDomainMsg> replyMsg)
140 {
141 SHARING_LOGD("traces.");
142 auto listener = stubListener_.lock();
143 if (listener) {
144 listener->OnDomainRequest(msg);
145 } else {
146 SHARING_LOGE("stub listener is null.");
147 }
148
149 return 0;
150 }
151
GetSubSystemAbility(int32_t type)152 sptr<IRemoteObject> DomainRpcServiceStub::GetSubSystemAbility(int32_t type)
153 {
154 SHARING_LOGD("trace.");
155 return nullptr;
156 }
157
CreateDeathListener(std::string key)158 void DomainRpcServiceStub::CreateDeathListener(std::string key)
159 {
160 SHARING_LOGD("trace.");
161 }
162
163 } // namespace Sharing
164 } // namespace OHOS
165