• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "multi_user_manager.h"
17 
18 #include "device_profile_manager.h"
19 #include "distributed_device_profile_constants.h"
20 #include "distributed_device_profile_errors.h"
21 #include "distributed_device_profile_log.h"
22 #include "profile_utils.h"
23 
24 #include "account_info.h"
25 #include "ohos_account_kits.h"
26 #include "os_account_manager.h"
27 
28 namespace OHOS {
29 namespace DistributedDeviceProfile {
30 IMPLEMENT_SINGLE_INSTANCE(MultiUserManager);
31 
32 namespace {
33     const std::string TAG = "MultiUserManager";
34     const std::string OHOS_ANONYMOUS_UID = "ohosAnonymousUid";
35 }
36 
Init()37 int32_t MultiUserManager::Init()
38 {
39     HILOGI("Init");
40     int32_t foregroundId = DEFAULT_USER_ID;
41     int32_t res = OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(foregroundId);
42     if (res != DP_SUCCESS || foregroundId == DEFAULT_USER_ID) {
43         HILOGE("GetForegroundId failed, res: %{public}d", res);
44     }
45     HILOGI("current foregroundId = %{public}d", foregroundId);
46     SetCurrentForegroundUserID(foregroundId);
47 
48     return DP_SUCCESS;
49 }
50 
UnInit()51 int32_t MultiUserManager::UnInit()
52 {
53     return DP_SUCCESS;
54 }
55 
SetCurrentForegroundUserID(int32_t userId)56 void MultiUserManager::SetCurrentForegroundUserID(int32_t userId)
57 {
58     std::lock_guard<std::mutex> lock(foregroundUserIdLock_);
59     DeviceProfileManager::GetInstance().OnUserChange(foregroundUserId_, userId);
60     foregroundUserId_ = userId;
61 }
62 
GetCurrentForegroundUserID()63 int32_t MultiUserManager::GetCurrentForegroundUserID()
64 {
65     std::lock_guard<std::mutex> lock(foregroundUserIdLock_);
66     if (foregroundUserId_ == DEFAULT_USER_ID) {
67         GetForegroundUserIDFromOs(foregroundUserId_);
68     }
69     return foregroundUserId_;
70 }
71 
GetForegroundUserIDFromOs(int32_t & foregroundId)72 int32_t MultiUserManager::GetForegroundUserIDFromOs(int32_t& foregroundId)
73 {
74     foregroundId = DEFAULT_USER_ID;
75     int32_t res = OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(foregroundId);
76     if (res != DP_SUCCESS || foregroundId == DEFAULT_USER_ID) {
77         HILOGE("GetForegroundId failed, res:%{public}d", res);
78         return DP_GET_FOREGROUND_ID_FAIL;
79     }
80     HILOGI("GetForegroundUserIDFromOs foregroundId = %{public}d", foregroundId);
81     return DP_SUCCESS;
82 }
83 
GetOhosAccountId()84 std::string MultiUserManager::GetOhosAccountId()
85 {
86 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
87     return "";
88 #elif DP_OS_ACCOUNT_PART_EXISTS
89     AccountSA::OhosAccountInfo accountInfo;
90     ErrCode ret = OHOS::AccountSA::OhosAccountKits::GetInstance().GetOhosAccountInfo(accountInfo);
91     if (ret != 0 || accountInfo.uid_ == "") {
92         HILOGE("GetOhosAccountId error ret: %{public}d", ret);
93         return "";
94     }
95     return accountInfo.uid_;
96 #else
97     return "";
98 #endif
99 }
100 
GetOhosAccountIdByUserId(int32_t userId)101 std::string MultiUserManager::GetOhosAccountIdByUserId(int32_t userId)
102 {
103 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
104     (void)userId;
105     return "";
106 #elif DP_OS_ACCOUNT_PART_EXISTS
107     AccountSA::OhosAccountInfo accountInfo;
108     ErrCode ret = OHOS::AccountSA::OhosAccountKits::GetInstance().GetOsAccountDistributedInfo(userId, accountInfo);
109     if (ret != 0 || accountInfo.uid_ == "" || accountInfo.uid_ == OHOS_ANONYMOUS_UID) {
110         HILOGE("error ret: %{public}d", ret);
111         return "";
112     }
113     return accountInfo.uid_;
114 #else
115     (void)userId;
116     return "";
117 #endif
118 }
119 
120 } // namespace DistributedHardware
121 } // namespace OHOS
122