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 = 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     HKS_FREE(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, nullptr, "invalid arguments");
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 
IsKeyExistWriteResult(napi_env env,IsKeyExistAsyncContext context)90 static napi_value IsKeyExistWriteResult(napi_env env, IsKeyExistAsyncContext context)
91 {
92     napi_value isKeyExist = nullptr;
93     if (context->result == HKS_SUCCESS) {
94         NAPI_CALL(env, napi_get_boolean(env, true, &isKeyExist));
95     } else {
96         NAPI_CALL(env, napi_get_boolean(env, false, &isKeyExist));
97     }
98     return isKeyExist;
99 }
100 
IsKeyExistAsyncWork(napi_env env,IsKeyExistAsyncContext & context)101 static napi_value IsKeyExistAsyncWork(napi_env env, IsKeyExistAsyncContext &context)
102 {
103     napi_value promise = nullptr;
104     if (context->callback == nullptr) {
105         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
106     }
107 
108     napi_value resourceName = nullptr;
109     napi_create_string_latin1(env, "isKeyExistAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
110 
111     napi_create_async_work(
112         env,
113         nullptr,
114         resourceName,
115         [](napi_env env, void *data) {
116             IsKeyExistAsyncContext napiContext = static_cast<IsKeyExistAsyncContext>(data);
117 
118             napiContext->result = HksKeyExist(napiContext->keyAlias, napiContext->paramSet);
119         },
120         [](napi_env env, napi_status status, void *data) {
121             IsKeyExistAsyncContext napiContext = static_cast<IsKeyExistAsyncContext>(data);
122             napi_value result = IsKeyExistWriteResult(env, napiContext);
123             if (napiContext->callback == nullptr) {
124                 napi_resolve_deferred(env, napiContext->deferred, result);
125             } else if (result != nullptr) {
126                 CallAsyncCallback(env, napiContext->callback, napiContext->result, result);
127             }
128             DeleteIsKeyExistAsyncContext(env, napiContext);
129         },
130         static_cast<void *>(context),
131         &context->asyncWork);
132 
133     napi_status status = napi_queue_async_work(env, context->asyncWork);
134     if (status != napi_ok) {
135         GET_AND_THROW_LAST_ERROR((env));
136         DeleteIsKeyExistAsyncContext(env, context);
137         HKS_LOG_E("could not queue async work");
138         return nullptr;
139     }
140 
141     if (context->callback == nullptr) {
142         return promise;
143     } else {
144         return GetNull(env);
145     }
146 }
147 
HuksNapiIsKeyExist(napi_env env,napi_callback_info info)148 napi_value HuksNapiIsKeyExist(napi_env env, napi_callback_info info)
149 {
150     IsKeyExistAsyncContext context = CreateIsKeyExistAsyncContext();
151     if (context == nullptr) {
152         HKS_LOG_E("could not create context");
153         return nullptr;
154     }
155 
156     napi_value result = IsKeyExistParseParams(env, info, context);
157     if (result == nullptr) {
158         HKS_LOG_E("could not parse params");
159         DeleteIsKeyExistAsyncContext(env, context);
160         return nullptr;
161     }
162 
163     result = IsKeyExistAsyncWork(env, context);
164     if (result == nullptr) {
165         HKS_LOG_E("could not start async work");
166         DeleteIsKeyExistAsyncContext(env, context);
167         return nullptr;
168     }
169     return result;
170 }
171 }  // namespace HuksNapi
172