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 RINGTONE_COMMON_NAPI_H_ 17 #define RINGTONE_COMMON_NAPI_H_ 18 19 #include <string> 20 21 #include "format.h" 22 23 #include "napi/native_api.h" 24 #include "napi/native_node_api.h" 25 26 27 #define THROW_ERROR_ASSERT(env, assertion, code) \ 28 do { \ 29 if (!(assertion)) { \ 30 RingtoneCommonNapi::ThrowError(env, code); \ 31 return nullptr; \ 32 } \ 33 } while (0) 34 35 namespace OHOS { 36 namespace Media { 37 const int32_t NAPI_ERR_INPUT_INVALID = 401; 38 const int32_t NAPI_ERR_INVALID_PARAM = 6800101; 39 const int32_t NAPI_ERR_NO_MEMORY = 6800102; 40 const int32_t NAPI_ERR_UNSUPPORTED = 6800104; 41 const int32_t NAPI_ERR_SYSTEM = 6800301; 42 43 const std::string NAPI_ERR_INPUT_INVALID_INFO = "input parameter type or number mismatch"; 44 const std::string NAPI_ERR_INVALID_PARAM_INFO = "invalid parameter"; 45 const std::string NAPI_ERR_NO_MEMORY_INFO = "allocate memory failed"; 46 const std::string NAPI_ERR_SYSTEM_INFO = "system error"; 47 48 class RingtoneCommonNapi { 49 public: 50 RingtoneCommonNapi() = delete; 51 ~RingtoneCommonNapi() = delete; 52 static std::string GetStringArgument(napi_env env, napi_value value); 53 static void ThrowError(napi_env env, int32_t code); 54 static std::string GetMessageByCode(int32_t &code); 55 }; 56 57 struct AutoRef { AutoRefAutoRef58 AutoRef(napi_env env, napi_ref cb) 59 : env_(env), cb_(cb) 60 { 61 } ~AutoRefAutoRef62 ~AutoRef() 63 { 64 if (env_ != nullptr && cb_ != nullptr) { 65 (void)napi_delete_reference(env_, cb_); 66 } 67 } 68 napi_env env_; 69 napi_ref cb_; 70 }; 71 } // namespace Media 72 } // namespace OHOS 73 #endif /* RINGTONE_COMMON_NAPI_H_ */