1 /*
2 * Copyright (c) 2024 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 "js_systemload_napi_init.h"
17
18 #include "res_type.h"
19 #include "js_systemload.h"
20
21 namespace OHOS {
22 namespace ResourceSchedule {
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 static napi_ref g_systemloadLevelTypeConstructor = nullptr;
28
EnumSystemloadLevelConstructor(napi_env env,napi_callback_info info)29 static napi_value EnumSystemloadLevelConstructor(napi_env env, napi_callback_info info)
30 {
31 size_t argc = 0;
32 napi_value args[1] = { nullptr };
33 napi_value retValue = nullptr;
34 void* dataInfo = nullptr;
35
36 napi_status status = napi_get_cb_info(env, info, &argc, args, &retValue, &dataInfo);
37 if (status != napi_ok) {
38 return nullptr;
39 }
40 return retValue;
41 }
42
InitSystemloadLevelType(napi_env env,napi_value exports)43 static napi_value InitSystemloadLevelType(napi_env env, napi_value exports)
44 {
45 napi_value systemloadLevelLow;
46 napi_value systemloadLevelNormal;
47 napi_value systemloadLevelMedium;
48 napi_value systemloadLevelHigh;
49 napi_value systemloadLevelOverheated;
50 napi_value systemloadLevelWarning;
51 napi_value systemloadLevelEmergency;
52 napi_value systemloadLevelEscape;
53
54 napi_create_uint32(env, static_cast<uint32_t>(ResType::SystemloadLevel::LOW),
55 &systemloadLevelLow);
56 napi_create_uint32(env, static_cast<uint32_t>(ResType::SystemloadLevel::NORMAL),
57 &systemloadLevelNormal);
58 napi_create_uint32(env, static_cast<uint32_t>(ResType::SystemloadLevel::MEDIUM),
59 &systemloadLevelMedium);
60 napi_create_uint32(env, static_cast<uint32_t>(ResType::SystemloadLevel::HIGH),
61 &systemloadLevelHigh);
62 napi_create_uint32(env, static_cast<uint32_t>(ResType::SystemloadLevel::OVERHEATED),
63 &systemloadLevelOverheated);
64 napi_create_uint32(env, static_cast<uint32_t>(ResType::SystemloadLevel::WARNING),
65 &systemloadLevelWarning);
66 napi_create_uint32(env, static_cast<uint32_t>(ResType::SystemloadLevel::EMERGENCY),
67 &systemloadLevelEmergency);
68 napi_create_uint32(env, static_cast<uint32_t>(ResType::SystemloadLevel::ESCAPE),
69 &systemloadLevelEscape);
70
71 napi_property_descriptor desc[] = {
72 DECLARE_NAPI_STATIC_PROPERTY("LOW", systemloadLevelLow),
73 DECLARE_NAPI_STATIC_PROPERTY("NORMAL", systemloadLevelNormal),
74 DECLARE_NAPI_STATIC_PROPERTY("MEDIUM", systemloadLevelMedium),
75 DECLARE_NAPI_STATIC_PROPERTY("HIGH", systemloadLevelHigh),
76 DECLARE_NAPI_STATIC_PROPERTY("OVERHEATED", systemloadLevelOverheated),
77 DECLARE_NAPI_STATIC_PROPERTY("WARNING", systemloadLevelWarning),
78 DECLARE_NAPI_STATIC_PROPERTY("EMERGENCY", systemloadLevelEmergency),
79 DECLARE_NAPI_STATIC_PROPERTY("ESCAPE", systemloadLevelEscape),
80 };
81
82 napi_value result = nullptr;
83 int32_t refCount = 1;
84 napi_define_class(env, "SystemLoadLevel", NAPI_AUTO_LENGTH, EnumSystemloadLevelConstructor,
85 nullptr, sizeof(desc) / sizeof(*desc), desc, &result);
86 napi_create_reference(env, result, refCount, &g_systemloadLevelTypeConstructor);
87 napi_set_named_property(env, exports, "SystemLoadLevel", result);
88 return exports;
89 }
90
InitSystemloadApi(napi_env env,napi_value exports)91 napi_value InitSystemloadApi(napi_env env, napi_value exports)
92 {
93 napi_property_descriptor desc[] = {
94 DECLARE_NAPI_FUNCTION("on", Systemload::SystemloadOn),
95 DECLARE_NAPI_FUNCTION("off", Systemload::SystemloadOff),
96 DECLARE_NAPI_FUNCTION("getLevel", Systemload::GetLevel),
97 };
98
99 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(*desc), desc));
100
101 InitSystemloadLevelType(env, exports);
102
103 return exports;
104 }
105
RegisterModule(void)106 __attribute__((constructor)) void RegisterModule(void)
107 {
108 napi_module_register(&systemloadModule);
109 }
110
111 #ifdef __cplusplus
112 }
113 #endif
114 } // ResourceSchedule
115 } // OHOS