1 /*
2 * Copyright (c) 2023 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 #ifndef NAPI_COMMON_H
17 #define NAPI_COMMON_H
18
19 #include "cf_type.h"
20 #include "napi_cert_defines.h"
21 #include "napi/native_api.h"
22 #include "napi/native_common.h"
23
24 namespace OHOS {
25 namespace CertFramework {
26 constexpr int32_t CALLBACK_NUM = 2;
27
28 struct AsyncContext {
29 AsyncType asyncType = ASYNC_TYPE_CALLBACK;
30 napi_value promise = nullptr;
31 napi_ref callback = nullptr;
32 napi_deferred deferred = nullptr;
33 napi_async_work asyncWork = nullptr;
34 napi_ref paramRef = nullptr;
35 int32_t errCode = 0;
36 const char *errMsg = nullptr;
37 };
38 using AsyncCtx = AsyncContext *;
39
40 bool GetCallbackAndPromise(napi_env env, AsyncCtx async, napi_value arg);
41 void ReturnJSResult(napi_env env, AsyncCtx async, napi_value result);
42 napi_value GetResourceName(napi_env env, const char *name);
43 napi_value ConvertBlobArrayToNapiValue(napi_env env, const CfParamSet *paramSet);
44 int32_t GetBlobArrayFromParamSet(const CfParamSet *paramSet, CfArray *outArray);
45 void FreeAsyncContext(napi_env env, AsyncCtx &ctx);
46
NapiGetNull(napi_env env)47 inline napi_value NapiGetNull(napi_env env)
48 {
49 napi_value result = nullptr;
50 NAPI_CALL(env, napi_get_null(env, &result));
51 return result;
52 }
53
NapiGetInt32(napi_env env,int32_t value)54 inline napi_value NapiGetInt32(napi_env env, int32_t value)
55 {
56 napi_value result = nullptr;
57 NAPI_CALL(env, napi_create_int32(env, value, &result));
58 return result;
59 }
60 } // namespace CertFramework
61 } // namespace OHOS
62
63 #endif // NAPI_COMMON_H