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 "cj_osaccount_ffi.h"
17 #include "account_error_no.h"
18 #include "account_permission_manager.h"
19 #include "account_log_wrapper.h"
20 #include "osaccount_manager_impl.h"
21 
22 using namespace OHOS::AccountSA;
23 
24 namespace OHOS::AccountJsKit {
25     EXTERN_C_START
FfiOHOSOsAccountIsOsAccountConstraintEnabled(char *constraint, int32_t *errCode)26     bool FfiOHOSOsAccountIsOsAccountConstraintEnabled(char *constraint, int32_t *errCode)
27     {
28         if (constraint == nullptr) {
29             *errCode = ERR_JS_PARAMETER_ERROR;
30             ACCOUNT_LOGE("[osAccount] IsOsAccountConstraintEnabled constraint is null! errCode %{public}d",
31                 *errCode);
32             return false;
33         }
34         if (AccountPermissionManager::CheckSystemApp(false) != ERR_OK) {
35             *errCode = ERR_JS_IS_NOT_SYSTEM_APP;
36             ACCOUNT_LOGE("[osAccount] IsOsAccountConstraintEnabled CheckSystemApp failed! errCode %{public}d",
37                 *errCode);
38             return false;
39         }
40         ACCOUNT_LOGE("[osAccount] IsOsAccountConstraintEnabled start");
41         bool ret = OsAccountManagerImpl::IsOsAccountConstraintEnabled(constraint, errCode);
42         ACCOUNT_LOGE("[osAccount] IsOsAccountConstraintEnabled success. errCode %{public}d", *errCode);
43         return ret;
44     }
45 
FfiOHOSOsAccountGetOsAccountType(int32_t *errCode)46     int32_t FfiOHOSOsAccountGetOsAccountType(int32_t *errCode)
47     {
48         ACCOUNT_LOGE("[osAccount] GetOsAccountType start");
49         int32_t ret = OsAccountManagerImpl::GetOsAccountType(errCode);
50         ACCOUNT_LOGE("[osAccount] GetOsAccountType success. errCode %{public}d", *errCode);
51         return ret;
52     }
53 
FfiOHOSOsAccountCheckOsAccountTestable(int32_t *errCode)54     bool FfiOHOSOsAccountCheckOsAccountTestable(int32_t *errCode)
55     {
56         *errCode = ERR_JS_SUCCESS;
57         return false;
58     }
59 
FfiOHOSOsAccountCheckMultiOsAccountEnabled(int32_t *errCode)60     bool FfiOHOSOsAccountCheckMultiOsAccountEnabled(int32_t *errCode)
61     {
62         ACCOUNT_LOGE("[osAccount] CheckMultiOsAccountEnabled start");
63         bool ret = OsAccountManagerImpl::CheckMultiOsAccountEnabled(errCode);
64         ACCOUNT_LOGE("[osAccount] CheckMultiOsAccountEnabled success. errCode %{public}d", *errCode);
65         return ret;
66     }
67 
FfiOHOSOsAccountGetOsAccountLocalId(int32_t *errCode)68     int32_t FfiOHOSOsAccountGetOsAccountLocalId(int32_t *errCode)
69     {
70         ACCOUNT_LOGE("[osAccount] GetOsAccountLocalId start");
71         int32_t ret = OsAccountManagerImpl::GetOsAccountLocalId(errCode);
72         ACCOUNT_LOGE("[osAccount] GetOsAccountLocalId success. errCode %{public}d", *errCode);
73         return ret;
74     }
75 
FfiOHOSOsAccountGetActivatedOsAccountLocalIds()76     RetDataCArrI32 FfiOHOSOsAccountGetActivatedOsAccountLocalIds()
77     {
78         std::vector<int32_t> osAccountIds;
79         ACCOUNT_LOGE("[osAccount] GetActivatedOsAccountLocalIds start");
80         int32_t code = OsAccountManagerImpl::GetActivatedOsAccountLocalIds(osAccountIds);
81         ACCOUNT_LOGE("[osAccount] GetActivatedOsAccountLocalIds success. errCode %{public}d", code);
82         CArrI32 data = {.head = nullptr, .size = 0};
83         RetDataCArrI32 ret = {.code = code, .data = data};
84         if (code != ERR_JS_SUCCESS) {
85             return ret;
86         }
87         size_t listSize = osAccountIds.size();
88         ret.data.size = static_cast<int64_t>(listSize);
89         if (listSize > 0) {
90             int32_t *retValue = static_cast<int32_t *>(malloc(sizeof(int32_t) * listSize));
91             if (retValue == nullptr) {
92                 ret.code = ERR_JS_SYSTEM_SERVICE_EXCEPTION;
93                 return ret;
94             }
95             for (int32_t i = 0; i < listSize; i++) {
96                 retValue[i] = osAccountIds[i];
97             }
98             ret.data.head = retValue;
99         }
100         return ret;
101     }
102 
FfiOHOSOsAccountGetOsAccountCount(int32_t *errCode)103     uint32_t FfiOHOSOsAccountGetOsAccountCount(int32_t *errCode)
104     {
105         ACCOUNT_LOGE("[osAccount] GetOsAccountCount start");
106         int32_t ret = OsAccountManagerImpl::GetOsAccountCount(errCode);
107         ACCOUNT_LOGE("[osAccount] GetOsAccountCount success. errCode %{public}d", *errCode);
108         return ret;
109     }
110 
FfiOHOSOsAccountQueryDistributedVirtualDeviceId(int32_t *errCode)111     char *FfiOHOSOsAccountQueryDistributedVirtualDeviceId(int32_t *errCode)
112     {
113         ACCOUNT_LOGE("[osAccount] QueryDistributedVirtualDeviceId start");
114         char *ret = OsAccountManagerImpl::QueryDistributedVirtualDeviceId(errCode);
115         ACCOUNT_LOGE("[osAccount] QueryDistributedVirtualDeviceId success. errCode %{public}d", *errCode);
116         return ret;
117     }
118 
FfiOHOSOsAccountGetSerialNumberForOsAccountLocalId(uint32_t localId, int32_t *errCode)119     int64_t FfiOHOSOsAccountGetSerialNumberForOsAccountLocalId(uint32_t localId, int32_t *errCode)
120     {
121         ACCOUNT_LOGE("[osAccount] GetSerialNumberForOsAccountLocalId start");
122         int32_t ret = OsAccountManagerImpl::GetSerialNumberForOsAccountLocalId(localId, errCode);
123         ACCOUNT_LOGE("[osAccount] GetSerialNumberForOsAccountLocalId success. errCode %{public}d", *errCode);
124         return ret;
125     }
126 
FfiOHOSOsAccountGetOsAccountLocalIdForSerialNumber(int64_t serialNumber, int32_t *errCode)127     int32_t FfiOHOSOsAccountGetOsAccountLocalIdForSerialNumber(int64_t serialNumber, int32_t *errCode)
128     {
129         ACCOUNT_LOGE("[osAccount] GetOsAccountLocalIdForSerialNumber start");
130         int32_t ret = OsAccountManagerImpl::GetOsAccountLocalIdForSerialNumber(serialNumber, errCode);
131         ACCOUNT_LOGE("[osAccount] GetOsAccountLocalIdForSerialNumber success. errCode %{public}d", *errCode);
132         return ret;
133     }
134 
FfiOHOSOsAccountGetOsAccountLocalIdForDomain(CDomainAccountInfo cDoaminInfo, int32_t *errCode)135     int32_t FfiOHOSOsAccountGetOsAccountLocalIdForDomain(CDomainAccountInfo cDoaminInfo, int32_t *errCode)
136     {
137         ACCOUNT_LOGE("[osAccount] GetOsAccountLocalIdForDomain start");
138         int32_t ret = OsAccountManagerImpl::GetOsAccountLocalIdForDomain(
139             cDoaminInfo.domain, cDoaminInfo.accountName, errCode);
140         ACCOUNT_LOGE("[osAccount] GetOsAccountLocalIdForDomain success. errCode %{public}d", *errCode);
141         return ret;
142     }
143 
FfiOHOSOsAccountGetOsAccountLocalIdForUid(int32_t uid, int32_t *errCode)144     int32_t FfiOHOSOsAccountGetOsAccountLocalIdForUid(int32_t uid, int32_t *errCode)
145     {
146         ACCOUNT_LOGE("[osAccount] GetOsAccountLocalIdForUid start");
147         int32_t ret = OsAccountManagerImpl::GetOsAccountLocalIdForUid(uid, errCode);
148         ACCOUNT_LOGE("[osAccount] GetOsAccountLocalIdForUid success. errCode %{public}d", *errCode);
149         return ret;
150     }
151 
FfiOHOSOsAccountGetOsAccountName(int32_t *errCode)152     char *FfiOHOSOsAccountGetOsAccountName(int32_t *errCode)
153     {
154         ACCOUNT_LOGE("[osAccount] GetOsAccountName start");
155         char *ret = OsAccountManagerImpl::GetOsAccountName(errCode);
156         ACCOUNT_LOGE("[osAccount] GetOsAccountName success. errCode %{public}d", *errCode);
157         return ret;
158     }
159 
FfiOHOSOsAccountIsOsAccountUnlocked(int32_t *errCode)160     bool FfiOHOSOsAccountIsOsAccountUnlocked(int32_t *errCode)
161     {
162         if (AccountPermissionManager::CheckSystemApp(false) != ERR_OK) {
163             *errCode = ERR_JS_IS_NOT_SYSTEM_APP;
164             ACCOUNT_LOGE("[osAccount] IsOsAccountUnlocked CheckSystemApp failed! errCode %{public}d", *errCode);
165             return false;
166         }
167         ACCOUNT_LOGE("[osAccount] IsOsAccountUnlocked start");
168         bool ret = OsAccountManagerImpl::IsOsAccountUnlocked(errCode);
169         ACCOUNT_LOGE("[osAccount] IsOsAccountUnlocked success. errCode %{public}d", *errCode);
170         return ret;
171     }
172     EXTERN_C_END
173 } // namespace OHOS::AccountJsKit
174