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