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