1 // Copyright (C) 2024 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #include "system_ability_manager_wrapper.h"
15 
16 #include <algorithm>
17 #include <cstdint>
18 #include <iostream>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "cxx.h"
24 #include "ipc_skeleton.h"
25 #include "iservice_registry.h"
26 #include "isystem_ability_status_change.h"
27 #include "isystem_process_status_change.h"
28 #include "refbase.h"
29 #include "status_change_wrapper.h"
30 #include "string_ex.h"
31 #include "wrapper.rs.h"
32 
33 namespace OHOS {
34 namespace SamgrRust {
35 
36 static constexpr size_t MAX_RUST_STR_LEN = 1024;
37 
ListSystemAbilities()38 rust::Vec<rust::String> ListSystemAbilities()
39 {
40     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
41     auto res = rust::Vec<rust::String>();
42 
43     if (sysm == nullptr) {
44         return res;
45     }
46 
47     auto list = sysm->ListSystemAbilities();
48     for (auto s : list) {
49         res.push_back(s.data());
50     }
51     return res;
52 }
53 
ListSystemAbilitiesWithDumpFlag(unsigned int dumpFlags)54 rust::Vec<rust::String> ListSystemAbilitiesWithDumpFlag(unsigned int dumpFlags)
55 {
56     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
57     auto res = rust::Vec<rust::String>();
58 
59     if (sysm == nullptr) {
60         return res;
61     }
62 
63     auto list = sysm->ListSystemAbilities(dumpFlags);
64     for (auto s : list) {
65         char16_t *c = s.data();
66         res.push_back(c);
67     }
68     return res;
69 }
70 
LoadSystemAbility(int32_t systemAbilityId,int32_t timeout)71 std::unique_ptr<SptrIRemoteObject> LoadSystemAbility(int32_t systemAbilityId, int32_t timeout)
72 {
73     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
74     if (sysm == nullptr) {
75         return nullptr;
76     }
77 
78     auto ability = sysm->LoadSystemAbility(systemAbilityId, timeout);
79     if (ability == nullptr) {
80         return nullptr;
81     }
82     return std::make_unique<SptrIRemoteObject>(std::move(ability));
83 }
84 
LoadSystemAbilityWithCallback(int32_t systemAbilityId,rust::Fn<void ()> on_success,rust::Fn<void ()> on_fail)85 int32_t LoadSystemAbilityWithCallback(int32_t systemAbilityId, rust::Fn<void()> on_success, rust::Fn<void()> on_fail)
86 {
87     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
88     if (sysm == nullptr) {
89         return -1;
90     }
91     auto callback = sptr<LoadCallbackWrapper>::MakeSptr(on_success, on_fail);
92     return sysm->LoadSystemAbility(systemAbilityId, callback);
93 }
94 
GetContextManager()95 std::unique_ptr<SptrIRemoteObject> GetContextManager()
96 {
97     sptr<IRemoteObject> saMgr = IPCSkeleton::GetContextObject();
98     if (saMgr == nullptr) {
99         return nullptr;
100     }
101     return std::make_unique<SptrIRemoteObject>(std::move(saMgr));
102 }
103 
GetSystemAbility(int32_t systemAbilityId)104 std::unique_ptr<SptrIRemoteObject> GetSystemAbility(int32_t systemAbilityId)
105 {
106     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
107     if (sysm == nullptr) {
108         return nullptr;
109     }
110 
111     auto ability = sysm->GetSystemAbility(systemAbilityId);
112     if (ability == nullptr) {
113         return nullptr;
114     }
115     return std::make_unique<SptrIRemoteObject>(std::move(ability));
116 }
117 
CheckSystemAbility(int32_t systemAbilityId)118 std::unique_ptr<SptrIRemoteObject> CheckSystemAbility(int32_t systemAbilityId)
119 {
120     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
121     if (sysm == nullptr) {
122         return nullptr;
123     }
124 
125     auto ability = sysm->CheckSystemAbility(systemAbilityId);
126     if (ability == nullptr) {
127         return nullptr;
128     }
129     return std::make_unique<SptrIRemoteObject>(std::move(ability));
130 }
131 
RemoveSystemAbility(int32_t systemAbilityId)132 int32_t RemoveSystemAbility(int32_t systemAbilityId)
133 {
134     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
135     if (sysm == nullptr) {
136         return -1;
137     }
138     return sysm->RemoveSystemAbility(systemAbilityId);
139 }
140 
GetSystemAbilityWithDeviceId(int32_t systemAbilityId,const std::string & deviceId)141 std::unique_ptr<SptrIRemoteObject> GetSystemAbilityWithDeviceId(int32_t systemAbilityId, const std::string &deviceId)
142 {
143     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
144     if (sysm == nullptr) {
145         return nullptr;
146     }
147     auto ability = sysm->GetSystemAbility(systemAbilityId, deviceId);
148     if (ability == nullptr) {
149         return nullptr;
150     }
151     return std::make_unique<SptrIRemoteObject>(std::move(ability));
152 }
153 
CheckSystemAbilityWithDeviceId(int32_t systemAbilityId,const std::string & deviceId)154 std::unique_ptr<SptrIRemoteObject> CheckSystemAbilityWithDeviceId(int32_t systemAbilityId, const std::string &deviceId)
155 {
156     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
157     if (sysm == nullptr) {
158         return nullptr;
159     }
160     auto ability = sysm->CheckSystemAbility(systemAbilityId, deviceId);
161     if (ability == nullptr) {
162         return nullptr;
163     }
164     return std::make_unique<SptrIRemoteObject>(std::move(ability));
165 }
166 
SubscribeSystemAbility(int32_t systemAbilityId,rust::Fn<void (int32_t systemAbilityId,const rust::str deviceId)> onAdd,rust::Fn<void (int32_t systemAbilityId,const rust::str deviceId)> onRemove)167 std::unique_ptr<UnSubscribeSystemAbilityHandler> SubscribeSystemAbility(int32_t systemAbilityId,
168     rust::Fn<void(int32_t systemAbilityId, const rust::str deviceId)> onAdd,
169     rust::Fn<void(int32_t systemAbilityId, const rust::str deviceId)> onRemove)
170 {
171     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
172     if (sysm == nullptr) {
173         return nullptr;
174     }
175 
176     sptr<ISystemAbilityStatusChange> listener = new SystemAbilityStatusChangeWrapper(onAdd, onRemove);
177     sysm->SubscribeSystemAbility(systemAbilityId, listener);
178     return std::make_unique<UnSubscribeSystemAbilityHandler>(systemAbilityId, listener);
179 }
180 
AddOnDemandSystemAbilityInfo(int32_t systemAbilityId,const rust::str localAbilityManagerName)181 int32_t AddOnDemandSystemAbilityInfo(int32_t systemAbilityId, const rust::str localAbilityManagerName)
182 {
183     if (localAbilityManagerName.length() > MAX_RUST_STR_LEN) {
184         return -1;
185     }
186 
187     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
188     if (sysm == nullptr) {
189         return -1;
190     }
191     std::u16string s = Str8ToStr16(std::string(localAbilityManagerName));
192     return sysm->AddOnDemandSystemAbilityInfo(systemAbilityId, s);
193 }
194 
UnloadSystemAbility(int32_t systemAbilityId)195 int32_t UnloadSystemAbility(int32_t systemAbilityId)
196 {
197     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
198     if (sysm == nullptr) {
199         return -1;
200     }
201     return sysm->UnloadSystemAbility(systemAbilityId);
202 }
203 
CancelUnloadSystemAbility(int32_t systemAbilityId)204 int32_t CancelUnloadSystemAbility(int32_t systemAbilityId)
205 {
206     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
207     if (sysm == nullptr) {
208         return -1;
209     }
210     return sysm->CancelUnloadSystemAbility(systemAbilityId);
211 }
212 
UnloadAllIdleSystemAbility()213 int32_t UnloadAllIdleSystemAbility()
214 {
215     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
216     if (sysm == nullptr) {
217         return -1;
218     }
219     return sysm->UnloadAllIdleSystemAbility();
220 }
221 
AddSystemAbility(int32_t systemAbilityId,rust::Box<AbilityStub> ability,AddSystemAbilityConfig config)222 int32_t AddSystemAbility(int32_t systemAbilityId, rust::Box<AbilityStub> ability, AddSystemAbilityConfig config)
223 {
224     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
225     if (sysm == nullptr) {
226         return -1;
227     }
228     auto capability_u16 = Str8ToStr16(std::string(config.capability));
229     auto permission_u16 = Str8ToStr16(std::string(config.permission));
230 
231     ISystemAbilityManager::SAExtraProp extra(config.is_distributed, config.dump_flags, capability_u16, permission_u16);
232     auto stub = sptr<RemoteServiceStub>::MakeSptr(ability.into_raw());
233 
234     return sysm->AddSystemAbility(systemAbilityId, stub);
235 }
236 
GetSystemProcessInfo(int32_t systemAbilityId)237 SystemProcessInfo GetSystemProcessInfo(int32_t systemAbilityId)
238 {
239     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
240     OHOS::SystemProcessInfo info;
241     if (sysm == nullptr) {
242         return SystemProcessInfo{
243             .processName = info.processName.data(),
244             .pid = info.pid,
245             .uid = info.uid,
246         };
247     }
248     sysm->GetSystemProcessInfo(systemAbilityId, info);
249     return SystemProcessInfo{
250         .processName = info.processName.data(),
251         .pid = info.pid,
252         .uid = info.uid,
253     };
254 }
255 
GetRunningSystemProcess()256 rust::Vec<SystemProcessInfo> GetRunningSystemProcess()
257 {
258     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
259 
260     auto res = rust::Vec<SystemProcessInfo>();
261     if (sysm == nullptr) {
262         return res;
263     }
264 
265     auto infos = std::list<OHOS::SystemProcessInfo>();
266     sysm->GetRunningSystemProcess(infos);
267     for (auto info : infos) {
268         res.push_back(SystemProcessInfo{
269             .processName = info.processName,
270             .pid = info.pid,
271             .uid = info.uid,
272         });
273     }
274     return res;
275 }
276 
GetCommonEventExtraDataIdlist(int32_t saId,rust::Vec<int64_t> & extraDataIdList,const std::string & eventName)277 int32_t GetCommonEventExtraDataIdlist(int32_t saId, rust::Vec<int64_t> &extraDataIdList, const std::string &eventName)
278 {
279     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
280     if (sysm == nullptr) {
281         return -1;
282     }
283 
284     std::vector<int64_t> idList;
285     auto ret = sysm->GetCommonEventExtraDataIdlist(saId, idList, eventName);
286     if (ret != ERR_OK) {
287         return ret;
288     }
289     for (auto id : idList) {
290         extraDataIdList.push_back(id);
291     }
292     return ret;
293 }
294 
SubscribeSystemProcess(rust::Fn<void (const OHOS::SamgrRust::SystemProcessInfo & systemProcessInfo)> onStart_,rust::Fn<void (const OHOS::SamgrRust::SystemProcessInfo & systemProcessInfo)> onStop_)295 std::unique_ptr<UnSubscribeSystemProcessHandler> SubscribeSystemProcess(
296     rust::Fn<void(const OHOS::SamgrRust::SystemProcessInfo &systemProcessInfo)> onStart_,
297     rust::Fn<void(const OHOS::SamgrRust::SystemProcessInfo &systemProcessInfo)> onStop_)
298 {
299     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
300     if (sysm == nullptr) {
301         return nullptr;
302     }
303     sptr<ISystemProcessStatusChange> listener = new SystemProcessStatusChangeWrapper(nullptr, onStart_, onStop_);
304     sysm->SubscribeSystemProcess(listener);
305     return std::make_unique<UnSubscribeSystemProcessHandler>(listener);
306 }
307 
GetOnDemandReasonExtraData(int64_t extraDataId,MessageParcel & parcel)308 int32_t GetOnDemandReasonExtraData(int64_t extraDataId, MessageParcel &parcel)
309 {
310     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
311     if (sysm == nullptr) {
312         return -1;
313     }
314     return sysm->GetOnDemandReasonExtraData(extraDataId, parcel);
315 }
316 
SendStrategy(int32_t type,rust::Vec<int32_t> systemAbilityIds,int32_t level,std::string & action)317 int32_t SendStrategy(int32_t type, rust::Vec<int32_t> systemAbilityIds, int32_t level, std::string &action)
318 {
319     auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
320     if (sysm == nullptr) {
321         return -1;
322     }
323     auto v = std::vector<int32_t>();
324     for (auto id : systemAbilityIds) {
325         v.push_back(id);
326     }
327     return sysm->SendStrategy(type, v, level, action);
328 }
329 
RemoteServiceStub(AbilityStub * ability)330 RemoteServiceStub::RemoteServiceStub(AbilityStub *ability)
331 {
332     this->inner_ = ability;
333 }
334 
~RemoteServiceStub()335 RemoteServiceStub::~RemoteServiceStub()
336 {
337     auto ability = rust::Box<AbilityStub>::from_raw(this->inner_);
338 }
339 
OnRemoteRequest(uint32_t code,OHOS::MessageParcel & data,OHOS::MessageParcel & reply,OHOS::MessageOption & option)340 int RemoteServiceStub ::OnRemoteRequest(
341     uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply, OHOS::MessageOption &option)
342 {
343     return inner_->on_remote_request(code, data, reply);
344 }
345 
OnRemoteDump(uint32_t code,OHOS::MessageParcel & data,OHOS::MessageParcel & reply,OHOS::MessageOption & option)346 int RemoteServiceStub ::OnRemoteDump(
347     uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply, OHOS::MessageOption &option)
348 {
349     return 0;
350 }
351 
LoadCallbackWrapper(rust::Fn<void ()> on_success,rust::Fn<void ()> on_fail)352 LoadCallbackWrapper::LoadCallbackWrapper(rust::Fn<void()> on_success, rust::Fn<void()> on_fail)
353     : on_success_(on_success), on_fail_(on_fail)
354 {
355 }
356 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)357 void LoadCallbackWrapper::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
358 {
359     on_success_();
360 }
361 
OnLoadSystemAbilityFail(int32_t systemAbilityId)362 void LoadCallbackWrapper::OnLoadSystemAbilityFail(int32_t systemAbilityId)
363 {
364     on_fail_();
365 }
366 
367 } // namespace SamgrRust
368 } // namespace OHOS