• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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     int32_t GetCurrentUserId();
85 
86     std::shared_ptr<UserItem> GetUserItem(int32_t userId);
87 
88     void ProcessEvent(const EventWrap &event);
89 
90 private:
91     bool IsCurrentUser(int32_t userId);
92     bool IsExistOsAccount(int32_t userId);
93     std::shared_ptr<UserItem> GetOrCreateUserItem(int32_t userId);
94     void SetCurrentUserId(int32_t userId);
95     void BroadcastUserStarted(int32_t userId);
96     void MoveUserToForeground(int32_t oldUserId, int32_t newUserId);
97     void UserBootDone(std::shared_ptr<UserItem> &item);
98     void BroadcastUserBackground(int32_t userId);
99     void BroadcastUserForeground(int32_t userId);
100     void BroadcastUserStopping(int32_t userId);
101     void BroadcastUserStopped(int32_t userId);
102 
103     void SendSystemUserStart(int32_t userId);
104     void SendSystemUserCurrent(int32_t oldUserId, int32_t newUserId);
105     void SendReportUserSwitch(int32_t oldUserId, int32_t newUserId,
106         std::shared_ptr<UserItem> &usrItem);
107     void SendUserSwitchTimeout(int32_t oldUserId, int32_t newUserId,
108         std::shared_ptr<UserItem> &usrItem);
109     void SendContinueUserSwitch(int32_t oldUserId, int32_t newUserId,
110         std::shared_ptr<UserItem> &usrItem);
111     void SendUserSwitchDone(int32_t userId);
112 
113     void HandleSystemUserStart(int32_t userId);
114     void HandleSystemUserCurrent(int32_t oldUserId, int32_t newUserId);
115     void HandleReportUserSwitch(int32_t oldUserId, int32_t newUserId,
116         std::shared_ptr<UserItem> &usrItem);
117     void HandleUserSwitchTimeout(int32_t oldUserId, int32_t newUserId,
118         std::shared_ptr<UserItem> &usrItem);
119     void HandleContinueUserSwitch(int32_t oldUserId, int32_t newUserId,
120         std::shared_ptr<UserItem> &usrItem);
121     void HandleUserSwitchDone(int32_t userId);
122 
123 private:
124     ffrt::mutex userLock_;
125     int32_t currentUserId_ = USER_ID_NO_HEAD;
126     std::unordered_map<int32_t, std::shared_ptr<UserItem>> userItems_;
127     std::shared_ptr<UserEventHandler> eventHandler_;
128 };
129 }  // namespace AAFwk
130 }  // namespace OHOS
131 #endif  // OHOS_ABILITY_RUNTIME_USER_CONTROLLER_H
132