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