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_ACCOUNT_MGR_SERVICE_H
17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_MGR_SERVICE_H
18 
19 #include <memory>
20 #include <mutex>
21 #include "account_dump_helper.h"
22 #include "account_event_provider.h"
23 #include "account_info.h"
24 #include "account_stub.h"
25 #include "os_account_manager_service.h"
26 #include "iaccount.h"
27 #include "iremote_object.h"
28 #include "ohos_account_manager.h"
29 #include "singleton.h"
30 #include "system_ability.h"
31 
32 namespace OHOS {
33 namespace AccountSA {
34 enum ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
35 
36 class AccountMgrService : public SystemAbility,
37                           public AccountStub,
38                           public OHOS::DelayedRefSingleton<AccountMgrService> {
39 public:
40     AccountMgrService();
41     ~AccountMgrService() override;
42     DISALLOW_COPY_AND_MOVE(AccountMgrService);
43     DECLARE_SYSTEM_ABILITY(AccountMgrService);
44     bool UpdateOhosAccountInfo(
45         const std::string &accountName, const std::string &uid, const std::string &eventStr) override;
46     std::int32_t SetOhosAccountInfo(const OhosAccountInfo &ohosAccountInfo,
47         const std::string &eventStr) override;
48     std::int32_t SetOhosAccountInfoByUserId(
49         const int32_t userId, const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr) override;
50     ErrCode QueryOhosAccountInfo(OhosAccountInfo &accountInfo) override;
51     ErrCode QueryOhosAccountInfoByUserId(std::int32_t userId, OhosAccountInfo &accountInfo) override;
52     ErrCode GetOhosAccountInfo(OhosAccountInfo &info) override;
53     ErrCode GetOhosAccountInfoByUserId(int32_t userId, OhosAccountInfo &info) override;
54     std::int32_t QueryDeviceAccountId(std::int32_t &accountId) override;
55     ErrCode SubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
56         const sptr<IRemoteObject> &eventListener) override;
57     ErrCode UnsubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
58         const sptr<IRemoteObject> &eventListener) override;
59     sptr<IRemoteObject> GetAppAccountService() override;
60     sptr<IRemoteObject> GetOsAccountService() override;
61     sptr<IRemoteObject> GetAccountIAMService() override;
62     sptr<IRemoteObject> GetDomainAccountService() override;
63 
64     void OnStart() override;
65     void OnStop() override;
66     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
67     bool IsServiceStarted(void) const override;
GetInstance()68     static AccountMgrService &GetInstance()
69     {
70         return DelayedRefSingleton<AccountMgrService>::GetInstance();
71     }
72     std::int32_t Dump(std::int32_t fd, const std::vector<std::u16string> &args) override;
73     void HandleNotificationEvents(const std::string &eventStr) override;
74     std::int32_t GetCallingUserID();
75 
76 private:
77     bool Init();
78     void SelfClean();
79     std::int32_t GetDeviceAccountIdFromCurrentProcess();
80     bool CreateOsAccountService();
81     bool CreateAppAccountService();
82     bool CreateIAMService();
83     bool CreateDomainService();
84 #ifdef HAS_APP_ACCOUNT_PART
85     void MoveAppAccountData();
86 #endif
87 
88     bool registerToService_ = false;
89     ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START;
90     std::unique_ptr<AccountDumpHelper> dumpHelper_{};
91 
92     sptr<IRemoteObject> appAccountManagerService_ = nullptr;
93     sptr<OsAccountManagerService> osAccountManagerService_ = nullptr;
94     sptr<IRemoteObject> accountIAMService_ = nullptr;
95     sptr<IRemoteObject> domainAccountMgrService_ = nullptr;
96 
97     std::mutex statusMutex_;
98     bool isStorageReady_ = false;
99     bool isAmsReady_ = false;
100     bool isBmsReady_ = false;
101     bool isDefaultOsAccountActivated_ = false;
102 };
103 }  // namespace AccountSA
104 }  // namespace OHOS
105 
106 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_MGR_SERVICE_H
107