• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 #ifndef ASSET_NAPI_COMMON_H
17 #define ASSET_NAPI_COMMON_H
18 
19 #include <vector>
20 
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 
24 #include "asset_system_type.h"
25 
26 #include "asset_napi_context.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 IF_ERROR_THROW_RETURN(env, result)                 \
49 if ((result) != nullptr) {                                 \
50     napi_throw((env), (result));                           \
51     return napi_invalid_arg;                               \
52 }
53 
54 #define IF_FALSE_RETURN(result, returnValue)    \
55 if (!(result)) {                                \
56     return (returnValue);                       \
57 }
58 
59 #define IF_ERR_RETURN(result)                   \
60 if ((result) != napi_ok) {                      \
61     return (result);                            \
62 }
63 
64 napi_value CreateJsError(const napi_env env, int32_t errCode);
65 
66 napi_value CreateJsError(const napi_env env, int32_t errCode, const char *errorMsg);
67 
68 napi_value CreateJsUint8Array(const napi_env env, const AssetBlob &blob);
69 
70 napi_value CreateJsMapArray(const napi_env env, const AssetResultSet &resultSet);
71 
72 napi_value CreateJsUndefined(const napi_env env);
73 
74 napi_status ParseJsArgs(const napi_env env, napi_callback_info info, napi_value *value, size_t valueSize);
75 
76 napi_status ParseJsMap(const napi_env env, napi_value arg, std::vector<AssetAttr> &attrs);
77 
78 napi_status ParseJsUserId(const napi_env env, napi_value arg, std::vector<AssetAttr> &attrs);
79 
80 napi_status NapiSetProperty(const napi_env env, napi_value object, const char *propertyName, uint32_t propertyValue);
81 
82 napi_value CreateAsyncWork(const napi_env env, napi_callback_info info, std::unique_ptr<BaseContext> context,
83     const char *resourceName);
84 
85 napi_value CreateSyncWork(const napi_env env, napi_callback_info info, BaseContext *context);
86 
87 } // Asset
88 } // Security
89 } // OHOS
90 
91 #endif // ASSET_NAPI_COMMON_H