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 QueryDistributedVirtualDeviceId(std::string &dvid) override;
52     ErrCode QueryOhosAccountInfoByUserId(std::int32_t userId, OhosAccountInfo &accountInfo) override;
53     ErrCode GetOhosAccountInfo(OhosAccountInfo &info) override;
54     ErrCode GetOhosAccountInfoByUserId(int32_t userId, OhosAccountInfo &info) override;
55     std::int32_t QueryDeviceAccountId(std::int32_t &accountId) override;
56     ErrCode SubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
57         const sptr<IRemoteObject> &eventListener) override;
58     ErrCode UnsubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
59         const sptr<IRemoteObject> &eventListener) override;
60     sptr<IRemoteObject> GetAppAccountService() override;
61     sptr<IRemoteObject> GetOsAccountService() override;
62     sptr<IRemoteObject> GetAccountIAMService() override;
63     sptr<IRemoteObject> GetDomainAccountService() override;
64 
65     void OnStart() override;
66     void OnStop() override;
67     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
68     bool IsServiceStarted(void) const override;
GetInstance()69     static AccountMgrService &GetInstance()
70     {
71         return DelayedRefSingleton<AccountMgrService>::GetInstance();
72     }
73     std::int32_t Dump(std::int32_t fd, const std::vector<std::u16string> &args) override;
74     void HandleNotificationEvents(const std::string &eventStr) override;
75     std::int32_t GetCallingUserID();
76 
77 private:
78     bool Init();
79     void SelfClean();
80     std::int32_t GetDeviceAccountIdFromCurrentProcess();
81     bool CreateOsAccountService();
82     bool CreateAppAccountService();
83     bool CreateIAMService();
84     bool CreateDomainService();
85 #ifdef HAS_APP_ACCOUNT_PART
86     void MoveAppAccountData();
87 #endif
88 
89     bool registerToService_ = false;
90     ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START;
91     std::unique_ptr<AccountDumpHelper> dumpHelper_{};
92 
93     sptr<IRemoteObject> appAccountManagerService_ = nullptr;
94     sptr<OsAccountManagerService> osAccountManagerService_ = nullptr;
95     sptr<IRemoteObject> accountIAMService_ = nullptr;
96     sptr<IRemoteObject> domainAccountMgrService_ = nullptr;
97 
98     std::mutex statusMutex_;
99     bool isStorageReady_ = false;
100     bool isAmsReady_ = false;
101     bool isBmsReady_ = false;
102     bool isDefaultOsAccountActivated_ = false;
103 };
104 }  // namespace AccountSA
105 }  // namespace OHOS
106 
107 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_MGR_SERVICE_H
108