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