1 /*
2 * Copyright (c) 2022 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 #include "account_proxy.h"
21 #include "domain_account_plugin_service.h"
22 #include "domain_account_proxy.h"
23 #include "domain_auth_callback_service.h"
24 #include "ohos_account_kits_impl.h"
25
26 namespace OHOS {
27 namespace AccountSA {
RegisterPlugin(const std::shared_ptr<DomainAccountPlugin> & plugin)28 ErrCode DomainAccountClient::RegisterPlugin(const std::shared_ptr<DomainAccountPlugin> &plugin)
29 {
30 if (plugin == nullptr) {
31 ACCOUNT_LOGE("plugin is nullptr");
32 return ERR_ACCOUNT_COMMON_INVALID_PARAMTER;
33 }
34 sptr<DomainAccountPluginService> pluginService = new (std::nothrow) DomainAccountPluginService(plugin);
35 if (pluginService == nullptr) {
36 ACCOUNT_LOGE("failed to create DomainAccountPluginService");
37 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
38 }
39 auto proxy = GetDomainAccountProxy();
40 if (proxy == nullptr) {
41 ACCOUNT_LOGE("failed to get domain account proxy");
42 return ERR_ACCOUNT_COMMON_GET_PROXY;
43 }
44 return proxy->RegisterPlugin(pluginService);
45 }
46
UnregisterPlugin()47 ErrCode DomainAccountClient::UnregisterPlugin()
48 {
49 auto proxy = GetDomainAccountProxy();
50 if (proxy == nullptr) {
51 ACCOUNT_LOGE("failed to get domain account proxy");
52 return ERR_ACCOUNT_COMMON_GET_PROXY;
53 }
54 return proxy->UnregisterPlugin();
55 }
56
Auth(const DomainAccountInfo & info,const std::vector<uint8_t> & password,const std::shared_ptr<DomainAuthCallback> & callback)57 ErrCode DomainAccountClient::Auth(const DomainAccountInfo &info, const std::vector<uint8_t> &password,
58 const std::shared_ptr<DomainAuthCallback> &callback)
59 {
60 if (callback == nullptr) {
61 ACCOUNT_LOGE("callback is nullptr");
62 return ERR_ACCOUNT_COMMON_INVALID_PARAMTER;
63 }
64 sptr<DomainAuthCallbackService> callbackService = new (std::nothrow) DomainAuthCallbackService(callback);
65 if (callbackService == nullptr) {
66 ACCOUNT_LOGE("failed to create DomainAuthCallbackService");
67 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
68 }
69 auto proxy = GetDomainAccountProxy();
70 if (proxy == nullptr) {
71 ACCOUNT_LOGE("failed to get domain account proxy");
72 return ERR_ACCOUNT_COMMON_GET_PROXY;
73 }
74 return proxy->Auth(info, password, callbackService);
75 }
76
AuthUser(int32_t userId,const std::vector<uint8_t> & password,const std::shared_ptr<DomainAuthCallback> & callback)77 ErrCode DomainAccountClient::AuthUser(int32_t userId, const std::vector<uint8_t> &password,
78 const std::shared_ptr<DomainAuthCallback> &callback)
79 {
80 if (callback == nullptr) {
81 ACCOUNT_LOGE("callback is nullptr");
82 return ERR_ACCOUNT_COMMON_INVALID_PARAMTER;
83 }
84 sptr<DomainAuthCallbackService> callbackService = new (std::nothrow) DomainAuthCallbackService(callback);
85 if (callbackService == nullptr) {
86 ACCOUNT_LOGE("failed to create DomainAuthCallbackService");
87 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
88 }
89 auto proxy = GetDomainAccountProxy();
90 if (proxy == nullptr) {
91 ACCOUNT_LOGE("failed to get domain account proxy");
92 return ERR_ACCOUNT_COMMON_GET_PROXY;
93 }
94 return proxy->AuthUser(userId, password, callbackService);
95 }
96
ResetDomainAccountProxy(const wptr<IRemoteObject> & remote)97 void DomainAccountClient::ResetDomainAccountProxy(const wptr<IRemoteObject>& remote)
98 {
99 std::lock_guard<std::mutex> lock(mutex_);
100 if (proxy_ == nullptr) {
101 ACCOUNT_LOGD("proxy is nullptr");
102 return;
103 }
104 auto serviceRemote = proxy_->AsObject();
105 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
106 serviceRemote->RemoveDeathRecipient(deathRecipient_);
107 }
108 proxy_ = nullptr;
109 deathRecipient_ = nullptr;
110 }
111
OnRemoteDied(const wptr<IRemoteObject> & remote)112 void DomainAccountClient::DomainAccountDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
113 {
114 if (remote == nullptr) {
115 ACCOUNT_LOGE("remote is nullptr");
116 return;
117 }
118 DomainAccountClient::GetInstance().ResetDomainAccountProxy(remote);
119 }
120
GetDomainAccountProxy()121 sptr<IDomainAccount> DomainAccountClient::GetDomainAccountProxy()
122 {
123 std::lock_guard<std::mutex> lock(mutex_);
124 if (proxy_ != nullptr) {
125 return proxy_;
126 }
127 sptr<IRemoteObject> object = DelayedRefSingleton<OhosAccountKitsImpl>::GetInstance().GetDomainAccountService();
128 if (object == nullptr) {
129 ACCOUNT_LOGE("failed to get domain account service");
130 return nullptr;
131 }
132 deathRecipient_ = new (std::nothrow) DomainAccountDeathRecipient();
133 if (deathRecipient_ == nullptr) {
134 ACCOUNT_LOGE("failed to create domain account death recipient");
135 return nullptr;
136 }
137 if (!object->AddDeathRecipient(deathRecipient_)) {
138 ACCOUNT_LOGE("failed to add app account death recipient");
139 deathRecipient_ = nullptr;
140 return nullptr;
141 }
142 proxy_ = iface_cast<IDomainAccount>(object);
143 return proxy_;
144 }
145 } // namespace AccountSA
146 } // namespace OHOS
147