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_manager.h"
17 #include "access_token.h"
18 #include "accesstoken_kit.h"
19 #include "hap_token_info.h"
20 #include "interaction/device_kit/dm_kit.h"
21 #include "interaction/domain/domain_manager.h"
22 #include "interaction/ipc_codec/ipc_msg.h"
23 #include "nativetoken_kit.h"
24 #include "token_setproc.h"
25
26 namespace OHOS {
27 namespace Sharing {
28
DomainRpcManager()29 DomainRpcManager::DomainRpcManager()
30 {
31 SHARING_LOGD("trace.");
32 domainType_ = DOMAIN_TYPE_RPC;
33 }
34
~DomainRpcManager()35 DomainRpcManager::~DomainRpcManager()
36 {
37 SHARING_LOGD("trace.");
38 DeInit();
39 }
40
SendDomainRequest(std::string remoteId,std::shared_ptr<BaseDomainMsg> BaseMsg)41 int32_t DomainRpcManager::SendDomainRequest(std::string remoteId, std::shared_ptr<BaseDomainMsg> BaseMsg)
42 {
43 SHARING_LOGD("trace.");
44 BaseMsg->fromDevId = DmKit::GetLocalDevicesInfo().deviceId;
45 SHARING_LOGD("msg from %{public}s -> to %{public}s.", BaseMsg->fromDevId.c_str(), BaseMsg->toDevId.c_str());
46
47 if (rpcClients_.find(BaseMsg->toDevId) != rpcClients_.end()) {
48 rpcClients_[BaseMsg->toDevId]->SendDomainRequest(remoteId, BaseMsg);
49 } else {
50 SHARING_LOGE("rpc client is null.");
51 }
52
53 return 0;
54 }
55
IsPeerExist(std::string peerId)56 bool DomainRpcManager::IsPeerExist(std::string peerId)
57 {
58 SHARING_LOGD("trace.");
59 if (rpcClients_.find(peerId) != rpcClients_.end()) {
60 return true;
61 }
62
63 return false;
64 }
65
AddDomainRpcService(DomainRpcService * service)66 int32_t DomainRpcManager::AddDomainRpcService(DomainRpcService *service)
67 {
68 SHARING_LOGD("trace.");
69 localService_ = service;
70 localService_->SetPeerListener(shared_from_this());
71 DomainManager::GetInstance()->AddServiceManager(shared_from_this());
72 return 0;
73 }
74
Init()75 int32_t DomainRpcManager::Init()
76 {
77 SHARING_LOGD("trace.");
78 return 0;
79 }
80
DeInit()81 int32_t DomainRpcManager::DeInit()
82 {
83 SHARING_LOGD("trace.");
84 ClearRpcClient();
85 std::unique_lock lock(mutex_);
86 localService_ = nullptr;
87 return 0;
88 }
89
AddRpcClient(std::string remoteId)90 int32_t DomainRpcManager::AddRpcClient(std::string remoteId)
91 {
92 SHARING_LOGD("remoteId: %{public}s.", remoteId.c_str());
93 std::unique_lock lock(mutex_);
94 std::shared_ptr<DomainRpcClient> client = std::make_shared<DomainRpcClient>();
95 if (client->GetDomainProxy(remoteId) == nullptr) {
96 SHARING_LOGE("failed.");
97 return -1;
98 }
99
100 client->Initialize();
101 client->SetPeerListener(shared_from_this());
102 rpcClients_.insert(std::make_pair(remoteId, client));
103
104 auto listener = transMgrListener_.lock();
105 if (listener != nullptr) {
106 listener->AddPeer(shared_from_this(), client);
107 }
108
109 return 0;
110 }
111
AddRpcClient(std::string remoteId,sptr<IDomainRpcService> peerProxy)112 int32_t DomainRpcManager::AddRpcClient(std::string remoteId, sptr<IDomainRpcService> peerProxy)
113 {
114 SHARING_LOGD("remoteId: %{public}s.", remoteId.c_str());
115 std::unique_lock lock(mutex_);
116 std::shared_ptr<DomainRpcClient> client = std::make_shared<DomainRpcClient>();
117 client->SetDomainProxy(peerProxy);
118 client->SetPeerListener(shared_from_this());
119 client->SetRemoteId(remoteId);
120 rpcClients_.insert(std::make_pair(remoteId, client));
121
122 auto listener = transMgrListener_.lock();
123 if (listener != nullptr) {
124 listener->AddPeer(shared_from_this(), client);
125 }
126
127 return 0;
128 }
129
DelRpcClient(std::string remoteId)130 int32_t DomainRpcManager::DelRpcClient(std::string remoteId)
131 {
132 SHARING_LOGD("remoteId: %{public}s.", remoteId.c_str());
133 std::unique_lock lock(mutex_);
134 if (rpcClients_.find(remoteId) != rpcClients_.end()) {
135 rpcClients_.erase(remoteId);
136 }
137
138 auto listener = transMgrListener_.lock();
139 if (listener != nullptr) {
140 listener->DelPeer(remoteId);
141 }
142
143 return 0;
144 }
145
ClearRpcClient()146 int32_t DomainRpcManager::ClearRpcClient()
147 {
148 SHARING_LOGD("trace.");
149 std::unique_lock lock(mutex_);
150 rpcClients_.clear();
151 return 0;
152 }
153
OnDomainRequest(std::string remoteId,std::shared_ptr<BaseDomainMsg> BaseMsg)154 void DomainRpcManager::OnDomainRequest(std::string remoteId, std::shared_ptr<BaseDomainMsg> BaseMsg)
155 {
156 SHARING_LOGD("trace.");
157 auto listener = transMgrListener_.lock();
158 if (listener) {
159 listener->OnDomainRequest(remoteId, BaseMsg);
160 } else {
161 SHARING_LOGE("peer listener is null.");
162 }
163 }
164
OnPeerConnected(std::string remoteId)165 void DomainRpcManager::OnPeerConnected(std::string remoteId)
166 {
167 SHARING_LOGI("remoteId: %{public}s.", remoteId.c_str());
168 }
169
OnPeerDisconnected(std::string remoteId)170 void DomainRpcManager::OnPeerDisconnected(std::string remoteId)
171 {
172 SHARING_LOGI("remoteId: %{public}s.", remoteId.c_str());
173 DelRpcClient(remoteId);
174 }
175
176 } // namespace Sharing
177 } // namespace OHOS