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 #ifndef OHOS_ABILITY_RUNTIME_USER_CONTROLLER_H 17 #define OHOS_ABILITY_RUNTIME_USER_CONTROLLER_H 18 19 #include <unordered_map> 20 #include <memory> 21 #include "cpp/mutex.h" 22 23 #include "user_event_handler.h" 24 #include "refbase.h" 25 26 namespace OHOS { 27 namespace AAFwk { 28 class IUserCallback; 29 30 const int32_t USER_ID_NO_HEAD = 0; 31 const int32_t USER_ID_DEFAULT = 100; 32 33 enum UserState { 34 STATE_BOOTING = 0, 35 STATE_STARTED, 36 STATE_STOPPING, 37 STATE_SHUTDOWN 38 }; 39 40 class UserItem { 41 public: 42 explicit UserItem(int32_t id); 43 virtual ~UserItem(); 44 45 int32_t GetUserId(); 46 void SetState(const UserState &state); 47 UserState GetState(); 48 49 private: 50 int32_t userId_; 51 UserState curState_ = STATE_BOOTING; 52 UserState lastState_ = STATE_BOOTING; 53 }; 54 55 class UserEvent : public EventDataBase { 56 public: 57 virtual ~UserEvent() = default; 58 int32_t oldUserId; 59 int32_t newUserId; 60 std::shared_ptr<UserItem> userItem; 61 }; 62 63 class UserController : public std::enable_shared_from_this<UserController> { 64 public: 65 UserController(); 66 virtual ~UserController(); 67 68 void Init(); 69 70 /** 71 * Start user, if it is not running.. 72 * 73 * @param userId id of started user. 74 * @param isForeground whether user should brout to foreground. 75 * @return 0 if the user has been successfully started. 76 */ 77 void StartUser(int32_t userId, sptr<IUserCallback> callback); 78 79 /** 80 * Stop user, if it is running.. 81 * 82 * @param userId id of started user. 83 * @return 0 if the user has been successfully started. 84 */ 85 int32_t StopUser(int32_t userId); 86 87 /** 88 * Logout user, if it is running.. 89 * 90 * @param userId id of Logout user. 91 * @return 0 if the user has been successfully Logout. 92 */ 93 int32_t LogoutUser(int32_t userId); 94 95 int32_t GetCurrentUserId(); 96 97 std::shared_ptr<UserItem> GetUserItem(int32_t userId); 98 99 void ProcessEvent(const EventWrap &event); 100 101 int32_t GetFreezingNewUserId() const; 102 103 void SetFreezingNewUserId(int32_t userId); 104 105 void ClearAbilityUserItems(int32_t userId); 106 107 private: 108 bool IsCurrentUser(int32_t userId); 109 bool IsExistOsAccount(int32_t userId); 110 std::shared_ptr<UserItem> GetOrCreateUserItem(int32_t userId); 111 void SetCurrentUserId(int32_t userId); 112 void BroadcastUserStarted(int32_t userId); 113 void MoveUserToForeground(int32_t oldUserId, int32_t newUserId, sptr<IUserCallback> callback); 114 void UserBootDone(std::shared_ptr<UserItem> &item); 115 void BroadcastUserBackground(int32_t userId); 116 void BroadcastUserForeground(int32_t userId); 117 void BroadcastUserStopping(int32_t userId); 118 void BroadcastUserStopped(int32_t userId); 119 120 void SendSystemUserStart(int32_t userId); 121 void SendSystemUserCurrent(int32_t oldUserId, int32_t newUserId); 122 void SendReportUserSwitch(int32_t oldUserId, int32_t newUserId, 123 std::shared_ptr<UserItem> &usrItem); 124 void SendUserSwitchTimeout(int32_t oldUserId, int32_t newUserId, 125 std::shared_ptr<UserItem> &usrItem); 126 void SendContinueUserSwitch(int32_t oldUserId, int32_t newUserId, 127 std::shared_ptr<UserItem> &usrItem); 128 void SendUserSwitchDone(int32_t userId); 129 130 void HandleSystemUserStart(int32_t userId); 131 void HandleSystemUserCurrent(int32_t oldUserId, int32_t newUserId); 132 void HandleReportUserSwitch(int32_t oldUserId, int32_t newUserId, 133 std::shared_ptr<UserItem> &usrItem); 134 void HandleUserSwitchTimeout(int32_t oldUserId, int32_t newUserId, 135 std::shared_ptr<UserItem> &usrItem); 136 void HandleContinueUserSwitch(int32_t oldUserId, int32_t newUserId, 137 std::shared_ptr<UserItem> &usrItem); 138 void HandleUserSwitchDone(int32_t userId); 139 140 private: 141 ffrt::mutex userLock_; 142 int32_t currentUserId_ = USER_ID_NO_HEAD; 143 std::unordered_map<int32_t, std::shared_ptr<UserItem>> userItems_; 144 std::shared_ptr<UserEventHandler> eventHandler_; 145 int32_t freezingNewUserId_ = -1; 146 }; 147 } // namespace AAFwk 148 } // namespace OHOS 149 #endif // OHOS_ABILITY_RUNTIME_USER_CONTROLLER_H 150