1 /*
2 * Copyright (c) 2022-2023 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 "domain_account_client.h"
17
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20 #ifdef SUPPORT_DOMAIN_ACCOUNTS
21 #include "domain_account_callback_adapters.h"
22 #include "domain_account_plugin_service.h"
23 #include "domain_account_proxy.h"
24 #include "ohos_account_kits_impl.h"
25 #include "system_ability_definition.h"
26 #endif // SUPPORT_DOMAIN_ACCOUNTS
27
28 namespace OHOS {
29 namespace AccountSA {
GetInstance()30 DomainAccountClient &DomainAccountClient::GetInstance()
31 {
32 static DomainAccountClient *instance = new (std::nothrow) DomainAccountClient();
33 return *instance;
34 }
35
36 #ifdef SUPPORT_DOMAIN_ACCOUNTS
callbackFunc()37 std::function<void(int32_t, const std::string &)> callbackFunc()
38 {
39 return [](int32_t systemAbilityId, const std::string &deviceId) {
40 if (systemAbilityId == SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN) {
41 DomainAccountClient::GetInstance().RestoreListenerRecords();
42 DomainAccountClient::GetInstance().RestorePlugin();
43 }
44 };
45 }
46 #endif // SUPPORT_DOMAIN_ACCOUNTS
47
DomainAccountClient()48 DomainAccountClient::DomainAccountClient()
49 {
50 #ifdef SUPPORT_DOMAIN_ACCOUNTS
51 (void)OhosAccountKitsImpl::GetInstance().SubscribeSystemAbility(callbackFunc());
52 #endif // SUPPORT_DOMAIN_ACCOUNTS
53 }
54
RegisterPlugin(const std::shared_ptr<DomainAccountPlugin> & plugin)55 ErrCode DomainAccountClient::RegisterPlugin(const std::shared_ptr<DomainAccountPlugin> &plugin)
56 {
57 #ifdef SUPPORT_DOMAIN_ACCOUNTS
58 if (plugin == nullptr) {
59 ACCOUNT_LOGE("plugin is nullptr");
60 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
61 }
62 sptr<DomainAccountPluginService> pluginService = new (std::nothrow) DomainAccountPluginService(plugin);
63 if (pluginService == nullptr) {
64 ACCOUNT_LOGE("failed to create DomainAccountPluginService");
65 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
66 }
67 auto proxy = GetDomainAccountProxy();
68 if (proxy == nullptr) {
69 ACCOUNT_LOGE("failed to get domain account proxy");
70 return ERR_ACCOUNT_COMMON_GET_PROXY;
71 }
72 ErrCode result = proxy->RegisterPlugin(pluginService);
73 if (result == ERR_OK) {
74 std::lock_guard<std::mutex> lock(pluginServiceMutex_);
75 pluginService_ = pluginService;
76 }
77 return result;
78 #else
79 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
80 #endif // SUPPORT_DOMAIN_ACCOUNTS
81 }
82
UnregisterPlugin()83 ErrCode DomainAccountClient::UnregisterPlugin()
84 {
85 #ifdef SUPPORT_DOMAIN_ACCOUNTS
86 auto proxy = GetDomainAccountProxy();
87 if (proxy == nullptr) {
88 ACCOUNT_LOGE("failed to get domain account proxy");
89 return ERR_ACCOUNT_COMMON_GET_PROXY;
90 }
91 ErrCode ret = proxy->UnregisterPlugin();
92 if (ret == ERR_OK) {
93 std::lock_guard<std::mutex> lock(pluginServiceMutex_);
94 pluginService_ = nullptr;
95 }
96 return ret;
97 #else
98 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
99 #endif // SUPPORT_DOMAIN_ACCOUNTS
100 }
101
102 #ifdef SUPPORT_DOMAIN_ACCOUNTS
AuthProxyInit(const std::shared_ptr<DomainAccountCallback> & callback,sptr<DomainAccountCallbackService> & callbackService,sptr<IDomainAccount> & proxy)103 ErrCode DomainAccountClient::AuthProxyInit(const std::shared_ptr<DomainAccountCallback> &callback,
104 sptr<DomainAccountCallbackService> &callbackService, sptr<IDomainAccount> &proxy)
105 {
106 if (callback == nullptr) {
107 ACCOUNT_LOGE("callback is nullptr");
108 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
109 }
110 callbackService = new (std::nothrow) DomainAccountCallbackService(callback);
111 if (callbackService == nullptr) {
112 ACCOUNT_LOGE("failed to create DomainAccountCallbackService");
113 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
114 }
115 proxy = GetDomainAccountProxy();
116 if (proxy == nullptr) {
117 ACCOUNT_LOGE("failed to get domain account proxy");
118 return ERR_ACCOUNT_COMMON_GET_PROXY;
119 }
120 return ERR_OK;
121 }
122 #endif // SUPPORT_DOMAIN_ACCOUNTS
123
GetAccessToken(const DomainAccountInfo & info,const AAFwk::WantParams & parameters,const std::shared_ptr<GetAccessTokenCallback> & callback)124 ErrCode DomainAccountClient::GetAccessToken(const DomainAccountInfo &info, const AAFwk::WantParams ¶meters,
125 const std::shared_ptr<GetAccessTokenCallback> &callback)
126 {
127 #ifdef SUPPORT_DOMAIN_ACCOUNTS
128 if (callback == nullptr) {
129 ACCOUNT_LOGE("callback is nullptr");
130 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
131 }
132 auto callbackPtr = std::make_shared<GetAccessTokenCallbackAdapter>(callback);
133 sptr<DomainAccountCallbackService> callbackService = new (std::nothrow) DomainAccountCallbackService(callbackPtr);
134 if (callbackService == nullptr) {
135 ACCOUNT_LOGE("failed to new callback service");
136 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
137 }
138 auto proxy = GetDomainAccountProxy();
139 if (proxy == nullptr) {
140 ACCOUNT_LOGE("failed to get domain account proxy");
141 return ERR_ACCOUNT_COMMON_GET_PROXY;
142 }
143 return proxy->GetAccessToken(info, parameters, callbackService);
144 #else
145 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
146 #endif // SUPPORT_DOMAIN_ACCOUNTS
147 }
148
HasAccount(const DomainAccountInfo & info,const std::shared_ptr<DomainAccountCallback> & callback)149 ErrCode DomainAccountClient::HasAccount(
150 const DomainAccountInfo &info, const std::shared_ptr<DomainAccountCallback> &callback)
151 {
152 #ifdef SUPPORT_DOMAIN_ACCOUNTS
153 if (callback == nullptr) {
154 ACCOUNT_LOGE("callback is nullptr");
155 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
156 }
157 sptr<DomainAccountCallbackService> callbackService = new (std::nothrow) DomainAccountCallbackService(callback);
158 if (callbackService == nullptr) {
159 ACCOUNT_LOGE("failed to check domain account callback service");
160 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
161 }
162 auto proxy = GetDomainAccountProxy();
163 if (proxy == nullptr) {
164 ACCOUNT_LOGE("failed to get domain account proxy");
165 return ERR_ACCOUNT_COMMON_GET_PROXY;
166 }
167 return proxy->HasDomainAccount(info, callbackService);
168 #else
169 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
170 #endif // SUPPORT_DOMAIN_ACCOUNTS
171 }
172
Auth(const DomainAccountInfo & info,const std::vector<uint8_t> & password,const std::shared_ptr<DomainAccountCallback> & callback)173 ErrCode DomainAccountClient::Auth(const DomainAccountInfo &info, const std::vector<uint8_t> &password,
174 const std::shared_ptr<DomainAccountCallback> &callback)
175 {
176 #ifdef SUPPORT_DOMAIN_ACCOUNTS
177 sptr<DomainAccountCallbackService> callbackService = nullptr;
178 sptr<IDomainAccount> proxy = nullptr;
179 ErrCode result = AuthProxyInit(callback, callbackService, proxy);
180 if (result != ERR_OK) {
181 return result;
182 }
183 return proxy->Auth(info, password, callbackService);
184 #else
185 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
186 #endif // SUPPORT_DOMAIN_ACCOUNTS
187 }
188
AuthUser(int32_t userId,const std::vector<uint8_t> & password,const std::shared_ptr<DomainAccountCallback> & callback)189 ErrCode DomainAccountClient::AuthUser(int32_t userId, const std::vector<uint8_t> &password,
190 const std::shared_ptr<DomainAccountCallback> &callback)
191 {
192 #ifdef SUPPORT_DOMAIN_ACCOUNTS
193 sptr<DomainAccountCallbackService> callbackService = nullptr;
194 sptr<IDomainAccount> proxy = nullptr;
195 ErrCode result = AuthProxyInit(callback, callbackService, proxy);
196 if (result != ERR_OK) {
197 return result;
198 }
199 return proxy->AuthUser(userId, password, callbackService);
200 #else
201 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
202 #endif // SUPPORT_DOMAIN_ACCOUNTS
203 }
204
AuthWithPopup(int32_t userId,const std::shared_ptr<DomainAccountCallback> & callback)205 ErrCode DomainAccountClient::AuthWithPopup(int32_t userId, const std::shared_ptr<DomainAccountCallback> &callback)
206 {
207 #ifdef SUPPORT_DOMAIN_ACCOUNTS
208 sptr<DomainAccountCallbackService> callbackService = nullptr;
209 sptr<IDomainAccount> proxy = nullptr;
210 ErrCode result = AuthProxyInit(callback, callbackService, proxy);
211 if (result != ERR_OK) {
212 return result;
213 }
214 return proxy->AuthWithPopup(userId, callbackService);
215 #else
216 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
217 #endif // SUPPORT_DOMAIN_ACCOUNTS
218 }
219
UpdateAccountToken(const DomainAccountInfo & info,const std::vector<uint8_t> & token)220 ErrCode DomainAccountClient::UpdateAccountToken(const DomainAccountInfo &info, const std::vector<uint8_t> &token)
221 {
222 #ifdef SUPPORT_DOMAIN_ACCOUNTS
223 auto proxy = GetDomainAccountProxy();
224 if (proxy == nullptr) {
225 ACCOUNT_LOGE("failed to get domain account proxy");
226 return ERR_ACCOUNT_COMMON_GET_PROXY;
227 }
228 return proxy->UpdateAccountToken(info, token);
229 #else
230 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
231 #endif // SUPPORT_DOMAIN_ACCOUNTS
232 }
233
IsAuthenticationExpired(const DomainAccountInfo & info,bool & isExpired)234 ErrCode DomainAccountClient::IsAuthenticationExpired(const DomainAccountInfo &info, bool &isExpired)
235 {
236 #ifdef SUPPORT_DOMAIN_ACCOUNTS
237 isExpired = true;
238 auto proxy = GetDomainAccountProxy();
239 if (proxy == nullptr) {
240 ACCOUNT_LOGE("Get domain account proxy failed.");
241 return ERR_ACCOUNT_COMMON_GET_PROXY;
242 }
243 return proxy->IsAuthenticationExpired(info, isExpired);
244 #else
245 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
246 #endif // SUPPORT_DOMAIN_ACCOUNTS
247 }
248
249 #ifdef SUPPORT_DOMAIN_ACCOUNTS
ResetDomainAccountProxy(const wptr<IRemoteObject> & remote)250 void DomainAccountClient::ResetDomainAccountProxy(const wptr<IRemoteObject>& remote)
251 {
252 std::lock_guard<std::mutex> lock(mutex_);
253 if (proxy_ == nullptr) {
254 ACCOUNT_LOGE("Proxy is nullptr");
255 return;
256 }
257 auto serviceRemote = proxy_->AsObject();
258 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
259 serviceRemote->RemoveDeathRecipient(deathRecipient_);
260 }
261 proxy_ = nullptr;
262 deathRecipient_ = nullptr;
263 }
264 #endif // SUPPORT_DOMAIN_ACCOUNTS
265
GetAccountStatus(const DomainAccountInfo & info,DomainAccountStatus & status)266 ErrCode DomainAccountClient::GetAccountStatus(const DomainAccountInfo &info, DomainAccountStatus &status)
267 {
268 #ifdef SUPPORT_DOMAIN_ACCOUNTS
269 auto proxy = GetDomainAccountProxy();
270 if (proxy == nullptr) {
271 ACCOUNT_LOGE("failed to get domain account proxy");
272 return ERR_ACCOUNT_COMMON_GET_PROXY;
273 }
274 int32_t statusResult;
275 auto errCode = proxy->GetAccountStatus(info, statusResult);
276 status = static_cast<DomainAccountStatus>(statusResult);
277 return errCode;
278 #else
279 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
280 #endif // SUPPORT_DOMAIN_ACCOUNTS
281 }
282
GetDomainAccountInfo(const DomainAccountInfo & info,const std::shared_ptr<DomainAccountCallback> & callback)283 ErrCode DomainAccountClient::GetDomainAccountInfo(
284 const DomainAccountInfo &info, const std::shared_ptr<DomainAccountCallback> &callback)
285 {
286 #ifdef SUPPORT_DOMAIN_ACCOUNTS
287 if (callback == nullptr) {
288 ACCOUNT_LOGE("callback is nullptr");
289 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
290 }
291 sptr<DomainAccountCallbackService> callbackService = new (std::nothrow) DomainAccountCallbackService(callback);
292 auto proxy = GetDomainAccountProxy();
293 if (proxy == nullptr) {
294 ACCOUNT_LOGE("failed to get domain account proxy");
295 return ERR_ACCOUNT_COMMON_GET_PROXY;
296 }
297 return proxy->GetDomainAccountInfo(info, callbackService);
298 #else
299 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
300 #endif // SUPPORT_DOMAIN_ACCOUNTS
301 }
302
UpdateAccountInfo(const DomainAccountInfo & oldAccountInfo,const DomainAccountInfo & newAccountInfo)303 ErrCode DomainAccountClient::UpdateAccountInfo(
304 const DomainAccountInfo &oldAccountInfo, const DomainAccountInfo &newAccountInfo)
305 {
306 #ifdef SUPPORT_DOMAIN_ACCOUNTS
307 auto proxy = GetDomainAccountProxy();
308 if (proxy == nullptr) {
309 ACCOUNT_LOGE("Failed to get domain account proxy");
310 return ERR_ACCOUNT_COMMON_GET_PROXY;
311 }
312 return proxy->UpdateAccountInfo(oldAccountInfo, newAccountInfo);
313 #else
314 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
315 #endif // SUPPORT_DOMAIN_ACCOUNTS
316 }
317
RegisterAccountStatusListener(const std::shared_ptr<DomainAccountStatusListener> & listener)318 ErrCode DomainAccountClient::RegisterAccountStatusListener(const std::shared_ptr<DomainAccountStatusListener> &listener)
319 {
320 #ifdef SUPPORT_DOMAIN_ACCOUNTS
321 if (listener == nullptr) {
322 ACCOUNT_LOGE("callback is nullptr");
323 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
324 }
325 std::lock_guard<std::mutex> lock(recordMutex_);
326 if (listenerManager_ == nullptr) {
327 listenerManager_ = std::make_shared<DomainAccountStatusListenerManager>();
328 }
329 if (!listenerManager_->IsRecordEmpty()) {
330 listenerManager_->InsertRecord(listener);
331 return ERR_OK;
332 }
333 if (callback_ == nullptr) {
334 callback_ = new (std::nothrow) DomainAccountCallbackService(listenerManager_);
335 if (callback_ == nullptr) {
336 ACCOUNT_LOGE("failed to check domain account callback service");
337 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
338 }
339 }
340 auto proxy = GetDomainAccountProxy();
341 if (proxy == nullptr) {
342 ACCOUNT_LOGE("failed to get domain account proxy");
343 return ERR_ACCOUNT_COMMON_GET_PROXY;
344 }
345 ErrCode result = proxy->RegisterAccountStatusListener(callback_);
346 if (result == ERR_OK) {
347 listenerManager_->InsertRecord(listener);
348 }
349 return result;
350 #else
351 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
352 #endif // SUPPORT_DOMAIN_ACCOUNTS
353 }
354
UnregisterAccountStatusListener(const std::shared_ptr<DomainAccountStatusListener> & listener)355 ErrCode DomainAccountClient::UnregisterAccountStatusListener(
356 const std::shared_ptr<DomainAccountStatusListener> &listener)
357 {
358 #ifdef SUPPORT_DOMAIN_ACCOUNTS
359 if (listener == nullptr) {
360 ACCOUNT_LOGE("callback is nullptr");
361 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
362 }
363 std::lock_guard<std::mutex> lock(recordMutex_);
364 if (listenerManager_ == nullptr) {
365 ACCOUNT_LOGE("listenerManager_ is nullptr");
366 return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
367 }
368 auto proxy = GetDomainAccountProxy();
369 if (proxy == nullptr) {
370 ACCOUNT_LOGE("failed to get domain account proxy");
371 return ERR_ACCOUNT_COMMON_GET_PROXY;
372 }
373 listenerManager_->RemoveRecord(listener);
374 if (!listenerManager_->IsRecordEmpty()) {
375 return ERR_OK;
376 }
377 ErrCode result = proxy->UnregisterAccountStatusListener(callback_);
378 if (result != ERR_OK) {
379 listenerManager_->InsertRecord(listener);
380 return result;
381 }
382 listenerManager_ = nullptr;
383 callback_ = nullptr;
384 return ERR_OK;
385 #else
386 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
387 #endif // SUPPORT_DOMAIN_ACCOUNTS
388 }
389
AddServerConfig(const std::string & parameters,DomainServerConfig & config)390 ErrCode DomainAccountClient::AddServerConfig(const std::string ¶meters, DomainServerConfig &config)
391 {
392 #ifdef SUPPORT_DOMAIN_ACCOUNTS
393 auto proxy = GetDomainAccountProxy();
394 if (proxy == nullptr) {
395 ACCOUNT_LOGE("Failed to get domain account proxy.");
396 return ERR_ACCOUNT_COMMON_GET_PROXY;
397 }
398 return proxy->AddServerConfig(parameters, config);
399 #else
400 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
401 #endif // SUPPORT_DOMAIN_ACCOUNTS
402 }
403
RemoveServerConfig(const std::string & configId)404 ErrCode DomainAccountClient::RemoveServerConfig(const std::string &configId)
405 {
406 #ifdef SUPPORT_DOMAIN_ACCOUNTS
407 auto proxy = GetDomainAccountProxy();
408 if (proxy == nullptr) {
409 ACCOUNT_LOGE("Failed to get domain account proxy.");
410 return ERR_ACCOUNT_COMMON_GET_PROXY;
411 }
412 return proxy->RemoveServerConfig(configId);
413 #else
414 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
415 #endif // SUPPORT_DOMAIN_ACCOUNTS
416 }
417
UpdateServerConfig(const std::string & configId,const std::string & parameters,DomainServerConfig & config)418 ErrCode DomainAccountClient::UpdateServerConfig(const std::string &configId, const std::string ¶meters,
419 DomainServerConfig &config)
420 {
421 #ifdef SUPPORT_DOMAIN_ACCOUNTS
422 auto proxy = GetDomainAccountProxy();
423 if (proxy == nullptr) {
424 ACCOUNT_LOGE("Failed to get domain account proxy.");
425 return ERR_ACCOUNT_COMMON_GET_PROXY;
426 }
427 return proxy->UpdateServerConfig(configId, parameters, config);
428 #else
429 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
430 #endif // SUPPORT_DOMAIN_ACCOUNTS
431 }
432
GetServerConfig(const std::string & configId,DomainServerConfig & config)433 ErrCode DomainAccountClient::GetServerConfig(const std::string &configId, DomainServerConfig &config)
434 {
435 #ifdef SUPPORT_DOMAIN_ACCOUNTS
436 auto proxy = GetDomainAccountProxy();
437 if (proxy == nullptr) {
438 ACCOUNT_LOGE("Failed to get domain account proxy.");
439 return ERR_ACCOUNT_COMMON_GET_PROXY;
440 }
441 return proxy->GetServerConfig(configId, config);
442 #else
443 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
444 #endif // SUPPORT_DOMAIN_ACCOUNTS
445 }
446
GetAllServerConfigs(std::vector<DomainServerConfig> & configs)447 ErrCode DomainAccountClient::GetAllServerConfigs(std::vector<DomainServerConfig> &configs)
448 {
449 #ifdef SUPPORT_DOMAIN_ACCOUNTS
450 auto proxy = GetDomainAccountProxy();
451 if (proxy == nullptr) {
452 ACCOUNT_LOGE("Failed to get domain account proxy.");
453 return ERR_ACCOUNT_COMMON_GET_PROXY;
454 }
455 return proxy->GetAllServerConfigs(configs);
456 #else
457 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
458 #endif // SUPPORT_DOMAIN_ACCOUNTS
459 }
460
GetAccountServerConfig(const DomainAccountInfo & info,DomainServerConfig & config)461 ErrCode DomainAccountClient::GetAccountServerConfig(const DomainAccountInfo &info, DomainServerConfig &config)
462 {
463 #ifdef SUPPORT_DOMAIN_ACCOUNTS
464 auto proxy = GetDomainAccountProxy();
465 if (proxy == nullptr) {
466 ACCOUNT_LOGE("Failed to get domain account proxy.");
467 return ERR_ACCOUNT_COMMON_GET_PROXY;
468 }
469 return proxy->GetAccountServerConfig(info, config);
470 #else
471 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
472 #endif // SUPPORT_DOMAIN_ACCOUNTS
473 }
474
SetAccountPolicy(const DomainAccountInfo & info,const std::string & policy)475 ErrCode DomainAccountClient::SetAccountPolicy(const DomainAccountInfo &info, const std::string &policy)
476 {
477 #ifdef SUPPORT_DOMAIN_ACCOUNTS
478 auto proxy = GetDomainAccountProxy();
479 if (proxy == nullptr) {
480 ACCOUNT_LOGE("Failed to get domain account proxy.");
481 return ERR_ACCOUNT_COMMON_GET_PROXY;
482 }
483 return proxy->SetAccountPolicy(info, policy);
484 #else
485 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
486 #endif // SUPPORT_DOMAIN_ACCOUNTS
487 }
488
GetAccountPolicy(const DomainAccountInfo & info,std::string & policy)489 ErrCode DomainAccountClient::GetAccountPolicy(const DomainAccountInfo &info, std::string &policy)
490 {
491 #ifdef SUPPORT_DOMAIN_ACCOUNTS
492 auto proxy = GetDomainAccountProxy();
493 if (proxy == nullptr) {
494 ACCOUNT_LOGE("Failed to get domain account proxy.");
495 return ERR_ACCOUNT_COMMON_GET_PROXY;
496 }
497 return proxy->GetAccountPolicy(info, policy);
498 #else
499 return ERR_DOMAIN_ACCOUNT_NOT_SUPPORT;
500 #endif // SUPPORT_DOMAIN_ACCOUNTS
501 }
502
503 #ifdef SUPPORT_DOMAIN_ACCOUNTS
RestoreListenerRecords()504 void DomainAccountClient::RestoreListenerRecords()
505 {
506 std::lock_guard<std::mutex> lock(recordMutex_);
507 if (listenerManager_ == nullptr) {
508 ACCOUNT_LOGI("listenerManager_ is nullptr");
509 return;
510 }
511 sptr<IDomainAccount> proxy = GetDomainAccountProxy();
512 if (proxy == nullptr) {
513 ACCOUNT_LOGE("proxy is nullptr");
514 return;
515 }
516 if (!listenerManager_->IsRecordEmpty()) {
517 (void)proxy->RegisterAccountStatusListener(callback_);
518 }
519 }
520
RestorePlugin()521 void DomainAccountClient::RestorePlugin()
522 {
523 std::lock_guard<std::mutex> lock(pluginServiceMutex_);
524 if (pluginService_ == nullptr) {
525 ACCOUNT_LOGI("pluginService_ is nullptr");
526 return;
527 }
528 sptr<IDomainAccount> proxy = GetDomainAccountProxy();
529 if (proxy == nullptr) {
530 ACCOUNT_LOGE("proxy is nullptr");
531 return;
532 }
533 (void)proxy->RegisterPlugin(pluginService_);
534 }
535
OnRemoteDied(const wptr<IRemoteObject> & remote)536 void DomainAccountClient::DomainAccountDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
537 {
538 if (remote == nullptr) {
539 ACCOUNT_LOGE("remote is nullptr");
540 return;
541 }
542 DomainAccountClient::GetInstance().ResetDomainAccountProxy(remote);
543 }
544
GetDomainAccountProxy()545 sptr<IDomainAccount> DomainAccountClient::GetDomainAccountProxy()
546 {
547 std::lock_guard<std::mutex> lock(mutex_);
548 if (proxy_ != nullptr) {
549 return proxy_;
550 }
551 sptr<IRemoteObject> object = OhosAccountKitsImpl::GetInstance().GetDomainAccountService();
552 if (object == nullptr) {
553 ACCOUNT_LOGE("failed to get domain account service");
554 return nullptr;
555 }
556 deathRecipient_ = new (std::nothrow) DomainAccountDeathRecipient();
557 if (deathRecipient_ == nullptr) {
558 ACCOUNT_LOGE("failed to create domain account death recipient");
559 return nullptr;
560 }
561
562 if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
563 ACCOUNT_LOGE("Failed to add death recipient");
564 deathRecipient_ = nullptr;
565 }
566 proxy_ = iface_cast<IDomainAccount>(object);
567 return proxy_;
568 }
569 #endif // SUPPORT_DOMAIN_ACCOUNTS
570 } // namespace AccountSA
571 } // namespace OHOS
572