1 /* 2 * Copyright (c) 2021-2025 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_ACCOUNT_MGR_SERVICE_H 17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_MGR_SERVICE_H 18 19 #include <memory> 20 #include <mutex> 21 22 #include "account_dump_helper.h" 23 #include "account_event_provider.h" 24 #include "account_info.h" 25 #include "account_stub.h" 26 #include "iaccount.h" 27 #include "iaccount_context.h" 28 #include "iremote_object.h" 29 #include "ohos_account_manager.h" 30 #include "os_account_manager_service.h" 31 #include "singleton.h" 32 #include "system_ability.h" 33 34 namespace OHOS { 35 namespace AccountSA { 36 enum ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 37 38 class AccountMgrService : public SystemAbility, 39 public AccountStub, 40 public IAccountContext, 41 public OHOS::DelayedRefSingleton<AccountMgrService> { 42 public: 43 AccountMgrService(); 44 ~AccountMgrService() override; 45 DISALLOW_COPY_AND_MOVE(AccountMgrService); 46 DECLARE_SYSTEM_ABILITY(AccountMgrService); 47 ErrCode UpdateOhosAccountInfo( 48 const std::string &accountName, const std::string &uid, const std::string &eventStr) override; 49 ErrCode SetOhosAccountInfo(const OhosAccountInfo &ohosAccountInfo, 50 const std::string &eventStr) override; 51 ErrCode SetOsAccountDistributedInfo( 52 int32_t localId, const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr) override; 53 ErrCode QueryOhosAccountInfo(std::string& accountName, std::string& uid, int32_t& status) override; 54 ErrCode QueryDistributedVirtualDeviceId(std::string &dvid) override; 55 ErrCode QueryDistributedVirtualDeviceId(const std::string &bundleName, int32_t localId, std::string &dvid) override; 56 ErrCode QueryOsAccountDistributedInfo( 57 int32_t localId, std::string& accountName, std::string& uid, int32_t& status) override; 58 ErrCode GetOhosAccountInfo(OhosAccountInfo &info) override; 59 ErrCode GetOsAccountDistributedInfo(int32_t localId, OhosAccountInfo &info) override; 60 ErrCode QueryDeviceAccountId(std::int32_t &accountId) override; 61 ErrCode SubscribeDistributedAccountEvent(int32_t typeInt, const sptr<IRemoteObject>& eventListener) override; 62 ErrCode UnsubscribeDistributedAccountEvent(int32_t typeInt, const sptr<IRemoteObject>& eventListener) override; 63 ErrCode GetAppAccountService(sptr<IRemoteObject>& funcResult) override; 64 ErrCode GetOsAccountService(sptr<IRemoteObject>& funcResult) override; 65 ErrCode GetAccountIAMService(sptr<IRemoteObject>& funcResult) override; 66 ErrCode GetDomainAccountService(sptr<IRemoteObject>& funcResult) override; 67 68 void OnStart() override; 69 void OnStop() override; 70 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 71 bool IsServiceStarted(void) const override; GetInstance()72 static AccountMgrService &GetInstance() 73 { 74 return DelayedRefSingleton<AccountMgrService>::GetInstance(); 75 } 76 ErrCode Dump(std::int32_t fd, const std::vector<std::u16string> &args) override; 77 void HandleNotificationEvents(const std::string &eventStr) override; 78 std::int32_t GetCallingUserID(); 79 80 int32_t CallbackEnter([[maybe_unused]] uint32_t code) override; 81 int32_t CallbackExit([[maybe_unused]] uint32_t code, [[maybe_unused]] int32_t result) override; 82 83 private: 84 bool Init(); 85 void SelfClean(); 86 std::int32_t GetDeviceAccountIdFromCurrentProcess(); 87 bool CreateOsAccountService(); 88 bool CreateAppAccountService(); 89 bool CreateIAMService(); 90 bool CreateDomainService(); 91 bool IsDefaultOsAccountVerified(); 92 void GetUncreatedInitAccounts(std::set<int32_t> &initAccounts); 93 ErrCode GetOsAccountDistributedInfoInner(int32_t localId, OhosAccountInfo &info); 94 bool HasAccountRequestPermission(const std::string &permissionName); 95 int32_t CheckUserIdValid(int32_t userId); 96 #ifdef HAS_APP_ACCOUNT_PART 97 void MoveAppAccountData(); 98 #endif 99 ErrCode InnerQueryOsAccountDistributedInfo( 100 int32_t localId, std::string& accountName, std::string& uid, int32_t& status); 101 102 bool registerToService_ = false; 103 ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START; 104 std::unique_ptr<AccountDumpHelper> dumpHelper_{}; 105 106 std::mutex serviceMutex_; 107 wptr<IRemoteObject> appAccountManagerService_ = nullptr; 108 wptr<OsAccountManagerService> osAccountManagerService_ = nullptr; 109 wptr<IRemoteObject> accountIAMService_ = nullptr; 110 wptr<IRemoteObject> domainAccountMgrService_ = nullptr; 111 112 std::mutex statusMutex_; 113 bool isStorageReady_ = false; 114 bool isAmsReady_ = false; 115 bool isBmsReady_ = false; 116 bool isDefaultOsAccountActivated_ = false; 117 }; 118 } // namespace AccountSA 119 } // namespace OHOS 120 121 #endif // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_MGR_SERVICE_H 122