1 /*
2 * Copyright (c) 2021-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 "app_account.h"
17
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20 #include "account_proxy.h"
21 #include "app_account_common.h"
22 #include "app_account_constants.h"
23 #include "app_account_death_recipient.h"
24 #include "ohos_account_kits_impl.h"
25 #include "system_ability_definition.h"
26 #include <string>
27
28 namespace OHOS {
29 namespace AccountSA {
30 namespace {
ConvertToAccountErrCode(ErrCode idlErrCode,int32_t funcResult)31 ErrCode ConvertToAccountErrCode(ErrCode idlErrCode, int32_t funcResult)
32 {
33 if (idlErrCode == ERR_OK) {
34 return funcResult;
35 }
36 if (idlErrCode == ERR_INVALID_VALUE) {
37 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
38 } else if (idlErrCode == ERR_INVALID_DATA) {
39 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
40 } else {
41 return ERR_APPACCOUNT_KIT_SEND_REQUEST;
42 }
43 }
44 } // namespace
45 #define RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(str) \
46 if (CheckSpecialCharacters(str) != ERR_OK) { \
47 ACCOUNT_LOGE("failed to check special characters"); \
48 NativeErrMsg() = "Invalid name. The name cannot contain space characters"; \
49 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; \
50 } \
51
52 #define RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(str, maxSize, msg) \
53 if ((str).empty() || ((str).size() > (maxSize))) { \
54 ACCOUNT_LOGE("%{public}s, input size: %{public}zu, max size: %{public}zu", msg, (str).size(), maxSize); \
55 NativeErrMsg() = msg; \
56 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; \
57 }
58
59 #define RETURN_IF_STRING_IS_OVERSIZE(str, maxSize, msg) \
60 if ((str).size() > (maxSize)) { \
61 ACCOUNT_LOGE("%{public}s, input size: %{public}zu, max size: %{public}zu", msg, (str).size(), maxSize); \
62 NativeErrMsg() = msg; \
63 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; \
64 } \
65
GetInstance()66 AppAccount &AppAccount::GetInstance()
67 {
68 static AppAccount *instance = new (std::nothrow) AppAccount();
69 return *instance;
70 }
71
AppAccount()72 AppAccount::AppAccount()
73 {
74 auto callbackFunc = [] (int32_t systemAbilityId, const std::string &deviceId) {
75 if (systemAbilityId == SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN) {
76 AppAccount::GetInstance().RestoreListenerRecords();
77 }
78 };
79 OhosAccountKitsImpl::GetInstance().SubscribeSystemAbility(callbackFunc);
80 }
81
RestoreListenerRecords()82 void AppAccount::RestoreListenerRecords()
83 {
84 auto proxy = GetAppAccountProxy();
85 if (proxy == nullptr) {
86 return;
87 }
88
89 std::lock_guard<std::mutex> lock(eventListenersMutex_);
90 AppAccountSubscribeInfo subscribeInfo;
91 bool flag = AppAccountEventListener::GetInstance()->GetRestoreData(subscribeInfo);
92 if (!flag) {
93 return;
94 }
95 int32_t funcResult = 0;
96 ErrCode result = proxy->SubscribeAppAccount(
97 subscribeInfo, AppAccountEventListener::GetInstance()->AsObject(), funcResult);
98 result = ConvertToAccountErrCode(result, funcResult);
99 if (result != ERR_OK) {
100 std::vector<std::string> owners;
101 subscribeInfo.GetOwners(owners);
102 ACCOUNT_LOGE("SubscribeAppAccount owners size=%{public}d failed, errCode=%{public}d",
103 static_cast<uint32_t>(owners.size()), result);
104 return;
105 }
106
107 ACCOUNT_LOGI("The data recovery was successful.");
108 }
109
AddAccount(const std::string & name,const std::string & extraInfo)110 ErrCode AppAccount::AddAccount(const std::string &name, const std::string &extraInfo)
111 {
112 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
113 "Invalid name. The length of the name must be greater than 0 and less than 513");
114 RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
115 RETURN_IF_STRING_IS_OVERSIZE(extraInfo, Constants::EXTRA_INFO_MAX_SIZE,
116 "Invalid extraInfo. The length of the extraInfo must be less than 1025");
117 auto proxy = GetAppAccountProxy();
118 if (proxy == nullptr) {
119 return ERR_ACCOUNT_COMMON_GET_PROXY;
120 }
121 int32_t funcResult = 0;
122 auto idlErrCode = proxy->AddAccount(name, extraInfo, funcResult);
123 return ConvertToAccountErrCode(idlErrCode, funcResult);
124 }
125
AddAccountImplicitly(const std::string & owner,const std::string & authType,const AAFwk::Want & options,const sptr<IAppAccountAuthenticatorCallback> & callback)126 ErrCode AppAccount::AddAccountImplicitly(const std::string &owner, const std::string &authType,
127 const AAFwk::Want &options, const sptr<IAppAccountAuthenticatorCallback> &callback)
128 {
129 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE,
130 "Invalid owner. The length of the owner must be greater than 0 and less than 1025");
131 RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE,
132 "Invalid authType. The length of the authType must be less than 1025");
133 auto proxy = GetAppAccountProxy();
134 if (proxy == nullptr) {
135 return ERR_ACCOUNT_COMMON_GET_PROXY;
136 }
137 int32_t funcResult = 0;
138 auto idlErrCode = proxy->AddAccountImplicitly(owner, authType, options, callback, funcResult);
139 return ConvertToAccountErrCode(idlErrCode, funcResult);
140 }
141
CreateAccount(const std::string & name,const CreateAccountOptions & options)142 ErrCode AppAccount::CreateAccount(const std::string &name, const CreateAccountOptions &options)
143 {
144 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
145 "Invalid name. The length of the name must be greater than 0 and less than 513");
146 auto proxy = GetAppAccountProxy();
147 if (proxy == nullptr) {
148 return ERR_ACCOUNT_COMMON_GET_PROXY;
149 }
150 if (options.customData.size() > Constants::MAX_CUSTOM_DATA_SIZE) {
151 NativeErrMsg() = "Invalid options.customData."
152 "The length of the options.customData must be greater than 0 and less than 513";
153 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
154 }
155 for (auto it : options.customData) {
156 RETURN_IF_STRING_IS_OVERSIZE(it.first, Constants::ASSOCIATED_KEY_MAX_SIZE,
157 "Invalid options.customData key."
158 "The length of the options.customData key must be less than 1025");
159 RETURN_IF_STRING_IS_OVERSIZE(it.second, Constants::ASSOCIATED_VALUE_MAX_SIZE,
160 "Invalid options.customData value."
161 "The length of the options.customData value must be less than 1025");
162 }
163 int32_t funcResult = 0;
164 auto idlErrCode = proxy->CreateAccount(name, options, funcResult);
165 return ConvertToAccountErrCode(idlErrCode, funcResult);
166 }
167
CreateAccountImplicitly(const std::string & owner,const CreateAccountImplicitlyOptions & options,const sptr<IAppAccountAuthenticatorCallback> & callback)168 ErrCode AppAccount::CreateAccountImplicitly(const std::string &owner, const CreateAccountImplicitlyOptions &options,
169 const sptr<IAppAccountAuthenticatorCallback> &callback)
170 {
171 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE,
172 "Invalid owner. The length of the owner must be greater than 0 and less than 1025");
173 RETURN_IF_STRING_IS_OVERSIZE(options.authType, Constants::AUTH_TYPE_MAX_SIZE,
174 "Invalid options.authType. The length of the options.authType must be less than 1025");
175 RETURN_IF_STRING_IS_OVERSIZE(options.requiredLabels, Constants::MAX_ALLOWED_ARRAY_SIZE_INPUT,
176 "Invalid options.requiredLabels. The length of the options.requiredLabels must be less than 1025");
177 auto proxy = GetAppAccountProxy();
178 if (proxy == nullptr) {
179 return ERR_ACCOUNT_COMMON_GET_PROXY;
180 }
181 int32_t funcResult = 0;
182 auto idlErrCode = proxy->CreateAccountImplicitly(owner, options, callback, funcResult);
183 return ConvertToAccountErrCode(idlErrCode, funcResult);
184 }
185
DeleteAccount(const std::string & name)186 ErrCode AppAccount::DeleteAccount(const std::string &name)
187 {
188 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
189 "Invalid name. The length of the name must be greater than 0 and less than 513");
190 auto proxy = GetAppAccountProxy();
191 if (proxy == nullptr) {
192 return ERR_ACCOUNT_COMMON_GET_PROXY;
193 }
194 int32_t funcResult = 0;
195 auto idlErrCode = proxy->DeleteAccount(name, funcResult);
196 return ConvertToAccountErrCode(idlErrCode, funcResult);
197 }
198
GetAccountExtraInfo(const std::string & name,std::string & extraInfo)199 ErrCode AppAccount::GetAccountExtraInfo(const std::string &name, std::string &extraInfo)
200 {
201 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
202 "Invalid name. The length of the name must be greater than 0 and less than 513");
203 RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
204 RETURN_IF_STRING_IS_OVERSIZE(extraInfo, Constants::EXTRA_INFO_MAX_SIZE,
205 "Invalid extraInfo. The length of the extraInfo must be less than 1025");
206 auto proxy = GetAppAccountProxy();
207 if (proxy == nullptr) {
208 return ERR_ACCOUNT_COMMON_GET_PROXY;
209 }
210 int32_t funcResult = 0;
211 auto idlErrCode = proxy->GetAccountExtraInfo(name, extraInfo, funcResult);
212 return ConvertToAccountErrCode(idlErrCode, funcResult);
213 }
214
SetAccountExtraInfo(const std::string & name,const std::string & extraInfo)215 ErrCode AppAccount::SetAccountExtraInfo(const std::string &name, const std::string &extraInfo)
216 {
217 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
218 "Invalid name. The length of the name must be greater than 0 and less than 513");
219 RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
220 RETURN_IF_STRING_IS_OVERSIZE(extraInfo, Constants::EXTRA_INFO_MAX_SIZE,
221 "Invalid extraInfo. The length of the extraInfo must be less than 1025");
222 auto proxy = GetAppAccountProxy();
223 if (proxy == nullptr) {
224 return ERR_ACCOUNT_COMMON_GET_PROXY;
225 }
226 int32_t funcResult = 0;
227 auto idlErrCode = proxy->SetAccountExtraInfo(name, extraInfo, funcResult);
228 return ConvertToAccountErrCode(idlErrCode, funcResult);
229 }
230
EnableAppAccess(const std::string & name,const std::string & bundleName)231 ErrCode AppAccount::EnableAppAccess(const std::string &name, const std::string &bundleName)
232 {
233 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
234 "Invalid name. The length of the name must be greater than 0 and less than 513");
235 RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
236 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(bundleName, Constants::BUNDLE_NAME_MAX_SIZE,
237 "Invalid bundleName. The length of the bundleName must be greater than 0 and less than 513");
238 auto proxy = GetAppAccountProxy();
239 if (proxy == nullptr) {
240 return ERR_ACCOUNT_COMMON_GET_PROXY;
241 }
242 int32_t funcResult = 0;
243 auto idlErrCode = proxy->EnableAppAccess(name, bundleName, funcResult);
244 return ConvertToAccountErrCode(idlErrCode, funcResult);
245 }
246
DisableAppAccess(const std::string & name,const std::string & bundleName)247 ErrCode AppAccount::DisableAppAccess(const std::string &name, const std::string &bundleName)
248 {
249 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
250 "Invalid name. The length of the name must be greater than 0 and less than 513");
251 RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
252 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(bundleName, Constants::BUNDLE_NAME_MAX_SIZE,
253 "Invalid bundleName. The length of the bundleName must be greater than 0 and less than 513");
254 auto proxy = GetAppAccountProxy();
255 if (proxy == nullptr) {
256 return ERR_ACCOUNT_COMMON_GET_PROXY;
257 }
258 int32_t funcResult = 0;
259 auto idlErrCode = proxy->DisableAppAccess(name, bundleName, funcResult);
260 return ConvertToAccountErrCode(idlErrCode, funcResult);
261 }
262
SetAppAccess(const std::string & name,const std::string & authorizedApp,bool isAccessible)263 ErrCode AppAccount::SetAppAccess(const std::string &name, const std::string &authorizedApp, bool isAccessible)
264 {
265 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
266 "Invalid name. The length of the name must be greater than 0 and less than 513");
267 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(authorizedApp, Constants::BUNDLE_NAME_MAX_SIZE,
268 "Invalid bundleName. The length of the bundleName must be greater than 0 and less than 513");
269 auto proxy = GetAppAccountProxy();
270 if (proxy == nullptr) {
271 return ERR_ACCOUNT_COMMON_GET_PROXY;
272 }
273 int32_t funcResult = 0;
274 auto idlErrCode = proxy->SetAppAccess(name, authorizedApp, isAccessible, funcResult);
275 return ConvertToAccountErrCode(idlErrCode, funcResult);
276 }
277
CheckAppAccountSyncEnable(const std::string & name,bool & syncEnable)278 ErrCode AppAccount::CheckAppAccountSyncEnable(const std::string &name, bool &syncEnable)
279 {
280 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
281 "Invalid name. The length of the name must be greater than 0 and less than 513");
282 auto proxy = GetAppAccountProxy();
283 if (proxy == nullptr) {
284 return ERR_ACCOUNT_COMMON_GET_PROXY;
285 }
286 int32_t funcResult = 0;
287 auto idlErrCode = proxy->CheckAppAccountSyncEnable(name, syncEnable, funcResult);
288 return ConvertToAccountErrCode(idlErrCode, funcResult);
289 }
290
SetAppAccountSyncEnable(const std::string & name,const bool & syncEnable)291 ErrCode AppAccount::SetAppAccountSyncEnable(const std::string &name, const bool &syncEnable)
292 {
293 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
294 "Invalid name. The length of the name must be greater than 0 and less than 513");
295 auto proxy = GetAppAccountProxy();
296 if (proxy == nullptr) {
297 return ERR_ACCOUNT_COMMON_GET_PROXY;
298 }
299 int32_t funcResult = 0;
300 auto idlErrCode = proxy->SetAppAccountSyncEnable(name, syncEnable, funcResult);
301 return ConvertToAccountErrCode(idlErrCode, funcResult);
302 }
303
GetAssociatedData(const std::string & name,const std::string & key,std::string & value)304 ErrCode AppAccount::GetAssociatedData(const std::string &name, const std::string &key, std::string &value)
305 {
306 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
307 "Invalid name. The length of the name must be greater than 0 and less than 513");
308 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(key, Constants::ASSOCIATED_KEY_MAX_SIZE,
309 "Invalid key. The length of the key must be greater than 0 and less than 1025");
310 auto proxy = GetAppAccountProxy();
311 if (proxy == nullptr) {
312 return ERR_ACCOUNT_COMMON_GET_PROXY;
313 }
314 int32_t funcResult = 0;
315 auto idlErrCode = proxy->GetAssociatedData(name, key, value, funcResult);
316 return ConvertToAccountErrCode(idlErrCode, funcResult);
317 }
318
SetAssociatedData(const std::string & name,const std::string & key,const std::string & value)319 ErrCode AppAccount::SetAssociatedData(const std::string &name, const std::string &key, const std::string &value)
320 {
321 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
322 "Invalid name. The length of the name must be greater than 0 and less than 513");
323 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(key, Constants::ASSOCIATED_KEY_MAX_SIZE,
324 "Invalid key. The length of the key must be greater than 0 and less than 1025");
325 RETURN_IF_STRING_IS_OVERSIZE(value, Constants::ASSOCIATED_VALUE_MAX_SIZE,
326 "Invalid value. The length of the value must be less than 1025");
327 auto proxy = GetAppAccountProxy();
328 if (proxy == nullptr) {
329 return ERR_ACCOUNT_COMMON_GET_PROXY;
330 }
331 int32_t funcResult = 0;
332 auto idlErrCode = proxy->SetAssociatedData(name, key, value, funcResult);
333 return ConvertToAccountErrCode(idlErrCode, funcResult);
334 }
335
GetAccountCredential(const std::string & name,const std::string & credentialType,std::string & credential)336 ErrCode AppAccount::GetAccountCredential(
337 const std::string &name, const std::string &credentialType, std::string &credential)
338 {
339 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
340 "Invalid name. The length of the name must be greater than 0 and less than 513");
341 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(credentialType, Constants::CREDENTIAL_TYPE_MAX_SIZE,
342 "Invalid credentialType. The length of the credentialType must be greater than 0 and less than 1025");
343 auto proxy = GetAppAccountProxy();
344 if (proxy == nullptr) {
345 return ERR_ACCOUNT_COMMON_GET_PROXY;
346 }
347 int32_t funcResult = 0;
348 auto idlErrCode = proxy->GetAccountCredential(name, credentialType, credential, funcResult);
349 return ConvertToAccountErrCode(idlErrCode, funcResult);
350 }
351
SetAccountCredential(const std::string & name,const std::string & credentialType,const std::string & credential)352 ErrCode AppAccount::SetAccountCredential(
353 const std::string &name, const std::string &credentialType, const std::string &credential)
354 {
355 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
356 "Invalid name. The length of the name must be greater than 0 and less than 513");
357 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(credentialType, Constants::CREDENTIAL_TYPE_MAX_SIZE,
358 "Invalid credentialType. The length of the credentialType must be greater than 0 and less than 1025");
359 RETURN_IF_STRING_IS_OVERSIZE(credential, Constants::CREDENTIAL_MAX_SIZE,
360 "Invalid credential. The length of the credential must be less than 1025");
361 auto proxy = GetAppAccountProxy();
362 if (proxy == nullptr) {
363 return ERR_ACCOUNT_COMMON_GET_PROXY;
364 }
365 int32_t funcResult = 0;
366 auto idlErrCode = proxy->SetAccountCredential(name, credentialType, credential, funcResult);
367 return ConvertToAccountErrCode(idlErrCode, funcResult);
368 }
369
Authenticate(const std::string & name,const std::string & owner,const std::string & authType,const AAFwk::Want & options,const sptr<IAppAccountAuthenticatorCallback> & callback)370 ErrCode AppAccount::Authenticate(const std::string &name, const std::string &owner, const std::string &authType,
371 const AAFwk::Want &options, const sptr<IAppAccountAuthenticatorCallback> &callback)
372 {
373 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
374 "Invalid name. The length of the name must be greater than 0 and less than 513");
375 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE,
376 "Invalid owner. The length of the owner must be greater than 0 and less than 1025");
377 RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE,
378 "Invalid authType. The length of the authType must be less than 1025");
379 auto proxy = GetAppAccountProxy();
380 if (proxy == nullptr) {
381 return ERR_ACCOUNT_COMMON_GET_PROXY;
382 }
383 int32_t funcResult = 0;
384 AppAccountStringInfo appAccountStringInfo;
385 appAccountStringInfo.name = name;
386 appAccountStringInfo.owner = owner;
387 appAccountStringInfo.authType = authType;
388
389 auto idlErrCode = proxy->Authenticate(appAccountStringInfo, options, callback, funcResult);
390 return ConvertToAccountErrCode(idlErrCode, funcResult);
391 }
392
GetOAuthToken(const std::string & name,const std::string & owner,const std::string & authType,std::string & token)393 ErrCode AppAccount::GetOAuthToken(
394 const std::string &name, const std::string &owner, const std::string &authType, std::string &token)
395 {
396 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
397 "Invalid name. The length of the name must be greater than 0 and less than 513");
398 RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
399 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE,
400 "Invalid owner. The length of the owner must be greater than 0 and less than 1025");
401 RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE,
402 "Invalid authType. The length of the authType must be less than 1025");
403 auto proxy = GetAppAccountProxy();
404 if (proxy == nullptr) {
405 return ERR_ACCOUNT_COMMON_GET_PROXY;
406 }
407 int32_t funcResult = 0;
408 auto idlErrCode = proxy->GetOAuthToken(name, owner, authType, token, funcResult);
409 return ConvertToAccountErrCode(idlErrCode, funcResult);
410 }
411
GetAuthToken(const std::string & name,const std::string & owner,const std::string & authType,std::string & token)412 ErrCode AppAccount::GetAuthToken(
413 const std::string &name, const std::string &owner, const std::string &authType, std::string &token)
414 {
415 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
416 "Invalid name. The length of the name must be greater than 0 and less than 513");
417 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE,
418 "Invalid owner. The length of the owner must be greater than 0 and less than 1025");
419 RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE,
420 "Invalid authType. The length of the authType must be less than 1025");
421 auto proxy = GetAppAccountProxy();
422 if (proxy == nullptr) {
423 return ERR_ACCOUNT_COMMON_GET_PROXY;
424 }
425 int32_t funcResult = 0;
426 auto idlErrCode = proxy->GetAuthToken(name, owner, authType, token, funcResult);
427 return ConvertToAccountErrCode(idlErrCode, funcResult);
428 }
429
SetOAuthToken(const std::string & name,const std::string & authType,const std::string & token)430 ErrCode AppAccount::SetOAuthToken(const std::string &name, const std::string &authType, const std::string &token)
431 {
432 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
433 "Invalid name. The length of the name must be greater than 0 and less than 513");
434 RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE,
435 "Invalid authType. The length of the authType must be less than 1025");
436 RETURN_IF_STRING_IS_OVERSIZE(token, Constants::TOKEN_MAX_SIZE,
437 "Invalid token. The length of the token must be less than 1025");
438 auto proxy = GetAppAccountProxy();
439 if (proxy == nullptr) {
440 return ERR_ACCOUNT_COMMON_GET_PROXY;
441 }
442 int32_t funcResult = 0;
443 auto idlErrCode = proxy->SetOAuthToken(name, authType, token, funcResult);
444 return ConvertToAccountErrCode(idlErrCode, funcResult);
445 }
446
DeleteOAuthToken(const std::string & name,const std::string & owner,const std::string & authType,const std::string & token)447 ErrCode AppAccount::DeleteOAuthToken(
448 const std::string &name, const std::string &owner, const std::string &authType, const std::string &token)
449 {
450 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
451 "Invalid name. The length of the name must be greater than 0 and less than 513");
452 RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
453 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE,
454 "Invalid owner. The length of the owner must be greater than 0 and less than 1025");
455 RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE,
456 "Invalid authType. The length of the authType must be less than 1025");
457 RETURN_IF_STRING_IS_OVERSIZE(token, Constants::TOKEN_MAX_SIZE,
458 "Invalid token. The length of the token must be less than 1025");
459 auto proxy = GetAppAccountProxy();
460 if (proxy == nullptr) {
461 return ERR_ACCOUNT_COMMON_GET_PROXY;
462 }
463 int32_t funcResult = 0;
464 auto idlErrCode = proxy->DeleteOAuthToken(name, owner, authType, token, funcResult);
465 return ConvertToAccountErrCode(idlErrCode, funcResult);
466 }
467
DeleteAuthToken(const std::string & name,const std::string & owner,const std::string & authType,const std::string & token)468 ErrCode AppAccount::DeleteAuthToken(
469 const std::string &name, const std::string &owner, const std::string &authType, const std::string &token)
470 {
471 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
472 "Invalid name. The length of the name must be greater than 0 and less than 513");
473 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE,
474 "Invalid owner. The length of the owner must be greater than 0 and less than 1025");
475 RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE,
476 "Invalid authType. The length of the authType must be less than 1025");
477 RETURN_IF_STRING_IS_OVERSIZE(token, Constants::TOKEN_MAX_SIZE,
478 "Invalid token. The length of the token must be less than 1025");
479 auto proxy = GetAppAccountProxy();
480 if (proxy == nullptr) {
481 return ERR_ACCOUNT_COMMON_GET_PROXY;
482 }
483 int32_t funcResult = 0;
484 auto idlErrCode = proxy->DeleteAuthToken(name, owner, authType, token, funcResult);
485 return ConvertToAccountErrCode(idlErrCode, funcResult);
486 }
487
CheckTokenVisibilityParam(const std::string & name,const std::string & authType,const std::string & bundleName)488 ErrCode AppAccount::CheckTokenVisibilityParam(
489 const std::string &name, const std::string &authType, const std::string &bundleName)
490 {
491 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
492 "Invalid name. The length of the name must be greater than 0 and less than 513");
493 RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE,
494 "Invalid authType. The length of the authType must be less than 1025");
495 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(bundleName, Constants::BUNDLE_NAME_MAX_SIZE,
496 "Invalid bundleName. The length of the bundleName must be greater than 0 and less than 513");
497 return ERR_OK;
498 }
499
SetAuthTokenVisibility(const std::string & name,const std::string & authType,const std::string & bundleName,bool isVisible)500 ErrCode AppAccount::SetAuthTokenVisibility(
501 const std::string &name, const std::string &authType, const std::string &bundleName, bool isVisible)
502 {
503 ErrCode ret = CheckTokenVisibilityParam(name, authType, bundleName);
504 if (ret != ERR_OK) {
505 return ret;
506 }
507 auto proxy = GetAppAccountProxy();
508 if (proxy == nullptr) {
509 return ERR_ACCOUNT_COMMON_GET_PROXY;
510 }
511 int32_t funcResult = 0;
512 auto idlErrCode = proxy->SetAuthTokenVisibility(name, authType, bundleName, isVisible, funcResult);
513 return ConvertToAccountErrCode(idlErrCode, funcResult);
514 }
515
SetOAuthTokenVisibility(const std::string & name,const std::string & authType,const std::string & bundleName,bool isVisible)516 ErrCode AppAccount::SetOAuthTokenVisibility(
517 const std::string &name, const std::string &authType, const std::string &bundleName, bool isVisible)
518 {
519 RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
520 ErrCode ret = CheckTokenVisibilityParam(name, authType, bundleName);
521 if (ret != ERR_OK) {
522 return ret;
523 }
524 auto proxy = GetAppAccountProxy();
525 if (proxy == nullptr) {
526 return ERR_ACCOUNT_COMMON_GET_PROXY;
527 }
528 int32_t funcResult = 0;
529 auto idlErrCode = proxy->SetOAuthTokenVisibility(name, authType, bundleName, isVisible, funcResult);
530 return ConvertToAccountErrCode(idlErrCode, funcResult);
531 }
532
CheckAuthTokenVisibility(const std::string & name,const std::string & authType,const std::string & bundleName,bool & isVisible)533 ErrCode AppAccount::CheckAuthTokenVisibility(
534 const std::string &name, const std::string &authType, const std::string &bundleName, bool &isVisible)
535 {
536 ErrCode ret = CheckTokenVisibilityParam(name, authType, bundleName);
537 if (ret != ERR_OK) {
538 return ret;
539 }
540 auto proxy = GetAppAccountProxy();
541 if (proxy == nullptr) {
542 return ERR_ACCOUNT_COMMON_GET_PROXY;
543 }
544 int32_t funcResult = 0;
545 auto idlErrCode = proxy->CheckAuthTokenVisibility(name, authType, bundleName, isVisible, funcResult);
546 return ConvertToAccountErrCode(idlErrCode, funcResult);
547 }
548
CheckOAuthTokenVisibility(const std::string & name,const std::string & authType,const std::string & bundleName,bool & isVisible)549 ErrCode AppAccount::CheckOAuthTokenVisibility(
550 const std::string &name, const std::string &authType, const std::string &bundleName, bool &isVisible)
551 {
552 RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
553 ErrCode ret = CheckTokenVisibilityParam(name, authType, bundleName);
554 if (ret != ERR_OK) {
555 return ret;
556 }
557 auto proxy = GetAppAccountProxy();
558 if (proxy == nullptr) {
559 return ERR_ACCOUNT_COMMON_GET_PROXY;
560 }
561 int32_t funcResult = 0;
562 auto idlErrCode = proxy->CheckOAuthTokenVisibility(name, authType, bundleName, isVisible, funcResult);
563 return ConvertToAccountErrCode(idlErrCode, funcResult);
564 }
565
GetAuthenticatorInfo(const std::string & owner,AuthenticatorInfo & info)566 ErrCode AppAccount::GetAuthenticatorInfo(const std::string &owner, AuthenticatorInfo &info)
567 {
568 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE,
569 "Invalid owner. The length of the owner must be greater than 0 and less than 1025");
570 auto proxy = GetAppAccountProxy();
571 if (proxy == nullptr) {
572 return ERR_ACCOUNT_COMMON_GET_PROXY;
573 }
574 int32_t funcResult = 0;
575 auto idlErrCode = proxy->GetAuthenticatorInfo(owner, info, funcResult);
576 return ConvertToAccountErrCode(idlErrCode, funcResult);
577 }
578
GetAllOAuthTokens(const std::string & name,const std::string & owner,std::vector<OAuthTokenInfo> & tokenInfos)579 ErrCode AppAccount::GetAllOAuthTokens(
580 const std::string &name, const std::string &owner, std::vector<OAuthTokenInfo> &tokenInfos)
581 {
582 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
583 "Invalid name. The length of the name must be greater than 0 and less than 513");
584 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE,
585 "Invalid owner. The length of the owner must be greater than 0 and less than 1025");
586 auto proxy = GetAppAccountProxy();
587 if (proxy == nullptr) {
588 return ERR_ACCOUNT_COMMON_GET_PROXY;
589 }
590 tokenInfos.clear();
591 int32_t funcResult = 0;
592 auto idlErrCode = proxy->GetAllOAuthTokens(name, owner, tokenInfos, funcResult);
593 return ConvertToAccountErrCode(idlErrCode, funcResult);
594 }
595
GetOAuthList(const std::string & name,const std::string & authType,std::set<std::string> & oauthList)596 ErrCode AppAccount::GetOAuthList(
597 const std::string &name, const std::string &authType, std::set<std::string> &oauthList)
598 {
599 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
600 "Invalid name. The length of the name must be greater than 0 and less than 513");
601 RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
602 RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE,
603 "Invalid authType. The length of the authType must be less than 1025");
604 auto proxy = GetAppAccountProxy();
605 if (proxy == nullptr) {
606 return ERR_ACCOUNT_COMMON_GET_PROXY;
607 }
608 int32_t funcResult = 0;
609 auto idlErrCode = proxy->GetOAuthList(name, authType, oauthList, funcResult);
610 return ConvertToAccountErrCode(idlErrCode, funcResult);
611 }
612
GetAuthList(const std::string & name,const std::string & authType,std::set<std::string> & oauthList)613 ErrCode AppAccount::GetAuthList(
614 const std::string &name, const std::string &authType, std::set<std::string> &oauthList)
615 {
616 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
617 "Invalid name. The length of the name must be greater than 0 and less than 513");
618 RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE,
619 "Invalid authType. The length of the authType must be less than 1025");
620 auto proxy = GetAppAccountProxy();
621 if (proxy == nullptr) {
622 return ERR_ACCOUNT_COMMON_GET_PROXY;
623 }
624 int32_t funcResult = 0;
625 auto idlErrCode = proxy->GetAuthList(name, authType, oauthList, funcResult);
626 return ConvertToAccountErrCode(idlErrCode, funcResult);
627 }
628
GetAuthenticatorCallback(const std::string & sessionId,sptr<IRemoteObject> & callback)629 ErrCode AppAccount::GetAuthenticatorCallback(const std::string &sessionId, sptr<IRemoteObject> &callback)
630 {
631 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(sessionId, Constants::SESSION_ID_MAX_SIZE,
632 "Invalid sessionId. The length of the sessionId must be greater than 0 and less than 1025");
633 auto proxy = GetAppAccountProxy();
634 if (proxy == nullptr) {
635 return ERR_ACCOUNT_COMMON_GET_PROXY;
636 }
637 int32_t funcResult = 0;
638 auto idlErrCode = proxy->GetAuthenticatorCallback(sessionId, funcResult, callback);
639 return ConvertToAccountErrCode(idlErrCode, funcResult);
640 }
641
GetAllAccounts(const std::string & owner,std::vector<AppAccountInfo> & appAccounts)642 ErrCode AppAccount::GetAllAccounts(const std::string &owner, std::vector<AppAccountInfo> &appAccounts)
643 {
644 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE,
645 "Invalid owner. The length of the owner must be greater than 0 and less than 1025");
646 auto proxy = GetAppAccountProxy();
647 if (proxy == nullptr) {
648 return ERR_ACCOUNT_COMMON_GET_PROXY;
649 }
650 appAccounts.clear();
651 int32_t funcResult = 0;
652 auto idlErrCode = proxy->GetAllAccounts(owner, appAccounts, funcResult);
653 return ConvertToAccountErrCode(idlErrCode, funcResult);
654 }
655
CheckAppAccess(const std::string & name,const std::string & authorizedApp,bool & isAccessible)656 ErrCode AppAccount::CheckAppAccess(const std::string &name, const std::string &authorizedApp, bool &isAccessible)
657 {
658 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
659 "Invalid name. The length of the name must be greater than 0 and less than 513");
660 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(authorizedApp, Constants::BUNDLE_NAME_MAX_SIZE,
661 "Invalid bundleName. The length of the bundleName must be greater than 0 and less than 513");
662 auto proxy = GetAppAccountProxy();
663 if (proxy == nullptr) {
664 return ERR_ACCOUNT_COMMON_GET_PROXY;
665 }
666 int32_t funcResult = 0;
667 auto idlErrCode = proxy->CheckAppAccess(name, authorizedApp, isAccessible, funcResult);
668 return ConvertToAccountErrCode(idlErrCode, funcResult);
669 }
670
DeleteAccountCredential(const std::string & name,const std::string & credentialType)671 ErrCode AppAccount::DeleteAccountCredential(const std::string &name, const std::string &credentialType)
672 {
673 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
674 "Invalid name. The length of the name must be greater than 0 and less than 513");
675 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(credentialType, Constants::CREDENTIAL_TYPE_MAX_SIZE,
676 "Invalid credentialType. The length of the credentialType must be greater than 0 and less than 1025");
677 auto proxy = GetAppAccountProxy();
678 if (proxy == nullptr) {
679 return ERR_ACCOUNT_COMMON_GET_PROXY;
680 }
681 int32_t funcResult = 0;
682 auto idlErrCode = proxy->DeleteAccountCredential(name, credentialType, funcResult);
683 return ConvertToAccountErrCode(idlErrCode, funcResult);
684 }
685
SelectAccountsByOptions(const SelectAccountsOptions & options,const sptr<IAppAccountAuthenticatorCallback> & callback)686 ErrCode AppAccount::SelectAccountsByOptions(
687 const SelectAccountsOptions &options, const sptr<IAppAccountAuthenticatorCallback> &callback)
688 {
689 RETURN_IF_STRING_IS_OVERSIZE(options.allowedAccounts, Constants::MAX_ALLOWED_ARRAY_SIZE_INPUT,
690 "Invalid options.allowedAccounts."
691 "The length of the options.allowedAccounts must be less than 1025");
692 RETURN_IF_STRING_IS_OVERSIZE(options.allowedOwners, Constants::MAX_ALLOWED_ARRAY_SIZE_INPUT,
693 "Invalid options.allowedOwners."
694 "The length of the options.allowedOwners must be less than 1025");
695 RETURN_IF_STRING_IS_OVERSIZE(options.requiredLabels, Constants::MAX_ALLOWED_ARRAY_SIZE_INPUT,
696 "Invalid options.requiredLabels."
697 "The length of the options.requiredLabels must be less than 1025");
698 auto proxy = GetAppAccountProxy();
699 if (proxy == nullptr) {
700 return ERR_ACCOUNT_COMMON_GET_PROXY;
701 }
702 int32_t funcResult = 0;
703 auto idlErrCode = proxy->SelectAccountsByOptions(options, callback, funcResult);
704 return ConvertToAccountErrCode(idlErrCode, funcResult);
705 }
706
VerifyCredential(const std::string & name,const std::string & owner,const VerifyCredentialOptions & options,const sptr<IAppAccountAuthenticatorCallback> & callback)707 ErrCode AppAccount::VerifyCredential(const std::string &name, const std::string &owner,
708 const VerifyCredentialOptions &options, const sptr<IAppAccountAuthenticatorCallback> &callback)
709 {
710 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
711 "Invalid name. The length of the name must be greater than 0 and less than 513");
712 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE,
713 "Invalid owner. The length of the owner must be greater than 0 and less than 1025");
714 RETURN_IF_STRING_IS_OVERSIZE(options.credentialType, Constants::CREDENTIAL_TYPE_MAX_SIZE,
715 "Invalid options.credentialType. The length of the options.credentialType must be less than 1025");
716 RETURN_IF_STRING_IS_OVERSIZE(options.credential, Constants::CREDENTIAL_MAX_SIZE,
717 "Invalid options.credential. The length of the options.credential must be less than 1025");
718 auto proxy = GetAppAccountProxy();
719 if (proxy == nullptr) {
720 return ERR_ACCOUNT_COMMON_GET_PROXY;
721 }
722 int32_t funcResult = 0;
723 auto idlErrCode = proxy->VerifyCredential(name, owner, options, callback, funcResult);
724 return ConvertToAccountErrCode(idlErrCode, funcResult);
725 }
726
CheckAccountLabels(const std::string & name,const std::string & owner,const std::vector<std::string> & labels,const sptr<IAppAccountAuthenticatorCallback> & callback)727 ErrCode AppAccount::CheckAccountLabels(const std::string &name, const std::string &owner,
728 const std::vector<std::string> &labels, const sptr<IAppAccountAuthenticatorCallback> &callback)
729 {
730 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE,
731 "Invalid name. The length of the name must be greater than 0 and less than 513");
732 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE,
733 "Invalid owner. The length of the owner must be greater than 0 and less than 1025");
734 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(labels, Constants::MAX_ALLOWED_ARRAY_SIZE_INPUT,
735 "Invalid labels. The length of the labels must be greater than 0 and less than 1025");
736 auto proxy = GetAppAccountProxy();
737 if (proxy == nullptr) {
738 return ERR_ACCOUNT_COMMON_GET_PROXY;
739 }
740 int32_t funcResult = 0;
741 auto idlErrCode = proxy->CheckAccountLabels(name, owner, labels, callback, funcResult);
742 return ConvertToAccountErrCode(idlErrCode, funcResult);
743 }
744
SetAuthenticatorProperties(const std::string & owner,const SetPropertiesOptions & options,const sptr<IAppAccountAuthenticatorCallback> & callback)745 ErrCode AppAccount::SetAuthenticatorProperties(const std::string &owner,
746 const SetPropertiesOptions &options, const sptr<IAppAccountAuthenticatorCallback> &callback)
747 {
748 RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE,
749 "Invalid owner. The length of the owner must be greater than 0 and less than 1025");
750 auto proxy = GetAppAccountProxy();
751 if (proxy == nullptr) {
752 return ERR_ACCOUNT_COMMON_GET_PROXY;
753 }
754 int32_t funcResult = 0;
755 auto idlErrCode = proxy->SetAuthenticatorProperties(owner, options, callback, funcResult);
756 return ConvertToAccountErrCode(idlErrCode, funcResult);
757 }
758
GetAllAccessibleAccounts(std::vector<AppAccountInfo> & appAccounts)759 ErrCode AppAccount::GetAllAccessibleAccounts(std::vector<AppAccountInfo> &appAccounts)
760 {
761 auto proxy = GetAppAccountProxy();
762 if (proxy == nullptr) {
763 return ERR_ACCOUNT_COMMON_GET_PROXY;
764 }
765 appAccounts.clear();
766 int32_t funcResult = 0;
767 auto idlErrCode = proxy->GetAllAccessibleAccounts(appAccounts, funcResult);
768 return ConvertToAccountErrCode(idlErrCode, funcResult);
769 }
770
QueryAllAccessibleAccounts(const std::string & owner,std::vector<AppAccountInfo> & appAccounts)771 ErrCode AppAccount::QueryAllAccessibleAccounts(
772 const std::string &owner, std::vector<AppAccountInfo> &appAccounts)
773 {
774 auto proxy = GetAppAccountProxy();
775 if (proxy == nullptr) {
776 return ERR_ACCOUNT_COMMON_GET_PROXY;
777 }
778 RETURN_IF_STRING_IS_OVERSIZE(owner, Constants::OWNER_MAX_SIZE,
779 "Invalid owner. The length of the owner must be less than 1025");
780 appAccounts.clear();
781 int32_t funcResult = 0;
782 auto idlErrCode = proxy->QueryAllAccessibleAccounts(owner, appAccounts, funcResult);
783 return ConvertToAccountErrCode(idlErrCode, funcResult);
784 }
785
SubscribeAppAccount(const std::shared_ptr<AppAccountSubscriber> & subscriber)786 ErrCode AppAccount::SubscribeAppAccount(const std::shared_ptr<AppAccountSubscriber> &subscriber)
787 {
788 auto proxy = GetAppAccountProxy();
789 if (proxy == nullptr) {
790 return ERR_ACCOUNT_COMMON_GET_PROXY;
791 }
792
793 if (subscriber == nullptr) {
794 ACCOUNT_LOGE("subscriber is nullptr");
795 return ERR_APPACCOUNT_KIT_SUBSCRIBER_IS_NULLPTR;
796 }
797
798 sptr<IRemoteObject> appAccountEventListener = nullptr;
799 std::lock_guard<std::mutex> lock(eventListenersMutex_);
800 CreateAppAccountEventListener(subscriber, appAccountEventListener);
801
802 bool needNotifyService = false;
803 ErrCode result = AppAccountEventListener::GetInstance()->SubscribeAppAccount(subscriber, needNotifyService);
804 if (result != ERR_OK) {
805 return result;
806 }
807 if (!needNotifyService) {
808 return ERR_OK;
809 }
810 // Refresh to full owners
811 AppAccountSubscribeInfo subscribeInfo;
812 AppAccountEventListener::GetInstance()->GetRestoreData(subscribeInfo);
813 int32_t funcResult = 0;
814 result = proxy->SubscribeAppAccount(subscribeInfo, appAccountEventListener, funcResult);
815 result = ConvertToAccountErrCode(result, funcResult);
816 if (result != ERR_OK) {
817 std::vector<std::string> deleteOwners;
818 AppAccountEventListener::GetInstance()->UnsubscribeAppAccount(subscriber, needNotifyService, deleteOwners);
819 }
820 return result;
821 }
822
UnsubscribeAppAccount(const std::shared_ptr<AppAccountSubscriber> & subscriber)823 ErrCode AppAccount::UnsubscribeAppAccount(const std::shared_ptr<AppAccountSubscriber> &subscriber)
824 {
825 auto proxy = GetAppAccountProxy();
826 if (proxy == nullptr) {
827 return ERR_ACCOUNT_COMMON_GET_PROXY;
828 }
829 if (subscriber == nullptr) {
830 ACCOUNT_LOGE("subscriber is nullptr");
831 return ERR_APPACCOUNT_KIT_SUBSCRIBER_IS_NULLPTR;
832 }
833
834 std::lock_guard<std::mutex> lock(eventListenersMutex_);
835 bool needNotifyService = false;
836 std::vector<std::string> deleteOwners;
837 ErrCode result = AppAccountEventListener::GetInstance()->UnsubscribeAppAccount(
838 subscriber, needNotifyService, deleteOwners);
839 if (result != ERR_OK) {
840 return result;
841 }
842 if (!needNotifyService) {
843 return ERR_OK;
844 }
845 int32_t funcResult = 0;
846 result = proxy->UnsubscribeAppAccount(AppAccountEventListener::GetInstance()->AsObject(), deleteOwners, funcResult);
847 result = ConvertToAccountErrCode(result, funcResult);
848 if (result != ERR_OK) {
849 AppAccountEventListener::GetInstance()->SubscribeAppAccount(subscriber, needNotifyService);
850 }
851
852 return result;
853 }
854
ResetAppAccountProxy()855 ErrCode AppAccount::ResetAppAccountProxy()
856 {
857 std::lock_guard<std::mutex> lock(mutex_);
858 if ((proxy_ != nullptr) && (proxy_->AsObject() != nullptr)) {
859 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
860 }
861 proxy_ = nullptr;
862 return ERR_OK;
863 }
864
CheckSpecialCharacters(const std::string & name)865 ErrCode AppAccount::CheckSpecialCharacters(const std::string &name)
866 {
867 for (auto specialCharacter : Constants::SPECIAL_CHARACTERS) {
868 std::size_t found = name.find(specialCharacter);
869 if (found != std::string::npos) {
870 ACCOUNT_LOGE("found a special character, specialCharacter = %{public}c", specialCharacter);
871 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
872 }
873 }
874
875 return ERR_OK;
876 }
877
GetAppAccountProxy()878 sptr<IAppAccount> AppAccount::GetAppAccountProxy()
879 {
880 std::lock_guard<std::mutex> lock(mutex_);
881 if (proxy_ != nullptr) {
882 return proxy_;
883 }
884 sptr<IRemoteObject> object = OhosAccountKitsImpl::GetInstance().GetAppAccountService();
885 if (object == nullptr) {
886 ACCOUNT_LOGE("failed to get app account service");
887 return nullptr;
888 }
889 deathRecipient_ = new (std::nothrow) AppAccountDeathRecipient();
890 if (deathRecipient_ == nullptr) {
891 ACCOUNT_LOGE("failed to create app account death recipient");
892 return nullptr;
893 }
894 if (!object->AddDeathRecipient(deathRecipient_)) {
895 ACCOUNT_LOGE("failed to add app account death recipient");
896 deathRecipient_ = nullptr;
897 return nullptr;
898 }
899 proxy_ = iface_cast<IAppAccount>(object);
900 return proxy_;
901 }
902
CreateAppAccountEventListener(const std::shared_ptr<AppAccountSubscriber> & subscriber,sptr<IRemoteObject> & appAccountEventListener)903 ErrCode AppAccount::CreateAppAccountEventListener(
904 const std::shared_ptr<AppAccountSubscriber> &subscriber, sptr<IRemoteObject> &appAccountEventListener)
905 {
906 if (subscriber == nullptr) {
907 ACCOUNT_LOGE("subscriber is nullptr");
908 return SUBSCRIBE_FAILED;
909 }
910
911 appAccountEventListener = AppAccountEventListener::GetInstance()->AsObject();
912 return INITIAL_SUBSCRIPTION;
913 }
914 } // namespace AccountSA
915 } // namespace OHOS
916