1 /*
2 * Copyright (c) 2022 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 "account_helper.h"
17
18 #include <thread>
19
20 #include "app_log_wrapper.h"
21 #include "bundle_constants.h"
22
23 #ifdef ACCOUNT_ENABLE
24 #include "os_account_manager.h"
25 #endif
26
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace {
30 constexpr int32_t RETRY_TIMES = 20;
31 constexpr int32_t RETRY_INTERVAL = 50; // 50ms
32 }
33
IsOsAccountExists(const int32_t id,bool & isOsAccountExists)34 int32_t AccountHelper::IsOsAccountExists(const int32_t id, bool &isOsAccountExists)
35 {
36 #ifdef ACCOUNT_ENABLE
37 return AccountSA::OsAccountManager::IsOsAccountCompleted(id, isOsAccountExists);
38 #else
39 APP_LOGI("ACCOUNT_ENABLE is false");
40 // ACCOUNT_ENABLE is false, do nothing and return -1.
41 return -1;
42 #endif
43 }
44
QueryAllCreatedOsAccounts(std::set<int32_t> & userIds)45 void AccountHelper::QueryAllCreatedOsAccounts(std::set<int32_t> &userIds)
46 {
47 #ifdef ACCOUNT_ENABLE
48 std::vector<AccountSA::OsAccountInfo> osAccountInfos;
49 if (AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos) == 0) {
50 for (AccountSA::OsAccountInfo acct : osAccountInfos) {
51 userIds.insert(acct.GetLocalId());
52 }
53 }
54 #else
55 APP_LOGI("ACCOUNT_ENABLE is false");
56 // ACCOUNT_ENABLE is false, do nothing .
57 #endif
58 }
59
GetCurrentActiveUserId()60 int32_t AccountHelper::GetCurrentActiveUserId()
61 {
62 #ifdef ACCOUNT_ENABLE
63 int32_t localId;
64 int32_t ret = AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(localId);
65 if (ret != 0) {
66 APP_LOGE_NOFUNC("GetForegroundOsAccountLocalId failed ret:%{public}d", ret);
67 return Constants::INVALID_USERID;
68 }
69 return localId;
70 #else
71 APP_LOGI("ACCOUNT_ENABLE is false");
72 return 0;
73 #endif
74 }
75
IsOsAccountVerified(const int32_t userId)76 bool AccountHelper::IsOsAccountVerified(const int32_t userId)
77 {
78 #ifdef ACCOUNT_ENABLE
79 bool isOsAccountVerified = false;
80 int32_t ret = AccountSA::OsAccountManager::IsOsAccountVerified(userId, isOsAccountVerified);
81 if (ret != 0) {
82 APP_LOGE("IsOsAccountVerified failed ret:%{public}d", ret);
83 return false;
84 }
85 return isOsAccountVerified;
86 #else
87 APP_LOGI("ACCOUNT_ENABLE is false");
88 return false;
89 #endif
90 }
91
GetOsAccountLocalIdFromUid(const int32_t callingUid)92 int32_t AccountHelper::GetOsAccountLocalIdFromUid(const int32_t callingUid)
93 {
94 int32_t localId = callingUid < Constants::DEFAULT_USERID ? Constants::INVALID_USERID :
95 callingUid / Constants::BASE_USER_RANGE;
96 #ifdef ACCOUNT_ENABLE
97 if (localId <= Constants::DEFAULT_USERID) {
98 APP_LOGW_NOFUNC("GetOsAccountLocalIdFromUid fail uid:%{public}d req from active userid", callingUid);
99 return AccountHelper::GetCurrentActiveUserId();
100 }
101 return localId;
102 #else
103 APP_LOGI("ACCOUNT_ENABLE is false");
104 // ACCOUNT_ENABLE is false, do nothing and return -1.
105 return localId;
106 #endif
107 }
108
GetCurrentActiveUserIdWithRetry(bool isOtaInstall)109 int32_t AccountHelper::GetCurrentActiveUserIdWithRetry(bool isOtaInstall)
110 {
111 #ifdef ACCOUNT_ENABLE
112 // Failed to obtain foreground Os Account when OTA. BMS SA is not registered.
113 int32_t localId = Constants::INVALID_USERID;
114 if (!isOtaInstall) {
115 localId = GetCurrentActiveUserId();
116 if (localId != Constants::INVALID_USERID) {
117 return localId;
118 }
119 }
120 int32_t retryCnt = 0;
121 do {
122 int32_t ret = AccountSA::OsAccountManager::GetDefaultActivatedOsAccount(localId);
123 if (ret == 0) {
124 break;
125 }
126 ++retryCnt;
127 std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_INTERVAL));
128 APP_LOGW("get foregroud osAccount failed, retry count: %{public}d, ret: %{public}d", retryCnt, ret);
129 } while (retryCnt < RETRY_TIMES);
130
131 return localId;
132 #else
133 APP_LOGI("ACCOUNT_ENABLE is false");
134 return Constants::INVALID_USERID;
135 #endif
136 }
137
CheckOsAccountConstraintEnabled(const int32_t userId,const std::string & constraint)138 bool AccountHelper::CheckOsAccountConstraintEnabled(const int32_t userId, const std::string &constraint)
139 {
140 #ifdef ACCOUNT_ENABLE
141 bool isEnabled = false;
142 int32_t ret = AccountSA::OsAccountManager::CheckOsAccountConstraintEnabled(userId, constraint, isEnabled);
143 if (ret != 0) {
144 APP_LOGE("failed ret:%{public}d", ret);
145 return false;
146 }
147 return isEnabled;
148 #else
149 APP_LOGI("ACCOUNT_ENABLE is false");
150 return false;
151 #endif
152 }
153 } // namespace AppExecFwk
154 } // namespace OHOS
155