• 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.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.h"
26 
27 namespace HuksNapi {
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 = (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, nullptr, "invalid arguments");
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 
AbortWriteResult(napi_env env,AbortAsyncContext context)90 static napi_value AbortWriteResult(napi_env env, AbortAsyncContext context)
91 {
92     return GenerateHksResult(env, context->result, nullptr, 0);
93 }
94 
AbortAsyncWork(napi_env env,AbortAsyncContext context)95 static napi_value AbortAsyncWork(napi_env env, AbortAsyncContext context)
96 {
97     napi_value promise = nullptr;
98     if (context->callback == nullptr) {
99         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
100     }
101 
102     napi_value resourceName;
103     napi_create_string_latin1(env, "AbortAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
104 
105     napi_create_async_work(
106         env,
107         nullptr,
108         resourceName,
109         [](napi_env env, void *data) {
110             AbortAsyncContext napiContext = static_cast<AbortAsyncContext>(data);
111             napiContext->result = HksAbort(napiContext->handle, napiContext->paramSet);
112         },
113         [](napi_env env, napi_status status, void *data) {
114             AbortAsyncContext napiContext = static_cast<AbortAsyncContext>(data);
115             napi_value result = AbortWriteResult(env, napiContext);
116             if (napiContext->callback == nullptr) {
117                 napi_resolve_deferred(env, napiContext->deferred, result);
118             } else if (result != nullptr) {
119                 CallAsyncCallback(env, napiContext->callback, napiContext->result, result);
120             }
121             DeleteAbortAsyncContext(env, napiContext);
122         },
123         static_cast<void *>(context),
124         &context->asyncWork);
125 
126     napi_status status = napi_queue_async_work(env, context->asyncWork);
127     if (status != napi_ok) {
128         GET_AND_THROW_LAST_ERROR((env));
129         DeleteAbortAsyncContext(env, context);
130         HKS_LOG_E("could not queue async work");
131         return nullptr;
132     }
133 
134     if (context->callback == nullptr) {
135         return promise;
136     } else {
137         return GetNull(env);
138     }
139 }
140 
HuksNapiAbort(napi_env env,napi_callback_info info)141 napi_value HuksNapiAbort(napi_env env, napi_callback_info info)
142 {
143     AbortAsyncContext context = CreateAbortAsyncContext();
144     if (context == nullptr) {
145         HKS_LOG_E("could not create context");
146         return nullptr;
147     }
148 
149     napi_value result = ParseAbortParams(env, info, context);
150     if (result == nullptr) {
151         HKS_LOG_E("could not parse params");
152         DeleteAbortAsyncContext(env, context);
153         return nullptr;
154     }
155 
156     result = AbortAsyncWork(env, context);
157     if (result == nullptr) {
158         HKS_LOG_E("could not start async work");
159         DeleteAbortAsyncContext(env, context);
160         return nullptr;
161     }
162     return result;
163 }
164 }  // namespace HuksNapi
165