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 "napi_account_iam_inputer_manager.h"
17 
18 #include "account_iam_client.h"
19 #include "account_log_wrapper.h"
20 #include "napi_account_common.h"
21 #include "napi_account_error.h"
22 #include "napi_account_iam_common.h"
23 
24 namespace OHOS {
25 namespace AccountJsKit {
26 using namespace OHOS::AccountSA;
27 
Init(napi_env env,napi_value exports)28 napi_value NapiAccountIAMInputerManager::Init(napi_env env, napi_value exports)
29 {
30     napi_property_descriptor clzDes[] = {
31         DECLARE_NAPI_STATIC_FUNCTION("registerInputer", RegisterInputer),
32         DECLARE_NAPI_STATIC_FUNCTION("unregisterInputer", UnregisterInputer),
33         DECLARE_NAPI_FUNCTION("registerInputer", RegisterInputer),
34         DECLARE_NAPI_FUNCTION("unregisterInputer", UnregisterInputer)
35     };
36     napi_value cons;
37     NAPI_CALL(env, napi_define_class(env, "InputerManager", NAPI_AUTO_LENGTH, InputerManagerConstructor,
38         nullptr, sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons));
39     NAPI_CALL(env, napi_set_named_property(env, exports, "InputerManager", cons));
40     return exports;
41 }
42 
InputerManagerConstructor(napi_env env,napi_callback_info info)43 napi_value NapiAccountIAMInputerManager::InputerManagerConstructor(napi_env env, napi_callback_info info)
44 {
45     napi_value thisVar = nullptr;
46     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
47     return thisVar;
48 }
49 
ParseContextForRegisterInputer(napi_env env,napi_callback_info info,int32_t & authType,napi_ref & callback)50 static bool ParseContextForRegisterInputer(
51     napi_env env, napi_callback_info info, int32_t &authType, napi_ref &callback)
52 {
53     size_t argc = ARG_SIZE_TWO;
54     napi_value argv[ARG_SIZE_TWO] = {nullptr};
55     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
56     if (argc != ARG_SIZE_TWO) {
57         std::string errMsg = "Parameter error. The number of parameters should be at least 2";
58         ACCOUNT_LOGE("%{public}s", errMsg.c_str());
59         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
60         return false;
61     }
62     if (!GetIntProperty(env, argv[PARAM_ZERO], authType)) {
63         std::string errMsg = "Parameter error. The type of \"authType\" must be AuthType";
64         ACCOUNT_LOGE("%{public}s", errMsg.c_str());
65         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
66         return false;
67     }
68     bool hasProp = false;
69     napi_has_named_property(env, argv[PARAM_ONE], "onGetData", &hasProp);
70     if (!hasProp) {
71         std::string errMsg = "Parameter error. The type of \"inputer\" must be IInputer";
72         ACCOUNT_LOGE("%{public}s", errMsg.c_str());
73         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
74         return false;
75     }
76     napi_value onGetData = nullptr;
77     napi_get_named_property(env, argv[PARAM_ONE], "onGetData", &onGetData);
78     if (!GetCallbackProperty(env, onGetData, callback, 1)) {
79         std::string errMsg = "Parameter error. The type of \"inputer\" must be IInputer";
80         ACCOUNT_LOGE("%{public}s", errMsg.c_str());
81         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
82         return false;
83     }
84     return true;
85 }
86 
RegisterInputer(napi_env env,napi_callback_info info)87 napi_value NapiAccountIAMInputerManager::RegisterInputer(napi_env env, napi_callback_info info)
88 {
89     int32_t authType = -1;
90     napi_ref callback = nullptr;
91     if (!ParseContextForRegisterInputer(env, info, authType, callback)) {
92         return nullptr;
93     }
94     auto napiCallback = std::make_shared<NapiCallbackRef>(env, callback);
95     auto inputer = std::make_shared<NapiGetDataCallback>(env, napiCallback);
96     ErrCode errCode = AccountIAMClient::GetInstance().RegisterInputer(authType, inputer);
97     if (errCode != ERR_OK) {
98         ACCOUNT_LOGE("Failed to register inputer, errCode=%{public}d", errCode);
99         AccountIAMNapiThrow(env, AccountIAMConvertToJSErrCode(errCode), true);
100     }
101     return nullptr;
102 }
103 
UnregisterInputer(napi_env env,napi_callback_info info)104 napi_value NapiAccountIAMInputerManager::UnregisterInputer(napi_env env, napi_callback_info info)
105 {
106     size_t argc = ARG_SIZE_ONE;
107     napi_value argv[ARG_SIZE_ONE] = {nullptr};
108     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
109     if (argc != ARG_SIZE_ONE) {
110         std::string errMsg = "Parameter error. The number of parameters should be at least 1";
111         ACCOUNT_LOGE("%{public}s", errMsg.c_str());
112         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
113         return nullptr;
114     }
115     int32_t authType = -1;
116     if (!GetIntProperty(env, argv[PARAM_ZERO], authType)) {
117         std::string errMsg = "Parameter error. The type of \"authType\" must be AuthType";
118         ACCOUNT_LOGE("%{public}s", errMsg.c_str());
119         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
120         return nullptr;
121     }
122     ErrCode errCode = AccountIAMClient::GetInstance().UnregisterInputer(authType);
123     if (errCode != ERR_OK) {
124         ACCOUNT_LOGE("Failed to unregister inputer, errCode=%{public}d", errCode);
125         AccountIAMNapiThrow(env, AccountIAMConvertToJSErrCode(errCode), true);
126     }
127     return nullptr;
128 }
129 }  // namespace AccountJsKit
130 }  // namespace OHOS
131