• 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 "user_auth_callback_v6.h"
17 
18 #include <uv.h>
19 
20 #include "napi/native_node_api.h"
21 
22 #include "iam_logger.h"
23 #include "iam_ptr.h"
24 
25 #include "user_auth_napi_helper.h"
26 
27 #define LOG_TAG "USER_AUTH_NAPI"
28 
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
32 namespace {
33 struct ResultCallbackV6Holder {
34     std::shared_ptr<UserAuthCallbackV6> callback {nullptr};
35     int32_t result {0};
36     napi_env env;
37 };
38 
39 const std::map<int32_t, AuthenticationResult> g_result2ExecuteResult = {
40     {ResultCode::SUCCESS, AuthenticationResult::SUCCESS},
41     {ResultCode::FAIL, AuthenticationResult::COMPARE_FAILURE},
42     {ResultCode::GENERAL_ERROR, AuthenticationResult::GENERAL_ERROR},
43     {ResultCode::CANCELED, AuthenticationResult::CANCELED},
44     {ResultCode::TIMEOUT, AuthenticationResult::TIMEOUT},
45     {ResultCode::TYPE_NOT_SUPPORT, AuthenticationResult::NO_SUPPORT},
46     {ResultCode::TRUST_LEVEL_NOT_SUPPORT, AuthenticationResult::NO_SUPPORT},
47     {ResultCode::BUSY, AuthenticationResult::BUSY},
48     {ResultCode::INVALID_PARAMETERS, AuthenticationResult::INVALID_PARAMETERS},
49     {ResultCode::LOCKED, AuthenticationResult::LOCKED},
50     {ResultCode::NOT_ENROLLED, AuthenticationResult::NOT_ENROLLED},
51     {ResultCode::IPC_ERROR, AuthenticationResult::GENERAL_ERROR},
52     {ResultCode::INVALID_CONTEXT_ID, AuthenticationResult::GENERAL_ERROR},
53     {ResultCode::WRITE_PARCEL_ERROR, AuthenticationResult::GENERAL_ERROR},
54     {ResultCode::READ_PARCEL_ERROR, AuthenticationResult::GENERAL_ERROR},
55     {ResultCode::CHECK_PERMISSION_FAILED, AuthenticationResult::GENERAL_ERROR},
56     {ResultCode::PIN_EXPIRED, AuthenticationResult::GENERAL_ERROR},
57 };
58 }
59 
UserAuthCallbackV6(napi_env env,const std::shared_ptr<JsRefHolder> & callback,napi_deferred promise)60 UserAuthCallbackV6::UserAuthCallbackV6(napi_env env,
61     const std::shared_ptr<JsRefHolder> &callback, napi_deferred promise)
62     : env_(env), callback_(callback), promise_(promise)
63 {
64     if (env_ == nullptr) {
65         IAM_LOGE("UserAuthCallbackV6 get null env");
66     }
67 }
68 
~UserAuthCallbackV6()69 UserAuthCallbackV6::~UserAuthCallbackV6()
70 {
71 }
72 
DoPromise(int32_t result)73 napi_status UserAuthCallbackV6::DoPromise(int32_t result)
74 {
75     if (promise_ == nullptr) {
76         return napi_ok;
77     }
78     IAM_LOGI("start");
79     napi_value resultVal;
80     napi_status ret = napi_create_int32(env_, result, &resultVal);
81     if (ret != napi_ok) {
82         IAM_LOGE("napi_create_int32 failed %{public}d", ret);
83         return ret;
84     }
85     if (result == ResultCode::SUCCESS) {
86         ret = napi_resolve_deferred(env_, promise_, resultVal);
87         if (ret != napi_ok) {
88             IAM_LOGE("napi_resolve_deferred failed %{public}d", ret);
89         }
90     } else {
91         ret = napi_reject_deferred(env_, promise_, resultVal);
92         if (ret != napi_ok) {
93             IAM_LOGE("napi_reject_deferred failed %{public}d", ret);
94         }
95     }
96     return ret;
97 }
98 
DoCallback(int32_t result)99 napi_status UserAuthCallbackV6::DoCallback(int32_t result)
100 {
101     if (callback_ == nullptr) {
102         return napi_ok;
103     }
104     IAM_LOGI("start");
105     napi_value resultVal;
106     napi_status ret = napi_create_int32(env_, result, &resultVal);
107     if (ret != napi_ok) {
108         IAM_LOGE("napi_create_int32 failed %{public}d", ret);
109         return ret;
110     }
111     return UserAuthNapiHelper::CallVoidNapiFunc(env_, callback_->Get(), ARGS_ONE, &resultVal);
112 }
113 
OnAcquireInfo(int32_t module,uint32_t acquireInfo,const UserIam::UserAuth::Attributes & extraInfo)114 void UserAuthCallbackV6::OnAcquireInfo(int32_t module, uint32_t acquireInfo,
115     const UserIam::UserAuth::Attributes &extraInfo)
116 {
117     IAM_LOGI("start module:%{public}d acquireInfo:%{public}u", module, acquireInfo);
118 }
119 
OnCallbackV6Work(std::shared_ptr<ResultCallbackV6Holder> resultHolder)120 void OnCallbackV6Work(std::shared_ptr<ResultCallbackV6Holder> resultHolder)
121 {
122     IAM_LOGI("start");
123     if (resultHolder == nullptr || resultHolder->callback == nullptr) {
124         IAM_LOGE("resultHolder is invalid");
125         return;
126     }
127     napi_handle_scope scope = nullptr;
128     napi_open_handle_scope(resultHolder->env, &scope);
129     if (scope == nullptr) {
130         IAM_LOGE("scope is invalid");
131         return;
132     }
133     napi_status ret = resultHolder->callback->DoPromise(resultHolder->result);
134     if (ret != napi_ok) {
135         IAM_LOGE("DoPromise fail %{public}d", ret);
136         napi_close_handle_scope(resultHolder->env, scope);
137         return;
138     }
139     ret = resultHolder->callback->DoCallback(resultHolder->result);
140     if (ret != napi_ok) {
141         IAM_LOGE("DoCallback fail %{public}d", ret);
142         napi_close_handle_scope(resultHolder->env, scope);
143         return;
144     }
145     napi_close_handle_scope(resultHolder->env, scope);
146     return;
147 }
148 
OnResult(int32_t result,const Attributes & extraInfo)149 void UserAuthCallbackV6::OnResult(int32_t result, const Attributes &extraInfo)
150 {
151     IAM_LOGI("start, result:%{public}d", result);
152     uv_loop_s *loop = nullptr;
153     napi_status napiStatus = napi_get_uv_event_loop(env_, &loop);
154     if (napiStatus != napi_ok || loop == nullptr) {
155         IAM_LOGE("napi_get_uv_event_loop fail");
156         return;
157     }
158     std::shared_ptr<ResultCallbackV6Holder> resultHolder = Common::MakeShared<ResultCallbackV6Holder>();
159     if (resultHolder == nullptr) {
160         IAM_LOGE("resultHolder is null");
161         return;
162     }
163     resultHolder->callback = shared_from_this();
164     auto res = g_result2ExecuteResult.find(result);
165     if (res == g_result2ExecuteResult.end()) {
166         resultHolder->result = static_cast<int32_t>(ResultCode::GENERAL_ERROR);
167         IAM_LOGE("result %{public}d not found, set execute result GENERAL_ERROR", result);
168     } else {
169         resultHolder->result = static_cast<int32_t>(res->second);
170         IAM_LOGI("convert result %{public}d to execute result %{public}d", result, resultHolder->result);
171     }
172     resultHolder->env = env_;
173     auto task = [resultHolder] () {
174         OnCallbackV6Work(resultHolder);
175     };
176     if (napi_status::napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) {
177         IAM_LOGE("napi_send_event: Failed to SendEvent");
178     }
179 }
180 } // namespace UserAuth
181 } // namespace UserIam
182 } // namespace OHOS
183