1 /* 2 * Copyright (c) 2021 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 #ifndef INTERFACE_KITS_JS_GETINPUT_METHOD_SETTING_H 16 #define INTERFACE_KITS_JS_GETINPUT_METHOD_SETTING_H 17 18 #include <uv.h> 19 20 #include "async_call.h" 21 #include "event_handler.h" 22 #include "global.h" 23 #include "input_method_controller.h" 24 #include "input_method_status.h" 25 #include "js_callback_object.h" 26 27 namespace OHOS { 28 namespace MiscServices { 29 struct ListInputContext : public AsyncCall::Context { 30 InputMethodStatus inputMethodStatus = InputMethodStatus::ALL; 31 std::vector<Property> properties; 32 std::vector<SubProperty> subProperties; 33 Property property; 34 napi_status status = napi_generic_failure; ListInputContextListInputContext35 ListInputContext() : Context(nullptr, nullptr){}; ListInputContextListInputContext36 ListInputContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; 37 operatorListInputContext38 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 39 { 40 CHECK_RETURN(self != nullptr, "self is nullptr!", napi_invalid_arg); 41 return Context::operator()(env, argc, argv, self); 42 } operatorListInputContext43 napi_status operator()(napi_env env, napi_value *result) override 44 { 45 if (status != napi_ok) { 46 output_ = nullptr; 47 return status; 48 } 49 return Context::operator()(env, result); 50 } 51 }; 52 53 struct DisplayOptionalInputMethodContext : public AsyncCall::Context { 54 napi_status status = napi_generic_failure; 55 bool isDisplayed = false; DisplayOptionalInputMethodContextDisplayOptionalInputMethodContext56 DisplayOptionalInputMethodContext() : Context(nullptr, nullptr){}; DisplayOptionalInputMethodContextDisplayOptionalInputMethodContext57 DisplayOptionalInputMethodContext(InputAction input, OutputAction output) 58 : Context(std::move(input), std::move(output)){}; 59 operatorDisplayOptionalInputMethodContext60 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 61 { 62 CHECK_RETURN(self != nullptr, "self is nullptr!", napi_invalid_arg); 63 return Context::operator()(env, argc, argv, self); 64 } operatorDisplayOptionalInputMethodContext65 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 75 struct GetInputMethodControllerContext : public AsyncCall::Context { 76 bool isStopInput = false; 77 napi_status status = napi_generic_failure; GetInputMethodControllerContextGetInputMethodControllerContext78 GetInputMethodControllerContext() : Context(nullptr, nullptr){}; GetInputMethodControllerContextGetInputMethodControllerContext79 GetInputMethodControllerContext(InputAction input, OutputAction output) 80 : Context(std::move(input), std::move(output)){}; 81 operatorGetInputMethodControllerContext82 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 83 { 84 CHECK_RETURN(self != nullptr, "self is nullptr!", napi_invalid_arg); 85 return Context::operator()(env, argc, argv, self); 86 } operatorGetInputMethodControllerContext87 napi_status operator()(napi_env env, napi_value *result) override 88 { 89 if (status != napi_ok) { 90 output_ = nullptr; 91 return status; 92 } 93 return Context::operator()(env, result); 94 } 95 }; 96 97 struct EnableInputContext : public AsyncCall::Context { 98 std::string bundleName; 99 std::string extName; 100 EnabledStatus enabledStatus{ EnabledStatus::DISABLED }; 101 napi_status status = napi_generic_failure; EnableInputContextEnableInputContext102 EnableInputContext() : Context(nullptr, nullptr){}; EnableInputContextEnableInputContext103 EnableInputContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; 104 operatorEnableInputContext105 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 106 { 107 CHECK_RETURN(self != nullptr, "self is nullptr!", napi_invalid_arg); 108 return Context::operator()(env, argc, argv, self); 109 } operatorEnableInputContext110 napi_status operator()(napi_env env, napi_value *result) override 111 { 112 if (status != napi_ok) { 113 output_ = nullptr; 114 return status; 115 } 116 return Context::operator()(env, result); 117 } 118 }; 119 120 struct GetInputMethodStateContext : public AsyncCall::Context { 121 napi_status status = napi_generic_failure; 122 EnabledStatus enableStatus = EnabledStatus::DISABLED; GetInputMethodStateContextGetInputMethodStateContext123 GetInputMethodStateContext() : Context(nullptr, nullptr){}; GetInputMethodStateContextGetInputMethodStateContext124 GetInputMethodStateContext(InputAction input, OutputAction output) 125 : Context(std::move(input), std::move(output)){}; operatorGetInputMethodStateContext126 napi_status operator()(napi_env env, napi_value *result) override 127 { 128 if (status != napi_ok) { 129 output_ = nullptr; 130 return status; 131 } 132 return Context::operator()(env, result); 133 } 134 }; 135 136 class JsGetInputMethodSetting : public ImeEventListener { 137 public: 138 JsGetInputMethodSetting() = default; 139 ~JsGetInputMethodSetting() = default; 140 static napi_value Init(napi_env env, napi_value exports); 141 static napi_value GetSetting(napi_env env, napi_callback_info info); 142 static napi_value GetInputMethodSetting(napi_env env, napi_callback_info info); 143 static napi_value ListInputMethod(napi_env env, napi_callback_info info); 144 static napi_value ListInputMethodSubtype(napi_env env, napi_callback_info info); 145 static napi_value ListCurrentInputMethodSubtype(napi_env env, napi_callback_info info); 146 static napi_value GetInputMethods(napi_env env, napi_callback_info info); 147 static napi_value GetInputMethodsSync(napi_env env, napi_callback_info info); 148 static napi_value GetAllInputMethods(napi_env env, napi_callback_info info); 149 static napi_value GetAllInputMethodsSync(napi_env env, napi_callback_info info); 150 static napi_value DisplayOptionalInputMethod(napi_env env, napi_callback_info info); 151 static napi_value ShowOptionalInputMethods(napi_env env, napi_callback_info info); 152 static napi_value IsPanelShown(napi_env env, napi_callback_info info); 153 static napi_value EnableInputMethod(napi_env env, napi_callback_info info); 154 static napi_value GetInputMethodState(napi_env env, napi_callback_info info); 155 static napi_value Subscribe(napi_env env, napi_callback_info info); 156 static napi_value UnSubscribe(napi_env env, napi_callback_info info); 157 static std::shared_ptr<JsGetInputMethodSetting> GetInputMethodSettingInstance(); 158 void OnImeChange(const Property &property, const SubProperty &subProperty) override; 159 void OnImeShow(const ImeWindowInfo &info) override; 160 void OnImeHide(const ImeWindowInfo &info) override; 161 162 private: 163 static napi_status GetInputMethodProperty(napi_env env, napi_value argv, std::shared_ptr<ListInputContext> ctxt); 164 static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); 165 static napi_value GetJsConstProperty(napi_env env, uint32_t num); 166 static napi_value GetIMSetting(napi_env env, napi_callback_info info, bool needThrowException); 167 static std::shared_ptr<AppExecFwk::EventHandler> GetEventHandler(); 168 int32_t RegisterListener(napi_value callback, std::string type, std::shared_ptr<JSCallbackObject> callbackObj); 169 void UnRegisterListener(napi_value callback, std::string type, bool &isUpdateFlag); 170 void OnPanelStatusChange(const std::string &type, const InputWindowInfo &info); 171 struct UvEntry { 172 std::vector<std::shared_ptr<JSCallbackObject>> vecCopy; 173 std::string type; 174 Property property; 175 SubProperty subProperty; 176 std::vector<InputWindowInfo> windowInfo; UvEntryUvEntry177 UvEntry(const std::vector<std::shared_ptr<JSCallbackObject>> &cbVec, const std::string &type) 178 : vecCopy(cbVec), type(type) 179 { 180 } 181 }; 182 using EntrySetter = std::function<void(UvEntry &)>; 183 std::shared_ptr<UvEntry> GetEntry(const std::string &type, EntrySetter entrySetter = nullptr); 184 static const std::string IMS_CLASS_NAME; 185 static thread_local napi_ref IMSRef_; 186 static std::mutex eventHandlerMutex_; 187 static std::shared_ptr<AppExecFwk::EventHandler> handler_; 188 std::recursive_mutex mutex_; 189 std::map<std::string, std::vector<std::shared_ptr<JSCallbackObject>>> jsCbMap_; 190 static std::mutex msMutex_; 191 static std::shared_ptr<JsGetInputMethodSetting> inputMethod_; 192 193 PanelFlag softKbShowingFlag_{ FLG_CANDIDATE_COLUMN }; 194 PanelFlag GetSoftKbShowingFlag(); 195 void SetSoftKbShowingFlag(PanelFlag flag); 196 }; 197 } // namespace MiscServices 198 } // namespace OHOS 199 #endif // INTERFACE_KITS_JS_GETINPUT_METHOD_SETTING_H