• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include "window.h"
19 #include "window_manager_hilog.h"
20 #include "window_option.h"
21 namespace OHOS {
22 namespace Rosen {
23 using namespace AbilityRuntime;
24 namespace {
25     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "JsWindow"};
26     constexpr Rect g_emptyRect = {0, 0, 0, 0};
27 }
28 
29 static thread_local std::map<std::string, std::shared_ptr<NativeReference>> g_jsWindowMap;
30 std::recursive_mutex g_mutex;
JsWindow(const sptr<Window> & window)31 JsWindow::JsWindow(const sptr<Window>& window)
32     : windowToken_(window), registerManager_(std::make_unique<JsWindowRegisterManager>())
33 {
34     NotifyNativeWinDestroyFunc func = [](std::string windowName) {
35         WLOGE("NotifyNativeWinDestroyFunc is called %{public}s", windowName.c_str());
36         std::lock_guard<std::recursive_mutex> lock(g_mutex);
37         if (windowName.empty() || g_jsWindowMap.count(windowName) == 0) {
38             WLOGE("windowName not exist %{public}s", windowName.c_str());
39             return;
40         }
41         g_jsWindowMap.erase(windowName);
42         WLOGFI("JsWindow::NotifyNativeWinDestroyFuncwindowName %{public}s is destroyed", windowName.c_str());
43     };
44     windowToken_->RegisterWindowDestroyedListener(func);
45 }
46 
~JsWindow()47 JsWindow::~JsWindow()
48 {
49     WLOGFI("JsWindow::~JsWindow is called");
50     windowToken_ = nullptr;
51 }
52 
GetWindowName()53 std::string JsWindow::GetWindowName()
54 {
55     if (windowToken_ == nullptr) {
56         return "";
57     }
58     return windowToken_->GetWindowName();
59 }
60 
Finalizer(NativeEngine * engine,void * data,void * hint)61 void JsWindow::Finalizer(NativeEngine* engine, void* data, void* hint)
62 {
63     WLOGFI("JsWindow::Finalizer is called");
64     auto jsWin = std::unique_ptr<JsWindow>(static_cast<JsWindow*>(data));
65     if (jsWin == nullptr) {
66         WLOGFE("JsWindow::Finalizer JsWindow is null");
67         return;
68     }
69     std::string windowName = jsWin->GetWindowName();
70     WLOGFI("JsWindow::Finalizer windowName : %{public}s", windowName.c_str());
71     std::lock_guard<std::recursive_mutex> lock(g_mutex);
72     if (g_jsWindowMap.find(windowName) != g_jsWindowMap.end()) {
73         WLOGFI("JsWindow::windowName %{public}s is destroyed", windowName.c_str());
74         g_jsWindowMap.erase(windowName);
75     }
76 }
77 
Show(NativeEngine * engine,NativeCallbackInfo * info)78 NativeValue* JsWindow::Show(NativeEngine* engine, NativeCallbackInfo* info)
79 {
80     WLOGFI("JsWindow::Show is called");
81     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
82     return (me != nullptr) ? me->OnShow(*engine, *info) : nullptr;
83 }
84 
Destroy(NativeEngine * engine,NativeCallbackInfo * info)85 NativeValue* JsWindow::Destroy(NativeEngine* engine, NativeCallbackInfo* info)
86 {
87     WLOGFI("JsWindow::Destroy is called");
88     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
89     return (me != nullptr) ? me->OnDestroy(*engine, *info) : nullptr;
90 }
91 
Hide(NativeEngine * engine,NativeCallbackInfo * info)92 NativeValue* JsWindow::Hide(NativeEngine* engine, NativeCallbackInfo* info)
93 {
94     WLOGFI("JsWindow::Hide is called");
95     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
96     return (me != nullptr) ? me->OnHide(*engine, *info) : nullptr;
97 }
98 
MoveTo(NativeEngine * engine,NativeCallbackInfo * info)99 NativeValue* JsWindow::MoveTo(NativeEngine* engine, NativeCallbackInfo* info)
100 {
101     WLOGFI("JsWindow::MoveTo is called");
102     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
103     return (me != nullptr) ? me->OnMoveTo(*engine, *info) : nullptr;
104 }
105 
Resize(NativeEngine * engine,NativeCallbackInfo * info)106 NativeValue* JsWindow::Resize(NativeEngine* engine, NativeCallbackInfo* info)
107 {
108     WLOGFI("JsWindow::Resize is called");
109     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
110     return (me != nullptr) ? me->OnResize(*engine, *info) : nullptr;
111 }
112 
SetWindowType(NativeEngine * engine,NativeCallbackInfo * info)113 NativeValue* JsWindow::SetWindowType(NativeEngine* engine, NativeCallbackInfo* info)
114 {
115     WLOGFI("JsWindow::SetWindowType is called");
116     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
117     return (me != nullptr) ? me->OnSetWindowType(*engine, *info) : nullptr;
118 }
119 
SetWindowMode(NativeEngine * engine,NativeCallbackInfo * info)120 NativeValue* JsWindow::SetWindowMode(NativeEngine* engine, NativeCallbackInfo* info)
121 {
122     WLOGFI("JsWindow::SetWindowMode is called");
123     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
124     return (me != nullptr) ? me->OnSetWindowMode(*engine, *info) : nullptr;
125 }
126 
GetProperties(NativeEngine * engine,NativeCallbackInfo * info)127 NativeValue* JsWindow::GetProperties(NativeEngine* engine, NativeCallbackInfo* info)
128 {
129     WLOGFI("JsWindow::GetProperties is called");
130     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
131     return (me != nullptr) ? me->OnGetProperties(*engine, *info) : nullptr;
132 }
133 
RegisterWindowCallback(NativeEngine * engine,NativeCallbackInfo * info)134 NativeValue* JsWindow::RegisterWindowCallback(NativeEngine* engine, NativeCallbackInfo* info)
135 {
136     WLOGFI("JsWindow::RegisterWindowCallback is called");
137     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
138     return (me != nullptr) ? me->OnRegisterWindowCallback(*engine, *info) : nullptr;
139 }
140 
UnregisterWindowCallback(NativeEngine * engine,NativeCallbackInfo * info)141 NativeValue* JsWindow::UnregisterWindowCallback(NativeEngine* engine, NativeCallbackInfo* info)
142 {
143     WLOGFI("JsWindow::UnregisterWindowCallback is called");
144     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
145     return (me != nullptr) ? me->OnUnregisterWindowCallback(*engine, *info) : nullptr;
146 }
147 
LoadContent(NativeEngine * engine,NativeCallbackInfo * info)148 NativeValue* JsWindow::LoadContent(NativeEngine* engine, NativeCallbackInfo* info)
149 {
150     WLOGFI("JsWindow::LoadContent is called");
151     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
152     return (me != nullptr) ? me->OnLoadContent(*engine, *info) : nullptr;
153 }
154 
SetFullScreen(NativeEngine * engine,NativeCallbackInfo * info)155 NativeValue* JsWindow::SetFullScreen(NativeEngine* engine, NativeCallbackInfo* info)
156 {
157     WLOGFI("JsWindow::SetFullScreen is called");
158     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
159     return (me != nullptr) ? me->OnSetFullScreen(*engine, *info) : nullptr;
160 }
161 
SetLayoutFullScreen(NativeEngine * engine,NativeCallbackInfo * info)162 NativeValue* JsWindow::SetLayoutFullScreen(NativeEngine* engine, NativeCallbackInfo* info)
163 {
164     WLOGFI("JsWindow::SetLayoutFullScreen is called");
165     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
166     return (me != nullptr) ? me->OnSetLayoutFullScreen(*engine, *info) : nullptr;
167 }
168 
SetSystemBarEnable(NativeEngine * engine,NativeCallbackInfo * info)169 NativeValue* JsWindow::SetSystemBarEnable(NativeEngine* engine, NativeCallbackInfo* info)
170 {
171     WLOGFI("JsWindow::SetSystemBarEnable is called");
172     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
173     return (me != nullptr) ? me->OnSetSystemBarEnable(*engine, *info) : nullptr;
174 }
175 
SetSystemBarProperties(NativeEngine * engine,NativeCallbackInfo * info)176 NativeValue* JsWindow::SetSystemBarProperties(NativeEngine* engine, NativeCallbackInfo* info)
177 {
178     WLOGFI("JsWindow::SetSystemBarProperties is called");
179     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
180     return (me != nullptr) ? me->OnSetSystemBarProperties(*engine, *info) : nullptr;
181 }
182 
GetAvoidArea(NativeEngine * engine,NativeCallbackInfo * info)183 NativeValue* JsWindow::GetAvoidArea(NativeEngine* engine, NativeCallbackInfo* info)
184 {
185     WLOGFI("JsWindow::GetAvoidArea is called");
186     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
187     return (me != nullptr) ? me->OnGetAvoidArea(*engine, *info) : nullptr;
188 }
189 
IsShowing(NativeEngine * engine,NativeCallbackInfo * info)190 NativeValue* JsWindow::IsShowing(NativeEngine* engine, NativeCallbackInfo* info)
191 {
192     WLOGFI("JsWindow::IsShowing is called");
193     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
194     return (me != nullptr) ? me->OnIsShowing(*engine, *info) : nullptr;
195 }
196 
IsSupportWideGamut(NativeEngine * engine,NativeCallbackInfo * info)197 NativeValue* JsWindow::IsSupportWideGamut(NativeEngine* engine, NativeCallbackInfo* info)
198 {
199     WLOGFI("JsWindow::IsSupportWideGamut is called");
200     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
201     return (me != nullptr) ? me->OnIsSupportWideGamut(*engine, *info) : nullptr;
202 }
203 
SetBackgroundColor(NativeEngine * engine,NativeCallbackInfo * info)204 NativeValue* JsWindow::SetBackgroundColor(NativeEngine* engine, NativeCallbackInfo* info)
205 {
206     WLOGFI("JsWindow::SetBackgroundColor is called");
207     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
208     return (me != nullptr) ? me->OnSetBackgroundColor(*engine, *info) : nullptr;
209 }
210 
SetBrightness(NativeEngine * engine,NativeCallbackInfo * info)211 NativeValue* JsWindow::SetBrightness(NativeEngine* engine, NativeCallbackInfo* info)
212 {
213     WLOGFI("JsWindow::SetBrightness is called");
214     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
215     return (me != nullptr) ? me->OnSetBrightness(*engine, *info) : nullptr;
216 }
217 
SetDimBehind(NativeEngine * engine,NativeCallbackInfo * info)218 NativeValue* JsWindow::SetDimBehind(NativeEngine* engine, NativeCallbackInfo* info)
219 {
220     WLOGFI("JsWindow::SetDimBehind is called");
221     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
222     return (me != nullptr) ? me->OnSetDimBehind(*engine, *info) : nullptr;
223 }
224 
SetFocusable(NativeEngine * engine,NativeCallbackInfo * info)225 NativeValue* JsWindow::SetFocusable(NativeEngine* engine, NativeCallbackInfo* info)
226 {
227     WLOGFI("JsWindow::SetFocusable is called");
228     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
229     return (me != nullptr) ? me->OnSetFocusable(*engine, *info) : nullptr;
230 }
231 
SetKeepScreenOn(NativeEngine * engine,NativeCallbackInfo * info)232 NativeValue* JsWindow::SetKeepScreenOn(NativeEngine* engine, NativeCallbackInfo* info)
233 {
234     WLOGFI("JsWindow::SetkeepScreenOn is called");
235     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
236     return (me != nullptr) ? me->OnSetKeepScreenOn(*engine, *info) : nullptr;
237 }
238 
SetOutsideTouchable(NativeEngine * engine,NativeCallbackInfo * info)239 NativeValue* JsWindow::SetOutsideTouchable(NativeEngine* engine, NativeCallbackInfo* info)
240 {
241     WLOGFI("JsWindow::SetOutsideTouchable is called");
242     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
243     return (me != nullptr) ? me->OnSetOutsideTouchable(*engine, *info) : nullptr;
244 }
245 
SetPrivacyMode(NativeEngine * engine,NativeCallbackInfo * info)246 NativeValue* JsWindow::SetPrivacyMode(NativeEngine* engine, NativeCallbackInfo* info)
247 {
248     WLOGFI("JsWindow::SetPrivacyMode is called");
249     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
250     return (me != nullptr) ? me->OnSetPrivacyMode(*engine, *info) : nullptr;
251 }
252 
SetTouchable(NativeEngine * engine,NativeCallbackInfo * info)253 NativeValue* JsWindow::SetTouchable(NativeEngine* engine, NativeCallbackInfo* info)
254 {
255     WLOGFI("JsWindow::SetTouchable is called");
256     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
257     return (me != nullptr) ? me->OnSetTouchable(*engine, *info) : nullptr;
258 }
259 
SetCallingWindow(NativeEngine * engine,NativeCallbackInfo * info)260 NativeValue* JsWindow::SetCallingWindow(NativeEngine* engine, NativeCallbackInfo* info)
261 {
262     WLOGFI("[NAPI]SetCallingWindow");
263     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
264     return (me != nullptr) ? me->OnSetCallingWindow(*engine, *info) : nullptr;
265 }
266 
SetColorSpace(NativeEngine * engine,NativeCallbackInfo * info)267 NativeValue* JsWindow::SetColorSpace(NativeEngine* engine, NativeCallbackInfo* info)
268 {
269     WLOGFI("JsWindow::SetColorSpace is called");
270     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
271     return (me != nullptr) ? me->OnSetColorSpace(*engine, *info) : nullptr;
272 }
273 
GetColorSpace(NativeEngine * engine,NativeCallbackInfo * info)274 NativeValue* JsWindow::GetColorSpace(NativeEngine* engine, NativeCallbackInfo* info)
275 {
276     WLOGFI("JsWindow::GetColorSpace is called");
277     JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
278     return (me != nullptr) ? me->OnGetColorSpace(*engine, *info) : nullptr;
279 }
280 
OnShow(NativeEngine & engine,NativeCallbackInfo & info)281 NativeValue* JsWindow::OnShow(NativeEngine& engine, NativeCallbackInfo& info)
282 {
283     WLOGFI("JsWindow::OnShow is called");
284     WMError errCode = WMError::WM_OK;
285     if (info.argc > 1) {
286         WLOGFE("JsWindow params not match");
287         errCode = WMError::WM_ERROR_INVALID_PARAM;
288     }
289     AsyncTask::CompleteCallback complete =
290         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
291             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
292                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
293                 WLOGFE("JsWindow windowToken_ is nullptr or invalid param");
294                 return;
295             }
296             WMError ret = windowToken_->Show();
297             if (ret == WMError::WM_OK) {
298                 task.Resolve(engine, engine.CreateUndefined());
299                 WLOGFI("JsWindow::OnShow success");
300             } else {
301                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "ShowWindow failed."));
302             }
303         };
304 
305     NativeValue* lastParam = (info.argc == 0) ? nullptr :
306         (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
307     NativeValue* result = nullptr;
308     AsyncTask::Schedule(
309         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
310     return result;
311 }
312 
OnDestroy(NativeEngine & engine,NativeCallbackInfo & info)313 NativeValue* JsWindow::OnDestroy(NativeEngine& engine, NativeCallbackInfo& info)
314 {
315     WLOGFI("JsWindow::OnDestroy is called");
316     WMError errCode = WMError::WM_OK;
317     if (info.argc > 1) {
318         WLOGFE("JsWindow params not match");
319         errCode = WMError::WM_ERROR_INVALID_PARAM;
320     }
321     AsyncTask::CompleteCallback complete =
322         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
323             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
324                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
325                 WLOGFE("JsWindow windowToken_ is nullptr or invalid param");
326                 return;
327             }
328             WMError ret = windowToken_->Destroy();
329             if (ret != WMError::WM_OK) {
330                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "JsWindow::OnDestroy failed."));
331                 return;
332             }
333             windowToken_ = nullptr;
334             task.Resolve(engine, engine.CreateUndefined());
335             WLOGFI("JsWindow::OnDestroy success");
336         };
337 
338     NativeValue* lastParam = (info.argc == 0) ? nullptr :
339         (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
340     NativeValue* result = nullptr;
341     AsyncTask::Schedule(
342         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
343     return result;
344 }
345 
OnHide(NativeEngine & engine,NativeCallbackInfo & info)346 NativeValue* JsWindow::OnHide(NativeEngine& engine, NativeCallbackInfo& info)
347 {
348     WLOGFI("JsWindow::OnHide is called");
349     WMError errCode = WMError::WM_OK;
350     if (info.argc > 1) {
351         WLOGFE("JsWindow params not match");
352         errCode = WMError::WM_ERROR_INVALID_PARAM;
353     }
354     AsyncTask::CompleteCallback complete =
355         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
356             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
357                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
358                 WLOGFE("JsWindow windowToken_ is nullptr or invalid param");
359                 return;
360             }
361             WMError ret = windowToken_->Hide();
362             if (ret == WMError::WM_OK) {
363                 task.Resolve(engine, engine.CreateUndefined());
364                 WLOGFI("JsWindow::OnHide success");
365             } else {
366                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "JsWindow::OnHide failed."));
367             }
368         };
369 
370     NativeValue* lastParam = (info.argc == 0) ? nullptr :
371         (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
372     NativeValue* result = nullptr;
373     AsyncTask::Schedule(
374         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
375     return result;
376 }
377 
OnMoveTo(NativeEngine & engine,NativeCallbackInfo & info)378 NativeValue* JsWindow::OnMoveTo(NativeEngine& engine, NativeCallbackInfo& info)
379 {
380     WLOGFI("JsWindow::OnMoveTo is called");
381     WMError errCode = WMError::WM_OK;
382     if (info.argc < 2 || info.argc > 3) { // 2:minimum param num, 3: maximum param num
383         WLOGFE("JsWindow params not match");
384         errCode = WMError::WM_ERROR_INVALID_PARAM;
385     }
386     int32_t x = 0;
387     if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[0], x)) {
388         WLOGFE("Failed to convert parameter to x");
389         errCode = WMError::WM_ERROR_INVALID_PARAM;
390     }
391 
392     int32_t y = 0;
393     if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[1], y)) {
394         WLOGFE("Failed to convert parameter to y");
395         errCode = WMError::WM_ERROR_INVALID_PARAM;
396     }
397     AsyncTask::CompleteCallback complete =
398         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
399             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
400                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
401                 WLOGFE("JsWindow windowToken_ is nullptr or invalid param");
402                 return;
403             }
404             WMError ret = windowToken_->MoveTo(x, y);
405             if (ret == WMError::WM_OK) {
406                 task.Resolve(engine, engine.CreateUndefined());
407                 WLOGFI("JsWindow::OnMoveTo success");
408             } else {
409                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "JsWindow::OnMoveTo failed."));
410             }
411         };
412     // 2: params num; 2: index of callback
413     NativeValue* lastParam = (info.argc <= 2) ? nullptr :
414         (info.argv[2]->TypeOf() == NATIVE_FUNCTION ? info.argv[2] : nullptr);
415     NativeValue* result = nullptr;
416     AsyncTask::Schedule(
417         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
418     return result;
419 }
420 
OnResize(NativeEngine & engine,NativeCallbackInfo & info)421 NativeValue* JsWindow::OnResize(NativeEngine& engine, NativeCallbackInfo& info)
422 {
423     WLOGFI("JsWindow::OnResize is called");
424     WMError errCode = WMError::WM_OK;
425     if (info.argc < 2 || info.argc > 3) { // 2: minimum param num, 3: maximum param num
426         WLOGFE("JsWindow windowToken_ is nullptr or params not match");
427         errCode = WMError::WM_ERROR_INVALID_PARAM;
428     }
429     int32_t width = 0;
430     if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[0], width)) {
431         WLOGFE("Failed to convert parameter to width");
432         errCode = WMError::WM_ERROR_INVALID_PARAM;
433     }
434 
435     int32_t height = 0;
436     if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[1], height)) {
437         WLOGFE("Failed to convert parameter to height");
438         errCode = WMError::WM_ERROR_INVALID_PARAM;
439     }
440     if (width <= 0 || height <= 0) {
441         WLOGFE("[NAPI]width or height should greater than 0!");
442         errCode = WMError::WM_ERROR_INVALID_PARAM;
443     }
444     AsyncTask::CompleteCallback complete =
445         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
446             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
447                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
448                 WLOGFE("JsWindow windowToken_ is nullptr or invalid param");
449                 return;
450             }
451             WMError ret = windowToken_->Resize(static_cast<uint32_t>(width), static_cast<uint32_t>(height));
452             if (ret == WMError::WM_OK) {
453                 task.Resolve(engine, engine.CreateUndefined());
454             } else {
455                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "JsWindow::OnResize failed."));
456             }
457         };
458     // 2: params num; 2: index of callback
459     NativeValue* lastParam = (info.argc <= 2) ? nullptr :
460         (info.argv[2]->TypeOf() == NATIVE_FUNCTION ? info.argv[2] : nullptr);
461     NativeValue* result = nullptr;
462     AsyncTask::Schedule(
463         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
464     return result;
465 }
466 
OnSetWindowType(NativeEngine & engine,NativeCallbackInfo & info)467 NativeValue* JsWindow::OnSetWindowType(NativeEngine& engine, NativeCallbackInfo& info)
468 {
469     WLOGFI("JsWindow::OnSetWindowType is called");
470     WMError errCode = WMError::WM_OK;
471     if (info.argc < 1 || info.argc > 2) { // 2 is max num of argc
472         WLOGFE("JsWindow windowToken_ is nullptr or params not match");
473         errCode = WMError::WM_ERROR_INVALID_PARAM;
474     }
475     WindowType winType = WindowType::SYSTEM_WINDOW_BASE;
476     if (errCode == WMError::WM_OK) {
477         NativeNumber* nativeType = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
478         if (nativeType == nullptr) {
479             WLOGFE("Failed to convert parameter to windowType");
480             errCode = WMError::WM_ERROR_INVALID_PARAM;
481         } else if (static_cast<uint32_t>(*nativeType) >= static_cast<uint32_t>(WindowType::SYSTEM_WINDOW_BASE)) {
482             winType = static_cast<WindowType>(static_cast<uint32_t>(*nativeType)); // adapt to the old version
483         } else {
484             if (JS_TO_NATIVE_WINDOW_TYPE_MAP.count(
485                 static_cast<ApiWindowType>(static_cast<uint32_t>(*nativeType))) != 0) {
486                 winType = JS_TO_NATIVE_WINDOW_TYPE_MAP.at(
487                     static_cast<ApiWindowType>(static_cast<uint32_t>(*nativeType)));
488             } else {
489                 WLOGFE("Do not surppot this type");
490                 errCode = WMError::WM_ERROR_INVALID_PARAM;
491             }
492         }
493     }
494 
495     AsyncTask::CompleteCallback complete =
496         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
497             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
498                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
499                 WLOGFE("JsWindow windowToken_ is nullptr or invalid param");
500                 return;
501             }
502             WMError ret = windowToken_->SetWindowType(winType);
503             if (ret == WMError::WM_OK) {
504                 task.Resolve(engine, engine.CreateUndefined());
505                 WLOGFI("JsWindow::OnSetWindowType success");
506             } else {
507                 task.Reject(engine, CreateJsError(engine,
508                     static_cast<int32_t>(ret), "JsWindow::OnSetWindowType failed."));
509             }
510         };
511 
512     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
513         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
514     NativeValue* result = nullptr;
515     AsyncTask::Schedule(
516         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
517     return result;
518 }
519 
OnSetWindowMode(NativeEngine & engine,NativeCallbackInfo & info)520 NativeValue* JsWindow::OnSetWindowMode(NativeEngine& engine, NativeCallbackInfo& info)
521 {
522     WLOGFI("JsWindow::OnSetWindowMode is called");
523     WMError errCode = WMError::WM_OK;
524     if (info.argc < 1 || info.argc > 2) { // 2 is max num of argc
525         WLOGFE("JsWindow windowToken_ is nullptr or params not match");
526         errCode = WMError::WM_ERROR_INVALID_PARAM;
527     }
528     WindowMode winMode = WindowMode::WINDOW_MODE_FULLSCREEN;
529     if (errCode == WMError::WM_OK) {
530         NativeNumber* nativeMode = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
531         if (nativeMode == nullptr) {
532             WLOGFE("Failed to convert parameter to windowMode");
533             errCode = WMError::WM_ERROR_INVALID_PARAM;
534         } else {
535             if (static_cast<uint32_t>(*nativeMode) >= static_cast<uint32_t>(WindowMode::WINDOW_MODE_SPLIT_PRIMARY)) {
536                 winMode = static_cast<WindowMode>(static_cast<uint32_t>(*nativeMode));
537             } else if (static_cast<uint32_t>(*nativeMode) >= static_cast<uint32_t>(ApiWindowMode::UNDEFINED) &&
538                 static_cast<uint32_t>(*nativeMode) <= static_cast<uint32_t>(ApiWindowMode::MODE_END)) {
539                 winMode = JS_TO_NATIVE_WINDOW_MODE_MAP.at(
540                     static_cast<ApiWindowMode>(static_cast<uint32_t>(*nativeMode)));
541             } else {
542                 WLOGFE("Do not surppot this type");
543                 errCode = WMError::WM_ERROR_INVALID_PARAM;
544             }
545         }
546     }
547 
548     AsyncTask::CompleteCallback complete =
549         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
550             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
551                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
552                 WLOGFE("JsWindow windowToken_ is nullptr or invalid param");
553                 return;
554             }
555             WMError ret = windowToken_->SetWindowMode(winMode);
556             if (ret == WMError::WM_OK) {
557                 task.Resolve(engine, engine.CreateUndefined());
558                 WLOGFI("JsWindow::OnSetWindowMode success");
559             } else {
560                 task.Reject(engine,
561                     CreateJsError(engine, static_cast<int32_t>(ret), "JsWindow::OnSetWindowMode failed."));
562             }
563         };
564 
565     NativeValue* lastParam = (info.argc == 1) ? nullptr :
566         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
567     NativeValue* result = nullptr;
568     AsyncTask::Schedule(
569         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
570     return result;
571 }
572 
OnGetProperties(NativeEngine & engine,NativeCallbackInfo & info)573 NativeValue* JsWindow::OnGetProperties(NativeEngine& engine, NativeCallbackInfo& info)
574 {
575     WLOGFI("JsWindow::OnGetProperties is called");
576     WMError errCode = WMError::WM_OK;
577     if (info.argc > 1) {
578         WLOGFE("JsWindow params not match");
579         errCode = WMError::WM_ERROR_INVALID_PARAM;
580     }
581     AsyncTask::CompleteCallback complete =
582         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
583             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
584                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
585                 WLOGFE("JsWindow windowToken_ is nullptr or invalid param");
586                 return;
587             }
588             auto objValue = CreateJsWindowPropertiesObject(engine, windowToken_);
589             WLOGFI("JsWindow::OnGetProperties objValue %{public}p", objValue);
590             if (objValue != nullptr) {
591                 task.Resolve(engine, objValue);
592                 WLOGFI("JsWindow::OnGetProperties success");
593             } else {
594                 task.Reject(engine, CreateJsError(engine,
595                     static_cast<int32_t>(WMError::WM_ERROR_NULLPTR), "JsWindow::OnGetProperties failed."));
596             }
597         };
598 
599     NativeValue* lastParam = (info.argc == 0) ? nullptr :
600         (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
601     NativeValue* result = nullptr;
602     AsyncTask::Schedule(
603         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
604     return result;
605 }
606 
OnRegisterWindowCallback(NativeEngine & engine,NativeCallbackInfo & info)607 NativeValue* JsWindow::OnRegisterWindowCallback(NativeEngine& engine, NativeCallbackInfo& info)
608 
609 {
610     WLOGFI("JsWindow::OnRegisterWindowCallback is called");
611     if (windowToken_ == nullptr) {
612         WLOGFE("JsWindow windowToken_ is nullptr");
613         return engine.CreateUndefined();
614     }
615     if (info.argc != 2) { // 2: params num
616         WLOGFE("Params not match");
617         return engine.CreateUndefined();
618     }
619     std::string cbType;
620     if (!ConvertFromJsValue(engine, info.argv[0], cbType)) {
621         WLOGFE("Failed to convert parameter to callbackType");
622         return engine.CreateUndefined();
623     }
624     NativeValue* value = info.argv[1];
625     if (!value->IsCallable()) {
626         WLOGFI("JsWindow::OnRegisterWindowCallback info->argv[1] is not callable");
627         return engine.CreateUndefined();
628     }
629     registerManager_->RegisterListener(windowToken_, cbType, CaseType::CASE_WINDOW, engine, value);
630     return engine.CreateUndefined();
631 }
632 
OnUnregisterWindowCallback(NativeEngine & engine,NativeCallbackInfo & info)633 NativeValue* JsWindow::OnUnregisterWindowCallback(NativeEngine& engine, NativeCallbackInfo& info)
634 {
635     WLOGFI("JsWindow::OnUnregisterWindowCallback is called");
636     if (windowToken_ == nullptr || info.argc < 1 || info.argc > 2) { // 2: maximum params nums
637         WLOGFE("JsWindow windowToken_ is nullptr or params not match");
638         return engine.CreateUndefined();
639     }
640     std::string cbType;
641     if (!ConvertFromJsValue(engine, info.argv[0], cbType)) {
642         WLOGFE("Failed to convert parameter to callbackType");
643         return engine.CreateUndefined();
644     }
645 
646     NativeValue* value = nullptr;
647     if (info.argc == 2) { // 2: maximum params nums
648         value = info.argv[1];
649         if (!value->IsCallable()) {
650             WLOGFI("JsWindow::OnUnregisterWindowManagerCallback info->argv[1] is not callable");
651             return engine.CreateUndefined();
652         }
653     }
654     registerManager_->UnregisterListener(windowToken_, cbType, CaseType::CASE_WINDOW, value);
655 
656     WLOGFI("[NAPI]Unregister end, window [%{public}u, %{public}s], type = %{public}s, callback = %{public}p",
657         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), cbType.c_str(), value);
658     return engine.CreateUndefined();
659 }
660 
OnLoadContent(NativeEngine & engine,NativeCallbackInfo & info)661 NativeValue* JsWindow::OnLoadContent(NativeEngine& engine, NativeCallbackInfo& info)
662 {
663     WLOGFI("JsWindow::OnLoadContent is called");
664     WMError errCode = WMError::WM_OK;
665     if (info.argc < 1 || info.argc > 3) { // 3 maximum param num
666         WLOGFE("JsWindow windowToken_ is nullptr or params not match");
667         errCode = WMError::WM_ERROR_INVALID_PARAM;
668     }
669     std::string contextUrl;
670     if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[0], contextUrl)) {
671         WLOGFE("Failed to convert parameter to context url");
672         errCode = WMError::WM_ERROR_INVALID_PARAM;
673     }
674     NativeValue* storage = nullptr;
675     NativeValue* callBack = nullptr;
676     if (info.argc == 2) { // 2: num of params
677         NativeValue* value = info.argv[1];
678         if (value->TypeOf() == NATIVE_FUNCTION) {
679             callBack = info.argv[1];
680         } else {
681             storage = info.argv[1];
682         }
683     } else if (info.argc == 3) { // 3: num of params
684         storage = info.argv[1];
685         // 2: index of callback
686         callBack = (info.argv[2]->TypeOf() == NATIVE_FUNCTION ? info.argv[2] : nullptr);
687     }
688     std::shared_ptr<NativeReference> contentStorage = (storage == nullptr) ? nullptr :
689         std::shared_ptr<NativeReference>(engine.CreateReference(storage, 1));
690     AsyncTask::CompleteCallback complete =
691         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
692             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
693                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
694                 WLOGFE("JsWindow windowToken_ is nullptr or invalid param");
695                 return;
696             }
697             NativeValue* nativeStorage = (contentStorage == nullptr) ? nullptr : contentStorage->Get();
698             AppExecFwk::Ability* ability = nullptr;
699             GetAPI7Ability(engine, ability);
700             WMError ret = windowToken_->SetUIContent(contextUrl, &engine, nativeStorage, false, ability);
701             if (ret == WMError::WM_OK) {
702                 task.Resolve(engine, engine.CreateUndefined());
703                 WLOGFI("JsWindow::OnLoadContent success");
704             } else {
705                 task.Reject(engine,
706                     CreateJsError(engine, static_cast<int32_t>(ret), "JsWindow::OnLoadContent failed."));
707             }
708         };
709     NativeValue* result = nullptr;
710     AsyncTask::Schedule(
711         engine, CreateAsyncTaskWithLastParam(engine, callBack, nullptr, std::move(complete), &result));
712     return result;
713 }
714 
OnSetFullScreen(NativeEngine & engine,NativeCallbackInfo & info)715 NativeValue* JsWindow::OnSetFullScreen(NativeEngine& engine, NativeCallbackInfo& info)
716 {
717     WLOGFI("JsWindow::OnSetFullScreen is called");
718     WMError errCode = WMError::WM_OK;
719     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
720         WLOGFE("JsWindow param not match!");
721         errCode = WMError::WM_ERROR_INVALID_PARAM;
722     }
723     bool isFullScreen = false;
724     if (errCode == WMError::WM_OK) {
725         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
726         if (nativeVal == nullptr) {
727             WLOGFE("Failed to convert parameter to isFullScreen");
728             errCode = WMError::WM_ERROR_INVALID_PARAM;
729         } else {
730             isFullScreen = static_cast<bool>(*nativeVal);
731         }
732     }
733 
734     AsyncTask::CompleteCallback complete =
735         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
736             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
737                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
738                 return;
739             }
740             WMError ret = windowToken_->SetFullScreen(isFullScreen);
741             if (ret == WMError::WM_OK) {
742                 task.Resolve(engine, engine.CreateUndefined());
743                 WLOGFI("JsWindow::OnSetFullScreen success");
744             } else {
745                 task.Reject(engine, CreateJsError(engine,
746                     static_cast<int32_t>(ret), "JsWindow::OnSetFullScreen failed."));
747             }
748         };
749 
750     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
751         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
752     NativeValue* result = nullptr;
753     AsyncTask::Schedule(
754         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
755     return result;
756 }
757 
OnSetLayoutFullScreen(NativeEngine & engine,NativeCallbackInfo & info)758 NativeValue* JsWindow::OnSetLayoutFullScreen(NativeEngine& engine, NativeCallbackInfo& info)
759 {
760     WLOGFI("JsWindow::OnSetLayoutFullScreen is called");
761     WMError errCode = WMError::WM_OK;
762     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
763         WLOGFE("JsWindow param not match!");
764         errCode = WMError::WM_ERROR_INVALID_PARAM;
765     }
766     bool isLayoutFullScreen = false;
767     if (errCode == WMError::WM_OK) {
768         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
769         if (nativeVal == nullptr) {
770             WLOGFE("Failed to convert parameter to isLayoutFullScreen");
771             errCode = WMError::WM_ERROR_INVALID_PARAM;
772         } else {
773             isLayoutFullScreen = static_cast<bool>(*nativeVal);
774         }
775     }
776     AsyncTask::CompleteCallback complete =
777         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
778             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
779                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
780                 return;
781             }
782             WMError ret = windowToken_->SetLayoutFullScreen(isLayoutFullScreen);
783             if (ret == WMError::WM_OK) {
784                 task.Resolve(engine, engine.CreateUndefined());
785                 WLOGFI("JsWindow::OnSetLayoutFullScreen success");
786             } else {
787                 task.Reject(engine, CreateJsError(engine,
788                     static_cast<int32_t>(ret), "JsWindow::OnSetLayoutFullScreen failed."));
789             }
790         };
791     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
792         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
793     NativeValue* result = nullptr;
794     AsyncTask::Schedule(
795         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
796     return result;
797 }
798 
OnSetSystemBarEnable(NativeEngine & engine,NativeCallbackInfo & info)799 NativeValue* JsWindow::OnSetSystemBarEnable(NativeEngine& engine, NativeCallbackInfo& info)
800 {
801     WLOGFI("JsWindow::OnSetSystemBarEnable is called");
802     WMError errCode = WMError::WM_OK;
803     if (info.argc > 2 || windowToken_ == nullptr) { // 2: maximum params num
804         WLOGFE("JsWindow params not match or window token is nullptr!");
805         errCode = WMError::WM_ERROR_INVALID_PARAM;
806     }
807     std::map<WindowType, SystemBarProperty> systemBarProperties;
808     if (errCode == WMError::WM_OK && !GetSystemBarStatus(systemBarProperties, engine, info, windowToken_)) {
809         errCode = WMError::WM_ERROR_INVALID_PARAM;
810     }
811     AsyncTask::CompleteCallback complete =
812         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
813             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
814                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
815                 WLOGFE("JsWindow windowToken_ is nullptr or invalid param");
816             }
817             WMError ret = windowToken_->SetSystemBarProperty(WindowType::WINDOW_TYPE_STATUS_BAR,
818                 systemBarProperties.at(WindowType::WINDOW_TYPE_STATUS_BAR));
819             ret = windowToken_->SetSystemBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_BAR,
820                 systemBarProperties.at(WindowType::WINDOW_TYPE_NAVIGATION_BAR));
821             if (ret == WMError::WM_OK) {
822                 task.Resolve(engine, engine.CreateUndefined());
823                 WLOGFI("JsWindow::OnSetSystemBarEnable success");
824             } else {
825                 task.Reject(engine, CreateJsError(engine,
826                     static_cast<int32_t>(ret), "JsWindow::OnSetSystemBarEnable failed."));
827             }
828         };
829 
830     NativeValue* lastParam = nullptr;
831     if (info.argc > 0 && info.argv[0]->TypeOf() == NATIVE_FUNCTION) {
832         lastParam = info.argv[0];
833     } else if (info.argc > 1 && info.argv[1]->TypeOf() == NATIVE_FUNCTION) {
834         lastParam = info.argv[1];
835     }
836     NativeValue* result = nullptr;
837     AsyncTask::Schedule(
838         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
839     return result;
840 }
841 
OnSetSystemBarProperties(NativeEngine & engine,NativeCallbackInfo & info)842 NativeValue* JsWindow::OnSetSystemBarProperties(NativeEngine& engine, NativeCallbackInfo& info)
843 {
844     WLOGFI("JsWindow::OnSetSystemBarProperties is called");
845     WMError errCode = WMError::WM_OK;
846     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
847         WLOGFE("JsWindow params not match!");
848         errCode = WMError::WM_ERROR_INVALID_PARAM;
849     }
850     std::map<WindowType, SystemBarProperty> systemBarProperties;
851     if (errCode == WMError::WM_OK) {
852         NativeObject* nativeObj = ConvertNativeValueTo<NativeObject>(info.argv[0]);
853         if (nativeObj == nullptr) {
854             WLOGFE("Failed to convert object to SystemBarProperties");
855             errCode = WMError::WM_ERROR_INVALID_PARAM;
856         } else {
857             if (!SetSystemBarPropertiesFromJs(engine, nativeObj, systemBarProperties, windowToken_)) {
858                 WLOGFE("Failed to GetSystemBarProperties From Js Object");
859                 errCode = WMError::WM_ERROR_INVALID_PARAM;
860             }
861         }
862     }
863     AsyncTask::CompleteCallback complete =
864         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
865             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
866                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
867                 WLOGFE("JsWindow windowToken_ is nullptr or invalid param");
868             }
869             WMError ret = windowToken_->SetSystemBarProperty(WindowType::WINDOW_TYPE_STATUS_BAR,
870                 systemBarProperties.at(WindowType::WINDOW_TYPE_STATUS_BAR));
871             ret = windowToken_->SetSystemBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_BAR,
872                 systemBarProperties.at(WindowType::WINDOW_TYPE_NAVIGATION_BAR));
873             if (ret == WMError::WM_OK) {
874                 task.Resolve(engine, engine.CreateUndefined());
875                 WLOGFI("JsWindow::OnSetSystemBarProperties success");
876             } else {
877                 task.Reject(engine, CreateJsError(engine,
878                     static_cast<int32_t>(WMError::WM_ERROR_NULLPTR), "JsWindow::OnSetSystemBarProperties failed."));
879             }
880         };
881 
882     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
883         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
884     NativeValue* result = nullptr;
885     AsyncTask::Schedule(
886         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
887     return result;
888 }
889 
OnGetAvoidArea(NativeEngine & engine,NativeCallbackInfo & info)890 NativeValue* JsWindow::OnGetAvoidArea(NativeEngine& engine, NativeCallbackInfo& info)
891 {
892     WLOGFI("JsWindow::OnGetAvoidArea is called info.argc: %{public}d", info.argc);
893     WMError errCode = WMError::WM_OK;
894     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
895         WLOGFE("JsWindow params not match!");
896         errCode = WMError::WM_ERROR_INVALID_PARAM;
897     }
898     AvoidAreaType avoidAreaType = AvoidAreaType::TYPE_SYSTEM;
899     if (errCode == WMError::WM_OK) {
900         // Parse info->argv[0] as AvoidAreaType
901         NativeNumber* nativeMode = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
902         if (nativeMode == nullptr) {
903             WLOGFE("Failed to convert parameter to AvoidAreaType");
904             errCode = WMError::WM_ERROR_INVALID_PARAM;
905         } else {
906             avoidAreaType = static_cast<AvoidAreaType>(static_cast<uint32_t>(*nativeMode));
907             errCode = ((avoidAreaType > AvoidAreaType::TYPE_SYSTEM_GESTURE) ||
908                 (avoidAreaType < AvoidAreaType::TYPE_SYSTEM)) ? WMError::WM_ERROR_INVALID_PARAM : WMError::WM_OK;
909         }
910     }
911     AsyncTask::CompleteCallback complete =
912         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
913             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
914                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
915                 WLOGFE("JsWindow windowToken_ is nullptr or invalid param");
916             }
917             // getAvoidRect by avoidAreaType
918             AvoidArea avoidArea;
919             WMError ret = windowToken_->GetAvoidAreaByType(avoidAreaType, avoidArea);
920             if (ret == WMError::WM_OK) {
921                 WLOGFI("JsWindow::OnGetAvoidArea GetAvoidAreaByType Success");
922             } else {
923                 WLOGFE("JsWindow::OnGetAvoidArea GetAvoidAreaByType Failed");
924                 avoidArea = { g_emptyRect, g_emptyRect, g_emptyRect, g_emptyRect }; // left, top, right, bottom
925             }
926             // native avoidArea -> js avoidArea
927             NativeValue* avoidAreaObj = ChangeAvoidAreaToJsValue(engine, avoidArea);
928             if (avoidAreaObj != nullptr) {
929                 WLOGFI("JsWindow::OnGetAvoidArea ChangeAvoidAreaToJsValue Success");
930                 task.Resolve(engine, avoidAreaObj);
931             } else {
932                 task.Reject(engine, CreateJsError(engine,
933                     static_cast<int32_t>(WMError::WM_ERROR_NULLPTR), "JsWindow::OnGetAvoidArea failed."));
934             }
935         };
936     WLOGFI("JsWindow::OnGetAvoidArea AsyncTask end");
937     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
938         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
939     NativeValue* result = nullptr;
940     AsyncTask::Schedule(
941         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
942     return result;
943 }
944 
OnIsShowing(NativeEngine & engine,NativeCallbackInfo & info)945 NativeValue* JsWindow::OnIsShowing(NativeEngine& engine, NativeCallbackInfo& info)
946 {
947     WLOGFI("JsWindow::OnIsShowing is called");
948     WMError errCode = WMError::WM_OK;
949     if (info.argc > 1) {
950         WLOGFE("JsWindow params not match");
951         errCode = WMError::WM_ERROR_INVALID_PARAM;
952     }
953     AsyncTask::CompleteCallback complete =
954         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
955             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
956                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
957                 WLOGFE("JsWindow windowToken_ is nullptr or invalid param");
958                 return;
959             }
960             bool state = windowToken_->GetShowState();
961             task.Resolve(engine, CreateJsValue(engine, state));
962         };
963 
964     NativeValue* lastParam = (info.argc == 0) ? nullptr :
965         (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
966     NativeValue* result = nullptr;
967     AsyncTask::Schedule(
968         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
969     return result;
970 }
971 
OnIsSupportWideGamut(NativeEngine & engine,NativeCallbackInfo & info)972 NativeValue* JsWindow::OnIsSupportWideGamut(NativeEngine& engine, NativeCallbackInfo& info)
973 {
974     WLOGFI("JsWindow::OnIsSupportWideGamut is called");
975     WMError errCode = WMError::WM_OK;
976     if (info.argc > 1) {
977         WLOGFE("JsWindow params not match");
978         errCode = WMError::WM_ERROR_INVALID_PARAM;
979     }
980     AsyncTask::CompleteCallback complete =
981         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
982             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
983                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
984                 WLOGFE("JsWindow windowToken_ is nullptr or invalid param");
985                 return;
986             }
987             bool flag = windowToken_->IsSupportWideGamut();
988             task.Resolve(engine, CreateJsValue(engine, flag));
989         };
990 
991     NativeValue* lastParam = (info.argc == 0) ? nullptr :
992         (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
993     NativeValue* result = nullptr;
994     AsyncTask::Schedule(
995         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
996     return result;
997 }
998 
OnSetBackgroundColor(NativeEngine & engine,NativeCallbackInfo & info)999 NativeValue* JsWindow::OnSetBackgroundColor(NativeEngine& engine, NativeCallbackInfo& info)
1000 {
1001     WMError errCode = WMError::WM_OK;
1002     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
1003         WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
1004         errCode = WMError::WM_ERROR_INVALID_PARAM;
1005     }
1006     std::string color;
1007     if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[0], color)) {
1008         WLOGFE("[NAPI]Failed to convert parameter to background color");
1009         errCode = WMError::WM_ERROR_INVALID_PARAM;
1010     }
1011 
1012     AsyncTask::CompleteCallback complete =
1013         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
1014             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
1015                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
1016                 return;
1017             }
1018             WMError ret = windowToken_->SetBackgroundColor(color);
1019             if (ret == WMError::WM_OK) {
1020                 task.Resolve(engine, engine.CreateUndefined());
1021             } else {
1022                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret),
1023                     "Window set background color failed"));
1024             }
1025             WLOGFI("[NAPI]Window [%{public}u, %{public}s] set background color end",
1026                 windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1027         };
1028 
1029     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
1030         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
1031     NativeValue* result = nullptr;
1032     AsyncTask::Schedule(
1033         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1034     return result;
1035 }
1036 
OnSetBrightness(NativeEngine & engine,NativeCallbackInfo & info)1037 NativeValue* JsWindow::OnSetBrightness(NativeEngine& engine, NativeCallbackInfo& info)
1038 {
1039     WMError errCode = WMError::WM_OK;
1040     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
1041         WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
1042         errCode = WMError::WM_ERROR_INVALID_PARAM;
1043     }
1044     float brightness = UNDEFINED_BRIGHTNESS;
1045     if (errCode == WMError::WM_OK) {
1046         NativeNumber* nativeVal = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
1047         if (nativeVal == nullptr) {
1048             WLOGFE("[NAPI]Failed to convert parameter to brightness");
1049             errCode = WMError::WM_ERROR_INVALID_PARAM;
1050         } else {
1051             brightness = static_cast<double>(*nativeVal);
1052         }
1053     }
1054 
1055     AsyncTask::CompleteCallback complete =
1056         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
1057             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
1058                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
1059                 return;
1060             }
1061             WMError ret = windowToken_->SetBrightness(brightness);
1062             if (ret == WMError::WM_OK) {
1063                 task.Resolve(engine, engine.CreateUndefined());
1064             } else {
1065                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window set brightness failed"));
1066             }
1067             WLOGFI("[NAPI]Window [%{public}u, %{public}s] set brightness end",
1068                 windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1069         };
1070 
1071     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
1072         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
1073     NativeValue* result = nullptr;
1074     AsyncTask::Schedule(
1075         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1076     return result;
1077 }
1078 
OnSetDimBehind(NativeEngine & engine,NativeCallbackInfo & info)1079 NativeValue* JsWindow::OnSetDimBehind(NativeEngine& engine, NativeCallbackInfo& info)
1080 {
1081     WLOGFI("JsWindow::OnSetDimBehind is called");
1082     AsyncTask::CompleteCallback complete =
1083         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
1084             task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_DEVICE_NOT_SUPPORT)));
1085         };
1086 
1087     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
1088         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
1089     NativeValue* result = nullptr;
1090     AsyncTask::Schedule(
1091         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1092     return result;
1093 }
1094 
OnSetFocusable(NativeEngine & engine,NativeCallbackInfo & info)1095 NativeValue* JsWindow::OnSetFocusable(NativeEngine& engine, NativeCallbackInfo& info)
1096 {
1097     WMError errCode = WMError::WM_OK;
1098     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
1099         WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
1100         errCode = WMError::WM_ERROR_INVALID_PARAM;
1101     }
1102     bool focusable = true;
1103     if (errCode == WMError::WM_OK) {
1104         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
1105         if (nativeVal == nullptr) {
1106             WLOGFE("[NAPI]Failed to convert parameter to focusable");
1107             errCode = WMError::WM_ERROR_INVALID_PARAM;
1108         } else {
1109             focusable = static_cast<bool>(*nativeVal);
1110         }
1111     }
1112 
1113     AsyncTask::CompleteCallback complete =
1114         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
1115             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
1116                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
1117                 return;
1118             }
1119             WMError ret = windowToken_->SetFocusable(focusable);
1120             if (ret == WMError::WM_OK) {
1121                 task.Resolve(engine, engine.CreateUndefined());
1122             } else {
1123                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window set focusable failed"));
1124             }
1125             WLOGFI("[NAPI]Window [%{public}u, %{public}s] set focusable end",
1126                 windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1127         };
1128 
1129     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
1130         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
1131     NativeValue* result = nullptr;
1132     AsyncTask::Schedule(
1133         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1134     return result;
1135 }
1136 
OnSetKeepScreenOn(NativeEngine & engine,NativeCallbackInfo & info)1137 NativeValue* JsWindow::OnSetKeepScreenOn(NativeEngine& engine, NativeCallbackInfo& info)
1138 {
1139     WMError errCode = WMError::WM_OK;
1140     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
1141         WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
1142         errCode = WMError::WM_ERROR_INVALID_PARAM;
1143     }
1144     bool keepScreenOn = true;
1145     if (errCode == WMError::WM_OK) {
1146         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
1147         if (nativeVal == nullptr) {
1148             WLOGFE("[NAPI]Failed to convert parameter to keepScreenOn");
1149             errCode = WMError::WM_ERROR_INVALID_PARAM;
1150         } else {
1151             keepScreenOn = static_cast<bool>(*nativeVal);
1152         }
1153     }
1154 
1155     AsyncTask::CompleteCallback complete =
1156         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
1157             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
1158                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
1159                 return;
1160             }
1161             WMError ret = windowToken_->SetKeepScreenOn(keepScreenOn);
1162             if (ret == WMError::WM_OK) {
1163                 task.Resolve(engine, engine.CreateUndefined());
1164             } else {
1165                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret),
1166                     "Window set keep screen on failed"));
1167             }
1168             WLOGFI("[NAPI]Window [%{public}u, %{public}s] set keep screen on end",
1169                 windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1170         };
1171 
1172     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
1173         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
1174     NativeValue* result = nullptr;
1175     AsyncTask::Schedule(
1176         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1177     return result;
1178 }
1179 
OnSetOutsideTouchable(NativeEngine & engine,NativeCallbackInfo & info)1180 NativeValue* JsWindow::OnSetOutsideTouchable(NativeEngine& engine, NativeCallbackInfo& info)
1181 {
1182     WLOGFI("JsWindow::OnSetOutsideTouchable is called");
1183     AsyncTask::CompleteCallback complete =
1184         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
1185             task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_DEVICE_NOT_SUPPORT)));
1186         };
1187 
1188     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
1189         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
1190     NativeValue* result = nullptr;
1191     AsyncTask::Schedule(
1192         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1193     return result;
1194 }
1195 
OnSetPrivacyMode(NativeEngine & engine,NativeCallbackInfo & info)1196 NativeValue* JsWindow::OnSetPrivacyMode(NativeEngine& engine, NativeCallbackInfo& info)
1197 {
1198     WMError errCode = WMError::WM_OK;
1199     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
1200         WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
1201         errCode = WMError::WM_ERROR_INVALID_PARAM;
1202     }
1203     bool isPrivacyMode = false;
1204     if (errCode == WMError::WM_OK) {
1205         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
1206         if (nativeVal == nullptr) {
1207             WLOGFE("[NAPI]Failed to convert parameter to isPrivacyMode");
1208             errCode = WMError::WM_ERROR_INVALID_PARAM;
1209         } else {
1210             isPrivacyMode = static_cast<bool>(*nativeVal);
1211         }
1212     }
1213 
1214     AsyncTask::CompleteCallback complete =
1215         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
1216             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
1217                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params"));
1218                 return;
1219             }
1220             windowToken_->SetPrivacyMode(isPrivacyMode);
1221             task.Resolve(engine, engine.CreateUndefined());
1222             WLOGFI("[NAPI]Window [%{public}u, %{public}s] set privacy mode end, mode = %{public}u",
1223                 windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), isPrivacyMode);
1224         };
1225 
1226     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
1227         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
1228     NativeValue* result = nullptr;
1229     AsyncTask::Schedule(
1230         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1231     return result;
1232 }
1233 
OnSetTouchable(NativeEngine & engine,NativeCallbackInfo & info)1234 NativeValue* JsWindow::OnSetTouchable(NativeEngine& engine, NativeCallbackInfo& info)
1235 {
1236     WMError errCode = WMError::WM_OK;
1237     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
1238         WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
1239         errCode = WMError::WM_ERROR_INVALID_PARAM;
1240     }
1241     bool touchable = true;
1242     if (errCode == WMError::WM_OK) {
1243         NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
1244         if (nativeVal == nullptr) {
1245             WLOGFE("[NAPI]Failed to convert parameter to touchable");
1246             errCode = WMError::WM_ERROR_INVALID_PARAM;
1247         } else {
1248             touchable = static_cast<bool>(*nativeVal);
1249         }
1250     }
1251 
1252     AsyncTask::CompleteCallback complete =
1253         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
1254             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
1255                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
1256                 return;
1257             }
1258             WMError ret = windowToken_->SetTouchable(touchable);
1259             if (ret == WMError::WM_OK) {
1260                 task.Resolve(engine, engine.CreateUndefined());
1261             } else {
1262                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window set touchable failed"));
1263             }
1264             WLOGFI("[NAPI]Window [%{public}u, %{public}s] set touchable end",
1265                 windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1266         };
1267 
1268     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
1269         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
1270     NativeValue* result = nullptr;
1271     AsyncTask::Schedule(
1272         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1273     return result;
1274 }
1275 
OnSetCallingWindow(NativeEngine & engine,NativeCallbackInfo & info)1276 NativeValue* JsWindow::OnSetCallingWindow(NativeEngine& engine, NativeCallbackInfo& info)
1277 {
1278     WMError errCode = WMError::WM_OK;
1279     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
1280         WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
1281         errCode = WMError::WM_ERROR_INVALID_PARAM;
1282     }
1283     uint32_t callingWindow = INVALID_WINDOW_ID;
1284     if (errCode == WMError::WM_OK) {
1285         NativeNumber* nativeVal = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
1286         if (nativeVal == nullptr) {
1287             WLOGFE("[NAPI]Failed to convert parameter to touchable");
1288             errCode = WMError::WM_ERROR_INVALID_PARAM;
1289         } else {
1290             callingWindow = static_cast<uint32_t>(*nativeVal);
1291         }
1292     }
1293 
1294     AsyncTask::CompleteCallback complete =
1295         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
1296             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
1297                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
1298                 return;
1299             }
1300             WMError ret = windowToken_->SetCallingWindow(callingWindow);
1301             if (ret == WMError::WM_OK) {
1302                 task.Resolve(engine, engine.CreateUndefined());
1303             } else {
1304                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret),
1305                     "Window set calling window failed"));
1306             }
1307             WLOGFI("[NAPI]Window [%{public}u, %{public}s] set calling window end",
1308                 windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1309         };
1310 
1311     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
1312         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
1313     NativeValue* result = nullptr;
1314     AsyncTask::Schedule(
1315         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1316     return result;
1317 }
1318 
OnSetColorSpace(NativeEngine & engine,NativeCallbackInfo & info)1319 NativeValue* JsWindow::OnSetColorSpace(NativeEngine& engine, NativeCallbackInfo& info)
1320 {
1321     WLOGFI("JsWindow::OnSetColorSpace is called");
1322     WMError errCode = WMError::WM_OK;
1323     ColorSpace colorSpace = ColorSpace::COLOR_SPACE_DEFAULT;
1324     if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
1325         errCode = WMError::WM_ERROR_INVALID_PARAM;
1326         WLOGFE("JsWindow::OnSetColorSpace argc < 1");
1327     } else {
1328         NativeNumber* nativeType = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
1329         if (nativeType == nullptr) {
1330             errCode = WMError::WM_ERROR_INVALID_PARAM;
1331             WLOGFE("JsWindow::OnSetColorSpace Failed to convert parameter to ColorSpace");
1332         } else {
1333             colorSpace = static_cast<ColorSpace>(static_cast<uint32_t>(*nativeType));
1334             if (colorSpace > ColorSpace::COLOR_SPACE_WIDE_GAMUT) {
1335                 WLOGFE("JsWindow::OnSetColorSpace Failed, colorSpace %{public}u invalid!",
1336                     static_cast<uint32_t>(colorSpace));
1337                 errCode = WMError::WM_ERROR_INVALID_PARAM;
1338             } else {
1339                 WLOGFI("JsWindow::OnSetColorSpace %{public}u", static_cast<uint32_t>(colorSpace));
1340             }
1341         }
1342     }
1343 
1344     AsyncTask::CompleteCallback complete =
1345         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
1346             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
1347                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode),
1348                     "JsWindow::OnSetColorSpace failed."));
1349                 WLOGFE("JsWindow windowToken_ is nullptr or args error");
1350                 return;
1351             }
1352             windowToken_->SetColorSpace(colorSpace);
1353             task.Resolve(engine, engine.CreateUndefined());
1354         };
1355 
1356     NativeValue* lastParam = (info.argc <= 1) ? nullptr :
1357         (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
1358     NativeValue* result = nullptr;
1359     AsyncTask::Schedule(
1360         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1361     return result;
1362 }
1363 
OnGetColorSpace(NativeEngine & engine,NativeCallbackInfo & info)1364 NativeValue* JsWindow::OnGetColorSpace(NativeEngine& engine, NativeCallbackInfo& info)
1365 {
1366     WLOGFI("JsWindow::OnGetColorSpace is called");
1367     WMError errCode = WMError::WM_OK;
1368     if (info.argc > 1) {
1369         WLOGFE("JsWindow params not match");
1370         errCode = WMError::WM_ERROR_INVALID_PARAM;
1371     }
1372     AsyncTask::CompleteCallback complete =
1373         [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
1374             if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
1375                 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
1376                 WLOGFE("JsWindow windowToken_ is nullptr or invalid param");
1377                 return;
1378             }
1379             ColorSpace colorSpace = windowToken_->GetColorSpace();
1380             task.Resolve(engine, CreateJsValue(engine, static_cast<uint32_t>(colorSpace)));
1381         };
1382 
1383     NativeValue* lastParam = (info.argc == 0) ? nullptr :
1384         (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
1385     NativeValue* result = nullptr;
1386     AsyncTask::Schedule(
1387         engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1388     return result;
1389 }
1390 
FindJsWindowObject(std::string windowName)1391 std::shared_ptr<NativeReference> FindJsWindowObject(std::string windowName)
1392 {
1393     WLOGFI("JsWindow::FindJsWindowObject is called");
1394     std::lock_guard<std::recursive_mutex> lock(g_mutex);
1395     if (g_jsWindowMap.find(windowName) == g_jsWindowMap.end()) {
1396         return nullptr;
1397     }
1398     return g_jsWindowMap[windowName];
1399 }
1400 
CreateJsWindowObject(NativeEngine & engine,sptr<Window> & window)1401 NativeValue* CreateJsWindowObject(NativeEngine& engine, sptr<Window>& window)
1402 {
1403     WLOGFI("JsWindow::CreateJsWindow is called");
1404     std::string windowName = window->GetWindowName();
1405     // avoid repeatedly create js window when getWindow
1406     std::shared_ptr<NativeReference> jsWindowObj = FindJsWindowObject(windowName);
1407     if (jsWindowObj != nullptr && jsWindowObj->Get() != nullptr) {
1408         WLOGFI("JsWindow::CreateJsWindow FindJsWindowObject %{public}s", windowName.c_str());
1409         return jsWindowObj->Get();
1410     }
1411     NativeValue* objValue = engine.CreateObject();
1412     NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
1413 
1414     std::unique_ptr<JsWindow> jsWindow = std::make_unique<JsWindow>(window);
1415     object->SetNativePointer(jsWindow.release(), JsWindow::Finalizer, nullptr);
1416 
1417     BindNativeFunction(engine, *object, "show", JsWindow::Show);
1418     BindNativeFunction(engine, *object, "destroy", JsWindow::Destroy);
1419     BindNativeFunction(engine, *object, "hide", JsWindow::Hide);
1420     BindNativeFunction(engine, *object, "moveTo", JsWindow::MoveTo);
1421     BindNativeFunction(engine, *object, "resetSize", JsWindow::Resize);
1422     BindNativeFunction(engine, *object, "setWindowType", JsWindow::SetWindowType);
1423     BindNativeFunction(engine, *object, "setWindowMode", JsWindow::SetWindowMode);
1424     BindNativeFunction(engine, *object, "getProperties", JsWindow::GetProperties);
1425     BindNativeFunction(engine, *object, "on", JsWindow::RegisterWindowCallback);
1426     BindNativeFunction(engine, *object, "off", JsWindow::UnregisterWindowCallback);
1427     BindNativeFunction(engine, *object, "loadContent", JsWindow::LoadContent);
1428     BindNativeFunction(engine, *object, "setFullScreen", JsWindow::SetFullScreen);
1429     BindNativeFunction(engine, *object, "setLayoutFullScreen", JsWindow::SetLayoutFullScreen);
1430     BindNativeFunction(engine, *object, "setSystemBarEnable", JsWindow::SetSystemBarEnable);
1431     BindNativeFunction(engine, *object, "setSystemBarProperties", JsWindow::SetSystemBarProperties);
1432     BindNativeFunction(engine, *object, "getAvoidArea", JsWindow::GetAvoidArea);
1433     BindNativeFunction(engine, *object, "isShowing", JsWindow::IsShowing);
1434     BindNativeFunction(engine, *object, "isSupportWideGamut", JsWindow::IsSupportWideGamut);
1435     BindNativeFunction(engine, *object, "setColorSpace", JsWindow::SetColorSpace);
1436     BindNativeFunction(engine, *object, "getColorSpace", JsWindow::GetColorSpace);
1437     BindNativeFunction(engine, *object, "setBackgroundColor", JsWindow::SetBackgroundColor);
1438     BindNativeFunction(engine, *object, "setBrightness", JsWindow::SetBrightness);
1439     BindNativeFunction(engine, *object, "setDimBehind", JsWindow::SetDimBehind);
1440     BindNativeFunction(engine, *object, "setFocusable", JsWindow::SetFocusable);
1441     BindNativeFunction(engine, *object, "setKeepScreenOn", JsWindow::SetKeepScreenOn);
1442     BindNativeFunction(engine, *object, "setOutsideTouchable", JsWindow::SetOutsideTouchable);
1443     BindNativeFunction(engine, *object, "setPrivacyMode", JsWindow::SetPrivacyMode);
1444     BindNativeFunction(engine, *object, "setTouchable", JsWindow::SetTouchable);
1445     BindNativeFunction(engine, *object, "setCallingWindow", JsWindow::SetCallingWindow);
1446     std::shared_ptr<NativeReference> jsWindowRef;
1447     jsWindowRef.reset(engine.CreateReference(objValue, 1));
1448     std::lock_guard<std::recursive_mutex> lock(g_mutex);
1449     g_jsWindowMap[windowName] = jsWindowRef;
1450     return objValue;
1451 }
1452 }  // namespace Rosen
1453 }  // namespace OHOS
1454