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 "status_change_wrapper.h"
15 
16 #include <algorithm>
17 #include <memory>
18 #include <vector>
19 
20 #include "cxx.h"
21 #include "iservice_registry.h"
22 #include "wrapper.rs.h"
23 namespace OHOS {
24 namespace SamgrRust {
SystemAbilityStatusChangeWrapper(const rust::Fn<void (int32_t systemAbilityId,const rust::str deviceId)> onAdd,const rust::Fn<void (int32_t systemAbilityId,const rust::str deviceId)> onRemove)25 SystemAbilityStatusChangeWrapper::SystemAbilityStatusChangeWrapper(
26     const rust::Fn<void(int32_t systemAbilityId, const rust::str deviceId)> onAdd,
27     const rust::Fn<void(int32_t systemAbilityId, const rust::str deviceId)> onRemove)
28 {
29     this->onAdd_ = onAdd;
30     this->onRemove_ = onRemove;
31 }
32 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)33 void SystemAbilityStatusChangeWrapper::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
34 {
35     onAdd_(systemAbilityId, deviceId);
36 }
37 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)38 void SystemAbilityStatusChangeWrapper::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
39 {
40     onRemove_(systemAbilityId, deviceId);
41 }
42 
SystemProcessStatusChangeWrapper(const sptr<IRemoteObject> & impl,const rust::Fn<void (const OHOS::SamgrRust::SystemProcessInfo & systemProcessInfo)> onStart,const rust::Fn<void (const OHOS::SamgrRust::SystemProcessInfo & systemProcessInfo)> onStop)43 SystemProcessStatusChangeWrapper::SystemProcessStatusChangeWrapper(const sptr<IRemoteObject> &impl,
44     const rust::Fn<void(const OHOS::SamgrRust::SystemProcessInfo &systemProcessInfo)> onStart,
45     const rust::Fn<void(const OHOS::SamgrRust::SystemProcessInfo &systemProcessInfo)> onStop)
46     : IRemoteProxy<ISystemProcessStatusChange>(impl)
47 {
48     this->onStart_ = onStart;
49     this->onStop_ = onStop;
50 }
51 
OnSystemProcessStarted(OHOS::SystemProcessInfo & systemProcessInfo)52 void SystemProcessStatusChangeWrapper::OnSystemProcessStarted(OHOS::SystemProcessInfo &systemProcessInfo)
53 {
54     auto info = OHOS::SamgrRust::SystemProcessInfo{
55         .processName = systemProcessInfo.processName,
56         .pid = systemProcessInfo.pid,
57         .uid = systemProcessInfo.uid,
58     };
59     onStart_(info);
60 }
OnSystemProcessStopped(OHOS::SystemProcessInfo & systemProcessInfo)61 void SystemProcessStatusChangeWrapper::OnSystemProcessStopped(OHOS::SystemProcessInfo &systemProcessInfo)
62 {
63     auto info = OHOS::SamgrRust::SystemProcessInfo{
64         .processName = systemProcessInfo.processName,
65         .pid = systemProcessInfo.pid,
66         .uid = systemProcessInfo.uid,
67     };
68     onStop_(info);
69 }
70 
UnSubscribeSystemAbilityHandler(int32_t systemAbilityId,sptr<ISystemAbilityStatusChange> listener)71 UnSubscribeSystemAbilityHandler::UnSubscribeSystemAbilityHandler(
72     int32_t systemAbilityId, sptr<ISystemAbilityStatusChange> listener)
73 {
74     this->said_ = systemAbilityId;
75     this->listener_ = listener;
76 }
77 
UnSubscribe()78 void UnSubscribeSystemAbilityHandler::UnSubscribe()
79 {
80     SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager()->UnSubscribeSystemAbility(said_, listener_);
81 }
82 
UnSubscribeSystemProcessHandler(sptr<ISystemProcessStatusChange> listener)83 UnSubscribeSystemProcessHandler::UnSubscribeSystemProcessHandler(sptr<ISystemProcessStatusChange> listener)
84 {
85     this->listener_ = listener;
86 }
87 
UnSubscribe()88 void UnSubscribeSystemProcessHandler::UnSubscribe()
89 {
90     SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager()->UnSubscribeSystemProcess(listener_);
91 }
92 
93 } // namespace SamgrRust
94 } // namespace OHOS