• 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 #ifndef INTERFACE_KITS_JS_NAPI_INPUTMETHODENGINE_INCLUDE_JS_TEXT_INPUT_CLIENT_H
16 #define INTERFACE_KITS_JS_NAPI_INPUTMETHODENGINE_INCLUDE_JS_TEXT_INPUT_CLIENT_H
17 
18 #include "async_call.h"
19 #include "global.h"
20 #include "native_engine/native_engine.h"
21 #include "native_engine/native_value.h"
22 
23 namespace OHOS {
24 namespace MiscServices {
25 struct SendKeyFunctionContext : public AsyncCall::Context {
26     bool isSendKeyFunction = false;
27     int32_t action = 0;
28     napi_status status = napi_generic_failure;
SendKeyFunctionContextSendKeyFunctionContext29     SendKeyFunctionContext() : Context(nullptr, nullptr) { };
SendKeyFunctionContextSendKeyFunctionContext30     SendKeyFunctionContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { };
31 
operatorSendKeyFunctionContext32     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
33     {
34         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
35         return Context::operator()(env, argc, argv, self);
36     }
37 
operatorSendKeyFunctionContext38     napi_status operator()(napi_env env, napi_value *result) override
39     {
40         if (status != napi_ok) {
41             output_ = nullptr;
42             return status;
43         }
44         return Context::operator()(env, result);
45     }
46 };
47 
48 struct MoveCursorContext : public AsyncCall::Context {
49     int32_t num = 0;
50     napi_status status = napi_generic_failure;
MoveCursorContextMoveCursorContext51     MoveCursorContext() : Context(nullptr, nullptr) { };
MoveCursorContextMoveCursorContext52     MoveCursorContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { };
53 
operatorMoveCursorContext54     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
55     {
56         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
57         return Context::operator()(env, argc, argv, self);
58     }
operatorMoveCursorContext59     napi_status operator()(napi_env env, napi_value *result) override
60     {
61         if (status != napi_ok) {
62             output_ = nullptr;
63             return status;
64         }
65         return Context::operator()(env, result);
66     }
67 };
68 
69 struct DeleteForwardContext : public AsyncCall::Context {
70     bool isDeleteForward = false;
71     int32_t length = 0;
72     napi_status status = napi_generic_failure;
DeleteForwardContextDeleteForwardContext73     DeleteForwardContext() : Context(nullptr, nullptr) { };
DeleteForwardContextDeleteForwardContext74     DeleteForwardContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { };
75 
operatorDeleteForwardContext76     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
77     {
78         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
79         return Context::operator()(env, argc, argv, self);
80     }
operatorDeleteForwardContext81     napi_status operator()(napi_env env, napi_value *result) override
82     {
83         if (status != napi_ok) {
84             output_ = nullptr;
85             return status;
86         }
87         return Context::operator()(env, result);
88     }
89 };
90 
91 struct DeleteBackwardContext : public AsyncCall::Context {
92     bool isDeleteBackward = false;
93     int32_t length = 0;
94     napi_status status = napi_generic_failure;
DeleteBackwardContextDeleteBackwardContext95     DeleteBackwardContext() : Context(nullptr, nullptr) { };
DeleteBackwardContextDeleteBackwardContext96     DeleteBackwardContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { };
97 
operatorDeleteBackwardContext98     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
99     {
100         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
101         return Context::operator()(env, argc, argv, self);
102     }
operatorDeleteBackwardContext103     napi_status operator()(napi_env env, napi_value *result) override
104     {
105         if (status != napi_ok) {
106             output_ = nullptr;
107             return status;
108         }
109         return Context::operator()(env, result);
110     }
111 };
112 
113 struct InsertTextContext : public AsyncCall::Context {
114     bool isInsertText = false;
115     std::string text;
116     napi_status status = napi_generic_failure;
InsertTextContextInsertTextContext117     InsertTextContext() : Context(nullptr, nullptr) { };
InsertTextContextInsertTextContext118     InsertTextContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { };
119 
operatorInsertTextContext120     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
121     {
122         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
123         return Context::operator()(env, argc, argv, self);
124     }
operatorInsertTextContext125     napi_status operator()(napi_env env, napi_value *result) override
126     {
127         if (status != napi_ok) {
128             output_ = nullptr;
129             return status;
130         }
131         return Context::operator()(env, result);
132     }
133 };
134 
135 struct GetForwardContext : public AsyncCall::Context {
136     int32_t length = 0;
137     std::string text;
138     napi_status status = napi_generic_failure;
GetForwardContextGetForwardContext139     GetForwardContext() : Context(nullptr, nullptr) { };
GetForwardContextGetForwardContext140     GetForwardContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { };
141 
operatorGetForwardContext142     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
143     {
144         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
145         return Context::operator()(env, argc, argv, self);
146     }
operatorGetForwardContext147     napi_status operator()(napi_env env, napi_value *result) override
148     {
149         if (status != napi_ok) {
150             output_ = nullptr;
151             return status;
152         }
153         return Context::operator()(env, result);
154     }
155 };
156 
157 struct GetBackwardContext : public AsyncCall::Context {
158     int32_t length = 0;
159     std::string text;
160     napi_status status = napi_generic_failure;
GetBackwardContextGetBackwardContext161     GetBackwardContext() : Context(nullptr, nullptr) { };
GetBackwardContextGetBackwardContext162     GetBackwardContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { };
163 
operatorGetBackwardContext164     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
165     {
166         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
167         return Context::operator()(env, argc, argv, self);
168     }
operatorGetBackwardContext169     napi_status operator()(napi_env env, napi_value *result) override
170     {
171         if (status != napi_ok) {
172             output_ = nullptr;
173             return status;
174         }
175         return Context::operator()(env, result);
176     }
177 };
178 
179 struct GetEditorAttributeContext : public AsyncCall::Context {
180     int32_t inputPattern = 0;
181     int32_t enterKeyType = 0;
182     napi_status status = napi_generic_failure;
GetEditorAttributeContextGetEditorAttributeContext183     GetEditorAttributeContext() : Context(nullptr, nullptr) { };
GetEditorAttributeContextGetEditorAttributeContext184     GetEditorAttributeContext(InputAction input, OutputAction output)
185         : Context(std::move(input), std::move(output)) { };
186 
operatorGetEditorAttributeContext187     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
188     {
189         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
190         return Context::operator()(env, argc, argv, self);
191     }
operatorGetEditorAttributeContext192     napi_status operator()(napi_env env, napi_value *result) override
193     {
194         if (status != napi_ok) {
195             output_ = nullptr;
196             return status;
197         }
198         return Context::operator()(env, result);
199     }
200 };
201 
202 class JsTextInputClientEngine {
203 public:
204     JsTextInputClientEngine() = default;
205     ~JsTextInputClientEngine() = default;
206     static napi_value Init(napi_env env, napi_value info);
207     static napi_value SendKeyFunction(napi_env env, napi_callback_info info);
208     static napi_value DeleteForward(napi_env env, napi_callback_info info);
209     static napi_value DeleteBackward(napi_env env, napi_callback_info info);
210     static napi_value InsertText(napi_env env, napi_callback_info info);
211     static napi_value GetForward(napi_env env, napi_callback_info info);
212     static napi_value GetBackward(napi_env env, napi_callback_info info);
213     static napi_value MoveCursor(napi_env env, napi_callback_info info);
214     static napi_value GetEditorAttribute(napi_env env, napi_callback_info info);
215     static napi_value GetTextInputClientInstance(napi_env env);
216 private:
217     static napi_status GetAction(napi_env env, napi_value argv,
218         std::shared_ptr<SendKeyFunctionContext> ctxt);
219     static napi_status GetDeleteForwardLength(napi_env env, napi_value argv,
220         std::shared_ptr<DeleteForwardContext> ctxt);
221     static napi_status GetDeleteBackwardLength(napi_env env, napi_value argv,
222         std::shared_ptr<DeleteBackwardContext> ctxt);
223     static napi_status GetMoveCursorParam(napi_env env, napi_value argv,
224         std::shared_ptr<MoveCursorContext> ctxt);
225     static napi_status GetInsertText(napi_env env, napi_value argv,
226         std::shared_ptr<InsertTextContext> ctxt);
227     static napi_status GetForwardLength(napi_env env, napi_value argv,
228         std::shared_ptr<GetForwardContext> ctxt);
229     static napi_status GetBackwardLength(napi_env env, napi_value argv,
230         std::shared_ptr<GetBackwardContext> ctxt);
231 
232     static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo);
233     static int32_t GetNumberProperty(napi_env env, napi_value obj);
234     static std::string GetStringProperty(napi_env env, napi_value obj);
235     static napi_value GetResult(napi_env env, std::string &text);
236     static napi_value GetResultEditorAttribute(napi_env env,
237         std::shared_ptr<GetEditorAttributeContext> getEditorAttribute);
238 
239     static const std::string TIC_CLASS_NAME;
240     static thread_local napi_ref TICRef_;
241     static constexpr std::int32_t MAX_VALUE_LEN = 4096;
242 };
243 } // namespace MiscServices
244 } // namespace OHOS
245 #endif // INTERFACE_KITS_JS_NAPI_INPUTMETHODENGINE_INCLUDE_JS_TEXT_INPUT_CLIENT_H