• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_window.h"
17 #include <new>
18 
19 #ifndef WINDOW_PREVIEW
20 #include "js_transition_controller.h"
21 #else
22 #include "mock/js_transition_controller.h"
23 #endif
24 
25 #include "js_err_utils.h"
26 #include "js_window_utils.h"
27 #include "window.h"
28 #include "window_helper.h"
29 #include "window_manager_hilog.h"
30 #include "window_option.h"
31 #include "wm_math.h"
32 #include "pixel_map.h"
33 #include "pixel_map_napi.h"
34 #include "napi_remote_object.h"
35 #include "permission.h"
36 #include "request_info.h"
37 #include "ui_content.h"
38 
39 namespace OHOS {
40 namespace Rosen {
41 using namespace AbilityRuntime;
42 namespace {
43 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "JsWindow"};
44 constexpr Rect g_emptyRect = {0, 0, 0, 0};
45 constexpr int32_t MIN_DECOR_HEIGHT = 37;
46 constexpr int32_t MAX_DECOR_HEIGHT = 112;
47 constexpr size_t INDEX_ZERO = 0;
48 constexpr size_t INDEX_ONE = 1;
49 constexpr size_t INDEX_TWO = 2;
50 constexpr size_t FOUR_PARAMS_SIZE = 4;
51 constexpr double MIN_GRAY_SCALE = 0.0;
52 constexpr double MAX_GRAY_SCALE = 1.0;
53 constexpr uint32_t DEFAULT_WINDOW_MAX_WIDTH = 3840;
54 }
55 
56 static thread_local std::map<std::string, std::shared_ptr<NativeReference>> g_jsWindowMap;
57 static std::mutex g_mutex;
58 static int g_ctorCnt = 0;
59 static int g_dtorCnt = 0;
60 static int g_finalizerCnt = 0;
JsWindow(const sptr<Window> & window)61 JsWindow::JsWindow(const sptr<Window>& window)
62     : windowToken_(window), registerManager_(std::make_unique<JsWindowRegisterManager>())
63 {
64     NotifyNativeWinDestroyFunc func = [this](const std::string& windowName) {
65         {
66             std::lock_guard<std::mutex> lock(g_mutex);
67             if (windowName.empty() || g_jsWindowMap.count(windowName) == 0) {
68                 WLOGFE("Can not find window %{public}s ", windowName.c_str());
69                 return;
70             }
71             g_jsWindowMap.erase(windowName);
72         }
73         windowToken_ = nullptr;
74         WLOGI("Destroy window %{public}s in js window", windowName.c_str());
75     };
76     windowToken_->RegisterWindowDestroyedListener(func);
77     WLOGI(" constructorCnt: %{public}d", ++g_ctorCnt);
78 }
79 
~JsWindow()80 JsWindow::~JsWindow()
81 {
82     WLOGI(" deConstructorCnt:%{public}d", ++g_dtorCnt);
83     if (windowToken_ != nullptr) {
84         windowToken_->UnregisterWindowDestroyedListener();
85     }
86     windowToken_ = nullptr;
87 }
88 
GetWindowName()89 std::string JsWindow::GetWindowName()
90 {
91     if (windowToken_ == nullptr) {
92         return "";
93     }
94     return windowToken_->GetWindowName();
95 }
96 
Finalizer(napi_env env,void * data,void * hint)97 void JsWindow::Finalizer(napi_env env, void* data, void* hint)
98 {
99     WLOGI("g_finalizerCnt:%{public}d", ++g_finalizerCnt);
100     auto jsWin = std::unique_ptr<JsWindow>(static_cast<JsWindow*>(data));
101     if (jsWin == nullptr) {
102         WLOGFE("jsWin is nullptr");
103         return;
104     }
105     std::string windowName = jsWin->GetWindowName();
106     std::lock_guard<std::mutex> lock(g_mutex);
107     g_jsWindowMap.erase(windowName);
108     WLOGI("Remove window %{public}s from g_jsWindowMap", windowName.c_str());
109 }
110 
Show(napi_env env,napi_callback_info info)111 napi_value JsWindow::Show(napi_env env, napi_callback_info info)
112 {
113     WLOGI("Show");
114     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
115     return (me != nullptr) ? me->OnShow(env, info) : nullptr;
116 }
117 
ShowWindow(napi_env env,napi_callback_info info)118 napi_value JsWindow::ShowWindow(napi_env env, napi_callback_info info)
119 {
120     WLOGI("Show");
121     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
122     return (me != nullptr) ? me->OnShowWindow(env, info) : nullptr;
123 }
124 
ShowWithAnimation(napi_env env,napi_callback_info info)125 napi_value JsWindow::ShowWithAnimation(napi_env env, napi_callback_info info)
126 {
127     WLOGI("ShowWithAnimation");
128     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
129     return (me != nullptr) ? me->OnShowWithAnimation(env, info) : nullptr;
130 }
131 
Destroy(napi_env env,napi_callback_info info)132 napi_value JsWindow::Destroy(napi_env env, napi_callback_info info)
133 {
134     WLOGI("Destroy");
135     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
136     return (me != nullptr) ? me->OnDestroy(env, info) : nullptr;
137 }
138 
DestroyWindow(napi_env env,napi_callback_info info)139 napi_value JsWindow::DestroyWindow(napi_env env, napi_callback_info info)
140 {
141     WLOGI("Destroy");
142     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
143     return (me != nullptr) ? me->OnDestroyWindow(env, info) : nullptr;
144 }
145 
Hide(napi_env env,napi_callback_info info)146 napi_value JsWindow::Hide(napi_env env, napi_callback_info info)
147 {
148     WLOGD("Hide");
149     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
150     return (me != nullptr) ? me->OnHide(env, info) : nullptr;
151 }
152 
HideWithAnimation(napi_env env,napi_callback_info info)153 napi_value JsWindow::HideWithAnimation(napi_env env, napi_callback_info info)
154 {
155     WLOGI("HideWithAnimation");
156     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
157     return (me != nullptr) ? me->OnHideWithAnimation(env, info) : nullptr;
158 }
159 
Recover(napi_env env,napi_callback_info info)160 napi_value JsWindow::Recover(napi_env env, napi_callback_info info)
161 {
162     WLOGI("Recover");
163     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
164     return (me != nullptr) ? me->OnRecover(env, info) : nullptr;
165 }
166 
MoveTo(napi_env env,napi_callback_info info)167 napi_value JsWindow::MoveTo(napi_env env, napi_callback_info info)
168 {
169     WLOGD("MoveTo");
170     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
171     return (me != nullptr) ? me->OnMoveTo(env, info) : nullptr;
172 }
173 
MoveWindowTo(napi_env env,napi_callback_info info)174 napi_value JsWindow::MoveWindowTo(napi_env env, napi_callback_info info)
175 {
176     WLOGD("MoveTo");
177     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
178     return (me != nullptr) ? me->OnMoveWindowTo(env, info) : nullptr;
179 }
180 
MoveWindowToAsync(napi_env env,napi_callback_info info)181 napi_value JsWindow::MoveWindowToAsync(napi_env env, napi_callback_info info)
182 {
183     TLOGI(WmsLogTag::WMS_LAYOUT, "MoveToAsync");
184     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
185     return (me != nullptr) ? me->OnMoveWindowToAsync(env, info) : nullptr;
186 }
187 
188 /** @note @window.layout */
MoveWindowToGlobal(napi_env env,napi_callback_info info)189 napi_value JsWindow::MoveWindowToGlobal(napi_env env, napi_callback_info info)
190 {
191     TLOGI(WmsLogTag::WMS_LAYOUT, "MoveWindowToGlobal");
192     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
193     return (me != nullptr) ? me->OnMoveWindowToGlobal(env, info) : nullptr;
194 }
195 
196 /** @note @window.layout */
GetGlobalScaledRect(napi_env env,napi_callback_info info)197 napi_value JsWindow::GetGlobalScaledRect(napi_env env, napi_callback_info info)
198 {
199     TLOGD(WmsLogTag::WMS_LAYOUT, "[NAPI]");
200     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
201     return (me != nullptr) ? me->OnGetGlobalScaledRect(env, info) : nullptr;
202 }
203 
Resize(napi_env env,napi_callback_info info)204 napi_value JsWindow::Resize(napi_env env, napi_callback_info info)
205 {
206     WLOGD("Resize");
207     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
208     return (me != nullptr) ? me->OnResize(env, info) : nullptr;
209 }
210 
ResizeWindow(napi_env env,napi_callback_info info)211 napi_value JsWindow::ResizeWindow(napi_env env, napi_callback_info info)
212 {
213     WLOGI("Resize");
214     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
215     return (me != nullptr) ? me->OnResizeWindow(env, info) : nullptr;
216 }
217 
ResizeWindowAsync(napi_env env,napi_callback_info info)218 napi_value JsWindow::ResizeWindowAsync(napi_env env, napi_callback_info info)
219 {
220     TLOGI(WmsLogTag::WMS_LAYOUT, "ResizeAsync");
221     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
222     return (me != nullptr) ? me->OnResizeWindowAsync(env, info) : nullptr;
223 }
224 
SetWindowType(napi_env env,napi_callback_info info)225 napi_value JsWindow::SetWindowType(napi_env env, napi_callback_info info)
226 {
227     WLOGI("SetWindowType");
228     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
229     return (me != nullptr) ? me->OnSetWindowType(env, info) : nullptr;
230 }
231 
SetWindowMode(napi_env env,napi_callback_info info)232 napi_value JsWindow::SetWindowMode(napi_env env, napi_callback_info info)
233 {
234     WLOGI("SetWindowMode");
235     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
236     return (me != nullptr) ? me->OnSetWindowMode(env, info) : nullptr;
237 }
238 
GetProperties(napi_env env,napi_callback_info info)239 napi_value JsWindow::GetProperties(napi_env env, napi_callback_info info)
240 {
241     WLOGD("GetProperties");
242     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
243     return (me != nullptr) ? me->OnGetProperties(env, info) : nullptr;
244 }
245 
GetWindowPropertiesSync(napi_env env,napi_callback_info info)246 napi_value JsWindow::GetWindowPropertiesSync(napi_env env, napi_callback_info info)
247 {
248     WLOGD("GetProperties");
249     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
250     return (me != nullptr) ? me->OnGetWindowPropertiesSync(env, info) : nullptr;
251 }
252 
RegisterWindowCallback(napi_env env,napi_callback_info info)253 napi_value JsWindow::RegisterWindowCallback(napi_env env, napi_callback_info info)
254 {
255     WLOGI("RegisterWindowCallback");
256     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
257     return (me != nullptr) ? me->OnRegisterWindowCallback(env, info) : nullptr;
258 }
259 
UnregisterWindowCallback(napi_env env,napi_callback_info info)260 napi_value JsWindow::UnregisterWindowCallback(napi_env env, napi_callback_info info)
261 {
262     WLOGI("UnregisterWindowCallback");
263     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
264     return (me != nullptr) ? me->OnUnregisterWindowCallback(env, info) : nullptr;
265 }
266 
BindDialogTarget(napi_env env,napi_callback_info info)267 napi_value JsWindow::BindDialogTarget(napi_env env, napi_callback_info info)
268 {
269     WLOGI("BindDialogTarget");
270     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
271     return (me != nullptr) ? me->OnBindDialogTarget(env, info) : nullptr;
272 }
273 
SetDialogBackGestureEnabled(napi_env env,napi_callback_info info)274 napi_value JsWindow::SetDialogBackGestureEnabled(napi_env env, napi_callback_info info)
275 {
276     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
277     return (me != nullptr) ? me->OnSetDialogBackGestureEnabled(env, info) : nullptr;
278 }
279 
LoadContent(napi_env env,napi_callback_info info)280 napi_value JsWindow::LoadContent(napi_env env, napi_callback_info info)
281 {
282     WLOGFI("[NAPI]");
283     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
284     return (me != nullptr) ? me->OnLoadContent(env, info, false) : nullptr;
285 }
286 
LoadContentByName(napi_env env,napi_callback_info info)287 napi_value JsWindow::LoadContentByName(napi_env env, napi_callback_info info)
288 {
289     WLOGFI("[NAPI]");
290     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
291     return (me != nullptr) ? me->OnLoadContent(env, info, true) : nullptr;
292 }
293 
GetUIContext(napi_env env,napi_callback_info info)294 napi_value JsWindow::GetUIContext(napi_env env, napi_callback_info info)
295 {
296     WLOGD("GetUIContext");
297     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
298     return (me != nullptr) ? me->OnGetUIContext(env, info) : nullptr;
299 }
300 
SetUIContent(napi_env env,napi_callback_info info)301 napi_value JsWindow::SetUIContent(napi_env env, napi_callback_info info)
302 {
303     WLOGFI("[NAPI]");
304     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
305     return (me != nullptr) ? me->OnSetUIContent(env, info) : nullptr;
306 }
307 
SetFullScreen(napi_env env,napi_callback_info info)308 napi_value JsWindow::SetFullScreen(napi_env env, napi_callback_info info)
309 {
310     TLOGD(WmsLogTag::WMS_IMMS, "SetFullScreen");
311     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
312     return (me != nullptr) ? me->OnSetFullScreen(env, info) : nullptr;
313 }
314 
SetLayoutFullScreen(napi_env env,napi_callback_info info)315 napi_value JsWindow::SetLayoutFullScreen(napi_env env, napi_callback_info info)
316 {
317     TLOGD(WmsLogTag::WMS_IMMS, "SetLayoutFullScreen");
318     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
319     return (me != nullptr) ? me->OnSetLayoutFullScreen(env, info) : nullptr;
320 }
321 
SetWindowLayoutFullScreen(napi_env env,napi_callback_info info)322 napi_value JsWindow::SetWindowLayoutFullScreen(napi_env env, napi_callback_info info)
323 {
324     TLOGD(WmsLogTag::WMS_IMMS, "SetWindowLayoutFullScreen");
325     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
326     return (me != nullptr) ? me->OnSetWindowLayoutFullScreen(env, info) : nullptr;
327 }
328 
SetSystemBarEnable(napi_env env,napi_callback_info info)329 napi_value JsWindow::SetSystemBarEnable(napi_env env, napi_callback_info info)
330 {
331     TLOGD(WmsLogTag::WMS_IMMS, "SetSystemBarEnable");
332     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
333     return (me != nullptr) ? me->OnSetSystemBarEnable(env, info) : nullptr;
334 }
335 
SetWindowSystemBarEnable(napi_env env,napi_callback_info info)336 napi_value JsWindow::SetWindowSystemBarEnable(napi_env env, napi_callback_info info)
337 {
338     TLOGD(WmsLogTag::WMS_IMMS, "SetSystemBarEnable");
339     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
340     return (me != nullptr) ? me->OnSetWindowSystemBarEnable(env, info) : nullptr;
341 }
342 
SetSpecificSystemBarEnabled(napi_env env,napi_callback_info info)343 napi_value JsWindow::SetSpecificSystemBarEnabled(napi_env env, napi_callback_info info)
344 {
345     WLOGI("SetSystemBarEnable");
346     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
347     return (me != nullptr) ? me->OnSetSpecificSystemBarEnabled(env, info) : nullptr;
348 }
349 
EnableLandscapeMultiWindow(napi_env env,napi_callback_info info)350 napi_value JsWindow::EnableLandscapeMultiWindow(napi_env env, napi_callback_info info)
351 {
352     TLOGI(WmsLogTag::WMS_MULTI_WINDOW, "EnableLandscapeMultiWindow");
353     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
354     return (me != nullptr) ? me->OnEnableLandscapeMultiWindow(env, info) : nullptr;
355 }
356 
DisableLandscapeMultiWindow(napi_env env,napi_callback_info info)357 napi_value JsWindow::DisableLandscapeMultiWindow(napi_env env, napi_callback_info info)
358 {
359     TLOGI(WmsLogTag::WMS_MULTI_WINDOW, "DisableLandscapeMultiWindow");
360     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
361     return (me != nullptr) ? me->OnDisableLandscapeMultiWindow(env, info) : nullptr;
362 }
363 
SetSystemBarProperties(napi_env env,napi_callback_info info)364 napi_value JsWindow::SetSystemBarProperties(napi_env env, napi_callback_info info)
365 {
366     TLOGD(WmsLogTag::WMS_IMMS, "SetSystemBarProperties");
367     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
368     return (me != nullptr) ? me->OnSetSystemBarProperties(env, info) : nullptr;
369 }
370 
GetWindowSystemBarPropertiesSync(napi_env env,napi_callback_info info)371 napi_value JsWindow::GetWindowSystemBarPropertiesSync(napi_env env, napi_callback_info info)
372 {
373     TLOGD(WmsLogTag::WMS_IMMS, "GetWindowSystemBarPropertiesSync");
374     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
375     return (me != nullptr) ? me->OnGetWindowSystemBarPropertiesSync(env, info) : nullptr;
376 }
377 
SetWindowSystemBarProperties(napi_env env,napi_callback_info info)378 napi_value JsWindow::SetWindowSystemBarProperties(napi_env env, napi_callback_info info)
379 {
380     TLOGD(WmsLogTag::WMS_IMMS, "SetWindowSystemBarProperties");
381     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
382     return (me != nullptr) ? me->OnSetWindowSystemBarProperties(env, info) : nullptr;
383 }
384 
GetAvoidArea(napi_env env,napi_callback_info info)385 napi_value JsWindow::GetAvoidArea(napi_env env, napi_callback_info info)
386 {
387     TLOGD(WmsLogTag::WMS_IMMS, "GetAvoidArea");
388     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
389     return (me != nullptr) ? me->OnGetAvoidArea(env, info) : nullptr;
390 }
391 
GetWindowAvoidAreaSync(napi_env env,napi_callback_info info)392 napi_value JsWindow::GetWindowAvoidAreaSync(napi_env env, napi_callback_info info)
393 {
394     TLOGD(WmsLogTag::WMS_IMMS, "GetWindowAvoidAreaSync");
395     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
396     return (me != nullptr) ? me->OnGetWindowAvoidAreaSync(env, info) : nullptr;
397 }
398 
IsShowing(napi_env env,napi_callback_info info)399 napi_value JsWindow::IsShowing(napi_env env, napi_callback_info info)
400 {
401     WLOGD("IsShowing");
402     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
403     return (me != nullptr) ? me->OnIsShowing(env, info) : nullptr;
404 }
405 
IsWindowShowingSync(napi_env env,napi_callback_info info)406 napi_value JsWindow::IsWindowShowingSync(napi_env env, napi_callback_info info)
407 {
408     WLOGD("IsShowing");
409     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
410     return (me != nullptr) ? me->OnIsWindowShowingSync(env, info) : nullptr;
411 }
412 
IsSupportWideGamut(napi_env env,napi_callback_info info)413 napi_value JsWindow::IsSupportWideGamut(napi_env env, napi_callback_info info)
414 {
415     WLOGI("IsSupportWideGamut");
416     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
417     return (me != nullptr) ? me->OnIsSupportWideGamut(env, info) : nullptr;
418 }
419 
IsWindowSupportWideGamut(napi_env env,napi_callback_info info)420 napi_value JsWindow::IsWindowSupportWideGamut(napi_env env, napi_callback_info info)
421 {
422     WLOGI("IsSupportWideGamut");
423     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
424     return (me != nullptr) ? me->OnIsWindowSupportWideGamut(env, info) : nullptr;
425 }
426 
SetBackgroundColor(napi_env env,napi_callback_info info)427 napi_value JsWindow::SetBackgroundColor(napi_env env, napi_callback_info info)
428 {
429     WLOGFD("SetBackgroundColor");
430     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
431     return (me != nullptr) ? me->OnSetBackgroundColor(env, info) : nullptr;
432 }
433 
SetWindowBackgroundColorSync(napi_env env,napi_callback_info info)434 napi_value JsWindow::SetWindowBackgroundColorSync(napi_env env, napi_callback_info info)
435 {
436     WLOGI("SetBackgroundColor");
437     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
438     return (me != nullptr) ? me->OnSetWindowBackgroundColorSync(env, info) : nullptr;
439 }
440 
SetBrightness(napi_env env,napi_callback_info info)441 napi_value JsWindow::SetBrightness(napi_env env, napi_callback_info info)
442 {
443     WLOGI("SetBrightness");
444     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
445     return (me != nullptr) ? me->OnSetBrightness(env, info) : nullptr;
446 }
447 
SetWindowBrightness(napi_env env,napi_callback_info info)448 napi_value JsWindow::SetWindowBrightness(napi_env env, napi_callback_info info)
449 {
450     WLOGI("SetBrightness");
451     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
452     return (me != nullptr) ? me->OnSetWindowBrightness(env, info) : nullptr;
453 }
454 
SetDimBehind(napi_env env,napi_callback_info info)455 napi_value JsWindow::SetDimBehind(napi_env env, napi_callback_info info)
456 {
457     WLOGI("SetDimBehind");
458     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
459     return (me != nullptr) ? me->OnSetDimBehind(env, info) : nullptr;
460 }
461 
SetFocusable(napi_env env,napi_callback_info info)462 napi_value JsWindow::SetFocusable(napi_env env, napi_callback_info info)
463 {
464     WLOGI("SetFocusable");
465     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
466     return (me != nullptr) ? me->OnSetFocusable(env, info) : nullptr;
467 }
468 
SetWindowFocusable(napi_env env,napi_callback_info info)469 napi_value JsWindow::SetWindowFocusable(napi_env env, napi_callback_info info)
470 {
471     WLOGI("SetFocusable");
472     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
473     return (me != nullptr) ? me->OnSetWindowFocusable(env, info) : nullptr;
474 }
475 
476 /** @note @window.hierarchy */
SetTopmost(napi_env env,napi_callback_info info)477 napi_value JsWindow::SetTopmost(napi_env env, napi_callback_info info)
478 {
479     TLOGI(WmsLogTag::WMS_LAYOUT, "SetTopmost");
480     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
481     return (me != nullptr) ? me->OnSetTopmost(env, info) : nullptr;
482 }
483 
SetKeepScreenOn(napi_env env,napi_callback_info info)484 napi_value JsWindow::SetKeepScreenOn(napi_env env, napi_callback_info info)
485 {
486     WLOGI("SetKeepScreenOn");
487     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
488     return (me != nullptr) ? me->OnSetKeepScreenOn(env, info) : nullptr;
489 }
490 
SetWindowKeepScreenOn(napi_env env,napi_callback_info info)491 napi_value JsWindow::SetWindowKeepScreenOn(napi_env env, napi_callback_info info)
492 {
493     WLOGI("SetKeepScreenOn");
494     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
495     return (me != nullptr) ? me->OnSetWindowKeepScreenOn(env, info) : nullptr;
496 }
497 
SetWakeUpScreen(napi_env env,napi_callback_info info)498 napi_value JsWindow::SetWakeUpScreen(napi_env env, napi_callback_info info)
499 {
500     WLOGI("SetWakeUpScreen");
501     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
502     return (me != nullptr) ? me->OnSetWakeUpScreen(env, info) : nullptr;
503 }
504 
SetOutsideTouchable(napi_env env,napi_callback_info info)505 napi_value JsWindow::SetOutsideTouchable(napi_env env, napi_callback_info info)
506 {
507     WLOGI("SetOutsideTouchable");
508     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
509     return (me != nullptr) ? me->OnSetOutsideTouchable(env, info) : nullptr;
510 }
511 
SetPrivacyMode(napi_env env,napi_callback_info info)512 napi_value JsWindow::SetPrivacyMode(napi_env env, napi_callback_info info)
513 {
514     WLOGI("SetPrivacyMode");
515     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
516     return (me != nullptr) ? me->OnSetPrivacyMode(env, info) : nullptr;
517 }
518 
SetWindowPrivacyMode(napi_env env,napi_callback_info info)519 napi_value JsWindow::SetWindowPrivacyMode(napi_env env, napi_callback_info info)
520 {
521     WLOGI("SetPrivacyMode");
522     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
523     return (me != nullptr) ? me->OnSetWindowPrivacyMode(env, info) : nullptr;
524 }
525 
SetTouchable(napi_env env,napi_callback_info info)526 napi_value JsWindow::SetTouchable(napi_env env, napi_callback_info info)
527 {
528     WLOGI("SetTouchable");
529     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
530     return (me != nullptr) ? me->OnSetTouchable(env, info) : nullptr;
531 }
532 
SetTouchableAreas(napi_env env,napi_callback_info info)533 napi_value JsWindow::SetTouchableAreas(napi_env env, napi_callback_info info)
534 {
535     TLOGI(WmsLogTag::WMS_EVENT, "SetTouchableAreas");
536     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
537     return (me != nullptr) ? me->OnSetTouchableAreas(env, info) : nullptr;
538 }
539 
SetResizeByDragEnabled(napi_env env,napi_callback_info info)540 napi_value JsWindow::SetResizeByDragEnabled(napi_env env, napi_callback_info info)
541 {
542     WLOGI("SetResizeByDragEnabled");
543     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
544     return (me != nullptr) ? me->OnSetResizeByDragEnabled(env, info) : nullptr;
545 }
546 
547 /** @note @window.hierarchy */
SetRaiseByClickEnabled(napi_env env,napi_callback_info info)548 napi_value JsWindow::SetRaiseByClickEnabled(napi_env env, napi_callback_info info)
549 {
550     WLOGI("SetRaiseByClickEnabled");
551     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
552     return (me != nullptr) ? me->OnSetRaiseByClickEnabled(env, info) : nullptr;
553 }
554 
HideNonSystemFloatingWindows(napi_env env,napi_callback_info info)555 napi_value JsWindow::HideNonSystemFloatingWindows(napi_env env, napi_callback_info info)
556 {
557     WLOGI("HideNonSystemFloatingWindows");
558     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
559     return (me != nullptr) ? me->OnHideNonSystemFloatingWindows(env, info) : nullptr;
560 }
561 
SetWindowTouchable(napi_env env,napi_callback_info info)562 napi_value JsWindow::SetWindowTouchable(napi_env env, napi_callback_info info)
563 {
564     WLOGI("SetTouchable");
565     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
566     return (me != nullptr) ? me->OnSetWindowTouchable(env, info) : nullptr;
567 }
568 
SetTransparent(napi_env env,napi_callback_info info)569 napi_value JsWindow::SetTransparent(napi_env env, napi_callback_info info)
570 {
571     WLOGI("SetTransparent");
572     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
573     return (me != nullptr) ? me->OnSetTransparent(env, info) : nullptr;
574 }
575 
SetCallingWindow(napi_env env,napi_callback_info info)576 napi_value JsWindow::SetCallingWindow(napi_env env, napi_callback_info info)
577 {
578     WLOGI("SetCallingWindow");
579     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
580     return (me != nullptr) ? me->OnSetCallingWindow(env, info) : nullptr;
581 }
582 
SetPreferredOrientation(napi_env env,napi_callback_info info)583 napi_value JsWindow::SetPreferredOrientation(napi_env env, napi_callback_info info)
584 {
585     WLOGD("SetPreferredOrientation");
586     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
587     return (me != nullptr) ? me->OnSetPreferredOrientation(env, info) : nullptr;
588 }
589 
GetPreferredOrientation(napi_env env,napi_callback_info info)590 napi_value JsWindow::GetPreferredOrientation(napi_env env, napi_callback_info info)
591 {
592     WLOGD("GetPreferredOrientation");
593     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
594     return (me != nullptr) ? me->OnGetPreferredOrientation(env, info) : nullptr;
595 }
596 
SetSnapshotSkip(napi_env env,napi_callback_info info)597 napi_value JsWindow::SetSnapshotSkip(napi_env env, napi_callback_info info)
598 {
599     WLOGI("SetSnapshotSkip");
600     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
601     return (me != nullptr) ? me->OnSetSnapshotSkip(env, info) : nullptr;
602 }
603 
SetSingleFrameComposerEnabled(napi_env env,napi_callback_info info)604 napi_value JsWindow::SetSingleFrameComposerEnabled(napi_env env, napi_callback_info info)
605 {
606     WLOGI("SetSingleFrameComposerEnabled");
607     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
608     return (me != nullptr) ? me->OnSetSingleFrameComposerEnabled(env, info) : nullptr;
609 }
610 
611 /** @note @window.hierarchy */
RaiseToAppTop(napi_env env,napi_callback_info info)612 napi_value JsWindow::RaiseToAppTop(napi_env env, napi_callback_info info)
613 {
614     WLOGI("RaiseToAppTop");
615     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
616     return (me != nullptr) ? me->OnRaiseToAppTop(env, info) : nullptr;
617 }
618 
DisableWindowDecor(napi_env env,napi_callback_info info)619 napi_value JsWindow::DisableWindowDecor(napi_env env, napi_callback_info info)
620 {
621     WLOGI("DisableWindowDecor");
622     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
623     return (me != nullptr) ? me->OnDisableWindowDecor(env, info) : nullptr;
624 }
625 
SetColorSpace(napi_env env,napi_callback_info info)626 napi_value JsWindow::SetColorSpace(napi_env env, napi_callback_info info)
627 {
628     WLOGI("SetColorSpace");
629     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
630     return (me != nullptr) ? me->OnSetColorSpace(env, info) : nullptr;
631 }
632 
SetWindowColorSpace(napi_env env,napi_callback_info info)633 napi_value JsWindow::SetWindowColorSpace(napi_env env, napi_callback_info info)
634 {
635     WLOGI("SetColorSpace");
636     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
637     return (me != nullptr) ? me->OnSetWindowColorSpace(env, info) : nullptr;
638 }
639 
GetColorSpace(napi_env env,napi_callback_info info)640 napi_value JsWindow::GetColorSpace(napi_env env, napi_callback_info info)
641 {
642     WLOGI("GetColorSpace");
643     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
644     return (me != nullptr) ? me->OnGetColorSpace(env, info) : nullptr;
645 }
646 
GetWindowColorSpaceSync(napi_env env,napi_callback_info info)647 napi_value JsWindow::GetWindowColorSpaceSync(napi_env env, napi_callback_info info)
648 {
649     WLOGI("GetColorSpace");
650     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
651     return (me != nullptr) ? me->OnGetWindowColorSpaceSync(env, info) : nullptr;
652 }
653 
Dump(napi_env env,napi_callback_info info)654 napi_value JsWindow::Dump(napi_env env, napi_callback_info info)
655 {
656     WLOGI("Dump");
657     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
658     return (me != nullptr) ? me->OnDump(env, info) : nullptr;
659 }
660 
SetForbidSplitMove(napi_env env,napi_callback_info info)661 napi_value JsWindow::SetForbidSplitMove(napi_env env, napi_callback_info info)
662 {
663     WLOGI("SetForbidSplitMove");
664     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
665     return (me != nullptr) ? me->OnSetForbidSplitMove(env, info) : nullptr;
666 }
667 
Opacity(napi_env env,napi_callback_info info)668 napi_value JsWindow::Opacity(napi_env env, napi_callback_info info)
669 {
670     WLOGI("Opacity");
671     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
672     return (me != nullptr) ? me->OnOpacity(env, info) : nullptr;
673 }
674 
Scale(napi_env env,napi_callback_info info)675 napi_value JsWindow::Scale(napi_env env, napi_callback_info info)
676 {
677     WLOGI("Scale");
678     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
679     return (me != nullptr) ? me->OnScale(env, info) : nullptr;
680 }
681 
Rotate(napi_env env,napi_callback_info info)682 napi_value JsWindow::Rotate(napi_env env, napi_callback_info info)
683 {
684     WLOGI("Rotate");
685     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
686     return (me != nullptr) ? me->OnRotate(env, info) : nullptr;
687 }
688 
Translate(napi_env env,napi_callback_info info)689 napi_value JsWindow::Translate(napi_env env, napi_callback_info info)
690 {
691     WLOGI("Translate");
692     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
693     return (me != nullptr) ? me->OnTranslate(env, info) : nullptr;
694 }
695 
GetTransitionController(napi_env env,napi_callback_info info)696 napi_value JsWindow::GetTransitionController(napi_env env, napi_callback_info info)
697 {
698     WLOGI("GetTransitionController");
699     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
700     return (me != nullptr) ? me->OnGetTransitionController(env, info) : nullptr;
701 }
702 
SetCornerRadius(napi_env env,napi_callback_info info)703 napi_value JsWindow::SetCornerRadius(napi_env env, napi_callback_info info)
704 {
705     WLOGI("SetCornerRadius");
706     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
707     return (me != nullptr) ? me->OnSetCornerRadius(env, info) : nullptr;
708 }
709 
SetShadow(napi_env env,napi_callback_info info)710 napi_value JsWindow::SetShadow(napi_env env, napi_callback_info info)
711 {
712     WLOGI("SetShadow");
713     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
714     return (me != nullptr) ? me->OnSetShadow(env, info) : nullptr;
715 }
716 
SetBlur(napi_env env,napi_callback_info info)717 napi_value JsWindow::SetBlur(napi_env env, napi_callback_info info)
718 {
719     WLOGI("SetBlur");
720     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
721     return (me != nullptr) ? me->OnSetBlur(env, info) : nullptr;
722 }
723 
SetBackdropBlur(napi_env env,napi_callback_info info)724 napi_value JsWindow::SetBackdropBlur(napi_env env, napi_callback_info info)
725 {
726     WLOGI("SetBackdropBlur");
727     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
728     return (me != nullptr) ? me->OnSetBackdropBlur(env, info) : nullptr;
729 }
730 
SetBackdropBlurStyle(napi_env env,napi_callback_info info)731 napi_value JsWindow::SetBackdropBlurStyle(napi_env env, napi_callback_info info)
732 {
733     WLOGI("SetBackdropBlurStyle");
734     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
735     return (me != nullptr) ? me->OnSetBackdropBlurStyle(env, info) : nullptr;
736 }
737 
SetWaterMarkFlag(napi_env env,napi_callback_info info)738 napi_value JsWindow::SetWaterMarkFlag(napi_env env, napi_callback_info info)
739 {
740     WLOGI("SetWaterMarkFlag");
741     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
742     return (me != nullptr) ? me->OnSetWaterMarkFlag(env, info) : nullptr;
743 }
744 
SetHandwritingFlag(napi_env env,napi_callback_info info)745 napi_value JsWindow::SetHandwritingFlag(napi_env env, napi_callback_info info)
746 {
747     WLOGI("SetHandwritingFlag");
748     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
749     return (me != nullptr) ? me->OnSetHandwritingFlag(env, info) : nullptr;
750 }
751 
SetAspectRatio(napi_env env,napi_callback_info info)752 napi_value JsWindow::SetAspectRatio(napi_env env, napi_callback_info info)
753 {
754     WLOGI("[NAPI]SetAspectRatio");
755     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
756     return (me != nullptr) ? me->OnSetAspectRatio(env, info) : nullptr;
757 }
758 
ResetAspectRatio(napi_env env,napi_callback_info info)759 napi_value JsWindow::ResetAspectRatio(napi_env env, napi_callback_info info)
760 {
761     WLOGI("[NAPI]ResetAspectRatio");
762     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
763     return (me != nullptr) ? me->OnResetAspectRatio(env, info) : nullptr;
764 }
765 
Minimize(napi_env env,napi_callback_info info)766 napi_value JsWindow::Minimize(napi_env env, napi_callback_info info)
767 {
768     WLOGI("[NAPI]Minimize");
769     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
770     return (me != nullptr) ? me->OnMinimize(env, info) : nullptr;
771 }
772 
Maximize(napi_env env,napi_callback_info info)773 napi_value JsWindow::Maximize(napi_env env, napi_callback_info info)
774 {
775     WLOGI("[NAPI]Maximize");
776     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
777     return (me != nullptr) ? me->OnMaximize(env, info) : nullptr;
778 }
779 
780 /** @note @window.hierarchy */
RaiseAboveTarget(napi_env env,napi_callback_info info)781 napi_value JsWindow::RaiseAboveTarget(napi_env env, napi_callback_info info)
782 {
783     WLOGI("[NAPI]RaiseAboveTarget");
784     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
785     return (me != nullptr) ? me->OnRaiseAboveTarget(env, info) : nullptr;
786 }
787 
KeepKeyboardOnFocus(napi_env env,napi_callback_info info)788 napi_value JsWindow::KeepKeyboardOnFocus(napi_env env, napi_callback_info info)
789 {
790     WLOGI("[NAPI]KeepKeyboardOnFocus");
791     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
792     return (me != nullptr) ? me->OnKeepKeyboardOnFocus(env, info) : nullptr;
793 }
794 
GetWindowLimits(napi_env env,napi_callback_info info)795 napi_value JsWindow::GetWindowLimits(napi_env env, napi_callback_info info)
796 {
797     WLOGI("[NAPI]GetWindowLimits");
798     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
799     return (me != nullptr) ? me->OnGetWindowLimits(env, info) : nullptr;
800 }
801 
SetWindowLimits(napi_env env,napi_callback_info info)802 napi_value JsWindow::SetWindowLimits(napi_env env, napi_callback_info info)
803 {
804     WLOGI("[NAPI]SetWindowLimits");
805     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
806     return (me != nullptr) ? me->OnSetWindowLimits(env, info) : nullptr;
807 }
808 
SetWindowDecorVisible(napi_env env,napi_callback_info info)809 napi_value JsWindow::SetWindowDecorVisible(napi_env env, napi_callback_info info)
810 {
811     WLOGI("[NAPI]SetWindowDecorVisible");
812     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
813     return (me != nullptr) ? me->OnSetWindowDecorVisible(env, info) : nullptr;
814 }
815 
SetSubWindowModal(napi_env env,napi_callback_info info)816 napi_value JsWindow::SetSubWindowModal(napi_env env, napi_callback_info info)
817 {
818     TLOGI(WmsLogTag::WMS_SUB, "[NAPI]");
819     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
820     return (me != nullptr) ? me->OnSetSubWindowModal(env, info) : nullptr;
821 }
822 
SetWindowDecorHeight(napi_env env,napi_callback_info info)823 napi_value JsWindow::SetWindowDecorHeight(napi_env env, napi_callback_info info)
824 {
825     WLOGI("[NAPI]SetWindowDecorHeight");
826     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
827     return (me != nullptr) ? me->OnSetWindowDecorHeight(env, info) : nullptr;
828 }
829 
GetWindowDecorHeight(napi_env env,napi_callback_info info)830 napi_value JsWindow::GetWindowDecorHeight(napi_env env, napi_callback_info info)
831 {
832     WLOGI("[NAPI]GetWindowDecorHeight");
833     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
834     return (me != nullptr) ? me->OnGetWindowDecorHeight(env, info) : nullptr;
835 }
836 
GetTitleButtonRect(napi_env env,napi_callback_info info)837 napi_value JsWindow::GetTitleButtonRect(napi_env env, napi_callback_info info)
838 {
839     WLOGI("[NAPI]GetTitleButtonsRect");
840     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
841     return (me != nullptr) ? me->OnGetTitleButtonRect(env, info) : nullptr;
842 }
843 
SetWindowMask(napi_env env,napi_callback_info info)844 napi_value JsWindow::SetWindowMask(napi_env env, napi_callback_info info)
845 {
846     WLOGI("[NAPI]SetWindowMask");
847     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
848     return (me != nullptr) ? me->OnSetWindowMask(env, info) : nullptr;
849 }
850 
SetTitleButtonVisible(napi_env env,napi_callback_info info)851 napi_value JsWindow::SetTitleButtonVisible(napi_env env, napi_callback_info info)
852 {
853     TLOGI(WmsLogTag::WMS_LAYOUT, "[NAPI]SetTitleButtonVisible");
854     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
855     return (me != nullptr) ? me->OnSetTitleButtonVisible(env, info) : nullptr;
856 }
857 
SetWindowGrayScale(napi_env env,napi_callback_info info)858 napi_value JsWindow::SetWindowGrayScale(napi_env env, napi_callback_info info)
859 {
860     WLOGI("[NAPI]SetWindowGrayScale");
861     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
862     return (me != nullptr) ? me->OnSetWindowGrayScale(env, info) : nullptr;
863 }
864 
SetImmersiveModeEnabledState(napi_env env,napi_callback_info info)865 napi_value JsWindow::SetImmersiveModeEnabledState(napi_env env, napi_callback_info info)
866 {
867     TLOGD(WmsLogTag::WMS_IMMS, "[NAPI]SetImmersiveModeEnabledState");
868     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
869     return (me != nullptr) ? me->OnSetImmersiveModeEnabledState(env, info) : nullptr;
870 }
871 
GetImmersiveModeEnabledState(napi_env env,napi_callback_info info)872 napi_value JsWindow::GetImmersiveModeEnabledState(napi_env env, napi_callback_info info)
873 {
874     TLOGD(WmsLogTag::WMS_IMMS, "[NAPI]GetImmersiveModeEnabledState");
875     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
876     return (me != nullptr) ? me->OnGetImmersiveModeEnabledState(env, info) : nullptr;
877 }
878 
GetWindowStatus(napi_env env,napi_callback_info info)879 napi_value JsWindow::GetWindowStatus(napi_env env, napi_callback_info info)
880 {
881     TLOGD(WmsLogTag::DEFAULT, "[NAPI]");
882     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
883     return (me != nullptr) ? me->OnGetWindowStatus(env, info) : nullptr;
884 }
885 
IsFocused(napi_env env,napi_callback_info info)886 napi_value JsWindow::IsFocused(napi_env env, napi_callback_info info)
887 {
888     TLOGD(WmsLogTag::WMS_FOCUS, "[NAPI]");
889     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
890     return (me != nullptr) ? me->OnIsFocused(env, info) : nullptr;
891 }
892 
RequestFocus(napi_env env,napi_callback_info info)893 napi_value JsWindow::RequestFocus(napi_env env, napi_callback_info info)
894 {
895     TLOGD(WmsLogTag::WMS_FOCUS, "[NAPI]");
896     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
897     return (me != nullptr) ? me->OnRequestFocus(env, info) : nullptr;
898 }
899 
CreateSubWindowWithOptions(napi_env env,napi_callback_info info)900 napi_value JsWindow::CreateSubWindowWithOptions(napi_env env, napi_callback_info info)
901 {
902     TLOGD(WmsLogTag::WMS_SUB, "[NAPI]");
903     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
904     return (me != nullptr) ? me->OnCreateSubWindowWithOptions(env, info) : nullptr;
905 }
906 
SetGestureBackEnabled(napi_env env,napi_callback_info info)907 napi_value JsWindow::SetGestureBackEnabled(napi_env env, napi_callback_info info)
908 {
909     TLOGD(WmsLogTag::WMS_IMMS, "[NAPI]");
910     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
911     return (me != nullptr) ? me->OnSetGestureBackEnabled(env, info) : nullptr;
912 }
913 
GetGestureBackEnabled(napi_env env,napi_callback_info info)914 napi_value JsWindow::GetGestureBackEnabled(napi_env env, napi_callback_info info)
915 {
916     TLOGD(WmsLogTag::WMS_IMMS, "[NAPI]");
917     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
918     return (me != nullptr) ? me->OnGetGestureBackEnabled(env, info) : nullptr;
919 }
920 
UpdateSystemBarProperties(std::map<WindowType,SystemBarProperty> & systemBarProperties,const std::map<WindowType,SystemBarPropertyFlag> & systemBarPropertyFlags,sptr<Window> windowToken)921 static void UpdateSystemBarProperties(std::map<WindowType, SystemBarProperty>& systemBarProperties,
922     const std::map<WindowType, SystemBarPropertyFlag>& systemBarPropertyFlags, sptr<Window> windowToken)
923 {
924     for (auto it : systemBarPropertyFlags) {
925         WindowType type = it.first;
926         SystemBarPropertyFlag flag = it.second;
927         auto property = windowToken->GetSystemBarPropertyByType(type);
928         if (flag.enableFlag == false) {
929             systemBarProperties[type].enable_ = property.enable_;
930         }
931         if (flag.backgroundColorFlag == false) {
932             systemBarProperties[type].backgroundColor_ = property.backgroundColor_;
933         }
934         if (flag.contentColorFlag == false) {
935             systemBarProperties[type].contentColor_ = property.contentColor_;
936         }
937         if (flag.enableAnimationFlag == false) {
938             systemBarProperties[type].enableAnimation_ = property.enableAnimation_;
939         }
940         if (flag.enableFlag == true) {
941             systemBarProperties[type].settingFlag_ =
942                 static_cast<SystemBarSettingFlag>(static_cast<uint32_t>(property.settingFlag_) |
943                 static_cast<uint32_t>(SystemBarSettingFlag::ENABLE_SETTING));
944         }
945         if (flag.backgroundColorFlag == true || flag.contentColorFlag == true) {
946             systemBarProperties[type].settingFlag_ =
947                 static_cast<SystemBarSettingFlag>(static_cast<uint32_t>(property.settingFlag_) |
948                 static_cast<uint32_t>(SystemBarSettingFlag::COLOR_SETTING));
949         }
950     }
951 
952     return;
953 }
954 
NapiGetUndefined(napi_env env)955 napi_value NapiGetUndefined(napi_env env)
956 {
957     napi_value result = nullptr;
958     napi_get_undefined(env, &result);
959     return result;
960 }
961 
NapiThrowError(napi_env env,WmErrorCode errCode)962 napi_value NapiThrowError(napi_env env, WmErrorCode errCode)
963 {
964     napi_throw(env, JsErrUtils::CreateJsError(env, errCode));
965     return NapiGetUndefined(env);
966 }
967 
GetType(napi_env env,napi_value value)968 napi_valuetype GetType(napi_env env, napi_value value)
969 {
970     napi_valuetype res = napi_undefined;
971     napi_typeof(env, value, &res);
972     return res;
973 }
974 
OnShow(napi_env env,napi_callback_info info)975 napi_value JsWindow::OnShow(napi_env env, napi_callback_info info)
976 {
977     WMError errCode = WMError::WM_OK;
978     size_t argc = 4;
979     napi_value argv[4] = {nullptr};
980     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
981     if (argc > 1) {
982         WLOGFE("Argc is invalid: %{public}zu", argc);
983         errCode = WMError::WM_ERROR_INVALID_PARAM;
984     }
985     wptr<Window> weakToken(windowToken_);
986     NapiAsyncTask::CompleteCallback complete =
987         [weakToken, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
988             auto weakWindow = weakToken.promote();
989             if (weakWindow == nullptr) {
990                 WLOGFE("window is nullptr");
991                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
992                 return;
993             }
994             if (errCode != WMError::WM_OK) {
995                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
996                 WLOGFE("window is nullptr or get invalid param");
997                 return;
998             }
999             if (WindowHelper::IsMainWindowAndNotShown(weakWindow->GetType(), weakWindow->GetWindowState())) {
1000                 TLOGW(WmsLogTag::WMS_LIFE,
1001                     "window Type %{public}u and window state %{public}u is not supported, [%{public}u, %{public}s]",
1002                     static_cast<uint32_t>(weakWindow->GetType()), static_cast<uint32_t>(weakWindow->GetWindowState()),
1003                     weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
1004                 task.Resolve(env, NapiGetUndefined(env));
1005                 return;
1006             }
1007             WMError ret = weakWindow->Show(0, false, true);
1008             if (ret == WMError::WM_OK) {
1009                 task.Resolve(env, NapiGetUndefined(env));
1010             } else {
1011                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window show failed"));
1012             }
1013             WLOGI("Window [%{public}u] show end, ret = %{public}d", weakWindow->GetWindowId(), ret);
1014         };
1015     napi_value result = nullptr;
1016     napi_value lastParam = (argc == 0) ? nullptr : (GetType(env, argv[0]) == napi_function ? argv[0] : nullptr);
1017     NapiAsyncTask::Schedule("JsWindow::OnShow",
1018         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
1019     return result;
1020 }
1021 
OnShowWindow(napi_env env,napi_callback_info info)1022 napi_value JsWindow::OnShowWindow(napi_env env, napi_callback_info info)
1023 {
1024     wptr<Window> weakToken(windowToken_);
1025     NapiAsyncTask::CompleteCallback complete =
1026         [weakToken](napi_env env, NapiAsyncTask& task, int32_t status) {
1027             auto weakWindow = weakToken.promote();
1028             if (weakWindow == nullptr) {
1029                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
1030                 WLOGFE("window is nullptr or get invalid param");
1031                 return;
1032             }
1033             if (WindowHelper::IsMainWindowAndNotShown(weakWindow->GetType(), weakWindow->GetWindowState())) {
1034                 TLOGW(WmsLogTag::WMS_LIFE,
1035                     "window Type %{public}u and window state %{public}u is not supported, [%{public}u, %{public}s]",
1036                     static_cast<uint32_t>(weakWindow->GetType()), static_cast<uint32_t>(weakWindow->GetWindowState()),
1037                     weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
1038                 task.Resolve(env, NapiGetUndefined(env));
1039                 return;
1040             }
1041             WMError ret = weakWindow->Show(0, false, true);
1042             WLOGI("Window [%{public}u, %{public}s] show with ret = %{public}d",
1043                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1044             if (ret == WMError::WM_OK) {
1045                 task.Resolve(env, NapiGetUndefined(env));
1046             } else {
1047                 task.Reject(env, JsErrUtils::CreateJsError(env, WM_JS_TO_ERROR_CODE_MAP.at(ret),
1048                     "Window show failed"));
1049             }
1050             WLOGI("Window [%{public}u, %{public}s] show end, ret = %{public}d",
1051                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1052         };
1053     napi_value result = nullptr;
1054     size_t argc = 4;
1055     napi_value argv[4] = {nullptr};
1056     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1057     napi_value lastParam = (argc == 0) ? nullptr :
1058         ((argv[0] != nullptr && GetType(env, argv[0]) == napi_function) ? argv[0] : nullptr);
1059     NapiAsyncTask::Schedule("JsWindow::OnShow",
1060         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
1061     return result;
1062 }
1063 
OnShowWithAnimation(napi_env env,napi_callback_info info)1064 napi_value JsWindow::OnShowWithAnimation(napi_env env, napi_callback_info info)
1065 {
1066     WmErrorCode errCode = Permission::IsSystemCallingOrStartByHdcd(true) ?
1067         WmErrorCode::WM_OK : WmErrorCode::WM_ERROR_NOT_SYSTEM_APP;
1068     if (errCode == WmErrorCode::WM_OK) {
1069         if (windowToken_ == nullptr) {
1070             errCode = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
1071         } else {
1072             auto winType = windowToken_->GetType();
1073             if (!WindowHelper::IsSystemWindow(winType)) {
1074                 TLOGE(WmsLogTag::WMS_LIFE,
1075                     "Window Type %{public}u is not supported", static_cast<uint32_t>(winType));
1076                 errCode = WmErrorCode::WM_ERROR_INVALID_CALLING;
1077             }
1078         }
1079     }
1080     wptr<Window> weakToken(windowToken_);
1081     NapiAsyncTask::CompleteCallback complete =
1082         [weakToken, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
1083             if (errCode != WmErrorCode::WM_OK) {
1084                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
1085                 return;
1086             }
1087             auto weakWindow = weakToken.promote();
1088             if (weakWindow == nullptr) {
1089                 WLOGFE("window is nullptr");
1090                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
1091                 return;
1092             }
1093             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->Show(0, true, true));
1094             if (ret == WmErrorCode::WM_OK) {
1095                 task.Resolve(env, NapiGetUndefined(env));
1096             } else {
1097                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window show failed"));
1098             }
1099             WLOGI("Window [%{public}u, %{public}s] ShowWithAnimation end, ret = %{public}d",
1100                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1101         };
1102     napi_value result = nullptr;
1103     size_t argc = 4;
1104     napi_value argv[4] = {nullptr};
1105     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1106     napi_value lastParam = (argc == 0) ? nullptr :
1107         ((argv[0] != nullptr && GetType(env, argv[0]) == napi_function) ? argv[0] : nullptr);
1108     NapiAsyncTask::Schedule("JsWindow::OnShowWithAnimation",
1109         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
1110     return result;
1111 }
1112 
OnDestroy(napi_env env,napi_callback_info info)1113 napi_value JsWindow::OnDestroy(napi_env env, napi_callback_info info)
1114 {
1115     WMError errCode = WMError::WM_OK;
1116     size_t argc = 4;
1117     napi_value argv[4] = {nullptr};
1118     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1119     if (argc > 1) {
1120         WLOGFE("Argc is invalid: %{public}zu", argc);
1121         errCode = WMError::WM_ERROR_INVALID_PARAM;
1122     }
1123     wptr<Window> weakToken(windowToken_);
1124     NapiAsyncTask::CompleteCallback complete =
1125         [this, weakToken, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
1126             auto weakWindow = weakToken.promote();
1127             if (weakWindow == nullptr) {
1128                 WLOGFE("window is nullptr");
1129                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
1130                 return;
1131             }
1132             if (errCode != WMError::WM_OK) {
1133                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
1134                 WLOGFE("window is nullptr or get invalid param");
1135                 return;
1136             }
1137             if (WindowHelper::IsMainWindow(weakWindow->GetType())) {
1138                 TLOGW(WmsLogTag::WMS_LIFE, "window Type %{public}u is not supported, [%{public}u, %{public}s]",
1139                     static_cast<uint32_t>(weakWindow->GetType()),
1140                     weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
1141                 task.Resolve(env, NapiGetUndefined(env));
1142                 return;
1143             }
1144             WMError ret = weakWindow->Destroy();
1145             WLOGI("Window [%{public}u, %{public}s] destroy end, ret = %{public}d",
1146                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1147             if (ret != WMError::WM_OK) {
1148                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window destroy failed"));
1149                 return;
1150             }
1151             windowToken_ = nullptr; // ensure window dtor when finalizer invalid
1152             task.Resolve(env, NapiGetUndefined(env));
1153         };
1154 
1155     napi_value lastParam = (argc == 0) ? nullptr : (GetType(env, argv[0]) == napi_function ? argv[0] : nullptr);
1156     napi_value result = nullptr;
1157     NapiAsyncTask::Schedule("JsWindow::OnDestroy",
1158         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
1159     return result;
1160 }
1161 
OnDestroyWindow(napi_env env,napi_callback_info info)1162 napi_value JsWindow::OnDestroyWindow(napi_env env, napi_callback_info info)
1163 {
1164     wptr<Window> weakToken(windowToken_);
1165     NapiAsyncTask::CompleteCallback complete =
1166         [this, weakToken](napi_env env, NapiAsyncTask& task, int32_t status) {
1167             auto weakWindow = weakToken.promote();
1168             if (weakWindow == nullptr) {
1169                 WLOGFE("window is nullptr or get invalid param");
1170                 task.Reject(env,
1171                     JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
1172                 return;
1173             }
1174             if (WindowHelper::IsMainWindow(weakWindow->GetType())) {
1175                 TLOGW(WmsLogTag::WMS_LIFE, "window Type %{public}u is not supported, [%{public}u, %{public}s]",
1176                     static_cast<uint32_t>(weakWindow->GetType()),
1177                     weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
1178                 task.Resolve(env, NapiGetUndefined(env));
1179                 return;
1180             }
1181             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->Destroy());
1182             WLOGI("Window [%{public}u, %{public}s] destroy end, ret = %{public}d",
1183                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1184             if (ret != WmErrorCode::WM_OK) {
1185                 task.Reject(env,
1186                     JsErrUtils::CreateJsError(env, ret, "Window destroy failed"));
1187                 return;
1188             }
1189             windowToken_ = nullptr; // ensure window dtor when finalizer invalid
1190             task.Resolve(env, NapiGetUndefined(env));
1191         };
1192     size_t argc = 4;
1193     napi_value argv[4] = {nullptr};
1194     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1195     napi_value lastParam = (argc == 0) ? nullptr :
1196         ((argv[0] != nullptr && GetType(env, argv[0]) == napi_function) ? argv[0] : nullptr);
1197     napi_value result = nullptr;
1198     NapiAsyncTask::Schedule("JsWindow::OnDestroyWindow",
1199         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
1200     return result;
1201 }
1202 
OnHide(napi_env env,napi_callback_info info)1203 napi_value JsWindow::OnHide(napi_env env, napi_callback_info info)
1204 {
1205     WmErrorCode errCode = Permission::IsSystemCallingOrStartByHdcd(true) ?
1206         WmErrorCode::WM_OK : WmErrorCode::WM_ERROR_NOT_SYSTEM_APP;
1207     return HideWindowFunction(env, info, errCode);
1208 }
1209 
HideWindowFunction(napi_env env,napi_callback_info info,WmErrorCode errCode)1210 napi_value JsWindow::HideWindowFunction(napi_env env, napi_callback_info info, WmErrorCode errCode)
1211 {
1212     wptr<Window> weakToken(windowToken_);
1213     NapiAsyncTask::CompleteCallback complete =
1214         [weakToken, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
1215             if (errCode != WmErrorCode::WM_OK) {
1216                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
1217                 return;
1218             }
1219             auto weakWindow = weakToken.promote();
1220             if (weakWindow == nullptr) {
1221                 WLOGFE("window is nullptr or get invalid param");
1222                 task.Reject(env,
1223                     JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
1224                 return;
1225             }
1226             if (WindowHelper::IsMainWindow(weakWindow->GetType())) {
1227                 TLOGW(WmsLogTag::WMS_LIFE, "window Type %{public}u is not supported, [%{public}u, %{public}s]",
1228                     static_cast<uint32_t>(weakWindow->GetType()),
1229                     weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
1230                 task.Resolve(env, NapiGetUndefined(env));
1231                 return;
1232             }
1233             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->Hide(0, false, false));
1234             if (ret == WmErrorCode::WM_OK) {
1235                 task.Resolve(env, NapiGetUndefined(env));
1236             } else {
1237                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window hide failed"));
1238             }
1239             WLOGI("Window [%{public}u] hide end, ret = %{public}d", weakWindow->GetWindowId(), ret);
1240         };
1241 
1242     size_t argc = 4;
1243     napi_value argv[4] = {nullptr};
1244     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1245     napi_value lastParam = (argc == 0) ? nullptr :
1246         (argv[0] != nullptr && GetType(env, argv[0]) == napi_function ? argv[0] : nullptr);
1247     napi_value result = nullptr;
1248     NapiAsyncTask::Schedule("JsWindow::OnHide",
1249         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
1250     return result;
1251 }
1252 
OnHideWithAnimation(napi_env env,napi_callback_info info)1253 napi_value JsWindow::OnHideWithAnimation(napi_env env, napi_callback_info info)
1254 {
1255     WmErrorCode errCode = Permission::IsSystemCallingOrStartByHdcd(true) ?
1256         WmErrorCode::WM_OK : WmErrorCode::WM_ERROR_NOT_SYSTEM_APP;
1257     if (errCode == WmErrorCode::WM_OK) {
1258         if (windowToken_) {
1259             auto winType = windowToken_->GetType();
1260             if (!WindowHelper::IsSystemWindow(winType)) {
1261                 TLOGE(WmsLogTag::WMS_LIFE,
1262                     "window Type %{public}u is not supported", static_cast<uint32_t>(winType));
1263                 errCode = WmErrorCode::WM_ERROR_INVALID_CALLING;
1264             }
1265         } else {
1266             errCode = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
1267         }
1268     }
1269     wptr<Window> weakToken(windowToken_);
1270     NapiAsyncTask::CompleteCallback complete =
1271         [weakToken, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
1272             if (errCode != WmErrorCode::WM_OK) {
1273                 task.Reject(env,
1274                     JsErrUtils::CreateJsError(env, errCode));
1275                 return;
1276             }
1277             auto weakWindow = weakToken.promote();
1278             if (weakWindow == nullptr) {
1279                 WLOGFE("window is nullptr");
1280                 task.Reject(env,
1281                     JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
1282                 return;
1283             }
1284             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->Hide(0, true, false));
1285             if (ret == WmErrorCode::WM_OK) {
1286                 task.Resolve(env, NapiGetUndefined(env));
1287             } else {
1288                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window show failed"));
1289             }
1290             WLOGI("Window [%{public}u, %{public}s] HideWithAnimation end, ret = %{public}d",
1291                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1292         };
1293     size_t argc = 4;
1294     napi_value argv[4] = {nullptr};
1295     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1296     napi_value lastParam = (argc == 0) ? nullptr :
1297         ((argv[0] != nullptr && GetType(env, argv[0]) == napi_function) ? argv[0] : nullptr);
1298     napi_value result = nullptr;
1299     NapiAsyncTask::Schedule("JsWindow::OnHideWithAnimation",
1300         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
1301     return result;
1302 }
1303 
OnRecover(napi_env env,napi_callback_info info)1304 napi_value JsWindow::OnRecover(napi_env env, napi_callback_info info)
1305 {
1306     wptr<Window> weakToken(windowToken_);
1307     NapiAsyncTask::CompleteCallback complete =
1308         [weakToken](napi_env env, NapiAsyncTask& task, int32_t status) {
1309             auto weakWindow = weakToken.promote();
1310             if (weakWindow == nullptr) {
1311                 WLOGFE("window is nullptr or get invalid param");
1312                 task.Reject(env,
1313                     JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
1314                 return;
1315             }
1316             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->Recover(1));
1317             if (ret == WmErrorCode::WM_OK) {
1318                 task.Resolve(env, NapiGetUndefined(env));
1319             } else {
1320                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window recover failed"));
1321             }
1322             WLOGI("Window [%{public}u] recover end, ret = %{public}d", weakWindow->GetWindowId(), ret);
1323         };
1324 
1325     size_t argc = 4;
1326     napi_value argv[4] = {nullptr};
1327     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1328     napi_value lastParam = (argc == 0) ? nullptr :
1329         (argv[0] != nullptr && GetType(env, argv[0]) == napi_function ? argv[0] : nullptr);
1330     napi_value result = nullptr;
1331     NapiAsyncTask::Schedule("JsWindow::OnRecover",
1332         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
1333     return result;
1334 }
1335 
OnMoveTo(napi_env env,napi_callback_info info)1336 napi_value JsWindow::OnMoveTo(napi_env env, napi_callback_info info)
1337 {
1338     WMError errCode = WMError::WM_OK;
1339     size_t argc = 4;
1340     napi_value argv[4] = {nullptr};
1341     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1342     if (argc < 2 || argc > 3) { // 2:minimum param num, 3: maximum param num
1343         WLOGFE("Argc is invalid: %{public}zu", argc);
1344         errCode = WMError::WM_ERROR_INVALID_PARAM;
1345     }
1346     int32_t x = 0;
1347     if (errCode == WMError::WM_OK && !ConvertFromJsValue(env, argv[0], x)) {
1348         WLOGFE("Failed to convert parameter to x");
1349         errCode = WMError::WM_ERROR_INVALID_PARAM;
1350     }
1351 
1352     int32_t y = 0;
1353     if (errCode == WMError::WM_OK && !ConvertFromJsValue(env, argv[1], y)) {
1354         WLOGFE("Failed to convert parameter to y");
1355         errCode = WMError::WM_ERROR_INVALID_PARAM;
1356     }
1357 
1358     wptr<Window> weakToken(windowToken_);
1359     NapiAsyncTask::CompleteCallback complete =
1360         [weakToken, errCode, x, y](napi_env env, NapiAsyncTask& task, int32_t status) {
1361             auto weakWindow = weakToken.promote();
1362             if (weakWindow == nullptr) {
1363                 WLOGFE("window is nullptr");
1364                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
1365                 return;
1366             }
1367             if (errCode != WMError::WM_OK) {
1368                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
1369                 WLOGFE("window is nullptr or get invalid param");
1370                 return;
1371             }
1372             WMError ret = weakWindow->MoveTo(x, y);
1373             if (ret == WMError::WM_OK) {
1374                 task.Resolve(env, NapiGetUndefined(env));
1375             } else {
1376                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window move failed"));
1377             }
1378             WLOGFD("Window [%{public}u, %{public}s] move end, ret = %{public}d",
1379                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1380         };
1381     // 2: params num; 2: index of callback
1382     napi_value lastParam = (argc <= 2) ? nullptr : (GetType(env, argv[2]) == napi_function ? argv[2] : nullptr);
1383     napi_value result = nullptr;
1384     NapiAsyncTask::Schedule("JsWindow::OnMoveTo",
1385         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
1386     return result;
1387 }
1388 
OnMoveWindowTo(napi_env env,napi_callback_info info)1389 napi_value JsWindow::OnMoveWindowTo(napi_env env, napi_callback_info info)
1390 {
1391     WmErrorCode errCode = WmErrorCode::WM_OK;
1392     size_t argc = 4;
1393     napi_value argv[4] = {nullptr};
1394     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1395     if (argc < 2) { // 2:minimum param num
1396         WLOGFE("Argc is invalid: %{public}zu", argc);
1397         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1398     }
1399     int32_t x = 0;
1400     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(env, argv[0], x)) {
1401         WLOGFE("Failed to convert parameter to x");
1402         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1403     }
1404     int32_t y = 0;
1405     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(env, argv[1], y)) {
1406         WLOGFE("Failed to convert parameter to y");
1407         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1408     }
1409     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
1410         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1411     }
1412 
1413     wptr<Window> weakToken(windowToken_);
1414     NapiAsyncTask::CompleteCallback complete =
1415         [weakToken, x, y](napi_env env, NapiAsyncTask& task, int32_t status) {
1416             auto weakWindow = weakToken.promote();
1417             if (weakWindow == nullptr) {
1418                 WLOGFE("window is nullptr");
1419                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
1420                 return;
1421             }
1422             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->MoveTo(x, y));
1423             if (ret == WmErrorCode::WM_OK) {
1424                 task.Resolve(env, NapiGetUndefined(env));
1425             } else {
1426                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window move failed"));
1427             }
1428             WLOGI("Window [%{public}u, %{public}s] move end, ret = %{public}d",
1429                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1430         };
1431 
1432     // 2: params num; 2: index of callback
1433     napi_value lastParam = (argc <= 2) ? nullptr :
1434         ((argv[2] != nullptr && GetType(env, argv[2]) == napi_function) ? argv[2] : nullptr);
1435     napi_value result = nullptr;
1436     NapiAsyncTask::Schedule("JsWindow::OnMoveWindowTo",
1437         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
1438     return result;
1439 }
1440 
SetMoveWindowToAsyncTask(NapiAsyncTask::ExecuteCallback & execute,NapiAsyncTask::CompleteCallback & complete,wptr<Window> weakToken,int32_t x,int32_t y)1441 static void SetMoveWindowToAsyncTask(NapiAsyncTask::ExecuteCallback& execute, NapiAsyncTask::CompleteCallback& complete,
1442     wptr<Window> weakToken, int32_t x, int32_t y)
1443 {
1444     std::shared_ptr<WmErrorCode> errCodePtr = std::make_shared<WmErrorCode>(WmErrorCode::WM_OK);
1445     execute = [weakToken, errCodePtr, x, y] {
1446         if (errCodePtr == nullptr) {
1447             return;
1448         }
1449         if (*errCodePtr != WmErrorCode::WM_OK) {
1450             return;
1451         }
1452         auto weakWindow = weakToken.promote();
1453         if (weakWindow == nullptr) {
1454             TLOGE(WmsLogTag::WMS_LAYOUT, "window is nullptr");
1455             *errCodePtr = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
1456             return;
1457         }
1458         *errCodePtr = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->MoveToAsync(x, y));
1459         TLOGI(WmsLogTag::WMS_LAYOUT, "Window [%{public}u, %{public}s] move end, err = %{public}d",
1460             weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), *errCodePtr);
1461     };
1462     complete = [weakToken, errCodePtr](napi_env env, NapiAsyncTask& task, int32_t status) {
1463         if (errCodePtr == nullptr) {
1464             task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
1465             return;
1466         }
1467         if (*errCodePtr == WmErrorCode::WM_OK) {
1468             task.Resolve(env, NapiGetUndefined(env));
1469         } else {
1470             task.Reject(env, JsErrUtils::CreateJsError(env, *errCodePtr, "JsWindow::OnMoveWindowToAsync failed"));
1471         }
1472     };
1473 }
1474 
OnMoveWindowToAsync(napi_env env,napi_callback_info info)1475 napi_value JsWindow::OnMoveWindowToAsync(napi_env env, napi_callback_info info)
1476 {
1477     WmErrorCode errCode = WmErrorCode::WM_OK;
1478     size_t argc = 4; // 4: number of arg
1479     napi_value argv[4] = {nullptr};
1480     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1481     if (argc < 2) { // 2: minimum param num
1482         WLOGFE("Argc is invalid: %{public}zu", argc);
1483         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1484     }
1485     int32_t x = 0;
1486     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(env, argv[0], x)) {
1487         WLOGFE("Failed to convert parameter to x");
1488         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1489     }
1490     int32_t y = 0;
1491     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(env, argv[1], y)) {
1492         WLOGFE("Failed to convert parameter to y");
1493         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1494     }
1495     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
1496         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1497     }
1498 
1499     wptr<Window> weakToken(windowToken_);
1500     NapiAsyncTask::ExecuteCallback execute;
1501     NapiAsyncTask::CompleteCallback complete;
1502     SetMoveWindowToAsyncTask(execute, complete, weakToken, x, y);
1503 
1504     // 2: params num; 2: index of callback
1505     napi_value lastParam = (argc <= 2) ? nullptr :
1506         ((argv[2] != nullptr && GetType(env, argv[2]) == napi_function) ? argv[2] : nullptr);
1507     napi_value result = nullptr;
1508     NapiAsyncTask::Schedule("JsWindow::OnMoveWindowToAsync",
1509         env, CreateAsyncTaskWithLastParam(env, lastParam, std::move(execute), std::move(complete), &result));
1510     return result;
1511 }
1512 
SetMoveWindowToGlobalAsyncTask(NapiAsyncTask::ExecuteCallback & execute,NapiAsyncTask::CompleteCallback & complete,wptr<Window> weakToken,int32_t x,int32_t y)1513 static void SetMoveWindowToGlobalAsyncTask(NapiAsyncTask::ExecuteCallback &execute,
1514     NapiAsyncTask::CompleteCallback &complete, wptr<Window> weakToken, int32_t x, int32_t y)
1515 {
1516     std::shared_ptr<WmErrorCode> errCodePtr = std::make_shared<WmErrorCode>(WmErrorCode::WM_OK);
1517     execute = [weakToken, errCodePtr, x, y] {
1518         if (errCodePtr == nullptr) {
1519             return;
1520         }
1521         if (*errCodePtr != WmErrorCode::WM_OK) {
1522             return;
1523         }
1524         auto weakWindow = weakToken.promote();
1525         if (weakWindow == nullptr) {
1526             TLOGE(WmsLogTag::WMS_LAYOUT, "window is nullptr");
1527             *errCodePtr = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
1528             return;
1529         }
1530         *errCodePtr = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->MoveWindowToGlobal(x, y));
1531         TLOGI(WmsLogTag::WMS_LAYOUT, "Window [%{public}u, %{public}s] move end, err = %{public}d",
1532             weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), *errCodePtr);
1533     };
1534     complete = [weakToken, errCodePtr](napi_env env, NapiAsyncTask& task, int32_t status) {
1535         if (errCodePtr == nullptr) {
1536             task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
1537             return;
1538         }
1539         if (*errCodePtr == WmErrorCode::WM_OK) {
1540             task.Resolve(env, NapiGetUndefined(env));
1541         } else {
1542             task.Reject(env, JsErrUtils::CreateJsError(
1543                 env, *errCodePtr, "JsWindow::OnMoveWindowToGlobal failed"));
1544         }
1545     };
1546 }
1547 
1548 /** @note @window.layout */
OnMoveWindowToGlobal(napi_env env,napi_callback_info info)1549 napi_value JsWindow::OnMoveWindowToGlobal(napi_env env, napi_callback_info info)
1550 {
1551     WmErrorCode errCode = WmErrorCode::WM_OK;
1552     size_t argc = 4;
1553     napi_value argv[4] = {nullptr};
1554     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1555     if (argc < 2) { // 2:minimum param num
1556         WLOGFE("Argc is invalid: %{public}zu", argc);
1557         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1558     }
1559     int32_t x = 0;
1560     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(env, argv[0], x)) {
1561         WLOGFE("Failed to convert parameter to x");
1562         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1563     }
1564     int32_t y = 0;
1565     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(env, argv[1], y)) {
1566         WLOGFE("Failed to convert parameter to y");
1567         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1568     }
1569     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
1570         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1571     }
1572 
1573     wptr<Window> weakToken(windowToken_);
1574     NapiAsyncTask::ExecuteCallback execute;
1575     NapiAsyncTask::CompleteCallback complete;
1576     SetMoveWindowToGlobalAsyncTask(execute, complete, weakToken, x, y);
1577 
1578     // 2: params num; 2: index of callback
1579     napi_value lastParam = (argc <= 2) ? nullptr :
1580         ((argv[2] != nullptr && GetType(env, argv[2]) == napi_function) ? argv[2] : nullptr);
1581     napi_value result = nullptr;
1582     NapiAsyncTask::Schedule("JsWindow::OnMoveWindowToGlobal",
1583         env, CreateAsyncTaskWithLastParam(env, lastParam, std::move(execute), std::move(complete), &result));
1584     return result;
1585 }
1586 
1587 /** @note @window.layout */
OnGetGlobalScaledRect(napi_env env,napi_callback_info info)1588 napi_value JsWindow::OnGetGlobalScaledRect(napi_env env, napi_callback_info info)
1589 {
1590     if (windowToken_ == nullptr) {
1591         TLOGE(WmsLogTag::WMS_LAYOUT, "window is nullptr");
1592         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1593     }
1594     Rect globalScaledRect;
1595     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->GetGlobalScaledRect(globalScaledRect));
1596     if (ret != WmErrorCode::WM_OK) {
1597         return NapiThrowError(env, ret);
1598     }
1599     TLOGI(WmsLogTag::WMS_LAYOUT, "Window [%{public}u, %{public}s] end",
1600         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1601     napi_value globalScaledRectObj = GetRectAndConvertToJsValue(env, globalScaledRect);
1602     if (globalScaledRectObj == nullptr) {
1603         TLOGE(WmsLogTag::WMS_LAYOUT, "globalScaledRectObj is nullptr");
1604         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1605     }
1606     return globalScaledRectObj;
1607 }
1608 
OnResize(napi_env env,napi_callback_info info)1609 napi_value JsWindow::OnResize(napi_env env, napi_callback_info info)
1610 {
1611     WMError errCode = WMError::WM_OK;
1612     size_t argc = 4;
1613     napi_value argv[4] = {nullptr};
1614     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1615     if (argc < 2 || argc > 3) { // 2: minimum param num, 3: maximum param num
1616         WLOGFE("Argc is invalid: %{public}zu", argc);
1617         errCode = WMError::WM_ERROR_INVALID_PARAM;
1618     }
1619     int32_t width = 0;
1620     if (errCode == WMError::WM_OK && !ConvertFromJsValue(env, argv[0], width)) {
1621         WLOGFE("Failed to convert parameter to width");
1622         errCode = WMError::WM_ERROR_INVALID_PARAM;
1623     }
1624     int32_t height = 0;
1625     if (errCode == WMError::WM_OK && !ConvertFromJsValue(env, argv[1], height)) {
1626         WLOGFE("Failed to convert parameter to height");
1627         errCode = WMError::WM_ERROR_INVALID_PARAM;
1628     }
1629     if (width <= 0 || height <= 0) {
1630         WLOGFE("width or height should greater than 0!");
1631         errCode = WMError::WM_ERROR_INVALID_PARAM;
1632     }
1633     wptr<Window> weakToken(windowToken_);
1634     NapiAsyncTask::CompleteCallback complete =
1635         [weakToken, errCode, width, height](napi_env env, NapiAsyncTask& task, int32_t status) {
1636             auto weakWindow = weakToken.promote();
1637             if (weakWindow == nullptr) {
1638                 WLOGFE("window is nullptr");
1639                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
1640                 return;
1641             }
1642             if (errCode != WMError::WM_OK) {
1643                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
1644                 WLOGFE("window is nullptr or get invalid param");
1645                 return;
1646             }
1647             WMError ret = weakWindow->Resize(static_cast<uint32_t>(width), static_cast<uint32_t>(height));
1648             if (ret == WMError::WM_OK) {
1649                 task.Resolve(env, NapiGetUndefined(env));
1650             } else {
1651                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window resize failed"));
1652             }
1653             WLOGFD("Window [%{public}u, %{public}s] resize end, ret = %{public}d",
1654                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1655         };
1656     // 2: params num; 2: index of callback
1657     napi_value lastParam = (argc <= 2) ? nullptr : (GetType(env, argv[2]) == napi_function ? argv[2] : nullptr);
1658     napi_value result = nullptr;
1659     NapiAsyncTask::Schedule("JsWindow::OnResize",
1660         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
1661     return result;
1662 }
1663 
OnResizeWindow(napi_env env,napi_callback_info info)1664 napi_value JsWindow::OnResizeWindow(napi_env env, napi_callback_info info)
1665 {
1666     WmErrorCode errCode = WmErrorCode::WM_OK;
1667     size_t argc = 4;
1668     napi_value argv[4] = {nullptr};
1669     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1670     if (argc < 2) { // 2: minimum param num
1671         WLOGFE("Argc is invalid: %{public}zu", argc);
1672         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1673     }
1674     int32_t width = 0;
1675     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(env, argv[0], width)) {
1676         WLOGFE("Failed to convert parameter to width");
1677         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1678     }
1679     int32_t height = 0;
1680     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(env, argv[1], height)) {
1681         WLOGFE("Failed to convert parameter to height");
1682         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1683     }
1684     if (width <= 0 || height <= 0) {
1685         WLOGFE("width or height should greater than 0!");
1686         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1687     }
1688     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
1689         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1690     }
1691 
1692     wptr<Window> weakToken(windowToken_);
1693     NapiAsyncTask::CompleteCallback complete =
1694         [weakToken, width, height](napi_env env, NapiAsyncTask& task, int32_t status) {
1695             auto weakWindow = weakToken.promote();
1696             if (weakWindow == nullptr) {
1697                 task.Reject(env,
1698                     JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
1699                 return;
1700             }
1701             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(
1702                 weakWindow->Resize(static_cast<uint32_t>(width), static_cast<uint32_t>(height)));
1703             if (ret == WmErrorCode::WM_OK) {
1704                 task.Resolve(env, NapiGetUndefined(env));
1705             } else {
1706                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window resize failed"));
1707             }
1708             WLOGI("Window [%{public}u, %{public}s] resize end, ret = %{public}d",
1709                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1710         };
1711 
1712     // 2: params num; 2: index of callback
1713     napi_value lastParam = (argc <= 2) ? nullptr :
1714         ((argv[2] != nullptr && GetType(env, argv[2]) == napi_function) ? argv[2] : nullptr);
1715     napi_value result = nullptr;
1716     NapiAsyncTask::Schedule("JsWindow::OnResizeWindow",
1717         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
1718     return result;
1719 }
1720 
SetResizeWindowAsyncTask(NapiAsyncTask::ExecuteCallback & execute,NapiAsyncTask::CompleteCallback & complete,wptr<Window> weakToken,int32_t width,int32_t height)1721 static void SetResizeWindowAsyncTask(NapiAsyncTask::ExecuteCallback& execute, NapiAsyncTask::CompleteCallback& complete,
1722     wptr<Window> weakToken, int32_t width, int32_t height)
1723 {
1724     std::shared_ptr<WmErrorCode> errCodePtr = std::make_shared<WmErrorCode>(WmErrorCode::WM_OK);
1725     execute = [weakToken, errCodePtr, width, height] {
1726         if (errCodePtr == nullptr) {
1727             return;
1728         }
1729         if (*errCodePtr != WmErrorCode::WM_OK) {
1730             return;
1731         }
1732         auto weakWindow = weakToken.promote();
1733         if (weakWindow == nullptr) {
1734             TLOGE(WmsLogTag::WMS_LAYOUT, "window is nullptr");
1735             *errCodePtr = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
1736             return;
1737         }
1738         *errCodePtr = WM_JS_TO_ERROR_CODE_MAP.at(
1739             weakWindow->ResizeAsync(static_cast<uint32_t>(width), static_cast<uint32_t>(height)));
1740         TLOGI(WmsLogTag::WMS_LAYOUT, "Window [%{public}u, %{public}s] resize end, err = %{public}d",
1741             weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), *errCodePtr);
1742     };
1743     complete = [weakToken, errCodePtr](napi_env env, NapiAsyncTask& task, int32_t status) {
1744         if (errCodePtr == nullptr) {
1745             task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
1746             return;
1747         }
1748         if (*errCodePtr == WmErrorCode::WM_OK) {
1749             task.Resolve(env, NapiGetUndefined(env));
1750         } else {
1751             task.Reject(env, JsErrUtils::CreateJsError(env, *errCodePtr, "JsWindow::OnResizeWindowAsync failed"));
1752         }
1753     };
1754 }
1755 
OnResizeWindowAsync(napi_env env,napi_callback_info info)1756 napi_value JsWindow::OnResizeWindowAsync(napi_env env, napi_callback_info info)
1757 {
1758     WmErrorCode errCode = WmErrorCode::WM_OK;
1759     size_t argc = 4; // 4: number of arg
1760     napi_value argv[4] = {nullptr};
1761     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1762     if (argc < 2) { // 2: minimum param num
1763         WLOGFE("Argc is invalid: %{public}zu", argc);
1764         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1765     }
1766     int32_t width = 0;
1767     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(env, argv[0], width)) {
1768         WLOGFE("Failed to convert parameter to width");
1769         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1770     }
1771     int32_t height = 0;
1772     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(env, argv[1], height)) {
1773         WLOGFE("Failed to convert parameter to height");
1774         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1775     }
1776     if (width <= 0 || height <= 0) {
1777         WLOGFE("width or height should greater than 0!");
1778         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1779     }
1780     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
1781         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1782     }
1783 
1784     wptr<Window> weakToken(windowToken_);
1785     NapiAsyncTask::ExecuteCallback execute;
1786     NapiAsyncTask::CompleteCallback complete;
1787     SetResizeWindowAsyncTask(execute, complete, weakToken, width, height);
1788 
1789     // 2: params num; 2: index of callback
1790     napi_value lastParam = (argc <= 2) ? nullptr :
1791         ((argv[2] != nullptr && GetType(env, argv[2]) == napi_function) ? argv[2] : nullptr);
1792     napi_value result = nullptr;
1793     NapiAsyncTask::Schedule("JsWindow::OnResizeWindowAsync",
1794         env, CreateAsyncTaskWithLastParam(env, lastParam, std::move(execute), std::move(complete), &result));
1795     return result;
1796 }
1797 
OnSetWindowType(napi_env env,napi_callback_info info)1798 napi_value JsWindow::OnSetWindowType(napi_env env, napi_callback_info info)
1799 {
1800     WMError errCode = WMError::WM_OK;
1801     size_t argc = 4;
1802     napi_value argv[4] = {nullptr};
1803     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1804     if (argc < 1 || argc > 2) { // 2 is max num of argc
1805         WLOGFE("Argc is invalid: %{public}zu", argc);
1806         errCode = WMError::WM_ERROR_INVALID_PARAM;
1807     }
1808     WindowType winType = WindowType::SYSTEM_WINDOW_BASE;
1809     uint32_t resultValue = 0;
1810     if (errCode == WMError::WM_OK && !ConvertFromJsValue(env, argv[0], resultValue)) {
1811         WLOGFE("Failed to convert parameter to windowType");
1812         errCode = WMError::WM_ERROR_INVALID_PARAM;
1813     }
1814     if (resultValue >= static_cast<uint32_t>(WindowType::SYSTEM_WINDOW_BASE) &&
1815         resultValue <= static_cast<uint32_t>(WindowType::SYSTEM_WINDOW_END)) {
1816         winType = static_cast<WindowType>(resultValue); // adapt to the old version
1817     } else if (JS_TO_NATIVE_WINDOW_TYPE_MAP.count(static_cast<ApiWindowType>(resultValue)) != 0) {
1818         winType = JS_TO_NATIVE_WINDOW_TYPE_MAP.at(static_cast<ApiWindowType>(resultValue));
1819     } else {
1820         WLOGFE("Do not support this type: %{public}u", resultValue);
1821         errCode = WMError::WM_ERROR_INVALID_PARAM;
1822     }
1823 
1824     wptr<Window> weakToken(windowToken_);
1825     NapiAsyncTask::CompleteCallback complete =
1826         [weakToken, winType, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
1827             auto weakWindow = weakToken.promote();
1828             if (weakWindow == nullptr) {
1829                 WLOGFE("window is nullptr");
1830                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
1831                 return;
1832             }
1833             if (errCode != WMError::WM_OK) {
1834                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
1835                 WLOGFE("get invalid param");
1836                 return;
1837             }
1838             WMError ret = weakWindow->SetWindowType(winType);
1839             if (ret == WMError::WM_OK) {
1840                 task.Resolve(env, NapiGetUndefined(env));
1841             } else {
1842                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window set type failed"));
1843             }
1844             WLOGI("Window [%{public}u, %{public}s] set type end, ret = %{public}d",
1845                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1846         };
1847 
1848     napi_value lastParam = (argc <= 1) ? nullptr :
1849         (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
1850     napi_value result = nullptr;
1851     NapiAsyncTask::Schedule("JsWindow::OnSetWindowType",
1852         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
1853     return result;
1854 }
1855 
OnSetWindowMode(napi_env env,napi_callback_info info)1856 napi_value JsWindow::OnSetWindowMode(napi_env env, napi_callback_info info)
1857 {
1858     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
1859         WLOGFE("set window mode permission denied!");
1860         return NapiThrowError(env, WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
1861     }
1862     WmErrorCode errCode = WmErrorCode::WM_OK;
1863     size_t argc = 4;
1864     napi_value argv[4] = {nullptr};
1865     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1866     if (argc < 1) {
1867         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1868     }
1869     WindowMode winMode = WindowMode::WINDOW_MODE_FULLSCREEN;
1870     if (errCode == WmErrorCode::WM_OK) {
1871         napi_value nativeMode = argv[0];
1872         if (nativeMode == nullptr) {
1873             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1874         } else {
1875             uint32_t resultValue = 0;
1876             napi_get_value_uint32(env, nativeMode, &resultValue);
1877             if (resultValue >= static_cast<uint32_t>(WindowMode::WINDOW_MODE_SPLIT_PRIMARY)) {
1878                 winMode = static_cast<WindowMode>(resultValue);
1879             } else if (resultValue >= static_cast<uint32_t>(ApiWindowMode::UNDEFINED) &&
1880                 resultValue <= static_cast<uint32_t>(ApiWindowMode::MODE_END)) {
1881                 winMode = JS_TO_NATIVE_WINDOW_MODE_MAP.at(
1882                     static_cast<ApiWindowMode>(resultValue));
1883             } else {
1884                 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1885             }
1886         }
1887     }
1888     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
1889         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1890     }
1891     wptr<Window> weakToken(windowToken_);
1892     NapiAsyncTask::CompleteCallback complete =
1893         [weakToken, winMode](napi_env env, NapiAsyncTask& task, int32_t status) {
1894             auto weakWindow = weakToken.promote();
1895             if (weakWindow == nullptr) {
1896                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
1897                 return;
1898             }
1899             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetWindowMode(winMode));
1900             if (ret == WmErrorCode::WM_OK) {
1901                 task.Resolve(env, NapiGetUndefined(env));
1902             } else {
1903                 task.Reject(env,
1904                     JsErrUtils::CreateJsError(env, ret, "Window set mode failed"));
1905             }
1906             WLOGI("Window [%{public}u, %{public}s] set type end, ret = %{public}d",
1907                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1908         };
1909 
1910     napi_value lastParam = (argc == 1) ? nullptr :
1911         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
1912     napi_value result = nullptr;
1913     NapiAsyncTask::Schedule("JsWindow::OnSetWindowMode",
1914         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
1915     return result;
1916 }
1917 
OnGetProperties(napi_env env,napi_callback_info info)1918 napi_value JsWindow::OnGetProperties(napi_env env, napi_callback_info info)
1919 {
1920     WMError errCode = WMError::WM_OK;
1921     size_t argc = 4;
1922     napi_value argv[4] = {nullptr};
1923     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1924     if (argc > 1) {
1925         WLOGFE("Argc is invalid: %{public}zu", argc);
1926         errCode = WMError::WM_ERROR_INVALID_PARAM;
1927     }
1928     wptr<Window> weakToken(windowToken_);
1929     NapiAsyncTask::CompleteCallback complete =
1930         [weakToken, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
1931             auto weakWindow = weakToken.promote();
1932             if (weakWindow == nullptr) {
1933                 WLOGFE("window is nullptr");
1934                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
1935                 return;
1936             }
1937             if (errCode != WMError::WM_OK) {
1938                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
1939                 WLOGFE("window is nullptr or get invalid param");
1940                 return;
1941             }
1942             Rect drawableRect = g_emptyRect;
1943             auto uicontent = weakWindow->GetUIContent();
1944             if (uicontent == nullptr) {
1945                 WLOGFW("uicontent is nullptr");
1946             } else {
1947                 uicontent->GetAppPaintSize(drawableRect);
1948             }
1949             auto objValue = CreateJsWindowPropertiesObject(env, weakWindow, drawableRect);
1950             if (objValue != nullptr) {
1951                 task.Resolve(env, objValue);
1952             } else {
1953                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR,
1954                     "Window get properties failed"));
1955             }
1956             WLOGFD("Window [%{public}u, %{public}s] get properties end",
1957                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
1958         };
1959 
1960     napi_value lastParam = (argc == 0) ? nullptr : (GetType(env, argv[0]) == napi_function ? argv[0] : nullptr);
1961     napi_value result = nullptr;
1962     NapiAsyncTask::Schedule("JsWindow::OnGetProperties",
1963         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
1964     return result;
1965 }
1966 
OnGetWindowPropertiesSync(napi_env env,napi_callback_info info)1967 napi_value JsWindow::OnGetWindowPropertiesSync(napi_env env, napi_callback_info info)
1968 {
1969     wptr<Window> weakToken(windowToken_);
1970     auto window = weakToken.promote();
1971     if (window == nullptr) {
1972         WLOGFW("window is nullptr or get invalid param");
1973         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1974     }
1975     Rect drawableRect = g_emptyRect;
1976     auto uicontent = window->GetUIContent();
1977     if (uicontent == nullptr) {
1978         WLOGFW("uicontent is nullptr");
1979     } else {
1980         uicontent->GetWindowPaintSize(drawableRect);
1981     }
1982     auto objValue = CreateJsWindowPropertiesObject(env, window, drawableRect);
1983     WLOGI("Window [%{public}u, %{public}s] get properties end",
1984         window->GetWindowId(), window->GetWindowName().c_str());
1985     if (objValue != nullptr) {
1986         return objValue;
1987     } else {
1988         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1989     }
1990 }
1991 
NapiIsCallable(napi_env env,napi_value value)1992 bool NapiIsCallable(napi_env env, napi_value value)
1993 {
1994     bool result = false;
1995     napi_is_callable(env, value, &result);
1996     return result;
1997 }
1998 
OnRegisterWindowCallback(napi_env env,napi_callback_info info)1999 napi_value JsWindow::OnRegisterWindowCallback(napi_env env, napi_callback_info info)
2000 {
2001     if (windowToken_ == nullptr) {
2002         WLOGFE("Window is nullptr");
2003         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
2004     }
2005     sptr<Window> windowToken = windowToken_;
2006     constexpr size_t argcMin = 2;
2007     constexpr size_t argcMax = 3;
2008     size_t argc = 4;
2009     napi_value argv[4] = {nullptr};
2010     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2011     if (argc < argcMin || argc > argcMax) {
2012         WLOGFE("Argc is invalid: %{public}zu", argc);
2013         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2014     }
2015     std::string cbType;
2016     if (!ConvertFromJsValue(env, argv[0], cbType)) {
2017         WLOGFE("Failed to convert parameter to callbackType");
2018         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2019     }
2020     size_t cbIndex = argc - 1;
2021     napi_value callback = argv[cbIndex];
2022     if (!NapiIsCallable(env, callback)) {
2023         WLOGI("Callback(info->argv[%{public}zu]) is not callable", cbIndex);
2024         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2025     }
2026 
2027     napi_value parameter = nullptr;
2028     if (argc > argcMin) {
2029         parameter = argv[cbIndex - 1];
2030     }
2031 
2032     WmErrorCode ret = registerManager_->RegisterListener(windowToken, cbType, CaseType::CASE_WINDOW,
2033         env, callback, parameter);
2034     if (ret != WmErrorCode::WM_OK) {
2035         return NapiThrowError(env, ret);
2036     }
2037     WLOGI("Register end, window [%{public}u, %{public}s], type = %{public}s",
2038         windowToken->GetWindowId(), windowToken->GetWindowName().c_str(), cbType.c_str());
2039     return NapiGetUndefined(env);
2040 }
2041 
OnUnregisterWindowCallback(napi_env env,napi_callback_info info)2042 napi_value JsWindow::OnUnregisterWindowCallback(napi_env env, napi_callback_info info)
2043 {
2044     if (windowToken_ == nullptr) {
2045         WLOGFE("Window is nullptr");
2046         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
2047     }
2048     size_t argc = 4;
2049     napi_value argv[4] = {nullptr};
2050     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2051     if (argc < 1) { // 2: maximum params nums
2052         WLOGFE("Argc is invalid: %{public}zu", argc);
2053         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2054     }
2055     std::string cbType;
2056     if (!ConvertFromJsValue(env, argv[0], cbType)) {
2057         WLOGFE("Failed to convert parameter to callbackType");
2058         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2059     }
2060 
2061     napi_value value = nullptr;
2062     WmErrorCode ret = WmErrorCode::WM_OK;
2063     if (argc == 1) {
2064         ret = registerManager_->UnregisterListener(windowToken_, cbType, CaseType::CASE_WINDOW, env, value);
2065     } else {
2066         value = argv[1];
2067         if (value == nullptr || !NapiIsCallable(env, value)) {
2068             ret = registerManager_->UnregisterListener(windowToken_, cbType, CaseType::CASE_WINDOW, env, nullptr);
2069         } else {
2070             ret = registerManager_->UnregisterListener(windowToken_, cbType, CaseType::CASE_WINDOW, env, value);
2071         }
2072     }
2073 
2074     if (ret != WmErrorCode::WM_OK) {
2075         return NapiThrowError(env, ret);
2076     }
2077     WLOGI("Unregister end, window [%{public}u, %{public}s], type = %{public}s",
2078         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), cbType.c_str());
2079     return NapiGetUndefined(env);
2080 }
2081 
GetBindDialogToken(napi_env env,napi_value argv0)2082 static sptr<IRemoteObject> GetBindDialogToken(napi_env env, napi_value argv0)
2083 {
2084     sptr<IRemoteObject> token = NAPI_ohos_rpc_getNativeRemoteObject(env, argv0);
2085     if (token != nullptr) {
2086         return token;
2087     }
2088     std::shared_ptr<AbilityRuntime::RequestInfo> requestInfo =
2089         AbilityRuntime::RequestInfo::UnwrapRequestInfo(env, argv0);
2090     return (requestInfo != nullptr) ? requestInfo->GetToken() : nullptr;
2091 }
2092 
OnBindDialogTarget(napi_env env,napi_callback_info info)2093 napi_value JsWindow::OnBindDialogTarget(napi_env env, napi_callback_info info)
2094 {
2095     if (windowToken_ == nullptr) {
2096         TLOGE(WmsLogTag::WMS_DIALOG, "window is nullptr!");
2097         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
2098     }
2099     if (!Permission::IsSystemCalling()) {
2100         TLOGE(WmsLogTag::WMS_DIALOG, "permission denied, require system application!");
2101         return NapiThrowError(env, WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
2102     }
2103 
2104     size_t argc = 4;
2105     napi_value argv[4] = {nullptr};
2106     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2107 
2108     if (argc < 2) { // at least 2 params
2109         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2110     }
2111     sptr<IRemoteObject> token = GetBindDialogToken(env, argv[0]);
2112     if (token == nullptr) {
2113         TLOGE(WmsLogTag::WMS_DIALOG, "token is null!");
2114         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2115     }
2116     napi_value value = argv[1];
2117     if (value == nullptr || !NapiIsCallable(env, value)) {
2118         registerManager_->RegisterListener(windowToken_,
2119             "dialogDeathRecipient", CaseType::CASE_WINDOW, env, nullptr);
2120     } else {
2121         registerManager_->RegisterListener(windowToken_, "dialogDeathRecipient", CaseType::CASE_WINDOW, env, value);
2122     }
2123 
2124     wptr<Window> weakToken(windowToken_);
2125     NapiAsyncTask::CompleteCallback complete =
2126         [weakToken, token](napi_env env, NapiAsyncTask& task, int32_t status) mutable {
2127             auto weakWindow = weakToken.promote();
2128             if (weakWindow == nullptr) {
2129                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
2130                 return;
2131             }
2132 
2133             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->BindDialogTarget(token));
2134             if (ret == WmErrorCode::WM_OK) {
2135                 task.Resolve(env, NapiGetUndefined(env));
2136             } else {
2137                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Bind Dialog Target failed"));
2138             }
2139 
2140             WLOGI("BindDialogTarget end, window [%{public}u, %{public}s]",
2141                 weakToken->GetWindowId(), weakToken->GetWindowName().c_str());
2142     };
2143 
2144     napi_value result = nullptr;
2145     napi_value lastParam = (argc == 2) ? nullptr : (GetType(env, argv[2]) == napi_function ? argv[2] : nullptr);
2146     NapiAsyncTask::Schedule("JsWindow::OnBindDialogTarget",
2147         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
2148     return result;
2149 }
2150 
OnSetDialogBackGestureEnabled(napi_env env,napi_callback_info info)2151 napi_value JsWindow::OnSetDialogBackGestureEnabled(napi_env env, napi_callback_info info)
2152 {
2153     size_t argc = 4;
2154     napi_value argv[4] = {nullptr};
2155     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2156     if (argc < 1) { // at least 1 params
2157         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2158     }
2159 
2160     napi_value nativeVal = argv[0];
2161     if (nativeVal == nullptr) {
2162         TLOGE(WmsLogTag::WMS_DIALOG, "Failed to convert parameter to enable");
2163         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2164     }
2165     bool isEnabled = false;
2166     napi_status retCode = napi_get_value_bool(env, nativeVal, &isEnabled);
2167     if (retCode != napi_ok) {
2168         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2169     }
2170 
2171     wptr<Window> weakToken(windowToken_);
2172     std::shared_ptr<WmErrorCode> errCodePtr = std::make_shared<WmErrorCode>(WmErrorCode::WM_OK);
2173     NapiAsyncTask::ExecuteCallback execute = [weakToken, isEnabled, errCodePtr]() {
2174         if (errCodePtr == nullptr) {
2175             return;
2176         }
2177         auto window = weakToken.promote();
2178         if (window == nullptr) {
2179             *errCodePtr = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
2180             return;
2181         }
2182         *errCodePtr = WM_JS_TO_ERROR_CODE_MAP.at(window->SetDialogBackGestureEnabled(isEnabled));
2183         TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] set dialog window end",
2184             window->GetWindowId(), window->GetWindowName().c_str());
2185     };
2186     NapiAsyncTask::CompleteCallback complete =
2187         [weakToken, errCodePtr](napi_env env, NapiAsyncTask& task, int32_t status) {
2188             if (errCodePtr == nullptr) {
2189                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
2190                 return;
2191             }
2192             if (*errCodePtr == WmErrorCode::WM_OK) {
2193                 task.Resolve(env, NapiGetUndefined(env));
2194             } else {
2195                 task.Reject(env, JsErrUtils::CreateJsError(env, *errCodePtr, "Set dialog window failed"));
2196             }
2197         };
2198     napi_value result = nullptr;
2199     NapiAsyncTask::Schedule("JsWindow::OnSetTopmost",
2200         env, CreateAsyncTaskWithLastParam(env, nullptr, std::move(execute), std::move(complete), &result));
2201     return result;
2202 }
2203 
LoadContentTask(std::shared_ptr<NativeReference> contentStorage,std::string contextUrl,sptr<Window> weakWindow,napi_env env,NapiAsyncTask & task,bool isLoadedByName)2204 static void LoadContentTask(std::shared_ptr<NativeReference> contentStorage, std::string contextUrl,
2205     sptr<Window> weakWindow, napi_env env, NapiAsyncTask& task, bool isLoadedByName)
2206 {
2207     napi_value nativeStorage =  (contentStorage == nullptr) ? nullptr : contentStorage->GetNapiValue();
2208     AppExecFwk::Ability* ability = nullptr;
2209     GetAPI7Ability(env, ability);
2210     WmErrorCode ret;
2211     if (isLoadedByName) {
2212         ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetUIContentByName(contextUrl, env, nativeStorage, ability));
2213     } else {
2214         ret = WM_JS_TO_ERROR_CODE_MAP.at(
2215             weakWindow->NapiSetUIContent(contextUrl, env, nativeStorage, false, nullptr, ability));
2216     }
2217     if (ret == WmErrorCode::WM_OK) {
2218         task.Resolve(env, NapiGetUndefined(env));
2219     } else {
2220         task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window load content failed"));
2221     }
2222     WLOGFI("[NAPI]Window [%{public}u, %{public}s] load content end, ret = %{public}d",
2223         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
2224     return;
2225 }
2226 
LoadContentScheduleOld(napi_env env,napi_callback_info info,bool isLoadedByName)2227 napi_value JsWindow::LoadContentScheduleOld(napi_env env, napi_callback_info info, bool isLoadedByName)
2228 {
2229     WMError errCode = WMError::WM_OK;
2230     size_t argc = 4;
2231     napi_value argv[4] = {nullptr};
2232     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2233     if (argc < 1 || argc > 2) { // 2 maximum param num
2234         WLOGFE("Argc is invalid: %{public}zu", argc);
2235         errCode = WMError::WM_ERROR_INVALID_PARAM;
2236     }
2237     std::string contextUrl;
2238     if (errCode == WMError::WM_OK && !ConvertFromJsValue(env, argv[0], contextUrl)) {
2239         WLOGFE("Failed to convert parameter to context url");
2240         errCode = WMError::WM_ERROR_INVALID_PARAM;
2241     }
2242     napi_value callBack = nullptr;
2243     if (argc == 2) { // 2 param num
2244         callBack = argv[1];
2245     }
2246     std::shared_ptr<NativeReference> contentStorage = nullptr;
2247     wptr<Window> weakToken(windowToken_);
2248     NapiAsyncTask::CompleteCallback complete = [weakToken, contentStorage, contextUrl, errCode, isLoadedByName](
2249                                                napi_env env, NapiAsyncTask& task, int32_t status) {
2250         auto weakWindow = weakToken.promote();
2251         if (weakWindow == nullptr) {
2252             WLOGFE("window is nullptr");
2253             task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
2254             return;
2255         }
2256         if (errCode != WMError::WM_OK) {
2257             task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
2258             WLOGFE("Window is nullptr or get invalid param");
2259             return;
2260         }
2261         LoadContentTask(contentStorage, contextUrl, weakWindow, env, task, isLoadedByName);
2262     };
2263     napi_value result = nullptr;
2264     NapiAsyncTask::Schedule("JsWindow::OnLoadContent",
2265         env, CreateAsyncTaskWithLastParam(env, callBack, nullptr, std::move(complete), &result));
2266     return result;
2267 }
2268 
LoadContentScheduleNew(napi_env env,napi_callback_info info,bool isLoadedByName)2269 napi_value JsWindow::LoadContentScheduleNew(napi_env env, napi_callback_info info, bool isLoadedByName)
2270 {
2271     WmErrorCode errCode = WmErrorCode::WM_OK;
2272     size_t argc = 4;
2273     napi_value argv[4] = {nullptr};
2274     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2275     if (argc < 2) { // 2 param num
2276         WLOGFE("Argc is invalid: %{public}zu", argc);
2277         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2278     }
2279     std::string contextUrl;
2280     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(env, argv[0], contextUrl)) {
2281         WLOGFE("Failed to convert parameter to context url");
2282         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2283     }
2284     napi_value storage = nullptr;
2285     napi_value callBack = nullptr;
2286     if (argc == 2) { // 2: num of params
2287         storage = argv[1];
2288     } else if (argc >= 3) { // 3: num of params
2289         storage = argv[1];
2290         callBack = ((argv[2] != nullptr && GetType(env, argv[2]) == napi_function) ? // 2 param num
2291             argv[2] : nullptr); // 2 param num
2292     }
2293     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
2294         WLOGFE("Window is nullptr or get invalid param");
2295         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2296     }
2297     std::shared_ptr<NativeReference> contentStorage = nullptr;
2298     if (storage != nullptr) {
2299         napi_ref result = nullptr;
2300         napi_create_reference(env, storage, 1, &result);
2301         contentStorage = std::shared_ptr<NativeReference>(reinterpret_cast<NativeReference*>(result));
2302     }
2303     wptr<Window> weakToken(windowToken_);
2304     NapiAsyncTask::CompleteCallback complete = [weakToken, contentStorage, contextUrl, isLoadedByName](
2305                                                napi_env env, NapiAsyncTask& task, int32_t status) {
2306         auto weakWindow = weakToken.promote();
2307         if (weakWindow == nullptr) {
2308             WLOGFE("Window is nullptr or get invalid param");
2309             task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
2310             return;
2311         }
2312         LoadContentTask(contentStorage, contextUrl, weakWindow, env, task, isLoadedByName);
2313     };
2314     napi_value result = nullptr;
2315     NapiAsyncTask::Schedule("JsWindow::OnLoadContent",
2316         env, CreateAsyncTaskWithLastParam(env, callBack, nullptr, std::move(complete), &result));
2317     return result;
2318 }
2319 
OnLoadContent(napi_env env,napi_callback_info info,bool isLoadedByName)2320 napi_value JsWindow::OnLoadContent(napi_env env, napi_callback_info info, bool isLoadedByName)
2321 {
2322     bool oldApi = false;
2323     size_t argc = 4;
2324     napi_value argv[4] = {nullptr};
2325     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2326     if (argc == 1) {
2327         oldApi = true;
2328     } else if (argc == 2) { // 2 param num
2329         napi_value value = argv[1];
2330         if (value== nullptr || GetType(env, value) != napi_function) {
2331             oldApi = false;
2332         } else {
2333             oldApi = true;
2334         }
2335     }
2336     if (oldApi) {
2337         return LoadContentScheduleOld(env, info, isLoadedByName);
2338     } else {
2339         return LoadContentScheduleNew(env, info, isLoadedByName);
2340     }
2341 }
2342 
OnGetUIContext(napi_env env,napi_callback_info info)2343 napi_value JsWindow::OnGetUIContext(napi_env env, napi_callback_info info)
2344 {
2345     size_t argc = 4;
2346     napi_value argv[4] = {nullptr};
2347     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2348     if (argc >= 1) {
2349         WLOGFE("Argc is invalid: %{public}zu, expect zero params", argc);
2350         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2351     }
2352 
2353     wptr<Window> weakToken(windowToken_);
2354     auto window = weakToken.promote();
2355     if (window == nullptr) {
2356         WLOGFE("window is nullptr");
2357         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
2358     }
2359 
2360     auto uicontent = window->GetUIContent();
2361     if (uicontent == nullptr) {
2362         WLOGFW("uicontent is nullptr");
2363         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
2364     }
2365 
2366     napi_value uiContext = uicontent->GetUINapiContext();
2367     if (uiContext == nullptr) {
2368         WLOGFE("uiContext obtained from jsEngine is nullptr");
2369         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
2370     } else {
2371         return uiContext;
2372     }
2373 }
2374 
OnSetUIContent(napi_env env,napi_callback_info info)2375 napi_value JsWindow::OnSetUIContent(napi_env env, napi_callback_info info)
2376 {
2377     WmErrorCode errCode = WmErrorCode::WM_OK;
2378     size_t argc = 4;
2379     napi_value argv[4] = {nullptr};
2380     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2381     if (argc < 1) { // 2 maximum param num
2382         WLOGFE("Argc is invalid: %{public}zu", argc);
2383         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2384     }
2385     std::string contextUrl;
2386     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(env, argv[0], contextUrl)) {
2387         WLOGFE("Failed to convert parameter to context url");
2388         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2389     }
2390     napi_value callBack = nullptr;
2391     if (argc >= 2) { // 2 param num
2392         callBack = argv[1];
2393     }
2394     std::shared_ptr<NativeReference> contentStorage = nullptr;
2395     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
2396         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2397     }
2398 
2399     wptr<Window> weakToken(windowToken_);
2400     NapiAsyncTask::CompleteCallback complete =
2401         [weakToken, contentStorage, contextUrl](napi_env env, NapiAsyncTask& task, int32_t status) {
2402             auto weakWindow = weakToken.promote();
2403             if (weakWindow == nullptr) {
2404                 WLOGFE("Window is nullptr");
2405                 task.Reject(env,
2406                     JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
2407                 return;
2408             }
2409             LoadContentTask(contentStorage, contextUrl, weakWindow, env, task, false);
2410         };
2411     napi_value result = nullptr;
2412     NapiAsyncTask::Schedule("JsWindow::OnSetUIContent",
2413         env, CreateAsyncTaskWithLastParam(env, callBack, nullptr, std::move(complete), &result));
2414     return result;
2415 }
2416 
OnSetFullScreen(napi_env env,napi_callback_info info)2417 napi_value JsWindow::OnSetFullScreen(napi_env env, napi_callback_info info)
2418 {
2419     WMError errCode = WMError::WM_OK;
2420     size_t argc = 4;
2421     napi_value argv[4] = {nullptr};
2422     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2423     if (argc < 1 || argc > 2) { // 2: maximum params num
2424         TLOGE(WmsLogTag::WMS_IMMS, "Argc is invalid: %{public}zu", argc);
2425         errCode = WMError::WM_ERROR_INVALID_PARAM;
2426     }
2427     bool isFullScreen = false;
2428     if (errCode == WMError::WM_OK) {
2429         napi_value nativeVal = argv[0];
2430         if (nativeVal == nullptr) {
2431             TLOGE(WmsLogTag::WMS_IMMS, "Failed to convert parameter to isFullScreen");
2432             errCode = WMError::WM_ERROR_INVALID_PARAM;
2433         } else {
2434             CHECK_NAPI_RETCODE(errCode, WMError::WM_ERROR_INVALID_PARAM,
2435                 napi_get_value_bool(env, nativeVal, &isFullScreen));
2436         }
2437     }
2438 
2439     wptr<Window> weakToken(windowToken_);
2440     NapiAsyncTask::CompleteCallback complete =
2441         [weakToken, isFullScreen, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
2442             auto weakWindow = weakToken.promote();
2443             if (weakWindow == nullptr) {
2444                 TLOGE(WmsLogTag::WMS_IMMS, "window is nullptr");
2445                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
2446                 return;
2447             }
2448             if (errCode != WMError::WM_OK) {
2449                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode, "Invalidate params."));
2450                 return;
2451             }
2452             WMError ret = weakWindow->SetFullScreen(isFullScreen);
2453             if (ret == WMError::WM_OK) {
2454                 task.Resolve(env, NapiGetUndefined(env));
2455             } else {
2456                 TLOGE(WmsLogTag::WMS_IMMS, "SetFullScreen failed, ret = %{public}d", ret);
2457                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window SetFullScreen failed."));
2458             }
2459         };
2460 
2461     napi_value lastParam = (argc <= 1) ? nullptr : (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
2462     napi_value result = nullptr;
2463     NapiAsyncTask::Schedule("JsWindow::OnSetFullScreen",
2464         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
2465     return result;
2466 }
2467 
OnSetLayoutFullScreen(napi_env env,napi_callback_info info)2468 napi_value JsWindow::OnSetLayoutFullScreen(napi_env env, napi_callback_info info)
2469 {
2470     WMError errCode = WMError::WM_OK;
2471     size_t argc = 4;
2472     napi_value argv[4] = {nullptr};
2473     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2474     if (argc < 1 || argc > 2) { // 2: maximum params num
2475         TLOGE(WmsLogTag::WMS_IMMS, "Argc is invalid: %{public}zu", argc);
2476         errCode = WMError::WM_ERROR_INVALID_PARAM;
2477     }
2478     bool isLayoutFullScreen = false;
2479     if (errCode == WMError::WM_OK) {
2480         napi_value nativeVal = argv[0];
2481         if (nativeVal == nullptr) {
2482             TLOGE(WmsLogTag::WMS_IMMS, "Failed to convert parameter to isLayoutFullScreen");
2483             errCode = WMError::WM_ERROR_INVALID_PARAM;
2484         } else {
2485             CHECK_NAPI_RETCODE(errCode, WMError::WM_ERROR_INVALID_PARAM,
2486                 napi_get_value_bool(env, nativeVal, &isLayoutFullScreen));
2487         }
2488     }
2489     wptr<Window> weakToken(windowToken_);
2490     NapiAsyncTask::CompleteCallback complete =
2491         [weakToken, isLayoutFullScreen, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
2492             auto weakWindow = weakToken.promote();
2493             if (weakWindow == nullptr) {
2494                 TLOGE(WmsLogTag::WMS_IMMS, "window is nullptr");
2495                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
2496                 return;
2497             }
2498             if (errCode != WMError::WM_OK) {
2499                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode, "Invalidate params."));
2500                 return;
2501             }
2502             WMError ret = weakWindow->SetLayoutFullScreen(isLayoutFullScreen);
2503             if (ret == WMError::WM_OK) {
2504                 task.Resolve(env, NapiGetUndefined(env));
2505             } else {
2506                 TLOGE(WmsLogTag::WMS_IMMS, "SetLayoutFullScreen failed, ret = %{public}d", ret);
2507                 task.Reject(env, JsErrUtils::CreateJsError(env,
2508                     ret, "Window OnSetLayoutFullScreen failed."));
2509             }
2510         };
2511     napi_value lastParam = (argc <= 1) ? nullptr : (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
2512     napi_value result = nullptr;
2513     NapiAsyncTask::Schedule("JsWindow::OnSetLayoutFullScreen",
2514         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
2515     return result;
2516 }
2517 
OnSetWindowLayoutFullScreen(napi_env env,napi_callback_info info)2518 napi_value JsWindow::OnSetWindowLayoutFullScreen(napi_env env, napi_callback_info info)
2519 {
2520     WmErrorCode errCode = WmErrorCode::WM_OK;
2521     size_t argc = 4;
2522     napi_value argv[4] = {nullptr};
2523     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2524     if (argc < 1) { // 1: params num
2525         TLOGE(WmsLogTag::WMS_IMMS, "Argc is invalid: %{public}zu", argc);
2526         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2527     }
2528     bool isLayoutFullScreen = false;
2529     if (errCode == WmErrorCode::WM_OK) {
2530         napi_value nativeVal = argv[0];
2531         if (nativeVal == nullptr) {
2532             TLOGE(WmsLogTag::WMS_IMMS, "Failed to convert parameter to isLayoutFullScreen");
2533             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2534         } else {
2535             CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
2536                 napi_get_value_bool(env, nativeVal, &isLayoutFullScreen));
2537         }
2538     }
2539     if (errCode != WmErrorCode::WM_OK) {
2540         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2541     }
2542 
2543     wptr<Window> weakToken(windowToken_);
2544     NapiAsyncTask::CompleteCallback complete =
2545         [weakToken, isLayoutFullScreen](napi_env env, NapiAsyncTask& task, int32_t status) {
2546             auto weakWindow = weakToken.promote();
2547             if (weakWindow == nullptr) {
2548                 TLOGE(WmsLogTag::WMS_IMMS, "window is nullptr");
2549                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY,
2550                     "Invalidate params."));
2551                 return;
2552             }
2553             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetLayoutFullScreen(isLayoutFullScreen));
2554             if (ret == WmErrorCode::WM_OK) {
2555                 task.Resolve(env, NapiGetUndefined(env));
2556             } else {
2557                 TLOGE(WmsLogTag::WMS_IMMS, "SetWindowLayoutFullScreen failed, ret = %{public}d", ret);
2558                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window OnSetLayoutFullScreen failed."));
2559             }
2560         };
2561 
2562     napi_value lastParam = (argc <= 1) ? nullptr :
2563         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
2564     napi_value result = nullptr;
2565     auto asyncTask = CreateAsyncTask(env, lastParam, nullptr,
2566         std::make_unique<NapiAsyncTask::CompleteCallback>(std::move(complete)), &result);
2567     NapiAsyncTask::Schedule("JsWindow::OnSetWindowLayoutFullScreen", env, std::move(asyncTask));
2568     return result;
2569 }
2570 
SetSystemBarPropertiesByFlags(std::map<WindowType,SystemBarPropertyFlag> & systemBarPropertyFlags,std::map<WindowType,SystemBarProperty> & systemBarProperties,sptr<Window> windowToken)2571 static WMError SetSystemBarPropertiesByFlags(std::map<WindowType, SystemBarPropertyFlag>& systemBarPropertyFlags,
2572     std::map<WindowType, SystemBarProperty>& systemBarProperties, sptr<Window> windowToken)
2573 {
2574     WMError ret = WMError::WM_OK;
2575     WMError err = WMError::WM_OK;
2576 
2577     for (auto it : systemBarPropertyFlags) {
2578         WindowType type = it.first;
2579         SystemBarPropertyFlag flag = it.second;
2580         if (flag.enableFlag || flag.backgroundColorFlag || flag.contentColorFlag || flag.enableAnimationFlag) {
2581             err = windowToken->SetSystemBarProperty(type, systemBarProperties.at(type));
2582             if (err != WMError::WM_OK) {
2583                 TLOGE(WmsLogTag::WMS_IMMS, "SetSystemBarProperty failed, ret = %{public}d", err);
2584                 ret = err;
2585             }
2586         }
2587     }
2588 
2589     return ret;
2590 }
2591 
SetSystemBarEnableTask(NapiAsyncTask::ExecuteCallback & execute,NapiAsyncTask::CompleteCallback & complete,wptr<Window> weakToken,std::map<WindowType,SystemBarProperty> & systemBarProperties,std::map<WindowType,SystemBarPropertyFlag> & systemBarPropertyFlags,std::shared_ptr<WMError> errCodePtr)2592 void SetSystemBarEnableTask(NapiAsyncTask::ExecuteCallback& execute, NapiAsyncTask::CompleteCallback& complete,
2593     wptr<Window> weakToken, std::map<WindowType, SystemBarProperty>& systemBarProperties,
2594     std::map<WindowType, SystemBarPropertyFlag>& systemBarPropertyFlags, std::shared_ptr<WMError> errCodePtr)
2595 {
2596     execute = [weakToken, systemBarProperties, systemBarPropertyFlags, errCodePtr]() mutable {
2597         if (errCodePtr == nullptr) {
2598             return;
2599         }
2600         if (*errCodePtr != WMError::WM_OK) {
2601             return;
2602         }
2603         auto spWindow = weakToken.promote();
2604         if (spWindow == nullptr) {
2605             *errCodePtr = WMError::WM_ERROR_NULLPTR;
2606             return;
2607         }
2608         UpdateSystemBarProperties(systemBarProperties, systemBarPropertyFlags, spWindow);
2609         *errCodePtr = spWindow->SetSystemBarProperty(
2610             WindowType::WINDOW_TYPE_STATUS_BAR, systemBarProperties.at(WindowType::WINDOW_TYPE_STATUS_BAR));
2611         *errCodePtr = spWindow->SetSystemBarProperty(
2612             WindowType::WINDOW_TYPE_NAVIGATION_BAR, systemBarProperties.at(WindowType::WINDOW_TYPE_NAVIGATION_BAR));
2613         *errCodePtr = spWindow->SetSystemBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR,
2614             systemBarProperties.at(WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR));
2615         TLOGI(WmsLogTag::WMS_IMMS, "Window [%{public}u, %{public}s] set system bar enable end, ret = %{public}d",
2616             spWindow->GetWindowId(), spWindow->GetWindowName().c_str(), *errCodePtr);
2617     };
2618     complete = [weakToken, errCodePtr](napi_env env, NapiAsyncTask& task, int32_t status) {
2619         if (errCodePtr == nullptr) {
2620             task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
2621             return;
2622         }
2623         if (*errCodePtr == WMError::WM_OK) {
2624             task.Resolve(env, NapiGetUndefined(env));
2625         } else {
2626             task.Reject(env, JsErrUtils::CreateJsError(env, *errCodePtr, "JsWindow::OnSetSystemBarEnable failed"));
2627         }
2628     };
2629 }
2630 
OnSetSystemBarEnable(napi_env env,napi_callback_info info)2631 napi_value JsWindow::OnSetSystemBarEnable(napi_env env, napi_callback_info info)
2632 {
2633     std::shared_ptr<WMError> errCodePtr = std::make_shared<WMError>(WMError::WM_OK);
2634     if (windowToken_ == nullptr) {
2635         TLOGE(WmsLogTag::WMS_IMMS, "window is null");
2636         *errCodePtr = WMError::WM_ERROR_NULLPTR;
2637     }
2638     size_t argc = 4;
2639     napi_value argv[4] = {nullptr};
2640     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2641     if (argc > 2) { // 2: maximum params num
2642         TLOGE(WmsLogTag::WMS_IMMS, "Argc is invalid: %{public}zu", argc);
2643         *errCodePtr = WMError::WM_ERROR_INVALID_PARAM;
2644     }
2645     std::map<WindowType, SystemBarProperty> systemBarProperties;
2646     std::map<WindowType, SystemBarPropertyFlag> systemBarPropertyFlags;
2647     if (*errCodePtr == WMError::WM_OK && !GetSystemBarStatus(systemBarProperties, systemBarPropertyFlags,
2648         env, info, windowToken_)) {
2649         TLOGE(WmsLogTag::WMS_IMMS, "Failed to convert parameter to systemBarProperties");
2650         *errCodePtr = WMError::WM_ERROR_INVALID_PARAM;
2651     }
2652     wptr<Window> weakToken(windowToken_);
2653     NapiAsyncTask::ExecuteCallback execute;
2654     NapiAsyncTask::CompleteCallback complete;
2655     SetSystemBarEnableTask(execute, complete, weakToken, systemBarProperties, systemBarPropertyFlags, errCodePtr);
2656 
2657     napi_value lastParam = nullptr;
2658     if (argc > 0 && GetType(env, argv[0]) == napi_function) {
2659         lastParam = argv[0];
2660     } else if (argc > 1 && GetType(env, argv[1]) == napi_function) {
2661         lastParam = argv[1];
2662     }
2663     napi_value result = nullptr;
2664     NapiAsyncTask::Schedule("JsWindow::OnSetSystemBarEnable",
2665         env, CreateAsyncTaskWithLastParam(env, lastParam, std::move(execute), std::move(complete), &result));
2666     return result;
2667 }
2668 
OnSetWindowSystemBarEnable(napi_env env,napi_callback_info info)2669 napi_value JsWindow::OnSetWindowSystemBarEnable(napi_env env, napi_callback_info info)
2670 {
2671     WmErrorCode errCode = WmErrorCode::WM_OK;
2672     std::map<WindowType, SystemBarProperty> systemBarProperties;
2673     std::map<WindowType, SystemBarPropertyFlag> systemBarPropertyFlags;
2674     errCode = (windowToken_ == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : WmErrorCode::WM_OK;
2675     size_t argc = 4;
2676     napi_value argv[4] = {nullptr};
2677     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2678     if (errCode == WmErrorCode::WM_OK && (argc < 1 || // 1: params num
2679         !GetSystemBarStatus(systemBarProperties, systemBarPropertyFlags, env, info, windowToken_))) {
2680         TLOGE(WmsLogTag::WMS_IMMS, "invalid param or argc:%{public}zu", argc);
2681         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2682     }
2683     wptr<Window> weakToken(windowToken_);
2684     NapiAsyncTask::CompleteCallback complete = [weakToken, systemBarProperties, systemBarPropertyFlags, errCode]
2685         (napi_env env, NapiAsyncTask& task, int32_t status) mutable {
2686             auto weakWindow = weakToken.promote();
2687             errCode = (weakWindow == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : errCode;
2688             if (errCode != WmErrorCode::WM_OK) {
2689                 TLOGE(WmsLogTag::WMS_IMMS, "window is nullptr");
2690                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
2691                 return;
2692             }
2693             UpdateSystemBarProperties(systemBarProperties, systemBarPropertyFlags, weakWindow);
2694             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(
2695                 SetSystemBarPropertiesByFlags(systemBarPropertyFlags, systemBarProperties, weakWindow));
2696             if (ret == WmErrorCode::WM_OK) {
2697                 task.Resolve(env, NapiGetUndefined(env));
2698             } else {
2699                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "JsWindow::OnSetWindowSystemBarEnable failed"));
2700             }
2701         };
2702     napi_value lastParam = nullptr;
2703     if (argc >= 1 && argv[0] != nullptr && GetType(env, argv[0]) == napi_function) {
2704         lastParam = argv[0];
2705     } else if (argc >= 2 && argv[1] != nullptr && GetType(env, argv[1]) == napi_function) { // 2 arg count
2706         lastParam = argv[1];
2707     }
2708     napi_value result = nullptr;
2709     NapiAsyncTask::Schedule("JsWindow::OnSetWindowSystemBarEnable",
2710         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
2711     return result;
2712 }
2713 
OnSetSpecificSystemBarEnabled(napi_env env,napi_callback_info info)2714 napi_value JsWindow::OnSetSpecificSystemBarEnabled(napi_env env, napi_callback_info info)
2715 {
2716     WmErrorCode err = (windowToken_ == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : WmErrorCode::WM_OK;
2717     size_t argc = 4;
2718     napi_value argv[4] = {nullptr};
2719     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2720     std::string name;
2721     if (!ConvertFromJsValue(env, argv[0], name)) {
2722         TLOGE(WmsLogTag::WMS_IMMS, "Failed to convert parameter to SystemBarName");
2723         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2724     }
2725     std::map<WindowType, SystemBarProperty> jsSystemBarProperties;
2726     if (err == WmErrorCode::WM_OK && (argc < 1 || // 1: params num
2727         !GetSpecificBarStatus(env, info, jsSystemBarProperties))) {
2728         TLOGE(WmsLogTag::WMS_IMMS, "invalid param or argc:%{public}zu", argc);
2729         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2730     }
2731     NapiAsyncTask::CompleteCallback complete = [weakToken = wptr<Window>(windowToken_),
2732         jsSystemBarProperties = std::move(jsSystemBarProperties), name = std::move(name), err]
2733         (napi_env env, NapiAsyncTask& task, int32_t status) mutable {
2734         auto weakWindow = weakToken.promote();
2735         err = (weakWindow == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : err;
2736         if (err != WmErrorCode::WM_OK) {
2737             TLOGE(WmsLogTag::WMS_IMMS, "window is nullptr");
2738             task.Reject(env, JsErrUtils::CreateJsError(env, err));
2739             return;
2740         }
2741         std::map<WindowType, SystemBarProperty> systemBarProperties;
2742         GetSpecificBarStatus(weakWindow, name, jsSystemBarProperties, systemBarProperties);
2743         if (name.compare("status") == 0) {
2744             err = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetSpecificBarProperty(
2745                 WindowType::WINDOW_TYPE_STATUS_BAR, systemBarProperties.at(WindowType::WINDOW_TYPE_STATUS_BAR)));
2746         } else if (name.compare("navigation") == 0) {
2747             err = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetSpecificBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_BAR,
2748                 systemBarProperties.at(WindowType::WINDOW_TYPE_NAVIGATION_BAR)));
2749         } else if (name.compare("navigationIndicator") == 0) {
2750             err = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetSpecificBarProperty(
2751                 WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR,
2752                 systemBarProperties.at(WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR)));
2753         }
2754         if (err == WmErrorCode::WM_OK) {
2755             task.Resolve(env, NapiGetUndefined(env));
2756         } else {
2757             TLOGE(WmsLogTag::WMS_IMMS, "SetSpecificSystemBarEnabled failed, ret = %{public}d", err);
2758             task.Reject(env, JsErrUtils::CreateJsError(env, err, "JsWindow::OnSetSpecificSystemBarEnabled failed"));
2759         }
2760     };
2761     napi_value result = nullptr;
2762     NapiAsyncTask::Schedule("JsWindow::OnSetSpecificSystemBarEnabled",
2763         env, CreateAsyncTaskWithLastParam(env, nullptr, nullptr, std::move(complete), &result));
2764     return result;
2765 }
2766 
OnEnableLandscapeMultiWindow(napi_env env,napi_callback_info info)2767 napi_value JsWindow::OnEnableLandscapeMultiWindow(napi_env env, napi_callback_info info)
2768 {
2769     TLOGI(WmsLogTag::WMS_MULTI_WINDOW, "OnEnableLandscapeMultiWindow");
2770     WmErrorCode err = (windowToken_ == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : WmErrorCode::WM_OK;
2771     size_t argc = 4;
2772     napi_value argv[4] = {nullptr};
2773     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2774 
2775     wptr<Window> weakToken(windowToken_);
2776     NapiAsyncTask::CompleteCallback complete =
2777         [weakToken, err](napi_env env, NapiAsyncTask& task, int32_t status) mutable {
2778         auto weakWindow = weakToken.promote();
2779         err = (weakWindow == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : err;
2780         if (err != WmErrorCode::WM_OK) {
2781             task.Reject(env, JsErrUtils::CreateJsError(env, err));
2782             return;
2783         }
2784         WMError ret = weakWindow->SetLandscapeMultiWindow(true);
2785         if (ret == WMError::WM_OK) {
2786             task.Resolve(env, NapiGetUndefined(env));
2787         } else {
2788             task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY,
2789                                            "JsWindow::OnEnableLandscapeMultiWindow failed"));
2790         }
2791     };
2792     napi_value result = nullptr;
2793     NapiAsyncTask::Schedule("JsWindow::OnEnableLandscapeMultiWindow",
2794                             env, CreateAsyncTaskWithLastParam(env, nullptr, nullptr, std::move(complete), &result));
2795     return result;
2796 }
2797 
OnDisableLandscapeMultiWindow(napi_env env,napi_callback_info info)2798 napi_value JsWindow::OnDisableLandscapeMultiWindow(napi_env env, napi_callback_info info)
2799 {
2800     TLOGI(WmsLogTag::WMS_MULTI_WINDOW, "OnDisableLandscapeMultiWindow");
2801     WmErrorCode err = (windowToken_ == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : WmErrorCode::WM_OK;
2802     size_t argc = 4;
2803     napi_value argv[4] = {nullptr};
2804     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2805 
2806     wptr<Window> weakToken(windowToken_);
2807     NapiAsyncTask::CompleteCallback complete =
2808         [weakToken, err](napi_env env, NapiAsyncTask& task, int32_t status) mutable {
2809         auto weakWindow = weakToken.promote();
2810         err = (weakWindow == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : err;
2811         if (err != WmErrorCode::WM_OK) {
2812             task.Reject(env, JsErrUtils::CreateJsError(env, err));
2813             return;
2814         }
2815         WMError ret = weakWindow->SetLandscapeMultiWindow(false);
2816         if (ret == WMError::WM_OK) {
2817             task.Resolve(env, NapiGetUndefined(env));
2818         } else {
2819             task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY,
2820                                            "JsWindow::OnDisableLandscapeMultiWindow failed"));
2821         }
2822     };
2823     napi_value result = nullptr;
2824     NapiAsyncTask::Schedule("JsWindow::OnDisableLandscapeMultiWindow",
2825                             env, CreateAsyncTaskWithLastParam(env, nullptr, nullptr, std::move(complete), &result));
2826     return result;
2827 }
2828 
OnSetSystemBarProperties(napi_env env,napi_callback_info info)2829 napi_value JsWindow::OnSetSystemBarProperties(napi_env env, napi_callback_info info)
2830 {
2831     WMError errCode = WMError::WM_OK;
2832     if (windowToken_ == nullptr) {
2833         TLOGE(WmsLogTag::WMS_IMMS, "windowToken_ is nullptr");
2834         errCode = WMError::WM_ERROR_NULLPTR;
2835     }
2836     size_t argc = 4;
2837     napi_value argv[4] = {nullptr};
2838     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2839     if (argc < 1 || argc > 2) { // 2: maximum params num
2840         TLOGE(WmsLogTag::WMS_IMMS, "Argc is invalid: %{public}zu", argc);
2841         errCode = WMError::WM_ERROR_INVALID_PARAM;
2842     }
2843     std::map<WindowType, SystemBarProperty> jsSystemBarProperties;
2844     std::map<WindowType, SystemBarPropertyFlag> jsSystemBarPropertyFlags;
2845     if (errCode == WMError::WM_OK) {
2846         napi_value nativeObj = argv[0];
2847         if (nativeObj == nullptr ||
2848             !SetSystemBarPropertiesFromJs(env, nativeObj, windowToken_,
2849                                           jsSystemBarProperties, jsSystemBarPropertyFlags)) {
2850             TLOGE(WmsLogTag::WMS_IMMS, "Failed to convert parameter to systemBarProperties");
2851             errCode = WMError::WM_ERROR_INVALID_PARAM;
2852         }
2853     }
2854     NapiAsyncTask::CompleteCallback complete = [weakToken = wptr<Window>(windowToken_),
2855         jsSystemBarProperties = std::move(jsSystemBarProperties),
2856         jsSystemBarPropertyFlags = std::move(jsSystemBarPropertyFlags), errCode]
2857         (napi_env env, NapiAsyncTask& task, int32_t status) mutable {
2858             auto windowToken = weakToken.promote();
2859             errCode = (windowToken == nullptr) ? WMError::WM_ERROR_NULLPTR : errCode;
2860             if (errCode != WMError::WM_OK) {
2861                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
2862                 return;
2863             }
2864             std::map<WindowType, SystemBarProperty> systemBarProperties;
2865             std::map<WindowType, SystemBarPropertyFlag> systemBarPropertyFlags;
2866             GetSystemBarPropertiesFromJs(windowToken, jsSystemBarProperties, jsSystemBarPropertyFlags,
2867                 systemBarProperties, systemBarPropertyFlags);
2868             UpdateSystemBarProperties(systemBarProperties, systemBarPropertyFlags, windowToken);
2869             WMError ret = SetSystemBarPropertiesByFlags(
2870                 systemBarPropertyFlags, systemBarProperties, windowToken);
2871             if (ret == WMError::WM_OK) {
2872                 task.Resolve(env, NapiGetUndefined(env));
2873             } else {
2874                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR,
2875                     "JsWindow::OnSetSystemBarProperties failed"));
2876             }
2877         };
2878 
2879     napi_value lastParam = (argc <= 1) ? nullptr : (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
2880     napi_value result = nullptr;
2881     NapiAsyncTask::Schedule("JsWindow::OnSetSystemBarProperties",
2882         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
2883     return result;
2884 }
2885 
OnGetWindowSystemBarPropertiesSync(napi_env env,napi_callback_info info)2886 napi_value JsWindow::OnGetWindowSystemBarPropertiesSync(napi_env env, napi_callback_info info)
2887 {
2888     wptr<Window> weakToken(windowToken_);
2889     auto weakWindow = weakToken.promote();
2890     if (weakWindow == nullptr) {
2891         TLOGE(WmsLogTag::WMS_IMMS, "window is null");
2892         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
2893     }
2894     if (!WindowHelper::IsMainWindow(weakWindow->GetType())) {
2895         TLOGE(WmsLogTag::WMS_IMMS, "only main window has right to call");
2896         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
2897     }
2898     auto objValue = CreateJsSystemBarPropertiesObject(env, weakWindow);
2899     if (objValue == nullptr) {
2900         TLOGE(WmsLogTag::WMS_IMMS, "get properties failed");
2901         return NapiThrowError(env, WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY);
2902     }
2903     return objValue;
2904 }
2905 
OnSetWindowSystemBarProperties(napi_env env,napi_callback_info info)2906 napi_value JsWindow::OnSetWindowSystemBarProperties(napi_env env, napi_callback_info info)
2907 {
2908     WmErrorCode errCode = WmErrorCode::WM_OK;
2909     errCode = (windowToken_ == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : WmErrorCode::WM_OK;
2910     size_t argc = 4;
2911     napi_value argv[4] = {nullptr};
2912     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2913     if (argc < 1) { // 2: maximum params num
2914         TLOGE(WmsLogTag::WMS_IMMS, "Argc is invalid: %{public}zu", argc);
2915         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2916     }
2917     std::map<WindowType, SystemBarProperty> jsSystemBarProperties;
2918     std::map<WindowType, SystemBarPropertyFlag> jsSystemBarPropertyFlags;
2919     if (errCode == WmErrorCode::WM_OK) {
2920         napi_value nativeObj = argv[0];
2921         if (nativeObj == nullptr ||
2922             !SetSystemBarPropertiesFromJs(env, nativeObj, windowToken_,
2923                                           jsSystemBarProperties, jsSystemBarPropertyFlags)) {
2924             TLOGE(WmsLogTag::WMS_IMMS, "Failed to convert parameter to systemBarProperties");
2925             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2926         }
2927     }
2928     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
2929         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
2930     }
2931     NapiAsyncTask::CompleteCallback complete = [weakToken = wptr<Window>(windowToken_),
2932         jsSystemBarProperties = std::move(jsSystemBarProperties),
2933         jsSystemBarPropertyFlags = std::move(jsSystemBarPropertyFlags), errCode]
2934         (napi_env env, NapiAsyncTask& task, int32_t status) mutable {
2935             auto weakWindow = weakToken.promote();
2936             errCode = (weakWindow == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : errCode;
2937             if (errCode != WmErrorCode::WM_OK) {
2938                 TLOGE(WmsLogTag::WMS_IMMS, "window is nullptr");
2939                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
2940                 return;
2941             }
2942             std::map<WindowType, SystemBarProperty> systemBarProperties;
2943             std::map<WindowType, SystemBarPropertyFlag> systemBarPropertyFlags;
2944             GetSystemBarPropertiesFromJs(weakWindow, jsSystemBarProperties, jsSystemBarPropertyFlags,
2945                 systemBarProperties, systemBarPropertyFlags);
2946             UpdateSystemBarProperties(systemBarProperties, systemBarPropertyFlags, weakWindow);
2947             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(
2948                 SetSystemBarPropertiesByFlags(systemBarPropertyFlags, systemBarProperties, weakWindow));
2949             if (ret == WmErrorCode::WM_OK) {
2950                 task.Resolve(env, NapiGetUndefined(env));
2951             } else {
2952                 task.Reject(env, JsErrUtils::CreateJsError(env, ret,
2953                     "JsWindow::OnSetWindowSystemBarProperties failed"));
2954             }
2955         };
2956 
2957     napi_value lastParam = (argc <= 1) ? nullptr :
2958         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
2959     napi_value result = nullptr;
2960     NapiAsyncTask::Schedule("JsWindow::OnSetWindowSystemBarProperties",
2961         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
2962     return result;
2963 }
2964 
ParseAvoidAreaParam(napi_env env,napi_callback_info info,WMError & errCode,AvoidAreaType & avoidAreaType)2965 static void ParseAvoidAreaParam(napi_env env, napi_callback_info info, WMError& errCode, AvoidAreaType& avoidAreaType)
2966 {
2967     size_t argc = 4;
2968     napi_value argv[4] = {nullptr};
2969     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
2970     if (argc < 1 || argc > 2) { // 2: maximum params num
2971         TLOGE(WmsLogTag::WMS_IMMS, "Argc is invalid: %{public}zu", argc);
2972         errCode = WMError::WM_ERROR_INVALID_PARAM;
2973     }
2974     if (errCode == WMError::WM_OK) {
2975         napi_value nativeMode = argv[0];
2976         if (nativeMode == nullptr) {
2977             TLOGE(WmsLogTag::WMS_IMMS, "Failed to convert parameter to AvoidAreaType");
2978             errCode = WMError::WM_ERROR_INVALID_PARAM;
2979         } else {
2980             uint32_t resultValue = 0;
2981             CHECK_NAPI_RETCODE(errCode, WMError::WM_ERROR_INVALID_PARAM,
2982                 napi_get_value_uint32(env, nativeMode, &resultValue));
2983             avoidAreaType = static_cast<AvoidAreaType>(resultValue);
2984             errCode = ((avoidAreaType > AvoidAreaType::TYPE_KEYBOARD) ||
2985                 (avoidAreaType < AvoidAreaType::TYPE_SYSTEM)) ? WMError::WM_ERROR_INVALID_PARAM : WMError::WM_OK;
2986         }
2987     }
2988 }
2989 
OnGetAvoidArea(napi_env env,napi_callback_info info)2990 napi_value JsWindow::OnGetAvoidArea(napi_env env, napi_callback_info info)
2991 {
2992     WMError errCode = WMError::WM_OK;
2993     AvoidAreaType avoidAreaType = AvoidAreaType::TYPE_SYSTEM;
2994     ParseAvoidAreaParam(env, info, errCode, avoidAreaType);
2995     wptr<Window> weakToken(windowToken_);
2996     NapiAsyncTask::CompleteCallback complete =
2997         [weakToken, errCode, avoidAreaType](napi_env env, NapiAsyncTask& task, int32_t status) {
2998             auto weakWindow = weakToken.promote();
2999             if (weakWindow == nullptr) {
3000                 TLOGE(WmsLogTag::WMS_IMMS, "window is nullptr");
3001                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
3002                 return;
3003             }
3004             if (errCode != WMError::WM_OK) {
3005                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
3006                 TLOGE(WmsLogTag::WMS_IMMS, "window is nullptr or get invalid param");
3007                 return;
3008             }
3009             // getAvoidRect by avoidAreaType
3010             AvoidArea avoidArea;
3011             WMError ret = weakWindow->GetAvoidAreaByType(avoidAreaType, avoidArea);
3012             if (ret != WMError::WM_OK) {
3013                 TLOGE(WmsLogTag::WMS_IMMS, "GetAvoidArea failed, ret = %{public}d", ret);
3014                 avoidArea.topRect_ = g_emptyRect;
3015                 avoidArea.leftRect_ = g_emptyRect;
3016                 avoidArea.rightRect_ = g_emptyRect;
3017                 avoidArea.bottomRect_ = g_emptyRect;
3018             }
3019             napi_value avoidAreaObj = ConvertAvoidAreaToJsValue(env, avoidArea, avoidAreaType);
3020             if (avoidAreaObj != nullptr) {
3021                 task.Resolve(env, avoidAreaObj);
3022             } else {
3023                 TLOGE(WmsLogTag::WMS_IMMS, "ConvertAvoidAreaToJsValue failed");
3024                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR,
3025                     "JsWindow::OnGetAvoidArea failed"));
3026             }
3027         };
3028     size_t argc = 4;
3029     napi_value argv[4] = {nullptr};
3030     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3031     napi_value lastParam = (argc <= 1) ? nullptr :
3032         (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
3033     napi_value result = nullptr;
3034     NapiAsyncTask::Schedule("JsWindow::OnGetAvoidArea",
3035         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
3036     return result;
3037 }
3038 
OnGetWindowAvoidAreaSync(napi_env env,napi_callback_info info)3039 napi_value JsWindow::OnGetWindowAvoidAreaSync(napi_env env, napi_callback_info info)
3040 {
3041     WmErrorCode errCode = WmErrorCode::WM_OK;
3042     size_t argc = 4;
3043     napi_value argv[4] = {nullptr};
3044     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3045     if (argc < 1) { // 1: params num
3046         TLOGE(WmsLogTag::WMS_IMMS, "invalid argc:%{public}zu", argc);
3047         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
3048     }
3049     AvoidAreaType avoidAreaType = AvoidAreaType::TYPE_SYSTEM;
3050     napi_value nativeMode = argv[0];
3051     if (nativeMode == nullptr) {
3052         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3053     } else {
3054         uint32_t resultValue = 0;
3055         CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
3056             napi_get_value_uint32(env, nativeMode, &resultValue));
3057         avoidAreaType = static_cast<AvoidAreaType>(resultValue);
3058         errCode = ((avoidAreaType > AvoidAreaType::TYPE_NAVIGATION_INDICATOR) ||
3059                    (avoidAreaType < AvoidAreaType::TYPE_SYSTEM)) ?
3060             WmErrorCode::WM_ERROR_INVALID_PARAM : WmErrorCode::WM_OK;
3061     }
3062     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
3063         TLOGE(WmsLogTag::WMS_IMMS, "invalid param");
3064         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
3065     }
3066 
3067     wptr<Window> weakToken(windowToken_);
3068     auto window = weakToken.promote();
3069     if (window == nullptr) {
3070         TLOGE(WmsLogTag::WMS_IMMS, "window is nullptr");
3071         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
3072     }
3073     // getAvoidRect by avoidAreaType
3074     AvoidArea avoidArea;
3075     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(window->GetAvoidAreaByType(avoidAreaType, avoidArea));
3076     if (ret != WmErrorCode::WM_OK) {
3077         TLOGE(WmsLogTag::WMS_IMMS, "GetWindowAvoidAreaSync failed, ret = %{public}d", ret);
3078         avoidArea.topRect_ = g_emptyRect;
3079         avoidArea.leftRect_ = g_emptyRect;
3080         avoidArea.rightRect_ = g_emptyRect;
3081         avoidArea.bottomRect_ = g_emptyRect;
3082     }
3083     napi_value avoidAreaObj = ConvertAvoidAreaToJsValue(env, avoidArea, avoidAreaType);
3084     if (avoidAreaObj != nullptr) {
3085         return avoidAreaObj;
3086     } else {
3087         TLOGE(WmsLogTag::WMS_IMMS, "ConvertAvoidAreaToJsValue failed");
3088         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
3089     }
3090 }
3091 
OnIsShowing(napi_env env,napi_callback_info info)3092 napi_value JsWindow::OnIsShowing(napi_env env, napi_callback_info info)
3093 {
3094     WMError errCode = WMError::WM_OK;
3095     size_t argc = 4;
3096     napi_value argv[4] = {nullptr};
3097     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3098     if (argc > 1) {
3099         WLOGFE("Argc is invalid: %{public}zu", argc);
3100         errCode = WMError::WM_ERROR_INVALID_PARAM;
3101     }
3102     wptr<Window> weakToken(windowToken_);
3103     NapiAsyncTask::CompleteCallback complete =
3104         [weakToken, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
3105             auto weakWindow = weakToken.promote();
3106             if (weakWindow == nullptr) {
3107                 WLOGFE("window is nullptr");
3108                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
3109                 return;
3110             }
3111             if (errCode != WMError::WM_OK) {
3112                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
3113                 WLOGFE("window is nullptr or get invalid param");
3114                 return;
3115             }
3116             bool state = weakWindow->GetWindowState() == WindowState::STATE_SHOWN;
3117             task.Resolve(env, CreateJsValue(env, state));
3118             WLOGI("Window [%{public}u, %{public}s] get show state end, state = %{public}u",
3119                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), state);
3120         };
3121 
3122     napi_value lastParam = (argc == 0) ? nullptr : (GetType(env, argv[0]) == napi_function ? argv[0] : nullptr);
3123     napi_value result = nullptr;
3124     NapiAsyncTask::Schedule("JsWindow::OnIsShowing",
3125         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
3126     return result;
3127 }
3128 
OnIsWindowShowingSync(napi_env env,napi_callback_info info)3129 napi_value JsWindow::OnIsWindowShowingSync(napi_env env, napi_callback_info info)
3130 {
3131     wptr<Window> weakToken(windowToken_);
3132     auto window = weakToken.promote();
3133     if (window == nullptr) {
3134         WLOGFE("window is nullptr");
3135         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
3136     }
3137     bool state = (window->GetWindowState() == WindowState::STATE_SHOWN);
3138     WLOGI("Window [%{public}u, %{public}s] get show state end, state = %{public}u",
3139         window->GetWindowId(), window->GetWindowName().c_str(), state);
3140     return CreateJsValue(env, state);
3141 }
3142 
OnSetPreferredOrientation(napi_env env,napi_callback_info info)3143 napi_value JsWindow::OnSetPreferredOrientation(napi_env env, napi_callback_info info)
3144 {
3145     WmErrorCode errCode = WmErrorCode::WM_OK;
3146     Orientation requestedOrientation = Orientation::UNSPECIFIED;
3147     size_t argc = 4;
3148     napi_value argv[4] = {nullptr};
3149     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3150     if (argc < 1) { // 1: params num
3151         WLOGFE("Argc is invalid: %{public}zu", argc);
3152         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3153     } else {
3154         if (argv[0] == nullptr) {
3155             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3156             WLOGFE("Failed to convert parameter to Orientation");
3157         } else {
3158             uint32_t resultValue = 0;
3159             if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(env, argv[0], resultValue)) {
3160                 WLOGFE("Failed to convert parameter to orientation");
3161                 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3162             }
3163             auto apiOrientation = static_cast<ApiOrientation>(resultValue);
3164             if (apiOrientation < ApiOrientation::BEGIN ||
3165                 apiOrientation > ApiOrientation::END) {
3166                 WLOGFE("Orientation %{public}u invalid!", static_cast<uint32_t>(apiOrientation));
3167                 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3168             } else {
3169                 requestedOrientation = JS_TO_NATIVE_ORIENTATION_MAP.at(apiOrientation);
3170             }
3171         }
3172     }
3173     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
3174         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
3175     }
3176 
3177     wptr<Window> weakToken(windowToken_);
3178     NapiAsyncTask::CompleteCallback complete =
3179         [weakToken, requestedOrientation](napi_env env, NapiAsyncTask& task, int32_t status) {
3180             auto weakWindow = weakToken.promote();
3181             if (weakWindow == nullptr) {
3182                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY,
3183                     "OnSetPreferredOrientation failed"));
3184                 return;
3185             }
3186             weakWindow->SetRequestedOrientation(requestedOrientation);
3187             task.Resolve(env, NapiGetUndefined(env));
3188             WLOGI("Window [%{public}u, %{public}s] OnSetPreferredOrientation end, orientation = %{public}u",
3189                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(),
3190                 static_cast<uint32_t>(requestedOrientation));
3191         };
3192 
3193     napi_value lastParam = (argc <= 1) ? nullptr :
3194         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
3195     napi_value result = nullptr;
3196     NapiAsyncTask::Schedule("JsWindow::OnSetPreferredOrientation",
3197         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
3198     return result;
3199 }
3200 
OnGetPreferredOrientation(napi_env env,napi_callback_info info)3201 napi_value JsWindow::OnGetPreferredOrientation(napi_env env, napi_callback_info info)
3202 {
3203     size_t argc = 4;
3204     napi_value argv[4] = {nullptr};
3205     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3206     if (argc >= 1) {
3207         WLOGFE("Argc is invalid: %{public}zu, expect zero params", argc);
3208         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
3209     }
3210     wptr<Window> weakToken(windowToken_);
3211     auto window = weakToken.promote();
3212     if (window == nullptr) {
3213         WLOGFE("window is nullptr");
3214         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
3215     }
3216     Orientation requestedOrientation = window->GetRequestedOrientation();
3217     ApiOrientation apiOrientation = ApiOrientation::UNSPECIFIED;
3218     if (requestedOrientation >= Orientation::BEGIN &&
3219         requestedOrientation <= Orientation::END) {
3220         apiOrientation = NATIVE_TO_JS_ORIENTATION_MAP.at(requestedOrientation);
3221     } else {
3222         WLOGFE("OnGetPreferredOrientation Orientation %{public}u invalid!",
3223             static_cast<uint32_t>(requestedOrientation));
3224     }
3225     WLOGI("Window [%{public}u, %{public}s] OnGetPreferredOrientation end, Orientation = %{public}u",
3226         window->GetWindowId(), window->GetWindowName().c_str(), static_cast<uint32_t>(apiOrientation));
3227     return CreateJsValue(env, static_cast<uint32_t>(apiOrientation));
3228 }
3229 
OnIsSupportWideGamut(napi_env env,napi_callback_info info)3230 napi_value JsWindow::OnIsSupportWideGamut(napi_env env, napi_callback_info info)
3231 {
3232     WMError errCode = WMError::WM_OK;
3233     size_t argc = 4;
3234     napi_value argv[4] = {nullptr};
3235     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3236     if (argc > 1) {
3237         WLOGFE("Argc is invalid: %{public}zu", argc);
3238         errCode = WMError::WM_ERROR_INVALID_PARAM;
3239     }
3240     wptr<Window> weakToken(windowToken_);
3241     NapiAsyncTask::CompleteCallback complete =
3242         [weakToken, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
3243             auto weakWindow = weakToken.promote();
3244             if (weakWindow == nullptr) {
3245                 WLOGFE("window is nullptr");
3246                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
3247                 return;
3248             }
3249             if (errCode != WMError::WM_OK) {
3250                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
3251                 WLOGFE("window is nullptr or get invalid param");
3252                 return;
3253             }
3254             bool flag = weakWindow->IsSupportWideGamut();
3255             task.Resolve(env, CreateJsValue(env, flag));
3256             WLOGI("Window [%{public}u, %{public}s] OnIsSupportWideGamut end, ret = %{public}u",
3257                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), flag);
3258         };
3259 
3260     napi_value lastParam = (argc == 0) ? nullptr :
3261         (GetType(env, argv[0]) == napi_function ? argv[0] : nullptr);
3262     napi_value result = nullptr;
3263     NapiAsyncTask::Schedule("JsWindow::OnIsSupportWideGamut",
3264         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
3265     return result;
3266 }
3267 
OnIsWindowSupportWideGamut(napi_env env,napi_callback_info info)3268 napi_value JsWindow::OnIsWindowSupportWideGamut(napi_env env, napi_callback_info info)
3269 {
3270     wptr<Window> weakToken(windowToken_);
3271     NapiAsyncTask::CompleteCallback complete =
3272         [weakToken](napi_env env, NapiAsyncTask& task, int32_t status) {
3273             auto weakWindow = weakToken.promote();
3274             if (weakWindow == nullptr) {
3275                 WLOGFE("window is nullptr or get invalid param");
3276                 task.Reject(env,
3277                     JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
3278                 return;
3279             }
3280             bool flag = weakWindow->IsSupportWideGamut();
3281             task.Resolve(env, CreateJsValue(env, flag));
3282             WLOGI("Window [%{public}u, %{public}s] OnIsWindowSupportWideGamut end, ret = %{public}u",
3283                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), flag);
3284         };
3285 
3286     size_t argc = 4;
3287     napi_value argv[4] = {nullptr};
3288     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3289     napi_value lastParam = (argc == 0) ? nullptr :
3290         ((argv[0] != nullptr && GetType(env, argv[0]) == napi_function) ? argv[0] : nullptr);
3291     napi_value result = nullptr;
3292     NapiAsyncTask::Schedule("JsWindow::OnIsWindowSupportWideGamut",
3293         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
3294     return result;
3295 }
3296 
OnSetBackgroundColor(napi_env env,napi_callback_info info)3297 napi_value JsWindow::OnSetBackgroundColor(napi_env env, napi_callback_info info)
3298 {
3299     WMError errCode = WMError::WM_OK;
3300     size_t argc = 4;
3301     napi_value argv[4] = {nullptr};
3302     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3303     if (argc < 1 || argc > 2) { // 2: maximum params num
3304         WLOGFE("Argc is invalid: %{public}zu", argc);
3305         errCode = WMError::WM_ERROR_INVALID_PARAM;
3306     }
3307     std::string color;
3308     if (errCode == WMError::WM_OK && !ConvertFromJsValue(env, argv[0], color)) {
3309         WLOGFE("Failed to convert parameter to background color");
3310         errCode = WMError::WM_ERROR_INVALID_PARAM;
3311     }
3312 
3313     wptr<Window> weakToken(windowToken_);
3314     NapiAsyncTask::CompleteCallback complete =
3315         [weakToken, color, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
3316             auto weakWindow = weakToken.promote();
3317             if (weakWindow == nullptr) {
3318                 WLOGFE("window is nullptr");
3319                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
3320                 return;
3321             }
3322             if (errCode != WMError::WM_OK) {
3323                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode, "Invalidate params."));
3324                 return;
3325             }
3326             WMError ret = weakWindow->SetBackgroundColor(color);
3327             if (ret == WMError::WM_OK) {
3328                 task.Resolve(env, NapiGetUndefined(env));
3329             } else {
3330                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window set background color failed"));
3331             }
3332             WLOGFD("Window [%{public}u, %{public}s] set background color end",
3333                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
3334         };
3335 
3336     napi_value lastParam = (argc <= 1) ? nullptr :
3337         (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
3338     napi_value result = nullptr;
3339     NapiAsyncTask::Schedule("JsWindow::OnSetBackgroundColor",
3340         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
3341     return result;
3342 }
3343 
OnSetWindowBackgroundColorSync(napi_env env,napi_callback_info info)3344 napi_value JsWindow::OnSetWindowBackgroundColorSync(napi_env env, napi_callback_info info)
3345 {
3346     WmErrorCode errCode = WmErrorCode::WM_OK;
3347     size_t argc = 4;
3348     napi_value argv[4] = {nullptr};
3349     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3350     if (argc < 1) { // 1: params num
3351         WLOGFE("Argc is invalid: %{public}zu", argc);
3352         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3353     }
3354     std::string color;
3355     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(env, argv[0], color)) {
3356         WLOGFE("Failed to convert parameter to background color");
3357         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3358     }
3359     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
3360         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
3361     }
3362 
3363     wptr<Window> weakToken(windowToken_);
3364     auto window = weakToken.promote();
3365     if (window == nullptr) {
3366         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
3367     }
3368     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(window->SetBackgroundColor(color));
3369     if (ret == WmErrorCode::WM_OK) {
3370         WLOGI("Window [%{public}u, %{public}s] set background color end",
3371             window->GetWindowId(), window->GetWindowName().c_str());
3372         return NapiGetUndefined(env);
3373     } else {
3374         return NapiThrowError(env, ret);
3375     }
3376 }
3377 
OnSetBrightness(napi_env env,napi_callback_info info)3378 napi_value JsWindow::OnSetBrightness(napi_env env, napi_callback_info info)
3379 {
3380     WMError errCode = WMError::WM_OK;
3381     size_t argc = 4;
3382     napi_value argv[4] = {nullptr};
3383     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3384     if (argc < 1 || argc > 2) { // 2: maximum params num
3385         WLOGFE("Argc is invalid: %{public}zu", argc);
3386         errCode = WMError::WM_ERROR_INVALID_PARAM;
3387     }
3388     double brightness = UNDEFINED_BRIGHTNESS;
3389     if (errCode == WMError::WM_OK) {
3390         napi_value nativeVal = argv[0];
3391         if (nativeVal == nullptr) {
3392             WLOGFE("Failed to convert parameter to brightness");
3393             errCode = WMError::WM_ERROR_INVALID_PARAM;
3394         } else {
3395             CHECK_NAPI_RETCODE(errCode, WMError::WM_ERROR_INVALID_PARAM,
3396                 napi_get_value_double(env, nativeVal, &brightness));
3397         }
3398     }
3399 
3400     wptr<Window> weakToken(windowToken_);
3401     NapiAsyncTask::CompleteCallback complete =
3402         [weakToken, brightness, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
3403             auto weakWindow = weakToken.promote();
3404             if (weakWindow == nullptr) {
3405                 WLOGFE("window is nullptr");
3406                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
3407                 return;
3408             }
3409             if (errCode != WMError::WM_OK) {
3410                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode, "Invalidate params."));
3411                 return;
3412             }
3413             WMError ret = weakWindow->SetBrightness(brightness);
3414             if (ret == WMError::WM_OK) {
3415                 task.Resolve(env, NapiGetUndefined(env));
3416             } else {
3417                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window set brightness failed"));
3418             }
3419             WLOGI("Window [%{public}u, %{public}s] set brightness end",
3420                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
3421         };
3422 
3423     napi_value lastParam = (argc <= 1) ? nullptr :
3424         (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
3425     napi_value result = nullptr;
3426     NapiAsyncTask::Schedule("JsWindow::OnSetBrightness",
3427         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
3428     return result;
3429 }
3430 
OnSetWindowBrightness(napi_env env,napi_callback_info info)3431 napi_value JsWindow::OnSetWindowBrightness(napi_env env, napi_callback_info info)
3432 {
3433     WmErrorCode errCode = WmErrorCode::WM_OK;
3434     size_t argc = 4;
3435     napi_value argv[4] = {nullptr};
3436     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3437     if (argc < 1) { // 1: params num
3438         WLOGFE("Argc is invalid: %{public}zu", argc);
3439         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3440     }
3441     double brightness = UNDEFINED_BRIGHTNESS;
3442     if (errCode == WmErrorCode::WM_OK) {
3443         napi_value nativeVal = argv[0];
3444         if (nativeVal == nullptr) {
3445             WLOGFE("Failed to convert parameter to brightness");
3446             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3447         } else {
3448             CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
3449                 napi_get_value_double(env, nativeVal, &brightness));
3450         }
3451     }
3452     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
3453         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
3454     }
3455 
3456     wptr<Window> weakToken(windowToken_);
3457     NapiAsyncTask::CompleteCallback complete =
3458         [weakToken, brightness](napi_env env, NapiAsyncTask& task, int32_t status) {
3459             auto weakWindow = weakToken.promote();
3460             if (weakWindow == nullptr) {
3461                 task.Reject(env,
3462                     JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY, "Invalidate params."));
3463                 return;
3464             }
3465             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetBrightness(brightness));
3466             if (ret == WmErrorCode::WM_OK) {
3467                 task.Resolve(env, NapiGetUndefined(env));
3468             } else {
3469                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window set brightness failed"));
3470             }
3471             WLOGI("Window [%{public}u, %{public}s] set brightness end",
3472                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
3473         };
3474 
3475     napi_value lastParam = (argc <= 1) ? nullptr :
3476         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
3477     napi_value result = nullptr;
3478     NapiAsyncTask::Schedule("JsWindow::OnSetWindowBrightness",
3479         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
3480     return result;
3481 }
3482 
OnSetDimBehind(napi_env env,napi_callback_info info)3483 napi_value JsWindow::OnSetDimBehind(napi_env env, napi_callback_info info)
3484 {
3485     NapiAsyncTask::CompleteCallback complete =
3486         [](napi_env env, NapiAsyncTask& task, int32_t status) {
3487             task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_DEVICE_NOT_SUPPORT));
3488         };
3489     size_t argc = 4;
3490     napi_value argv[4] = {nullptr};
3491     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3492     napi_value lastParam = (argc <= 1) ? nullptr : (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
3493     napi_value result = nullptr;
3494     NapiAsyncTask::Schedule("JsWindow::OnSetDimBehind",
3495         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
3496     return result;
3497 }
3498 
OnSetFocusable(napi_env env,napi_callback_info info)3499 napi_value JsWindow::OnSetFocusable(napi_env env, napi_callback_info info)
3500 {
3501     WMError errCode = WMError::WM_OK;
3502     size_t argc = 4;
3503     napi_value argv[4] = {nullptr};
3504     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3505     if (argc < 1 || argc > 2) { // 2: maximum params num
3506         WLOGFE("Argc is invalid: %{public}zu", argc);
3507         errCode = WMError::WM_ERROR_INVALID_PARAM;
3508     }
3509     bool focusable = true;
3510     if (errCode == WMError::WM_OK) {
3511         napi_value nativeVal = argv[0];
3512         if (nativeVal == nullptr) {
3513             WLOGFE("Failed to convert parameter to focusable");
3514             errCode = WMError::WM_ERROR_INVALID_PARAM;
3515         } else {
3516             CHECK_NAPI_RETCODE(errCode, WMError::WM_ERROR_INVALID_PARAM,
3517                 napi_get_value_bool(env, nativeVal, &focusable));
3518         }
3519     }
3520 
3521     wptr<Window> weakToken(windowToken_);
3522     NapiAsyncTask::CompleteCallback complete =
3523         [weakToken, focusable, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
3524             auto weakWindow = weakToken.promote();
3525             if (weakWindow == nullptr) {
3526                 WLOGFE("window is nullptr");
3527                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
3528                 return;
3529             }
3530             if (errCode != WMError::WM_OK) {
3531                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode, "Invalidate params."));
3532                 return;
3533             }
3534             WMError ret = weakWindow->SetFocusable(focusable);
3535             if (ret == WMError::WM_OK) {
3536                 task.Resolve(env, NapiGetUndefined(env));
3537             } else {
3538                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window set focusable failed"));
3539             }
3540             WLOGI("Window [%{public}u, %{public}s] set focusable end",
3541                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
3542         };
3543 
3544     napi_value lastParam = (argc <= 1) ? nullptr : (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
3545     napi_value result = nullptr;
3546     NapiAsyncTask::Schedule("JsWindow::OnSetFocusable",
3547         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
3548     return result;
3549 }
3550 
OnSetWindowFocusable(napi_env env,napi_callback_info info)3551 napi_value JsWindow::OnSetWindowFocusable(napi_env env, napi_callback_info info)
3552 {
3553     WmErrorCode errCode = WmErrorCode::WM_OK;
3554     size_t argc = 4;
3555     napi_value argv[4] = {nullptr};
3556     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3557     if (argc < 1) { // 1: maximum params num
3558         WLOGFE("Argc is invalid: %{public}zu", argc);
3559         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3560     }
3561     bool focusable = true;
3562     if (errCode == WmErrorCode::WM_OK) {
3563         napi_value nativeVal = argv[0];
3564         if (nativeVal == nullptr) {
3565             WLOGFE("Failed to convert parameter to focusable");
3566             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3567         } else {
3568             CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
3569                 napi_get_value_bool(env, nativeVal, &focusable));
3570         }
3571     }
3572     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
3573         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
3574     }
3575 
3576     wptr<Window> weakToken(windowToken_);
3577     NapiAsyncTask::CompleteCallback complete =
3578         [weakToken, focusable](napi_env env, NapiAsyncTask& task, int32_t status) {
3579             auto weakWindow = weakToken.promote();
3580             if (weakWindow == nullptr) {
3581                 task.Reject(env,
3582                     JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY, "Invalidate params."));
3583                 return;
3584             }
3585             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetFocusable(focusable));
3586             if (ret == WmErrorCode::WM_OK) {
3587                 task.Resolve(env, NapiGetUndefined(env));
3588             } else {
3589                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window set focusable failed"));
3590             }
3591             WLOGI("Window [%{public}u, %{public}s] set focusable end",
3592                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
3593         };
3594 
3595     napi_value lastParam = (argc <= 1) ? nullptr :
3596         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
3597     napi_value result = nullptr;
3598     NapiAsyncTask::Schedule("JsWindow::OnSetWindowFocusable",
3599         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
3600     return result;
3601 }
3602 
OnSetTopmost(napi_env env,napi_callback_info info)3603 napi_value JsWindow::OnSetTopmost(napi_env env, napi_callback_info info)
3604 {
3605     if (!Permission::IsSystemCalling()) {
3606         TLOGE(WmsLogTag::WMS_LAYOUT, "[NAPI]SetTopmost permission denied!");
3607         return NapiThrowError(env, WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
3608     }
3609     if (windowToken_ == nullptr) {
3610         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
3611     }
3612     if (!WindowHelper::IsMainWindow(windowToken_->GetType())) {
3613         TLOGE(WmsLogTag::WMS_LAYOUT, "[NAPI]SetTopmost is not allowed since window is not main window");
3614         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
3615     }
3616 
3617     size_t argc = 4;
3618     napi_value argv[4] = {nullptr};
3619     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3620     if (argc != 1 || argv[0] == nullptr) {
3621         TLOGE(WmsLogTag::WMS_LAYOUT, "Argc is invalid: %{public}zu. Failed to convert parameter to topmost", argc);
3622         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
3623     }
3624     bool topmost = false;
3625     napi_get_value_bool(env, argv[0], &topmost);
3626 
3627     wptr<Window> weakToken(windowToken_);
3628     std::shared_ptr<WmErrorCode> errCodePtr = std::make_shared<WmErrorCode>(WmErrorCode::WM_OK);
3629     NapiAsyncTask::ExecuteCallback execute = [weakToken, topmost, errCodePtr]() {
3630         if (errCodePtr == nullptr) {
3631             return;
3632         }
3633         auto window = weakToken.promote();
3634         if (window == nullptr) {
3635             *errCodePtr = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
3636             return;
3637         }
3638         *errCodePtr = WM_JS_TO_ERROR_CODE_MAP.at(window->SetTopmost(topmost));
3639         TLOGI(WmsLogTag::WMS_LAYOUT, "Window [%{public}u, %{public}s] set topmost end",
3640             window->GetWindowId(), window->GetWindowName().c_str());
3641     };
3642     NapiAsyncTask::CompleteCallback complete =
3643         [weakToken, errCodePtr](napi_env env, NapiAsyncTask& task, int32_t status) {
3644             if (errCodePtr == nullptr) {
3645                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
3646                 return;
3647             }
3648             if (*errCodePtr == WmErrorCode::WM_OK) {
3649                 task.Resolve(env, NapiGetUndefined(env));
3650             } else {
3651                 task.Reject(env, JsErrUtils::CreateJsError(env, *errCodePtr, "Window set topmost failed"));
3652             }
3653         };
3654     napi_value result = nullptr;
3655     NapiAsyncTask::Schedule("JsWindow::OnSetTopmost",
3656         env, CreateAsyncTaskWithLastParam(env, nullptr, std::move(execute), std::move(complete), &result));
3657     return result;
3658 }
3659 
OnSetKeepScreenOn(napi_env env,napi_callback_info info)3660 napi_value JsWindow::OnSetKeepScreenOn(napi_env env, napi_callback_info info)
3661 {
3662     WMError errCode = WMError::WM_OK;
3663     size_t argc = 4;
3664     napi_value argv[4] = {nullptr};
3665     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3666     if (argc < 1 || argc > 2) { // 2: maximum params num
3667         WLOGFE("Argc is invalid: %{public}zu", argc);
3668         errCode = WMError::WM_ERROR_INVALID_PARAM;
3669     }
3670     bool keepScreenOn = true;
3671     if (errCode == WMError::WM_OK) {
3672         napi_value nativeVal = argv[0];
3673         if (nativeVal == nullptr) {
3674             WLOGFE("Failed to convert parameter to keepScreenOn");
3675             errCode = WMError::WM_ERROR_INVALID_PARAM;
3676         } else {
3677             CHECK_NAPI_RETCODE(errCode, WMError::WM_ERROR_INVALID_PARAM,
3678                 napi_get_value_bool(env, nativeVal, &keepScreenOn));
3679         }
3680     }
3681 
3682     wptr<Window> weakToken(windowToken_);
3683     NapiAsyncTask::CompleteCallback complete =
3684         [weakToken, keepScreenOn, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
3685             auto weakWindow = weakToken.promote();
3686             if (weakWindow == nullptr) {
3687                 WLOGFE("window is nullptr");
3688                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
3689                 return;
3690             }
3691             if (errCode != WMError::WM_OK) {
3692                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode, "Invalidate params."));
3693                 return;
3694             }
3695             WMError ret = weakWindow->SetKeepScreenOn(keepScreenOn);
3696             if (ret == WMError::WM_OK) {
3697                 task.Resolve(env, NapiGetUndefined(env));
3698             } else {
3699                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window set keep screen on failed"));
3700             }
3701             WLOGI("Window [%{public}u, %{public}s] set keep screen on end",
3702                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
3703         };
3704 
3705     napi_value lastParam = (argc <= 1) ? nullptr :
3706         (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
3707     napi_value result = nullptr;
3708     NapiAsyncTask::Schedule("JsWindow::OnSetKeepScreenOn",
3709         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
3710     return result;
3711 }
3712 
OnSetWindowKeepScreenOn(napi_env env,napi_callback_info info)3713 napi_value JsWindow::OnSetWindowKeepScreenOn(napi_env env, napi_callback_info info)
3714 {
3715     WmErrorCode errCode = WmErrorCode::WM_OK;
3716     size_t argc = 4;
3717     napi_value argv[4] = {nullptr};
3718     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3719     if (argc < 1) { // 1: params num
3720         WLOGFE("Argc is invalid: %{public}zu", argc);
3721         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3722     }
3723     bool keepScreenOn = true;
3724     if (errCode == WmErrorCode::WM_OK) {
3725         napi_value nativeVal = argv[0];
3726         if (nativeVal == nullptr) {
3727             WLOGFE("Failed to convert parameter to keepScreenOn");
3728             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3729         } else {
3730             CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
3731                 napi_get_value_bool(env, nativeVal, &keepScreenOn));
3732         }
3733     }
3734     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
3735         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
3736     }
3737     wptr<Window> weakToken(windowToken_);
3738     std::shared_ptr<WmErrorCode> errCodePtr = std::make_shared<WmErrorCode>(WmErrorCode::WM_OK);
3739     NapiAsyncTask::ExecuteCallback execute = [weakToken, keepScreenOn, errCodePtr] () {
3740         if (errCodePtr == nullptr) {
3741             return;
3742         }
3743         auto weakWindow = weakToken.promote();
3744         if (weakWindow == nullptr) {
3745             *errCodePtr = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
3746             return;
3747         }
3748         *errCodePtr = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetKeepScreenOn(keepScreenOn));
3749         WLOGI("Window [%{public}u, %{public}s] set keep screen on end",
3750             weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
3751     };
3752     NapiAsyncTask::CompleteCallback complete =
3753         [weakToken, keepScreenOn, errCodePtr](napi_env env, NapiAsyncTask& task, int32_t status) {
3754             if (errCodePtr == nullptr) {
3755                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY,
3756                 "System abnormal."));
3757                 return;
3758             }
3759             if (*errCodePtr == WmErrorCode::WM_OK) {
3760                 task.Resolve(env, NapiGetUndefined(env));
3761             } else {
3762                 task.Reject(env, JsErrUtils::CreateJsError(env, *errCodePtr, "Window set keep screen on failed"));
3763             }
3764         };
3765 
3766     napi_value lastParam = (argc <= 1) ? nullptr :
3767         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
3768     napi_value result = nullptr;
3769     NapiAsyncTask::Schedule("JsWindow::OnSetWindowKeepScreenOn",
3770         env, CreateAsyncTaskWithLastParam(env, lastParam, std::move(execute), std::move(complete), &result));
3771     return result;
3772 }
3773 
OnSetWakeUpScreen(napi_env env,napi_callback_info info)3774 napi_value JsWindow::OnSetWakeUpScreen(napi_env env, napi_callback_info info)
3775 {
3776     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
3777         WLOGFE("set wake up screen permission denied!");
3778         return NapiThrowError(env, WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
3779     }
3780     if (windowToken_ == nullptr) {
3781         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
3782     }
3783     size_t argc = 4;
3784     napi_value argv[4] = {nullptr};
3785     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3786     if (argc < 1) {
3787         WLOGFE("Argc is invalid: %{public}zu", argc);
3788         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
3789     }
3790     bool wakeUp = false;
3791     napi_value nativeVal = argv[0];
3792     if (nativeVal == nullptr) {
3793         WLOGFE("Failed to convert parameter to keepScreenOn");
3794         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
3795     } else {
3796         napi_get_value_bool(env, nativeVal, &wakeUp);
3797     }
3798 
3799     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetTurnScreenOn(wakeUp));
3800     if (ret != WmErrorCode::WM_OK) {
3801         return NapiThrowError(env, ret);
3802     }
3803 
3804     WLOGI("Window [%{public}u, %{public}s] set wake up screen %{public}d end",
3805         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), wakeUp);
3806     return NapiGetUndefined(env);
3807 }
3808 
OnSetOutsideTouchable(napi_env env,napi_callback_info info)3809 napi_value JsWindow::OnSetOutsideTouchable(napi_env env, napi_callback_info info)
3810 {
3811     NapiAsyncTask::CompleteCallback complete =
3812         [](napi_env env, NapiAsyncTask& task, int32_t status) {
3813             task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_DEVICE_NOT_SUPPORT));
3814         };
3815     size_t argc = 4;
3816     napi_value argv[4] = {nullptr};
3817     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3818     napi_value lastParam = (argc <= 1) ? nullptr :
3819         (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
3820     napi_value result = nullptr;
3821     NapiAsyncTask::Schedule("JsWindow::OnSetOutsideTouchable",
3822         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
3823     return result;
3824 }
3825 
OnSetPrivacyMode(napi_env env,napi_callback_info info)3826 napi_value JsWindow::OnSetPrivacyMode(napi_env env, napi_callback_info info)
3827 {
3828     WMError errCode = WMError::WM_OK;
3829     size_t argc = 4;
3830     napi_value argv[4] = {nullptr};
3831     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3832     if (argc < 1 || argc > 2) { // 2: maximum params num
3833         WLOGFE("Argc is invalid: %{public}zu", argc);
3834         errCode = WMError::WM_ERROR_INVALID_PARAM;
3835     }
3836     bool isPrivacyMode = false;
3837     if (errCode == WMError::WM_OK) {
3838         napi_value nativeVal = argv[0];
3839         if (nativeVal == nullptr) {
3840             WLOGFE("Failed to convert parameter to isPrivacyMode");
3841             errCode = WMError::WM_ERROR_INVALID_PARAM;
3842         } else {
3843             CHECK_NAPI_RETCODE(errCode, WMError::WM_ERROR_INVALID_PARAM,
3844                 napi_get_value_bool(env, nativeVal, &isPrivacyMode));
3845         }
3846     }
3847 
3848     wptr<Window> weakToken(windowToken_);
3849     NapiAsyncTask::CompleteCallback complete =
3850         [weakToken, isPrivacyMode, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
3851             auto weakWindow = weakToken.promote();
3852             if (weakWindow == nullptr) {
3853                 WLOGFE("window is nullptr");
3854                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
3855                 return;
3856             }
3857             if (errCode != WMError::WM_OK) {
3858                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode, "Invalidate params"));
3859                 return;
3860             }
3861             weakWindow->SetPrivacyMode(isPrivacyMode);
3862             task.Resolve(env, NapiGetUndefined(env));
3863             WLOGI("Window [%{public}u, %{public}s] set privacy mode end, mode = %{public}u",
3864                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), isPrivacyMode);
3865         };
3866 
3867     napi_value lastParam = (argc <= 1) ? nullptr :
3868         (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
3869     napi_value result = nullptr;
3870     NapiAsyncTask::Schedule("JsWindow::OnSetPrivacyMode",
3871         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
3872     return result;
3873 }
3874 
OnSetWindowPrivacyMode(napi_env env,napi_callback_info info)3875 napi_value JsWindow::OnSetWindowPrivacyMode(napi_env env, napi_callback_info info)
3876 {
3877     WmErrorCode errCode = WmErrorCode::WM_OK;
3878     size_t argc = 4;
3879     napi_value argv[4] = {nullptr};
3880     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3881     if (argc < 1) { // 1: params num
3882         WLOGFE("Argc is invalid: %{public}zu", argc);
3883         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3884     }
3885     bool isPrivacyMode = false;
3886     if (errCode == WmErrorCode::WM_OK) {
3887         napi_value nativeVal = argv[0];
3888         if (nativeVal == nullptr) {
3889             WLOGFE("Failed to convert parameter to isPrivacyMode");
3890             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3891         } else {
3892             CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
3893                 napi_get_value_bool(env, nativeVal, &isPrivacyMode));
3894         }
3895     }
3896     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
3897         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
3898     }
3899 
3900     wptr<Window> weakToken(windowToken_);
3901     NapiAsyncTask::CompleteCallback complete =
3902         [weakToken, isPrivacyMode](napi_env env, NapiAsyncTask& task, int32_t status) {
3903             auto weakWindow = weakToken.promote();
3904             if (weakWindow == nullptr) {
3905                 task.Reject(env,
3906                     JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY, "Invalidate params"));
3907                 return;
3908             }
3909             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetPrivacyMode(isPrivacyMode));
3910             if (ret == WmErrorCode::WM_ERROR_NO_PERMISSION) {
3911                 task.Reject(env, JsErrUtils::CreateJsError(env, ret));
3912                 WLOGI("Window [%{public}u, %{public}s] set privacy mode failed, mode = %{public}u",
3913                     weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), isPrivacyMode);
3914                 return;
3915             }
3916             task.Resolve(env, NapiGetUndefined(env));
3917             WLOGI("Window [%{public}u, %{public}s] set privacy mode succeed, mode = %{public}u",
3918                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), isPrivacyMode);
3919         };
3920 
3921     napi_value lastParam = (argc <= 1) ? nullptr :
3922         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
3923     napi_value result = nullptr;
3924     NapiAsyncTask::Schedule("JsWindow::OnSetWindowPrivacyMode",
3925         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
3926     return result;
3927 }
3928 
OnSetTouchable(napi_env env,napi_callback_info info)3929 napi_value JsWindow::OnSetTouchable(napi_env env, napi_callback_info info)
3930 {
3931     WMError errCode = WMError::WM_OK;
3932     size_t argc = 4;
3933     napi_value argv[4] = {nullptr};
3934     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
3935     if (argc < 1 || argc > 2) { // 2: maximum params num
3936         WLOGFE("Argc is invalid: %{public}zu", argc);
3937         errCode = WMError::WM_ERROR_INVALID_PARAM;
3938     }
3939     bool touchable = true;
3940     if (errCode == WMError::WM_OK) {
3941         napi_value nativeVal = argv[0];
3942         if (nativeVal == nullptr) {
3943             WLOGFE("Failed to convert parameter to touchable");
3944             errCode = WMError::WM_ERROR_INVALID_PARAM;
3945         } else {
3946             CHECK_NAPI_RETCODE(errCode, WMError::WM_ERROR_INVALID_PARAM,
3947                 napi_get_value_bool(env, nativeVal, &touchable));
3948         }
3949     }
3950 
3951     wptr<Window> weakToken(windowToken_);
3952     NapiAsyncTask::CompleteCallback complete =
3953         [weakToken, touchable, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
3954             auto weakWindow = weakToken.promote();
3955             if (weakWindow == nullptr) {
3956                 WLOGFE("window is nullptr");
3957                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
3958                 return;
3959             }
3960             if (errCode != WMError::WM_OK) {
3961                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode, "Invalidate params."));
3962                 return;
3963             }
3964             WMError ret = weakWindow->SetTouchable(touchable);
3965             if (ret == WMError::WM_OK) {
3966                 task.Resolve(env, NapiGetUndefined(env));
3967             } else {
3968                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window set touchable failed"));
3969             }
3970             WLOGI("Window [%{public}u, %{public}s] set touchable end",
3971                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
3972         };
3973 
3974     napi_value lastParam = (argc <= 1) ? nullptr :
3975         (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
3976     napi_value result = nullptr;
3977     NapiAsyncTask::Schedule("JsWindow::OnSetTouchable",
3978         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
3979     return result;
3980 }
3981 
OnSetTouchableAreas(napi_env env,napi_callback_info info)3982 napi_value JsWindow::OnSetTouchableAreas(napi_env env, napi_callback_info info)
3983 {
3984     if (!Permission::IsSystemCalling()) {
3985         TLOGE(WmsLogTag::WMS_EVENT, "OnSetTouchableAreas permission denied!");
3986         return NapiThrowError(env, WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
3987     }
3988     if (windowToken_ == nullptr) {
3989         TLOGE(WmsLogTag::WMS_EVENT, "WindowToken_ is nullptr");
3990         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
3991     }
3992     Rect windowRect = windowToken_->GetRect();
3993     std::vector<Rect> touchableAreas;
3994     WmErrorCode errCode = ParseTouchableAreas(env, info, windowRect, touchableAreas);
3995     if (errCode != WmErrorCode::WM_OK) {
3996         return NapiThrowError(env, errCode);
3997     }
3998     wptr<Window> weakToken(windowToken_);
3999     NapiAsyncTask::CompleteCallback complete =
4000         [weakToken, touchableAreas](napi_env env, NapiAsyncTask& task, int32_t status) {
4001             auto weakWindow = weakToken.promote();
4002             if (weakWindow == nullptr) {
4003                 TLOGE(WmsLogTag::WMS_EVENT, "CompleteCallback window is nullptr");
4004                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
4005                 return;
4006             }
4007             WMError ret = weakWindow->SetTouchHotAreas(touchableAreas);
4008             if (ret == WMError::WM_OK) {
4009                 task.Resolve(env, NapiGetUndefined(env));
4010             } else {
4011                 WmErrorCode wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(ret);
4012                 task.Reject(env, JsErrUtils::CreateJsError(env, wmErrorCode, "OnSetTouchableAreas failed"));
4013             }
4014             TLOGI(WmsLogTag::WMS_EVENT, "Window [%{public}u, %{public}s] setTouchableAreas end",
4015                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
4016         };
4017     napi_value result = nullptr;
4018     NapiAsyncTask::Schedule("JsWindow::OnSetTouchableAreas",
4019         env, CreateAsyncTaskWithLastParam(env, nullptr, nullptr, std::move(complete), &result));
4020     return result;
4021 }
4022 
OnSetResizeByDragEnabled(napi_env env,napi_callback_info info)4023 napi_value JsWindow::OnSetResizeByDragEnabled(napi_env env, napi_callback_info info)
4024 {
4025     WMError errCode = WMError::WM_OK;
4026     size_t argc = 4;
4027     napi_value argv[4] = {nullptr};
4028     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4029     if (argc < 1 || argc > 2) { // 2: maximum params num
4030         WLOGFE("Argc is invalid: %{public}zu", argc);
4031         errCode = WMError::WM_ERROR_INVALID_PARAM;
4032     }
4033     bool dragEnabled = true;
4034     if (errCode == WMError::WM_OK) {
4035         if (argv[0] == nullptr) {
4036             WLOGFE("Failed to convert parameter to dragEnabled");
4037             errCode = WMError::WM_ERROR_INVALID_PARAM;
4038         } else {
4039             CHECK_NAPI_RETCODE(errCode, WMError::WM_ERROR_INVALID_PARAM,
4040                 napi_get_value_bool(env, argv[0], &dragEnabled));
4041         }
4042     }
4043 
4044     wptr<Window> weakToken(windowToken_);
4045     NapiAsyncTask::CompleteCallback complete =
4046         [weakToken, dragEnabled, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
4047             auto weakWindow = weakToken.promote();
4048             WmErrorCode wmErrorCode;
4049             if (weakWindow == nullptr) {
4050                 WLOGFE("window is nullptr");
4051                 wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(WMError::WM_ERROR_NULLPTR);
4052                 task.Reject(env, JsErrUtils::CreateJsError(env, wmErrorCode));
4053                 return;
4054             }
4055             if (errCode != WMError::WM_OK) {
4056                 wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(errCode);
4057                 task.Reject(env, JsErrUtils::CreateJsError(env, wmErrorCode, "Invalidate params."));
4058                 return;
4059             }
4060             WMError ret = weakWindow->SetResizeByDragEnabled(dragEnabled);
4061             if (ret == WMError::WM_OK) {
4062                 task.Resolve(env, NapiGetUndefined(env));
4063             } else {
4064                 wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(ret);
4065                 task.Reject(env, JsErrUtils::CreateJsError(env, wmErrorCode, "set dragEnabled failed"));
4066             }
4067             WLOGI("Window [%{public}u, %{public}s] set dragEnabled end",
4068                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
4069         };
4070 
4071     napi_value lastParam = (argc <= 1) ? nullptr : (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
4072     napi_value result = nullptr;
4073     NapiAsyncTask::Schedule("JsWindow::SetResizeByDragEnabled",
4074         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
4075     return result;
4076 }
4077 
OnSetRaiseByClickEnabled(napi_env env,napi_callback_info info)4078 napi_value JsWindow::OnSetRaiseByClickEnabled(napi_env env, napi_callback_info info)
4079 {
4080     WMError errCode = WMError::WM_OK;
4081     size_t argc = 4;
4082     napi_value argv[4] = {nullptr};
4083     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4084     if (argc < 1 || argc > 2) { // 2: maximum params num
4085         WLOGFE("Argc is invalid: %{public}zu", argc);
4086         errCode = WMError::WM_ERROR_INVALID_PARAM;
4087     }
4088     bool raiseEnabled = true;
4089     if (errCode == WMError::WM_OK) {
4090         if (argv[0] == nullptr) {
4091             WLOGFE("Failed to convert parameter to raiseEnabled");
4092             errCode = WMError::WM_ERROR_INVALID_PARAM;
4093         } else {
4094             CHECK_NAPI_RETCODE(errCode, WMError::WM_ERROR_INVALID_PARAM,
4095                 napi_get_value_bool(env, argv[0], &raiseEnabled));
4096         }
4097     }
4098 
4099     wptr<Window> weakToken(windowToken_);
4100     NapiAsyncTask::CompleteCallback complete =
4101         [weakToken, raiseEnabled, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
4102             auto weakWindow = weakToken.promote();
4103             WmErrorCode wmErrorCode;
4104             if (weakWindow == nullptr) {
4105                 WLOGFE("window is nullptr");
4106                 wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(WMError::WM_ERROR_NULLPTR);
4107                 task.Reject(env, JsErrUtils::CreateJsError(env, wmErrorCode));
4108                 return;
4109             }
4110             if (errCode != WMError::WM_OK) {
4111                 wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(errCode);
4112                 task.Reject(env, JsErrUtils::CreateJsError(env, wmErrorCode, "Invalidate params."));
4113                 return;
4114             }
4115             WMError ret = weakWindow->SetRaiseByClickEnabled(raiseEnabled);
4116             if (ret == WMError::WM_OK) {
4117                 task.Resolve(env, NapiGetUndefined(env));
4118             } else {
4119                 wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(ret);
4120                 task.Reject(env, JsErrUtils::CreateJsError(env, wmErrorCode, "set raiseEnabled failed"));
4121             }
4122             WLOGI("Window [%{public}u, %{public}s] set raiseEnabled end",
4123                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
4124         };
4125 
4126     napi_value lastParam = (argc <= 1) ? nullptr : (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
4127     napi_value result = nullptr;
4128     NapiAsyncTask::Schedule("JsWindow::SetRaiseByClickEnabled",
4129         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
4130     return result;
4131 }
4132 
OnHideNonSystemFloatingWindows(napi_env env,napi_callback_info info)4133 napi_value JsWindow::OnHideNonSystemFloatingWindows(napi_env env, napi_callback_info info)
4134 {
4135     WMError errCode = WMError::WM_OK;
4136     size_t argc = 4;
4137     napi_value argv[4] = {nullptr};
4138     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4139     if (argc < 1 || argc > 2) { // 2: maximum params num
4140         WLOGFE("Argc is invalid: %{public}zu", argc);
4141         errCode = WMError::WM_ERROR_INVALID_PARAM;
4142     }
4143     bool shouldHide = false;
4144     if (errCode == WMError::WM_OK) {
4145         napi_value nativeVal = argv[0];
4146         if (nativeVal == nullptr) {
4147             WLOGFE("Failed to convert parameter to shouldHide");
4148             errCode = WMError::WM_ERROR_INVALID_PARAM;
4149         } else {
4150             napi_get_value_bool(env, nativeVal, &shouldHide);
4151         }
4152     }
4153     wptr<Window> weakToken(windowToken_);
4154     NapiAsyncTask::CompleteCallback complete =
4155         [weakToken, shouldHide, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
4156             auto weakWindow = weakToken.promote();
4157             if (weakWindow == nullptr) {
4158                 WLOGFE("window is nullptr");
4159                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR, "window is nullptr."));
4160                 return;
4161             }
4162             if (weakWindow->IsFloatingWindowAppType()) {
4163                 WLOGFE("HideNonSystemFloatingWindows is not allowed since window is app floating window");
4164                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_INVALID_CALLING,
4165                     "HideNonSystemFloatingWindows is not allowed since window is app window"));
4166                 return;
4167             }
4168             if (errCode != WMError::WM_OK) {
4169                 WLOGFE("Invalidate params");
4170                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode, "Invalidate params."));
4171                 return;
4172             }
4173             WMError ret = weakWindow->HideNonSystemFloatingWindows(shouldHide);
4174             if (ret == WMError::WM_OK) {
4175                 task.Resolve(env, NapiGetUndefined(env));
4176             } else {
4177                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Hide non-system floating windows failed"));
4178             }
4179             WLOGI("Window [%{public}u, %{public}s] hide non-system floating windows end",
4180                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
4181         };
4182     napi_value lastParam = (argc <= 1) ? nullptr :
4183         (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
4184     napi_value result = nullptr;
4185     NapiAsyncTask::Schedule("JsWindow::HideNonSystemFloatingWindows",
4186         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
4187     return result;
4188 }
4189 
OnSetSingleFrameComposerEnabled(napi_env env,napi_callback_info info)4190 napi_value JsWindow::OnSetSingleFrameComposerEnabled(napi_env env, napi_callback_info info)
4191 {
4192     WmErrorCode errCode = WmErrorCode::WM_OK;
4193     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
4194         WLOGFE("set single frame composer enabled permission denied!");
4195         errCode = WmErrorCode::WM_ERROR_NOT_SYSTEM_APP;
4196     }
4197 
4198     bool enabled = false;
4199     if (errCode == WmErrorCode::WM_OK) {
4200         size_t argc = 4;
4201         napi_value argv[4] = {nullptr};
4202         napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4203         if (argc != 1) { // 1: the param num
4204             WLOGFE("Invalid parameter, argc is invalid: %{public}zu", argc);
4205             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
4206         }
4207         if (errCode == WmErrorCode::WM_OK) {
4208             napi_value nativeVal = argv[0];
4209             if (nativeVal == nullptr) {
4210                 WLOGFE("Invalid parameter, failed to convert parameter to enabled");
4211                 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
4212             } else {
4213                 CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
4214                     napi_get_value_bool(env, nativeVal, &enabled));
4215             }
4216         }
4217     }
4218 
4219     wptr<Window> weakToken(windowToken_);
4220     NapiAsyncTask::CompleteCallback complete =
4221         [weakToken, enabled, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
4222             if (errCode != WmErrorCode::WM_OK) {
4223                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode, "permission denied or invalid parameter."));
4224                 return;
4225             }
4226 
4227             auto weakWindow = weakToken.promote();
4228             WmErrorCode wmErrorCode;
4229             if (weakWindow == nullptr) {
4230                 WLOGFE("window is nullptr");
4231                 wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(WMError::WM_ERROR_NULLPTR);
4232                 task.Reject(env, JsErrUtils::CreateJsError(env, wmErrorCode, "window is nullptr."));
4233                 return;
4234             }
4235 
4236             WMError ret = weakWindow->SetSingleFrameComposerEnabled(enabled);
4237             if (ret == WMError::WM_OK) {
4238                 task.Resolve(env, NapiGetUndefined(env));
4239             } else {
4240                 wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(ret);
4241                 WLOGFE("Set single frame composer enabled failed, ret is %{public}d", wmErrorCode);
4242                 task.Reject(env, JsErrUtils::CreateJsError(env, wmErrorCode,
4243                             "Set single frame composer enabled failed"));
4244                 return;
4245             }
4246             WLOGI("Window [%{public}u, %{public}s] Set single frame composer enabled end, enabled flag = %{public}d",
4247                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), enabled);
4248         };
4249     napi_value lastParam = nullptr;
4250     napi_value result = nullptr;
4251     NapiAsyncTask::Schedule("JsWindow::SetSingleFrameComposerEnabled",
4252         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
4253     return result;
4254 }
4255 
GetSubWindowId(napi_env env,napi_value nativeVal,WmErrorCode & errCode,int32_t & subWindowId)4256 void GetSubWindowId(napi_env env, napi_value nativeVal, WmErrorCode& errCode, int32_t& subWindowId)
4257 {
4258     if (nativeVal == nullptr) {
4259         WLOGFE("Failed to get subWindowId");
4260         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
4261     } else {
4262         int32_t resultValue = 0;
4263         CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
4264             napi_get_value_int32(env, nativeVal, &resultValue));
4265         if (resultValue <= 0) {
4266             WLOGFE("Failed to get subWindowId due to resultValue less than or equal to 0");
4267             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
4268         } else {
4269             subWindowId = resultValue;
4270         }
4271     }
4272     return;
4273 }
4274 
OnRaiseAboveTarget(napi_env env,napi_callback_info info)4275 napi_value JsWindow::OnRaiseAboveTarget(napi_env env, napi_callback_info info)
4276 {
4277     WmErrorCode errCode = WmErrorCode::WM_OK;
4278     size_t argc = 4;
4279     napi_value argv[4] = {nullptr};
4280     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4281     if (argc < 1 || argc > 2) { // 2: maximum params num
4282         WLOGFE("Argc is invalid: %{public}zu", argc);
4283         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
4284     }
4285     int32_t subWindowId = -1;
4286     if (errCode == WmErrorCode::WM_OK) {
4287         GetSubWindowId(env, argv[0], errCode, subWindowId);
4288     }
4289 
4290     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
4291         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
4292     }
4293 
4294     wptr<Window> weakToken(windowToken_);
4295     NapiAsyncTask::CompleteCallback complete =
4296         [weakToken, subWindowId, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
4297             auto weakWindow = weakToken.promote();
4298             if (weakWindow == nullptr) {
4299                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
4300                 return;
4301             }
4302             if (errCode != WmErrorCode::WM_OK) {
4303                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode, "Invalidate params."));
4304                 return;
4305             }
4306             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->RaiseAboveTarget(subWindowId));
4307             if (ret == WmErrorCode::WM_OK) {
4308                 task.Resolve(env, NapiGetUndefined(env));
4309             } else {
4310                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window set raiseAboveTarget failed"));
4311             }
4312         };
4313 
4314     napi_value lastParam = (argc <= 1) ? nullptr : (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
4315     napi_value result = nullptr;
4316     NapiAsyncTask::Schedule("JsWindow::RaiseAboveTarget",
4317         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
4318     return result;
4319 }
4320 
OnKeepKeyboardOnFocus(napi_env env,napi_callback_info info)4321 napi_value JsWindow::OnKeepKeyboardOnFocus(napi_env env, napi_callback_info info)
4322 {
4323     size_t argc = 4;
4324     napi_value argv[4] = {nullptr};
4325     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4326     if (argc < 1) {
4327         WLOGFE("Argc is invalid: %{public}zu", argc);
4328         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
4329     }
4330     bool keepKeyboardFlag = false;
4331     napi_value nativeVal = argv[0];
4332     if (nativeVal == nullptr) {
4333         WLOGFE("Failed to get parameter keepKeyboardFlag");
4334         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
4335     } else {
4336         WmErrorCode errCode = WmErrorCode::WM_OK;
4337         CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
4338             napi_get_value_bool(env, nativeVal, &keepKeyboardFlag));
4339         if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
4340             return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
4341         }
4342     }
4343 
4344     if (windowToken_ == nullptr) {
4345         WLOGFE("WindowToken_ is nullptr");
4346         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
4347     }
4348     if (!WindowHelper::IsSystemWindow(windowToken_->GetType()) &&
4349         !WindowHelper::IsSubWindow(windowToken_->GetType())) {
4350         WLOGFE("KeepKeyboardOnFocus is not allowed since window is not system window or app subwindow");
4351         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
4352     }
4353 
4354     WmErrorCode ret = windowToken_->KeepKeyboardOnFocus(keepKeyboardFlag);
4355     if (ret != WmErrorCode::WM_OK) {
4356         WLOGFE("Window KeepKeyboardOnFocus failed");
4357         return NapiThrowError(env, ret);
4358     }
4359 
4360     WLOGI("Window [%{public}u, %{public}s] KeepKeyboardOnFocus end, keepKeyboardFlag = %{public}d",
4361         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), keepKeyboardFlag);
4362 
4363     return NapiGetUndefined(env);
4364 }
4365 
OnSetWindowTouchable(napi_env env,napi_callback_info info)4366 napi_value JsWindow::OnSetWindowTouchable(napi_env env, napi_callback_info info)
4367 {
4368     WmErrorCode errCode = WmErrorCode::WM_OK;
4369     size_t argc = 4;
4370     napi_value argv[4] = {nullptr};
4371     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4372     if (argc < 1) { // 1: params num
4373         WLOGFE("Argc is invalid: %{public}zu", argc);
4374         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
4375     }
4376     bool touchable = true;
4377     if (errCode == WmErrorCode::WM_OK) {
4378         napi_value nativeVal = argv[0];
4379         if (nativeVal == nullptr) {
4380             WLOGFE("Failed to convert parameter to touchable");
4381             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
4382         } else {
4383             CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
4384                 napi_get_value_bool(env, nativeVal, &touchable));
4385         }
4386     }
4387     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
4388         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
4389     }
4390 
4391     wptr<Window> weakToken(windowToken_);
4392     NapiAsyncTask::CompleteCallback complete =
4393         [weakToken, touchable](napi_env env, NapiAsyncTask& task, int32_t status) {
4394             auto weakWindow = weakToken.promote();
4395             if (weakWindow == nullptr) {
4396                 task.Reject(env,
4397                     JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY, "Invalidate params."));
4398                 return;
4399             }
4400             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetTouchable(touchable));
4401             if (ret == WmErrorCode::WM_OK) {
4402                 task.Resolve(env, NapiGetUndefined(env));
4403             } else {
4404                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window set touchable failed"));
4405             }
4406             WLOGI("Window [%{public}u, %{public}s] set touchable end",
4407                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
4408         };
4409 
4410     napi_value lastParam = (argc <= 1) ? nullptr :
4411         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
4412     napi_value result = nullptr;
4413     NapiAsyncTask::Schedule("JsWindow::OnSetWindowTouchable",
4414         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
4415     return result;
4416 }
4417 
OnSetTransparent(napi_env env,napi_callback_info info)4418 napi_value JsWindow::OnSetTransparent(napi_env env, napi_callback_info info)
4419 {
4420     WMError errCode = WMError::WM_OK;
4421     size_t argc = 4;
4422     napi_value argv[4] = {nullptr};
4423     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4424     if (argc < 1 || argc > 2) { // 2: maximum params num
4425         WLOGFE("Argc is invalid: %{public}zu", argc);
4426         errCode = WMError::WM_ERROR_INVALID_PARAM;
4427     }
4428     bool isTransparent = true;
4429     if (errCode == WMError::WM_OK) {
4430         napi_value nativeVal = argv[0];
4431         if (nativeVal == nullptr) {
4432             WLOGFE("Failed to convert parameter to isTransparent");
4433             errCode = WMError::WM_ERROR_INVALID_PARAM;
4434         } else {
4435             CHECK_NAPI_RETCODE(errCode, WMError::WM_ERROR_INVALID_PARAM,
4436                 napi_get_value_bool(env, nativeVal, &isTransparent));
4437         }
4438     }
4439 
4440     wptr<Window> weakToken(windowToken_);
4441     NapiAsyncTask::CompleteCallback complete =
4442         [weakToken, isTransparent, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
4443             auto weakWindow = weakToken.promote();
4444             if (weakWindow == nullptr) {
4445                 WLOGFE("window is nullptr");
4446                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
4447                 return;
4448             }
4449             if (errCode != WMError::WM_OK) {
4450                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode, "Invalidate params."));
4451                 return;
4452             }
4453             WMError ret = weakWindow->SetTransparent(isTransparent);
4454             if (ret == WMError::WM_OK) {
4455                 task.Resolve(env, NapiGetUndefined(env));
4456             } else {
4457                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window set transparent failed"));
4458             }
4459             WLOGI("Window [%{public}u, %{public}s] set transparent end",
4460                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
4461         };
4462 
4463     napi_value lastParam = (argc <= 1) ? nullptr :
4464         (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
4465     napi_value result = nullptr;
4466     NapiAsyncTask::Schedule("JsWindow::OnSetTransparent",
4467         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
4468     return result;
4469 }
4470 
OnSetCallingWindow(napi_env env,napi_callback_info info)4471 napi_value JsWindow::OnSetCallingWindow(napi_env env, napi_callback_info info)
4472 {
4473     WMError errCode = WMError::WM_OK;
4474     size_t argc = 4;
4475     napi_value argv[4] = {nullptr};
4476     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4477     if (argc < 1 || argc > 2) { // 2: maximum params num
4478         WLOGFE("Argc is invalid: %{public}zu", argc);
4479         errCode = WMError::WM_ERROR_INVALID_PARAM;
4480     }
4481     uint32_t callingWindow = INVALID_WINDOW_ID;
4482     if (errCode == WMError::WM_OK) {
4483         napi_value nativeVal = argv[0];
4484         if (nativeVal == nullptr) {
4485             WLOGFE("Failed to convert parameter to touchable");
4486             errCode = WMError::WM_ERROR_INVALID_PARAM;
4487         } else {
4488             napi_get_value_uint32(env, nativeVal, &callingWindow);
4489         }
4490     }
4491 
4492     wptr<Window> weakToken(windowToken_);
4493     NapiAsyncTask::CompleteCallback complete =
4494         [weakToken, callingWindow, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
4495             auto weakWindow = weakToken.promote();
4496             if (weakWindow == nullptr) {
4497                 WLOGFE("window is nullptr");
4498                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
4499                 return;
4500             }
4501             if (errCode != WMError::WM_OK) {
4502                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode, "Invalidate params."));
4503                 return;
4504             }
4505             WMError ret = weakWindow->SetCallingWindow(callingWindow);
4506             if (ret == WMError::WM_OK) {
4507                 task.Resolve(env, NapiGetUndefined(env));
4508             } else {
4509                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window set calling window failed"));
4510             }
4511             WLOGI("Window [%{public}u, %{public}s] set calling window end",
4512                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
4513         };
4514 
4515     napi_value lastParam = (argc <= 1) ? nullptr : (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
4516     napi_value result = nullptr;
4517     NapiAsyncTask::Schedule("JsWindow::OnSetCallingWindow",
4518         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
4519     return result;
4520 }
4521 
OnDisableWindowDecor(napi_env env,napi_callback_info info)4522 napi_value JsWindow::OnDisableWindowDecor(napi_env env, napi_callback_info info)
4523 {
4524     if (windowToken_ == nullptr) {
4525         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
4526     }
4527     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->DisableAppWindowDecor());
4528     if (ret != WmErrorCode::WM_OK) {
4529         WLOGFE("Window DisableWindowDecor failed");
4530         return NapiThrowError(env, ret);
4531     }
4532     WLOGI("Window [%{public}u, %{public}s] disable app window decor end",
4533         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
4534     return NapiGetUndefined(env);
4535 }
4536 
OnSetColorSpace(napi_env env,napi_callback_info info)4537 napi_value JsWindow::OnSetColorSpace(napi_env env, napi_callback_info info)
4538 {
4539     WMError errCode = WMError::WM_OK;
4540     ColorSpace colorSpace = ColorSpace::COLOR_SPACE_DEFAULT;
4541     size_t argc = 4;
4542     napi_value argv[4] = {nullptr};
4543     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4544     if (argc < 1 || argc > 2) { // 2: maximum params num
4545         WLOGFE("Argc is invalid: %{public}zu", argc);
4546         errCode = WMError::WM_ERROR_INVALID_PARAM;
4547     } else {
4548         napi_value nativeType = argv[0];
4549         if (nativeType == nullptr) {
4550             errCode = WMError::WM_ERROR_INVALID_PARAM;
4551             WLOGFE("Failed to convert parameter to ColorSpace");
4552         } else {
4553             uint32_t resultValue = 0;
4554             CHECK_NAPI_RETCODE(errCode, WMError::WM_ERROR_INVALID_PARAM,
4555                 napi_get_value_uint32(env, nativeType, &resultValue));
4556             colorSpace = static_cast<ColorSpace>(resultValue);
4557             if (colorSpace > ColorSpace::COLOR_SPACE_WIDE_GAMUT || colorSpace < ColorSpace::COLOR_SPACE_DEFAULT) {
4558                 WLOGFE("ColorSpace %{public}u invalid!", static_cast<uint32_t>(colorSpace));
4559                 errCode = WMError::WM_ERROR_INVALID_PARAM;
4560             }
4561         }
4562     }
4563 
4564     wptr<Window> weakToken(windowToken_);
4565     NapiAsyncTask::CompleteCallback complete =
4566         [weakToken, colorSpace, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
4567             auto weakWindow = weakToken.promote();
4568             if (weakWindow == nullptr) {
4569                 WLOGFE("window is nullptr");
4570                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
4571                 return;
4572             }
4573             if (errCode != WMError::WM_OK) {
4574                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode, "OnSetColorSpace failed"));
4575                 WLOGFE("window is nullptr or get invalid param");
4576                 return;
4577             }
4578             weakWindow->SetColorSpace(colorSpace);
4579             task.Resolve(env, NapiGetUndefined(env));
4580             WLOGI("Window [%{public}u, %{public}s] OnSetColorSpace end, colorSpace = %{public}u",
4581                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), static_cast<uint32_t>(colorSpace));
4582         };
4583 
4584     napi_value lastParam = (argc <= 1) ? nullptr :
4585         (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
4586     napi_value result = nullptr;
4587     NapiAsyncTask::Schedule("JsWindow::OnSetColorSpace",
4588         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
4589     return result;
4590 }
4591 
OnSetWindowColorSpace(napi_env env,napi_callback_info info)4592 napi_value JsWindow::OnSetWindowColorSpace(napi_env env, napi_callback_info info)
4593 {
4594     WmErrorCode errCode = WmErrorCode::WM_OK;
4595     ColorSpace colorSpace = ColorSpace::COLOR_SPACE_DEFAULT;
4596     size_t argc = 4;
4597     napi_value argv[4] = {nullptr};
4598     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4599     if (argc < 1) { // 1: params num
4600         WLOGFE("Argc is invalid: %{public}zu", argc);
4601         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
4602     } else {
4603         napi_value nativeType = argv[0];
4604         if (nativeType == nullptr) {
4605             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
4606             WLOGFE("Failed to convert parameter to ColorSpace");
4607         } else {
4608             uint32_t resultValue = 0;
4609             CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
4610                 napi_get_value_uint32(env, nativeType, &resultValue));
4611             colorSpace = static_cast<ColorSpace>(resultValue);
4612             if (colorSpace > ColorSpace::COLOR_SPACE_WIDE_GAMUT || colorSpace < ColorSpace::COLOR_SPACE_DEFAULT) {
4613                 WLOGFE("ColorSpace %{public}u invalid!", static_cast<uint32_t>(colorSpace));
4614                 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
4615             }
4616         }
4617     }
4618     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
4619         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
4620     }
4621 
4622     wptr<Window> weakToken(windowToken_);
4623     NapiAsyncTask::CompleteCallback complete =
4624         [weakToken, colorSpace](napi_env env, NapiAsyncTask& task, int32_t status) {
4625             auto weakWindow = weakToken.promote();
4626             if (weakWindow == nullptr) {
4627                 WLOGFE("window is nullptr");
4628                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY,
4629                     "OnSetWindowColorSpace failed"));
4630                 return;
4631             }
4632             weakWindow->SetColorSpace(colorSpace);
4633             task.Resolve(env, NapiGetUndefined(env));
4634             WLOGI("Window [%{public}u, %{public}s] OnSetWindowColorSpace end, colorSpace = %{public}u",
4635                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), static_cast<uint32_t>(colorSpace));
4636         };
4637 
4638     napi_value lastParam = (argc <= 1) ? nullptr :
4639         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
4640     napi_value result = nullptr;
4641     NapiAsyncTask::Schedule("JsWindow::OnSetWindowColorSpace",
4642         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
4643     return result;
4644 }
4645 
OnGetColorSpace(napi_env env,napi_callback_info info)4646 napi_value JsWindow::OnGetColorSpace(napi_env env, napi_callback_info info)
4647 {
4648     WMError errCode = WMError::WM_OK;
4649     size_t argc = 4;
4650     napi_value argv[4] = {nullptr};
4651     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4652     if (argc > 1) {
4653         WLOGFE("Argc is invalid: %{public}zu", argc);
4654         errCode = WMError::WM_ERROR_INVALID_PARAM;
4655     }
4656     wptr<Window> weakToken(windowToken_);
4657     NapiAsyncTask::CompleteCallback complete =
4658         [weakToken, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
4659             auto weakWindow = weakToken.promote();
4660             if (weakWindow == nullptr) {
4661                 WLOGFE("window is nullptr");
4662                 task.Reject(env, JsErrUtils::CreateJsError(env, WMError::WM_ERROR_NULLPTR));
4663                 return;
4664             }
4665             if (errCode != WMError::WM_OK) {
4666                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
4667                 WLOGFE("window is nullptr or get invalid param");
4668                 return;
4669             }
4670             ColorSpace colorSpace = weakWindow->GetColorSpace();
4671             task.Resolve(env, CreateJsValue(env, static_cast<uint32_t>(colorSpace)));
4672             WLOGI("Window [%{public}u, %{public}s] OnGetColorSpace end, colorSpace = %{public}u",
4673                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), static_cast<uint32_t>(colorSpace));
4674         };
4675 
4676     napi_value lastParam = (argc == 0) ? nullptr :
4677         (GetType(env, argv[0]) == napi_function ? argv[0] : nullptr);
4678     napi_value result = nullptr;
4679     NapiAsyncTask::Schedule("JsWindow::OnGetColorSpace",
4680         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
4681     return result;
4682 }
4683 
OnGetWindowColorSpaceSync(napi_env env,napi_callback_info info)4684 napi_value JsWindow::OnGetWindowColorSpaceSync(napi_env env, napi_callback_info info)
4685 {
4686     wptr<Window> weakToken(windowToken_);
4687     auto window = weakToken.promote();
4688     if (window == nullptr) {
4689         WLOGFE("window is nullptr");
4690         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
4691     }
4692     ColorSpace colorSpace = window->GetColorSpace();
4693     WLOGI("Window [%{public}u, %{public}s] OnGetColorSpace end, colorSpace = %{public}u",
4694         window->GetWindowId(), window->GetWindowName().c_str(), static_cast<uint32_t>(colorSpace));
4695 
4696     return CreateJsValue(env, static_cast<uint32_t>(colorSpace));
4697 }
4698 
OnDump(napi_env env,napi_callback_info info)4699 napi_value JsWindow::OnDump(napi_env env, napi_callback_info info)
4700 {
4701     WLOGI("dump window start");
4702     size_t argc = 4;
4703     napi_value argv[4] = {nullptr};
4704     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4705     if (argc < 1 || argc > 2) { // 2: maximum params num
4706         WLOGFE("Argc is invalid: %{public}zu", argc);
4707         return nullptr;
4708     }
4709     if (windowToken_ == nullptr) {
4710         WLOGFE("window is nullptr or get invalid param");
4711         return nullptr;
4712     }
4713     std::vector<std::string> params;
4714     if (!ConvertNativeValueToVector(env, argv[0], params)) {
4715         WLOGFE("ConvertNativeValueToVector fail");
4716         return nullptr;
4717     }
4718     std::vector<std::string> dumpInfo;
4719     windowToken_->DumpInfo(params, dumpInfo);
4720     napi_value dumpInfoValue = CreateNativeArray(env, dumpInfo);
4721     WLOGI("Window [%{public}u, %{public}s] dump end",
4722         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
4723     return dumpInfoValue;
4724 }
4725 
Snapshot(napi_env env,napi_callback_info info)4726 napi_value JsWindow::Snapshot(napi_env env, napi_callback_info info)
4727 {
4728     WLOGI("Snapshot");
4729     JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
4730     return (me != nullptr) ? me->OnSnapshot(env, info) : nullptr;
4731 }
4732 
OnSetForbidSplitMove(napi_env env,napi_callback_info info)4733 napi_value JsWindow::OnSetForbidSplitMove(napi_env env, napi_callback_info info)
4734 {
4735     WmErrorCode errCode = WmErrorCode::WM_OK;
4736     size_t argc = 4;
4737     napi_value argv[4] = {nullptr};
4738     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4739     if (argc < 1) { // 1: params num
4740         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
4741     }
4742     bool isForbidSplitMove = false;
4743     if (errCode == WmErrorCode::WM_OK) {
4744         if (argv[0] == nullptr) {
4745             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
4746         } else {
4747             CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
4748                 napi_get_value_bool(env, argv[0], &isForbidSplitMove));
4749         }
4750     }
4751     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
4752         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
4753     }
4754     wptr<Window> weakToken(windowToken_);
4755     NapiAsyncTask::CompleteCallback complete =
4756         [weakToken, isForbidSplitMove](napi_env env, NapiAsyncTask& task, int32_t status) {
4757             auto weakWindow = weakToken.promote();
4758             if (weakWindow == nullptr) {
4759                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY,
4760                     "Invalidate params."));
4761                 return;
4762             }
4763             WmErrorCode ret;
4764             if (isForbidSplitMove) {
4765                 ret = WM_JS_TO_ERROR_CODE_MAP.at(
4766                     weakWindow->AddWindowFlag(WindowFlag::WINDOW_FLAG_FORBID_SPLIT_MOVE));
4767             } else {
4768                 ret = WM_JS_TO_ERROR_CODE_MAP.at(
4769                     weakWindow->RemoveWindowFlag(WindowFlag::WINDOW_FLAG_FORBID_SPLIT_MOVE));
4770             }
4771             if (ret == WmErrorCode::WM_OK) {
4772                 task.Resolve(env, NapiGetUndefined(env));
4773             } else {
4774                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window OnSetForbidSplitMove failed."));
4775             }
4776         };
4777     napi_value lastParam = (argc <= 1) ? nullptr :
4778         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
4779     napi_value result = nullptr;
4780     NapiAsyncTask::Schedule("JsWindow::OnSetForbidSplitMove",
4781         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
4782     return result;
4783 }
4784 
OnSnapshot(napi_env env,napi_callback_info info)4785 napi_value JsWindow::OnSnapshot(napi_env env, napi_callback_info info)
4786 {
4787     wptr<Window> weakToken(windowToken_);
4788     NapiAsyncTask::CompleteCallback complete =
4789         [weakToken](napi_env env, NapiAsyncTask& task, int32_t status) {
4790             auto weakWindow = weakToken.promote();
4791             if (weakWindow == nullptr) {
4792                 WLOGFE("window is nullptr");
4793                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
4794                 return;
4795             }
4796 
4797             std::shared_ptr<Media::PixelMap> pixelMap = weakWindow->Snapshot();
4798             if (pixelMap == nullptr) {
4799                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
4800                 WLOGFE("window snapshot get pixelmap is null");
4801                 return;
4802             }
4803 
4804             auto nativePixelMap = Media::PixelMapNapi::CreatePixelMap(env, pixelMap);
4805             if (nativePixelMap == nullptr) {
4806                 WLOGFE("window snapshot get nativePixelMap is null");
4807             }
4808             task.Resolve(env, nativePixelMap);
4809             WLOGI("Window [%{public}u, %{public}s] OnSnapshot, WxH=%{public}dx%{public}d",
4810                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(),
4811                 pixelMap->GetWidth(), pixelMap->GetHeight());
4812         };
4813     size_t argc = 4;
4814     napi_value argv[4] = {nullptr};
4815     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4816     napi_value lastParam = (argc == 0) ? nullptr :
4817         ((argv[0] != nullptr && GetType(env, argv[0]) == napi_function) ? argv[0] : nullptr);
4818     napi_value result = nullptr;
4819     NapiAsyncTask::Schedule("JsWindow::OnSnapshot",
4820         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
4821     return result;
4822 }
4823 
OnSetSnapshotSkip(napi_env env,napi_callback_info info)4824 napi_value JsWindow::OnSetSnapshotSkip(napi_env env, napi_callback_info info)
4825 {
4826     WmErrorCode errCode = WmErrorCode::WM_OK;
4827     size_t argc = 4;
4828     napi_value argv[4] = {nullptr};
4829     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4830     if (argc < 1) { // 1: params num
4831         WLOGFE(" inbalid param");
4832         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
4833     }
4834     bool isSkip = false;
4835     if (errCode == WmErrorCode::WM_OK) {
4836         napi_value nativeVal = argv[0];
4837         if (nativeVal == nullptr) {
4838             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
4839         } else {
4840             CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
4841                 napi_get_value_bool(env, nativeVal, &isSkip));
4842         }
4843     }
4844     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
4845         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
4846     }
4847 
4848     wptr<Window> weakToken(windowToken_);
4849     auto window = weakToken.promote();
4850     if (window == nullptr) {
4851         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
4852     }
4853 
4854     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(window->SetSnapshotSkip(isSkip));
4855     if (ret != WmErrorCode::WM_OK) {
4856         WLOGFE("Window SetSnapshotSkip failed");
4857         return NapiThrowError(env, ret);
4858     }
4859     WLOGI("[%{public}u, %{public}s] set snapshotSkip end",
4860         window->GetWindowId(), window->GetWindowName().c_str());
4861 
4862     return NapiGetUndefined(env);
4863 }
4864 
OnRaiseToAppTop(napi_env env,napi_callback_info info)4865 napi_value JsWindow::OnRaiseToAppTop(napi_env env, napi_callback_info info)
4866 {
4867     NapiAsyncTask::CompleteCallback complete =
4868         [this](napi_env env, NapiAsyncTask& task, int32_t status) {
4869             wptr<Window> weakToken(windowToken_);
4870             auto window = weakToken.promote();
4871             if (window == nullptr) {
4872                 WLOGFE("window is nullptr");
4873                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
4874                 return;
4875             }
4876 
4877             WmErrorCode errCode = WM_JS_TO_ERROR_CODE_MAP.at(window->RaiseToAppTop());
4878             if (errCode != WmErrorCode::WM_OK) {
4879                 WLOGFE("raise window zorder failed");
4880                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode));
4881                 return;
4882             }
4883             task.Resolve(env, NapiGetUndefined(env));
4884             WLOGI("Window [%{public}u, %{public}s] zorder raise success",
4885                 window->GetWindowId(), window->GetWindowName().c_str());
4886         };
4887     size_t argc = 4;
4888     napi_value argv[4] = {nullptr};
4889     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4890     napi_value lastParam = (argc == 0) ? nullptr :
4891         ((argv[0] != nullptr && GetType(env, argv[0]) == napi_function) ? argv[0] : nullptr);
4892     napi_value result = nullptr;
4893     NapiAsyncTask::Schedule("JsWindow::OnRaiseToAppTop",
4894         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
4895     return result;
4896 }
4897 
OnOpacity(napi_env env,napi_callback_info info)4898 napi_value JsWindow::OnOpacity(napi_env env, napi_callback_info info)
4899 {
4900     size_t argc = 4;
4901     napi_value argv[4] = {nullptr};
4902     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4903     if (argc < 1) {
4904         WLOGFE("Argc is invalid: %{public}zu", argc);
4905         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
4906     }
4907     if (windowToken_ == nullptr) {
4908         WLOGFE("WindowToken_ is nullptr");
4909         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
4910     }
4911     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
4912         WLOGFE("Opacity is not allowed since window is not system window");
4913         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
4914     }
4915     napi_value nativeVal = argv[0];
4916     if (nativeVal == nullptr) {
4917         WLOGFE("Failed to convert parameter to alpha");
4918         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
4919     }
4920     double alpha = 0.0;
4921     napi_status statusCode = napi_get_value_double(env, nativeVal, &alpha);
4922     if (statusCode != napi_ok) {
4923         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
4924     }
4925     if (MathHelper::LessNotEqual(alpha, 0.0) || MathHelper::GreatNotEqual(alpha, 1.0)) {
4926         WLOGFE("alpha should greater than 0 or smaller than 1.0");
4927         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
4928     }
4929 
4930     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetAlpha(alpha));
4931     if (ret != WmErrorCode::WM_OK) {
4932         WLOGFE("Window Opacity failed");
4933         return NapiThrowError(env, ret);
4934     }
4935     WLOGI("Window [%{public}u, %{public}s] Opacity end, alpha = %{public}f",
4936         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), alpha);
4937     return NapiGetUndefined(env);
4938 }
4939 
IsPivotValid(double data)4940 static bool IsPivotValid(double data)
4941 {
4942     if (MathHelper::LessNotEqual(data, 0.0) || (MathHelper::GreatNotEqual(data, 1.0))) {
4943         return false;
4944     }
4945     return true;
4946 }
4947 
IsScaleValid(double data)4948 static bool IsScaleValid(double data)
4949 {
4950     if (!MathHelper::GreatNotEqual(data, 0.0)) {
4951         return false;
4952     }
4953     return true;
4954 }
4955 
ParseScaleOption(napi_env env,napi_value jsObject,Transform & trans)4956 bool JsWindow::ParseScaleOption(napi_env env, napi_value jsObject, Transform& trans)
4957 {
4958     double data = 0.0f;
4959     if (ParseJsValue(jsObject, env, "pivotX", data)) {
4960         if (!IsPivotValid(data)) {
4961             return false;
4962         }
4963         trans.pivotX_ = data;
4964     }
4965     if (ParseJsValue(jsObject, env, "pivotY", data)) {
4966         if (!IsPivotValid(data)) {
4967             return false;
4968         }
4969         trans.pivotY_ = data;
4970     }
4971     if (ParseJsValue(jsObject, env, "x", data)) {
4972         if (!IsScaleValid(data)) {
4973             return false;
4974         }
4975         trans.scaleX_ = data;
4976     }
4977     if (ParseJsValue(jsObject, env, "y", data)) {
4978         if (!IsScaleValid(data)) {
4979             return false;
4980         }
4981         trans.scaleY_ = data;
4982     }
4983     return true;
4984 }
4985 
OnScale(napi_env env,napi_callback_info info)4986 napi_value JsWindow::OnScale(napi_env env, napi_callback_info info)
4987 {
4988     if (!Permission::IsSystemCalling()) {
4989         TLOGE(WmsLogTag::WMS_SYSTEM, "not system app, permission denied!");
4990         return NapiThrowError(env, WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
4991     }
4992 
4993     size_t argc = 4;
4994     napi_value argv[4] = {nullptr};
4995     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
4996     if (argc < 1) {
4997         WLOGFE("Argc is invalid: %{public}zu", argc);
4998         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
4999     }
5000     if (windowToken_ == nullptr) {
5001         WLOGFE("WindowToken_ is nullptr");
5002         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
5003     }
5004     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
5005         WLOGFE("Scale is not allowed since window is not system window");
5006         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
5007     }
5008     napi_value nativeObj = argv[0];
5009     if (nativeObj == nullptr) {
5010         WLOGFE("Failed to convert object to ScaleOptions");
5011         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5012     }
5013     auto trans = windowToken_->GetTransform();
5014     if (!ParseScaleOption(env, nativeObj, trans)) {
5015         WLOGFE(" PivotX or PivotY should between 0.0 ~ 1.0, scale should greater than 0.0");
5016         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5017     }
5018     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetTransform(trans));
5019     if (ret != WmErrorCode::WM_OK) {
5020         WLOGFE("Window Scale failed");
5021         return NapiThrowError(env, ret);
5022     }
5023     WLOGI("Window [%{public}u, %{public}s] Scale end",
5024         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
5025     WLOGI("scaleX = %{public}f, scaleY = %{public}f, pivotX = %{public}f pivotY = %{public}f",
5026         trans.scaleX_, trans.scaleY_, trans.pivotX_, trans.pivotY_);
5027     return NapiGetUndefined(env);
5028 }
5029 
ParseRotateOption(napi_env env,napi_value jsObject,Transform & trans)5030 bool JsWindow::ParseRotateOption(napi_env env, napi_value jsObject, Transform& trans)
5031 {
5032     double data = 0.0f;
5033     if (ParseJsValue(jsObject, env, "pivotX", data)) {
5034         if (!IsPivotValid(data)) {
5035             return false;
5036         }
5037         trans.pivotX_ = data;
5038     }
5039     if (ParseJsValue(jsObject, env, "pivotY", data)) {
5040         if (!IsPivotValid(data)) {
5041             return false;
5042         }
5043         trans.pivotY_ = data;
5044     }
5045     if (ParseJsValue(jsObject, env, "x", data)) {
5046         trans.rotationX_ = data;
5047     }
5048     if (ParseJsValue(jsObject, env, "y", data)) {
5049         trans.rotationY_ = data;
5050     }
5051     if (ParseJsValue(jsObject, env, "z", data)) {
5052         trans.rotationZ_ = data;
5053     }
5054     return true;
5055 }
5056 
OnRotate(napi_env env,napi_callback_info info)5057 napi_value JsWindow::OnRotate(napi_env env, napi_callback_info info)
5058 {
5059     if (!Permission::IsSystemCalling()) {
5060         TLOGE(WmsLogTag::WMS_SYSTEM, "not system app, permission denied!");
5061         return NapiThrowError(env, WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
5062     }
5063 
5064     size_t argc = 4;
5065     napi_value argv[4] = {nullptr};
5066     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5067     if (argc < 1) {
5068         WLOGFE("Argc is invalid: %{public}zu", argc);
5069         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5070     }
5071     if (windowToken_ == nullptr) {
5072         WLOGFE("WindowToken_ is nullptr");
5073         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
5074     }
5075     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
5076         WLOGFE("Rotate is not allowed since window is not system window");
5077         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
5078     }
5079     napi_value nativeObj = argv[0];
5080     if (nativeObj == nullptr) {
5081         WLOGFE("Failed to convert object to RotateOptions");
5082         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5083     }
5084     // cannot use sync task since next transform base on current transform
5085     auto trans = windowToken_->GetTransform();
5086     if (!ParseRotateOption(env, nativeObj, trans)) {
5087         WLOGFE(" PivotX or PivotY should between 0.0 ~ 1.0");
5088         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5089     }
5090     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetTransform(trans));
5091     if (ret != WmErrorCode::WM_OK) {
5092         WLOGFE("Window Rotate failed");
5093         return NapiThrowError(env, ret);
5094     }
5095     WLOGI("Window [%{public}u, %{public}s] Rotate end",
5096         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
5097     WLOGI("rotateX = %{public}f, rotateY = %{public}f," \
5098         "rotateZ = %{public}f pivotX = %{public}f pivotY = %{public}f",
5099         trans.rotationX_, trans.rotationY_, trans.rotationZ_, trans.pivotX_, trans.pivotY_);
5100     return NapiGetUndefined(env);
5101 }
5102 
ParseTranslateOption(napi_env env,napi_value jsObject,Transform & trans)5103 bool JsWindow::ParseTranslateOption(napi_env env, napi_value jsObject, Transform& trans)
5104 {
5105     double data = 0.0f;
5106     if (ParseJsValue(jsObject, env, "x", data)) {
5107         trans.translateX_ = data;
5108     }
5109     if (ParseJsValue(jsObject, env, "y", data)) {
5110         trans.translateY_ = data;
5111     }
5112     if (ParseJsValue(jsObject, env, "z", data)) {
5113         trans.translateZ_ = data;
5114     }
5115     return true;
5116 }
5117 
OnTranslate(napi_env env,napi_callback_info info)5118 napi_value JsWindow::OnTranslate(napi_env env, napi_callback_info info)
5119 {
5120     if (!Permission::IsSystemCalling()) {
5121         TLOGE(WmsLogTag::WMS_SYSTEM, "not system app, permission denied!");
5122         return NapiThrowError(env, WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
5123     }
5124 
5125     size_t argc = 4;
5126     napi_value argv[4] = {nullptr};
5127     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5128     if (argc < 1) {
5129         WLOGFE("Argc is invalid: %{public}zu", argc);
5130         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5131     }
5132     if (windowToken_ == nullptr) {
5133         WLOGFE("WindowToken_ is nullptr");
5134         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
5135     }
5136     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
5137         WLOGFE("Translate is not allowed since window is not system window");
5138         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
5139     }
5140     napi_value nativeObj = argv[0];
5141     if (nativeObj == nullptr) {
5142         WLOGFE("Failed to convert object to TranslateOptions");
5143         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5144     }
5145     auto trans = windowToken_->GetTransform();
5146     if (!ParseTranslateOption(env, nativeObj, trans)) {
5147         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5148     }
5149     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetTransform(trans));
5150     if (ret != WmErrorCode::WM_OK) {
5151         WLOGFE("Window Translate failed");
5152         return NapiThrowError(env, ret);
5153     }
5154     WLOGI("Window [%{public}u, %{public}s] Translate end," \
5155         "translateX = %{public}f, translateY = %{public}f, translateZ = %{public}f",
5156         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(),
5157         trans.translateX_, trans.translateY_, trans.translateZ_);
5158     return NapiGetUndefined(env);
5159 }
5160 
CreateTransitionController(napi_env env)5161 WmErrorCode JsWindow::CreateTransitionController(napi_env env)
5162 {
5163     if (windowToken_ == nullptr) {
5164         WLOGFE("windowToken_ is nullptr not match");
5165         return WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
5166     }
5167     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
5168         WLOGFE("CreateTransitionController is not allowed since window is not system window");
5169         return WmErrorCode::WM_ERROR_INVALID_CALLING;
5170     }
5171     napi_value objValue = nullptr;
5172     napi_create_object(env, &objValue);
5173     if (objValue == nullptr) {
5174         WLOGFE("Failed to convert to TransitionController Object");
5175         return WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
5176     }
5177     auto name = GetWindowName();
5178     std::shared_ptr<NativeReference> jsWindowObj = FindJsWindowObject(name);
5179     if (jsWindowObj == nullptr || jsWindowObj->GetNapiValue() == nullptr) {
5180         return WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
5181     }
5182     sptr<JsTransitionController> nativeController = new JsTransitionController(
5183         env, jsWindowObj, windowToken_);
5184     auto nativeControllerVal = new wptr<JsTransitionController>(nativeController);
5185     auto finalizer = [](napi_env, void* data, void*) {
5186         WLOGFI("Finalizer for wptr JsTransitionController called");
5187         delete static_cast<wptr<JsTransitionController>*>(data);
5188     };
5189     if (napi_wrap(env, objValue, nativeControllerVal, finalizer, nullptr, nullptr) != napi_ok) {
5190         finalizer(env, nativeControllerVal, nullptr);
5191         WLOGFE("Failed to wrap TransitionController Object");
5192         return WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
5193     };
5194     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->RegisterAnimationTransitionController(nativeController));
5195     napi_ref result = nullptr;
5196     napi_create_reference(env, objValue, 1, &result);
5197     jsTransControllerObj_.reset(reinterpret_cast<NativeReference*>(result));
5198     nativeController->SetJsController(jsTransControllerObj_);
5199     WLOGI("Window [%{public}u, %{public}s] CreateTransitionController end",
5200         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
5201     return ret;
5202 }
5203 
OnGetTransitionController(napi_env env,napi_callback_info info)5204 napi_value JsWindow::OnGetTransitionController(napi_env env, napi_callback_info info)
5205 {
5206     if (!Permission::IsSystemCalling()) {
5207         TLOGE(WmsLogTag::WMS_SYSTEM, "not system app, permission denied!");
5208         return NapiThrowError(env, WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
5209     }
5210 
5211     if (windowToken_ == nullptr) {
5212         WLOGFE("WindowToken_ is nullptr");
5213         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
5214     }
5215     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
5216         WLOGFE("OnGetTransitionController is not allowed since window is not system window");
5217         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
5218     }
5219     if (jsTransControllerObj_ == nullptr || jsTransControllerObj_->GetNapiValue() == nullptr) {
5220         WmErrorCode ret = CreateTransitionController(env);
5221         if (ret != WmErrorCode::WM_OK) {
5222             WLOGFE("Window GetTransitionController failed");
5223             napi_throw(env, JsErrUtils::CreateJsError(env, ret));
5224         }
5225     }
5226     return jsTransControllerObj_ == nullptr ? nullptr : jsTransControllerObj_->GetNapiValue();
5227 }
5228 
OnSetCornerRadius(napi_env env,napi_callback_info info)5229 napi_value JsWindow::OnSetCornerRadius(napi_env env, napi_callback_info info)
5230 {
5231     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
5232         WLOGFE("set corner radius permission denied!");
5233         return NapiThrowError(env, WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
5234     }
5235 
5236     size_t argc = 4;
5237     napi_value argv[4] = {nullptr};
5238     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5239     if (argc < 1) {
5240         WLOGFE("Argc is invalid: %{public}zu", argc);
5241         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5242     }
5243     if (windowToken_ == nullptr) {
5244         WLOGFE("WindowToken_ is nullptr");
5245         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
5246     }
5247     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
5248         WLOGFE("SetCornerRadius is not allowed since window is not system window");
5249         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
5250     }
5251     napi_value nativeVal = argv[0];
5252     if (nativeVal == nullptr) {
5253         WLOGFE("SetCornerRadius invalid radius due to nativeVal is nullptr");
5254         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5255     }
5256     double radius = 0.0;
5257     napi_status statusCode = napi_get_value_double(env, nativeVal, &radius);
5258     if (statusCode != napi_ok) {
5259         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5260     }
5261     if (MathHelper::LessNotEqual(radius, 0.0)) {
5262         WLOGFE("SetCornerRadius invalid radius");
5263         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5264     }
5265     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetCornerRadius(radius));
5266     if (ret != WmErrorCode::WM_OK) {
5267         WLOGFE("Window SetCornerRadius failed");
5268         return NapiThrowError(env, ret);
5269     }
5270     WLOGI("Window [%{public}u, %{public}s] SetCornerRadius end, radius = %{public}f",
5271         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), radius);
5272     return NapiGetUndefined(env);
5273 }
5274 
OnSetShadow(napi_env env,napi_callback_info info)5275 napi_value JsWindow::OnSetShadow(napi_env env, napi_callback_info info)
5276 {
5277     WmErrorCode ret = WmErrorCode::WM_OK;
5278     double result = 0.0;
5279     size_t argc = 4;
5280     napi_value argv[4] = {nullptr};
5281     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5282     if (argc < 1) { // 1: min param num
5283         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5284     }
5285     if (windowToken_ == nullptr) {
5286         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
5287     }
5288     if (!WindowHelper::IsSystemWindow(windowToken_->GetType()) &&
5289         !WindowHelper::IsSubWindow(windowToken_->GetType())) {
5290         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
5291     }
5292 
5293     if (argv[0] == nullptr) {
5294         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5295     }
5296     napi_status statusCode = napi_get_value_double(env, argv[0], &result);
5297     if (statusCode != napi_ok) {
5298         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5299     }
5300     if (MathHelper::LessNotEqual(result, 0.0)) {
5301         return NapiThrowError(env,  WmErrorCode::WM_ERROR_INVALID_PARAM);
5302     }
5303     ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetShadowRadius(result));
5304     if ((ret == WmErrorCode::WM_OK) && (argc >= 2)) { // parse the 2nd param: color
5305         std::string color;
5306         if (ConvertFromJsValue(env, argv[1], color)) {
5307             ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetShadowColor(color));
5308         }
5309     }
5310 
5311     if ((ret == WmErrorCode::WM_OK) && argc >= 3) { // parse the 3rd param: offsetX
5312         if (argv[2] != nullptr) { // 2: the 3rd param
5313             napi_get_value_double(env, argv[2], &result);
5314             ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetShadowOffsetX(result));
5315         }
5316     }
5317 
5318     if ((ret == WmErrorCode::WM_OK) && argc >= 4) { // parse the 4th param: offsetY
5319         if (argv[3] != nullptr) {  // 3: the 4th param
5320             napi_get_value_double(env, argv[3], &result);
5321             ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetShadowOffsetY(result));
5322         }
5323     }
5324 
5325     if (ret != WmErrorCode::WM_OK) {
5326         napi_throw(env, JsErrUtils::CreateJsError(env, ret));
5327     }
5328 
5329     return NapiGetUndefined(env);
5330 }
5331 
OnSetBlur(napi_env env,napi_callback_info info)5332 napi_value JsWindow::OnSetBlur(napi_env env, napi_callback_info info)
5333 {
5334     size_t argc = 4;
5335     napi_value argv[4] = {nullptr};
5336     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5337     if (argc < 1) {
5338         WLOGFE("Argc is invalid: %{public}zu", argc);
5339         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5340     }
5341     if (windowToken_ == nullptr) {
5342         WLOGFE("WindowToken_ is nullptr");
5343         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
5344     }
5345     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
5346         WLOGFE("SetBlur is not allowed since window is not system window");
5347         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
5348     }
5349     napi_value nativeVal = argv[0];
5350     if (nativeVal == nullptr) {
5351         WLOGFE("SetBlur invalid radius due to nativeVal is nullptr");
5352         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5353     }
5354     double radius = 0.0;
5355     napi_status statusCode = napi_get_value_double(env, nativeVal, &radius);
5356     if (statusCode != napi_ok) {
5357         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5358     }
5359     if (MathHelper::LessNotEqual(radius, 0.0)) {
5360         WLOGFE("SetBlur invalid radius");
5361         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5362     }
5363     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetBlur(radius));
5364     if (ret != WmErrorCode::WM_OK) {
5365         WLOGFE("Window SetBlur failed");
5366         return NapiThrowError(env, ret);
5367     }
5368     WLOGI("Window [%{public}u, %{public}s] SetBlur end, radius = %{public}f",
5369         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), radius);
5370     return NapiGetUndefined(env);
5371 }
5372 
OnSetBackdropBlur(napi_env env,napi_callback_info info)5373 napi_value JsWindow::OnSetBackdropBlur(napi_env env, napi_callback_info info)
5374 {
5375     size_t argc = 4;
5376     napi_value argv[4] = {nullptr};
5377     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5378     if (argc < 1) {
5379         WLOGFE("Argc is invalid: %{public}zu", argc);
5380         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5381     }
5382     if (windowToken_ == nullptr) {
5383         WLOGFE("WindowToken_ is nullptr");
5384         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
5385     }
5386     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
5387         WLOGFE("SetBackdropBlur is not allowed since window is not system window");
5388         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
5389     }
5390     napi_value nativeVal = argv[0];
5391     if (nativeVal == nullptr) {
5392         WLOGFE("SetBackdropBlur invalid radius due to nativeVal is nullptr");
5393         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5394     }
5395     double radius = 0.0;
5396     napi_status statusCode = napi_get_value_double(env, nativeVal, &radius);
5397     if (statusCode != napi_ok) {
5398         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5399     }
5400     if (MathHelper::LessNotEqual(radius, 0.0)) {
5401         WLOGFE("SetBackdropBlur invalid radius");
5402         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5403     }
5404     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetBackdropBlur(radius));
5405     if (ret != WmErrorCode::WM_OK) {
5406         WLOGFE("Window SetBackdropBlur failed");
5407         return NapiThrowError(env, ret);
5408     }
5409     WLOGI("Window [%{public}u, %{public}s] SetBackdropBlur end, radius = %{public}f",
5410         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), radius);
5411     return NapiGetUndefined(env);
5412 }
5413 
OnSetBackdropBlurStyle(napi_env env,napi_callback_info info)5414 napi_value JsWindow::OnSetBackdropBlurStyle(napi_env env, napi_callback_info info)
5415 {
5416     size_t argc = 4;
5417     napi_value argv[4] = {nullptr};
5418     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5419     if (argc < 1) {
5420         WLOGFE("Argc is invalid: %{public}zu", argc);
5421         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5422     }
5423     if (windowToken_ == nullptr) {
5424         WLOGFE("WindowToken_ is nullptr");
5425         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
5426     }
5427     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
5428         WLOGFE("SetBackdropBlurStyle is not allowed since window is not system window");
5429         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
5430     }
5431 
5432     napi_value nativeMode = argv[0];
5433     if (nativeMode == nullptr) {
5434         WLOGFE("SetBackdropBlurStyle Invalid window blur style due to nativeMode is nullptr");
5435         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5436     }
5437     uint32_t resultValue = 0;
5438     napi_status statusCode = napi_get_value_uint32(env, nativeMode, &resultValue);
5439     if (statusCode != napi_ok) {
5440         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5441     }
5442     if (resultValue > static_cast<uint32_t>(WindowBlurStyle::WINDOW_BLUR_THICK)) {
5443         WLOGFE("SetBackdropBlurStyle Invalid window blur style");
5444         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5445     }
5446     WindowBlurStyle style = static_cast<WindowBlurStyle>(resultValue);
5447     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetBackdropBlurStyle(style));
5448     if (ret != WmErrorCode::WM_OK) {
5449         WLOGFE("Window SetBackdropBlurStyle failed");
5450         return NapiThrowError(env, ret);
5451     }
5452 
5453     WLOGI("Window [%{public}u, %{public}s] SetBackdropBlurStyle end, style = %{public}u",
5454         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), style);
5455     return NapiGetUndefined(env);
5456 }
5457 
OnSetWaterMarkFlag(napi_env env,napi_callback_info info)5458 napi_value JsWindow::OnSetWaterMarkFlag(napi_env env, napi_callback_info info)
5459 {
5460     size_t argc = 4;
5461     napi_value argv[4] = {nullptr};
5462     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5463     if (argc < 1) {
5464         WLOGFE("Argc is invalid: %{public}zu", argc);
5465         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5466     }
5467 
5468     napi_value nativeBool = argv[0];
5469     if (nativeBool == nullptr) {
5470         WLOGFE("SetWaterMarkFlag Invalid window flag");
5471         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5472     }
5473 
5474     bool isAddSafetyLayer = false;
5475     napi_status statusCode = napi_get_value_bool(env, nativeBool, &isAddSafetyLayer);
5476     if (statusCode != napi_ok) {
5477         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5478     }
5479     wptr<Window> weakToken(windowToken_);
5480     NapiAsyncTask::CompleteCallback complete =
5481         [weakToken, isAddSafetyLayer](napi_env env, NapiAsyncTask& task, int32_t status) {
5482             auto window = weakToken.promote();
5483             if (window == nullptr) {
5484                 task.Reject(env,
5485                     JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY,
5486                     "OnSetWaterMarkFlag failed."));
5487                 return;
5488             }
5489             WMError ret = WMError::WM_OK;
5490             if (isAddSafetyLayer) {
5491                 ret = window->AddWindowFlag(WindowFlag::WINDOW_FLAG_WATER_MARK);
5492             } else {
5493                 ret = window->RemoveWindowFlag(WindowFlag::WINDOW_FLAG_WATER_MARK);
5494             }
5495             if (ret == WMError::WM_OK) {
5496                 task.Resolve(env, NapiGetUndefined(env));
5497             } else {
5498                 task.Reject(env, JsErrUtils::CreateJsError(env, WM_JS_TO_ERROR_CODE_MAP.at(ret),
5499                     "SetWaterMarkFlag failed."));
5500             }
5501             WLOGI("[NAPI]Window [%{public}u, %{public}s] set waterMark flag end, ret = %{public}d",
5502                 window->GetWindowId(), window->GetWindowName().c_str(), ret);
5503         };
5504 
5505     napi_value lastParam = (argc == 1) ? nullptr :
5506         (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
5507     napi_value result = nullptr;
5508     NapiAsyncTask::Schedule("JsWindow::OnSetWaterMarkFlag",
5509         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
5510     return result;
5511 }
5512 
OnSetHandwritingFlag(napi_env env,napi_callback_info info)5513 napi_value JsWindow::OnSetHandwritingFlag(napi_env env, napi_callback_info info)
5514 {
5515     size_t argc = 4;
5516     napi_value argv[4] = {nullptr};
5517     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5518     if (argc < 1) {
5519         WLOGFE("Argc is invalid: %{public}zu", argc);
5520         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5521     }
5522 
5523     napi_value nativeBool = argv[0];
5524     if (nativeBool == nullptr) {
5525         WLOGFE("SetHandwritingFlag Invalid window flag");
5526         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5527     }
5528     bool isAddFlag = false;
5529     napi_get_value_bool(env, nativeBool, &isAddFlag);
5530     wptr<Window> weakToken(windowToken_);
5531     std::shared_ptr<WmErrorCode> errCodePtr = std::make_shared<WmErrorCode>(WmErrorCode::WM_OK);
5532     NapiAsyncTask::ExecuteCallback execute = [weakToken, isAddFlag, errCodePtr]() {
5533         if (errCodePtr == nullptr) {
5534             return;
5535         }
5536         auto weakWindow = weakToken.promote();
5537         if (weakWindow == nullptr) {
5538             *errCodePtr = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
5539             return;
5540         }
5541         WMError ret = isAddFlag ? weakWindow->AddWindowFlag(WindowFlag::WINDOW_FLAG_HANDWRITING) :
5542             weakWindow->RemoveWindowFlag(WindowFlag::WINDOW_FLAG_HANDWRITING);
5543         *errCodePtr = WM_JS_TO_ERROR_CODE_MAP.at(ret);
5544         WLOGI("Window [%{public}u, %{public}s] set handwriting flag on end",
5545             weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
5546     };
5547     NapiAsyncTask::CompleteCallback complete =
5548         [weakToken, isAddFlag, errCodePtr](napi_env env, NapiAsyncTask& task, int32_t status) {
5549             if (errCodePtr == nullptr) {
5550                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY,
5551                     "System abnormal."));
5552                 return;
5553             }
5554             if (*errCodePtr == WmErrorCode::WM_OK) {
5555                 task.Resolve(env, NapiGetUndefined(env));
5556             } else {
5557                 task.Reject(env, JsErrUtils::CreateJsError(env, *errCodePtr, "SetHandwritingFlag failed."));
5558             }
5559         };
5560 
5561     napi_value lastParam = (argc == 1) ? nullptr : (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
5562     napi_value result = nullptr;
5563     NapiAsyncTask::Schedule("JsWindow::OnSetHandwritingFlag",
5564         env, CreateAsyncTaskWithLastParam(env, lastParam, std::move(execute), std::move(complete), &result));
5565     return result;
5566 }
5567 
OnSetAspectRatio(napi_env env,napi_callback_info info)5568 napi_value JsWindow::OnSetAspectRatio(napi_env env, napi_callback_info info)
5569 {
5570     WMError errCode = WMError::WM_OK;
5571     size_t argc = 4;
5572     napi_value argv[4] = {nullptr};
5573     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5574     if (argc < 1 || argc > 2) { // 2: maximum params num
5575         WLOGFE("[NAPI]Argc is invalid: %{public}zu", argc);
5576         errCode = WMError::WM_ERROR_INVALID_PARAM;
5577     }
5578 
5579     if (windowToken_ == nullptr) {
5580         WLOGFE("WindowToken_ is nullptr");
5581         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
5582     }
5583 
5584     if (!WindowHelper::IsMainWindow(windowToken_->GetType())) {
5585         WLOGFE("[NAPI]SetAspectRatio is not allowed since window is main window");
5586         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
5587     }
5588 
5589     double aspectRatio = 0.0;
5590     if (errCode == WMError::WM_OK) {
5591         napi_value nativeVal = argv[0];
5592         if (nativeVal == nullptr) {
5593             errCode = WMError::WM_ERROR_INVALID_PARAM;
5594         } else {
5595             CHECK_NAPI_RETCODE(errCode, WMError::WM_ERROR_INVALID_PARAM,
5596                 napi_get_value_double(env, nativeVal, &aspectRatio));
5597         }
5598     }
5599 
5600     if (errCode == WMError::WM_ERROR_INVALID_PARAM || aspectRatio <= 0.0) {
5601         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5602     }
5603 
5604     wptr<Window> weakToken(windowToken_);
5605     NapiAsyncTask::CompleteCallback complete =
5606         [weakToken, aspectRatio](napi_env env, NapiAsyncTask& task, int32_t status) {
5607             auto weakWindow = weakToken.promote();
5608             if (weakWindow == nullptr) {
5609                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY,
5610                     "OnSetAspectRatio failed."));
5611                 return;
5612             }
5613             WMError ret = weakWindow->SetAspectRatio(aspectRatio);
5614             if (ret == WMError::WM_OK) {
5615                 task.Resolve(env, NapiGetUndefined(env));
5616             } else {
5617                 task.Reject(env, JsErrUtils::CreateJsError(env, WM_JS_TO_ERROR_CODE_MAP.at(ret),
5618                     "SetAspectRatio failed."));
5619             }
5620             WLOGI("[NAPI]Window [%{public}u, %{public}s] set aspect ratio end, ret = %{public}d",
5621                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
5622         };
5623 
5624     napi_value lastParam = (argc == 1) ? nullptr : (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
5625     napi_value result = nullptr;
5626     NapiAsyncTask::Schedule("JsWindow::SetAspectRatio",
5627         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
5628     return result;
5629 }
5630 
OnResetAspectRatio(napi_env env,napi_callback_info info)5631 napi_value JsWindow::OnResetAspectRatio(napi_env env, napi_callback_info info)
5632 {
5633     size_t argc = 4;
5634     napi_value argv[4] = {nullptr};
5635     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5636     if (argc > 1) {
5637         WLOGFE("[NAPI]Argc is invalid: %{public}zu", argc);
5638         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5639     }
5640 
5641     if (windowToken_ == nullptr) {
5642         WLOGFE("WindowToken_ is nullptr");
5643         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
5644     }
5645 
5646     if (!WindowHelper::IsMainWindow(windowToken_->GetType())) {
5647         WLOGFE("[NAPI]ResetAspectRatio is not allowed since window is main window");
5648         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
5649     }
5650 
5651     wptr<Window> weakToken(windowToken_);
5652     NapiAsyncTask::CompleteCallback complete =
5653         [weakToken](napi_env env, NapiAsyncTask& task, int32_t status) {
5654             auto weakWindow = weakToken.promote();
5655             if (weakWindow == nullptr) {
5656                 task.Reject(env,
5657                     JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY,
5658                     "OnResetAspectRatio failed."));
5659                 return;
5660             }
5661             WMError ret = weakWindow->ResetAspectRatio();
5662             if (ret == WMError::WM_OK) {
5663                 task.Resolve(env, NapiGetUndefined(env));
5664             } else {
5665                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "ResetAspectRatio failed."));
5666             }
5667             WLOGI("[NAPI]Window [%{public}u, %{public}s] reset aspect ratio end, ret = %{public}d",
5668                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
5669         };
5670 
5671     napi_value lastParam = (argc == 0) ? nullptr :
5672         (GetType(env, argv[0]) == napi_function ? argv[0] : nullptr);
5673     napi_value result = nullptr;
5674     NapiAsyncTask::Schedule("JsWindow::OnResetAspectRatio",
5675         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
5676     return result;
5677 }
5678 
OnMinimize(napi_env env,napi_callback_info info)5679 napi_value JsWindow::OnMinimize(napi_env env, napi_callback_info info)
5680 {
5681     WmErrorCode errCode = WmErrorCode::WM_OK;
5682     errCode = (windowToken_ == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : WmErrorCode::WM_OK;
5683     size_t argc = 4;
5684     napi_value argv[4] = {nullptr};
5685     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5686     if (argc > 1) {
5687         WLOGFE("[NAPI]Argc is invalid: %{public}zu", argc);
5688         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
5689     }
5690 
5691     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
5692         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5693     }
5694     if (errCode == WmErrorCode::WM_OK && WindowHelper::IsSubWindow(windowToken_->GetType())) {
5695         WLOGFE("subWindow hide");
5696         return HideWindowFunction(env, info, WmErrorCode::WM_OK);
5697     }
5698 
5699     wptr<Window> weakToken(windowToken_);
5700     NapiAsyncTask::CompleteCallback complete =
5701         [weakToken, errCode](napi_env env, NapiAsyncTask& task, int32_t status) mutable {
5702             auto weakWindow = weakToken.promote();
5703             errCode = (weakWindow == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : errCode;
5704             if (errCode != WmErrorCode::WM_OK) {
5705                 task.Reject(env,
5706                     JsErrUtils::CreateJsError(env, errCode, "OnMinimize failed."));
5707                 WLOGFE("window is nullptr");
5708                 return;
5709             }
5710             WMError ret = weakWindow->Minimize();
5711             if (ret == WMError::WM_OK) {
5712                 task.Resolve(env, NapiGetUndefined(env));
5713             } else {
5714                 WmErrorCode wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(ret);
5715                 task.Reject(env, JsErrUtils::CreateJsError(env, wmErrorCode, "Minimize failed."));
5716             }
5717             WLOGI("[NAPI]Window [%{public}u, %{public}s] minimize end, ret = %{public}d",
5718                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
5719         };
5720 
5721     napi_value lastParam = (argc == 0) ? nullptr :
5722         (GetType(env, argv[0]) == napi_function ? argv[0] : nullptr);
5723     napi_value result = nullptr;
5724     NapiAsyncTask::Schedule("JsWindow::OnMinimize",
5725         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
5726     return result;
5727 }
5728 
OnMaximize(napi_env env,napi_callback_info info)5729 napi_value JsWindow::OnMaximize(napi_env env, napi_callback_info info)
5730 {
5731     WmErrorCode errCode = WmErrorCode::WM_OK;
5732     if (windowToken_ == nullptr) {
5733         WLOGFE("WindowToken_ is nullptr");
5734         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
5735     }
5736     size_t argc = 4;
5737     napi_value argv[4] = {nullptr};
5738     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5739     wptr<Window> weakToken(windowToken_);
5740     if (!WindowHelper::IsMainWindow(weakToken->GetType())) {
5741         WLOGFE("[NAPI] maximize interface only support main Window");
5742         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
5743     }
5744     MaximizePresentation presentation = MaximizePresentation::ENTER_IMMERSIVE;
5745     if (argc == 1) {
5746         int32_t nativeValue;
5747         CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
5748             napi_get_value_int32(env, argv[0], &nativeValue));
5749         presentation = static_cast<MaximizePresentation>(nativeValue);
5750     }
5751     if (errCode != WmErrorCode::WM_OK) {
5752         return NapiThrowError(env, errCode);
5753     }
5754     NapiAsyncTask::CompleteCallback complete =
5755         [weakToken, presentation](napi_env env, NapiAsyncTask& task, int32_t status) mutable {
5756             auto weakWindow = weakToken.promote();
5757             if (weakWindow == nullptr) {
5758                 task.Reject(env,
5759                     JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY, "OnMaximize failed."));
5760                 return;
5761             }
5762             WMError ret = weakWindow->Maximize(presentation);
5763             if (ret == WMError::WM_OK) {
5764                 task.Resolve(env, NapiGetUndefined(env));
5765             } else {
5766                 WmErrorCode wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(ret);
5767                 task.Reject(env, JsErrUtils::CreateJsError(env, wmErrorCode, "Maximize failed."));
5768             }
5769         };
5770 
5771     napi_value lastParam = (argc == 0) ? nullptr :
5772         (GetType(env, argv[0]) == napi_function ? argv[0] : nullptr);
5773     napi_value result = nullptr;
5774     NapiAsyncTask::Schedule("JsWindow::OnMaximize",
5775         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
5776     return result;
5777 }
5778 
FindJsWindowObject(const std::string & windowName)5779 std::shared_ptr<NativeReference> FindJsWindowObject(const std::string& windowName)
5780 {
5781     WLOGFD("Try to find window %{public}s in g_jsWindowMap", windowName.c_str());
5782     std::lock_guard<std::mutex> lock(g_mutex);
5783     if (g_jsWindowMap.find(windowName) == g_jsWindowMap.end()) {
5784         WLOGFD("Can not find window %{public}s in g_jsWindowMap", windowName.c_str());
5785         return nullptr;
5786     }
5787     return g_jsWindowMap[windowName];
5788 }
5789 
CreateJsWindowObject(napi_env env,sptr<Window> & window)5790 napi_value CreateJsWindowObject(napi_env env, sptr<Window>& window)
5791 __attribute__((no_sanitize("cfi")))
5792 {
5793     std::string windowName = window->GetWindowName();
5794     // avoid repeatedly create js window when getWindow
5795     std::shared_ptr<NativeReference> jsWindowObj = FindJsWindowObject(windowName);
5796     if (jsWindowObj != nullptr && jsWindowObj->GetNapiValue() != nullptr) {
5797         WLOGD("FindJsWindowObject %{public}s", windowName.c_str());
5798         return jsWindowObj->GetNapiValue();
5799     }
5800     napi_value objValue = nullptr;
5801     napi_create_object(env, &objValue);
5802 
5803     WLOGI("CreateJsWindow %{public}s", windowName.c_str());
5804     std::unique_ptr<JsWindow> jsWindow = std::make_unique<JsWindow>(window);
5805     napi_wrap(env, objValue, jsWindow.release(), JsWindow::Finalizer, nullptr, nullptr);
5806 
5807     BindFunctions(env, objValue, "JsWindow");
5808 
5809     std::shared_ptr<NativeReference> jsWindowRef;
5810     napi_ref result = nullptr;
5811     napi_create_reference(env, objValue, 1, &result);
5812     jsWindowRef.reset(reinterpret_cast<NativeReference*>(result));
5813     std::lock_guard<std::mutex> lock(g_mutex);
5814     g_jsWindowMap[windowName] = jsWindowRef;
5815     return objValue;
5816 }
5817 
ParseWindowLimits(napi_env env,napi_value jsObject,WindowLimits & windowLimits)5818 bool JsWindow::ParseWindowLimits(napi_env env, napi_value jsObject, WindowLimits& windowLimits)
5819 {
5820     uint32_t data = 0;
5821     if (ParseJsValue(jsObject, env, "maxWidth", data)) {
5822         windowLimits.maxWidth_ = data;
5823     } else {
5824         WLOGFE("Failed to convert object to windowLimits");
5825         return false;
5826     }
5827     if (ParseJsValue(jsObject, env, "minWidth", data)) {
5828         windowLimits.minWidth_ = data;
5829     } else {
5830         WLOGFE("Failed to convert object to windowLimits");
5831         return false;
5832     }
5833     if (ParseJsValue(jsObject, env, "maxHeight", data)) {
5834         windowLimits.maxHeight_ = data;
5835     } else {
5836         WLOGFE("Failed to convert object to windowLimits");
5837         return false;
5838     }
5839     if (ParseJsValue(jsObject, env, "minHeight", data)) {
5840         windowLimits.minHeight_ = data;
5841     } else {
5842         WLOGFE("Failed to convert object to windowLimits");
5843         return false;
5844     }
5845     return true;
5846 }
5847 
OnSetWindowLimits(napi_env env,napi_callback_info info)5848 napi_value JsWindow::OnSetWindowLimits(napi_env env, napi_callback_info info)
5849 {
5850     size_t argc = 4;
5851     napi_value argv[4] = {nullptr};
5852     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5853     if (argc < 1) {
5854         WLOGFE("Argc is invalid: %{public}zu", argc);
5855         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5856     }
5857     napi_value nativeObj = argv[0];
5858     if (nativeObj == nullptr) {
5859         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5860     }
5861     WindowLimits windowLimits;
5862     if (!ParseWindowLimits(env, nativeObj, windowLimits)) {
5863         WLOGFE("Failed to convert object to windowLimits");
5864         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5865     }
5866     if (windowLimits.maxWidth_ < 0 || windowLimits.maxHeight_ < 0 ||
5867         windowLimits.minWidth_ < 0 || windowLimits.minHeight_ < 0) {
5868         WLOGFE("Width or height should be greater than or equal to 0");
5869         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5870     }
5871     wptr<Window> weakToken(windowToken_);
5872     NapiAsyncTask::CompleteCallback complete =
5873         [weakToken, windowLimits](napi_env env, NapiAsyncTask& task, int32_t status) {
5874             WindowLimits sizeLimits(windowLimits);
5875             auto weakWindow = weakToken.promote();
5876             if (weakWindow == nullptr) {
5877                 task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
5878                 return;
5879             }
5880             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetWindowLimits(sizeLimits));
5881             if (ret == WmErrorCode::WM_OK) {
5882                 auto objValue = GetWindowLimitsAndConvertToJsValue(env, sizeLimits);
5883                 if (objValue == nullptr) {
5884                     task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY,
5885                         "Window set window limits failed"));
5886                 } else {
5887                     task.Resolve(env, objValue);
5888                 }
5889             } else {
5890                 task.Reject(env, JsErrUtils::CreateJsError(env, ret, "Window set window limits failed"));
5891             }
5892         };
5893     napi_value lastParam = (argc <= 1) ? nullptr : (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
5894     napi_value result = nullptr;
5895     NapiAsyncTask::Schedule("JsWindow::OnSetWindowLimits",
5896         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
5897     return result;
5898 }
5899 
OnGetWindowLimits(napi_env env,napi_callback_info info)5900 napi_value JsWindow::OnGetWindowLimits(napi_env env, napi_callback_info info)
5901 {
5902     size_t argc = 4;
5903     napi_value argv[4] = {nullptr};
5904     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5905     if (argc > 1) {
5906         WLOGFE("Argc is invalid: %{public}zu", argc);
5907         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5908     }
5909 
5910     wptr<Window> weakToken(windowToken_);
5911     auto window = weakToken.promote();
5912     if (window == nullptr) {
5913         WLOGFE("window is nullptr or get invalid param");
5914         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
5915     }
5916     WindowLimits windowLimits;
5917     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(window->GetWindowLimits(windowLimits));
5918     if (ret != WmErrorCode::WM_OK) {
5919         return NapiThrowError(env, ret);
5920     }
5921     auto objValue = GetWindowLimitsAndConvertToJsValue(env, windowLimits);
5922     WLOGI("Windwo [%{public}u, %{public}s] get window limits end",
5923         window->GetWindowId(), window->GetWindowName().c_str());
5924     if (objValue != nullptr) {
5925         return objValue;
5926     } else {
5927         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
5928     }
5929 }
5930 
OnSetWindowDecorVisible(napi_env env,napi_callback_info info)5931 napi_value JsWindow::OnSetWindowDecorVisible(napi_env env, napi_callback_info info)
5932 {
5933     size_t argc = 4;
5934     napi_value argv[4] = {nullptr};
5935     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5936     if (argc < 1) {
5937         WLOGFE("Argc is invalid: %{public}zu", argc);
5938         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5939     }
5940     if (windowToken_ == nullptr) {
5941         WLOGFE("WindowToken_ is nullptr");
5942         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
5943     }
5944     napi_value nativeVal = argv[0];
5945     if (nativeVal == nullptr) {
5946         WLOGFE("Failed to convert parameter to visible");
5947         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5948     }
5949     bool isVisible = true;
5950     WmErrorCode errCode = WmErrorCode::WM_OK;
5951     CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
5952         napi_get_value_bool(env, nativeVal, &isVisible));
5953     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
5954         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
5955     }
5956     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetDecorVisible(isVisible));
5957     if (ret != WmErrorCode::WM_OK) {
5958         WLOGFE("Window decor set visible failed");
5959         return NapiThrowError(env, ret);
5960     }
5961     WLOGI("Window [%{public}u, %{public}s] OnSetWindowDecorVisible end",
5962         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
5963     return NapiGetUndefined(env);
5964 }
5965 
OnSetSubWindowModal(napi_env env,napi_callback_info info)5966 napi_value JsWindow::OnSetSubWindowModal(napi_env env, napi_callback_info info)
5967 {
5968     WmErrorCode errCode = WmErrorCode::WM_OK;
5969     bool isModal = false;
5970     size_t argc = 4;
5971     napi_value argv[4] = { nullptr };
5972     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
5973     if (argc != 1 || argv[0] == nullptr) { // 1: the param num
5974         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
5975     } else {
5976         CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM, napi_get_value_bool(env, argv[0], &isModal));
5977     }
5978 
5979     wptr<Window> weakToken(windowToken_);
5980     NapiAsyncTask::CompleteCallback complete =
5981         [weakToken, isModal, errCode](napi_env env, NapiAsyncTask& task, int32_t status) {
5982             if (errCode != WmErrorCode::WM_OK) {
5983                 TLOGE(WmsLogTag::WMS_SUB, "OnSetSubWindowModal invalid parameter");
5984                 task.Reject(env, JsErrUtils::CreateJsError(env, errCode, "invalid parameter."));
5985                 return;
5986             }
5987             auto window = weakToken.promote();
5988             if (window == nullptr) {
5989                 TLOGE(WmsLogTag::WMS_SUB, "OnSetSubWindowModal window is nullptr");
5990                 WmErrorCode wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(WMError::WM_ERROR_NULLPTR);
5991                 task.Reject(env, JsErrUtils::CreateJsError(env, wmErrorCode, "window is nullptr."));
5992                 return;
5993             }
5994             if (!WindowHelper::IsSubWindow(window->GetType())) {
5995                 TLOGE(WmsLogTag::WMS_SUB, "OnSetSubWindowModal invalid call, type:%{public}d", window->GetType());
5996                 WmErrorCode wmErrorCode = WmErrorCode::WM_ERROR_INVALID_CALLING;
5997                 task.Reject(env, JsErrUtils::CreateJsError(env, wmErrorCode, "invalid window type."));
5998                 return;
5999             }
6000             WMError ret = window->SetSubWindowModal(isModal);
6001             if (ret == WMError::WM_OK) {
6002                 task.Resolve(env, NapiGetUndefined(env));
6003             } else {
6004                 WmErrorCode wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(ret);
6005                 TLOGE(WmsLogTag::WMS_SUB, "OnSetSubWindowModal set failed, ret is %{public}d", wmErrorCode);
6006                 task.Reject(env, JsErrUtils::CreateJsError(env, wmErrorCode, "Set subwindow modal failed"));
6007             }
6008             TLOGI(WmsLogTag::WMS_SUB, "OnSetSubWindowModal id:%{public}u, name:%{public}s, isModal:%{public}d",
6009                 window->GetWindowId(), window->GetWindowName().c_str(), isModal);
6010         };
6011     napi_value lastParam = nullptr;
6012     napi_value result = nullptr;
6013     NapiAsyncTask::Schedule("JsWindow::SetSubWindowModal",
6014         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
6015     return result;
6016 }
6017 
OnSetWindowDecorHeight(napi_env env,napi_callback_info info)6018 napi_value JsWindow::OnSetWindowDecorHeight(napi_env env, napi_callback_info info)
6019 {
6020     size_t argc = 4;
6021     napi_value argv[4] = {nullptr};
6022     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
6023     if (argc < 1) {
6024         WLOGFE("Argc is invalid: %{public}zu", argc);
6025         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6026     }
6027     if (windowToken_ == nullptr) {
6028         WLOGFE("WindowToken_ is nullptr");
6029         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
6030     }
6031     napi_value nativeVal = argv[0];
6032     if (nativeVal == nullptr) {
6033         WLOGFE("Failed to convert parameter to height");
6034         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6035     }
6036     int32_t height = 0;
6037     WmErrorCode errCode = WmErrorCode::WM_OK;
6038     CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
6039         napi_get_value_int32(env, nativeVal, &height));
6040     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
6041         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6042     }
6043 
6044     if (height < MIN_DECOR_HEIGHT || height > MAX_DECOR_HEIGHT) {
6045         WLOGFE("height should greater than 37 or smaller than 112");
6046         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6047     }
6048 
6049     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetDecorHeight(height));
6050     if (ret != WmErrorCode::WM_OK) {
6051         WLOGFE("Set window decor height failed");
6052         return NapiThrowError(env, ret);
6053     }
6054     WLOGI("Window [%{public}u, %{public}s] OnSetDecorHeight end, height = %{public}d",
6055         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), height);
6056     return NapiGetUndefined(env);
6057 }
6058 
OnGetWindowDecorHeight(napi_env env,napi_callback_info info)6059 napi_value JsWindow::OnGetWindowDecorHeight(napi_env env, napi_callback_info info)
6060 {
6061     wptr<Window> weakToken(windowToken_);
6062     auto window = weakToken.promote();
6063     if (window == nullptr) {
6064         WLOGFE("window is nullptr");
6065         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
6066     }
6067     int32_t height = 0;
6068     WMError ret = window->GetDecorHeight(height);
6069     if (ret != WMError::WM_OK) {
6070         if (ret == WMError::WM_ERROR_DEVICE_NOT_SUPPORT) {
6071             return NapiThrowError(env, WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT);
6072         }
6073         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
6074     }
6075     WLOGI("Window [%{public}u, %{public}s] OnGetDecorHeight end, height = %{public}d",
6076         window->GetWindowId(), window->GetWindowName().c_str(), height);
6077     return CreateJsValue(env, height);
6078 }
6079 
OnGetTitleButtonRect(napi_env env,napi_callback_info info)6080 napi_value JsWindow::OnGetTitleButtonRect(napi_env env, napi_callback_info info)
6081 {
6082     wptr<Window> weakToken(windowToken_);
6083     auto window = weakToken.promote();
6084     if (window == nullptr) {
6085         WLOGFE("window is nullptr");
6086         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
6087     }
6088     TitleButtonRect titleButtonRect;
6089     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->GetTitleButtonArea(titleButtonRect));
6090     if (ret != WmErrorCode::WM_OK) {
6091         return NapiThrowError(env, ret);
6092     }
6093     WLOGI("Window [%{public}u, %{public}s] OnGetTitleButtonRect end",
6094         window->GetWindowId(), window->GetWindowName().c_str());
6095     napi_value TitleButtonAreaObj = ConvertTitleButtonAreaToJsValue(env, titleButtonRect);
6096     if (TitleButtonAreaObj == nullptr) {
6097         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
6098     }
6099     return TitleButtonAreaObj;
6100 }
6101 
OnSetTitleButtonVisible(napi_env env,napi_callback_info info)6102 napi_value JsWindow::OnSetTitleButtonVisible(napi_env env, napi_callback_info info)
6103 {
6104     if (!Permission::IsSystemCalling()) {
6105         TLOGE(WmsLogTag::WMS_LAYOUT, "set title button visible permission denied!");
6106         return NapiThrowError(env, WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
6107     }
6108     size_t argc = 4;
6109     napi_value argv[4] = {nullptr};
6110     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
6111     if (argc != 3) { // 3: params num
6112         WLOGFE("Argc is invalid: %{public}zu", argc);
6113         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6114     }
6115     bool isMaximizeVisible = true;
6116     if (!ConvertFromJsValue(env, argv[INDEX_ZERO], isMaximizeVisible)) {
6117         TLOGE(WmsLogTag::WMS_LAYOUT, "Failed to convert parameter to isMaximizeVisible");
6118         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6119     }
6120     bool isMinimizeVisible = true;
6121     if (!ConvertFromJsValue(env, argv[INDEX_ONE], isMinimizeVisible)) {
6122         TLOGE(WmsLogTag::WMS_LAYOUT, "Failed to convert parameter to isMinimizeVisible");
6123         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6124     }
6125     bool isSplitVisible = true;
6126     if (!ConvertFromJsValue(env, argv[INDEX_TWO], isSplitVisible)) {
6127         TLOGE(WmsLogTag::WMS_LAYOUT, "Failed to convert parameter to isSplitVisible");
6128         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6129     }
6130     if (windowToken_ == nullptr) {
6131         TLOGE(WmsLogTag::WMS_LAYOUT, "WindowToken_ is nullptr");
6132         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
6133     }
6134     WMError errCode = windowToken_->SetTitleButtonVisible(isMaximizeVisible, isMinimizeVisible, isSplitVisible);
6135     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(errCode);
6136     if (ret != WmErrorCode::WM_OK) {
6137         TLOGE(WmsLogTag::WMS_LAYOUT, "set title button visible failed!");
6138         return NapiThrowError(env, ret);
6139     }
6140     TLOGI(WmsLogTag::WMS_LAYOUT,
6141         "Window [%{public}u, %{public}s] set title button visible [%{public}d, %{public}d, %{public}d]",
6142         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), isMaximizeVisible, isMinimizeVisible,
6143         isSplitVisible);
6144     return NapiGetUndefined(env);
6145 }
6146 
OnSetWindowMask(napi_env env,napi_callback_info info)6147 napi_value JsWindow::OnSetWindowMask(napi_env env, napi_callback_info info)
6148 {
6149     size_t argc = 4;
6150     napi_value argv[4] = {nullptr};
6151     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
6152     if (argc < 1) {
6153         WLOGFE("Argc is invalid: %{public}zu", argc);
6154         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6155     }
6156     if (!CheckWindowMaskParams(env, argv[0])) {
6157         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6158     }
6159     std::vector<std::vector<uint32_t>> windowMask;
6160     if (!GetWindowMaskFromJsValue(env, argv[0], windowMask)) {
6161         WLOGFE("GetWindowMaskFromJsValue failed");
6162         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6163     }
6164     wptr<Window> weakToken(windowToken_);
6165     NapiAsyncTask::CompleteCallback complete =
6166         [weakToken, windowMask](napi_env env, NapiAsyncTask& task, int32_t status) {
6167             auto weakWindow = weakToken.promote();
6168             if (weakWindow == nullptr) {
6169                 WmErrorCode wmErrorCode = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
6170                 task.Reject(env, JsErrUtils::CreateJsError(env, wmErrorCode, "Invalidate params"));
6171                 return;
6172             }
6173             if (!WindowHelper::IsSubWindow(weakWindow->GetType()) &&
6174                 !WindowHelper::IsAppFloatingWindow(weakWindow->GetType())) {
6175                 WmErrorCode wmErrorCode = WmErrorCode::WM_ERROR_INVALID_CALLING;
6176                 task.Reject(env, JsErrUtils::CreateJsError(env, wmErrorCode, "Invalidate window type"));
6177                 return;
6178             }
6179             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetWindowMask(windowMask));
6180             if (ret != WmErrorCode::WM_OK) {
6181                 task.Reject(env, JsErrUtils::CreateJsError(env, ret));
6182                 WLOGFE("Window [%{public}u, %{public}s] set window mask failed",
6183                     weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
6184                 return;
6185             }
6186             task.Resolve(env, NapiGetUndefined(env));
6187             WLOGI("Window [%{public}u, %{public}s] set window mask succeed",
6188                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
6189         };
6190     napi_value lastParam = nullptr;
6191     napi_value result = nullptr;
6192     NapiAsyncTask::Schedule("JsWindow::OnSetWindowMask",
6193         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
6194     return result;
6195 }
6196 
CheckWindowMaskParams(napi_env env,napi_value jsObject)6197 bool JsWindow::CheckWindowMaskParams(napi_env env, napi_value jsObject)
6198 {
6199     if (env == nullptr || jsObject == nullptr) {
6200         TLOGE(WmsLogTag::WMS_LAYOUT, "Env is nullptr or jsObject is nullptr");
6201         return false;
6202     }
6203     if (windowToken_ == nullptr) {
6204         TLOGE(WmsLogTag::WMS_LAYOUT, "windowToken is nullptr");
6205         return false;
6206     }
6207     uint32_t size = 0;
6208     napi_get_array_length(env, jsObject, &size);
6209     WindowLimits windowLimits;
6210     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->GetWindowLimits(windowLimits));
6211     if (ret == WmErrorCode::WM_OK) {
6212         if (size == 0 || size > windowLimits.maxWidth_) {
6213             TLOGE(WmsLogTag::WMS_LAYOUT, "Invalid windowMask size:%{public}u, vpRatio:%{public}f, maxWidth:%{public}u",
6214                 size, windowLimits.vpRatio_, windowLimits.maxWidth_);
6215             return false;
6216         }
6217     } else {
6218         TLOGW(WmsLogTag::WMS_LAYOUT, "Get windowLimits failed, error code is %{public}d", ret);
6219         if (size == 0 || size > DEFAULT_WINDOW_MAX_WIDTH) {
6220             TLOGE(WmsLogTag::WMS_LAYOUT, "Invalid windowMask size:%{public}u", size);
6221             return false;
6222         }
6223     }
6224     return true;
6225 }
6226 
SetWindowGrayScaleTask(const wptr<Window> & weakToken,double grayScale,NapiAsyncTask::ExecuteCallback & execute,NapiAsyncTask::CompleteCallback & complete)6227 void SetWindowGrayScaleTask(const wptr<Window>& weakToken, double grayScale,
6228     NapiAsyncTask::ExecuteCallback& execute, NapiAsyncTask::CompleteCallback& complete)
6229 {
6230     std::shared_ptr<WmErrorCode> err = std::make_shared<WmErrorCode>(WmErrorCode::WM_OK);
6231     execute = [weakToken, grayScale, err]() {
6232         if (err == nullptr) {
6233             TLOGE(WmsLogTag::DEFAULT, "wm error code is null");
6234             return;
6235         }
6236         auto window = weakToken.promote();
6237         if (window == nullptr) {
6238             TLOGE(WmsLogTag::DEFAULT, "window is null");
6239             *err = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
6240             return;
6241         }
6242         *err = WM_JS_TO_ERROR_CODE_MAP.at(window->SetGrayScale(static_cast<float>(grayScale)));
6243         TLOGI(WmsLogTag::DEFAULT,
6244             "Window [%{public}u, %{public}s] OnSetWindowGrayScale end, grayScale = %{public}f",
6245             window->GetWindowId(), window->GetWindowName().c_str(), grayScale);
6246     };
6247 
6248     complete = [err](napi_env env, NapiAsyncTask& task, int32_t status) {
6249         if (err == nullptr) {
6250             task.Reject(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
6251             return;
6252         }
6253         if (*err == WmErrorCode::WM_OK) {
6254             task.Resolve(env, NapiGetUndefined(env));
6255         } else {
6256             task.Reject(env, CreateJsError(env, static_cast<int32_t>(*err), "Set window gray scale failed"));
6257         }
6258     };
6259 }
6260 
OnSetWindowGrayScale(napi_env env,napi_callback_info info)6261 napi_value JsWindow::OnSetWindowGrayScale(napi_env env, napi_callback_info info)
6262 {
6263     size_t argc = 4;
6264     napi_value argv[4] = {nullptr};
6265     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
6266     if (argc != 1) {    // 1: the param num
6267         TLOGE(WmsLogTag::DEFAULT, "Argc is invalid: %{public}zu", argc);
6268         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6269     }
6270     napi_value nativeVal = argv[0];
6271     if (nativeVal == nullptr) {
6272         TLOGE(WmsLogTag::DEFAULT, "Failed to convert parameter to grayScale");
6273         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6274     }
6275     double grayScale = 0.0;
6276     napi_get_value_double(env, nativeVal, &grayScale);
6277     constexpr double eps = 1e-6;
6278     if (grayScale < (MIN_GRAY_SCALE - eps) || grayScale > (MAX_GRAY_SCALE + eps)) {
6279         TLOGE(WmsLogTag::DEFAULT,
6280             "grayScale should be greater than or equal to 0.0, and should be smaller than or equal to 1.0");
6281         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6282     }
6283 
6284     wptr<Window> weakToken(windowToken_);
6285     NapiAsyncTask::ExecuteCallback execute;
6286     NapiAsyncTask::CompleteCallback complete;
6287     SetWindowGrayScaleTask(weakToken, grayScale, execute, complete);
6288 
6289     napi_value result = nullptr;
6290     NapiAsyncTask::Schedule("JsWindow::OnSetWindowGrayScale",
6291         env, CreateAsyncTaskWithLastParam(env, nullptr, std::move(execute), std::move(complete), &result));
6292     return result;
6293 }
6294 
OnSetImmersiveModeEnabledState(napi_env env,napi_callback_info info)6295 napi_value JsWindow::OnSetImmersiveModeEnabledState(napi_env env, napi_callback_info info)
6296 {
6297     size_t argc = 4;
6298     napi_value argv[4] = {nullptr};
6299     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
6300     if (argc != 1) {
6301         TLOGW(WmsLogTag::WMS_IMMS, "Argc is invalid: %{public}zu", argc);
6302         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6303     }
6304     if (windowToken_ == nullptr) {
6305         TLOGE(WmsLogTag::WMS_IMMS, "windowToken_ is nullptr");
6306         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
6307     }
6308     if (!WindowHelper::IsMainWindow(windowToken_->GetType()) &&
6309         !WindowHelper::IsSubWindow(windowToken_->GetType())) {
6310         TLOGE(WmsLogTag::WMS_IMMS, "[NAPI]OnSetImmersiveModeEnabledState is not allowed since invalid window type");
6311         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
6312     }
6313     napi_value nativeVal = argv[0];
6314     if (nativeVal == nullptr) {
6315         TLOGE(WmsLogTag::WMS_IMMS, "Failed to convert parameter to enable");
6316         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6317     }
6318     bool enable = true;
6319     napi_get_value_bool(env, nativeVal, &enable);
6320     TLOGI(WmsLogTag::WMS_IMMS, "[NAPI]OnSetImmersiveModeEnabledState to %{public}d", static_cast<int32_t>(enable));
6321     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetImmersiveModeEnabledState(enable));
6322     if (ret != WmErrorCode::WM_OK) {
6323         TLOGE(WmsLogTag::WMS_IMMS, "Window immersive mode set enabled failed, ret = %{public}d", ret);
6324         return NapiThrowError(env, WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY);
6325     }
6326     TLOGI(WmsLogTag::WMS_IMMS, "window [%{public}u, %{public}s] OnSetImmersiveModeEnabledState end",
6327         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
6328     return NapiGetUndefined(env);
6329 }
6330 
OnGetImmersiveModeEnabledState(napi_env env,napi_callback_info info)6331 napi_value JsWindow::OnGetImmersiveModeEnabledState(napi_env env, napi_callback_info info)
6332 {
6333     if (windowToken_ == nullptr) {
6334         TLOGE(WmsLogTag::WMS_IMMS, "windowToken_ is nullptr");
6335         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
6336     }
6337     if (!WindowHelper::IsMainWindow(windowToken_->GetType()) &&
6338         !WindowHelper::IsSubWindow(windowToken_->GetType())) {
6339         TLOGE(WmsLogTag::WMS_IMMS, "[NAPI]OnGetImmersiveModeEnabledState is not allowed since invalid window type");
6340         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
6341     }
6342 
6343     bool isEnabled = windowToken_->GetImmersiveModeEnabledState();
6344     TLOGI(WmsLogTag::WMS_IMMS, "window [%{public}u, %{public}s] get isImmersiveMode end, isEnabled = %{public}u",
6345         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), isEnabled);
6346     return CreateJsValue(env, isEnabled);
6347 }
6348 
OnGetWindowStatus(napi_env env,napi_callback_info info)6349 napi_value JsWindow::OnGetWindowStatus(napi_env env, napi_callback_info info)
6350 {
6351     auto window = windowToken_;
6352     if (window == nullptr) {
6353         TLOGE(WmsLogTag::DEFAULT, "window is nullptr");
6354         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
6355     }
6356     WindowStatus windowStatus;
6357     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(window->GetWindowStatus(windowStatus));
6358     if (ret != WmErrorCode::WM_OK) {
6359         TLOGE(WmsLogTag::DEFAULT, "get window status failed, ret = %{public}d", ret);
6360         return NapiThrowError(env, ret);
6361     }
6362     auto objValue = CreateJsValue(env, windowStatus);
6363     if (objValue != nullptr) {
6364         TLOGI(WmsLogTag::DEFAULT, "window [%{public}u, %{public}s] get window status end",
6365             window->GetWindowId(), window->GetWindowName().c_str());
6366         return objValue;
6367     } else {
6368         TLOGE(WmsLogTag::DEFAULT, "create js value windowStatus failed");
6369         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
6370     }
6371 }
6372 
OnIsFocused(napi_env env,napi_callback_info info)6373 napi_value JsWindow::OnIsFocused(napi_env env, napi_callback_info info)
6374 {
6375     auto window = windowToken_;
6376     if (window == nullptr) {
6377         TLOGE(WmsLogTag::WMS_FOCUS, "window is nullptr");
6378         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
6379     }
6380 
6381     bool isFocused = window->IsFocused();
6382     TLOGI(WmsLogTag::WMS_FOCUS, "window [%{public}u, %{public}s] get isFocused end, isFocused = %{public}u",
6383         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), isFocused);
6384     return CreateJsValue(env, isFocused);
6385 }
6386 
SetRequestFocusTask(NapiAsyncTask::ExecuteCallback & execute,NapiAsyncTask::CompleteCallback & complete,wptr<Window> weakToken,bool isFocused)6387 static void SetRequestFocusTask(NapiAsyncTask::ExecuteCallback& execute, NapiAsyncTask::CompleteCallback& complete,
6388     wptr<Window> weakToken, bool isFocused)
6389 {
6390     std::shared_ptr<WmErrorCode> errCodePtr = std::make_shared<WmErrorCode>(WmErrorCode::WM_OK);
6391     execute = [weakToken, errCodePtr, isFocused] {
6392         if (errCodePtr == nullptr) {
6393             return;
6394         }
6395         if (*errCodePtr != WmErrorCode::WM_OK) {
6396             return;
6397         }
6398         auto weakWindow = weakToken.promote();
6399         if (weakWindow == nullptr) {
6400             *errCodePtr = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
6401             return;
6402         }
6403         *errCodePtr = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->RequestFocusByClient(isFocused));
6404         TLOGI(WmsLogTag::WMS_FOCUS, "Window [%{public}u, %{public}s] request focus end, err = %{public}d",
6405             weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), *errCodePtr);
6406     };
6407     complete = [weakToken, errCodePtr](napi_env env, NapiAsyncTask& task, int32_t status) {
6408         if (errCodePtr == nullptr) {
6409             task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
6410             return;
6411         }
6412         if (*errCodePtr == WmErrorCode::WM_OK) {
6413             task.Resolve(env, NapiGetUndefined(env));
6414         } else {
6415             task.Reject(env, JsErrUtils::CreateJsError(env, *errCodePtr, "JsWindow::OnRequestFocus failed"));
6416         }
6417     };
6418 }
6419 
OnRequestFocus(napi_env env,napi_callback_info info)6420 napi_value JsWindow::OnRequestFocus(napi_env env, napi_callback_info info)
6421 {
6422     if (!Permission::IsSystemCalling()) {
6423         TLOGE(WmsLogTag::WMS_FOCUS, "permission denied!");
6424         return NapiThrowError(env, WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
6425     }
6426     if (windowToken_ == nullptr) {
6427         TLOGE(WmsLogTag::WMS_FOCUS, "window is nullptr");
6428         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
6429     }
6430 
6431     size_t argc = 4; // number of arg
6432     napi_value argv[4] = {nullptr};
6433     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
6434     if (argc != 1 || argv[0] == nullptr) { // 1: maximum params num
6435         TLOGE(WmsLogTag::WMS_FOCUS, "Argc is invalid: %{public}zu", argc);
6436         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6437     }
6438 
6439     bool isFocused = false;
6440     napi_status retCode = napi_get_value_bool(env, argv[0], &isFocused);
6441     if (retCode != napi_ok) {
6442         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6443     }
6444     wptr<Window> weakToken(windowToken_);
6445     NapiAsyncTask::ExecuteCallback execute;
6446     NapiAsyncTask::CompleteCallback complete;
6447     SetRequestFocusTask(execute, complete, weakToken, isFocused);
6448     // only return promise<void>
6449     napi_value result = nullptr;
6450     NapiAsyncTask::Schedule("JsWindow::OnRequestFocus",
6451         env, CreateAsyncTaskWithLastParam(env, nullptr, std::move(execute), std::move(complete), &result));
6452     return result;
6453 }
6454 
ParseSubWindowOptions(napi_env env,napi_value jsObject,const sptr<WindowOption> & WindowOption)6455 static bool ParseSubWindowOptions(napi_env env, napi_value jsObject, const sptr<WindowOption>& WindowOption)
6456 {
6457     if (jsObject == nullptr) {
6458         TLOGE(WmsLogTag::WMS_SUB, "jsObject is null");
6459         return true;
6460     }
6461 
6462     std::string title;
6463     if (ParseJsValue(jsObject, env, "title", title)) {
6464         WindowOption->SetSubWindowTitle(title);
6465     } else {
6466         TLOGE(WmsLogTag::WMS_SUB, "Failed to convert parameter to title");
6467         return false;
6468     }
6469 
6470     bool decorEnabled;
6471     if (ParseJsValue(jsObject, env, "decorEnabled", decorEnabled)) {
6472         WindowOption->SetSubWindowDecorEnable(decorEnabled);
6473     } else {
6474         TLOGE(WmsLogTag::WMS_SUB, "Failed to convert parameter to decorEnabled");
6475         return false;
6476     }
6477 
6478     bool isModal = false;
6479     if (ParseJsValue(jsObject, env, "isModal", isModal)) {
6480         TLOGD(WmsLogTag::WMS_SUB, "isModal:%{public}d", isModal);
6481         if (isModal) {
6482             WindowOption->AddWindowFlag(WindowFlag::WINDOW_FLAG_IS_MODAL);
6483         }
6484     }
6485 
6486     bool isTopmost = false;
6487     if (ParseJsValue(jsObject, env, "isTopmost", isTopmost)) {
6488         if (!isModal && isTopmost) {
6489             TLOGE(WmsLogTag::WMS_SUB, "Normal subwindow is topmost");
6490             return false;
6491         }
6492         WindowOption->SetWindowTopmost(isTopmost);
6493     }
6494 
6495     return true;
6496 }
6497 
OnSetGestureBackEnabled(napi_env env,napi_callback_info info)6498 napi_value JsWindow::OnSetGestureBackEnabled(napi_env env, napi_callback_info info)
6499 {
6500     size_t argc = FOUR_PARAMS_SIZE;
6501     napi_value argv[FOUR_PARAMS_SIZE] = {nullptr};
6502     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
6503     if (argc < INDEX_ONE) {
6504         TLOGE(WmsLogTag::WMS_IMMS, "argc is invalid: %{public}zu.", argc);
6505         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6506     }
6507     bool enabled = true;
6508     if (argv[INDEX_ZERO] == nullptr || napi_get_value_bool(env, argv[INDEX_ZERO], &enabled) != napi_ok) {
6509         TLOGE(WmsLogTag::WMS_IMMS, "failed to convert parameter to enabled.");
6510         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6511     }
6512     std::shared_ptr<WmErrorCode> errCodePtr = std::make_shared<WmErrorCode>(WmErrorCode::WM_OK);
6513     auto execute = [weakToken = wptr<Window>(windowToken_), errCodePtr, enabled] {
6514         auto window = weakToken.promote();
6515         if (window == nullptr) {
6516             TLOGNE(WmsLogTag::WMS_IMMS, "window is nullptr.");
6517             *errCodePtr = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
6518             return;
6519         }
6520         if (!WindowHelper::IsMainWindow(window->GetType())) {
6521             TLOGNE(WmsLogTag::WMS_IMMS, "invalid window type.");
6522             *errCodePtr = WmErrorCode::WM_ERROR_INVALID_CALLING;
6523             return;
6524         }
6525         *errCodePtr = WM_JS_TO_ERROR_CODE_MAP.at(window->SetGestureBackEnabled(enabled));
6526     };
6527     auto complete = [errCodePtr](napi_env env, NapiAsyncTask& task, int32_t status) {
6528         if (*errCodePtr == WmErrorCode::WM_OK) {
6529             task.Resolve(env, NapiGetUndefined(env));
6530         } else {
6531             TLOGNE(WmsLogTag::WMS_IMMS, "set failed, ret = %{public}d.", *errCodePtr);
6532             task.Reject(env, JsErrUtils::CreateJsError(env, *errCodePtr, "set failed."));
6533         }
6534     };
6535     napi_value result = nullptr;
6536     NapiAsyncTask::Schedule("JsWindow::OnSetGestureBackEnabled",
6537         env, CreateAsyncTaskWithLastParam(env, nullptr, std::move(execute), std::move(complete), &result));
6538     return result;
6539 }
6540 
OnGetGestureBackEnabled(napi_env env,napi_callback_info info)6541 napi_value JsWindow::OnGetGestureBackEnabled(napi_env env, napi_callback_info info)
6542 {
6543     if (windowToken_ == nullptr) {
6544         TLOGE(WmsLogTag::WMS_IMMS, "windowToken is nullptr");
6545         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
6546     }
6547     if (!WindowHelper::IsMainWindow(windowToken_->GetType())) {
6548         TLOGE(WmsLogTag::WMS_IMMS, "[NAPI] get failed since invalid window type");
6549         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
6550     }
6551     bool enable = true;
6552     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->GetGestureBackEnabled(enable));
6553     if (ret == WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT) {
6554         TLOGE(WmsLogTag::WMS_IMMS, "device is not support.");
6555         return NapiThrowError(env, WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT);
6556     } else if (ret != WmErrorCode::WM_OK) {
6557         TLOGE(WmsLogTag::WMS_IMMS, "get failed, ret = %{public}d", ret);
6558         return NapiThrowError(env, WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY);
6559     }
6560     TLOGI(WmsLogTag::WMS_IMMS, "window [%{public}u, %{public}s], enable = %{public}u",
6561         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), enable);
6562     return CreateJsValue(env, enable);
6563 }
6564 
CreateNewSubWindowTask(const sptr<Window> & windowToken,const std::string & windowName,sptr<WindowOption> & windowOption,napi_env env,NapiAsyncTask & task)6565 static void CreateNewSubWindowTask(const sptr<Window>& windowToken, const std::string& windowName,
6566     sptr<WindowOption>& windowOption, napi_env env, NapiAsyncTask& task)
6567 {
6568     if (windowToken == nullptr) {
6569         TLOGE(WmsLogTag::WMS_SUB, "window is null");
6570         task.Reject(env, CreateJsError(env,
6571             static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "window is null"));
6572         return;
6573     }
6574     if (!WindowHelper::IsSubWindow(windowToken->GetType()) &&
6575         !WindowHelper::IsMainWindow(windowToken->GetType())) {
6576         TLOGE(WmsLogTag::WMS_SUB, "This is not subWindow or mainWindow.");
6577         task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_INVALID_CALLING,
6578             "This is not subWindow or mainWindow"));
6579         return;
6580     }
6581     windowOption->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
6582     windowOption->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
6583     windowOption->SetOnlySupportSceneBoard(true);
6584     windowOption->SetParentId(windowToken->GetWindowId());
6585     windowOption->SetWindowTag(WindowTag::SUB_WINDOW);
6586     auto window = Window::Create(windowName, windowOption, windowToken->GetContext());
6587     if (window == nullptr) {
6588         TLOGE(WmsLogTag::WMS_SUB, "create sub window failed");
6589         task.Reject(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY,
6590             "create sub window failed"));
6591         return;
6592     }
6593     task.Resolve(env, CreateJsWindowObject(env, window));
6594     TLOGI(WmsLogTag::WMS_SUB, "create sub window %{public}s end", windowName.c_str());
6595 }
6596 
OnCreateSubWindowWithOptions(napi_env env,napi_callback_info info)6597 napi_value JsWindow::OnCreateSubWindowWithOptions(napi_env env, napi_callback_info info)
6598 {
6599     if (windowToken_ == nullptr) {
6600         TLOGE(WmsLogTag::WMS_SUB, "window is null");
6601         napi_throw(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY));
6602         return NapiGetUndefined(env);
6603     }
6604     if (!windowToken_->IsPcOrPadCapabilityEnabled()) {
6605         TLOGE(WmsLogTag::WMS_SUB, "device not support");
6606         return NapiGetUndefined(env);
6607     }
6608     size_t argc = 4;
6609     napi_value argv[4] = {nullptr};
6610     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
6611     if (argc < 2) { // 2: minimum params num
6612         TLOGE(WmsLogTag::WMS_SUB, "Argc is invalid: %{public}zu", argc);
6613         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
6614     }
6615     std::string windowName;
6616     if (!ConvertFromJsValue(env, argv[0], windowName)) {
6617         TLOGE(WmsLogTag::WMS_SUB, "Failed to convert parameter to windowName");
6618         napi_throw(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_INVALID_PARAM));
6619         return NapiGetUndefined(env);
6620     }
6621     sptr<WindowOption> windowOption = new WindowOption();
6622     if (windowOption == nullptr) {
6623         TLOGE(WmsLogTag::WMS_SUB, "window option is null");
6624         napi_throw(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY));
6625         return NapiGetUndefined(env);
6626     }
6627     if (!ParseSubWindowOptions(env, argv[1], windowOption)) {
6628         TLOGE(WmsLogTag::WMS_SUB, "Failed to convert parameter to options");
6629         napi_throw(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_INVALID_PARAM));
6630         return NapiGetUndefined(env);
6631     }
6632     if (windowOption->GetWindowTopmost() && !Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
6633         TLOGE(WmsLogTag::WMS_SUB, "Modal subwindow has topmost, but no system permission");
6634         napi_throw(env, JsErrUtils::CreateJsError(env, WmErrorCode::WM_ERROR_NOT_SYSTEM_APP));
6635         return NapiGetUndefined(env);
6636     }
6637     NapiAsyncTask::CompleteCallback complete =
6638         [windowToken = windowToken_, windowName = std::move(windowName), windowOption](napi_env env,
6639             NapiAsyncTask& task, int32_t status) mutable {
6640         CreateNewSubWindowTask(windowToken, windowName, windowOption, env, task);
6641     };
6642     napi_value callback = (argc > 2 && argv[2] != nullptr && GetType(env, argv[2]) == napi_function) ?
6643         argv[2] : nullptr;
6644     napi_value result = nullptr;
6645     NapiAsyncTask::Schedule("JsWindow::OnCreateSubWindowWithOptions",
6646         env, CreateAsyncTaskWithLastParam(env, callback, nullptr, std::move(complete), &result));
6647     return result;
6648 }
6649 
BindFunctions(napi_env env,napi_value object,const char * moduleName)6650 void BindFunctions(napi_env env, napi_value object, const char* moduleName)
6651 {
6652     BindNativeFunction(env, object, "show", moduleName, JsWindow::Show);
6653     BindNativeFunction(env, object, "showWindow", moduleName, JsWindow::ShowWindow);
6654     BindNativeFunction(env, object, "showWithAnimation", moduleName, JsWindow::ShowWithAnimation);
6655     BindNativeFunction(env, object, "destroy", moduleName, JsWindow::Destroy);
6656     BindNativeFunction(env, object, "destroyWindow", moduleName, JsWindow::DestroyWindow);
6657     BindNativeFunction(env, object, "hide", moduleName, JsWindow::Hide);
6658     BindNativeFunction(env, object, "hideWithAnimation", moduleName, JsWindow::HideWithAnimation);
6659     BindNativeFunction(env, object, "recover", moduleName, JsWindow::Recover);
6660     BindNativeFunction(env, object, "moveTo", moduleName, JsWindow::MoveTo);
6661     BindNativeFunction(env, object, "moveWindowTo", moduleName, JsWindow::MoveWindowTo);
6662     BindNativeFunction(env, object, "moveWindowToAsync", moduleName, JsWindow::MoveWindowToAsync);
6663     BindNativeFunction(env, object, "moveWindowToGlobal", moduleName, JsWindow::MoveWindowToGlobal);
6664     BindNativeFunction(env, object, "getGlobalRect", moduleName, JsWindow::GetGlobalScaledRect);
6665     BindNativeFunction(env, object, "resetSize", moduleName, JsWindow::Resize);
6666     BindNativeFunction(env, object, "resize", moduleName, JsWindow::ResizeWindow);
6667     BindNativeFunction(env, object, "resizeAsync", moduleName, JsWindow::ResizeWindowAsync);
6668     BindNativeFunction(env, object, "setWindowType", moduleName, JsWindow::SetWindowType);
6669     BindNativeFunction(env, object, "setWindowMode", moduleName, JsWindow::SetWindowMode);
6670     BindNativeFunction(env, object, "getProperties", moduleName, JsWindow::GetProperties);
6671     BindNativeFunction(env, object, "getWindowProperties", moduleName, JsWindow::GetWindowPropertiesSync);
6672     BindNativeFunction(env, object, "on", moduleName, JsWindow::RegisterWindowCallback);
6673     BindNativeFunction(env, object, "off", moduleName, JsWindow::UnregisterWindowCallback);
6674     BindNativeFunction(env, object, "bindDialogTarget", moduleName, JsWindow::BindDialogTarget);
6675     BindNativeFunction(env, object, "setDialogBackGestureEnabled", moduleName, JsWindow::SetDialogBackGestureEnabled);
6676     BindNativeFunction(env, object, "loadContent", moduleName, JsWindow::LoadContent);
6677     BindNativeFunction(env, object, "loadContentByName", moduleName, JsWindow::LoadContentByName);
6678     BindNativeFunction(env, object, "getUIContext", moduleName, JsWindow::GetUIContext);
6679     BindNativeFunction(env, object, "setUIContent", moduleName, JsWindow::SetUIContent);
6680     BindNativeFunction(env, object, "setFullScreen", moduleName, JsWindow::SetFullScreen);
6681     BindNativeFunction(env, object, "setLayoutFullScreen", moduleName, JsWindow::SetLayoutFullScreen);
6682     BindNativeFunction(env, object, "setWindowLayoutFullScreen", moduleName, JsWindow::SetWindowLayoutFullScreen);
6683     BindNativeFunction(env, object, "setSystemBarEnable", moduleName, JsWindow::SetSystemBarEnable);
6684     BindNativeFunction(env, object, "setWindowSystemBarEnable", moduleName, JsWindow::SetWindowSystemBarEnable);
6685     BindNativeFunction(env, object, "setSystemBarProperties", moduleName, JsWindow::SetSystemBarProperties);
6686     BindNativeFunction(env, object, "getWindowSystemBarProperties",
6687         moduleName, JsWindow::GetWindowSystemBarPropertiesSync);
6688     BindNativeFunction(env, object, "setWindowSystemBarProperties",
6689         moduleName, JsWindow::SetWindowSystemBarProperties);
6690     BindNativeFunction(env, object, "getAvoidArea", moduleName, JsWindow::GetAvoidArea);
6691     BindNativeFunction(env, object, "getWindowAvoidArea", moduleName, JsWindow::GetWindowAvoidAreaSync);
6692     BindNativeFunction(env, object, "isShowing", moduleName, JsWindow::IsShowing);
6693     BindNativeFunction(env, object, "isWindowShowing", moduleName, JsWindow::IsWindowShowingSync);
6694     BindNativeFunction(env, object, "isSupportWideGamut", moduleName, JsWindow::IsSupportWideGamut);
6695     BindNativeFunction(env, object, "isWindowSupportWideGamut", moduleName, JsWindow::IsWindowSupportWideGamut);
6696     BindNativeFunction(env, object, "setColorSpace", moduleName, JsWindow::SetColorSpace);
6697     BindNativeFunction(env, object, "setWindowColorSpace", moduleName, JsWindow::SetWindowColorSpace);
6698     BindNativeFunction(env, object, "getColorSpace", moduleName, JsWindow::GetColorSpace);
6699     BindNativeFunction(env, object, "getWindowColorSpace", moduleName, JsWindow::GetWindowColorSpaceSync);
6700     BindNativeFunction(env, object, "setBackgroundColor", moduleName, JsWindow::SetBackgroundColor);
6701     BindNativeFunction(env, object, "setWindowBackgroundColor", moduleName, JsWindow::SetWindowBackgroundColorSync);
6702     BindNativeFunction(env, object, "setBrightness", moduleName, JsWindow::SetBrightness);
6703     BindNativeFunction(env, object, "setWindowBrightness", moduleName, JsWindow::SetWindowBrightness);
6704     BindNativeFunction(env, object, "setTopmost", moduleName, JsWindow::SetTopmost);
6705     BindNativeFunction(env, object, "setDimBehind", moduleName, JsWindow::SetDimBehind);
6706     BindNativeFunction(env, object, "setFocusable", moduleName, JsWindow::SetFocusable);
6707     BindNativeFunction(env, object, "setWindowFocusable", moduleName, JsWindow::SetWindowFocusable);
6708     BindNativeFunction(env, object, "setKeepScreenOn", moduleName, JsWindow::SetKeepScreenOn);
6709     BindNativeFunction(env, object, "setWindowKeepScreenOn", moduleName, JsWindow::SetWindowKeepScreenOn);
6710     BindNativeFunction(env, object, "setWakeUpScreen", moduleName, JsWindow::SetWakeUpScreen);
6711     BindNativeFunction(env, object, "setOutsideTouchable", moduleName, JsWindow::SetOutsideTouchable);
6712     BindNativeFunction(env, object, "setPrivacyMode", moduleName, JsWindow::SetPrivacyMode);
6713     BindNativeFunction(env, object, "setWindowPrivacyMode", moduleName, JsWindow::SetWindowPrivacyMode);
6714     BindNativeFunction(env, object, "setTouchable", moduleName, JsWindow::SetTouchable);
6715     BindNativeFunction(env, object, "setTouchableAreas", moduleName, JsWindow::SetTouchableAreas);
6716     BindNativeFunction(env, object, "setWindowTouchable", moduleName, JsWindow::SetWindowTouchable);
6717     BindNativeFunction(env, object, "setTransparent", moduleName, JsWindow::SetTransparent);
6718     BindNativeFunction(env, object, "setCallingWindow", moduleName, JsWindow::SetCallingWindow);
6719     BindNativeFunction(env, object, "setSnapshotSkip", moduleName, JsWindow::SetSnapshotSkip);
6720     BindNativeFunction(env, object, "raiseToAppTop", moduleName, JsWindow::RaiseToAppTop);
6721     BindNativeFunction(env, object, "disableWindowDecor", moduleName, JsWindow::DisableWindowDecor);
6722     BindNativeFunction(env, object, "dump", moduleName, JsWindow::Dump);
6723     BindNativeFunction(env, object, "setForbidSplitMove", moduleName, JsWindow::SetForbidSplitMove);
6724     BindNativeFunction(env, object, "setPreferredOrientation", moduleName, JsWindow::SetPreferredOrientation);
6725     BindNativeFunction(env, object, "getPreferredOrientation", moduleName, JsWindow::GetPreferredOrientation);
6726     BindNativeFunction(env, object, "opacity", moduleName, JsWindow::Opacity);
6727     BindNativeFunction(env, object, "scale", moduleName, JsWindow::Scale);
6728     BindNativeFunction(env, object, "rotate", moduleName, JsWindow::Rotate);
6729     BindNativeFunction(env, object, "translate", moduleName, JsWindow::Translate);
6730     BindNativeFunction(env, object, "getTransitionController", moduleName, JsWindow::GetTransitionController);
6731     BindNativeFunction(env, object, "snapshot", moduleName, JsWindow::Snapshot);
6732     BindNativeFunction(env, object, "setCornerRadius", moduleName, JsWindow::SetCornerRadius);
6733     BindNativeFunction(env, object, "setShadow", moduleName, JsWindow::SetShadow);
6734     BindNativeFunction(env, object, "setBlur", moduleName, JsWindow::SetBlur);
6735     BindNativeFunction(env, object, "setBackdropBlur", moduleName, JsWindow::SetBackdropBlur);
6736     BindNativeFunction(env, object, "setBackdropBlurStyle", moduleName, JsWindow::SetBackdropBlurStyle);
6737     BindNativeFunction(env, object, "setAspectRatio", moduleName, JsWindow::SetAspectRatio);
6738     BindNativeFunction(env, object, "resetAspectRatio", moduleName, JsWindow::ResetAspectRatio);
6739     BindNativeFunction(env, object, "setWaterMarkFlag", moduleName, JsWindow::SetWaterMarkFlag);
6740     BindNativeFunction(env, object, "setHandwritingFlag", moduleName, JsWindow::SetHandwritingFlag);
6741     BindNativeFunction(env, object, "minimize", moduleName, JsWindow::Minimize);
6742     BindNativeFunction(env, object, "maximize", moduleName, JsWindow::Maximize);
6743     BindNativeFunction(env, object, "setResizeByDragEnabled", moduleName, JsWindow::SetResizeByDragEnabled);
6744     BindNativeFunction(env, object, "setRaiseByClickEnabled", moduleName, JsWindow::SetRaiseByClickEnabled);
6745     BindNativeFunction(env, object, "raiseAboveTarget", moduleName, JsWindow::RaiseAboveTarget);
6746     BindNativeFunction(env, object, "hideNonSystemFloatingWindows", moduleName,
6747         JsWindow::HideNonSystemFloatingWindows);
6748     BindNativeFunction(env, object, "keepKeyboardOnFocus", moduleName, JsWindow::KeepKeyboardOnFocus);
6749     BindNativeFunction(env, object, "setWindowLimits", moduleName, JsWindow::SetWindowLimits);
6750     BindNativeFunction(env, object, "getWindowLimits", moduleName, JsWindow::GetWindowLimits);
6751     BindNativeFunction(env, object, "setSpecificSystemBarEnabled", moduleName, JsWindow::SetSpecificSystemBarEnabled);
6752     BindNativeFunction(env, object, "setSingleFrameComposerEnabled", moduleName,
6753         JsWindow::SetSingleFrameComposerEnabled);
6754     BindNativeFunction(env, object, "enableLandscapeMultiWindow", moduleName, JsWindow::EnableLandscapeMultiWindow);
6755     BindNativeFunction(env, object, "disableLandscapeMultiWindow", moduleName, JsWindow::DisableLandscapeMultiWindow);
6756     BindNativeFunction(env, object, "setWindowDecorVisible", moduleName, JsWindow::SetWindowDecorVisible);
6757     BindNativeFunction(env, object, "setSubWindowModal", moduleName, JsWindow::SetSubWindowModal);
6758     BindNativeFunction(env, object, "setWindowDecorHeight", moduleName, JsWindow::SetWindowDecorHeight);
6759     BindNativeFunction(env, object, "getWindowDecorHeight", moduleName, JsWindow::GetWindowDecorHeight);
6760     BindNativeFunction(env, object, "getTitleButtonRect", moduleName, JsWindow::GetTitleButtonRect);
6761     BindNativeFunction(env, object, "setWindowMask", moduleName, JsWindow::SetWindowMask);
6762     BindNativeFunction(env, object, "setTitleButtonVisible", moduleName, JsWindow::SetTitleButtonVisible);
6763     BindNativeFunction(env, object, "setWindowGrayScale", moduleName, JsWindow::SetWindowGrayScale);
6764     BindNativeFunction(env, object, "setImmersiveModeEnabledState", moduleName, JsWindow::SetImmersiveModeEnabledState);
6765     BindNativeFunction(env, object, "getImmersiveModeEnabledState", moduleName, JsWindow::GetImmersiveModeEnabledState);
6766     BindNativeFunction(env, object, "getWindowStatus", moduleName, JsWindow::GetWindowStatus);
6767     BindNativeFunction(env, object, "isFocused", moduleName, JsWindow::IsFocused);
6768     BindNativeFunction(env, object, "requestFocus", moduleName, JsWindow::RequestFocus);
6769     BindNativeFunction(env, object, "createSubWindowWithOptions", moduleName, JsWindow::CreateSubWindowWithOptions);
6770     BindNativeFunction(env, object, "setGestureBackEnabled", moduleName, JsWindow::SetGestureBackEnabled);
6771     BindNativeFunction(env, object, "isGestureBackEnabled", moduleName, JsWindow::GetGestureBackEnabled);
6772 }
6773 }  // namespace Rosen
6774 }  // namespace OHOS
6775