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_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<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(const std::shared_ptr<PrepareRemoteAuthCallback> & impl)55 UserAuthCallbackService::UserAuthCallbackService(const std::shared_ptr<PrepareRemoteAuthCallback> &impl)
56     : prepareRemoteAuthCallback_(impl),
57     iamHitraceHelper_(Common::MakeShared<UserIam::UserAuth::IamHitraceHelper>("UserAuth InnerKit"))
58 {
59     CallbackManager::CallbackAction action = [impl]() {
60         if (impl != nullptr) {
61             IAM_LOGI("user auth service death, prepare remote auth callback return default result to caller");
62             impl->OnResult(GENERAL_ERROR);
63         }
64     };
65     CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
66 }
67 
~UserAuthCallbackService()68 UserAuthCallbackService::~UserAuthCallbackService()
69 {
70     IAM_LOGI("start");
71     iamHitraceHelper_= nullptr;
72     CallbackManager::GetInstance().RemoveCallback(reinterpret_cast<uintptr_t>(this));
73 }
74 
OnResult(int32_t result,const Attributes & extraInfo)75 void UserAuthCallbackService::OnResult(int32_t result, const Attributes &extraInfo)
76 {
77     IAM_LOGI("start, result:%{public}d", result);
78     if (authCallback_ != nullptr) {
79         authCallback_->OnResult(result, extraInfo);
80     } else if (identifyCallback_ != nullptr) {
81         identifyCallback_->OnResult(result, extraInfo);
82     } else if (prepareRemoteAuthCallback_ != nullptr) {
83         prepareRemoteAuthCallback_->OnResult(result);
84     } else {
85         IAM_LOGE("all callback is nullptr");
86         return;
87     }
88     iamHitraceHelper_= nullptr;
89 }
90 
OnAcquireInfo(int32_t module,int32_t acquireInfo,const Attributes & extraInfo)91 void UserAuthCallbackService::OnAcquireInfo(int32_t module, int32_t acquireInfo, const Attributes &extraInfo)
92 {
93     IAM_LOGI("start, module:%{public}d acquireInfo:%{public}d", module, acquireInfo);
94     if (authCallback_ != nullptr) {
95         authCallback_->OnAcquireInfo(module, acquireInfo, extraInfo);
96     } else if (identifyCallback_ != nullptr) {
97         identifyCallback_->OnAcquireInfo(module, acquireInfo, extraInfo);
98     } else if (prepareRemoteAuthCallback_ != nullptr) {
99         IAM_LOGE("prepare remote auth callback not support acquire info");
100     } else {
101         IAM_LOGE("all callback is nullptr");
102         return;
103     }
104 }
105 
GetExecutorPropertyCallbackService(const std::shared_ptr<GetPropCallback> & impl)106 GetExecutorPropertyCallbackService::GetExecutorPropertyCallbackService(const std::shared_ptr<GetPropCallback> &impl)
107     : getPropCallback_(impl)
108 {
109     CallbackManager::CallbackAction action = [impl]() {
110         if (impl != nullptr) {
111             IAM_LOGI("user auth service death, get prop callback return default result to caller");
112             Attributes extraInfo;
113             impl->OnResult(GENERAL_ERROR, extraInfo);
114         }
115     };
116     CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
117 }
118 
~GetExecutorPropertyCallbackService()119 GetExecutorPropertyCallbackService::~GetExecutorPropertyCallbackService()
120 {
121     CallbackManager::GetInstance().RemoveCallback(reinterpret_cast<uintptr_t>(this));
122 }
123 
OnGetExecutorPropertyResult(int32_t result,const Attributes & attributes)124 void GetExecutorPropertyCallbackService::OnGetExecutorPropertyResult(int32_t result, const Attributes &attributes)
125 {
126     IAM_LOGI("start, result:%{public}d", result);
127     if (getPropCallback_ == nullptr) {
128         IAM_LOGE("get prop callback is nullptr");
129         return;
130     }
131     getPropCallback_->OnResult(result, attributes);
132 }
133 
SetExecutorPropertyCallbackService(const std::shared_ptr<SetPropCallback> & impl)134 SetExecutorPropertyCallbackService::SetExecutorPropertyCallbackService(const std::shared_ptr<SetPropCallback> &impl)
135     : setPropCallback_(impl)
136 {
137     CallbackManager::CallbackAction action = [impl]() {
138         if (impl != nullptr) {
139             IAM_LOGI("user auth service death, set prop callback return default result to caller");
140             Attributes extraInfo;
141             impl->OnResult(GENERAL_ERROR, extraInfo);
142         }
143     };
144     CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
145 }
146 
~SetExecutorPropertyCallbackService()147 SetExecutorPropertyCallbackService::~SetExecutorPropertyCallbackService()
148 {
149     CallbackManager::GetInstance().RemoveCallback(reinterpret_cast<uintptr_t>(this));
150 }
151 
OnSetExecutorPropertyResult(int32_t result)152 void SetExecutorPropertyCallbackService::OnSetExecutorPropertyResult(int32_t result)
153 {
154     IAM_LOGI("start, result:%{public}d", result);
155     if (setPropCallback_ == nullptr) {
156         IAM_LOGE("set prop callback is nullptr");
157         return;
158     }
159     Attributes attr;
160     setPropCallback_->OnResult(result, attr);
161 }
162 } // namespace UserAuth
163 } // namespace UserIam
164 } // namespace OHOS