• 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_window_utils.h"
26 #include "window.h"
27 #include "window_helper.h"
28 #include "window_manager_hilog.h"
29 #include "window_option.h"
30 #include "wm_math.h"
31 #include "pixel_map.h"
32 #include "pixel_map_napi.h"
33 #include "napi_remote_object.h"
34 #include "permission.h"
35 #include "request_info.h"
36 #include "ui_content.h"
37 
38 namespace OHOS {
39 namespace Rosen {
40 using namespace AbilityRuntime;
41 namespace {
42     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "JsWindow"};
43     constexpr Rect g_emptyRect = {0, 0, 0, 0};
44 }
45 
46 static thread_local std::map<std::string, std::shared_ptr<NativeReference>> g_jsWindowMap;
47 std::recursive_mutex g_mutex;
48 static int ctorCnt = 0;
49 static int dtorCnt = 0;
50 static int finalizerCnt = 0;
JsWindow(const sptr<Window> & window)51 JsWindow::JsWindow(const sptr<Window>& window)
52     : windowToken_(window), registerManager_(std::make_unique<JsWindowRegisterManager>())
53 {
54     NotifyNativeWinDestroyFunc func = [](std::string windowName) {
55         std::lock_guard<std::recursive_mutex> lock(g_mutex);
56         if (windowName.empty() || g_jsWindowMap.count(windowName) == 0) {
57             WLOGFE("Can not find window %{public}s ", windowName.c_str());
58             return;
59         }
60         g_jsWindowMap.erase(windowName);
61         WLOGI("Destroy window %{public}s in js window", windowName.c_str());
62     };
63     windowToken_->RegisterWindowDestroyedListener(func);
64     WLOGI(" constructorCnt: %{public}d", ++ctorCnt);
65 }
66 
~JsWindow()67 JsWindow::~JsWindow()
68 {
69     WLOGI(" deConstructorCnt:%{public}d", ++dtorCnt);
70     windowToken_ = nullptr;
71 }
72 
GetWindowName()73 std::string JsWindow::GetWindowName()
74 {
75     if (windowToken_ == nullptr) {
76         return "";
77     }
78     return windowToken_->GetWindowName();
79 }
80 
Finalizer(NativeEngine * engine,void * data,void * hint)81 void JsWindow::Finalizer(NativeEngine* engine, void* data, void* hint)
82 {
83     WLOGI("finalizerCnt:%{public}d", ++finalizerCnt);
84     auto jsWin = std::unique_ptr<JsWindow>(static_cast<JsWindow*>(data));
85     if (jsWin == nullptr) {
86         WLOGFE("jsWin is nullptr");
87         return;
88     }
89     std::string windowName = jsWin->GetWindowName();
90     std::lock_guard<std::recursive_mutex> lock(g_mutex);
91     g_jsWindowMap.erase(windowName);
92     WLOGI("Remove window %{public}s from g_jsWindowMap", windowName.c_str());
93 }
94 
Show(NativeEngine * engine,NativeCallbackInfo * info)95 NativeValue* JsWindow::Show(NativeEngine* engine, NativeCallbackInfo* info)
96 {
97     WLOGI("Show");
98     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
99     return (me != nullptr) ? me->OnShow(*engine, *info) : nullptr;
100 }
101 
ShowWindow(NativeEngine * engine,NativeCallbackInfo * info)102 NativeValue* JsWindow::ShowWindow(NativeEngine* engine, NativeCallbackInfo* info)
103 {
104     WLOGI("Show");
105     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
106     return (me != nullptr) ? me->OnShowWindow(*engine, *info) : nullptr;
107 }
108 
ShowWithAnimation(NativeEngine * engine,NativeCallbackInfo * info)109 NativeValue* JsWindow::ShowWithAnimation(NativeEngine* engine, NativeCallbackInfo* info)
110 {
111     WLOGI("ShowWithAnimation");
112     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
113     return (me != nullptr) ? me->OnShowWithAnimation(*engine, *info) : nullptr;
114 }
115 
Destroy(NativeEngine * engine,NativeCallbackInfo * info)116 NativeValue* JsWindow::Destroy(NativeEngine* engine, NativeCallbackInfo* info)
117 {
118     WLOGI("Destroy");
119     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
120     return (me != nullptr) ? me->OnDestroy(*engine, *info) : nullptr;
121 }
122 
DestroyWindow(NativeEngine * engine,NativeCallbackInfo * info)123 NativeValue* JsWindow::DestroyWindow(NativeEngine* engine, NativeCallbackInfo* info)
124 {
125     WLOGI("Destroy");
126     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
127     return (me != nullptr) ? me->OnDestroyWindow(*engine, *info) : nullptr;
128 }
129 
Hide(NativeEngine * engine,NativeCallbackInfo * info)130 NativeValue* JsWindow::Hide(NativeEngine* engine, NativeCallbackInfo* info)
131 {
132     WLOGI("Hide");
133     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
134     return (me != nullptr) ? me->OnHide(*engine, *info) : nullptr;
135 }
136 
HideWithAnimation(NativeEngine * engine,NativeCallbackInfo * info)137 NativeValue* JsWindow::HideWithAnimation(NativeEngine* engine, NativeCallbackInfo* info)
138 {
139     WLOGI("HideWithAnimation");
140     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
141     return (me != nullptr) ? me->OnHideWithAnimation(*engine, *info) : nullptr;
142 }
143 
MoveTo(NativeEngine * engine,NativeCallbackInfo * info)144 NativeValue* JsWindow::MoveTo(NativeEngine* engine, NativeCallbackInfo* info)
145 {
146     WLOGD("MoveTo");
147     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
148     return (me != nullptr) ? me->OnMoveTo(*engine, *info) : nullptr;
149 }
150 
MoveWindowTo(NativeEngine * engine,NativeCallbackInfo * info)151 NativeValue* JsWindow::MoveWindowTo(NativeEngine* engine, NativeCallbackInfo* info)
152 {
153     WLOGD("MoveTo");
154     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
155     return (me != nullptr) ? me->OnMoveWindowTo(*engine, *info) : nullptr;
156 }
157 
Resize(NativeEngine * engine,NativeCallbackInfo * info)158 NativeValue* JsWindow::Resize(NativeEngine* engine, NativeCallbackInfo* info)
159 {
160     WLOGD("Resize");
161     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
162     return (me != nullptr) ? me->OnResize(*engine, *info) : nullptr;
163 }
164 
ResizeWindow(NativeEngine * engine,NativeCallbackInfo * info)165 NativeValue* JsWindow::ResizeWindow(NativeEngine* engine, NativeCallbackInfo* info)
166 {
167     WLOGI("Resize");
168     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
169     return (me != nullptr) ? me->OnResizeWindow(*engine, *info) : nullptr;
170 }
171 
SetWindowType(NativeEngine * engine,NativeCallbackInfo * info)172 NativeValue* JsWindow::SetWindowType(NativeEngine* engine, NativeCallbackInfo* info)
173 {
174     WLOGI("SetWindowType");
175     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
176     return (me != nullptr) ? me->OnSetWindowType(*engine, *info) : nullptr;
177 }
178 
SetWindowMode(NativeEngine * engine,NativeCallbackInfo * info)179 NativeValue* JsWindow::SetWindowMode(NativeEngine* engine, NativeCallbackInfo* info)
180 {
181     WLOGI("SetWindowMode");
182     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
183     return (me != nullptr) ? me->OnSetWindowMode(*engine, *info) : nullptr;
184 }
185 
GetProperties(NativeEngine * engine,NativeCallbackInfo * info)186 NativeValue* JsWindow::GetProperties(NativeEngine* engine, NativeCallbackInfo* info)
187 {
188     WLOGD("GetProperties");
189     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
190     return (me != nullptr) ? me->OnGetProperties(*engine, *info) : nullptr;
191 }
192 
GetWindowPropertiesSync(NativeEngine * engine,NativeCallbackInfo * info)193 NativeValue* JsWindow::GetWindowPropertiesSync(NativeEngine* engine, NativeCallbackInfo* info)
194 {
195     WLOGI("GetProperties");
196     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
197     return (me != nullptr) ? me->OnGetWindowPropertiesSync(*engine, *info) : nullptr;
198 }
199 
RegisterWindowCallback(NativeEngine * engine,NativeCallbackInfo * info)200 NativeValue* JsWindow::RegisterWindowCallback(NativeEngine* engine, NativeCallbackInfo* info)
201 {
202     WLOGI("RegisterWindowCallback");
203     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
204     return (me != nullptr) ? me->OnRegisterWindowCallback(*engine, *info) : nullptr;
205 }
206 
UnregisterWindowCallback(NativeEngine * engine,NativeCallbackInfo * info)207 NativeValue* JsWindow::UnregisterWindowCallback(NativeEngine* engine, NativeCallbackInfo* info)
208 {
209     WLOGI("UnregisterWindowCallback");
210     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
211     return (me != nullptr) ? me->OnUnregisterWindowCallback(*engine, *info) : nullptr;
212 }
213 
BindDialogTarget(NativeEngine * engine,NativeCallbackInfo * info)214 NativeValue* JsWindow::BindDialogTarget(NativeEngine* engine, NativeCallbackInfo* info)
215 {
216     WLOGI("BindDialogTarget");
217     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
218     return (me != nullptr) ? me->OnBindDialogTarget(*engine, *info) : nullptr;
219 }
220 
LoadContent(NativeEngine * engine,NativeCallbackInfo * info)221 NativeValue* JsWindow::LoadContent(NativeEngine* engine, NativeCallbackInfo* info)
222 {
223     WLOGD("LoadContent");
224     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
225     return (me != nullptr) ? me->OnLoadContent(*engine, *info) : nullptr;
226 }
227 
GetUIContext(NativeEngine * engine,NativeCallbackInfo * info)228 NativeValue* JsWindow::GetUIContext(NativeEngine* engine, NativeCallbackInfo* info)
229 {
230     WLOGD("GetUIContext");
231     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
232     return (me != nullptr) ? me->OnGetUIContext(*engine, *info) : nullptr;
233 }
234 
SetUIContent(NativeEngine * engine,NativeCallbackInfo * info)235 NativeValue* JsWindow::SetUIContent(NativeEngine* engine, NativeCallbackInfo* info)
236 {
237     WLOGI("LoadContent");
238     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
239     return (me != nullptr) ? me->OnSetUIContent(*engine, *info) : nullptr;
240 }
241 
SetFullScreen(NativeEngine * engine,NativeCallbackInfo * info)242 NativeValue* JsWindow::SetFullScreen(NativeEngine* engine, NativeCallbackInfo* info)
243 {
244     WLOGI("SetFullScreen");
245     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
246     return (me != nullptr) ? me->OnSetFullScreen(*engine, *info) : nullptr;
247 }
248 
SetLayoutFullScreen(NativeEngine * engine,NativeCallbackInfo * info)249 NativeValue* JsWindow::SetLayoutFullScreen(NativeEngine* engine, NativeCallbackInfo* info)
250 {
251     WLOGI("SetLayoutFullScreen");
252     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
253     return (me != nullptr) ? me->OnSetLayoutFullScreen(*engine, *info) : nullptr;
254 }
255 
SetWindowLayoutFullScreen(NativeEngine * engine,NativeCallbackInfo * info)256 NativeValue* JsWindow::SetWindowLayoutFullScreen(NativeEngine* engine, NativeCallbackInfo* info)
257 {
258     WLOGI("SetLayoutFullScreen");
259     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
260     return (me != nullptr) ? me->OnSetWindowLayoutFullScreen(*engine, *info) : nullptr;
261 }
262 
SetSystemBarEnable(NativeEngine * engine,NativeCallbackInfo * info)263 NativeValue* JsWindow::SetSystemBarEnable(NativeEngine* engine, NativeCallbackInfo* info)
264 {
265     WLOGI("SetSystemBarEnable");
266     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
267     return (me != nullptr) ? me->OnSetSystemBarEnable(*engine, *info) : nullptr;
268 }
269 
SetWindowSystemBarEnable(NativeEngine * engine,NativeCallbackInfo * info)270 NativeValue* JsWindow::SetWindowSystemBarEnable(NativeEngine* engine, NativeCallbackInfo* info)
271 {
272     WLOGI("SetSystemBarEnable");
273     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
274     return (me != nullptr) ? me->OnSetWindowSystemBarEnable(*engine, *info) : nullptr;
275 }
276 
SetSystemBarProperties(NativeEngine * engine,NativeCallbackInfo * info)277 NativeValue* JsWindow::SetSystemBarProperties(NativeEngine* engine, NativeCallbackInfo* info)
278 {
279     WLOGI("SetSystemBarProperties");
280     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
281     return (me != nullptr) ? me->OnSetSystemBarProperties(*engine, *info) : nullptr;
282 }
283 
SetWindowSystemBarProperties(NativeEngine * engine,NativeCallbackInfo * info)284 NativeValue* JsWindow::SetWindowSystemBarProperties(NativeEngine* engine, NativeCallbackInfo* info)
285 {
286     WLOGI("SetWindowSystemBarProperties");
287     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
288     return (me != nullptr) ? me->OnSetWindowSystemBarProperties(*engine, *info) : nullptr;
289 }
290 
GetAvoidArea(NativeEngine * engine,NativeCallbackInfo * info)291 NativeValue* JsWindow::GetAvoidArea(NativeEngine* engine, NativeCallbackInfo* info)
292 {
293     WLOGI("GetAvoidArea");
294     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
295     return (me != nullptr) ? me->OnGetAvoidArea(*engine, *info) : nullptr;
296 }
297 
GetWindowAvoidAreaSync(NativeEngine * engine,NativeCallbackInfo * info)298 NativeValue* JsWindow::GetWindowAvoidAreaSync(NativeEngine* engine, NativeCallbackInfo* info)
299 {
300     WLOGI("GetAvoidArea");
301     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
302     return (me != nullptr) ? me->OnGetWindowAvoidAreaSync(*engine, *info) : nullptr;
303 }
304 
IsShowing(NativeEngine * engine,NativeCallbackInfo * info)305 NativeValue* JsWindow::IsShowing(NativeEngine* engine, NativeCallbackInfo* info)
306 {
307     WLOGI("IsShowing");
308     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
309     return (me != nullptr) ? me->OnIsShowing(*engine, *info) : nullptr;
310 }
311 
IsWindowShowingSync(NativeEngine * engine,NativeCallbackInfo * info)312 NativeValue* JsWindow::IsWindowShowingSync(NativeEngine* engine, NativeCallbackInfo* info)
313 {
314     WLOGI("IsShowing");
315     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
316     return (me != nullptr) ? me->OnIsWindowShowingSync(*engine, *info) : nullptr;
317 }
318 
IsSupportWideGamut(NativeEngine * engine,NativeCallbackInfo * info)319 NativeValue* JsWindow::IsSupportWideGamut(NativeEngine* engine, NativeCallbackInfo* info)
320 {
321     WLOGI("IsSupportWideGamut");
322     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
323     return (me != nullptr) ? me->OnIsSupportWideGamut(*engine, *info) : nullptr;
324 }
325 
IsWindowSupportWideGamut(NativeEngine * engine,NativeCallbackInfo * info)326 NativeValue* JsWindow::IsWindowSupportWideGamut(NativeEngine* engine, NativeCallbackInfo* info)
327 {
328     WLOGI("IsSupportWideGamut");
329     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
330     return (me != nullptr) ? me->OnIsWindowSupportWideGamut(*engine, *info) : nullptr;
331 }
332 
SetBackgroundColor(NativeEngine * engine,NativeCallbackInfo * info)333 NativeValue* JsWindow::SetBackgroundColor(NativeEngine* engine, NativeCallbackInfo* info)
334 {
335     WLOGFD("SetBackgroundColor");
336     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
337     return (me != nullptr) ? me->OnSetBackgroundColor(*engine, *info) : nullptr;
338 }
339 
SetWindowBackgroundColorSync(NativeEngine * engine,NativeCallbackInfo * info)340 NativeValue* JsWindow::SetWindowBackgroundColorSync(NativeEngine* engine, NativeCallbackInfo* info)
341 {
342     WLOGI("SetBackgroundColor");
343     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
344     return (me != nullptr) ? me->OnSetWindowBackgroundColorSync(*engine, *info) : nullptr;
345 }
346 
SetBrightness(NativeEngine * engine,NativeCallbackInfo * info)347 NativeValue* JsWindow::SetBrightness(NativeEngine* engine, NativeCallbackInfo* info)
348 {
349     WLOGI("SetBrightness");
350     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
351     return (me != nullptr) ? me->OnSetBrightness(*engine, *info) : nullptr;
352 }
353 
SetWindowBrightness(NativeEngine * engine,NativeCallbackInfo * info)354 NativeValue* JsWindow::SetWindowBrightness(NativeEngine* engine, NativeCallbackInfo* info)
355 {
356     WLOGI("SetBrightness");
357     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
358     return (me != nullptr) ? me->OnSetWindowBrightness(*engine, *info) : nullptr;
359 }
360 
SetDimBehind(NativeEngine * engine,NativeCallbackInfo * info)361 NativeValue* JsWindow::SetDimBehind(NativeEngine* engine, NativeCallbackInfo* info)
362 {
363     WLOGI("SetDimBehind");
364     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
365     return (me != nullptr) ? me->OnSetDimBehind(*engine, *info) : nullptr;
366 }
367 
SetFocusable(NativeEngine * engine,NativeCallbackInfo * info)368 NativeValue* JsWindow::SetFocusable(NativeEngine* engine, NativeCallbackInfo* info)
369 {
370     WLOGI("SetFocusable");
371     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
372     return (me != nullptr) ? me->OnSetFocusable(*engine, *info) : nullptr;
373 }
374 
SetWindowFocusable(NativeEngine * engine,NativeCallbackInfo * info)375 NativeValue* JsWindow::SetWindowFocusable(NativeEngine* engine, NativeCallbackInfo* info)
376 {
377     WLOGI("SetFocusable");
378     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
379     return (me != nullptr) ? me->OnSetWindowFocusable(*engine, *info) : nullptr;
380 }
381 
SetKeepScreenOn(NativeEngine * engine,NativeCallbackInfo * info)382 NativeValue* JsWindow::SetKeepScreenOn(NativeEngine* engine, NativeCallbackInfo* info)
383 {
384     WLOGI("SetKeepScreenOn");
385     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
386     return (me != nullptr) ? me->OnSetKeepScreenOn(*engine, *info) : nullptr;
387 }
388 
SetWindowKeepScreenOn(NativeEngine * engine,NativeCallbackInfo * info)389 NativeValue* JsWindow::SetWindowKeepScreenOn(NativeEngine* engine, NativeCallbackInfo* info)
390 {
391     WLOGI("SetKeepScreenOn");
392     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
393     return (me != nullptr) ? me->OnSetWindowKeepScreenOn(*engine, *info) : nullptr;
394 }
395 
SetWakeUpScreen(NativeEngine * engine,NativeCallbackInfo * info)396 NativeValue* JsWindow::SetWakeUpScreen(NativeEngine* engine, NativeCallbackInfo* info)
397 {
398     WLOGI("SetWakeUpScreen");
399     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
400     return (me != nullptr) ? me->OnSetWakeUpScreen(*engine, *info) : nullptr;
401 }
402 
SetOutsideTouchable(NativeEngine * engine,NativeCallbackInfo * info)403 NativeValue* JsWindow::SetOutsideTouchable(NativeEngine* engine, NativeCallbackInfo* info)
404 {
405     WLOGI("SetOutsideTouchable");
406     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
407     return (me != nullptr) ? me->OnSetOutsideTouchable(*engine, *info) : nullptr;
408 }
409 
SetPrivacyMode(NativeEngine * engine,NativeCallbackInfo * info)410 NativeValue* JsWindow::SetPrivacyMode(NativeEngine* engine, NativeCallbackInfo* info)
411 {
412     WLOGI("SetPrivacyMode");
413     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
414     return (me != nullptr) ? me->OnSetPrivacyMode(*engine, *info) : nullptr;
415 }
416 
SetWindowPrivacyMode(NativeEngine * engine,NativeCallbackInfo * info)417 NativeValue* JsWindow::SetWindowPrivacyMode(NativeEngine* engine, NativeCallbackInfo* info)
418 {
419     WLOGI("SetPrivacyMode");
420     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
421     return (me != nullptr) ? me->OnSetWindowPrivacyMode(*engine, *info) : nullptr;
422 }
423 
SetTouchable(NativeEngine * engine,NativeCallbackInfo * info)424 NativeValue* JsWindow::SetTouchable(NativeEngine* engine, NativeCallbackInfo* info)
425 {
426     WLOGI("SetTouchable");
427     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
428     return (me != nullptr) ? me->OnSetTouchable(*engine, *info) : nullptr;
429 }
430 
SetResizeByDragEnabled(NativeEngine * engine,NativeCallbackInfo * info)431 NativeValue* JsWindow::SetResizeByDragEnabled(NativeEngine* engine, NativeCallbackInfo* info)
432 {
433     WLOGI("SetResizeByDragEnabled");
434     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
435     return (me != nullptr) ? me->OnSetResizeByDragEnabled(*engine, *info) : nullptr;
436 }
437 
SetRaiseByClickEnabled(NativeEngine * engine,NativeCallbackInfo * info)438 NativeValue* JsWindow::SetRaiseByClickEnabled(NativeEngine* engine, NativeCallbackInfo* info)
439 {
440     WLOGI("SetRaiseByClickEnabled");
441     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
442     return (me != nullptr) ? me->OnSetRaiseByClickEnabled(*engine, *info) : nullptr;
443 }
444 
SetWindowTouchable(NativeEngine * engine,NativeCallbackInfo * info)445 NativeValue* JsWindow::SetWindowTouchable(NativeEngine* engine, NativeCallbackInfo* info)
446 {
447     WLOGI("SetTouchable");
448     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
449     return (me != nullptr) ? me->OnSetWindowTouchable(*engine, *info) : nullptr;
450 }
451 
SetTransparent(NativeEngine * engine,NativeCallbackInfo * info)452 NativeValue* JsWindow::SetTransparent(NativeEngine* engine, NativeCallbackInfo* info)
453 {
454     WLOGI("SetTransparent");
455     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
456     return (me != nullptr) ? me->OnSetTransparent(*engine, *info) : nullptr;
457 }
458 
SetCallingWindow(NativeEngine * engine,NativeCallbackInfo * info)459 NativeValue* JsWindow::SetCallingWindow(NativeEngine* engine, NativeCallbackInfo* info)
460 {
461     WLOGI("SetCallingWindow");
462     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
463     return (me != nullptr) ? me->OnSetCallingWindow(*engine, *info) : nullptr;
464 }
465 
SetPreferredOrientation(NativeEngine * engine,NativeCallbackInfo * info)466 NativeValue* JsWindow::SetPreferredOrientation(NativeEngine* engine, NativeCallbackInfo* info)
467 {
468     WLOGI("SetPreferredOrientation");
469     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
470     return (me != nullptr) ? me->OnSetPreferredOrientation(*engine, *info) : nullptr;
471 }
472 
SetSnapshotSkip(NativeEngine * engine,NativeCallbackInfo * info)473 NativeValue* JsWindow::SetSnapshotSkip(NativeEngine* engine, NativeCallbackInfo* info)
474 {
475     WLOGI("SetSnapshotSkip");
476     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
477     return (me != nullptr) ? me->OnSetSnapshotSkip(*engine, *info) : nullptr;
478 }
479 
RaiseToAppTop(NativeEngine * engine,NativeCallbackInfo * info)480 NativeValue* JsWindow::RaiseToAppTop(NativeEngine* engine, NativeCallbackInfo* info)
481 {
482     WLOGI("RaiseToAppTop");
483     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
484     return (me != nullptr) ? me->OnRaiseToAppTop(*engine, *info) : nullptr;
485 }
486 
DisableWindowDecor(NativeEngine * engine,NativeCallbackInfo * info)487 NativeValue* JsWindow::DisableWindowDecor(NativeEngine* engine, NativeCallbackInfo* info)
488 {
489     WLOGI("DisableWindowDecor");
490     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
491     return (me != nullptr) ? me->OnDisableWindowDecor(*engine, *info) : nullptr;
492 }
493 
SetColorSpace(NativeEngine * engine,NativeCallbackInfo * info)494 NativeValue* JsWindow::SetColorSpace(NativeEngine* engine, NativeCallbackInfo* info)
495 {
496     WLOGI("SetColorSpace");
497     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
498     return (me != nullptr) ? me->OnSetColorSpace(*engine, *info) : nullptr;
499 }
500 
SetWindowColorSpace(NativeEngine * engine,NativeCallbackInfo * info)501 NativeValue* JsWindow::SetWindowColorSpace(NativeEngine* engine, NativeCallbackInfo* info)
502 {
503     WLOGI("SetColorSpace");
504     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
505     return (me != nullptr) ? me->OnSetWindowColorSpace(*engine, *info) : nullptr;
506 }
507 
GetColorSpace(NativeEngine * engine,NativeCallbackInfo * info)508 NativeValue* JsWindow::GetColorSpace(NativeEngine* engine, NativeCallbackInfo* info)
509 {
510     WLOGI("GetColorSpace");
511     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
512     return (me != nullptr) ? me->OnGetColorSpace(*engine, *info) : nullptr;
513 }
514 
GetWindowColorSpaceSync(NativeEngine * engine,NativeCallbackInfo * info)515 NativeValue* JsWindow::GetWindowColorSpaceSync(NativeEngine* engine, NativeCallbackInfo* info)
516 {
517     WLOGI("GetColorSpace");
518     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
519     return (me != nullptr) ? me->OnGetWindowColorSpaceSync(*engine, *info) : nullptr;
520 }
521 
Dump(NativeEngine * engine,NativeCallbackInfo * info)522 NativeValue* JsWindow::Dump(NativeEngine* engine, NativeCallbackInfo* info)
523 {
524     WLOGI("Dump");
525     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
526     return (me != nullptr) ? me->OnDump(*engine, *info) : nullptr;
527 }
528 
SetForbidSplitMove(NativeEngine * engine,NativeCallbackInfo * info)529 NativeValue* JsWindow::SetForbidSplitMove(NativeEngine* engine, NativeCallbackInfo* info)
530 {
531     WLOGI("SetForbidSplitMove");
532     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
533     return (me != nullptr) ? me->OnSetForbidSplitMove(*engine, *info) : nullptr;
534 }
535 
Opacity(NativeEngine * engine,NativeCallbackInfo * info)536 NativeValue* JsWindow::Opacity(NativeEngine* engine, NativeCallbackInfo* info)
537 {
538     WLOGI("Opacity");
539     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
540     return (me != nullptr) ? me->OnOpacity(*engine, *info) : nullptr;
541 }
542 
Scale(NativeEngine * engine,NativeCallbackInfo * info)543 NativeValue* JsWindow::Scale(NativeEngine* engine, NativeCallbackInfo* info)
544 {
545     WLOGI("Scale");
546     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
547     return (me != nullptr) ? me->OnScale(*engine, *info) : nullptr;
548 }
549 
Rotate(NativeEngine * engine,NativeCallbackInfo * info)550 NativeValue* JsWindow::Rotate(NativeEngine* engine, NativeCallbackInfo* info)
551 {
552     WLOGI("Rotate");
553     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
554     return (me != nullptr) ? me->OnRotate(*engine, *info) : nullptr;
555 }
556 
Translate(NativeEngine * engine,NativeCallbackInfo * info)557 NativeValue* JsWindow::Translate(NativeEngine* engine, NativeCallbackInfo* info)
558 {
559     WLOGI("Translate");
560     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
561     return (me != nullptr) ? me->OnTranslate(*engine, *info) : nullptr;
562 }
563 
GetTransitionController(NativeEngine * engine,NativeCallbackInfo * info)564 NativeValue* JsWindow::GetTransitionController(NativeEngine* engine, NativeCallbackInfo* info)
565 {
566     WLOGI("GetTransitionController");
567     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
568     return (me != nullptr) ? me->OnGetTransitionController(*engine, *info) : nullptr;
569 }
570 
SetCornerRadius(NativeEngine * engine,NativeCallbackInfo * info)571 NativeValue* JsWindow::SetCornerRadius(NativeEngine* engine, NativeCallbackInfo* info)
572 {
573     WLOGI("SetCornerRadius");
574     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
575     return (me != nullptr) ? me->OnSetCornerRadius(*engine, *info) : nullptr;
576 }
577 
SetShadow(NativeEngine * engine,NativeCallbackInfo * info)578 NativeValue* JsWindow::SetShadow(NativeEngine* engine, NativeCallbackInfo* info)
579 {
580     WLOGI("SetShadow");
581     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
582     return (me != nullptr) ? me->OnSetShadow(*engine, *info) : nullptr;
583 }
584 
SetBlur(NativeEngine * engine,NativeCallbackInfo * info)585 NativeValue* JsWindow::SetBlur(NativeEngine* engine, NativeCallbackInfo* info)
586 {
587     WLOGI("SetBlur");
588     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
589     return (me != nullptr) ? me->OnSetBlur(*engine, *info) : nullptr;
590 }
591 
SetBackdropBlur(NativeEngine * engine,NativeCallbackInfo * info)592 NativeValue* JsWindow::SetBackdropBlur(NativeEngine* engine, NativeCallbackInfo* info)
593 {
594     WLOGI("SetBackdropBlur");
595     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
596     return (me != nullptr) ? me->OnSetBackdropBlur(*engine, *info) : nullptr;
597 }
598 
SetBackdropBlurStyle(NativeEngine * engine,NativeCallbackInfo * info)599 NativeValue* JsWindow::SetBackdropBlurStyle(NativeEngine* engine, NativeCallbackInfo* info)
600 {
601     WLOGI("SetBackdropBlurStyle");
602     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
603     return (me != nullptr) ? me->OnSetBackdropBlurStyle(*engine, *info) : nullptr;
604 }
605 
SetWaterMarkFlag(NativeEngine * engine,NativeCallbackInfo * info)606 NativeValue* JsWindow::SetWaterMarkFlag(NativeEngine* engine, NativeCallbackInfo* info)
607 {
608     WLOGI("SetWaterMarkFlag");
609     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
610     return (me != nullptr) ? me->OnSetWaterMarkFlag(*engine, *info) : nullptr;
611 }
612 
SetAspectRatio(NativeEngine * engine,NativeCallbackInfo * info)613 NativeValue* JsWindow::SetAspectRatio(NativeEngine* engine, NativeCallbackInfo* info)
614 {
615     WLOGI("[NAPI]SetAspectRatio");
616     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
617     return (me != nullptr) ? me->OnSetAspectRatio(*engine, *info) : nullptr;
618 }
619 
ResetAspectRatio(NativeEngine * engine,NativeCallbackInfo * info)620 NativeValue* JsWindow::ResetAspectRatio(NativeEngine* engine, NativeCallbackInfo* info)
621 {
622     WLOGI("[NAPI]ResetAspectRatio");
623     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
624     return (me != nullptr) ? me->OnResetAspectRatio(*engine, *info) : nullptr;
625 }
626 
Minimize(NativeEngine * engine,NativeCallbackInfo * info)627 NativeValue* JsWindow::Minimize(NativeEngine* engine, NativeCallbackInfo* info)
628 {
629     WLOGI("[NAPI]Minimize");
630     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
631     return (me != nullptr) ? me->OnMinimize(*engine, *info) : nullptr;
632 }
633 
RaiseAboveTarget(NativeEngine * engine,NativeCallbackInfo * info)634 NativeValue* JsWindow::RaiseAboveTarget(NativeEngine* engine, NativeCallbackInfo* info)
635 {
636     WLOGI("[NAPI]RaiseAboveTarget");
637     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
638     return (me != nullptr) ? me->OnRaiseAboveTarget(*engine, *info) : nullptr;
639 }
640 
UpdateSystemBarProperties(std::map<WindowType,SystemBarProperty> & systemBarProperties,const std::map<WindowType,SystemBarPropertyFlag> & systemBarPropertyFlags,wptr<Window> weakToken)641 static void UpdateSystemBarProperties(std::map<WindowType, SystemBarProperty>& systemBarProperties,
642     const std::map<WindowType, SystemBarPropertyFlag>& systemBarPropertyFlags, wptr<Window> weakToken)
643 {
644     auto weakWindow = weakToken.promote();
645 
646     for (auto it : systemBarPropertyFlags) {
647         WindowType type = it.first;
648         SystemBarPropertyFlag flag = it.second;
649         auto property = weakWindow->GetSystemBarPropertyByType(type);
650         if (flag.enableFlag == false) {
651             systemBarProperties[type].enable_ = property.enable_;
652         }
653         if (flag.backgroundColorFlag == false) {
654             systemBarProperties[type].backgroundColor_ = property.backgroundColor_;
655         }
656         if (flag.contentColorFlag == false) {
657             systemBarProperties[type].contentColor_ = property.contentColor_;
658         }
659     }
660 
661     return;
662 }
663 
OnShow(NativeEngine & engine,NativeCallbackInfo & info)664 NativeValue* JsWindow::OnShow(NativeEngine& engine, NativeCallbackInfo& info)
665 {
666     WMError errCode = WMError::WM_OK;
667     if (info.argc > 1) {
668         WLOGFE("Argc is invalid: %{public}zu", info.argc);
669         errCode = WMError::WM_ERROR_INVALID_PARAM;
670     }
671     wptr<Window> weakToken(windowToken_);
672     AsyncTask::CompleteCallback complete =
673         [weakToken, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
674             auto weakWindow = weakToken.promote();
675             if (weakWindow == nullptr) {
676                 WLOGFE("window is nullptr");
677                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
678                 return;
679             }
680             if (errCode != WMError::WM_OK) {
681                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
682                 WLOGFE("window is nullptr or get invalid param");
683                 return;
684             }
685             WMError ret = weakWindow->Show(0, false);
686             if (ret == WMError::WM_OK) {
687                 task.Resolve(engine, engine.CreateUndefined());
688             } else {
689                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window show failed"));
690             }
691             WLOGI("Window [%{public}u] show end, ret = %{public}d", weakWindow->GetWindowId(), ret);
692         };
693     NativeValue* result = nullptr;
694     NativeValue* lastParam = (info.argc == 0) ? nullptr :
695         (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
696     AsyncTask::Schedule("JsWindow::OnShow",
697         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
698     return result;
699 }
700 
OnShowWindow(NativeEngine & engine,NativeCallbackInfo & info)701 NativeValue* JsWindow::OnShowWindow(NativeEngine& engine, NativeCallbackInfo& info)
702 {
703     wptr<Window> weakToken(windowToken_);
704     AsyncTask::CompleteCallback complete =
705         [weakToken](NativeEngine& engine, AsyncTask& task, int32_t status) {
706             auto weakWindow = weakToken.promote();
707             if (weakWindow == nullptr) {
708                 task.Reject(engine, CreateJsError(engine,
709                     static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
710                 WLOGFE("window is nullptr or get invalid param");
711                 return;
712             }
713             WMError ret = weakWindow->Show(0, false);
714             if (ret == WMError::WM_OK) {
715                 task.Resolve(engine, engine.CreateUndefined());
716             } else {
717                 task.Reject(engine, CreateJsError(engine,
718                     static_cast<int32_t>(WM_JS_TO_ERROR_CODE_MAP.at(ret)), "Window show failed"));
719             }
720             WLOGI("Window [%{public}u, %{public}s] show end, ret = %{public}d",
721                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
722         };
723     NativeValue* result = nullptr;
724     NativeValue* lastParam = (info.argc == 0) ? nullptr :
725         ((info.argv[0] != nullptr && info.argv[0]->TypeOf() == NATIVE_FUNCTION) ?
726         info.argv[0] : nullptr);
727     AsyncTask::Schedule("JsWindow::OnShow",
728         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
729     return result;
730 }
731 
OnShowWithAnimation(NativeEngine & engine,NativeCallbackInfo & info)732 NativeValue* JsWindow::OnShowWithAnimation(NativeEngine& engine, NativeCallbackInfo& info)
733 {
734     WmErrorCode errCode = WmErrorCode::WM_OK;
735     auto winType = windowToken_->GetType();
736     if (!WindowHelper::IsSystemWindow(winType)) {
737         WLOGFE("window Type %{public}u is not supported", static_cast<uint32_t>(winType));
738         errCode = WmErrorCode::WM_ERROR_INVALID_CALLING;
739     }
740     wptr<Window> weakToken(windowToken_);
741     AsyncTask::CompleteCallback complete =
742         [weakToken, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
743             if (errCode != WmErrorCode::WM_OK) {
744                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
745                 return;
746             }
747             auto weakWindow = weakToken.promote();
748             if (weakWindow == nullptr) {
749                 WLOGFE("window is nullptr");
750                 task.Reject(engine, CreateJsError(engine,
751                     static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
752                 return;
753             }
754             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->Show(0, true));
755             if (ret == WmErrorCode::WM_OK) {
756                 task.Resolve(engine, engine.CreateUndefined());
757             } else {
758                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window show failed"));
759             }
760             WLOGI("Window [%{public}u, %{public}s] ShowWithAnimation end, ret = %{public}d",
761                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
762         };
763     NativeValue* result = nullptr;
764     NativeValue* lastParam = (info.argc == 0) ? nullptr :
765         ((info.argv[0] != nullptr && info.argv[0]->TypeOf() == NATIVE_FUNCTION) ?
766         info.argv[0] : nullptr);
767     AsyncTask::Schedule("JsWindow::OnShowWithAnimation",
768         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
769     return result;
770 }
771 
OnDestroy(NativeEngine & engine,NativeCallbackInfo & info)772 NativeValue* JsWindow::OnDestroy(NativeEngine& engine, NativeCallbackInfo& info)
773 {
774     WMError errCode = WMError::WM_OK;
775     if (info.argc > 1) {
776         WLOGFE("Argc is invalid: %{public}zu", info.argc);
777         errCode = WMError::WM_ERROR_INVALID_PARAM;
778     }
779     wptr<Window> weakToken(windowToken_);
780     AsyncTask::CompleteCallback complete =
781         [this, weakToken, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
782             auto weakWindow = weakToken.promote();
783             if (weakWindow == nullptr) {
784                 WLOGFE("window is nullptr");
785                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
786                 return;
787             }
788             if (errCode != WMError::WM_OK) {
789                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
790                 WLOGFE("window is nullptr or get invalid param");
791                 return;
792             }
793             WMError ret = weakWindow->Destroy();
794             WLOGI("Window [%{public}u, %{public}s] destroy end, ret = %{public}d",
795                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
796             if (ret != WMError::WM_OK) {
797                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window destroy failed"));
798                 return;
799             }
800             windowToken_ = nullptr; // ensure window dtor when finalizer invalid
801             task.Resolve(engine, engine.CreateUndefined());
802         };
803 
804     NativeValue* lastParam = (info.argc == 0) ? nullptr :
805         (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
806     NativeValue* result = nullptr;
807     AsyncTask::Schedule("JsWindow::OnDestroy",
808         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
809     return result;
810 }
811 
OnDestroyWindow(NativeEngine & engine,NativeCallbackInfo & info)812 NativeValue* JsWindow::OnDestroyWindow(NativeEngine& engine, NativeCallbackInfo& info)
813 {
814     wptr<Window> weakToken(windowToken_);
815     AsyncTask::CompleteCallback complete =
816         [this, weakToken](NativeEngine& engine, AsyncTask& task, int32_t status) {
817             auto weakWindow = weakToken.promote();
818             if (weakWindow == nullptr) {
819                 WLOGFE("window is nullptr or get invalid param");
820                 task.Reject(engine,
821                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
822                 return;
823             }
824             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->Destroy());
825             WLOGI("Window [%{public}u, %{public}s] destroy end, ret = %{public}d",
826                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
827             if (ret != WmErrorCode::WM_OK) {
828                 task.Reject(engine,
829                     CreateJsError(engine, static_cast<int32_t>(ret),
830                     "Window destroy failed"));
831                 return;
832             }
833             windowToken_ = nullptr; // ensure window dtor when finalizer invalid
834             task.Resolve(engine, engine.CreateUndefined());
835         };
836 
837     NativeValue* lastParam = (info.argc == 0) ? nullptr :
838         ((info.argv[0] != nullptr && info.argv[0]->TypeOf() == NATIVE_FUNCTION) ?
839         info.argv[0] : nullptr);
840     NativeValue* result = nullptr;
841     AsyncTask::Schedule("JsWindow::OnDestroyWindow",
842         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
843     return result;
844 }
845 
OnHide(NativeEngine & engine,NativeCallbackInfo & info)846 NativeValue* JsWindow::OnHide(NativeEngine& engine, NativeCallbackInfo& info)
847 {
848     wptr<Window> weakToken(windowToken_);
849     AsyncTask::CompleteCallback complete =
850         [weakToken](NativeEngine& engine, AsyncTask& task, int32_t status) {
851             auto weakWindow = weakToken.promote();
852             if (weakWindow == nullptr) {
853                 WLOGFE("window is nullptr or get invalid param");
854                 task.Reject(engine,
855                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
856                 return;
857             }
858             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->Hide(0, false, false));
859             if (ret == WmErrorCode::WM_OK) {
860                 task.Resolve(engine, engine.CreateUndefined());
861             } else {
862                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window hide failed"));
863             }
864             WLOGI("Window [%{public}u] hide end, ret = %{public}d", weakWindow->GetWindowId(), ret);
865         };
866 
867     NativeValue* result = nullptr;
868     NativeValue* lastParam = (info.argc == 0) ? nullptr :
869         (info.argv[0] != nullptr && info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
870     AsyncTask::Schedule("JsWindow::OnHide",
871         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
872     return result;
873 }
874 
OnHideWithAnimation(NativeEngine & engine,NativeCallbackInfo & info)875 NativeValue* JsWindow::OnHideWithAnimation(NativeEngine& engine, NativeCallbackInfo& info)
876 {
877     WmErrorCode errCode = WmErrorCode::WM_OK;
878     if (windowToken_) {
879         auto winType = windowToken_->GetType();
880         if (!WindowHelper::IsSystemWindow(winType)) {
881             WLOGFE("window Type %{public}u is not supported", static_cast<uint32_t>(winType));
882             errCode = WmErrorCode::WM_ERROR_INVALID_CALLING;
883         }
884     } else {
885         errCode = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
886     }
887     wptr<Window> weakToken(windowToken_);
888     AsyncTask::CompleteCallback complete =
889         [weakToken, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
890             if (errCode != WmErrorCode::WM_OK) {
891                 task.Reject(engine,
892                     CreateJsError(engine, static_cast<int32_t>(errCode)));
893                 return;
894             }
895             auto weakWindow = weakToken.promote();
896             if (weakWindow == nullptr) {
897                 WLOGFE("window is nullptr");
898                 task.Reject(engine,
899                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
900                 return;
901             }
902             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->Hide(0, true, false));
903             if (ret == WmErrorCode::WM_OK) {
904                 task.Resolve(engine, engine.CreateUndefined());
905             } else {
906                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window show failed"));
907             }
908             WLOGI("Window [%{public}u, %{public}s] HideWithAnimation end, ret = %{public}d",
909                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
910         };
911     NativeValue* result = nullptr;
912     NativeValue* lastParam = (info.argc == 0) ? nullptr :
913         ((info.argv[0] != nullptr && info.argv[0]->TypeOf() == NATIVE_FUNCTION) ?
914         info.argv[0] : nullptr);
915     AsyncTask::Schedule("JsWindow::OnHideWithAnimation",
916         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
917     return result;
918 }
919 
OnMoveTo(NativeEngine & engine,NativeCallbackInfo & info)920 NativeValue* JsWindow::OnMoveTo(NativeEngine& engine, NativeCallbackInfo& info)
921 {
922     WMError errCode = WMError::WM_OK;
923     if (info.argc < 2 || info.argc > 3) { // 2:minimum param num, 3: maximum param num
924         WLOGFE("Argc is invalid: %{public}zu", info.argc);
925         errCode = WMError::WM_ERROR_INVALID_PARAM;
926     }
927     int32_t x = 0;
928     if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[0], x)) {
929         WLOGFE("Failed to convert parameter to x");
930         errCode = WMError::WM_ERROR_INVALID_PARAM;
931     }
932 
933     int32_t y = 0;
934     if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[1], y)) {
935         WLOGFE("Failed to convert parameter to y");
936         errCode = WMError::WM_ERROR_INVALID_PARAM;
937     }
938 
939     wptr<Window> weakToken(windowToken_);
940     AsyncTask::CompleteCallback complete =
941         [weakToken, errCode, x, y](NativeEngine& engine, AsyncTask& task, int32_t status) {
942             auto weakWindow = weakToken.promote();
943             if (weakWindow == nullptr) {
944                 WLOGFE("window is nullptr");
945                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
946                 return;
947             }
948             if (errCode != WMError::WM_OK) {
949                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
950                 WLOGFE("window is nullptr or get invalid param");
951                 return;
952             }
953             WMError ret = weakWindow->MoveTo(x, y);
954             if (ret == WMError::WM_OK) {
955                 task.Resolve(engine, engine.CreateUndefined());
956             } else {
957                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window move failed"));
958             }
959             WLOGFD("Window [%{public}u, %{public}s] move end, ret = %{public}d",
960                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
961         };
962     // 2: params num; 2: index of callback
963     NativeValue* lastParam = (info.argc <= 2) ? nullptr :
964         (info.argv[2]->TypeOf() == NATIVE_FUNCTION ? info.argv[2] : nullptr);
965     NativeValue* result = nullptr;
966     AsyncTask::Schedule("JsWindow::OnMoveTo",
967         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
968     return result;
969 }
970 
OnMoveWindowTo(NativeEngine & engine,NativeCallbackInfo & info)971 NativeValue* JsWindow::OnMoveWindowTo(NativeEngine& engine, NativeCallbackInfo& info)
972 {
973     WmErrorCode errCode = WmErrorCode::WM_OK;
974     if (info.argc < 2) { // 2:minimum param num
975         WLOGFE("Argc is invalid: %{public}zu", info.argc);
976         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
977     }
978     int32_t x = 0;
979     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(engine, info.argv[0], x)) {
980         WLOGFE("Failed to convert parameter to x");
981         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
982     }
983     int32_t y = 0;
984     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(engine, info.argv[1], y)) {
985         WLOGFE("Failed to convert parameter to y");
986         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
987     }
988     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
989         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
990         return engine.CreateUndefined();
991     }
992 
993     wptr<Window> weakToken(windowToken_);
994     AsyncTask::CompleteCallback complete =
995         [weakToken, x, y](NativeEngine& engine, AsyncTask& task, int32_t status) {
996             auto weakWindow = weakToken.promote();
997             if (weakWindow == nullptr) {
998                 WLOGFE("window is nullptr");
999                 task.Reject(engine, CreateJsError(engine,
1000                     static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
1001                 return;
1002             }
1003             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->MoveTo(x, y));
1004             if (ret == WmErrorCode::WM_OK) {
1005                 task.Resolve(engine, engine.CreateUndefined());
1006             } else {
1007                 task.Reject(engine,
1008                     CreateJsError(engine, static_cast<int32_t>(ret),
1009                     "Window move failed"));
1010             }
1011             WLOGI("Window [%{public}u, %{public}s] move end, ret = %{public}d",
1012                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1013         };
1014 
1015     // 2: params num; 2: index of callback
1016     NativeValue* lastParam = (info.argc <= 2) ? nullptr :
1017         ((info.argv[2] != nullptr && info.argv[2]->TypeOf() == NATIVE_FUNCTION) ?
1018         info.argv[2] : nullptr);
1019     NativeValue* result = nullptr;
1020     AsyncTask::Schedule("JsWindow::OnMoveWindowTo",
1021         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1022     return result;
1023 }
1024 
OnResize(NativeEngine & engine,NativeCallbackInfo & info)1025 NativeValue* JsWindow::OnResize(NativeEngine& engine, NativeCallbackInfo& info)
1026 {
1027     WMError errCode = WMError::WM_OK;
1028     if (info.argc < 2 || info.argc > 3) { // 2: minimum param num, 3: maximum param num
1029         WLOGFE("Argc is invalid: %{public}zu", info.argc);
1030         errCode = WMError::WM_ERROR_INVALID_PARAM;
1031     }
1032     int32_t width = 0;
1033     if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[0], width)) {
1034         WLOGFE("Failed to convert parameter to width");
1035         errCode = WMError::WM_ERROR_INVALID_PARAM;
1036     }
1037     int32_t height = 0;
1038     if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[1], height)) {
1039         WLOGFE("Failed to convert parameter to height");
1040         errCode = WMError::WM_ERROR_INVALID_PARAM;
1041     }
1042     if (width <= 0 || height <= 0) {
1043         WLOGFE("width or height should greater than 0!");
1044         errCode = WMError::WM_ERROR_INVALID_PARAM;
1045     }
1046     wptr<Window> weakToken(windowToken_);
1047     AsyncTask::CompleteCallback complete =
1048         [weakToken, errCode, width, height](NativeEngine& engine, AsyncTask& task, int32_t status) {
1049             auto weakWindow = weakToken.promote();
1050             if (weakWindow == nullptr) {
1051                 WLOGFE("window is nullptr");
1052                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
1053                 return;
1054             }
1055             if (errCode != WMError::WM_OK) {
1056                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
1057                 WLOGFE("window is nullptr or get invalid param");
1058                 return;
1059             }
1060             WMError ret = weakWindow->Resize(static_cast<uint32_t>(width), static_cast<uint32_t>(height));
1061             if (ret == WMError::WM_OK) {
1062                 task.Resolve(engine, engine.CreateUndefined());
1063             } else {
1064                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window resize failed"));
1065             }
1066             WLOGFD("Window [%{public}u, %{public}s] resize end, ret = %{public}d",
1067                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1068         };
1069     // 2: params num; 2: index of callback
1070     NativeValue* lastParam = (info.argc <= 2) ? nullptr :
1071         (info.argv[2]->TypeOf() == NATIVE_FUNCTION ? info.argv[2] : nullptr);
1072     NativeValue* result = nullptr;
1073     AsyncTask::Schedule("JsWindow::OnResize",
1074         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1075     return result;
1076 }
1077 
OnResizeWindow(NativeEngine & engine,NativeCallbackInfo & info)1078 NativeValue* JsWindow::OnResizeWindow(NativeEngine& engine, NativeCallbackInfo& info)
1079 {
1080     WmErrorCode errCode = WmErrorCode::WM_OK;
1081     if (info.argc < 2) { // 2: minimum param num
1082         WLOGFE("Argc is invalid: %{public}zu", info.argc);
1083         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1084     }
1085     int32_t width = 0;
1086     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(engine, info.argv[0], width)) {
1087         WLOGFE("Failed to convert parameter to width");
1088         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1089     }
1090     int32_t height = 0;
1091     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(engine, info.argv[1], height)) {
1092         WLOGFE("Failed to convert parameter to height");
1093         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1094     }
1095     if (width <= 0 || height <= 0) {
1096         WLOGFE("width or height should greater than 0!");
1097         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1098     }
1099     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
1100         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
1101         return engine.CreateUndefined();
1102     }
1103 
1104     wptr<Window> weakToken(windowToken_);
1105     AsyncTask::CompleteCallback complete =
1106         [weakToken, width, height](NativeEngine& engine, AsyncTask& task, int32_t status) {
1107             auto weakWindow = weakToken.promote();
1108             if (weakWindow == nullptr) {
1109                 task.Reject(engine,
1110                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
1111                 return;
1112             }
1113             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(
1114                 weakWindow->Resize(static_cast<uint32_t>(width), static_cast<uint32_t>(height)));
1115             if (ret == WmErrorCode::WM_OK) {
1116                 task.Resolve(engine, engine.CreateUndefined());
1117             } else {
1118                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window resize failed"));
1119             }
1120             WLOGI("Window [%{public}u, %{public}s] resize end, ret = %{public}d",
1121                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1122         };
1123 
1124     // 2: params num; 2: index of callback
1125     NativeValue* lastParam = (info.argc <= 2) ? nullptr :
1126         ((info.argv[2] != nullptr && info.argv[2]->TypeOf() == NATIVE_FUNCTION) ?
1127         info.argv[2] : nullptr);
1128     NativeValue* result = nullptr;
1129     AsyncTask::Schedule("JsWindow::OnResizeWindow",
1130         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1131     return result;
1132 }
1133 
OnSetWindowType(NativeEngine & engine,NativeCallbackInfo & info)1134 NativeValue* JsWindow::OnSetWindowType(NativeEngine& engine, NativeCallbackInfo& info)
1135 {
1136     WMError errCode = WMError::WM_OK;
1137     if (info.argc < 1 || info.argc > 2) { // 2 is max num of argc
1138         WLOGFE("Argc is invalid: %{public}zu", info.argc);
1139         errCode = WMError::WM_ERROR_INVALID_PARAM;
1140     }
1141     WindowType winType = WindowType::SYSTEM_WINDOW_BASE;
1142     if (errCode == WMError::WM_OK) {
1143         NativeNumber* nativeType = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
1144         if (nativeType == nullptr) {
1145             WLOGFE("Failed to convert parameter to windowType");
1146             errCode = WMError::WM_ERROR_INVALID_PARAM;
1147         } else if (static_cast<uint32_t>(*nativeType) >= static_cast<uint32_t>(WindowType::SYSTEM_WINDOW_BASE)) {
1148             winType = static_cast<WindowType>(static_cast<uint32_t>(*nativeType)); // adapt to the old version
1149         } else {
1150             if (JS_TO_NATIVE_WINDOW_TYPE_MAP.count(
1151                 static_cast<ApiWindowType>(static_cast<uint32_t>(*nativeType))) != 0) {
1152                 winType = JS_TO_NATIVE_WINDOW_TYPE_MAP.at(
1153                     static_cast<ApiWindowType>(static_cast<uint32_t>(*nativeType)));
1154             } else {
1155                 WLOGFE("Do not surppot this type: %{public}u", static_cast<uint32_t>(*nativeType));
1156                 errCode = WMError::WM_ERROR_INVALID_PARAM;
1157             }
1158         }
1159     }
1160 
1161     wptr<Window> weakToken(windowToken_);
1162     AsyncTask::CompleteCallback complete =
1163         [weakToken, winType, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
1164             auto weakWindow = weakToken.promote();
1165             if (weakWindow == nullptr) {
1166                 WLOGFE("window is nullptr");
1167                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
1168                 return;
1169             }
1170             if (errCode != WMError::WM_OK) {
1171                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
1172                 WLOGFE("get invalid param");
1173                 return;
1174             }
1175             WMError ret = weakWindow->SetWindowType(winType);
1176             if (ret == WMError::WM_OK) {
1177                 task.Resolve(engine, engine.CreateUndefined());
1178             } else {
1179                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window set type failed"));
1180             }
1181             WLOGI("Window [%{public}u, %{public}s] set type end, ret = %{public}d",
1182                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1183         };
1184 
1185     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
1186         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
1187     NativeValue* result = nullptr;
1188     AsyncTask::Schedule("JsWindow::OnSetWindowType",
1189         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1190     return result;
1191 }
1192 
OnSetWindowMode(NativeEngine & engine,NativeCallbackInfo & info)1193 NativeValue* JsWindow::OnSetWindowMode(NativeEngine& engine, NativeCallbackInfo& info)
1194 {
1195     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
1196         WLOGFE("set window mode permission denied!");
1197         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP)));
1198         return engine.CreateUndefined();
1199     }
1200     WmErrorCode errCode = WmErrorCode::WM_OK;
1201     if (info.argc < 1) {
1202         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1203     }
1204     WindowMode winMode = WindowMode::WINDOW_MODE_FULLSCREEN;
1205     if (errCode == WmErrorCode::WM_OK) {
1206         NativeNumber* nativeMode = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
1207         if (nativeMode == nullptr) {
1208             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1209         } else {
1210             if (static_cast<uint32_t>(*nativeMode) >= static_cast<uint32_t>(WindowMode::WINDOW_MODE_SPLIT_PRIMARY)) {
1211                 winMode = static_cast<WindowMode>(static_cast<uint32_t>(*nativeMode));
1212             } else if (static_cast<uint32_t>(*nativeMode) >= static_cast<uint32_t>(ApiWindowMode::UNDEFINED) &&
1213                 static_cast<uint32_t>(*nativeMode) <= static_cast<uint32_t>(ApiWindowMode::MODE_END)) {
1214                 winMode = JS_TO_NATIVE_WINDOW_MODE_MAP.at(
1215                     static_cast<ApiWindowMode>(static_cast<uint32_t>(*nativeMode)));
1216             } else {
1217                 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1218             }
1219         }
1220     }
1221     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
1222         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
1223         return engine.CreateUndefined();
1224     }
1225     wptr<Window> weakToken(windowToken_);
1226     AsyncTask::CompleteCallback complete =
1227         [weakToken, winMode](NativeEngine& engine, AsyncTask& task, int32_t status) {
1228             auto weakWindow = weakToken.promote();
1229             if (weakWindow == nullptr) {
1230                 task.Reject(engine, CreateJsError(engine,
1231                     static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
1232                 return;
1233             }
1234             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetWindowMode(winMode));
1235             if (ret == WmErrorCode::WM_OK) {
1236                 task.Resolve(engine, engine.CreateUndefined());
1237             } else {
1238                 task.Reject(engine,
1239                     CreateJsError(engine, static_cast<int32_t>(ret), "Window set mode failed"));
1240             }
1241             WLOGI("Window [%{public}u, %{public}s] set type end, ret = %{public}d",
1242                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1243         };
1244 
1245     NativeValue* lastParam = (info.argc == 1) ? nullptr :
1246         ((info.argv[1] != nullptr && info.argv[1]->TypeOf() == NATIVE_FUNCTION) ?
1247         info.argv[1] : nullptr);
1248     NativeValue* result = nullptr;
1249     AsyncTask::Schedule("JsWindow::OnSetWindowMode",
1250         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1251     return result;
1252 }
1253 
OnGetProperties(NativeEngine & engine,NativeCallbackInfo & info)1254 NativeValue* JsWindow::OnGetProperties(NativeEngine& engine, NativeCallbackInfo& info)
1255 {
1256     WMError errCode = WMError::WM_OK;
1257     if (info.argc > 1) {
1258         WLOGFE("Argc is invalid: %{public}zu", info.argc);
1259         errCode = WMError::WM_ERROR_INVALID_PARAM;
1260     }
1261     wptr<Window> weakToken(windowToken_);
1262     AsyncTask::CompleteCallback complete =
1263         [weakToken, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
1264             auto weakWindow = weakToken.promote();
1265             if (weakWindow == nullptr) {
1266                 WLOGFE("window is nullptr");
1267                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
1268                 return;
1269             }
1270             if (errCode != WMError::WM_OK) {
1271                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
1272                 WLOGFE("window is nullptr or get invalid param");
1273                 return;
1274             }
1275             auto objValue = CreateJsWindowPropertiesObject(engine, weakWindow);
1276             if (objValue != nullptr) {
1277                 task.Resolve(engine, objValue);
1278             } else {
1279                 task.Reject(engine, CreateJsError(engine,
1280                     static_cast<int32_t>(WMError::WM_ERROR_NULLPTR), "Window get properties failed"));
1281             }
1282             WLOGFD("Window [%{public}u, %{public}s] get properties end",
1283                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
1284         };
1285 
1286     NativeValue* lastParam = (info.argc == 0) ? nullptr :
1287         (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
1288     NativeValue* result = nullptr;
1289     AsyncTask::Schedule("JsWindow::OnGetProperties",
1290         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1291     return result;
1292 }
1293 
OnGetWindowPropertiesSync(NativeEngine & engine,NativeCallbackInfo & info)1294 NativeValue* JsWindow::OnGetWindowPropertiesSync(NativeEngine& engine, NativeCallbackInfo& info)
1295 {
1296     wptr<Window> weakToken(windowToken_);
1297     auto window = weakToken.promote();
1298     if (window == nullptr) {
1299         WLOGFE("window is nullptr or get invalid param");
1300         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
1301         return engine.CreateUndefined();
1302     }
1303     auto objValue = CreateJsWindowPropertiesObject(engine, window);
1304     WLOGI("Window [%{public}u, %{public}s] get properties end",
1305         window->GetWindowId(), window->GetWindowName().c_str());
1306     if (objValue != nullptr) {
1307         return objValue;
1308     } else {
1309         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
1310         return engine.CreateUndefined();
1311     }
1312 }
1313 
OnRegisterWindowCallback(NativeEngine & engine,NativeCallbackInfo & info)1314 NativeValue* JsWindow::OnRegisterWindowCallback(NativeEngine& engine, NativeCallbackInfo& info)
1315 {
1316     if (windowToken_ == nullptr) {
1317         WLOGFE("Window is nullptr");
1318         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
1319         return engine.CreateUndefined();
1320     }
1321     if (info.argc < 2) { // 2: params num
1322         WLOGFE("Argc is invalid: %{public}zu", info.argc);
1323         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
1324         return engine.CreateUndefined();
1325     }
1326     std::string cbType;
1327     if (!ConvertFromJsValue(engine, info.argv[0], cbType)) {
1328         WLOGFE("Failed to convert parameter to callbackType");
1329         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
1330         return engine.CreateUndefined();
1331     }
1332     NativeValue* value = info.argv[1];
1333     if (!value->IsCallable()) {
1334         WLOGI("Callback(info->argv[1]) is not callable");
1335         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
1336         return engine.CreateUndefined();
1337     }
1338     WmErrorCode ret = registerManager_->RegisterListener(windowToken_, cbType, CaseType::CASE_WINDOW, engine, value);
1339     if (ret != WmErrorCode::WM_OK) {
1340         engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
1341         return engine.CreateUndefined();
1342     }
1343     WLOGI("Register end, window [%{public}u, %{public}s], type = %{public}s",
1344         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), cbType.c_str());
1345     return engine.CreateUndefined();
1346 }
1347 
OnUnregisterWindowCallback(NativeEngine & engine,NativeCallbackInfo & info)1348 NativeValue* JsWindow::OnUnregisterWindowCallback(NativeEngine& engine, NativeCallbackInfo& info)
1349 {
1350     if (windowToken_ == nullptr) {
1351         WLOGFE("Window is nullptr");
1352         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
1353         return engine.CreateUndefined();
1354     }
1355     if (info.argc < 1) { // 2: maximum params nums
1356         WLOGFE("Argc is invalid: %{public}zu", info.argc);
1357         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
1358         return engine.CreateUndefined();
1359     }
1360     std::string cbType;
1361     if (!ConvertFromJsValue(engine, info.argv[0], cbType)) {
1362         WLOGFE("Failed to convert parameter to callbackType");
1363         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
1364         return engine.CreateUndefined();
1365     }
1366 
1367     NativeValue* value = nullptr;
1368     WmErrorCode ret = WmErrorCode::WM_OK;
1369     if (info.argc == 1) {
1370         ret = registerManager_->UnregisterListener(windowToken_, cbType, CaseType::CASE_WINDOW, value);
1371     } else {
1372         value = info.argv[1];
1373         if (value == nullptr || !value->IsCallable()) {
1374             ret = registerManager_->UnregisterListener(windowToken_, cbType, CaseType::CASE_WINDOW, nullptr);
1375         } else {
1376             ret = registerManager_->UnregisterListener(windowToken_, cbType, CaseType::CASE_WINDOW, value);
1377         }
1378     }
1379 
1380     if (ret != WmErrorCode::WM_OK) {
1381         engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
1382         return engine.CreateUndefined();
1383     }
1384     WLOGI("Unregister end, window [%{public}u, %{public}s], type = %{public}s",
1385         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), cbType.c_str());
1386     return engine.CreateUndefined();
1387 }
1388 
OnBindDialogTarget(NativeEngine & engine,NativeCallbackInfo & info)1389 NativeValue* JsWindow::OnBindDialogTarget(NativeEngine& engine, NativeCallbackInfo& info)
1390 {
1391     WmErrorCode errCode = WmErrorCode::WM_OK;
1392     errCode = (windowToken_ == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : WmErrorCode::WM_OK;
1393 
1394     sptr<IRemoteObject> token = nullptr;
1395     if (info.argc > 1) {
1396         token = NAPI_ohos_rpc_getNativeRemoteObject(
1397             reinterpret_cast<napi_env>(&engine), reinterpret_cast<napi_value>(info.argv[0]));
1398         if (token == nullptr) {
1399             std::shared_ptr<AbilityRuntime::RequestInfo> requestInfo =
1400                 AbilityRuntime::RequestInfo::UnwrapRequestInfo(engine, info.argv[0]);
1401             token = (requestInfo != nullptr) ? requestInfo->GetToken() : nullptr;
1402         }
1403     }
1404 
1405     if ((info.argc <= 1) || (token == nullptr)) { // 1: invalid params nums
1406         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
1407         return engine.CreateUndefined();
1408     }
1409 
1410     NativeValue* value = info.argv[1];
1411     if (value == nullptr || !value->IsCallable()) {
1412         registerManager_->RegisterListener(windowToken_,
1413             "dialogDeathRecipient", CaseType::CASE_WINDOW, engine, nullptr);
1414     } else {
1415         registerManager_->RegisterListener(windowToken_, "dialogDeathRecipient", CaseType::CASE_WINDOW, engine, value);
1416     }
1417 
1418     wptr<Window> weakToken(windowToken_);
1419     AsyncTask::CompleteCallback complete =
1420         [weakToken, token, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) mutable {
1421             auto weakWindow = weakToken.promote();
1422             errCode = (weakWindow == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : errCode;
1423             if (errCode != WmErrorCode::WM_OK) {
1424                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
1425                 return;
1426             }
1427 
1428             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->BindDialogTarget(token));
1429             if (ret == WmErrorCode::WM_OK) {
1430                 task.Resolve(engine, engine.CreateUndefined());
1431             } else {
1432                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Bind Dialog Target failed"));
1433             }
1434 
1435             WLOGI("BindDialogTarget end, window [%{public}u, %{public}s]",
1436                 weakToken->GetWindowId(), weakToken->GetWindowName().c_str());
1437     };
1438 
1439     NativeValue* result = nullptr;
1440     NativeValue* lastParam = (info.argc == 2) ? nullptr :
1441         (info.argv[2]->TypeOf() == NATIVE_FUNCTION ? info.argv[2] : nullptr);
1442     AsyncTask::Schedule("JsWindow::OnBindDialogTarget",
1443         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1444     return result;
1445 }
1446 
LoadContentTask(std::shared_ptr<NativeReference> contentStorage,std::string contextUrl,sptr<Window> weakWindow,NativeEngine & engine,AsyncTask & task)1447 static void LoadContentTask(std::shared_ptr<NativeReference> contentStorage, std::string contextUrl,
1448     sptr<Window> weakWindow, NativeEngine& engine, AsyncTask& task)
1449 {
1450     NativeValue* nativeStorage =  (contentStorage == nullptr) ? nullptr : contentStorage->Get();
1451     AppExecFwk::Ability* ability = nullptr;
1452     GetAPI7Ability(engine, ability);
1453     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(
1454         weakWindow->SetUIContent(contextUrl, &engine, nativeStorage, false, ability));
1455     if (ret == WmErrorCode::WM_OK) {
1456         task.Resolve(engine, engine.CreateUndefined());
1457     } else {
1458         task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window load content failed"));
1459     }
1460     WLOGFD("Window [%{public}u, %{public}s] load content end, ret = %{public}d",
1461         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1462     return;
1463 }
1464 
LoadContentScheduleOld(NativeEngine & engine,NativeCallbackInfo & info)1465 NativeValue* JsWindow::LoadContentScheduleOld(NativeEngine& engine, NativeCallbackInfo& info)
1466 {
1467     WMError errCode = WMError::WM_OK;
1468     if (info.argc < 1 || info.argc > 2) { // 2 maximum param num
1469         WLOGFE("Argc is invalid: %{public}zu", info.argc);
1470         errCode = WMError::WM_ERROR_INVALID_PARAM;
1471     }
1472     std::string contextUrl;
1473     if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[0], contextUrl)) {
1474         WLOGFE("Failed to convert parameter to context url");
1475         errCode = WMError::WM_ERROR_INVALID_PARAM;
1476     }
1477     NativeValue* storage = nullptr;
1478     NativeValue* callBack = nullptr;
1479     if (info.argc == 2) { // 2 param num
1480         callBack = info.argv[1];
1481     }
1482     std::shared_ptr<NativeReference> contentStorage = (storage == nullptr) ? nullptr :
1483         std::shared_ptr<NativeReference>(engine.CreateReference(storage, 1));
1484     wptr<Window> weakToken(windowToken_);
1485     AsyncTask::CompleteCallback complete =
1486         [weakToken, contentStorage, contextUrl, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
1487             auto weakWindow = weakToken.promote();
1488             if (weakWindow == nullptr) {
1489                 WLOGFE("window is nullptr");
1490                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
1491                 return;
1492             }
1493             if (errCode != WMError::WM_OK) {
1494                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
1495                 WLOGFE("Window is nullptr or get invalid param");
1496                 return;
1497             }
1498             LoadContentTask(contentStorage, contextUrl, weakWindow, engine, task);
1499         };
1500     NativeValue* result = nullptr;
1501     AsyncTask::Schedule("JsWindow::OnLoadContent",
1502         engine, CreateAsyncTaskWithLastParam(engine, callBack, nullptr, std::move(complete), &result));
1503     return result;
1504 }
1505 
LoadContentScheduleNew(NativeEngine & engine,NativeCallbackInfo & info)1506 NativeValue* JsWindow::LoadContentScheduleNew(NativeEngine& engine, NativeCallbackInfo& info)
1507 {
1508     WmErrorCode errCode = WmErrorCode::WM_OK;
1509     if (info.argc < 2) { // 2 param num
1510         WLOGFE("Argc is invalid: %{public}zu", info.argc);
1511         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1512     }
1513     std::string contextUrl;
1514     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(engine, info.argv[0], contextUrl)) {
1515         WLOGFE("Failed to convert parameter to context url");
1516         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1517     }
1518     NativeValue* storage = nullptr;
1519     NativeValue* callBack = nullptr;
1520     if (info.argc == 2) { // 2: num of params
1521         storage = info.argv[1];
1522     } else if (info.argc >= 3) { // 3: num of params
1523         storage = info.argv[1];
1524         callBack = ((info.argv[2] != nullptr && info.argv[2]->TypeOf() == NATIVE_FUNCTION) ? // 2 param num
1525             info.argv[2] : nullptr); // 2 param num
1526     }
1527     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
1528         WLOGFE("Window is nullptr or get invalid param");
1529         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
1530         return engine.CreateUndefined();
1531     }
1532     std::shared_ptr<NativeReference> contentStorage = (storage == nullptr) ? nullptr :
1533         std::shared_ptr<NativeReference>(engine.CreateReference(storage, 1));
1534     wptr<Window> weakToken(windowToken_);
1535     AsyncTask::CompleteCallback complete =
1536         [weakToken, contentStorage, contextUrl](NativeEngine& engine, AsyncTask& task, int32_t status) {
1537             auto weakWindow = weakToken.promote();
1538             if (weakWindow == nullptr) {
1539                 WLOGFE("Window is nullptr or get invalid param");
1540                 task.Reject(engine, CreateJsError(engine,
1541                     static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
1542                 return;
1543             }
1544             LoadContentTask(contentStorage, contextUrl, weakWindow, engine, task);
1545         };
1546     NativeValue* result = nullptr;
1547     AsyncTask::Schedule("JsWindow::OnLoadContent",
1548         engine, CreateAsyncTaskWithLastParam(engine, callBack, nullptr, std::move(complete), &result));
1549     return result;
1550 }
1551 
OnLoadContent(NativeEngine & engine,NativeCallbackInfo & info)1552 NativeValue* JsWindow::OnLoadContent(NativeEngine& engine, NativeCallbackInfo& info)
1553 {
1554     bool oldApi = false;
1555     if (info.argc == 1) {
1556         oldApi = true;
1557     } else if (info.argc == 2) { // 2 param num
1558         NativeValue* value = info.argv[1];
1559         if (value== nullptr || value->TypeOf() != NATIVE_FUNCTION) {
1560             oldApi = false;
1561         } else {
1562             oldApi = true;
1563         }
1564     }
1565     if (oldApi) {
1566         return LoadContentScheduleOld(engine, info);
1567     } else {
1568         return LoadContentScheduleNew(engine, info);
1569     }
1570 }
1571 
OnGetUIContext(NativeEngine & engine,NativeCallbackInfo & info)1572 NativeValue* JsWindow::OnGetUIContext(NativeEngine& engine, NativeCallbackInfo& info)
1573 {
1574     if (info.argc >= 1) {
1575         WLOGFE("Argc is invalid: %{public}zu, expect zero params", info.argc);
1576         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
1577         return engine.CreateUndefined();
1578     }
1579 
1580     wptr<Window> weakToken(windowToken_);
1581     auto window = weakToken.promote();
1582     if (window == nullptr) {
1583         WLOGFE("window is nullptr");
1584         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
1585         return engine.CreateUndefined();
1586     }
1587 
1588     auto uicontent = window->GetUIContent();
1589     if (uicontent == nullptr) {
1590         WLOGFE("uicontent is nullptr");
1591         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
1592         return engine.CreateUndefined();
1593     }
1594 
1595     NativeValue* uiContext = uicontent->GetUIContext();
1596     if (uiContext == nullptr) {
1597         WLOGFE("uiContext obtained from jsEngine is nullptr");
1598         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
1599         return engine.CreateUndefined();
1600     } else {
1601         return uiContext;
1602     }
1603 }
1604 
OnSetUIContent(NativeEngine & engine,NativeCallbackInfo & info)1605 NativeValue* JsWindow::OnSetUIContent(NativeEngine& engine, NativeCallbackInfo& info)
1606 {
1607     WmErrorCode errCode = WmErrorCode::WM_OK;
1608     if (info.argc < 1) { // 2 maximum param num
1609         WLOGFE("Argc is invalid: %{public}zu", info.argc);
1610         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1611     }
1612     std::string contextUrl;
1613     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(engine, info.argv[0], contextUrl)) {
1614         WLOGFE("Failed to convert parameter to context url");
1615         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1616     }
1617     NativeValue* storage = nullptr;
1618     NativeValue* callBack = nullptr;
1619     if (info.argc >= 2) { // 2 param num
1620         callBack = info.argv[1];
1621     }
1622     std::shared_ptr<NativeReference> contentStorage = (storage == nullptr) ? nullptr :
1623         std::shared_ptr<NativeReference>(engine.CreateReference(storage, 1));
1624     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
1625         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
1626         return engine.CreateUndefined();
1627     }
1628 
1629     wptr<Window> weakToken(windowToken_);
1630     AsyncTask::CompleteCallback complete =
1631         [weakToken, contentStorage, contextUrl](NativeEngine& engine, AsyncTask& task, int32_t status) {
1632             auto weakWindow = weakToken.promote();
1633             if (weakWindow == nullptr) {
1634                 WLOGFE("Window is nullptr");
1635                 task.Reject(engine,
1636                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
1637                 return;
1638             }
1639             LoadContentTask(contentStorage, contextUrl, weakWindow, engine, task);
1640         };
1641     NativeValue* result = nullptr;
1642     AsyncTask::Schedule("JsWindow::OnSetUIContent",
1643         engine, CreateAsyncTaskWithLastParam(engine, callBack, nullptr, std::move(complete), &result));
1644     return result;
1645 }
1646 
OnSetFullScreen(NativeEngine & engine,NativeCallbackInfo & info)1647 NativeValue* JsWindow::OnSetFullScreen(NativeEngine& engine, NativeCallbackInfo& info)
1648 {
1649     WMError errCode = WMError::WM_OK;
1650     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
1651         WLOGFE("Argc is invalid: %{public}zu", info.argc);
1652         errCode = WMError::WM_ERROR_INVALID_PARAM;
1653     }
1654     bool isFullScreen = false;
1655     if (errCode == WMError::WM_OK) {
1656         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
1657         if (nativeVal == nullptr) {
1658             WLOGFE("Failed to convert parameter to isFullScreen");
1659             errCode = WMError::WM_ERROR_INVALID_PARAM;
1660         } else {
1661             isFullScreen = static_cast<bool>(*nativeVal);
1662         }
1663     }
1664 
1665     wptr<Window> weakToken(windowToken_);
1666     AsyncTask::CompleteCallback complete =
1667         [weakToken, isFullScreen, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
1668             auto weakWindow = weakToken.promote();
1669             if (weakWindow == nullptr) {
1670                 WLOGFE("window is nullptr");
1671                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
1672                 return;
1673             }
1674             if (errCode != WMError::WM_OK) {
1675                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
1676                 return;
1677             }
1678             WMError ret = weakWindow->SetFullScreen(isFullScreen);
1679             if (ret == WMError::WM_OK) {
1680                 task.Resolve(engine, engine.CreateUndefined());
1681             } else {
1682                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window SetFullScreen failed."));
1683             }
1684             WLOGI("Window [%{public}u, %{public}s] set full screen end, ret = %{public}d",
1685                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1686         };
1687 
1688     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
1689         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
1690     NativeValue* result = nullptr;
1691     AsyncTask::Schedule("JsWindow::OnSetFullScreen",
1692         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1693     return result;
1694 }
1695 
OnSetLayoutFullScreen(NativeEngine & engine,NativeCallbackInfo & info)1696 NativeValue* JsWindow::OnSetLayoutFullScreen(NativeEngine& engine, NativeCallbackInfo& info)
1697 {
1698     WMError errCode = WMError::WM_OK;
1699     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
1700         WLOGFE("Argc is invalid: %{public}zu", info.argc);
1701         errCode = WMError::WM_ERROR_INVALID_PARAM;
1702     }
1703     bool isLayoutFullScreen = false;
1704     if (errCode == WMError::WM_OK) {
1705         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
1706         if (nativeVal == nullptr) {
1707             WLOGFE("Failed to convert parameter to isLayoutFullScreen");
1708             errCode = WMError::WM_ERROR_INVALID_PARAM;
1709         } else {
1710             isLayoutFullScreen = static_cast<bool>(*nativeVal);
1711         }
1712     }
1713     wptr<Window> weakToken(windowToken_);
1714     AsyncTask::CompleteCallback complete =
1715         [weakToken, isLayoutFullScreen, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
1716             auto weakWindow = weakToken.promote();
1717             if (weakWindow == nullptr) {
1718                 WLOGFE("window is nullptr");
1719                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
1720                 return;
1721             }
1722             if (errCode != WMError::WM_OK) {
1723                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
1724                 return;
1725             }
1726             WMError ret = weakWindow->SetLayoutFullScreen(isLayoutFullScreen);
1727             if (ret == WMError::WM_OK) {
1728                 task.Resolve(engine, engine.CreateUndefined());
1729             } else {
1730                 task.Reject(engine, CreateJsError(engine,
1731                     static_cast<int32_t>(ret), "Window OnSetLayoutFullScreen failed."));
1732             }
1733             WLOGI("Window [%{public}u, %{public}s] set layout full screen end, ret = %{public}d",
1734                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1735         };
1736     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
1737         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
1738     NativeValue* result = nullptr;
1739     AsyncTask::Schedule("JsWindow::OnSetLayoutFullScreen",
1740         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1741     return result;
1742 }
1743 
OnSetWindowLayoutFullScreen(NativeEngine & engine,NativeCallbackInfo & info)1744 NativeValue* JsWindow::OnSetWindowLayoutFullScreen(NativeEngine& engine, NativeCallbackInfo& info)
1745 {
1746     WmErrorCode errCode = WmErrorCode::WM_OK;
1747     if (info.argc < 1) { // 1: params num
1748         WLOGFE("Argc is invalid: %{public}zu", info.argc);
1749         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1750     }
1751     bool isLayoutFullScreen = false;
1752     if (errCode == WmErrorCode::WM_OK) {
1753         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
1754         if (nativeVal == nullptr) {
1755             WLOGFE("Failed to convert parameter to isLayoutFullScreen");
1756             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1757         } else {
1758             isLayoutFullScreen = static_cast<bool>(*nativeVal);
1759         }
1760     }
1761     if (errCode != WmErrorCode::WM_OK) {
1762         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
1763         return engine.CreateUndefined();
1764     }
1765 
1766     wptr<Window> weakToken(windowToken_);
1767     AsyncTask::CompleteCallback complete =
1768         [weakToken, isLayoutFullScreen](NativeEngine& engine, AsyncTask& task, int32_t status) {
1769             auto weakWindow = weakToken.promote();
1770             if (weakWindow == nullptr) {
1771                 task.Reject(engine,
1772                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY),
1773                     "Invalidate params."));
1774                 return;
1775             }
1776             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetLayoutFullScreen(isLayoutFullScreen));
1777             if (ret == WmErrorCode::WM_OK) {
1778                 task.Resolve(engine, engine.CreateUndefined());
1779             } else {
1780                 task.Reject(engine, CreateJsError(engine,
1781                     static_cast<int32_t>(ret), "Window OnSetLayoutFullScreen failed."));
1782             }
1783             WLOGI("Window [%{public}u, %{public}s] set layout full screen end, ret = %{public}d",
1784                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1785         };
1786 
1787     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
1788         ((info.argv[1] != nullptr && info.argv[1]->TypeOf() == NATIVE_FUNCTION) ?
1789         info.argv[1] : nullptr);
1790     NativeValue* result = nullptr;
1791     AsyncTask::Schedule("JsWindow::OnSetWindowLayoutFullScreen",
1792         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1793     return result;
1794 }
1795 
OnSetSystemBarEnable(NativeEngine & engine,NativeCallbackInfo & info)1796 NativeValue* JsWindow::OnSetSystemBarEnable(NativeEngine& engine, NativeCallbackInfo& info)
1797 {
1798     WMError errCode = WMError::WM_OK;
1799     errCode = (windowToken_ == nullptr) ? WMError::WM_ERROR_NULLPTR : WMError::WM_OK;
1800     if (info.argc > 2) { // 2: maximum params num
1801         WLOGFE("Argc is invalid: %{public}zu", info.argc);
1802         errCode = WMError::WM_ERROR_INVALID_PARAM;
1803     }
1804     std::map<WindowType, SystemBarProperty> systemBarProperties;
1805     std::map<WindowType, SystemBarPropertyFlag> systemBarPropertyFlags;
1806     if (errCode == WMError::WM_OK && !GetSystemBarStatus(systemBarProperties, systemBarPropertyFlags,
1807         engine, info, windowToken_)) {
1808         errCode = WMError::WM_ERROR_INVALID_PARAM;
1809     }
1810     wptr<Window> weakToken(windowToken_);
1811     AsyncTask::CompleteCallback complete = [weakToken, systemBarProperties, systemBarPropertyFlags, errCode]
1812         (NativeEngine& engine, AsyncTask& task, int32_t status) mutable {
1813             auto weakWindow = weakToken.promote();
1814             if (weakWindow == nullptr) {
1815                 errCode = WMError::WM_ERROR_NULLPTR;
1816             }
1817             if (errCode != WMError::WM_OK) {
1818                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
1819                 return;
1820             }
1821             UpdateSystemBarProperties(systemBarProperties, systemBarPropertyFlags, weakToken);
1822             WMError ret = weakWindow->SetSystemBarProperty(WindowType::WINDOW_TYPE_STATUS_BAR,
1823                 systemBarProperties.at(WindowType::WINDOW_TYPE_STATUS_BAR));
1824             ret = weakWindow->SetSystemBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_BAR,
1825                 systemBarProperties.at(WindowType::WINDOW_TYPE_NAVIGATION_BAR));
1826             if (ret == WMError::WM_OK) {
1827                 task.Resolve(engine, engine.CreateUndefined());
1828             } else {
1829                 task.Reject(engine, CreateJsError(engine,
1830                     static_cast<int32_t>(ret), "JsWindow::OnSetSystemBarEnable failed"));
1831             }
1832             WLOGI("Window [%{public}u, %{public}s] set system bar enable end, ret = %{public}d",
1833                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1834         };
1835     NativeValue* lastParam = nullptr;
1836     if (info.argc > 0 && info.argv[0]->TypeOf() == NATIVE_FUNCTION) {
1837         lastParam = info.argv[0];
1838     } else if (info.argc > 1 && info.argv[1]->TypeOf() == NATIVE_FUNCTION) {
1839         lastParam = info.argv[1];
1840     }
1841     NativeValue* result = nullptr;
1842     AsyncTask::Schedule("JsWindow::OnSetSystemBarEnable",
1843         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1844     return result;
1845 }
1846 
OnSetWindowSystemBarEnable(NativeEngine & engine,NativeCallbackInfo & info)1847 NativeValue* JsWindow::OnSetWindowSystemBarEnable(NativeEngine& engine, NativeCallbackInfo& info)
1848 {
1849     WmErrorCode errCode = WmErrorCode::WM_OK;
1850     std::map<WindowType, SystemBarProperty> systemBarProperties;
1851     std::map<WindowType, SystemBarPropertyFlag> systemBarPropertyFlags;
1852     errCode = (windowToken_ == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : WmErrorCode::WM_OK;
1853     if (errCode == WmErrorCode::WM_OK && (info.argc < 1 || // 1: params num
1854         !GetSystemBarStatus(systemBarProperties, systemBarPropertyFlags, engine, info, windowToken_))) {
1855         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
1856         return engine.CreateUndefined();
1857     }
1858     wptr<Window> weakToken(windowToken_);
1859     AsyncTask::CompleteCallback complete = [weakToken, systemBarProperties, systemBarPropertyFlags, errCode]
1860         (NativeEngine& engine, AsyncTask& task, int32_t status) mutable {
1861             auto weakWindow = weakToken.promote();
1862             errCode = (weakWindow == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : errCode;
1863             if (errCode != WmErrorCode::WM_OK) {
1864                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
1865                 return;
1866             }
1867             UpdateSystemBarProperties(systemBarProperties, systemBarPropertyFlags, weakToken);
1868             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetSystemBarProperty(
1869                 WindowType::WINDOW_TYPE_STATUS_BAR, systemBarProperties.at(WindowType::WINDOW_TYPE_STATUS_BAR)));
1870             if (ret != WmErrorCode::WM_OK) {
1871                 task.Reject(engine, CreateJsError(engine,
1872                     static_cast<int32_t>(ret), "JsWindow::OnSetWindowSystemBarEnable failed"));
1873             }
1874             ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetSystemBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_BAR,
1875                 systemBarProperties.at(WindowType::WINDOW_TYPE_NAVIGATION_BAR)));
1876             if (ret == WmErrorCode::WM_OK) {
1877                 task.Resolve(engine, engine.CreateUndefined());
1878             } else {
1879                 task.Reject(engine, CreateJsError(engine,
1880                     static_cast<int32_t>(ret), "JsWindow::OnSetWindowSystemBarEnable failed"));
1881             }
1882         };
1883     NativeValue* lastParam = nullptr;
1884     if (info.argc >= 1 && info.argv[0] != nullptr && info.argv[0]->TypeOf() == NATIVE_FUNCTION) {
1885         lastParam = info.argv[0];
1886     } else if (info.argc >= 2 && // 2 arg count
1887         info.argv[1] != nullptr && info.argv[1]->TypeOf() == NATIVE_FUNCTION) {
1888         lastParam = info.argv[1];
1889     }
1890     NativeValue* result = nullptr;
1891     AsyncTask::Schedule("JsWindow::OnSetWindowSystemBarEnable",
1892         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1893     return result;
1894 }
1895 
OnSetSystemBarProperties(NativeEngine & engine,NativeCallbackInfo & info)1896 NativeValue* JsWindow::OnSetSystemBarProperties(NativeEngine& engine, NativeCallbackInfo& info)
1897 {
1898     WMError errCode = WMError::WM_OK;
1899     if (windowToken_ == nullptr) {
1900         errCode = WMError::WM_ERROR_NULLPTR;
1901     }
1902     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
1903         WLOGFE("Argc is invalid: %{public}zu", info.argc);
1904         errCode = WMError::WM_ERROR_INVALID_PARAM;
1905     }
1906     std::map<WindowType, SystemBarProperty> systemBarProperties;
1907     std::map<WindowType, SystemBarPropertyFlag> systemBarPropertyFlags;
1908     if (errCode == WMError::WM_OK) {
1909         NativeObject* nativeObj = ConvertNativeValueTo<NativeObject>(info.argv[0]);
1910         if (nativeObj == nullptr || !SetSystemBarPropertiesFromJs(engine, nativeObj,
1911             systemBarProperties, systemBarPropertyFlags, windowToken_)) {
1912             errCode = WMError::WM_ERROR_INVALID_PARAM;
1913         }
1914     }
1915     wptr<Window> weakToken(windowToken_);
1916     AsyncTask::CompleteCallback complete = [weakToken, systemBarProperties, systemBarPropertyFlags, errCode]
1917         (NativeEngine& engine, AsyncTask& task, int32_t status) mutable {
1918             auto weakWindow = weakToken.promote();
1919             if (weakWindow == nullptr) {
1920                 errCode = WMError::WM_ERROR_NULLPTR;
1921             }
1922             if (errCode != WMError::WM_OK) {
1923                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
1924                 return;
1925             }
1926             UpdateSystemBarProperties(systemBarProperties, systemBarPropertyFlags, weakToken);
1927             WMError ret = weakWindow->SetSystemBarProperty(WindowType::WINDOW_TYPE_STATUS_BAR,
1928                 systemBarProperties.at(WindowType::WINDOW_TYPE_STATUS_BAR));
1929             ret = weakWindow->SetSystemBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_BAR,
1930                 systemBarProperties.at(WindowType::WINDOW_TYPE_NAVIGATION_BAR));
1931             if (ret == WMError::WM_OK) {
1932                 task.Resolve(engine, engine.CreateUndefined());
1933             } else {
1934                 task.Reject(engine, CreateJsError(engine,
1935                     static_cast<int32_t>(WMError::WM_ERROR_NULLPTR), "JsWindow::OnSetSystemBarProperties failed"));
1936             }
1937             WLOGI("Window [%{public}u, %{public}s] set prop end, ret = %{public}d",
1938                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
1939         };
1940 
1941     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
1942         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
1943     NativeValue* result = nullptr;
1944     AsyncTask::Schedule("JsWindow::OnSetSystemBarProperties",
1945         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1946     return result;
1947 }
1948 
OnSetWindowSystemBarProperties(NativeEngine & engine,NativeCallbackInfo & info)1949 NativeValue* JsWindow::OnSetWindowSystemBarProperties(NativeEngine& engine, NativeCallbackInfo& info)
1950 {
1951     WmErrorCode errCode = WmErrorCode::WM_OK;
1952     errCode = (windowToken_ == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : WmErrorCode::WM_OK;
1953     if (info.argc < 1) { // 2: maximum params num
1954         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1955     }
1956     std::map<WindowType, SystemBarProperty> systemBarProperties;
1957     std::map<WindowType, SystemBarPropertyFlag> systemBarPropertyFlags;
1958     if (errCode == WmErrorCode::WM_OK) {
1959         NativeObject* nativeObj = ConvertNativeValueTo<NativeObject>(info.argv[0]);
1960         if (nativeObj == nullptr || !SetSystemBarPropertiesFromJs(engine, nativeObj,
1961             systemBarProperties, systemBarPropertyFlags, windowToken_)) {
1962             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1963         }
1964     }
1965     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
1966         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
1967         return engine.CreateUndefined();
1968     }
1969 
1970     wptr<Window> weakToken(windowToken_);
1971     AsyncTask::CompleteCallback complete = [weakToken, systemBarProperties, systemBarPropertyFlags, errCode]
1972         (NativeEngine& engine, AsyncTask& task, int32_t status) mutable {
1973             auto weakWindow = weakToken.promote();
1974             errCode = (weakWindow == nullptr) ? WmErrorCode::WM_ERROR_STATE_ABNORMALLY : errCode;
1975             if (errCode != WmErrorCode::WM_OK) {
1976                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
1977                 return;
1978             }
1979             UpdateSystemBarProperties(systemBarProperties, systemBarPropertyFlags, weakToken);
1980             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetSystemBarProperty(
1981                 WindowType::WINDOW_TYPE_STATUS_BAR, systemBarProperties.at(WindowType::WINDOW_TYPE_STATUS_BAR)));
1982             if (ret != WmErrorCode::WM_OK) {
1983                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret)));
1984             }
1985             ret = WM_JS_TO_ERROR_CODE_MAP.at(
1986                 weakWindow->SetSystemBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_BAR,
1987                 systemBarProperties.at(WindowType::WINDOW_TYPE_NAVIGATION_BAR)));
1988             if (ret != WmErrorCode::WM_OK) {
1989                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret)));
1990             }
1991             task.Resolve(engine, engine.CreateUndefined());
1992         };
1993 
1994     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
1995         ((info.argv[1] != nullptr && info.argv[1]->TypeOf() == NATIVE_FUNCTION) ? info.argv[1] : nullptr);
1996     NativeValue* result = nullptr;
1997     AsyncTask::Schedule("JsWindow::OnSetWindowSystemBarProperties",
1998         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1999     return result;
2000 }
2001 
ParseAvoidAreaParam(NativeCallbackInfo & info,WMError & errCode,AvoidAreaType & avoidAreaType)2002 static void ParseAvoidAreaParam(NativeCallbackInfo& info, WMError& errCode, AvoidAreaType& avoidAreaType)
2003 {
2004     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
2005         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2006         errCode = WMError::WM_ERROR_INVALID_PARAM;
2007     }
2008     if (errCode == WMError::WM_OK) {
2009         NativeNumber* nativeMode = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
2010         if (nativeMode == nullptr) {
2011             WLOGFE("Failed to convert parameter to AvoidAreaType");
2012             errCode = WMError::WM_ERROR_INVALID_PARAM;
2013         } else {
2014             avoidAreaType = static_cast<AvoidAreaType>(static_cast<uint32_t>(*nativeMode));
2015             errCode = ((avoidAreaType > AvoidAreaType::TYPE_KEYBOARD) ||
2016                 (avoidAreaType < AvoidAreaType::TYPE_SYSTEM)) ? WMError::WM_ERROR_INVALID_PARAM : WMError::WM_OK;
2017         }
2018     }
2019 }
2020 
OnGetAvoidArea(NativeEngine & engine,NativeCallbackInfo & info)2021 NativeValue* JsWindow::OnGetAvoidArea(NativeEngine& engine, NativeCallbackInfo& info)
2022 {
2023     WMError errCode = WMError::WM_OK;
2024     AvoidAreaType avoidAreaType = AvoidAreaType::TYPE_SYSTEM;
2025     ParseAvoidAreaParam(info, errCode, avoidAreaType);
2026     wptr<Window> weakToken(windowToken_);
2027     AsyncTask::CompleteCallback complete =
2028         [weakToken, errCode, avoidAreaType](NativeEngine& engine, AsyncTask& task, int32_t status) {
2029             auto weakWindow = weakToken.promote();
2030             if (weakWindow == nullptr) {
2031                 WLOGFE("window is nullptr");
2032                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
2033                 return;
2034             }
2035             if (errCode != WMError::WM_OK) {
2036                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
2037                 WLOGFE("window is nullptr or get invalid param");
2038                 return;
2039             }
2040             // getAvoidRect by avoidAreaType
2041             AvoidArea avoidArea;
2042             WMError ret = weakWindow->GetAvoidAreaByType(avoidAreaType, avoidArea);
2043             if (ret != WMError::WM_OK) {
2044                 avoidArea.topRect_ = g_emptyRect;
2045                 avoidArea.leftRect_ = g_emptyRect;
2046                 avoidArea.rightRect_ = g_emptyRect;
2047                 avoidArea.bottomRect_ = g_emptyRect;
2048             }
2049             NativeValue* avoidAreaObj = ConvertAvoidAreaToJsValue(engine, avoidArea, avoidAreaType);
2050             if (avoidAreaObj != nullptr) {
2051                 task.Resolve(engine, avoidAreaObj);
2052             } else {
2053                 task.Reject(engine, CreateJsError(engine,
2054                     static_cast<int32_t>(WMError::WM_ERROR_NULLPTR), "JsWindow::OnGetAvoidArea failed"));
2055             }
2056             WLOGI("Window [%{public}u, %{public}s] get avoid area end, ret = %{public}d",
2057                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
2058         };
2059     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
2060         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
2061     NativeValue* result = nullptr;
2062     AsyncTask::Schedule("JsWindow::OnGetAvoidArea",
2063         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2064     return result;
2065 }
2066 
OnGetWindowAvoidAreaSync(NativeEngine & engine,NativeCallbackInfo & info)2067 NativeValue* JsWindow::OnGetWindowAvoidAreaSync(NativeEngine& engine, NativeCallbackInfo& info)
2068 {
2069     WmErrorCode errCode = WmErrorCode::WM_OK;
2070     if (info.argc < 1) { // 1: params num
2071         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
2072         return engine.CreateUndefined();
2073     }
2074     AvoidAreaType avoidAreaType = AvoidAreaType::TYPE_SYSTEM;
2075     NativeNumber* nativeMode = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
2076     if (nativeMode == nullptr) {
2077         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2078     } else {
2079         avoidAreaType = static_cast<AvoidAreaType>(static_cast<uint32_t>(*nativeMode));
2080         errCode = ((avoidAreaType > AvoidAreaType::TYPE_KEYBOARD) || (avoidAreaType < AvoidAreaType::TYPE_SYSTEM)) ?
2081             WmErrorCode::WM_ERROR_INVALID_PARAM : WmErrorCode::WM_OK;
2082     }
2083     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
2084         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
2085         return engine.CreateUndefined();
2086     }
2087 
2088     wptr<Window> weakToken(windowToken_);
2089     auto window = weakToken.promote();
2090     if (window == nullptr) {
2091         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
2092         return engine.CreateUndefined();
2093     }
2094     // getAvoidRect by avoidAreaType
2095     AvoidArea avoidArea;
2096     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(window->GetAvoidAreaByType(avoidAreaType, avoidArea));
2097     if (ret != WmErrorCode::WM_OK) {
2098         avoidArea.topRect_ = g_emptyRect;
2099         avoidArea.leftRect_ = g_emptyRect;
2100         avoidArea.rightRect_ = g_emptyRect;
2101         avoidArea.bottomRect_ = g_emptyRect;
2102     }
2103     NativeValue* avoidAreaObj = ConvertAvoidAreaToJsValue(engine, avoidArea, avoidAreaType);
2104     if (avoidAreaObj != nullptr) {
2105         return avoidAreaObj;
2106     } else {
2107         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
2108         return engine.CreateUndefined();
2109     }
2110 }
2111 
OnIsShowing(NativeEngine & engine,NativeCallbackInfo & info)2112 NativeValue* JsWindow::OnIsShowing(NativeEngine& engine, NativeCallbackInfo& info)
2113 {
2114     WMError errCode = WMError::WM_OK;
2115     if (info.argc > 1) {
2116         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2117         errCode = WMError::WM_ERROR_INVALID_PARAM;
2118     }
2119     wptr<Window> weakToken(windowToken_);
2120     AsyncTask::CompleteCallback complete =
2121         [weakToken, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
2122             auto weakWindow = weakToken.promote();
2123             if (weakWindow == nullptr) {
2124                 WLOGFE("window is nullptr");
2125                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
2126                 return;
2127             }
2128             if (errCode != WMError::WM_OK) {
2129                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
2130                 WLOGFE("window is nullptr or get invalid param");
2131                 return;
2132             }
2133             bool state = weakWindow->GetWindowState() == WindowState::STATE_SHOWN;
2134             task.Resolve(engine, CreateJsValue(engine, state));
2135             WLOGI("Window [%{public}u, %{public}s] get show state end, state = %{public}u",
2136                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), state);
2137         };
2138 
2139     NativeValue* lastParam = (info.argc == 0) ? nullptr :
2140         (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
2141     NativeValue* result = nullptr;
2142     AsyncTask::Schedule("JsWindow::OnIsShowing",
2143         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2144     return result;
2145 }
2146 
OnIsWindowShowingSync(NativeEngine & engine,NativeCallbackInfo & info)2147 NativeValue* JsWindow::OnIsWindowShowingSync(NativeEngine& engine, NativeCallbackInfo& info)
2148 {
2149     wptr<Window> weakToken(windowToken_);
2150     auto window = weakToken.promote();
2151     if (window == nullptr) {
2152         WLOGFE("window is nullptr");
2153         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
2154         return engine.CreateUndefined();
2155     }
2156     bool state = (window->GetWindowState() == WindowState::STATE_SHOWN);
2157     WLOGI("Window [%{public}u, %{public}s] get show state end, state = %{public}u",
2158         window->GetWindowId(), window->GetWindowName().c_str(), state);
2159     return CreateJsValue(engine, state);
2160 }
2161 
OnSetPreferredOrientation(NativeEngine & engine,NativeCallbackInfo & info)2162 NativeValue* JsWindow::OnSetPreferredOrientation(NativeEngine& engine, NativeCallbackInfo& info)
2163 {
2164     WmErrorCode errCode = WmErrorCode::WM_OK;
2165     Orientation requestedOrientation = Orientation::UNSPECIFIED;
2166     if (info.argc < 1) { // 1: params num
2167         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2168         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2169     } else {
2170         NativeNumber* nativeType = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
2171         if (nativeType == nullptr) {
2172             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2173             WLOGFE("Failed to convert parameter to Orientation");
2174         } else {
2175             auto requestedApiOrientation = static_cast<ApiOrientation>(static_cast<uint32_t>(*nativeType));
2176             if (requestedApiOrientation < ApiOrientation::UNSPECIFIED ||
2177                 requestedApiOrientation > ApiOrientation::LOCKED) {
2178                 WLOGFE("Orientation %{public}u invalid!", static_cast<uint32_t>(requestedApiOrientation));
2179                 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2180             } else {
2181                 requestedOrientation = JS_TO_NATIVE_ORIENTATION_MAP.at(requestedApiOrientation);
2182             }
2183         }
2184     }
2185     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
2186         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
2187         return engine.CreateUndefined();
2188     }
2189 
2190     wptr<Window> weakToken(windowToken_);
2191     AsyncTask::CompleteCallback complete =
2192         [weakToken, requestedOrientation](NativeEngine& engine, AsyncTask& task, int32_t status) {
2193             auto weakWindow = weakToken.promote();
2194             if (weakWindow == nullptr) {
2195                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY),
2196                     "OnSetPreferredOrientation failed"));
2197                 return;
2198             }
2199             weakWindow->SetRequestedOrientation(requestedOrientation);
2200             task.Resolve(engine, engine.CreateUndefined());
2201             WLOGI("Window [%{public}u, %{public}s] OnSetPreferredOrientation end, orientation = %{public}u",
2202                 weakWindow->GetWindowId(),
2203                 weakWindow->GetWindowName().c_str(),
2204                 static_cast<uint32_t>(requestedOrientation));
2205         };
2206 
2207     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
2208         ((info.argv[1] != nullptr && info.argv[1]->TypeOf() == NATIVE_FUNCTION) ?
2209         info.argv[1] : nullptr);
2210     NativeValue* result = nullptr;
2211     AsyncTask::Schedule("JsWindow::OnSetPreferredOrientation",
2212         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2213     return result;
2214 }
2215 
OnIsSupportWideGamut(NativeEngine & engine,NativeCallbackInfo & info)2216 NativeValue* JsWindow::OnIsSupportWideGamut(NativeEngine& engine, NativeCallbackInfo& info)
2217 {
2218     WMError errCode = WMError::WM_OK;
2219     if (info.argc > 1) {
2220         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2221         errCode = WMError::WM_ERROR_INVALID_PARAM;
2222     }
2223     wptr<Window> weakToken(windowToken_);
2224     AsyncTask::CompleteCallback complete =
2225         [weakToken, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
2226             auto weakWindow = weakToken.promote();
2227             if (weakWindow == nullptr) {
2228                 WLOGFE("window is nullptr");
2229                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
2230                 return;
2231             }
2232             if (errCode != WMError::WM_OK) {
2233                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
2234                 WLOGFE("window is nullptr or get invalid param");
2235                 return;
2236             }
2237             bool flag = weakWindow->IsSupportWideGamut();
2238             task.Resolve(engine, CreateJsValue(engine, flag));
2239             WLOGI("Window [%{public}u, %{public}s] OnIsSupportWideGamut end, ret = %{public}u",
2240                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), flag);
2241         };
2242 
2243     NativeValue* lastParam = (info.argc == 0) ? nullptr :
2244         (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
2245     NativeValue* result = nullptr;
2246     AsyncTask::Schedule("JsWindow::OnIsSupportWideGamut",
2247         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2248     return result;
2249 }
2250 
OnIsWindowSupportWideGamut(NativeEngine & engine,NativeCallbackInfo & info)2251 NativeValue* JsWindow::OnIsWindowSupportWideGamut(NativeEngine& engine, NativeCallbackInfo& info)
2252 {
2253     wptr<Window> weakToken(windowToken_);
2254     AsyncTask::CompleteCallback complete =
2255         [weakToken](NativeEngine& engine, AsyncTask& task, int32_t status) {
2256             auto weakWindow = weakToken.promote();
2257             if (weakWindow == nullptr) {
2258                 WLOGFE("window is nullptr or get invalid param");
2259                 task.Reject(engine,
2260                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
2261                 return;
2262             }
2263             bool flag = weakWindow->IsSupportWideGamut();
2264             task.Resolve(engine, CreateJsValue(engine, flag));
2265             WLOGI("Window [%{public}u, %{public}s] OnIsWindowSupportWideGamut end, ret = %{public}u",
2266                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), flag);
2267         };
2268 
2269     NativeValue* lastParam = (info.argc == 0) ? nullptr :
2270         ((info.argv[0] != nullptr && info.argv[0]->TypeOf() == NATIVE_FUNCTION) ?
2271         info.argv[0] : nullptr);
2272     NativeValue* result = nullptr;
2273     AsyncTask::Schedule("JsWindow::OnIsWindowSupportWideGamut",
2274         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2275     return result;
2276 }
2277 
OnSetBackgroundColor(NativeEngine & engine,NativeCallbackInfo & info)2278 NativeValue* JsWindow::OnSetBackgroundColor(NativeEngine& engine, NativeCallbackInfo& info)
2279 {
2280     WMError errCode = WMError::WM_OK;
2281     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
2282         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2283         errCode = WMError::WM_ERROR_INVALID_PARAM;
2284     }
2285     std::string color;
2286     if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[0], color)) {
2287         WLOGFE("Failed to convert parameter to background color");
2288         errCode = WMError::WM_ERROR_INVALID_PARAM;
2289     }
2290 
2291     wptr<Window> weakToken(windowToken_);
2292     AsyncTask::CompleteCallback complete =
2293         [weakToken, color, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
2294             auto weakWindow = weakToken.promote();
2295             if (weakWindow == nullptr) {
2296                 WLOGFE("window is nullptr");
2297                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
2298                 return;
2299             }
2300             if (errCode != WMError::WM_OK) {
2301                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
2302                 return;
2303             }
2304             WMError ret = weakWindow->SetBackgroundColor(color);
2305             if (ret == WMError::WM_OK) {
2306                 task.Resolve(engine, engine.CreateUndefined());
2307             } else {
2308                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret),
2309                     "Window set background color failed"));
2310             }
2311             WLOGFD("Window [%{public}u, %{public}s] set background color end",
2312                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
2313         };
2314 
2315     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
2316         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
2317     NativeValue* result = nullptr;
2318     AsyncTask::Schedule("JsWindow::OnSetBackgroundColor",
2319         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2320     return result;
2321 }
2322 
OnSetWindowBackgroundColorSync(NativeEngine & engine,NativeCallbackInfo & info)2323 NativeValue* JsWindow::OnSetWindowBackgroundColorSync(NativeEngine& engine, NativeCallbackInfo& info)
2324 {
2325     WmErrorCode errCode = WmErrorCode::WM_OK;
2326     if (info.argc < 1) { // 1: params num
2327         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2328         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2329     }
2330     std::string color;
2331     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(engine, info.argv[0], color)) {
2332         WLOGFE("Failed to convert parameter to background color");
2333         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2334     }
2335     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
2336         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
2337         return engine.CreateUndefined();
2338     }
2339 
2340     wptr<Window> weakToken(windowToken_);
2341     auto window = weakToken.promote();
2342     if (window == nullptr) {
2343         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
2344         return engine.CreateUndefined();
2345     }
2346     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(window->SetBackgroundColor(color));
2347     if (ret == WmErrorCode::WM_OK) {
2348         WLOGI("Window [%{public}u, %{public}s] set background color end",
2349             window->GetWindowId(), window->GetWindowName().c_str());
2350         return engine.CreateUndefined();
2351     } else {
2352         engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
2353         return engine.CreateUndefined();
2354     }
2355 }
2356 
OnSetBrightness(NativeEngine & engine,NativeCallbackInfo & info)2357 NativeValue* JsWindow::OnSetBrightness(NativeEngine& engine, NativeCallbackInfo& info)
2358 {
2359     WMError errCode = WMError::WM_OK;
2360     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
2361         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2362         errCode = WMError::WM_ERROR_INVALID_PARAM;
2363     }
2364     float brightness = UNDEFINED_BRIGHTNESS;
2365     if (errCode == WMError::WM_OK) {
2366         NativeNumber* nativeVal = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
2367         if (nativeVal == nullptr) {
2368             WLOGFE("Failed to convert parameter to brightness");
2369             errCode = WMError::WM_ERROR_INVALID_PARAM;
2370         } else {
2371             brightness = static_cast<double>(*nativeVal);
2372         }
2373     }
2374 
2375     wptr<Window> weakToken(windowToken_);
2376     AsyncTask::CompleteCallback complete =
2377         [weakToken, brightness, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
2378             auto weakWindow = weakToken.promote();
2379             if (weakWindow == nullptr) {
2380                 WLOGFE("window is nullptr");
2381                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
2382                 return;
2383             }
2384             if (errCode != WMError::WM_OK) {
2385                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
2386                 return;
2387             }
2388             WMError ret = weakWindow->SetBrightness(brightness);
2389             if (ret == WMError::WM_OK) {
2390                 task.Resolve(engine, engine.CreateUndefined());
2391             } else {
2392                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window set brightness failed"));
2393             }
2394             WLOGI("Window [%{public}u, %{public}s] set brightness end",
2395                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
2396         };
2397 
2398     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
2399         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
2400     NativeValue* result = nullptr;
2401     AsyncTask::Schedule("JsWindow::OnSetBrightness",
2402         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2403     return result;
2404 }
2405 
OnSetWindowBrightness(NativeEngine & engine,NativeCallbackInfo & info)2406 NativeValue* JsWindow::OnSetWindowBrightness(NativeEngine& engine, NativeCallbackInfo& info)
2407 {
2408     WmErrorCode errCode = WmErrorCode::WM_OK;
2409     if (info.argc < 1) { // 1: params num
2410         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2411         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2412     }
2413     float brightness = UNDEFINED_BRIGHTNESS;
2414     if (errCode == WmErrorCode::WM_OK) {
2415         NativeNumber* nativeVal = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
2416         if (nativeVal == nullptr) {
2417             WLOGFE("Failed to convert parameter to brightness");
2418             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2419         } else {
2420             brightness = static_cast<double>(*nativeVal);
2421         }
2422     }
2423     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
2424         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
2425         return engine.CreateUndefined();
2426     }
2427 
2428     wptr<Window> weakToken(windowToken_);
2429     AsyncTask::CompleteCallback complete =
2430         [weakToken, brightness](NativeEngine& engine, AsyncTask& task, int32_t status) {
2431             auto weakWindow = weakToken.promote();
2432             if (weakWindow == nullptr) {
2433                 task.Reject(engine,
2434                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY),
2435                     "Invalidate params."));
2436                 return;
2437             }
2438             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetBrightness(brightness));
2439             if (ret == WmErrorCode::WM_OK) {
2440                 task.Resolve(engine, engine.CreateUndefined());
2441             } else {
2442                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window set brightness failed"));
2443             }
2444             WLOGI("Window [%{public}u, %{public}s] set brightness end",
2445                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
2446         };
2447 
2448     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
2449         ((info.argv[1] != nullptr && info.argv[1]->TypeOf() == NATIVE_FUNCTION) ?
2450         info.argv[1] : nullptr);
2451     NativeValue* result = nullptr;
2452     AsyncTask::Schedule("JsWindow::OnSetWindowBrightness",
2453         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2454     return result;
2455 }
2456 
OnSetDimBehind(NativeEngine & engine,NativeCallbackInfo & info)2457 NativeValue* JsWindow::OnSetDimBehind(NativeEngine& engine, NativeCallbackInfo& info)
2458 {
2459     AsyncTask::CompleteCallback complete =
2460         [](NativeEngine& engine, AsyncTask& task, int32_t status) {
2461             task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_DEVICE_NOT_SUPPORT)));
2462         };
2463 
2464     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
2465         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
2466     NativeValue* result = nullptr;
2467     AsyncTask::Schedule("JsWindow::OnSetDimBehind",
2468         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2469     return result;
2470 }
2471 
OnSetFocusable(NativeEngine & engine,NativeCallbackInfo & info)2472 NativeValue* JsWindow::OnSetFocusable(NativeEngine& engine, NativeCallbackInfo& info)
2473 {
2474     WMError errCode = WMError::WM_OK;
2475     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
2476         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2477         errCode = WMError::WM_ERROR_INVALID_PARAM;
2478     }
2479     bool focusable = true;
2480     if (errCode == WMError::WM_OK) {
2481         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
2482         if (nativeVal == nullptr) {
2483             WLOGFE("Failed to convert parameter to focusable");
2484             errCode = WMError::WM_ERROR_INVALID_PARAM;
2485         } else {
2486             focusable = static_cast<bool>(*nativeVal);
2487         }
2488     }
2489 
2490     wptr<Window> weakToken(windowToken_);
2491     AsyncTask::CompleteCallback complete =
2492         [weakToken, focusable, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
2493             auto weakWindow = weakToken.promote();
2494             if (weakWindow == nullptr) {
2495                 WLOGFE("window is nullptr");
2496                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
2497                 return;
2498             }
2499             if (errCode != WMError::WM_OK) {
2500                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
2501                 return;
2502             }
2503             WMError ret = weakWindow->SetFocusable(focusable);
2504             if (ret == WMError::WM_OK) {
2505                 task.Resolve(engine, engine.CreateUndefined());
2506             } else {
2507                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window set focusable failed"));
2508             }
2509             WLOGI("Window [%{public}u, %{public}s] set focusable end",
2510                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
2511         };
2512 
2513     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
2514         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
2515     NativeValue* result = nullptr;
2516     AsyncTask::Schedule("JsWindow::OnSetFocusable",
2517         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2518     return result;
2519 }
2520 
OnSetWindowFocusable(NativeEngine & engine,NativeCallbackInfo & info)2521 NativeValue* JsWindow::OnSetWindowFocusable(NativeEngine& engine, NativeCallbackInfo& info)
2522 {
2523     WmErrorCode errCode = WmErrorCode::WM_OK;
2524     if (info.argc < 1) { // 1: maximum params num
2525         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2526         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2527     }
2528     bool focusable = true;
2529     if (errCode == WmErrorCode::WM_OK) {
2530         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
2531         if (nativeVal == nullptr) {
2532             WLOGFE("Failed to convert parameter to focusable");
2533             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2534         } else {
2535             focusable = static_cast<bool>(*nativeVal);
2536         }
2537     }
2538     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
2539         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
2540         return engine.CreateUndefined();
2541     }
2542 
2543     wptr<Window> weakToken(windowToken_);
2544     AsyncTask::CompleteCallback complete =
2545         [weakToken, focusable](NativeEngine& engine, AsyncTask& task, int32_t status) {
2546             auto weakWindow = weakToken.promote();
2547             if (weakWindow == nullptr) {
2548                 task.Reject(engine,
2549                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY),
2550                     "Invalidate params."));
2551                 return;
2552             }
2553             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetFocusable(focusable));
2554             if (ret == WmErrorCode::WM_OK) {
2555                 task.Resolve(engine, engine.CreateUndefined());
2556             } else {
2557                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window set focusable failed"));
2558             }
2559             WLOGI("Window [%{public}u, %{public}s] set focusable end",
2560                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
2561         };
2562 
2563     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
2564         ((info.argv[1] != nullptr && info.argv[1]->TypeOf() == NATIVE_FUNCTION) ?
2565         info.argv[1] : nullptr);
2566     NativeValue* result = nullptr;
2567     AsyncTask::Schedule("JsWindow::OnSetWindowFocusable",
2568         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2569     return result;
2570 }
2571 
OnSetKeepScreenOn(NativeEngine & engine,NativeCallbackInfo & info)2572 NativeValue* JsWindow::OnSetKeepScreenOn(NativeEngine& engine, NativeCallbackInfo& info)
2573 {
2574     WMError errCode = WMError::WM_OK;
2575     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
2576         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2577         errCode = WMError::WM_ERROR_INVALID_PARAM;
2578     }
2579     bool keepScreenOn = true;
2580     if (errCode == WMError::WM_OK) {
2581         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
2582         if (nativeVal == nullptr) {
2583             WLOGFE("Failed to convert parameter to keepScreenOn");
2584             errCode = WMError::WM_ERROR_INVALID_PARAM;
2585         } else {
2586             keepScreenOn = static_cast<bool>(*nativeVal);
2587         }
2588     }
2589 
2590     wptr<Window> weakToken(windowToken_);
2591     AsyncTask::CompleteCallback complete =
2592         [weakToken, keepScreenOn, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
2593             auto weakWindow = weakToken.promote();
2594             if (weakWindow == nullptr) {
2595                 WLOGFE("window is nullptr");
2596                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
2597                 return;
2598             }
2599             if (errCode != WMError::WM_OK) {
2600                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
2601                 return;
2602             }
2603             WMError ret = weakWindow->SetKeepScreenOn(keepScreenOn);
2604             if (ret == WMError::WM_OK) {
2605                 task.Resolve(engine, engine.CreateUndefined());
2606             } else {
2607                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret),
2608                     "Window set keep screen on failed"));
2609             }
2610             WLOGI("Window [%{public}u, %{public}s] set keep screen on end",
2611                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
2612         };
2613 
2614     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
2615         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
2616     NativeValue* result = nullptr;
2617     AsyncTask::Schedule("JsWindow::OnSetKeepScreenOn",
2618         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2619     return result;
2620 }
2621 
OnSetWindowKeepScreenOn(NativeEngine & engine,NativeCallbackInfo & info)2622 NativeValue* JsWindow::OnSetWindowKeepScreenOn(NativeEngine& engine, NativeCallbackInfo& info)
2623 {
2624     WmErrorCode errCode = WmErrorCode::WM_OK;
2625     if (info.argc < 1) { // 1: params num
2626         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2627         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2628     }
2629     bool keepScreenOn = true;
2630     if (errCode == WmErrorCode::WM_OK) {
2631         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
2632         if (nativeVal == nullptr) {
2633             WLOGFE("Failed to convert parameter to keepScreenOn");
2634             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2635         } else {
2636             keepScreenOn = static_cast<bool>(*nativeVal);
2637         }
2638     }
2639     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
2640         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
2641         return engine.CreateUndefined();
2642     }
2643 
2644     wptr<Window> weakToken(windowToken_);
2645     AsyncTask::CompleteCallback complete =
2646         [weakToken, keepScreenOn](NativeEngine& engine, AsyncTask& task, int32_t status) {
2647             auto weakWindow = weakToken.promote();
2648             if (weakWindow == nullptr) {
2649                 task.Reject(engine,
2650                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY),
2651                     "Invalidate params."));
2652                 return;
2653             }
2654             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetKeepScreenOn(keepScreenOn));
2655             if (ret == WmErrorCode::WM_OK) {
2656                 task.Resolve(engine, engine.CreateUndefined());
2657             } else {
2658                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret),
2659                     "Window set keep screen on failed"));
2660             }
2661             WLOGI("Window [%{public}u, %{public}s] set keep screen on end",
2662                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
2663         };
2664 
2665     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
2666         ((info.argv[1] != nullptr && info.argv[1]->TypeOf() == NATIVE_FUNCTION) ?
2667         info.argv[1] : nullptr);
2668     NativeValue* result = nullptr;
2669     AsyncTask::Schedule("JsWindow::OnSetWindowKeepScreenOn",
2670         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2671     return result;
2672 }
2673 
OnSetWakeUpScreen(NativeEngine & engine,NativeCallbackInfo & info)2674 NativeValue* JsWindow::OnSetWakeUpScreen(NativeEngine& engine, NativeCallbackInfo& info)
2675 {
2676     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
2677         WLOGFE("set wake up screen permission denied!");
2678         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP)));
2679         return engine.CreateUndefined();
2680     }
2681     WmErrorCode errCode = WmErrorCode::WM_OK;
2682     if (windowToken_ == nullptr) {
2683         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
2684         return engine.CreateUndefined();
2685     }
2686     if (info.argc < 1) {
2687         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2688         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
2689         return engine.CreateUndefined();
2690     }
2691     bool wakeUp = false;
2692     if (errCode == WmErrorCode::WM_OK) {
2693         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
2694         if (nativeVal == nullptr) {
2695             WLOGFE("Failed to convert parameter to keepScreenOn");
2696             engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
2697             return engine.CreateUndefined();
2698         } else {
2699             wakeUp = static_cast<bool>(*nativeVal);
2700         }
2701     }
2702 
2703     windowToken_->SetTurnScreenOn(wakeUp);
2704     WLOGI("Window [%{public}u, %{public}s] set wake up screen %{public}d end",
2705         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), wakeUp);
2706     return engine.CreateUndefined();
2707 }
2708 
OnSetOutsideTouchable(NativeEngine & engine,NativeCallbackInfo & info)2709 NativeValue* JsWindow::OnSetOutsideTouchable(NativeEngine& engine, NativeCallbackInfo& info)
2710 {
2711     AsyncTask::CompleteCallback complete =
2712         [](NativeEngine& engine, AsyncTask& task, int32_t status) {
2713             task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_DEVICE_NOT_SUPPORT)));
2714         };
2715 
2716     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
2717         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
2718     NativeValue* result = nullptr;
2719     AsyncTask::Schedule("JsWindow::OnSetOutsideTouchable",
2720         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2721     return result;
2722 }
2723 
OnSetPrivacyMode(NativeEngine & engine,NativeCallbackInfo & info)2724 NativeValue* JsWindow::OnSetPrivacyMode(NativeEngine& engine, NativeCallbackInfo& info)
2725 {
2726     WMError errCode = WMError::WM_OK;
2727     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
2728         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2729         errCode = WMError::WM_ERROR_INVALID_PARAM;
2730     }
2731     bool isPrivacyMode = false;
2732     if (errCode == WMError::WM_OK) {
2733         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
2734         if (nativeVal == nullptr) {
2735             WLOGFE("Failed to convert parameter to isPrivacyMode");
2736             errCode = WMError::WM_ERROR_INVALID_PARAM;
2737         } else {
2738             isPrivacyMode = static_cast<bool>(*nativeVal);
2739         }
2740     }
2741 
2742     wptr<Window> weakToken(windowToken_);
2743     AsyncTask::CompleteCallback complete =
2744         [weakToken, isPrivacyMode, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
2745             auto weakWindow = weakToken.promote();
2746             if (weakWindow == nullptr) {
2747                 WLOGFE("window is nullptr");
2748                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
2749                 return;
2750             }
2751             if (errCode != WMError::WM_OK) {
2752                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params"));
2753                 return;
2754             }
2755             weakWindow->SetPrivacyMode(isPrivacyMode);
2756             task.Resolve(engine, engine.CreateUndefined());
2757             WLOGI("Window [%{public}u, %{public}s] set privacy mode end, mode = %{public}u",
2758                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), isPrivacyMode);
2759         };
2760 
2761     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
2762         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
2763     NativeValue* result = nullptr;
2764     AsyncTask::Schedule("JsWindow::OnSetPrivacyMode",
2765         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2766     return result;
2767 }
2768 
OnSetWindowPrivacyMode(NativeEngine & engine,NativeCallbackInfo & info)2769 NativeValue* JsWindow::OnSetWindowPrivacyMode(NativeEngine& engine, NativeCallbackInfo& info)
2770 {
2771     WmErrorCode errCode = WmErrorCode::WM_OK;
2772     if (info.argc < 1) { // 1: params num
2773         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2774         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2775     }
2776     bool isPrivacyMode = false;
2777     if (errCode == WmErrorCode::WM_OK) {
2778         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
2779         if (nativeVal == nullptr) {
2780             WLOGFE("Failed to convert parameter to isPrivacyMode");
2781             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2782         } else {
2783             isPrivacyMode = static_cast<bool>(*nativeVal);
2784         }
2785     }
2786     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
2787         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
2788         return engine.CreateUndefined();
2789     }
2790 
2791     wptr<Window> weakToken(windowToken_);
2792     AsyncTask::CompleteCallback complete =
2793         [weakToken, isPrivacyMode](NativeEngine& engine, AsyncTask& task, int32_t status) {
2794             auto weakWindow = weakToken.promote();
2795             if (weakWindow == nullptr) {
2796                 task.Reject(engine,
2797                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY),
2798                     "Invalidate params"));
2799                 return;
2800             }
2801             weakWindow->SetPrivacyMode(isPrivacyMode);
2802             task.Resolve(engine, engine.CreateUndefined());
2803             WLOGI("Window [%{public}u, %{public}s] set privacy mode end, mode = %{public}u",
2804                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), isPrivacyMode);
2805         };
2806 
2807     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
2808         ((info.argv[1] != nullptr && info.argv[1]->TypeOf() == NATIVE_FUNCTION) ?
2809         info.argv[1] : nullptr);
2810     NativeValue* result = nullptr;
2811     AsyncTask::Schedule("JsWindow::OnSetWindowPrivacyMode",
2812         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2813     return result;
2814 }
2815 
OnSetTouchable(NativeEngine & engine,NativeCallbackInfo & info)2816 NativeValue* JsWindow::OnSetTouchable(NativeEngine& engine, NativeCallbackInfo& info)
2817 {
2818     WMError errCode = WMError::WM_OK;
2819     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
2820         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2821         errCode = WMError::WM_ERROR_INVALID_PARAM;
2822     }
2823     bool touchable = true;
2824     if (errCode == WMError::WM_OK) {
2825         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
2826         if (nativeVal == nullptr) {
2827             WLOGFE("Failed to convert parameter to touchable");
2828             errCode = WMError::WM_ERROR_INVALID_PARAM;
2829         } else {
2830             touchable = static_cast<bool>(*nativeVal);
2831         }
2832     }
2833 
2834     wptr<Window> weakToken(windowToken_);
2835     AsyncTask::CompleteCallback complete =
2836         [weakToken, touchable, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
2837             auto weakWindow = weakToken.promote();
2838             if (weakWindow == nullptr) {
2839                 WLOGFE("window is nullptr");
2840                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
2841                 return;
2842             }
2843             if (errCode != WMError::WM_OK) {
2844                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
2845                 return;
2846             }
2847             WMError ret = weakWindow->SetTouchable(touchable);
2848             if (ret == WMError::WM_OK) {
2849                 task.Resolve(engine, engine.CreateUndefined());
2850             } else {
2851                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window set touchable failed"));
2852             }
2853             WLOGI("Window [%{public}u, %{public}s] set touchable end",
2854                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
2855         };
2856 
2857     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
2858         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
2859     NativeValue* result = nullptr;
2860     AsyncTask::Schedule("JsWindow::OnSetTouchable",
2861         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2862     return result;
2863 }
2864 
OnSetResizeByDragEnabled(NativeEngine & engine,NativeCallbackInfo & info)2865 NativeValue* JsWindow::OnSetResizeByDragEnabled(NativeEngine& engine, NativeCallbackInfo& info)
2866 {
2867     WMError errCode = WMError::WM_OK;
2868     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
2869         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2870         errCode = WMError::WM_ERROR_INVALID_PARAM;
2871     }
2872     bool dragEnabled = true;
2873     if (errCode == WMError::WM_OK) {
2874         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
2875         if (nativeVal == nullptr) {
2876             WLOGFE("Failed to convert parameter to dragEnabled");
2877             errCode = WMError::WM_ERROR_INVALID_PARAM;
2878         } else {
2879             dragEnabled = static_cast<bool>(*nativeVal);
2880         }
2881     }
2882 
2883     wptr<Window> weakToken(windowToken_);
2884     AsyncTask::CompleteCallback complete =
2885         [weakToken, dragEnabled, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
2886             auto weakWindow = weakToken.promote();
2887             WmErrorCode wmErrorCode;
2888             if (weakWindow == nullptr || errCode != WMError::WM_OK) {
2889                 wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(errCode);
2890                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(wmErrorCode), "Invalidate params."));
2891                 return;
2892             }
2893             WMError ret = weakWindow->SetResizeByDragEnabled(dragEnabled);
2894             if (ret == WMError::WM_OK) {
2895                 task.Resolve(engine, engine.CreateUndefined());
2896             } else {
2897                 wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(ret);
2898                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(wmErrorCode), "Window set dragEnabled failed"));
2899             }
2900             WLOGI("Window [%{public}u, %{public}s] set dragEnabled end",
2901                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
2902         };
2903 
2904     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
2905         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
2906     NativeValue* result = nullptr;
2907     AsyncTask::Schedule("JsWindow::SetResizeByDragEnabled",
2908         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2909     return result;
2910 }
2911 
OnSetRaiseByClickEnabled(NativeEngine & engine,NativeCallbackInfo & info)2912 NativeValue* JsWindow::OnSetRaiseByClickEnabled(NativeEngine& engine, NativeCallbackInfo& info)
2913 {
2914     WMError errCode = WMError::WM_OK;
2915     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
2916         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2917         errCode = WMError::WM_ERROR_INVALID_PARAM;
2918     }
2919     bool raiseEnabled = true;
2920     if (errCode == WMError::WM_OK) {
2921         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
2922         if (nativeVal == nullptr) {
2923             WLOGFE("Failed to convert parameter to raiseEnabled");
2924             errCode = WMError::WM_ERROR_INVALID_PARAM;
2925         } else {
2926             raiseEnabled = static_cast<bool>(*nativeVal);
2927         }
2928     }
2929 
2930     wptr<Window> weakToken(windowToken_);
2931     AsyncTask::CompleteCallback complete =
2932         [weakToken, raiseEnabled, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
2933             auto weakWindow = weakToken.promote();
2934             WmErrorCode wmErrorCode;
2935             if (weakWindow == nullptr || errCode != WMError::WM_OK) {
2936                 wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(errCode);
2937                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(wmErrorCode), "Invalidate params."));
2938                 return;
2939             }
2940             WMError ret = weakWindow->SetRaiseByClickEnabled(raiseEnabled);
2941             if (ret == WMError::WM_OK) {
2942                 task.Resolve(engine, engine.CreateUndefined());
2943             } else {
2944                 wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(ret);
2945                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(wmErrorCode),
2946                                                   "Window set raiseEnabled failed"));
2947             }
2948             WLOGI("Window [%{public}u, %{public}s] set raiseEnabled end",
2949                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
2950         };
2951 
2952     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
2953         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
2954     NativeValue* result = nullptr;
2955     AsyncTask::Schedule("JsWindow::SetRaiseByClickEnabled",
2956         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
2957     return result;
2958 }
2959 
OnRaiseAboveTarget(NativeEngine & engine,NativeCallbackInfo & info)2960 NativeValue* JsWindow::OnRaiseAboveTarget(NativeEngine& engine, NativeCallbackInfo& info)
2961 {
2962     WmErrorCode errCode = WmErrorCode::WM_OK;
2963     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
2964         WLOGFE("Argc is invalid: %{public}zu", info.argc);
2965         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2966     }
2967     int32_t subWindowId = -1;
2968     if (errCode == WmErrorCode::WM_OK) {
2969         NativeNumber* nativeVal = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
2970         if (nativeVal == nullptr || static_cast<int32_t>(*nativeVal) <= 0) {
2971             WLOGFE("Failed to convert parameter to subWindowId");
2972             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
2973         } else {
2974             subWindowId = static_cast<int32_t>(*nativeVal);
2975         }
2976     }
2977 
2978     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
2979         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
2980         return engine.CreateUndefined();
2981     }
2982 
2983     wptr<Window> weakToken(windowToken_);
2984     AsyncTask::CompleteCallback complete =
2985         [weakToken, subWindowId, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
2986             auto weakWindow = weakToken.promote();
2987             if (weakWindow == nullptr) {
2988                 task.Reject(engine, CreateJsError(engine,
2989                     static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
2990                 return;
2991             }
2992             if (errCode != WmErrorCode::WM_OK) {
2993                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
2994                 return;
2995             }
2996             WmErrorCode ret = weakWindow->RaiseAboveTarget(subWindowId);
2997             if (ret == WmErrorCode::WM_OK) {
2998                 task.Resolve(engine, engine.CreateUndefined());
2999             } else {
3000                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window set raiseAboveTarget failed"));
3001             }
3002         };
3003 
3004     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
3005         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
3006     NativeValue* result = nullptr;
3007     AsyncTask::Schedule("JsWindow::RaiseAboveTarget",
3008         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
3009     return result;
3010 }
3011 
OnSetWindowTouchable(NativeEngine & engine,NativeCallbackInfo & info)3012 NativeValue* JsWindow::OnSetWindowTouchable(NativeEngine& engine, NativeCallbackInfo& info)
3013 {
3014     WmErrorCode errCode = WmErrorCode::WM_OK;
3015     if (info.argc < 1) { // 1: params num
3016         WLOGFE("Argc is invalid: %{public}zu", info.argc);
3017         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3018     }
3019     bool touchable = true;
3020     if (errCode == WmErrorCode::WM_OK) {
3021         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
3022         if (nativeVal == nullptr) {
3023             WLOGFE("Failed to convert parameter to touchable");
3024             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3025         } else {
3026             touchable = static_cast<bool>(*nativeVal);
3027         }
3028     }
3029     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
3030         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3031         return engine.CreateUndefined();
3032     }
3033 
3034     wptr<Window> weakToken(windowToken_);
3035     AsyncTask::CompleteCallback complete =
3036         [weakToken, touchable](NativeEngine& engine, AsyncTask& task, int32_t status) {
3037             auto weakWindow = weakToken.promote();
3038             if (weakWindow == nullptr) {
3039                 task.Reject(engine,
3040                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY),
3041                     "Invalidate params."));
3042                 return;
3043             }
3044             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetTouchable(touchable));
3045             if (ret == WmErrorCode::WM_OK) {
3046                 task.Resolve(engine, engine.CreateUndefined());
3047             } else {
3048                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window set touchable failed"));
3049             }
3050             WLOGI("Window [%{public}u, %{public}s] set touchable end",
3051                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
3052         };
3053 
3054     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
3055         ((info.argv[1] != nullptr && info.argv[1]->TypeOf() == NATIVE_FUNCTION) ?
3056         info.argv[1] : nullptr);
3057     NativeValue* result = nullptr;
3058     AsyncTask::Schedule("JsWindow::OnSetWindowTouchable",
3059         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
3060     return result;
3061 }
3062 
OnSetTransparent(NativeEngine & engine,NativeCallbackInfo & info)3063 NativeValue* JsWindow::OnSetTransparent(NativeEngine& engine, NativeCallbackInfo& info)
3064 {
3065     WMError errCode = WMError::WM_OK;
3066     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
3067         WLOGFE("Argc is invalid: %{public}zu", info.argc);
3068         errCode = WMError::WM_ERROR_INVALID_PARAM;
3069     }
3070     bool isTransparent = true;
3071     if (errCode == WMError::WM_OK) {
3072         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
3073         if (nativeVal == nullptr) {
3074             WLOGFE("Failed to convert parameter to isTransparent");
3075             errCode = WMError::WM_ERROR_INVALID_PARAM;
3076         } else {
3077             isTransparent = static_cast<bool>(*nativeVal);
3078         }
3079     }
3080 
3081     wptr<Window> weakToken(windowToken_);
3082     AsyncTask::CompleteCallback complete =
3083         [weakToken, isTransparent, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
3084             auto weakWindow = weakToken.promote();
3085             if (weakWindow == nullptr) {
3086                 WLOGFE("window is nullptr");
3087                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
3088                 return;
3089             }
3090             if (errCode != WMError::WM_OK) {
3091                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
3092                 return;
3093             }
3094             WMError ret = weakWindow->SetTransparent(isTransparent);
3095             if (ret == WMError::WM_OK) {
3096                 task.Resolve(engine, engine.CreateUndefined());
3097             } else {
3098                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window set transparent failed"));
3099             }
3100             WLOGI("Window [%{public}u, %{public}s] set transparent end",
3101                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
3102         };
3103 
3104     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
3105         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
3106     NativeValue* result = nullptr;
3107     AsyncTask::Schedule("JsWindow::OnSetTransparent",
3108         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
3109     return result;
3110 }
3111 
OnSetCallingWindow(NativeEngine & engine,NativeCallbackInfo & info)3112 NativeValue* JsWindow::OnSetCallingWindow(NativeEngine& engine, NativeCallbackInfo& info)
3113 {
3114     WMError errCode = WMError::WM_OK;
3115     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
3116         WLOGFE("Argc is invalid: %{public}zu", info.argc);
3117         errCode = WMError::WM_ERROR_INVALID_PARAM;
3118     }
3119     uint32_t callingWindow = INVALID_WINDOW_ID;
3120     if (errCode == WMError::WM_OK) {
3121         NativeNumber* nativeVal = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
3122         if (nativeVal == nullptr) {
3123             WLOGFE("Failed to convert parameter to touchable");
3124             errCode = WMError::WM_ERROR_INVALID_PARAM;
3125         } else {
3126             callingWindow = static_cast<uint32_t>(*nativeVal);
3127         }
3128     }
3129 
3130     wptr<Window> weakToken(windowToken_);
3131     AsyncTask::CompleteCallback complete =
3132         [weakToken, callingWindow, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
3133             auto weakWindow = weakToken.promote();
3134             if (weakWindow == nullptr) {
3135                 WLOGFE("window is nullptr");
3136                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
3137                 return;
3138             }
3139             if (errCode != WMError::WM_OK) {
3140                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
3141                 return;
3142             }
3143             WMError ret = weakWindow->SetCallingWindow(callingWindow);
3144             if (ret == WMError::WM_OK) {
3145                 task.Resolve(engine, engine.CreateUndefined());
3146             } else {
3147                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret),
3148                     "Window set calling window failed"));
3149             }
3150             WLOGI("Window [%{public}u, %{public}s] set calling window end",
3151                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
3152         };
3153 
3154     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
3155         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
3156     NativeValue* result = nullptr;
3157     AsyncTask::Schedule("JsWindow::OnSetCallingWindow",
3158         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
3159     return result;
3160 }
3161 
OnDisableWindowDecor(NativeEngine & engine,NativeCallbackInfo & info)3162 NativeValue* JsWindow::OnDisableWindowDecor(NativeEngine& engine, NativeCallbackInfo& info)
3163 {
3164     if (windowToken_ == nullptr) {
3165         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
3166         return engine.CreateUndefined();
3167     }
3168     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->DisableAppWindowDecor());
3169     if (ret != WmErrorCode::WM_OK) {
3170         WLOGFE("Window DisableWindowDecor failed");
3171         engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
3172         return engine.CreateUndefined();
3173     }
3174     WLOGI("Window [%{public}u, %{public}s] disable app window decor end",
3175         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
3176     return engine.CreateUndefined();
3177 }
3178 
OnSetColorSpace(NativeEngine & engine,NativeCallbackInfo & info)3179 NativeValue* JsWindow::OnSetColorSpace(NativeEngine& engine, NativeCallbackInfo& info)
3180 {
3181     WMError errCode = WMError::WM_OK;
3182     ColorSpace colorSpace = ColorSpace::COLOR_SPACE_DEFAULT;
3183     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
3184         WLOGFE("Argc is invalid: %{public}zu", info.argc);
3185         errCode = WMError::WM_ERROR_INVALID_PARAM;
3186     } else {
3187         NativeNumber* nativeType = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
3188         if (nativeType == nullptr) {
3189             errCode = WMError::WM_ERROR_INVALID_PARAM;
3190             WLOGFE("Failed to convert parameter to ColorSpace");
3191         } else {
3192             colorSpace = static_cast<ColorSpace>(static_cast<uint32_t>(*nativeType));
3193             if (colorSpace > ColorSpace::COLOR_SPACE_WIDE_GAMUT) {
3194                 WLOGFE("ColorSpace %{public}u invalid!", static_cast<uint32_t>(colorSpace));
3195                 errCode = WMError::WM_ERROR_INVALID_PARAM;
3196             }
3197         }
3198     }
3199 
3200     wptr<Window> weakToken(windowToken_);
3201     AsyncTask::CompleteCallback complete =
3202         [weakToken, colorSpace, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
3203             auto weakWindow = weakToken.promote();
3204             if (weakWindow == nullptr) {
3205                 WLOGFE("window is nullptr");
3206                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
3207                 return;
3208             }
3209             if (errCode != WMError::WM_OK) {
3210                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "OnSetColorSpace failed"));
3211                 WLOGFE("window is nullptr or get invalid param");
3212                 return;
3213             }
3214             weakWindow->SetColorSpace(colorSpace);
3215             task.Resolve(engine, engine.CreateUndefined());
3216             WLOGI("Window [%{public}u, %{public}s] OnSetColorSpace end, colorSpace = %{public}u",
3217                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), static_cast<uint32_t>(colorSpace));
3218         };
3219 
3220     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
3221         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
3222     NativeValue* result = nullptr;
3223     AsyncTask::Schedule("JsWindow::OnSetColorSpace",
3224         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
3225     return result;
3226 }
3227 
OnSetWindowColorSpace(NativeEngine & engine,NativeCallbackInfo & info)3228 NativeValue* JsWindow::OnSetWindowColorSpace(NativeEngine& engine, NativeCallbackInfo& info)
3229 {
3230     WmErrorCode errCode = WmErrorCode::WM_OK;
3231     ColorSpace colorSpace = ColorSpace::COLOR_SPACE_DEFAULT;
3232     if (info.argc < 1) { // 1: params num
3233         WLOGFE("Argc is invalid: %{public}zu", info.argc);
3234         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3235     } else {
3236         NativeNumber* nativeType = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
3237         if (nativeType == nullptr) {
3238             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3239             WLOGFE("Failed to convert parameter to ColorSpace");
3240         } else {
3241             colorSpace = static_cast<ColorSpace>(static_cast<uint32_t>(*nativeType));
3242             if (colorSpace > ColorSpace::COLOR_SPACE_WIDE_GAMUT) {
3243                 WLOGFE("ColorSpace %{public}u invalid!", static_cast<uint32_t>(colorSpace));
3244                 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3245             }
3246         }
3247     }
3248     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
3249         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3250         return engine.CreateUndefined();
3251     }
3252 
3253     wptr<Window> weakToken(windowToken_);
3254     AsyncTask::CompleteCallback complete =
3255         [weakToken, colorSpace](NativeEngine& engine, AsyncTask& task, int32_t status) {
3256             auto weakWindow = weakToken.promote();
3257             if (weakWindow == nullptr) {
3258                 WLOGFE("window is nullptr");
3259                 task.Reject(engine,
3260                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY),
3261                     "OnSetWindowColorSpace failed"));
3262                 return;
3263             }
3264             weakWindow->SetColorSpace(colorSpace);
3265             task.Resolve(engine, engine.CreateUndefined());
3266             WLOGI("Window [%{public}u, %{public}s] OnSetWindowColorSpace end, colorSpace = %{public}u",
3267                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), static_cast<uint32_t>(colorSpace));
3268         };
3269 
3270     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
3271         ((info.argv[1] != nullptr && info.argv[1]->TypeOf() == NATIVE_FUNCTION) ?
3272         info.argv[1] : nullptr);
3273     NativeValue* result = nullptr;
3274     AsyncTask::Schedule("JsWindow::OnSetWindowColorSpace",
3275         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
3276     return result;
3277 }
3278 
OnGetColorSpace(NativeEngine & engine,NativeCallbackInfo & info)3279 NativeValue* JsWindow::OnGetColorSpace(NativeEngine& engine, NativeCallbackInfo& info)
3280 {
3281     WMError errCode = WMError::WM_OK;
3282     if (info.argc > 1) {
3283         WLOGFE("Argc is invalid: %{public}zu", info.argc);
3284         errCode = WMError::WM_ERROR_INVALID_PARAM;
3285     }
3286     wptr<Window> weakToken(windowToken_);
3287     AsyncTask::CompleteCallback complete =
3288         [weakToken, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
3289             auto weakWindow = weakToken.promote();
3290             if (weakWindow == nullptr) {
3291                 WLOGFE("window is nullptr");
3292                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
3293                 return;
3294             }
3295             if (errCode != WMError::WM_OK) {
3296                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
3297                 WLOGFE("window is nullptr or get invalid param");
3298                 return;
3299             }
3300             ColorSpace colorSpace = weakWindow->GetColorSpace();
3301             task.Resolve(engine, CreateJsValue(engine, static_cast<uint32_t>(colorSpace)));
3302             WLOGI("Window [%{public}u, %{public}s] OnGetColorSpace end, colorSpace = %{public}u",
3303                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), static_cast<uint32_t>(colorSpace));
3304         };
3305 
3306     NativeValue* lastParam = (info.argc == 0) ? nullptr :
3307         (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
3308     NativeValue* result = nullptr;
3309     AsyncTask::Schedule("JsWindow::OnGetColorSpace",
3310         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
3311     return result;
3312 }
3313 
OnGetWindowColorSpaceSync(NativeEngine & engine,NativeCallbackInfo & info)3314 NativeValue* JsWindow::OnGetWindowColorSpaceSync(NativeEngine& engine, NativeCallbackInfo& info)
3315 {
3316     wptr<Window> weakToken(windowToken_);
3317     auto window = weakToken.promote();
3318     if (window == nullptr) {
3319         WLOGFE("window is nullptr");
3320         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
3321         return engine.CreateUndefined();
3322     }
3323     ColorSpace colorSpace = window->GetColorSpace();
3324     WLOGI("Window [%{public}u, %{public}s] OnGetColorSpace end, colorSpace = %{public}u",
3325         window->GetWindowId(), window->GetWindowName().c_str(), static_cast<uint32_t>(colorSpace));
3326 
3327     return CreateJsValue(engine, static_cast<uint32_t>(colorSpace));
3328 }
3329 
OnDump(NativeEngine & engine,NativeCallbackInfo & info)3330 NativeValue* JsWindow::OnDump(NativeEngine& engine, NativeCallbackInfo& info)
3331 {
3332     WLOGI("dump window start");
3333     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
3334         WLOGFE("Argc is invalid: %{public}zu", info.argc);
3335         return nullptr;
3336     }
3337     if (windowToken_ == nullptr) {
3338         WLOGFE("window is nullptr or get invalid param");
3339         return nullptr;
3340     }
3341     std::vector<std::string> params;
3342     if (!ConvertNativeValueToVector(engine, info.argv[0], params)) {
3343         WLOGFE("ConvertNativeValueToVector fail");
3344         return nullptr;
3345     }
3346     std::vector<std::string> dumpInfo;
3347     windowToken_->DumpInfo(params, dumpInfo);
3348     NativeValue* dumpInfoValue = CreateNativeArray(engine, dumpInfo);
3349     WLOGI("Window [%{public}u, %{public}s] dump end",
3350         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
3351     return dumpInfoValue;
3352 }
3353 
Snapshot(NativeEngine * engine,NativeCallbackInfo * info)3354 NativeValue* JsWindow::Snapshot(NativeEngine* engine, NativeCallbackInfo* info)
3355 {
3356     WLOGI("Snapshot");
3357     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
3358     return (me != nullptr) ? me->OnSnapshot(*engine, *info) : nullptr;
3359 }
3360 
OnSetForbidSplitMove(NativeEngine & engine,NativeCallbackInfo & info)3361 NativeValue* JsWindow::OnSetForbidSplitMove(NativeEngine& engine, NativeCallbackInfo& info)
3362 {
3363     WmErrorCode errCode = WmErrorCode::WM_OK;
3364     if (info.argc < 1) { // 1: params num
3365         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3366     }
3367     bool isForbidSplitMove = false;
3368     if (errCode == WmErrorCode::WM_OK) {
3369         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
3370         if (nativeVal == nullptr) {
3371             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3372         } else {
3373             isForbidSplitMove = static_cast<bool>(*nativeVal);
3374         }
3375     }
3376     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
3377         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3378         return engine.CreateUndefined();
3379     }
3380     wptr<Window> weakToken(windowToken_);
3381     AsyncTask::CompleteCallback complete =
3382         [weakToken, isForbidSplitMove](NativeEngine& engine, AsyncTask& task, int32_t status) {
3383             auto weakWindow = weakToken.promote();
3384             if (weakWindow == nullptr) {
3385                 task.Reject(engine, CreateJsError(engine,
3386                     static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "Invalidate params."));
3387                 return;
3388             }
3389             WmErrorCode ret;
3390             if (isForbidSplitMove) {
3391                 ret = WM_JS_TO_ERROR_CODE_MAP.at(
3392                     weakWindow->AddWindowFlag(WindowFlag::WINDOW_FLAG_FORBID_SPLIT_MOVE));
3393             } else {
3394                 ret = WM_JS_TO_ERROR_CODE_MAP.at(
3395                     weakWindow->RemoveWindowFlag(WindowFlag::WINDOW_FLAG_FORBID_SPLIT_MOVE));
3396             }
3397             if (ret == WmErrorCode::WM_OK) {
3398                 task.Resolve(engine, engine.CreateUndefined());
3399             } else {
3400                 task.Reject(engine, CreateJsError(engine,
3401                     static_cast<int32_t>(ret), "Window OnSetForbidSplitMove failed."));
3402             }
3403         };
3404     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
3405         ((info.argv[1] != nullptr && info.argv[1]->TypeOf() == NATIVE_FUNCTION) ?
3406         info.argv[1] : nullptr);
3407     NativeValue* result = nullptr;
3408     AsyncTask::Schedule("JsWindow::OnSetForbidSplitMove",
3409         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
3410     return result;
3411 }
3412 
OnSnapshot(NativeEngine & engine,NativeCallbackInfo & info)3413 NativeValue* JsWindow::OnSnapshot(NativeEngine& engine, NativeCallbackInfo& info)
3414 {
3415     wptr<Window> weakToken(windowToken_);
3416     AsyncTask::CompleteCallback complete =
3417         [weakToken](NativeEngine& engine, AsyncTask& task, int32_t status) {
3418             auto weakWindow = weakToken.promote();
3419             if (weakWindow == nullptr) {
3420                 WLOGFE("window is nullptr");
3421                 task.Reject(engine, CreateJsError(engine,
3422                     static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
3423                 return;
3424             }
3425 
3426             std::shared_ptr<Media::PixelMap> pixelMap = weakWindow->Snapshot();
3427             if (pixelMap == nullptr) {
3428                 task.Reject(engine, CreateJsError(engine,
3429                     static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
3430                 WLOGFE("window snapshot get pixelmap is null");
3431                 return;
3432             }
3433 
3434             auto nativePixelMap = reinterpret_cast<NativeValue*>(
3435                 Media::PixelMapNapi::CreatePixelMap(reinterpret_cast<napi_env>(&engine), pixelMap));
3436             if (nativePixelMap == nullptr) {
3437                 WLOGFE("window snapshot get nativePixelMap is null");
3438             }
3439             task.Resolve(engine, nativePixelMap);
3440             WLOGI("Window [%{public}u, %{public}s] OnSnapshot, WxH=%{public}dx%{public}d",
3441                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(),
3442                 pixelMap->GetWidth(), pixelMap->GetHeight());
3443         };
3444 
3445     NativeValue* lastParam = (info.argc == 0) ? nullptr :
3446         ((info.argv[0] != nullptr && info.argv[0]->TypeOf() == NATIVE_FUNCTION) ?
3447         info.argv[0] : nullptr);
3448     NativeValue* result = nullptr;
3449     AsyncTask::Schedule("JsWindow::OnSnapshot",
3450         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
3451     return result;
3452 }
3453 
OnSetSnapshotSkip(NativeEngine & engine,NativeCallbackInfo & info)3454 NativeValue* JsWindow::OnSetSnapshotSkip(NativeEngine& engine, NativeCallbackInfo& info)
3455 {
3456     WmErrorCode errCode = WmErrorCode::WM_OK;
3457     if (info.argc < 1) { // 1: params num
3458         WLOGFE(" inbalid param");
3459         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3460     }
3461     bool isSkip = false;
3462     if (errCode == WmErrorCode::WM_OK) {
3463         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
3464         if (nativeVal == nullptr) {
3465             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
3466         } else {
3467             isSkip = static_cast<bool>(*nativeVal);
3468         }
3469     }
3470     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
3471         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3472         return engine.CreateUndefined();
3473     }
3474 
3475     wptr<Window> weakToken(windowToken_);
3476     auto window = weakToken.promote();
3477     if (window == nullptr) {
3478         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
3479         return engine.CreateUndefined();
3480     }
3481 
3482     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(window->SetSnapshotSkip(isSkip));
3483     if (ret != WmErrorCode::WM_OK) {
3484         WLOGFE("Window SetSnapshotSkip failed");
3485         engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
3486         return engine.CreateUndefined();
3487     }
3488     WLOGI("[%{public}u, %{public}s] set snapshotSkip end",
3489         window->GetWindowId(), window->GetWindowName().c_str());
3490 
3491     return engine.CreateUndefined();
3492 }
3493 
OnRaiseToAppTop(NativeEngine & engine,NativeCallbackInfo & info)3494 NativeValue* JsWindow::OnRaiseToAppTop(NativeEngine& engine, NativeCallbackInfo& info)
3495 {
3496     AsyncTask::CompleteCallback complete =
3497         [this](NativeEngine& engine, AsyncTask& task, int32_t status) {
3498             wptr<Window> weakToken(windowToken_);
3499             auto window = weakToken.promote();
3500             if (window == nullptr) {
3501                 WLOGFE("window is nullptr");
3502                 task.Reject(engine, CreateJsError(engine,
3503                     static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
3504                 return;
3505             }
3506 
3507             WmErrorCode errCode = window->RaiseToAppTop();
3508             if (errCode != WmErrorCode::WM_OK) {
3509                 WLOGFE("raise window zorder failed");
3510                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
3511                 return;
3512             }
3513             task.Resolve(engine, engine.CreateUndefined());
3514             WLOGI("Window [%{public}u, %{public}s] zorder raise success",
3515                 window->GetWindowId(), window->GetWindowName().c_str());
3516         };
3517     NativeValue* lastParam = (info.argc == 0) ? nullptr :
3518         ((info.argv[0] != nullptr && info.argv[0]->TypeOf() == NATIVE_FUNCTION) ?
3519         info.argv[0] : nullptr);
3520     NativeValue* result = nullptr;
3521     AsyncTask::Schedule("JsWindow::OnRaiseToAppTop",
3522         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
3523     return result;
3524 }
3525 
OnOpacity(NativeEngine & engine,NativeCallbackInfo & info)3526 NativeValue* JsWindow::OnOpacity(NativeEngine& engine, NativeCallbackInfo& info)
3527 {
3528     if (info.argc < 1) {
3529         WLOGFE("Argc is invalid: %{public}zu", info.argc);
3530         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3531         return engine.CreateUndefined();
3532     }
3533     if (windowToken_ == nullptr) {
3534         WLOGFE("WindowToken_ is nullptr");
3535         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
3536         return engine.CreateUndefined();
3537     }
3538     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
3539         WLOGFE("Opacity is not allowed since window is not system window");
3540         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING)));
3541         return engine.CreateUndefined();
3542     }
3543     NativeNumber* nativeVal = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
3544     if (nativeVal == nullptr) {
3545         WLOGFE("Failed to convert parameter to alpha");
3546         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3547         return engine.CreateUndefined();
3548     }
3549     if (MathHelper::LessNotEqual(*nativeVal, 0.0) || MathHelper::GreatNotEqual(*nativeVal, 1.0)) {
3550         WLOGFE("alpha should greater than 0 or smaller than 1.0");
3551         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3552         return engine.CreateUndefined();
3553     }
3554     float alpha = static_cast<double>(*nativeVal);
3555     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetAlpha(alpha));
3556     if (ret != WmErrorCode::WM_OK) {
3557         WLOGFE("Window Opacity failed");
3558         engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
3559         return engine.CreateUndefined();
3560     }
3561     WLOGI("Window [%{public}u, %{public}s] Opacity end, alpha = %{public}f",
3562         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), alpha);
3563     return engine.CreateUndefined();
3564 }
3565 
IsPivotValid(double data)3566 static bool IsPivotValid(double data)
3567 {
3568     if (MathHelper::LessNotEqual(data, 0.0) || (MathHelper::GreatNotEqual(data, 1.0))) {
3569         return false;
3570     }
3571     return true;
3572 }
3573 
IsScaleValid(double data)3574 static bool IsScaleValid(double data)
3575 {
3576     if (!MathHelper::GreatNotEqual(data, 0.0)) {
3577         return false;
3578     }
3579     return true;
3580 }
3581 
ParseScaleOption(NativeEngine & engine,NativeObject * jsObject,Transform & trans)3582 bool JsWindow::ParseScaleOption(NativeEngine& engine, NativeObject* jsObject, Transform& trans)
3583 {
3584     double data = 0.0f;
3585     if (ParseJsValue(jsObject, engine, "pivotX", data)) {
3586         if (!IsPivotValid(data)) {
3587             return false;
3588         }
3589         trans.pivotX_ = data;
3590     }
3591     if (ParseJsValue(jsObject, engine, "pivotY", data)) {
3592         if (!IsPivotValid(data)) {
3593             return false;
3594         }
3595         trans.pivotY_ = data;
3596     }
3597     if (ParseJsValue(jsObject, engine, "x", data)) {
3598         if (!IsScaleValid(data)) {
3599             return false;
3600         }
3601         trans.scaleX_ = data;
3602     }
3603     if (ParseJsValue(jsObject, engine, "y", data)) {
3604         if (!IsScaleValid(data)) {
3605             return false;
3606         }
3607         trans.scaleY_ = data;
3608     }
3609     return true;
3610 }
3611 
OnScale(NativeEngine & engine,NativeCallbackInfo & info)3612 NativeValue* JsWindow::OnScale(NativeEngine& engine, NativeCallbackInfo& info)
3613 {
3614     if (info.argc < 1) {
3615         WLOGFE("Argc is invalid: %{public}zu", info.argc);
3616         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3617         return engine.CreateUndefined();
3618     }
3619     if (windowToken_ == nullptr) {
3620         WLOGFE("WindowToken_ is nullptr");
3621         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
3622         return engine.CreateUndefined();
3623     }
3624     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
3625         WLOGFE("Scale is not allowed since window is not system window");
3626         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING)));
3627         return engine.CreateUndefined();
3628     }
3629     NativeObject* nativeObj = ConvertNativeValueTo<NativeObject>(info.argv[0]);
3630     if (nativeObj == nullptr) {
3631         WLOGFE("Failed to convert object to ScaleOptions");
3632         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3633         return engine.CreateUndefined();
3634     }
3635     auto trans = windowToken_->GetTransform();
3636     if (!ParseScaleOption(engine, nativeObj, trans)) {
3637         WLOGFE(" PivotX or PivotY should between 0.0 ~ 1.0, scale should greater than 0.0");
3638         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3639         return engine.CreateUndefined();
3640     }
3641     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetTransform(trans));
3642     if (ret != WmErrorCode::WM_OK) {
3643         WLOGFE("Window Scale failed");
3644         engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
3645         return engine.CreateUndefined();
3646     }
3647     WLOGI("Window [%{public}u, %{public}s] Scale end",
3648         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
3649     WLOGI("scaleX = %{public}f, scaleY = %{public}f, pivotX = %{public}f pivotY = %{public}f",
3650         trans.scaleX_, trans.scaleY_, trans.pivotX_, trans.pivotY_);
3651     return engine.CreateUndefined();
3652 }
3653 
ParseRotateOption(NativeEngine & engine,NativeObject * jsObject,Transform & trans)3654 bool JsWindow::ParseRotateOption(NativeEngine& engine, NativeObject* jsObject, Transform& trans)
3655 {
3656     double data = 0.0f;
3657     if (ParseJsValue(jsObject, engine, "pivotX", data)) {
3658         if (!IsPivotValid(data)) {
3659             return false;
3660         }
3661         trans.pivotX_ = data;
3662     }
3663     if (ParseJsValue(jsObject, engine, "pivotY", data)) {
3664         if (!IsPivotValid(data)) {
3665             return false;
3666         }
3667         trans.pivotY_ = data;
3668     }
3669     if (ParseJsValue(jsObject, engine, "x", data)) {
3670         trans.rotationX_ = data;
3671     }
3672     if (ParseJsValue(jsObject, engine, "y", data)) {
3673         trans.rotationY_ = data;
3674     }
3675     if (ParseJsValue(jsObject, engine, "z", data)) {
3676         trans.rotationZ_ = data;
3677     }
3678     return true;
3679 }
3680 
OnRotate(NativeEngine & engine,NativeCallbackInfo & info)3681 NativeValue* JsWindow::OnRotate(NativeEngine& engine, NativeCallbackInfo& info)
3682 {
3683     if (info.argc < 1) {
3684         WLOGFE("Argc is invalid: %{public}zu", info.argc);
3685         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3686         return engine.CreateUndefined();
3687     }
3688     if (windowToken_ == nullptr) {
3689         WLOGFE("WindowToken_ is nullptr");
3690         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
3691         return engine.CreateUndefined();
3692     }
3693     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
3694         WLOGFE("Rotate is not allowed since window is not system window");
3695         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING)));
3696         return engine.CreateUndefined();
3697     }
3698     NativeObject* nativeObj = ConvertNativeValueTo<NativeObject>(info.argv[0]);
3699     if (nativeObj == nullptr) {
3700         WLOGFE("Failed to convert object to RotateOptions");
3701         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3702         return engine.CreateUndefined();
3703     }
3704     // cannot use sync task since next transform base on current transform
3705     auto trans = windowToken_->GetTransform();
3706     if (!ParseRotateOption(engine, nativeObj, trans)) {
3707         WLOGFE(" PivotX or PivotY should between 0.0 ~ 1.0");
3708         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3709         return engine.CreateUndefined();
3710     }
3711     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetTransform(trans));
3712     if (ret != WmErrorCode::WM_OK) {
3713         WLOGFE("Window Rotate failed");
3714         engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
3715         return engine.CreateUndefined();
3716     }
3717     WLOGI("Window [%{public}u, %{public}s] Rotate end",
3718         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
3719     WLOGI("rotateX = %{public}f, rotateY = %{public}f," \
3720         "rotateZ = %{public}f pivotX = %{public}f pivotY = %{public}f",
3721         trans.rotationX_, trans.rotationY_, trans.rotationZ_, trans.pivotX_, trans.pivotY_);
3722     return engine.CreateUndefined();
3723 }
3724 
ParseTranslateOption(NativeEngine & engine,NativeObject * jsObject,Transform & trans)3725 bool JsWindow::ParseTranslateOption(NativeEngine& engine, NativeObject* jsObject, Transform& trans)
3726 {
3727     double data = 0.0f;
3728     if (ParseJsValue(jsObject, engine, "x", data)) {
3729         trans.translateX_ = data;
3730     }
3731     if (ParseJsValue(jsObject, engine, "y", data)) {
3732         trans.translateY_ = data;
3733     }
3734     if (ParseJsValue(jsObject, engine, "z", data)) {
3735         trans.translateZ_ = data;
3736     }
3737     return true;
3738 }
3739 
OnTranslate(NativeEngine & engine,NativeCallbackInfo & info)3740 NativeValue* JsWindow::OnTranslate(NativeEngine& engine, NativeCallbackInfo& info)
3741 {
3742     if (info.argc < 1) {
3743         WLOGFE("Argc is invalid: %{public}zu", info.argc);
3744         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3745         return engine.CreateUndefined();
3746     }
3747     if (windowToken_ == nullptr) {
3748         WLOGFE("WindowToken_ is nullptr");
3749         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
3750         return engine.CreateUndefined();
3751     }
3752     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
3753         WLOGFE("Translate is not allowed since window is not system window");
3754         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING)));
3755         return engine.CreateUndefined();
3756     }
3757     NativeObject* nativeObj = ConvertNativeValueTo<NativeObject>(info.argv[0]);
3758     if (nativeObj == nullptr) {
3759         WLOGFE("Failed to convert object to TranslateOptions");
3760         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3761         return engine.CreateUndefined();
3762     }
3763     auto trans = windowToken_->GetTransform();
3764     if (!ParseTranslateOption(engine, nativeObj, trans)) {
3765         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3766         return engine.CreateUndefined();
3767     }
3768     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetTransform(trans));
3769     if (ret != WmErrorCode::WM_OK) {
3770         WLOGFE("Window Translate failed");
3771         engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
3772         return engine.CreateUndefined();
3773     }
3774     WLOGI("Window [%{public}u, %{public}s] Translate end," \
3775         "translateX = %{public}f, translateY = %{public}f, translateZ = %{public}f",
3776         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(),
3777         trans.translateX_, trans.translateY_, trans.translateZ_);
3778     return engine.CreateUndefined();
3779 }
3780 
CreateTransitionController(NativeEngine & engine)3781 WmErrorCode JsWindow::CreateTransitionController(NativeEngine& engine)
3782 {
3783     if (windowToken_ == nullptr) {
3784         WLOGFE("windowToken_ is nullptr not match");
3785         return WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
3786     }
3787     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
3788         WLOGFE("CreateTransitionController is not allowed since window is not system window");
3789         return WmErrorCode::WM_ERROR_INVALID_CALLING;
3790     }
3791     NativeValue* objValue = engine.CreateObject();
3792     auto name = GetWindowName();
3793     std::shared_ptr<NativeReference> jsWindowObj = FindJsWindowObject(name);
3794     if (jsWindowObj == nullptr || jsWindowObj->Get() == nullptr) {
3795         return WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
3796     }
3797     sptr<JsTransitionController> nativeController = new JsTransitionController(
3798         engine, jsWindowObj, windowToken_);
3799     NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
3800     if (object == nullptr) {
3801         WLOGFE("Failed to convert to TransitionController Object");
3802         return WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
3803     }
3804     object->SetNativePointer(new wptr<JsTransitionController>(nativeController),
3805         [](NativeEngine*, void* data, void*) {
3806             WLOGFE("Finalizer for wptr form native Transition Controller is called");
3807             delete static_cast<wptr<JsTransitionController>*>(data);
3808         }, nullptr);
3809     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->RegisterAnimationTransitionController(nativeController));
3810     jsTransControllerObj_.reset(engine.CreateReference(objValue, 1));
3811     nativeController->SetJsController(jsTransControllerObj_);
3812     WLOGI("Window [%{public}u, %{public}s] CreateTransitionController end",
3813         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
3814     return ret;
3815 }
3816 
OnGetTransitionController(NativeEngine & engine,NativeCallbackInfo & info)3817 NativeValue* JsWindow::OnGetTransitionController(NativeEngine& engine, NativeCallbackInfo& info)
3818 {
3819     if (windowToken_ == nullptr) {
3820         WLOGFE("WindowToken_ is nullptr");
3821         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
3822         return engine.CreateUndefined();
3823     }
3824     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
3825         WLOGFE("OnGetTransitionController is not allowed since window is not system window");
3826         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING)));
3827         return engine.CreateUndefined();
3828     }
3829     if (jsTransControllerObj_ == nullptr || jsTransControllerObj_->Get() == nullptr) {
3830         WmErrorCode ret = CreateTransitionController(engine);
3831         if (ret != WmErrorCode::WM_OK) {
3832             WLOGFE("Window GetTransitionController failed");
3833             engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
3834         }
3835     }
3836     return jsTransControllerObj_ == nullptr ? nullptr : jsTransControllerObj_->Get();
3837 }
3838 
OnSetCornerRadius(NativeEngine & engine,NativeCallbackInfo & info)3839 NativeValue* JsWindow::OnSetCornerRadius(NativeEngine& engine, NativeCallbackInfo& info)
3840 {
3841     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
3842         WLOGFE("set corner radius permission denied!");
3843         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP)));
3844         return engine.CreateUndefined();
3845     }
3846     if (info.argc < 1) {
3847         WLOGFE("Argc is invalid: %{public}zu", info.argc);
3848         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3849         return engine.CreateUndefined();
3850     }
3851     if (windowToken_ == nullptr) {
3852         WLOGFE("WindowToken_ is nullptr");
3853         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
3854         return engine.CreateUndefined();
3855     }
3856     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
3857         WLOGFE("SetCornerRadius is not allowed since window is not system window");
3858         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING)));
3859         return engine.CreateUndefined();
3860     }
3861     NativeNumber* nativeVal = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
3862     if (nativeVal == nullptr || MathHelper::LessNotEqual(static_cast<double>(*nativeVal), 0.0)) {
3863         WLOGFE("SetCornerRadius invalid radius");
3864         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3865         return engine.CreateUndefined();
3866     }
3867     float radius = static_cast<double>(*nativeVal);
3868     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetCornerRadius(radius));
3869     if (ret != WmErrorCode::WM_OK) {
3870         WLOGFE("Window SetCornerRadius failed");
3871         engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
3872         return engine.CreateUndefined();
3873     }
3874     WLOGI("Window [%{public}u, %{public}s] SetCornerRadius end, radius = %{public}f",
3875         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), radius);
3876     return engine.CreateUndefined();
3877 }
3878 
OnSetShadow(NativeEngine & engine,NativeCallbackInfo & info)3879 NativeValue* JsWindow::OnSetShadow(NativeEngine& engine, NativeCallbackInfo& info)
3880 {
3881     WmErrorCode ret = WmErrorCode::WM_OK;
3882     if (info.argc < 1) { // 1: min param num
3883         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3884         return engine.CreateUndefined();
3885     }
3886     if (windowToken_ == nullptr) {
3887         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
3888         return engine.CreateUndefined();
3889     }
3890     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
3891         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING)));
3892         return engine.CreateUndefined();
3893     }
3894 
3895     { // parse the 1st param: radius
3896         NativeNumber* nativeVal = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
3897         if (nativeVal == nullptr || MathHelper::LessNotEqual(static_cast<double>(*nativeVal), 0.0)) {
3898             engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3899             return engine.CreateUndefined();
3900         }
3901         ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetShadowRadius(static_cast<double>(*nativeVal)));
3902     }
3903 
3904     if ((ret == WmErrorCode::WM_OK) && (info.argc >= 2)) { // parse the 2nd param: color
3905         std::string color;
3906         if (ConvertFromJsValue(engine, info.argv[1], color)) {
3907             ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetShadowColor(color));
3908         }
3909     }
3910 
3911     if ((ret == WmErrorCode::WM_OK) && info.argc >= 3) { // parse the 3rd param: offsetX
3912         NativeNumber* nativeVal = ConvertNativeValueTo<NativeNumber>(info.argv[2]); // 2: the 3rd param
3913         if (nativeVal != nullptr) {
3914             ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetShadowOffsetX(static_cast<double>(*nativeVal)));
3915         }
3916     }
3917 
3918     if ((ret == WmErrorCode::WM_OK) && info.argc >= 4) { // parse the 4th param: offsetY
3919         NativeNumber* nativeVal = ConvertNativeValueTo<NativeNumber>(info.argv[3]); // 3: the 4th param
3920         if (nativeVal != nullptr) {
3921             ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetShadowOffsetY(static_cast<double>(*nativeVal)));
3922         }
3923     }
3924 
3925     if (ret != WmErrorCode::WM_OK) {
3926         engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
3927     }
3928 
3929     return engine.CreateUndefined();
3930 }
3931 
OnSetBlur(NativeEngine & engine,NativeCallbackInfo & info)3932 NativeValue* JsWindow::OnSetBlur(NativeEngine& engine, NativeCallbackInfo& info)
3933 {
3934     if (info.argc < 1) {
3935         WLOGFE("Argc is invalid: %{public}zu", info.argc);
3936         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3937         return engine.CreateUndefined();
3938     }
3939     if (windowToken_ == nullptr) {
3940         WLOGFE("WindowToken_ is nullptr");
3941         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
3942         return engine.CreateUndefined();
3943     }
3944     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
3945         WLOGFE("SetBlur is not allowed since window is not system window");
3946         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING)));
3947         return engine.CreateUndefined();
3948     }
3949     NativeNumber* nativeVal = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
3950     if (nativeVal == nullptr || MathHelper::LessNotEqual(static_cast<double>(*nativeVal), 0.0)) {
3951         WLOGFE("SetBlur invalid radius");
3952         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3953         return engine.CreateUndefined();
3954     }
3955     float radius = static_cast<double>(*nativeVal);
3956     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetBlur(radius));
3957     if (ret != WmErrorCode::WM_OK) {
3958         WLOGFE("Window SetBlur failed");
3959         engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
3960         return engine.CreateUndefined();
3961     }
3962     WLOGI("Window [%{public}u, %{public}s] SetBlur end, radius = %{public}f",
3963         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), radius);
3964     return engine.CreateUndefined();
3965 }
3966 
OnSetBackdropBlur(NativeEngine & engine,NativeCallbackInfo & info)3967 NativeValue* JsWindow::OnSetBackdropBlur(NativeEngine& engine, NativeCallbackInfo& info)
3968 {
3969     if (info.argc < 1) {
3970         WLOGFE("Argc is invalid: %{public}zu", info.argc);
3971         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3972         return engine.CreateUndefined();
3973     }
3974     if (windowToken_ == nullptr) {
3975         WLOGFE("WindowToken_ is nullptr");
3976         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
3977         return engine.CreateUndefined();
3978     }
3979     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
3980         WLOGFE("SetBackdropBlur is not allowed since window is not system window");
3981         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING)));
3982         return engine.CreateUndefined();
3983     }
3984     NativeNumber* nativeVal = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
3985     if (nativeVal == nullptr || MathHelper::LessNotEqual(static_cast<double>(*nativeVal), 0.0)) {
3986         WLOGFE("SetBackdropBlur invalid radius");
3987         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
3988         return engine.CreateUndefined();
3989     }
3990     float radius = static_cast<double>(*nativeVal);
3991     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetBackdropBlur(radius));
3992     if (ret != WmErrorCode::WM_OK) {
3993         WLOGFE("Window SetBackdropBlur failed");
3994         engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
3995         return engine.CreateUndefined();
3996     }
3997     WLOGI("Window [%{public}u, %{public}s] SetBackdropBlur end, radius = %{public}f",
3998         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), radius);
3999     return engine.CreateUndefined();
4000 }
4001 
OnSetBackdropBlurStyle(NativeEngine & engine,NativeCallbackInfo & info)4002 NativeValue* JsWindow::OnSetBackdropBlurStyle(NativeEngine& engine, NativeCallbackInfo& info)
4003 {
4004     if (info.argc < 1) {
4005         WLOGFE("Argc is invalid: %{public}zu", info.argc);
4006         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
4007         return engine.CreateUndefined();
4008     }
4009     if (windowToken_ == nullptr) {
4010         WLOGFE("WindowToken_ is nullptr");
4011         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
4012         return engine.CreateUndefined();
4013     }
4014     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
4015         WLOGFE("SetBackdropBlurStyle is not allowed since window is not system window");
4016         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING)));
4017         return engine.CreateUndefined();
4018     }
4019 
4020     NativeNumber* nativeMode = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
4021     if (nativeMode == nullptr ||
4022         static_cast<uint32_t>(*nativeMode) > static_cast<uint32_t>(WindowBlurStyle::WINDOW_BLUR_THICK)) {
4023         WLOGFE("SetBackdropBlurStyle Invalid window blur style");
4024         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
4025         return engine.CreateUndefined();
4026     }
4027     WindowBlurStyle style = static_cast<WindowBlurStyle>(static_cast<uint32_t>(*nativeMode));
4028     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetBackdropBlurStyle(style));
4029     if (ret != WmErrorCode::WM_OK) {
4030         WLOGFE("Window SetBackdropBlurStyle failed");
4031         engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
4032         return engine.CreateUndefined();
4033     }
4034 
4035     WLOGI("Window [%{public}u, %{public}s] SetBackdropBlurStyle end, style = %{public}u",
4036         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), style);
4037     return engine.CreateUndefined();
4038 }
4039 
OnSetWaterMarkFlag(NativeEngine & engine,NativeCallbackInfo & info)4040 NativeValue* JsWindow::OnSetWaterMarkFlag(NativeEngine& engine, NativeCallbackInfo& info)
4041 {
4042     if (info.argc < 1) {
4043         WLOGFE("Argc is invalid: %{public}zu", info.argc);
4044         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
4045         return engine.CreateUndefined();
4046     }
4047 
4048     NativeBoolean* nativeBool = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
4049     if (nativeBool == nullptr) {
4050         WLOGFE("SetWaterMarkFlag Invalid window flag");
4051         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
4052         return engine.CreateUndefined();
4053     }
4054 
4055     bool isAddSafetyLayer = static_cast<bool>(*nativeBool);
4056     wptr<Window> weakToken(windowToken_);
4057     AsyncTask::CompleteCallback complete =
4058         [weakToken, isAddSafetyLayer](NativeEngine& engine, AsyncTask& task, int32_t status) {
4059             auto window = weakToken.promote();
4060             if (window == nullptr) {
4061                 task.Reject(engine,
4062                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY),
4063                     "OnSetWaterMarkFlag failed."));
4064                 return;
4065             }
4066             WMError ret = WMError::WM_OK;
4067             if (isAddSafetyLayer) {
4068                 ret = window->AddWindowFlag(WindowFlag::WINDOW_FLAG_WATER_MARK);
4069             } else {
4070                 ret = window->RemoveWindowFlag(WindowFlag::WINDOW_FLAG_WATER_MARK);
4071             }
4072             if (ret == WMError::WM_OK) {
4073                 task.Resolve(engine, engine.CreateUndefined());
4074             } else {
4075                 task.Reject(engine, CreateJsError(engine,
4076                     static_cast<int32_t>(WM_JS_TO_ERROR_CODE_MAP.at(ret)), "SetWaterMarkFlag failed."));
4077             }
4078             WLOGI("[NAPI]Window [%{public}u, %{public}s] set waterMark flag end, ret = %{public}d",
4079                 window->GetWindowId(), window->GetWindowName().c_str(), ret);
4080         };
4081 
4082     NativeValue* lastParam = (info.argc == 1) ? nullptr :
4083         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
4084     NativeValue* result = nullptr;
4085     AsyncTask::Schedule("JsWindow::OnSetWaterMarkFlag",
4086         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
4087     return result;
4088 }
4089 
4090 
OnSetAspectRatio(NativeEngine & engine,NativeCallbackInfo & info)4091 NativeValue* JsWindow::OnSetAspectRatio(NativeEngine& engine, NativeCallbackInfo& info)
4092 {
4093     WMError errCode = WMError::WM_OK;
4094     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
4095         WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
4096         errCode = WMError::WM_ERROR_INVALID_PARAM;
4097     }
4098 
4099     if (!WindowHelper::IsMainWindow(windowToken_->GetType())) {
4100         WLOGFE("[NAPI]SetAspectRatio is not allowed since window is main window");
4101         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING)));
4102         return engine.CreateUndefined();
4103     }
4104 
4105     float aspectRatio = 0.0;
4106     if (errCode == WMError::WM_OK) {
4107         NativeNumber* nativeVal = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
4108         if (nativeVal == nullptr) {
4109             errCode = WMError::WM_ERROR_INVALID_PARAM;
4110         } else {
4111             aspectRatio = static_cast<double>(*nativeVal);
4112         }
4113     }
4114 
4115     if (errCode == WMError::WM_ERROR_INVALID_PARAM || aspectRatio <= 0.0) {
4116         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
4117         return engine.CreateUndefined();
4118     }
4119 
4120     wptr<Window> weakToken(windowToken_);
4121     AsyncTask::CompleteCallback complete =
4122         [weakToken, aspectRatio](NativeEngine& engine, AsyncTask& task, int32_t status) {
4123             auto weakWindow = weakToken.promote();
4124             if (weakWindow == nullptr) {
4125                 task.Reject(engine,
4126                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY),
4127                     "OnSetAspectRatio failed."));
4128                 return;
4129             }
4130             WMError ret = weakWindow->SetAspectRatio(aspectRatio);
4131             if (ret == WMError::WM_OK) {
4132                 task.Resolve(engine, engine.CreateUndefined());
4133             } else {
4134                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "SetAspectRatio failed."));
4135             }
4136             WLOGI("[NAPI]Window [%{public}u, %{public}s] set aspect ratio end, ret = %{public}d",
4137                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
4138         };
4139 
4140     NativeValue* lastParam = (info.argc == 1) ? nullptr :
4141         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
4142     NativeValue* result = nullptr;
4143     AsyncTask::Schedule("JsWindow::SetAspectRatio",
4144         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
4145     return result;
4146 }
4147 
OnResetAspectRatio(NativeEngine & engine,NativeCallbackInfo & info)4148 NativeValue* JsWindow::OnResetAspectRatio(NativeEngine& engine, NativeCallbackInfo& info)
4149 {
4150     if (info.argc > 1) {
4151         WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
4152         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
4153         return engine.CreateUndefined();
4154     }
4155 
4156     if (!WindowHelper::IsMainWindow(windowToken_->GetType())) {
4157         WLOGFE("[NAPI]ResetAspectRatio is not allowed since window is main window");
4158         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING)));
4159         return engine.CreateUndefined();
4160     }
4161 
4162     wptr<Window> weakToken(windowToken_);
4163     AsyncTask::CompleteCallback complete =
4164         [weakToken](NativeEngine& engine, AsyncTask& task, int32_t status) {
4165             auto weakWindow = weakToken.promote();
4166             if (weakWindow == nullptr) {
4167                 task.Reject(engine,
4168                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY),
4169                     "OnResetAspectRatio failed."));
4170                 return;
4171             }
4172             WMError ret = weakWindow->ResetAspectRatio();
4173             if (ret == WMError::WM_OK) {
4174                 task.Resolve(engine, engine.CreateUndefined());
4175             } else {
4176                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "ResetAspectRatio failed."));
4177             }
4178             WLOGI("[NAPI]Window [%{public}u, %{public}s] reset aspect ratio end, ret = %{public}d",
4179                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
4180         };
4181 
4182     NativeValue* lastParam = (info.argc == 0) ? nullptr :
4183         (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
4184     NativeValue* result = nullptr;
4185     AsyncTask::Schedule("JsWindow::OnResetAspectRatio",
4186         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
4187     return result;
4188 }
4189 
OnMinimize(NativeEngine & engine,NativeCallbackInfo & info)4190 NativeValue* JsWindow::OnMinimize(NativeEngine& engine, NativeCallbackInfo& info)
4191 {
4192     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
4193         WLOGFE("minimize the window permission denied!");
4194         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP)));
4195         return engine.CreateUndefined();
4196     }
4197 
4198     if (info.argc > 1) {
4199         WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
4200         engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
4201         return engine.CreateUndefined();
4202     }
4203 
4204     wptr<Window> weakToken(windowToken_);
4205     AsyncTask::CompleteCallback complete =
4206         [weakToken](NativeEngine& engine, AsyncTask& task, int32_t status) {
4207             auto weakWindow = weakToken.promote();
4208             if (weakWindow == nullptr) {
4209                 task.Reject(engine,
4210                     CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY),
4211                     "OnMinimize failed."));
4212                 WLOGFE("window is nullptr");
4213                 return;
4214             }
4215             WMError ret = weakWindow->Minimize();
4216             if (ret == WMError::WM_OK) {
4217                 task.Resolve(engine, engine.CreateUndefined());
4218             } else {
4219                 WmErrorCode wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(ret);
4220                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(wmErrorCode), "Minimize failed."));
4221             }
4222             WLOGI("[NAPI]Window [%{public}u, %{public}s] minimize end, ret = %{public}d",
4223                 weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
4224         };
4225 
4226     NativeValue* lastParam = (info.argc == 0) ? nullptr :
4227         (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
4228     NativeValue* result = nullptr;
4229     AsyncTask::Schedule("JsWindow::OnMinimize",
4230         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
4231     return result;
4232 }
4233 
FindJsWindowObject(std::string windowName)4234 std::shared_ptr<NativeReference> FindJsWindowObject(std::string windowName)
4235 {
4236     WLOGFD("Try to find window %{public}s in g_jsWindowMap", windowName.c_str());
4237     std::lock_guard<std::recursive_mutex> lock(g_mutex);
4238     if (g_jsWindowMap.find(windowName) == g_jsWindowMap.end()) {
4239         WLOGFD("Can not find window %{public}s in g_jsWindowMap", windowName.c_str());
4240         return nullptr;
4241     }
4242     return g_jsWindowMap[windowName];
4243 }
4244 
CreateJsWindowObject(NativeEngine & engine,sptr<Window> & window)4245 NativeValue* CreateJsWindowObject(NativeEngine& engine, sptr<Window>& window)
4246 {
4247     std::string windowName = window->GetWindowName();
4248     // avoid repeatedly create js window when getWindow
4249     std::shared_ptr<NativeReference> jsWindowObj = FindJsWindowObject(windowName);
4250     if (jsWindowObj != nullptr && jsWindowObj->Get() != nullptr) {
4251         WLOGI("FindJsWindowObject %{public}s", windowName.c_str());
4252         return jsWindowObj->Get();
4253     }
4254     NativeValue* objValue = engine.CreateObject();
4255     NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
4256 
4257     WLOGI("CreateJsWindow %{public}s", windowName.c_str());
4258     std::unique_ptr<JsWindow> jsWindow = std::make_unique<JsWindow>(window);
4259     object->SetNativePointer(jsWindow.release(), JsWindow::Finalizer, nullptr);
4260 
4261     BindFunctions(engine, object, "JsWindow");
4262 
4263     std::shared_ptr<NativeReference> jsWindowRef;
4264     jsWindowRef.reset(engine.CreateReference(objValue, 1));
4265     std::lock_guard<std::recursive_mutex> lock(g_mutex);
4266     g_jsWindowMap[windowName] = jsWindowRef;
4267     return objValue;
4268 }
4269 
BindFunctions(NativeEngine & engine,NativeObject * object,const char * moduleName)4270 void BindFunctions(NativeEngine& engine, NativeObject* object, const char *moduleName)
4271 {
4272     BindNativeFunction(engine, *object, "show", moduleName, JsWindow::Show);
4273     BindNativeFunction(engine, *object, "showWindow", moduleName, JsWindow::ShowWindow);
4274     BindNativeFunction(engine, *object, "showWithAnimation", moduleName, JsWindow::ShowWithAnimation);
4275     BindNativeFunction(engine, *object, "destroy", moduleName, JsWindow::Destroy);
4276     BindNativeFunction(engine, *object, "destroyWindow", moduleName, JsWindow::DestroyWindow);
4277     BindNativeFunction(engine, *object, "hide", moduleName, JsWindow::Hide);
4278     BindNativeFunction(engine, *object, "hideWithAnimation", moduleName, JsWindow::HideWithAnimation);
4279     BindNativeFunction(engine, *object, "moveTo", moduleName, JsWindow::MoveTo);
4280     BindNativeFunction(engine, *object, "moveWindowTo", moduleName, JsWindow::MoveWindowTo);
4281     BindNativeFunction(engine, *object, "resetSize", moduleName, JsWindow::Resize);
4282     BindNativeFunction(engine, *object, "resize", moduleName, JsWindow::ResizeWindow);
4283     BindNativeFunction(engine, *object, "setWindowType", moduleName, JsWindow::SetWindowType);
4284     BindNativeFunction(engine, *object, "setWindowMode", moduleName, JsWindow::SetWindowMode);
4285     BindNativeFunction(engine, *object, "getProperties", moduleName, JsWindow::GetProperties);
4286     BindNativeFunction(engine, *object, "getWindowProperties", moduleName, JsWindow::GetWindowPropertiesSync);
4287     BindNativeFunction(engine, *object, "on", moduleName, JsWindow::RegisterWindowCallback);
4288     BindNativeFunction(engine, *object, "off", moduleName, JsWindow::UnregisterWindowCallback);
4289     BindNativeFunction(engine, *object, "bindDialogTarget", moduleName, JsWindow::BindDialogTarget);
4290     BindNativeFunction(engine, *object, "loadContent", moduleName, JsWindow::LoadContent);
4291     BindNativeFunction(engine, *object, "getUIContext", moduleName, JsWindow::GetUIContext);
4292     BindNativeFunction(engine, *object, "setUIContent", moduleName, JsWindow::SetUIContent);
4293     BindNativeFunction(engine, *object, "setFullScreen", moduleName, JsWindow::SetFullScreen);
4294     BindNativeFunction(engine, *object, "setLayoutFullScreen", moduleName, JsWindow::SetLayoutFullScreen);
4295     BindNativeFunction(engine, *object, "setWindowLayoutFullScreen", moduleName, JsWindow::SetWindowLayoutFullScreen);
4296     BindNativeFunction(engine, *object, "setSystemBarEnable", moduleName, JsWindow::SetSystemBarEnable);
4297     BindNativeFunction(engine, *object, "setWindowSystemBarEnable", moduleName, JsWindow::SetWindowSystemBarEnable);
4298     BindNativeFunction(engine, *object, "setSystemBarProperties", moduleName, JsWindow::SetSystemBarProperties);
4299     BindNativeFunction(engine, *object, "setWindowSystemBarProperties",
4300         moduleName, JsWindow::SetWindowSystemBarProperties);
4301     BindNativeFunction(engine, *object, "getAvoidArea", moduleName, JsWindow::GetAvoidArea);
4302     BindNativeFunction(engine, *object, "getWindowAvoidArea", moduleName, JsWindow::GetWindowAvoidAreaSync);
4303     BindNativeFunction(engine, *object, "isShowing", moduleName, JsWindow::IsShowing);
4304     BindNativeFunction(engine, *object, "isWindowShowing", moduleName, JsWindow::IsWindowShowingSync);
4305     BindNativeFunction(engine, *object, "isSupportWideGamut", moduleName, JsWindow::IsSupportWideGamut);
4306     BindNativeFunction(engine, *object, "isWindowSupportWideGamut", moduleName, JsWindow::IsWindowSupportWideGamut);
4307     BindNativeFunction(engine, *object, "setColorSpace", moduleName, JsWindow::SetColorSpace);
4308     BindNativeFunction(engine, *object, "setWindowColorSpace", moduleName, JsWindow::SetWindowColorSpace);
4309     BindNativeFunction(engine, *object, "getColorSpace", moduleName, JsWindow::GetColorSpace);
4310     BindNativeFunction(engine, *object, "getWindowColorSpace", moduleName, JsWindow::GetWindowColorSpaceSync);
4311     BindNativeFunction(engine, *object, "setBackgroundColor", moduleName, JsWindow::SetBackgroundColor);
4312     BindNativeFunction(engine, *object, "setWindowBackgroundColor", moduleName, JsWindow::SetWindowBackgroundColorSync);
4313     BindNativeFunction(engine, *object, "setBrightness", moduleName, JsWindow::SetBrightness);
4314     BindNativeFunction(engine, *object, "setWindowBrightness", moduleName, JsWindow::SetWindowBrightness);
4315     BindNativeFunction(engine, *object, "setDimBehind", moduleName, JsWindow::SetDimBehind);
4316     BindNativeFunction(engine, *object, "setFocusable", moduleName, JsWindow::SetFocusable);
4317     BindNativeFunction(engine, *object, "setWindowFocusable", moduleName, JsWindow::SetWindowFocusable);
4318     BindNativeFunction(engine, *object, "setKeepScreenOn", moduleName, JsWindow::SetKeepScreenOn);
4319     BindNativeFunction(engine, *object, "setWindowKeepScreenOn", moduleName, JsWindow::SetWindowKeepScreenOn);
4320     BindNativeFunction(engine, *object, "setWakeUpScreen", moduleName, JsWindow::SetWakeUpScreen);
4321     BindNativeFunction(engine, *object, "setOutsideTouchable", moduleName, JsWindow::SetOutsideTouchable);
4322     BindNativeFunction(engine, *object, "setPrivacyMode", moduleName, JsWindow::SetPrivacyMode);
4323     BindNativeFunction(engine, *object, "setWindowPrivacyMode", moduleName, JsWindow::SetWindowPrivacyMode);
4324     BindNativeFunction(engine, *object, "setTouchable", moduleName, JsWindow::SetTouchable);
4325     BindNativeFunction(engine, *object, "setWindowTouchable", moduleName, JsWindow::SetWindowTouchable);
4326     BindNativeFunction(engine, *object, "setTransparent", moduleName, JsWindow::SetTransparent);
4327     BindNativeFunction(engine, *object, "setCallingWindow", moduleName, JsWindow::SetCallingWindow);
4328     BindNativeFunction(engine, *object, "setSnapshotSkip", moduleName, JsWindow::SetSnapshotSkip);
4329     BindNativeFunction(engine, *object, "raiseToAppTop", moduleName, JsWindow::RaiseToAppTop);
4330     BindNativeFunction(engine, *object, "disableWindowDecor", moduleName, JsWindow::DisableWindowDecor);
4331     BindNativeFunction(engine, *object, "dump", moduleName, JsWindow::Dump);
4332     BindNativeFunction(engine, *object, "setForbidSplitMove", moduleName, JsWindow::SetForbidSplitMove);
4333     BindNativeFunction(engine, *object, "setPreferredOrientation", moduleName, JsWindow::SetPreferredOrientation);
4334     BindNativeFunction(engine, *object, "opacity", moduleName, JsWindow::Opacity);
4335     BindNativeFunction(engine, *object, "scale", moduleName, JsWindow::Scale);
4336     BindNativeFunction(engine, *object, "rotate", moduleName, JsWindow::Rotate);
4337     BindNativeFunction(engine, *object, "translate", moduleName, JsWindow::Translate);
4338     BindNativeFunction(engine, *object, "getTransitionController", moduleName, JsWindow::GetTransitionController);
4339     BindNativeFunction(engine, *object, "snapshot", moduleName, JsWindow::Snapshot);
4340     BindNativeFunction(engine, *object, "setCornerRadius", moduleName, JsWindow::SetCornerRadius);
4341     BindNativeFunction(engine, *object, "setShadow", moduleName, JsWindow::SetShadow);
4342     BindNativeFunction(engine, *object, "setBlur", moduleName, JsWindow::SetBlur);
4343     BindNativeFunction(engine, *object, "setBackdropBlur", moduleName, JsWindow::SetBackdropBlur);
4344     BindNativeFunction(engine, *object, "setBackdropBlurStyle", moduleName, JsWindow::SetBackdropBlurStyle);
4345     BindNativeFunction(engine, *object, "setAspectRatio", moduleName, JsWindow::SetAspectRatio);
4346     BindNativeFunction(engine, *object, "resetAspectRatio", moduleName, JsWindow::ResetAspectRatio);
4347     BindNativeFunction(engine, *object, "setWaterMarkFlag", moduleName, JsWindow::SetWaterMarkFlag);
4348     BindNativeFunction(engine, *object, "minimize", moduleName, JsWindow::Minimize);
4349     BindNativeFunction(engine, *object, "setResizeByDragEnabled", moduleName, JsWindow::SetResizeByDragEnabled);
4350     BindNativeFunction(engine, *object, "setRaiseByClickEnabled", moduleName, JsWindow::SetRaiseByClickEnabled);
4351     BindNativeFunction(engine, *object, "raiseAboveTarget", moduleName, JsWindow::RaiseAboveTarget);
4352 }
4353 }  // namespace Rosen
4354 }  // namespace OHOS
4355