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     if (rpcClients_.find(BaseMsg->toDevId) != rpcClients_.end()) {
51         rpcClients_[BaseMsg->toDevId]->SendDomainRequest(remoteId, BaseMsg);
52     } else {
53         SHARING_LOGE("rpc client is null.");
54     }
55 
56     return 0;
57 }
58 
IsPeerExist(std::string peerId)59 bool DomainRpcManager::IsPeerExist(std::string peerId)
60 {
61     SHARING_LOGD("trace.");
62     if (rpcClients_.find(peerId) != rpcClients_.end()) {
63         return true;
64     }
65 
66     return false;
67 }
68 
AddDomainRpcService(DomainRpcService *service)69 int32_t DomainRpcManager::AddDomainRpcService(DomainRpcService *service)
70 {
71     SHARING_LOGD("trace.");
72     RETURN_INVALID_IF_NULL(service);
73     localService_ = service;
74     localService_->SetPeerListener(shared_from_this());
75     DomainManager::GetInstance()->AddServiceManager(shared_from_this());
76     return 0;
77 }
78 
Init()79 int32_t DomainRpcManager::Init()
80 {
81     SHARING_LOGD("trace.");
82     return 0;
83 }
84 
DeInit()85 int32_t DomainRpcManager::DeInit()
86 {
87     SHARING_LOGD("trace.");
88     ClearRpcClient();
89     std::unique_lock lock(mutex_);
90     localService_ = nullptr;
91     return 0;
92 }
93 
AddRpcClient(std::string remoteId)94 int32_t DomainRpcManager::AddRpcClient(std::string remoteId)
95 {
96     SHARING_LOGD("remoteId: %{public}s.", remoteId.c_str());
97     std::unique_lock lock(mutex_);
98     std::shared_ptr<DomainRpcClient> client = std::make_shared<DomainRpcClient>();
99     if (client->GetDomainProxy(remoteId) == nullptr) {
100         SHARING_LOGE("failed.");
101         return -1;
102     }
103 
104     client->Initialize();
105     client->SetPeerListener(shared_from_this());
106     rpcClients_.insert(std::make_pair(remoteId, client));
107 
108     auto listener = transMgrListener_.lock();
109     if (listener != nullptr) {
110         listener->AddPeer(shared_from_this(), client);
111     }
112 
113     return 0;
114 }
115 
AddRpcClient(std::string remoteId, sptr<IDomainRpcService> peerProxy)116 int32_t DomainRpcManager::AddRpcClient(std::string remoteId, sptr<IDomainRpcService> peerProxy)
117 {
118     SHARING_LOGD("remoteId: %{public}s.", remoteId.c_str());
119     std::unique_lock lock(mutex_);
120     std::shared_ptr<DomainRpcClient> client = std::make_shared<DomainRpcClient>();
121     client->SetDomainProxy(peerProxy);
122     client->SetPeerListener(shared_from_this());
123     client->SetRemoteId(remoteId);
124     rpcClients_.insert(std::make_pair(remoteId, client));
125 
126     auto listener = transMgrListener_.lock();
127     if (listener != nullptr) {
128         listener->AddPeer(shared_from_this(), client);
129     }
130 
131     return 0;
132 }
133 
DelRpcClient(std::string remoteId)134 int32_t DomainRpcManager::DelRpcClient(std::string remoteId)
135 {
136     SHARING_LOGD("remoteId: %{public}s.", remoteId.c_str());
137     std::unique_lock lock(mutex_);
138     if (rpcClients_.find(remoteId) != rpcClients_.end()) {
139         rpcClients_.erase(remoteId);
140     }
141 
142     auto listener = transMgrListener_.lock();
143     if (listener != nullptr) {
144         listener->DelPeer(remoteId);
145     }
146 
147     return 0;
148 }
149 
ClearRpcClient()150 int32_t DomainRpcManager::ClearRpcClient()
151 {
152     SHARING_LOGD("trace.");
153     std::unique_lock lock(mutex_);
154     rpcClients_.clear();
155     return 0;
156 }
157 
OnDomainRequest(std::string remoteId, std::shared_ptr<BaseDomainMsg> BaseMsg)158 void DomainRpcManager::OnDomainRequest(std::string remoteId, std::shared_ptr<BaseDomainMsg> BaseMsg)
159 {
160     SHARING_LOGD("trace.");
161     auto listener = transMgrListener_.lock();
162     if (listener) {
163         listener->OnDomainRequest(remoteId, BaseMsg);
164     } else {
165         SHARING_LOGE("peer listener is null.");
166     }
167 }
168 
OnPeerConnected(std::string remoteId)169 void DomainRpcManager::OnPeerConnected(std::string remoteId)
170 {
171     SHARING_LOGI("remoteId: %{public}s.", remoteId.c_str());
172 }
173 
OnPeerDisconnected(std::string remoteId)174 void DomainRpcManager::OnPeerDisconnected(std::string remoteId)
175 {
176     SHARING_LOGI("remoteId: %{public}s.", remoteId.c_str());
177     DelRpcClient(remoteId);
178 }
179 
180 } // namespace Sharing
181 } // namespace OHOS