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