• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 INTERFACES_KITS_COMMON_INCLUDE_NAPI_EDM_COMMON_ADDON_H
17 #define INTERFACES_KITS_COMMON_INCLUDE_NAPI_EDM_COMMON_ADDON_H
18 
19 #include <string>
20 
21 #include "napi/native_api.h"
22 #include "want.h"
23 
24 namespace OHOS {
25 namespace EDM {
26 struct AsyncCallbackInfo {
27     napi_env env;
28     napi_async_work asyncWork;
29     napi_deferred deferred;
30     napi_ref callback = 0;
31     ErrCode ret;
32     bool boolRet = true;
33     std::string stringRet;
34     std::vector<std::string> arrayStringRet;
35     int32_t intRet = 0;
36     int32_t err = 0;
37     uint32_t errCode = 0;
38     std::string errMessage;
39     int policyCode = 0;
40     std::string innerCodeMsg;
41 
~AsyncCallbackInfoAsyncCallbackInfo42     virtual ~AsyncCallbackInfo() {};
43 };
44 
45 constexpr int32_t ARR_INDEX_ZERO = 0;
46 constexpr int32_t ARR_INDEX_ONE = 1;
47 constexpr int32_t ARR_INDEX_TWO = 2;
48 constexpr int32_t ARR_INDEX_THREE = 3;
49 constexpr int32_t ARR_INDEX_FOUR = 4;
50 
51 constexpr size_t ARGS_SIZE_ZERO = 0;
52 constexpr size_t ARGS_SIZE_ONE = 1;
53 constexpr size_t ARGS_SIZE_TWO = 2;
54 constexpr size_t ARGS_SIZE_THREE = 3;
55 constexpr size_t ARGS_SIZE_FOUR = 4;
56 constexpr size_t ARGS_SIZE_FIVE = 5;
57 
58 constexpr size_t CALLBACK_SIZE = 1;
59 
60 constexpr int32_t NAPI_RETURN_ZERO = 0;
61 constexpr int32_t NAPI_RETURN_ONE = 1;
62 
63 constexpr int NAPI_MAX_DATA_LEN = 0x6400000;
64 
65 bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType);
66 bool ParseElementName(napi_env env, AppExecFwk::ElementName &elementName, napi_value args);
67 bool ParseLong(napi_env env, int64_t &param, napi_value args);
68 bool ParseInt(napi_env env, int32_t &param, napi_value args);
69 bool ParseUint(napi_env env, uint32_t &param, napi_value args);
70 bool ParseBool(napi_env env, bool &param, napi_value args);
71 bool ParseString(napi_env env, std::string &param, napi_value args);
72 bool ParseCallback(napi_env env, napi_ref &param, napi_value args);
73 napi_value ParseStringArray(napi_env env, std::vector<std::string> &stringArray, napi_value args);
74 bool ParseCharArray(napi_env env, napi_value args, size_t maxLength, char *param);
75 bool GetStringFromNAPI(napi_env env, napi_value value, std::string &resultStr);
76 bool JsObjectToInt(napi_env env, napi_value object, const char *filedStr, bool isNecessaryProp, int32_t &result);
77 bool JsObjectToUint(napi_env env, napi_value object, const char *filedStr, bool isNecessaryProp, uint32_t &result);
78 bool JsObjectToBool(napi_env env, napi_value object, const char *filedStr, bool isNecessaryProp, bool &result);
79 bool JsObjectToString(napi_env env, napi_value object, const char *filedStr, bool isNecessaryProp,
80     std::string &resultStr);
81 bool JsObjectToCharArray(napi_env env, napi_value object, const char *filedStr, int maxLength, bool isNecessaryProp,
82     char *result);
83 bool GetJsProperty(napi_env env, napi_value object, const char *filedStr, napi_value &result);
84 bool JsObjectToU8Vector(napi_env env, napi_value object, const char *fieldStr,
85     std::vector<uint8_t> &certVector);
86 bool JsObjectToStringVector(napi_env env, napi_value object, const char *fieldStr, bool isNecessaryProp,
87     std::vector<std::string> &stringVector);
88 void NativeVoidCallbackComplete(napi_env env, napi_status status, void *data);
89 napi_value HandleAsyncWork(napi_env env, AsyncCallbackInfo *context, const std::string &workName,
90     napi_async_execute_callback execute, napi_async_complete_callback complete);
91 void NativeBoolCallbackComplete(napi_env env, napi_status status, void *data);
92 void NativeStringCallbackComplete(napi_env env, napi_status status, void *data);
93 void NativeNumberCallbackComplete(napi_env env, napi_status status, void *data);
94 void NativeArrayStringCallbackComplete(napi_env env, napi_status status, void *data);
95 void ConvertStringVectorToJS(napi_env env, const std::vector<std::string> &stringVector, napi_value result);
96 bool CheckAdminWithUserIdParamType(napi_env env, size_t argc, napi_value* argv, bool &hasCallback, bool &hasUserId);
97 } // namespace EDM
98 } // namespace OHOS
99 
100 #endif // INTERFACES_KITS_COMMON_INCLUDE_NAPI_EDM_COMMON_ADDON_H
101