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