11ebd3d54Sopenharmony_ci/*
21ebd3d54Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
31ebd3d54Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
41ebd3d54Sopenharmony_ci * you may not use this file except in compliance with the License.
51ebd3d54Sopenharmony_ci * You may obtain a copy of the License at
61ebd3d54Sopenharmony_ci *
71ebd3d54Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
81ebd3d54Sopenharmony_ci *
91ebd3d54Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
101ebd3d54Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
111ebd3d54Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121ebd3d54Sopenharmony_ci * See the License for the specific language governing permissions and
131ebd3d54Sopenharmony_ci * limitations under the License.
141ebd3d54Sopenharmony_ci */
151ebd3d54Sopenharmony_ci
161ebd3d54Sopenharmony_ci#ifndef FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_INTERFACES_KITS_NAPI_INCLUDE_COMMON_H
171ebd3d54Sopenharmony_ci#define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_INTERFACES_KITS_NAPI_INCLUDE_COMMON_H
181ebd3d54Sopenharmony_ci
191ebd3d54Sopenharmony_ci#include <string_ex.h>
201ebd3d54Sopenharmony_ci#include <memory>
211ebd3d54Sopenharmony_ci
221ebd3d54Sopenharmony_ci#include "napi/native_api.h"
231ebd3d54Sopenharmony_ci#include "napi/native_node_api.h"
241ebd3d54Sopenharmony_ci
251ebd3d54Sopenharmony_ci#include "bgtaskmgr_inner_errors.h"
261ebd3d54Sopenharmony_ci#include "delay_suspend_info.h"
271ebd3d54Sopenharmony_ci
281ebd3d54Sopenharmony_ci#define BGTASK_NAPI_CALL_BASE(env, theCall, retVal) \
291ebd3d54Sopenharmony_ci    do {                                     \
301ebd3d54Sopenharmony_ci        if ((theCall) != napi_ok) {          \
311ebd3d54Sopenharmony_ci            return retVal;                   \
321ebd3d54Sopenharmony_ci        }                                    \
331ebd3d54Sopenharmony_ci    } while (0)
341ebd3d54Sopenharmony_ci
351ebd3d54Sopenharmony_ci#define BGTASK_NAPI_CALL(env, theCall) BGTASK_NAPI_CALL_BASE(env, theCall, nullptr)
361ebd3d54Sopenharmony_ci
371ebd3d54Sopenharmony_cinamespace OHOS {
381ebd3d54Sopenharmony_cinamespace BackgroundTaskMgr {
391ebd3d54Sopenharmony_cistruct AsyncWorkData {
401ebd3d54Sopenharmony_ci    explicit AsyncWorkData(napi_env napiEnv);
411ebd3d54Sopenharmony_ci    virtual ~AsyncWorkData();
421ebd3d54Sopenharmony_ci    napi_env env;
431ebd3d54Sopenharmony_ci    napi_async_work asyncWork = nullptr;
441ebd3d54Sopenharmony_ci    napi_deferred deferred = nullptr;
451ebd3d54Sopenharmony_ci    napi_ref callback = nullptr;
461ebd3d54Sopenharmony_ci    bool isCallback = false;
471ebd3d54Sopenharmony_ci    int32_t errCode = 0;
481ebd3d54Sopenharmony_ci};
491ebd3d54Sopenharmony_ci
501ebd3d54Sopenharmony_ciclass Common {
511ebd3d54Sopenharmony_cipublic:
521ebd3d54Sopenharmony_ci    static napi_value NapiGetboolean(const napi_env &env, const bool isValue);
531ebd3d54Sopenharmony_ci
541ebd3d54Sopenharmony_ci    static napi_value NapiGetNull(napi_env env);
551ebd3d54Sopenharmony_ci
561ebd3d54Sopenharmony_ci    static napi_value NapiGetUndefined(napi_env env);
571ebd3d54Sopenharmony_ci
581ebd3d54Sopenharmony_ci    static napi_value GetCallbackErrorValue(napi_env env, int32_t errCode);
591ebd3d54Sopenharmony_ci
601ebd3d54Sopenharmony_ci    static napi_value GetExpireCallbackValue(napi_env env, int32_t errCode, const napi_value &value);
611ebd3d54Sopenharmony_ci
621ebd3d54Sopenharmony_ci    static void PaddingAsyncWorkData(
631ebd3d54Sopenharmony_ci        const napi_env &env, const napi_ref &callback, AsyncWorkData &info, napi_value &promise);
641ebd3d54Sopenharmony_ci
651ebd3d54Sopenharmony_ci    static napi_value GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg);
661ebd3d54Sopenharmony_ci
671ebd3d54Sopenharmony_ci    static void SetCallback(const napi_env &env, const napi_ref &callbackIn, const napi_value &result);
681ebd3d54Sopenharmony_ci
691ebd3d54Sopenharmony_ci    static void SetCallback(const napi_env &env, const napi_ref &callbackIn,
701ebd3d54Sopenharmony_ci        const int32_t &errCode, const napi_value &result);
711ebd3d54Sopenharmony_ci
721ebd3d54Sopenharmony_ci    static napi_value SetPromise(const napi_env &env, const AsyncWorkData &info, const napi_value &result);
731ebd3d54Sopenharmony_ci
741ebd3d54Sopenharmony_ci    static void ReturnCallbackPromise(const napi_env &env, const AsyncWorkData &info,
751ebd3d54Sopenharmony_ci        const napi_value &result);
761ebd3d54Sopenharmony_ci
771ebd3d54Sopenharmony_ci    static napi_value JSParaError(const napi_env &env, const napi_ref &callback);
781ebd3d54Sopenharmony_ci
791ebd3d54Sopenharmony_ci    static napi_value GetU16StringValue(const napi_env &env, const napi_value &value, std::u16string &result);
801ebd3d54Sopenharmony_ci
811ebd3d54Sopenharmony_ci    static napi_value GetInt32NumberValue(const napi_env &env, const napi_value &value, int32_t &result);
821ebd3d54Sopenharmony_ci
831ebd3d54Sopenharmony_ci    static napi_value SetDelaySuspendInfo(
841ebd3d54Sopenharmony_ci        const napi_env &env, std::shared_ptr<DelaySuspendInfo>& delaySuspendInfo, napi_value &result);
851ebd3d54Sopenharmony_ci
861ebd3d54Sopenharmony_ci    static napi_value GetStringValue(const napi_env &env, const napi_value &value, std::string &result);
871ebd3d54Sopenharmony_ci
881ebd3d54Sopenharmony_ci    static void HandleErrCode(const napi_env &env, int32_t errCode, bool isThrow);
891ebd3d54Sopenharmony_ci
901ebd3d54Sopenharmony_ci    static bool HandleParamErr(const napi_env &env, int32_t errCode, bool isThrow);
911ebd3d54Sopenharmony_ci
921ebd3d54Sopenharmony_ci    static std::string FindErrMsg(const napi_env &env, const int32_t errCode);
931ebd3d54Sopenharmony_ci
941ebd3d54Sopenharmony_ci    static int32_t FindErrCode(const napi_env &env, const int32_t errCodeIn);
951ebd3d54Sopenharmony_ci
961ebd3d54Sopenharmony_ci    static napi_value GetBooleanValue(const napi_env &env, const napi_value &value, bool &result);
971ebd3d54Sopenharmony_ci};
981ebd3d54Sopenharmony_ci}  // namespace BackgroundTaskMgr
991ebd3d54Sopenharmony_ci}  // namespace OHOS
1001ebd3d54Sopenharmony_ci#endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_INTERFACES_KITS_NAPI_INCLUDE_COMMON_H