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