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