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_client.h"
17 #include "iservice_registry.h"
18 #include "system_ability_definition.h"
19
20 namespace OHOS {
21 namespace Sharing {
22
DomainRpcClient()23 DomainRpcClient::DomainRpcClient() : IDomainPeer(REMOTE_CALLER_CLIENT)
24 {
25 SHARING_LOGD("trace.");
26 domainType_ = DOMAIN_TYPE_RPC;
27 }
28
OnRemoteDied(std::string deviceId)29 void DomainRpcClient::OnRemoteDied(std::string deviceId)
30 {
31 SHARING_LOGD("trace.");
32 std::lock_guard<std::mutex> lock(mutex_);
33 if (domainProxy_ != nullptr) {
34 (void)domainProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
35 domainProxy_ = nullptr;
36 }
37
38 auto listener = peerListener_.lock();
39 if (listener != nullptr) {
40 listener->OnPeerDisconnected(remoteId_);
41 }
42
43 if (deathRecipient_ != nullptr) {
44 deathRecipient_ = nullptr;
45 }
46
47 listenerStub_ = nullptr;
48 remoteId_ = "";
49 }
50
GetDomainProxy(std::string deviceId)51 sptr<IDomainRpcService> DomainRpcClient::GetDomainProxy(std::string deviceId)
52 {
53 SHARING_LOGD("trace.");
54 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
55 if (saMgr == nullptr) {
56 SHARING_LOGE("get system ability manager failed.");
57 return nullptr;
58 }
59
60 sptr<IRemoteObject> object = saMgr->GetSystemAbility(SHARING_SERVICE_DOMAIN_TEMP_SA_ID, deviceId);
61 if (object == nullptr) {
62 SHARING_LOGE("get system ability failed id: %{public}d.", SHARING_SERVICE_DOMAIN_TEMP_SA_ID);
63 return nullptr;
64 } else {
65 SHARING_LOGD("GetSystemAbility success.");
66 remoteId_ = deviceId;
67 }
68
69 sptr<IDomainRpcService> proxy = iface_cast<IDomainRpcService>(object);
70 if (proxy == nullptr) {
71 SHARING_LOGE("iface_cast IDomainRpcService failed id: %{public}d.", SHARING_SERVICE_DOMAIN_TEMP_SA_ID);
72 return nullptr;
73 } else {
74 SHARING_LOGD("get domain proxy iface_cast success.");
75 }
76
77 deathRecipient_ = new (std::nothrow) DomainRpcDeathRecipient(deviceId);
78 if (deathRecipient_ == nullptr) {
79 SHARING_LOGE("deathRecipient create failed.");
80 return nullptr;
81 }
82 deathRecipient_->SetDeathListener(shared_from_this());
83 if (!object->AddDeathRecipient(deathRecipient_)) {
84 SHARING_LOGE("add IDomainRpcService death recipient failed.");
85 return nullptr;
86 } else {
87 SHARING_LOGD("get domain AddDeathRecipient success.");
88 }
89
90 auto listener = peerListener_.lock();
91 if (listener) {
92 listener->OnPeerConnected(deviceId);
93 }
94
95 domainProxy_ = proxy;
96 SHARING_LOGD("get domain proxy success.");
97 return proxy;
98 }
99
SetDomainProxy(sptr<IDomainRpcService> peerProxy)100 void DomainRpcClient::SetDomainProxy(sptr<IDomainRpcService> peerProxy)
101 {
102 SHARING_LOGD("trace.");
103 domainProxy_ = peerProxy;
104 }
105
GetSubProxy(int32_t type)106 sptr<IDomainRpcService> DomainRpcClient::GetSubProxy(int32_t type)
107 {
108 SHARING_LOGD("trace.");
109 if (domainProxy_ == nullptr) {
110 SHARING_LOGE("get subsystem ability '%{public}d' proxy null.", type);
111 return nullptr;
112 }
113
114 sptr<IRemoteObject> object = domainProxy_->GetSubSystemAbility(type);
115 if (!object) {
116 SHARING_LOGE("get subsystem ability type '%{public}d' not exist.", type);
117 return nullptr;
118 }
119
120 sptr<IDomainRpcService> subProxy = iface_cast<IDomainRpcService>(object);
121 if (!subProxy) {
122 SHARING_LOGE("get subsystem ability '%{public}d' iface_cast null.", type);
123 return nullptr;
124 }
125
126 return subProxy;
127 }
128
CreateListenerObject()129 int32_t DomainRpcClient::CreateListenerObject()
130 {
131 SHARING_LOGD("trace.");
132 if (domainProxy_ == nullptr) {
133 SHARING_LOGE("proxy null.");
134 return -1;
135 }
136
137 listenerStub_ = new (std::nothrow) DomainRpcServiceStub();
138 if (listenerStub_ == nullptr) {
139 SHARING_LOGE("listenerStub create failed.");
140 return -1;
141 }
142 listenerStub_->SetStubListener(shared_from_this());
143 sptr<IRemoteObject> object = listenerStub_->AsObject();
144
145 return domainProxy_->SetListenerObject(object);
146 }
147
Initialize()148 void DomainRpcClient::Initialize()
149 {
150 SHARING_LOGD("trace.");
151 std::lock_guard<std::mutex> lock(mutex_);
152 if (domainProxy_) {
153 CreateListenerObject();
154 } else {
155 SHARING_LOGE("new StandardClient proxy null.");
156 }
157 }
158
SetPeerListener(std::weak_ptr<IDomainPeerListener> listener)159 void DomainRpcClient::SetPeerListener(std::weak_ptr<IDomainPeerListener> listener)
160 {
161 SHARING_LOGD("trace.");
162 peerListener_ = listener;
163 }
164
SendDomainRequest(std::string remoteId,std::shared_ptr<BaseDomainMsg> BaseMsg)165 int32_t DomainRpcClient::SendDomainRequest(std::string remoteId, std::shared_ptr<BaseDomainMsg> BaseMsg)
166 {
167 SHARING_LOGD("trace.");
168 std::shared_ptr<BaseDomainMsg> replyMsg = std::make_shared<BaseDomainMsg>();
169 if (domainProxy_ != nullptr) {
170 domainProxy_->DoRpcCommand(BaseMsg, replyMsg);
171 } else {
172 SHARING_LOGE("domain proxy is null.");
173 }
174
175 return 0;
176 }
177
OnDomainRequest(std::shared_ptr<BaseDomainMsg> msg)178 void DomainRpcClient::OnDomainRequest(std::shared_ptr<BaseDomainMsg> msg)
179 {
180 SHARING_LOGD("trace.");
181 auto listener = peerListener_.lock();
182 if (listener != nullptr) {
183 listener->OnDomainRequest(remoteId_, msg);
184 } else {
185 SHARING_LOGE("peer listener is null.");
186 }
187 }
188
189 } // namespace Sharing
190 } // namespace OHOS
191