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 #ifndef RESSCHED_INTERFACES_KITS_JS_SYSTEMLOAD_H 17 #define RESSCHED_INTERFACES_KITS_JS_SYSTEMLOAD_H 18 19 #include <list> 20 #include <memory> 21 #include <mutex> 22 #include <string> 23 #include <map> 24 25 #include "js_systemload_listener.h" 26 #include "napi/native_api.h" 27 #include "napi/native_node_api.h" 28 #include "native_engine/native_engine.h" 29 #include "refbase.h" 30 #include "single_instance.h" 31 32 namespace OHOS { 33 namespace ResourceSchedule { 34 class Systemload { 35 DECLARE_SINGLE_INSTANCE_BASE(Systemload); 36 public: 37 struct SystemloadLevelCbInfo { SystemloadLevelCbInfoSystemloadLevelCbInfo38 explicit SystemloadLevelCbInfo(napi_env env) 39 : nativeEnv(env) {} ~SystemloadLevelCbInfoSystemloadLevelCbInfo40 ~SystemloadLevelCbInfo() 41 { 42 if (nativeEnv) { 43 if (callback) { 44 napi_delete_reference(nativeEnv, callback); 45 callback = nullptr; 46 } 47 if (asyncWork) { 48 napi_delete_async_work(nativeEnv, asyncWork); 49 asyncWork = nullptr; 50 } 51 } 52 } 53 napi_ref callback = nullptr; 54 napi_async_work asyncWork = nullptr; 55 napi_deferred deferred = nullptr; 56 napi_env nativeEnv = nullptr; 57 int32_t result = 0; 58 }; 59 60 using CallBackPair = std::pair<std::unique_ptr<NativeReference>, sptr<SystemloadListener>>; 61 // static function 62 static napi_value SystemloadOn(napi_env env, napi_callback_info info); 63 static napi_value SystemloadOff(napi_env env, napi_callback_info info); 64 static napi_value GetLevel(napi_env env, napi_callback_info info); 65 66 void OnSystemloadLevel(napi_env env, napi_value callbackObj, int32_t level); 67 private: 68 Systemload() = default; 69 ~Systemload(); 70 napi_value RegisterSystemloadCallback(napi_env env, napi_callback_info info); 71 napi_value UnRegisterSystemloadCallback(napi_env env, napi_callback_info info); 72 napi_value GetSystemloadLevel(napi_env env, napi_callback_info info); 73 bool CheckCallbackParam(napi_env env, napi_callback_info info, std::string &cbType, napi_value *jsCallback); 74 75 static void Execute(napi_env env, void *data); 76 static void Complete(napi_env env, napi_status status, void *data); 77 static void CompleteCb(napi_env env, napi_status status, void* data); 78 79 std::mutex jsCallbackMapLock_; 80 std::map<std::string, std::list<CallBackPair> > jsCallBackMap_; 81 }; 82 } // ResourceSchedule 83 } // OHOS 84 85 #endif // RESSCHED_INTERFACES_KITS_JS_SYSTEMLOAD_H