• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 InitInputMethodSetting();
55     static bool Equals(napi_env env, napi_value value, napi_ref copy, std::thread::id threadId);
56     static napi_value GetJsConstProperty(napi_env env, uint32_t num);
57     static napi_value GetIntJsConstProperty(napi_env env, int32_t num);
58     static napi_value GetIMEInstance(napi_env env, napi_callback_info info);
59     void RegisterListener(napi_value callback, std::string type, std::shared_ptr<JSCallbackObject> callbackObj);
60     void UnRegisterListener(napi_value callback, std::string type);
61     static napi_value GetResultOnSetSubtype(napi_env env, const SubProperty &property);
62     static std::string GetStringProperty(napi_env env, napi_value jsString);
63     static constexpr int32_t MAX_VALUE_LEN = 1024;
64     static const std::string IMES_CLASS_NAME;
65     static thread_local napi_ref IMESRef_;
66     struct UvEntry {
67         std::vector<std::shared_ptr<JSCallbackObject>> vecCopy;
68         std::string type;
69         std::string imeid;
70         uint32_t windowid = 0;
71         SubProperty subProperty;
UvEntryUvEntry72         UvEntry(std::vector<std::shared_ptr<JSCallbackObject>> cbVec, std::string type) : vecCopy(cbVec), type(type)
73         {
74         }
75     };
76     using EntrySetter = std::function<void(UvEntry &)>;
77     uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr);
78     uv_loop_s *loop_ = nullptr;
79     std::recursive_mutex mutex_;
80     std::map<std::string, std::vector<std::shared_ptr<JSCallbackObject>>> jsCbMap_;
81     static std::mutex engineMutex_;
82     static std::shared_ptr<JsInputMethodEngineSetting> inputMethodEngine_;
83 };
84 } // namespace MiscServices
85 } // namespace OHOS
86 #endif // INTERFACE_KITS_JS_INPUT_METHOD_ENGINE_SETTING_H