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 "huks_napi_abort_session.h"
17
18 #include "securec.h"
19
20 #include "hks_api.h"
21 #include "hks_log.h"
22 #include "hks_mem.h"
23 #include "hks_param.h"
24 #include "hks_type.h"
25 #include "huks_napi_common_item.h"
26
27 namespace HuksNapiItem {
28 constexpr int HUKS_NAPI_ABORT_MIN_ARGS = 2;
29 constexpr int HUKS_NAPI_ABORT_MAX_ARGS = 3;
30
CreateAbortAsyncContext()31 AbortAsyncContext CreateAbortAsyncContext()
32 {
33 AbortAsyncContext context = static_cast<AbortAsyncContext>(HksMalloc(sizeof(AbortAsyncContextT)));
34 if (context != nullptr) {
35 (void)memset_s(context, sizeof(AbortAsyncContextT), 0, sizeof(AbortAsyncContextT));
36 }
37 return context;
38 }
39
DeleteAbortAsyncContext(napi_env env,AbortAsyncContext & context)40 void DeleteAbortAsyncContext(napi_env env, AbortAsyncContext &context)
41 {
42 if (context == nullptr) {
43 return;
44 }
45 DeleteCommonAsyncContext(env, context->asyncWork, context->callback, context->handle, context->paramSet);
46 HKS_FREE(context);
47 context = nullptr;
48 }
49
ParseAbortParams(napi_env env,napi_callback_info info,AbortAsyncContext context)50 static napi_value ParseAbortParams(napi_env env, napi_callback_info info, AbortAsyncContext context)
51 {
52 size_t argc = HUKS_NAPI_ABORT_MAX_ARGS;
53 napi_value argv[HUKS_NAPI_ABORT_MAX_ARGS] = { 0 };
54 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
55
56 if (argc < HUKS_NAPI_ABORT_MIN_ARGS) {
57 HksNapiThrow(env, HUKS_ERR_CODE_ILLEGAL_ARGUMENT, "no enough params input");
58 HKS_LOG_E("no enough params");
59 return nullptr;
60 }
61
62 size_t index = 0;
63 napi_value result = GetHandleValue(env, argv[index], context->handle);
64 if (result == nullptr) {
65 HKS_LOG_E("could not get handle value");
66 return nullptr;
67 }
68
69 index++;
70 napi_value property = GetPropertyFromOptions(env, argv[index], HKS_OPTIONS_PROPERTY_PROPERTIES);
71 if (property == nullptr) {
72 return nullptr;
73 }
74
75 result = ParseHksParamSetAndAddParam(env, property, context->paramSet);
76 if (result == nullptr) {
77 HKS_LOG_E("could not get paramset");
78 return nullptr;
79 }
80
81 index++;
82 if (index < argc) {
83 context->callback = GetCallback(env, argv[index]);
84 }
85
86 return GetInt32(env, 0);
87 }
88
AbortAsyncWork(napi_env env,AbortAsyncContext & context)89 napi_value AbortAsyncWork(napi_env env, AbortAsyncContext &context)
90 {
91 napi_value promise = nullptr;
92 if (context->callback == nullptr) {
93 NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
94 }
95
96 napi_value resourceName;
97 napi_create_string_latin1(env, "AbortAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
98
99 napi_create_async_work(
100 env,
101 nullptr,
102 resourceName,
103 [](napi_env env, void *data) {
104 AbortAsyncContext napiContext = static_cast<AbortAsyncContext>(data);
105 napiContext->result = HksAbort(napiContext->handle, napiContext->paramSet);
106 },
107 [](napi_env env, napi_status status, void *data) {
108 AbortAsyncContext napiContext = static_cast<AbortAsyncContext>(data);
109 HksSuccessReturnResult resultData;
110 SuccessReturnResultInit(resultData);
111 HksReturnNapiResult(env, napiContext->callback, napiContext->deferred, napiContext->result, resultData);
112 DeleteAbortAsyncContext(env, napiContext);
113 },
114 static_cast<void *>(context),
115 &context->asyncWork);
116
117 napi_status status = napi_queue_async_work(env, context->asyncWork);
118 if (status != napi_ok) {
119 DeleteAbortAsyncContext(env, context);
120 HKS_LOG_E("could not queue async work");
121 return nullptr;
122 }
123
124 if (context->callback == nullptr) {
125 return promise;
126 } else {
127 return GetNull(env);
128 }
129 }
130
HuksNapiAbortSession(napi_env env,napi_callback_info info)131 napi_value HuksNapiAbortSession(napi_env env, napi_callback_info info)
132 {
133 AbortAsyncContext context = CreateAbortAsyncContext();
134 if (context == nullptr) {
135 HKS_LOG_E("could not create context");
136 return nullptr;
137 }
138
139 napi_value result = ParseAbortParams(env, info, context);
140 if (result == nullptr) {
141 HKS_LOG_E("could not parse params");
142 DeleteAbortAsyncContext(env, context);
143 return nullptr;
144 }
145
146 result = AbortAsyncWork(env, context);
147 if (result == nullptr) {
148 HKS_LOG_E("could not start async work");
149 DeleteAbortAsyncContext(env, context);
150 return nullptr;
151 }
152 return result;
153 }
154 } // namespace HuksNapiItem
155