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 FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_INTERFACES_KITS_NAPI_INCLUDE_COMMON_H 17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_INTERFACES_KITS_NAPI_INCLUDE_COMMON_H 18 19 #include <string_ex.h> 20 #include <memory> 21 22 #include "napi/native_api.h" 23 #include "napi/native_node_api.h" 24 25 #include "bgtaskmgr_inner_errors.h" 26 #include "delay_suspend_info.h" 27 28 namespace OHOS { 29 namespace BackgroundTaskMgr { 30 struct AsyncWorkData { 31 explicit AsyncWorkData(napi_env napiEnv); 32 virtual ~AsyncWorkData(); 33 napi_env env; 34 napi_async_work asyncWork = nullptr; 35 napi_deferred deferred = nullptr; 36 napi_ref callback = nullptr; 37 bool isCallback = false; 38 int32_t errCode = 0; 39 }; 40 41 class Common { 42 public: 43 static napi_value NapiGetboolean(const napi_env &env, const bool isValue); 44 45 static napi_value NapiGetNull(napi_env env); 46 47 static napi_value NapiGetUndefined(napi_env env); 48 49 static napi_value GetCallbackErrorValue(napi_env env, int32_t errCode); 50 51 static napi_value GetExpireCallbackValue(napi_env env, int32_t errCode, const napi_value &value); 52 53 static void PaddingAsyncWorkData( 54 const napi_env &env, const napi_ref &callback, AsyncWorkData &info, napi_value &promise); 55 56 static napi_value GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg); 57 58 static void SetCallback(const napi_env &env, const napi_ref &callbackIn, const napi_value &result); 59 60 static void SetCallback(const napi_env &env, const napi_ref &callbackIn, 61 const int32_t &errCode, const napi_value &result); 62 63 static napi_value SetPromise(const napi_env &env, const AsyncWorkData &info, const napi_value &result); 64 65 static void ReturnCallbackPromise(const napi_env &env, const AsyncWorkData &info, 66 const napi_value &result); 67 68 static napi_value JSParaError(const napi_env &env, const napi_ref &callback); 69 70 static napi_value GetU16StringValue(const napi_env &env, const napi_value &value, std::u16string &result); 71 72 static napi_value GetInt32NumberValue(const napi_env &env, const napi_value &value, int32_t &result); 73 74 static napi_value SetDelaySuspendInfo( 75 const napi_env &env, std::shared_ptr<DelaySuspendInfo>& delaySuspendInfo, napi_value &result); 76 77 static napi_value GetStringValue(const napi_env &env, const napi_value &value, std::string &result); 78 79 static void HandleErrCode(const napi_env &env, int32_t errCode, bool isThrow); 80 81 static bool HandleParamErr(const napi_env &env, int32_t errCode, bool isThrow); 82 83 static std::string FindErrMsg(const napi_env &env, const int32_t errCode); 84 85 static int32_t FindErrCode(const napi_env &env, const int32_t errCodeIn); 86 87 static napi_value GetBooleanValue(const napi_env &env, const napi_value &value, bool &result); 88 }; 89 } // namespace BackgroundTaskMgr 90 } // namespace OHOS 91 #endif // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_INTERFACES_KITS_NAPI_INCLUDE_COMMON_H