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_delete_key_item.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_DELETE_KEY_MIN_ARGS = 2;
30 constexpr int HUKS_NAPI_DELETE_KEY_MAX_ARGS = 3;
31 } // namespace
32
CreateDeleteKeyAsyncContext()33 DeleteKeyAsyncContext CreateDeleteKeyAsyncContext()
34 {
35 DeleteKeyAsyncContext context = static_cast<DeleteKeyAsyncContext>(HksMalloc(sizeof(DeleteKeyAsyncContextT)));
36 if (context != nullptr) {
37 (void)memset_s(context, sizeof(DeleteKeyAsyncContextT), 0, sizeof(DeleteKeyAsyncContextT));
38 }
39 return context;
40 }
41
DeleteDeleteKeyAsyncContext(napi_env env,DeleteKeyAsyncContext & context)42 void DeleteDeleteKeyAsyncContext(napi_env env, DeleteKeyAsyncContext &context)
43 {
44 if (context == nullptr) {
45 return;
46 }
47 DeleteCommonAsyncContext(env, context->asyncWork, context->callback, context->keyAlias, context->paramSet);
48 HKS_FREE(context);
49 context = nullptr;
50 }
51
DeleteKeyParseParams(napi_env env,napi_callback_info info,DeleteKeyAsyncContext context)52 static napi_value DeleteKeyParseParams(napi_env env, napi_callback_info info, DeleteKeyAsyncContext context)
53 {
54 size_t argc = HUKS_NAPI_DELETE_KEY_MAX_ARGS;
55 napi_value argv[HUKS_NAPI_DELETE_KEY_MAX_ARGS] = { 0 };
56 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
57
58 if (argc < HUKS_NAPI_DELETE_KEY_MIN_ARGS) {
59 HksNapiThrow(env, HUKS_ERR_CODE_ILLEGAL_ARGUMENT, "no enough params input");
60 HKS_LOG_E("no enough params");
61 return nullptr;
62 }
63
64 size_t index = 0;
65 napi_value result = ParseKeyAliasAndHksParamSet(env, argv, index, context->keyAlias, context->paramSet);
66 if (result == nullptr) {
67 HKS_LOG_E("deleteKey parse params failed");
68 return nullptr;
69 }
70
71 index++;
72 if (index < argc) {
73 context->callback = GetCallback(env, argv[index]);
74 }
75
76 return GetInt32(env, 0);
77 }
78
DeleteKeyAsyncWork(napi_env env,DeleteKeyAsyncContext & context)79 napi_value DeleteKeyAsyncWork(napi_env env, DeleteKeyAsyncContext &context)
80 {
81 napi_value promise = nullptr;
82 if (context->callback == nullptr) {
83 NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
84 }
85
86 napi_value resourceName = nullptr;
87 napi_create_string_latin1(env, "deleteKeyAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
88
89 napi_create_async_work(
90 env,
91 nullptr,
92 resourceName,
93 [](napi_env env, void *data) {
94 DeleteKeyAsyncContext napiContext = static_cast<DeleteKeyAsyncContext>(data);
95
96 napiContext->result = HksDeleteKey(napiContext->keyAlias, napiContext->paramSet);
97 },
98 [](napi_env env, napi_status status, void *data) {
99 DeleteKeyAsyncContext napiContext = static_cast<DeleteKeyAsyncContext>(data);
100 HksSuccessReturnResult resultData;
101 SuccessReturnResultInit(resultData);
102 HksReturnNapiResult(env, napiContext->callback, napiContext->deferred, napiContext->result, resultData);
103 DeleteDeleteKeyAsyncContext(env, napiContext);
104 },
105 static_cast<void *>(context),
106 &context->asyncWork);
107
108 napi_status status = napi_queue_async_work(env, context->asyncWork);
109 if (status != napi_ok) {
110 DeleteDeleteKeyAsyncContext(env, context);
111 HKS_LOG_E("could not queue async work");
112 return nullptr;
113 }
114
115 if (context->callback == nullptr) {
116 return promise;
117 } else {
118 return GetNull(env);
119 }
120 }
121
HuksNapiDeleteKeyItem(napi_env env,napi_callback_info info)122 napi_value HuksNapiDeleteKeyItem(napi_env env, napi_callback_info info)
123 {
124 DeleteKeyAsyncContext context = CreateDeleteKeyAsyncContext();
125 if (context == nullptr) {
126 HKS_LOG_E("could not create context");
127 return nullptr;
128 }
129
130 napi_value result = DeleteKeyParseParams(env, info, context);
131 if (result == nullptr) {
132 HKS_LOG_E("could not parse params");
133 DeleteDeleteKeyAsyncContext(env, context);
134 return nullptr;
135 }
136
137 result = DeleteKeyAsyncWork(env, context);
138 if (result == nullptr) {
139 HKS_LOG_E("could not start async work");
140 DeleteDeleteKeyAsyncContext(env, context);
141 return nullptr;
142 }
143 return result;
144 }
145 } // namespace HuksNapiItem
146