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