• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "napi_account_iam_pin_auth.h"
17 
18 #include "account_iam_client.h"
19 #include "account_log_wrapper.h"
20 #include "napi_account_error.h"
21 #include "napi_account_iam_common.h"
22 
23 namespace OHOS {
24 namespace AccountJsKit {
25 using namespace OHOS::AccountSA;
26 
Init(napi_env env,napi_value exports)27 napi_value NapiAccountIAMPINAuth::Init(napi_env env, napi_value exports)
28 {
29     napi_value cons;
30     napi_property_descriptor clzDes[] = {
31         DECLARE_NAPI_FUNCTION("registerInputer", RegisterInputer),
32         DECLARE_NAPI_FUNCTION("unregisterInputer", UnregisterInputer)
33     };
34     NAPI_CALL(env, napi_define_class(env, "PINAuth", NAPI_AUTO_LENGTH, JsConstructor,
35         nullptr, sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons));
36     NAPI_CALL(env, napi_set_named_property(env, exports, "PINAuth", cons));
37     return exports;
38 }
39 
JsConstructor(napi_env env,napi_callback_info info)40 napi_value NapiAccountIAMPINAuth::JsConstructor(napi_env env, napi_callback_info info)
41 {
42     napi_value thisVar = nullptr;
43     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
44     return thisVar;
45 }
46 
ParseContextForRegisterInputer(napi_env env,napi_callback_info info,napi_ref * callback)47 static bool ParseContextForRegisterInputer(napi_env env, napi_callback_info info, napi_ref *callback)
48 {
49     size_t argc = ARG_SIZE_ONE;
50     napi_value argv[ARG_SIZE_ONE] = {nullptr};
51     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
52     if (argc != ARG_SIZE_ONE) {
53         ACCOUNT_LOGE("expect at least one parameter, but got %zu", argc);
54         std::string errMsg = "The arg number must be at least " + std::to_string(ARG_SIZE_ONE) + " characters";
55         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
56         return false;
57     }
58     napi_valuetype valuetype = napi_undefined;
59     napi_typeof(env, argv[PARAM_ZERO], &valuetype);
60     napi_value onGetData = nullptr;
61     if (valuetype == napi_object) {
62         napi_get_named_property(env, argv[PARAM_ZERO], "onGetData", &onGetData);
63     } else if (valuetype == napi_function) {
64         onGetData = argv[PARAM_ZERO];
65     } else {
66         ACCOUNT_LOGE("inputer param type error");
67         std::string errMsg = "The type of arg 1 must be function or object";
68         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
69         return false;
70     }
71     return napi_create_reference(env, onGetData, 1, callback) == napi_ok ? true : false;
72 }
73 
RegisterInputer(napi_env env,napi_callback_info info)74 napi_value NapiAccountIAMPINAuth::RegisterInputer(napi_env env, napi_callback_info info)
75 {
76     napi_value result;
77     NAPI_CALL(env, napi_get_boolean(env, false, &result));
78     napi_ref callback = nullptr;
79     if (!ParseContextForRegisterInputer(env, info, &callback)) {
80         return result;
81     }
82     auto inputer = std::make_shared<NapiGetDataCallback>(env, callback);
83     int32_t errCode = AccountIAMClient::GetInstance().RegisterPINInputer(inputer);
84     napi_value napiResult = nullptr;
85     if (errCode == ERR_OK) {
86         NAPI_CALL(env, napi_get_boolean(env, true, &napiResult));
87         return napiResult;
88     }
89     ACCOUNT_LOGE("Failed to register inputer, errCode=%{public}d", errCode);
90     AccountIAMNapiThrow(env, AccountIAMConvertToJSErrCode(errCode), true);
91     return nullptr;
92 }
93 
UnregisterInputer(napi_env env,napi_callback_info info)94 napi_value NapiAccountIAMPINAuth::UnregisterInputer(napi_env env, napi_callback_info info)
95 {
96     AccountIAMClient::GetInstance().UnregisterPINInputer();
97     napi_value result = nullptr;
98     NAPI_CALL(env, napi_get_null(env, &result));
99     return result;
100 }
101 }  // namespace AccountJsKit
102 }  // namespace OHOS
103