1 /* 2 * Copyright (c) 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 AUDIO_HAPTIC_COMMON_NAPI_H 17 #define AUDIO_HAPTIC_COMMON_NAPI_H 18 19 #include <string> 20 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 24 namespace OHOS { 25 namespace Media { 26 const int32_t NAPI_ERR_INPUT_INVALID = 401; 27 const int32_t NAPI_ERR_OPERATE_NOT_ALLOWED = 5400102; 28 const int32_t NAPI_ERR_IO_ERROR = 5400103; 29 const int32_t NAPI_ERR_SERVICE_DIED = 5400105; 30 const int32_t NAPI_ERR_UNSUPPORTED_FORMAT = 5400106; 31 const int32_t NAPI_ERR_PARAM_OUT_OF_RANGE = 5400108; 32 const int32_t NAPI_ERR_PERMISSION_DENIED = 202; 33 const int32_t NAPI_ERR_NOT_SUPPORTED = 801; 34 35 /* Constants for array index */ 36 const int32_t PARAM0 = 0; 37 const int32_t PARAM1 = 1; 38 const int32_t PARAM2 = 2; 39 40 /* Constants for array size */ 41 const int32_t ARGS_ZERO = 0; 42 const int32_t ARGS_ONE = 1; 43 const int32_t ARGS_TWO = 2; 44 const int32_t ARGS_THREE = 3; 45 46 struct AsyncContext { 47 napi_async_work work; 48 napi_deferred deferred = nullptr; 49 napi_value argv[ARGS_THREE] = {0}; 50 void* objectInfo = nullptr; 51 }; 52 53 class AudioHapticCommonNapi { 54 public: 55 AudioHapticCommonNapi() = delete; 56 ~AudioHapticCommonNapi() = delete; 57 static void ThrowError(napi_env env, int32_t code); 58 static void ThrowError(napi_env env, int32_t code, const std::string &errMessage); 59 static std::string GetMessageByCode(int32_t &code); 60 static std::string GetStringArgument(napi_env env, napi_value value); 61 static void PromiseReject(napi_env env, napi_deferred deferred, 62 const int32_t &errCode, const std::string &errMessage); 63 static void PromiseReject(napi_env env, napi_deferred deferred, int32_t errCode); 64 static bool InitPromiseFunc(napi_env env, napi_callback_info info, 65 AsyncContext* asyncContext, napi_value* promise, size_t paramLength); 66 static bool VerifySelfSystemPermission(); 67 static bool InitNormalFunc(napi_env env, napi_callback_info info, 68 void **native, napi_value *argv, size_t paramLength); 69 }; 70 71 struct AutoRef { AutoRefAutoRef72 AutoRef(napi_env env, napi_ref cb) 73 : env_(env), cb_(cb) 74 { 75 } ~AutoRefAutoRef76 ~AutoRef() 77 { 78 if (env_ != nullptr && cb_ != nullptr) { 79 (void)napi_delete_reference(env_, cb_); 80 } 81 } 82 napi_env env_; 83 napi_ref cb_; 84 }; 85 } // namespace Media 86 } // namespace OHOS 87 #endif // AUDIO_HAPTIC_COMMON_NAPI_H