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 "global.h" 22 #include "input_method_controller.h" 23 #include "input_method_status.h" 24 #include "js_callback_object.h" 25 26 namespace OHOS { 27 namespace MiscServices { 28 struct ListInputContext : public AsyncCall::Context { 29 InputMethodStatus inputMethodStatus = InputMethodStatus::ALL; 30 std::vector<Property> properties; 31 std::vector<SubProperty> subProperties; 32 Property property; 33 napi_status status = napi_generic_failure; ListInputContextListInputContext34 ListInputContext() : Context(nullptr, nullptr) { }; ListInputContextListInputContext35 ListInputContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { }; 36 operatorListInputContext37 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 38 { 39 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 40 return Context::operator()(env, argc, argv, self); 41 } operatorListInputContext42 napi_status operator()(napi_env env, napi_value *result) override 43 { 44 if (status != napi_ok) { 45 output_ = nullptr; 46 return status; 47 } 48 return Context::operator()(env, result); 49 } 50 }; 51 52 struct DisplayOptionalInputMethodContext : public AsyncCall::Context { 53 napi_status status = napi_generic_failure; 54 bool isDisplayed = false; DisplayOptionalInputMethodContextDisplayOptionalInputMethodContext55 DisplayOptionalInputMethodContext() : Context(nullptr, nullptr) { }; DisplayOptionalInputMethodContextDisplayOptionalInputMethodContext56 DisplayOptionalInputMethodContext(InputAction input, OutputAction output) 57 : Context(std::move(input), std::move(output)) { }; 58 operatorDisplayOptionalInputMethodContext59 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 60 { 61 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 62 return Context::operator()(env, argc, argv, self); 63 } operatorDisplayOptionalInputMethodContext64 napi_status operator()(napi_env env, napi_value *result) override 65 { 66 if (status != napi_ok) { 67 output_ = nullptr; 68 return status; 69 } 70 return Context::operator()(env, result); 71 } 72 }; 73 74 struct GetInputMethodControllerContext : public AsyncCall::Context { 75 bool isStopInput = false; 76 napi_status status = napi_generic_failure; GetInputMethodControllerContextGetInputMethodControllerContext77 GetInputMethodControllerContext() : Context(nullptr, nullptr) { }; GetInputMethodControllerContextGetInputMethodControllerContext78 GetInputMethodControllerContext(InputAction input, OutputAction output) 79 : Context(std::move(input), std::move(output)) { }; 80 operatorGetInputMethodControllerContext81 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 82 { 83 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 84 return Context::operator()(env, argc, argv, self); 85 } operatorGetInputMethodControllerContext86 napi_status operator()(napi_env env, napi_value *result) override 87 { 88 if (status != napi_ok) { 89 output_ = nullptr; 90 return status; 91 } 92 return Context::operator()(env, result); 93 } 94 }; 95 96 class JsGetInputMethodSetting : public InputMethodSettingListener { 97 public: 98 JsGetInputMethodSetting() = default; 99 ~JsGetInputMethodSetting() = default; 100 static napi_value Init(napi_env env, napi_value info); 101 static napi_value GetSetting(napi_env env, napi_callback_info info); 102 static napi_value GetInputMethodSetting(napi_env env, napi_callback_info info); 103 static napi_value ListInputMethod(napi_env env, napi_callback_info info); 104 static napi_value ListInputMethodSubtype(napi_env env, napi_callback_info info); 105 static napi_value ListCurrentInputMethodSubtype(napi_env env, napi_callback_info info); 106 static napi_value GetInputMethods(napi_env env, napi_callback_info info); 107 static napi_value DisplayOptionalInputMethod(napi_env env, napi_callback_info info); 108 static napi_value ShowOptionalInputMethods(napi_env env, napi_callback_info info); 109 static napi_value Subscribe(napi_env env, napi_callback_info info); 110 static napi_value UnSubscribe(napi_env env, napi_callback_info info); 111 static std::shared_ptr<JsGetInputMethodSetting> GetInputMethodSettingInstance(); 112 void OnImeChange(const Property &property, const SubProperty &subProperty) override; 113 114 private: 115 static napi_status GetInputMethodProperty(napi_env env, napi_value argv, std::shared_ptr<ListInputContext> ctxt); 116 static JsGetInputMethodSetting *GetNative(napi_env env, napi_callback_info info); 117 static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); 118 static napi_value GetJsConstProperty(napi_env env, uint32_t num); 119 static napi_value GetIMSetting(napi_env env, napi_callback_info info, bool needThrowException); 120 static bool Equals(napi_env env, napi_value value, napi_ref copy, std::thread::id threadId); 121 void RegisterListener(napi_value callback, std::string type, std::shared_ptr<JSCallbackObject> callbackObj); 122 void UnRegisterListener(napi_value callback, std::string type); 123 struct UvEntry { 124 std::vector<std::shared_ptr<JSCallbackObject>> vecCopy; 125 std::string type; 126 Property property; 127 SubProperty subProperty; UvEntryUvEntry128 UvEntry(std::vector<std::shared_ptr<JSCallbackObject>> cbVec, std::string type) : vecCopy(cbVec), type(type) 129 { 130 } 131 }; 132 using EntrySetter = std::function<void(UvEntry &)>; 133 uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr); 134 static const std::string IMS_CLASS_NAME; 135 static thread_local napi_ref IMSRef_; 136 uv_loop_s *loop_ = nullptr; 137 std::recursive_mutex mutex_; 138 std::map<std::string, std::vector<std::shared_ptr<JSCallbackObject>>> jsCbMap_; 139 static std::mutex msMutex_; 140 static std::shared_ptr<JsGetInputMethodSetting> inputMethod_; 141 }; 142 } // namespace MiscServices 143 } // namespace OHOS 144 #endif // INTERFACE_KITS_JS_GETINPUT_METHOD_SETTING_H 145