1 /*
2 * Copyright (c) 2024 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_has_key_item_as_user.h"
17 #include "huks_napi_has_key_item.h"
18 #include "huks_napi_common_item.h"
19
20 #include "securec.h"
21
22 #include "hks_api.h"
23 #include "hks_log.h"
24 #include "hks_mem.h"
25 #include "hks_param.h"
26 #include "hks_type.h"
27
28 namespace HuksNapiItem {
29 constexpr int HUKS_NAPI_HAS_KEY_ITEM_AS_USER_ARGS_COUNT = 3;
HasKeyItemAsUserParseParams(napi_env env,napi_callback_info info,IsKeyExistAsyncContext context)30 static napi_value HasKeyItemAsUserParseParams(napi_env env, napi_callback_info info, IsKeyExistAsyncContext context)
31 {
32 size_t argc = HUKS_NAPI_HAS_KEY_ITEM_AS_USER_ARGS_COUNT;
33 napi_value argv[HUKS_NAPI_HAS_KEY_ITEM_AS_USER_ARGS_COUNT] = { 0 };
34 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
35
36 if (argc != HUKS_NAPI_HAS_KEY_ITEM_AS_USER_ARGS_COUNT) {
37 HksNapiThrowInvalidParamCount(env);
38 HKS_LOG_E("invalid params count %" LOG_PUBLIC "zu", argc);
39 return nullptr;
40 }
41
42 int userId = 0;
43 size_t index = 0;
44 napi_value result = GetUserIdValue(env, argv[index], userId);
45 if (result == nullptr) {
46 HksNapiThrowGetUserIdFail(env);
47 HKS_LOG_E("HasKeyItemAsUserParseParams could not get user id value");
48 return nullptr;
49 }
50 index++;
51 result = ParseKeyAliasAndHksParamSetAsUser(env, userId, argv, index, {context->keyAlias, context->paramSet});
52 if (result == nullptr) {
53 HKS_LOG_E("isKeyExist parse params failed");
54 return nullptr;
55 }
56 return GetInt32(env, 0);
57 }
58
HuksNapiHasKeyItemAsUser(napi_env env,napi_callback_info info)59 napi_value HuksNapiHasKeyItemAsUser(napi_env env, napi_callback_info info)
60 {
61 IsKeyExistAsyncContext context = CreateIsKeyExistAsyncContext();
62 if (context == nullptr) {
63 HksNapiThrowInsufficientMemory(env);
64 HKS_LOG_E("could not create context");
65 return nullptr;
66 }
67
68 napi_value result = HasKeyItemAsUserParseParams(env, info, context);
69 if (result == nullptr) {
70 HKS_LOG_E("could not parse params");
71 DeleteIsKeyExistAsyncContext(env, context);
72 return nullptr;
73 }
74
75 result = IsKeyExistAsyncWork(env, context);
76 if (result == nullptr) {
77 HKS_LOG_E("could not start async work");
78 DeleteIsKeyExistAsyncContext(env, context);
79 return nullptr;
80 }
81 return result;
82 }
83 } // namespace HuksNapiItem
84