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 enum { 35 E_PARAM_ERROR = 401, 36 }; 37 const inline std::map<int32_t, std::string> systemloadParamErrMsgMap = { 38 {E_PARAM_ERROR, "Error 401: Parameter error. The input param is error"}, 39 }; 40 class Systemload { 41 DECLARE_SINGLE_INSTANCE_BASE(Systemload); 42 public: 43 struct SystemloadLevelCbInfo { SystemloadLevelCbInfoSystemloadLevelCbInfo44 explicit SystemloadLevelCbInfo(napi_env env) 45 : nativeEnv(env) {} ~SystemloadLevelCbInfoSystemloadLevelCbInfo46 ~SystemloadLevelCbInfo() 47 { 48 if (nativeEnv) { 49 if (callback) { 50 napi_delete_reference(nativeEnv, callback); 51 callback = nullptr; 52 } 53 if (asyncWork) { 54 napi_delete_async_work(nativeEnv, asyncWork); 55 asyncWork = nullptr; 56 } 57 } 58 } 59 napi_ref callback = nullptr; 60 napi_async_work asyncWork = nullptr; 61 napi_deferred deferred = nullptr; 62 napi_env nativeEnv = nullptr; 63 int32_t result = 0; 64 }; 65 66 using CallBackPair = std::pair<std::unique_ptr<NativeReference>, sptr<SystemloadListener>>; 67 // static function 68 static napi_value SystemloadOn(napi_env env, napi_callback_info info); 69 static napi_value SystemloadOff(napi_env env, napi_callback_info info); 70 static napi_value GetLevel(napi_env env, napi_callback_info info); 71 72 void OnSystemloadLevel(napi_env env, napi_value callbackObj, int32_t level); 73 private: 74 Systemload() = default; 75 ~Systemload(); 76 napi_value RegisterSystemloadCallback(napi_env env, napi_callback_info info); 77 napi_value UnRegisterSystemloadCallback(napi_env env, napi_callback_info info); 78 napi_value GetSystemloadLevel(napi_env env, napi_callback_info info); 79 bool CheckCallbackParam(napi_env env, napi_callback_info info, std::string &cbType, 80 napi_value *jsCallback, int32_t status); 81 82 static void Execute(napi_env env, void *data); 83 static void Complete(napi_env env, napi_status status, void *data); 84 static void CompleteCb(napi_env env, SystemloadLevelCbInfo* info); 85 static void HandleErrCode(const napi_env& env, int32_t errCode); 86 87 std::mutex jsCallbackMapLock_; 88 std::map<std::string, std::list<CallBackPair> > jsCallBackMap_; 89 }; 90 } // ResourceSchedule 91 } // OHOS 92 93 #endif // RESSCHED_INTERFACES_KITS_JS_SYSTEMLOAD_H