• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "js_keyboard_panel_manager.h"
17 
18 #include "event_checker.h"
19 #include "input_method_utils.h"
20 #include "js_callback_handler.h"
21 #include "js_util.h"
22 #include "js_utils.h"
23 
24 namespace OHOS {
25 namespace MiscServices {
26 constexpr int32_t MAX_WAIT_TIME_PRIVATE_COMMAND = 2000;
27 BlockQueue<PrivateCommandInfo> JsKeyboardPanelManager::privateCommandQueue_{ MAX_WAIT_TIME_PRIVATE_COMMAND };
28 std::mutex JsKeyboardPanelManager::managerMutex_;
29 sptr<JsKeyboardPanelManager> JsKeyboardPanelManager::keyboardPanelManager_{ nullptr };
30 
JsKeyboardPanelManager()31 JsKeyboardPanelManager::JsKeyboardPanelManager()
32 {
33     std::lock_guard<std::mutex> lock(eventHandlerMutex_);
34     handler_ = AppExecFwk::EventHandler::Current();
35 }
36 
Init(napi_env env,napi_value info)37 napi_value JsKeyboardPanelManager::Init(napi_env env, napi_value info)
38 {
39     napi_property_descriptor descriptor[] = {
40         DECLARE_NAPI_FUNCTION("sendPrivateCommand", SendPrivateCommand),
41         DECLARE_NAPI_FUNCTION("getSmartMenuCfg", GetSmartMenuCfg),
42         DECLARE_NAPI_FUNCTION("on", Subscribe),
43         DECLARE_NAPI_FUNCTION("off", UnSubscribe),
44         DECLARE_NAPI_FUNCTION("getDefaultInputMethod", GetDefaultInputMethod),
45         DECLARE_NAPI_FUNCTION("connectSystemCmd", ConnectSystemCmd),
46     };
47     NAPI_CALL(env,
48         napi_define_properties(env, info, sizeof(descriptor) / sizeof(napi_property_descriptor), descriptor));
49     return info;
50 }
51 
GetInstance()52 sptr<JsKeyboardPanelManager> JsKeyboardPanelManager::GetInstance()
53 {
54     if (keyboardPanelManager_ == nullptr) {
55         std::lock_guard<std::mutex> lock(managerMutex_);
56         if (keyboardPanelManager_ == nullptr) {
57             keyboardPanelManager_ = new (std::nothrow) JsKeyboardPanelManager();
58         }
59     }
60     return keyboardPanelManager_;
61 }
62 
63 struct PanelManagerContext : public AsyncCall::Context {
PanelManagerContextOHOS::MiscServices::PanelManagerContext64     PanelManagerContext() : Context(nullptr, nullptr){};
operator ()OHOS::MiscServices::PanelManagerContext65     napi_status operator()(napi_env env, napi_value *result) override
66     {
67         if (status_ != napi_ok) {
68             output_ = nullptr;
69             return status_;
70         }
71         return Context::operator()(env, result);
72     }
73 };
74 
ConnectSystemCmd(napi_env env,napi_callback_info info)75 napi_value JsKeyboardPanelManager::ConnectSystemCmd(napi_env env, napi_callback_info info)
76 {
77     auto ctxt = std::make_shared<PanelManagerContext>();
78     auto manager = JsKeyboardPanelManager::GetInstance();
79     if (manager == nullptr) {
80         IMSA_HILOGE("manager is nullptr!");
81         return nullptr;
82     }
83     auto exec = [ctxt, env, manager](AsyncCall::Context *ctx) {
84         auto channel = ImeSystemCmdChannel::GetInstance();
85         if (channel == nullptr) {
86             ctxt->SetErrorCode(ErrorCode::ERROR_NULL_POINTER);
87             ctxt->SetState(napi_generic_failure);
88             IMSA_HILOGE("channel is nullptr!");
89             return;
90         }
91         auto ret = channel->ConnectSystemCmd(manager);
92         ctxt->SetErrorCode(ret);
93         CHECK_RETURN_VOID(ret == ErrorCode::NO_ERROR, "ConnectSystemCmd return error!");
94         ctxt->SetState(napi_ok);
95     };
96     // 0 means JsAPI:ConnectSystemCmd has 0 params at most.
97     AsyncCall asyncCall(env, info, ctxt, 0);
98     return asyncCall.Call(env, exec, "ConnectSystemCmd");
99 }
100 
Subscribe(napi_env env,napi_callback_info info)101 napi_value JsKeyboardPanelManager::Subscribe(napi_env env, napi_callback_info info)
102 {
103     size_t argc = 2; // has 2 param
104     napi_value argv[2] = { nullptr };
105     napi_value thisVar = nullptr;
106     void *data = nullptr;
107     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
108     std::string type;
109     // 2 means least param num.
110     if (argc < 2 || !JsUtil::GetValue(env, argv[0], type) ||
111         !EventChecker::IsValidEventType(EventSubscribeModule::KEYBOARD_PANEL_MANAGER, type) ||
112         JsUtil::GetType(env, argv[1]) != napi_function) {
113         IMSA_HILOGE("subscribe failed, type: %{public}s!", type.c_str());
114         return nullptr;
115     }
116     auto manager = JsKeyboardPanelManager::GetInstance();
117     if (manager == nullptr) {
118         IMSA_HILOGE("manager is nullptr!");
119         return nullptr;
120     }
121     IMSA_HILOGD("subscribe type: %{public}s.", type.c_str());
122     std::shared_ptr<JSCallbackObject> callback =
123         std::make_shared<JSCallbackObject>(env, argv[1], std::this_thread::get_id(),
124             AppExecFwk::EventHandler::Current());
125     manager->RegisterListener(argv[1], type, callback);
126     return JsUtil::Const::Null(env);
127 }
128 
UnSubscribe(napi_env env,napi_callback_info info)129 napi_value JsKeyboardPanelManager::UnSubscribe(napi_env env, napi_callback_info info)
130 {
131     size_t argc = 2; // has 2 param
132     napi_value argv[2] = { nullptr };
133     napi_value thisVar = nullptr;
134     void *data = nullptr;
135     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
136     std::string type;
137     // 1 means least param num.
138     if (argc < 1 || !JsUtil::GetValue(env, argv[0], type) ||
139         !EventChecker::IsValidEventType(EventSubscribeModule::KEYBOARD_PANEL_MANAGER, type)) {
140         IMSA_HILOGE("unsubscribe failed, type: %{public}s!", type.c_str());
141         return nullptr;
142     }
143     auto manager = JsKeyboardPanelManager::GetInstance();
144     if (manager == nullptr) {
145         IMSA_HILOGE("manager is nullptr!");
146         return nullptr;
147     }
148     // if the second param is not napi_function/napi_null/napi_undefined, return
149     auto paramType = JsUtil::GetType(env, argv[1]);
150     if (paramType != napi_function && paramType != napi_null && paramType != napi_undefined) {
151         return nullptr;
152     }
153     // if the second param is napi_function, delete it, else delete all
154     argv[1] = paramType == napi_function ? argv[1] : nullptr;
155 
156     IMSA_HILOGD("unsubscribe type: %{public}s.", type.c_str());
157     manager->UnRegisterListener(argv[1], type);
158     return JsUtil::Const::Null(env);
159 }
160 
RegisterListener(napi_value callback,std::string type,std::shared_ptr<JSCallbackObject> callbackObj)161 void JsKeyboardPanelManager::RegisterListener(napi_value callback, std::string type,
162     std::shared_ptr<JSCallbackObject> callbackObj)
163 {
164     IMSA_HILOGD("register listener: %{public}s.", type.c_str());
165     std::lock_guard<std::recursive_mutex> lock(mutex_);
166     if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
167         IMSA_HILOGD("methodName: %{public}s is not registered!", type.c_str());
168     }
169     auto callbacks = jsCbMap_[type];
170     bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr<JSCallbackObject> cb) {
171         if (cb == nullptr) {
172             return false;
173         }
174         return JsUtils::Equals(cb->env_, callback, cb->callback_, cb->threadId_);
175     });
176     if (ret) {
177         IMSA_HILOGD("callback already registered!");
178         return;
179     }
180 
181     IMSA_HILOGI("add %{public}s callbackObj into jsCbMap_.", type.c_str());
182     jsCbMap_[type].push_back(std::move(callbackObj));
183 }
184 
UnRegisterListener(napi_value callback,std::string type)185 void JsKeyboardPanelManager::UnRegisterListener(napi_value callback, std::string type)
186 {
187     IMSA_HILOGI("event: %{public}s.", type.c_str());
188     std::lock_guard<std::recursive_mutex> lock(mutex_);
189     if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
190         IMSA_HILOGE("methodName: %{public}s already unRegistered!", type.c_str());
191         return;
192     }
193 
194     if (callback == nullptr) {
195         jsCbMap_.erase(type);
196         IMSA_HILOGI("callback is nullptr.");
197         return;
198     }
199 
200     for (auto item = jsCbMap_[type].begin(); item != jsCbMap_[type].end(); item++) {
201         if (JsUtils::Equals((*item)->env_, callback, (*item)->callback_, (*item)->threadId_)) {
202             jsCbMap_[type].erase(item);
203             break;
204         }
205     }
206 
207     if (jsCbMap_[type].empty()) {
208         jsCbMap_.erase(type);
209     }
210 }
211 
GetSmartMenuCfg(napi_env env,napi_callback_info info)212 napi_value JsKeyboardPanelManager::GetSmartMenuCfg(napi_env env, napi_callback_info info)
213 {
214     auto ctxt = std::make_shared<SmartMenuContext>();
215     auto output = [ctxt](napi_env env, napi_value *result) -> napi_status {
216         *result = JsUtil::GetValue(env, ctxt->smartMenu);
217         return napi_ok;
218     };
219     auto exec = [ctxt](AsyncCall::Context *ctx) {
220         auto channel = ImeSystemCmdChannel::GetInstance();
221         if (channel == nullptr) {
222             ctxt->SetState(napi_generic_failure);
223             ctxt->smartMenu = {};
224             ctxt->SetErrorCode(ErrorCode::ERROR_NULL_POINTER);
225             return;
226         }
227         ctxt->smartMenu = channel->GetSmartMenuCfg();
228         ctxt->SetState(napi_ok);
229     };
230     ctxt->SetAction(nullptr, std::move(output));
231     // 1 means JsAPI:displayOptionalInputMethod has 1 params at most.
232     AsyncCall asyncCall(env, info, ctxt, 1);
233     return asyncCall.Call(env, exec, "GetSmartMenuCfg");
234 }
235 
SendPrivateCommand(napi_env env,napi_callback_info info)236 napi_value JsKeyboardPanelManager::SendPrivateCommand(napi_env env, napi_callback_info info)
237 {
238     auto ctxt = std::make_shared<SendPrivateCommandContext>();
239     auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
240         PARAM_CHECK_RETURN(env, argc > 0, "at least one parameter is required!", TYPE_NONE, napi_generic_failure);
241         CHECK_RETURN(JsUtils::GetValue(env, argv[0], ctxt->privateCommand) == napi_ok,
242             "commandData covert failed, type must be Record<string, CommandDataType>", napi_generic_failure);
243         if (!TextConfig::IsPrivateCommandValid(ctxt->privateCommand)) {
244             PARAM_CHECK_RETURN(env, false, "commandData size limit 32KB, count limit 5.", TYPE_NONE,
245                 napi_generic_failure);
246         }
247         ctxt->info = { std::chrono::system_clock::now(), ctxt->privateCommand };
248         privateCommandQueue_.Push(ctxt->info);
249         return napi_ok;
250     };
251     auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { return napi_ok; };
252     auto exec = [ctxt](AsyncCall::Context *ctx) {
253         privateCommandQueue_.Wait(ctxt->info);
254         auto channel = ImeSystemCmdChannel::GetInstance();
255         if (channel == nullptr) {
256             ctxt->SetState(napi_generic_failure);
257             return;
258         }
259         int32_t code = channel->SendPrivateCommand(ctxt->privateCommand);
260         privateCommandQueue_.Pop();
261         if (code == ErrorCode::NO_ERROR) {
262             ctxt->SetState(napi_ok);
263         } else {
264             ctxt->SetErrorCode(code);
265         }
266     };
267     ctxt->SetAction(std::move(input), std::move(output));
268     // 1 means JsAPI:SendPrivateCommand has 1 params at most.
269     AsyncCall asyncCall(env, info, ctxt, 1);
270     return asyncCall.Call(env, exec, "SendPrivateCommand");
271 }
272 
GetDefaultInputMethod(napi_env env,napi_callback_info info)273 napi_value JsKeyboardPanelManager::GetDefaultInputMethod(napi_env env, napi_callback_info info)
274 {
275     std::shared_ptr<Property> property;
276     auto channel = ImeSystemCmdChannel::GetInstance();
277     if (channel == nullptr) {
278         return nullptr;
279     }
280     int32_t ret = channel->GetDefaultImeCfg(property);
281     if (ret != ErrorCode::NO_ERROR || property == nullptr) {
282         IMSA_HILOGE("GetDefaultImeCfg failed or property is nullptr ret: %{public}d!", ret);
283         return nullptr;
284     }
285     return GetJsInputMethodProperty(env, *property);
286 }
287 
GetJsInputMethodProperty(napi_env env,const Property & property)288 napi_value JsKeyboardPanelManager::GetJsInputMethodProperty(napi_env env, const Property &property)
289 {
290     napi_value obj = nullptr;
291     napi_create_object(env, &obj);
292 
293     auto ret = JsUtil::Object::WriteProperty(env, obj, "packageName", property.name);
294     ret = ret && JsUtil::Object::WriteProperty(env, obj, "name", property.name);
295     ret = ret && JsUtil::Object::WriteProperty(env, obj, "methodId", property.id);
296     ret = ret && JsUtil::Object::WriteProperty(env, obj, "id", property.id);
297     ret = ret && JsUtil::Object::WriteProperty(env, obj, "icon", property.icon);
298     ret = ret && JsUtil::Object::WriteProperty(env, obj, "iconId", property.iconId);
299     ret = ret && JsUtil::Object::WriteProperty(env, obj, "label", property.label);
300     ret = ret && JsUtil::Object::WriteProperty(env, obj, "labelId", property.labelId);
301     if (!ret) {
302         IMSA_HILOGE("init module inputMethod.Panel.PanelType failed, ret: %{public}d", ret);
303     }
304     return obj;
305 }
306 
ReceivePrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)307 void JsKeyboardPanelManager::ReceivePrivateCommand(
308     const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
309 {
310     IMSA_HILOGD("start.");
311     std::string type = "panelPrivateCommand";
312     auto entry = GetEntry(type, [&privateCommand](UvEntry &entry) { entry.privateCommand = privateCommand; });
313     if (entry == nullptr) {
314         return;
315     }
316     auto eventHandler = GetEventHandler();
317     if (eventHandler == nullptr) {
318         IMSA_HILOGE("eventHandler is nullptr!");
319         return;
320     }
321     InputMethodSyncTrace tracer("ReceivePrivateCommand trace");
322     auto task = [entry]() {
323         auto paramGetter = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
324             if (argc < 1) {
325                 return false;
326             }
327             napi_value jsObject = JsUtils::GetJsPrivateCommand(env, entry->privateCommand);
328             if (jsObject == nullptr) {
329                 IMSA_HILOGE("jsObject is nullptr");
330                 return false;
331             }
332             // 0 means the first param of callback.
333             args[0] = { jsObject };
334             return true;
335         };
336         // 1 means callback has 1 params.
337         JsCallbackHandler::Traverse(entry->vecCopy, { 1, paramGetter });
338     };
339     eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
340 }
341 
NotifyPanelStatus(const SysPanelStatus & sysPanelStatus)342 void JsKeyboardPanelManager::NotifyPanelStatus(const SysPanelStatus &sysPanelStatus)
343 {
344     IMSA_HILOGD("start");
345     std::string type = "isPanelShow";
346     auto entry =
347         GetEntry(type, [sysPanelStatus](UvEntry &entry) { entry.sysPanelStatus = sysPanelStatus; });
348     if (entry == nullptr) {
349         return;
350     }
351     auto eventHandler = GetEventHandler();
352     if (eventHandler == nullptr) {
353         IMSA_HILOGE("eventHandler is nullptr!");
354         return;
355     }
356     auto task = [entry]() {
357         auto paramGetter = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
358             if (argc < 1) {
359                 return false;
360             }
361             napi_value jsObject = JsPanelStatus::Write(env, entry->sysPanelStatus);
362             if (jsObject == nullptr) {
363                 IMSA_HILOGE("jsObject is nullptr!");
364                 return false;
365             }
366             // 0 means the first param of callback.
367             args[0] = { jsObject };
368             return true;
369         };
370         // 1 means callback has 1 params.
371         JsCallbackHandler::Traverse(entry->vecCopy, { 1, paramGetter });
372     };
373     eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
374 }
375 
GetEventHandler()376 std::shared_ptr<AppExecFwk::EventHandler> JsKeyboardPanelManager::GetEventHandler()
377 {
378     if (handler_ == nullptr) {
379         std::lock_guard<std::mutex> lock(eventHandlerMutex_);
380         if (handler_ == nullptr) {
381             handler_ = AppExecFwk::EventHandler::Current();
382         }
383     }
384     return handler_;
385 }
386 
GetEntry(const std::string & type,EntrySetter entrySetter)387 std::shared_ptr<JsKeyboardPanelManager::UvEntry> JsKeyboardPanelManager::GetEntry(const std::string &type,
388     EntrySetter entrySetter)
389 {
390     IMSA_HILOGD("start, type: %{public}s", type.c_str());
391     std::shared_ptr<UvEntry> entry = nullptr;
392     {
393         std::lock_guard<std::recursive_mutex> lock(mutex_);
394         if (jsCbMap_[type].empty()) {
395             IMSA_HILOGD("%{public}s cb-vector is empty.", type.c_str());
396             return nullptr;
397         }
398         entry = std::make_shared<UvEntry>(jsCbMap_[type], type);
399     }
400     if (entrySetter != nullptr) {
401         entrySetter(*entry);
402     }
403     return entry;
404 }
405 
Write(napi_env env,const SysPanelStatus & in)406 napi_value JsPanelStatus::Write(napi_env env, const SysPanelStatus &in)
407 {
408     napi_value jsObject = nullptr;
409     napi_create_object(env, &jsObject);
410     bool ret = JsUtil::Object::WriteProperty(env, jsObject, "inputType", static_cast<int32_t>(in.inputType));
411     ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "flag", in.flag);
412     ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "width", in.width);
413     ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "height", in.height);
414     ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "isPanelRaised", in.isPanelRaised);
415     ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "needFuncButton", in.needFuncButton);
416     return ret ? jsObject : JsUtil::Const::Null(env);
417 }
418 } // namespace MiscServices
419 } // namespace OHOS
420