1 /* 2 * Copyright (C) 2022 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 NAPI_UTILS_H 17 #define NAPI_UTILS_H 18 19 #include <map> 20 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 namespace OHOS { 24 namespace MiscServices { 25 namespace Time { 26 #define NAPI_ASSERTS_BASE_RETURN(env, assertion, code, message, retVal) \ 27 do { \ 28 if (!(assertion)) { \ 29 NapiUtils::ThrowError(env, message, code); \ 30 return retVal; \ 31 } \ 32 } while (0) 33 34 #define NAPI_ASSERTP_RETURN(env, assertion, message) \ 35 NAPI_ASSERTS_BASE_RETURN(env, assertion, ERROR, message, nullptr) 36 37 /* check condition related to argc/argv, return and logging. */ 38 #define CHECK_ARGS_RETURN_VOID(module, context, condition, message, code) \ 39 do { \ 40 if (!(condition)) { \ 41 (context)->status = napi_invalid_arg; \ 42 (context)->errMessage = std::string(message); \ 43 (context)->errCode = code; \ 44 TIME_HILOGE(module, "test (" #condition ") failed: %{public}s", (context)->errMessage.c_str()); \ 45 return; \ 46 } \ 47 } while (0) 48 49 #define CHECK_STATUS_RETURN_VOID(module, context, message, code) \ 50 do { \ 51 if ((context)->status != napi_ok) { \ 52 (context)->errMessage = std::string(message); \ 53 (context)->errCode = code; \ 54 TIME_HILOGE(module, "test (context->status == napi_ok) failed: %{public}s", \ 55 (context)->errMessage.c_str()); \ 56 return; \ 57 } \ 58 } while (0) 59 60 /* check condition, return and logging if condition not true. */ 61 #define CHECK_RETURN(module, condition, message, retVal) \ 62 do { \ 63 if (!(condition)) { \ 64 TIME_HILOGE(module, "test (" #condition ") failed: " message); \ 65 return retVal; \ 66 } \ 67 } while (0) 68 69 #define CHECK_RETURN_VOID(module, condition, message) \ 70 do { \ 71 if (!(condition)) { \ 72 TIME_HILOGE(module, "test (" #condition ") failed: " message); \ 73 return; \ 74 } \ 75 } while (0) 76 77 constexpr size_t ARGC_ZERO = 0; 78 constexpr size_t ARGC_ONE = 1; 79 constexpr size_t ARGC_TWO = 2; 80 constexpr size_t ARGC_THREE = 3; 81 82 constexpr size_t ARGV_FIRST = 0; 83 constexpr size_t ARGV_SECOND = 1; 84 constexpr size_t ARGV_THIRD = 2; 85 86 constexpr int32_t SET_TIME_MAX_PARA = 2; 87 constexpr int32_t SET_TIMEZONE_MAX_PARA = 2; 88 89 constexpr int32_t CREATE_MAX_PARA = 2; 90 constexpr int32_t START_MAX_PARA = 3; 91 constexpr int32_t STOP_MAX_PARA = 2; 92 constexpr int32_t DESTROY_MAX_PARA = 2; 93 94 constexpr int32_t MAX_TIME_ZONE_ID = 1024; 95 constexpr int32_t INVALID_TIME = -1; 96 97 enum JsErrorCode : int32_t { 98 ERROR_OK = 0, 99 ERROR = -1, 100 PERMISSION_ERROR = 201, 101 SYSTEM_APP_ERROR = 202, 102 PARAMETER_ERROR = 401, 103 NTP_UPDATE_ERROR = 13000001, 104 NTP_NOT_UPDATE_ERROR = 13000002, 105 }; 106 107 struct CallbackPromiseInfo { 108 napi_ref callback = nullptr; 109 napi_deferred deferred = nullptr; 110 bool isCallback = false; 111 int errorCode = JsErrorCode::ERROR_OK; 112 std::string message; 113 }; 114 115 const std::map<int32_t, std::string> CODE_TO_MESSAGE = { 116 { JsErrorCode::SYSTEM_APP_ERROR, "Permission verification failed. A non-system application calls a system API" }, 117 { JsErrorCode::PARAMETER_ERROR, "Parameter error" }, 118 { JsErrorCode::PERMISSION_ERROR, "Permission denied" }, 119 { JsErrorCode::ERROR, "Parameter check failed, permission denied, or system error." }, 120 }; 121 122 class NapiUtils { 123 public: 124 static napi_value GetCallbackErrorValue(napi_env env, int32_t errCode, const std::string &message); 125 static napi_value NapiGetNull(napi_env env); 126 static void SetPromise(napi_env env, const napi_deferred deferred, int errorCode, const std::string &message, 127 napi_value result); 128 static void SetCallback(napi_env env, napi_ref callbackIn, int32_t errorCode, const std::string &message, 129 napi_value result); 130 static void ReturnCallbackPromise(napi_env env, const CallbackPromiseInfo &info, napi_value result); 131 static napi_value JSParaError(napi_env env, napi_ref callback); 132 static napi_value ParseParametersBySetTime(napi_env env, const napi_value (&argv)[SET_TIME_MAX_PARA], size_t argc, 133 int64_t ×, napi_ref &callback); 134 static napi_value ParseParametersBySetTimezone(napi_env env, const napi_value (&argv)[SET_TIMEZONE_MAX_PARA], 135 size_t argc, std::string &timezoneId, napi_ref &callback); 136 static napi_value ParseParametersGet(napi_env env, const napi_value (&argv)[SET_TIMEZONE_MAX_PARA], size_t argc, 137 napi_ref &callback); 138 static napi_value ParseParametersGetNA(napi_env env, const napi_value (&argv)[SET_TIMEZONE_MAX_PARA], size_t argc, 139 napi_ref &callback, bool *isNano); 140 static int32_t ConvertErrorCode(int32_t timeErrorCode); 141 static napi_value CreateNapiNumber(napi_env env, int32_t objName); 142 static napi_value GetUndefinedValue(napi_env env); 143 static napi_status GetValue(napi_env env, napi_value in, std::string &out); 144 static napi_status ThrowError(napi_env env, const char *napiMessage, int32_t napiCode); 145 static std::string GetErrorMessage(int32_t errCode); 146 }; 147 } // namespace Time 148 } // namespace MiscServices 149 } // namespace OHOS 150 151 #endif // NAPI_UTILS_H