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