• 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     auto exec = [ctxt, env, manager](AsyncCall::Context *ctx) {
80         auto ret = ImeSystemCmdChannel::GetInstance()->ConnectSystemCmd(manager);
81         ctxt->SetErrorCode(ret);
82         CHECK_RETURN_VOID(ret == ErrorCode::NO_ERROR, "ConnectSystemCmd return error!");
83         ctxt->SetState(napi_ok);
84     };
85     // 0 means JsAPI:ConnectSystemCmd has 0 params at most.
86     AsyncCall asyncCall(env, info, ctxt, 0);
87     return asyncCall.Call(env, exec, "ConnectSystemCmd");
88 }
89 
Subscribe(napi_env env,napi_callback_info info)90 napi_value JsKeyboardPanelManager::Subscribe(napi_env env, napi_callback_info info)
91 {
92     size_t argc = 2; // has 2 param
93     napi_value argv[2] = { nullptr };
94     napi_value thisVar = nullptr;
95     void *data = nullptr;
96     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
97     std::string type;
98     // 2 means least param num.
99     if (argc < 2 || !JsUtil::GetValue(env, argv[0], type) ||
100         !EventChecker::IsValidEventType(EventSubscribeModule::KEYBOARD_PANEL_MANAGER, type) ||
101         JsUtil::GetType(env, argv[1]) != napi_function) {
102         IMSA_HILOGE("subscribe failed, type: %{public}s!", type.c_str());
103         return nullptr;
104     }
105     auto manager = JsKeyboardPanelManager::GetInstance();
106     IMSA_HILOGD("subscribe type: %{public}s.", type.c_str());
107     std::shared_ptr<JSCallbackObject> callback =
108         std::make_shared<JSCallbackObject>(env, argv[1], std::this_thread::get_id(),
109             AppExecFwk::EventHandler::Current());
110     manager->RegisterListener(argv[1], type, callback);
111     return JsUtil::Const::Null(env);
112 }
113 
UnSubscribe(napi_env env,napi_callback_info info)114 napi_value JsKeyboardPanelManager::UnSubscribe(napi_env env, napi_callback_info info)
115 {
116     size_t argc = 2; // has 2 param
117     napi_value argv[2] = { nullptr };
118     napi_value thisVar = nullptr;
119     void *data = nullptr;
120     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
121     std::string type;
122     // 1 means least param num.
123     if (argc < 1 || !JsUtil::GetValue(env, argv[0], type) ||
124         !EventChecker::IsValidEventType(EventSubscribeModule::KEYBOARD_PANEL_MANAGER, type)) {
125         IMSA_HILOGE("unsubscribe failed, type: %{public}s!", type.c_str());
126         return nullptr;
127     }
128     auto manager = JsKeyboardPanelManager::GetInstance();
129     // if the second param is not napi_function/napi_null/napi_undefined, return
130     auto paramType = JsUtil::GetType(env, argv[1]);
131     if (paramType != napi_function && paramType != napi_null && paramType != napi_undefined) {
132         return nullptr;
133     }
134     // if the second param is napi_function, delete it, else delete all
135     argv[1] = paramType == napi_function ? argv[1] : nullptr;
136 
137     IMSA_HILOGD("unsubscribe type: %{public}s.", type.c_str());
138     manager->UnRegisterListener(argv[1], type);
139     return JsUtil::Const::Null(env);
140 }
141 
RegisterListener(napi_value callback,std::string type,std::shared_ptr<JSCallbackObject> callbackObj)142 void JsKeyboardPanelManager::RegisterListener(napi_value callback, std::string type,
143     std::shared_ptr<JSCallbackObject> callbackObj)
144 {
145     IMSA_HILOGD("register listener: %{public}s.", type.c_str());
146     std::lock_guard<std::recursive_mutex> lock(mutex_);
147     if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
148         IMSA_HILOGD("methodName: %{public}s is not registered!", type.c_str());
149     }
150     auto callbacks = jsCbMap_[type];
151     bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr<JSCallbackObject> cb) {
152         return JsUtils::Equals(cb->env_, callback, cb->callback_, cb->threadId_);
153     });
154     if (ret) {
155         IMSA_HILOGD("callback already registered!");
156         return;
157     }
158 
159     IMSA_HILOGI("add %{public}s callbackObj into jsCbMap_.", type.c_str());
160     jsCbMap_[type].push_back(std::move(callbackObj));
161 }
162 
UnRegisterListener(napi_value callback,std::string type)163 void JsKeyboardPanelManager::UnRegisterListener(napi_value callback, std::string type)
164 {
165     IMSA_HILOGI("event: %{public}s.", type.c_str());
166     std::lock_guard<std::recursive_mutex> lock(mutex_);
167     if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
168         IMSA_HILOGE("methodName: %{public}s already unRegistered!", type.c_str());
169         return;
170     }
171 
172     if (callback == nullptr) {
173         jsCbMap_.erase(type);
174         IMSA_HILOGI("callback is nullptr.");
175         return;
176     }
177 
178     for (auto item = jsCbMap_[type].begin(); item != jsCbMap_[type].end(); item++) {
179         if (JsUtils::Equals((*item)->env_, callback, (*item)->callback_, (*item)->threadId_)) {
180             jsCbMap_[type].erase(item);
181             break;
182         }
183     }
184 
185     if (jsCbMap_[type].empty()) {
186         jsCbMap_.erase(type);
187     }
188 }
189 
GetSmartMenuCfg(napi_env env,napi_callback_info info)190 napi_value JsKeyboardPanelManager::GetSmartMenuCfg(napi_env env, napi_callback_info info)
191 {
192     auto ctxt = std::make_shared<SmartMenuContext>();
193     auto output = [ctxt](napi_env env, napi_value *result) -> napi_status {
194         *result = JsUtil::GetValue(env, ctxt->smartMenu);
195         return napi_ok;
196     };
197     auto exec = [ctxt](AsyncCall::Context *ctx) {
198         ctxt->smartMenu = ImeSystemCmdChannel::GetInstance()->GetSmartMenuCfg();
199         ctxt->SetState(napi_ok);
200     };
201     ctxt->SetAction(nullptr, std::move(output));
202     // 1 means JsAPI:displayOptionalInputMethod has 1 params at most.
203     AsyncCall asyncCall(env, info, ctxt, 1);
204     return asyncCall.Call(env, exec, "GetSmartMenuCfg");
205 }
206 
SendPrivateCommand(napi_env env,napi_callback_info info)207 napi_value JsKeyboardPanelManager::SendPrivateCommand(napi_env env, napi_callback_info info)
208 {
209     auto ctxt = std::make_shared<SendPrivateCommandContext>();
210     auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
211         PARAM_CHECK_RETURN(env, argc > 0, "at least one parameter is required!", TYPE_NONE, napi_generic_failure);
212         CHECK_RETURN(JsUtils::GetValue(env, argv[0], ctxt->privateCommand) == napi_ok,
213             "commandData covert failed, type must be Record<string, CommandDataType>", napi_generic_failure);
214         if (!TextConfig::IsPrivateCommandValid(ctxt->privateCommand)) {
215             PARAM_CHECK_RETURN(env, false, "commandData size limit 32KB, count limit 5.", TYPE_NONE,
216                 napi_generic_failure);
217         }
218         ctxt->info = { std::chrono::system_clock::now(), ctxt->privateCommand };
219         privateCommandQueue_.Push(ctxt->info);
220         return napi_ok;
221     };
222     auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { return napi_ok; };
223     auto exec = [ctxt](AsyncCall::Context *ctx) {
224         privateCommandQueue_.Wait(ctxt->info);
225         int32_t code = ImeSystemCmdChannel::GetInstance()->SendPrivateCommand(ctxt->privateCommand);
226         privateCommandQueue_.Pop();
227         if (code == ErrorCode::NO_ERROR) {
228             ctxt->SetState(napi_ok);
229         } else {
230             ctxt->SetErrorCode(code);
231         }
232     };
233     ctxt->SetAction(std::move(input), std::move(output));
234     // 1 means JsAPI:SendPrivateCommand has 1 params at most.
235     AsyncCall asyncCall(env, info, ctxt, 1);
236     return asyncCall.Call(env, exec, "SendPrivateCommand");
237 }
238 
GetDefaultInputMethod(napi_env env,napi_callback_info info)239 napi_value JsKeyboardPanelManager::GetDefaultInputMethod(napi_env env, napi_callback_info info)
240 {
241     std::shared_ptr<Property> property;
242     int32_t ret = ImeSystemCmdChannel::GetInstance()->GetDefaultImeCfg(property);
243     if (ret != ErrorCode::NO_ERROR || property == nullptr) {
244         IMSA_HILOGE("GetDefaultImeCfg failed or property is nullptr ret: %{public}d!", ret);
245         return nullptr;
246     }
247     return GetJsInputMethodProperty(env, *property);
248 }
249 
GetJsInputMethodProperty(napi_env env,const Property & property)250 napi_value JsKeyboardPanelManager::GetJsInputMethodProperty(napi_env env, const Property &property)
251 {
252     napi_value obj = nullptr;
253     napi_create_object(env, &obj);
254 
255     auto ret = JsUtil::Object::WriteProperty(env, obj, "packageName", property.name);
256     ret = ret && JsUtil::Object::WriteProperty(env, obj, "name", property.name);
257     ret = ret && JsUtil::Object::WriteProperty(env, obj, "methodId", property.id);
258     ret = ret && JsUtil::Object::WriteProperty(env, obj, "id", property.id);
259     ret = ret && JsUtil::Object::WriteProperty(env, obj, "icon", property.icon);
260     ret = ret && JsUtil::Object::WriteProperty(env, obj, "iconId", property.iconId);
261     ret = ret && JsUtil::Object::WriteProperty(env, obj, "label", property.label);
262     ret = ret && JsUtil::Object::WriteProperty(env, obj, "labelId", property.labelId);
263     if (!ret) {
264         IMSA_HILOGE("init module inputMethod.Panel.PanelType failed, ret: %{public}d", ret);
265     }
266     return obj;
267 }
268 
ReceivePrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)269 void JsKeyboardPanelManager::ReceivePrivateCommand(
270     const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
271 {
272     IMSA_HILOGD("start.");
273     std::string type = "panelPrivateCommand";
274     auto entry = GetEntry(type, [&privateCommand](UvEntry &entry) { entry.privateCommand = privateCommand; });
275     if (entry == nullptr) {
276         return;
277     }
278     auto eventHandler = GetEventHandler();
279     if (eventHandler == nullptr) {
280         IMSA_HILOGE("eventHandler is nullptr!");
281         return;
282     }
283     InputMethodSyncTrace tracer("ReceivePrivateCommand trace");
284     auto task = [entry]() {
285         auto paramGetter = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
286             if (argc < 1) {
287                 return false;
288             }
289             napi_value jsObject = JsUtils::GetJsPrivateCommand(env, entry->privateCommand);
290             if (jsObject == nullptr) {
291                 IMSA_HILOGE("jsObject is nullptr");
292                 return false;
293             }
294             // 0 means the first param of callback.
295             args[0] = { jsObject };
296             return true;
297         };
298         // 1 means callback has 1 params.
299         JsCallbackHandler::Traverse(entry->vecCopy, { 1, paramGetter });
300     };
301     eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
302 }
303 
NotifyPanelStatus(const SysPanelStatus & sysPanelStatus)304 void JsKeyboardPanelManager::NotifyPanelStatus(const SysPanelStatus &sysPanelStatus)
305 {
306     IMSA_HILOGD("start");
307     std::string type = "isPanelShow";
308     auto entry =
309         GetEntry(type, [sysPanelStatus](UvEntry &entry) { entry.sysPanelStatus = sysPanelStatus; });
310     if (entry == nullptr) {
311         return;
312     }
313     auto eventHandler = GetEventHandler();
314     if (eventHandler == nullptr) {
315         IMSA_HILOGE("eventHandler is nullptr!");
316         return;
317     }
318     auto task = [entry]() {
319         auto paramGetter = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool {
320             if (argc < 1) {
321                 return false;
322             }
323             napi_value jsObject = JsPanelStatus::Write(env, entry->sysPanelStatus);
324             if (jsObject == nullptr) {
325                 IMSA_HILOGE("jsObject is nullptr!");
326                 return false;
327             }
328             // 0 means the first param of callback.
329             args[0] = { jsObject };
330             return true;
331         };
332         // 1 means callback has 1 params.
333         JsCallbackHandler::Traverse(entry->vecCopy, { 1, paramGetter });
334     };
335     eventHandler->PostTask(task, type, 0, AppExecFwk::EventQueue::Priority::VIP);
336 }
337 
GetEventHandler()338 std::shared_ptr<AppExecFwk::EventHandler> JsKeyboardPanelManager::GetEventHandler()
339 {
340     if (handler_ == nullptr) {
341         std::lock_guard<std::mutex> lock(eventHandlerMutex_);
342         if (handler_ == nullptr) {
343             handler_ = AppExecFwk::EventHandler::Current();
344         }
345     }
346     return handler_;
347 }
348 
GetEntry(const std::string & type,EntrySetter entrySetter)349 std::shared_ptr<JsKeyboardPanelManager::UvEntry> JsKeyboardPanelManager::GetEntry(const std::string &type,
350     EntrySetter entrySetter)
351 {
352     IMSA_HILOGD("start, type: %{public}s", type.c_str());
353     std::shared_ptr<UvEntry> entry = nullptr;
354     {
355         std::lock_guard<std::recursive_mutex> lock(mutex_);
356         if (jsCbMap_[type].empty()) {
357             IMSA_HILOGD("%{public}s cb-vector is empty.", type.c_str());
358             return nullptr;
359         }
360         entry = std::make_shared<UvEntry>(jsCbMap_[type], type);
361     }
362     if (entrySetter != nullptr) {
363         entrySetter(*entry);
364     }
365     return entry;
366 }
367 
Write(napi_env env,const SysPanelStatus & in)368 napi_value JsPanelStatus::Write(napi_env env, const SysPanelStatus &in)
369 {
370     napi_value jsObject = nullptr;
371     napi_create_object(env, &jsObject);
372     bool ret = JsUtil::Object::WriteProperty(env, jsObject, "inputType", static_cast<int32_t>(in.inputType));
373     ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "flag", in.flag);
374     ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "width", in.width);
375     ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "height", in.height);
376     ret = ret && JsUtil::Object::WriteProperty(env, jsObject, "isMainDisplay", in.isMainDisplay);
377     return ret ? jsObject : JsUtil::Const::Null(env);
378 }
379 } // namespace MiscServices
380 } // namespace OHOS
381