1 /* 2 * Copyright (c) 2021-2023 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 "ios_account_control.h" 22 #include "ios_account_subscribe.h" 23 #include "os_account_interface.h" 24 #include "singleton.h" 25 26 namespace OHOS { 27 namespace AccountSA { 28 class IInnerOsAccountManager : public IInnerOsAccount, public DelayedSingleton<IInnerOsAccountManager> { 29 public: 30 IInnerOsAccountManager(); 31 virtual ~IInnerOsAccountManager() = default; 32 void Init() override; 33 ErrCode CreateOsAccount( 34 const std::string &name, const OsAccountType &type, OsAccountInfo &osAccountInfo) override; 35 ErrCode CreateOsAccountForDomain( 36 const OsAccountType &type, const DomainAccountInfo &domainInfo, OsAccountInfo &osAccountInfo) override; 37 ErrCode RemoveOsAccount(const int id) override; 38 ErrCode IsOsAccountExists(const int id, bool &isOsAccountExits) override; 39 ErrCode IsOsAccountActived(const int id, bool &isOsAccountActived) override; 40 ErrCode IsOsAccountConstraintEnable( 41 const int id, const std::string &constraint, bool &isOsAccountConstraintEnable) override; 42 ErrCode IsOsAccountVerified(const int id, bool &isVerified) override; 43 ErrCode GetCreatedOsAccountsCount(unsigned int &createdOsAccountCount) override; 44 ErrCode QueryMaxOsAccountNumber(int &maxOsAccountNumber) override; 45 ErrCode GetOsAccountAllConstraints(const int id, std::vector<std::string> &constraints) override; 46 ErrCode QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> &osAccountInfos) override; 47 ErrCode QueryOsAccountById(const int id, OsAccountInfo &osAccountInfo) override; 48 ErrCode GetOsAccountType(const int id, OsAccountType &type) override; 49 ErrCode GetOsAccountProfilePhoto(const int id, std::string &photo) override; 50 ErrCode IsMultiOsAccountEnable(bool &isMultiOsAccountEnable) override; 51 ErrCode SetOsAccountName(const int id, const std::string &name) override; 52 ErrCode SetOsAccountConstraints( 53 const int id, const std::vector<std::string> &constraints, const bool enable) override; 54 ErrCode SetOsAccountProfilePhoto(const int id, const std::string &photo) override; 55 ErrCode ActivateOsAccount(const int id) override; 56 ErrCode StartOsAccount(const int id) override; 57 ErrCode StopOsAccount(const int id) override; 58 ErrCode GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber, int &id) override; 59 ErrCode GetSerialNumberByOsAccountLocalId(const int &id, int64_t &serialNumber) override; 60 ErrCode SubscribeOsAccount( 61 const OsAccountSubscribeInfo &subscribeInfo, const sptr<IRemoteObject> &eventListener) override; 62 ErrCode UnsubscribeOsAccount(const sptr<IRemoteObject> &eventListener) override; 63 OS_ACCOUNT_SWITCH_MOD GetOsAccountSwitchMod() override; 64 ErrCode IsOsAccountCompleted(const int id, bool &isOsAccountCompleted) override; 65 ErrCode SetOsAccountIsVerified(const int id, const bool isVerified) override; 66 ErrCode IsAllowedCreateAdmin(bool &isAllowedCreateAdmin) override; 67 ErrCode GetOsAccountLocalIdFromDomain(const DomainAccountInfo &domainInfo, int &id) override; 68 ErrCode GetCreatedOsAccountNumFromDatabase(const std::string& storeID, 69 int &createdOsAccountNum) override; 70 ErrCode GetSerialNumberFromDatabase(const std::string& storeID, int64_t &serialNumber) override; 71 ErrCode GetMaxAllowCreateIdFromDatabase(const std::string& storeID, int &id) override; 72 ErrCode GetOsAccountFromDatabase(const std::string& storeID, const int id, 73 OsAccountInfo &osAccountInfo) override; 74 ErrCode GetOsAccountListFromDatabase(const std::string& storeID, 75 std::vector<OsAccountInfo> &osAccountList) override; 76 ErrCode QueryActiveOsAccountIds(std::vector<int32_t>& ids) override; 77 ErrCode QueryOsAccountConstraintSourceTypes(const int32_t id, 78 const std::string &constraint, std::vector<ConstraintSourceTypeInfo> &constraintSourceTypeInfos) override; 79 ErrCode SetBaseOsAccountConstraints(const int32_t id, 80 const std::vector<std::string> &constraints, const bool enable) override; 81 ErrCode SetGlobalOsAccountConstraints(const std::vector<std::string> &constraints, 82 const bool enable, const int32_t enforcerId, const bool isDeviceOwner) override; 83 ErrCode SetSpecificOsAccountConstraints(const std::vector<std::string> &constraints, 84 const bool enable, const int32_t targetId, const int32_t enforcerId, const bool isDeviceOwner) override; 85 86 private: 87 void SetOsAccountControl(std::shared_ptr<IOsAccountControl> ptr); 88 void StartAccount(); 89 void RestartActiveAccount(); 90 void CreateBaseAdminAccount(); 91 void CreateBaseStandardAccount(); 92 void CreateBaseStandardAccountSendToOther(); 93 void StartBaseStandardAccount(OsAccountInfo &osAccountInfo); 94 void ResetAccountStatus(void); 95 void StartActivatedAccount(int32_t id); 96 ErrCode DeActivateOsAccount(const int id); 97 ErrCode GetEventHandler(void); 98 ErrCode PrepareOsAccountInfo(const std::string &name, const OsAccountType &type, 99 const DomainAccountInfo &domainAccount, OsAccountInfo &osAccountInfo); 100 ErrCode SendMsgForAccountCreate(OsAccountInfo &osAccountInfo); 101 ErrCode SendMsgForAccountActivate(OsAccountInfo &osAccountInfo); 102 ErrCode SendMsgForAccountStop(OsAccountInfo &osAccountInfo); 103 ErrCode SendMsgForAccountRemove(OsAccountInfo &osAccountInfo); 104 void AddLocalIdToOperating(int32_t localId); 105 void RemoveLocalIdToOperating(int32_t localId); 106 bool IsLocalIdInOperating(int32_t localId); 107 void CleanGarbageAccounts(); 108 ErrCode DealWithDeviceOwnerId(const bool isDeviceOwner, const int32_t localId); 109 110 // operations for active list 111 void PushIdIntoActiveList(int32_t id); 112 void EraseIdFromActiveList(int32_t id); 113 bool IsOsAccountIDInActiveList(int32_t id); 114 void CopyFromActiveList(std::vector<int32_t>& idList); 115 void RefreshActiveList(int32_t newId); 116 117 private: 118 std::shared_ptr<IOsAccountControl> osAccountControl_; 119 std::vector<int32_t> activeAccountId_; 120 std::vector<int32_t> operatingId_; 121 std::shared_ptr<IOsAccountSubscribe> subscribeManagerPtr_; 122 std::int32_t counterForStandard_; 123 std::int32_t counterForStandardCreate_; 124 std::int32_t counterForAccountStart_; 125 std::int32_t deviceOwnerId_; 126 bool isSendToStorageCreate_; 127 bool isSendToStorageStart_; 128 std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_; 129 static constexpr std::int32_t DELAY_FOR_FOUNDATION_SERVICE = 2 * 1000; // 2s 130 static constexpr std::int32_t DELAY_FOR_TIME_INTERVAL = 100; // 0.1s 131 static constexpr std::int32_t MAX_TRY_TIMES = 1000; 132 mutable std::mutex ativeMutex_; 133 mutable std::mutex operatingMutex_; 134 }; 135 } // namespace AccountSA 136 } // namespace OHOS 137 138 #endif // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OSACCOUNT_IINNER_OS_ACCOUNT_MANAGER_H 139