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.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
25 namespace OHOS {
26 namespace BackgroundTaskMgr {
27 EXTERN_C_START
28
BackgroundTaskMgrInit(napi_env env,napi_value exports)29 napi_value BackgroundTaskMgrInit(napi_env env, napi_value exports)
30 {
31 BGTASK_LOGI("BackgroundTaskMgrInit");
32 napi_property_descriptor desc[] = {
33 DECLARE_NAPI_FUNCTION("requestSuspendDelay", RequestSuspendDelay),
34 DECLARE_NAPI_FUNCTION("cancelSuspendDelay", CancelSuspendDelay),
35 DECLARE_NAPI_FUNCTION("getRemainingDelayTime", GetRemainingDelayTime),
36 DECLARE_NAPI_FUNCTION("startBackgroundRunning", StartBackgroundRunning),
37 DECLARE_NAPI_FUNCTION("stopBackgroundRunning", StopBackgroundRunning),
38 };
39
40 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
41
42 return exports;
43 }
44
SetNamedPropertyByInteger(napi_env env,napi_value dstObj,int32_t objName,const char * propName)45 void SetNamedPropertyByInteger(napi_env env, napi_value dstObj, int32_t objName, const char *propName)
46 {
47 napi_value prop = nullptr;
48 if (napi_create_int32(env, objName, &prop) == napi_ok) {
49 napi_set_named_property(env, dstObj, propName, prop);
50 }
51 }
52
BackgroundModeInit(napi_env env,napi_value exports)53 napi_value BackgroundModeInit(napi_env env, napi_value exports)
54 {
55 BGTASK_LOGI("begin");
56
57 napi_value obj = nullptr;
58 napi_create_object(env, &obj);
59
60 SetNamedPropertyByInteger(env, obj, (uint32_t)BackgroundMode::DATA_TRANSFER, "DATA_TRANSFER");
61 SetNamedPropertyByInteger(env, obj, (uint32_t)BackgroundMode::AUDIO_PLAYBACK, "AUDIO_PLAYBACK");
62 SetNamedPropertyByInteger(env, obj, (uint32_t)BackgroundMode::AUDIO_RECORDING, "AUDIO_RECORDING");
63 SetNamedPropertyByInteger(env, obj, (uint32_t)BackgroundMode::LOCATION, "LOCATION");
64 SetNamedPropertyByInteger(env, obj, (uint32_t)BackgroundMode::BLUETOOTH_INTERACTION, "BLUETOOTH_INTERACTION");
65 SetNamedPropertyByInteger(env, obj, (uint32_t)BackgroundMode::MULTI_DEVICE_CONNECTION, "MULTI_DEVICE_CONNECTION");
66 SetNamedPropertyByInteger(env, obj, (uint32_t)BackgroundMode::WIFI_INTERACTION, "WIFI_INTERACTION");
67 SetNamedPropertyByInteger(env, obj, (uint32_t)BackgroundMode::VOIP, "VOIP");
68 SetNamedPropertyByInteger(env, obj, (uint32_t)BackgroundMode::TASK_KEEPING, "TASK_KEEPING");
69
70 napi_property_descriptor exportFuncs[] = {
71 DECLARE_NAPI_PROPERTY("BackgroundMode", obj),
72 };
73
74 napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
75 return exports;
76 }
77
78 /*
79 * Module export function
80 */
Init(napi_env env,napi_value exports)81 static napi_value Init(napi_env env, napi_value exports)
82 {
83 /*
84 * Propertise define
85 */
86 BackgroundTaskMgrInit(env, exports);
87 BackgroundModeInit(env, exports);
88 return exports;
89 }
90
91 /*
92 * Module register function
93 */
RegisterModule(void)94 __attribute__((constructor)) void RegisterModule(void)
95 {
96 napi_module_register(&_module);
97 }
98 EXTERN_C_END
99 } // namespace BackgroundTaskMgr
100 } // namespace OHOS