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