• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "js_panel.h"
17 
18 #include "event_checker.h"
19 #include "input_method_ability.h"
20 #include "js_util.h"
21 #include "js_utils.h"
22 #include "napi/native_common.h"
23 #include "panel_listener_impl.h"
24 
25 namespace OHOS {
26 namespace MiscServices {
27 using WMError = OHOS::Rosen::WMError;
28 const std::string JsPanel::CLASS_NAME = "Panel";
29 thread_local napi_ref JsPanel::panelConstructorRef_ = nullptr;
30 std::mutex JsPanel::panelConstructorMutex_;
31 
Init(napi_env env)32 napi_value JsPanel::Init(napi_env env)
33 {
34     IMSA_HILOGI("JsPanel in.");
35     napi_value constructor = nullptr;
36     std::lock_guard<std::mutex> lock(panelConstructorMutex_);
37     if (panelConstructorRef_ != nullptr) {
38         napi_status status = napi_get_reference_value(env, panelConstructorRef_, &constructor);
39         CHECK_RETURN(status == napi_ok, "Failed to get jsPanel constructor.", nullptr);
40         return constructor;
41     }
42     const napi_property_descriptor properties[] = {
43         DECLARE_NAPI_FUNCTION("setUiContent", SetUiContent),
44         DECLARE_NAPI_FUNCTION("resize", Resize),
45         DECLARE_NAPI_FUNCTION("moveTo", MoveTo),
46         DECLARE_NAPI_FUNCTION("show", Show),
47         DECLARE_NAPI_FUNCTION("hide", Hide),
48         DECLARE_NAPI_FUNCTION("changeFlag", ChangeFlag),
49         DECLARE_NAPI_FUNCTION("setPrivacyMode", SetPrivacyMode),
50         DECLARE_NAPI_FUNCTION("on", Subscribe),
51         DECLARE_NAPI_FUNCTION("off", UnSubscribe),
52     };
53     NAPI_CALL(env, napi_define_class(env, CLASS_NAME.c_str(), CLASS_NAME.size(), JsNew, nullptr,
54                        sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor));
55     CHECK_RETURN(constructor != nullptr, "napi_define_class failed!", nullptr);
56     NAPI_CALL(env, napi_create_reference(env, constructor, 1, &panelConstructorRef_));
57     return constructor;
58 }
59 
JsNew(napi_env env,napi_callback_info info)60 napi_value JsPanel::JsNew(napi_env env, napi_callback_info info)
61 {
62     IMSA_HILOGD("JsPanel, create panel instance in.");
63     JsPanel *panel = new (std::nothrow) JsPanel();
64     CHECK_RETURN(panel != nullptr, "no memory for JsPanel", nullptr);
65     auto finalize = [](napi_env env, void *data, void *hint) {
66         IMSA_HILOGD("jsPanel finalize.");
67         auto *jsPanel = reinterpret_cast<JsPanel *>(data);
68         CHECK_RETURN_VOID(jsPanel != nullptr, "finalize null!");
69         jsPanel->GetNative() = nullptr;
70         delete jsPanel;
71     };
72     napi_value thisVar = nullptr;
73     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
74     napi_status status = napi_wrap(env, thisVar, panel, finalize, nullptr, nullptr);
75     if (status != napi_ok) {
76         IMSA_HILOGE("JsPanel napi_wrap failed: %{public}d", status);
77         delete panel;
78         return nullptr;
79     }
80     return thisVar;
81 }
82 
~JsPanel()83 JsPanel::~JsPanel()
84 {
85     inputMethodPanel_ = nullptr;
86 }
87 
SetNative(const std::shared_ptr<InputMethodPanel> & panel)88 void JsPanel::SetNative(const std::shared_ptr<InputMethodPanel> &panel)
89 {
90     inputMethodPanel_ = panel;
91 }
92 
GetNative()93 std::shared_ptr<InputMethodPanel> JsPanel::GetNative()
94 {
95     return inputMethodPanel_;
96 }
97 
SetUiContent(napi_env env,napi_callback_info info)98 napi_value JsPanel::SetUiContent(napi_env env, napi_callback_info info)
99 {
100     IMSA_HILOGI("JsPanel in.");
101     auto ctxt = std::make_shared<PanelContentContext>(env, info);
102     auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
103         napi_status status = napi_generic_failure;
104         PARAM_CHECK_RETURN(env, argc >= 1, "should 1 or 2 parameters!", TYPE_NONE, status);
105         // 0 means the first param path<std::string>
106         status = JsUtils::GetValue(env, argv[0], ctxt->path);
107         CHECK_RETURN(status == napi_ok, "get path failed!", status);
108         // if type of argv[1] is object, we will get value of 'storage' from it.
109         if (argc >= 2) {
110             napi_valuetype valueType = napi_undefined;
111             status = napi_typeof(env, argv[1], &valueType);
112             CHECK_RETURN(status == napi_ok, "get valueType failed!", status);
113             if (valueType == napi_object) {
114                 napi_ref storage = nullptr;
115                 napi_create_reference(env, argv[1], 1, &storage);
116                 auto contentStorage = (storage == nullptr)
117                                           ? nullptr
118                                           : std::shared_ptr<NativeReference>(
119                                               reinterpret_cast<NativeReference *>(storage));
120                 ctxt->contentStorage = contentStorage;
121             }
122         }
123         return napi_ok;
124     };
125 
126     auto exec = [ctxt](AsyncCall::Context *ctx) { ctxt->SetState(napi_ok); };
127     auto output = [ctxt](napi_env env, napi_value *result) -> napi_status {
128         CHECK_RETURN(ctxt->inputMethodPanel != nullptr, "inputMethodPanel is nullptr!", napi_generic_failure);
129         auto code = ctxt->inputMethodPanel->SetUiContent(
130             ctxt->path, env, ctxt->contentStorage);
131         CHECK_RETURN(code == ErrorCode::NO_ERROR, "SetUiContent failed!", napi_generic_failure);
132         return napi_ok;
133     };
134     ctxt->SetAction(std::move(input), std::move(output));
135     // 3 means JsAPI:setUiContent has 3 params at most.
136     AsyncCall asyncCall(env, info, ctxt, 3);
137     return asyncCall.Call(env, exec, "setUiContent");
138 }
139 
Resize(napi_env env,napi_callback_info info)140 napi_value JsPanel::Resize(napi_env env, napi_callback_info info)
141 {
142     auto ctxt = std::make_shared<PanelContentContext>(env, info);
143     auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
144         napi_status status = napi_generic_failure;
145         PARAM_CHECK_RETURN(env, argc > 1, "should 2 or 3 parameters!", TYPE_NONE, status);
146         // 0 means the first param width<uint32_t>
147         status = JsUtils::GetValue(env, argv[0], ctxt->width);
148         CHECK_RETURN(status == napi_ok, "get width failed!", status);
149         // 1 means the second param height<uint32_t>
150         status = JsUtils::GetValue(env, argv[1], ctxt->height);
151         CHECK_RETURN(status == napi_ok, "get height failed!", status);
152         return napi_ok;
153     };
154 
155     auto exec = [ctxt](AsyncCall::Context *ctx) {
156         CHECK_RETURN_VOID(ctxt->inputMethodPanel != nullptr, "inputMethodPanel_ is nullptr.");
157         auto code = ctxt->inputMethodPanel->Resize(ctxt->width, ctxt->height);
158         if (code == ErrorCode::NO_ERROR) {
159             InputMethodAbility::GetInstance()->NotifyKeyboardHeight(ctxt->inputMethodPanel);
160             ctxt->SetState(napi_ok);
161             return;
162         }
163         ctxt->SetErrorCode(code);
164     };
165     ctxt->SetAction(std::move(input));
166     // 3 means JsAPI:resize has 3 params at most.
167     AsyncCall asyncCall(env, info, ctxt, 3);
168     return asyncCall.Call(env, exec, "resize");
169 }
170 
MoveTo(napi_env env,napi_callback_info info)171 napi_value JsPanel::MoveTo(napi_env env, napi_callback_info info)
172 {
173     auto ctxt = std::make_shared<PanelContentContext>(env, info);
174     auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
175         napi_status status = napi_generic_failure;
176         PARAM_CHECK_RETURN(env, argc > 1, " should 2 or 3 parameters! ", TYPE_NONE, status);
177         // 0 means the first param x<int32_t>
178         status = JsUtils::GetValue(env, argv[0], ctxt->x);
179         CHECK_RETURN(status == napi_ok, "get x failed!", status);
180         // 1 means the second param y<int32_t>
181         status = JsUtils::GetValue(env, argv[1], ctxt->y);
182         CHECK_RETURN(status == napi_ok, "get y failed!", status);
183         return napi_ok;
184     };
185 
186     auto exec = [ctxt](AsyncCall::Context *ctx) {
187         CHECK_RETURN_VOID(ctxt->inputMethodPanel != nullptr, "inputMethodPanel_ is nullptr.");
188         auto code = ctxt->inputMethodPanel->MoveTo(ctxt->x, ctxt->y);
189         if (code == ErrorCode::NO_ERROR) {
190             ctxt->SetState(napi_ok);
191             return;
192         }
193         ctxt->SetErrorCode(code);
194     };
195     ctxt->SetAction(std::move(input));
196     // 3 means JsAPI:moveTo has 3 params at most.
197     AsyncCall asyncCall(env, info, ctxt, 3);
198     return asyncCall.Call(env, exec, "moveTo");
199 }
200 
Show(napi_env env,napi_callback_info info)201 napi_value JsPanel::Show(napi_env env, napi_callback_info info)
202 {
203     auto ctxt = std::make_shared<PanelContentContext>(env, info);
204     auto exec = [ctxt](AsyncCall::Context *ctx) {
205         CHECK_RETURN_VOID(ctxt->inputMethodPanel != nullptr, "inputMethodPanel_ is nullptr.");
206         auto code = InputMethodAbility::GetInstance()->ShowPanel(ctxt->inputMethodPanel);
207         if (code == ErrorCode::NO_ERROR) {
208             ctxt->SetState(napi_ok);
209             return;
210         }
211         ctxt->SetErrorCode(code);
212     };
213     // 1 means JsAPI:show has 1 params at most.
214     AsyncCall asyncCall(env, info, ctxt, 1);
215     return asyncCall.Call(env, exec, "show");
216 }
217 
Hide(napi_env env,napi_callback_info info)218 napi_value JsPanel::Hide(napi_env env, napi_callback_info info)
219 {
220     auto ctxt = std::make_shared<PanelContentContext>(env, info);
221     auto exec = [ctxt](AsyncCall::Context *ctx) {
222         CHECK_RETURN_VOID(ctxt->inputMethodPanel != nullptr, "inputMethodPanel_ is nullptr.");
223         auto code = InputMethodAbility::GetInstance()->HidePanel(ctxt->inputMethodPanel);
224         if (code == ErrorCode::NO_ERROR) {
225             ctxt->SetState(napi_ok);
226             return;
227         }
228         ctxt->SetErrorCode(code);
229     };
230     // 1 means JsAPI:hide has 1 params at most.
231     AsyncCall asyncCall(env, info, ctxt, 1);
232     return asyncCall.Call(env, exec, "panel.hide");
233 }
234 
ChangeFlag(napi_env env,napi_callback_info info)235 napi_value JsPanel::ChangeFlag(napi_env env, napi_callback_info info)
236 {
237     size_t argc = ARGC_MAX;
238     napi_value argv[ARGC_MAX] = { nullptr };
239     napi_value thisVar = nullptr;
240     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
241     PARAM_CHECK_RETURN(env, argc > 0, " should 1 parameter! ", TYPE_NONE, nullptr);
242     int32_t panelFlag = 0;
243     // 0 means the first param flag<PanelFlag>
244     napi_status status = JsUtils::GetValue(env, argv[0], panelFlag);
245     CHECK_RETURN(status == napi_ok, "get panelFlag failed!", nullptr);
246     auto inputMethodPanel = UnwrapPanel(env, thisVar);
247     auto ret = inputMethodPanel->ChangePanelFlag(PanelFlag(panelFlag));
248     CHECK_RETURN(ret == ErrorCode::NO_ERROR, "ChangePanelFlag failed!", nullptr);
249     InputMethodAbility::GetInstance()->NotifyKeyboardHeight(inputMethodPanel);
250     return nullptr;
251 }
252 
SetPrivacyMode(napi_env env,napi_callback_info info)253 napi_value JsPanel::SetPrivacyMode(napi_env env, napi_callback_info info)
254 {
255     size_t argc = ARGC_MAX;
256     napi_value argv[ARGC_MAX] = {nullptr};
257     napi_value thisVar = nullptr;
258     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
259     PARAM_CHECK_RETURN(env, argc > 0, " should 1 parameter! ", TYPE_NONE, nullptr);
260     bool isPrivacyMode = false;
261     // 0 means the first param isPrivacyMode<boolean>
262     napi_status status = JsUtils::GetValue(env, argv[0], isPrivacyMode);
263     if (status != napi_ok) {
264         JsUtils::ThrowException(env, JsUtils::Convert(ErrorCode::ERROR_PARAMETER_CHECK_FAILED), " param check failed!",
265                                 TYPE_BOOLEAN);
266     }
267     CHECK_RETURN(status == napi_ok, "get isPrivacyMode failed!", nullptr);
268     auto inputMethodPanel = UnwrapPanel(env, thisVar);
269     auto ret = inputMethodPanel->SetPrivacyMode(isPrivacyMode);
270     if (ret == static_cast<int32_t>(WMError::WM_ERROR_INVALID_PERMISSION)) {
271         JsUtils::ThrowException(env, JsUtils::Convert(ErrorCode::ERROR_STATUS_PERMISSION_DENIED),
272                                 " ohos.permission.PRIVACY_WINDOW permission denied", TYPE_NONE);
273     }
274     CHECK_RETURN(ret == ErrorCode::NO_ERROR, "SetPrivacyMode failed!", nullptr);
275     return nullptr;
276 }
277 
Subscribe(napi_env env,napi_callback_info info)278 napi_value JsPanel::Subscribe(napi_env env, napi_callback_info info)
279 {
280     IMSA_HILOGD("JsPanel in");
281     size_t argc = ARGC_MAX;
282     napi_value argv[ARGC_MAX] = { nullptr };
283     napi_value thisVar = nullptr;
284     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
285     std::string type;
286     // 2 means least param num.
287     if (argc < 2 || !JsUtil::GetValue(env, argv[0], type)
288         || !EventChecker::IsValidEventType(EventSubscribeModule::PANEL, type)
289         || JsUtil::GetType(env, argv[1]) != napi_function) {
290         IMSA_HILOGE("Subscribe failed, type:%{public}s", type.c_str());
291         return nullptr;
292     }
293     IMSA_HILOGD("Subscribe type:%{public}s", type.c_str());
294     std::shared_ptr<PanelListenerImpl> observer = PanelListenerImpl::GetInstance();
295     auto inputMethodPanel = UnwrapPanel(env, thisVar);
296     // 1 means the second param callback.
297     observer->SaveInfo(env, type, argv[1], inputMethodPanel->windowId_);
298     inputMethodPanel->SetPanelStatusListener(observer, type);
299     napi_value result = nullptr;
300     napi_get_undefined(env, &result);
301     return result;
302 }
303 
UnSubscribe(napi_env env,napi_callback_info info)304 napi_value JsPanel::UnSubscribe(napi_env env, napi_callback_info info)
305 {
306     size_t argc = ARGC_MAX;
307     napi_value argv[ARGC_MAX] = { nullptr };
308     napi_value thisVar = nullptr;
309     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
310     std::string type;
311     // 1 means least param num.
312     if (argc < 1 || !JsUtil::GetValue(env, argv[0], type)
313         || !EventChecker::IsValidEventType(EventSubscribeModule::PANEL, type)) {
314         IMSA_HILOGE("UnSubscribe failed, type:%{public}s", type.c_str());
315         JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, "please check the params", TYPE_NONE);
316         return nullptr;
317     }
318 
319     // if the second param is not napi_function/napi_null/napi_undefined, return
320     auto paramType = JsUtil::GetType(env, argv[1]);
321     if (paramType != napi_function && paramType != napi_null && paramType != napi_undefined) {
322         JsUtils::ThrowException(env, IMFErrorCode::EXCEPTION_PARAMCHECK, "please check the params", TYPE_FUNCTION);
323         return nullptr;
324     }
325     // if the second param is napi_function, delete it, else delete all
326     argv[1] = paramType == napi_function ? argv[1] : nullptr;
327 
328     IMSA_HILOGD("UnSubscribe type:%{public}s", type.c_str());
329     std::shared_ptr<PanelListenerImpl> observer = PanelListenerImpl::GetInstance();
330     auto inputMethodPanel = UnwrapPanel(env, thisVar);
331     observer->RemoveInfo(type, inputMethodPanel->windowId_);
332     inputMethodPanel->ClearPanelListener(type);
333     napi_value result = nullptr;
334     napi_get_null(env, &result);
335     return result;
336 }
337 
UnwrapPanel(napi_env env,napi_value thisVar)338 std::shared_ptr<InputMethodPanel> JsPanel::UnwrapPanel(napi_env env, napi_value thisVar)
339 {
340     void *native = nullptr;
341     napi_status status = napi_unwrap(env, thisVar, &native);
342     CHECK_RETURN((status == napi_ok && native != nullptr), "napi_unwrap failed!", nullptr);
343     auto jsPanel = reinterpret_cast<JsPanel *>(native);
344     if (jsPanel == nullptr) {
345         return nullptr;
346     }
347     auto inputMethodPanel = jsPanel->GetNative();
348     CHECK_RETURN(inputMethodPanel != nullptr, "inputMethodPanel is nullptr", nullptr);
349     return inputMethodPanel;
350 }
351 } // namespace MiscServices
352 } // namespace OHOS