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_COMMON_INCLUDE_NAPI_EDM_ADAPTER_H 17 #define INTERFACES_KITS_COMMON_INCLUDE_NAPI_EDM_ADAPTER_H 18 19 #include <string> 20 #include <vector> 21 #include "napi/native_api.h" 22 #include "napi/native_common.h" 23 #include "napi_edm_common.h" 24 25 namespace OHOS { 26 namespace EDM { 27 28 enum class EdmAddonCommonType { 29 VOID = 0, 30 ELEMENT_NULL = 1, 31 ELEMENT = 2, 32 UINT32 = 3, 33 STRING = 4, 34 ARRAY_STRING = 5, 35 BOOLEAN = 6, 36 USERID = 7, 37 INT32 = 8, 38 INT64 = 9, 39 ARRAY_INT32 = 10, 40 CUSTOM = 100, 41 }; 42 43 enum class MethodAttribute { 44 HANDLE = 0, 45 GET = 1, 46 OPERATE_ADMIN = 2, 47 }; 48 49 struct BusinessError { 50 int32_t errorCode = 0; 51 std::string errorMessage; 52 }; 53 54 struct AddonMethodSign; 55 56 typedef bool (*JsArgToData)(napi_env env, napi_value argv, MessageParcel &data, const AddonMethodSign &methodSign); 57 58 struct AddonMethodSign { 59 std::string name; 60 // Parameter type 61 std::vector<EdmAddonCommonType> argsType; 62 // Parameter handler pointer 63 std::vector<JsArgToData> argsConvert; 64 std::string apiVersionTag; 65 MethodAttribute methodAttribute = MethodAttribute::GET; 66 int32_t defaultArgSize = 0; 67 int32_t policyCode = 0; 68 }; 69 70 struct AsyncAddonData { 71 napi_async_work asyncWork = nullptr; 72 napi_deferred deferred = nullptr; 73 napi_ref callback = nullptr; 74 }; 75 76 struct AdapterAddonData : public AsyncCallbackInfo { 77 // Business parameters 78 MessageParcel data; 79 // Business callback error 80 BusinessError error; 81 // Business callback result data 82 napi_value result; 83 }; 84 85 napi_value JsObjectToData(napi_env env, napi_callback_info info, const AddonMethodSign &methodSign, 86 AdapterAddonData *addonData, bool isAsync = false); 87 88 napi_value AddonMethodAdapter(napi_env env, napi_callback_info info, const AddonMethodSign &methodSign, 89 napi_async_execute_callback execute, napi_async_complete_callback complete); 90 } // namespace EDM 91 } // namespace OHOS 92 93 #endif // INTERFACES_KITS_COMMON_INCLUDE_NAPI_EDM_ADAPTER_H 94