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