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 
17 #include "js_systemload.h"
18 
19 #include <functional>
20 
21 #include "js_napi_utils.h"
22 #include "res_sched_client.h"
23 #include "res_sched_log.h"
24 
25 namespace OHOS {
26 namespace ResourceSchedule {
27 static constexpr size_t ARG_COUNT_ONE = 1;
28 static constexpr size_t ARG_COUNT_TWO = 2;
29 static const std::string SYSTEMLOAD_LEVEL = "systemLoadChange";
30 
31 
GetInstance()32 Systemload& Systemload::GetInstance()
33 {
34     static Systemload systemload;
35     return systemload;
36 }
37 
~Systemload()38 Systemload::~Systemload()
39 {
40     std::lock_guard<std::mutex> autoLock(jsCallbackMapLock_);
41     jsCallBackMap_.clear();
42 }
43 
SystemloadOn(napi_env env,napi_callback_info info)44 napi_value Systemload::SystemloadOn(napi_env env, napi_callback_info info)
45 {
46     return GetInstance().RegisterSystemloadCallback(env, info);
47 }
48 
SystemloadOff(napi_env env,napi_callback_info info)49 napi_value Systemload::SystemloadOff(napi_env env, napi_callback_info info)
50 {
51     return GetInstance().UnRegisterSystemloadCallback(env, info);
52 }
53 
GetLevel(napi_env env,napi_callback_info info)54 napi_value Systemload::GetLevel(napi_env env, napi_callback_info info)
55 {
56     return GetInstance().GetSystemloadLevel(env, info);
57 }
58 
OnSystemloadLevel(napi_env env,napi_value callbackObj,int32_t level)59 void Systemload::OnSystemloadLevel(napi_env env, napi_value callbackObj, int32_t level)
60 {
61     RESSCHED_LOGI("OnSystemloadLevel asyncCallback.");
62     std::lock_guard<std::mutex> autoLock(jsCallbackMapLock_);
63     if (jsCallBackMap_.find(SYSTEMLOAD_LEVEL) == jsCallBackMap_.end()) {
64         RESSCHED_LOGE("OnSystemloadLevel cb type has not register yet.");
65         return;
66     }
67     auto& callbackList = jsCallBackMap_[SYSTEMLOAD_LEVEL];
68     auto iter = callbackList.begin();
69     bool isEqual = false;
70     for (; iter != callbackList.end(); iter++) {
71         NAPI_CALL_RETURN_VOID(env, napi_strict_equals(env, callbackObj, iter->first->GetNapiValue(), &isEqual));
72         if (isEqual) {
73             break;
74         }
75     }
76     if (!isEqual) {
77         RESSCHED_LOGE("OnSystemload level callback not found in registered array.");
78         return;
79     }
80     std::unique_ptr<SystemloadLevelCbInfo> cbInfo = std::make_unique<SystemloadLevelCbInfo>(env);
81     if (cbInfo == nullptr) {
82         RESSCHED_LOGE("OnSystemloadLevel cbInfo null.");
83         return;
84     }
85     cbInfo->result = level;
86     napi_value resourceName = nullptr;
87     NAPI_CALL_RETURN_VOID(env,
88         napi_create_string_latin1(env, "OnSystemloadLevel", NAPI_AUTO_LENGTH, &resourceName));
89 
90     NAPI_CALL_RETURN_VOID(env,
91         napi_create_reference(env, iter->first->GetNapiValue(), 1, &cbInfo->callback));
92 
93     NAPI_CALL_RETURN_VOID(env, napi_create_async_work(env, nullptr, resourceName,
94         [] (napi_env env, void* data) {},
95         CompleteCb,
96         static_cast<void *>(cbInfo.get()),
97         &cbInfo->asyncWork));
98     NAPI_CALL_RETURN_VOID(env, napi_queue_async_work(env, cbInfo->asyncWork));
99     cbInfo.release();
100     napi_value result = nullptr;
101     NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result));
102     RESSCHED_LOGI("OnSystemloadLevel asyncCallback end");
103 }
104 
RegisterSystemloadCallback(napi_env env,napi_callback_info info)105 napi_value Systemload::RegisterSystemloadCallback(napi_env env, napi_callback_info info)
106 {
107     RESSCHED_LOGD("Regster Systemload Callback");
108     std::string cbType;
109     napi_value jsCallback = nullptr;
110 
111     if (!CheckCallbackParam(env, info, cbType, &jsCallback)) {
112         RESSCHED_LOGE("Register Systemload Callback parameter error.");
113         return CreateJsUndefined(env);
114     }
115 
116     napi_ref tempRef = nullptr;
117     napi_create_reference(env, jsCallback, 1, &tempRef);
118     std::unique_ptr<NativeReference> callbackRef;
119     callbackRef.reset(reinterpret_cast<NativeReference*>(tempRef));
120     std::lock_guard<std::mutex> autoLock(jsCallbackMapLock_);
121     if (jsCallBackMap_.find(cbType) == jsCallBackMap_.end()) {
122         jsCallBackMap_[cbType] = std::list<CallBackPair>();
123     }
124     auto& callbackList = jsCallBackMap_[cbType];
125     auto iter = callbackList.begin();
126     for (; iter != callbackList.end(); iter++) {
127         bool isEqual = false;
128         napi_strict_equals(env, jsCallback, iter->first->GetNapiValue(), &isEqual);
129         if (isEqual) {
130             RESSCHED_LOGW("Register a exist callback type.");
131             return CreateJsUndefined(env);
132         }
133     }
134     auto systemloadLevelCb = [](napi_env env, napi_value callbackObj, int32_t level) {
135         Systemload::GetInstance().OnSystemloadLevel(env, callbackObj, level);
136     };
137     sptr<SystemloadListener> systemloadListener =
138         new (std::nothrow) SystemloadListener(env, jsCallback, systemloadLevelCb);
139     if (systemloadListener == nullptr) {
140         RESSCHED_LOGE("Register Systemload listener nullptr.");
141         return CreateJsUndefined(env);
142     }
143     ResSchedClient::GetInstance().RegisterSystemloadNotifier(systemloadListener);
144     callbackList.emplace_back(std::move(callbackRef), systemloadListener);
145     return CreateJsUndefined(env);
146 }
147 
UnRegisterSystemloadCallback(napi_env env,napi_callback_info info)148 napi_value Systemload::UnRegisterSystemloadCallback(napi_env env, napi_callback_info info)
149 {
150     RESSCHED_LOGD("UnRegster Systemload Callback");
151     std::string cbType;
152     napi_value jsCallback = nullptr;
153 
154     if (!CheckCallbackParam(env, info, cbType, &jsCallback)) {
155         RESSCHED_LOGE("UnRegister Systemload Callback parameter error.");
156         return CreateJsUndefined(env);
157     }
158 
159     std::lock_guard<std::mutex> autoLock(jsCallbackMapLock_);
160     if (jsCallBackMap_.find(cbType) == jsCallBackMap_.end()) {
161         RESSCHED_LOGE("unRegister cbType has not registered");
162         return CreateJsUndefined(env);
163     }
164     auto& callbackList = jsCallBackMap_[cbType];
165     for (auto iter = callbackList.begin(); iter != callbackList.end(); iter++) {
166         bool isEqual = false;
167         napi_strict_equals(env, jsCallback, iter->first->GetNapiValue(), &isEqual);
168         if (isEqual) {
169             ResSchedClient::GetInstance().UnRegisterSystemloadNotifier(iter->second);
170             callbackList.erase(iter);
171             break;
172         }
173     }
174     return CreateJsUndefined(env);
175 }
176 
GetSystemloadLevel(napi_env env,napi_callback_info info)177 napi_value Systemload::GetSystemloadLevel(napi_env env, napi_callback_info info)
178 {
179     RESSCHED_LOGI("GetSystemloadLevel, promise.");
180     std::unique_ptr<SystemloadLevelCbInfo> cbInfo = std::make_unique<SystemloadLevelCbInfo>(env);
181     if (cbInfo == nullptr) {
182         return CreateJsUndefined(env);
183     }
184     napi_value resourceName;
185     NAPI_CALL(env, napi_create_string_latin1(env, "GetSystemloadLevel", NAPI_AUTO_LENGTH, &resourceName));
186     napi_deferred deferred;
187     napi_value promise = nullptr;
188     NAPI_CALL(env, napi_create_promise(env, &deferred, &promise));
189     cbInfo->deferred = deferred;
190 
191     NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName,
192         Execute,
193         Complete,
194         static_cast<void *>(cbInfo.get()),
195         &cbInfo->asyncWork));
196     NAPI_CALL(env, napi_queue_async_work(env, cbInfo->asyncWork));
197     cbInfo.release();
198     RESSCHED_LOGI("GetSystemloadLevel, promise end");
199     return promise;
200 }
201 
Execute(napi_env env,void * data)202 void Systemload::Execute(napi_env env, void* data)
203 {
204     RESSCHED_LOGI("GetSystemloadLevel, worker pool thread execute.");
205     auto* cbInfo = static_cast<SystemloadLevelCbInfo*>(data);
206     if (cbInfo == nullptr) {
207         RESSCHED_LOGW("GetSystemloadLevel execute cb info is nullptr.");
208         return;
209     }
210     cbInfo->result = ResSchedClient::GetInstance().GetSystemloadLevel();
211     RESSCHED_LOGI("GetSystemloadLevel, worker pool thread execute end.");
212 }
213 
Complete(napi_env env,napi_status status,void * data)214 void Systemload::Complete(napi_env env, napi_status status, void* data)
215 {
216     RESSCHED_LOGI("GetSystemloadLevel, main event thread complete.");
217     auto* info = static_cast<SystemloadLevelCbInfo*>(data);
218     if (info == nullptr) {
219         RESSCHED_LOGW("GetSystemloadLevel Complete cb info is nullptr.");
220         return;
221     }
222     std::unique_ptr<SystemloadLevelCbInfo> cbInfo(info);
223     napi_value result = nullptr;
224     napi_create_uint32(env, static_cast<uint32_t>(cbInfo->result), &result);
225     NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, cbInfo->deferred, result));
226     NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, cbInfo->asyncWork));
227     cbInfo->asyncWork = nullptr;
228     RESSCHED_LOGI("GetSystemloadLevel,  main event thread complete end.");
229 }
230 
CompleteCb(napi_env env,napi_status status,void * data)231 void Systemload::CompleteCb(napi_env env, napi_status status, void* data)
232 {
233     RESSCHED_LOGI("CompleteCb, main event thread complete callback.");
234     auto* info = static_cast<SystemloadLevelCbInfo*>(data);
235     if (info == nullptr) {
236         RESSCHED_LOGW("Complete cb info is nullptr.");
237         return;
238     }
239     std::unique_ptr<SystemloadLevelCbInfo> cbInfo(info);
240     napi_value callback = nullptr;
241     napi_value undefined = nullptr;
242     napi_value result = nullptr;
243     napi_value callResult = nullptr;
244     NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined));
245     NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, cbInfo->callback, &callback));
246     napi_create_uint32(env, static_cast<uint32_t>(cbInfo->result), &result);
247     // call js callback
248     NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, 1, &result, &callResult));
249     // delete resources
250     NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, cbInfo->asyncWork));
251     cbInfo->asyncWork = nullptr;
252     if (cbInfo->callback != nullptr) {
253         NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, cbInfo->callback));
254         cbInfo->callback = nullptr;
255     }
256     RESSCHED_LOGI("CompleteCb, main event thread complete end.");
257 }
258 
CheckCallbackParam(napi_env env,napi_callback_info info,std::string & cbType,napi_value * jsCallback)259 bool Systemload::CheckCallbackParam(napi_env env, napi_callback_info info,
260                                     std::string &cbType, napi_value *jsCallback)
261 {
262     if (jsCallback == nullptr) {
263         RESSCHED_LOGE("Input callback is nullptr.");
264         return false;
265     }
266     size_t argc = ARG_COUNT_TWO;
267     napi_value argv[ARG_COUNT_TWO] = { 0 };
268     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
269     if (argc != ARG_COUNT_TWO) {
270         RESSCHED_LOGE("Parameter error. The type of \"number of parameters\" must be 2");
271         return false;
272     }
273     if (!ConvertFromJsValue(env, argv[0], cbType)) {
274         RESSCHED_LOGE("Parameter error. The type of \"type\" must be string");
275         return false;
276     }
277 
278     *jsCallback = argv[ARG_COUNT_ONE];
279     if (*jsCallback == nullptr) {
280         RESSCHED_LOGE("listenerObj is nullptr");
281         return false;
282     }
283     bool isCallable = false;
284     napi_is_callable(env, *jsCallback, &isCallable);
285     if (!isCallable) {
286         RESSCHED_LOGE("Parameter error. The type of \"callback\" must be Callback");
287         return false;
288     }
289     return true;
290 }
291 } // ResourceSchedule
292 } // OHOS