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