• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "service_unload_manager.h"
17 
18 #include "iam_check.h"
19 #include "iam_logger.h"
20 #include "relative_timer.h"
21 #include "system_param_manager.h"
22 
23 #define LOG_TAG "USER_AUTH_SA"
24 
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
GetInstance()28 ServiceUnloadManager &ServiceUnloadManager::GetInstance()
29 {
30     static ServiceUnloadManager instance;
31     return instance;
32 }
33 
StartSubscribe()34 void ServiceUnloadManager::StartSubscribe()
35 {
36     std::lock_guard<std::recursive_mutex> lock(mutex_);
37 
38     if (isSubscribed_) {
39         return;
40     }
41 
42     auto isPinEnrolledListener = [](const std::string &value) {
43         bool isPinEnrolled = false;
44         if (value == TRUE_STR) {
45             isPinEnrolled = true;
46         }
47         ServiceUnloadManager::GetInstance().OnIsPinEnrolledChange(isPinEnrolled);
48     };
49 
50     SystemParamManager::GetInstance().WatchParam(IS_PIN_ENROLLED_KEY, isPinEnrolledListener);
51     bool isPinEnrolled = SystemParamManager::GetInstance().GetParam(IS_PIN_ENROLLED_KEY, FALSE_STR) == TRUE_STR;
52     OnIsPinEnrolledChange(isPinEnrolled);
53 
54     auto startSaListener = [](const std::string &value) {
55         bool startSa = false;
56         if (value == TRUE_STR) {
57             startSa = true;
58         }
59         ServiceUnloadManager::GetInstance().OnStartSaChange(startSa);
60     };
61     SystemParamManager::GetInstance().WatchParam(START_SA_KEY, startSaListener);
62 
63     bool startSa = SystemParamManager::GetInstance().GetParam(START_SA_KEY, FALSE_STR) == TRUE_STR;
64     OnStartSaChange(startSa);
65 
66     isSubscribed_ = true;
67 }
68 
OnTimeout()69 void ServiceUnloadManager::OnTimeout()
70 {
71     std::lock_guard<std::recursive_mutex> lock(mutex_);
72     IAM_LOGI("timer timeout, stop sa");
73     StopTimer();
74     if (isPinEnrolled_) {
75         IAM_LOGI("isPinEnrolled is true, not stop sa");
76         return;
77     }
78     SystemParamManager::GetInstance().SetParamTwice(STOP_SA_KEY, FALSE_STR, TRUE_STR);
79 }
80 
OnIsPinEnrolledChange(bool isPinEnrolled)81 void ServiceUnloadManager::OnIsPinEnrolledChange(bool isPinEnrolled)
82 {
83     std::lock_guard<std::recursive_mutex> lock(mutex_);
84     if (isPinEnrolled_ == isPinEnrolled) {
85         return;
86     }
87     IAM_LOGI("isPinEnrolled change from %{public}d to %{public}d", isPinEnrolled_, isPinEnrolled);
88     isPinEnrolled_ = isPinEnrolled;
89 
90     if (isPinEnrolled) {
91         IAM_LOGI("isPinEnrolled is true, stop timer");
92         StopTimer();
93     } else {
94         bool isCredentialChecked = SystemParamManager::GetInstance().GetParam(IS_CREDENTIAL_CHECKED_KEY, FALSE_STR) ==
95             TRUE_STR;
96         if (isCredentialChecked) {
97             IAM_LOGI("isPinEnrolled is false, isCredentialChecked is true, start timer");
98             RestartTimer();
99         } else {
100             IAM_LOGI("isPinEnrolled is false, isCredentialChecked is false, not start timer");
101         }
102     }
103 }
104 
OnStartSaChange(bool startSa)105 void ServiceUnloadManager::OnStartSaChange(bool startSa)
106 {
107     std::lock_guard<std::recursive_mutex> lock(mutex_);
108     if (startSa_ == startSa) {
109         return;
110     }
111     IAM_LOGI("startSa change from %{public}d to %{public}d", startSa_, startSa);
112     startSa_ = startSa;
113     if (!startSa) {
114         return;
115     }
116 
117     bool isCredentialChecked = SystemParamManager::GetInstance().GetParam(IS_CREDENTIAL_CHECKED_KEY, FALSE_STR) ==
118         TRUE_STR;
119     if (startSa && !isPinEnrolled_ && isCredentialChecked) {
120         IAM_LOGI("start sa and isPinEnrolled is false, start timer");
121         RestartTimer();
122     } else {
123         IAM_LOGI("start sa %{public}d, isPinEnrolled %{public}d, isCredentialChecked %{public}d, not start timer",
124             startSa, isPinEnrolled_, isCredentialChecked);
125     }
126 }
127 
RestartTimer()128 void ServiceUnloadManager::RestartTimer()
129 {
130     std::lock_guard<std::recursive_mutex> lock(mutex_);
131     const uint32_t TEN_MINUTE = 10 * 60 * 1000;
132     IAM_LOGI("start timer");
133     StopTimer();
134     timerId_ =
135         RelativeTimer::GetInstance().Register([]() { ServiceUnloadManager::GetInstance().OnTimeout(); }, TEN_MINUTE);
136 }
137 
StopTimer()138 void ServiceUnloadManager::StopTimer()
139 {
140     std::lock_guard<std::recursive_mutex> lock(mutex_);
141     if (timerId_ == std::nullopt) {
142         return;
143     }
144     IAM_LOGI("stop timer");
145     RelativeTimer::GetInstance().Unregister(timerId_.value());
146     timerId_ = std::nullopt;
147 }
148 
OnFwkReady(bool & isStopSa)149 void ServiceUnloadManager::OnFwkReady(bool &isStopSa)
150 {
151     std::lock_guard<std::recursive_mutex> lock(mutex_);
152     isStopSa = false;
153     isPinEnrolled_ = SystemParamManager::GetInstance().GetParam(IS_PIN_ENROLLED_KEY, FALSE_STR) == TRUE_STR;
154     if (isPinEnrolled_) {
155         IAM_LOGI("fwk ready, isPinEnrolled is true, sa should be running");
156         return;
157     }
158 
159     if (timerId_ != std::nullopt) {
160         IAM_LOGI("fwk ready, timer is running, wait timer timeout");
161         return;
162     }
163 
164     IAM_LOGI("fwk ready, timer is not running and isPinEnrolled is false, stop sa");
165     SystemParamManager::GetInstance().SetParamTwice(STOP_SA_KEY, FALSE_STR, TRUE_STR);
166     isStopSa = true;
167 }
168 } // namespace UserAuth
169 } // namespace UserIam
170 } // namespace OHOS