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 #include "init_bgtaskmgr.h"
17
18 #include "background_mode.h"
19 #include "bg_continuous_task_napi_module.h"
20 #include "cancel_suspend_delay.h"
21 #include "get_remaining_delay_time.h"
22 #include "request_suspend_delay.h"
23 #include "transient_task_log.h"
24 #include "efficiency_resources_operation.h"
25 #include "resource_type.h"
26 #include "common.h"
27
28 namespace OHOS {
29 namespace BackgroundTaskMgr {
30 EXTERN_C_START
31
BackgroundTaskMgrInit(napi_env env,napi_value exports)32 napi_value BackgroundTaskMgrInit(napi_env env, napi_value exports)
33 {
34 napi_property_descriptor desc[] = {
35 DECLARE_NAPI_FUNCTION("requestSuspendDelay", RequestSuspendDelayThrow),
36 DECLARE_NAPI_FUNCTION("cancelSuspendDelay", CancelSuspendDelayThrow),
37 DECLARE_NAPI_FUNCTION("getRemainingDelayTime", GetRemainingDelayTimeThrow),
38 DECLARE_NAPI_FUNCTION("startBackgroundRunning", StartBackgroundRunningThrow),
39 DECLARE_NAPI_FUNCTION("updateBackgroundRunning", UpdateBackgroundRunningThrow),
40 DECLARE_NAPI_FUNCTION("stopBackgroundRunning", StopBackgroundRunningThrow),
41 DECLARE_NAPI_FUNCTION("applyEfficiencyResources", ApplyEfficiencyResources),
42 DECLARE_NAPI_FUNCTION("resetAllEfficiencyResources", ResetAllEfficiencyResources),
43 };
44
45 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
46
47 return exports;
48 }
49
SetNamedPropertyByInteger(napi_env env,napi_value dstObj,int32_t objName,const char * propName)50 void SetNamedPropertyByInteger(napi_env env, napi_value dstObj, int32_t objName, const char *propName)
51 {
52 napi_value prop = nullptr;
53 if (napi_create_int32(env, objName, &prop) == napi_ok) {
54 napi_set_named_property(env, dstObj, propName, prop);
55 }
56 }
57
BackgroundModeInit(napi_env env,napi_value exports)58 napi_value BackgroundModeInit(napi_env env, napi_value exports)
59 {
60 napi_value obj = nullptr;
61 napi_create_object(env, &obj);
62
63 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::DATA_TRANSFER), "DATA_TRANSFER");
64 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::AUDIO_PLAYBACK), "AUDIO_PLAYBACK");
65 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::AUDIO_RECORDING),
66 "AUDIO_RECORDING");
67 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::LOCATION),
68 "LOCATION");
69 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::BLUETOOTH_INTERACTION),
70 "BLUETOOTH_INTERACTION");
71 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::MULTI_DEVICE_CONNECTION),
72 "MULTI_DEVICE_CONNECTION");
73 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::WIFI_INTERACTION), "WIFI_INTERACTION");
74 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::VOIP), "VOIP");
75 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::TASK_KEEPING), "TASK_KEEPING");
76
77 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::CPU), "CPU");
78 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::COMMON_EVENT), "COMMON_EVENT");
79 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::TIMER), "TIMER");
80 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::WORK_SCHEDULER), "WORK_SCHEDULER");
81 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::BLUETOOTH), "BLUETOOTH");
82 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::GPS), "GPS");
83 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::AUDIO), "AUDIO");
84 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::RUNNING_LOCK), "RUNNING_LOCK");
85 SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::SENSOR), "SENSOR");
86
87 napi_property_descriptor exportFuncs[] = {
88 DECLARE_NAPI_PROPERTY("BackgroundMode", obj),
89 DECLARE_NAPI_PROPERTY("ResourceType", obj),
90 };
91
92 napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
93 return exports;
94 }
95
96 /*
97 * Module export function
98 */
InitApi(napi_env env,napi_value exports)99 static napi_value InitApi(napi_env env, napi_value exports)
100 {
101 /*
102 * Properties define
103 */
104 BackgroundTaskMgrInit(env, exports);
105 BackgroundModeInit(env, exports);
106 return exports;
107 }
108
109 /*
110 * Module register function
111 */
RegisterModule(void)112 __attribute__((constructor)) void RegisterModule(void)
113 {
114 napi_module_register(&g_apiModule);
115 }
116 EXTERN_C_END
117 } // namespace BackgroundTaskMgr
118 } // namespace OHOS
119