1 /* 2 * Copyright (c) 2022 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 int32_t err = 0; 35 uint32_t errCode = 0; 36 std::string errMessage; 37 ~AsyncCallbackInfoAsyncCallbackInfo38 virtual ~AsyncCallbackInfo() {}; 39 }; 40 41 constexpr int32_t ARR_INDEX_ZERO = 0; 42 constexpr int32_t ARR_INDEX_ONE = 1; 43 constexpr int32_t ARR_INDEX_TWO = 2; 44 constexpr int32_t ARR_INDEX_THREE = 3; 45 constexpr int32_t ARR_INDEX_FOUR = 4; 46 47 constexpr size_t ARGS_SIZE_ZERO = 0; 48 constexpr size_t ARGS_SIZE_ONE = 1; 49 constexpr size_t ARGS_SIZE_TWO = 2; 50 constexpr size_t ARGS_SIZE_THREE = 3; 51 constexpr size_t ARGS_SIZE_FOUR = 4; 52 constexpr size_t ARGS_SIZE_FIVE = 5; 53 54 constexpr size_t CALLBACK_SIZE = 1; 55 56 constexpr int32_t NAPI_RETURN_ZERO = 0; 57 constexpr int32_t NAPI_RETURN_ONE = 1; 58 59 bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType); 60 bool ParseElementName(napi_env env, AppExecFwk::ElementName &elementName, napi_value args); 61 bool ParseLong(napi_env env, int64_t ¶m, napi_value args); 62 bool ParseInt(napi_env env, int32_t ¶m, napi_value args); 63 bool ParseString(napi_env env, std::string ¶m, napi_value args); 64 bool GetStringFromNAPI(napi_env env, napi_value value, std::string &resultStr); 65 void NativeVoidCallbackComplete(napi_env env, napi_status status, void *data); 66 napi_value HandleAsyncWork(napi_env env, AsyncCallbackInfo *context, const std::string &workName, 67 napi_async_execute_callback execute, napi_async_complete_callback complete); 68 void NativeBoolCallbackComplete(napi_env env, napi_status status, void *data); 69 void NativeStringCallbackComplete(napi_env env, napi_status status, void *data); 70 } // namespace EDM 71 } // namespace OHOS 72 73 #endif // INTERFACES_KITS_COMMON_INCLUDE_NAPI_EDM_COMMON_ADDON_H 74