• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "js_input_monitor_manager.h"
17 
18 #include <uv.h>
19 
20 #include "define_multimodal.h"
21 #include "napi_constants.h"
22 #include "util_napi_error.h"
23 
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, MMI_LOG_DOMAIN, "JsInputMonitorManager" };
28 
29 const std::string REFERENCE_UNREF = "napi_reference_unref";
30 constexpr int32_t MONITOR_REGISTER_EXCEED_MAX { 4100001 };
31 } // namespace
32 
GetInstance()33 JsInputMonitorManager& JsInputMonitorManager::GetInstance()
34 {
35     static JsInputMonitorManager instance;
36     return instance;
37 }
38 
AddMonitor(napi_env jsEnv,const std::string & typeName,napi_value callback)39 void JsInputMonitorManager::AddMonitor(napi_env jsEnv, const std::string &typeName, napi_value callback)
40 {
41     CALL_DEBUG_ENTER;
42     std::lock_guard<std::mutex> guard(mutex_);
43     for (const auto &item : monitors_) {
44         if ((item != nullptr) && (item->IsMatch(jsEnv, callback) != RET_ERR)) {
45             MMI_HILOGW("Add js monitor failed");
46             return;
47         }
48     }
49     auto monitor = std::make_shared<JsInputMonitor>(jsEnv, typeName, callback, nextId_++);
50     int32_t ret = monitor->Start();
51     if (ret < 0) {
52         MMI_HILOGE("js monitor startup failed");
53         ThrowError(jsEnv, ret);
54         return;
55     }
56     monitors_.push_back(monitor);
57 }
58 
RemoveMonitor(napi_env jsEnv,const std::string & typeName,napi_value callback)59 void JsInputMonitorManager::RemoveMonitor(napi_env jsEnv, const std::string &typeName, napi_value callback)
60 {
61     CALL_DEBUG_ENTER;
62     std::shared_ptr<JsInputMonitor> monitor = nullptr;
63     do {
64         std::lock_guard<std::mutex> guard(mutex_);
65         for (auto it = monitors_.begin(); it != monitors_.end();) {
66             if ((*it) == nullptr) {
67                 monitors_.erase(it++);
68                 continue;
69             }
70             if ((*it)->GetTypeName() == typeName) {
71                 if ((*it)->IsMatch(jsEnv, callback) == RET_OK) {
72                     monitor = *it;
73                     monitors_.erase(it++);
74                     MMI_HILOGD("Found monitor");
75                     break;
76                 }
77             }
78             ++it;
79         }
80     } while (0);
81     if (monitor != nullptr) {
82         monitor->Stop();
83     }
84 }
85 
RemoveMonitor(napi_env jsEnv,const std::string & typeName)86 void JsInputMonitorManager::RemoveMonitor(napi_env jsEnv, const std::string &typeName)
87 {
88     CALL_DEBUG_ENTER;
89     std::list<std::shared_ptr<JsInputMonitor>> monitors;
90     do {
91         std::lock_guard<std::mutex> guard(mutex_);
92         for (auto it = monitors_.begin(); it != monitors_.end();) {
93             if ((*it) == nullptr) {
94                 monitors_.erase(it++);
95                 continue;
96             }
97             if ((*it)->GetTypeName() == typeName) {
98                 if ((*it)->IsMatch(jsEnv) == RET_OK) {
99                     monitors.push_back(*it);
100                     monitors_.erase(it++);
101                     continue;
102                 }
103             }
104             ++it;
105         }
106     } while (0);
107 
108     for (const auto &item : monitors) {
109         if (item != nullptr) {
110             item->Stop();
111         }
112     }
113 }
114 
RemoveMonitor(napi_env jsEnv)115 void JsInputMonitorManager::RemoveMonitor(napi_env jsEnv)
116 {
117     CALL_DEBUG_ENTER;
118     std::list<std::shared_ptr<JsInputMonitor>> monitors;
119     do {
120         std::lock_guard<std::mutex> guard(mutex_);
121         for (auto it = monitors_.begin(); it != monitors_.end();) {
122             if ((*it) == nullptr) {
123                 monitors_.erase(it++);
124                 continue;
125             }
126             if ((*it)->IsMatch(jsEnv) == RET_OK) {
127                 monitors.push_back(*it);
128                 monitors_.erase(it++);
129                 continue;
130             }
131             ++it;
132         }
133     } while (0);
134 
135     for (const auto &item : monitors) {
136         if (item != nullptr) {
137             item->Stop();
138         }
139     }
140 }
141 
GetMonitor(int32_t id)142 const std::shared_ptr<JsInputMonitor> JsInputMonitorManager::GetMonitor(int32_t id)
143 {
144     CALL_DEBUG_ENTER;
145     std::lock_guard<std::mutex> guard(mutex_);
146     for (const auto &item : monitors_) {
147         if ((item != nullptr) && (item->GetId() == id)) {
148             return item;
149         }
150     }
151     MMI_HILOGD("No monitor found");
152     return nullptr;
153 }
154 
AddEnv(napi_env env,napi_callback_info cbInfo)155 bool JsInputMonitorManager::AddEnv(napi_env env, napi_callback_info cbInfo)
156 {
157     CALL_DEBUG_ENTER;
158     if (IsExisting(env)) {
159         MMI_HILOGD("Env is already existent");
160         return true;
161     }
162     napi_value thisVar = nullptr;
163     void *data = nullptr;
164     int32_t *id = new (std::nothrow) int32_t;
165     CHKPF(id);
166     *id = 0;
167     if (napi_get_cb_info(env, cbInfo, nullptr, nullptr, &thisVar, &data) != napi_ok) {
168         MMI_HILOGE("GET_CB_INFO failed");
169         auto infoTemp = std::string("AddEnv GET_CB_INFO failed");
170         napi_throw_error(env, nullptr, infoTemp.c_str());
171         delete id;
172         return false;
173     }
174     auto status = napi_wrap(env, thisVar, static_cast<void*>(id),
175                             [](napi_env env, void *data, void *hint) {
176                                 MMI_HILOGD("napi_wrap enter");
177                                 int32_t *id = static_cast<int32_t *>(data);
178                                 delete id;
179                                 id = nullptr;
180                                 JsInputMonMgr.RemoveMonitor(env);
181                                 JsInputMonMgr.RemoveEnv(env);
182                                 MMI_HILOGD("napi_wrap leave");
183                                 }, nullptr, nullptr);
184     if (status != napi_ok) {
185         MMI_HILOGE("napi_wrap failed");
186         delete id;
187         return false;
188     }
189     napi_ref ref = nullptr;
190     status = napi_create_reference(env, thisVar, 1, &ref);
191     if (status != napi_ok) {
192         MMI_HILOGE("napi_create_reference failed");
193         return false;
194     }
195     auto iter = envManager_.insert(std::pair<napi_env, napi_ref>(env, ref));
196     if (!iter.second) {
197         MMI_HILOGE("Insert value failed");
198         return false;
199     }
200     return true;
201 }
202 
RemoveEnv(napi_env env)203 void JsInputMonitorManager::RemoveEnv(napi_env env)
204 {
205     CALL_DEBUG_ENTER;
206     auto it = envManager_.find(env);
207     if (it == envManager_.end()) {
208         MMI_HILOGD("No env found");
209         return;
210     }
211     RemoveEnv(it);
212 }
213 
RemoveEnv(std::map<napi_env,napi_ref>::iterator it)214 void JsInputMonitorManager::RemoveEnv(std::map<napi_env, napi_ref>::iterator it)
215 {
216     CALL_DEBUG_ENTER;
217     uint32_t refCount;
218     CHKRV(it->first, napi_reference_unref(it->first, it->second, &refCount), REFERENCE_UNREF);
219     envManager_.erase(it);
220 }
221 
RemoveAllEnv()222 void JsInputMonitorManager::RemoveAllEnv()
223 {
224     CALL_DEBUG_ENTER;
225     for (auto it = envManager_.begin(); it != envManager_.end();) {
226         RemoveEnv(it++);
227     }
228 }
229 
IsExisting(napi_env env)230 bool JsInputMonitorManager::IsExisting(napi_env env)
231 {
232     CALL_DEBUG_ENTER;
233     auto it = envManager_.find(env);
234     if (it == envManager_.end()) {
235         MMI_HILOGD("No env found");
236         return false;
237     }
238 
239     return true;
240 }
241 
ThrowError(napi_env env,int32_t code)242 void JsInputMonitorManager::ThrowError(napi_env env, int32_t code)
243 {
244     int32_t errorCode = -code;
245     if (errorCode == MONITOR_REGISTER_EXCEED_MAX) {
246         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Maximum number of listeners exceeded for a single process");
247     } else if (errorCode == COMMON_PERMISSION_CHECK_ERROR) {
248         THROWERR_API9(env, COMMON_PERMISSION_CHECK_ERROR, "monitor", "ohos.permission.INPUT_MONITORING");
249     } else {
250         MMI_HILOGE("Add monitor failed");
251     }
252 }
253 } // namespace MMI
254 } // namespace OHOS
255