1 /*
2 * Copyright (c) 2021-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 "user_controller.h"
17
18 #include "ability_manager_service.h"
19 #include "hilog_tag_wrapper.h"
20 #include "mock_session_manager_service.h"
21 #include "os_account_manager_wrapper.h"
22 #include "scene_board_judgement.h"
23
24 namespace OHOS {
25 namespace AAFwk {
26 using namespace OHOS::AppExecFwk;
27 namespace {
28 const int64_t USER_SWITCH_TIMEOUT = 3 * 1000; // 3s
29 constexpr const char* DEVELOPER_MODE_STATE = "const.security.developermode.state";
30 }
31
UserItem(int32_t id)32 UserItem::UserItem(int32_t id) : userId_(id)
33 {}
34
~UserItem()35 UserItem::~UserItem() {}
36
GetUserId()37 int32_t UserItem::GetUserId()
38 {
39 return userId_;
40 }
41
SetState(const UserState & state)42 void UserItem::SetState(const UserState &state)
43 {
44 if (curState_ == state) {
45 return;
46 }
47 lastState_ = curState_;
48 curState_ = state;
49 }
50
GetState()51 UserState UserItem::GetState()
52 {
53 return curState_;
54 }
55
UserController()56 UserController::UserController()
57 {
58 }
59
~UserController()60 UserController::~UserController()
61 {
62 }
63
Init()64 void UserController::Init()
65 {
66 auto handler = DelayedSingleton<AbilityManagerService>::GetInstance()->GetTaskHandler();
67 if (!handler) {
68 return;
69 }
70
71 if (eventHandler_) {
72 return;
73 }
74 eventHandler_ = std::make_shared<UserEventHandler>(handler, shared_from_this());
75 }
76
ClearAbilityUserItems(int32_t userId)77 void UserController::ClearAbilityUserItems(int32_t userId)
78 {
79 std::lock_guard<ffrt::mutex> guard(userLock_);
80 if (userItems_.count(userId)) {
81 userItems_.erase(userId);
82 }
83 }
84
StartUser(int32_t userId,sptr<IUserCallback> callback,bool isAppRecovery)85 int UserController::StartUser(int32_t userId, sptr<IUserCallback> callback, bool isAppRecovery)
86 {
87 if (userId == U1_USER_ID) {
88 return StartNoHeadUser(userId, callback);
89 }
90
91 if (userId < 0 || userId == USER_ID_NO_HEAD) {
92 TAG_LOGE(AAFwkTag::ABILITYMGR, "StartUserId invalid:%{public}d", userId);
93 if (callback != nullptr) {
94 callback->OnStartUserDone(userId, INVALID_USERID_VALUE);
95 }
96 return INVALID_USERID_VALUE;
97 }
98
99 if (IsCurrentUser(userId)) {
100 TAG_LOGW(AAFwkTag::ABILITYMGR, "StartUser current:%{public}d", userId);
101 if (callback != nullptr) {
102 callback->OnStartUserDone(userId, ERR_OK);
103 }
104 return ERR_OK;
105 }
106
107 auto appScheduler = DelayedSingleton<AppScheduler>::GetInstance();
108 if (!appScheduler) {
109 TAG_LOGE(AAFwkTag::ABILITYMGR, "null appScheduler");
110 return ABILITY_SERVICE_NOT_CONNECTED;
111 }
112 appScheduler->SetEnableStartProcessFlagByUserId(userId, true);
113
114 if (!IsExistOsAccount(userId)) {
115 TAG_LOGE(AAFwkTag::ABILITYMGR, "null StartUser account:%{public}d", userId);
116 if (callback != nullptr) {
117 callback->OnStartUserDone(userId, INVALID_USERID_VALUE);
118 }
119 return INVALID_USERID_VALUE;
120 }
121
122 if (GetCurrentUserId() != USER_ID_NO_HEAD && !Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
123 // start freezing screen
124 SetFreezingNewUserId(userId);
125 DelayedSingleton<AbilityManagerService>::GetInstance()->StartFreezingScreen();
126 }
127
128 auto oldUserId = GetCurrentUserId();
129 auto userItem = GetOrCreateUserItem(userId);
130 auto state = userItem->GetState();
131 if (state == STATE_STOPPING || state == STATE_SHUTDOWN) {
132 TAG_LOGE(AAFwkTag::ABILITYMGR, "StartUser user stop, userId:%{public}d", userId);
133 if (callback != nullptr) {
134 callback->OnStartUserDone(userId, ERR_DEAD_OBJECT);
135 }
136 return ERR_DEAD_OBJECT;
137 }
138
139 SetCurrentUserId(userId);
140 if (state == STATE_BOOTING) {
141 // send user start msg.
142 SendSystemUserStart(userId);
143 }
144
145 SendSystemUserCurrent(oldUserId, userId);
146 SendReportUserSwitch(oldUserId, userId, userItem);
147 SendUserSwitchTimeout(oldUserId, userId, userItem);
148 return MoveUserToForeground(oldUserId, userId, callback, isAppRecovery);
149 }
150
StartNoHeadUser(int32_t userId,sptr<IUserCallback> callback) const151 int32_t UserController::StartNoHeadUser(int32_t userId, sptr<IUserCallback> callback) const
152 {
153 if (!IsExistOsAccount(userId)) {
154 TAG_LOGE(AAFwkTag::ABILITYMGR, "U1 not exist");
155 callback->OnStartUserDone(userId, INVALID_USERID_VALUE);
156 return INVALID_USERID_VALUE;
157 }
158
159 auto abilityManagerService = DelayedSingleton<AbilityManagerService>::GetInstance();
160 if (!abilityManagerService) {
161 TAG_LOGE(AAFwkTag::ABILITYMGR, "abilityManagerService is nullptr");
162 callback->OnLogoutUserDone(userId, GET_ABILITY_SERVICE_FAILED);
163 return GET_ABILITY_SERVICE_FAILED;
164 }
165 abilityManagerService->UserStarted(userId);
166 callback->OnStartUserDone(userId, ERR_OK);
167 return ERR_OK;
168 }
169
StopUser(int32_t userId)170 int32_t UserController::StopUser(int32_t userId)
171 {
172 if (userId < 0 || userId == USER_ID_NO_HEAD || userId == USER_ID_DEFAULT) {
173 TAG_LOGE(AAFwkTag::ABILITYMGR, "userId invalid:%{public}d", userId);
174 return -1;
175 }
176
177 if (IsCurrentUser(userId)) {
178 TAG_LOGW(AAFwkTag::ABILITYMGR, "user current:%{public}d", userId);
179 return 0;
180 }
181
182 if (!IsExistOsAccount(userId)) {
183 TAG_LOGE(AAFwkTag::ABILITYMGR, "null account:%{public}d", userId);
184 return -1;
185 }
186
187 BroadcastUserStopping(userId);
188
189 auto appScheduler = DelayedSingleton<AppScheduler>::GetInstance();
190 if (!appScheduler) {
191 TAG_LOGE(AAFwkTag::ABILITYMGR, "null appScheduler");
192 return -1;
193 }
194 appScheduler->KillProcessesByUserId(userId);
195
196 auto abilityManagerService = DelayedSingleton<AbilityManagerService>::GetInstance();
197 if (!abilityManagerService) {
198 TAG_LOGE(AAFwkTag::ABILITYMGR, "abilityManagerService null");
199 return -1;
200 }
201
202 if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
203 auto missionListWrap = abilityManagerService->GetMissionListWrap();
204 if (!missionListWrap) {
205 TAG_LOGE(AAFwkTag::ABILITYMGR, "missionListWrap null");
206 return -1;
207 }
208 missionListWrap->RemoveUserDir(userId);
209 }
210
211 abilityManagerService->ClearUserData(userId);
212
213 BroadcastUserStopped(userId);
214 return 0;
215 }
216
LogoutUser(int32_t userId,sptr<IUserCallback> callback)217 int32_t UserController::LogoutUser(int32_t userId, sptr<IUserCallback> callback)
218 {
219 if (userId < 0 || userId == USER_ID_NO_HEAD) {
220 TAG_LOGE(AAFwkTag::ABILITYMGR, "userId invalid:%{public}d", userId);
221 if (callback) {
222 callback->OnLogoutUserDone(userId, INVALID_USERID_VALUE);
223 }
224 return INVALID_USERID_VALUE;
225 }
226 if (!IsExistOsAccount(userId)) {
227 TAG_LOGE(AAFwkTag::ABILITYMGR, "null account:%{public}d", userId);
228 if (callback) {
229 callback->OnLogoutUserDone(userId, INVALID_USERID_VALUE);
230 }
231 return INVALID_USERID_VALUE;
232 }
233 auto abilityManagerService = DelayedSingleton<AbilityManagerService>::GetInstance();
234 if (!abilityManagerService) {
235 TAG_LOGE(AAFwkTag::ABILITYMGR, "null abilityManagerService");
236 if (callback) {
237 callback->OnLogoutUserDone(userId, -1);
238 }
239 return -1;
240 }
241 abilityManagerService->RemoveLauncherDeathRecipient(userId);
242 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
243 TAG_LOGI(AAFwkTag::ABILITYMGR, "SceneBoard exit normally.");
244 Rosen::MockSessionManagerService::GetInstance().NotifyNotKillService();
245 }
246 auto appScheduler = DelayedSingleton<AppScheduler>::GetInstance();
247 if (!appScheduler) {
248 TAG_LOGE(AAFwkTag::ABILITYMGR, "null appScheduler");
249 if (callback) {
250 callback->OnLogoutUserDone(userId, INVALID_USERID_VALUE);
251 }
252 return INVALID_USERID_VALUE;
253 }
254 abilityManagerService->ClearUserData(userId);
255 appScheduler->SetEnableStartProcessFlagByUserId(userId, false);
256 if (IsCurrentUser(userId)) {
257 SetCurrentUserId(0);
258 }
259 appScheduler->KillProcessesByUserId(userId, system::GetBoolParameter(DEVELOPER_MODE_STATE, false), callback);
260 ClearAbilityUserItems(userId);
261 return 0;
262 }
263
GetCurrentUserId()264 int32_t UserController::GetCurrentUserId()
265 {
266 std::lock_guard<ffrt::mutex> guard(userLock_);
267 return currentUserId_;
268 }
269
GetUserItem(int32_t userId)270 std::shared_ptr<UserItem> UserController::GetUserItem(int32_t userId)
271 {
272 std::lock_guard<ffrt::mutex> guard(userLock_);
273 auto it = userItems_.find(userId);
274 if (it != userItems_.end()) {
275 return it->second;
276 }
277
278 return nullptr;
279 }
280
IsCurrentUser(int32_t userId)281 bool UserController::IsCurrentUser(int32_t userId)
282 {
283 int32_t oldUserId = GetCurrentUserId();
284 if (oldUserId == userId) {
285 auto userItem = GetUserItem(userId);
286 if (userItem) {
287 TAG_LOGW(AAFwkTag::ABILITYMGR, "IsCurrentUserId current:%{public}d", userId);
288 return true;
289 }
290 }
291 return false;
292 }
293
IsExistOsAccount(int32_t userId) const294 bool UserController::IsExistOsAccount(int32_t userId) const
295 {
296 bool isExist = false;
297 auto errCode = DelayedSingleton<OsAccountManagerWrapper>::GetInstance()->IsOsAccountExists(userId, isExist);
298 return (errCode == 0) && isExist;
299 }
300
GetOrCreateUserItem(int32_t userId)301 std::shared_ptr<UserItem> UserController::GetOrCreateUserItem(int32_t userId)
302 {
303 std::lock_guard<ffrt::mutex> guard(userLock_);
304 auto it = userItems_.find(userId);
305 if (it != userItems_.end()) {
306 return it->second;
307 }
308
309 auto userItem = std::make_shared<UserItem>(userId);
310 userItems_.emplace(userId, userItem);
311 return userItem;
312 }
313
SetCurrentUserId(int32_t userId)314 void UserController::SetCurrentUserId(int32_t userId)
315 {
316 std::lock_guard<ffrt::mutex> guard(userLock_);
317 currentUserId_ = userId;
318 TAG_LOGD(AAFwkTag::ABILITYMGR, "set current userId: %{public}d", userId);
319 DelayedSingleton<AppScheduler>::GetInstance()->SetCurrentUserId(userId);
320 }
321
MoveUserToForeground(int32_t oldUserId,int32_t newUserId,sptr<IUserCallback> callback,bool isAppRecovery)322 int UserController::MoveUserToForeground(int32_t oldUserId, int32_t newUserId, sptr<IUserCallback> callback,
323 bool isAppRecovery)
324 {
325 auto manager = DelayedSingleton<AbilityManagerService>::GetInstance();
326 if (!manager) {
327 return ABILITY_SERVICE_NOT_CONNECTED;
328 }
329 auto ret = manager->SwitchToUser(oldUserId, newUserId, callback, isAppRecovery);
330 if (ret != ERR_OK) {
331 TAG_LOGE(AAFwkTag::ABILITYMGR, "SwitchToUser failed: %{public}d", ret);
332 }
333 BroadcastUserBackground(oldUserId);
334 BroadcastUserForeground(newUserId);
335 return ret;
336 }
337
UserBootDone(std::shared_ptr<UserItem> & item)338 void UserController::UserBootDone(std::shared_ptr<UserItem> &item)
339 {
340 if (!item) {
341 TAG_LOGE(AAFwkTag::ABILITYMGR, "null item");
342 return;
343 }
344 int32_t userId = item->GetUserId();
345
346 std::lock_guard<ffrt::mutex> guard(userLock_);
347 auto it = userItems_.find(userId);
348 if (it == userItems_.end()) {
349 TAG_LOGE(AAFwkTag::ABILITYMGR, "invalid userId");
350 return;
351 }
352
353 if (item != it->second) {
354 TAG_LOGE(AAFwkTag::ABILITYMGR, "invalid item");
355 return;
356 }
357 item->SetState(UserState::STATE_STARTED);
358 auto manager = DelayedSingleton<AbilityManagerService>::GetInstance();
359 if (!manager) {
360 return;
361 }
362 manager->UserStarted(userId);
363 }
364
BroadcastUserBackground(int32_t userId)365 void UserController::BroadcastUserBackground(int32_t userId)
366 {
367 TAG_LOGD(AAFwkTag::ABILITYMGR, "user background");
368 }
369
BroadcastUserForeground(int32_t userId)370 void UserController::BroadcastUserForeground(int32_t userId)
371 {
372 TAG_LOGD(AAFwkTag::ABILITYMGR, "user foreground");
373 }
374
BroadcastUserStopping(int32_t userId)375 void UserController::BroadcastUserStopping(int32_t userId)
376 {
377 TAG_LOGD(AAFwkTag::ABILITYMGR, "user stopping");
378 }
379
BroadcastUserStopped(int32_t userId)380 void UserController::BroadcastUserStopped(int32_t userId)
381 {
382 TAG_LOGD(AAFwkTag::ABILITYMGR, "user stopped");
383 }
384
SendSystemUserStart(int32_t userId)385 void UserController::SendSystemUserStart(int32_t userId)
386 {
387 auto handler = eventHandler_;
388 if (!handler) {
389 TAG_LOGE(AAFwkTag::ABILITYMGR, "null handler");
390 return;
391 }
392
393 auto eventData = std::make_shared<UserEvent>();
394 eventData->newUserId = userId;
395 handler->SendEvent(EventWrap(UserEventHandler::EVENT_SYSTEM_USER_START, eventData));
396 TAG_LOGD(AAFwkTag::ABILITYMGR, "SendEvent(EVENT_SYSTEM_USER_START)");
397 }
398
ProcessEvent(const EventWrap & event)399 void UserController::ProcessEvent(const EventWrap &event)
400 {
401 auto eventId = event.GetEventId();
402 auto eventData = static_cast<UserEvent*>(event.GetEventData().get());
403 if (!eventData) {
404 TAG_LOGD(AAFwkTag::ABILITYMGR, "no event data, event id: %{public}u.", eventId);
405 return;
406 }
407
408 TAG_LOGD(AAFwkTag::ABILITYMGR, "Event id obtained: %{public}u.", eventId);
409 switch (eventId) {
410 case UserEventHandler::EVENT_SYSTEM_USER_START: {
411 HandleSystemUserStart(eventData->newUserId);
412 break;
413 }
414 case UserEventHandler::EVENT_SYSTEM_USER_CURRENT: {
415 HandleSystemUserCurrent(eventData->oldUserId, eventData->newUserId);
416 break;
417 }
418 case UserEventHandler::EVENT_REPORT_USER_SWITCH: {
419 HandleReportUserSwitch(eventData->oldUserId, eventData->newUserId, eventData->userItem);
420 break;
421 }
422 case UserEventHandler::EVENT_CONTINUE_USER_SWITCH: {
423 HandleContinueUserSwitch(eventData->oldUserId, eventData->newUserId, eventData->userItem);
424 break;
425 }
426 case UserEventHandler::EVENT_USER_SWITCH_TIMEOUT: {
427 HandleUserSwitchTimeout(eventData->oldUserId, eventData->newUserId, eventData->userItem);
428 break;
429 }
430 case UserEventHandler::EVENT_REPORT_USER_SWITCH_DONE: {
431 HandleUserSwitchDone(eventData->newUserId);
432 break;
433 }
434 default: {
435 TAG_LOGW(AAFwkTag::ABILITYMGR, "Unsupported event.");
436 break;
437 }
438 }
439 }
440
SendSystemUserCurrent(int32_t oldUserId,int32_t newUserId)441 void UserController::SendSystemUserCurrent(int32_t oldUserId, int32_t newUserId)
442 {
443 auto handler = eventHandler_;
444 if (!handler) {
445 TAG_LOGE(AAFwkTag::ABILITYMGR, "null handler");
446 return;
447 }
448
449 auto eventData = std::make_shared<UserEvent>();
450 eventData->oldUserId = oldUserId;
451 eventData->newUserId = newUserId;
452 handler->SendEvent(EventWrap(UserEventHandler::EVENT_SYSTEM_USER_CURRENT, eventData));
453 TAG_LOGD(AAFwkTag::ABILITYMGR, "SendEvent(EVENT_SYSTEM_USER_CURRENT)");
454 }
455
SendReportUserSwitch(int32_t oldUserId,int32_t newUserId,std::shared_ptr<UserItem> & usrItem)456 void UserController::SendReportUserSwitch(int32_t oldUserId, int32_t newUserId,
457 std::shared_ptr<UserItem> &usrItem)
458 {
459 auto handler = eventHandler_;
460 if (!handler) {
461 TAG_LOGE(AAFwkTag::ABILITYMGR, "null handler");
462 return;
463 }
464
465 auto eventData = std::make_shared<UserEvent>();
466 eventData->oldUserId = oldUserId;
467 eventData->newUserId = newUserId;
468 eventData->userItem = usrItem;
469 handler->SendEvent(EventWrap(UserEventHandler::EVENT_REPORT_USER_SWITCH, eventData));
470 TAG_LOGD(AAFwkTag::ABILITYMGR, "SendEvent(EVENT_REPORT_USER_SWITCH)");
471 }
472
SendUserSwitchTimeout(int32_t oldUserId,int32_t newUserId,std::shared_ptr<UserItem> & usrItem)473 void UserController::SendUserSwitchTimeout(int32_t oldUserId, int32_t newUserId,
474 std::shared_ptr<UserItem> &usrItem)
475 {
476 auto handler = eventHandler_;
477 if (!handler) {
478 TAG_LOGE(AAFwkTag::ABILITYMGR, "null handler");
479 return;
480 }
481
482 auto eventData = std::make_shared<UserEvent>();
483 eventData->oldUserId = oldUserId;
484 eventData->newUserId = newUserId;
485 eventData->userItem = usrItem;
486 handler->SendEvent(EventWrap(UserEventHandler::EVENT_USER_SWITCH_TIMEOUT,
487 eventData), USER_SWITCH_TIMEOUT);
488 TAG_LOGD(AAFwkTag::ABILITYMGR, "SendEvent(EVENT_USER_SWITCH_TIMEOUT)");
489 }
490
SendContinueUserSwitch(int32_t oldUserId,int32_t newUserId,std::shared_ptr<UserItem> & usrItem)491 void UserController::SendContinueUserSwitch(int32_t oldUserId, int32_t newUserId,
492 std::shared_ptr<UserItem> &usrItem)
493 {
494 auto handler = eventHandler_;
495 if (!handler) {
496 TAG_LOGE(AAFwkTag::ABILITYMGR, "null handler");
497 return;
498 }
499
500 auto eventData = std::make_shared<UserEvent>();
501 eventData->oldUserId = oldUserId;
502 eventData->newUserId = newUserId;
503 eventData->userItem = usrItem;
504 handler->SendEvent(EventWrap(UserEventHandler::EVENT_CONTINUE_USER_SWITCH, eventData));
505 TAG_LOGD(AAFwkTag::ABILITYMGR, "SendEvent(EVENT_CONTINUE_USER_SWITCH)");
506 }
507
SendUserSwitchDone(int32_t userId)508 void UserController::SendUserSwitchDone(int32_t userId)
509 {
510 auto handler = eventHandler_;
511 if (!handler) {
512 TAG_LOGE(AAFwkTag::ABILITYMGR, "null handler");
513 return;
514 }
515
516 auto eventData = std::make_shared<UserEvent>();
517 eventData->newUserId = userId;
518 handler->SendEvent(EventWrap(UserEventHandler::EVENT_REPORT_USER_SWITCH_DONE,
519 eventData));
520 TAG_LOGD(AAFwkTag::ABILITYMGR, "SendEvent(EVENT_REPORT_USER_SWITCH_DONE)");
521 }
522
HandleSystemUserStart(int32_t userId)523 void UserController::HandleSystemUserStart(int32_t userId)
524 {
525 TAG_LOGD(AAFwkTag::ABILITYMGR, "notify system user start.");
526 }
527
HandleSystemUserCurrent(int32_t oldUserId,int32_t newUserId)528 void UserController::HandleSystemUserCurrent(int32_t oldUserId, int32_t newUserId)
529 {
530 TAG_LOGD(AAFwkTag::ABILITYMGR, "notify system user current.");
531 }
532
HandleReportUserSwitch(int32_t oldUserId,int32_t newUserId,std::shared_ptr<UserItem> & usrItem)533 void UserController::HandleReportUserSwitch(int32_t oldUserId, int32_t newUserId,
534 std::shared_ptr<UserItem> &usrItem)
535 {
536 TAG_LOGD(AAFwkTag::ABILITYMGR, "notify report user switch.");
537 }
538
HandleUserSwitchTimeout(int32_t oldUserId,int32_t newUserId,std::shared_ptr<UserItem> & usrItem)539 void UserController::HandleUserSwitchTimeout(int32_t oldUserId, int32_t newUserId,
540 std::shared_ptr<UserItem> &usrItem)
541 {
542 SendContinueUserSwitch(oldUserId, newUserId, usrItem);
543 }
544
HandleContinueUserSwitch(int32_t oldUserId,int32_t newUserId,std::shared_ptr<UserItem> & usrItem)545 void UserController::HandleContinueUserSwitch(int32_t oldUserId, int32_t newUserId,
546 std::shared_ptr<UserItem> &usrItem)
547 {
548 auto manager = DelayedSingleton<AbilityManagerService>::GetInstance();
549 if (manager && !Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
550 manager->StopFreezingScreen();
551 }
552 SendUserSwitchDone(newUserId);
553 }
554
HandleUserSwitchDone(int32_t userId)555 void UserController::HandleUserSwitchDone(int32_t userId)
556 {
557 TAG_LOGD(AAFwkTag::ABILITYMGR, "handle user switch done.");
558 }
559
GetFreezingNewUserId() const560 int32_t UserController::GetFreezingNewUserId() const
561 {
562 return freezingNewUserId_;
563 }
564
SetFreezingNewUserId(int32_t userId)565 void UserController::SetFreezingNewUserId(int32_t userId)
566 {
567 freezingNewUserId_ = userId;
568 }
569 }
570 }
571