• 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 "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("stopBackgroundRunning", StopBackgroundRunningThrow),
40         DECLARE_NAPI_FUNCTION("applyEfficiencyResources", ApplyEfficiencyResources),
41         DECLARE_NAPI_FUNCTION("resetAllEfficiencyResources", ResetAllEfficiencyResources),
42     };
43 
44     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
45 
46     return exports;
47 }
48 
SetNamedPropertyByInteger(napi_env env,napi_value dstObj,int32_t objName,const char * propName)49 void SetNamedPropertyByInteger(napi_env env, napi_value dstObj, int32_t objName, const char *propName)
50 {
51     napi_value prop = nullptr;
52     if (napi_create_int32(env, objName, &prop) == napi_ok) {
53         napi_set_named_property(env, dstObj, propName, prop);
54     }
55 }
56 
BackgroundModeInit(napi_env env,napi_value exports)57 napi_value BackgroundModeInit(napi_env env, napi_value exports)
58 {
59     napi_value obj = nullptr;
60     napi_create_object(env, &obj);
61 
62     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::DATA_TRANSFER), "DATA_TRANSFER");
63     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::AUDIO_PLAYBACK), "AUDIO_PLAYBACK");
64     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::AUDIO_RECORDING),
65         "AUDIO_RECORDING");
66     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::LOCATION),
67         "LOCATION");
68     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::BLUETOOTH_INTERACTION),
69         "BLUETOOTH_INTERACTION");
70     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::MULTI_DEVICE_CONNECTION),
71         "MULTI_DEVICE_CONNECTION");
72     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::WIFI_INTERACTION), "WIFI_INTERACTION");
73     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::VOIP), "VOIP");
74     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(BackgroundMode::TASK_KEEPING), "TASK_KEEPING");
75 
76     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::CPU), "CPU");
77     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::COMMON_EVENT), "COMMON_EVENT");
78     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::TIMER), "TIMER");
79     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::WORK_SCHEDULER), "WORK_SCHEDULER");
80     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::BLUETOOTH), "BLUETOOTH");
81     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::GPS), "GPS");
82     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::AUDIO), "AUDIO");
83     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::RUNNING_LOCK), "RUNNING_LOCK");
84     SetNamedPropertyByInteger(env, obj, static_cast<uint32_t>(ResourceType::SENSOR), "SENSOR");
85 
86     napi_property_descriptor exportFuncs[] = {
87         DECLARE_NAPI_PROPERTY("BackgroundMode", obj),
88         DECLARE_NAPI_PROPERTY("ResourceType", obj),
89     };
90 
91     napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
92     return exports;
93 }
94 
95 /*
96  * Module export function
97  */
InitApi(napi_env env,napi_value exports)98 static napi_value InitApi(napi_env env, napi_value exports)
99 {
100     /*
101      * Properties define
102      */
103     BackgroundTaskMgrInit(env, exports);
104     BackgroundModeInit(env, exports);
105     return exports;
106 }
107 
108 /*
109  * Module register function
110  */
RegisterModule(void)111 __attribute__((constructor)) void RegisterModule(void)
112 {
113     napi_module_register(&g_apiModule);
114 }
115 EXTERN_C_END
116 }  // namespace BackgroundTaskMgr
117 }  // namespace OHOS
118