1 /* 2 * Copyright (C) 2024 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_JS_PICKER_INCLUDE_PICKER_NAPI_UTILS_H 17 #define INTERFACES_KITS_JS_PICKER_INCLUDE_PICKER_NAPI_UTILS_H 18 19 #include <memory> 20 #include <vector> 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 #include "hilog_wrapper.h" 24 25 namespace OHOS { 26 namespace Picker { 27 /* Constants for array index */ 28 const int32_t PARAM0 = 0; 29 const int32_t PARAM1 = 1; 30 const int32_t PARAM2 = 2; 31 const int32_t PARAM3 = 3; 32 const int32_t PARAM4 = 4; 33 34 /* Constants for array size */ 35 const int32_t ARGS_ZERO = 0; 36 const int32_t ARGS_ONE = 1; 37 const int32_t ARGS_TWO = 2; 38 const int32_t ARGS_THREE = 3; 39 const int32_t ARGS_FOUR = 4; 40 const int32_t ARGS_FIVE = 5; 41 const int32_t ARG_BUF_SIZE = 384; // 256 for display name and 128 for relative path 42 constexpr uint32_t NAPI_INIT_REF_COUNT = 1; 43 44 constexpr size_t NAPI_ARGC_MAX = 5; 45 46 // Error codes 47 const int32_t ERR_OK = 0; 48 const int32_t ERR_MEM_ALLOCATION = 2; 49 const int32_t ERR_INVALID_OUTPUT = 3; 50 const int32_t ERR_INV = -1; 51 52 struct JSAsyncContextOutput { 53 napi_value error; 54 napi_value data; 55 bool status; 56 }; 57 58 struct NapiClassInfo { 59 std::string name; 60 napi_ref *ref; 61 napi_value (*constructor)(napi_env, napi_callback_info); 62 std::vector<napi_property_descriptor> props; 63 }; 64 65 /* Util class used by napi asynchronous methods for making call to js callback function */ 66 class PickerNapiUtils { 67 public: 68 static void CreateNapiErrorObject(napi_env env, napi_value &errorObj, const int32_t errCode, 69 const std::string errMsg); 70 static void InvokeJSAsyncMethod(napi_env env, napi_deferred deferred, napi_ref callbackRef, napi_async_work work, 71 const JSAsyncContextOutput &asyncContext); 72 template <class AsyncContext> 73 static napi_value NapiCreateAsyncWork(napi_env env, std::unique_ptr<AsyncContext> &asyncContext, 74 const std::string &resourceName, void (*execute)(napi_env, void *), 75 void (*complete)(napi_env, napi_status, void *)); 76 }; 77 78 } // namespace Picker 79 } // namespace OHOS 80 #endif // INTERFACES_KITS_JS_PICKER_INCLUDE_PICKER_NAPI_UTILS_H 81