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_service.h"
17 #include "access_token.h"
18 #include "accesstoken_kit.h"
19 #include "common/common_macro.h"
20 #include "domain_rpc_service_death_listener.h"
21 #include "hap_token_info.h"
22 #include "interaction/domain/domain_manager.h"
23 #include "interaction/domain/rpc/domain_rpc_manager.h"
24 #include "ipc_skeleton.h"
25 #include "nativetoken_kit.h"
26 #include "token_setproc.h"
27
28 namespace OHOS {
29 namespace Sharing {
30 REGISTER_SYSTEM_ABILITY_BY_ID(DomainRpcService, SHARING_SERVICE_DOMAIN_TEMP_SA_ID, true)
31
DomainRpcService(int32_t systemAbilityId, bool runOnCreate)32 DomainRpcService::DomainRpcService(int32_t systemAbilityId, bool runOnCreate)
33 : SystemAbility(systemAbilityId, runOnCreate), IDomainPeer(REMOTE_CALLER_SERVER)
34 {
35 SHARING_LOGD("trace.");
36 }
37
~DomainRpcService()38 DomainRpcService::~DomainRpcService()
39 {
40 SHARING_LOGD("trace.");
41 std::unique_lock<std::mutex> lock(mutex_);
42 DomainRpcStubs_.clear();
43 shared_from_this_.reset();
44 }
45
OnDump()46 void DomainRpcService::OnDump()
47 {
48 SHARING_LOGD("trace.");
49 }
50
OnStart()51 void DomainRpcService::OnStart()
52 {
53 SHARING_LOGD("trace.");
54 if (Publish(this)) {
55 SHARING_LOGD("success.");
56 DomainRpcManager::GetInstance()->Init();
57 DomainRpcManager::GetInstance()->AddDomainRpcService(this);
58 } else {
59 SHARING_LOGD("start failed.");
60 }
61 }
62
OnStop()63 void DomainRpcService::OnStop()
64 {
65 SHARING_LOGD("trace.");
66 }
67
GetSubSystemAbility(int32_t type)68 sptr<IRemoteObject> DomainRpcService::GetSubSystemAbility(int32_t type)
69 {
70 SHARING_LOGD("trace.");
71 std::unique_lock<std::mutex> lock(mutex_);
72 sptr<IRemoteObject> peerObject = IPCSkeleton::GetContextObject();
73 sptr<DomainRpcServiceStub> stub = new (std::nothrow) DomainRpcServiceStub();
74 if (stub == nullptr) {
75 SHARING_LOGE("stub null");
76 return nullptr;
77 }
78
79 sptr<IRemoteObject> object = stub->AsObject();
80 std::string remoteId = IPCSkeleton::GetCallingDeviceID();
81 DomainRpcStubs_.insert(std::make_pair(remoteId, object));
82
83 return object;
84 }
85
DoRpcCommand(std::shared_ptr<BaseDomainMsg> msg, std::shared_ptr<BaseDomainMsg> replyMsg)86 int32_t DomainRpcService::DoRpcCommand(std::shared_ptr<BaseDomainMsg> msg, std::shared_ptr<BaseDomainMsg> replyMsg)
87 {
88 RETURN_INVALID_IF_NULL(msg);
89 (void)replyMsg;
90 SHARING_LOGD("msg from %{public}s -> to %{public}s.", GetAnonyString(msg->fromDevId).c_str(),
91 GetAnonyString(msg->toDevId).c_str());
92 auto listener = peerListener_.lock();
93 if (listener) {
94 listener->OnDomainRequest(msg->fromDevId, msg);
95 } else {
96 SHARING_LOGE("peer listener is null!");
97 }
98
99 return 0;
100 }
101
CreateDeathListener(std::string deviceId)102 void DomainRpcService::CreateDeathListener(std::string deviceId)
103 {
104 SHARING_LOGD("deviceId: %{public}s.", GetAnonyString(deviceId).c_str());
105 if (deathRecipients_.find(deviceId) != deathRecipients_.end()) {
106 auto listener = std::make_shared<DomainRpcServiceDeathListener>();
107 if (shared_from_this_ == nullptr) {
108 shared_from_this_ = Ptr(this, [](DomainRpcService *) { SHARING_LOGD("trace."); });
109 }
110 listener->SetService(shared_from_this_);
111 deathRecipients_[deviceId]->SetDeathListener(listener);
112 } else {
113 SHARING_LOGE("deviceId not find: %{public}s.", GetAnonyString(deviceId).c_str());
114 }
115 }
116
SetPeerListener(std::weak_ptr<IDomainPeerListener> listener)117 void DomainRpcService::SetPeerListener(std::weak_ptr<IDomainPeerListener> listener)
118 {
119 SHARING_LOGD("trace.");
120 peerListener_ = listener;
121 }
122
SendDomainRequest(std::string remoteId, std::shared_ptr<BaseDomainMsg> msg)123 int32_t DomainRpcService::SendDomainRequest(std::string remoteId, std::shared_ptr<BaseDomainMsg> msg)
124 {
125 RETURN_INVALID_IF_NULL(msg);
126 SHARING_LOGD("msg from %{public}s -> to %{public}s.", GetAnonyString(msg->fromDevId).c_str(),
127 GetAnonyString(msg->toDevId).c_str());
128 return 0;
129 }
130
DelPeerProxy(std::string remoteId)131 void DomainRpcService::DelPeerProxy(std::string remoteId)
132 {
133 SHARING_LOGD("remoteId: %{public}s.", remoteId.c_str());
134 std::lock_guard<std::mutex> lock(mutex_);
135 if (peerProxys_.find(remoteId) != peerProxys_.end()) {
136 peerProxys_[remoteId]->AsObject()->RemoveDeathRecipient(deathRecipients_[remoteId]);
137 peerProxys_.erase(remoteId);
138 deathRecipients_.erase(remoteId);
139 auto listener = peerListener_.lock();
140 if (listener) {
141 listener->OnPeerDisconnected(remoteId);
142 }
143 } else {
144 SHARING_LOGE("remoteId: %{public}s not find.", remoteId.c_str());
145 }
146 }
147
148 } // namespace Sharing
149 } // namespace OHOS
150