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_OHOS_ACCOUNT_MANAGER_H 17#define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OHOS_ACCOUNT_MANAGER_H 18 19#include <map> 20#include <mutex> 21#include <string> 22#ifdef HAS_CES_PART 23#include "account_event_subscribe.h" 24#endif // HAS_CES_PART 25#include "account_info.h" 26#include "account_state_machine.h" 27#include "idistributed_account_subscribe.h" 28#include "iinner_os_account_manager.h" 29#include "ohos_account_data_deal.h" 30 31namespace OHOS { 32namespace AccountSA { 33#ifndef ACCOUNT_TEST 34const std::string ACCOUNT_CFG_DIR_ROOT_PATH = "/data/service/el1/public/account/"; 35#else 36const std::string ACCOUNT_CFG_DIR_ROOT_PATH = "/data/service/el1/public/account/test/"; 37#endif // ACCOUNT_TEST 38 39 40class OhosAccountManager; 41using OhosAccountEventFunc = std::function<ErrCode(const std::int32_t, const OhosAccountInfo &, const std::string &)>; 42/** 43 * Ohos account manager 44 */ 45class OhosAccountManager { 46public: 47 static OhosAccountManager &GetInstance(); 48 /** 49 * Get current ohos account information. 50 * 51 * @return current account information. 52 */ 53 AccountInfo GetCurrentOhosAccountInfo(); 54 55 /** 56 * Get ohos account information by local userId. 57 * 58 * @param userId target local account id 59 * @return ohos account info which is bound to the local userId. 60 */ 61 ErrCode GetAccountInfoByUserId(std::int32_t userId, AccountInfo &info); 62 63 /** 64 * Subscribe distributed account event by type. 65 * 66 * @param type event type 67 * @param eventListener event listener 68 * @return subscribe resule. 69 */ 70 ErrCode SubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type, 71 const sptr<IRemoteObject> &eventListener); 72 73 /** 74 * Unsubscribe distributed account event by type. 75 * 76 * @param type event type 77 * @param eventListener event listener 78 * @return unsubscribe resule. 79 */ 80 ErrCode UnsubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type, 81 const sptr<IRemoteObject> &eventListener); 82 83 /** 84 * Get current account state. 85 * 86 * @return current account state id. 87 */ 88 int GetCurrentOhosAccountState(); 89 90 /** 91 * Init ohos account manager. 92 * 93 */ 94 bool OnInitialize(); 95 96 /** 97 * Process an account event. 98 * 99 * @param curOhosAccount current ohos account info 100 * @param eventStr ohos account state change event 101 * @return true if the processing was completed, otherwise false 102 */ 103 bool HandleEvent(AccountInfo &curOhosAccount, const std::string &eventStr); 104 105 /** 106 * login ohos (for distributed network) account. 107 * 108 * @param userId target local account id. 109 * @param ohosAccountInfo ohos account information 110 * @param eventStr ohos account state change event 111 * @return true if the processing was completed, otherwise false 112 */ 113 ErrCode LoginOhosAccount(const int32_t userId, const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr); 114 115 /** 116 * logout ohos (for distributed network) account. 117 * 118 * @param userId target local account id. 119 * @param ohosAccountInfo ohos account information 120 * @param eventStr ohos account state change event 121 * @return true if the processing was completed, otherwise false 122 */ 123 ErrCode LogoutOhosAccount(const int32_t userId, const OhosAccountInfo &ohosAccountInfo, 124 const std::string &eventStr); 125 126 /** 127 * logoff ohos (for distributed network) account. 128 * 129 * @param userId target local account id. 130 * @param ohosAccountInfo ohos account information 131 * @param eventStr ohos account state change event 132 * @return true if the processing was completed, otherwise false 133 */ 134 ErrCode LogoffOhosAccount(const int32_t userId, const OhosAccountInfo &ohosAccountInfo, 135 const std::string &eventStr); 136 137 /** 138 * Handle token_invalid event of ohos (for distributed network) account . 139 * 140 * @param userId target local account id. 141 * @param ohosAccountInfo ohos account information 142 * @param eventStr ohos account state change event 143 * @return true if the processing was completed, otherwise false 144 */ 145 ErrCode HandleOhosAccountTokenInvalidEvent( 146 const int32_t userId, const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr); 147 148 /** 149 * Handle device account switch event. 150 * 151 * @param None 152 * @return None 153 */ 154 void HandleDevAccountSwitchEvent(); 155 156 /** 157 * Ohos account state change. 158 * 159 * @param name ohos account name 160 * @param uid ohos account uid 161 * @param eventStr ohos account state change event 162 * @return true if the processing was completed, otherwise false 163 */ 164 ErrCode OhosAccountStateChange(const std::string &name, const std::string &uid, const std::string &eventStr); 165 166 /** 167 * Ohos account state change. 168 * 169 * @param ohosAccountInfo ohos account information 170 * @param eventStr ohos account state change event 171 * @return true if the processing was completed, otherwise false 172 */ 173 ErrCode OhosAccountStateChange( 174 const int32_t userId, const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr); 175 176private: 177 bool isInit_ = false; 178 OhosAccountManager(); 179 DISALLOW_COPY_AND_MOVE(OhosAccountManager); 180 IDistributedAccountSubscribe &subscribeManager_; 181 182 /** 183 * Account state machine. 184 */ 185 std::unique_ptr<AccountStateMachine> accountState_{}; 186 187 /** 188 * Deal with file storage. 189 */ 190 std::unique_ptr<OhosAccountDataDeal> dataDealer_{}; 191 192 /** 193 * event mapper. 194 */ 195 std::map<std::string, ACCOUNT_INNER_EVENT_TYPE> eventMap_; 196 197 /** 198 * mutex lock for synchronization. 199 */ 200 std::mutex mgrMutex_; 201 std::mutex initMutex_; 202 203 /** 204 * build event mapper. 205 */ 206 void BuildEventsMapper(); 207 208 /** 209 * Config current account config. 210 * 211 * @param ohosAccountInfo target ohos account information. 212 * @return true if success. 213 */ 214 bool SaveOhosAccountInfo(AccountInfo &ohosAccountInfo) const; 215 216 /** 217 * Clear current account config. 218 * @param curOhosAccountInfo current ohos account info. 219 * @param clrStatus account status. 220 */ 221 bool ClearOhosAccount(AccountInfo &curOhosAccountInfo, std::int32_t clrStatus = ACCOUNT_STATE_UNBOUND) const; 222 223 /** 224 * Check whether the ohos account can be bound to the current user or not 225 * @return true if can. 226 */ 227 bool CheckOhosAccountCanBind(const AccountInfo &currAccountInfo, 228 const OhosAccountInfo &newOhosAccountInfo, const std::string &newOhosUid) const; 229 230 /** 231 * Get current ohos account info and check whether input information match or not 232 * @return true if matches. 233 */ 234 bool GetCurOhosAccountAndCheckMatch(AccountInfo &curOhosAccountInfo, 235 const std::string &inputName, 236 const std::string &inputUid, 237 const std::int32_t callingUserId) const; 238 239 /** 240 * event function map 241 */ 242 std::map<std::string, OhosAccountEventFunc> eventFuncMap_; 243#ifdef HAS_CES_PART 244 void OnPackageRemoved(const std::int32_t callingUid); 245 bool CreateCommonEventSubscribe(); 246 std::shared_ptr<AccountEventSubscriber> accountEventSubscribe_{}; 247#endif // HAS_CES_PART 248}; 249} // namespace AccountSA 250} // namespace OHOS 251 252#endif // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OHOS_ACCOUNT_MANAGER_H 253