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_init_manager.h"
17
18 #include "iam_check.h"
19 #include "iam_logger.h"
20
21 #include "co_auth_service.h"
22 #include "driver_state_manager.h"
23 #include "keyguard_status_listener.h"
24 #include "load_mode_handler.h"
25 #include "remote_auth_service.h"
26 #include "soft_bus_manager.h"
27 #include "system_param_manager.h"
28
29 #define IAM_LOG_TAG "USER_AUTH_SA"
30
31 namespace OHOS {
32 namespace UserIam {
33 namespace UserAuth {
GetInstance()34 ServiceInitManager &ServiceInitManager::GetInstance()
35 {
36 static ServiceInitManager instance;
37 return instance;
38 }
39
OnIdmServiceStart()40 void ServiceInitManager::OnIdmServiceStart()
41 {
42 std::lock_guard<std::recursive_mutex> lock(mutex_);
43 isIdmServiceStart_ = true;
44 IAM_LOGI("idm service start");
45 CheckAllServiceStart();
46 }
47
OnIdmServiceStop()48 void ServiceInitManager::OnIdmServiceStop()
49 {
50 std::lock_guard<std::recursive_mutex> lock(mutex_);
51 isIdmServiceStart_ = false;
52 IAM_LOGI("idm service stop");
53 CheckAllServiceStop();
54 }
55
OnCoAuthServiceStart()56 void ServiceInitManager::OnCoAuthServiceStart()
57 {
58 std::lock_guard<std::recursive_mutex> lock(mutex_);
59 isCoAuthServiceStart_ = true;
60 IAM_LOGI("co auth service start");
61 CheckAllServiceStart();
62 }
63
OnCoAuthServiceStop()64 void ServiceInitManager::OnCoAuthServiceStop()
65 {
66 std::lock_guard<std::recursive_mutex> lock(mutex_);
67 isCoAuthServiceStart_ = false;
68 IAM_LOGI("co auth service stop");
69 CheckAllServiceStop();
70 }
71
OnUserAuthServiceStart()72 void ServiceInitManager::OnUserAuthServiceStart()
73 {
74 std::lock_guard<std::recursive_mutex> lock(mutex_);
75 isUserAuthServiceStart_ = true;
76 IAM_LOGI("user auth service start");
77 CheckAllServiceStart();
78 }
79
OnUserAuthServiceStop()80 void ServiceInitManager::OnUserAuthServiceStop()
81 {
82 std::lock_guard<std::recursive_mutex> lock(mutex_);
83 isUserAuthServiceStart_ = false;
84 IAM_LOGI("user auth service stop");
85 CheckAllServiceStop();
86 }
87
CheckAllServiceStart()88 void ServiceInitManager::CheckAllServiceStart()
89 {
90 bool isAllServiceStart = isIdmServiceStart_ && isCoAuthServiceStart_ && isUserAuthServiceStart_;
91 IAM_LOGI("idm service: %{public}d, coauth service: %{public}d, user authservice: %{public}d, all "
92 "service start: %{public}d",
93 isIdmServiceStart_, isCoAuthServiceStart_, isUserAuthServiceStart_, isAllServiceStart);
94
95 LoadModeHandler::GetInstance().StartSubscribe();
96
97 if (!isAllServiceStart) {
98 LoadModeHandler::GetInstance().StartCheckServiceReadyTimer();
99 return;
100 }
101
102 LoadModeHandler::GetInstance().CancelCheckServiceReadyTimer();
103
104 IAM_LOGI("all service start, init global instance begin");
105
106 SoftBusManager::GetInstance().Start();
107 KeyguardStatusListenerManager::GetInstance().RegisterCommonEventListener();
108 const bool REMOTE_AUTH_SERVICE_RESULT = RemoteAuthService::GetInstance().Start();
109 (void)REMOTE_AUTH_SERVICE_RESULT;
110
111 auto coAuthService = CoAuthService::GetInstance();
112 IF_FALSE_LOGE_AND_RETURN(coAuthService != nullptr);
113 coAuthService->RegisterAccessTokenListener();
114
115 DriverStateManager::GetInstance().StartSubscribe();
116
117 IAM_LOGI("all service start, init global instance end");
118 }
119
CheckAllServiceStop()120 void ServiceInitManager::CheckAllServiceStop()
121 {
122 std::lock_guard<std::recursive_mutex> lock(mutex_);
123 bool isAllServiceStop = !isIdmServiceStart_ && !isCoAuthServiceStart_ && !isUserAuthServiceStart_;
124 IAM_LOGI("idm service: %{public}d, coauth service: %{public}d, user authservice: %{public}d, all "
125 "service stop: %{public}d",
126 isIdmServiceStart_, isCoAuthServiceStart_, isUserAuthServiceStart_, isAllServiceStop);
127 if (!isAllServiceStop) {
128 return;
129 }
130
131 IAM_LOGI("all service stop, destroy global instance begin");
132
133 SoftBusManager::GetInstance().Stop();
134 KeyguardStatusListenerManager::GetInstance().UnRegisterCommonEventListener();
135
136 auto coAuthService = CoAuthService::GetInstance();
137 IF_FALSE_LOGE_AND_RETURN(coAuthService != nullptr);
138 coAuthService->UnRegisterAccessTokenListener();
139
140 IAM_LOGI("all service stop, destroy global instance end");
141 }
142
143 } // namespace UserAuth
144 } // namespace UserIam
145 } // namespace OHOS