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