1 /*
2  * Copyright (c) 2024 Huawei Device 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 "system_ability_listener.h"
17 
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 
21 #define LOG_TAG "USER_AUTH_SA"
22 namespace OHOS {
23 namespace UserIam {
24 namespace UserAuth {
SystemAbilityListener(std::string name,int32_t systemAbilityId,AddFunc addFunc,RemoveFunc removeFunc)25 SystemAbilityListener::SystemAbilityListener(std::string name, int32_t systemAbilityId,
26     AddFunc addFunc, RemoveFunc removeFunc)
27     : name_(name), systemAbilityId_(systemAbilityId), addFunc_(addFunc), removeFunc_(removeFunc)
28 {
29     IAM_LOGI("start.");
30 }
31 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)32 void SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
33 {
34     IAM_LOGI("start.");
35     if (systemAbilityId != systemAbilityId_) {
36         IAM_LOGI("systemAbilityId is not same.");
37         return;
38     }
39 
40     if (addFunc_ != nullptr) {
41         addFunc_();
42         IAM_LOGI("addFunc_ proc.");
43     }
44 }
45 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)46 void SystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
47 {
48     IAM_LOGI("start.");
49     if (systemAbilityId != systemAbilityId_) {
50         IAM_LOGI("systemAbilityId is not same.");
51         return;
52     }
53 
54     if (removeFunc_ != nullptr) {
55         removeFunc_();
56         IAM_LOGI("removeFunc_ proc.");
57     }
58 }
59 
Subscribe(std::string name,int32_t systemAbilityId,AddFunc addFunc,RemoveFunc removeFunc)60 sptr<SystemAbilityListener> SystemAbilityListener::Subscribe(std::string name, int32_t systemAbilityId,
61     AddFunc addFunc, RemoveFunc removeFunc)
62 {
63     IAM_LOGI("start name:%{public}s, systemAbilityId::%{public}d", name.c_str(), systemAbilityId);
64     auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
65     IF_FALSE_LOGE_AND_RETURN_VAL(sam != nullptr, nullptr);
66 
67     sptr<SystemAbilityListener> listener(
68         new (std::nothrow) SystemAbilityListener(name, systemAbilityId, addFunc, removeFunc));
69     IF_FALSE_LOGE_AND_RETURN_VAL(listener != nullptr, nullptr);
70 
71     int32_t ret = sam->SubscribeSystemAbility(systemAbilityId, listener);
72     if (ret != ERR_OK) {
73         IAM_LOGE("SubscribeSystemAbility fail, name:%{public}s, systemAbilityId::%{public}d",
74             name.c_str(), systemAbilityId);
75         return nullptr;
76     }
77 
78     IAM_LOGI("Subscribe service name:%{public}s success", name.c_str());
79     return listener;
80 }
81 
UnSubscribe(int32_t systemAbilityId,sptr<SystemAbilityListener> & listener)82 int32_t SystemAbilityListener::UnSubscribe(int32_t systemAbilityId, sptr<SystemAbilityListener> &listener)
83 {
84     IAM_LOGI("start systemAbilityId::%{public}d", systemAbilityId);
85     auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
86     IF_FALSE_LOGE_AND_RETURN_VAL(sam != nullptr, ERR_OK);
87 
88     int32_t ret = sam->UnSubscribeSystemAbility(systemAbilityId, listener);
89     if (ret != ERR_OK) {
90         IAM_LOGE("UnSubscribeSystemAbility fail.");
91         return ret;
92     }
93 
94     IAM_LOGI("UnSubscribe service success");
95     return ret;
96 }
97 } // namespace UserAuth
98 } // namespace UserIam
99 } // namespace OHOS
100