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