• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "continuous_task_cancel_reason.h"
22 #include "get_remaining_delay_time.h"
23 #include "request_suspend_delay.h"
24 #include "transient_task_log.h"
25 #include "efficiency_resources_operation.h"
26 #include "resource_type.h"
27 #include "common.h"
28 
29 namespace OHOS {
30 namespace BackgroundTaskMgr {
31 EXTERN_C_START
32 
BackgroundTaskMgrInit(napi_env env,napi_value exports)33 napi_value BackgroundTaskMgrInit(napi_env env, napi_value exports)
34 {
35     napi_property_descriptor desc[] = {
36         DECLARE_NAPI_FUNCTION("requestSuspendDelay", RequestSuspendDelayThrow),
37         DECLARE_NAPI_FUNCTION("cancelSuspendDelay", CancelSuspendDelayThrow),
38         DECLARE_NAPI_FUNCTION("getRemainingDelayTime", GetRemainingDelayTimeThrow),
39         DECLARE_NAPI_FUNCTION("startBackgroundRunning", StartBackgroundRunningThrow),
40         DECLARE_NAPI_FUNCTION("updateBackgroundRunning", UpdateBackgroundRunningThrow),
41         DECLARE_NAPI_FUNCTION("stopBackgroundRunning", StopBackgroundRunningThrow),
42         DECLARE_NAPI_FUNCTION("applyEfficiencyResources", ApplyEfficiencyResources),
43         DECLARE_NAPI_FUNCTION("resetAllEfficiencyResources", ResetAllEfficiencyResources),
44         DECLARE_NAPI_FUNCTION("on", OnOnContinuousTaskCancel),
45         DECLARE_NAPI_FUNCTION("off", OffOnContinuousTaskCancel),
46     };
47 
48     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
49 
50     return exports;
51 }
52 
SetNamedPropertyByInteger(napi_env env,napi_value dstObj,int32_t objName,const char * propName)53 void SetNamedPropertyByInteger(napi_env env, napi_value dstObj, int32_t objName, const char *propName)
54 {
55     napi_value prop = nullptr;
56     if (napi_create_int32(env, objName, &prop) == napi_ok) {
57         napi_set_named_property(env, dstObj, propName, prop);
58     }
59 }
60 
BackgroundModeInit(napi_env env,napi_value exports)61 napi_value BackgroundModeInit(napi_env env, napi_value exports)
62 {
63     napi_value obj = nullptr;
64     napi_create_object(env, &obj);
65 
66     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::DATA_TRANSFER), "DATA_TRANSFER");
67     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::AUDIO_PLAYBACK), "AUDIO_PLAYBACK");
68     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::AUDIO_RECORDING),
69         "AUDIO_RECORDING");
70     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::LOCATION),
71         "LOCATION");
72     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::BLUETOOTH_INTERACTION),
73         "BLUETOOTH_INTERACTION");
74     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::MULTI_DEVICE_CONNECTION),
75         "MULTI_DEVICE_CONNECTION");
76     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::WIFI_INTERACTION), "WIFI_INTERACTION");
77     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::VOIP), "VOIP");
78     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::TASK_KEEPING), "TASK_KEEPING");
79 
80     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::CPU), "CPU");
81     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::COMMON_EVENT), "COMMON_EVENT");
82     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::TIMER), "TIMER");
83     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::WORK_SCHEDULER), "WORK_SCHEDULER");
84     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::BLUETOOTH), "BLUETOOTH");
85     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::GPS), "GPS");
86     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::AUDIO), "AUDIO");
87     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::RUNNING_LOCK), "RUNNING_LOCK");
88     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::SENSOR), "SENSOR");
89 
90     napi_property_descriptor exportFuncs[] = {
91         DECLARE_NAPI_PROPERTY("BackgroundMode", obj),
92         DECLARE_NAPI_PROPERTY("ResourceType", obj),
93     };
94 
95     napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
96     return exports;
97 }
98 
ContinuousTaskCancelReasonInit(napi_env env,napi_value exports)99 napi_value ContinuousTaskCancelReasonInit(napi_env env, napi_value exports)
100 {
101     napi_value obj = nullptr;
102     napi_create_object(env, &obj);
103     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ContinuousTaskCancelReason::USER_CANCEL), "USER_CANCEL");
104     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ContinuousTaskCancelReason::SYSTEM_CANCEL),
105         "SYSTEM_CANCEL");
106     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
107         ContinuousTaskCancelReason::USER_CANCEL_REMOVE_NOTIFICATION), "USER_CANCEL_REMOVE_NOTIFICATION");
108     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
109         ContinuousTaskCancelReason::SYSTEM_CANCEL_DATA_TRANSFER_LOW_SPEED), "SYSTEM_CANCEL_DATA_TRANSFER_LOW_SPEED");
110     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
111         ContinuousTaskCancelReason::SYSTEM_CANCEL_AUDIO_PLAYBACK_NOT_USE_AVSESSION),
112         "SYSTEM_CANCEL_AUDIO_PLAYBACK_NOT_USE_AVSESSION");
113     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
114         ContinuousTaskCancelReason::SYSTEM_CANCEL_AUDIO_PLAYBACK_NOT_RUNNING),
115         "SYSTEM_CANCEL_AUDIO_PLAYBACK_NOT_RUNNING");
116     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
117         ContinuousTaskCancelReason::SYSTEM_CANCEL_AUDIO_RECORDING_NOT_RUNNING),
118         "SYSTEM_CANCEL_AUDIO_RECORDING_NOT_RUNNING");
119     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
120         ContinuousTaskCancelReason::SYSTEM_CANCEL_NOT_USE_LOCATION), "SYSTEM_CANCEL_NOT_USE_LOCATION");
121     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
122         ContinuousTaskCancelReason::SYSTEM_CANCEL_NOT_USE_BLUETOOTH), "SYSTEM_CANCEL_NOT_USE_BLUETOOTH");
123     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
124         ContinuousTaskCancelReason::SYSTEM_CANCEL_NOT_USE_MULTI_DEVICE), "SYSTEM_CANCEL_NOT_USE_MULTI_DEVICE");
125     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(
126         ContinuousTaskCancelReason::SYSTEM_CANCEL_USE_ILLEGALLY), "SYSTEM_CANCEL_USE_ILLEGALLY");
127     napi_property_descriptor exportFuncs[] = {
128         DECLARE_NAPI_PROPERTY("ContinuousTaskCancelReason", obj),
129     };
130     napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
131     return exports;
132 }
133 
134 /*
135  * Module export function
136  */
InitApi(napi_env env,napi_value exports)137 static napi_value InitApi(napi_env env, napi_value exports)
138 {
139     /*
140      * Properties define
141      */
142     BackgroundTaskMgrInit(env, exports);
143     BackgroundModeInit(env, exports);
144     ContinuousTaskCancelReasonInit(env, exports);
145     return exports;
146 }
147 
148 /*
149  * Module register function
150  */
RegisterModule(void)151 __attribute__((constructor)) void RegisterModule(void)
152 {
153     napi_module_register(&g_apiModule);
154 }
155 EXTERN_C_END
156 }  // namespace BackgroundTaskMgr
157 }  // namespace OHOS
158