• 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_is_key_item_exist.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_IS_KEY_EXIST_MIN_ARGS = 2;
30 constexpr int HUKS_NAPI_IS_KEY_EXIST_MAX_ARGS = 3;
31 }  // namespace
32 
33 struct IsKeyExistAsyncContextT {
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 *keyAlias = nullptr;
40     struct HksParamSet *paramSet = nullptr;
41 };
42 using IsKeyExistAsyncContext = IsKeyExistAsyncContextT *;
43 
CreateIsKeyExistAsyncContext()44 static IsKeyExistAsyncContext CreateIsKeyExistAsyncContext()
45 {
46     IsKeyExistAsyncContext context = static_cast<IsKeyExistAsyncContext>(HksMalloc(sizeof(IsKeyExistAsyncContextT)));
47     if (context != nullptr) {
48         (void)memset_s(context, sizeof(IsKeyExistAsyncContextT), 0, sizeof(IsKeyExistAsyncContextT));
49     }
50     return context;
51 }
52 
DeleteIsKeyExistAsyncContext(napi_env env,IsKeyExistAsyncContext & context)53 static void DeleteIsKeyExistAsyncContext(napi_env env, IsKeyExistAsyncContext &context)
54 {
55     if (context == nullptr) {
56         return;
57     }
58     DeleteCommonAsyncContext(env, context->asyncWork, context->callback, context->keyAlias, context->paramSet);
59     HksFree(context);
60     context = nullptr;
61 }
62 
IsKeyExistParseParams(napi_env env,napi_callback_info info,IsKeyExistAsyncContext context)63 static napi_value IsKeyExistParseParams(napi_env env, napi_callback_info info, IsKeyExistAsyncContext context)
64 {
65     size_t argc = HUKS_NAPI_IS_KEY_EXIST_MAX_ARGS;
66     napi_value argv[HUKS_NAPI_IS_KEY_EXIST_MAX_ARGS] = { 0 };
67     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
68 
69     if (argc < HUKS_NAPI_IS_KEY_EXIST_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 = ParseKeyAliasAndHksParamSet(env, argv, index, context->keyAlias, context->paramSet);
77     if (result == nullptr) {
78         HKS_LOG_E("isKeyExist parse params 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 
IsKeyExistAsyncWork(napi_env env,IsKeyExistAsyncContext context)90 static napi_value IsKeyExistAsyncWork(napi_env env, IsKeyExistAsyncContext 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 = nullptr;
98     napi_create_string_latin1(env, "isKeyExistAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
99 
100     napi_create_async_work(
101         env,
102         nullptr,
103         resourceName,
104         [](napi_env env, void *data) {
105             IsKeyExistAsyncContext napiContext = static_cast<IsKeyExistAsyncContext>(data);
106 
107             napiContext->result = HksKeyExist(napiContext->keyAlias, napiContext->paramSet);
108         },
109         [](napi_env env, napi_status status, void *data) {
110             IsKeyExistAsyncContext napiContext = static_cast<IsKeyExistAsyncContext>(data);
111             HksSuccessReturnResult resultData;
112             SuccessReturnResultInit(resultData);
113             resultData.isOnlyReturnBoolResult = true;
114             HksReturnNapiResult(env, napiContext->callback, napiContext->deferred, napiContext->result, resultData);
115             DeleteIsKeyExistAsyncContext(env, napiContext);
116         },
117         static_cast<void *>(context),
118         &context->asyncWork);
119 
120     napi_status status = napi_queue_async_work(env, context->asyncWork);
121     if (status != napi_ok) {
122         DeleteIsKeyExistAsyncContext(env, context);
123         HKS_LOG_E("could not queue async work");
124         return nullptr;
125     }
126 
127     if (context->callback == nullptr) {
128         return promise;
129     } else {
130         return GetNull(env);
131     }
132 }
133 
HuksNapiIsKeyItemExist(napi_env env,napi_callback_info info)134 napi_value HuksNapiIsKeyItemExist(napi_env env, napi_callback_info info)
135 {
136     IsKeyExistAsyncContext context = CreateIsKeyExistAsyncContext();
137     if (context == nullptr) {
138         HKS_LOG_E("could not create context");
139         return nullptr;
140     }
141 
142     napi_value result = IsKeyExistParseParams(env, info, context);
143     if (result == nullptr) {
144         HKS_LOG_E("could not parse params");
145         DeleteIsKeyExistAsyncContext(env, context);
146         return nullptr;
147     }
148 
149     result = IsKeyExistAsyncWork(env, context);
150     if (result == nullptr) {
151         HKS_LOG_E("could not start async work");
152         DeleteIsKeyExistAsyncContext(env, context);
153         return nullptr;
154     }
155     return result;
156 }
157 }  // namespace HuksNapiItem
158