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