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