• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_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.h"
26 
27 namespace HuksNapi {
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 = (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 
59     if (context->asyncWork != nullptr) {
60         napi_delete_async_work(env, context->asyncWork);
61         context->asyncWork = nullptr;
62     }
63 
64     if (context->callback != nullptr) {
65         napi_delete_reference(env, context->callback);
66         context->callback = nullptr;
67     }
68 
69     if (context->keyAlias != nullptr) {
70         FreeHksBlob(context->keyAlias);
71     }
72 
73     if (context->paramSet != nullptr) {
74         HksFreeParamSet(&context->paramSet);
75     }
76 
77     HksFree(context);
78     context = nullptr;
79 }
80 
IsKeyExistParseParams(napi_env env,napi_callback_info info,IsKeyExistAsyncContext context)81 static napi_value IsKeyExistParseParams(napi_env env, napi_callback_info info, IsKeyExistAsyncContext context)
82 {
83     size_t argc = HUKS_NAPI_IS_KEY_EXIST_MAX_ARGS;
84     napi_value argv[HUKS_NAPI_IS_KEY_EXIST_MAX_ARGS] = {0};
85     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
86 
87     if (argc < HUKS_NAPI_IS_KEY_EXIST_MIN_ARGS) {
88         napi_throw_error(env, NULL, "invalid arguments");
89         HKS_LOG_E("no enough params");
90         return nullptr;
91     }
92 
93     size_t index = 0;
94     napi_value result = ParseKeyAlias(env, argv[index], context->keyAlias);
95     if (result == nullptr) {
96         HKS_LOG_E("could not get alias");
97         return nullptr;
98     }
99 
100     index++;
101     napi_value properties = nullptr;
102     napi_status status =
103         napi_get_named_property(env, argv[index], HKS_OPTIONS_PROPERTY_PROPERTIES.c_str(), &properties);
104     if (status != napi_ok || properties == nullptr) {
105         GET_AND_THROW_LAST_ERROR((env));
106         HKS_LOG_E("could not get properties");
107         return nullptr;
108     }
109     result = ParseHksParamSet(env, properties, context->paramSet);
110     if (result == nullptr) {
111         HKS_LOG_E("could not get paramset");
112         return nullptr;
113     }
114 
115     index++;
116     if (index < argc) {
117         context->callback = GetCallback(env, argv[index]);
118     }
119 
120     return GetInt32(env, 0);
121 }
122 
IsKeyExistWriteResult(napi_env env,IsKeyExistAsyncContext context)123 static napi_value IsKeyExistWriteResult(napi_env env, IsKeyExistAsyncContext context)
124 {
125     napi_value isKeyExist = nullptr;
126     if (context->result == HKS_SUCCESS) {
127         NAPI_CALL(env, napi_get_boolean(env, true, &isKeyExist));
128     } else {
129         NAPI_CALL(env, napi_get_boolean(env, false, &isKeyExist));
130     }
131     return isKeyExist;
132 }
133 
IsKeyExistAsyncWork(napi_env env,IsKeyExistAsyncContext context)134 static napi_value IsKeyExistAsyncWork(napi_env env, IsKeyExistAsyncContext context)
135 {
136     napi_value promise = nullptr;
137     if (context->callback == nullptr) {
138         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
139     }
140 
141     napi_value resourceName = nullptr;
142     napi_create_string_latin1(env, "isKeyExistAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
143 
144     napi_create_async_work(
145         env,
146         nullptr,
147         resourceName,
148         [](napi_env env, void *data) {
149             IsKeyExistAsyncContext context = static_cast<IsKeyExistAsyncContext>(data);
150 
151             context->result = HksKeyExist(context->keyAlias, context->paramSet);
152         },
153         [](napi_env env, napi_status status, void *data) {
154             IsKeyExistAsyncContext context = static_cast<IsKeyExistAsyncContext>(data);
155             napi_value result = IsKeyExistWriteResult(env, context);
156             if (result == nullptr) {
157                 return;
158             }
159             if (context->callback != nullptr) {
160                 CallAsyncCallback(env, context->callback, context->result, result);
161             } else {
162                 napi_resolve_deferred(env, context->deferred, result);
163             }
164             DeleteIsKeyExistAsyncContext(env, context);
165         },
166         (void *)context,
167         &context->asyncWork);
168 
169     napi_status status = napi_queue_async_work(env, context->asyncWork);
170     if (status != napi_ok) {
171         GET_AND_THROW_LAST_ERROR((env));
172         DeleteIsKeyExistAsyncContext(env, context);
173         HKS_LOG_E("could not queue async work");
174         return nullptr;
175     }
176 
177     if (context->callback == nullptr) {
178         return promise;
179     } else {
180         return GetNull(env);
181     }
182 }
183 
HuksNapiIsKeyExist(napi_env env,napi_callback_info info)184 napi_value HuksNapiIsKeyExist(napi_env env, napi_callback_info info)
185 {
186     IsKeyExistAsyncContext context = CreateIsKeyExistAsyncContext();
187     if (context == nullptr) {
188         HKS_LOG_E("could not create context");
189         return nullptr;
190     }
191 
192     napi_value result = IsKeyExistParseParams(env, info, context);
193     if (result == nullptr) {
194         HKS_LOG_E("could not parse params");
195         DeleteIsKeyExistAsyncContext(env, context);
196         return nullptr;
197     }
198 
199     result = IsKeyExistAsyncWork(env, context);
200     if (result == nullptr) {
201         HKS_LOG_E("could not start async work");
202         DeleteIsKeyExistAsyncContext(env, context);
203         return nullptr;
204     }
205     return result;
206 }
207 }  // namespace HuksNapi