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 "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_LABEL UserIam::Common::LABEL_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<IdentificationCallback> & impl)41 UserAuthCallbackService::UserAuthCallbackService(const std::shared_ptr<IdentificationCallback> &impl)
42 : identifyCallback_(impl),
43 iamHitraceHelper_(Common::MakeShared<UserIam::UserAuth::IamHitraceHelper>("UserAuth InnerKit"))
44 {
45 CallbackManager::CallbackAction action = [impl]() {
46 if (impl != nullptr) {
47 IAM_LOGI("user auth service death, identify callback return default result to caller");
48 Attributes extraInfo;
49 impl->OnResult(GENERAL_ERROR, extraInfo);
50 }
51 };
52 CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
53 }
54
~UserAuthCallbackService()55 UserAuthCallbackService::~UserAuthCallbackService()
56 {
57 CallbackManager::GetInstance().RemoveCallback(reinterpret_cast<uintptr_t>(this));
58 }
59
OnResult(int32_t result,const Attributes & extraInfo)60 void UserAuthCallbackService::OnResult(int32_t result, const Attributes &extraInfo)
61 {
62 IAM_LOGI("start, result:%{public}d", result);
63 if (authCallback_ != nullptr) {
64 authCallback_->OnResult(result, extraInfo);
65 } else if (identifyCallback_ != nullptr) {
66 identifyCallback_->OnResult(result, extraInfo);
67 } else {
68 IAM_LOGE("both auth and identify callback is nullptr");
69 return;
70 }
71 iamHitraceHelper_= nullptr;
72 }
73
OnAcquireInfo(int32_t module,int32_t acquireInfo,const Attributes & extraInfo)74 void UserAuthCallbackService::OnAcquireInfo(int32_t module, int32_t acquireInfo, const Attributes &extraInfo)
75 {
76 IAM_LOGI("start, module:%{public}d acquireInfo:%{public}d", module, acquireInfo);
77 if (authCallback_ != nullptr) {
78 authCallback_->OnAcquireInfo(module, acquireInfo, extraInfo);
79 } else if (identifyCallback_ != nullptr) {
80 identifyCallback_->OnAcquireInfo(module, acquireInfo, extraInfo);
81 } else {
82 IAM_LOGE("both auth and identify callback is nullptr");
83 return;
84 }
85 }
86
GetExecutorPropertyCallbackService(const std::shared_ptr<GetPropCallback> & impl)87 GetExecutorPropertyCallbackService::GetExecutorPropertyCallbackService(const std::shared_ptr<GetPropCallback> &impl)
88 : getPropCallback_(impl)
89 {
90 CallbackManager::CallbackAction action = [impl]() {
91 if (impl != nullptr) {
92 IAM_LOGI("user auth service death, get prop callback return default result to caller");
93 Attributes extraInfo;
94 impl->OnResult(GENERAL_ERROR, extraInfo);
95 }
96 };
97 CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
98 }
99
~GetExecutorPropertyCallbackService()100 GetExecutorPropertyCallbackService::~GetExecutorPropertyCallbackService()
101 {
102 CallbackManager::GetInstance().RemoveCallback(reinterpret_cast<uintptr_t>(this));
103 }
104
OnGetExecutorPropertyResult(int32_t result,const Attributes & attributes)105 void GetExecutorPropertyCallbackService::OnGetExecutorPropertyResult(int32_t result, const Attributes &attributes)
106 {
107 IAM_LOGI("start, result:%{public}d", result);
108 if (getPropCallback_ == nullptr) {
109 IAM_LOGE("get prop callback is nullptr");
110 return;
111 }
112 getPropCallback_->OnResult(result, attributes);
113 }
114
SetExecutorPropertyCallbackService(const std::shared_ptr<SetPropCallback> & impl)115 SetExecutorPropertyCallbackService::SetExecutorPropertyCallbackService(const std::shared_ptr<SetPropCallback> &impl)
116 : setPropCallback_(impl)
117 {
118 CallbackManager::CallbackAction action = [impl]() {
119 if (impl != nullptr) {
120 IAM_LOGI("user auth service death, set prop callback return default result to caller");
121 Attributes extraInfo;
122 impl->OnResult(GENERAL_ERROR, extraInfo);
123 }
124 };
125 CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
126 }
127
~SetExecutorPropertyCallbackService()128 SetExecutorPropertyCallbackService::~SetExecutorPropertyCallbackService()
129 {
130 CallbackManager::GetInstance().RemoveCallback(reinterpret_cast<uintptr_t>(this));
131 }
132
OnSetExecutorPropertyResult(int32_t result)133 void SetExecutorPropertyCallbackService::OnSetExecutorPropertyResult(int32_t result)
134 {
135 IAM_LOGI("start, result:%{public}d", result);
136 if (setPropCallback_ == nullptr) {
137 IAM_LOGE("set prop callback is nullptr");
138 return;
139 }
140 Attributes attr;
141 setPropCallback_->OnResult(result, attr);
142 }
143 } // namespace UserAuth
144 } // namespace UserIam
145 } // namespace OHOS