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