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 CUSTOM = 100, 40 }; 41 42 enum class MethodAttribute { 43 HANDLE = 0, 44 GET = 1, 45 OPERATE_ADMIN = 2, 46 }; 47 48 struct BusinessError { 49 int32_t errorCode = 0; 50 std::string errorMessage; 51 }; 52 53 struct AddonMethodSign; 54 55 typedef bool (*JsArgToData)(napi_env env, napi_value argv, MessageParcel &data, const AddonMethodSign &methodSign); 56 57 struct AddonMethodSign { 58 std::string name; 59 // Parameter type 60 std::vector<EdmAddonCommonType> argsType; 61 // Parameter handler pointer 62 std::vector<JsArgToData> argsConvert; 63 std::string apiVersionTag; 64 MethodAttribute methodAttribute = MethodAttribute::GET; 65 int32_t defaultArgSize = 0; 66 int32_t policyCode = 0; 67 }; 68 69 struct AsyncAddonData { 70 napi_async_work asyncWork = nullptr; 71 napi_deferred deferred = nullptr; 72 napi_ref callback = nullptr; 73 }; 74 75 struct AdapterAddonData : public AsyncCallbackInfo { 76 // Business parameters 77 MessageParcel data; 78 // Business callback error 79 BusinessError error; 80 // Business callback result data 81 napi_value result; 82 }; 83 84 napi_value JsObjectToData(napi_env env, napi_callback_info info, const AddonMethodSign &methodSign, 85 AdapterAddonData *addonData); 86 87 napi_value AddonMethodAdapter(napi_env env, napi_callback_info info, const AddonMethodSign &methodSign, 88 napi_async_execute_callback execute, napi_async_complete_callback complete); 89 } // namespace EDM 90 } // namespace OHOS 91 92 #endif // INTERFACES_KITS_COMMON_INCLUDE_NAPI_EDM_ADAPTER_H 93