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