1 /*
2 * Copyright (c) 2022-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 #include "multiple_user_connector.h"
17
18 #include "dm_error_type.h"
19 #include "dm_log.h"
20 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
21 #include "account_info.h"
22 #include "ipc_skeleton.h"
23 #include "ohos_account_kits.h"
24 #ifdef OS_ACCOUNT_PART_EXISTS
25 #include "os_account_manager.h"
26 using namespace OHOS::AccountSA;
27 #endif // OS_ACCOUNT_PART_EXISTS
28 #endif
29
30 namespace OHOS {
31 namespace DistributedHardware {
32 int32_t MultipleUserConnector::oldUserId_ = -1;
33 std::string MultipleUserConnector::accountId_ = "";
34 std::string MultipleUserConnector::accountName_ = "";
35 std::mutex MultipleUserConnector::lock_;
36 std::map<int32_t, DMAccountInfo> MultipleUserConnector::dmAccountInfoMap_ = {};
37 std::mutex MultipleUserConnector::dmAccountInfoMaplock_;
38 #ifndef OS_ACCOUNT_PART_EXISTS
39 const int32_t DEFAULT_OS_ACCOUNT_ID = 0; // 0 is the default id when there is no os_account part
40 #endif // OS_ACCOUNT_PART_EXISTS
41
GetCurrentAccountUserID(void)42 int32_t MultipleUserConnector::GetCurrentAccountUserID(void)
43 {
44 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
45 return 0;
46 #elif OS_ACCOUNT_PART_EXISTS
47 std::vector<int> ids;
48 ErrCode ret = OsAccountManager::QueryActiveOsAccountIds(ids);
49 if (ret != 0 || ids.empty()) {
50 LOGE("GetCurrentAccountUserID error ret: %{public}d", ret);
51 return -1;
52 }
53 return ids[0];
54 #else // OS_ACCOUNT_PART_EXISTS
55 return DEFAULT_OS_ACCOUNT_ID;
56 #endif
57 }
58
GetOhosAccountId(void)59 std::string MultipleUserConnector::GetOhosAccountId(void)
60 {
61 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
62 return "";
63 #elif OS_ACCOUNT_PART_EXISTS
64 OhosAccountInfo accountInfo;
65 ErrCode ret = OhosAccountKits::GetInstance().GetOhosAccountInfo(accountInfo);
66 if (ret != 0 || accountInfo.uid_ == "") {
67 LOGE("GetOhosAccountId error ret: %{public}d", ret);
68 return "";
69 }
70 return accountInfo.uid_;
71 #else
72 return "";
73 #endif
74 }
75
GetOhosAccountIdByUserId(int32_t userId)76 std::string MultipleUserConnector::GetOhosAccountIdByUserId(int32_t userId)
77 {
78 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
79 (void)userId;
80 return "";
81 #elif OS_ACCOUNT_PART_EXISTS
82 OhosAccountInfo accountInfo;
83 ErrCode ret = OhosAccountKits::GetInstance().GetOsAccountDistributedInfo(userId, accountInfo);
84 if (ret != 0 || accountInfo.uid_ == "") {
85 LOGE("error ret: %{public}d", ret);
86 return "";
87 }
88 return accountInfo.uid_;
89 #else
90 (void)userId;
91 return "";
92 #endif
93 }
94
GetOhosAccountName(void)95 std::string MultipleUserConnector::GetOhosAccountName(void)
96 {
97 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
98 return "";
99 #elif OS_ACCOUNT_PART_EXISTS
100 auto accountInfo = OhosAccountKits::GetInstance().QueryOhosAccountInfo();
101 if (!accountInfo.first) {
102 LOGE("QueryOhosAccountInfo failed.");
103 return "";
104 }
105 if (accountInfo.second.name_.empty()) {
106 LOGE("QueryOhosAccountInfo name empty.");
107 return "";
108 }
109 return accountInfo.second.name_;
110 #else
111 return "";
112 #endif
113 }
114
GetTokenIdAndForegroundUserId(uint32_t & tokenId,int32_t & userId)115 void MultipleUserConnector::GetTokenIdAndForegroundUserId(uint32_t &tokenId, int32_t &userId)
116 {
117 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
118 tokenId = OHOS::IPCSkeleton::GetCallingTokenID();
119 #else
120 (void)tokenId;
121 #endif
122 userId = GetFirstForegroundUserId();
123 }
124
GetCallerUserId(int32_t & userId)125 void MultipleUserConnector::GetCallerUserId(int32_t &userId)
126 {
127 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
128 (void)userId;
129 return;
130 #elif OS_ACCOUNT_PART_EXISTS
131 int32_t uid = OHOS::IPCSkeleton::GetCallingUid();
132 ErrCode ret = OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId);
133 if (ret != 0) {
134 LOGE("GetOsAccountLocalIdFromUid error ret: %{public}d", ret);
135 }
136 return;
137 #else // OS_ACCOUNT_PART_EXISTS
138 (void)userId;
139 return;
140 #endif
141 }
142
SetSwitchOldUserId(int32_t userId)143 void MultipleUserConnector::SetSwitchOldUserId(int32_t userId)
144 {
145 std::lock_guard<std::mutex> lock(lock_);
146 oldUserId_ = userId;
147 }
148
GetSwitchOldUserId(void)149 int32_t MultipleUserConnector::GetSwitchOldUserId(void)
150 {
151 std::lock_guard<std::mutex> lock(lock_);
152 return oldUserId_;
153 }
154
SetSwitchOldAccountId(std::string accountId)155 void MultipleUserConnector::SetSwitchOldAccountId(std::string accountId)
156 {
157 std::lock_guard<std::mutex> lock(lock_);
158 accountId_ = accountId;
159 }
160
GetSwitchOldAccountId(void)161 std::string MultipleUserConnector::GetSwitchOldAccountId(void)
162 {
163 std::lock_guard<std::mutex> lock(lock_);
164 return accountId_;
165 }
166
SetSwitchOldAccountName(std::string accountName)167 void MultipleUserConnector::SetSwitchOldAccountName(std::string accountName)
168 {
169 std::lock_guard<std::mutex> lock(lock_);
170 accountName_ = accountName;
171 }
172
GetSwitchOldAccountName(void)173 std::string MultipleUserConnector::GetSwitchOldAccountName(void)
174 {
175 std::lock_guard<std::mutex> lock(lock_);
176 return accountName_;
177 }
178
SetAccountInfo(int32_t userId,DMAccountInfo dmAccountInfo)179 void MultipleUserConnector::SetAccountInfo(int32_t userId, DMAccountInfo dmAccountInfo)
180 {
181 std::lock_guard<std::mutex> lock(dmAccountInfoMaplock_);
182 dmAccountInfoMap_[userId] = dmAccountInfo;
183 }
184
GetAccountInfoByUserId(int32_t userId)185 DMAccountInfo MultipleUserConnector::GetAccountInfoByUserId(int32_t userId)
186 {
187 DMAccountInfo dmAccountInfo;
188 {
189 std::lock_guard<std::mutex> lock(dmAccountInfoMaplock_);
190 if (dmAccountInfoMap_.find(userId) != dmAccountInfoMap_.end()) {
191 dmAccountInfo = dmAccountInfoMap_[userId];
192 return dmAccountInfo;
193 }
194 }
195 LOGE("userId is not exist.");
196 return dmAccountInfo;
197 }
198
DeleteAccountInfoByUserId(int32_t userId)199 void MultipleUserConnector::DeleteAccountInfoByUserId(int32_t userId)
200 {
201 std::lock_guard<std::mutex> lock(dmAccountInfoMaplock_);
202 LOGI("userId: %{public}d", userId);
203 if (dmAccountInfoMap_.find(userId) != dmAccountInfoMap_.end()) {
204 dmAccountInfoMap_.erase(userId);
205 }
206 }
207
GetForegroundUserIds(std::vector<int32_t> & userVec)208 int32_t MultipleUserConnector::GetForegroundUserIds(std::vector<int32_t> &userVec)
209 {
210 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
211 userVec.push_back(DEFAULT_OS_ACCOUNT_ID);
212 return DM_OK;
213 #elif OS_ACCOUNT_PART_EXISTS
214 userVec.clear();
215 std::vector<AccountSA::ForegroundOsAccount> accounts;
216 ErrCode ret = OsAccountManager::GetForegroundOsAccounts(accounts);
217 if (ret != 0 || accounts.empty()) {
218 LOGE("error ret: %{public}d", ret);
219 return ret;
220 }
221 for (auto &account : accounts) {
222 userVec.push_back(account.localId);
223 }
224 return DM_OK;
225 #else // OS_ACCOUNT_PART_EXISTS
226 userVec.push_back(DEFAULT_OS_ACCOUNT_ID);
227 return DM_OK;
228 #endif
229 }
230
GetFirstForegroundUserId(void)231 int32_t MultipleUserConnector::GetFirstForegroundUserId(void)
232 {
233 std::vector<int32_t> userVec;
234 int32_t ret = GetForegroundUserIds(userVec);
235 if (ret != DM_OK || userVec.size() == 0) {
236 LOGE("get userid error ret: %{public}d.", ret);
237 return -1;
238 }
239 return userVec[0];
240 }
241
GetBackgroundUserIds(std::vector<int32_t> & userIdVec)242 int32_t MultipleUserConnector::GetBackgroundUserIds(std::vector<int32_t> &userIdVec)
243 {
244 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
245 return DM_OK;
246 #elif OS_ACCOUNT_PART_EXISTS
247 userIdVec.clear();
248 std::vector<OsAccountInfo> allOsAccounts;
249 ErrCode ret = OsAccountManager::QueryAllCreatedOsAccounts(allOsAccounts);
250 if (ret != 0) {
251 LOGE("Get all created accounts error, ret: %{public}d", ret);
252 return ret;
253 }
254
255 std::vector<AccountSA::ForegroundOsAccount> foregroundAccounts;
256 ret = OsAccountManager::GetForegroundOsAccounts(foregroundAccounts);
257 if (ret != 0) {
258 LOGE("Get foreground accounts error ret: %{public}d", ret);
259 return ret;
260 }
261
262 std::vector<int32_t> allUserIds;
263 std::vector<int32_t> foregroundUserIds;
264 for (const auto &u : allOsAccounts) {
265 allUserIds.push_back(u.GetLocalId());
266 }
267 for (const auto &u : foregroundAccounts) {
268 foregroundUserIds.push_back(u.localId);
269 }
270
271 for (const auto &userId : allUserIds) {
272 if (std::find(foregroundUserIds.begin(), foregroundUserIds.end(), userId) == foregroundUserIds.end()) {
273 userIdVec.push_back(userId);
274 }
275 }
276 return DM_OK;
277 #else
278 return DM_OK;
279 #endif
280 }
281
GetAllUserIds(std::vector<int32_t> & userIdVec)282 int32_t MultipleUserConnector::GetAllUserIds(std::vector<int32_t> &userIdVec)
283 {
284 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
285 return DM_OK;
286 #elif OS_ACCOUNT_PART_EXISTS
287 userIdVec.clear();
288 std::vector<OsAccountInfo> allOsAccounts;
289 ErrCode ret = OsAccountManager::QueryAllCreatedOsAccounts(allOsAccounts);
290 if (ret != 0) {
291 LOGE("Get all created accounts error, ret: %{public}d", ret);
292 return ret;
293 }
294
295 for (const auto &u : allOsAccounts) {
296 userIdVec.push_back(u.GetLocalId());
297 }
298 return DM_OK;
299 #else
300 return DM_OK;
301 #endif
302 }
303
GetAccountNickName(int32_t userId)304 std::string MultipleUserConnector::GetAccountNickName(int32_t userId)
305 {
306 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
307 return "";
308 #elif OS_ACCOUNT_PART_EXISTS
309 OhosAccountInfo accountInfo;
310 ErrCode ret = OhosAccountKits::GetInstance().GetOsAccountDistributedInfo(userId, accountInfo);
311 if (ret != 0 || accountInfo.uid_ == "") {
312 LOGE("error ret: %{public}d", ret);
313 return "";
314 }
315 return accountInfo.nickname_;
316 #else
317 return "";
318 #endif
319 }
320
IsUserUnlocked(int32_t userId)321 bool MultipleUserConnector::IsUserUnlocked(int32_t userId)
322 {
323 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
324 return true;
325 #elif OS_ACCOUNT_PART_EXISTS
326 bool isUserUnlocked = false;
327 ErrCode ret = OsAccountManager::IsOsAccountVerified(userId, isUserUnlocked);
328 if (ret != 0) {
329 LOGE("IsUserUnlocked error ret: %{public}d", ret);
330 return false;
331 }
332 return isUserUnlocked;
333 #else
334 return true;
335 #endif
336 }
337
ClearLockedUser(std::vector<int32_t> & foregroundUserVec)338 void MultipleUserConnector::ClearLockedUser(std::vector<int32_t> &foregroundUserVec)
339 {
340 for (auto iter = foregroundUserVec.begin(); iter != foregroundUserVec.end();) {
341 if (!IsUserUnlocked(*iter)) {
342 iter = foregroundUserVec.erase(iter);
343 } else {
344 ++iter;
345 }
346 }
347 }
348
ClearLockedUser(std::vector<int32_t> & foregroundUserVec,std::vector<int32_t> & backgroundUserVec)349 void MultipleUserConnector::ClearLockedUser(std::vector<int32_t> &foregroundUserVec,
350 std::vector<int32_t> &backgroundUserVec)
351 {
352 for (auto iter = foregroundUserVec.begin(); iter != foregroundUserVec.end();) {
353 if (!IsUserUnlocked(*iter)) {
354 backgroundUserVec.push_back(*iter);
355 iter = foregroundUserVec.erase(iter);
356 } else {
357 ++iter;
358 }
359 }
360 }
361
GetCurrentDMAccountInfo()362 DMAccountInfo MultipleUserConnector::GetCurrentDMAccountInfo()
363 {
364 DMAccountInfo dmAccountInfo;
365 dmAccountInfo.accountId = GetOhosAccountId();
366 dmAccountInfo.accountName = GetOhosAccountName();
367 return dmAccountInfo;
368 }
369 } // namespace DistributedHardware
370 } // namespace OHOS
371