1 /*
2  * Copyright (c) 2021-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 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 final {
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,
39         std::vector<Rect> hotRectArea, int32_t rectTotal, napi_value callback, const int32_t fingers = 0);
40     void AddMonitor(napi_env jsEnv, const std::string &typeName, napi_value callback, const int32_t fingers = 0);
41     void RemoveMonitor(napi_env jsEnv, const std::string &typeName, napi_value callback, const int32_t fingers = 0);
42     void RemoveMonitor(napi_env jsEnv, const std::string &typeName, const int32_t fingers = 0);
43     void RemoveMonitor(napi_env jsEnv);
44     void OnPointerEventByMonitorId(int32_t id, int32_t fingers, std::shared_ptr<PointerEvent> pointEvent);
45     const std::shared_ptr<JsInputMonitor> GetMonitor(int32_t id, int32_t fingers);
46     std::string GetMonitorTypeName(int32_t id, int32_t fingers);
47     bool AddEnv(napi_env env, napi_callback_info cbInfo);
48     void RemoveEnv(napi_env env);
49     void ThrowError(napi_env env, int32_t code);
50     std::vector<Rect> GetHotRectAreaList(napi_env env, napi_value rectNapiValue, uint32_t rectListLength);
51 
52 private:
53     JsInputMonitorManager() = default;
54     bool IsExisting(napi_env env);
55     void RemoveEnv(std::map<napi_env, napi_ref>::iterator it);
56     void RemoveAllEnv();
57     bool IsFindJsInputMonitor(const std::shared_ptr<JsInputMonitor> monitor,
58         napi_env jsEnv, const std::string &typeName, napi_value callback, const int32_t fingers);
59     bool IsFindJsInputMonitor(const std::shared_ptr<JsInputMonitor> monitor,
60         napi_env jsEnv, const std::string &typeName, const int32_t fingers);
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     std::mutex envMutex_;
68 };
69 
70 #define JS_INPUT_MONITOR_MGR JsInputMonitorManager::GetInstance()
71 } // namespace MMI
72 } // namespace OHOS
73 #endif // JS_INPUT_MONITOR_MANAGER_H
74