• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 "continuous_task_info.h"
27 #include "delay_suspend_info.h"
28 #include "efficiency_resource_info.h"
29 
30 #define BGTASK_NAPI_CALL_BASE(env, theCall, retVal) \
31     do {                                     \
32         if ((theCall) != napi_ok) {          \
33             return retVal;                   \
34         }                                    \
35     } while (0)
36 
37 #define BGTASK_NAPI_CALL(env, theCall) BGTASK_NAPI_CALL_BASE(env, theCall, nullptr)
38 
39 namespace OHOS {
40 namespace BackgroundTaskMgr {
41 struct AsyncWorkData {
42     explicit AsyncWorkData(napi_env napiEnv);
43     virtual ~AsyncWorkData();
44     napi_env env;
45     napi_async_work asyncWork = nullptr;
46     napi_deferred deferred = nullptr;
47     napi_ref callback = nullptr;
48     bool isCallback = false;
49     int32_t errCode = 0;
50 };
51 
52 class Common {
53 public:
54     static napi_value NapiGetboolean(const napi_env &env, const bool isValue);
55 
56     static napi_value NapiGetNull(napi_env env);
57 
58     static napi_value NapiGetUndefined(napi_env env);
59 
60     static napi_value GetCallbackErrorValue(napi_env env, int32_t errCode);
61 
62     static napi_value GetExpireCallbackValue(napi_env env, int32_t errCode, const napi_value &value);
63 
64     static void PaddingAsyncWorkData(
65         const napi_env &env, const napi_ref &callback, AsyncWorkData &info, napi_value &promise);
66 
67     static napi_value GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg);
68 
69     static void SetCallback(const napi_env &env, const napi_ref &callbackIn, const napi_value &result);
70 
71     static void SetCallback(const napi_env &env, const napi_ref &callbackIn,
72         const int32_t &errCode, const napi_value &result);
73 
74     static napi_value SetPromise(const napi_env &env, const AsyncWorkData &info, const napi_value &result);
75 
76     static void ReturnCallbackPromise(const napi_env &env, const AsyncWorkData &info,
77         const napi_value &result);
78 
79     static napi_value JSParaError(const napi_env &env, const napi_ref &callback);
80 
81     static napi_value GetU16StringValue(const napi_env &env, const napi_value &value, std::u16string &result);
82 
83     static napi_value GetInt32NumberValue(const napi_env &env, const napi_value &value, int32_t &result);
84 
85     static napi_value SetDelaySuspendInfo(
86         const napi_env &env, std::shared_ptr<DelaySuspendInfo>& delaySuspendInfo, napi_value &result);
87 
88     static napi_value GetStringValue(const napi_env &env, const napi_value &value, std::string &result);
89 
90     static void HandleErrCode(const napi_env &env, int32_t errCode, bool isThrow);
91 
92     static bool HandleParamErr(const napi_env &env, int32_t errCode, bool isThrow);
93 
94     static std::string FindErrMsg(const napi_env &env, const int32_t errCode);
95 
96     static int32_t FindErrCode(const napi_env &env, const int32_t errCodeIn);
97 
98     static napi_value GetBooleanValue(const napi_env &env, const napi_value &value, bool &result);
99 
100     static napi_value GetNapiContinuousTaskInfo(napi_env env,
101         const std::shared_ptr<ContinuousTaskInfo> &continuousTaskInfo);
102 
103     static napi_value NapiSetBgTaskMode(napi_env env, napi_value napiInfo,
104         const std::shared_ptr<ContinuousTaskInfo> &continuousTaskInfo);
105 
106     static napi_value GetNapiDelaySuspendInfo(napi_env env,
107         const std::shared_ptr<DelaySuspendInfo> &delaySuspendInfo);
108 
109     static napi_value GetNapiEfficiencyResourcesInfo(const napi_env &env,
110         std::shared_ptr<EfficiencyResourceInfo> efficiencyResourceInfo);
111 };
112 }  // namespace BackgroundTaskMgr
113 }  // namespace OHOS
114 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_INTERFACES_KITS_NAPI_INCLUDE_COMMON_H