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