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