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 #include "init_bgtaskmgr.h"
17
18 #include "background_mode.h"
19 #include "background_sub_mode.h"
20 #include "bg_continuous_task_napi_module.h"
21 #include "cancel_suspend_delay.h"
22 #include "continuous_task_cancel_reason.h"
23 #include "continuous_task_suspend_reason.h"
24 #include "get_all_transient_tasks.h"
25 #include "get_remaining_delay_time.h"
26 #include "request_suspend_delay.h"
27 #include "transient_task_log.h"
28 #include "efficiency_resources_operation.h"
29 #include "resource_type.h"
30 #include "common.h"
31
32 namespace OHOS {
33 namespace BackgroundTaskMgr {
34 static constexpr char BG_TASK_SUB_MODE_TYPE[] = "subMode";
35 EXTERN_C_START
36
BackgroundTaskMgrInit(napi_env env,napi_value exports)37 napi_value BackgroundTaskMgrInit(napi_env env, napi_value exports)
38 {
39 napi_property_descriptor desc[] = {
40 DECLARE_NAPI_FUNCTION("requestSuspendDelay", RequestSuspendDelayThrow),
41 DECLARE_NAPI_FUNCTION("cancelSuspendDelay", CancelSuspendDelayThrow),
42 DECLARE_NAPI_FUNCTION("getRemainingDelayTime", GetRemainingDelayTimeThrow),
43 DECLARE_NAPI_FUNCTION("getTransientTaskInfo", GetAllTransientTasksThrow),
44 DECLARE_NAPI_FUNCTION("startBackgroundRunning", StartBackgroundRunningThrow),
45 DECLARE_NAPI_FUNCTION("updateBackgroundRunning", UpdateBackgroundRunningThrow),
46 DECLARE_NAPI_FUNCTION("stopBackgroundRunning", StopBackgroundRunningThrow),
47 DECLARE_NAPI_FUNCTION("getAllContinuousTasks", GetAllContinuousTasksThrow),
48 DECLARE_NAPI_FUNCTION("applyEfficiencyResources", ApplyEfficiencyResources),
49 DECLARE_NAPI_FUNCTION("resetAllEfficiencyResources", ResetAllEfficiencyResources),
50 DECLARE_NAPI_FUNCTION("getAllEfficiencyResources", GetAllEfficiencyResources),
51 DECLARE_NAPI_FUNCTION("on", OnOnContinuousTaskCallback),
52 DECLARE_NAPI_FUNCTION("off", OffOnContinuousTaskCallback),
53 };
54
55 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
56
57 return exports;
58 }
59
SetNamedPropertyByInteger(napi_env env,napi_value dstObj,int32_t objName,const char * propName)60 void SetNamedPropertyByInteger(napi_env env, napi_value dstObj, int32_t objName, const char *propName)
61 {
62 napi_value prop = nullptr;
63 if (napi_create_int32(env, objName, &prop) == napi_ok) {
64 napi_set_named_property(env, dstObj, propName, prop);
65 }
66 }
67
SetNamedPropertyByString(napi_env env,napi_value dstObj,const char * objName,const char * propName)68 void SetNamedPropertyByString(napi_env env, napi_value dstObj, const char *objName, const char *propName)
69 {
70 napi_value prop = nullptr;
71 if (napi_create_string_utf8(env, objName, strlen(objName), &prop) == napi_ok) {
72 napi_set_named_property(env, dstObj, propName, prop);
73 }
74 }
75
BackgroundModeInit(napi_env env,napi_value exports)76 napi_value BackgroundModeInit(napi_env env, napi_value exports)
77 {
78 napi_value obj = nullptr;
79 napi_create_object(env, &obj);
80
81 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::DATA_TRANSFER), "DATA_TRANSFER");
82 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::AUDIO_PLAYBACK), "AUDIO_PLAYBACK");
83 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::AUDIO_RECORDING),
84 "AUDIO_RECORDING");
85 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::LOCATION),
86 "LOCATION");
87 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::BLUETOOTH_INTERACTION),
88 "BLUETOOTH_INTERACTION");
89 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::MULTI_DEVICE_CONNECTION),
90 "MULTI_DEVICE_CONNECTION");
91 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::WIFI_INTERACTION), "WIFI_INTERACTION");
92 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::VOIP), "VOIP");
93 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::TASK_KEEPING), "TASK_KEEPING");
94
95 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::CPU), "CPU");
96 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::COMMON_EVENT), "COMMON_EVENT");
97 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::TIMER), "TIMER");
98 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::WORK_SCHEDULER), "WORK_SCHEDULER");
99 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::BLUETOOTH), "BLUETOOTH");
100 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::GPS), "GPS");
101 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::AUDIO), "AUDIO");
102 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::RUNNING_LOCK), "RUNNING_LOCK");
103 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::SENSOR), "SENSOR");
104
105 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundSubMode::CAR_KEY), "CAR_KEY");
106 SetNamedPropertyByString(env, obj, BG_TASK_SUB_MODE_TYPE, "SUB_MODE");
107
108 napi_property_descriptor exportFuncs[] = {
109 DECLARE_NAPI_PROPERTY("BackgroundMode", obj),
110 DECLARE_NAPI_PROPERTY("ResourceType", obj),
111 DECLARE_NAPI_PROPERTY("BackgroundSubMode", obj),
112 DECLARE_NAPI_PROPERTY("BackgroundModeType", obj),
113 };
114
115 napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
116 return exports;
117 }
118
ContinuousTaskCancelReasonInit(napi_env env,napi_value exports)119 napi_value ContinuousTaskCancelReasonInit(napi_env env, napi_value exports)
120 {
121 napi_value obj = nullptr;
122 napi_create_object(env, &obj);
123 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ContinuousTaskCancelReason::USER_CANCEL), "USER_CANCEL");
124 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ContinuousTaskCancelReason::SYSTEM_CANCEL),
125 "SYSTEM_CANCEL");
126 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
127 ContinuousTaskCancelReason::USER_CANCEL_REMOVE_NOTIFICATION), "USER_CANCEL_REMOVE_NOTIFICATION");
128 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
129 ContinuousTaskCancelReason::SYSTEM_CANCEL_DATA_TRANSFER_LOW_SPEED), "SYSTEM_CANCEL_DATA_TRANSFER_LOW_SPEED");
130 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
131 ContinuousTaskCancelReason::SYSTEM_CANCEL_AUDIO_PLAYBACK_NOT_USE_AVSESSION),
132 "SYSTEM_CANCEL_AUDIO_PLAYBACK_NOT_USE_AVSESSION");
133 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
134 ContinuousTaskCancelReason::SYSTEM_CANCEL_AUDIO_PLAYBACK_NOT_RUNNING),
135 "SYSTEM_CANCEL_AUDIO_PLAYBACK_NOT_RUNNING");
136 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
137 ContinuousTaskCancelReason::SYSTEM_CANCEL_AUDIO_RECORDING_NOT_RUNNING),
138 "SYSTEM_CANCEL_AUDIO_RECORDING_NOT_RUNNING");
139 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
140 ContinuousTaskCancelReason::SYSTEM_CANCEL_NOT_USE_LOCATION), "SYSTEM_CANCEL_NOT_USE_LOCATION");
141 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
142 ContinuousTaskCancelReason::SYSTEM_CANCEL_NOT_USE_BLUETOOTH), "SYSTEM_CANCEL_NOT_USE_BLUETOOTH");
143 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
144 ContinuousTaskCancelReason::SYSTEM_CANCEL_NOT_USE_MULTI_DEVICE), "SYSTEM_CANCEL_NOT_USE_MULTI_DEVICE");
145 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
146 ContinuousTaskCancelReason::SYSTEM_CANCEL_USE_ILLEGALLY), "SYSTEM_CANCEL_USE_ILLEGALLY");
147 napi_property_descriptor exportFuncs[] = {
148 DECLARE_NAPI_PROPERTY("ContinuousTaskCancelReason", obj),
149 };
150 napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
151 return exports;
152 }
153
ContinuousTaskSuspendReasonInit(napi_env env,napi_value exports)154 napi_value ContinuousTaskSuspendReasonInit(napi_env env, napi_value exports)
155 {
156 napi_value obj = nullptr;
157 napi_create_object(env, &obj);
158 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
159 ContinuousTaskSuspendReason::SYSTEM_SUSPEND_DATA_TRANSFER_LOW_SPEED),
160 "SYSTEM_SUSPEND_DATA_TRANSFER_LOW_SPEED");
161 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
162 ContinuousTaskSuspendReason::SYSTEM_SUSPEND_AUDIO_PLAYBACK_NOT_USE_AVSESSION),
163 "SYSTEM_SUSPEND_AUDIO_PLAYBACK_NOT_USE_AVSESSION");
164 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
165 ContinuousTaskSuspendReason::SYSTEM_SUSPEND_AUDIO_PLAYBACK_NOT_RUNNING),
166 "SYSTEM_SUSPEND_AUDIO_PLAYBACK_NOT_RUNNING");
167 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
168 ContinuousTaskSuspendReason::SYSTEM_SUSPEND_AUDIO_RECORDING_NOT_RUNNING),
169 "SYSTEM_SUSPEND_AUDIO_RECORDING_NOT_RUNNING");
170 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
171 ContinuousTaskSuspendReason::SYSTEM_SUSPEND_LOCATION_NOT_USED), "SYSTEM_SUSPEND_LOCATION_NOT_USED");
172 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
173 ContinuousTaskSuspendReason::SYSTEM_SUSPEND_BLUETOOTH_NOT_USED), "SYSTEM_SUSPEND_BLUETOOTH_NOT_USED");
174 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
175 ContinuousTaskSuspendReason::SYSTEM_SUSPEND_MULTI_DEVICE_NOT_USED), "SYSTEM_SUSPEND_MULTI_DEVICE_NOT_USED");
176 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
177 ContinuousTaskSuspendReason::SYSTEM_SUSPEND_USED_ILLEGALLY), "SYSTEM_SUSPEND_USED_ILLEGALLY");
178 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
179 ContinuousTaskSuspendReason::SYSTEM_SUSPEND_SYSTEM_LOAD_WARNING), "SYSTEM_SUSPEND_SYSTEM_LOAD_WARNING");
180 napi_property_descriptor exportFuncs[] = {
181 DECLARE_NAPI_PROPERTY("ContinuousTaskSuspendReason", obj),
182 };
183 napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
184 return exports;
185 }
186
187 /*
188 * Module export function
189 */
InitApi(napi_env env,napi_value exports)190 static napi_value InitApi(napi_env env, napi_value exports)
191 {
192 /*
193 * Properties define
194 */
195 BackgroundTaskMgrInit(env, exports);
196 BackgroundModeInit(env, exports);
197 ContinuousTaskCancelReasonInit(env, exports);
198 ContinuousTaskSuspendReasonInit(env, exports);
199 return exports;
200 }
201
202 /*
203 * Module register function
204 */
RegisterModule(void)205 __attribute__((constructor)) void RegisterModule(void)
206 {
207 napi_module_register(&g_apiModule);
208 }
209 EXTERN_C_END
210 } // namespace BackgroundTaskMgr
211 } // namespace OHOS
212