• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OSACCOUNT_IINNER_OS_ACCOUNT_MANAGER_H
17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OSACCOUNT_IINNER_OS_ACCOUNT_MANAGER_H
18 
19 #include <memory>
20 #include "iinner_os_account.h"
21 #ifdef SUPPORT_DOMAIN_ACCOUNTS
22 #include "inner_domain_account_manager.h"
23 #endif // SUPPORT_DOMAIN_ACCOUNTS
24 #include "ios_account_control.h"
25 #include "ios_account_subscribe.h"
26 #include "ohos_account_manager.h"
27 #include "os_account_control_file_manager.h"
28 #include "os_account_interface.h"
29 #include "os_account_activate_lock_plugin_manager.h"
30 #ifdef SUPPORT_LOCK_OS_ACCOUNT
31 #include "os_account_lock_os_account_plugin_manager.h"
32 #endif // SUPPORT_LOCK_OS_ACCOUNT
33 #include "safe_map.h"
34 #include "singleton.h"
35 
36 namespace OHOS {
37 namespace AccountSA {
38 class IInnerOsAccountManager : public IInnerOsAccount {
39 public:
40     static IInnerOsAccountManager &GetInstance();
41     bool Init(const std::set<int32_t> &initAccounts = {Constants::START_USER_ID}) override;
42     ErrCode CreateOsAccount(
43         const std::string &name, const OsAccountType &type, OsAccountInfo &osAccountInfo) override;
44     ErrCode CreateOsAccount(const std::string &localName, const std::string &shortName,
45         const OsAccountType &type, OsAccountInfo &osAccountInfo, const CreateOsAccountOptions &options = {}) override;
46     ErrCode CreateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo,
47         const CreateOsAccountOptions &options = {}) override;
48     ErrCode UpdateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo) override;
49     ErrCode UpdateFirstOsAccountInfo(OsAccountInfo& accountInfoOld, OsAccountInfo& osAccountInfo) override;
50     ErrCode RemoveOsAccount(const int id) override;
51     ErrCode IsOsAccountExists(const int id, bool &isOsAccountExits) override;
52     ErrCode IsOsAccountActived(const int id, bool &isOsAccountActived) override;
53     ErrCode IsOsAccountConstraintEnable(
54         const int id, const std::string &constraint, bool &isOsAccountConstraintEnable) override;
55     ErrCode IsOsAccountVerified(const int id, bool &isVerified) override;
56     ErrCode IsOsAccountDeactivating(const int id, bool &isDeactivating) override;
57     ErrCode GetCreatedOsAccountsCount(unsigned int &createdOsAccountCount) override;
58     ErrCode QueryMaxOsAccountNumber(uint32_t &maxOsAccountNumber) override;
59     ErrCode QueryMaxLoggedInOsAccountNumber(uint32_t &maxNum) override;
60     ErrCode GetOsAccountAllConstraints(const int id, std::vector<std::string> &constraints) override;
61     ErrCode QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> &osAccountInfos) override;
62     ErrCode QueryOsAccountById(const int id, OsAccountInfo &osAccountInfo) override;
63     ErrCode GetOsAccountShortName(const int id, std::string &shortName) override;
64     ErrCode GetOsAccountName(const int id, std::string &name) override;
65     ErrCode GetOsAccountType(const int id, OsAccountType &type) override;
66     ErrCode GetOsAccountProfilePhoto(const int id, std::string &photo) override;
67     ErrCode IsMultiOsAccountEnable(bool &isMultiOsAccountEnable) override;
68     ErrCode SetOsAccountName(const int id, const std::string &name) override;
69     ErrCode SetOsAccountConstraints(
70         const int id, const std::vector<std::string> &constraints, const bool enable) override;
71     ErrCode SetOsAccountProfilePhoto(const int id, const std::string &photo) override;
72     ErrCode ActivateOsAccount(const int id, const bool startStorage = true,
73         const uint64_t displayId = 0, bool isAppRecovery = false) override;
74     ErrCode DeactivateOsAccount(const int id, bool isStopStorage = true) override;
75     ErrCode StartOsAccount(const int id) override;
76     ErrCode GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber, int &id) override;
77     ErrCode GetSerialNumberByOsAccountLocalId(const int &id, int64_t &serialNumber) override;
78     ErrCode SubscribeOsAccount(
79         const OsAccountSubscribeInfo &subscribeInfo, const sptr<IRemoteObject> &eventListener) override;
80     ErrCode UnsubscribeOsAccount(const sptr<IRemoteObject> &eventListener) override;
81     const std::shared_ptr<OsAccountSubscribeInfo> GetSubscribeRecordInfo(
82         const sptr<IRemoteObject> &eventListener) override;
83     OS_ACCOUNT_SWITCH_MOD GetOsAccountSwitchMod() override;
84     ErrCode IsOsAccountCompleted(const int id, bool &isOsAccountCompleted) override;
85     ErrCode SetOsAccountIsVerified(const int id, const bool isVerified) override;
86     ErrCode SetOsAccountIsLoggedIn(const int32_t id, const bool isLoggedIn) override;
87     ErrCode GetOsAccountCredentialId(const int id, uint64_t &credentialId) override;
88     ErrCode SetOsAccountCredentialId(const int id, uint64_t credentialId) override;
89     ErrCode IsAllowedCreateAdmin(bool &isAllowedCreateAdmin) override;
90     ErrCode GetCreatedOsAccountNumFromDatabase(const std::string& storeID,
91         int &createdOsAccountNum) override;
92     ErrCode GetSerialNumberFromDatabase(const std::string& storeID, int64_t &serialNumber) override;
93     ErrCode GetMaxAllowCreateIdFromDatabase(const std::string& storeID, int &id) override;
94     ErrCode GetOsAccountFromDatabase(const std::string& storeID, const int id,
95         OsAccountInfo &osAccountInfo) override;
96     ErrCode GetOsAccountListFromDatabase(const std::string& storeID,
97         std::vector<OsAccountInfo> &osAccountList) override;
98     ErrCode QueryActiveOsAccountIds(std::vector<int32_t>& ids) override;
99     ErrCode QueryOsAccountConstraintSourceTypes(const int32_t id,
100         const std::string &constraint, std::vector<ConstraintSourceTypeInfo> &constraintSourceTypeInfos) override;
101     ErrCode SetBaseOsAccountConstraints(const int32_t id,
102         const std::vector<std::string> &constraints, const bool enable) override;
103     ErrCode SetGlobalOsAccountConstraints(const std::vector<std::string> &constraints,
104         const bool enable, const int32_t enforcerId, const bool isDeviceOwner) override;
105     ErrCode SetSpecificOsAccountConstraints(const std::vector<std::string> &constraints,
106         const bool enable, const int32_t targetId, const int32_t enforcerId, const bool isDeviceOwner) override;
107 
108     ErrCode SetDefaultActivatedOsAccount(const int32_t id) override;
109     ErrCode GetDefaultActivatedOsAccount(int32_t &id) override;
110     ErrCode IsOsAccountForeground(const int32_t localId, const uint64_t displayId, bool &isForeground) override;
111     ErrCode GetForegroundOsAccountLocalId(const uint64_t displayId, int32_t &localId) override;
112     ErrCode GetForegroundOsAccounts(std::vector<ForegroundOsAccount> &accounts) override;
113     ErrCode GetBackgroundOsAccountLocalIds(std::vector<int32_t> &localIds) override;
114     ErrCode SetOsAccountToBeRemoved(int32_t localId, bool toBeRemoved) override;
115     ErrCode SendMsgForAccountCreate(OsAccountInfo &osAccountInfo, const CreateOsAccountOptions &options = {});
116     ErrCode GetOsAccountInfoById(const int id, OsAccountInfo &osAccountInfo);
117     ErrCode GetTypeNumber(const OsAccountType& type, int32_t& typeNumber) override;
118     ErrCode CheckTypeNumber(const OsAccountType& type) override;
119     ErrCode ActivateDefaultOsAccount() override;
120 
121     int32_t CleanGarbageOsAccounts(int32_t excludeId = -1) override;
122     void ResetAccountStatus() override;
123     bool CheckAndCleanOsAccounts();
124     ErrCode GetRealOsAccountInfoById(const int id, OsAccountInfo &osAccountInfo);
125     void CleanGarbageOsAccountsAsync() override;
126 #ifdef SUPPORT_DOMAIN_ACCOUNTS
127     ErrCode BindDomainAccount(const OsAccountType &type, const DomainAccountInfo &domainAccountInfo,
128         OsAccountInfo &osAccountInfo, const CreateOsAccountForDomainOptions &options = {});
129     ErrCode UpdateAccountStatusForDomain(const int id, DomainAccountStatus status);
130     ErrCode UpdateAccountInfoByDomainAccountInfo(int32_t userId, const DomainAccountInfo &newDomainAccountInfo);
131     bool IsSameAccount(const DomainAccountInfo &domainInfoSrc, const DomainAccountInfo &domainInfoTar);
132     ErrCode CheckDomainAccountBound(const DomainAccountInfo &info, bool &isBound);
133 #endif // SUPPORT_DOMAIN_ACCOUNTS
134     ErrCode CreateOsAccountForDomain(const OsAccountType &type, const DomainAccountInfo &domainInfo,
135         const sptr<IDomainAccountCallback> &callback, const CreateOsAccountForDomainOptions &options = {}) override;
136     ErrCode GetOsAccountLocalIdFromDomain(const DomainAccountInfo &domainInfo, int &id) override;
137     ErrCode GetOsAccountDomainInfo(const int32_t localId, DomainAccountInfo &domainInfo) override;
138     ErrCode UpdateServerConfig(const std::string &configId, const DomainServerConfig &config);
139 #ifdef SUPPORT_LOCK_OS_ACCOUNT
140     ErrCode IsOsAccountLocking(const int id, bool &isLocking) override;
141     ErrCode PublishOsAccountLockEvent(const int32_t localId, bool isLocking) override;
142     ErrCode LockOsAccount(const int32_t localId) override;
143 #endif
144     void RemoveLocalIdToOperating(int32_t localId);
145     bool CheckAndAddLocalIdOperating(int32_t localId);
146     OsAccountControlFileManager &GetFileController();
147 private:
148     IInnerOsAccountManager();
149     ~IInnerOsAccountManager() = default;
150     DISALLOW_COPY_AND_MOVE(IInnerOsAccountManager);
151     void RestartActiveAccount();
152     void CreateBaseAdminAccount();
153     bool CreateBaseStandardAccount(OsAccountInfo &osAccountInfo);
154     ErrCode SendMsgForAccountActivateInBackground(OsAccountInfo &osAccountInfo);
155     ErrCode ActivateOsAccountInBackground(const int32_t id);
156     bool IsLoggedInAccountsOversize();
157     void ExecuteDeactivationAnimation(int32_t pipeFd, const OsAccountInfo &osAccountInfo);
158     ErrCode WaitForAnimationReady(int32_t pipeFd);
159     void LaunchDeactivationAnimation(const OsAccountInfo &osAccountInfo);
160     ErrCode PrepareRemoveOsAccount(OsAccountInfo &osAccountInfo, bool isCleanGarbage = false);
161     ErrCode RemoveOsAccountOperate(const int id, OsAccountInfo &osAccountInfo, bool isCleanGarbage = false);
162     ErrCode DeactivateOsAccountById(const int id);
163     ErrCode DeactivateOsAccountByInfo(OsAccountInfo &osAccountInfo);
164     ErrCode PrepareOsAccountInfo(const std::string &name, const OsAccountType &type,
165         const DomainAccountInfo &domainAccount, OsAccountInfo &osAccountInfo);
166     ErrCode PrepareOsAccountInfo(const std::string &localName, const std::string &shortName, const OsAccountType &type,
167         const DomainAccountInfo &domainAccount, OsAccountInfo &osAccountInfo);
168     ErrCode FillOsAccountInfo(const std::string &localName, const std::string &shortName, const OsAccountType &type,
169         const DomainAccountInfo &domainAccount, OsAccountInfo &osAccountInfo);
170     ErrCode PrepareOsAccountInfoWithFullInfo(OsAccountInfo &osAccountInfo);
171     ErrCode SendMsgForAccountActivate(OsAccountInfo &osAccountInfo, const bool startStorage = true,
172                                       const uint64_t dispalyId = 0, const bool isAppRecovery = false);
173     ErrCode SendToStorageAccountStart(OsAccountInfo &osAccountInfo);
174     ErrCode SendToAMSAccountStart(OsAccountInfo &osAccountInfo, const uint64_t dispalyId, const bool isAppRecovery);
175     ErrCode SendMsgForAccountDeactivate(OsAccountInfo &osAccountInfo, bool isStopStorage = true);
176     ErrCode SendMsgForAccountStop(OsAccountInfo &osAccountInfo);
177     ErrCode SendMsgForAccountRemove(OsAccountInfo &osAccountInfo);
178     ErrCode ValidateOsAccount(const OsAccountInfo &osAccountInfo);
179     ErrCode DealWithDeviceOwnerId(const bool isDeviceOwner, const int32_t localId);
180     void CheckAndRefreshLocalIdRecord(const int id);
181     void RollBackToEarlierAccount(int32_t fromId, int32_t toId);
182     void RollbackOsAccount(OsAccountInfo &osAccountInfo, bool needDelStorage, bool needDelBms);
183     bool IsToBeRemoved(const int32_t localId);
184     // operations for active list
185     void PushIdIntoActiveList(int32_t id);
186     void EraseIdFromActiveList(int32_t id);
187     bool IsOsAccountIDInActiveList(int32_t id);
188     void CopyFromActiveList(std::vector<int32_t>& idList);
189 #ifdef SUPPORT_DOMAIN_ACCOUNTS
190     ErrCode GetOsAccountsByDomainInfo(const DomainAccountInfo &info, std::vector<OsAccountInfo> &osAccountInfos);
191 #endif // SUPPORT_DOMAIN_ACCOUNTS
192     void RetryToGetAccount(OsAccountInfo &osAccountInfo);
193     ErrCode RetryToInsertOsAccount(OsAccountInfo &osAccountInfo);
194     bool JudgeOsAccountUpdate(cJSON *accountIndexJson);
195     std::shared_ptr<std::mutex> GetOrInsertUpdateLock(int32_t id);
196     ErrCode UpdateAccountToBackground(int32_t oldId);
197     ErrCode IsValidOsAccount(const OsAccountInfo &osAccountInfo);
198     ErrCode GetNonSACreatedOACount(unsigned int &nonSACreatedOACount) const;
199     std::vector<int32_t> GetVerifiedAccountIds(const SafeMap<int32_t, bool> &verifiedAccounts);
200     ErrCode SendToStorageAndAMSAccountStart(OsAccountInfo &osAccountInfo, const bool startStorage,
201         const uint64_t displayId, const bool isAppRecovery, int32_t oldId);
202 
203 private:
204     std::shared_ptr<IOsAccountControl> osAccountControl_;
205     std::vector<int32_t> activeAccountId_;
206     std::vector<int32_t> operatingId_;
207     IOsAccountSubscribe &subscribeManager_;
208     std::int32_t deviceOwnerId_ = -1;
209     std::int32_t defaultActivatedId_ = -1;
210     OsAccountConfig config_;
211     mutable std::mutex ativeMutex_;
212     mutable std::mutex operatingMutex_;
213     mutable std::mutex updateLockMutex_;
214     SafeMap<uint64_t, int32_t> foregroundAccountMap_;
215 #ifdef SUPPORT_LOCK_OS_ACCOUNT
216     OsAccountLockOsAccountPluginManager &lockOsAccountPluginManager_;
217 #endif
218     OsAccountActivateLockPluginManager &activateLockPluginManager_;
219     SafeMap<int32_t, bool> loggedInAccounts_;
220     SafeMap<int32_t, bool> verifiedAccounts_;
221     SafeMap<int32_t, bool> deactivatingAccounts_;
222 #ifdef SUPPORT_LOCK_OS_ACCOUNT
223     SafeMap<int32_t, bool> lockingAccounts_;
224 #endif
225     std::map<int32_t, std::shared_ptr<std::mutex>> updateLocks_;
226 
227 public:
228 #ifdef SUPPORT_DOMAIN_ACCOUNTS
229     std::mutex createOrBindDomainAccountMutex_;
230 #endif // SUPPORT_DOMAIN_ACCOUNTS
231 };
232 }  // namespace AccountSA
233 }  // namespace OHOS
234 
235 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OSACCOUNT_IINNER_OS_ACCOUNT_MANAGER_H
236