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 deathRecipient_->SetDeathListener(shared_from_this());
79 if (!object->AddDeathRecipient(deathRecipient_)) {
80 SHARING_LOGE("add IDomainRpcService death recipient failed.");
81 return nullptr;
82 } else {
83 SHARING_LOGD("get domain AddDeathRecipient success.");
84 }
85
86 auto listener = peerListener_.lock();
87 if (listener) {
88 listener->OnPeerConnected(deviceId);
89 }
90
91 domainProxy_ = proxy;
92 SHARING_LOGD("get domain proxy success.");
93 return proxy;
94 }
95
SetDomainProxy(sptr<IDomainRpcService> peerProxy)96 void DomainRpcClient::SetDomainProxy(sptr<IDomainRpcService> peerProxy)
97 {
98 SHARING_LOGD("trace.");
99 domainProxy_ = peerProxy;
100 }
101
GetSubProxy(int32_t type)102 sptr<IDomainRpcService> DomainRpcClient::GetSubProxy(int32_t type)
103 {
104 SHARING_LOGD("trace.");
105 if (domainProxy_ == nullptr) {
106 SHARING_LOGE("get subsystem ability '%{public}d' proxy null.", type);
107 return nullptr;
108 }
109
110 sptr<IRemoteObject> object = domainProxy_->GetSubSystemAbility(type);
111 if (!object) {
112 SHARING_LOGE("get subsystem ability type '%{public}d' not exist.", type);
113 return nullptr;
114 }
115
116 sptr<IDomainRpcService> subProxy = iface_cast<IDomainRpcService>(object);
117 if (!subProxy) {
118 SHARING_LOGE("get subsystem ability '%{public}d' iface_cast null.", type);
119 return nullptr;
120 }
121
122 return subProxy;
123 }
124
CreateListenerObject()125 int32_t DomainRpcClient::CreateListenerObject()
126 {
127 SHARING_LOGD("trace.");
128 if (domainProxy_ == nullptr) {
129 SHARING_LOGE("proxy null.");
130 return -1;
131 }
132
133 listenerStub_ = new (std::nothrow) DomainRpcServiceStub();
134 listenerStub_->SetStubListener(shared_from_this());
135 sptr<IRemoteObject> object = listenerStub_->AsObject();
136
137 return domainProxy_->SetListenerObject(object);
138 }
139
Initialize()140 void DomainRpcClient::Initialize()
141 {
142 SHARING_LOGD("trace.");
143 std::lock_guard<std::mutex> lock(mutex_);
144 if (domainProxy_) {
145 CreateListenerObject();
146 } else {
147 SHARING_LOGE("new StandardClient proxy null.");
148 }
149 }
150
SetPeerListener(std::weak_ptr<IDomainPeerListener> listener)151 void DomainRpcClient::SetPeerListener(std::weak_ptr<IDomainPeerListener> listener)
152 {
153 SHARING_LOGD("trace.");
154 peerListener_ = listener;
155 }
156
SendDomainRequest(std::string remoteId,std::shared_ptr<BaseDomainMsg> BaseMsg)157 int32_t DomainRpcClient::SendDomainRequest(std::string remoteId, std::shared_ptr<BaseDomainMsg> BaseMsg)
158 {
159 SHARING_LOGD("trace.");
160 std::shared_ptr<BaseDomainMsg> replyMsg = std::make_shared<BaseDomainMsg>();
161 if (domainProxy_ != nullptr) {
162 domainProxy_->DoRpcCommand(BaseMsg, replyMsg);
163 } else {
164 SHARING_LOGE("domain proxy is null.");
165 }
166
167 return 0;
168 }
169
OnDomainRequest(std::shared_ptr<BaseDomainMsg> msg)170 void DomainRpcClient::OnDomainRequest(std::shared_ptr<BaseDomainMsg> msg)
171 {
172 SHARING_LOGD("trace.");
173 auto listener = peerListener_.lock();
174 if (listener != nullptr) {
175 listener->OnDomainRequest(remoteId_, msg);
176 } else {
177 SHARING_LOGE("peer listener is null.");
178 }
179 }
180
181 } // namespace Sharing
182 } // namespace OHOS
183