1 /*
2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "user_auth_callback_service.h"
17
18 #include "callback_manager.h"
19 #include "iam_logger.h"
20 #include "iam_ptr.h"
21
22 #define LOG_TAG "USER_AUTH_SDK"
23
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
UserAuthCallbackService(const std::shared_ptr<AuthenticationCallback> & impl)27 UserAuthCallbackService::UserAuthCallbackService(const std::shared_ptr<AuthenticationCallback> &impl)
28 : authCallback_(impl),
29 iamHitraceHelper_(Common::MakeShared<UserIam::UserAuth::IamHitraceHelper>("UserAuth InnerKit"))
30 {
31 CallbackManager::CallbackAction action = [impl]() {
32 if (impl != nullptr) {
33 IAM_LOGI("user auth service death, auth callback return default result to caller");
34 Attributes extraInfo;
35 impl->OnResult(GENERAL_ERROR, extraInfo);
36 }
37 };
38 CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
39 }
40
UserAuthCallbackService(const std::shared_ptr<AuthenticationCallback> & impl,const std::shared_ptr<UserAuthModalClientCallback> & modalCallback)41 UserAuthCallbackService::UserAuthCallbackService(const std::shared_ptr<AuthenticationCallback> &impl,
42 const std::shared_ptr<UserAuthModalClientCallback> &modalCallback)
43 : authCallback_(impl), modalCallback_(modalCallback),
44 iamHitraceHelper_(Common::MakeShared<UserIam::UserAuth::IamHitraceHelper>("UserAuth InnerKit"))
45 {
46 CallbackManager::CallbackAction action = [impl]() {
47 if (impl != nullptr) {
48 IAM_LOGI("user auth service death, auth callback return default result to caller");
49 Attributes extraInfo;
50 impl->OnResult(GENERAL_ERROR, extraInfo);
51 }
52 };
53 CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
54 }
55
UserAuthCallbackService(const std::shared_ptr<IdentificationCallback> & impl)56 UserAuthCallbackService::UserAuthCallbackService(const std::shared_ptr<IdentificationCallback> &impl)
57 : identifyCallback_(impl),
58 iamHitraceHelper_(Common::MakeShared<UserIam::UserAuth::IamHitraceHelper>("UserAuth InnerKit"))
59 {
60 CallbackManager::CallbackAction action = [impl]() {
61 if (impl != nullptr) {
62 IAM_LOGI("user auth service death, identify callback return default result to caller");
63 Attributes extraInfo;
64 impl->OnResult(GENERAL_ERROR, extraInfo);
65 }
66 };
67 CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
68 }
69
UserAuthCallbackService(const std::shared_ptr<PrepareRemoteAuthCallback> & impl)70 UserAuthCallbackService::UserAuthCallbackService(const std::shared_ptr<PrepareRemoteAuthCallback> &impl)
71 : prepareRemoteAuthCallback_(impl),
72 iamHitraceHelper_(Common::MakeShared<UserIam::UserAuth::IamHitraceHelper>("UserAuth InnerKit"))
73 {
74 CallbackManager::CallbackAction action = [impl]() {
75 if (impl != nullptr) {
76 IAM_LOGI("user auth service death, prepare remote auth callback return default result to caller");
77 impl->OnResult(GENERAL_ERROR);
78 }
79 };
80 CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
81 }
82
~UserAuthCallbackService()83 UserAuthCallbackService::~UserAuthCallbackService()
84 {
85 IAM_LOGI("start");
86 iamHitraceHelper_= nullptr;
87 CallbackManager::GetInstance().RemoveCallback(reinterpret_cast<uintptr_t>(this));
88 }
89
OnResult(int32_t result,const Attributes & extraInfo)90 void UserAuthCallbackService::OnResult(int32_t result, const Attributes &extraInfo)
91 {
92 IAM_LOGI("start, result:%{public}d", result);
93 if (authCallback_ != nullptr) {
94 if (modalCallback_ != nullptr) {
95 IAM_LOGI("IsModalInit :%{public}d, IsModalDestroy :%{public}d", modalCallback_->IsModalInit(),
96 modalCallback_->IsModalDestroy());
97 if (modalCallback_->IsModalInit() && !modalCallback_->IsModalDestroy()) {
98 const uint32_t sleepTime = 100000;
99 usleep(sleepTime);
100 IAM_LOGI("process result continue");
101 }
102 }
103 authCallback_->OnResult(result, extraInfo);
104 } else if (identifyCallback_ != nullptr) {
105 identifyCallback_->OnResult(result, extraInfo);
106 } else if (prepareRemoteAuthCallback_ != nullptr) {
107 prepareRemoteAuthCallback_->OnResult(result);
108 } else {
109 IAM_LOGE("all callback is nullptr");
110 return;
111 }
112 iamHitraceHelper_= nullptr;
113 }
114
OnAcquireInfo(int32_t module,int32_t acquireInfo,const Attributes & extraInfo)115 void UserAuthCallbackService::OnAcquireInfo(int32_t module, int32_t acquireInfo, const Attributes &extraInfo)
116 {
117 IAM_LOGI("start, module:%{public}d acquireInfo:%{public}d", module, acquireInfo);
118 if (authCallback_ != nullptr) {
119 authCallback_->OnAcquireInfo(module, acquireInfo, extraInfo);
120 } else if (identifyCallback_ != nullptr) {
121 identifyCallback_->OnAcquireInfo(module, acquireInfo, extraInfo);
122 } else if (prepareRemoteAuthCallback_ != nullptr) {
123 IAM_LOGE("prepare remote auth callback not support acquire info");
124 } else {
125 IAM_LOGE("all callback is nullptr");
126 return;
127 }
128 }
129
GetExecutorPropertyCallbackService(const std::shared_ptr<GetPropCallback> & impl)130 GetExecutorPropertyCallbackService::GetExecutorPropertyCallbackService(const std::shared_ptr<GetPropCallback> &impl)
131 : getPropCallback_(impl)
132 {
133 CallbackManager::CallbackAction action = [impl]() {
134 if (impl != nullptr) {
135 IAM_LOGI("user auth service death, get prop callback return default result to caller");
136 Attributes extraInfo;
137 impl->OnResult(GENERAL_ERROR, extraInfo);
138 }
139 };
140 CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
141 }
142
~GetExecutorPropertyCallbackService()143 GetExecutorPropertyCallbackService::~GetExecutorPropertyCallbackService()
144 {
145 CallbackManager::GetInstance().RemoveCallback(reinterpret_cast<uintptr_t>(this));
146 }
147
OnGetExecutorPropertyResult(int32_t result,const Attributes & attributes)148 void GetExecutorPropertyCallbackService::OnGetExecutorPropertyResult(int32_t result, const Attributes &attributes)
149 {
150 IAM_LOGI("start, result:%{public}d", result);
151 if (getPropCallback_ == nullptr) {
152 IAM_LOGE("get prop callback is nullptr");
153 return;
154 }
155 getPropCallback_->OnResult(result, attributes);
156 }
157
SetExecutorPropertyCallbackService(const std::shared_ptr<SetPropCallback> & impl)158 SetExecutorPropertyCallbackService::SetExecutorPropertyCallbackService(const std::shared_ptr<SetPropCallback> &impl)
159 : setPropCallback_(impl)
160 {
161 CallbackManager::CallbackAction action = [impl]() {
162 if (impl != nullptr) {
163 IAM_LOGI("user auth service death, set prop callback return default result to caller");
164 Attributes extraInfo;
165 impl->OnResult(GENERAL_ERROR, extraInfo);
166 }
167 };
168 CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
169 }
170
~SetExecutorPropertyCallbackService()171 SetExecutorPropertyCallbackService::~SetExecutorPropertyCallbackService()
172 {
173 CallbackManager::GetInstance().RemoveCallback(reinterpret_cast<uintptr_t>(this));
174 }
175
OnSetExecutorPropertyResult(int32_t result)176 void SetExecutorPropertyCallbackService::OnSetExecutorPropertyResult(int32_t result)
177 {
178 IAM_LOGI("start, result:%{public}d", result);
179 if (setPropCallback_ == nullptr) {
180 IAM_LOGE("set prop callback is nullptr");
181 return;
182 }
183 Attributes attr;
184 setPropCallback_->OnResult(result, attr);
185 }
186 } // namespace UserAuth
187 } // namespace UserIam
188 } // namespace OHOS