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 #include "js_window_manager.h"
16 #include <ability.h>
17 #include <cinttypes>
18 #include <hitrace_meter.h>
19 #include <new>
20 #include <transaction/rs_interfaces.h>
21 #include "ability_context.h"
22 #include "display_manager.h"
23 #include "dm_common.h"
24 #include "wm_common.h"
25 #include "js_window.h"
26 #include "js_window_utils.h"
27 #include "window_helper.h"
28 #include "window_manager_hilog.h"
29 #include "window_option.h"
30 #include "pixel_map_napi.h"
31 #include "permission.h"
32 #include "singleton_container.h"
33 namespace OHOS {
34 namespace Rosen {
35 using namespace AbilityRuntime;
36 namespace {
37 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "JsWindowManager"};
38 }
39
JsWindowManager()40 JsWindowManager::JsWindowManager() : registerManager_(std::make_unique<JsWindowRegisterManager>())
41 {
42 }
43
~JsWindowManager()44 JsWindowManager::~JsWindowManager()
45 {
46 }
47
Finalizer(NativeEngine * engine,void * data,void * hint)48 void JsWindowManager::Finalizer(NativeEngine* engine, void* data, void* hint)
49 {
50 WLOGI("Finalizer");
51 std::unique_ptr<JsWindowManager>(static_cast<JsWindowManager*>(data));
52 }
53
Create(NativeEngine * engine,NativeCallbackInfo * info)54 NativeValue* JsWindowManager::Create(NativeEngine* engine, NativeCallbackInfo* info)
55 {
56 JsWindowManager* me = CheckParamsAndGetThis<JsWindowManager>(engine, info);
57 return (me != nullptr) ? me->OnCreate(*engine, *info) : nullptr;
58 }
59
CreateWindow(NativeEngine * engine,NativeCallbackInfo * info)60 NativeValue* JsWindowManager::CreateWindow(NativeEngine* engine, NativeCallbackInfo* info)
61 {
62 JsWindowManager* me = CheckParamsAndGetThis<JsWindowManager>(engine, info);
63 return (me != nullptr) ? me->OnCreateWindow(*engine, *info) : nullptr;
64 }
65
FindWindow(NativeEngine * engine,NativeCallbackInfo * info)66 NativeValue* JsWindowManager::FindWindow(NativeEngine* engine, NativeCallbackInfo* info)
67 {
68 JsWindowManager* me = CheckParamsAndGetThis<JsWindowManager>(engine, info);
69 return (me != nullptr) ? me->OnFindWindow(*engine, *info) : nullptr;
70 }
71
FindWindowSync(NativeEngine * engine,NativeCallbackInfo * info)72 NativeValue* JsWindowManager::FindWindowSync(NativeEngine* engine, NativeCallbackInfo* info)
73 {
74 JsWindowManager* me = CheckParamsAndGetThis<JsWindowManager>(engine, info);
75 return (me != nullptr) ? me->OnFindWindowSync(*engine, *info) : nullptr;
76 }
77
MinimizeAll(NativeEngine * engine,NativeCallbackInfo * info)78 NativeValue* JsWindowManager::MinimizeAll(NativeEngine* engine, NativeCallbackInfo* info)
79 {
80 JsWindowManager* me = CheckParamsAndGetThis<JsWindowManager>(engine, info);
81 return (me != nullptr) ? me->OnMinimizeAll(*engine, *info) : nullptr;
82 }
83
ToggleShownStateForAllAppWindows(NativeEngine * engine,NativeCallbackInfo * info)84 NativeValue* JsWindowManager::ToggleShownStateForAllAppWindows(NativeEngine* engine, NativeCallbackInfo* info)
85 {
86 JsWindowManager* me = CheckParamsAndGetThis<JsWindowManager>(engine, info);
87 return (me != nullptr) ? me->OnToggleShownStateForAllAppWindows(*engine, *info) : nullptr;
88 }
89
RegisterWindowManagerCallback(NativeEngine * engine,NativeCallbackInfo * info)90 NativeValue* JsWindowManager::RegisterWindowManagerCallback(NativeEngine* engine, NativeCallbackInfo* info)
91 {
92 JsWindowManager* me = CheckParamsAndGetThis<JsWindowManager>(engine, info);
93 return (me != nullptr) ? me->OnRegisterWindowManagerCallback(*engine, *info) : nullptr;
94 }
95
UnregisterWindowMangerCallback(NativeEngine * engine,NativeCallbackInfo * info)96 NativeValue* JsWindowManager::UnregisterWindowMangerCallback(NativeEngine* engine, NativeCallbackInfo* info)
97 {
98 JsWindowManager* me = CheckParamsAndGetThis<JsWindowManager>(engine, info);
99 return (me != nullptr) ? me->OnUnregisterWindowManagerCallback(*engine, *info) : nullptr;
100 }
101
GetTopWindow(NativeEngine * engine,NativeCallbackInfo * info)102 NativeValue* JsWindowManager::GetTopWindow(NativeEngine* engine, NativeCallbackInfo* info)
103 {
104 JsWindowManager* me = CheckParamsAndGetThis<JsWindowManager>(engine, info);
105 return (me != nullptr) ? me->OnGetTopWindow(*engine, *info) : nullptr;
106 }
107
GetLastWindow(NativeEngine * engine,NativeCallbackInfo * info)108 NativeValue* JsWindowManager::GetLastWindow(NativeEngine* engine, NativeCallbackInfo* info)
109 {
110 JsWindowManager* me = CheckParamsAndGetThis<JsWindowManager>(engine, info);
111 return (me != nullptr) ? me->OnGetLastWindow(*engine, *info) : nullptr;
112 }
113
SetWindowLayoutMode(NativeEngine * engine,NativeCallbackInfo * info)114 NativeValue* JsWindowManager::SetWindowLayoutMode(NativeEngine* engine, NativeCallbackInfo* info)
115 {
116 JsWindowManager* me = CheckParamsAndGetThis<JsWindowManager>(engine, info);
117 return (me != nullptr) ? me->OnSetWindowLayoutMode(*engine, *info) : nullptr;
118 }
119
SetGestureNavigationEnabled(NativeEngine * engine,NativeCallbackInfo * info)120 NativeValue* JsWindowManager::SetGestureNavigationEnabled(NativeEngine* engine, NativeCallbackInfo* info)
121 {
122 JsWindowManager* me = CheckParamsAndGetThis<JsWindowManager>(engine, info);
123 return (me != nullptr) ? me->OnSetGestureNavigationEnabled(*engine, *info) : nullptr;
124 }
125
SetWaterMarkImage(NativeEngine * engine,NativeCallbackInfo * info)126 NativeValue* JsWindowManager::SetWaterMarkImage(NativeEngine* engine, NativeCallbackInfo* info)
127 {
128 JsWindowManager* me = CheckParamsAndGetThis<JsWindowManager>(engine, info);
129 return (me != nullptr) ? me->OnSetWaterMarkImage(*engine, *info) : nullptr;
130 }
131
GetNativeContext(NativeEngine & engine,NativeValue * nativeContext,void * & contextPtr,WMError & errCode)132 static void GetNativeContext(NativeEngine& engine, NativeValue* nativeContext, void*& contextPtr, WMError& errCode)
133 {
134 AppExecFwk::Ability* ability = nullptr;
135 bool isOldApi = GetAPI7Ability(engine, ability);
136 WLOGFD("FA mode:%{public}u", isOldApi);
137 if (isOldApi) {
138 return;
139 }
140 if (nativeContext != nullptr) {
141 auto objContext = AbilityRuntime::ConvertNativeValueTo<NativeObject>(nativeContext);
142 if (objContext == nullptr) {
143 WLOGFE("ConvertNativeValueTo Context Object failed");
144 errCode = WMError::WM_ERROR_INVALID_PARAM;
145 return;
146 }
147 contextPtr = objContext->GetNativePointer();
148 }
149 }
150
GetParentId(NativeEngine & engine)151 static uint32_t GetParentId(NativeEngine& engine)
152 {
153 AppExecFwk::Ability* ability = nullptr;
154 uint32_t parentId = 0;
155 bool isOldApi = GetAPI7Ability(engine, ability);
156 if (isOldApi) {
157 if (ability == nullptr) {
158 WLOGE("FA mode GetAPI7Ability failed");
159 return parentId;
160 }
161 auto window = ability->GetWindow();
162 if (window == nullptr) {
163 WLOGE("Get mainWindow failed");
164 return parentId;
165 }
166 parentId = window->GetWindowId();
167 }
168 return parentId;
169 }
170
GetWindowTypeAndParentId(NativeEngine & engine,uint32_t & parentId,WindowType & winType,NativeValue * nativeString,NativeValue * nativeType)171 static bool GetWindowTypeAndParentId(NativeEngine& engine, uint32_t& parentId, WindowType& winType,
172 NativeValue* nativeString, NativeValue* nativeType)
173 {
174 NativeNumber* type = ConvertNativeValueTo<NativeNumber>(nativeType);
175 if (type == nullptr) {
176 WLOGFE("Failed to convert parameter to windowType");
177 return false;
178 }
179
180 if (static_cast<uint32_t>(*type) >= static_cast<uint32_t>(ApiWindowType::TYPE_BASE) &&
181 static_cast<uint32_t>(*type) < static_cast<uint32_t>(ApiWindowType::TYPE_END)) {
182 winType = JS_TO_NATIVE_WINDOW_TYPE_MAP.at(static_cast<ApiWindowType>(static_cast<uint32_t>(*type)));
183 } else {
184 WLOGFE("Type %{public}u is not supported", static_cast<uint32_t>(*type));
185 return false;
186 }
187
188 AppExecFwk::Ability* ability = nullptr;
189 bool isOldApi = GetAPI7Ability(engine, ability);
190 if (isOldApi) {
191 if (ability == nullptr || !WindowHelper::IsSubWindow(winType)) {
192 WLOGE("FA mode GetAPI7Ability failed or type %{public}u is not subWinodw", winType);
193 return false;
194 }
195 auto window = ability->GetWindow();
196 if (window == nullptr) {
197 WLOGE("Get mainWindow failed");
198 return false;
199 }
200 parentId = window->GetWindowId();
201 } else {
202 if (!WindowHelper::IsSystemWindow(winType)) {
203 WLOGFE("Only SystemWindow support create in stage mode, type is %{public}u", winType);
204 return false;
205 }
206 }
207 return true;
208 }
209
CreateNewSystemWindowTask(void * contextPtr,sptr<WindowOption> windowOption,NativeEngine & engine,AsyncTask & task)210 static void CreateNewSystemWindowTask(void* contextPtr, sptr<WindowOption> windowOption,
211 NativeEngine& engine, AsyncTask& task)
212 {
213 WLOGI("CreateSystemWindowTask");
214 if (windowOption == nullptr) {
215 int32_t err = static_cast<int32_t>(WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY);
216 task.Reject(engine, CreateJsError(engine, err, "New window option failed"));
217 WLOGFE("New window option failed");
218 return;
219 }
220 auto context = static_cast<std::weak_ptr<AbilityRuntime::Context>*>(contextPtr);
221 if (contextPtr == nullptr || context == nullptr) {
222 int32_t err = static_cast<int32_t>(WmErrorCode::WM_ERROR_CONTEXT_ABNORMALLY);
223 task.Reject(engine, CreateJsError(engine, err, "Context is nullptr"));
224 WLOGFE("Context is nullptr");
225 return;
226 }
227 if (windowOption->GetWindowType() == WindowType::WINDOW_TYPE_FLOAT ||
228 windowOption->GetWindowType() == WindowType::WINDOW_TYPE_FLOAT_CAMERA) {
229 auto abilityContext = Context::ConvertTo<AbilityRuntime::AbilityContext>(context->lock());
230 if (abilityContext != nullptr) {
231 if (!CheckCallingPermission("ohos.permission.SYSTEM_FLOAT_WINDOW")) {
232 int32_t err = static_cast<int32_t>(WmErrorCode::WM_ERROR_NO_PERMISSION);
233 task.Reject(engine, CreateJsError(engine, err, "TYPE_FLOAT CheckCallingPermission failed"));
234 return;
235 }
236 }
237 }
238 WMError wmError = WMError::WM_OK;
239 sptr<Window> window = Window::Create(windowOption->GetWindowName(), windowOption, context->lock(), wmError);
240 WmErrorCode wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(wmError);
241 if (window != nullptr && wmErrorCode == WmErrorCode::WM_OK) {
242 task.Resolve(engine, CreateJsWindowObject(engine, window));
243 } else {
244 WLOGFE("Create window failed");
245 int32_t err = static_cast<int32_t>(wmErrorCode);
246 task.Reject(engine, CreateJsError(engine, err, "Create window failed"));
247 }
248 }
249
CreateSystemWindowTask(void * contextPtr,std::string windowName,WindowType winType,NativeEngine & engine,AsyncTask & task)250 static void CreateSystemWindowTask(void* contextPtr, std::string windowName, WindowType winType,
251 NativeEngine& engine, AsyncTask& task)
252 {
253 WLOGFD("CreateSystemWindowTask");
254 auto context = static_cast<std::weak_ptr<AbilityRuntime::Context>*>(contextPtr);
255 if (contextPtr == nullptr || context == nullptr) {
256 int32_t err = static_cast<int32_t>(WMError::WM_ERROR_NULLPTR);
257 task.Reject(engine, CreateJsError(engine, err, "Context is nullptr"));
258 WLOGFE("Context is nullptr");
259 return;
260 }
261 if (winType == WindowType::WINDOW_TYPE_FLOAT || winType == WindowType::WINDOW_TYPE_FLOAT_CAMERA) {
262 auto abilityContext = Context::ConvertTo<AbilityRuntime::AbilityContext>(context->lock());
263 if (abilityContext != nullptr) {
264 if (!CheckCallingPermission("ohos.permission.SYSTEM_FLOAT_WINDOW")) {
265 int32_t err = static_cast<int32_t>(WMError::WM_ERROR_INVALID_PERMISSION);
266 task.Reject(engine, CreateJsError(engine, err, "TYPE_FLOAT CheckCallingPermission failed"));
267 return;
268 }
269 }
270 }
271 sptr<WindowOption> windowOption = new(std::nothrow) WindowOption();
272 if (windowOption == nullptr) {
273 int32_t err = static_cast<int32_t>(WMError::WM_ERROR_NULLPTR);
274 task.Reject(engine, CreateJsError(engine, err, "New window option failed"));
275 WLOGFE("New window option failed");
276 return;
277 }
278 windowOption->SetWindowType(winType);
279 WMError wmError = WMError::WM_OK;
280 sptr<Window> window = Window::Create(windowName, windowOption, context->lock(), wmError);
281 WmErrorCode wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(wmError);
282 if (window != nullptr && wmErrorCode == WmErrorCode::WM_OK) {
283 task.Resolve(engine, CreateJsWindowObject(engine, window));
284 } else {
285 WLOGFE("Create window failed");
286 int32_t err = static_cast<int32_t>(wmErrorCode);
287 task.Reject(engine, CreateJsError(engine, err, "Create window failed"));
288 }
289 }
290
CreateNewSubWindowTask(sptr<WindowOption> windowOption,NativeEngine & engine,AsyncTask & task)291 static void CreateNewSubWindowTask(sptr<WindowOption> windowOption, NativeEngine& engine, AsyncTask& task)
292 {
293 if (windowOption == nullptr) {
294 int32_t err = static_cast<int32_t>(WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY);
295 task.Reject(engine, CreateJsError(engine, err, "New window option failed"));
296 WLOGFE("New window option failed");
297 return;
298 }
299 windowOption->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING);
300 if (windowOption->GetParentId() == INVALID_WINDOW_ID) {
301 uint32_t parentId = GetParentId(engine);
302 if (!parentId) {
303 int32_t err = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
304 task.Reject(engine, CreateJsError(engine, err, "parent window missed"));
305 WLOGFE("can not find parent window");
306 return;
307 }
308 windowOption->SetParentId(parentId);
309 }
310 sptr<Window> window = Window::Create(windowOption->GetWindowName(), windowOption);
311 if (window != nullptr) {
312 task.Resolve(engine, CreateJsWindowObject(engine, window));
313 } else {
314 WLOGFE("Create window failed");
315 int32_t err = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
316 task.Reject(engine, CreateJsError(engine, err, "Create window failed"));
317 }
318 }
319
CreateSubWindowTask(uint32_t parentWinId,std::string windowName,WindowType winType,NativeEngine & engine,AsyncTask & task,bool newErrorCode=false)320 static void CreateSubWindowTask(uint32_t parentWinId, std::string windowName, WindowType winType,
321 NativeEngine& engine, AsyncTask& task, bool newErrorCode = false)
322 {
323 WLOGI("CreateSubWindowTask, parent id = %{public}u", parentWinId);
324 sptr<WindowOption> windowOption = new(std::nothrow) WindowOption();
325 if (windowOption == nullptr) {
326 int32_t err = newErrorCode ? static_cast<int32_t>(WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY)
327 : static_cast<int32_t>(WMError::WM_ERROR_NULLPTR);
328 task.Reject(engine, CreateJsError(engine, err, "New window option failed"));
329 WLOGFE("New window option failed");
330 return;
331 }
332 windowOption->SetWindowType(winType);
333 windowOption->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING);
334 windowOption->SetParentId(parentWinId);
335 sptr<Window> window = Window::Create(windowName, windowOption);
336 if (window != nullptr) {
337 task.Resolve(engine, CreateJsWindowObject(engine, window));
338 } else {
339 WLOGFE("Create window failed");
340 int32_t err = newErrorCode ? static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)
341 : static_cast<int32_t>(WMError::WM_ERROR_NULLPTR);
342 task.Reject(engine, CreateJsError(engine, err, "Create window failed"));
343 }
344 }
345
isConfigOptionWindowTypeValid(NativeEngine & engine,WindowOption & option)346 static bool isConfigOptionWindowTypeValid(NativeEngine& engine, WindowOption& option)
347 {
348 WindowType type = option.GetWindowType();
349 AppExecFwk::Ability* ability = nullptr;
350 bool isOldApi = GetAPI7Ability(engine, ability);
351 if (isOldApi) {
352 if (ability == nullptr || !WindowHelper::IsSubWindow(type)) {
353 WLOGE("FA mode GetAPI7Ability failed or convert parameter to invalid winType %{public}u", type);
354 return false;
355 }
356 } else {
357 if (!WindowHelper::IsSystemWindow(type)) {
358 WLOGFE("Stage mode convert parameter to invalid winType %{public}u", type);
359 return false;
360 }
361 }
362
363 return true;
364 }
365
OnCreate(NativeEngine & engine,NativeCallbackInfo & info)366 NativeValue* JsWindowManager::OnCreate(NativeEngine& engine, NativeCallbackInfo& info)
367 {
368 WLOGFD("OnCreate");
369 NativeValue* nativeString = nullptr;
370 NativeValue* nativeContext = nullptr;
371 NativeValue* nativeType = nullptr;
372 NativeValue* callback = nullptr;
373 if (info.argc >= 2 && info.argv[0]->TypeOf() == NATIVE_STRING) { // 2: minimum params num
374 nativeString = info.argv[0];
375 nativeType = info.argv[1];
376 // 2: minimum params num
377 callback = (info.argc == 2) ? nullptr :
378 (info.argv[2]->TypeOf() == NATIVE_FUNCTION ? info.argv[2] : nullptr); // 2: index of callback
379 } else if (info.argc >= 3) { // 3: minimum params num
380 nativeContext = info.argv[0]->TypeOf() == NATIVE_OBJECT ? info.argv[0] : nullptr;
381 nativeString = info.argv[1];
382 nativeType = info.argv[2]; // 2: index of type
383 // 3: minimum params num;
384 callback = (info.argc == 3) ? nullptr :
385 (info.argv[3]->TypeOf() == NATIVE_FUNCTION ? info.argv[3] : nullptr); // 3: index of callback
386 }
387 std::string windowName;
388 WMError errCode = WMError::WM_OK;
389 if (!ConvertFromJsValue(engine, nativeString, windowName)) {
390 WLOGFE("Failed to convert parameter to windowName");
391 errCode = WMError::WM_ERROR_INVALID_PARAM;
392 }
393 uint32_t parentId = INVALID_WINDOW_ID;
394 WindowType winType = WindowType::SYSTEM_WINDOW_BASE;
395 if (errCode == WMError::WM_OK &&
396 !GetWindowTypeAndParentId(engine, parentId, winType, nativeString, nativeType)) {
397 errCode = WMError::WM_ERROR_INVALID_PARAM;
398 }
399 void* contextPtr = nullptr;
400 GetNativeContext(engine, nativeContext, contextPtr, errCode);
401
402 WLOGFD("Window name = %{public}s, type = %{public}u, err = %{public}d", windowName.c_str(), winType, errCode);
403 AsyncTask::CompleteCallback complete =
404 [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
405 if (errCode != WMError::WM_OK) {
406 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params"));
407 return;
408 }
409 if (parentId == INVALID_WINDOW_ID) {
410 return CreateSystemWindowTask(contextPtr, windowName, winType, engine, task);
411 } else {
412 return CreateSubWindowTask(parentId, windowName, winType, engine, task);
413 }
414 };
415 NativeValue* result = nullptr;
416 AsyncTask::Schedule("JsWindowManager::OnCreate", engine,
417 CreateAsyncTaskWithLastParam(engine, callback, nullptr, std::move(complete), &result));
418 return result;
419 }
420
ParseConfigOption(NativeEngine & engine,NativeObject * jsObject,WindowOption & option,void * & contextPtr)421 bool JsWindowManager::ParseConfigOption(NativeEngine& engine, NativeObject* jsObject,
422 WindowOption& option, void*& contextPtr)
423 {
424 std::string windowName;
425 if (ParseJsValue(jsObject, engine, "name", windowName)) {
426 option.SetWindowName(windowName);
427 } else {
428 WLOGFE("Failed to convert parameter to windowName");
429 return false;
430 }
431
432 uint32_t winType;
433 if (ParseJsValue(jsObject, engine, "windowType", winType)) {
434 if (winType >= static_cast<uint32_t>(ApiWindowType::TYPE_BASE) &&
435 winType < static_cast<uint32_t>(ApiWindowType::TYPE_END)) {
436 option.SetWindowType(JS_TO_NATIVE_WINDOW_TYPE_MAP.at(static_cast<ApiWindowType>(winType)));
437 } else {
438 option.SetWindowType(static_cast<WindowType>(winType));
439 }
440 } else {
441 WLOGFE("Failed to convert parameter to winType");
442 return false;
443 }
444
445 if (!isConfigOptionWindowTypeValid(engine, option)) {
446 return false;
447 }
448
449 NativeValue* value = jsObject->GetProperty("ctx");
450 if (value->TypeOf() == NATIVE_UNDEFINED) {
451 return true;
452 }
453 WMError errCode = WMError::WM_OK;
454 GetNativeContext(engine, value, contextPtr, errCode);
455 if (errCode != WMError::WM_OK) {
456 return false;
457 }
458
459 int64_t displayId = static_cast<int64_t>(DISPLAY_ID_INVALID);
460 if (ParseJsValue(jsObject, engine, "displayId", displayId)) {
461 if (displayId < 0 ||
462 SingletonContainer::Get<DisplayManager>().GetDisplayById(static_cast<uint64_t>(displayId)) == nullptr) {
463 return false;
464 }
465 option.SetDisplayId(displayId);
466 } else {
467 return true;
468 }
469
470 int64_t parentId = -1;
471 if (ParseJsValue(jsObject, engine, "parentId", parentId)) {
472 option.SetParentId(parentId);
473 }
474
475 return true;
476 }
477
OnCreateWindow(NativeEngine & engine,NativeCallbackInfo & info)478 NativeValue* JsWindowManager::OnCreateWindow(NativeEngine& engine, NativeCallbackInfo& info)
479 {
480 WLOGFD("Called");
481 if (info.argc < 1) {
482 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
483 return engine.CreateUndefined();
484 }
485 NativeObject* nativeObj = ConvertNativeValueTo<NativeObject>(info.argv[0]);
486 if (nativeObj == nullptr) {
487 WLOGFE("Failed to convert object to CreateWindow");
488 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
489 return engine.CreateUndefined();
490 }
491 WindowOption option;
492 void* contextPtr = nullptr;
493 if (!ParseConfigOption(engine, nativeObj, option, contextPtr)) {
494 WLOGFE("Failed to parse config");
495 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
496 return engine.CreateUndefined();
497 }
498 NativeValue* callback = nullptr;
499 if (info.argc > 1) {
500 callback = info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr; // 1: index of callback
501 }
502 AsyncTask::CompleteCallback complete =
503 [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
504 sptr<WindowOption> windowOption = new WindowOption(option);
505 if (WindowHelper::IsSystemWindow(option.GetWindowType())) {
506 return CreateNewSystemWindowTask(contextPtr, windowOption, engine, task);
507 }
508 if (WindowHelper::IsSubWindow(option.GetWindowType())) {
509 return CreateNewSubWindowTask(windowOption, engine, task);
510 }
511 };
512
513 NativeValue* result = nullptr;
514 AsyncTask::Schedule("JsWindowManager::OnCreateWindow", engine,
515 CreateAsyncTaskWithLastParam(engine, callback, nullptr, std::move(complete), &result));
516 return result;
517 }
518
OnFindWindow(NativeEngine & engine,NativeCallbackInfo & info)519 NativeValue* JsWindowManager::OnFindWindow(NativeEngine& engine, NativeCallbackInfo& info)
520 {
521 WLOGFD("OnFindWindow");
522 std::string windowName;
523 WMError errCode = WMError::WM_OK;
524 if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
525 WLOGFE("Argc is invalid: %{public}zu", info.argc);
526 errCode = WMError::WM_ERROR_INVALID_PARAM;
527 } else {
528 if (!ConvertFromJsValue(engine, info.argv[0], windowName)) {
529 WLOGFE("Failed to convert parameter to windowName");
530 errCode = WMError::WM_ERROR_INVALID_PARAM;
531 }
532 }
533 WLOGI("Window name = %{public}s, err = %{public}d", windowName.c_str(), errCode);
534 AsyncTask::CompleteCallback complete =
535 [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
536 if (errCode != WMError::WM_OK) {
537 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params"));
538 return;
539 }
540 HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "WM:Find %s", windowName.c_str());
541 std::shared_ptr<NativeReference> jsWindowObj = FindJsWindowObject(windowName);
542 if (jsWindowObj != nullptr && jsWindowObj->Get() != nullptr) {
543 WLOGI("Find window: %{public}s, use exist js window", windowName.c_str());
544 task.Resolve(engine, jsWindowObj->Get());
545 } else {
546 sptr<Window> window = Window::Find(windowName);
547 if (window == nullptr) {
548 WLOGFE("Cannot find window: %{public}s", windowName.c_str());
549 task.Reject(engine, CreateJsError(engine,
550 static_cast<int32_t>(WMError::WM_ERROR_NULLPTR), "Cannot find window"));
551 } else {
552 task.Resolve(engine, CreateJsWindowObject(engine, window));
553 WLOGI("Find window: %{public}s, create js window", windowName.c_str());
554 }
555 }
556 };
557
558 NativeValue* lastParam = (info.argc <= 1) ? nullptr :
559 (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
560 NativeValue* result = nullptr;
561 AsyncTask::Schedule("JsWindowManager::OnFindWindow",
562 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
563 return result;
564 }
565
OnFindWindowSync(NativeEngine & engine,NativeCallbackInfo & info)566 NativeValue* JsWindowManager::OnFindWindowSync(NativeEngine& engine, NativeCallbackInfo& info)
567 {
568 WLOGFD("OnFindWindowSync");
569 std::string windowName;
570 WmErrorCode errCode = WmErrorCode::WM_OK;
571 if (info.argc < 1) { // 1: params num
572 WLOGFE("Argc is invalid: %{public}zu", info.argc);
573 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
574 } else {
575 if (!ConvertFromJsValue(engine, info.argv[0], windowName)) {
576 WLOGFE("Failed to convert parameter to windowName");
577 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
578 }
579 }
580 if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
581 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
582 return engine.CreateUndefined();
583 }
584
585 WLOGI("Window name = %{public}s, err = %{public}d", windowName.c_str(), errCode);
586 std::shared_ptr<NativeReference> jsWindowObj = FindJsWindowObject(windowName);
587 if (jsWindowObj != nullptr && jsWindowObj->Get() != nullptr) {
588 WLOGI("Find window: %{public}s, use exist js window", windowName.c_str());
589 return jsWindowObj->Get();
590 } else {
591 sptr<Window> window = Window::Find(windowName);
592 if (window == nullptr) {
593 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
594 return engine.CreateUndefined();
595 } else {
596 return CreateJsWindowObject(engine, window);
597 }
598 }
599 }
600
OnMinimizeAll(NativeEngine & engine,NativeCallbackInfo & info)601 NativeValue* JsWindowManager::OnMinimizeAll(NativeEngine& engine, NativeCallbackInfo& info)
602 {
603 WLOGI("OnMinimizeAll");
604 WmErrorCode errCode = WmErrorCode::WM_OK;
605 if (info.argc < 1) {
606 WLOGFE("Argc is invalid: %{public}zu", info.argc);
607 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
608 }
609 int64_t displayId = static_cast<int64_t>(DISPLAY_ID_INVALID);
610 if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(engine, info.argv[0], displayId)) {
611 WLOGFE("Failed to convert parameter to displayId");
612 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
613 }
614 if (displayId < 0 ||
615 SingletonContainer::Get<DisplayManager>().GetDisplayById(static_cast<uint64_t>(displayId)) == nullptr) {
616 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
617 }
618 if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
619 WLOGFE("JsWindowManager::OnMinimizeAll failed, Invalidate params.");
620 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
621 return engine.CreateUndefined();
622 }
623
624 WLOGI("Display id = %{public}" PRIu64", err = %{public}d", static_cast<uint64_t>(displayId), errCode);
625 AsyncTask::CompleteCallback complete =
626 [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
627 HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "WM:MinimizeAll: " PRIu64"",
628 static_cast<uint64_t>(displayId));
629 WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(
630 SingletonContainer::Get<WindowManager>().MinimizeAllAppWindows(static_cast<uint64_t>(displayId)));
631 if (ret == WmErrorCode::WM_OK) {
632 task.Resolve(engine, engine.CreateUndefined());
633 WLOGFI("OnMinimizeAll success");
634 } else {
635 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "OnMinimizeAll failed"));
636 }
637 };
638 NativeValue* lastParam = (info.argc <= 1) ? nullptr :
639 ((info.argv[1] != nullptr && info.argv[1]->TypeOf() == NATIVE_FUNCTION) ?
640 info.argv[1] : nullptr);
641 NativeValue* result = nullptr;
642 AsyncTask::Schedule("JsWindowManager::OnMinimizeAll",
643 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
644 return result;
645 }
646
OnToggleShownStateForAllAppWindows(NativeEngine & engine,NativeCallbackInfo & info)647 NativeValue* JsWindowManager::OnToggleShownStateForAllAppWindows(NativeEngine& engine, NativeCallbackInfo& info)
648 {
649 WLOGI("OnToggleShownStateForAllAppWindows");
650 AsyncTask::CompleteCallback complete =
651 [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
652 WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(
653 SingletonContainer::Get<WindowManager>().ToggleShownStateForAllAppWindows());
654 if (ret == WmErrorCode::WM_OK) {
655 task.Resolve(engine, engine.CreateUndefined());
656 WLOGI("OnToggleShownStateForAllAppWindows success");
657 } else {
658 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret),
659 "OnToggleShownStateForAllAppWindows failed"));
660 }
661 };
662 NativeValue* lastParam = (info.argc <= 0) ? nullptr :
663 (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
664 NativeValue* result = nullptr;
665 AsyncTask::Schedule("JsWindowManager::OnToggleShownStateForAllAppWindows",
666 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
667 return result;
668 }
669
OnRegisterWindowManagerCallback(NativeEngine & engine,NativeCallbackInfo & info)670 NativeValue* JsWindowManager::OnRegisterWindowManagerCallback(NativeEngine& engine, NativeCallbackInfo& info)
671 {
672 WLOGFD("OnRegisterWindowManagerCallback");
673 if (info.argc < 2) { // 2: params num
674 WLOGFE("Argc is invalid: %{public}zu", info.argc);
675 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
676 return engine.CreateUndefined();
677 }
678 std::string cbType;
679 if (!ConvertFromJsValue(engine, info.argv[0], cbType)) {
680 WLOGFE("Failed to convert parameter to callbackType");
681 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
682 return engine.CreateUndefined();
683 }
684 NativeValue* value = info.argv[1];
685 if (!value->IsCallable()) {
686 WLOGI("Callback(argv[1]) is not callable");
687 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
688 return engine.CreateUndefined();
689 }
690
691 WmErrorCode ret = registerManager_->RegisterListener(nullptr, cbType, CaseType::CASE_WINDOW_MANAGER, engine, value);
692 if (ret != WmErrorCode::WM_OK) {
693 engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
694 return engine.CreateUndefined();
695 }
696 WLOGI("Register end, type = %{public}s", cbType.c_str());
697 return engine.CreateUndefined();
698 }
699
OnUnregisterWindowManagerCallback(NativeEngine & engine,NativeCallbackInfo & info)700 NativeValue* JsWindowManager::OnUnregisterWindowManagerCallback(NativeEngine& engine, NativeCallbackInfo& info)
701 {
702 WLOGFD("OnUnregisterWindowManagerCallback");
703 if (info.argc < 1) {
704 WLOGFE("Argc is invalid: %{public}zu", info.argc);
705 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
706 return engine.CreateUndefined();
707 }
708 std::string cbType;
709 if (!ConvertFromJsValue(engine, info.argv[0], cbType)) {
710 WLOGFE("Failed to convert parameter to callbackType");
711 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
712 return engine.CreateUndefined();
713 }
714
715 NativeValue* value = nullptr;
716 WmErrorCode ret = WmErrorCode::WM_OK;
717 if (info.argc == 1) {
718 ret = registerManager_->UnregisterListener(nullptr, cbType, CaseType::CASE_WINDOW_MANAGER, value);
719 } else {
720 value = info.argv[1];
721 if ((value == nullptr) || (!value->IsCallable())) {
722 ret = registerManager_->UnregisterListener(nullptr, cbType, CaseType::CASE_WINDOW_MANAGER, nullptr);
723 } else {
724 ret = registerManager_->UnregisterListener(nullptr, cbType, CaseType::CASE_WINDOW_MANAGER, value);
725 }
726 }
727 if (ret != WmErrorCode::WM_OK) {
728 engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
729 return engine.CreateUndefined();
730 }
731 WLOGI("Unregister end, type = %{public}s", cbType.c_str());
732 return engine.CreateUndefined();
733 }
734
GetTopWindowTask(void * contextPtr,NativeEngine & engine,AsyncTask & task,bool newApi)735 static void GetTopWindowTask(void* contextPtr, NativeEngine& engine, AsyncTask& task, bool newApi)
736 {
737 std::string windowName;
738 sptr<Window> window = nullptr;
739 AppExecFwk::Ability* ability = nullptr;
740 bool isOldApi = GetAPI7Ability(engine, ability);
741 int32_t error;
742 if (isOldApi) {
743 if (ability->GetWindow() == nullptr) {
744 error = newApi ? static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY) :
745 static_cast<int32_t>(WMError::WM_ERROR_NULLPTR);
746 task.Reject(engine, CreateJsError(engine, error, "FA mode can not get ability window"));
747 WLOGE("FA mode can not get ability window");
748 return;
749 }
750 window = Window::GetTopWindowWithId(ability->GetWindow()->GetWindowId());
751 } else {
752 auto context = static_cast<std::weak_ptr<AbilityRuntime::Context>*>(contextPtr);
753 if (contextPtr == nullptr || context == nullptr) {
754 error = newApi ? static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY) :
755 static_cast<int32_t>(WMError::WM_ERROR_NULLPTR);
756 task.Reject(engine, CreateJsError(engine, error, "Stage mode without context"));
757 WLOGFE("Stage mode without context");
758 return;
759 }
760 window = Window::GetTopWindowWithContext(context->lock());
761 }
762 if (window == nullptr) {
763 error = newApi ? static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY) :
764 static_cast<int32_t>(WMError::WM_ERROR_NULLPTR);
765 task.Reject(engine, CreateJsError(engine, error, "Get top window failed"));
766 WLOGFE("Get top window failed");
767 return;
768 }
769 windowName = window->GetWindowName();
770 std::shared_ptr<NativeReference> jsWindowObj = FindJsWindowObject(windowName);
771 if (jsWindowObj != nullptr && jsWindowObj->Get() != nullptr) {
772 task.Resolve(engine, jsWindowObj->Get());
773 } else {
774 task.Resolve(engine, CreateJsWindowObject(engine, window));
775 }
776 WLOGI("Get top window %{public}s success", windowName.c_str());
777 return;
778 }
779
OnGetTopWindow(NativeEngine & engine,NativeCallbackInfo & info)780 NativeValue* JsWindowManager::OnGetTopWindow(NativeEngine& engine, NativeCallbackInfo& info)
781 {
782 WLOGFD("OnGetTopWindow");
783 WMError errCode = WMError::WM_OK;
784 NativeValue* nativeContext = nullptr;
785 NativeValue* nativeCallback = nullptr;
786 void* contextPtr = nullptr;
787 if (info.argc > 2) { // 2: maximum params num
788 WLOGFE("Argc is invalid: %{public}zu", info.argc);
789 errCode = WMError::WM_ERROR_INVALID_PARAM;
790 } else {
791 if (info.argc > 0 && info.argv[0]->TypeOf() == NATIVE_OBJECT) { // (context, callback?)
792 nativeContext = info.argv[0];
793 nativeCallback = (info.argc == 1) ? nullptr :
794 (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
795 } else { // (callback?)
796 nativeCallback = (info.argc == 0) ? nullptr :
797 (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
798 }
799 GetNativeContext(engine, nativeContext, contextPtr, errCode);
800 }
801
802 WLOGI("err %{public}u", errCode);
803 AsyncTask::CompleteCallback complete =
804 [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
805 if (errCode != WMError::WM_OK) {
806 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params"));
807 return;
808 }
809 return GetTopWindowTask(contextPtr, engine, task, false);
810 };
811 NativeValue* result = nullptr;
812 AsyncTask::Schedule("JsWindowManager::OnGetTopWindow",
813 engine, CreateAsyncTaskWithLastParam(engine, nativeCallback, nullptr, std::move(complete), &result));
814 return result;
815 }
816
OnGetLastWindow(NativeEngine & engine,NativeCallbackInfo & info)817 NativeValue* JsWindowManager::OnGetLastWindow(NativeEngine& engine, NativeCallbackInfo& info)
818 {
819 WLOGFD("OnGetLastWindow");
820 WMError errCode = WMError::WM_OK;
821 NativeValue* nativeContext = nullptr;
822 NativeValue* nativeCallback = nullptr;
823 void* contextPtr = nullptr;
824 if (info.argc < 1) {
825 WLOGFE("Argc is invalid: %{public}zu", info.argc);
826 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
827 return engine.CreateUndefined();
828 } else {
829 nativeContext = info.argv[0];
830 nativeCallback = (info.argc == 1) ? nullptr : info.argv[1];
831 GetNativeContext(engine, nativeContext, contextPtr, errCode);
832 }
833 if (errCode != WMError::WM_OK) {
834 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
835 return engine.CreateUndefined();
836 }
837
838 AsyncTask::CompleteCallback complete =
839 [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
840 return GetTopWindowTask(contextPtr, engine, task, true);
841 };
842 NativeValue* result = nullptr;
843 AsyncTask::Schedule("JsWindowManager::OnGetTopWindow",
844 engine, CreateAsyncTaskWithLastParam(engine, nativeCallback, nullptr, std::move(complete), &result));
845 return result;
846 }
847
OnSetWindowLayoutMode(NativeEngine & engine,NativeCallbackInfo & info)848 NativeValue* JsWindowManager::OnSetWindowLayoutMode(NativeEngine& engine, NativeCallbackInfo& info)
849 {
850 WLOGFD("OnSetWindowLayoutMode");
851 WmErrorCode errCode = WmErrorCode::WM_OK;
852 if (info.argc < 1) { // 1: minimum params num
853 WLOGFE("Argc is invalid: %{public}zu", info.argc);
854 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
855 }
856 WindowLayoutMode winLayoutMode = WindowLayoutMode::CASCADE;
857 if (errCode == WmErrorCode::WM_OK) {
858 NativeNumber* nativeMode = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
859 if (nativeMode == nullptr) {
860 WLOGFE("Failed to convert parameter to windowLayoutMode");
861 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
862 } else {
863 winLayoutMode = static_cast<WindowLayoutMode>(static_cast<uint32_t>(*nativeMode));
864 }
865 }
866 if (winLayoutMode != WindowLayoutMode::CASCADE && winLayoutMode != WindowLayoutMode::TILE) {
867 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
868 }
869 if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
870 WLOGFE("JsWindowManager::OnSetWindowLayoutMode failed, Invalidate params.");
871 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
872 return engine.CreateUndefined();
873 }
874
875 WLOGI("LayoutMode = %{public}u, err = %{public}d", winLayoutMode, errCode);
876 AsyncTask::CompleteCallback complete =
877 [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
878 WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(
879 SingletonContainer::Get<WindowManager>().SetWindowLayoutMode(winLayoutMode));
880 if (ret == WmErrorCode::WM_OK) {
881 task.Resolve(engine, engine.CreateUndefined());
882 WLOGD("SetWindowLayoutMode success");
883 } else {
884 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "SetWindowLayoutMode failed"));
885 }
886 };
887 // 1: maximum params num; 1: index of callback
888 NativeValue* lastParam = (info.argc <= 1) ? nullptr :
889 ((info.argv[1] != nullptr && info.argv[1]->TypeOf() == NATIVE_FUNCTION) ?
890 info.argv[1] : nullptr);
891 NativeValue* result = nullptr;
892 AsyncTask::Schedule("JsWindowManager::OnSetWindowLayoutMode",
893 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
894 return result;
895 }
896
OnSetGestureNavigationEnabled(NativeEngine & engine,NativeCallbackInfo & info)897 NativeValue* JsWindowManager::OnSetGestureNavigationEnabled(NativeEngine& engine, NativeCallbackInfo& info)
898 {
899 WLOGFD("OnSetGestureNavigationEnabled");
900 if (info.argc < 1) { // 1: minimum params num
901 WLOGFE("Argc is invalid: %{public}zu", info.argc);
902 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
903 return engine.CreateUndefined();
904 }
905
906 NativeBoolean* nativeBool = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
907 if (nativeBool == nullptr) {
908 WLOGFE("Failed to convert parameter to bool");
909 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
910 return engine.CreateUndefined();
911 }
912 bool gestureNavigationEnable = static_cast<bool>(*nativeBool);
913
914 WLOGI("Set gesture navigation enable as %{public}d", gestureNavigationEnable);
915 AsyncTask::CompleteCallback complete =
916 [gestureNavigationEnable](NativeEngine& engine, AsyncTask& task, int32_t status) {
917 WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(
918 SingletonContainer::Get<WindowManager>().SetGestureNavigaionEnabled(gestureNavigationEnable));
919 if (ret == WmErrorCode::WM_OK) {
920 task.Resolve(engine, engine.CreateUndefined());
921 WLOGD("SetGestureNavigationEnabled success");
922 } else {
923 task.Reject(engine, CreateJsError(engine,
924 static_cast<int32_t>(ret), "SetGestureNavigationEnabled failed"));
925 }
926 };
927 // 1: maximum params num; 1: index of callback
928 NativeValue* lastParam = (info.argc <= 1) ? nullptr :
929 ((info.argv[1] != nullptr && info.argv[1]->TypeOf() == NATIVE_FUNCTION) ?
930 info.argv[1] : nullptr);
931 NativeValue* result = nullptr;
932 AsyncTask::Schedule("JsWindowManager::OnSetGestureNavigationEnabled",
933 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
934 return result;
935 }
936
OnSetWaterMarkImage(NativeEngine & engine,NativeCallbackInfo & info)937 NativeValue* JsWindowManager::OnSetWaterMarkImage(NativeEngine& engine, NativeCallbackInfo& info)
938 {
939 WLOGFD("OnSetWaterMarkImage");
940 NativeValue* nativeObject = nullptr;
941 NativeValue* nativeBoolean = nullptr;
942 if (info.argc < 2) { // 2: params num
943 WLOGFE("Argc is invalid: %{public}zu", info.argc);
944 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
945 return engine.CreateUndefined();
946 } else {
947 if (info.argc > 0 && info.argv[0]->TypeOf() == NATIVE_OBJECT) {
948 nativeObject = info.argv[0];
949 nativeBoolean = (info.argc == 1) ? nullptr :
950 (info.argv[1]->TypeOf() == NATIVE_BOOLEAN ? info.argv[1] : nullptr);
951 }
952 }
953
954 std::shared_ptr<Media::PixelMap> pixelMap;
955 pixelMap = OHOS::Media::PixelMapNapi::GetPixelMap(reinterpret_cast<napi_env>(&engine),
956 reinterpret_cast<napi_value>(nativeObject));
957 if (pixelMap == nullptr) {
958 WLOGFE("Failed to convert parameter to PixelMap");
959 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
960 return engine.CreateUndefined();
961 }
962
963 NativeBoolean* nativeBool = ConvertNativeValueTo<NativeBoolean>(nativeBoolean);
964 if (nativeBool == nullptr) {
965 WLOGFE("Failed to convert parameter to bool");
966 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
967 return engine.CreateUndefined();
968 }
969 bool isShow = static_cast<bool>(*nativeBool);
970
971 if (!Permission::IsSystemCalling()) {
972 WLOGFE("set watermark image permission denied!");
973 engine.Throw(CreateJsError(engine, static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP)));
974 return engine.CreateUndefined();
975 }
976
977 AsyncTask::CompleteCallback complete =
978 [=](NativeEngine& engine, AsyncTask& task, int32_t status) {
979 RSInterfaces::GetInstance().ShowWatermark(pixelMap, isShow);
980 task.Resolve(engine, engine.CreateUndefined());
981 WLOGD("OnSetWaterMarkImage success");
982 };
983 // 2: maximum params num; 2: index of callback
984 NativeValue* lastParam = (info.argc <= 2) ? nullptr :
985 (info.argv[2]->TypeOf() == NATIVE_FUNCTION ? info.argv[2] : nullptr);
986 NativeValue* result = nullptr;
987 AsyncTask::Schedule("JsWindowManager::OnSetWaterMarkImage",
988 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
989 return result;
990 }
991
JsWindowManagerInit(NativeEngine * engine,NativeValue * exportObj)992 NativeValue* JsWindowManagerInit(NativeEngine* engine, NativeValue* exportObj)
993 {
994 WLOGFD("JsWindowManagerInit");
995
996 if (engine == nullptr || exportObj == nullptr) {
997 WLOGFE("JsWindowManagerInit engine or exportObj is nullptr");
998 return nullptr;
999 }
1000
1001 NativeObject* object = ConvertNativeValueTo<NativeObject>(exportObj);
1002 if (object == nullptr) {
1003 WLOGFE("JsWindowManagerInit object is nullptr");
1004 return nullptr;
1005 }
1006
1007 std::unique_ptr<JsWindowManager> jsWinManager = std::make_unique<JsWindowManager>();
1008 object->SetNativePointer(jsWinManager.release(), JsWindowManager::Finalizer, nullptr);
1009 object->SetProperty("WindowType", WindowTypeInit(engine));
1010 object->SetProperty("AvoidAreaType", AvoidAreaTypeInit(engine));
1011 object->SetProperty("WindowMode", WindowModeInit(engine));
1012 object->SetProperty("ColorSpace", ColorSpaceInit(engine));
1013 object->SetProperty("WindowStageEventType", WindowStageEventTypeInit(engine));
1014 object->SetProperty("WindowEventType", WindowEventTypeInit(engine));
1015 object->SetProperty("WindowLayoutMode", WindowLayoutModeInit(engine));
1016 object->SetProperty("Orientation", OrientationInit(engine));
1017 object->SetProperty("BlurStyle", BlurStyleInit(engine));
1018 object->SetProperty("WmErrorCode", WindowErrorCodeInit(engine));
1019 object->SetProperty("WMError", WindowErrorInit(engine));
1020 const char *moduleName = "JsWindowManager";
1021 BindNativeFunction(*engine, *object, "create", moduleName, JsWindowManager::Create);
1022 BindNativeFunction(*engine, *object, "createWindow", moduleName, JsWindowManager::CreateWindow);
1023 BindNativeFunction(*engine, *object, "find", moduleName, JsWindowManager::FindWindow);
1024 BindNativeFunction(*engine, *object, "findWindow", moduleName, JsWindowManager::FindWindowSync);
1025 BindNativeFunction(*engine, *object, "on", moduleName, JsWindowManager::RegisterWindowManagerCallback);
1026 BindNativeFunction(*engine, *object, "off", moduleName, JsWindowManager::UnregisterWindowMangerCallback);
1027 BindNativeFunction(*engine, *object, "getTopWindow", moduleName, JsWindowManager::GetTopWindow);
1028 BindNativeFunction(*engine, *object, "getLastWindow", moduleName, JsWindowManager::GetLastWindow);
1029 BindNativeFunction(*engine, *object, "minimizeAll", moduleName, JsWindowManager::MinimizeAll);
1030 BindNativeFunction(*engine, *object, "toggleShownStateForAllAppWindows", moduleName,
1031 JsWindowManager::ToggleShownStateForAllAppWindows);
1032 BindNativeFunction(*engine, *object, "setWindowLayoutMode", moduleName, JsWindowManager::SetWindowLayoutMode);
1033 BindNativeFunction(*engine, *object, "setGestureNavigationEnabled", moduleName,
1034 JsWindowManager::SetGestureNavigationEnabled);
1035 BindNativeFunction(*engine, *object, "setWaterMarkImage", moduleName, JsWindowManager::SetWaterMarkImage);
1036 return engine->CreateUndefined();
1037 }
1038 } // namespace Rosen
1039 } // namespace OHOS
1040