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