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 the info of condition. 140 * 141 * @param env The env. 142 * @param workInfo The info of work. 143 * @return Napi work info. 144 */ 145 static void GetConditionInfo(napi_env env, std::shared_ptr<WorkInfo> &workInfo, napi_value &napiWork); 146 /** 147 * @brief Get callback error value. 148 * 149 * @param env The env. 150 * @param errCode The error code. 151 * @param errMsg The error message. 152 * @return Callback error value. 153 */ 154 static napi_value GetCallbackErrorValue(napi_env env, int32_t errCode, const std::string errMsg); 155 /** 156 * @brief Set callback. 157 * 158 * @param env The env. 159 * @param callbackIn The callback. 160 * @param errCode The errCode. 161 * @param result The result. 162 */ 163 static void SetCallback(const napi_env &env, const napi_ref &callbackIn, 164 int32_t errCode, const napi_value &result); 165 /** 166 * @brief Set promise. 167 * 168 * @param env The env. 169 * @param info The info. 170 * @param result The result. 171 * @return The result. 172 */ 173 static napi_value SetPromise(const napi_env &env, const AsyncWorkData &info, const napi_value &result); 174 /** 175 * @brief Return callback promise. 176 * 177 * @param env The env. 178 * @param info The info. 179 * @param result The result. 180 */ 181 static void ReturnCallbackPromise(const napi_env &env, const AsyncWorkData &info, 182 const napi_value &result); 183 /** 184 * @brief Handle error code and throw error. 185 * 186 * @param env The env. 187 * @param errCode The error code. 188 */ 189 static void HandleErrCode(const napi_env &env, int32_t errCode); 190 /** 191 * @brief Handle param error code and throw param error. 192 * 193 * @param env The env. 194 * @param errCode The error code. 195 */ 196 static void HandleParamErr(const napi_env &env, int32_t errCode); 197 /** 198 * @brief Find error message by code. 199 * 200 * @param env The env. 201 * @param errCode The error code. 202 */ 203 static std::string FindErrMsg(const napi_env &env, int32_t errCode); 204 /** 205 * @brief Find the error code actually reported by code. 206 * 207 * @param env The env. 208 * @param errCode The error code. 209 */ 210 static int32_t FindErrCode(const napi_env &env, int32_t errCodeIn); 211 212 private: 213 static bool GetBaseWorkInfo(napi_env env, napi_value objValue, WorkInfo &workInfo); 214 static bool GetNetWorkInfo(napi_env env, napi_value objValue, WorkInfo &workInfo); 215 static bool GetChargeInfo(napi_env env, napi_value objValue, WorkInfo &workInfo); 216 static bool GetBatteryInfo(napi_env env, napi_value objValue, WorkInfo &workInfo); 217 static bool GetStorageInfo(napi_env env, napi_value objValue, WorkInfo &workInfo); 218 static bool GetRepeatInfo(napi_env env, napi_value objValue, WorkInfo &workInfo); 219 static bool GetExtrasInfo(napi_env env, napi_value objValue, WorkInfo &workInfo); 220 static bool GetDeepIdleInfo(napi_env env, napi_value objValue, WorkInfo &workInfo); 221 }; 222 } // namespace WorkScheduler 223 } // namespace OHOS 224 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_INTERFACES_KITS_NAPI_INCLUDE_COMMON