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 <map> 20 #include <memory> 21 #include <mutex> 22 #include <thread> 23 #include <uv.h> 24 25 #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/context.h" 26 #include "async_call.h" 27 #include "global.h" 28 #include "input_method_engine_listener.h" 29 #include "input_method_property.h" 30 #include "js_callback_object.h" 31 #include "js_panel.h" 32 #include "napi/native_api.h" 33 #include "input_method_panel.h" 34 35 namespace OHOS { 36 namespace MiscServices { 37 class JsInputMethodEngineSetting : public InputMethodEngineListener { 38 public: 39 JsInputMethodEngineSetting() = default; 40 ~JsInputMethodEngineSetting() override = default; 41 static napi_value Init(napi_env env, napi_value exports); 42 static napi_value GetInputMethodEngine(napi_env env, napi_callback_info info); 43 static napi_value GetInputMethodAbility(napi_env env, napi_callback_info info); 44 static napi_value Subscribe(napi_env env, napi_callback_info info); 45 static napi_value UnSubscribe(napi_env env, napi_callback_info info); 46 static napi_value CreatePanel(napi_env env, napi_callback_info info); 47 static napi_value DestroyPanel(napi_env env, napi_callback_info info); 48 void OnInputStart() override; 49 void OnKeyboardStatus(bool isShow) override; 50 void OnInputStop(const std::string &imeId) override; 51 void OnSetCallingWindow(uint32_t windowId) override; 52 void OnSetSubtype(const SubProperty &property) override; 53 54 private: 55 struct PanelContext : public AsyncCall::Context { 56 PanelInfo panelInfo = PanelInfo(); 57 std::shared_ptr<InputMethodPanel> panel = nullptr; 58 std::shared_ptr<OHOS::AbilityRuntime::Context> context = nullptr; PanelContextPanelContext59 PanelContext() : Context(nullptr, nullptr){}; PanelContextPanelContext60 PanelContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; 61 operatorPanelContext62 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 63 { 64 CHECK_RETURN(self != nullptr, "self is nullptr", napi_invalid_arg); 65 return Context::operator()(env, argc, argv, self); 66 } operatorPanelContext67 napi_status operator()(napi_env env, napi_value *result) override 68 { 69 if (status_ != napi_ok) { 70 output_ = nullptr; 71 return status_; 72 } 73 return Context::operator()(env, result); 74 } 75 }; 76 77 static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); 78 static std::shared_ptr<JsInputMethodEngineSetting> GetInputMethodEngineSetting(); 79 static bool InitInputMethodSetting(); 80 static napi_value GetJsConstProperty(napi_env env, uint32_t num); 81 static napi_value GetJsPanelTypeProperty(napi_env env); 82 static napi_value GetJsPanelFlagProperty(napi_env env); 83 static napi_value GetJsDirectionProperty(napi_env env); 84 static napi_value GetJsExtendActionProperty(napi_env env); 85 static napi_value GetIntJsConstProperty(napi_env env, int32_t num); 86 static napi_value GetIMEInstance(napi_env env, napi_callback_info info); 87 static napi_status GetContext(napi_env env, napi_value in, 88 std::shared_ptr<OHOS::AbilityRuntime::Context> &context); 89 void RegisterListener(napi_value callback, std::string type, std::shared_ptr<JSCallbackObject> callbackObj); 90 void UnRegisterListener(napi_value callback, std::string type); 91 static napi_value GetResultOnSetSubtype(napi_env env, const SubProperty &property); 92 static const std::string IMES_CLASS_NAME; 93 static thread_local napi_ref IMESRef_; 94 struct UvEntry { 95 std::vector<std::shared_ptr<JSCallbackObject>> vecCopy; 96 std::string type; 97 std::string imeid; 98 uint32_t windowid = 0; 99 SubProperty subProperty; UvEntryUvEntry100 UvEntry(const std::vector<std::shared_ptr<JSCallbackObject>> &cbVec, const std::string &type) 101 : vecCopy(cbVec), type(type) 102 { 103 } 104 }; 105 using EntrySetter = std::function<void(UvEntry &)>; 106 uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr); 107 uv_loop_s *loop_ = nullptr; 108 std::recursive_mutex mutex_; 109 std::map<std::string, std::vector<std::shared_ptr<JSCallbackObject>>> jsCbMap_; 110 static std::mutex engineMutex_; 111 static std::shared_ptr<JsInputMethodEngineSetting> inputMethodEngine_; 112 }; 113 } // namespace MiscServices 114 } // namespace OHOS 115 #endif // INTERFACE_KITS_JS_INPUT_METHOD_ENGINE_SETTING_H