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