1 /*
2 * Copyright (c) 2025 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 "asset_napi_query_sync_result.h"
17
18 #include "asset_log.h"
19 #include "asset_system_api.h"
20 #include "asset_system_type.h"
21
22 #include "asset_napi_check.h"
23 #include "asset_napi_common.h"
24
25 namespace OHOS {
26 namespace Security {
27 namespace Asset {
28 namespace {
29 const uint32_t ARG_COUNT = 1;
30
31 const std::vector<uint32_t> OPTIONAL_TAGS = {
32 SEC_ASSET_TAG_GROUP_ID,
33 SEC_ASSET_TAG_REQUIRE_ATTR_ENCRYPTED,
34 };
35
CheckQuerySyncResultArgs(napi_env env,BaseContext * context)36 napi_value CheckQuerySyncResultArgs(napi_env env, BaseContext *context)
37 {
38 napi_value error = CheckAssetTagValidity(env, context->attrs, OPTIONAL_TAGS, SEC_ASSET_PARAM_VERIFICATION_FAILED);
39 if (error != nullptr) {
40 return error;
41 }
42 return CheckAssetValueValidity(env, context->attrs, SEC_ASSET_PARAM_VERIFICATION_FAILED);
43 }
44 } // anonymous namespace
45
NapiQuerySyncResult(const napi_env env,napi_callback_info info)46 napi_value NapiQuerySyncResult(const napi_env env, napi_callback_info info)
47 {
48 auto context = std::unique_ptr<QuerySyncResultContext>(new (std::nothrow)QuerySyncResultContext());
49 NAPI_THROW(env, context == nullptr, SEC_ASSET_OUT_OF_MEMORY, "Unable to allocate memory for Context.");
50
51 context->parse = [](napi_env env, napi_callback_info info, BaseContext *baseContext) -> napi_status {
52 QuerySyncResultContext *context = reinterpret_cast<QuerySyncResultContext *>(baseContext);
53 napi_value argv[MAX_ARGS_NUM] = { 0 };
54 IF_ERR_RETURN(ParseJsArgs(env, info, argv, ARG_COUNT));
55 IF_ERR_RETURN(ParseJsMap(env, argv[0], context->attrs));
56 return napi_ok;
57 };
58
59 context->check = CheckQuerySyncResultArgs;
60
61 context->execute = [](napi_env env, void *data) {
62 QuerySyncResultContext *context = static_cast<QuerySyncResultContext *>(data);
63 context->result = AssetQuerySyncResult(&context->attrs[0], context->attrs.size(), &context->syncResult);
64 };
65
66 context->resolve = [](napi_env env, BaseContext *baseContext) -> napi_value {
67 QuerySyncResultContext *context = static_cast<QuerySyncResultContext *>(baseContext);
68 napi_value syncResult = nullptr;
69 NAPI_CALL(env, napi_create_object(env, &syncResult));
70 NAPI_CALL(env, NapiSetProperty(env, syncResult, "resultCode", context->syncResult.resultCode));
71 if (context->syncResult.resultCode != SEC_ASSET_SUCCESS && context->syncResult.totalCount == 0) {
72 LOGI("Generic error found, not need to set totalCount and failedCount.");
73 return syncResult;
74 }
75 NAPI_CALL(env, NapiSetProperty(env, syncResult, "totalCount", context->syncResult.totalCount));
76 NAPI_CALL(env, NapiSetProperty(env, syncResult, "failedCount", context->syncResult.failedCount));
77 return syncResult;
78 };
79 return CreateAsyncWork(env, info, std::move(context), __func__);
80 }
81
82 } // Asset
83 } // Security
84 } // OHOS
85