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 "async_call.h" 26 #include "global.h" 27 #include "input_method_engine_listener.h" 28 #include "input_method_property.h" 29 #include "js_callback_object.h" 30 #include "napi/native_api.h" 31 32 namespace OHOS { 33 namespace MiscServices { 34 class JsInputMethodEngineSetting : public InputMethodEngineListener { 35 public: 36 JsInputMethodEngineSetting() = default; 37 ~JsInputMethodEngineSetting() override = default; 38 static napi_value Init(napi_env env, napi_value info); 39 static napi_value GetInputMethodEngine(napi_env env, napi_callback_info info); 40 static napi_value GetInputMethodAbility(napi_env env, napi_callback_info info); 41 static napi_value Subscribe(napi_env env, napi_callback_info info); 42 static napi_value UnSubscribe(napi_env env, napi_callback_info info); 43 static napi_value MoveCursor(napi_env env, napi_callback_info info); 44 void OnInputStart() override; 45 void OnKeyboardStatus(bool isShow) override; 46 void OnInputStop(const std::string &imeId) override; 47 void OnSetCallingWindow(uint32_t windowId) override; 48 void OnSetSubtype(const SubProperty &property) override; 49 50 private: 51 static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); 52 static JsInputMethodEngineSetting *GetNative(napi_env env, napi_callback_info info); 53 static std::shared_ptr<JsInputMethodEngineSetting> GetInputMethodEngineSetting(); 54 static bool Equals(napi_env env, napi_value value, napi_ref copy, std::thread::id threadId); 55 static napi_value GetJsConstProperty(napi_env env, uint32_t num); 56 static napi_value GetIntJsConstProperty(napi_env env, int32_t num); 57 static napi_value GetIMEInstance(napi_env env, napi_callback_info info, int flag); 58 void RegisterListener(napi_value callback, std::string type, std::shared_ptr<JSCallbackObject> callbackObj); 59 void UnRegisterListener(napi_value callback, std::string type); 60 static napi_value GetResultOnSetSubtype(napi_env env, const SubProperty &property); 61 static std::string GetStringProperty(napi_env env, napi_value jsString); 62 static constexpr int32_t MAX_VALUE_LEN = 1024; 63 static const std::string IMES_CLASS_NAME; 64 static thread_local napi_ref IMESRef_; 65 struct UvEntry { 66 std::vector<std::shared_ptr<JSCallbackObject>> vecCopy; 67 std::string type; 68 std::string imeid; 69 uint32_t windowid = 0; 70 SubProperty subProperty; UvEntryUvEntry71 UvEntry(std::vector<std::shared_ptr<JSCallbackObject>> cbVec, std::string type) : vecCopy(cbVec), type(type) 72 { 73 } 74 }; 75 using EntrySetter = std::function<void(UvEntry &)>; 76 uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr); 77 uv_loop_s *loop_ = nullptr; 78 std::recursive_mutex mutex_; 79 std::map<std::string, std::vector<std::shared_ptr<JSCallbackObject>>> jsCbMap_; 80 static std::mutex engineMutex_; 81 static std::shared_ptr<JsInputMethodEngineSetting> inputMethodEngine_; 82 }; 83 } // namespace MiscServices 84 } // namespace OHOS 85 #endif // INTERFACE_KITS_JS_INPUT_METHOD_ENGINE_SETTING_H