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 "appaccount_ffi.h"
17 
18 #include "cj_lambda.h"
19 #include "appaccount_impl.h"
20 
21 using namespace OHOS::FFI;
22 
23 namespace OHOS::AccountSA {
24 extern "C" {
FfiAppAccountCreateAppAccountManager(void)25 RetDataI64 FfiAppAccountCreateAppAccountManager(void)
26 {
27     RetDataI64 ret = {.code = ERR_OK, .data = -1};
28     auto nativeAppAccountManager = FFIData::Create<CJAppAccountImpl>();
29     if (nativeAppAccountManager == nullptr) {
30         ret.code = ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
31         return ret;
32     }
33     ret.data = nativeAppAccountManager->GetID();
34     return ret;
35 }
36 
FfiAppAccountAppAccountManagerCreateAccount(int id, char *name, CCreateAccountOptions options)37 int32_t FfiAppAccountAppAccountManagerCreateAccount(int id, char *name, CCreateAccountOptions options)
38 {
39     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
40     if (!instance) {
41         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
42     }
43     return instance->createAccount(name, options);
44 }
45 
FfiAppAccountAppAccountManagerRemoveAccount(int id, char *name)46 int32_t FfiAppAccountAppAccountManagerRemoveAccount(int id, char *name)
47 {
48     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
49     if (!instance) {
50         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
51     }
52     return instance->removeAccount(name);
53 }
54 
FfiAppAccountAppAccountManagerSetAppAccess(int id, char *name, char *bundleName, bool isAccessible)55 int32_t FfiAppAccountAppAccountManagerSetAppAccess(int id, char *name, char *bundleName, bool isAccessible)
56 {
57     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
58     if (!instance) {
59         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
60     }
61     return instance->setAppAccess(name, bundleName, isAccessible);
62 }
63 
FfiAppAccountAppAccountManagerCheckAppAccess(int id, char *name, char *bundleName)64 RetDataBool FfiAppAccountAppAccountManagerCheckAppAccess(int id, char *name, char *bundleName)
65 {
66     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
67     if (!instance) {
68         return {ERR_CJ_SYSTEM_SERVICE_EXCEPTION, false};
69     }
70     return instance->checkAppAccess(name, bundleName);
71 }
72 
FfiAppAccountAppAccountManagerCheckDataSyncEnabled(int id, char *name)73 RetDataBool FfiAppAccountAppAccountManagerCheckDataSyncEnabled(int id, char *name)
74 {
75     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
76     if (!instance) {
77         return {ERR_CJ_SYSTEM_SERVICE_EXCEPTION, false};
78     }
79     return instance->checkDataSyncEnabled(name);
80 }
81 
FfiAppAccountAppAccountManagerSetCredential(int id, char *name, char *credentialType, char *credential)82 int32_t FfiAppAccountAppAccountManagerSetCredential(int id, char *name, char *credentialType, char *credential)
83 {
84     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
85     if (!instance) {
86         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
87     }
88     return instance->setCredential(name, credentialType, credential);
89 }
90 
FfiAppAccountAppAccountManagerGetAccountsByOwner(int id, char *owner)91 ErrCArrAppAccountInfo FfiAppAccountAppAccountManagerGetAccountsByOwner(int id, char *owner)
92 {
93     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
94     if (!instance) {
95         ErrCArrAppAccountInfo res{};
96         res.err = ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
97         return res;
98     }
99     return instance->getAccountsByOwner(owner);
100 }
101 
FfiAppAccountAppAccountManagerGetCredential(int id, char *name, char *credentialType)102 RetDataCString FfiAppAccountAppAccountManagerGetCredential(int id, char *name, char *credentialType)
103 {
104     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
105     if (!instance) {
106         RetDataCString ret = { .code = ERR_CJ_SYSTEM_SERVICE_EXCEPTION, .data = nullptr };
107         return ret;
108     }
109     return instance->getCredential(name, credentialType);
110 }
111 
FfiAppAccountAppAccountManagerGetCustomData(int id, char *name, char *key)112 RetDataCString FfiAppAccountAppAccountManagerGetCustomData(int id, char *name, char *key)
113 {
114     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
115     if (!instance) {
116         RetDataCString ret = { .code = ERR_CJ_SYSTEM_SERVICE_EXCEPTION, .data = nullptr };
117         return ret;
118     }
119     return instance->getCustomData(name, key);
120 }
121 
FfiAppAccountAppAccountManagerGetAuthToken(int id, char *name, char *owner, char *authType)122 RetDataCString FfiAppAccountAppAccountManagerGetAuthToken(int id, char *name, char *owner, char *authType)
123 {
124     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
125     if (!instance) {
126         RetDataCString ret = { .code = ERR_CJ_SYSTEM_SERVICE_EXCEPTION, .data = nullptr };
127         return ret;
128     }
129     return instance->getAuthToken(name, owner, authType);
130 }
131 
FfiAppAccountAppAccountManagerSetAuthToken(int id, char *name, char *authType, char *token)132 int32_t FfiAppAccountAppAccountManagerSetAuthToken(int id, char *name, char *authType, char *token)
133 {
134     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
135     if (!instance) {
136         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
137     }
138     return instance->setAuthToken(name, authType, token);
139 }
140 
FfiAppAccountAppAccountManagerDeleteAuthToken(int id, char *name, char *owner, char *authType, char *token)141 int32_t FfiAppAccountAppAccountManagerDeleteAuthToken(int id, char *name, char *owner, char *authType, char *token)
142 {
143     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
144     if (!instance) {
145         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
146     }
147     return instance->deleteAuthToken(name, owner, authType, token);
148 }
149 
FfiAppAccountAppAccountManagerSetAuthTokenVisibility( int id, char *name, char *authType, char *bundleName, bool isVisible)150 int32_t FfiAppAccountAppAccountManagerSetAuthTokenVisibility(
151     int id, char *name, char *authType, char *bundleName, bool isVisible)
152 {
153     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
154     if (!instance) {
155         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
156     }
157     return instance->setAuthTokenVisibility(name, authType, bundleName, isVisible);
158 }
159 
FfiAppAccountAppAccountManagerCheckAuthTokenVisibility( int id, char *name, char *authType, char *bundleName, bool isVisible)160 RetDataBool FfiAppAccountAppAccountManagerCheckAuthTokenVisibility(
161     int id, char *name, char *authType, char *bundleName, bool isVisible)
162 {
163     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
164     if (!instance) {
165         return {ERR_CJ_SYSTEM_SERVICE_EXCEPTION, false};
166     }
167     return instance->checkAuthTokenVisibility(name, authType, bundleName);
168 }
169 
FfiAppAccountAppAccountManagerGetAllAuthTokens(int id, char *name, char *owner)170 ErrCArrAuthTokenInfo FfiAppAccountAppAccountManagerGetAllAuthTokens(int id, char *name, char *owner)
171 {
172     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
173     if (!instance) {
174         ErrCArrAuthTokenInfo res{};
175         res.err = ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
176         return res;
177     }
178     return instance->getAllAuthTokens(name, owner);
179 }
180 
FfiAppAccountAppAccountManagerGetAuthList(int id, char *name, char *authType)181 RetDataCArrString FfiAppAccountAppAccountManagerGetAuthList(int id, char *name, char *authType)
182 {
183     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
184     if (!instance) {
185         RetDataCArrString res = { .code = ERR_CJ_SYSTEM_SERVICE_EXCEPTION, .data = {.head = nullptr, .size = 0}};
186         return res;
187     }
188     return instance->getAuthList(name, authType);
189 }
190 
FfiAppAccountAppAccountManagerQueryAuthenticatorInfo(int id, char *owner)191 ErrCAuthenticatorInfo FfiAppAccountAppAccountManagerQueryAuthenticatorInfo(int id, char *owner)
192 {
193     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
194     if (!instance) {
195         ErrCAuthenticatorInfo res{};
196         res.err = ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
197         return res;
198     }
199     return instance->queryAuthenticatorInfo(owner);
200 }
201 
FfiAppAccountAppAccountManagerDeleteCredential(int id, char *name, char *credentialType)202 int32_t FfiAppAccountAppAccountManagerDeleteCredential(int id, char *name, char *credentialType)
203 {
204     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
205     if (!instance) {
206         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
207     }
208     return instance->deleteCredential(name, credentialType);
209 }
210 
FfiAppAccountAppAccountManagerGetAllAccounts(int id)211 ErrCArrAppAccountInfo FfiAppAccountAppAccountManagerGetAllAccounts(int id)
212 {
213     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
214     if (!instance) {
215         ErrCArrAppAccountInfo res{};
216         res.err = ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
217         return res;
218     }
219     return instance->getAllAccounts();
220 }
221 
FfiAppAccountAppAccountManagerSetCustomData(int id, char *name, char *key, char *value)222 int32_t FfiAppAccountAppAccountManagerSetCustomData(int id, char *name, char *key, char *value)
223 {
224     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
225     if (!instance) {
226         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
227     }
228     return instance->setCustomData(name, key, value);
229 }
230 
FfiAppAccountAppAccountManagerSetDataSyncEnabled(int id, char *name, bool isEnabled)231 int32_t FfiAppAccountAppAccountManagerSetDataSyncEnabled(int id, char *name, bool isEnabled)
232 {
233     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
234     if (!instance) {
235         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
236     }
237     return instance->setDataSyncEnabled(name, isEnabled);
238 }
239 
FfiAppAccountAppAccountManagerOn( int id, char *type, CArrString owners, void (*callback)(CArrAppAccountInfo cArrAppAccountInfo))240 int32_t FfiAppAccountAppAccountManagerOn(
241     int id, char *type, CArrString owners, void (*callback)(CArrAppAccountInfo cArrAppAccountInfo))
242 {
243     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
244     if (!instance) {
245         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
246     }
247     return instance->on(type, owners, callback);
248 }
249 
FfiAppAccountAppAccountManagerOff(int id, char *type, void (*callback)(CArrAppAccountInfo cArrAppAccountInfo))250 int32_t FfiAppAccountAppAccountManagerOff(int id, char *type, void (*callback)(CArrAppAccountInfo cArrAppAccountInfo))
251 {
252     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
253     if (!instance) {
254         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
255     }
256     return instance->off(type, callback);
257 }
258 
FfiAppAccountAppAccountManagerCheckAccountLabels( int id, char *name, char *owner, CArrString labels, void (*callback)(RetDataBool infoRef))259 int32_t FfiAppAccountAppAccountManagerCheckAccountLabels(
260     int id, char *name, char *owner, CArrString labels, void (*callback)(RetDataBool infoRef))
261 {
262     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
263     if (!instance) {
264         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
265     }
266     auto onChange = [lambda = CJLambda::Create(callback)]
267         (RetDataBool infoRef) -> void { lambda(infoRef); };
268     return instance->checkAccountLabels(name, owner, labels, onChange);
269 }
270 
FfiAppAccountAppAccountManagerSelectAccountsByOptions( int id, CSelectAccountsOptions cOptions, void (*callback)(ErrCArrAppAccountInfo infoRef))271 int32_t FfiAppAccountAppAccountManagerSelectAccountsByOptions(
272     int id, CSelectAccountsOptions cOptions, void (*callback)(ErrCArrAppAccountInfo infoRef))
273 {
274     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
275     if (!instance) {
276         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
277     }
278     auto onChange = [lambda = CJLambda::Create(callback)]
279         (ErrCArrAppAccountInfo infoRef) -> void { lambda(infoRef); };
280     return instance->selectAccountByOptions(cOptions, onChange);
281 }
282 
FfiAppAccountAppAccountManagerVerifyCredential( int id, char *name, char *owner, CAuthCallback callbackId, CVerifyCredentialOptions cOptions)283 int32_t FfiAppAccountAppAccountManagerVerifyCredential(
284     int id, char *name, char *owner, CAuthCallback callbackId, CVerifyCredentialOptions cOptions)
285 {
286     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
287     if (!instance) {
288         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
289     }
290     return instance->verifyCredential(name, owner, callbackId, cOptions);
291 }
292 
FfiAppAccountAppAccountManagerSetAuthenticatorProperties( int id, char *owner, CAuthCallback callbackId, CSetPropertiesOptions cOptions)293 int32_t FfiAppAccountAppAccountManagerSetAuthenticatorProperties(
294     int id, char *owner, CAuthCallback callbackId, CSetPropertiesOptions cOptions)
295 {
296     auto instance = FFIData::GetData<CJAppAccountImpl>(id);
297     if (!instance) {
298         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
299     }
300     return instance->setAuthenticatorProperties(owner, callbackId, cOptions);
301 }
302 }
303 } // namespace::OHOS::AccountSA