1 /* 2 * Copyright (c) 2021-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 #ifndef JS_INPUT_MONITOR_MANAGER_H 17 #define JS_INPUT_MONITOR_MANAGER_H 18 19 #include <list> 20 #include <mutex> 21 #include <cinttypes> 22 #include "js_input_monitor.h" 23 #include "napi/native_api.h" 24 #include "napi/native_node_api.h" 25 26 namespace OHOS { 27 namespace MMI { 28 class JsInputMonitorManager { 29 public: 30 static JsInputMonitorManager& GetInstance(); 31 32 ~JsInputMonitorManager(); 33 34 void AddMonitor(napi_env jsEnv, napi_value receiver); 35 36 void RemoveMonitor(napi_env jsEnv, napi_value receiver); 37 38 void RemoveMonitor(napi_env jsEnv); 39 40 std::shared_ptr<JsInputMonitor> GetMonitor(int32_t id); 41 42 bool AddEnv(napi_env env, napi_callback_info cbInfo); 43 44 void RemoveEnv(napi_env env); 45 private: 46 bool IsExisting(napi_env env); 47 48 void RemoveEnv(std::map<napi_env, napi_ref>::iterator it); 49 50 void RemoveAllEnv(); 51 52 JsInputMonitorManager() = default; 53 54 JsInputMonitorManager(const JsInputMonitorManager&) = delete; 55 56 JsInputMonitorManager(JsInputMonitorManager&&) = delete; 57 58 JsInputMonitorManager& operator=(const JsInputMonitorManager&) = delete; 59 60 private: 61 int32_t nextId_ {0}; 62 std::mutex mutex_; 63 std::list<std::shared_ptr<JsInputMonitor>> monitors_; 64 std::map<napi_env, napi_ref> envManager_; 65 }; 66 67 #define JSIMM JsInputMonitorManager::GetInstance() 68 } // namespace MMI 69 } // namespace OHOS 70 #endif // JS_INPUT_MONITOR_MANAGER_H 71