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