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 ASSET_NAPI_COMMON_H
17 #define ASSET_NAPI_COMMON_H
18 
19 #include <functional>
20 #include <vector>
21 
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 
25 #include "asset_system_api.h"
26 #include "asset_system_type.h"
27 
28 namespace OHOS {
29 namespace Security {
30 namespace Asset {
31 
32 #define NORMAL_ARGS_NUM 1
33 #define AS_USER_ARGS_NUM 2
34 
35 #define MAX_MESSAGE_LEN 256
36 #define MAX_ARGS_NUM 5
37 
38 #define NAPI_THROW_BASE(env, condition, ret, code, message)             \
39 if ((condition)) {                                                      \
40     LOGE("[FATAL][NAPI]%{public}s", (message));                         \
41     napi_throw((env), CreateJsError((env), (code), (message)));         \
42     return (ret);                                                       \
43 }
44 
45 #define NAPI_THROW(env, condition, code, message)                       \
46     NAPI_THROW_BASE(env, condition, nullptr, code, message)
47 
48 #define CHECK_RESULT_BREAK(env, ret)                        \
49 if ((ret) != SEC_ASSET_SUCCESS) {                           \
50     napi_throw((env), CreateJsError((env), (ret)));         \
51     break;                                                  \
52 }
53 
54 #define IF_FALSE_RETURN(result, returnValue)    \
55 if (!(result)) {                                \
56     return (returnValue);                       \
57 }
58 
59 struct AsyncContext {
60     // common
61     napi_async_work work = nullptr;
62     napi_deferred deferred = nullptr;
63 
64     // input
65     std::vector<AssetAttr> attrs;
66     std::vector<AssetAttr> updateAttrs;
67 
68     // output
69     int32_t result = 0;
70     AssetBlob challenge = { 0 };
71     AssetResultSet resultSet = { 0 };
72 };
73 
74 using CheckFuncPtr = std::function<napi_status(const napi_env, const std::vector<AssetAttr> &)>;
75 
76 struct NapiCallerArgs {
77     size_t expectArgNum;
78     bool isUpdate;
79     bool isAsUser;
80 };
81 
82 AsyncContext *CreateAsyncContext();
83 
84 void DestroyAsyncContext(const napi_env env, AsyncContext *context);
85 
86 napi_value CreateAsyncWork(const napi_env env, AsyncContext *context, const char *funcName,
87     napi_async_execute_callback execute);
88 
89 void FreeAssetAttrs(std::vector<AssetAttr> &attrs);
90 
91 napi_value CreateJsError(const napi_env env, int32_t errCode);
92 
93 napi_value CreateJsError(const napi_env env, int32_t errCode, const char *errorMsg);
94 
95 napi_value CreateJsUint8Array(const napi_env env, const AssetBlob &blob);
96 
97 napi_value CreateJsMapArray(const napi_env env, const AssetResultSet &resultSet);
98 
99 napi_status ParseParam(const napi_env env, napi_callback_info info, const NapiCallerArgs &args,
100     std::vector<AssetAttr> &attrs);
101 
102 napi_status ParseParam(const napi_env env, napi_callback_info info, const NapiCallerArgs &args,
103     std::vector<AssetAttr> &attrs, std::vector<AssetAttr> &updateAttrs);
104 
105 napi_value NapiAsync(const napi_env env, napi_callback_info info, napi_async_execute_callback execute,
106     const NapiCallerArgs &args, CheckFuncPtr checkFunc);
107 
108 } // Asset
109 } // Security
110 } // OHOS
111 
112 #endif // ASSET_NAPI_COMMON_H