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