• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 < 0 || userId == USER_ID_NO_HEAD) {
88         TAG_LOGE(AAFwkTag::ABILITYMGR, "StartUserId invalid:%{public}d", userId);
89         callback->OnStartUserDone(userId, INVALID_USERID_VALUE);
90         return INVALID_USERID_VALUE;
91     }
92 
93     if (IsCurrentUser(userId)) {
94         TAG_LOGW(AAFwkTag::ABILITYMGR, "StartUser current:%{public}d", userId);
95         callback->OnStartUserDone(userId, ERR_OK);
96         return ERR_OK;
97     }
98 
99     auto appScheduler = DelayedSingleton<AppScheduler>::GetInstance();
100     if (!appScheduler) {
101         TAG_LOGE(AAFwkTag::ABILITYMGR, "null appScheduler");
102         return ABILITY_SERVICE_NOT_CONNECTED;
103     }
104     appScheduler->SetEnableStartProcessFlagByUserId(userId, true);
105 
106     if (!IsExistOsAccount(userId)) {
107         TAG_LOGE(AAFwkTag::ABILITYMGR, "null StartUser account:%{public}d", userId);
108         callback->OnStartUserDone(userId, INVALID_USERID_VALUE);
109         return INVALID_USERID_VALUE;
110     }
111 
112     if (GetCurrentUserId() != USER_ID_NO_HEAD && !Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
113         // start freezing screen
114         SetFreezingNewUserId(userId);
115         DelayedSingleton<AbilityManagerService>::GetInstance()->StartFreezingScreen();
116     }
117 
118     auto oldUserId = GetCurrentUserId();
119     auto userItem = GetOrCreateUserItem(userId);
120     auto state = userItem->GetState();
121     if (state == STATE_STOPPING || state == STATE_SHUTDOWN) {
122         TAG_LOGE(AAFwkTag::ABILITYMGR, "StartUser user stop, userId:%{public}d", userId);
123         callback->OnStartUserDone(userId, ERR_DEAD_OBJECT);
124         return ERR_DEAD_OBJECT;
125     }
126 
127     SetCurrentUserId(userId);
128     // notify wms switching now
129 
130     bool needStart = false;
131     if (state == STATE_BOOTING) {
132         needStart = true;
133         // send user start msg.
134         SendSystemUserStart(userId);
135     }
136 
137     SendSystemUserCurrent(oldUserId, userId);
138     SendReportUserSwitch(oldUserId, userId, userItem);
139     SendUserSwitchTimeout(oldUserId, userId, userItem);
140 
141     if (needStart) {
142         BroadcastUserStarted(userId);
143     }
144 
145     UserBootDone(userItem);
146     return MoveUserToForeground(oldUserId, userId, callback, isAppRecovery);
147 }
148 
StopUser(int32_t userId)149 int32_t UserController::StopUser(int32_t userId)
150 {
151     if (userId < 0 || userId == USER_ID_NO_HEAD || userId == USER_ID_DEFAULT) {
152         TAG_LOGE(AAFwkTag::ABILITYMGR, "userId invalid:%{public}d", userId);
153         return -1;
154     }
155 
156     if (IsCurrentUser(userId)) {
157         TAG_LOGW(AAFwkTag::ABILITYMGR, "user current:%{public}d", userId);
158         return 0;
159     }
160 
161     if (!IsExistOsAccount(userId)) {
162         TAG_LOGE(AAFwkTag::ABILITYMGR, "null account:%{public}d", userId);
163         return -1;
164     }
165 
166     BroadcastUserStopping(userId);
167 
168     auto appScheduler = DelayedSingleton<AppScheduler>::GetInstance();
169     if (!appScheduler) {
170         TAG_LOGE(AAFwkTag::ABILITYMGR, "null appScheduler");
171         return -1;
172     }
173     appScheduler->KillProcessesByUserId(userId);
174 
175     auto abilityManagerService = DelayedSingleton<AbilityManagerService>::GetInstance();
176     if (!abilityManagerService) {
177         TAG_LOGE(AAFwkTag::ABILITYMGR, "abilityManagerService null");
178         return -1;
179     }
180 
181     if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
182         auto missionListWrap = abilityManagerService->GetMissionListWrap();
183         if (!missionListWrap) {
184             TAG_LOGE(AAFwkTag::ABILITYMGR, "missionListWrap null");
185             return -1;
186         }
187         missionListWrap->RemoveUserDir(userId);
188     }
189 
190     abilityManagerService->ClearUserData(userId);
191 
192     BroadcastUserStopped(userId);
193     return 0;
194 }
195 
LogoutUser(int32_t userId,sptr<IUserCallback> callback)196 int32_t UserController::LogoutUser(int32_t userId, sptr<IUserCallback> callback)
197 {
198     if (userId < 0 || userId == USER_ID_NO_HEAD) {
199         TAG_LOGE(AAFwkTag::ABILITYMGR, "userId invalid:%{public}d", userId);
200         if (callback) {
201             callback->OnLogoutUserDone(userId, INVALID_USERID_VALUE);
202         }
203         return INVALID_USERID_VALUE;
204     }
205     if (!IsExistOsAccount(userId)) {
206         TAG_LOGE(AAFwkTag::ABILITYMGR, "null account:%{public}d", userId);
207         if (callback) {
208             callback->OnLogoutUserDone(userId, INVALID_USERID_VALUE);
209         }
210         return INVALID_USERID_VALUE;
211     }
212     auto abilityManagerService = DelayedSingleton<AbilityManagerService>::GetInstance();
213     if (!abilityManagerService) {
214         TAG_LOGE(AAFwkTag::ABILITYMGR, "null abilityManagerService");
215         if (callback) {
216             callback->OnLogoutUserDone(userId, -1);
217         }
218         return -1;
219     }
220     abilityManagerService->RemoveLauncherDeathRecipient(userId);
221     if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
222         TAG_LOGI(AAFwkTag::ABILITYMGR, "SceneBoard exit normally.");
223         Rosen::MockSessionManagerService::GetInstance().NotifyNotKillService();
224     }
225     auto appScheduler = DelayedSingleton<AppScheduler>::GetInstance();
226     if (!appScheduler) {
227         TAG_LOGE(AAFwkTag::ABILITYMGR, "null appScheduler");
228         if (callback) {
229             callback->OnLogoutUserDone(userId, INVALID_USERID_VALUE);
230         }
231         return INVALID_USERID_VALUE;
232     }
233     abilityManagerService->ClearUserData(userId);
234     appScheduler->SetEnableStartProcessFlagByUserId(userId, false);
235     if (IsCurrentUser(userId)) {
236         SetCurrentUserId(0);
237     }
238     appScheduler->KillProcessesByUserId(userId, system::GetBoolParameter(DEVELOPER_MODE_STATE, false), callback);
239     ClearAbilityUserItems(userId);
240     return 0;
241 }
242 
GetCurrentUserId()243 int32_t UserController::GetCurrentUserId()
244 {
245     std::lock_guard<ffrt::mutex> guard(userLock_);
246     return currentUserId_;
247 }
248 
GetUserItem(int32_t userId)249 std::shared_ptr<UserItem> UserController::GetUserItem(int32_t userId)
250 {
251     std::lock_guard<ffrt::mutex> guard(userLock_);
252     auto it = userItems_.find(userId);
253     if (it != userItems_.end()) {
254         return it->second;
255     }
256 
257     return nullptr;
258 }
259 
IsCurrentUser(int32_t userId)260 bool UserController::IsCurrentUser(int32_t userId)
261 {
262     int32_t oldUserId = GetCurrentUserId();
263     if (oldUserId == userId) {
264         auto userItem = GetUserItem(userId);
265         if (userItem) {
266             TAG_LOGW(AAFwkTag::ABILITYMGR, "IsCurrentUserId current:%{public}d", userId);
267             return true;
268         }
269     }
270     return false;
271 }
272 
IsExistOsAccount(int32_t userId)273 bool UserController::IsExistOsAccount(int32_t userId)
274 {
275     bool isExist = false;
276     auto errCode = DelayedSingleton<OsAccountManagerWrapper>::GetInstance()->IsOsAccountExists(userId, isExist);
277     return (errCode == 0) && isExist;
278 }
279 
GetOrCreateUserItem(int32_t userId)280 std::shared_ptr<UserItem> UserController::GetOrCreateUserItem(int32_t userId)
281 {
282     std::lock_guard<ffrt::mutex> guard(userLock_);
283     auto it = userItems_.find(userId);
284     if (it != userItems_.end()) {
285         return it->second;
286     }
287 
288     auto userItem = std::make_shared<UserItem>(userId);
289     userItems_.emplace(userId, userItem);
290     return userItem;
291 }
292 
SetCurrentUserId(int32_t userId)293 void UserController::SetCurrentUserId(int32_t userId)
294 {
295     std::lock_guard<ffrt::mutex> guard(userLock_);
296     currentUserId_ = userId;
297     TAG_LOGD(AAFwkTag::ABILITYMGR, "set current userId: %{public}d", userId);
298     DelayedSingleton<AppScheduler>::GetInstance()->SetCurrentUserId(userId);
299 }
300 
MoveUserToForeground(int32_t oldUserId,int32_t newUserId,sptr<IUserCallback> callback,bool isAppRecovery)301 int UserController::MoveUserToForeground(int32_t oldUserId, int32_t newUserId, sptr<IUserCallback> callback,
302     bool isAppRecovery)
303 {
304     auto manager = DelayedSingleton<AbilityManagerService>::GetInstance();
305     if (!manager) {
306         return ABILITY_SERVICE_NOT_CONNECTED;
307     }
308     auto ret = manager->SwitchToUser(oldUserId, newUserId, callback, isAppRecovery);
309     if (ret != ERR_OK) {
310         TAG_LOGE(AAFwkTag::ABILITYMGR, "SwitchToUser failed: %{public}d", ret);
311     }
312     BroadcastUserBackground(oldUserId);
313     BroadcastUserForeground(newUserId);
314     return ret;
315 }
316 
UserBootDone(std::shared_ptr<UserItem> & item)317 void UserController::UserBootDone(std::shared_ptr<UserItem> &item)
318 {
319     if (!item) {
320         return;
321     }
322     int32_t userId = item->GetUserId();
323 
324     std::lock_guard<ffrt::mutex> guard(userLock_);
325     auto it = userItems_.find(userId);
326     if (it == userItems_.end()) {
327         return;
328     }
329 
330     if (item != it->second) {
331         return;
332     }
333     item->SetState(UserState::STATE_STARTED);
334     auto manager = DelayedSingleton<AbilityManagerService>::GetInstance();
335     if (!manager) {
336         return;
337     }
338     manager->UserStarted(userId);
339 }
340 
BroadcastUserStarted(int32_t userId)341 void UserController::BroadcastUserStarted(int32_t userId)
342 {
343     // broadcast event user start.
344 }
345 
BroadcastUserBackground(int32_t userId)346 void UserController::BroadcastUserBackground(int32_t userId)
347 {
348     // broadcast event user switch to bg.
349 }
350 
BroadcastUserForeground(int32_t userId)351 void UserController::BroadcastUserForeground(int32_t userId)
352 {
353     // broadcast event user switch to fg.
354 }
355 
BroadcastUserStopping(int32_t userId)356 void UserController::BroadcastUserStopping(int32_t userId)
357 {
358 }
359 
BroadcastUserStopped(int32_t userId)360 void UserController::BroadcastUserStopped(int32_t userId)
361 {
362 }
363 
SendSystemUserStart(int32_t userId)364 void UserController::SendSystemUserStart(int32_t userId)
365 {
366     auto handler = eventHandler_;
367     if (!handler) {
368         return;
369     }
370 
371     auto eventData = std::make_shared<UserEvent>();
372     eventData->newUserId = userId;
373     handler->SendEvent(EventWrap(UserEventHandler::EVENT_SYSTEM_USER_START, eventData));
374 }
375 
ProcessEvent(const EventWrap & event)376 void UserController::ProcessEvent(const EventWrap &event)
377 {
378     auto eventId = event.GetEventId();
379     auto eventData = static_cast<UserEvent*>(event.GetEventData().get());
380     if (!eventData) {
381         TAG_LOGD(AAFwkTag::ABILITYMGR, "no event data, event id: %{public}u.", eventId);
382         return;
383     }
384 
385     TAG_LOGD(AAFwkTag::ABILITYMGR, "Event id obtained: %{public}u.", eventId);
386     switch (eventId) {
387         case UserEventHandler::EVENT_SYSTEM_USER_START: {
388             HandleSystemUserStart(eventData->newUserId);
389             break;
390         }
391         case UserEventHandler::EVENT_SYSTEM_USER_CURRENT: {
392             HandleSystemUserCurrent(eventData->oldUserId, eventData->newUserId);
393             break;
394         }
395         case UserEventHandler::EVENT_REPORT_USER_SWITCH: {
396             HandleReportUserSwitch(eventData->oldUserId, eventData->newUserId, eventData->userItem);
397             break;
398         }
399         case UserEventHandler::EVENT_CONTINUE_USER_SWITCH: {
400             HandleContinueUserSwitch(eventData->oldUserId, eventData->newUserId, eventData->userItem);
401             break;
402         }
403         case UserEventHandler::EVENT_USER_SWITCH_TIMEOUT: {
404             HandleUserSwitchTimeout(eventData->oldUserId, eventData->newUserId, eventData->userItem);
405             break;
406         }
407         case UserEventHandler::EVENT_REPORT_USER_SWITCH_DONE: {
408             HandleUserSwitchDone(eventData->newUserId);
409             break;
410         }
411         default: {
412             TAG_LOGW(AAFwkTag::ABILITYMGR, "Unsupported  event.");
413             break;
414         }
415     }
416 }
417 
SendSystemUserCurrent(int32_t oldUserId,int32_t newUserId)418 void UserController::SendSystemUserCurrent(int32_t oldUserId, int32_t newUserId)
419 {
420     auto handler = eventHandler_;
421     if (!handler) {
422         return;
423     }
424 
425     auto eventData = std::make_shared<UserEvent>();
426     eventData->oldUserId = oldUserId;
427     eventData->newUserId = newUserId;
428     handler->SendEvent(EventWrap(UserEventHandler::EVENT_SYSTEM_USER_CURRENT, eventData));
429 }
430 
SendReportUserSwitch(int32_t oldUserId,int32_t newUserId,std::shared_ptr<UserItem> & usrItem)431 void UserController::SendReportUserSwitch(int32_t oldUserId, int32_t newUserId,
432     std::shared_ptr<UserItem> &usrItem)
433 {
434     auto handler = eventHandler_;
435     if (!handler) {
436         return;
437     }
438 
439     auto eventData = std::make_shared<UserEvent>();
440     eventData->oldUserId = oldUserId;
441     eventData->newUserId = newUserId;
442     eventData->userItem = usrItem;
443     handler->SendEvent(EventWrap(UserEventHandler::EVENT_REPORT_USER_SWITCH, eventData));
444 }
445 
SendUserSwitchTimeout(int32_t oldUserId,int32_t newUserId,std::shared_ptr<UserItem> & usrItem)446 void UserController::SendUserSwitchTimeout(int32_t oldUserId, int32_t newUserId,
447     std::shared_ptr<UserItem> &usrItem)
448 {
449     auto handler = eventHandler_;
450     if (!handler) {
451         return;
452     }
453 
454     auto eventData = std::make_shared<UserEvent>();
455     eventData->oldUserId = oldUserId;
456     eventData->newUserId = newUserId;
457     eventData->userItem = usrItem;
458     handler->SendEvent(EventWrap(UserEventHandler::EVENT_USER_SWITCH_TIMEOUT,
459         eventData), USER_SWITCH_TIMEOUT);
460 }
461 
SendContinueUserSwitch(int32_t oldUserId,int32_t newUserId,std::shared_ptr<UserItem> & usrItem)462 void UserController::SendContinueUserSwitch(int32_t oldUserId, int32_t newUserId,
463     std::shared_ptr<UserItem> &usrItem)
464 {
465     auto handler = eventHandler_;
466     if (!handler) {
467         return;
468     }
469 
470     auto eventData = std::make_shared<UserEvent>();
471     eventData->oldUserId = oldUserId;
472     eventData->newUserId = newUserId;
473     eventData->userItem = usrItem;
474     handler->SendEvent(EventWrap(UserEventHandler::EVENT_CONTINUE_USER_SWITCH, eventData));
475 }
476 
SendUserSwitchDone(int32_t userId)477 void UserController::SendUserSwitchDone(int32_t userId)
478 {
479     auto handler = eventHandler_;
480     if (!handler) {
481         return;
482     }
483 
484     auto eventData = std::make_shared<UserEvent>();
485     eventData->newUserId = userId;
486     handler->SendEvent(EventWrap(UserEventHandler::EVENT_REPORT_USER_SWITCH_DONE,
487         eventData));
488 }
489 
HandleSystemUserStart(int32_t userId)490 void UserController::HandleSystemUserStart(int32_t userId)
491 {
492     // notify system mgr user start.
493 }
494 
HandleSystemUserCurrent(int32_t oldUserId,int32_t newUserId)495 void UserController::HandleSystemUserCurrent(int32_t oldUserId, int32_t newUserId)
496 {
497     // notify system mgr user switch to new.
498 }
499 
HandleReportUserSwitch(int32_t oldUserId,int32_t newUserId,std::shared_ptr<UserItem> & usrItem)500 void UserController::HandleReportUserSwitch(int32_t oldUserId, int32_t newUserId,
501     std::shared_ptr<UserItem> &usrItem)
502 {
503     // notify user switch observers, not support yet.
504 }
505 
HandleUserSwitchTimeout(int32_t oldUserId,int32_t newUserId,std::shared_ptr<UserItem> & usrItem)506 void UserController::HandleUserSwitchTimeout(int32_t oldUserId, int32_t newUserId,
507     std::shared_ptr<UserItem> &usrItem)
508 {
509     // other observers
510     SendContinueUserSwitch(oldUserId, newUserId, usrItem);
511 }
512 
HandleContinueUserSwitch(int32_t oldUserId,int32_t newUserId,std::shared_ptr<UserItem> & usrItem)513 void UserController::HandleContinueUserSwitch(int32_t oldUserId, int32_t newUserId,
514     std::shared_ptr<UserItem> &usrItem)
515 {
516     auto manager = DelayedSingleton<AbilityManagerService>::GetInstance();
517     if (manager && !Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
518         manager->StopFreezingScreen();
519     }
520     SendUserSwitchDone(newUserId);
521 }
522 
HandleUserSwitchDone(int32_t userId)523 void UserController::HandleUserSwitchDone(int32_t userId)
524 {
525     // notify wms switching done.
526     // notify user switch observers.
527 }
528 
GetFreezingNewUserId() const529 int32_t UserController::GetFreezingNewUserId() const
530 {
531     return freezingNewUserId_;
532 }
533 
SetFreezingNewUserId(int32_t userId)534 void UserController::SetFreezingNewUserId(int32_t userId)
535 {
536     freezingNewUserId_ = userId;
537 }
538 }
539 }
540