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#define BGTASK_NAPI_CALL_BASE(env, theCall, retVal) \
29    do {                                     \
30        if ((theCall) != napi_ok) {          \
31            return retVal;                   \
32        }                                    \
33    } while (0)
34
35#define BGTASK_NAPI_CALL(env, theCall) BGTASK_NAPI_CALL_BASE(env, theCall, nullptr)
36
37namespace OHOS {
38namespace BackgroundTaskMgr {
39struct AsyncWorkData {
40    explicit AsyncWorkData(napi_env napiEnv);
41    virtual ~AsyncWorkData();
42    napi_env env;
43    napi_async_work asyncWork = nullptr;
44    napi_deferred deferred = nullptr;
45    napi_ref callback = nullptr;
46    bool isCallback = false;
47    int32_t errCode = 0;
48};
49
50class Common {
51public:
52    static napi_value NapiGetboolean(const napi_env &env, const bool isValue);
53
54    static napi_value NapiGetNull(napi_env env);
55
56    static napi_value NapiGetUndefined(napi_env env);
57
58    static napi_value GetCallbackErrorValue(napi_env env, int32_t errCode);
59
60    static napi_value GetExpireCallbackValue(napi_env env, int32_t errCode, const napi_value &value);
61
62    static void PaddingAsyncWorkData(
63        const napi_env &env, const napi_ref &callback, AsyncWorkData &info, napi_value &promise);
64
65    static napi_value GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg);
66
67    static void SetCallback(const napi_env &env, const napi_ref &callbackIn, const napi_value &result);
68
69    static void SetCallback(const napi_env &env, const napi_ref &callbackIn,
70        const int32_t &errCode, const napi_value &result);
71
72    static napi_value SetPromise(const napi_env &env, const AsyncWorkData &info, const napi_value &result);
73
74    static void ReturnCallbackPromise(const napi_env &env, const AsyncWorkData &info,
75        const napi_value &result);
76
77    static napi_value JSParaError(const napi_env &env, const napi_ref &callback);
78
79    static napi_value GetU16StringValue(const napi_env &env, const napi_value &value, std::u16string &result);
80
81    static napi_value GetInt32NumberValue(const napi_env &env, const napi_value &value, int32_t &result);
82
83    static napi_value SetDelaySuspendInfo(
84        const napi_env &env, std::shared_ptr<DelaySuspendInfo>& delaySuspendInfo, napi_value &result);
85
86    static napi_value GetStringValue(const napi_env &env, const napi_value &value, std::string &result);
87
88    static void HandleErrCode(const napi_env &env, int32_t errCode, bool isThrow);
89
90    static bool HandleParamErr(const napi_env &env, int32_t errCode, bool isThrow);
91
92    static std::string FindErrMsg(const napi_env &env, const int32_t errCode);
93
94    static int32_t FindErrCode(const napi_env &env, const int32_t errCodeIn);
95
96    static napi_value GetBooleanValue(const napi_env &env, const napi_value &value, bool &result);
97};
98}  // namespace BackgroundTaskMgr
99}  // namespace OHOS
100#endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_INTERFACES_KITS_NAPI_INCLUDE_COMMON_H