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