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 #include "context_appstate_observer.h"
16
17 #include <sstream>
18
19 #include "context_pool.h"
20 #include "iam_check.h"
21 #include "iam_logger.h"
22 #include "iam_para2str.h"
23 #ifdef SCREENLOCK_CLIENT_ENABLE
24 #include "screenlock_manager.h"
25 #endif
26 #include "system_ability_definition.h"
27
28 #define LOG_TAG "USER_AUTH_SA"
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
32 using namespace OHOS::AppExecFwk;
33 namespace {
34 constexpr std::uint32_t CONVERT_UID_TO_USERID = 200000;
35 }
36
GetAppManagerInstance()37 sptr<IAppMgr> ContextAppStateObserverManager::GetAppManagerInstance()
38 {
39 IAM_LOGD("start");
40 sptr<ISystemAbilityManager> systemAbilityManager =
41 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42 if (systemAbilityManager == nullptr) {
43 IAM_LOGE("systemAbilityManager is nullptr");
44 return nullptr;
45 }
46
47 sptr<IRemoteObject> object = systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID);
48 if (object == nullptr) {
49 IAM_LOGE("systemAbilityManager remote object is nullptr");
50 return nullptr;
51 }
52
53 return iface_cast<IAppMgr>(object);
54 }
55
SubscribeAppState(const std::shared_ptr<ContextCallback> & callback,const uint64_t contextId)56 void ContextAppStateObserverManager::SubscribeAppState(const std::shared_ptr<ContextCallback> &callback,
57 const uint64_t contextId)
58 {
59 IAM_LOGD("start");
60 IF_FALSE_LOGE_AND_RETURN(callback != nullptr);
61
62 const std::string bundleName = callback->GetCallerName();
63 if (bundleName.empty()) {
64 IAM_LOGE("bundleName is null");
65 return;
66 }
67
68 sptr<IAppMgr> appManager = GetAppManagerInstance();
69 if (appManager == nullptr) {
70 IAM_LOGE("GetAppManagerInstance failed");
71 return;
72 }
73
74 appStateObserver_ = new (std::nothrow) ContextAppStateObserver(contextId, bundleName);
75 if (appStateObserver_ == nullptr) {
76 IAM_LOGE("get appStateObserver failed");
77 return;
78 }
79
80 std::vector<std::string> bundleNameList;
81 bundleNameList.emplace_back(bundleName);
82 int32_t result = appManager->RegisterApplicationStateObserver(appStateObserver_, bundleNameList);
83 if (result != SUCCESS) {
84 IAM_LOGE("RegistApplicationStateObserver failed");
85 appStateObserver_ = nullptr;
86 return;
87 }
88
89 IAM_LOGI("SubscribeAppState success, contextId:****%{public}hx, bundleName:%{public}s",
90 static_cast<uint16_t>(contextId), bundleName.c_str());
91 return;
92 }
93
UnSubscribeAppState()94 void ContextAppStateObserverManager::UnSubscribeAppState()
95 {
96 IAM_LOGD("start");
97 if (appStateObserver_ == nullptr) {
98 IAM_LOGE("appStateObserver_ is nullptr");
99 return;
100 }
101
102 sptr<IAppMgr> appManager = GetAppManagerInstance();
103 if (appManager == nullptr) {
104 IAM_LOGE("GetAppManagerInstance failed");
105 return;
106 }
107
108 int32_t result = appManager->UnregisterApplicationStateObserver(appStateObserver_);
109 if (result != SUCCESS) {
110 IAM_LOGE("UnregisterApplicationStateObserver failed");
111 return;
112 }
113
114 appStateObserver_ = nullptr;
115 IAM_LOGI("UnSubscribeAppState success");
116 return;
117 }
118
GetInstance()119 ContextAppStateObserverManager &ContextAppStateObserverManager::GetInstance()
120 {
121 static ContextAppStateObserverManager instance;
122 return instance;
123 }
124
IsScreenLocked()125 bool ContextAppStateObserverManager::IsScreenLocked()
126 {
127 #ifdef SCREENLOCK_CLIENT_ENABLE
128 auto screenLockInstance = ScreenLock::ScreenLockManager::GetInstance();
129 if (screenLockInstance == nullptr) {
130 IAM_LOGE("screenLockInstance is null");
131 return false;
132 }
133 bool isScreenLocked = screenLockInstance->IsScreenLocked();
134 IAM_LOGI("IsScreenLocked: %{public}d", isScreenLocked);
135 return isScreenLocked;
136 #else
137 return false;
138 #endif
139 }
140
ContextAppStateObserver(const uint64_t contextId,const std::string bundleName)141 ContextAppStateObserver::ContextAppStateObserver(const uint64_t contextId,
142 const std::string bundleName) : contextId_(contextId), bundleName_(bundleName)
143 {
144 IAM_LOGD("start");
145 }
146
ProcAppStateChanged(int32_t userId)147 void ContextAppStateObserver::ProcAppStateChanged(int32_t userId)
148 {
149 IAM_LOGD("start");
150 auto context = ContextPool::Instance().Select(contextId_).lock();
151 if (context == nullptr) {
152 IAM_LOGE("context is nullptr");
153 return;
154 }
155 if (context->GetUserId() != userId) {
156 IAM_LOGI("context userId is %{public}d, appStateChanged userId is %{public}d", context->GetUserId(), userId);
157 return;
158 }
159 if (ContextAppStateObserverManager::GetInstance().IsScreenLocked()) {
160 IAM_LOGI("the screen is currently locked, skip auth cancel");
161 return;
162 }
163 if (!context->Stop()) {
164 IAM_LOGE("failed to cancel enroll or auth");
165 return;
166 }
167 return;
168 }
169
OnAppStateChanged(const AppStateData & appStateData)170 void ContextAppStateObserver::OnAppStateChanged(const AppStateData &appStateData)
171 {
172 auto bundleName = appStateData.bundleName;
173 auto state = static_cast<ApplicationState>(appStateData.state);
174 int32_t userId = appStateData.uid / CONVERT_UID_TO_USERID;
175 IAM_LOGI("OnAppStateChanged, contextId: ****%{public}hx, userId:%{public}d, bundleName:%{public}s, "
176 "state:%{public}d", static_cast<uint16_t>(contextId_), userId, bundleName.c_str(), state);
177
178 if (bundleName.compare(bundleName_) == 0 && state == ApplicationState::APP_STATE_BACKGROUND) {
179 ProcAppStateChanged(userId);
180 }
181 return;
182 }
183
OnForegroundApplicationChanged(const AppStateData & appStateData)184 void ContextAppStateObserver::OnForegroundApplicationChanged(const AppStateData &appStateData)
185 {
186 auto bundleName = appStateData.bundleName;
187 auto state = static_cast<ApplicationState>(appStateData.state);
188 int32_t userId = appStateData.uid / CONVERT_UID_TO_USERID;
189 IAM_LOGI("OnForegroundApplicationChanged, contextId: ****%{public}hx, userId:%{public}d, bundleName:%{public}s, "
190 "state:%{public}d", static_cast<uint16_t>(contextId_), userId, bundleName.c_str(), state);
191
192 if (bundleName.compare(bundleName_) == 0 && state == ApplicationState::APP_STATE_BACKGROUND) {
193 ProcAppStateChanged(userId);
194 }
195 return;
196 }
197 } // namespace UserAuth
198 } // namespace UserIam
199 } // namespace OHOS
200