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 #ifndef FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_INTERFACES_KITS_NAPI_INCLUDE_COMMON 16 #define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_INTERFACES_KITS_NAPI_INCLUDE_COMMON 17 18 #include <cstdint> 19 #include <cstddef> 20 #include <memory> 21 #include <string> 22 23 #include "napi/native_api.h" 24 #include "napi/native_node_api.h" 25 #include "work_info.h" 26 27 namespace OHOS { 28 namespace WorkScheduler { 29 struct AsyncWorkData { 30 explicit AsyncWorkData(napi_env napiEnv); 31 virtual ~AsyncWorkData(); 32 napi_env env; 33 napi_async_work asyncWork = nullptr; 34 napi_deferred deferred = nullptr; 35 napi_ref callback = nullptr; 36 bool isCallback = false; 37 int32_t errorCode = 0; 38 std::string errorMsg = ""; 39 }; 40 41 class Common { 42 public: 43 /** 44 * @brief Napi get null. 45 * 46 * @param env The env. 47 * @return The result. 48 */ 49 static napi_value NapiGetNull(napi_env env); 50 /** 51 * @brief Get the info of work. 52 * 53 * @param env The env. 54 * @param objValue The obj value. 55 * @param workInfo The info of work. 56 * @return True if success,else false 57 */ 58 static bool GetWorkInfo(napi_env env, napi_value objValue, WorkInfo &workInfo); 59 /** 60 * @brief Get int property. 61 * 62 * @param env The env. 63 * @param object The object. 64 * @param propertyName The name of property. 65 * @param errCode Throw the errCode if failed. 66 * @return Int value. 67 */ 68 static int32_t GetIntProperty(napi_env env, napi_value object, 69 const std::string &propertyName, ErrCode errCode); 70 /** 71 * @brief Get bool property. 72 * 73 * @param env The env. 74 * @param object The object. 75 * @param propertyName The name of property. 76 * @param errCode Throw the errCode if failed. 77 * @return True if success,else false 78 */ 79 static bool GetBoolProperty(napi_env env, napi_value object, 80 const std::string &propertyName, ErrCode errCode); 81 /** 82 * @brief Get bool to int property. 83 * 84 * @param env The env. 85 * @param object The object. 86 * @param propertyName The name of property. 87 * @param errCode Throw the errCode if failed. 88 * @return Bool to int property. 89 */ 90 static int32_t GetBoolToIntProperty(napi_env env, napi_value object, 91 const std::string &propertyName, ErrCode errCode); 92 /** 93 * @brief Get string property. 94 * 95 * @param env The env. 96 * @param object The object. 97 * @param propertyName The name of property. 98 * @param errCode Throw the errCode if failed. 99 * @return String property. 100 */ 101 static std::string GetStringProperty(napi_env env, napi_value object, 102 const std::string &propertyName, ErrCode errCode); 103 /** 104 * @brief Match value type. 105 * 106 * @param env The env. 107 * @param value The value. 108 * @param targetType The target type. 109 * @return True if success,else false 110 */ 111 static bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType); 112 /** 113 * @brief JS para error. 114 * 115 * @param env The env. 116 * @param callback The callback. 117 * @return Result or promise. 118 */ 119 static napi_value JSParaError(const napi_env &env, const napi_ref &callback); 120 /** 121 * @brief Padding callback promise info. 122 * 123 * @param env The env 124 * @param callback The callback. 125 * @param info The info. 126 * @param promise The promise. 127 */ 128 static void PaddingAsyncWorkData( 129 const napi_env &env, const napi_ref &callback, AsyncWorkData &info, napi_value &promise); 130 /** 131 * @brief Get the workInfo of napi. 132 * 133 * @param env The env. 134 * @param workInfo The info of work. 135 * @return Napi work info. 136 */ 137 static napi_value GetNapiWorkInfo(napi_env env, std::shared_ptr<WorkInfo> &workInfo); 138 /** 139 * @brief Get callback error value. 140 * 141 * @param env The env. 142 * @param errCode The error code. 143 * @param errMsg The error message. 144 * @return Callback error value. 145 */ 146 static napi_value GetCallbackErrorValue(napi_env env, int32_t errCode, const std::string errMsg); 147 /** 148 * @brief Set callback. 149 * 150 * @param env The env. 151 * @param callbackIn The callback. 152 * @param errCode The errCode. 153 * @param result The result. 154 */ 155 static void SetCallback(const napi_env &env, const napi_ref &callbackIn, 156 int32_t errCode, const napi_value &result); 157 /** 158 * @brief Set promise. 159 * 160 * @param env The env. 161 * @param info The info. 162 * @param result The result. 163 * @return The result. 164 */ 165 static napi_value SetPromise(const napi_env &env, const AsyncWorkData &info, const napi_value &result); 166 /** 167 * @brief Return callback promise. 168 * 169 * @param env The env. 170 * @param info The info. 171 * @param result The result. 172 */ 173 static void ReturnCallbackPromise(const napi_env &env, const AsyncWorkData &info, 174 const napi_value &result); 175 /** 176 * @brief Handle error code and throw error. 177 * 178 * @param env The env. 179 * @param errCode The error code. 180 */ 181 static void HandleErrCode(const napi_env &env, int32_t errCode); 182 /** 183 * @brief Handle param error code and throw param error. 184 * 185 * @param env The env. 186 * @param errCode The error code. 187 */ 188 static void HandleParamErr(const napi_env &env, int32_t errCode); 189 /** 190 * @brief Find error message by code. 191 * 192 * @param env The env. 193 * @param errCode The error code. 194 */ 195 static std::string FindErrMsg(const napi_env &env, int32_t errCode); 196 /** 197 * @brief Find the error code actually reported by code. 198 * 199 * @param env The env. 200 * @param errCode The error code. 201 */ 202 static int32_t FindErrCode(const napi_env &env, int32_t errCodeIn); 203 204 private: 205 static bool GetBaseWorkInfo(napi_env env, napi_value objValue, WorkInfo &workInfo); 206 static bool GetNetWorkInfo(napi_env env, napi_value objValue, WorkInfo &workInfo); 207 static bool GetChargeInfo(napi_env env, napi_value objValue, WorkInfo &workInfo); 208 static bool GetBatteryInfo(napi_env env, napi_value objValue, WorkInfo &workInfo); 209 static bool GetStorageInfo(napi_env env, napi_value objValue, WorkInfo &workInfo); 210 static bool GetRepeatInfo(napi_env env, napi_value objValue, WorkInfo &workInfo); 211 static bool GetExtrasInfo(napi_env env, napi_value objValue, WorkInfo &workInfo); 212 }; 213 } // namespace WorkScheduler 214 } // namespace OHOS 215 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_INTERFACES_KITS_NAPI_INCLUDE_COMMON