1 /* 2 * Copyright (c) 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 INTERFACE_KITS_JS_INPUT_METHOD_ENGINE_SETTING_H 17 #define INTERFACE_KITS_JS_INPUT_METHOD_ENGINE_SETTING_H 18 19 #include <uv.h> 20 21 #include <map> 22 #include <memory> 23 #include <mutex> 24 #include <thread> 25 #include <unordered_map> 26 27 #include "async_call.h" 28 #include "event_handler.h" 29 #include "global.h" 30 #include "input_method_engine_listener.h" 31 #include "input_method_panel.h" 32 #include "input_method_property.h" 33 #include "js_callback_object.h" 34 #include "js_panel.h" 35 #include "napi/native_api.h" 36 37 namespace OHOS { 38 namespace MiscServices { 39 class JsInputMethodEngineSetting : public InputMethodEngineListener { 40 public: 41 JsInputMethodEngineSetting() = default; 42 ~JsInputMethodEngineSetting() override = default; 43 static napi_value Init(napi_env env, napi_value exports); 44 static napi_value InitProperty(napi_env env, napi_value exports); 45 static napi_value GetInputMethodEngine(napi_env env, napi_callback_info info); 46 static napi_value GetInputMethodAbility(napi_env env, napi_callback_info info); 47 static napi_value Subscribe(napi_env env, napi_callback_info info); 48 static napi_value UnSubscribe(napi_env env, napi_callback_info info); 49 static napi_value CreatePanel(napi_env env, napi_callback_info info); 50 static napi_value DestroyPanel(napi_env env, napi_callback_info info); 51 static napi_value GetSecurityMode(napi_env env, napi_callback_info info); 52 void OnInputStart() override; 53 void OnKeyboardStatus(bool isShow) override; 54 int32_t OnInputStop() override; 55 void OnSetCallingWindow(uint32_t windowId) override; 56 void OnSetSubtype(const SubProperty &property) override; 57 void OnSecurityChange(int32_t security) override; 58 void ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override; 59 bool PostTaskToEventHandler(std::function<void()> task, const std::string &taskName) override; 60 61 private: 62 struct PanelContext : public AsyncCall::Context { 63 PanelInfo panelInfo = PanelInfo(); 64 std::shared_ptr<InputMethodPanel> panel = nullptr; 65 std::shared_ptr<OHOS::AbilityRuntime::Context> context = nullptr; PanelContextPanelContext66 PanelContext() : Context(nullptr, nullptr){}; PanelContextPanelContext67 PanelContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; 68 operatorPanelContext69 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 70 { 71 CHECK_RETURN(self != nullptr, "self is nullptr", napi_invalid_arg); 72 return Context::operator()(env, argc, argv, self); 73 } operatorPanelContext74 napi_status operator()(napi_env env, napi_value *result) override 75 { 76 if (status_ != napi_ok) { 77 output_ = nullptr; 78 return status_; 79 } 80 return Context::operator()(env, result); 81 } 82 }; 83 84 static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); 85 static std::shared_ptr<JsInputMethodEngineSetting> GetInputMethodEngineSetting(); 86 static bool InitInputMethodSetting(); 87 static napi_value GetJsConstProperty(napi_env env, uint32_t num); 88 static napi_value GetJsPanelTypeProperty(napi_env env); 89 static napi_value GetJsPanelFlagProperty(napi_env env); 90 static napi_value GetJsDirectionProperty(napi_env env); 91 static napi_value GetJsExtendActionProperty(napi_env env); 92 static napi_value GetJsSecurityModeProperty(napi_env env); 93 static napi_value GetIntJsConstProperty(napi_env env, int32_t num); 94 static napi_value GetIMEInstance(napi_env env, napi_callback_info info); 95 static napi_status GetContext(napi_env env, napi_value in, 96 std::shared_ptr<OHOS::AbilityRuntime::Context> &context); 97 void RegisterListener(napi_value callback, std::string type, std::shared_ptr<JSCallbackObject> callbackObj); 98 void UnRegisterListener(napi_value callback, std::string type); 99 static napi_value GetResultOnSetSubtype(napi_env env, const SubProperty &property); 100 static const std::string IMES_CLASS_NAME; 101 static thread_local napi_ref IMESRef_; 102 struct UvEntry { 103 std::vector<std::shared_ptr<JSCallbackObject>> vecCopy; 104 std::string type; 105 uint32_t windowid = 0; 106 int32_t security = 0; 107 SubProperty subProperty; 108 std::unordered_map<std::string, PrivateDataValue> privateCommand; UvEntryUvEntry109 UvEntry(const std::vector<std::shared_ptr<JSCallbackObject>> &cbVec, const std::string &type) 110 : vecCopy(cbVec), type(type) 111 { 112 } 113 }; 114 using EntrySetter = std::function<void(UvEntry &)>; 115 static std::shared_ptr<AppExecFwk::EventHandler> GetEventHandler(); 116 std::shared_ptr<UvEntry> GetEntry(const std::string &type, EntrySetter entrySetter = nullptr); 117 uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr); 118 void FreeWorkIfFail(int ret, uv_work_t *work); 119 uv_loop_s *loop_ = nullptr; 120 std::recursive_mutex mutex_; 121 std::map<std::string, std::vector<std::shared_ptr<JSCallbackObject>>> jsCbMap_; 122 static std::mutex engineMutex_; 123 static std::shared_ptr<JsInputMethodEngineSetting> inputMethodEngine_; 124 static std::mutex eventHandlerMutex_; 125 static std::shared_ptr<AppExecFwk::EventHandler> handler_; 126 }; 127 } // namespace MiscServices 128 } // namespace OHOS 129 #endif // INTERFACE_KITS_JS_INPUT_METHOD_ENGINE_SETTING_H