1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "window_session_impl.h"
17
18 #include <cstdlib>
19
20 #include <common/rs_common_def.h>
21 #include <filesystem>
22 #include <fstream>
23 #include <ipc_skeleton.h>
24 #include <hisysevent.h>
25 #include <parameters.h>
26 #include <int_wrapper.h>
27 #ifdef IMF_ENABLE
28 #include <input_method_controller.h>
29 #endif // IMF_ENABLE
30 #include <transaction/rs_interfaces.h>
31 #include <transaction/rs_transaction.h>
32
33 #include "color_parser.h"
34 #include "common/include/fold_screen_state_internel.h"
35 #include "common/include/fold_screen_common.h"
36 #include "display_info.h"
37 #include "display_manager.h"
38 #include "extension/extension_business_info.h"
39 #include "hitrace_meter.h"
40 #include "scene_board_judgement.h"
41 #include "session_permission.h"
42 #include "key_event.h"
43 #include "session/container/include/window_event_channel.h"
44 #include "session_manager/include/session_manager.h"
45 #include "vsync_station.h"
46 #include "window_adapter.h"
47 #include "window_inspector.h"
48 #include "window_manager_hilog.h"
49 #include "window_helper.h"
50 #include "color_parser.h"
51 #include "singleton_container.h"
52 #include "perform_reporter.h"
53 #include "picture_in_picture_manager.h"
54 #include "parameters.h"
55
56 namespace OHOS::Accessibility {
57 class AccessibilityEventInfo;
58 }
59 namespace OHOS {
60 namespace Rosen {
61 namespace {
62 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowSessionImpl"};
63 constexpr int32_t FORCE_SPLIT_MODE = 5;
64 constexpr int32_t API_VERSION_18 = 18;
65 constexpr uint32_t API_VERSION_MOD = 1000;
66 constexpr uint32_t LIFECYCLE_ISOLATE_VERSION = 18;
67 constexpr uint32_t INVALID_TARGET_API_VERSION = 0;
68 /*
69 * DFX
70 */
71 const std::string SET_UIEXTENSION_DESTROY_TIMEOUT_LISTENER_TASK_NAME = "SetUIExtDestroyTimeoutListener";
72 constexpr int64_t SET_UIEXTENSION_DESTROY_TIMEOUT_TIME_MS = 4000;
73
GetAceContentInfoType(BackupAndRestoreType type)74 Ace::ContentInfoType GetAceContentInfoType(BackupAndRestoreType type)
75 {
76 auto contentInfoType = Ace::ContentInfoType::NONE;
77 switch (type) {
78 case BackupAndRestoreType::CONTINUATION:
79 contentInfoType = Ace::ContentInfoType::CONTINUATION;
80 break;
81 case BackupAndRestoreType::APP_RECOVERY:
82 contentInfoType = Ace::ContentInfoType::APP_RECOVERY;
83 break;
84 case BackupAndRestoreType::RESOURCESCHEDULE_RECOVERY:
85 contentInfoType = Ace::ContentInfoType::RESOURCESCHEDULE_RECOVERY;
86 break;
87 case BackupAndRestoreType::NONE:
88 [[fallthrough]];
89 default:
90 break;
91 }
92 return contentInfoType;
93 }
94
FillViewportConfig(Rect rect,float density,int32_t orientation,uint32_t transformHint,uint64_t displayId)95 Ace::ViewportConfig FillViewportConfig(
96 Rect rect, float density, int32_t orientation, uint32_t transformHint, uint64_t displayId)
97 {
98 Ace::ViewportConfig config;
99 config.SetSize(rect.width_, rect.height_);
100 config.SetPosition(rect.posX_, rect.posY_);
101 config.SetDensity(density);
102 config.SetOrientation(orientation);
103 config.SetTransformHint(transformHint);
104 config.SetDisplayId(displayId);
105 return config;
106 }
107 }
108
109 std::map<int32_t, std::vector<sptr<IWindowLifeCycle>>> WindowSessionImpl::lifecycleListeners_;
110 std::map<int32_t, std::vector<sptr<IDisplayMoveListener>>> WindowSessionImpl::displayMoveListeners_;
111 std::map<int32_t, std::vector<sptr<IWindowChangeListener>>> WindowSessionImpl::windowChangeListeners_;
112 std::map<int32_t, std::vector<sptr<IWindowCrossAxisListener>>> WindowSessionImpl::windowCrossAxisListeners_;
113 std::map<int32_t, std::vector<sptr<IAvoidAreaChangedListener>>> WindowSessionImpl::avoidAreaChangeListeners_;
114 std::map<int32_t, std::vector<sptr<IDialogDeathRecipientListener>>> WindowSessionImpl::dialogDeathRecipientListeners_;
115 std::map<int32_t, std::vector<sptr<IDialogTargetTouchListener>>> WindowSessionImpl::dialogTargetTouchListener_;
116 std::map<int32_t, std::vector<sptr<IOccupiedAreaChangeListener>>> WindowSessionImpl::occupiedAreaChangeListeners_;
117 std::map<int32_t, std::vector<sptr<IKeyboardDidShowListener>>> WindowSessionImpl::keyboardDidShowListeners_;
118 std::map<int32_t, std::vector<sptr<IKeyboardDidHideListener>>> WindowSessionImpl::keyboardDidHideListeners_;
119 std::map<int32_t, std::vector<sptr<IScreenshotListener>>> WindowSessionImpl::screenshotListeners_;
120 std::map<int32_t, std::vector<sptr<ITouchOutsideListener>>> WindowSessionImpl::touchOutsideListeners_;
121 std::map<int32_t, std::vector<IWindowVisibilityListenerSptr>> WindowSessionImpl::windowVisibilityChangeListeners_;
122 std::mutex WindowSessionImpl::displayIdChangeListenerMutex_;
123 std::map<int32_t, std::vector<IDisplayIdChangeListenerSptr>> WindowSessionImpl::displayIdChangeListeners_;
124 std::mutex WindowSessionImpl::systemDensityChangeListenerMutex_;
125 std::unordered_map<int32_t, std::vector<ISystemDensityChangeListenerSptr>>
126 WindowSessionImpl::systemDensityChangeListeners_;
127 std::map<int32_t, std::vector<IWindowNoInteractionListenerSptr>> WindowSessionImpl::windowNoInteractionListeners_;
128 std::map<int32_t, std::vector<sptr<IWindowTitleButtonRectChangedListener>>>
129 WindowSessionImpl::windowTitleButtonRectChangeListeners_;
130 std::map<int32_t, std::vector<sptr<IWindowRectChangeListener>>> WindowSessionImpl::windowRectChangeListeners_;
131 std::map<int32_t, sptr<ISubWindowCloseListener>> WindowSessionImpl::subWindowCloseListeners_;
132 std::map<int32_t, sptr<IMainWindowCloseListener>> WindowSessionImpl::mainWindowCloseListeners_;
133 std::unordered_map<int32_t, std::vector<sptr<IWindowWillCloseListener>>> WindowSessionImpl::windowWillCloseListeners_;
134 std::map<int32_t, std::vector<sptr<ISwitchFreeMultiWindowListener>>> WindowSessionImpl::switchFreeMultiWindowListeners_;
135 std::map<int32_t, std::vector<sptr<IWindowHighlightChangeListener>>> WindowSessionImpl::highlightChangeListeners_;
136 std::recursive_mutex WindowSessionImpl::lifeCycleListenerMutex_;
137 std::recursive_mutex WindowSessionImpl::windowChangeListenerMutex_;
138 std::recursive_mutex WindowSessionImpl::windowCrossAxisListenerMutex_;
139 std::recursive_mutex WindowSessionImpl::avoidAreaChangeListenerMutex_;
140 std::recursive_mutex WindowSessionImpl::dialogDeathRecipientListenerMutex_;
141 std::recursive_mutex WindowSessionImpl::dialogTargetTouchListenerMutex_;
142 std::recursive_mutex WindowSessionImpl::occupiedAreaChangeListenerMutex_;
143 std::recursive_mutex WindowSessionImpl::keyboardDidShowListenerMutex_;
144 std::recursive_mutex WindowSessionImpl::keyboardDidHideListenerMutex_;
145 std::recursive_mutex WindowSessionImpl::screenshotListenerMutex_;
146 std::recursive_mutex WindowSessionImpl::touchOutsideListenerMutex_;
147 std::recursive_mutex WindowSessionImpl::windowVisibilityChangeListenerMutex_;
148 std::recursive_mutex WindowSessionImpl::windowNoInteractionListenerMutex_;
149 std::recursive_mutex WindowSessionImpl::windowStatusChangeListenerMutex_;
150 std::recursive_mutex WindowSessionImpl::windowTitleButtonRectChangeListenerMutex_;
151 std::mutex WindowSessionImpl::displayMoveListenerMutex_;
152 std::mutex WindowSessionImpl::windowRectChangeListenerMutex_;
153 std::mutex WindowSessionImpl::subWindowCloseListenersMutex_;
154 std::mutex WindowSessionImpl::mainWindowCloseListenersMutex_;
155 std::mutex WindowSessionImpl::windowWillCloseListenersMutex_;
156 std::mutex WindowSessionImpl::switchFreeMultiWindowListenerMutex_;
157 std::mutex WindowSessionImpl::highlightChangeListenerMutex_;
158 std::map<std::string, std::pair<int32_t, sptr<WindowSessionImpl>>> WindowSessionImpl::windowSessionMap_;
159 std::shared_mutex WindowSessionImpl::windowSessionMutex_;
160 std::set<sptr<WindowSessionImpl>> WindowSessionImpl::windowExtensionSessionSet_;
161 std::shared_mutex WindowSessionImpl::windowExtensionSessionMutex_;
162 std::recursive_mutex WindowSessionImpl::subWindowSessionMutex_;
163 std::map<int32_t, std::vector<sptr<WindowSessionImpl>>> WindowSessionImpl::subWindowSessionMap_;
164 std::map<int32_t, std::vector<sptr<IWindowStatusChangeListener>>> WindowSessionImpl::windowStatusChangeListeners_;
165 bool WindowSessionImpl::isUIExtensionAbilityProcess_ = false;
166
167 #define CALL_LIFECYCLE_LISTENER(windowLifecycleCb, listeners) \
168 do { \
169 for (auto& listener : (listeners)) { \
170 if (listener != nullptr) { \
171 listener->windowLifecycleCb(); \
172 } \
173 } \
174 } while (0)
175
176 #define CALL_LIFECYCLE_LISTENER_WITH_PARAM(windowLifecycleCb, listeners, param) \
177 do { \
178 for (auto& listener : (listeners)) { \
179 if (listener != nullptr) { \
180 listener->windowLifecycleCb(param); \
181 } \
182 } \
183 } while (0)
184
185 #define CALL_UI_CONTENT(uiContentCb) \
186 do { \
187 std::shared_ptr<Ace::UIContent> uiContent = GetUIContentSharedPtr(); \
188 if (uiContent != nullptr) { \
189 uiContent->uiContentCb(); \
190 } \
191 } while (0)
192
193 #define CHECK_HOST_SESSION_RETURN_IF_NULL(hostSession) \
194 do { \
195 if ((hostSession) == nullptr) { \
196 TLOGE(WmsLogTag::DEFAULT, "hostSession is null"); \
197 return; \
198 } \
199 } while (false)
200
201 #define CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, ret) \
202 do { \
203 if ((hostSession) == nullptr) { \
204 TLOGE(WmsLogTag::DEFAULT, "hostSession is null"); \
205 return ret; \
206 } \
207 } while (false)
208
209 #define CHECK_UI_CONTENT_RETURN_IF_NULL(uiContent) \
210 do { \
211 if ((uiContent) == nullptr) { \
212 TLOGE(WmsLogTag::WMS_LIFE, "uiContent is null"); \
213 return; \
214 } \
215 } while (false)
216
WindowSessionImpl(const sptr<WindowOption> & option)217 WindowSessionImpl::WindowSessionImpl(const sptr<WindowOption>& option)
218 {
219 WLOGFD("[WMSCom] Constructor");
220 property_ = sptr<WindowSessionProperty>::MakeSptr();
221 WindowType optionWindowType = option->GetWindowType();
222 SessionInfo sessionInfo;
223 sessionInfo.bundleName_ = option->GetBundleName();
224 property_->SetSessionInfo(sessionInfo);
225 property_->SetWindowName(option->GetWindowName());
226 property_->SetRequestRect(option->GetWindowRect());
227 property_->SetWindowType(optionWindowType);
228 property_->SetFocusable(option->GetFocusable());
229 property_->SetTouchable(option->GetTouchable());
230 property_->SetDisplayId(option->GetDisplayId());
231 property_->SetParentId(option->GetParentId());
232 property_->SetTurnScreenOn(option->IsTurnScreenOn());
233 property_->SetKeepScreenOn(option->IsKeepScreenOn());
234 property_->SetViewKeepScreenOn(option->IsViewKeepScreenOn());
235 property_->SetWindowMode(option->GetWindowMode());
236 property_->SetWindowFlags(option->GetWindowFlags());
237 property_->SetCallingSessionId(option->GetCallingWindow());
238 property_->SetIsUIExtFirstSubWindow(option->GetIsUIExtFirstSubWindow());
239 property_->SetTopmost(option->GetWindowTopmost());
240 property_->SetSubWindowZLevel(option->GetSubWindowZLevel());
241 property_->SetRealParentId(option->GetRealParentId());
242 property_->SetParentWindowType(option->GetParentWindowType());
243 property_->SetUIExtensionUsage(static_cast<UIExtensionUsage>(option->GetUIExtensionUsage()));
244 property_->SetIsUIExtAnySubWindow(option->GetIsUIExtAnySubWindow());
245 property_->SetIsSystemKeyboard(option->IsSystemKeyboard());
246 property_->SetConstrainedModal(option->IsConstrainedModal());
247 layoutCallback_ = sptr<FutureCallback>::MakeSptr();
248 isMainHandlerAvailable_ = option->GetMainHandlerAvailable();
249 isIgnoreSafeArea_ = WindowHelper::IsSubWindow(optionWindowType);
250 windowOption_ = option;
251 handler_ = std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::GetMainEventRunner());
252 surfaceNode_ = CreateSurfaceNode(property_->GetWindowName(), optionWindowType);
253 if (surfaceNode_ != nullptr) {
254 vsyncStation_ = std::make_shared<VsyncStation>(surfaceNode_->GetId());
255 if (WindowHelper::IsSubWindow(GetType())) {
256 surfaceNode_->SetFrameGravity(Gravity::TOP_LEFT);
257 }
258 }
259 }
260
IsPcWindow() const261 bool WindowSessionImpl::IsPcWindow() const
262 {
263 return windowSystemConfig_.IsPcWindow();
264 }
265
IsPcOrPadCapabilityEnabled() const266 bool WindowSessionImpl::IsPcOrPadCapabilityEnabled() const
267 {
268 return WindowSessionImpl::IsPcOrPadFreeMultiWindowMode() || property_->GetIsPcAppInPad();
269 }
270
IsPcOrPadFreeMultiWindowMode() const271 bool WindowSessionImpl::IsPcOrPadFreeMultiWindowMode() const
272 {
273 return windowSystemConfig_.IsPcWindow() || IsFreeMultiWindowMode();
274 }
275
IsSceneBoardEnabled() const276 bool WindowSessionImpl::IsSceneBoardEnabled() const
277 {
278 return SceneBoardJudgement::IsSceneBoardEnabled();
279 }
280
GetCompatibleModeInPc() const281 bool WindowSessionImpl::GetCompatibleModeInPc() const
282 {
283 return property_->GetCompatibleModeInPc();
284 }
285
MakeSubOrDialogWindowDragableAndMoveble()286 void WindowSessionImpl::MakeSubOrDialogWindowDragableAndMoveble()
287 {
288 TLOGI(WmsLogTag::WMS_PC, "Called %{public}d.", GetPersistentId());
289 if (IsPcOrPadCapabilityEnabled() && windowOption_ != nullptr) {
290 if (WindowHelper::IsSubWindow(property_->GetWindowType())) {
291 TLOGI(WmsLogTag::WMS_PC, "create subwindow, title: %{public}s, decorEnable: %{public}d",
292 windowOption_->GetSubWindowTitle().c_str(), windowOption_->GetSubWindowDecorEnable());
293 property_->SetDecorEnable(windowOption_->GetSubWindowDecorEnable());
294 property_->SetDragEnabled(windowOption_->GetSubWindowDecorEnable());
295 UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_DRAGENABLED);
296 subWindowTitle_ = windowOption_->GetSubWindowTitle();
297 }
298 bool isDialog = WindowHelper::IsDialogWindow(property_->GetWindowType());
299 if (isDialog) {
300 bool dialogDecorEnable = windowOption_->GetDialogDecorEnable();
301 property_->SetDecorEnable(dialogDecorEnable);
302 property_->SetDragEnabled(dialogDecorEnable);
303 UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_DRAGENABLED);
304 dialogTitle_ = windowOption_->GetDialogTitle();
305 TLOGI(WmsLogTag::WMS_PC, "create dialogWindow, title: %{public}s, decorEnable: %{public}d",
306 dialogTitle_.c_str(), dialogDecorEnable);
307 }
308 }
309 }
310
SetSubWindowZLevelToProperty()311 void WindowSessionImpl::SetSubWindowZLevelToProperty()
312 {
313 if (WindowHelper::IsNormalSubWindow(property_->GetWindowType(), property_->GetWindowFlags())) {
314 property_->SetSubWindowZLevel(windowOption_->GetSubWindowZLevel());
315 }
316 }
317
CreateSurfaceNode(const std::string & name,WindowType type)318 RSSurfaceNode::SharedPtr WindowSessionImpl::CreateSurfaceNode(const std::string& name, WindowType type)
319 {
320 struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
321 rsSurfaceNodeConfig.SurfaceNodeName = name;
322 RSSurfaceNodeType rsSurfaceNodeType = RSSurfaceNodeType::DEFAULT;
323 switch (type) {
324 case WindowType::WINDOW_TYPE_BOOT_ANIMATION:
325 case WindowType::WINDOW_TYPE_POINTER:
326 rsSurfaceNodeType = RSSurfaceNodeType::SELF_DRAWING_WINDOW_NODE;
327 break;
328 case WindowType::WINDOW_TYPE_APP_MAIN_WINDOW:
329 rsSurfaceNodeType = RSSurfaceNodeType::APP_WINDOW_NODE;
330 break;
331 case WindowType::WINDOW_TYPE_UI_EXTENSION:
332 TLOGI(WmsLogTag::WMS_UIEXT, "uiExtensionUsage=%{public}u", property_->GetUIExtensionUsage());
333 if (property_->GetUIExtensionUsage() == UIExtensionUsage::CONSTRAINED_EMBEDDED) {
334 rsSurfaceNodeType = RSSurfaceNodeType::UI_EXTENSION_SECURE_NODE;
335 } else {
336 rsSurfaceNodeType = RSSurfaceNodeType::UI_EXTENSION_COMMON_NODE;
337 }
338 break;
339 case WindowType::WINDOW_TYPE_PIP:
340 rsSurfaceNodeType = RSSurfaceNodeType::APP_WINDOW_NODE;
341 break;
342 default:
343 rsSurfaceNodeType = RSSurfaceNodeType::DEFAULT;
344 break;
345 }
346 return RSSurfaceNode::Create(rsSurfaceNodeConfig, rsSurfaceNodeType, true, property_->IsConstrainedModal());
347 }
348
~WindowSessionImpl()349 WindowSessionImpl::~WindowSessionImpl()
350 {
351 WLOGFD("[WMSCom] id: %{public}d", GetPersistentId());
352 Destroy(true, false);
353 }
354
GetWindowId() const355 uint32_t WindowSessionImpl::GetWindowId() const
356 {
357 return static_cast<uint32_t>(GetPersistentId()) & 0xffffffff; // 0xffffffff: to get low 32 bits
358 }
359
GetDisplayId() const360 uint64_t WindowSessionImpl::GetDisplayId() const
361 {
362 return property_->GetDisplayId();
363 }
364
GetParentId() const365 int32_t WindowSessionImpl::GetParentId() const
366 {
367 // 0xffffffff: to get low 32 bits
368 uint32_t parentID = static_cast<uint32_t>(property_->GetParentPersistentId()) & 0x7fffffff;
369 return static_cast<int32_t>(parentID);
370 }
371
IsWindowSessionInvalid() const372 bool WindowSessionImpl::IsWindowSessionInvalid() const
373 {
374 bool res = ((GetHostSession() == nullptr) || (GetPersistentId() == INVALID_SESSION_ID) ||
375 (state_ == WindowState::STATE_DESTROYED));
376 if (res) {
377 TLOGW(WmsLogTag::WMS_LIFE, "already destroyed or not created! id: %{public}d state_: %{public}u",
378 GetPersistentId(), state_);
379 }
380 return res;
381 }
382
IsMainHandlerAvailable() const383 bool WindowSessionImpl::IsMainHandlerAvailable() const
384 {
385 TLOGI(WmsLogTag::DEFAULT, "id:%{public}d, isAvailable:%{public}u",
386 GetPersistentId(), isMainHandlerAvailable_);
387 return isMainHandlerAvailable_;
388 }
389
GetPersistentId() const390 int32_t WindowSessionImpl::GetPersistentId() const
391 {
392 return property_->GetPersistentId();
393 }
394
GetProperty() const395 sptr<WindowSessionProperty> WindowSessionImpl::GetProperty() const
396 {
397 return property_;
398 }
399
GetSystemSessionConfig() const400 SystemSessionConfig WindowSessionImpl::GetSystemSessionConfig() const
401 {
402 return windowSystemConfig_;
403 }
404
GetHostSession() const405 sptr<ISession> WindowSessionImpl::GetHostSession() const
406 {
407 std::lock_guard<std::mutex> lock(hostSessionMutex_);
408 return hostSession_;
409 }
410
GetColorSpaceFromSurfaceGamut(GraphicColorGamut colorGamut)411 ColorSpace WindowSessionImpl::GetColorSpaceFromSurfaceGamut(GraphicColorGamut colorGamut)
412 {
413 if (colorGamut == GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB) {
414 return ColorSpace::COLOR_SPACE_DEFAULT;
415 } else if (colorGamut == GraphicColorGamut::GRAPHIC_COLOR_GAMUT_DCI_P3) {
416 return ColorSpace::COLOR_SPACE_WIDE_GAMUT;
417 } else {
418 WLOGFE("try to get not exist ColorSpace");
419 return ColorSpace::COLOR_SPACE_DEFAULT;
420 }
421 }
422
GetSurfaceGamutFromColorSpace(ColorSpace colorSpace)423 GraphicColorGamut WindowSessionImpl::GetSurfaceGamutFromColorSpace(ColorSpace colorSpace)
424 {
425 if (colorSpace == ColorSpace::COLOR_SPACE_DEFAULT) {
426 return GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB;
427 } else if (colorSpace == ColorSpace::COLOR_SPACE_WIDE_GAMUT) {
428 return GraphicColorGamut::GRAPHIC_COLOR_GAMUT_DCI_P3;
429 } else {
430 WLOGFE("try to get not exist colorGamut");
431 return GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB;
432 }
433 }
434
IsSupportWideGamut()435 bool WindowSessionImpl::IsSupportWideGamut()
436 {
437 return true;
438 }
439
SetColorSpace(ColorSpace colorSpace)440 void WindowSessionImpl::SetColorSpace(ColorSpace colorSpace)
441 {
442 if (IsWindowSessionInvalid() || surfaceNode_ == nullptr) {
443 TLOGE(WmsLogTag::DEFAULT, "session is invalid");
444 return;
445 }
446 auto colorGamut = GetSurfaceGamutFromColorSpace(colorSpace);
447 surfaceNode_->SetColorSpace(colorGamut);
448 }
449
GetColorSpace()450 ColorSpace WindowSessionImpl::GetColorSpace()
451 {
452 if (IsWindowSessionInvalid() || surfaceNode_ == nullptr) {
453 TLOGE(WmsLogTag::DEFAULT, "session is invalid");
454 return ColorSpace::COLOR_SPACE_DEFAULT;
455 }
456 GraphicColorGamut colorGamut = surfaceNode_->GetColorSpace();
457 return GetColorSpaceFromSurfaceGamut(colorGamut);
458 }
459
WindowSessionCreateCheck()460 WMError WindowSessionImpl::WindowSessionCreateCheck()
461 {
462 if (vsyncStation_ == nullptr || !vsyncStation_->IsVsyncReceiverCreated()) {
463 return WMError::WM_ERROR_NULLPTR;
464 }
465 const auto& name = property_->GetWindowName();
466 std::shared_lock<std::shared_mutex> lock(windowSessionMutex_);
467 // check window name, same window names are forbidden
468 if (windowSessionMap_.find(name) != windowSessionMap_.end()) {
469 WLOGFE("WindowName(%{public}s) already exists.", name.c_str());
470 return WMError::WM_ERROR_REPEAT_OPERATION;
471 }
472
473 // check if camera floating window is already exists
474 if (property_->GetWindowType() == WindowType::WINDOW_TYPE_FLOAT_CAMERA ||
475 property_->GetWindowType() == WindowType::WINDOW_TYPE_PIP) {
476 for (const auto& item : windowSessionMap_) {
477 if (item.second.second && item.second.second->property_ &&
478 item.second.second->property_->GetWindowType() == WindowType::WINDOW_TYPE_FLOAT_CAMERA) {
479 WLOGFE("Camera floating window is already exists.");
480 return WMError::WM_ERROR_REPEAT_OPERATION;
481 }
482 }
483 uint32_t accessTokenId = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
484 property_->SetAccessTokenId(accessTokenId);
485 TLOGI(WmsLogTag::DEFAULT, "Create camera float window, TokenId=%{private}u", accessTokenId);
486 }
487 return WMError::WM_OK;
488 }
489
SetDefaultDisplayIdIfNeed()490 void WindowSessionImpl::SetDefaultDisplayIdIfNeed()
491 {
492 TLOGD(WmsLogTag::DEFAULT, "in");
493 auto displayId = property_->GetDisplayId();
494 if (displayId == DISPLAY_ID_INVALID) {
495 auto defaultDisplayId = SingletonContainer::IsDestroyed() ? DISPLAY_ID_INVALID :
496 SingletonContainer::Get<DisplayManager>().GetDefaultDisplayId();
497 defaultDisplayId = (defaultDisplayId == DISPLAY_ID_INVALID) ? 0 : defaultDisplayId;
498 property_->SetDisplayId(defaultDisplayId);
499 TLOGI(WmsLogTag::DEFAULT, "Reset displayId to %{public}" PRIu64, defaultDisplayId);
500 }
501 }
502
503 /** @note @window.hierarchy */
GetSubWindowZLevelByFlags(WindowType type,uint32_t windowFlags,bool isTopmost)504 int32_t WindowSessionImpl::GetSubWindowZLevelByFlags(WindowType type, uint32_t windowFlags, bool isTopmost)
505 {
506 if (WindowHelper::IsApplicationModalSubWindow(type, windowFlags)) {
507 if (isTopmost) {
508 return APPLICATION_MODALITY_SUB_WINDOW_Z_LEVEL + TOPMOST_SUB_WINDOW_Z_LEVEL;
509 }
510 return APPLICATION_MODALITY_SUB_WINDOW_Z_LEVEL;
511 } else if (WindowHelper::IsModalSubWindow(type, windowFlags)) {
512 if (isTopmost) {
513 return MODALITY_SUB_WINDOW_Z_LEVEL + TOPMOST_SUB_WINDOW_Z_LEVEL;
514 }
515 return MODALITY_SUB_WINDOW_Z_LEVEL;
516 } else if (WindowHelper::IsToastSubWindow(type, windowFlags)) {
517 return TOAST_SUB_WINDOW_Z_LEVEL;
518 } else if (WindowHelper::IsTextMenuSubWindow(type, windowFlags)) {
519 return TEXT_MENU_SUB_WINDOW_Z_LEVEL;
520 } else if (WindowHelper::IsDialogWindow(type)) {
521 return DIALOG_SUB_WINDOW_Z_LEVEL;
522 }
523 return NORMAL_SUB_WINDOW_Z_LEVEL;
524 }
525
Connect()526 WMError WindowSessionImpl::Connect()
527 {
528 TLOGI(WmsLogTag::WMS_LIFE, "in");
529 auto hostSession = GetHostSession();
530 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, WMError::WM_ERROR_NULLPTR);
531 sptr<ISessionStage> iSessionStage(this);
532 auto windowEventChannel = sptr<WindowEventChannel>::MakeSptr(iSessionStage);
533 windowEventChannel->SetIsUIExtension(property_->GetWindowType() == WindowType::WINDOW_TYPE_UI_EXTENSION);
534 windowEventChannel->SetUIExtensionUsage(property_->GetUIExtensionUsage());
535 sptr<IWindowEventChannel> iWindowEventChannel(windowEventChannel);
536 sptr<IRemoteObject> token = context_ ? context_->GetToken() : nullptr;
537 if (token) {
538 property_->SetTokenState(true);
539 }
540 property_->SetApiVersion(GetTargetAPIVersion());
541 auto ret = hostSession->Connect(
542 iSessionStage, iWindowEventChannel, surfaceNode_, windowSystemConfig_, property_,
543 token, identityToken_);
544 TLOGI(WmsLogTag::WMS_LIFE, "Window Connect [name:%{public}s, id:%{public}d, type:%{public}u], ret:%{public}u",
545 property_->GetWindowName().c_str(), GetPersistentId(), property_->GetWindowType(), ret);
546 return static_cast<WMError>(ret);
547 }
548
ConsumePointerEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)549 void WindowSessionImpl::ConsumePointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
550 {
551 NotifyPointerEvent(pointerEvent);
552 }
553
ConsumeKeyEvent(std::shared_ptr<MMI::KeyEvent> & keyEvent)554 void WindowSessionImpl::ConsumeKeyEvent(std::shared_ptr<MMI::KeyEvent>& keyEvent)
555 {
556 bool isConsumed = false;
557 NotifyKeyEvent(keyEvent, isConsumed, false);
558 }
559
PreNotifyKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent)560 bool WindowSessionImpl::PreNotifyKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
561 {
562 TLOGI(WmsLogTag::WMS_EVENT, "id: %{public}d", keyEvent->GetId());
563 if (auto uiContent = GetUIContentSharedPtr()) {
564 return uiContent->ProcessKeyEvent(keyEvent, true);
565 }
566 return false;
567 }
568
NotifyOnKeyPreImeEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent)569 bool WindowSessionImpl::NotifyOnKeyPreImeEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
570 {
571 return PreNotifyKeyEvent(keyEvent);
572 }
573
GetSubWidnows(int32_t parentPersistentId,std::vector<sptr<WindowSessionImpl>> & subWindows)574 void WindowSessionImpl::GetSubWidnows(int32_t parentPersistentId, std::vector<sptr<WindowSessionImpl>>& subWindows)
575 {
576 std::lock_guard<std::recursive_mutex> lock(subWindowSessionMutex_);
577 auto iter = subWindowSessionMap_.find(parentPersistentId);
578 if (iter == subWindowSessionMap_.end()) {
579 TLOGD(WmsLogTag::WMS_SUB, "parent window: %{public}d has no child node", parentPersistentId);
580 return;
581 }
582 subWindows = iter->second;
583 }
584
UpdateSubWindowStateAndNotify(int32_t parentPersistentId,const WindowState newState)585 void WindowSessionImpl::UpdateSubWindowStateAndNotify(int32_t parentPersistentId, const WindowState newState)
586 {
587 std::vector<sptr<WindowSessionImpl>> subWindows;
588 GetSubWidnows(parentPersistentId, subWindows);
589 if (subWindows.empty()) {
590 TLOGD(WmsLogTag::WMS_SUB, "parent window: %{public}d, its subWindowMap is empty", parentPersistentId);
591 return;
592 }
593
594 // when parent window hide and subwindow whose state is shown should hide and notify user
595 if (newState == WindowState::STATE_HIDDEN) {
596 for (auto subwindow : subWindows) {
597 if (subwindow != nullptr && subwindow->GetWindowState() == WindowState::STATE_SHOWN) {
598 subwindow->state_ = WindowState::STATE_HIDDEN;
599 subwindow->NotifyAfterBackground();
600 TLOGD(WmsLogTag::WMS_SUB, "Notify subWindow background, id:%{public}d", subwindow->GetPersistentId());
601 UpdateSubWindowStateAndNotify(subwindow->GetPersistentId(), newState);
602 }
603 }
604 // when parent window show and subwindow whose state is shown should show and notify user
605 } else if (newState == WindowState::STATE_SHOWN) {
606 for (auto subwindow : subWindows) {
607 if (subwindow != nullptr && subwindow->GetWindowState() == WindowState::STATE_HIDDEN &&
608 subwindow->GetRequestWindowState() == WindowState::STATE_SHOWN) {
609 subwindow->state_ = WindowState::STATE_SHOWN;
610 subwindow->NotifyAfterForeground();
611 TLOGD(WmsLogTag::WMS_SUB, "Notify subWindow foreground, id:%{public}d", subwindow->GetPersistentId());
612 UpdateSubWindowStateAndNotify(subwindow->GetPersistentId(), newState);
613 }
614 }
615 }
616 }
617
Show(uint32_t reason,bool withAnimation,bool withFocus)618 WMError WindowSessionImpl::Show(uint32_t reason, bool withAnimation, bool withFocus)
619 {
620 TLOGI(WmsLogTag::WMS_LIFE, "name:%{public}s, id:%{public}d, type:%{public}u, reason:%{public}u, state:%{public}u",
621 property_->GetWindowName().c_str(), property_->GetPersistentId(), GetType(), reason, state_);
622 if (IsWindowSessionInvalid()) {
623 WLOGFE("session is invalid");
624 return WMError::WM_ERROR_INVALID_WINDOW;
625 }
626 if (state_ == WindowState::STATE_SHOWN) {
627 TLOGD(WmsLogTag::WMS_LIFE, "alreay shown [name:%{public}s, id:%{public}d, type: %{public}u]",
628 property_->GetWindowName().c_str(), GetPersistentId(), property_->GetWindowType());
629 NotifyAfterForeground(true, false);
630 return WMError::WM_OK;
631 }
632
633 auto hostSession = GetHostSession();
634 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, WMError::WM_ERROR_INVALID_WINDOW);
635 WSError ret = hostSession->Foreground(property_);
636 // delete after replace WSError with WMError
637 WMError res = static_cast<WMError>(ret);
638 if (res == WMError::WM_OK) {
639 UpdateSubWindowStateAndNotify(GetPersistentId(), WindowState::STATE_SHOWN);
640 state_ = WindowState::STATE_SHOWN;
641 requestState_ = WindowState::STATE_SHOWN;
642 NotifyAfterForeground();
643 } else {
644 NotifyForegroundFailed(res);
645 }
646 return res;
647 }
648
Hide(uint32_t reason,bool withAnimation,bool isFromInnerkits)649 WMError WindowSessionImpl::Hide(uint32_t reason, bool withAnimation, bool isFromInnerkits)
650 {
651 TLOGI(WmsLogTag::WMS_LIFE, "id:%{public}d Hide, reason:%{public}u, state:%{public}u",
652 GetPersistentId(), reason, state_);
653 if (IsWindowSessionInvalid()) {
654 WLOGFE("session is invalid");
655 return WMError::WM_ERROR_INVALID_WINDOW;
656 }
657 if (state_ == WindowState::STATE_HIDDEN || state_ == WindowState::STATE_CREATED) {
658 TLOGD(WmsLogTag::WMS_LIFE, "alreay hidden [name:%{public}s, id:%{public}d, type: %{public}u]",
659 property_->GetWindowName().c_str(), GetPersistentId(), property_->GetWindowType());
660 NotifyBackgroundFailed(WMError::WM_DO_NOTHING);
661 return WMError::WM_OK;
662 }
663 UpdateSubWindowStateAndNotify(GetPersistentId(), WindowState::STATE_HIDDEN);
664 state_ = WindowState::STATE_HIDDEN;
665 requestState_ = WindowState::STATE_HIDDEN;
666 NotifyAfterBackground();
667 return WMError::WM_OK;
668 }
669
RemoveSubWindow(int32_t parentPersistentId)670 void WindowSessionImpl::RemoveSubWindow(int32_t parentPersistentId)
671 {
672 const int32_t persistentId = GetPersistentId();
673 TLOGI(WmsLogTag::WMS_SUB, "Id: %{public}d, parentId: %{public}d", persistentId, parentPersistentId);
674 std::vector<sptr<WindowSessionImpl>> subWindows;
675 GetSubWidnows(parentPersistentId, subWindows);
676 for (auto iter = subWindows.begin(); iter != subWindows.end(); iter++) {
677 auto subWindow = *iter;
678 if (subWindow != nullptr && subWindow->GetPersistentId() == persistentId) {
679 TLOGD(WmsLogTag::WMS_SUB, "Destroy sub window, persistentId: %{public}d", persistentId);
680 subWindows.erase(iter);
681 break;
682 } else {
683 TLOGD(WmsLogTag::WMS_SUB, "Exists other sub window, persistentId: %{public}d", persistentId);
684 }
685 }
686 }
687
DestroySubWindow()688 void WindowSessionImpl::DestroySubWindow()
689 {
690 int32_t parentPersistentId = property_->GetParentPersistentId();
691 const int32_t persistentId = GetPersistentId();
692 if (property_->GetIsUIExtFirstSubWindow()) {
693 auto extensionWindow = FindExtensionWindowWithContext();
694 if (extensionWindow != nullptr) {
695 parentPersistentId = extensionWindow->GetPersistentId();
696 }
697 }
698 // remove from subWindowMap_ when destroy sub window
699 RemoveSubWindow(parentPersistentId);
700 {
701 std::lock_guard<std::recursive_mutex> lock(subWindowSessionMutex_);
702 auto subIter = subWindowSessionMap_.find(parentPersistentId);
703 if (subIter != subWindowSessionMap_.end() && property_->GetIsUIExtFirstSubWindow() &&
704 subWindowSessionMap_.empty()) {
705 auto extensionWindow = FindExtensionWindowWithContext();
706 if (extensionWindow != nullptr && extensionWindow->GetUIContentSharedPtr() == nullptr) {
707 extensionWindow->AddSetUIExtensionDestroyTimeoutCheck();
708 }
709 }
710 }
711 // remove from subWindowMap_ when destroy parent window
712 std::vector<sptr<WindowSessionImpl>> subWindows;
713 GetSubWidnows(persistentId, subWindows);
714 for (auto iter = subWindows.begin(); iter != subWindows.end(); iter = subWindows.begin()) {
715 auto subWindow = *iter;
716 if (subWindow == nullptr) {
717 TLOGW(WmsLogTag::WMS_SUB, "Destroy sub window which is nullptr");
718 subWindows.erase(iter);
719 continue;
720 }
721 bool isExtDestroyed = subWindow->property_->GetIsUIExtFirstSubWindow();
722 TLOGD(WmsLogTag::WMS_SUB, "Destroy sub window, persistentId: %{public}d, isExtDestroyed: %{public}d",
723 subWindow->GetPersistentId(), isExtDestroyed);
724 auto ret = subWindow->Destroy(isExtDestroyed);
725 if (ret != WMError::WM_OK) {
726 TLOGE(WmsLogTag::WMS_SUB, "Destroy failed. persistentId: %{public}d", subWindow->GetPersistentId());
727 subWindows.erase(iter);
728 }
729 }
730 {
731 std::lock_guard<std::recursive_mutex> lock(subWindowSessionMutex_);
732 auto mainIter = subWindowSessionMap_.find(persistentId);
733 if (mainIter != subWindowSessionMap_.end()) {
734 mainIter->second.clear();
735 subWindowSessionMap_.erase(mainIter);
736 }
737 }
738 }
739
Destroy(bool needNotifyServer,bool needClearListener)740 WMError WindowSessionImpl::Destroy(bool needNotifyServer, bool needClearListener)
741 {
742 TLOGI(WmsLogTag::WMS_LIFE, "id:%{public}d Destroy, state:%{public}u, needNotifyServer:%{public}d, "
743 "needClearListener:%{public}d", GetPersistentId(), state_, needNotifyServer, needClearListener);
744 if (IsWindowSessionInvalid()) {
745 WLOGFW("session is invalid");
746 return WMError::WM_ERROR_INVALID_WINDOW;
747 }
748 if (auto hostSession = GetHostSession()) {
749 hostSession->Disconnect();
750 }
751 NotifyBeforeDestroy(GetWindowName());
752 {
753 std::lock_guard<std::recursive_mutex> lock(mutex_);
754 state_ = WindowState::STATE_DESTROYED;
755 requestState_ = WindowState::STATE_DESTROYED;
756 }
757 DestroySubWindow();
758 {
759 std::lock_guard<std::mutex> lock(hostSessionMutex_);
760 hostSession_ = nullptr;
761 }
762 {
763 std::unique_lock<std::shared_mutex> lock(windowSessionMutex_);
764 windowSessionMap_.erase(property_->GetWindowName());
765 }
766 NotifyAfterDestroy();
767 if (needClearListener) {
768 ClearListenersById(GetPersistentId());
769 }
770 if (context_) {
771 context_.reset();
772 }
773 ClearVsyncStation();
774 return WMError::WM_OK;
775 }
776
Destroy()777 WMError WindowSessionImpl::Destroy()
778 {
779 return Destroy(true);
780 }
781
SetActive(bool active)782 WSError WindowSessionImpl::SetActive(bool active)
783 {
784 WLOGFD("active status: %{public}d", active);
785 if (active) {
786 NotifyAfterActive();
787 } else {
788 NotifyAfterInactive();
789 }
790 return WSError::WS_OK;
791 }
792
793 /** @note @window.layout */
UpdateRect(const WSRect & rect,SizeChangeReason reason,const SceneAnimationConfig & config,const std::map<AvoidAreaType,AvoidArea> & avoidAreas)794 WSError WindowSessionImpl::UpdateRect(const WSRect& rect, SizeChangeReason reason,
795 const SceneAnimationConfig& config, const std::map<AvoidAreaType, AvoidArea>& avoidAreas)
796 {
797 // delete after replace ws_common.h with wm_common.h
798 auto wmReason = static_cast<WindowSizeChangeReason>(reason);
799 Rect wmRect = { rect.posX_, rect.posY_, rect.width_, rect.height_ };
800 auto preRect = GetRect();
801 if (preRect.width_ != wmRect.width_ || preRect.height_ != wmRect.height_) {
802 windowSizeChanged_ = true;
803 }
804 property_->SetWindowRect(wmRect);
805 property_->SetRequestRect(wmRect);
806
807 TLOGI(WmsLogTag::WMS_LAYOUT, "%{public}s, preRect:%{public}s, reason:%{public}u, hasRSTransaction:%{public}d"
808 ", duration:%{public}d, [name:%{public}s, id:%{public}d], clientDisplayId: %{public}" PRIu64,
809 rect.ToString().c_str(), preRect.ToString().c_str(), wmReason, config.rsTransaction_ != nullptr,
810 config.animationDuration_, GetWindowName().c_str(), GetPersistentId(), property_->GetDisplayId());
811 HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER,
812 "WindowSessionImpl::UpdateRect id: %d [%d, %d, %u, %u] reason: %u hasRSTransaction: %u", GetPersistentId(),
813 wmRect.posX_, wmRect.posY_, wmRect.width_, wmRect.height_, wmReason, config.rsTransaction_ != nullptr);
814 if (handler_ != nullptr && wmReason == WindowSizeChangeReason::ROTATION) {
815 postTaskDone_ = false;
816 UpdateRectForRotation(wmRect, preRect, wmReason, config, avoidAreas);
817 } else if (handler_ != nullptr && wmReason == WindowSizeChangeReason::RESIZE_WITH_ANIMATION) {
818 RectAnimationConfig rectAnimationConfig = property_->GetRectAnimationConfig();
819 TLOGI(WmsLogTag::WMS_LAYOUT, "rectAnimationConfig.duration: %{public}d", rectAnimationConfig.duration);
820 postTaskDone_ = false;
821 UpdateRectForResizeWithAnimation(wmRect, preRect, wmReason, rectAnimationConfig,
822 config.rsTransaction_, avoidAreas);
823 } else {
824 UpdateRectForOtherReason(wmRect, preRect, wmReason, config.rsTransaction_, avoidAreas);
825 }
826
827 if (wmReason == WindowSizeChangeReason::MOVE || wmReason == WindowSizeChangeReason::RESIZE ||
828 wmReason == WindowSizeChangeReason::MOVE_WITH_ANIMATION ||
829 wmReason == WindowSizeChangeReason::RESIZE_WITH_ANIMATION) {
830 layoutCallback_->OnUpdateSessionRect(wmRect, wmReason, GetPersistentId());
831 }
832
833 return WSError::WS_OK;
834 }
835
836 /** @note @window.layout */
UpdateVirtualPixelRatio(const sptr<Display> & display)837 void WindowSessionImpl::UpdateVirtualPixelRatio(const sptr<Display>& display)
838 {
839 if (display == nullptr) {
840 TLOGE(WmsLogTag::WMS_LAYOUT, "display is null when rotation!");
841 return;
842 }
843 sptr<DisplayInfo> displayInfo = display->GetDisplayInfo();
844 if (displayInfo == nullptr) {
845 TLOGE(WmsLogTag::WMS_LAYOUT, "displayInfo is null when rotation!");
846 return;
847 }
848 virtualPixelRatio_ = GetVirtualPixelRatio(displayInfo);
849 TLOGD(WmsLogTag::WMS_LAYOUT, "virtualPixelRatio: %{public}f", virtualPixelRatio_);
850 }
851
UpdateRectForRotation(const Rect & wmRect,const Rect & preRect,WindowSizeChangeReason wmReason,const SceneAnimationConfig & config,const std::map<AvoidAreaType,AvoidArea> & avoidAreas)852 void WindowSessionImpl::UpdateRectForRotation(const Rect& wmRect, const Rect& preRect,
853 WindowSizeChangeReason wmReason, const SceneAnimationConfig& config,
854 const std::map<AvoidAreaType, AvoidArea>& avoidAreas)
855 {
856 handler_->PostTask([weak = wptr(this), wmReason, wmRect, preRect, config, avoidAreas]() mutable {
857 HITRACE_METER_NAME(HITRACE_TAG_WINDOW_MANAGER, "WindowSessionImpl::UpdateRectForRotation");
858 auto window = weak.promote();
859 if (!window) {
860 return;
861 }
862 auto display = SingletonContainer::Get<DisplayManager>().GetDisplayById(window->property_->GetDisplayId());
863 sptr<DisplayInfo> displayInfo = display ? display->GetDisplayInfo() : nullptr;
864 window->UpdateVirtualPixelRatio(display);
865 const std::shared_ptr<RSTransaction>& rsTransaction = config.rsTransaction_;
866 if (rsTransaction) {
867 RSTransaction::FlushImplicitTransaction();
868 rsTransaction->Begin();
869 }
870 window->rotationAnimationCount_++;
871 RSAnimationTimingProtocol protocol;
872 protocol.SetDuration(config.animationDuration_);
873 // animation curve: cubic [0.2, 0.0, 0.2, 1.0]
874 auto curve = RSAnimationTimingCurve::CreateCubicCurve(0.2, 0.0, 0.2, 1.0);
875 RSNode::OpenImplicitAnimation(protocol, curve, [weak]() {
876 auto window = weak.promote();
877 if (!window) {
878 return;
879 }
880 window->rotationAnimationCount_--;
881 if (window->rotationAnimationCount_ == 0) {
882 window->NotifyRotationAnimationEnd();
883 }
884 });
885 if ((wmRect != preRect) || (wmReason != window->lastSizeChangeReason_)) {
886 window->NotifySizeChange(wmRect, wmReason);
887 window->lastSizeChangeReason_ = wmReason;
888 }
889 window->UpdateViewportConfig(wmRect, wmReason, rsTransaction, displayInfo, avoidAreas);
890 RSNode::CloseImplicitAnimation();
891 if (rsTransaction) {
892 rsTransaction->Commit();
893 } else {
894 RSTransaction::FlushImplicitTransaction();
895 }
896 window->postTaskDone_ = true;
897 }, "WMS_WindowSessionImpl_UpdateRectForRotation");
898 }
899
UpdateRectForOtherReasonTask(const Rect & wmRect,const Rect & preRect,WindowSizeChangeReason wmReason,const std::shared_ptr<RSTransaction> & rsTransaction,const std::map<AvoidAreaType,AvoidArea> & avoidAreas)900 void WindowSessionImpl::UpdateRectForOtherReasonTask(const Rect& wmRect, const Rect& preRect,
901 WindowSizeChangeReason wmReason, const std::shared_ptr<RSTransaction>& rsTransaction,
902 const std::map<AvoidAreaType, AvoidArea>& avoidAreas)
903 {
904 if ((wmRect != preRect) || (wmReason != lastSizeChangeReason_) || !postTaskDone_) {
905 NotifySizeChange(wmRect, wmReason);
906 lastSizeChangeReason_ = wmReason;
907 postTaskDone_ = true;
908 }
909 UpdateViewportConfig(wmRect, wmReason, rsTransaction, nullptr, avoidAreas);
910 UpdateFrameLayoutCallbackIfNeeded(wmReason);
911 }
912
CheckIfNeedCommitRsTransaction(WindowSizeChangeReason wmReason)913 bool WindowSessionImpl::CheckIfNeedCommitRsTransaction(WindowSizeChangeReason wmReason)
914 {
915 if (wmReason == WindowSizeChangeReason::FULL_TO_SPLIT ||
916 wmReason == WindowSizeChangeReason::FULL_TO_FLOATING || wmReason == WindowSizeChangeReason::RECOVER ||
917 wmReason == WindowSizeChangeReason::MAXIMIZE) {
918 return false;
919 }
920 return true;
921 }
922
UpdateRectForOtherReason(const Rect & wmRect,const Rect & preRect,WindowSizeChangeReason wmReason,const std::shared_ptr<RSTransaction> & rsTransaction,const std::map<AvoidAreaType,AvoidArea> & avoidAreas)923 void WindowSessionImpl::UpdateRectForOtherReason(const Rect& wmRect, const Rect& preRect,
924 WindowSizeChangeReason wmReason, const std::shared_ptr<RSTransaction>& rsTransaction,
925 const std::map<AvoidAreaType, AvoidArea>& avoidAreas)
926 {
927 if (handler_ == nullptr) {
928 UpdateRectForOtherReasonTask(wmRect, preRect, wmReason, rsTransaction, avoidAreas);
929 return;
930 }
931
932 auto task = [weak = wptr(this), wmReason, wmRect, preRect, rsTransaction, avoidAreas] {
933 auto window = weak.promote();
934 if (!window) {
935 TLOGE(WmsLogTag::WMS_LAYOUT, "window is null, updateViewPortConfig failed");
936 return;
937 }
938 bool ifNeedCommitRsTransaction = window->CheckIfNeedCommitRsTransaction(wmReason);
939 if (rsTransaction && ifNeedCommitRsTransaction) {
940 RSTransaction::FlushImplicitTransaction();
941 rsTransaction->Begin();
942 }
943 if (wmReason == WindowSizeChangeReason::DRAG) {
944 window->UpdateRectForOtherReasonTask(window->GetRect(), preRect, wmReason, rsTransaction, avoidAreas);
945 window->isDragTaskPostDone_.store(true);
946 } else {
947 window->UpdateRectForOtherReasonTask(wmRect, preRect, wmReason, rsTransaction, avoidAreas);
948 }
949 if (rsTransaction && ifNeedCommitRsTransaction) {
950 rsTransaction->Commit();
951 }
952 };
953 if (wmReason == WindowSizeChangeReason::DRAG) {
954 bool isDragTaskPostDone = true;
955 if (isDragTaskPostDone_.compare_exchange_strong(isDragTaskPostDone, false)) {
956 handler_->PostTask(task, "WMS_WindowSessionImpl_UpdateRectForOtherReason");
957 }
958 } else {
959 handler_->PostTask(task, "WMS_WindowSessionImpl_UpdateRectForOtherReason");
960 }
961 }
962
UpdateRectForResizeWithAnimation(const Rect & wmRect,const Rect & preRect,WindowSizeChangeReason wmReason,const RectAnimationConfig & rectAnimationConfig,const std::shared_ptr<RSTransaction> & rsTransaction,const std::map<AvoidAreaType,AvoidArea> & avoidAreas)963 void WindowSessionImpl::UpdateRectForResizeWithAnimation(const Rect& wmRect, const Rect& preRect,
964 WindowSizeChangeReason wmReason, const RectAnimationConfig& rectAnimationConfig,
965 const std::shared_ptr<RSTransaction>& rsTransaction,
966 const std::map<AvoidAreaType, AvoidArea>& avoidAreas)
967 {
968 if (handler_ == nullptr) {
969 TLOGE(WmsLogTag::WMS_LAYOUT, "handler is null!");
970 return;
971 }
972 auto task = [weakThis = wptr(this), wmReason, wmRect, preRect, rectAnimationConfig,
973 rsTransaction, avoidAreas]() mutable {
974 HITRACE_METER_NAME(HITRACE_TAG_WINDOW_MANAGER, "WindowSessionImpl::UpdateRectForResizeWithAnimation");
975 auto window = weakThis.promote();
976 if (!window) {
977 TLOGNE(WmsLogTag::WMS_LAYOUT, "window is null, updateRectForResizeWithAnimation failed");
978 return;
979 }
980 if (rsTransaction) {
981 RSTransaction::FlushImplicitTransaction();
982 rsTransaction->Begin();
983 }
984 RSAnimationTimingProtocol protocol;
985 protocol.SetDuration(rectAnimationConfig.duration);
986 auto curve = RSAnimationTimingCurve::CreateCubicCurve(rectAnimationConfig.x1,
987 rectAnimationConfig.y1, rectAnimationConfig.x2, rectAnimationConfig.y2);
988 RSNode::OpenImplicitAnimation(protocol, curve, nullptr);
989 if (wmRect != preRect || wmReason != window->lastSizeChangeReason_) {
990 window->NotifySizeChange(wmRect, wmReason);
991 window->lastSizeChangeReason_ = wmReason;
992 }
993 window->UpdateViewportConfig(wmRect, wmReason, rsTransaction, nullptr, avoidAreas);
994 RSNode::CloseImplicitAnimation();
995 if (rsTransaction) {
996 rsTransaction->Commit();
997 } else {
998 RSTransaction::FlushImplicitTransaction();
999 }
1000 window->postTaskDone_ = true;
1001 };
1002 handler_->PostTask(task, "WMS_WindowSessionImpl_UpdateRectForResizeWithAnimation");
1003 }
1004
NotifyAfterUIContentReady()1005 void WindowSessionImpl::NotifyAfterUIContentReady()
1006 {
1007 auto uiContent = GetUIContentSharedPtr();
1008 CHECK_UI_CONTENT_RETURN_IF_NULL(uiContent);
1009 if (IsNeedRenotifyTransform()) {
1010 auto transform = GetCurrentTransform();
1011 TLOGI(WmsLogTag::WMS_LAYOUT, "Renotify transform, id:%{public}d, scaleX:%{public}f, scaleY:%{public}f",
1012 GetPersistentId(), transform.scaleX_, transform.scaleY_);
1013 uiContent->UpdateTransform(transform);
1014 SetNeedRenotifyTransform(false);
1015 }
1016 }
1017
NotifyRotationAnimationEnd()1018 void WindowSessionImpl::NotifyRotationAnimationEnd()
1019 {
1020 auto task = [weak = wptr(this)] {
1021 auto window = weak.promote();
1022 if (!window) {
1023 TLOGE(WmsLogTag::WMS_DIALOG, "window is null");
1024 return;
1025 }
1026 std::shared_ptr<Ace::UIContent> uiContent = window->GetUIContentSharedPtr();
1027 if (uiContent == nullptr) {
1028 WLOGFW("uiContent is null!");
1029 return;
1030 }
1031 uiContent->NotifyRotationAnimationEnd();
1032 };
1033 if (handler_ == nullptr) {
1034 TLOGW(WmsLogTag::WMS_DIALOG, "handler is null!");
1035 task();
1036 } else {
1037 handler_->PostTask(task, "WMS_WindowSessionImpl_NotifyRotationAnimationEnd");
1038 }
1039 }
1040
FlushLayoutSize(int32_t width,int32_t height)1041 void WindowSessionImpl::FlushLayoutSize(int32_t width, int32_t height)
1042 {
1043 if (!WindowHelper::IsMainWindow(GetType())) {
1044 return;
1045 }
1046 WSRect rect = { 0, 0, width, height };
1047 bool windowSizeChanged = true;
1048 bool enableFrameLayoutFinishCb = true;
1049 if (windowSizeChanged_.compare_exchange_strong(windowSizeChanged, false) ||
1050 enableFrameLayoutFinishCb_.compare_exchange_strong(enableFrameLayoutFinishCb, false) || layoutRect_ != rect) {
1051 HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER,
1052 "NotifyFrameLayoutFinishFromApp, id: %u, rect: %s, notifyListener: %d",
1053 GetWindowId(), rect.ToString().c_str(), enableFrameLayoutFinishCb_.load());
1054 TLOGI(WmsLogTag::WMS_PATTERN,
1055 "NotifyFrameLayoutFinishFromApp, id: %{public}u, rect: %{public}s, notifyListener: %{public}d",
1056 GetWindowId(), rect.ToString().c_str(), enableFrameLayoutFinishCb_.load());
1057 if (auto session = GetHostSession()) {
1058 session->NotifyFrameLayoutFinishFromApp(enableFrameLayoutFinishCb_, rect);
1059 }
1060 layoutRect_ = rect;
1061 enableFrameLayoutFinishCb_ = false;
1062 }
1063 }
1064
GetTitleButtonVisible(bool & hideMaximizeButton,bool & hideMinimizeButton,bool & hideSplitButton,bool & hideCloseButton)1065 void WindowSessionImpl::GetTitleButtonVisible(bool& hideMaximizeButton, bool& hideMinimizeButton,
1066 bool& hideSplitButton, bool& hideCloseButton)
1067 {
1068 if (!IsPcOrPadFreeMultiWindowMode()) {
1069 TLOGE(WmsLogTag::WMS_DECOR, "device not support");
1070 return;
1071 }
1072 if (hideMaximizeButton > !windowTitleVisibleFlags_.isMaximizeVisible) {
1073 TLOGW(WmsLogTag::WMS_DECOR, "isMaximizeVisible param INVALID");
1074 }
1075 hideMaximizeButton = hideMaximizeButton || (!windowTitleVisibleFlags_.isMaximizeVisible);
1076 if (hideMinimizeButton > !windowTitleVisibleFlags_.isMinimizeVisible) {
1077 TLOGW(WmsLogTag::WMS_DECOR, "isMinimizeVisible param INVALID");
1078 }
1079 hideMinimizeButton = hideMinimizeButton || (!windowTitleVisibleFlags_.isMinimizeVisible);
1080 if (hideSplitButton > !windowTitleVisibleFlags_.isSplitVisible) {
1081 TLOGW(WmsLogTag::WMS_DECOR, "isSplitVisible param INVALID");
1082 }
1083 hideSplitButton = hideSplitButton || (!windowTitleVisibleFlags_.isSplitVisible) || !isSplitButtonVisible_;
1084 if (hideCloseButton > !windowTitleVisibleFlags_.isCloseVisible) {
1085 TLOGW(WmsLogTag::WMS_DECOR, "isCloseVisible param INVALID");
1086 }
1087 hideCloseButton = hideCloseButton || (!windowTitleVisibleFlags_.isCloseVisible);
1088 }
1089
UpdateDensity()1090 void WindowSessionImpl::UpdateDensity()
1091 {
1092 auto preRect = GetRect();
1093 UpdateViewportConfig(preRect, WindowSizeChangeReason::UNDEFINED);
1094 WLOGFI("[%{public}d, %{public}d, %{public}u, %{public}u]",
1095 preRect.posX_, preRect.posY_, preRect.width_, preRect.height_);
1096 }
1097
SetUniqueVirtualPixelRatio(bool useUniqueDensity,float virtualPixelRatio)1098 void WindowSessionImpl::SetUniqueVirtualPixelRatio(bool useUniqueDensity, float virtualPixelRatio)
1099 {
1100 TLOGI(WmsLogTag::WMS_ATTRIBUTE, "old {useUniqueDensity: %{public}d, virtualPixelRatio: %{public}f}, "
1101 "new {useUniqueDensity: %{public}d, virtualPixelRatio: %{public}f}",
1102 useUniqueDensity_, virtualPixelRatio_, useUniqueDensity, virtualPixelRatio);
1103 bool oldUseUniqueDensity = useUniqueDensity_;
1104 useUniqueDensity_ = useUniqueDensity;
1105 if (useUniqueDensity_) {
1106 float oldVirtualPixelRatio = virtualPixelRatio_;
1107 virtualPixelRatio_ = virtualPixelRatio;
1108 if (!MathHelper::NearZero(oldVirtualPixelRatio - virtualPixelRatio)) {
1109 UpdateDensity();
1110 SetUniqueVirtualPixelRatioForSub(useUniqueDensity, virtualPixelRatio);
1111 }
1112 } else {
1113 if (oldUseUniqueDensity) {
1114 UpdateDensity();
1115 SetUniqueVirtualPixelRatioForSub(useUniqueDensity, virtualPixelRatio);
1116 }
1117 }
1118 }
1119
CopyUniqueDensityParameter(sptr<WindowSessionImpl> parentWindow)1120 void WindowSessionImpl::CopyUniqueDensityParameter(sptr<WindowSessionImpl> parentWindow)
1121 {
1122 if (parentWindow) {
1123 useUniqueDensity_ = parentWindow->useUniqueDensity_;
1124 virtualPixelRatio_ = parentWindow->virtualPixelRatio_;
1125 }
1126 }
1127
FindMainWindowWithContext()1128 sptr<WindowSessionImpl> WindowSessionImpl::FindMainWindowWithContext()
1129 {
1130 if (context_ == nullptr) {
1131 return nullptr;
1132 }
1133 std::shared_lock<std::shared_mutex> lock(windowSessionMutex_);
1134 for (const auto& winPair : windowSessionMap_) {
1135 auto win = winPair.second.second;
1136 if (win && win->GetType() == WindowType::WINDOW_TYPE_APP_MAIN_WINDOW &&
1137 context_.get() == win->GetContext().get()) {
1138 return win;
1139 }
1140 }
1141 TLOGD(WmsLogTag::WMS_MAIN, "Can not find main window, not app type");
1142 return nullptr;
1143 }
1144
FindExtensionWindowWithContext()1145 sptr<WindowSessionImpl> WindowSessionImpl::FindExtensionWindowWithContext()
1146 {
1147 if (context_ == nullptr) {
1148 return nullptr;
1149 }
1150 std::shared_lock<std::shared_mutex> lock(windowExtensionSessionMutex_);
1151 for (const auto& window : windowExtensionSessionSet_) {
1152 if (window && context_.get() == window->GetContext().get()) {
1153 return window;
1154 }
1155 }
1156 return nullptr;
1157 }
1158
SetUniqueVirtualPixelRatioForSub(bool useUniqueDensity,float virtualPixelRatio)1159 void WindowSessionImpl::SetUniqueVirtualPixelRatioForSub(bool useUniqueDensity, float virtualPixelRatio)
1160 {
1161 std::vector<sptr<WindowSessionImpl>> subWindows;
1162 GetSubWidnows(GetPersistentId(), subWindows);
1163 for (auto& subWindowSession : subWindows) {
1164 if (subWindowSession != nullptr) {
1165 subWindowSession->SetUniqueVirtualPixelRatio(useUniqueDensity, virtualPixelRatio);
1166 }
1167 }
1168 }
1169
UpdateOrientation()1170 WSError WindowSessionImpl::UpdateOrientation()
1171 {
1172 TLOGD(WmsLogTag::DMS, "wid: %{public}d", GetPersistentId());
1173 return WSError::WS_OK;
1174 }
1175
UpdateDisplayId(uint64_t displayId)1176 WSError WindowSessionImpl::UpdateDisplayId(uint64_t displayId)
1177 {
1178 TLOGI(WmsLogTag::WMS_ATTRIBUTE, "wid: %{public}d, displayId: %{public}" PRIu64, GetPersistentId(), displayId);
1179 property_->SetDisplayId(displayId);
1180 return WSError::WS_OK;
1181 }
1182
UpdateFocus(bool isFocused)1183 WSError WindowSessionImpl::UpdateFocus(bool isFocused)
1184 {
1185 TLOGI(WmsLogTag::WMS_FOCUS, "focus: %{public}u, id: %{public}d", isFocused, GetPersistentId());
1186 isFocused_ = isFocused;
1187 if (isFocused) {
1188 HiSysEventWrite(
1189 OHOS::HiviewDFX::HiSysEvent::Domain::WINDOW_MANAGER,
1190 "FOCUS_WINDOW",
1191 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
1192 "PID", getpid(),
1193 "UID", getuid(),
1194 "BUNDLE_NAME", property_->GetSessionInfo().bundleName_,
1195 "WINDOW_TYPE", static_cast<uint32_t>(GetType()));
1196 NotifyAfterFocused();
1197 } else {
1198 NotifyAfterUnfocused();
1199 }
1200 return WSError::WS_OK;
1201 }
1202
IsFocused() const1203 bool WindowSessionImpl::IsFocused() const
1204 {
1205 if (IsWindowSessionInvalid()) {
1206 TLOGE(WmsLogTag::WMS_FOCUS, "Session is invalid");
1207 return false;
1208 }
1209
1210 TLOGD(WmsLogTag::WMS_FOCUS, "window id=%{public}d, isFocused=%{public}d", GetPersistentId(), isFocused_.load());
1211 return isFocused_;
1212 }
1213
RequestFocus() const1214 WMError WindowSessionImpl::RequestFocus() const
1215 {
1216 if (IsWindowSessionInvalid()) {
1217 WLOGFD("session is invalid");
1218 return WMError::WM_ERROR_INVALID_WINDOW;
1219 }
1220 return SingletonContainer::Get<WindowAdapter>().RequestFocusStatus(GetPersistentId(), true);
1221 }
1222
1223 /** @note @window.focus */
RequestFocusByClient(bool isFocused) const1224 WMError WindowSessionImpl::RequestFocusByClient(bool isFocused) const
1225 {
1226 if (!SessionPermission::IsSystemCalling()) {
1227 TLOGE(WmsLogTag::WMS_FOCUS, "permission denied!");
1228 return WMError::WM_ERROR_NOT_SYSTEM_APP;
1229 }
1230 if (IsWindowSessionInvalid()) {
1231 TLOGD(WmsLogTag::WMS_FOCUS, "session is invalid");
1232 return WMError::WM_ERROR_INVALID_WINDOW;
1233 }
1234 auto hostSession = GetHostSession();
1235 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, WMError::WM_ERROR_INVALID_WINDOW);
1236 auto ret = hostSession->RequestFocus(isFocused);
1237 return static_cast<WMError>(ret);
1238 }
1239
IsNotifyInteractiveDuplicative(bool interactive)1240 bool WindowSessionImpl::IsNotifyInteractiveDuplicative(bool interactive)
1241 {
1242 if (interactive == interactive_ && hasFirstNotifyInteractive_) {
1243 return true;
1244 }
1245 hasFirstNotifyInteractive_ = true;
1246 if (interactive_ != interactive) {
1247 interactive_ = interactive;
1248 }
1249 return false;
1250 }
1251
NotifyForegroundInteractiveStatus(bool interactive)1252 void WindowSessionImpl::NotifyForegroundInteractiveStatus(bool interactive)
1253 {
1254 WLOGFI("%{public}d", interactive);
1255 if (IsWindowSessionInvalid()) {
1256 return;
1257 }
1258 if (IsNotifyInteractiveDuplicative(interactive)) {
1259 return;
1260 }
1261 if (state_ != WindowState::STATE_SHOWN) {
1262 TLOGI(WmsLogTag::WMS_LIFE, "window state %{public}d, is not shown", state_);
1263 return;
1264 }
1265 if (interactive) {
1266 if (GetTargetAPIVersion() >= LIFECYCLE_ISOLATE_VERSION && !isDidForeground_) {
1267 TLOGI(WmsLogTag::WMS_LIFE, "api version %{public}u, isDidForeground %{public}d", GetTargetAPIVersion(),
1268 isDidForeground_);
1269 return;
1270 }
1271 NotifyAfterResumed();
1272 } else {
1273 NotifyAfterPaused();
1274 }
1275 }
1276
UpdateWindowMode(WindowMode mode)1277 WSError WindowSessionImpl::UpdateWindowMode(WindowMode mode)
1278 {
1279 return WSError::WS_OK;
1280 }
1281
1282 /** @note @window.layout */
GetVirtualPixelRatio()1283 float WindowSessionImpl::GetVirtualPixelRatio()
1284 {
1285 TLOGD(WmsLogTag::WMS_LAYOUT, "virtualPixelRatio: %{public}f", virtualPixelRatio_);
1286 return virtualPixelRatio_;
1287 }
1288
GetVirtualPixelRatio(const sptr<DisplayInfo> & displayInfo)1289 float WindowSessionImpl::GetVirtualPixelRatio(const sptr<DisplayInfo>& displayInfo)
1290 {
1291 if (useUniqueDensity_) {
1292 return virtualPixelRatio_;
1293 }
1294 return displayInfo->GetVirtualPixelRatio();
1295 }
1296
GetVirtualPixelRatio(float & vpr)1297 WMError WindowSessionImpl::GetVirtualPixelRatio(float& vpr)
1298 {
1299 auto display = SingletonContainer::Get<DisplayManager>().GetDisplayById(property_->GetDisplayId());
1300 if (display == nullptr) {
1301 TLOGE(WmsLogTag::WMS_LAYOUT, "get display failed displayId: %{public}" PRIu64, property_->GetDisplayId());
1302 return WMError::WM_ERROR_NULLPTR;
1303 }
1304 auto displayInfo = display->GetDisplayInfo();
1305 if (displayInfo == nullptr) {
1306 TLOGE(WmsLogTag::WMS_LAYOUT, "get display info failed displayId: %{public}" PRIu64, property_->GetDisplayId());
1307 return WMError::WM_ERROR_NULLPTR;
1308 }
1309 vpr = GetVirtualPixelRatio(displayInfo);
1310 if (MathHelper::NearZero(vpr)) {
1311 TLOGE(WmsLogTag::WMS_LAYOUT, "get decor height failed, because of wrong vpr: %{public}f", vpr);
1312 return WMError::WM_ERROR_INVALID_WINDOW;
1313 }
1314 return WMError::WM_OK;
1315 }
1316
1317 /** @note @window.layout */
UpdateViewportConfig(const Rect & rect,WindowSizeChangeReason reason,const std::shared_ptr<RSTransaction> & rsTransaction,const sptr<DisplayInfo> & info,const std::map<AvoidAreaType,AvoidArea> & avoidAreas)1318 void WindowSessionImpl::UpdateViewportConfig(const Rect& rect, WindowSizeChangeReason reason,
1319 const std::shared_ptr<RSTransaction>& rsTransaction, const sptr<DisplayInfo>& info,
1320 const std::map<AvoidAreaType, AvoidArea>& avoidAreas)
1321 {
1322 // update avoid areas to listeners
1323 for (const auto& [type, avoidArea] : avoidAreas) {
1324 TLOGD(WmsLogTag::WMS_IMMS, "avoid type %{public}u area %{public}s",
1325 type, avoidArea.ToString().c_str());
1326 if (lastAvoidAreaMap_[type] != avoidArea) {
1327 lastAvoidAreaMap_[type] = avoidArea;
1328 NotifyAvoidAreaChange(new AvoidArea(avoidArea), type);
1329 }
1330 }
1331
1332 sptr<DisplayInfo> displayInfo;
1333 if (info == nullptr) {
1334 auto display = SingletonContainer::Get<DisplayManager>().GetDisplayById(property_->GetDisplayId());
1335 if (display == nullptr) {
1336 WLOGFE("display is null!");
1337 return;
1338 }
1339 displayInfo = display->GetDisplayInfo();
1340 } else {
1341 displayInfo = info;
1342 }
1343 if (displayInfo == nullptr) {
1344 WLOGFE("displayInfo is null!");
1345 return;
1346 }
1347 if (rect.width_ <= 0 || rect.height_ <= 0) {
1348 TLOGW(WmsLogTag::WMS_LAYOUT, "invalid width: %{public}d, height: %{public}d, id: %{public}d",
1349 rect.width_, rect.height_, GetPersistentId());
1350 return;
1351 }
1352 auto rotation = ONE_FOURTH_FULL_CIRCLE_DEGREE * static_cast<uint32_t>(displayInfo->GetOriginRotation());
1353 auto deviceRotation = static_cast<uint32_t>(displayInfo->GetDefaultDeviceRotationOffset());
1354 uint32_t transformHint = (rotation + deviceRotation) % FULL_CIRCLE_DEGREE;
1355 float density = GetVirtualPixelRatio(displayInfo);
1356 int32_t orientation = static_cast<int32_t>(displayInfo->GetDisplayOrientation());
1357 virtualPixelRatio_ = density;
1358 const auto& config = FillViewportConfig(rect, density, orientation, transformHint, GetDisplayId());
1359 std::shared_ptr<Ace::UIContent> uiContent = GetUIContentSharedPtr();
1360 if (uiContent == nullptr) {
1361 WLOGFW("uiContent is null!");
1362 return;
1363 }
1364 uiContent->UpdateViewportConfig(config, reason, rsTransaction, lastAvoidAreaMap_);
1365
1366 if (WindowHelper::IsUIExtensionWindow(GetType())) {
1367 TLOGD(WmsLogTag::WMS_LAYOUT, "Id: %{public}d, reason: %{public}d, windowRect: %{public}s, "
1368 "displayOrientation: %{public}d, config[%{public}u, %{public}u, %{public}u, "
1369 "%{public}f]", GetPersistentId(), reason, rect.ToString().c_str(), orientation,
1370 rotation, deviceRotation, transformHint, virtualPixelRatio_);
1371 } else {
1372 TLOGI(WmsLogTag::WMS_LAYOUT, "Id: %{public}d, reason: %{public}d, windowRect: %{public}s, "
1373 "displayOrientation: %{public}d, config[%{public}u, %{public}u, %{public}u, "
1374 "%{public}f]", GetPersistentId(), reason, rect.ToString().c_str(), orientation,
1375 rotation, deviceRotation, transformHint, virtualPixelRatio_);
1376 }
1377 }
1378
GetFloatingWindowParentId()1379 int32_t WindowSessionImpl::GetFloatingWindowParentId()
1380 {
1381 if (context_.get() == nullptr) {
1382 return INVALID_SESSION_ID;
1383 }
1384 std::shared_lock<std::shared_mutex> lock(windowSessionMutex_);
1385 for (const auto& winPair : windowSessionMap_) {
1386 if (winPair.second.second && WindowHelper::IsMainWindow(winPair.second.second->GetType()) &&
1387 context_.get() == winPair.second.second->GetContext().get()) {
1388 WLOGFD("Find parent, [parentName: %{public}s, selfPersistentId: %{public}d]",
1389 winPair.second.second->GetProperty()->GetWindowName().c_str(), GetPersistentId());
1390 return winPair.second.second->GetProperty()->GetPersistentId();
1391 }
1392 }
1393 return INVALID_SESSION_ID;
1394 }
1395
GetRect() const1396 Rect WindowSessionImpl::GetRect() const
1397 {
1398 return property_->GetWindowRect();
1399 }
1400
UpdateTitleButtonVisibility()1401 void WindowSessionImpl::UpdateTitleButtonVisibility()
1402 {
1403 std::shared_ptr<Ace::UIContent> uiContent = GetUIContentSharedPtr();
1404 if (uiContent == nullptr || !IsDecorEnable()) {
1405 return;
1406 }
1407 WindowType windowType = GetType();
1408 bool isSubWindow = WindowHelper::IsSubWindow(windowType);
1409 bool isDialogWindow = WindowHelper::IsDialogWindow(windowType);
1410 if (IsPcOrPadCapabilityEnabled() && (isSubWindow || isDialogWindow)) {
1411 uiContent->HideWindowTitleButton(true, !windowOption_->GetSubWindowMaximizeSupported(), true, false);
1412 return;
1413 }
1414 auto windowModeSupportType = property_->GetWindowModeSupportType();
1415 bool hideSplitButton = !(windowModeSupportType & WindowModeSupport::WINDOW_MODE_SUPPORT_SPLIT_PRIMARY);
1416 // not support fullscreen in split and floating mode, or not support float in fullscreen mode
1417 bool hideMaximizeButton = (!(windowModeSupportType & WindowModeSupport::WINDOW_MODE_SUPPORT_FULLSCREEN) &&
1418 (GetWindowMode() == WindowMode::WINDOW_MODE_FLOATING || WindowHelper::IsSplitWindowMode(GetWindowMode()))) ||
1419 (!(windowModeSupportType & WindowModeSupport::WINDOW_MODE_SUPPORT_FLOATING) &&
1420 GetWindowMode() == WindowMode::WINDOW_MODE_FULLSCREEN);
1421 bool hideMinimizeButton = false;
1422 bool hideCloseButton = false;
1423 GetTitleButtonVisible(hideMaximizeButton, hideMinimizeButton, hideSplitButton, hideCloseButton);
1424 TLOGI(WmsLogTag::WMS_DECOR, "[hideSplit, hideMaximize, hideMinimizeButton, hideCloseButton]:"
1425 "[%{public}d, %{public}d, %{public}d, %{public}d]",
1426 hideSplitButton, hideMaximizeButton, hideMinimizeButton, hideCloseButton);
1427 bool isSuperFoldDisplayDevice = FoldScreenStateInternel::IsSuperFoldDisplayDevice();
1428 if (property_->GetCompatibleModeInPc() && !property_->GetIsAtomicService()) {
1429 uiContent->HideWindowTitleButton(true, isSuperFoldDisplayDevice, hideMinimizeButton, hideCloseButton);
1430 uiContent->OnContainerModalEvent("scb_back_visibility", "true");
1431 } else {
1432 uiContent->HideWindowTitleButton(hideSplitButton, hideMaximizeButton, hideMinimizeButton, hideCloseButton);
1433 uiContent->OnContainerModalEvent("scb_back_visibility", "false");
1434 }
1435 if (isSuperFoldDisplayDevice) {
1436 handler_->PostTask([weakThis = wptr(this), where = __func__] {
1437 auto window = weakThis.promote();
1438 if (window == nullptr) {
1439 TLOGNE(WmsLogTag::WMS_LAYOUT_PC, "%{public}s window is null", where);
1440 return;
1441 }
1442 std::shared_ptr<Ace::UIContent> uiContent = window->GetUIContentSharedPtr();
1443 if (uiContent == nullptr || !window->IsDecorEnable()) {
1444 TLOGND(WmsLogTag::WMS_LAYOUT_PC, "%{public}s uiContent unavailable", where);
1445 return;
1446 }
1447 uiContent->OnContainerModalEvent(WINDOW_WATERFALL_VISIBILITY_EVENT,
1448 window->supportEnterWaterfallMode_ ? "true" : "false");
1449 }, "UIContentOnContainerModalEvent");
1450 }
1451 }
1452
NapiSetUIContent(const std::string & contentInfo,napi_env env,napi_value storage,BackupAndRestoreType type,sptr<IRemoteObject> token,AppExecFwk::Ability * ability)1453 WMError WindowSessionImpl::NapiSetUIContent(const std::string& contentInfo, napi_env env, napi_value storage,
1454 BackupAndRestoreType type, sptr<IRemoteObject> token, AppExecFwk::Ability* ability)
1455 {
1456 return SetUIContentInner(contentInfo, env, storage,
1457 type == BackupAndRestoreType::NONE ? WindowSetUIContentType::DEFAULT : WindowSetUIContentType::RESTORE,
1458 type, ability);
1459 }
1460
SetUIContentByName(const std::string & contentInfo,napi_env env,napi_value storage,AppExecFwk::Ability * ability)1461 WMError WindowSessionImpl::SetUIContentByName(
1462 const std::string& contentInfo, napi_env env, napi_value storage, AppExecFwk::Ability* ability)
1463 {
1464 return SetUIContentInner(contentInfo, env, storage, WindowSetUIContentType::BY_NAME,
1465 BackupAndRestoreType::NONE, ability);
1466 }
1467
SetUIContentByAbc(const std::string & contentInfo,napi_env env,napi_value storage,AppExecFwk::Ability * ability)1468 WMError WindowSessionImpl::SetUIContentByAbc(
1469 const std::string& contentInfo, napi_env env, napi_value storage, AppExecFwk::Ability* ability)
1470 {
1471 return SetUIContentInner(contentInfo, env, storage, WindowSetUIContentType::BY_ABC,
1472 BackupAndRestoreType::NONE, ability);
1473 }
1474
DestroyExistUIContent()1475 void WindowSessionImpl::DestroyExistUIContent()
1476 {
1477 if (auto uiContent = GetUIContentSharedPtr()) {
1478 uiContent->Destroy();
1479 }
1480 }
1481
InitUIContent(const std::string & contentInfo,napi_env env,napi_value storage,WindowSetUIContentType setUIContentType,BackupAndRestoreType restoreType,AppExecFwk::Ability * ability,OHOS::Ace::UIContentErrorCode & aceRet)1482 WMError WindowSessionImpl::InitUIContent(const std::string& contentInfo, napi_env env, napi_value storage,
1483 WindowSetUIContentType setUIContentType, BackupAndRestoreType restoreType, AppExecFwk::Ability* ability,
1484 OHOS::Ace::UIContentErrorCode& aceRet)
1485 {
1486 DestroyExistUIContent();
1487 std::unique_ptr<Ace::UIContent> uiContent = ability != nullptr ? Ace::UIContent::Create(ability) :
1488 Ace::UIContent::Create(context_.get(), reinterpret_cast<NativeEngine*>(env));
1489 if (uiContent == nullptr) {
1490 TLOGE(WmsLogTag::WMS_LIFE, "uiContent nullptr id: %{public}d", GetPersistentId());
1491 return WMError::WM_ERROR_NULLPTR;
1492 }
1493 switch (setUIContentType) {
1494 default:
1495 case WindowSetUIContentType::DEFAULT: {
1496 if (isUIExtensionAbilityProcess_ && property_->GetIsUIExtFirstSubWindow()) {
1497 // subWindow created by UIExtensionAbility
1498 uiContent->SetUIExtensionSubWindow(true);
1499 uiContent->SetUIExtensionAbilityProcess(true);
1500 } else {
1501 auto routerStack = GetRestoredRouterStack();
1502 auto type = GetAceContentInfoType(BackupAndRestoreType::RESOURCESCHEDULE_RECOVERY);
1503 if (!routerStack.empty() &&
1504 uiContent->Restore(this, routerStack, storage, type) == Ace::UIContentErrorCode::NO_ERRORS) {
1505 TLOGI(WmsLogTag::WMS_LIFE, "Restore router stack succeed.");
1506 break;
1507 }
1508 }
1509 aceRet = uiContent->Initialize(this, contentInfo, storage);
1510 break;
1511 }
1512 case WindowSetUIContentType::RESTORE:
1513 aceRet = uiContent->Restore(this, contentInfo, storage, GetAceContentInfoType(restoreType));
1514 break;
1515 case WindowSetUIContentType::BY_NAME:
1516 aceRet = uiContent->InitializeByName(this, contentInfo, storage);
1517 break;
1518 case WindowSetUIContentType::BY_ABC:
1519 auto abcContent = GetAbcContent(contentInfo);
1520 aceRet = uiContent->Initialize(this, abcContent, storage, contentInfo);
1521 break;
1522 }
1523 // make uiContent available after Initialize/Restore
1524 {
1525 std::unique_lock<std::shared_mutex> lock(uiContentMutex_);
1526 uiContent_ = std::move(uiContent);
1527 // compatibleMode app in pc, need change decor title to float title bar
1528 if (property_->GetCompatibleModeInPc()) {
1529 uiContent_->SetContainerModalTitleVisible(false, true);
1530 uiContent_->EnableContainerModalCustomGesture(true);
1531 }
1532 WLOGFI("Initialized, isUIExtensionSubWindow:%{public}d, isUIExtensionAbilityProcess:%{public}d",
1533 uiContent_->IsUIExtensionSubWindow(), uiContent_->IsUIExtensionAbilityProcess());
1534 }
1535 RegisterWatchFocusActiveChangeCallback();
1536 return WMError::WM_OK;
1537 }
1538
RegisterWatchFocusActiveChangeCallback()1539 void WindowSessionImpl::RegisterWatchFocusActiveChangeCallback()
1540 {
1541 if (auto uiContent = GetUIContentSharedPtr()) {
1542 uiContent->AddFocusActiveChangeCallback([this](bool isFocusActive) {
1543 NotifyWatchFocusActiveChange(isFocusActive);
1544 });
1545 } else {
1546 TLOGE(WmsLogTag::WMS_EVENT, "uiContent is nullptr");
1547 }
1548 }
1549
NotifyWatchFocusActiveChange(bool isActive)1550 WMError WindowSessionImpl::NotifyWatchFocusActiveChange(bool isActive)
1551 {
1552 TLOGD(WmsLogTag::WMS_EVENT, "isActive:%{public}d", isActive);
1553 if (IsWindowSessionInvalid()) {
1554 return WMError::WM_ERROR_INVALID_WINDOW;
1555 }
1556 return SingletonContainer::Get<WindowAdapter>().NotifyWatchFocusActiveChange(isActive);
1557 }
1558
SetUIContentInner(const std::string & contentInfo,napi_env env,napi_value storage,WindowSetUIContentType setUIContentType,BackupAndRestoreType restoreType,AppExecFwk::Ability * ability)1559 WMError WindowSessionImpl::SetUIContentInner(const std::string& contentInfo, napi_env env, napi_value storage,
1560 WindowSetUIContentType setUIContentType, BackupAndRestoreType restoreType, AppExecFwk::Ability* ability)
1561 {
1562 TLOGI(WmsLogTag::WMS_LIFE, "%{public}s, state:%{public}u, persistentId: %{public}d",
1563 contentInfo.c_str(), state_, GetPersistentId());
1564 if (IsWindowSessionInvalid()) {
1565 TLOGE(WmsLogTag::WMS_LIFE, "window is invalid! window state: %{public}d",
1566 state_);
1567 return WMError::WM_ERROR_INVALID_WINDOW;
1568 }
1569 NotifySetUIContentComplete();
1570 OHOS::Ace::UIContentErrorCode aceRet = OHOS::Ace::UIContentErrorCode::NO_ERRORS;
1571 WMError initUIContentRet = InitUIContent(contentInfo, env, storage, setUIContentType, restoreType, ability, aceRet);
1572 if (initUIContentRet != WMError::WM_OK) {
1573 TLOGE(WmsLogTag::WMS_LIFE, "Init UIContent fail, ret:%{public}u", initUIContentRet);
1574 return initUIContentRet;
1575 }
1576 if (auto uiContent = GetUIContentSharedPtr()) {
1577 TLOGI(WmsLogTag::WMS_LAYOUT, "id:%{public}d, posX:%{public}d, posY:%{public}d, "
1578 "scaleX:%{public}f, scaleY:%{public}f", GetPersistentId(),
1579 singleHandTransform_.posX, singleHandTransform_.posY,
1580 singleHandTransform_.scaleX, singleHandTransform_.scaleY);
1581 uiContent->UpdateSingleHandTransform(singleHandTransform_);
1582 }
1583 WindowType winType = GetType();
1584 bool isSubWindow = WindowHelper::IsSubWindow(winType);
1585 bool isDialogWindow = WindowHelper::IsDialogWindow(winType);
1586 if (IsDecorEnable()) {
1587 if (isSubWindow) {
1588 SetAPPWindowLabel(subWindowTitle_);
1589 } else if (isDialogWindow) {
1590 SetAPPWindowLabel(dialogTitle_);
1591 }
1592 }
1593
1594 AppForceLandscapeConfig config = {};
1595 if (WindowHelper::IsMainWindow(winType) && GetAppForceLandscapeConfig(config) == WMError::WM_OK &&
1596 config.mode_ == FORCE_SPLIT_MODE) {
1597 SetForceSplitEnable(true, config.homePage_);
1598 }
1599
1600 uint32_t version = 0;
1601 if ((context_ != nullptr) && (context_->GetApplicationInfo() != nullptr)) {
1602 version = context_->GetApplicationInfo()->apiCompatibleVersion;
1603 }
1604 // 10 ArkUI new framework support after API10
1605 if (version < 10) {
1606 SetLayoutFullScreenByApiVersion(isIgnoreSafeArea_);
1607 if (!isSystembarPropertiesSet_) {
1608 SetSystemBarProperty(WindowType::WINDOW_TYPE_STATUS_BAR, SystemBarProperty());
1609 }
1610 } else if (isIgnoreSafeAreaNeedNotify_) {
1611 SetLayoutFullScreenByApiVersion(isIgnoreSafeArea_);
1612 } else if (isSubWindow) {
1613 SetLayoutFullScreenByApiVersion(isIgnoreSafeArea_);
1614 isIgnoreSafeAreaNeedNotify_ = false;
1615 }
1616
1617 // UIContent may be nullptr on setting system bar properties, need to set menubar color on UIContent init.
1618 if (auto uiContent = GetUIContentSharedPtr()) {
1619 auto property = GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_STATUS_BAR);
1620 uiContent->SetStatusBarItemColor(property.contentColor_);
1621 }
1622
1623 UpdateDecorEnable(true);
1624 if (state_ == WindowState::STATE_SHOWN) {
1625 // UIContent may be nullptr when show window, need to notify again when window is shown
1626 if (auto uiContent = GetUIContentSharedPtr()) {
1627 uiContent->Foreground();
1628 }
1629 UpdateTitleButtonVisibility();
1630 }
1631 UpdateViewportConfig(GetRect(), WindowSizeChangeReason::UNDEFINED);
1632 if (shouldReNotifyFocus_) {
1633 // uiContent may be nullptr when notify focus status, need to notify again when uiContent is not empty.
1634 NotifyUIContentFocusStatus();
1635 shouldReNotifyFocus_ = false;
1636 }
1637 if (shouldReNotifyHighlight_) {
1638 // uiContent may be nullptr when notify highlight status, need to notify again when uiContent is not empty.
1639 NotifyUIContentHighlightStatus(isHighlighted_);
1640 shouldReNotifyHighlight_ = false;
1641 }
1642 if (aceRet != OHOS::Ace::UIContentErrorCode::NO_ERRORS) {
1643 WLOGFE("failed to init or restore uicontent with file %{public}s. errorCode: %{public}d",
1644 contentInfo.c_str(), static_cast<uint16_t>(aceRet));
1645 return WMError::WM_ERROR_INVALID_PARAM;
1646 }
1647 NotifyAfterUIContentReady();
1648 TLOGD(WmsLogTag::WMS_LIFE, "end");
1649 return WMError::WM_OK;
1650 }
1651
GetAbcContent(const std::string & abcPath)1652 std::shared_ptr<std::vector<uint8_t>> WindowSessionImpl::GetAbcContent(const std::string& abcPath)
1653 {
1654 std::filesystem::path abcFile { abcPath };
1655 if (abcFile.empty() || !abcFile.is_absolute() || !std::filesystem::exists(abcFile)) {
1656 WLOGFE("abc file path is not valid");
1657 return nullptr;
1658 }
1659 int begin, end;
1660 std::fstream file(abcFile, std::ios::in | std::ios::binary);
1661 if (!file) {
1662 WLOGFE("abc file is not valid");
1663 return nullptr;
1664 }
1665 begin = file.tellg();
1666 file.seekg(0, std::ios::end);
1667 end = file.tellg();
1668 int len = end - begin;
1669 WLOGFD("abc file: %{public}s, size: %{public}d", abcPath.c_str(), len);
1670
1671 if (len <= 0) {
1672 WLOGFE("abc file size is 0");
1673 return nullptr;
1674 }
1675 std::vector<uint8_t> abcBytes(len);
1676 file.seekg(0, std::ios::beg);
1677 file.read(reinterpret_cast<char *>(abcBytes.data()), len);
1678 return std::make_shared<std::vector<uint8_t>>(abcBytes);
1679 }
1680
UpdateDecorEnableToAce(bool isDecorEnable)1681 void WindowSessionImpl::UpdateDecorEnableToAce(bool isDecorEnable)
1682 {
1683 if (auto uiContent = GetUIContentSharedPtr()) {
1684 WindowMode mode = GetWindowMode();
1685 bool decorVisible = mode == WindowMode::WINDOW_MODE_FLOATING ||
1686 mode == WindowMode::WINDOW_MODE_SPLIT_PRIMARY || mode == WindowMode::WINDOW_MODE_SPLIT_SECONDARY ||
1687 (mode == WindowMode::WINDOW_MODE_FULLSCREEN && !property_->IsLayoutFullScreen());
1688 TLOGD(WmsLogTag::WMS_DECOR, "decorVisible:%{public}d", decorVisible);
1689 if (windowSystemConfig_.freeMultiWindowSupport_) {
1690 auto isSubWindow = WindowHelper::IsSubWindow(GetType());
1691 decorVisible = decorVisible && (windowSystemConfig_.freeMultiWindowEnable_ ||
1692 (property_->GetIsPcAppInPad() && isSubWindow));
1693 }
1694 uiContent->UpdateDecorVisible(decorVisible, isDecorEnable);
1695 return;
1696 }
1697
1698 std::lock_guard<std::recursive_mutex> lockListener(windowChangeListenerMutex_);
1699 auto windowChangeListeners = GetListeners<IWindowChangeListener>();
1700 for (auto& listener : windowChangeListeners) {
1701 if (listener.GetRefPtr() != nullptr) {
1702 listener.GetRefPtr()->OnModeChange(GetWindowMode(), isDecorEnable);
1703 }
1704 }
1705 }
1706
UpdateDecorEnable(bool needNotify,WindowMode mode)1707 void WindowSessionImpl::UpdateDecorEnable(bool needNotify, WindowMode mode)
1708 {
1709 if (mode == WindowMode::WINDOW_MODE_UNDEFINED) {
1710 mode = GetWindowMode();
1711 }
1712 if (needNotify) {
1713 if (auto uiContent = GetUIContentSharedPtr()) {
1714 bool decorVisible = mode == WindowMode::WINDOW_MODE_FLOATING ||
1715 mode == WindowMode::WINDOW_MODE_SPLIT_PRIMARY || mode == WindowMode::WINDOW_MODE_SPLIT_SECONDARY ||
1716 (mode == WindowMode::WINDOW_MODE_FULLSCREEN && !property_->IsLayoutFullScreen());
1717 if (windowSystemConfig_.freeMultiWindowSupport_) {
1718 auto isSubWindow = WindowHelper::IsSubWindow(GetType());
1719 decorVisible = decorVisible && (windowSystemConfig_.freeMultiWindowEnable_ ||
1720 (property_->GetIsPcAppInPad() && isSubWindow));
1721 }
1722 TLOGD(WmsLogTag::WMS_DECOR, "decorVisible:%{public}d", decorVisible);
1723 uiContent->UpdateDecorVisible(decorVisible, IsDecorEnable());
1724 uiContent->NotifyWindowMode(mode);
1725 }
1726 NotifyModeChange(mode, IsDecorEnable());
1727 }
1728 }
1729
1730 /** @note @window.layout */
NotifyModeChange(WindowMode mode,bool hasDeco)1731 void WindowSessionImpl::NotifyModeChange(WindowMode mode, bool hasDeco)
1732 {
1733 {
1734 std::lock_guard<std::recursive_mutex> lockListener(windowChangeListenerMutex_);
1735 auto windowChangeListeners = GetListeners<IWindowChangeListener>();
1736 for (auto& listener : windowChangeListeners) {
1737 if (listener.GetRefPtr() != nullptr) {
1738 listener.GetRefPtr()->OnModeChange(mode, hasDeco);
1739 }
1740 }
1741 }
1742
1743 if (GetHostSession()) {
1744 property_->SetWindowMode(mode);
1745 property_->SetDecorEnable(hasDeco);
1746 }
1747 UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_MODE);
1748 UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_DECOR_ENABLE);
1749 }
1750
GetSurfaceNode() const1751 std::shared_ptr<RSSurfaceNode> WindowSessionImpl::GetSurfaceNode() const
1752 {
1753 TLOGI(WmsLogTag::DEFAULT, "name:%{public}s, id:%{public}d",
1754 property_->GetWindowName().c_str(), GetPersistentId());
1755 return surfaceNode_;
1756 }
1757
GetContext() const1758 const std::shared_ptr<AbilityRuntime::Context> WindowSessionImpl::GetContext() const
1759 {
1760 TLOGI(WmsLogTag::DEFAULT, "name:%{public}s, id:%{public}d",
1761 property_->GetWindowName().c_str(), GetPersistentId());
1762 return context_;
1763 }
1764
GetRequestRect() const1765 Rect WindowSessionImpl::GetRequestRect() const
1766 {
1767 return property_->GetRequestRect();
1768 }
1769
GetType() const1770 WindowType WindowSessionImpl::GetType() const
1771 {
1772 return property_->GetWindowType();
1773 }
1774
GetWindowName() const1775 const std::string& WindowSessionImpl::GetWindowName() const
1776 {
1777 return property_->GetWindowName();
1778 }
1779
GetWindowState() const1780 WindowState WindowSessionImpl::GetWindowState() const
1781 {
1782 return state_;
1783 }
1784
GetRequestWindowState() const1785 WindowState WindowSessionImpl::GetRequestWindowState() const
1786 {
1787 return requestState_;
1788 }
1789
1790 /** @note @window.focus */
SetExclusivelyHighlighted(bool isExclusivelyHighlighted)1791 WMError WindowSessionImpl::SetExclusivelyHighlighted(bool isExclusivelyHighlighted)
1792 {
1793 if (IsWindowSessionInvalid()) {
1794 return WMError::WM_ERROR_INVALID_WINDOW;
1795 }
1796 TLOGI(WmsLogTag::WMS_FOCUS, "windowId: %{public}d, isExclusivelyHighlighted: %{public}d",
1797 property_->GetPersistentId(), isExclusivelyHighlighted);
1798 if (WindowHelper::IsMainWindow(GetType()) || WindowHelper::IsDialogWindow(GetType()) ||
1799 WindowHelper::IsModalWindow(property_->GetWindowFlags())) {
1800 TLOGE(WmsLogTag::WMS_FOCUS, "unsupport window, type: %{public}u, windowFlags: %{public}u ",
1801 GetType(), property_->GetWindowFlags());
1802 return WMError::WM_ERROR_INVALID_CALLING;
1803 }
1804 if (property_->GetExclusivelyHighlighted() == isExclusivelyHighlighted) {
1805 TLOGD(WmsLogTag::WMS_FOCUS, "already exclusivelyHighlighted");
1806 return WMError::WM_OK;
1807 }
1808 property_->SetExclusivelyHighlighted(isExclusivelyHighlighted);
1809 return UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_EXCLUSIVE_HIGHLIGHTED);
1810 }
1811
1812 /** @note @window.focus */
GetExclusivelyHighlighted() const1813 bool WindowSessionImpl::GetExclusivelyHighlighted() const
1814 {
1815 bool isExclusivelyHighlighted = property_->GetExclusivelyHighlighted();
1816 TLOGD(WmsLogTag::WMS_FOCUS, "windowId: %{public}d, isExclusivelyHighlighted: %{public}d",
1817 property_->GetPersistentId(), isExclusivelyHighlighted);
1818 return isExclusivelyHighlighted;
1819 }
1820
1821 /** @note @window.focus */
NotifyHighlightChange(bool isHighlight)1822 WSError WindowSessionImpl::NotifyHighlightChange(bool isHighlight)
1823 {
1824 TLOGI(WmsLogTag::WMS_FOCUS, "windowId: %{public}d, isHighlight: %{public}u,", GetPersistentId(), isHighlight);
1825 isHighlighted_ = isHighlight;
1826 if (GetUIContentSharedPtr() != nullptr) {
1827 NotifyUIContentHighlightStatus(isHighlighted_);
1828 } else {
1829 shouldReNotifyHighlight_ = true;
1830 }
1831 std::lock_guard<std::mutex> lockListener(highlightChangeListenerMutex_);
1832 auto highlightChangeListeners = GetListeners<IWindowHighlightChangeListener>();
1833 for (const auto& listener : highlightChangeListeners) {
1834 if (listener != nullptr) {
1835 listener->OnWindowHighlightChange(isHighlight);
1836 }
1837 }
1838 return WSError::WS_OK;
1839 }
1840
1841 /** @note @window.focus */
IsWindowHighlighted(bool & highlighted) const1842 WMError WindowSessionImpl::IsWindowHighlighted(bool& highlighted) const
1843 {
1844 if (IsWindowSessionInvalid()) {
1845 TLOGE(WmsLogTag::WMS_FOCUS, "session is invalid");
1846 return WMError::WM_ERROR_INVALID_WINDOW;
1847 }
1848 auto hostSession = GetHostSession();
1849 hostSession->GetIsHighlighted(highlighted);
1850 TLOGD(WmsLogTag::WMS_FOCUS, "windowId: %{public}d, isWindowHighlighted: %{public}d",
1851 GetPersistentId(), highlighted);
1852 return WMError::WM_OK;
1853 }
1854
SetFocusable(bool isFocusable)1855 WMError WindowSessionImpl::SetFocusable(bool isFocusable)
1856 {
1857 if (IsWindowSessionInvalid()) {
1858 return WMError::WM_ERROR_INVALID_WINDOW;
1859 }
1860 TLOGI(WmsLogTag::WMS_FOCUS, "set focusable: windowId=%{public}d, isFocusable=%{public}d",
1861 property_->GetPersistentId(), isFocusable);
1862 property_->SetFocusable(isFocusable);
1863 return UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_FOCUSABLE);
1864 }
1865
GetFocusable() const1866 bool WindowSessionImpl::GetFocusable() const
1867 {
1868 bool isFocusable = property_->GetFocusable();
1869 TLOGD(WmsLogTag::WMS_FOCUS, "get focusable: windowId=%{public}d, isFocusable=%{public}d",
1870 property_->GetPersistentId(), isFocusable);
1871 return isFocusable;
1872 }
1873
SetTouchable(bool isTouchable)1874 WMError WindowSessionImpl::SetTouchable(bool isTouchable)
1875 {
1876 WLOGFD("%{public}d", isTouchable);
1877 if (IsWindowSessionInvalid()) {
1878 return WMError::WM_ERROR_INVALID_WINDOW;
1879 }
1880 property_->SetTouchable(isTouchable);
1881 return UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_TOUCHABLE);
1882 }
1883
1884 /** @note @window.hierarchy */
SetTopmost(bool topmost)1885 WMError WindowSessionImpl::SetTopmost(bool topmost)
1886 {
1887 TLOGD(WmsLogTag::WMS_HIERARCHY, "%{public}d", topmost);
1888 if (!windowSystemConfig_.IsPcWindow()) {
1889 return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
1890 }
1891 if (IsWindowSessionInvalid()) {
1892 return WMError::WM_ERROR_INVALID_WINDOW;
1893 }
1894 property_->SetTopmost(topmost);
1895 return UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_TOPMOST);
1896 }
1897
1898 /** @note @window.hierarchy */
IsTopmost() const1899 bool WindowSessionImpl::IsTopmost() const
1900 {
1901 return property_->IsTopmost();
1902 }
1903
1904 /** @note @window.hierarchy */
SetMainWindowTopmost(bool isTopmost)1905 WMError WindowSessionImpl::SetMainWindowTopmost(bool isTopmost)
1906 {
1907 if (IsWindowSessionInvalid()) {
1908 TLOGE(WmsLogTag::WMS_HIERARCHY, "session is invalid");
1909 return WMError::WM_ERROR_INVALID_WINDOW;
1910 }
1911 if (!IsPcOrPadFreeMultiWindowMode()) {
1912 TLOGE(WmsLogTag::WMS_HIERARCHY, "device not support");
1913 return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
1914 }
1915 if (!WindowHelper::IsMainWindow(GetType())) {
1916 TLOGE(WmsLogTag::WMS_HIERARCHY, "window type is not supported");
1917 return WMError::WM_ERROR_INVALID_CALLING;
1918 }
1919 property_->SetMainWindowTopmost(isTopmost);
1920 uint32_t accessTokenId = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
1921 property_->SetAccessTokenId(accessTokenId);
1922 TLOGD(WmsLogTag::WMS_HIERARCHY, "tokenId=%{private}u, isTopmost=%{public}d", accessTokenId, isTopmost);
1923 return UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_MAIN_WINDOW_TOPMOST);
1924 }
1925
IsMainWindowTopmost() const1926 bool WindowSessionImpl::IsMainWindowTopmost() const
1927 {
1928 return property_->IsMainWindowTopmost();
1929 }
1930
SetWindowDelayRaiseEnabled(bool isEnabled)1931 WMError WindowSessionImpl::SetWindowDelayRaiseEnabled(bool isEnabled)
1932 {
1933 if (IsWindowSessionInvalid()) {
1934 return WMError::WM_ERROR_INVALID_WINDOW;
1935 }
1936 if (!IsPcOrPadFreeMultiWindowMode()) {
1937 TLOGE(WmsLogTag::WMS_FOCUS, "The device is not supported");
1938 return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
1939 }
1940 if (!WindowHelper::IsAppWindow(GetType())) {
1941 TLOGE(WmsLogTag::WMS_FOCUS, "window type is not supported");
1942 return WMError::WM_ERROR_INVALID_TYPE;
1943 }
1944 property_->SetWindowDelayRaiseEnabled(isEnabled);
1945 TLOGI(WmsLogTag::WMS_FOCUS, "isEnabled: %{public}d", isEnabled);
1946 return WMError::WM_OK;
1947 }
1948
IsWindowDelayRaiseEnabled() const1949 bool WindowSessionImpl::IsWindowDelayRaiseEnabled() const
1950 {
1951 return property_->IsWindowDelayRaiseEnabled();
1952 }
1953
SetResizeByDragEnabled(bool dragEnabled)1954 WMError WindowSessionImpl::SetResizeByDragEnabled(bool dragEnabled)
1955 {
1956 TLOGD(WmsLogTag::WMS_LAYOUT, "%{public}d", dragEnabled);
1957 if (IsWindowSessionInvalid()) {
1958 TLOGE(WmsLogTag::WMS_LAYOUT, "Session is invalid");
1959 return WMError::WM_ERROR_INVALID_WINDOW;
1960 }
1961 if (WindowHelper::IsMainWindow(GetType()) ||
1962 (WindowHelper::IsSubWindow(GetType()) && windowOption_->GetSubWindowDecorEnable())) {
1963 property_->SetDragEnabled(dragEnabled);
1964 } else {
1965 TLOGE(WmsLogTag::WMS_LAYOUT, "This is not main window or decor enabled sub window.");
1966 return WMError::WM_ERROR_INVALID_TYPE;
1967 }
1968 return UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_DRAGENABLED);
1969 }
1970
1971 /** @note @window.hierarchy */
SetRaiseByClickEnabled(bool raiseEnabled)1972 WMError WindowSessionImpl::SetRaiseByClickEnabled(bool raiseEnabled)
1973 {
1974 WLOGFD("%{public}d", raiseEnabled);
1975 auto parentId = GetParentId();
1976 if (parentId == INVALID_SESSION_ID) {
1977 TLOGE(WmsLogTag::WMS_HIERARCHY, "Window id: %{public}d Parent id is invalid!",
1978 GetPersistentId());
1979 return WMError::WM_ERROR_INVALID_PARENT;
1980 }
1981
1982 if (!WindowHelper::IsSubWindow(GetType())) {
1983 TLOGE(WmsLogTag::WMS_HIERARCHY, "Window id: %{public}d Must be app sub window!",
1984 GetPersistentId());
1985 return WMError::WM_ERROR_INVALID_CALLING;
1986 }
1987
1988 if (state_ != WindowState::STATE_SHOWN) {
1989 TLOGE(WmsLogTag::WMS_HIERARCHY, "Window id: %{public}d The sub window must be shown!",
1990 GetPersistentId());
1991 return WMError::WM_DO_NOTHING;
1992 }
1993 if (IsWindowSessionInvalid()) {
1994 return WMError::WM_ERROR_INVALID_WINDOW;
1995 }
1996
1997 property_->SetRaiseEnabled(raiseEnabled);
1998 return UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_RAISEENABLED);
1999 }
2000
2001 /** @note @window.immersive */
SetAvoidAreaOption(uint32_t avoidAreaOption)2002 WMError WindowSessionImpl::SetAvoidAreaOption(uint32_t avoidAreaOption)
2003 {
2004 if (IsWindowSessionInvalid()) {
2005 TLOGE(WmsLogTag::WMS_IMMS, "Session is invalid");
2006 return WMError::WM_ERROR_INVALID_WINDOW;
2007 }
2008 property_->SetAvoidAreaOption(avoidAreaOption);
2009 TLOGI(WmsLogTag::WMS_IMMS, "win %{public}d, set option %{public}d",
2010 GetPersistentId(), avoidAreaOption);
2011 return UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_AVOID_AREA_OPTION);
2012 }
2013
2014 /** @note @window.immersive */
GetAvoidAreaOption(uint32_t & avoidAreaOption)2015 WMError WindowSessionImpl::GetAvoidAreaOption(uint32_t& avoidAreaOption)
2016 {
2017 if (IsWindowSessionInvalid()) {
2018 TLOGE(WmsLogTag::WMS_IMMS, "Session is invalid");
2019 return WMError::WM_ERROR_INVALID_WINDOW;
2020 }
2021 avoidAreaOption = property_->GetAvoidAreaOption();
2022 return WMError::WM_OK;
2023 }
2024
HideNonSystemFloatingWindows(bool shouldHide)2025 WMError WindowSessionImpl::HideNonSystemFloatingWindows(bool shouldHide)
2026 {
2027 WLOGFD("%{public}d", shouldHide);
2028 if (IsWindowSessionInvalid()) {
2029 return WMError::WM_ERROR_INVALID_WINDOW;
2030 }
2031 property_->SetHideNonSystemFloatingWindows(shouldHide);
2032 return UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_HIDE_NON_SYSTEM_FLOATING_WINDOWS);
2033 }
2034
SetLandscapeMultiWindow(bool isLandscapeMultiWindow)2035 WMError WindowSessionImpl::SetLandscapeMultiWindow(bool isLandscapeMultiWindow)
2036 {
2037 TLOGI(WmsLogTag::WMS_MULTI_WINDOW, "isLandscapeMultiWindow:%{public}d",
2038 isLandscapeMultiWindow);
2039 if (IsWindowSessionInvalid()) {
2040 TLOGE(WmsLogTag::WMS_MULTI_WINDOW, "Session is invalid");
2041 return WMError::WM_ERROR_INVALID_WINDOW;
2042 }
2043 auto hostSession = GetHostSession();
2044 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, WMError::WM_ERROR_INVALID_WINDOW);
2045 hostSession->SetLandscapeMultiWindow(isLandscapeMultiWindow);
2046 return WMError::WM_OK;
2047 }
2048
SetSingleFrameComposerEnabled(bool enable)2049 WMError WindowSessionImpl::SetSingleFrameComposerEnabled(bool enable)
2050 {
2051 WLOGFD("enable %{public}d", enable);
2052 if (IsWindowSessionInvalid()) {
2053 WLOGE("The window state is invalid ");
2054 return WMError::WM_ERROR_INVALID_WINDOW;
2055 }
2056
2057 if (surfaceNode_ == nullptr) {
2058 WLOGE("The surface node is nullptr");
2059 return WMError::WM_ERROR_INVALID_WINDOW;
2060 }
2061
2062 surfaceNode_->MarkNodeSingleFrameComposer(enable);
2063 RSTransaction::FlushImplicitTransaction();
2064 return WMError::WM_OK;
2065 }
2066
IsFloatingWindowAppType() const2067 bool WindowSessionImpl::IsFloatingWindowAppType() const
2068 {
2069 if (IsWindowSessionInvalid()) {
2070 return false;
2071 }
2072 return property_->IsFloatingWindowAppType();
2073 }
2074
GetTouchable() const2075 bool WindowSessionImpl::GetTouchable() const
2076 {
2077 return property_->GetTouchable();
2078 }
2079
SetWindowType(WindowType type)2080 WMError WindowSessionImpl::SetWindowType(WindowType type)
2081 {
2082 TLOGD(WmsLogTag::DEFAULT, "%{public}u type %{public}u", GetWindowId(), static_cast<uint32_t>(type));
2083 if (type != WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW && !SessionPermission::IsSystemCalling()) {
2084 TLOGE(WmsLogTag::DEFAULT, "permission denied!");
2085 return WMError::WM_ERROR_NOT_SYSTEM_APP;
2086 }
2087 if (IsWindowSessionInvalid()) {
2088 TLOGE(WmsLogTag::DEFAULT, "Session is invalid");
2089 return WMError::WM_ERROR_INVALID_WINDOW;
2090 }
2091 property_->SetWindowType(type);
2092 UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_OTHER_PROPS);
2093 return WMError::WM_OK;
2094 }
2095
SetBrightness(float brightness)2096 WMError WindowSessionImpl::SetBrightness(float brightness)
2097 {
2098 if ((brightness < MINIMUM_BRIGHTNESS &&
2099 std::fabs(brightness - UNDEFINED_BRIGHTNESS) >= std::numeric_limits<float>::min()) ||
2100 brightness > MAXIMUM_BRIGHTNESS) {
2101 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "invalid brightness value: %{public}f", brightness);
2102 return WMError::WM_ERROR_INVALID_PARAM;
2103 }
2104 if (!WindowHelper::IsAppWindow(GetType())) {
2105 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "non app window does not support set brightness, type: %{public}u", GetType());
2106 return WMError::WM_ERROR_INVALID_TYPE;
2107 }
2108 property_->SetBrightness(brightness);
2109 return UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_SET_BRIGHTNESS);
2110 }
2111
GetBrightness() const2112 float WindowSessionImpl::GetBrightness() const
2113 {
2114 return property_->GetBrightness();
2115 }
2116
SetRequestedOrientation(Orientation orientation,bool needAnimation)2117 void WindowSessionImpl::SetRequestedOrientation(Orientation orientation, bool needAnimation)
2118 {
2119 if (IsWindowSessionInvalid()) {
2120 TLOGE(WmsLogTag::DEFAULT, "windowSession is invalid");
2121 return;
2122 }
2123 TLOGI(WmsLogTag::WMS_MAIN, "id:%{public}u lastReqOrientation:%{public}u target:%{public}u state:%{public}u",
2124 GetPersistentId(), property_->GetRequestedOrientation(), orientation, state_);
2125 bool isUserOrientation = IsUserOrientation(orientation);
2126 if (property_->GetRequestedOrientation() == orientation && !isUserOrientation) {
2127 return;
2128 }
2129 property_->SetRequestedOrientation(orientation);
2130 UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_ORIENTATION);
2131 }
2132
GetRequestedOrientation()2133 Orientation WindowSessionImpl::GetRequestedOrientation()
2134 {
2135 if (IsWindowSessionInvalid()) {
2136 TLOGE(WmsLogTag::DEFAULT, "windowSession is invalid");
2137 return Orientation::UNSPECIFIED;
2138 }
2139 return property_->GetRequestedOrientation();
2140 }
2141
GetContentInfo(BackupAndRestoreType type)2142 std::string WindowSessionImpl::GetContentInfo(BackupAndRestoreType type)
2143 {
2144 WLOGFD("in");
2145 if (type == BackupAndRestoreType::NONE) {
2146 return "";
2147 }
2148 std::shared_ptr<Ace::UIContent> uiContent = GetUIContentSharedPtr();
2149 if (uiContent == nullptr) {
2150 WLOGFE("fail to GetContentInfo id: %{public}d", GetPersistentId());
2151 return "";
2152 }
2153 return uiContent->GetContentInfo(GetAceContentInfoType(type));
2154 }
2155
SetRestoredRouterStack(const std::string & routerStack)2156 WMError WindowSessionImpl::SetRestoredRouterStack(const std::string& routerStack)
2157 {
2158 TLOGD(WmsLogTag::WMS_LIFE, "in");
2159 restoredRouterStack_ = routerStack;
2160 return WMError::WM_OK;
2161 }
2162
GetRestoredRouterStack()2163 std::string WindowSessionImpl::GetRestoredRouterStack()
2164 {
2165 TLOGD(WmsLogTag::WMS_LIFE, "in");
2166 return std::move(restoredRouterStack_);
2167 }
2168
GetUIContent() const2169 Ace::UIContent* WindowSessionImpl::GetUIContent() const
2170 {
2171 std::shared_lock<std::shared_mutex> lock(uiContentMutex_);
2172 return uiContent_.get();
2173 }
2174
GetUIContentSharedPtr() const2175 std::shared_ptr<Ace::UIContent> WindowSessionImpl::GetUIContentSharedPtr() const
2176 {
2177 std::shared_lock<std::shared_mutex> lock(uiContentMutex_);
2178 return uiContent_;
2179 }
2180
GetUIContentWithId(uint32_t winId) const2181 Ace::UIContent* WindowSessionImpl::GetUIContentWithId(uint32_t winId) const
2182 {
2183 sptr<Window> targetWindow = FindWindowById(winId);
2184 if (targetWindow == nullptr) {
2185 WLOGE("target window is null");
2186 return nullptr;
2187 }
2188 return targetWindow->GetUIContent();
2189 }
2190
OnNewWant(const AAFwk::Want & want)2191 void WindowSessionImpl::OnNewWant(const AAFwk::Want& want)
2192 {
2193 WLOGFI("[name:%{public}s, id:%{public}d]",
2194 property_->GetWindowName().c_str(), GetPersistentId());
2195 if (auto uiContent = GetUIContentSharedPtr()) {
2196 uiContent->OnNewWant(want);
2197 }
2198 }
2199
SetAPPWindowLabel(const std::string & label)2200 WMError WindowSessionImpl::SetAPPWindowLabel(const std::string& label)
2201 {
2202 TLOGI(WmsLogTag::WMS_DECOR, "Enter");
2203 std::shared_ptr<Ace::UIContent> uiContent = GetUIContentSharedPtr();
2204 if (uiContent == nullptr) {
2205 TLOGI(WmsLogTag::WMS_DECOR, "uicontent is empty");
2206 return WMError::WM_ERROR_NULLPTR;
2207 }
2208 uiContent->SetAppWindowTitle(label);
2209 return WMError::WM_OK;
2210 }
2211
SetAPPWindowIcon(const std::shared_ptr<Media::PixelMap> & icon)2212 WMError WindowSessionImpl::SetAPPWindowIcon(const std::shared_ptr<Media::PixelMap>& icon)
2213 {
2214 if (icon == nullptr) {
2215 TLOGE(WmsLogTag::WMS_DECOR, "icon is empty");
2216 return WMError::WM_ERROR_NULLPTR;
2217 }
2218 std::shared_ptr<Ace::UIContent> uiContent = GetUIContentSharedPtr();
2219 if (uiContent == nullptr) {
2220 TLOGE(WmsLogTag::WMS_DECOR, "uicontent is empty");
2221 return WMError::WM_ERROR_NULLPTR;
2222 }
2223 uiContent->SetAppWindowIcon(icon);
2224 TLOGD(WmsLogTag::WMS_DECOR, "end");
2225 return WMError::WM_OK;
2226 }
2227
RegisterLifeCycleListener(const sptr<IWindowLifeCycle> & listener)2228 WMError WindowSessionImpl::RegisterLifeCycleListener(const sptr<IWindowLifeCycle>& listener)
2229 {
2230 WLOGFD("in");
2231 std::lock_guard<std::recursive_mutex> lockListener(lifeCycleListenerMutex_);
2232 return RegisterListener(lifecycleListeners_[GetPersistentId()], listener);
2233 }
2234
RegisterDisplayMoveListener(sptr<IDisplayMoveListener> & listener)2235 WMError WindowSessionImpl::RegisterDisplayMoveListener(sptr<IDisplayMoveListener>& listener)
2236 {
2237 WLOGFD("in");
2238 std::lock_guard<std::mutex> lockListener(displayMoveListenerMutex_);
2239 return RegisterListener(displayMoveListeners_[GetPersistentId()], listener);
2240 }
2241
UnregisterDisplayMoveListener(sptr<IDisplayMoveListener> & listener)2242 WMError WindowSessionImpl::UnregisterDisplayMoveListener(sptr<IDisplayMoveListener>& listener)
2243 {
2244 WLOGFD("in");
2245 std::lock_guard<std::mutex> lockListener(displayMoveListenerMutex_);
2246 return UnregisterListener(displayMoveListeners_[GetPersistentId()], listener);
2247 }
2248
2249 /**
2250 * Currently only supports system windows.
2251 */
EnableDrag(bool enableDrag)2252 WMError WindowSessionImpl::EnableDrag(bool enableDrag)
2253 {
2254 if (!IsPcOrPadFreeMultiWindowMode()) {
2255 TLOGE(WmsLogTag::WMS_LAYOUT, "The device is not supported");
2256 return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
2257 }
2258 property_->SetDragEnabled(enableDrag);
2259 auto hostSession = GetHostSession();
2260 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, WMError::WM_ERROR_INVALID_WINDOW);
2261 WMError errorCode = hostSession->SetSystemWindowEnableDrag(enableDrag);
2262 TLOGI(WmsLogTag::WMS_LAYOUT, "Id:%{public}d, enableDrag:%{public}d, errcode:%{public}d",
2263 GetPersistentId(), enableDrag, static_cast<int>(errorCode));
2264 return static_cast<WMError>(errorCode);
2265 }
2266
RegisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener> & listener)2267 WMError WindowSessionImpl::RegisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener>& listener)
2268 {
2269 WLOGFD("in");
2270 std::lock_guard<std::recursive_mutex> lockListener(occupiedAreaChangeListenerMutex_);
2271 return RegisterListener(occupiedAreaChangeListeners_[GetPersistentId()], listener);
2272 }
2273
UnregisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener> & listener)2274 WMError WindowSessionImpl::UnregisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener>& listener)
2275 {
2276 WLOGFD("in");
2277 std::lock_guard<std::recursive_mutex> lockListener(occupiedAreaChangeListenerMutex_);
2278 return UnregisterListener(occupiedAreaChangeListeners_[GetPersistentId()], listener);
2279 }
2280
RegisterKeyboardDidShowListener(const sptr<IKeyboardDidShowListener> & listener)2281 WMError WindowSessionImpl::RegisterKeyboardDidShowListener(const sptr<IKeyboardDidShowListener>& listener)
2282 {
2283 TLOGD(WmsLogTag::WMS_KEYBOARD, "in");
2284 std::lock_guard<std::recursive_mutex> lockListener(keyboardDidShowListenerMutex_);
2285 WMError ret = RegisterListener(keyboardDidShowListeners_[GetPersistentId()], listener);
2286 if (ret == WMError::WM_OK && isKeyboardDidShowRegistered_ == false) {
2287 auto hostSession = GetHostSession();
2288 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, WMError::WM_ERROR_INVALID_WINDOW);
2289 hostSession->NotifyKeyboardDidShowRegistered(true);
2290 isKeyboardDidShowRegistered_ = true;
2291 }
2292 return ret;
2293 }
2294
UnregisterKeyboardDidShowListener(const sptr<IKeyboardDidShowListener> & listener)2295 WMError WindowSessionImpl::UnregisterKeyboardDidShowListener(const sptr<IKeyboardDidShowListener>& listener)
2296 {
2297 TLOGD(WmsLogTag::WMS_KEYBOARD, "in");
2298 std::lock_guard<std::recursive_mutex> lockListener(keyboardDidShowListenerMutex_);
2299 WMError ret = UnregisterListener(keyboardDidShowListeners_[GetPersistentId()], listener);
2300 if (keyboardDidShowListeners_[GetPersistentId()].empty()) {
2301 auto hostSession = GetHostSession();
2302 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, WMError::WM_ERROR_INVALID_WINDOW);
2303 hostSession->NotifyKeyboardDidShowRegistered(false);
2304 isKeyboardDidShowRegistered_ = false;
2305 }
2306 return ret;
2307 }
2308
RegisterKeyboardDidHideListener(const sptr<IKeyboardDidHideListener> & listener)2309 WMError WindowSessionImpl::RegisterKeyboardDidHideListener(const sptr<IKeyboardDidHideListener>& listener)
2310 {
2311 TLOGD(WmsLogTag::WMS_KEYBOARD, "in");
2312 std::lock_guard<std::recursive_mutex> lockListener(keyboardDidHideListenerMutex_);
2313 WMError ret = RegisterListener(keyboardDidHideListeners_[GetPersistentId()], listener);
2314 if (ret == WMError::WM_OK && isKeyboardDidHideRegistered_ == false) {
2315 auto hostSession = GetHostSession();
2316 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, WMError::WM_ERROR_INVALID_WINDOW);
2317 hostSession->NotifyKeyboardDidHideRegistered(true);
2318 isKeyboardDidHideRegistered_ = true;
2319 }
2320 return ret;
2321 }
2322
UnregisterKeyboardDidHideListener(const sptr<IKeyboardDidHideListener> & listener)2323 WMError WindowSessionImpl::UnregisterKeyboardDidHideListener(const sptr<IKeyboardDidHideListener>& listener)
2324 {
2325 TLOGD(WmsLogTag::WMS_KEYBOARD, "in");
2326 std::lock_guard<std::recursive_mutex> lockListener(keyboardDidHideListenerMutex_);
2327 WMError ret = UnregisterListener(keyboardDidHideListeners_[GetPersistentId()], listener);
2328 if (keyboardDidHideListeners_[GetPersistentId()].empty()) {
2329 auto hostSession = GetHostSession();
2330 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, WMError::WM_ERROR_INVALID_WINDOW);
2331 hostSession->NotifyKeyboardDidHideRegistered(false);
2332 isKeyboardDidHideRegistered_ = false;
2333 }
2334 return ret;
2335 }
2336
UnregisterLifeCycleListener(const sptr<IWindowLifeCycle> & listener)2337 WMError WindowSessionImpl::UnregisterLifeCycleListener(const sptr<IWindowLifeCycle>& listener)
2338 {
2339 WLOGFD("in");
2340 std::lock_guard<std::recursive_mutex> lockListener(lifeCycleListenerMutex_);
2341 return UnregisterListener(lifecycleListeners_[GetPersistentId()], listener);
2342 }
2343
RegisterWindowChangeListener(const sptr<IWindowChangeListener> & listener)2344 WMError WindowSessionImpl::RegisterWindowChangeListener(const sptr<IWindowChangeListener>& listener)
2345 {
2346 WLOGFD("in");
2347 std::lock_guard<std::recursive_mutex> lockListener(windowChangeListenerMutex_);
2348 return RegisterListener(windowChangeListeners_[GetPersistentId()], listener);
2349 }
2350
UnregisterWindowChangeListener(const sptr<IWindowChangeListener> & listener)2351 WMError WindowSessionImpl::UnregisterWindowChangeListener(const sptr<IWindowChangeListener>& listener)
2352 {
2353 WLOGFD("in");
2354 std::lock_guard<std::recursive_mutex> lockListener(windowChangeListenerMutex_);
2355 return UnregisterListener(windowChangeListeners_[GetPersistentId()], listener);
2356 }
2357
RegisterWindowCrossAxisListener(const sptr<IWindowCrossAxisListener> & listener)2358 WMError WindowSessionImpl::RegisterWindowCrossAxisListener(const sptr<IWindowCrossAxisListener>& listener)
2359 {
2360 WLOGFD("in");
2361 std::lock_guard<std::recursive_mutex> lockListener(windowChangeListenerMutex_);
2362 return RegisterListener(windowCrossAxisListeners_[GetPersistentId()], listener);
2363 }
2364
UnregisterWindowCrossAxisListener(const sptr<IWindowCrossAxisListener> & listener)2365 WMError WindowSessionImpl::UnregisterWindowCrossAxisListener(const sptr<IWindowCrossAxisListener>& listener)
2366 {
2367 WLOGFD("in");
2368 std::lock_guard<std::recursive_mutex> lockListener(windowChangeListenerMutex_);
2369 return UnregisterListener(windowCrossAxisListeners_[GetPersistentId()], listener);
2370 }
2371
RegisterWindowStatusChangeListener(const sptr<IWindowStatusChangeListener> & listener)2372 WMError WindowSessionImpl::RegisterWindowStatusChangeListener(const sptr<IWindowStatusChangeListener>& listener)
2373 {
2374 WLOGFD("in");
2375 std::lock_guard<std::recursive_mutex> lockListener(windowStatusChangeListenerMutex_);
2376 return RegisterListener(windowStatusChangeListeners_[GetPersistentId()], listener);
2377 }
2378
UnregisterWindowStatusChangeListener(const sptr<IWindowStatusChangeListener> & listener)2379 WMError WindowSessionImpl::UnregisterWindowStatusChangeListener(const sptr<IWindowStatusChangeListener>& listener)
2380 {
2381 WLOGFD("in");
2382 std::lock_guard<std::recursive_mutex> lockListener(windowStatusChangeListenerMutex_);
2383 return UnregisterListener(windowStatusChangeListeners_[GetPersistentId()], listener);
2384 }
2385
SetDecorVisible(bool isVisible)2386 WMError WindowSessionImpl::SetDecorVisible(bool isVisible)
2387 {
2388 if (IsWindowSessionInvalid()) {
2389 return WMError::WM_ERROR_INVALID_WINDOW;
2390 }
2391 std::shared_ptr<Ace::UIContent> uiContent = GetUIContentSharedPtr();
2392 if (uiContent == nullptr) {
2393 TLOGE(WmsLogTag::WMS_DECOR, "uicontent is null");
2394 return WMError::WM_ERROR_NULLPTR;
2395 }
2396 uiContent->SetContainerModalTitleVisible(isVisible, true);
2397 TLOGD(WmsLogTag::WMS_DECOR, "end");
2398 return WMError::WM_OK;
2399 }
2400
GetDecorVisible(bool & isVisible)2401 WMError WindowSessionImpl::GetDecorVisible(bool& isVisible)
2402 {
2403 TLOGD(WmsLogTag::WMS_DECOR, "%{public}u in", GetWindowId());
2404 if (IsWindowSessionInvalid()) {
2405 return WMError::WM_ERROR_INVALID_WINDOW;
2406 }
2407 if (!IsPcOrPadCapabilityEnabled()) {
2408 TLOGE(WmsLogTag::WMS_DECOR, "device not support");
2409 return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
2410 }
2411 std::shared_ptr<Ace::UIContent> uiContent = GetUIContentSharedPtr();
2412 if (uiContent == nullptr) {
2413 TLOGE(WmsLogTag::WMS_DECOR, "uicontent is null");
2414 return WMError::WM_ERROR_NULLPTR;
2415 }
2416 isVisible = uiContent->GetContainerModalTitleVisible(GetImmersiveModeEnabledState());
2417 return WMError::WM_OK;
2418 }
2419
SetWindowTitleMoveEnabled(bool enable)2420 WMError WindowSessionImpl::SetWindowTitleMoveEnabled(bool enable)
2421 {
2422 if (IsWindowSessionInvalid()) {
2423 return WMError::WM_ERROR_INVALID_WINDOW;
2424 }
2425 if (!IsPcOrPadFreeMultiWindowMode()) {
2426 TLOGE(WmsLogTag::WMS_DECOR, "The device is not supported");
2427 return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
2428 }
2429 if (!WindowHelper::IsMainWindow(GetType()) && !WindowHelper::IsSubWindow(GetType())) {
2430 TLOGE(WmsLogTag::WMS_DECOR, "called by invalid window type, type:%{public}d", GetType());
2431 return WMError::WM_ERROR_INVALID_CALLING;
2432 }
2433 std::shared_ptr<Ace::UIContent> uiContent = GetUIContentSharedPtr();
2434 if (uiContent == nullptr) {
2435 TLOGE(WmsLogTag::WMS_DECOR, "uicontent is null");
2436 return WMError::WM_ERROR_NULLPTR;
2437 }
2438 uiContent->EnableContainerModalGesture(enable);
2439 TLOGI(WmsLogTag::WMS_DECOR, "enable:%{public}d end", enable);
2440 return WMError::WM_OK;
2441 }
2442
SetSubWindowModal(bool isModal,ModalityType modalityType)2443 WMError WindowSessionImpl::SetSubWindowModal(bool isModal, ModalityType modalityType)
2444 {
2445 if (IsWindowSessionInvalid()) {
2446 return WMError::WM_ERROR_INVALID_WINDOW;
2447 }
2448 if (!WindowHelper::IsSubWindow(GetType())) {
2449 TLOGE(WmsLogTag::WMS_SUB, "called by invalid window type, type:%{public}d", GetType());
2450 return WMError::WM_ERROR_INVALID_CALLING;
2451 }
2452 if (modalityType == ModalityType::APPLICATION_MODALITY && !IsPcOrPadFreeMultiWindowMode()) {
2453 TLOGE(WmsLogTag::WMS_SUB, "device not support");
2454 return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
2455 }
2456
2457 WMError modalRet = isModal ?
2458 AddWindowFlag(WindowFlag::WINDOW_FLAG_IS_MODAL) :
2459 RemoveWindowFlag(WindowFlag::WINDOW_FLAG_IS_MODAL);
2460 if (modalRet != WMError::WM_OK) {
2461 return modalRet;
2462 }
2463 modalRet = isModal && modalityType == ModalityType::APPLICATION_MODALITY ?
2464 AddWindowFlag(WindowFlag::WINDOW_FLAG_IS_APPLICATION_MODAL) :
2465 RemoveWindowFlag(WindowFlag::WINDOW_FLAG_IS_APPLICATION_MODAL);
2466 auto hostSession = GetHostSession();
2467 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, WMError::WM_ERROR_INVALID_WINDOW);
2468 SubWindowModalType subWindowModalType = SubWindowModalType::TYPE_NORMAL;
2469 if (isModal) {
2470 subWindowModalType = modalityType == ModalityType::WINDOW_MODALITY ?
2471 SubWindowModalType::TYPE_WINDOW_MODALITY :
2472 SubWindowModalType::TYPE_APPLICATION_MODALITY;
2473 }
2474 if (!(property_->GetSubWindowZLevel() <= MAXIMUM_Z_LEVEL && !isModal)) {
2475 int32_t zLevel = GetSubWindowZLevelByFlags(GetType(), GetWindowFlags(), IsTopmost());
2476 property_->SetSubWindowZLevel(zLevel);
2477 UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_SUB_WINDOW_Z_LEVEL);
2478 }
2479 hostSession->NotifySubModalTypeChange(subWindowModalType);
2480 return modalRet;
2481 }
2482
SetWindowModal(bool isModal)2483 WMError WindowSessionImpl::SetWindowModal(bool isModal)
2484 {
2485 if (IsWindowSessionInvalid()) {
2486 return WMError::WM_ERROR_INVALID_WINDOW;
2487 }
2488 if (!IsPcOrPadFreeMultiWindowMode()) {
2489 TLOGE(WmsLogTag::WMS_MAIN, "device not support");
2490 return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
2491 }
2492 if (!WindowHelper::IsMainWindow(GetType())) {
2493 TLOGE(WmsLogTag::WMS_MAIN, "called by invalid window type, type:%{public}d", GetType());
2494 return WMError::WM_ERROR_INVALID_CALLING;
2495 }
2496 WMError modalRet = isModal ?
2497 AddWindowFlag(WindowFlag::WINDOW_FLAG_IS_MODAL) :
2498 RemoveWindowFlag(WindowFlag::WINDOW_FLAG_IS_MODAL);
2499 if (modalRet != WMError::WM_OK) {
2500 return modalRet;
2501 }
2502 auto hostSession = GetHostSession();
2503 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, WMError::WM_ERROR_SYSTEM_ABNORMALLY);
2504 hostSession->NotifyMainModalTypeChange(isModal);
2505 return modalRet;
2506 }
2507
SetDecorHeight(int32_t decorHeight)2508 WMError WindowSessionImpl::SetDecorHeight(int32_t decorHeight)
2509 {
2510 if (IsWindowSessionInvalid()) {
2511 return WMError::WM_ERROR_INVALID_WINDOW;
2512 }
2513 float vpr = 0.f;
2514 auto err = GetVirtualPixelRatio(vpr);
2515 if (err != WMError::WM_OK) {
2516 return err;
2517 }
2518 int32_t decorHeightWithPx = static_cast<int32_t>(decorHeight * vpr);
2519 std::shared_ptr<Ace::UIContent> uiContent = GetUIContentSharedPtr();
2520 if (uiContent == nullptr) {
2521 TLOGE(WmsLogTag::WMS_DECOR, "uicontent is empty");
2522 return WMError::WM_ERROR_NULLPTR;
2523 }
2524 uiContent->SetContainerModalTitleHeight(decorHeightWithPx);
2525
2526 if (auto hostSession = GetHostSession()) {
2527 hostSession->SetCustomDecorHeight(decorHeight);
2528 }
2529 decorHeight_ = decorHeight;
2530 TLOGD(WmsLogTag::WMS_DECOR, "end, height: %{public}d", decorHeight);
2531 return WMError::WM_OK;
2532 }
2533
GetDecorHeight(int32_t & height)2534 WMError WindowSessionImpl::GetDecorHeight(int32_t& height)
2535 {
2536 if (IsWindowSessionInvalid()) {
2537 return WMError::WM_ERROR_INVALID_WINDOW;
2538 }
2539 std::shared_ptr<Ace::UIContent> uiContent = GetUIContentSharedPtr();
2540 if (uiContent == nullptr) {
2541 TLOGE(WmsLogTag::WMS_DECOR, "uiContent is null, windowId: %{public}u", GetWindowId());
2542 return WMError::WM_ERROR_NULLPTR;
2543 }
2544 height = uiContent->GetContainerModalTitleHeight();
2545 if (height == -1) {
2546 height = 0;
2547 TLOGD(WmsLogTag::WMS_DECOR, "Get app window decor height failed");
2548 return WMError::WM_OK;
2549 }
2550 float vpr = 0.f;
2551 auto err = GetVirtualPixelRatio(vpr);
2552 if (err != WMError::WM_OK) {
2553 return err;
2554 }
2555 height = static_cast<int32_t>(height / vpr);
2556 TLOGD(WmsLogTag::WMS_DECOR, "end, height: %{public}d", height);
2557 return WMError::WM_OK;
2558 }
2559
SetDecorButtonStyle(const DecorButtonStyle & decorButtonStyle)2560 WMError WindowSessionImpl::SetDecorButtonStyle(const DecorButtonStyle& decorButtonStyle)
2561 {
2562 if (IsWindowSessionInvalid()) {
2563 return WMError::WM_ERROR_INVALID_WINDOW;
2564 }
2565
2566 if (!WindowHelper::CheckButtonStyleValid(decorButtonStyle)) {
2567 TLOGE(WmsLogTag::WMS_DECOR, "set decor button style param invalid");
2568 return WMError::WM_ERROR_INVALID_PARAM;
2569 }
2570
2571 if (!WindowHelper::IsMainWindow(GetType()) && !WindowHelper::IsSubWindow(GetType())) {
2572 TLOGE(WmsLogTag::WMS_DECOR, "called by invalid window type, type:%{public}d", GetType());
2573 return WMError::WM_ERROR_INVALID_CALLING;
2574 }
2575
2576 std::shared_ptr<Ace::UIContent> uiContent = GetUIContentSharedPtr();
2577 if (uiContent == nullptr) {
2578 TLOGE(WmsLogTag::WMS_DECOR, "uiContent is null, windowId: %{public}u", GetWindowId());
2579 return WMError::WM_ERROR_NULLPTR;
2580 }
2581 uiContent->SetContainerButtonStyle(decorButtonStyle);
2582 decorButtonStyle_ = decorButtonStyle;
2583 return WMError::WM_OK;
2584 }
2585
GetDecorButtonStyle(DecorButtonStyle & decorButtonStyle)2586 WMError WindowSessionImpl::GetDecorButtonStyle(DecorButtonStyle& decorButtonStyle)
2587 {
2588 if (IsWindowSessionInvalid()) {
2589 return WMError::WM_ERROR_INVALID_WINDOW;
2590 }
2591
2592 if (!WindowHelper::IsMainWindow(GetType()) && !WindowHelper::IsSubWindow(GetType())) {
2593 TLOGE(WmsLogTag::WMS_DECOR, "called by invalid window type, type:%{public}d", GetType());
2594 return WMError::WM_ERROR_INVALID_CALLING;
2595 }
2596 decorButtonStyle = decorButtonStyle_;
2597 return WMError::WM_OK;
2598 }
2599
GetTitleButtonArea(TitleButtonRect & titleButtonRect)2600 WMError WindowSessionImpl::GetTitleButtonArea(TitleButtonRect& titleButtonRect)
2601 {
2602 if (IsWindowSessionInvalid()) {
2603 return WMError::WM_ERROR_INVALID_WINDOW;
2604 }
2605 Rect decorRect;
2606 Rect titleButtonLeftRect;
2607 bool res = false;
2608 std::shared_ptr<Ace::UIContent> uiContent = GetUIContentSharedPtr();
2609 if (uiContent == nullptr) {
2610 TLOGE(WmsLogTag::WMS_DECOR, "uicontent is empty");
2611 return WMError::WM_ERROR_NULLPTR;
2612 }
2613 res = uiContent->GetContainerModalButtonsRect(decorRect, titleButtonLeftRect);
2614 if (!res) {
2615 TLOGE(WmsLogTag::WMS_DECOR, "GetContainerModalButtonsRect failed");
2616 titleButtonRect.IsUninitializedRect();
2617 return WMError::WM_OK;
2618 }
2619 float vpr = 0.f;
2620 auto err = GetVirtualPixelRatio(vpr);
2621 if (err != WMError::WM_OK) {
2622 return err;
2623 }
2624 titleButtonRect.posX_ = static_cast<int32_t>(decorRect.width_) -
2625 static_cast<int32_t>(titleButtonLeftRect.width_) - titleButtonLeftRect.posX_;
2626 titleButtonRect.posX_ = static_cast<int32_t>(titleButtonRect.posX_ / vpr);
2627 titleButtonRect.posY_ = static_cast<int32_t>(titleButtonLeftRect.posY_ / vpr);
2628 titleButtonRect.width_ = static_cast<uint32_t>(titleButtonLeftRect.width_ / vpr);
2629 titleButtonRect.height_ = static_cast<uint32_t>(titleButtonLeftRect.height_ / vpr);
2630 return WMError::WM_OK;
2631 }
2632
GetUIContentRemoteObj(sptr<IRemoteObject> & uiContentRemoteObj)2633 WSError WindowSessionImpl::GetUIContentRemoteObj(sptr<IRemoteObject>& uiContentRemoteObj)
2634 {
2635 std::shared_ptr<Ace::UIContent> uiContent = GetUIContentSharedPtr();
2636 if (uiContent == nullptr) {
2637 TLOGE(WmsLogTag::DEFAULT, "uiContent is nullptr");
2638 return WSError::WS_ERROR_NULLPTR;
2639 }
2640 uiContentRemoteObj = uiContent->GetRemoteObj();
2641 return WSError::WS_OK;
2642 }
2643
RegisterWindowTitleButtonRectChangeListener(const sptr<IWindowTitleButtonRectChangedListener> & listener)2644 WMError WindowSessionImpl::RegisterWindowTitleButtonRectChangeListener(
2645 const sptr<IWindowTitleButtonRectChangedListener>& listener)
2646 {
2647 if (IsWindowSessionInvalid()) {
2648 return WMError::WM_ERROR_INVALID_WINDOW;
2649 }
2650 auto persistentId = GetPersistentId();
2651 TLOGD(WmsLogTag::WMS_DECOR, "Start, id:%{public}d", persistentId);
2652 if (listener == nullptr) {
2653 TLOGE(WmsLogTag::WMS_DECOR, "listener is null");
2654 return WMError::WM_ERROR_NULLPTR;
2655 }
2656
2657 {
2658 std::lock_guard<std::recursive_mutex> lockListener(windowTitleButtonRectChangeListenerMutex_);
2659 WMError ret = RegisterListener(windowTitleButtonRectChangeListeners_[persistentId], listener);
2660 if (ret != WMError::WM_OK) {
2661 TLOGE(WmsLogTag::WMS_DECOR, "register failed");
2662 return ret;
2663 }
2664 }
2665 if (auto uiContent = GetUIContentSharedPtr()) {
2666 const char* const where = __func__;
2667 uiContent->SubscribeContainerModalButtonsRectChange(
2668 [where, weakThis = wptr(this)](Rect& decorRect, Rect& titleButtonLeftRect) {
2669 auto window = weakThis.promote();
2670 if (!window) {
2671 TLOGNE(WmsLogTag::WMS_DECOR, "%{public}s window is null", where);
2672 return;
2673 }
2674 float vpr = 0.f;
2675 auto err = window->GetVirtualPixelRatio(vpr);
2676 if (err != WMError::WM_OK) {
2677 return;
2678 }
2679 TitleButtonRect titleButtonRect;
2680 titleButtonRect.posX_ = static_cast<int32_t>(decorRect.width_) -
2681 static_cast<int32_t>(titleButtonLeftRect.width_) - titleButtonLeftRect.posX_;
2682 titleButtonRect.posX_ = static_cast<int32_t>(titleButtonRect.posX_ / vpr);
2683 titleButtonRect.posY_ = static_cast<int32_t>(titleButtonLeftRect.posY_ / vpr);
2684 titleButtonRect.width_ = static_cast<uint32_t>(titleButtonLeftRect.width_ / vpr);
2685 titleButtonRect.height_ = static_cast<uint32_t>(titleButtonLeftRect.height_ / vpr);
2686 window->NotifyWindowTitleButtonRectChange(titleButtonRect);
2687 });
2688 }
2689 return WMError::WM_OK;
2690 }
2691
UnregisterWindowTitleButtonRectChangeListener(const sptr<IWindowTitleButtonRectChangedListener> & listener)2692 WMError WindowSessionImpl::UnregisterWindowTitleButtonRectChangeListener(
2693 const sptr<IWindowTitleButtonRectChangedListener>& listener)
2694 {
2695 if (IsWindowSessionInvalid()) {
2696 return WMError::WM_ERROR_INVALID_WINDOW;
2697 }
2698 WMError ret = WMError::WM_OK;
2699 auto persistentId = GetPersistentId();
2700 TLOGD(WmsLogTag::WMS_DECOR, "Start, id:%{public}d", persistentId);
2701 if (listener == nullptr) {
2702 TLOGE(WmsLogTag::WMS_DECOR, "listener is nullptr");
2703 return WMError::WM_ERROR_NULLPTR;
2704 }
2705 {
2706 std::lock_guard<std::recursive_mutex> lockListener(windowTitleButtonRectChangeListenerMutex_);
2707 ret = UnregisterListener(windowTitleButtonRectChangeListeners_[persistentId], listener);
2708 if (ret != WMError::WM_OK) {
2709 TLOGE(WmsLogTag::WMS_DECOR, "failed");
2710 return ret;
2711 }
2712 }
2713 if (auto uiContent = GetUIContentSharedPtr()) {
2714 uiContent->SubscribeContainerModalButtonsRectChange(nullptr);
2715 }
2716 return ret;
2717 }
2718
2719 template<typename T>
2720 EnableIfSame<T, IWindowTitleButtonRectChangedListener,
GetListeners()2721 std::vector<sptr<IWindowTitleButtonRectChangedListener>>> WindowSessionImpl::GetListeners()
2722 {
2723 std::vector<sptr<IWindowTitleButtonRectChangedListener>> windowTitleButtonRectListeners;
2724 for (auto& listener : windowTitleButtonRectChangeListeners_[GetPersistentId()]) {
2725 windowTitleButtonRectListeners.push_back(listener);
2726 }
2727 return windowTitleButtonRectListeners;
2728 }
2729
NotifyWindowTitleButtonRectChange(TitleButtonRect titleButtonRect)2730 void WindowSessionImpl::NotifyWindowTitleButtonRectChange(TitleButtonRect titleButtonRect)
2731 {
2732 std::lock_guard<std::recursive_mutex> lockListener(windowTitleButtonRectChangeListenerMutex_);
2733 auto windowTitleButtonRectListeners = GetListeners<IWindowTitleButtonRectChangedListener>();
2734 for (auto& listener : windowTitleButtonRectListeners) {
2735 if (listener != nullptr) {
2736 listener->OnWindowTitleButtonRectChanged(titleButtonRect);
2737 }
2738 }
2739 }
2740
2741 template<typename T>
2742 EnableIfSame<T, IWindowRectChangeListener,
GetListeners()2743 std::vector<sptr<IWindowRectChangeListener>>> WindowSessionImpl::GetListeners()
2744 {
2745 std::vector<sptr<IWindowRectChangeListener>> windowRectChangeListeners;
2746 for (auto& listener : windowRectChangeListeners_[GetPersistentId()]) {
2747 windowRectChangeListeners.push_back(listener);
2748 }
2749 return windowRectChangeListeners;
2750 }
2751
RegisterWindowRectChangeListener(const sptr<IWindowRectChangeListener> & listener)2752 WMError WindowSessionImpl::RegisterWindowRectChangeListener(const sptr<IWindowRectChangeListener>& listener)
2753 {
2754 WMError ret = WMError::WM_OK;
2755 {
2756 std::lock_guard<std::mutex> lockListener(windowRectChangeListenerMutex_);
2757 ret = RegisterListener(windowRectChangeListeners_[GetPersistentId()], listener);
2758 }
2759 auto hostSession = GetHostSession();
2760 if (hostSession != nullptr && ret == WMError::WM_OK) {
2761 hostSession->UpdateRectChangeListenerRegistered(true);
2762 }
2763 return ret;
2764 }
2765
UnregisterWindowRectChangeListener(const sptr<IWindowRectChangeListener> & listener)2766 WMError WindowSessionImpl::UnregisterWindowRectChangeListener(const sptr<IWindowRectChangeListener>& listener)
2767 {
2768 WMError ret = WMError::WM_OK;
2769 bool windowRectChangeListenersEmpty = false;
2770 {
2771 std::lock_guard<std::mutex> lockListener(windowRectChangeListenerMutex_);
2772 ret = UnregisterListener(windowRectChangeListeners_[GetPersistentId()], listener);
2773 windowRectChangeListenersEmpty = windowRectChangeListeners_.count(GetPersistentId()) == 0 ||
2774 windowRectChangeListeners_[GetPersistentId()].empty();
2775 }
2776 auto hostSession = GetHostSession();
2777 if (hostSession != nullptr && windowRectChangeListenersEmpty) {
2778 hostSession->UpdateRectChangeListenerRegistered(false);
2779 }
2780 return ret;
2781 }
2782
2783 template<typename T>
GetListeners()2784 EnableIfSame<T, ISubWindowCloseListener, sptr<ISubWindowCloseListener>> WindowSessionImpl::GetListeners()
2785 {
2786 sptr<ISubWindowCloseListener> subWindowCloseListeners;
2787 subWindowCloseListeners = subWindowCloseListeners_[GetPersistentId()];
2788 return subWindowCloseListeners;
2789 }
2790
RegisterSubWindowCloseListeners(const sptr<ISubWindowCloseListener> & listener)2791 WMError WindowSessionImpl::RegisterSubWindowCloseListeners(const sptr<ISubWindowCloseListener>& listener)
2792 {
2793 if (listener == nullptr) {
2794 WLOGFE("listener is null");
2795 return WMError::WM_ERROR_NULLPTR;
2796 }
2797 if (!WindowHelper::IsSubWindow(GetType()) && !WindowHelper::IsSystemSubWindow(GetType())) {
2798 WLOGFE("window type is not supported");
2799 return WMError::WM_ERROR_INVALID_CALLING;
2800 }
2801 std::lock_guard<std::mutex> lockListener(subWindowCloseListenersMutex_);
2802 subWindowCloseListeners_[GetPersistentId()] = listener;
2803 return WMError::WM_OK;
2804 }
2805
UnregisterSubWindowCloseListeners(const sptr<ISubWindowCloseListener> & listener)2806 WMError WindowSessionImpl::UnregisterSubWindowCloseListeners(const sptr<ISubWindowCloseListener>& listener)
2807 {
2808 if (listener == nullptr) {
2809 WLOGFE("listener could not be null");
2810 return WMError::WM_ERROR_NULLPTR;
2811 }
2812 if (!WindowHelper::IsSubWindow(GetType()) && !WindowHelper::IsSystemSubWindow(GetType())) {
2813 WLOGFE("window type is not supported");
2814 return WMError::WM_ERROR_INVALID_CALLING;
2815 }
2816 std::lock_guard<std::mutex> lockListener(subWindowCloseListenersMutex_);
2817 subWindowCloseListeners_[GetPersistentId()] = nullptr;
2818 return WMError::WM_OK;
2819 }
2820
2821 template<typename T>
2822 EnableIfSame<T, IWindowHighlightChangeListener, std::vector<sptr<IWindowHighlightChangeListener>>>
GetListeners()2823 WindowSessionImpl::GetListeners()
2824 {
2825 std::vector<sptr<IWindowHighlightChangeListener>> highlightChangeListeners;
2826 for (auto& listener : highlightChangeListeners_[GetPersistentId()]) {
2827 highlightChangeListeners.push_back(listener);
2828 }
2829 return highlightChangeListeners;
2830 }
2831
RegisterWindowHighlightChangeListeners(const sptr<IWindowHighlightChangeListener> & listener)2832 WMError WindowSessionImpl::RegisterWindowHighlightChangeListeners(const sptr<IWindowHighlightChangeListener>& listener)
2833 {
2834 TLOGD(WmsLogTag::WMS_FOCUS, "name=%{public}s, id=%{public}u", GetWindowName().c_str(), GetPersistentId());
2835 std::lock_guard<std::mutex> lockListener(highlightChangeListenerMutex_);
2836 return RegisterListener(highlightChangeListeners_[GetPersistentId()], listener);
2837 }
2838
UnregisterWindowHighlightChangeListeners(const sptr<IWindowHighlightChangeListener> & listener)2839 WMError WindowSessionImpl::UnregisterWindowHighlightChangeListeners(
2840 const sptr<IWindowHighlightChangeListener>& listener)
2841 {
2842 TLOGD(WmsLogTag::WMS_FOCUS, "name=%{public}s, id=%{public}u", GetWindowName().c_str(), GetPersistentId());
2843 std::lock_guard<std::mutex> lockListener(highlightChangeListenerMutex_);
2844 return UnregisterListener(highlightChangeListeners_[GetPersistentId()], listener);
2845 }
2846
2847 template<typename T>
GetListeners()2848 EnableIfSame<T, IMainWindowCloseListener, sptr<IMainWindowCloseListener>> WindowSessionImpl::GetListeners()
2849 {
2850 return mainWindowCloseListeners_[GetPersistentId()];
2851 }
2852
RegisterMainWindowCloseListeners(const sptr<IMainWindowCloseListener> & listener)2853 WMError WindowSessionImpl::RegisterMainWindowCloseListeners(const sptr<IMainWindowCloseListener>& listener)
2854 {
2855 if (IsWindowSessionInvalid()) {
2856 return WMError::WM_ERROR_INVALID_WINDOW;
2857 }
2858 if (listener == nullptr) {
2859 TLOGE(WmsLogTag::WMS_PC, "listener is null");
2860 return WMError::WM_ERROR_NULLPTR;
2861 }
2862 if (!WindowHelper::IsMainWindow(GetType())) {
2863 TLOGE(WmsLogTag::WMS_PC, "window type is not supported");
2864 return WMError::WM_ERROR_INVALID_CALLING;
2865 }
2866 if (!IsPcOrPadCapabilityEnabled()) {
2867 TLOGE(WmsLogTag::WMS_PC, "The device is not supported");
2868 return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
2869 }
2870 std::lock_guard<std::mutex> lockListener(mainWindowCloseListenersMutex_);
2871 mainWindowCloseListeners_[GetPersistentId()] = listener;
2872 return WMError::WM_OK;
2873 }
2874
UnregisterMainWindowCloseListeners(const sptr<IMainWindowCloseListener> & listener)2875 WMError WindowSessionImpl::UnregisterMainWindowCloseListeners(const sptr<IMainWindowCloseListener>& listener)
2876 {
2877 if (IsWindowSessionInvalid()) {
2878 return WMError::WM_ERROR_INVALID_WINDOW;
2879 }
2880 if (listener == nullptr) {
2881 TLOGE(WmsLogTag::WMS_PC, "listener could not be null");
2882 return WMError::WM_ERROR_NULLPTR;
2883 }
2884 if (!IsPcOrPadCapabilityEnabled()) {
2885 TLOGE(WmsLogTag::WMS_PC, "The device is not supported");
2886 return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
2887 }
2888 if (!WindowHelper::IsMainWindow(GetType())) {
2889 TLOGE(WmsLogTag::WMS_PC, "window type is not supported");
2890 return WMError::WM_ERROR_INVALID_CALLING;
2891 }
2892 std::lock_guard<std::mutex> lockListener(mainWindowCloseListenersMutex_);
2893 mainWindowCloseListeners_[GetPersistentId()] = nullptr;
2894 return WMError::WM_OK;
2895 }
2896
2897 template<typename T>
GetListeners()2898 EnableIfSame<T, IWindowWillCloseListener, std::vector<sptr<IWindowWillCloseListener>>> WindowSessionImpl::GetListeners()
2899 {
2900 return windowWillCloseListeners_[GetPersistentId()];
2901 }
2902
RegisterWindowWillCloseListeners(const sptr<IWindowWillCloseListener> & listener)2903 WMError WindowSessionImpl::RegisterWindowWillCloseListeners(const sptr<IWindowWillCloseListener>& listener)
2904 {
2905 if (IsWindowSessionInvalid()) {
2906 return WMError::WM_ERROR_INVALID_WINDOW;
2907 }
2908 if (listener == nullptr) {
2909 TLOGE(WmsLogTag::WMS_DECOR, "listener is null");
2910 return WMError::WM_ERROR_NULLPTR;
2911 }
2912 if (!IsPcOrPadCapabilityEnabled()) {
2913 TLOGE(WmsLogTag::WMS_DECOR, "The device is not supported");
2914 return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
2915 }
2916 if (!WindowHelper::IsAppWindow(GetType())) {
2917 TLOGE(WmsLogTag::WMS_DECOR, "window type is not supported");
2918 return WMError::WM_ERROR_INVALID_CALLING;
2919 }
2920 std::lock_guard<std::mutex> lockListener(windowWillCloseListenersMutex_);
2921 return RegisterListener(windowWillCloseListeners_[GetPersistentId()], listener);
2922 }
2923
UnRegisterWindowWillCloseListeners(const sptr<IWindowWillCloseListener> & listener)2924 WMError WindowSessionImpl::UnRegisterWindowWillCloseListeners(const sptr<IWindowWillCloseListener>& listener)
2925 {
2926 if (IsWindowSessionInvalid()) {
2927 return WMError::WM_ERROR_INVALID_WINDOW;
2928 }
2929 if (listener == nullptr) {
2930 TLOGE(WmsLogTag::WMS_DECOR, "listener could not be null");
2931 return WMError::WM_ERROR_NULLPTR;
2932 }
2933 if (!IsPcOrPadCapabilityEnabled()) {
2934 TLOGE(WmsLogTag::WMS_DECOR, "The device is not supported");
2935 return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
2936 }
2937 if (!WindowHelper::IsAppWindow(GetType())) {
2938 TLOGE(WmsLogTag::WMS_DECOR, "window type is not supported");
2939 return WMError::WM_ERROR_INVALID_CALLING;
2940 }
2941 std::lock_guard<std::mutex> lockListener(windowWillCloseListenersMutex_);
2942 return UnregisterListener(windowWillCloseListeners_[GetPersistentId()], listener);
2943 }
2944
2945 template<typename T>
2946 EnableIfSame<T, ISwitchFreeMultiWindowListener,
GetListeners()2947 std::vector<sptr<ISwitchFreeMultiWindowListener>>> WindowSessionImpl::GetListeners()
2948 {
2949 std::vector<sptr<ISwitchFreeMultiWindowListener>> switchFreeMultiWindowListeners;
2950 for (auto& listener : switchFreeMultiWindowListeners_[GetPersistentId()]) {
2951 switchFreeMultiWindowListeners.push_back(listener);
2952 }
2953 return switchFreeMultiWindowListeners;
2954 }
2955
RegisterSwitchFreeMultiWindowListener(const sptr<ISwitchFreeMultiWindowListener> & listener)2956 WMError WindowSessionImpl::RegisterSwitchFreeMultiWindowListener(const sptr<ISwitchFreeMultiWindowListener>& listener)
2957 {
2958 if (listener == nullptr) {
2959 TLOGE(WmsLogTag::WMS_LAYOUT_PC, "listener is nullptr");
2960 return WMError::WM_ERROR_NULLPTR;
2961 }
2962 if (!WindowHelper::IsMainWindow(GetType())) {
2963 TLOGE(WmsLogTag::WMS_LAYOUT_PC, "window type is not supported");
2964 return WMError::WM_ERROR_INVALID_CALLING;
2965 }
2966 TLOGD(WmsLogTag::WMS_LAYOUT_PC, "Start register");
2967 std::lock_guard<std::mutex> lockListener(switchFreeMultiWindowListenerMutex_);
2968 return RegisterListener(switchFreeMultiWindowListeners_[GetPersistentId()], listener);
2969 }
2970
UnregisterSwitchFreeMultiWindowListener(const sptr<ISwitchFreeMultiWindowListener> & listener)2971 WMError WindowSessionImpl::UnregisterSwitchFreeMultiWindowListener(const sptr<ISwitchFreeMultiWindowListener>& listener)
2972 {
2973 if (listener == nullptr) {
2974 TLOGE(WmsLogTag::WMS_LAYOUT_PC, "listener could not be null");
2975 return WMError::WM_ERROR_NULLPTR;
2976 }
2977 if (!WindowHelper::IsMainWindow(GetType())) {
2978 TLOGE(WmsLogTag::WMS_LAYOUT_PC, "window type is not supported");
2979 return WMError::WM_ERROR_INVALID_CALLING;
2980 }
2981 TLOGD(WmsLogTag::WMS_LAYOUT_PC, "Start unregister");
2982 std::lock_guard<std::mutex> lockListener(switchFreeMultiWindowListenerMutex_);
2983 return UnregisterListener(switchFreeMultiWindowListeners_[GetPersistentId()], listener);
2984 }
2985
RecoverSessionListener()2986 void WindowSessionImpl::RecoverSessionListener()
2987 {
2988 auto persistentId = GetPersistentId();
2989 TLOGI(WmsLogTag::WMS_RECOVER, "with persistentId=%{public}d", persistentId);
2990 {
2991 std::lock_guard<std::recursive_mutex> lockListener(avoidAreaChangeListenerMutex_);
2992 if (avoidAreaChangeListeners_.find(persistentId) != avoidAreaChangeListeners_.end() &&
2993 !avoidAreaChangeListeners_[persistentId].empty()) {
2994 SingletonContainer::Get<WindowAdapter>().UpdateSessionAvoidAreaListener(persistentId, true);
2995 }
2996 }
2997 {
2998 std::lock_guard<std::recursive_mutex> lockListener(touchOutsideListenerMutex_);
2999 if (touchOutsideListeners_.find(persistentId) != touchOutsideListeners_.end() &&
3000 !touchOutsideListeners_[persistentId].empty()) {
3001 SingletonContainer::Get<WindowAdapter>().UpdateSessionTouchOutsideListener(persistentId, true);
3002 }
3003 }
3004 {
3005 std::lock_guard<std::recursive_mutex> lockListener(windowVisibilityChangeListenerMutex_);
3006 if (windowVisibilityChangeListeners_.find(persistentId) != windowVisibilityChangeListeners_.end() &&
3007 !windowVisibilityChangeListeners_[persistentId].empty()) {
3008 SingletonContainer::Get<WindowAdapter>().UpdateSessionWindowVisibilityListener(persistentId, true);
3009 }
3010 }
3011 {
3012 std::lock_guard<std::mutex> lockListener(windowRectChangeListenerMutex_);
3013 if (windowRectChangeListeners_.find(persistentId) != windowRectChangeListeners_.end() &&
3014 !windowRectChangeListeners_[persistentId].empty()) {
3015 if (auto hostSession = GetHostSession()) {
3016 hostSession->UpdateRectChangeListenerRegistered(true);
3017 }
3018 }
3019 }
3020 }
3021
3022 template<typename T>
GetListeners()3023 EnableIfSame<T, IWindowLifeCycle, std::vector<sptr<IWindowLifeCycle>>> WindowSessionImpl::GetListeners()
3024 {
3025 std::vector<sptr<IWindowLifeCycle>> lifecycleListeners;
3026 for (auto& listener : lifecycleListeners_[GetPersistentId()]) {
3027 lifecycleListeners.push_back(listener);
3028 }
3029 return lifecycleListeners;
3030 }
3031
3032 template<typename T>
GetListeners()3033 EnableIfSame<T, IWindowChangeListener, std::vector<sptr<IWindowChangeListener>>> WindowSessionImpl::GetListeners()
3034 {
3035 std::vector<sptr<IWindowChangeListener>> windowChangeListeners;
3036 for (auto& listener : windowChangeListeners_[GetPersistentId()]) {
3037 windowChangeListeners.push_back(listener);
3038 }
3039 return windowChangeListeners;
3040 }
3041
3042 template<typename T>
GetListeners()3043 EnableIfSame<T, IWindowCrossAxisListener, std::vector<sptr<IWindowCrossAxisListener>>> WindowSessionImpl::GetListeners()
3044 {
3045 std::vector<sptr<IWindowCrossAxisListener>> windowCrossAxisListeners;
3046 for (const auto& listener : windowCrossAxisListeners_[GetPersistentId()]) {
3047 windowCrossAxisListeners.push_back(listener);
3048 }
3049 return windowCrossAxisListeners;
3050 }
3051
NotifyWindowCrossAxisChange(CrossAxisState state)3052 void WindowSessionImpl::NotifyWindowCrossAxisChange(CrossAxisState state)
3053 {
3054 TLOGI(WmsLogTag::WMS_LAYOUT, "id: %{public}d, cross axis state %{public}d", GetPersistentId(),
3055 static_cast<uint32_t>(state));
3056 crossAxisState_ = state;
3057 AAFwk::Want want;
3058 want.SetParam(Extension::CROSS_AXIS_FIELD, static_cast<int32_t>(state));
3059 if (auto uiContent = GetUIContentSharedPtr()) {
3060 uiContent->SendUIExtProprty(static_cast<uint32_t>(Extension::Businesscode::SYNC_CROSS_AXIS_STATE),
3061 want, static_cast<uint8_t>(SubSystemId::WM_UIEXT));
3062 }
3063 std::lock_guard<std::recursive_mutex> lockListener(windowTitleButtonRectChangeListenerMutex_);
3064 auto windowCrossAxisListeners = GetListeners<IWindowCrossAxisListener>();
3065 for (const auto& listener : windowCrossAxisListeners) {
3066 if (listener != nullptr) {
3067 listener->OnCrossAxisChange(state);
3068 }
3069 }
3070 }
3071
3072 template<typename T>
3073 EnableIfSame<T, IOccupiedAreaChangeListener,
GetListeners()3074 std::vector<sptr<IOccupiedAreaChangeListener>>> WindowSessionImpl::GetListeners()
3075 {
3076 std::vector<sptr<IOccupiedAreaChangeListener>> occupiedAreaChangeListeners;
3077 for (auto& listener : occupiedAreaChangeListeners_[GetPersistentId()]) {
3078 occupiedAreaChangeListeners.push_back(listener);
3079 }
3080 return occupiedAreaChangeListeners;
3081 }
3082
3083 template<typename T>
GetListeners()3084 EnableIfSame<T, IKeyboardDidShowListener, std::vector<sptr<IKeyboardDidShowListener>>> WindowSessionImpl::GetListeners()
3085 {
3086 std::vector<sptr<IKeyboardDidShowListener>> keyboardDidShowListeners;
3087 for (auto& listener : keyboardDidShowListeners_[GetPersistentId()]) {
3088 keyboardDidShowListeners.push_back(listener);
3089 }
3090 return keyboardDidShowListeners;
3091 }
3092
3093 template<typename T>
GetListeners()3094 EnableIfSame<T, IKeyboardDidHideListener, std::vector<sptr<IKeyboardDidHideListener>>> WindowSessionImpl::GetListeners()
3095 {
3096 std::vector<sptr<IKeyboardDidHideListener>> keyboardDidHideListeners;
3097 for (auto& listener : keyboardDidHideListeners_[GetPersistentId()]) {
3098 keyboardDidHideListeners.push_back(listener);
3099 }
3100 return keyboardDidHideListeners;
3101 }
3102
3103 template<typename T>
RegisterListener(std::vector<sptr<T>> & holder,const sptr<T> & listener)3104 WMError WindowSessionImpl::RegisterListener(std::vector<sptr<T>>& holder, const sptr<T>& listener)
3105 {
3106 if (listener == nullptr) {
3107 WLOGFE("listener is null");
3108 return WMError::WM_ERROR_NULLPTR;
3109 }
3110 if (std::find(holder.begin(), holder.end(), listener) != holder.end()) {
3111 WLOGFE("already registered");
3112 return WMError::WM_OK;
3113 }
3114 holder.emplace_back(listener);
3115 return WMError::WM_OK;
3116 }
3117
3118 template<typename T>
UnregisterListener(std::vector<sptr<T>> & holder,const sptr<T> & listener)3119 WMError WindowSessionImpl::UnregisterListener(std::vector<sptr<T>>& holder, const sptr<T>& listener)
3120 {
3121 if (listener == nullptr) {
3122 WLOGFE("listener could not be null");
3123 return WMError::WM_ERROR_NULLPTR;
3124 }
3125 holder.erase(std::remove_if(holder.begin(), holder.end(),
3126 [listener](sptr<T> registeredListener) {
3127 return registeredListener == listener;
3128 }), holder.end());
3129 return WMError::WM_OK;
3130 }
3131
3132 template<typename T>
ClearUselessListeners(std::map<int32_t,T> & listeners,int32_t persistentId)3133 void WindowSessionImpl::ClearUselessListeners(std::map<int32_t, T>& listeners, int32_t persistentId)
3134 {
3135 listeners.erase(persistentId);
3136 }
3137
3138 template<typename T>
ClearUselessListeners(std::unordered_map<int32_t,T> & listeners,int32_t persistentId)3139 void WindowSessionImpl::ClearUselessListeners(std::unordered_map<int32_t, T>& listeners, int32_t persistentId)
3140 {
3141 listeners.erase(persistentId);
3142 }
3143
3144 template<typename T>
GetListeners()3145 EnableIfSame<T, IWindowStatusChangeListener, std::vector<sptr<IWindowStatusChangeListener>>> WindowSessionImpl::GetListeners()
3146 {
3147 std::vector<sptr<IWindowStatusChangeListener>> windowStatusChangeListeners;
3148 for (auto& listener : windowStatusChangeListeners_[GetPersistentId()]) {
3149 windowStatusChangeListeners.push_back(listener);
3150 }
3151 return windowStatusChangeListeners;
3152 }
3153
ClearListenersById(int32_t persistentId)3154 void WindowSessionImpl::ClearListenersById(int32_t persistentId)
3155 {
3156 TLOGI(WmsLogTag::WMS_LIFE, "Called id: %{public}d.", GetPersistentId());
3157 {
3158 std::lock_guard<std::mutex> lockListener(displayMoveListenerMutex_);
3159 ClearUselessListeners(displayMoveListeners_, persistentId);
3160 }
3161 {
3162 std::lock_guard<std::recursive_mutex> lockListener(lifeCycleListenerMutex_);
3163 ClearUselessListeners(lifecycleListeners_, persistentId);
3164 }
3165 {
3166 std::lock_guard<std::recursive_mutex> lockListener(windowChangeListenerMutex_);
3167 ClearUselessListeners(windowChangeListeners_, persistentId);
3168 }
3169 {
3170 std::lock_guard<std::recursive_mutex> lockListener(avoidAreaChangeListenerMutex_);
3171 ClearUselessListeners(avoidAreaChangeListeners_, persistentId);
3172 }
3173 {
3174 std::lock_guard<std::recursive_mutex> lockListener(dialogDeathRecipientListenerMutex_);
3175 ClearUselessListeners(dialogDeathRecipientListeners_, persistentId);
3176 }
3177 {
3178 std::lock_guard<std::recursive_mutex> lockListener(dialogTargetTouchListenerMutex_);
3179 ClearUselessListeners(dialogTargetTouchListener_, persistentId);
3180 }
3181 {
3182 std::lock_guard<std::recursive_mutex> lockListener(screenshotListenerMutex_);
3183 ClearUselessListeners(screenshotListeners_, persistentId);
3184 }
3185 {
3186 std::lock_guard<std::recursive_mutex> lockListener(windowStatusChangeListenerMutex_);
3187 ClearUselessListeners(windowStatusChangeListeners_, persistentId);
3188 }
3189 {
3190 std::lock_guard<std::recursive_mutex> lockListener(windowTitleButtonRectChangeListenerMutex_);
3191 ClearUselessListeners(windowTitleButtonRectChangeListeners_, persistentId);
3192 }
3193 {
3194 std::lock_guard<std::mutex> lockListener(displayIdChangeListenerMutex_);
3195 ClearUselessListeners(displayIdChangeListeners_, persistentId);
3196 }
3197 {
3198 std::lock_guard<std::mutex> lockListener(systemDensityChangeListenerMutex_);
3199 ClearUselessListeners(systemDensityChangeListeners_, persistentId);
3200 }
3201 {
3202 std::lock_guard<std::recursive_mutex> lockListener(windowNoInteractionListenerMutex_);
3203 ClearUselessListeners(windowNoInteractionListeners_, persistentId);
3204 }
3205 {
3206 std::lock_guard<std::mutex> lockListener(windowRectChangeListenerMutex_);
3207 ClearUselessListeners(windowRectChangeListeners_, persistentId);
3208 }
3209 {
3210 std::lock_guard<std::mutex> lockListener(subWindowCloseListenersMutex_);
3211 ClearUselessListeners(subWindowCloseListeners_, persistentId);
3212 }
3213 {
3214 std::lock_guard<std::mutex> lockListener(mainWindowCloseListenersMutex_);
3215 ClearUselessListeners(mainWindowCloseListeners_, persistentId);
3216 }
3217 {
3218 std::lock_guard<std::mutex> lockListener(windowWillCloseListenersMutex_);
3219 ClearUselessListeners(windowWillCloseListeners_, persistentId);
3220 }
3221 {
3222 std::lock_guard<std::recursive_mutex> lockListener(occupiedAreaChangeListenerMutex_);
3223 ClearUselessListeners(occupiedAreaChangeListeners_, persistentId);
3224 }
3225 {
3226 std::lock_guard<std::recursive_mutex> lockListener(keyboardDidShowListenerMutex_);
3227 ClearUselessListeners(keyboardDidShowListeners_, persistentId);
3228 }
3229 {
3230 std::lock_guard<std::recursive_mutex> lockListener(keyboardDidHideListenerMutex_);
3231 ClearUselessListeners(keyboardDidHideListeners_, persistentId);
3232 }
3233 {
3234 std::lock_guard<std::mutex> lockListener(highlightChangeListenerMutex_);
3235 ClearUselessListeners(highlightChangeListeners_, persistentId);
3236 }
3237 {
3238 std::lock_guard<std::recursive_mutex> lockListener(windowCrossAxisListenerMutex_);
3239 ClearUselessListeners(windowCrossAxisListeners_, persistentId);
3240 }
3241 ClearSwitchFreeMultiWindowListenersById(persistentId);
3242 TLOGI(WmsLogTag::WMS_LIFE, "Clear success, id: %{public}d.", GetPersistentId());
3243 }
3244
ClearSwitchFreeMultiWindowListenersById(int32_t persistentId)3245 void WindowSessionImpl::ClearSwitchFreeMultiWindowListenersById(int32_t persistentId)
3246 {
3247 std::lock_guard<std::mutex> lockListener(switchFreeMultiWindowListenerMutex_);
3248 ClearUselessListeners(switchFreeMultiWindowListeners_, persistentId);
3249 }
3250
RegisterWindowDestroyedListener(const NotifyNativeWinDestroyFunc & func)3251 void WindowSessionImpl::RegisterWindowDestroyedListener(const NotifyNativeWinDestroyFunc& func)
3252 {
3253 notifyNativeFunc_ = std::move(func);
3254 }
3255
ClearVsyncStation()3256 void WindowSessionImpl::ClearVsyncStation()
3257 {
3258 if (vsyncStation_ != nullptr) {
3259 vsyncStation_->Destroy();
3260 }
3261 }
3262
SetInputEventConsumer(const std::shared_ptr<IInputEventConsumer> & inputEventConsumer)3263 void WindowSessionImpl::SetInputEventConsumer(const std::shared_ptr<IInputEventConsumer>& inputEventConsumer)
3264 {
3265 TLOGI(WmsLogTag::WMS_EVENT, "in");
3266 std::lock_guard<std::recursive_mutex> lock(mutex_);
3267 inputEventConsumer_ = inputEventConsumer;
3268 }
3269
SetTitleButtonVisible(bool isMaximizeVisible,bool isMinimizeVisible,bool isSplitVisible,bool isCloseVisible)3270 WMError WindowSessionImpl::SetTitleButtonVisible(bool isMaximizeVisible, bool isMinimizeVisible, bool isSplitVisible,
3271 bool isCloseVisible)
3272 {
3273 if (IsWindowSessionInvalid()) {
3274 return WMError::WM_ERROR_INVALID_WINDOW;
3275 }
3276 if (!WindowHelper::IsMainWindow(GetType())) {
3277 return WMError::WM_ERROR_INVALID_CALLING;
3278 }
3279 if (!IsPcOrPadCapabilityEnabled()) {
3280 return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
3281 }
3282 if (GetUIContentSharedPtr() == nullptr || !IsDecorEnable()) {
3283 return WMError::WM_ERROR_INVALID_WINDOW;
3284 }
3285 windowTitleVisibleFlags_ = { isMaximizeVisible, isMinimizeVisible, isSplitVisible, isCloseVisible };
3286 UpdateTitleButtonVisibility();
3287 return WMError::WM_OK;
3288 }
3289
SetSplitButtonVisible(bool isVisible)3290 WSError WindowSessionImpl::SetSplitButtonVisible(bool isVisible)
3291 {
3292 TLOGI(WmsLogTag::WMS_DECOR, "isVisible: %{public}d", isVisible);
3293 auto task = [weakThis = wptr(this), isVisible] {
3294 auto window = weakThis.promote();
3295 if (!window) {
3296 return;
3297 }
3298 window->isSplitButtonVisible_ = isVisible;
3299 window->UpdateTitleButtonVisibility();
3300 };
3301 handler_->PostTask(task, "WMS_WindowSessionImpl_SetSplitButtonVisible");
3302 return WSError::WS_OK;
3303 }
3304
GetIsMidScene(bool & isMidScene)3305 WMError WindowSessionImpl::GetIsMidScene(bool& isMidScene)
3306 {
3307 if (IsWindowSessionInvalid()) {
3308 return WMError::WM_ERROR_INVALID_WINDOW;
3309 }
3310 auto hostSession = GetHostSession();
3311 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, WMError::WM_ERROR_INVALID_WINDOW);
3312 hostSession->GetIsMidScene(isMidScene);
3313 return WMError::WM_OK;
3314 }
3315
GetCrossAxisState()3316 CrossAxisState WindowSessionImpl::GetCrossAxisState()
3317 {
3318 if (crossAxisState_ != CrossAxisState::STATE_INVALID) {
3319 return crossAxisState_;
3320 }
3321 if (IsWindowSessionInvalid()) {
3322 return CrossAxisState::STATE_INVALID;
3323 }
3324 auto hostSession = GetHostSession();
3325 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, CrossAxisState::STATE_INVALID);
3326 CrossAxisState state = CrossAxisState::STATE_INVALID;
3327 if (hostSession->GetCrossAxisState(state) != WSError::WS_OK) {
3328 return CrossAxisState::STATE_INVALID;
3329 }
3330 TLOGI(WmsLogTag::WMS_LAYOUT, "window id is %{public}d, state is %{public}d", GetPersistentId(),
3331 static_cast<uint32_t>(state));
3332 return state;
3333 }
3334
SendContainerModalEvent(const std::string & eventName,const std::string & eventValue)3335 WSError WindowSessionImpl::SendContainerModalEvent(const std::string& eventName, const std::string& eventValue)
3336 {
3337 TLOGI(WmsLogTag::WMS_EVENT, "in");
3338 auto task = [weakThis = wptr(this), eventName, eventValue] {
3339 auto window = weakThis.promote();
3340 if (!window) {
3341 return;
3342 }
3343 std::shared_ptr<Ace::UIContent> uiContent = window->GetUIContentSharedPtr();
3344 if (uiContent == nullptr) {
3345 TLOGNE(WmsLogTag::WMS_EVENT, "uiContent is null!");
3346 return;
3347 }
3348 TLOGNI(WmsLogTag::WMS_EVENT, "name: %{public}s, value: %{public}s", eventName.c_str(), eventValue.c_str());
3349 uiContent->OnContainerModalEvent(eventName, eventValue);
3350 };
3351 handler_->PostTask(task, "WMS_WindowSessionImpl_SendContainerModalEvent");
3352 return WSError::WS_OK;
3353 }
3354
SetWindowContainerColor(const std::string & activeColor,const std::string & inactiveColor)3355 WMError WindowSessionImpl::SetWindowContainerColor(const std::string& activeColor, const std::string& inactiveColor)
3356 {
3357 if (!WindowHelper::IsMainWindow(GetType())) {
3358 return WMError::WM_ERROR_INVALID_CALLING;
3359 }
3360 if (!IsDecorEnable()) {
3361 return WMError::WM_ERROR_INVALID_WINDOW;
3362 }
3363 if (!(windowSystemConfig_.IsPcWindow() || windowSystemConfig_.IsPadWindow())) {
3364 return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
3365 }
3366 uint32_t activeColorValue;
3367 if (!ColorParser::Parse(activeColor, activeColorValue)) {
3368 TLOGW(WmsLogTag::WMS_DECOR, "window: %{public}s, value: [%{public}s, %{public}u]",
3369 GetWindowName().c_str(), activeColor.c_str(), activeColorValue);
3370 return WMError::WM_ERROR_INVALID_PARAM;
3371 }
3372 uint32_t inactiveColorValue;
3373 if (!ColorParser::Parse(inactiveColor, inactiveColorValue)) {
3374 TLOGW(WmsLogTag::WMS_DECOR, "window: %{public}s, value: [%{public}s, %{public}u]",
3375 GetWindowName().c_str(), inactiveColor.c_str(), inactiveColorValue);
3376 return WMError::WM_ERROR_INVALID_PARAM;
3377 }
3378 if (auto uiContent = GetUIContentSharedPtr()) {
3379 uiContent->SetWindowContainerColor(activeColorValue, inactiveColorValue);
3380 }
3381 return WMError::WM_OK;
3382 }
3383
NotifyAfterForeground(bool needNotifyListeners,bool needNotifyUiContent)3384 void WindowSessionImpl::NotifyAfterForeground(bool needNotifyListeners, bool needNotifyUiContent)
3385 {
3386 if (needNotifyListeners) {
3387 std::lock_guard<std::recursive_mutex> lockListener(lifeCycleListenerMutex_);
3388 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
3389 CALL_LIFECYCLE_LISTENER(AfterForeground, lifecycleListeners);
3390 }
3391 if (needNotifyUiContent) {
3392 CALL_UI_CONTENT(Foreground);
3393 }
3394
3395 if (vsyncStation_ != nullptr) {
3396 TLOGD(WmsLogTag::WMS_MAIN, "enable FrameRateLinker, linkerId=%{public}" PRIu64,
3397 vsyncStation_->GetFrameRateLinkerId());
3398 vsyncStation_->SetFrameRateLinkerEnable(true);
3399 if (WindowHelper::IsMainWindow(GetType())) {
3400 TLOGD(WmsLogTag::WMS_MAIN, "IsMainWindow: enable soloist linker");
3401 vsyncStation_->SetDisplaySoloistFrameRateLinkerEnable(true);
3402 }
3403 } else {
3404 TLOGW(WmsLogTag::WMS_MAIN, "vsyncStation is null");
3405 }
3406 }
3407
NotifyAfterDidForeground(uint32_t reason)3408 void WindowSessionImpl::NotifyAfterDidForeground(uint32_t reason)
3409 {
3410 TLOGI(WmsLogTag::WMS_LIFE, "reason: %{public}d", reason);
3411 if (reason != static_cast<uint32_t>(WindowStateChangeReason::USER_SWITCH) &&
3412 reason != static_cast<uint32_t>(WindowStateChangeReason::ABILITY_CALL)) {
3413 TLOGI(WmsLogTag::WMS_LIFE, "reason: %{public}d no need notify did foreground", reason);
3414 return;
3415 }
3416 if (handler_ == nullptr) {
3417 TLOGE(WmsLogTag::WMS_LIFE, "handler is nullptr");
3418 return;
3419 }
3420 const char* const where = __func__;
3421 handler_->PostTask([weak = wptr(this), where] {
3422 auto window = weak.promote();
3423 if (window == nullptr) {
3424 TLOGNE(WmsLogTag::WMS_LIFE, "%{public}s window is nullptr", where);
3425 return;
3426 }
3427 TLOGNI(WmsLogTag::WMS_LIFE, "%{public}s execute", where);
3428 auto lifecycleListeners = window->GetListeners<IWindowLifeCycle>();
3429 CALL_LIFECYCLE_LISTENER(AfterDidForeground, lifecycleListeners);
3430 }, where, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
3431 }
3432
NotifyAfterBackground(bool needNotifyListeners,bool needNotifyUiContent)3433 void WindowSessionImpl::NotifyAfterBackground(bool needNotifyListeners, bool needNotifyUiContent)
3434 {
3435 if (needNotifyListeners) {
3436 std::lock_guard<std::recursive_mutex> lockListener(lifeCycleListenerMutex_);
3437 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
3438 CALL_LIFECYCLE_LISTENER(AfterBackground, lifecycleListeners);
3439 }
3440 if (needNotifyUiContent) {
3441 CALL_UI_CONTENT(Background);
3442 }
3443
3444 if (vsyncStation_ != nullptr) {
3445 TLOGD(WmsLogTag::WMS_MAIN, "disable FrameRateLinker, linkerId=%{public}" PRIu64,
3446 vsyncStation_->GetFrameRateLinkerId());
3447 vsyncStation_->SetFrameRateLinkerEnable(false);
3448 if (WindowHelper::IsMainWindow(GetType())) {
3449 TLOGD(WmsLogTag::WMS_MAIN, "IsMainWindow: disable soloist linker");
3450 vsyncStation_->SetDisplaySoloistFrameRateLinkerEnable(false);
3451 }
3452 } else {
3453 TLOGW(WmsLogTag::WMS_MAIN, "vsyncStation is null");
3454 }
3455 }
3456
NotifyAfterDidBackground(uint32_t reason)3457 void WindowSessionImpl::NotifyAfterDidBackground(uint32_t reason)
3458 {
3459 TLOGI(WmsLogTag::WMS_LIFE, "reason: %{public}d", reason);
3460 if (reason != static_cast<uint32_t>(WindowStateChangeReason::USER_SWITCH) &&
3461 reason != static_cast<uint32_t>(WindowStateChangeReason::ABILITY_CALL)) {
3462 TLOGI(WmsLogTag::WMS_LIFE, "reason: %{public}d no need notify did background", reason);
3463 return;
3464 }
3465 if (handler_ == nullptr) {
3466 TLOGE(WmsLogTag::WMS_LIFE, "handler is nullptr");
3467 return;
3468 }
3469 const char* const where = __func__;
3470 handler_->PostTask([weak = wptr(this), where] {
3471 auto window = weak.promote();
3472 if (window == nullptr) {
3473 TLOGNI(WmsLogTag::WMS_LIFE, "%{public}s window is nullptr", where);
3474 return;
3475 }
3476 TLOGNI(WmsLogTag::WMS_LIFE, "%{public}s execute", where);
3477 auto lifecycleListeners = window->GetListeners<IWindowLifeCycle>();
3478 CALL_LIFECYCLE_LISTENER(AfterDidBackground, lifecycleListeners);
3479 }, where, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
3480 }
3481
RequestInputMethodCloseKeyboard(bool isNeedKeyboard,bool keepKeyboardFlag)3482 static void RequestInputMethodCloseKeyboard(bool isNeedKeyboard, bool keepKeyboardFlag)
3483 {
3484 if (!isNeedKeyboard && !keepKeyboardFlag) {
3485 #ifdef IMF_ENABLE
3486 if (MiscServices::InputMethodController::GetInstance()) {
3487 MiscServices::InputMethodController::GetInstance()->RequestHideInput();
3488 TLOGI(WmsLogTag::WMS_KEYBOARD, "Notify InputMethod framework close keyboard end.");
3489 }
3490 #endif
3491 }
3492 }
3493
NotifyUIContentFocusStatus()3494 void WindowSessionImpl::NotifyUIContentFocusStatus()
3495 {
3496 if (!isFocused_) {
3497 CALL_UI_CONTENT(UnFocus);
3498 return;
3499 }
3500 CALL_UI_CONTENT(Focus);
3501 auto task = [weak = wptr(this)]() {
3502 auto window = weak.promote();
3503 if (!window) {
3504 return;
3505 }
3506 bool isNeedKeyboard = false;
3507 if (auto uiContent = window->GetUIContentSharedPtr()) {
3508 // isNeedKeyboard is set by arkui and indicates whether the window needs a keyboard or not.
3509 isNeedKeyboard = uiContent->NeedSoftKeyboard();
3510 }
3511 // whether keep the keyboard created by other windows, support system window and app subwindow.
3512 bool keepKeyboardFlag = window->property_->GetKeepKeyboardFlag();
3513 TLOGNI(WmsLogTag::WMS_KEYBOARD, "id: %{public}d, isNeedKeyboard: %{public}d, keepKeyboardFlag: %{public}d",
3514 window->GetPersistentId(), isNeedKeyboard, keepKeyboardFlag);
3515 RequestInputMethodCloseKeyboard(isNeedKeyboard, keepKeyboardFlag);
3516 };
3517 if (auto uiContent = GetUIContentSharedPtr()) {
3518 uiContent->SetOnWindowFocused(task);
3519 }
3520 }
3521
NotifyAfterFocused()3522 void WindowSessionImpl::NotifyAfterFocused()
3523 {
3524 NotifyWindowAfterFocused();
3525 if (GetUIContentSharedPtr() != nullptr) {
3526 NotifyUIContentFocusStatus();
3527 } else {
3528 shouldReNotifyFocus_ = true;
3529 }
3530 }
3531
NotifyAfterUnfocused(bool needNotifyUiContent)3532 void WindowSessionImpl::NotifyAfterUnfocused(bool needNotifyUiContent)
3533 {
3534 NotifyWindowAfterUnfocused();
3535 if (needNotifyUiContent) {
3536 if (GetUIContentSharedPtr() == nullptr) {
3537 shouldReNotifyFocus_ = true;
3538 } else {
3539 CALL_UI_CONTENT(UnFocus);
3540 }
3541 }
3542 }
3543
NotifyWindowAfterFocused()3544 void WindowSessionImpl::NotifyWindowAfterFocused()
3545 {
3546 std::lock_guard<std::recursive_mutex> lockListener(lifeCycleListenerMutex_);
3547 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
3548 CALL_LIFECYCLE_LISTENER(AfterFocused, lifecycleListeners);
3549 }
3550
NotifyWindowAfterUnfocused()3551 void WindowSessionImpl::NotifyWindowAfterUnfocused()
3552 {
3553 std::lock_guard<std::recursive_mutex> lockListener(lifeCycleListenerMutex_);
3554 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
3555 // use needNotifyUinContent to separate ui content callbacks
3556 CALL_LIFECYCLE_LISTENER(AfterUnfocused, lifecycleListeners);
3557 }
3558
NotifyUIContentHighlightStatus(bool isHighlighted)3559 void WindowSessionImpl::NotifyUIContentHighlightStatus(bool isHighlighted)
3560 {
3561 if (isHighlighted) {
3562 CALL_UI_CONTENT(ActiveWindow);
3563 } else {
3564 CALL_UI_CONTENT(UnActiveWindow);
3565 }
3566 }
3567
NotifyBeforeDestroy(std::string windowName)3568 void WindowSessionImpl::NotifyBeforeDestroy(std::string windowName)
3569 {
3570 auto task = [this]() {
3571 if (auto uiContent = GetUIContentSharedPtr()) {
3572 uiContent->Destroy();
3573 }
3574 {
3575 std::unique_lock<std::shared_mutex> lock(uiContentMutex_);
3576 if (uiContent_ != nullptr) {
3577 uiContent_ = nullptr;
3578 TLOGND(WmsLogTag::WMS_LIFE, "NotifyBeforeDestroy: uiContent destroy success, persistentId:%{public}d",
3579 GetPersistentId());
3580 }
3581 }
3582 };
3583 if (handler_) {
3584 handler_->PostSyncTask(task, "wms:NotifyBeforeDestroy");
3585 } else {
3586 task();
3587 }
3588 TLOGI(WmsLogTag::WMS_LIFE, "Release uicontent successfully, id: %{public}d.", GetPersistentId());
3589 if (notifyNativeFunc_) {
3590 notifyNativeFunc_(windowName);
3591 }
3592 TLOGI(WmsLogTag::WMS_LIFE, "successed with id: %{public}d.", GetPersistentId());
3593 }
3594
NotifyAfterDestroy()3595 void WindowSessionImpl::NotifyAfterDestroy()
3596 {
3597 std::lock_guard<std::recursive_mutex> lockListener(lifeCycleListenerMutex_);
3598 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
3599 CALL_LIFECYCLE_LISTENER(AfterDestroyed, lifecycleListeners);
3600 }
3601
NotifyAfterActive()3602 void WindowSessionImpl::NotifyAfterActive()
3603 {
3604 std::lock_guard<std::recursive_mutex> lockListener(lifeCycleListenerMutex_);
3605 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
3606 CALL_LIFECYCLE_LISTENER(AfterActive, lifecycleListeners);
3607 }
3608
NotifyAfterInactive()3609 void WindowSessionImpl::NotifyAfterInactive()
3610 {
3611 std::lock_guard<std::recursive_mutex> lockListener(lifeCycleListenerMutex_);
3612 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
3613 CALL_LIFECYCLE_LISTENER(AfterInactive, lifecycleListeners);
3614 }
3615
NotifyForegroundFailed(WMError ret)3616 void WindowSessionImpl::NotifyForegroundFailed(WMError ret)
3617 {
3618 std::lock_guard<std::recursive_mutex> lockListener(lifeCycleListenerMutex_);
3619 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
3620 CALL_LIFECYCLE_LISTENER_WITH_PARAM(ForegroundFailed, lifecycleListeners, static_cast<int32_t>(ret));
3621 }
3622
NotifyBackgroundFailed(WMError ret)3623 void WindowSessionImpl::NotifyBackgroundFailed(WMError ret)
3624 {
3625 std::lock_guard<std::recursive_mutex> lockListener(lifeCycleListenerMutex_);
3626 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
3627 CALL_LIFECYCLE_LISTENER_WITH_PARAM(BackgroundFailed, lifecycleListeners, static_cast<int32_t>(ret));
3628 }
3629
NotifyAfterResumed()3630 void WindowSessionImpl::NotifyAfterResumed()
3631 {
3632 std::lock_guard<std::recursive_mutex> lockListener(lifeCycleListenerMutex_);
3633 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
3634 CALL_LIFECYCLE_LISTENER(AfterResumed, lifecycleListeners);
3635 }
3636
NotifyAfterPaused()3637 void WindowSessionImpl::NotifyAfterPaused()
3638 {
3639 std::lock_guard<std::recursive_mutex> lockListener(lifeCycleListenerMutex_);
3640 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
3641 CALL_LIFECYCLE_LISTENER(AfterPaused, lifecycleListeners);
3642 }
3643
MarkProcessed(int32_t eventId)3644 WSError WindowSessionImpl::MarkProcessed(int32_t eventId)
3645 {
3646 if (IsWindowSessionInvalid()) {
3647 WLOGFE("session is invalid");
3648 return WSError::WS_DO_NOTHING;
3649 }
3650 auto hostSession = GetHostSession();
3651 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, WSError::WS_DO_NOTHING);
3652 return hostSession->MarkProcessed(eventId);
3653 }
3654
RegisterDialogDeathRecipientListener(const sptr<IDialogDeathRecipientListener> & listener)3655 void WindowSessionImpl::RegisterDialogDeathRecipientListener(const sptr<IDialogDeathRecipientListener>& listener)
3656 {
3657 TLOGI(WmsLogTag::WMS_DIALOG, "window %{public}s id %{public}d register DialogDeathRecipientListener",
3658 GetWindowName().c_str(), GetPersistentId());
3659 if (listener == nullptr) {
3660 WLOGFE("listener is null");
3661 return;
3662 }
3663 std::lock_guard<std::recursive_mutex> lockListener(dialogDeathRecipientListenerMutex_);
3664 RegisterListener(dialogDeathRecipientListeners_[GetPersistentId()], listener);
3665 }
3666
UnregisterDialogDeathRecipientListener(const sptr<IDialogDeathRecipientListener> & listener)3667 void WindowSessionImpl::UnregisterDialogDeathRecipientListener(const sptr<IDialogDeathRecipientListener>& listener)
3668 {
3669 TLOGI(WmsLogTag::WMS_DIALOG, "window %{public}s id %{public}d",
3670 GetWindowName().c_str(), GetPersistentId());
3671 std::lock_guard<std::recursive_mutex> lockListener(dialogDeathRecipientListenerMutex_);
3672 UnregisterListener(dialogDeathRecipientListeners_[GetPersistentId()], listener);
3673 }
3674
RegisterDialogTargetTouchListener(const sptr<IDialogTargetTouchListener> & listener)3675 WMError WindowSessionImpl::RegisterDialogTargetTouchListener(const sptr<IDialogTargetTouchListener>& listener)
3676 {
3677 TLOGI(WmsLogTag::WMS_DIALOG, "window %{public}s id %{public}d",
3678 GetWindowName().c_str(), GetPersistentId());
3679 if (listener == nullptr) {
3680 WLOGFE("listener is nullptr");
3681 return WMError::WM_ERROR_NULLPTR;
3682 }
3683 std::lock_guard<std::recursive_mutex> lockListener(dialogTargetTouchListenerMutex_);
3684 return RegisterListener(dialogTargetTouchListener_[GetPersistentId()], listener);
3685 }
3686
UnregisterDialogTargetTouchListener(const sptr<IDialogTargetTouchListener> & listener)3687 WMError WindowSessionImpl::UnregisterDialogTargetTouchListener(const sptr<IDialogTargetTouchListener>& listener)
3688 {
3689 TLOGI(WmsLogTag::WMS_DIALOG, "window %{public}s id %{public}d",
3690 GetWindowName().c_str(), GetPersistentId());
3691 std::lock_guard<std::recursive_mutex> lockListener(dialogTargetTouchListenerMutex_);
3692 return UnregisterListener(dialogTargetTouchListener_[GetPersistentId()], listener);
3693 }
3694
RegisterScreenshotListener(const sptr<IScreenshotListener> & listener)3695 WMError WindowSessionImpl::RegisterScreenshotListener(const sptr<IScreenshotListener>& listener)
3696 {
3697 WLOGFD("in");
3698 std::lock_guard<std::recursive_mutex> lockListener(screenshotListenerMutex_);
3699 return RegisterListener(screenshotListeners_[GetPersistentId()], listener);
3700 }
3701
UnregisterScreenshotListener(const sptr<IScreenshotListener> & listener)3702 WMError WindowSessionImpl::UnregisterScreenshotListener(const sptr<IScreenshotListener>& listener)
3703 {
3704 WLOGFD("in");
3705 std::lock_guard<std::recursive_mutex> lockListener(screenshotListenerMutex_);
3706 return UnregisterListener(screenshotListeners_[GetPersistentId()], listener);
3707 }
3708
3709 template<typename T>
3710 EnableIfSame<T, IDialogDeathRecipientListener, std::vector<sptr<IDialogDeathRecipientListener>>> WindowSessionImpl::
GetListeners()3711 GetListeners()
3712 {
3713 std::vector<sptr<IDialogDeathRecipientListener>> dialogDeathRecipientListener;
3714 for (auto& listener : dialogDeathRecipientListeners_[GetPersistentId()]) {
3715 dialogDeathRecipientListener.push_back(listener);
3716 }
3717 return dialogDeathRecipientListener;
3718 }
3719
3720 template<typename T>
3721 EnableIfSame<T, IDialogTargetTouchListener,
GetListeners()3722 std::vector<sptr<IDialogTargetTouchListener>>> WindowSessionImpl::GetListeners()
3723 {
3724 std::vector<sptr<IDialogTargetTouchListener>> dialogTargetTouchListener;
3725 for (auto& listener : dialogTargetTouchListener_[GetPersistentId()]) {
3726 dialogTargetTouchListener.push_back(listener);
3727 }
3728 return dialogTargetTouchListener;
3729 }
3730
3731 template<typename T>
GetListeners()3732 EnableIfSame<T, IScreenshotListener, std::vector<sptr<IScreenshotListener>>> WindowSessionImpl::GetListeners()
3733 {
3734 std::vector<sptr<IScreenshotListener>> screenshotListeners;
3735 for (auto& listener : screenshotListeners_[GetPersistentId()]) {
3736 screenshotListeners.push_back(listener);
3737 }
3738 return screenshotListeners;
3739 }
3740
NotifyDestroy()3741 WSError WindowSessionImpl::NotifyDestroy()
3742 {
3743 if (WindowHelper::IsDialogWindow(property_->GetWindowType())) {
3744 std::lock_guard<std::recursive_mutex> lockListener(dialogDeathRecipientListenerMutex_);
3745 auto dialogDeathRecipientListener = GetListeners<IDialogDeathRecipientListener>();
3746 for (auto& listener : dialogDeathRecipientListener) {
3747 if (listener != nullptr) {
3748 listener->OnDialogDeathRecipient();
3749 }
3750 }
3751 } else if (WindowHelper::IsSubWindow(property_->GetWindowType())) {
3752 if (property_->GetIsUIExtFirstSubWindow() && !isUIExtensionAbilityProcess_) {
3753 Destroy();
3754 }
3755 }
3756 return WSError::WS_OK;
3757 }
3758
3759 template<typename T>
GetListeners()3760 EnableIfSame<T, IDisplayMoveListener, std::vector<sptr<IDisplayMoveListener>>> WindowSessionImpl::GetListeners()
3761 {
3762 std::vector<sptr<IDisplayMoveListener>> displayMoveListeners;
3763 for (auto& listener : displayMoveListeners_[GetPersistentId()]) {
3764 displayMoveListeners.push_back(listener);
3765 }
3766 return displayMoveListeners;
3767 }
3768
NotifyDisplayMove(DisplayId from,DisplayId to)3769 void WindowSessionImpl::NotifyDisplayMove(DisplayId from, DisplayId to)
3770 {
3771 WLOGFD("from %{public}" PRIu64 " to %{public}" PRIu64, from, to);
3772 std::lock_guard<std::mutex> lockListener(displayMoveListenerMutex_);
3773 auto displayMoveListeners = GetListeners<IDisplayMoveListener>();
3774 for (auto& listener : displayMoveListeners) {
3775 if (listener != nullptr) {
3776 listener->OnDisplayMove(from, to);
3777 }
3778 }
3779 }
3780
NotifyCloseExistPipWindow()3781 WSError WindowSessionImpl::NotifyCloseExistPipWindow()
3782 {
3783 TLOGI(WmsLogTag::WMS_PIP, "in");
3784 auto task = []() {
3785 PictureInPictureManager::DoClose(true, true);
3786 };
3787 handler_->PostTask(task, "WMS_WindowSessionImpl_NotifyCloseExistPipWindow");
3788 return WSError::WS_OK;
3789 }
3790
NotifyTouchDialogTarget(int32_t posX,int32_t posY)3791 void WindowSessionImpl::NotifyTouchDialogTarget(int32_t posX, int32_t posY)
3792 {
3793 TLOGI(WmsLogTag::WMS_DIALOG, "window %{public}s id %{public}d", GetWindowName().c_str(), GetPersistentId());
3794 if (auto hostSession = GetHostSession()) {
3795 hostSession->ProcessPointDownSession(posX, posY);
3796 }
3797 std::lock_guard<std::recursive_mutex> lockListener(dialogTargetTouchListenerMutex_);
3798 auto dialogTargetTouchListener = GetListeners<IDialogTargetTouchListener>();
3799 for (auto& listener : dialogTargetTouchListener) {
3800 if (listener != nullptr) {
3801 listener->OnDialogTargetTouch();
3802 }
3803 }
3804 }
3805
NotifyScreenshot()3806 void WindowSessionImpl::NotifyScreenshot()
3807 {
3808 std::lock_guard<std::recursive_mutex> lockListener(screenshotListenerMutex_);
3809 auto screenshotListeners = GetListeners<IScreenshotListener>();
3810 for (auto& listener : screenshotListeners) {
3811 if (listener != nullptr) {
3812 listener->OnScreenshot();
3813 }
3814 }
3815 }
3816
3817 /** @note @window.layout */
NotifySizeChange(Rect rect,WindowSizeChangeReason reason)3818 void WindowSessionImpl::NotifySizeChange(Rect rect, WindowSizeChangeReason reason)
3819 {
3820 {
3821 std::lock_guard<std::recursive_mutex> lockListener(windowChangeListenerMutex_);
3822 auto windowChangeListeners = GetListeners<IWindowChangeListener>();
3823 TLOGD(WmsLogTag::WMS_LAYOUT, "Id:%{public}d, sizeChange listenerSize:%{public}zu",
3824 GetPersistentId(), windowChangeListeners.size());
3825 for (auto& listener : windowChangeListeners) {
3826 if (listener != nullptr) {
3827 listener->OnSizeChange(rect, reason);
3828 }
3829 }
3830 }
3831 {
3832 std::lock_guard<std::mutex> lockRectListener(windowRectChangeListenerMutex_);
3833 auto windowRectChangeListeners = GetListeners<IWindowRectChangeListener>();
3834 TLOGD(WmsLogTag::WMS_LAYOUT, "Id:%{public}d, rectChange listenerSize:%{public}zu",
3835 GetPersistentId(), windowRectChangeListeners.size());
3836 for (auto& listener : windowRectChangeListeners) {
3837 if (listener != nullptr) {
3838 listener->OnRectChange(rect, reason);
3839 }
3840 }
3841 }
3842 }
3843
NotifySubWindowClose(bool & terminateCloseProcess)3844 void WindowSessionImpl::NotifySubWindowClose(bool& terminateCloseProcess)
3845 {
3846 WLOGFD("in");
3847 std::lock_guard<std::mutex> lockListener(subWindowCloseListenersMutex_);
3848 auto subWindowCloseListeners = GetListeners<ISubWindowCloseListener>();
3849 if (subWindowCloseListeners != nullptr) {
3850 subWindowCloseListeners->OnSubWindowClose(terminateCloseProcess);
3851 }
3852 }
3853
NotifyMainWindowClose(bool & terminateCloseProcess)3854 WMError WindowSessionImpl::NotifyMainWindowClose(bool& terminateCloseProcess)
3855 {
3856 std::lock_guard<std::mutex> lockListener(mainWindowCloseListenersMutex_);
3857 auto mainWindowCloseListener = GetListeners<IMainWindowCloseListener>();
3858 if (mainWindowCloseListener != nullptr) {
3859 mainWindowCloseListener->OnMainWindowClose(terminateCloseProcess);
3860 return WMError::WM_OK;
3861 }
3862 return WMError::WM_ERROR_NULLPTR;
3863 }
3864
NotifyWindowWillClose(sptr<Window> window)3865 WMError WindowSessionImpl::NotifyWindowWillClose(sptr<Window> window)
3866 {
3867 std::lock_guard<std::mutex> lockListener(windowWillCloseListenersMutex_);
3868 const auto& windowWillCloseListeners = GetListeners<IWindowWillCloseListener>();
3869 auto res = WMError::WM_ERROR_NULLPTR;
3870 for (const auto& listener : windowWillCloseListeners) {
3871 if (listener != nullptr) {
3872 listener->OnWindowWillClose(window);
3873 res = WMError::WM_OK;
3874 }
3875 }
3876 return res;
3877 }
3878
NotifySwitchFreeMultiWindow(bool enable)3879 void WindowSessionImpl::NotifySwitchFreeMultiWindow(bool enable)
3880 {
3881 std::lock_guard<std::mutex> lockListener(switchFreeMultiWindowListenerMutex_);
3882 auto switchFreeMultiWindowListeners = GetListeners<ISwitchFreeMultiWindowListener>();
3883 for (auto& listener : switchFreeMultiWindowListeners) {
3884 if (listener != nullptr) {
3885 listener->OnSwitchFreeMultiWindow(enable);
3886 }
3887 }
3888 }
3889
RegisterAvoidAreaChangeListener(const sptr<IAvoidAreaChangedListener> & listener)3890 WMError WindowSessionImpl::RegisterAvoidAreaChangeListener(const sptr<IAvoidAreaChangedListener>& listener)
3891 {
3892 auto persistentId = GetPersistentId();
3893 TLOGI(WmsLogTag::WMS_IMMS, "win %{public}d", persistentId);
3894 if (listener == nullptr) {
3895 TLOGE(WmsLogTag::WMS_IMMS, "listener is null");
3896 return WMError::WM_ERROR_NULLPTR;
3897 }
3898
3899 WMError ret = WMError::WM_OK;
3900 bool isUpdate = false;
3901 {
3902 std::lock_guard<std::recursive_mutex> lockListener(avoidAreaChangeListenerMutex_);
3903 ret = RegisterListener(avoidAreaChangeListeners_[persistentId], listener);
3904 if (ret != WMError::WM_OK) {
3905 return ret;
3906 }
3907 if (avoidAreaChangeListeners_[persistentId].size() == 1) {
3908 isUpdate = true;
3909 }
3910 }
3911 if (isUpdate) {
3912 ret = SingletonContainer::Get<WindowAdapter>().UpdateSessionAvoidAreaListener(persistentId, true);
3913 }
3914 return ret;
3915 }
3916
UnregisterAvoidAreaChangeListener(const sptr<IAvoidAreaChangedListener> & listener)3917 WMError WindowSessionImpl::UnregisterAvoidAreaChangeListener(const sptr<IAvoidAreaChangedListener>& listener)
3918 {
3919 auto persistentId = GetPersistentId();
3920 TLOGI(WmsLogTag::WMS_IMMS, "win %{public}d", persistentId);
3921 if (listener == nullptr) {
3922 WLOGFE("listener is null");
3923 return WMError::WM_ERROR_NULLPTR;
3924 }
3925
3926 WMError ret = WMError::WM_OK;
3927 bool isUpdate = false;
3928 {
3929 std::lock_guard<std::recursive_mutex> lockListener(avoidAreaChangeListenerMutex_);
3930 ret = UnregisterListener(avoidAreaChangeListeners_[persistentId], listener);
3931 if (ret != WMError::WM_OK) {
3932 return ret;
3933 }
3934 if (avoidAreaChangeListeners_[persistentId].empty()) {
3935 isUpdate = true;
3936 }
3937 }
3938 if (isUpdate) {
3939 ret = SingletonContainer::Get<WindowAdapter>().UpdateSessionAvoidAreaListener(persistentId, false);
3940 }
3941 return ret;
3942 }
3943
RegisterExtensionAvoidAreaChangeListener(const sptr<IAvoidAreaChangedListener> & listener)3944 WMError WindowSessionImpl::RegisterExtensionAvoidAreaChangeListener(const sptr<IAvoidAreaChangedListener>& listener)
3945 {
3946 auto persistentId = GetPersistentId();
3947 WLOGFI("Start, id:%{public}d", persistentId);
3948 std::lock_guard<std::recursive_mutex> lockListener(avoidAreaChangeListenerMutex_);
3949 return RegisterListener(avoidAreaChangeListeners_[persistentId], listener);
3950 }
3951
UnregisterExtensionAvoidAreaChangeListener(const sptr<IAvoidAreaChangedListener> & listener)3952 WMError WindowSessionImpl::UnregisterExtensionAvoidAreaChangeListener(const sptr<IAvoidAreaChangedListener>& listener)
3953 {
3954 auto persistentId = GetPersistentId();
3955 WLOGFI("Start, id:%{public}d", persistentId);
3956 std::lock_guard<std::recursive_mutex> lockListener(avoidAreaChangeListenerMutex_);
3957 return UnregisterListener(avoidAreaChangeListeners_[persistentId], listener);
3958 }
3959
3960 template<typename T>
3961 EnableIfSame<T, IAvoidAreaChangedListener,
GetListeners()3962 std::vector<sptr<IAvoidAreaChangedListener>>> WindowSessionImpl::GetListeners()
3963 {
3964 std::vector<sptr<IAvoidAreaChangedListener>> windowChangeListeners;
3965 for (auto& listener : avoidAreaChangeListeners_[GetPersistentId()]) {
3966 windowChangeListeners.push_back(listener);
3967 }
3968 return windowChangeListeners;
3969 }
3970
NotifyAvoidAreaChange(const sptr<AvoidArea> & avoidArea,AvoidAreaType type)3971 void WindowSessionImpl::NotifyAvoidAreaChange(const sptr<AvoidArea>& avoidArea, AvoidAreaType type)
3972 {
3973 std::lock_guard<std::recursive_mutex> lockListener(avoidAreaChangeListenerMutex_);
3974 auto avoidAreaChangeListeners = GetListeners<IAvoidAreaChangedListener>();
3975 bool isUIExtensionWithSystemHost =
3976 WindowHelper::IsUIExtensionWindow(GetType()) && WindowHelper::IsSystemWindow(GetRootHostWindowType());
3977 bool isSystemWindow = WindowHelper::IsSystemWindow(GetType());
3978 uint32_t currentApiVersion = GetTargetAPIVersionByApplicationInfo();
3979 AvoidArea newAvoidArea;
3980 // api 18 isolation for UEC with system host window
3981 if ((isUIExtensionWithSystemHost || isSystemWindow) && currentApiVersion < static_cast<uint32_t>(API_VERSION_18)) {
3982 TLOGI(WmsLogTag::WMS_IMMS, "win %{public}d api %{public}u type %{public}d not supported",
3983 GetPersistentId(), currentApiVersion, type);
3984 } else {
3985 newAvoidArea = *avoidArea;
3986 TLOGI(WmsLogTag::WMS_IMMS, "win %{public}d api %{public}u type %{public}d area %{public}s",
3987 GetPersistentId(), currentApiVersion, type, newAvoidArea.ToString().c_str());
3988 }
3989
3990 for (auto& listener : avoidAreaChangeListeners) {
3991 if (listener != nullptr) {
3992 listener->OnAvoidAreaChanged(newAvoidArea, type);
3993 }
3994 }
3995 }
3996
NotifyTransferComponentData(const AAFwk::WantParams & wantParams)3997 WSError WindowSessionImpl::NotifyTransferComponentData(const AAFwk::WantParams& wantParams)
3998 {
3999 return WSError::WS_OK;
4000 }
4001
NotifyTransferComponentDataSync(const AAFwk::WantParams & wantParams,AAFwk::WantParams & reWantParams)4002 WSErrorCode WindowSessionImpl::NotifyTransferComponentDataSync(const AAFwk::WantParams& wantParams,
4003 AAFwk::WantParams& reWantParams)
4004 {
4005 return WSErrorCode::WS_OK;
4006 }
4007
UpdateAvoidArea(const sptr<AvoidArea> & avoidArea,AvoidAreaType type)4008 WSError WindowSessionImpl::UpdateAvoidArea(const sptr<AvoidArea>& avoidArea, AvoidAreaType type)
4009 {
4010 auto task = [weak = wptr(this), avoidArea, type] {
4011 auto window = weak.promote();
4012 if (!window) {
4013 return;
4014 }
4015 if (window->lastAvoidAreaMap_[type] != *avoidArea) {
4016 window->lastAvoidAreaMap_[type] = *avoidArea;
4017 window->NotifyAvoidAreaChange(avoidArea, type);
4018 window->UpdateViewportConfig(window->GetRect(), WindowSizeChangeReason::AVOID_AREA_CHANGE);
4019 }
4020 };
4021 handler_->PostTask(std::move(task), __func__);
4022 return WSError::WS_OK;
4023 }
4024
SetPipActionEvent(const std::string & action,int32_t status)4025 WSError WindowSessionImpl::SetPipActionEvent(const std::string& action, int32_t status)
4026 {
4027 TLOGI(WmsLogTag::WMS_PIP, "action: %{public}s, status: %{public}d", action.c_str(), status);
4028 auto task = [action, status]() {
4029 PictureInPictureManager::DoActionEvent(action, status);
4030 };
4031 handler_->PostTask(task, "WMS_WindowSessionImpl_SetPipActionEvent");
4032 return WSError::WS_OK;
4033 }
4034
SetPiPControlEvent(WsPiPControlType controlType,WsPiPControlStatus status)4035 WSError WindowSessionImpl::SetPiPControlEvent(WsPiPControlType controlType, WsPiPControlStatus status)
4036 {
4037 TLOGI(WmsLogTag::WMS_PIP, "controlType:%{public}u, enabled:%{public}d", controlType, status);
4038 auto task = [controlType, status]() {
4039 PictureInPictureManager::DoControlEvent(static_cast<PiPControlType>(controlType),
4040 static_cast<PiPControlStatus>(status));
4041 };
4042 handler_->PostTask(task, "WMS_WindowSessionImpl_SetPiPControlEvent");
4043 return WSError::WS_OK;
4044 }
4045
NotifyPipWindowSizeChange(double width,double height,double scale)4046 WSError WindowSessionImpl::NotifyPipWindowSizeChange(double width, double height, double scale)
4047 {
4048 TLOGI(WmsLogTag::WMS_PIP, "width: %{public}f, height: %{public}f scale: %{public}f", width, height, scale);
4049 auto task = [width, height, scale]() {
4050 PictureInPictureManager::PipSizeChange(width, height, scale);
4051 };
4052 handler_->PostTask(task, "WMS_WindowSessionImpl_NotifyPipWindowSizeChange");
4053 return WSError::WS_OK;
4054 }
4055
RegisterTouchOutsideListener(const sptr<ITouchOutsideListener> & listener)4056 WMError WindowSessionImpl::RegisterTouchOutsideListener(const sptr<ITouchOutsideListener>& listener)
4057 {
4058 bool isUpdate = false;
4059 WMError ret = WMError::WM_OK;
4060 auto persistentId = GetPersistentId();
4061 TLOGI(WmsLogTag::WMS_EVENT, "name=%{public}s, id=%{public}u",
4062 GetWindowName().c_str(), GetPersistentId());
4063 if (listener == nullptr) {
4064 TLOGE(WmsLogTag::WMS_EVENT, "listener is null");
4065 return WMError::WM_ERROR_NULLPTR;
4066 }
4067
4068 {
4069 std::lock_guard<std::recursive_mutex> lockListener(touchOutsideListenerMutex_);
4070 ret = RegisterListener(touchOutsideListeners_[persistentId], listener);
4071 if (ret != WMError::WM_OK) {
4072 TLOGE(WmsLogTag::WMS_EVENT, "fail, ret:%{public}u", ret);
4073 return ret;
4074 }
4075 if (touchOutsideListeners_[persistentId].size() == 1) {
4076 isUpdate = true;
4077 }
4078 }
4079 if (isUpdate) {
4080 ret = SingletonContainer::Get<WindowAdapter>().UpdateSessionTouchOutsideListener(persistentId, true);
4081 }
4082 return ret;
4083 }
4084
UnregisterTouchOutsideListener(const sptr<ITouchOutsideListener> & listener)4085 WMError WindowSessionImpl::UnregisterTouchOutsideListener(const sptr<ITouchOutsideListener>& listener)
4086 {
4087 bool isUpdate = false;
4088 WMError ret = WMError::WM_OK;
4089 auto persistentId = GetPersistentId();
4090 TLOGI(WmsLogTag::WMS_EVENT, "name=%{public}s, id=%{public}u",
4091 GetWindowName().c_str(), GetPersistentId());
4092 if (listener == nullptr) {
4093 TLOGE(WmsLogTag::WMS_EVENT, "listener is null");
4094 return WMError::WM_ERROR_NULLPTR;
4095 }
4096
4097 {
4098 std::lock_guard<std::recursive_mutex> lockListener(touchOutsideListenerMutex_);
4099 ret = UnregisterListener(touchOutsideListeners_[persistentId], listener);
4100 if (ret != WMError::WM_OK) {
4101 TLOGE(WmsLogTag::WMS_EVENT, "fail, ret:%{public}u", ret);
4102 return ret;
4103 }
4104 if (touchOutsideListeners_[persistentId].empty()) {
4105 isUpdate = true;
4106 }
4107 }
4108 if (isUpdate) {
4109 ret = SingletonContainer::Get<WindowAdapter>().UpdateSessionTouchOutsideListener(persistentId, false);
4110 }
4111 return ret;
4112 }
4113
4114 template<typename T>
GetListeners()4115 EnableIfSame<T, ITouchOutsideListener, std::vector<sptr<ITouchOutsideListener>>> WindowSessionImpl::GetListeners()
4116 {
4117 std::vector<sptr<ITouchOutsideListener>> windowChangeListeners;
4118 for (auto& listener : touchOutsideListeners_[GetPersistentId()]) {
4119 windowChangeListeners.push_back(listener);
4120 }
4121 return windowChangeListeners;
4122 }
4123
NotifyTouchOutside()4124 WSError WindowSessionImpl::NotifyTouchOutside()
4125 {
4126 TLOGD(WmsLogTag::WMS_EVENT, "window: name=%{public}s, id=%{public}u",
4127 GetWindowName().c_str(), GetPersistentId());
4128 std::lock_guard<std::recursive_mutex> lockListener(touchOutsideListenerMutex_);
4129 auto touchOutsideListeners = GetListeners<ITouchOutsideListener>();
4130 for (auto& listener : touchOutsideListeners) {
4131 if (listener != nullptr) {
4132 listener->OnTouchOutside();
4133 }
4134 }
4135 return WSError::WS_OK;
4136 }
4137
RegisterWindowVisibilityChangeListener(const IWindowVisibilityListenerSptr & listener)4138 WMError WindowSessionImpl::RegisterWindowVisibilityChangeListener(const IWindowVisibilityListenerSptr& listener)
4139 {
4140 auto persistentId = GetPersistentId();
4141 WLOGFD("Start, persistentId=%{public}d.", persistentId);
4142 WMError ret = WMError::WM_OK;
4143 bool isFirstRegister = false;
4144 {
4145 std::lock_guard<std::recursive_mutex> lockListener(windowVisibilityChangeListenerMutex_);
4146 ret = RegisterListener(windowVisibilityChangeListeners_[persistentId], listener);
4147 if (ret != WMError::WM_OK) {
4148 return ret;
4149 }
4150 isFirstRegister = windowVisibilityChangeListeners_[persistentId].size() == 1;
4151 }
4152
4153 if (isFirstRegister) {
4154 ret = SingletonContainer::Get<WindowAdapter>().UpdateSessionWindowVisibilityListener(persistentId, true);
4155 }
4156 return ret;
4157 }
4158
UnregisterWindowVisibilityChangeListener(const IWindowVisibilityListenerSptr & listener)4159 WMError WindowSessionImpl::UnregisterWindowVisibilityChangeListener(const IWindowVisibilityListenerSptr& listener)
4160 {
4161 auto persistentId = GetPersistentId();
4162 WLOGFD("Start, persistentId=%{public}d.", persistentId);
4163 WMError ret = WMError::WM_OK;
4164 bool isLastUnregister = false;
4165 {
4166 std::lock_guard<std::recursive_mutex> lockListener(windowVisibilityChangeListenerMutex_);
4167 ret = UnregisterListener(windowVisibilityChangeListeners_[persistentId], listener);
4168 if (ret != WMError::WM_OK) {
4169 return ret;
4170 }
4171 isLastUnregister = windowVisibilityChangeListeners_[persistentId].empty();
4172 }
4173
4174 if (isLastUnregister) {
4175 ret = SingletonContainer::Get<WindowAdapter>().UpdateSessionWindowVisibilityListener(persistentId, false);
4176 }
4177 return ret;
4178 }
4179
RegisterDisplayIdChangeListener(const IDisplayIdChangeListenerSptr & listener)4180 WMError WindowSessionImpl::RegisterDisplayIdChangeListener(const IDisplayIdChangeListenerSptr& listener)
4181 {
4182 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "name=%{public}s, id=%{public}u", GetWindowName().c_str(), GetPersistentId());
4183 std::lock_guard<std::mutex> lockListener(displayIdChangeListenerMutex_);
4184 return RegisterListener(displayIdChangeListeners_[GetPersistentId()], listener);
4185 }
4186
UnregisterDisplayIdChangeListener(const IDisplayIdChangeListenerSptr & listener)4187 WMError WindowSessionImpl::UnregisterDisplayIdChangeListener(const IDisplayIdChangeListenerSptr& listener)
4188 {
4189 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "name=%{public}s, id=%{public}u", GetWindowName().c_str(), GetPersistentId());
4190 std::lock_guard<std::mutex> lockListener(displayIdChangeListenerMutex_);
4191 return UnregisterListener(displayIdChangeListeners_[GetPersistentId()], listener);
4192 }
4193
RegisterSystemDensityChangeListener(const ISystemDensityChangeListenerSptr & listener)4194 WMError WindowSessionImpl::RegisterSystemDensityChangeListener(const ISystemDensityChangeListenerSptr& listener)
4195 {
4196 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "name=%{public}s, id=%{public}d", GetWindowName().c_str(), GetPersistentId());
4197 std::lock_guard<std::mutex> lockListener(systemDensityChangeListenerMutex_);
4198 return RegisterListener(systemDensityChangeListeners_[GetPersistentId()], listener);
4199 }
4200
UnregisterSystemDensityChangeListener(const ISystemDensityChangeListenerSptr & listener)4201 WMError WindowSessionImpl::UnregisterSystemDensityChangeListener(const ISystemDensityChangeListenerSptr& listener)
4202 {
4203 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "name=%{public}s, id=%{public}d", GetWindowName().c_str(), GetPersistentId());
4204 std::lock_guard<std::mutex> lockListener(systemDensityChangeListenerMutex_);
4205 return UnregisterListener(systemDensityChangeListeners_[GetPersistentId()], listener);
4206 }
4207
RegisterWindowNoInteractionListener(const IWindowNoInteractionListenerSptr & listener)4208 WMError WindowSessionImpl::RegisterWindowNoInteractionListener(const IWindowNoInteractionListenerSptr& listener)
4209 {
4210 WLOGFD("in");
4211 std::lock_guard<std::recursive_mutex> lockListener(windowNoInteractionListenerMutex_);
4212 WMError ret = RegisterListener(windowNoInteractionListeners_[GetPersistentId()], listener);
4213 if (ret != WMError::WM_OK) {
4214 WLOGFE("register failed.");
4215 } else {
4216 SubmitNoInteractionMonitorTask(this->lastInteractionEventId_.load(), listener);
4217 }
4218 return ret;
4219 }
4220
UnregisterWindowNoInteractionListener(const IWindowNoInteractionListenerSptr & listener)4221 WMError WindowSessionImpl::UnregisterWindowNoInteractionListener(const IWindowNoInteractionListenerSptr& listener)
4222 {
4223 WLOGFD("in");
4224 std::lock_guard<std::recursive_mutex> lockListener(windowNoInteractionListenerMutex_);
4225 WMError ret = UnregisterListener(windowNoInteractionListeners_[GetPersistentId()], listener);
4226 if (windowNoInteractionListeners_[GetPersistentId()].empty()) {
4227 lastInteractionEventId_.store(-1);
4228 }
4229 return ret;
4230 }
4231
4232 template<typename T>
GetListeners()4233 EnableIfSame<T, IWindowVisibilityChangedListener, std::vector<IWindowVisibilityListenerSptr>> WindowSessionImpl::GetListeners()
4234 {
4235 std::vector<IWindowVisibilityListenerSptr> windowVisibilityChangeListeners;
4236 for (auto& listener : windowVisibilityChangeListeners_[GetPersistentId()]) {
4237 windowVisibilityChangeListeners.push_back(listener);
4238 }
4239 return windowVisibilityChangeListeners;
4240 }
4241
4242 template<typename T>
4243 EnableIfSame<T, IDisplayIdChangeListener,
GetListeners()4244 std::vector<IDisplayIdChangeListenerSptr>> WindowSessionImpl::GetListeners()
4245 {
4246 return displayIdChangeListeners_[GetPersistentId()];
4247 }
4248
4249 template<typename T>
4250 EnableIfSame<T, ISystemDensityChangeListener,
GetListeners()4251 std::vector<ISystemDensityChangeListenerSptr>> WindowSessionImpl::GetListeners()
4252 {
4253 return systemDensityChangeListeners_[GetPersistentId()];
4254 }
4255
4256 template<typename T>
GetListeners()4257 EnableIfSame<T, IWindowNoInteractionListener, std::vector<IWindowNoInteractionListenerSptr>> WindowSessionImpl::GetListeners()
4258 {
4259 std::vector<IWindowNoInteractionListenerSptr> noInteractionListeners;
4260 for (auto& listener : windowNoInteractionListeners_[GetPersistentId()]) {
4261 noInteractionListeners.push_back(listener);
4262 }
4263 return noInteractionListeners;
4264 }
4265
NotifyDisplayIdChange(DisplayId displayId)4266 WSError WindowSessionImpl::NotifyDisplayIdChange(DisplayId displayId)
4267 {
4268 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "id=%{public}u, displayId=%{public}" PRIu64, GetPersistentId(), displayId);
4269 std::lock_guard<std::mutex> lock(displayIdChangeListenerMutex_);
4270 auto displayIdChangeListeners = GetListeners<IDisplayIdChangeListener>();
4271 for (auto& listener : displayIdChangeListeners) {
4272 if (listener != nullptr) {
4273 listener->OnDisplayIdChanged(displayId);
4274 }
4275 }
4276 return WSError::WS_OK;
4277 }
4278
NotifyWatchGestureConsumeResult(int32_t keyCode,bool isConsumed)4279 WMError WindowSessionImpl::NotifyWatchGestureConsumeResult(int32_t keyCode, bool isConsumed)
4280 {
4281 TLOGD(WmsLogTag::WMS_EVENT, "keyCode:%{public}d, isConsumed:%{public}d", keyCode, isConsumed);
4282 if (IsWindowSessionInvalid()) {
4283 return WMError::WM_ERROR_INVALID_WINDOW;
4284 }
4285 return SingletonContainer::Get<WindowAdapter>().NotifyWatchGestureConsumeResult(keyCode, isConsumed);
4286 }
4287
GetWatchGestureConsumed() const4288 bool WindowSessionImpl::GetWatchGestureConsumed() const
4289 {
4290 return isWatchGestureConsumed_;
4291 }
4292
SetWatchGestureConsumed(bool isWatchGestureConsumed)4293 void WindowSessionImpl::SetWatchGestureConsumed(bool isWatchGestureConsumed)
4294 {
4295 TLOGD(WmsLogTag::WMS_EVENT, "wid:%{public}d, isWatchGestureConsumed:%{public}d",
4296 GetPersistentId(), isWatchGestureConsumed);
4297 isWatchGestureConsumed_ = isWatchGestureConsumed;
4298 }
4299
NotifySystemDensityChange(float density)4300 WSError WindowSessionImpl::NotifySystemDensityChange(float density)
4301 {
4302 std::lock_guard<std::mutex> lock(systemDensityChangeListenerMutex_);
4303 const auto& systemDensityChangeListeners = GetListeners<ISystemDensityChangeListener>();
4304 for (const auto& listener : systemDensityChangeListeners) {
4305 if (listener != nullptr) {
4306 listener->OnSystemDensityChanged(density);
4307 }
4308 }
4309 return WSError::WS_OK;
4310 }
4311
NotifyWindowVisibility(bool isVisible)4312 WSError WindowSessionImpl::NotifyWindowVisibility(bool isVisible)
4313 {
4314 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "window: name=%{public}s, id=%{public}u, isVisible=%{public}d",
4315 GetWindowName().c_str(), GetPersistentId(), isVisible);
4316 std::lock_guard<std::recursive_mutex> lockListener(windowVisibilityChangeListenerMutex_);
4317 auto windowVisibilityListeners = GetListeners<IWindowVisibilityChangedListener>();
4318 for (auto& listener : windowVisibilityListeners) {
4319 if (listener != nullptr) {
4320 listener->OnWindowVisibilityChangedCallback(isVisible);
4321 }
4322 }
4323 return WSError::WS_OK;
4324 }
4325
NotifyNoInteractionTimeout(const IWindowNoInteractionListenerSptr & listener)4326 WSError WindowSessionImpl::NotifyNoInteractionTimeout(const IWindowNoInteractionListenerSptr& listener)
4327 {
4328 if (listener == nullptr) {
4329 WLOGFE("invalid listener");
4330 return WSError::WS_ERROR_NULLPTR;
4331 }
4332 WLOGFD("name=%{public}s, id=%{public}u, timeout=%{public}" PRId64,
4333 GetWindowName().c_str(), GetPersistentId(), listener->GetTimeout());
4334
4335 listener->OnWindowNoInteractionCallback();
4336 return WSError::WS_OK;
4337 }
4338
NotifyPointerEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)4339 void WindowSessionImpl::NotifyPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
4340 {
4341 if (!pointerEvent) {
4342 TLOGE(WmsLogTag::WMS_INPUT_KEY_FLOW, "Pointer event is nullptr");
4343 return;
4344 }
4345
4346 std::shared_ptr<IInputEventConsumer> inputEventConsumer;
4347 {
4348 std::lock_guard<std::recursive_mutex> lock(mutex_);
4349 inputEventConsumer = inputEventConsumer_;
4350 }
4351 if (inputEventConsumer != nullptr) {
4352 WLOGFD("Transfer pointer event to inputEventConsumer");
4353 if (pointerEvent->GetPointerAction() != MMI::PointerEvent::POINTER_ACTION_MOVE) {
4354 TLOGI(WmsLogTag::WMS_INPUT_KEY_FLOW,
4355 "Transfer to inputEventConsumer InputTracking id:%{public}d",
4356 pointerEvent->GetId());
4357 }
4358 if (!(inputEventConsumer->OnInputEvent(pointerEvent))) {
4359 pointerEvent->MarkProcessed();
4360 }
4361 return;
4362 }
4363 if (FilterPointerEvent(pointerEvent)) {
4364 return;
4365 }
4366 if (auto uiContent = GetUIContentSharedPtr()) {
4367 if (pointerEvent->GetPointerAction() != MMI::PointerEvent::POINTER_ACTION_MOVE) {
4368 TLOGD(WmsLogTag::WMS_EVENT, "eid:%{public}d", pointerEvent->GetId());
4369 }
4370 if (IsWindowDelayRaiseEnabled()) {
4371 pointerEvent->MarkProcessed();
4372 return;
4373 }
4374 TLOGD(WmsLogTag::WMS_EVENT, "Start to process pointerEvent, id: %{public}d", pointerEvent->GetId());
4375 if (!uiContent->ProcessPointerEvent(pointerEvent)) {
4376 TLOGI(WmsLogTag::WMS_INPUT_KEY_FLOW, "UI content dose not consume");
4377 pointerEvent->MarkProcessed();
4378 }
4379 } else {
4380 if (pointerEvent->GetPointerAction() != MMI::PointerEvent::POINTER_ACTION_MOVE) {
4381 TLOGW(WmsLogTag::WMS_INPUT_KEY_FLOW, "pointerEvent not consumed, windowId:%{public}u", GetWindowId());
4382 }
4383 pointerEvent->MarkProcessed();
4384 }
4385 }
4386
SetKeyEventFilter(KeyEventFilterFunc filter)4387 WMError WindowSessionImpl::SetKeyEventFilter(KeyEventFilterFunc filter)
4388 {
4389 std::unique_lock<std::mutex> lock(keyEventFilterMutex_);
4390 keyEventFilter_ = std::move(filter);
4391 return WMError::WM_OK;
4392 }
4393
ClearKeyEventFilter()4394 WMError WindowSessionImpl::ClearKeyEventFilter()
4395 {
4396 std::unique_lock<std::mutex> lock(keyEventFilterMutex_);
4397 keyEventFilter_ = nullptr;
4398 return WMError::WM_OK;
4399 }
4400
SetMouseEventFilter(MouseEventFilterFunc filter)4401 WMError WindowSessionImpl::SetMouseEventFilter(MouseEventFilterFunc filter)
4402 {
4403 std::unique_lock<std::mutex> lock(mouseEventFilterMutex_);
4404 mouseEventFilter_ = std::move(filter);
4405 return WMError::WM_OK;
4406 }
4407
ClearMouseEventFilter()4408 WMError WindowSessionImpl::ClearMouseEventFilter()
4409 {
4410 std::unique_lock<std::mutex> lock(mouseEventFilterMutex_);
4411 mouseEventFilter_ = nullptr;
4412 return WMError::WM_OK;
4413 }
4414
SetTouchEventFilter(TouchEventFilterFunc filter)4415 WMError WindowSessionImpl::SetTouchEventFilter(TouchEventFilterFunc filter)
4416 {
4417 std::unique_lock<std::mutex> lock(touchEventFilterMutex_);
4418 touchEventFilter_ = std::move(filter);
4419 return WMError::WM_OK;
4420 }
4421
ClearTouchEventFilter()4422 WMError WindowSessionImpl::ClearTouchEventFilter()
4423 {
4424 std::unique_lock<std::mutex> lock(touchEventFilterMutex_);
4425 touchEventFilter_ = nullptr;
4426 return WMError::WM_OK;
4427 }
4428
FilterKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent)4429 bool WindowSessionImpl::FilterKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
4430 {
4431 std::lock_guard<std::mutex> lock(keyEventFilterMutex_);
4432 if (keyEventFilter_ != nullptr) {
4433 bool isFilter = keyEventFilter_(*keyEvent.get());
4434 TLOGE(WmsLogTag::WMS_SYSTEM, "keyCode:%{public}d isFilter:%{public}d",
4435 keyEvent->GetKeyCode(), isFilter);
4436 if (isFilter) {
4437 keyEvent->MarkProcessed();
4438 return true;
4439 }
4440 }
4441 return false;
4442 }
4443
NotifyConsumeResultToFloatWindow(const std::shared_ptr<MMI::KeyEvent> & keyEvent,bool isConsumed)4444 void WindowSessionImpl::NotifyConsumeResultToFloatWindow
4445 (const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool isConsumed)
4446 {
4447 if ((keyEvent->GetKeyCode() == MMI::KeyEvent::KEYCODE_TAB ||
4448 keyEvent->GetKeyCode() == MMI::KeyEvent::KEYCODE_ENTER) && !GetWatchGestureConsumed() &&
4449 keyEvent->GetKeyAction() == MMI::KeyEvent::KEY_ACTION_DOWN) {
4450 TLOGD(WmsLogTag::WMS_EVENT, "wid:%{public}d, keyCode:%{public}d, isConsumed:%{public}d",
4451 GetWindowId(), keyEvent->GetKeyCode(), isConsumed);
4452 NotifyWatchGestureConsumeResult(keyEvent->GetKeyCode(), isConsumed);
4453 }
4454 }
4455
FilterPointerEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)4456 bool WindowSessionImpl::FilterPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
4457 {
4458 bool isFiltered = false;
4459 auto sourceType = pointerEvent->GetSourceType();
4460 auto action = pointerEvent->GetPointerAction();
4461 if (sourceType == OHOS::MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
4462 std::lock_guard<std::mutex> lock(touchEventFilterMutex_);
4463 if (touchEventFilter_ == nullptr) {
4464 return false;
4465 }
4466 isFiltered = touchEventFilter_(*pointerEvent.get());
4467 } else if (sourceType == OHOS::MMI::PointerEvent::SOURCE_TYPE_MOUSE &&
4468 (action != OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_BEGIN &&
4469 action != OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_UPDATE &&
4470 action != OHOS::MMI::PointerEvent::POINTER_ACTION_AXIS_END)) {
4471 std::lock_guard<std::mutex> lock(mouseEventFilterMutex_);
4472 if (mouseEventFilter_ == nullptr) {
4473 return false;
4474 }
4475 isFiltered = mouseEventFilter_(*pointerEvent.get());
4476 }
4477 if (isFiltered) {
4478 pointerEvent->MarkProcessed();
4479 }
4480 return isFiltered;
4481 }
4482
DispatchKeyEventCallback(const std::shared_ptr<MMI::KeyEvent> & keyEvent,bool & isConsumed)4483 void WindowSessionImpl::DispatchKeyEventCallback(const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool& isConsumed)
4484 {
4485 std::shared_ptr<IInputEventConsumer> inputEventConsumer;
4486 {
4487 std::lock_guard<std::recursive_mutex> lock(mutex_);
4488 inputEventConsumer = inputEventConsumer_;
4489 }
4490 int32_t keyCode = keyEvent->GetKeyCode();
4491 int32_t keyAction = keyEvent->GetKeyAction();
4492 if (keyCode == MMI::KeyEvent::KEYCODE_BACK && keyAction == MMI::KeyEvent::KEY_ACTION_UP) {
4493 WLOGFI("event consumed by back");
4494 if (inputEventConsumer != nullptr) {
4495 WLOGFD("Transfer key event to inputEventConsumer");
4496 if (inputEventConsumer->OnInputEvent(keyEvent)) {
4497 return;
4498 }
4499 PerformBack();
4500 keyEvent->MarkProcessed();
4501 return;
4502 }
4503 HandleBackEvent();
4504 keyEvent->MarkProcessed();
4505 return;
4506 }
4507
4508 if (inputEventConsumer != nullptr) {
4509 WLOGD("Transfer key event to inputEventConsumer");
4510 if (!(inputEventConsumer->OnInputEvent(keyEvent))) {
4511 keyEvent->MarkProcessed();
4512 }
4513 return;
4514 }
4515
4516 if (auto uiContent = GetUIContentSharedPtr()) {
4517 if (FilterKeyEvent(keyEvent)) return;
4518 TLOGI(WmsLogTag::WMS_EVENT, "Start to process keyEvent, id: %{public}d", keyEvent->GetId());
4519 isConsumed = uiContent->ProcessKeyEvent(keyEvent);
4520 if (!isConsumed && keyEvent->GetKeyCode() == MMI::KeyEvent::KEYCODE_ESCAPE &&
4521 IsPcOrPadFreeMultiWindowMode() &&
4522 property_->GetWindowMode() == WindowMode::WINDOW_MODE_FULLSCREEN &&
4523 GetImmersiveModeEnabledState() &&
4524 keyAction == MMI::KeyEvent::KEY_ACTION_DOWN && !escKeyEventTriggered_) {
4525 WLOGI("recover from fullscreen cause KEYCODE_ESCAPE");
4526 Recover();
4527 }
4528 NotifyConsumeResultToFloatWindow(keyEvent, isConsumed);
4529 if (!isConsumed) {
4530 keyEvent->MarkProcessed();
4531 }
4532 if (keyEvent->GetKeyCode() == MMI::KeyEvent::KEYCODE_ESCAPE) {
4533 escKeyEventTriggered_ = (keyAction == MMI::KeyEvent::KEY_ACTION_UP) ? false : true;
4534 }
4535 }
4536 }
4537
HandleBackEvent()4538 WSError WindowSessionImpl::HandleBackEvent()
4539 {
4540 TLOGI(WmsLogTag::WMS_EVENT, "in");
4541 bool isConsumed = false;
4542 std::shared_ptr<IInputEventConsumer> inputEventConsumer;
4543 {
4544 std::lock_guard<std::recursive_mutex> lock(mutex_);
4545 inputEventConsumer = inputEventConsumer_;
4546 }
4547 if (inputEventConsumer != nullptr) {
4548 WLOGFD("Transfer back event to inputEventConsumer");
4549 std::shared_ptr<MMI::KeyEvent> backKeyEvent = MMI::KeyEvent::Create();
4550 if (backKeyEvent == nullptr) {
4551 WLOGFE("backKeyEvent is null");
4552 return WSError::WS_ERROR_NULLPTR;
4553 }
4554 backKeyEvent->SetKeyCode(MMI::KeyEvent::KEYCODE_BACK);
4555 backKeyEvent->SetKeyAction(MMI::KeyEvent::KEY_ACTION_UP);
4556 isConsumed = inputEventConsumer->OnInputEvent(backKeyEvent);
4557 } else {
4558 if (auto uiContent = GetUIContentSharedPtr()) {
4559 WLOGFD("Transfer back event to uiContent");
4560 isConsumed = uiContent->ProcessBackPressed();
4561 } else {
4562 WLOGFE("There is no back event consumer");
4563 }
4564 }
4565
4566 if (isConsumed) {
4567 WLOGD("Back key event is consumed");
4568 return WSError::WS_OK;
4569 }
4570 WLOGFD("report Back");
4571 SingletonContainer::Get<WindowInfoReporter>().ReportBackButtonInfoImmediately();
4572 if (handler_ == nullptr) {
4573 WLOGFE("HandleBackEvent handler_ is nullptr!");
4574 return WSError::WS_ERROR_INVALID_PARAM;
4575 }
4576 // notify back event to host session
4577 wptr<WindowSessionImpl> weak = this;
4578 auto task = [weak]() {
4579 auto weakSession = weak.promote();
4580 if (weakSession == nullptr) {
4581 WLOGFE("HandleBackEvent session wptr is nullptr");
4582 return;
4583 }
4584 weakSession->PerformBack();
4585 };
4586 if (!handler_->PostTask(task, "wms:PerformBack")) {
4587 WLOGFE("Failed to post PerformBack");
4588 return WSError::WS_ERROR_INVALID_OPERATION;
4589 }
4590 return WSError::WS_OK;
4591 }
4592
NotifyKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,bool & isConsumed,bool notifyInputMethod)4593 void WindowSessionImpl::NotifyKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool& isConsumed,
4594 bool notifyInputMethod)
4595 {
4596 if (keyEvent == nullptr) {
4597 WLOGFE("keyEvent is nullptr");
4598 return;
4599 }
4600
4601 #ifdef IMF_ENABLE
4602 bool isKeyboardEvent = IsKeyboardEvent(keyEvent);
4603 if (isKeyboardEvent && notifyInputMethod) {
4604 WLOGD("Async dispatch keyEvent to input method");
4605 auto callback = [weakThis = wptr(this)] (std::shared_ptr<MMI::KeyEvent>& keyEvent, bool consumed) {
4606 if (keyEvent == nullptr) {
4607 WLOGFW("keyEvent is null, consumed:%{public}" PRId32, consumed);
4608 return;
4609 }
4610
4611 if (consumed) {
4612 WLOGD("Input method has processed key event, id:%{public}" PRId32, keyEvent->GetId());
4613 return;
4614 }
4615
4616 auto promoteThis = weakThis.promote();
4617 if (promoteThis == nullptr) {
4618 WLOGFW("promoteThis is nullptr");
4619 keyEvent->MarkProcessed();
4620 return;
4621 }
4622 bool isConsumed = false;
4623 promoteThis->DispatchKeyEventCallback(keyEvent, isConsumed);
4624 };
4625 auto ret = MiscServices::InputMethodController::GetInstance()->DispatchKeyEvent(
4626 const_cast<std::shared_ptr<MMI::KeyEvent>&>(keyEvent), callback);
4627 if (ret != 0) {
4628 WLOGFE("DispatchKeyEvent failed, ret:%{public}" PRId32 ", id:%{public}" PRId32, ret, keyEvent->GetId());
4629 DispatchKeyEventCallback(keyEvent, isConsumed);
4630 }
4631 return;
4632 }
4633 #endif // IMF_ENABLE
4634 DispatchKeyEventCallback(keyEvent, isConsumed);
4635 }
4636
IsKeyboardEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent) const4637 bool WindowSessionImpl::IsKeyboardEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent) const
4638 {
4639 int32_t keyCode = keyEvent->GetKeyCode();
4640 bool isKeyFN = (keyCode == MMI::KeyEvent::KEYCODE_FN);
4641 bool isKeyBack = (keyCode == MMI::KeyEvent::KEYCODE_BACK);
4642 bool isKeyboard = (keyCode >= MMI::KeyEvent::KEYCODE_0 && keyCode <= MMI::KeyEvent::KEYCODE_NUMPAD_RIGHT_PAREN);
4643 bool isKeySound = (keyCode == MMI::KeyEvent::KEYCODE_SOUND);
4644 TLOGD(WmsLogTag::WMS_EVENT, "isKeyFN:%{public}d, isKeyboard:%{public}d", isKeyFN, isKeyboard);
4645 return (isKeyFN || isKeyboard || isKeyBack || isKeySound);
4646 }
4647
RequestVsync(const std::shared_ptr<VsyncCallback> & vsyncCallback)4648 void WindowSessionImpl::RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback)
4649 {
4650 if (vsyncStation_ == nullptr) {
4651 TLOGW(WmsLogTag::WMS_MAIN, "vsyncStation is null");
4652 return;
4653 }
4654 vsyncStation_->RequestVsync(vsyncCallback);
4655 }
4656
GetVSyncPeriod()4657 int64_t WindowSessionImpl::GetVSyncPeriod()
4658 {
4659 if (vsyncStation_ == nullptr) {
4660 TLOGW(WmsLogTag::WMS_MAIN, "vsyncStation is null");
4661 return 0;
4662 }
4663 return vsyncStation_->GetVSyncPeriod();
4664 }
4665
FlushFrameRate(uint32_t rate,int32_t animatorExpectedFrameRate,uint32_t rateType)4666 void WindowSessionImpl::FlushFrameRate(uint32_t rate, int32_t animatorExpectedFrameRate, uint32_t rateType)
4667 {
4668 if (vsyncStation_ == nullptr) {
4669 TLOGW(WmsLogTag::WMS_MAIN, "vsyncStation is null");
4670 return;
4671 }
4672 vsyncStation_->FlushFrameRate(rate, animatorExpectedFrameRate, rateType);
4673 }
4674
UpdateProperty(WSPropertyChangeAction action)4675 WMError WindowSessionImpl::UpdateProperty(WSPropertyChangeAction action)
4676 {
4677 TLOGD(WmsLogTag::DEFAULT, "action:%{public}" PRIu64, action);
4678 if (IsWindowSessionInvalid()) {
4679 TLOGE(WmsLogTag::DEFAULT, "session is invalid");
4680 return WMError::WM_ERROR_INVALID_WINDOW;
4681 }
4682 auto hostSession = GetHostSession();
4683 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, WMError::WM_ERROR_INVALID_WINDOW);
4684 return hostSession->UpdateSessionPropertyByAction(property_, action);
4685 }
4686
Find(const std::string & name)4687 sptr<Window> WindowSessionImpl::Find(const std::string& name)
4688 {
4689 std::shared_lock<std::shared_mutex> lock(windowSessionMutex_);
4690 auto iter = windowSessionMap_.find(name);
4691 if (iter == windowSessionMap_.end()) {
4692 TLOGE(WmsLogTag::DEFAULT, "Can not find window %{public}s", name.c_str());
4693 return nullptr;
4694 }
4695 return iter->second.second;
4696 }
4697
SetAceAbilityHandler(const sptr<IAceAbilityHandler> & handler)4698 void WindowSessionImpl::SetAceAbilityHandler(const sptr<IAceAbilityHandler>& handler)
4699 {
4700 if (handler == nullptr) {
4701 WLOGE("ace ability handler is nullptr");
4702 }
4703 std::lock_guard<std::recursive_mutex> lock(mutex_);
4704 aceAbilityHandler_ = handler;
4705 }
4706
SetBackgroundColor(const std::string & color)4707 WMError WindowSessionImpl::SetBackgroundColor(const std::string& color)
4708 {
4709 if (IsWindowSessionInvalid()) {
4710 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "session is invalid");
4711 return WMError::WM_ERROR_INVALID_WINDOW;
4712 }
4713 uint32_t colorValue;
4714 if (ColorParser::Parse(color, colorValue)) {
4715 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "window: %{public}s, value: [%{public}s, %{public}u]",
4716 GetWindowName().c_str(), color.c_str(), colorValue);
4717 return SetBackgroundColor(colorValue);
4718 }
4719 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "invalid color string: %{public}s", color.c_str());
4720 return WMError::WM_ERROR_INVALID_PARAM;
4721 }
4722
SetBackgroundColor(uint32_t color)4723 WMError WindowSessionImpl::SetBackgroundColor(uint32_t color)
4724 {
4725 TLOGI(WmsLogTag::WMS_ATTRIBUTE, "window: %{public}s, value:%{public}u", GetWindowName().c_str(), color);
4726
4727 // 0xff000000: ARGB style, means Opaque color.
4728 const bool isAlphaZero = !(color & 0xff000000);
4729 std::string bundleName;
4730 std::string abilityName;
4731 if ((context_ != nullptr) && (context_->GetApplicationInfo() != nullptr)) {
4732 bundleName = context_->GetBundleName();
4733 abilityName = context_->GetApplicationInfo()->name;
4734 }
4735
4736 if (isAlphaZero && WindowHelper::IsMainWindow(GetType())) {
4737 auto& reportInstance = SingletonContainer::Get<WindowInfoReporter>();
4738 reportInstance.ReportZeroOpacityInfoImmediately(bundleName, abilityName);
4739 }
4740
4741 if (auto uiContent = GetUIContentSharedPtr()) {
4742 uiContent->SetBackgroundColor(color);
4743 // 24: Shift right by 24 bits to move the alpha channel to the lowest 8 bits.
4744 uint8_t alpha = static_cast<uint8_t>((color >> 24) & 0xff);
4745 property_->SetBackgroundAlpha(alpha);
4746 UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_BACKGROUND_ALPHA);
4747 return WMError::WM_OK;
4748 }
4749
4750 if (aceAbilityHandler_ != nullptr) {
4751 aceAbilityHandler_->SetBackgroundColor(color);
4752 return WMError::WM_OK;
4753 }
4754 TLOGD(WmsLogTag::WMS_ATTRIBUTE, "FA mode could not set bg color: %{public}u", GetWindowId());
4755 return WMError::WM_ERROR_INVALID_OPERATION;
4756 }
4757
FindWindowById(uint32_t winId)4758 sptr<Window> WindowSessionImpl::FindWindowById(uint32_t winId)
4759 {
4760 std::shared_lock<std::shared_mutex> lock(windowSessionMutex_);
4761 if (windowSessionMap_.empty()) {
4762 WLOGFE("Please create mainWindow First!");
4763 return nullptr;
4764 }
4765 for (auto iter = windowSessionMap_.begin(); iter != windowSessionMap_.end(); iter++) {
4766 if (static_cast<int32_t>(winId) == iter->second.first) {
4767 WLOGD("FindWindow id: %{public}u", winId);
4768 return iter->second.second;
4769 }
4770 }
4771 WLOGFE("Cannot find Window, id: %{public}d", winId);
4772 return nullptr;
4773 }
4774
GetSubWindow(int parentId)4775 std::vector<sptr<Window>> WindowSessionImpl::GetSubWindow(int parentId)
4776 {
4777 std::lock_guard<std::recursive_mutex> lock(subWindowSessionMutex_);
4778 auto iter = subWindowSessionMap_.find(parentId);
4779 if (iter == subWindowSessionMap_.end()) {
4780 return std::vector<sptr<Window>>();
4781 }
4782 return std::vector<sptr<Window>>(subWindowSessionMap_[parentId].begin(), subWindowSessionMap_[parentId].end());
4783 }
4784
GetBackgroundColor() const4785 uint32_t WindowSessionImpl::GetBackgroundColor() const
4786 {
4787 if (auto uiContent = GetUIContentSharedPtr()) {
4788 return uiContent->GetBackgroundColor();
4789 }
4790 WLOGD("uiContent is null, windowId: %{public}u, use FA mode", GetWindowId());
4791 if (aceAbilityHandler_ != nullptr) {
4792 return aceAbilityHandler_->GetBackgroundColor();
4793 }
4794 WLOGFD("FA mode does not get bg color: %{public}u", GetWindowId());
4795 return 0xffffffff; // means no background color been set, default color is white
4796 }
4797
SetLayoutFullScreenByApiVersion(bool status)4798 WMError WindowSessionImpl::SetLayoutFullScreenByApiVersion(bool status)
4799 {
4800 return WMError::WM_OK;
4801 }
4802
UpdateSystemBarProperties(const std::unordered_map<WindowType,SystemBarProperty> & systemBarProperties,const std::unordered_map<WindowType,SystemBarPropertyFlag> & systemBarPropertyFlags)4803 WMError WindowSessionImpl::UpdateSystemBarProperties(
4804 const std::unordered_map<WindowType, SystemBarProperty>& systemBarProperties,
4805 const std::unordered_map<WindowType, SystemBarPropertyFlag>& systemBarPropertyFlags)
4806 {
4807 return WMError::WM_OK;
4808 }
4809
SetSystemBarProperty(WindowType type,const SystemBarProperty & property)4810 WMError WindowSessionImpl::SetSystemBarProperty(WindowType type, const SystemBarProperty& property)
4811 {
4812 return WMError::WM_OK;
4813 }
4814
UpdateSpecificSystemBarEnabled(bool systemBarEnable,bool systemBarEnableAnimation,SystemBarProperty & property)4815 void WindowSessionImpl::UpdateSpecificSystemBarEnabled(bool systemBarEnable, bool systemBarEnableAnimation,
4816 SystemBarProperty& property)
4817 {
4818 property.enable_ = systemBarEnable;
4819 property.enableAnimation_ = systemBarEnableAnimation;
4820 // isolate on api 18
4821 if (GetTargetAPIVersion() >= API_VERSION_18) {
4822 property.settingFlag_ |= SystemBarSettingFlag::ENABLE_SETTING;
4823 }
4824 }
4825
SetSpecificBarProperty(WindowType type,const SystemBarProperty & property)4826 WMError WindowSessionImpl::SetSpecificBarProperty(WindowType type, const SystemBarProperty& property)
4827 {
4828 return WMError::WM_OK;
4829 }
4830
NotifyOccupiedAreaChangeInfoInner(sptr<OccupiedAreaChangeInfo> info)4831 void WindowSessionImpl::NotifyOccupiedAreaChangeInfoInner(sptr<OccupiedAreaChangeInfo> info)
4832 {
4833 std::lock_guard<std::recursive_mutex> lockListener(occupiedAreaChangeListenerMutex_);
4834 auto occupiedAreaChangeListeners = GetListeners<IOccupiedAreaChangeListener>();
4835 for (auto& listener : occupiedAreaChangeListeners) {
4836 if (listener != nullptr) {
4837 listener->OnSizeChange(info);
4838 }
4839 }
4840 }
4841
NotifyOccupiedAreaChangeInfo(sptr<OccupiedAreaChangeInfo> info,const std::shared_ptr<RSTransaction> & rsTransaction)4842 void WindowSessionImpl::NotifyOccupiedAreaChangeInfo(sptr<OccupiedAreaChangeInfo> info,
4843 const std::shared_ptr<RSTransaction>& rsTransaction)
4844 {
4845 TLOGI(WmsLogTag::WMS_KEYBOARD, "transaction: %{public}d, safeHeight: %{public}u"
4846 ", occupied rect: x %{public}u, y %{public}u, w %{public}u, h %{public}u", rsTransaction != nullptr,
4847 info->safeHeight_, info->rect_.posX_, info->rect_.posY_, info->rect_.width_, info->rect_.height_);
4848 if (handler_ == nullptr) {
4849 TLOGE(WmsLogTag::WMS_KEYBOARD, "handler is nullptr");
4850 return;
4851 }
4852 auto task = [weak = wptr(this), info, rsTransaction]() {
4853 auto window = weak.promote();
4854 if (!window) {
4855 TLOGNE(WmsLogTag::WMS_KEYBOARD, "window is nullptr, notify occupied area change info failed");
4856 return;
4857 }
4858 if (rsTransaction) {
4859 RSTransaction::FlushImplicitTransaction();
4860 rsTransaction->Begin();
4861 }
4862 window->NotifyOccupiedAreaChangeInfoInner(info);
4863 if (rsTransaction) {
4864 rsTransaction->Commit();
4865 }
4866 };
4867 handler_->PostTask(task, "WMS_WindowSessionImpl_NotifyOccupiedAreaChangeInfo");
4868 }
4869
NotifyKeyboardDidShow(const KeyboardPanelInfo & keyboardPanelInfo)4870 void WindowSessionImpl::NotifyKeyboardDidShow(const KeyboardPanelInfo& keyboardPanelInfo)
4871 {
4872 std::lock_guard<std::recursive_mutex> lockListener(keyboardDidShowListenerMutex_);
4873 auto keyboardDidShowListeners = GetListeners<IKeyboardDidShowListener>();
4874 for (const auto& listener : keyboardDidShowListeners) {
4875 if (listener != nullptr) {
4876 listener->OnKeyboardDidShow(keyboardPanelInfo);
4877 }
4878 }
4879 }
4880
NotifyKeyboardDidHide(const KeyboardPanelInfo & keyboardPanelInfo)4881 void WindowSessionImpl::NotifyKeyboardDidHide(const KeyboardPanelInfo& keyboardPanelInfo)
4882 {
4883 std::lock_guard<std::recursive_mutex> lockListener(keyboardDidHideListenerMutex_);
4884 auto keyboardDidHideListeners = GetListeners<IKeyboardDidHideListener>();
4885 for (const auto& listener : keyboardDidHideListeners) {
4886 if (listener != nullptr) {
4887 listener->OnKeyboardDidHide(keyboardPanelInfo);
4888 }
4889 }
4890 }
4891
NotifyKeyboardAnimationCompleted(const KeyboardPanelInfo & keyboardPanelInfo)4892 void WindowSessionImpl::NotifyKeyboardAnimationCompleted(const KeyboardPanelInfo& keyboardPanelInfo)
4893 {
4894 TLOGI(WmsLogTag::WMS_KEYBOARD, "isShowAnimation: %{public}d, beginRect: %{public}s, endRect: %{public}s",
4895 keyboardPanelInfo.isShowing_, keyboardPanelInfo.beginRect_.ToString().c_str(),
4896 keyboardPanelInfo.endRect_.ToString().c_str());
4897 if (handler_ == nullptr) {
4898 TLOGE(WmsLogTag::WMS_KEYBOARD, "handler is nullptr");
4899 return;
4900 }
4901 auto task = [weak = wptr(this), keyboardPanelInfo]() {
4902 auto window = weak.promote();
4903 if (!window) {
4904 TLOGNE(WmsLogTag::WMS_KEYBOARD, "window is nullptr, notify keyboard animation completed failed.");
4905 return;
4906 }
4907 if (keyboardPanelInfo.isShowing_) {
4908 window->NotifyKeyboardDidShow(keyboardPanelInfo);
4909 } else {
4910 window->NotifyKeyboardDidHide(keyboardPanelInfo);
4911 }
4912 };
4913 handler_->PostTask(task, __func__);
4914 }
4915
GetKeyboardAnimationConfig()4916 KeyboardAnimationConfig WindowSessionImpl::GetKeyboardAnimationConfig()
4917 {
4918 return { windowSystemConfig_.animationIn_, windowSystemConfig_.animationOut_ };
4919 }
4920
DumpSessionElementInfo(const std::vector<std::string> & params)4921 void WindowSessionImpl::DumpSessionElementInfo(const std::vector<std::string>& params)
4922 {
4923 WLOGFD("in");
4924 }
4925
UpdateMaximizeMode(MaximizeMode mode)4926 WSError WindowSessionImpl::UpdateMaximizeMode(MaximizeMode mode)
4927 {
4928 return WSError::WS_OK;
4929 }
4930
TransferAccessibilityEvent(const Accessibility::AccessibilityEventInfo & info,int64_t uiExtensionIdLevel)4931 WMError WindowSessionImpl::TransferAccessibilityEvent(const Accessibility::AccessibilityEventInfo& info,
4932 int64_t uiExtensionIdLevel)
4933 {
4934 return WMError::WM_OK;
4935 }
4936
NotifySessionForeground(uint32_t reason,bool withAnimation)4937 void WindowSessionImpl::NotifySessionForeground(uint32_t reason, bool withAnimation)
4938 {
4939 WLOGFD("in");
4940 }
4941
NotifySessionBackground(uint32_t reason,bool withAnimation,bool isFromInnerkits)4942 void WindowSessionImpl::NotifySessionBackground(uint32_t reason, bool withAnimation, bool isFromInnerkits)
4943 {
4944 WLOGFD("in");
4945 }
4946
UpdateTitleInTargetPos(bool isShow,int32_t height)4947 WSError WindowSessionImpl::UpdateTitleInTargetPos(bool isShow, int32_t height)
4948 {
4949 return WSError::WS_OK;
4950 }
4951
SwitchFreeMultiWindow(bool enable)4952 WSError WindowSessionImpl::SwitchFreeMultiWindow(bool enable)
4953 {
4954 return WSError::WS_OK;
4955 }
4956
NotifyDialogStateChange(bool isForeground)4957 WSError WindowSessionImpl::NotifyDialogStateChange(bool isForeground)
4958 {
4959 return WSError::WS_OK;
4960 }
4961
UpdatePiPRect(const Rect & rect,WindowSizeChangeReason reason)4962 void WindowSessionImpl::UpdatePiPRect(const Rect& rect, WindowSizeChangeReason reason)
4963 {
4964 if (IsWindowSessionInvalid()) {
4965 WLOGFE("session is invalid");
4966 return;
4967 }
4968 auto hostSession = GetHostSession();
4969 CHECK_HOST_SESSION_RETURN_IF_NULL(hostSession);
4970 hostSession->UpdatePiPRect(rect, static_cast<SizeChangeReason>(reason));
4971 }
4972
UpdatePiPControlStatus(PiPControlType controlType,PiPControlStatus status)4973 void WindowSessionImpl::UpdatePiPControlStatus(PiPControlType controlType, PiPControlStatus status)
4974 {
4975 TLOGI(WmsLogTag::WMS_PIP, "controlType:%{public}u, status:%{public}d", controlType, status);
4976 if (IsWindowSessionInvalid()) {
4977 TLOGE(WmsLogTag::WMS_PIP, "HostSession is invalid");
4978 return;
4979 }
4980 auto hostSession = GetHostSession();
4981 CHECK_HOST_SESSION_RETURN_IF_NULL(hostSession);
4982 hostSession->UpdatePiPControlStatus(static_cast<WsPiPControlType>(controlType),
4983 static_cast<WsPiPControlStatus>(status));
4984 }
4985
SetAutoStartPiP(bool isAutoStart,uint32_t priority,uint32_t width,uint32_t height)4986 void WindowSessionImpl::SetAutoStartPiP(bool isAutoStart, uint32_t priority, uint32_t width, uint32_t height)
4987 {
4988 if (IsWindowSessionInvalid()) {
4989 TLOGE(WmsLogTag::WMS_PIP, "session is invalid");
4990 return;
4991 }
4992 if (auto hostSession = GetHostSession()) {
4993 hostSession->SetAutoStartPiP(isAutoStart, priority, width, height);
4994 }
4995 }
4996
GetWindowStatusInner(WindowMode mode)4997 WindowStatus WindowSessionImpl::GetWindowStatusInner(WindowMode mode)
4998 {
4999 auto windowStatus = WindowStatus::WINDOW_STATUS_UNDEFINED;
5000 if (mode == WindowMode::WINDOW_MODE_FLOATING) {
5001 windowStatus = WindowStatus::WINDOW_STATUS_FLOATING;
5002 if (property_->GetMaximizeMode() == MaximizeMode::MODE_AVOID_SYSTEM_BAR) {
5003 windowStatus = WindowStatus::WINDOW_STATUS_MAXIMIZE;
5004 }
5005 } else if (mode == WindowMode::WINDOW_MODE_SPLIT_PRIMARY || mode == WindowMode::WINDOW_MODE_SPLIT_SECONDARY) {
5006 windowStatus = WindowStatus::WINDOW_STATUS_SPLITSCREEN;
5007 }
5008 if (mode == WindowMode::WINDOW_MODE_FULLSCREEN) {
5009 if (IsPcOrPadFreeMultiWindowMode() && GetTargetAPIVersion() >= 14) { // 14: isolated version
5010 windowStatus = GetImmersiveModeEnabledState() ? WindowStatus::WINDOW_STATUS_FULLSCREEN :
5011 WindowStatus::WINDOW_STATUS_MAXIMIZE;
5012 } else {
5013 windowStatus = WindowStatus::WINDOW_STATUS_FULLSCREEN;
5014 }
5015 }
5016 if (state_ == WindowState::STATE_HIDDEN) {
5017 windowStatus = WindowStatus::WINDOW_STATUS_MINIMIZE;
5018 }
5019 return windowStatus;
5020 }
5021
GetStatusBarHeight()5022 uint32_t WindowSessionImpl::GetStatusBarHeight()
5023 {
5024 uint32_t height = 0;
5025 auto hostSession = GetHostSession();
5026 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, height);
5027 height = static_cast<uint32_t>(hostSession->GetStatusBarHeight());
5028 TLOGI(WmsLogTag::WMS_IMMS, "height %{public}u", height);
5029 return height;
5030 }
5031
5032 /** @note @window.layout */
NotifyWindowStatusChange(WindowMode mode)5033 void WindowSessionImpl::NotifyWindowStatusChange(WindowMode mode)
5034 {
5035 auto windowStatus = GetWindowStatusInner(mode);
5036 TLOGD(WmsLogTag::WMS_LAYOUT, "WindowMode: %{public}d, windowStatus: %{public}d", mode, windowStatus);
5037 std::lock_guard<std::recursive_mutex> lockListener(windowStatusChangeListenerMutex_);
5038 auto windowStatusChangeListeners = GetListeners<IWindowStatusChangeListener>();
5039 for (auto& listener : windowStatusChangeListeners) {
5040 if (listener != nullptr) {
5041 listener->OnWindowStatusChange(windowStatus);
5042 }
5043 }
5044 if (state_ != WindowState::STATE_HIDDEN) {
5045 auto ret = HiSysEventWrite(
5046 HiviewDFX::HiSysEvent::Domain::WINDOW_MANAGER,
5047 "WINDOW_STATUS_CHANGE",
5048 HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
5049 "BUNDLE_NAME", property_->GetSessionInfo().bundleName_,
5050 "WINDOW_MODE", static_cast<int32_t>(mode));
5051 if (ret) {
5052 TLOGW(WmsLogTag::WMS_FOCUS, "write event fail, WINDOW_STATUS_CHANGE, ret=%{public}d", ret);
5053 }
5054 }
5055 }
5056
NotifyTransformChange(const Transform & transform)5057 void WindowSessionImpl::NotifyTransformChange(const Transform& transform)
5058 {
5059 WLOGFI("in");
5060 SetCurrentTransform(transform);
5061 if (auto uiContent = GetUIContentSharedPtr()) {
5062 uiContent->UpdateTransform(transform);
5063 SetLayoutTransform(transform);
5064 SetNeedRenotifyTransform(false);
5065 } else {
5066 SetNeedRenotifyTransform(true);
5067 }
5068 }
5069
NotifySingleHandTransformChange(const SingleHandTransform & singleHandTransform)5070 void WindowSessionImpl::NotifySingleHandTransformChange(const SingleHandTransform& singleHandTransform)
5071 {
5072 singleHandTransform_ = singleHandTransform;
5073 if (auto uiContent = GetUIContentSharedPtr()) {
5074 TLOGI(WmsLogTag::WMS_LAYOUT, "id:%{public}d, posX:%{public}d, posY:%{public}d, "
5075 "scaleX:%{public}f, scaleY:%{public}f", GetPersistentId(),
5076 singleHandTransform.posX, singleHandTransform.posY,
5077 singleHandTransform.scaleX, singleHandTransform.scaleY);
5078 uiContent->UpdateSingleHandTransform(singleHandTransform);
5079 } else {
5080 TLOGW(WmsLogTag::WMS_LAYOUT, "id:%{public}d, uiContent is nullptr", GetPersistentId());
5081 }
5082 }
5083
SubmitNoInteractionMonitorTask(int32_t eventId,const IWindowNoInteractionListenerSptr & listener)5084 void WindowSessionImpl::SubmitNoInteractionMonitorTask(int32_t eventId,
5085 const IWindowNoInteractionListenerSptr& listener)
5086 {
5087 auto task = [sessionWptr = wptr(this), eventId, listenerWptr = wptr(listener)]() {
5088 auto session = sessionWptr.promote();
5089 if (session == nullptr) {
5090 WLOGFE("windowInteractionMonitor task running failed, window session is null");
5091 return;
5092 }
5093 if (eventId != session->lastInteractionEventId_.load()) {
5094 WLOGFD("event id of windowInteractionMonitor has been changed, need not notify!");
5095 return;
5096 }
5097 if (session->state_ != WindowState::STATE_SHOWN) {
5098 WLOGFD("window state is not show, need not notify!");
5099 return;
5100 }
5101 session->NotifyNoInteractionTimeout(listenerWptr.promote());
5102 };
5103 handler_->PostTask(task, listener->GetTimeout());
5104 }
5105
RefreshNoInteractionTimeoutMonitor()5106 void WindowSessionImpl::RefreshNoInteractionTimeoutMonitor()
5107 {
5108 std::lock_guard<std::recursive_mutex> lockListener(windowNoInteractionListenerMutex_);
5109 if (windowNoInteractionListeners_[GetPersistentId()].empty()) {
5110 return;
5111 }
5112 this->lastInteractionEventId_.fetch_add(1);
5113 int32_t eventId = lastInteractionEventId_.load();
5114 auto noInteractionListeners = GetListeners<IWindowNoInteractionListener>();
5115 for (const auto& listenerItem : noInteractionListeners) {
5116 SubmitNoInteractionMonitorTask(eventId, listenerItem);
5117 }
5118 }
5119
IsUserOrientation(Orientation orientation) const5120 bool WindowSessionImpl::IsUserOrientation(Orientation orientation) const
5121 {
5122 if (orientation == Orientation::USER_ROTATION_PORTRAIT ||
5123 orientation == Orientation::USER_ROTATION_LANDSCAPE ||
5124 orientation == Orientation::USER_ROTATION_PORTRAIT_INVERTED ||
5125 orientation == Orientation::USER_ROTATION_LANDSCAPE_INVERTED) {
5126 return true;
5127 }
5128 return false;
5129 }
5130
IsVerticalOrientation(Orientation orientation) const5131 bool WindowSessionImpl::IsVerticalOrientation(Orientation orientation) const
5132 {
5133 if (orientation == Orientation::VERTICAL ||
5134 orientation == Orientation::REVERSE_VERTICAL ||
5135 orientation == Orientation::SENSOR_VERTICAL ||
5136 orientation == Orientation::AUTO_ROTATION_PORTRAIT_RESTRICTED ||
5137 orientation == Orientation::USER_ROTATION_PORTRAIT ||
5138 orientation == Orientation::USER_ROTATION_PORTRAIT_INVERTED) {
5139 return true;
5140 }
5141 return false;
5142 }
5143
GetCallingWindowWindowStatus(WindowStatus & windowStatus) const5144 WMError WindowSessionImpl::GetCallingWindowWindowStatus(WindowStatus& windowStatus) const
5145 {
5146 TLOGD(WmsLogTag::WMS_KEYBOARD, "id: %{public}d", GetPersistentId());
5147 if (IsWindowSessionInvalid()) {
5148 TLOGE(WmsLogTag::WMS_KEYBOARD, "session is invalid");
5149 return WMError::WM_ERROR_INVALID_WINDOW;
5150 }
5151 return SingletonContainer::Get<WindowAdapter>().GetCallingWindowWindowStatus(GetPersistentId(), windowStatus);
5152 }
5153
GetCallingWindowRect(Rect & rect) const5154 WMError WindowSessionImpl::GetCallingWindowRect(Rect& rect) const
5155 {
5156 if (IsWindowSessionInvalid()) {
5157 TLOGE(WmsLogTag::WMS_KEYBOARD, "session is invalid");
5158 return WMError::WM_ERROR_INVALID_WINDOW;
5159 }
5160 return SingletonContainer::Get<WindowAdapter>().GetCallingWindowRect(GetPersistentId(), rect);
5161 }
5162
SetUiDvsyncSwitch(bool dvsyncSwitch)5163 void WindowSessionImpl::SetUiDvsyncSwitch(bool dvsyncSwitch)
5164 {
5165 if (vsyncStation_ == nullptr) {
5166 TLOGW(WmsLogTag::WMS_MAIN, "vsyncStation is null");
5167 return;
5168 }
5169 vsyncStation_->SetUiDvsyncSwitch(dvsyncSwitch);
5170 }
5171
GetAppForceLandscapeConfig(AppForceLandscapeConfig & config)5172 WMError WindowSessionImpl::GetAppForceLandscapeConfig(AppForceLandscapeConfig& config)
5173 {
5174 if (IsWindowSessionInvalid()) {
5175 TLOGE(WmsLogTag::DEFAULT, "HostSession is invalid");
5176 return WMError::WM_ERROR_INVALID_WINDOW;
5177 }
5178 auto hostSession = GetHostSession();
5179 CHECK_HOST_SESSION_RETURN_ERROR_IF_NULL(hostSession, WMError::WM_ERROR_NULLPTR);
5180 return hostSession->GetAppForceLandscapeConfig(config);
5181 }
5182
SetForceSplitEnable(bool isForceSplit,const std::string & homePage)5183 void WindowSessionImpl::SetForceSplitEnable(bool isForceSplit, const std::string& homePage)
5184 {
5185 std::shared_ptr<Ace::UIContent> uiContent = GetUIContentSharedPtr();
5186 if (uiContent == nullptr) {
5187 TLOGE(WmsLogTag::DEFAULT, "uiContent is null!");
5188 return;
5189 }
5190 TLOGI(WmsLogTag::DEFAULT, "isForceSplit: %{public}u, homePage: %{public}s",
5191 isForceSplit, homePage.c_str());
5192 uiContent->SetForceSplitEnable(isForceSplit, homePage);
5193 }
5194
SetFrameLayoutCallbackEnable(bool enable)5195 void WindowSessionImpl::SetFrameLayoutCallbackEnable(bool enable)
5196 {
5197 enableFrameLayoutFinishCb_ = enable;
5198 }
5199
UpdateFrameLayoutCallbackIfNeeded(WindowSizeChangeReason wmReason)5200 void WindowSessionImpl::UpdateFrameLayoutCallbackIfNeeded(WindowSizeChangeReason wmReason)
5201 {
5202 bool isDragInPcmode = IsFreeMultiWindowMode() && (wmReason == WindowSizeChangeReason::DRAG_END);
5203 if (wmReason == WindowSizeChangeReason::FULL_TO_SPLIT || wmReason == WindowSizeChangeReason::SPLIT_TO_FULL ||
5204 wmReason == WindowSizeChangeReason::FULL_TO_FLOATING || wmReason == WindowSizeChangeReason::FLOATING_TO_FULL ||
5205 isDragInPcmode) {
5206 TLOGI(WmsLogTag::WMS_MULTI_WINDOW, "enable framelayoutfinish callback reason:%{public}u", wmReason);
5207 SetFrameLayoutCallbackEnable(true);
5208 }
5209 }
5210
SetContinueState(int32_t continueState)5211 WMError WindowSessionImpl::SetContinueState(int32_t continueState)
5212 {
5213 if (continueState > ContinueState::CONTINUESTATE_MAX || continueState < ContinueState::CONTINUESTATE_UNKNOWN) {
5214 TLOGE(WmsLogTag::WMS_MAIN, "continueState is invalid: %{public}d", continueState);
5215 return WMError::WM_ERROR_INVALID_PARAM;
5216 }
5217 property_->EditSessionInfo().continueState = static_cast<ContinueState>(continueState);
5218 return WMError::WM_OK;
5219 }
5220
SetUIContentComplete()5221 void WindowSessionImpl::SetUIContentComplete()
5222 {
5223 bool setUIContentCompleted = false;
5224 if (setUIContentCompleted_.compare_exchange_strong(setUIContentCompleted, true)) {
5225 TLOGI(WmsLogTag::WMS_LIFE, "persistentId=%{public}d", GetPersistentId());
5226 handler_->RemoveTask(SET_UICONTENT_TIMEOUT_LISTENER_TASK_NAME + std::to_string(GetPersistentId()));
5227 } else {
5228 TLOGI(WmsLogTag::WMS_LIFE, "already SetUIContent, persistentId=%{public}d", GetPersistentId());
5229 }
5230 }
5231
AddSetUIContentTimeoutCheck()5232 void WindowSessionImpl::AddSetUIContentTimeoutCheck()
5233 {
5234 const auto checkBeginTime =
5235 std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now())
5236 .time_since_epoch().count();
5237 auto task = [weakThis = wptr(this), checkBeginTime] {
5238 auto window = weakThis.promote();
5239 if (window == nullptr) {
5240 TLOGNI(WmsLogTag::WMS_LIFE, "window is nullptr");
5241 return;
5242 }
5243 if (window->setUIContentCompleted_.load()) {
5244 TLOGNI(WmsLogTag::WMS_LIFE, "already SetUIContent, persistentId=%{public}d", window->GetPersistentId());
5245 return;
5246 }
5247
5248 const auto checkEndTime =
5249 std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now())
5250 .time_since_epoch().count();
5251 if (checkEndTime - checkBeginTime > SET_UICONTENT_TIMEOUT_TIME_AFTER_FREEZE_MS) {
5252 TLOGNI(WmsLogTag::WMS_LIFE, "will start re-check after freeze, persistentId=%{public}d",
5253 window->GetPersistentId());
5254 window->AddSetUIContentTimeoutCheck();
5255 return;
5256 }
5257
5258 TLOGNI(WmsLogTag::WMS_LIFE, "SetUIContent timeout, persistentId=%{public}d", window->GetPersistentId());
5259 std::ostringstream oss;
5260 oss << "SetUIContent timeout uid: " << getuid();
5261 oss << ", windowName: " << window->GetWindowName();
5262 if (window->context_) {
5263 oss << ", bundleName: " << window->context_->GetBundleName();
5264 if (window->context_->GetApplicationInfo()) {
5265 oss << ", abilityName: " << window->context_->GetApplicationInfo()->name;
5266 }
5267 }
5268 SingletonContainer::Get<WindowInfoReporter>().ReportWindowException(
5269 static_cast<int32_t>(WindowDFXHelperType::WINDOW_TRANSPARENT_CHECK), getpid(), oss.str());
5270
5271 if (WindowHelper::IsUIExtensionWindow(window->GetType())) {
5272 window->NotifyExtensionTimeout(TimeoutErrorCode::SET_UICONTENT_TIMEOUT);
5273 }
5274 };
5275 handler_->PostTask(task, SET_UICONTENT_TIMEOUT_LISTENER_TASK_NAME + std::to_string(GetPersistentId()),
5276 SET_UICONTENT_TIMEOUT_TIME_MS, AppExecFwk::EventQueue::Priority::HIGH);
5277 }
5278
NotifySetUIContentComplete()5279 void WindowSessionImpl::NotifySetUIContentComplete()
5280 {
5281 if (WindowHelper::IsSubWindow(GetType()) || WindowHelper::IsSystemWindow(GetType())) {
5282 // created by UIExtension
5283 auto extWindow = FindExtensionWindowWithContext();
5284 if (extWindow != nullptr) {
5285 extWindow->SetUIContentComplete();
5286 }
5287 }
5288 SetUIContentComplete();
5289 }
5290
SetUIExtensionDestroyComplete()5291 void WindowSessionImpl::SetUIExtensionDestroyComplete()
5292 {
5293 bool setUIExtensionDestroyCompleted = false;
5294 if (setUIExtensionDestroyCompleted_.compare_exchange_strong(setUIExtensionDestroyCompleted, true)) {
5295 TLOGI(WmsLogTag::WMS_LIFE, "persistentId=%{public}d", GetPersistentId());
5296 handler_->RemoveTask(SET_UIEXTENSION_DESTROY_TIMEOUT_LISTENER_TASK_NAME + std::to_string(GetPersistentId()));
5297 } else {
5298 TLOGI(WmsLogTag::WMS_LIFE, "already, persistentId=%{public}d", GetPersistentId());
5299 }
5300 }
5301
SetUIExtensionDestroyCompleteInSubWindow()5302 void WindowSessionImpl::SetUIExtensionDestroyCompleteInSubWindow()
5303 {
5304 if (WindowHelper::IsSubWindow(GetType()) || WindowHelper::IsSystemWindow(GetType())) {
5305 bool startUIExtensionDestroyTimer = true;
5306 auto extensionWindow = FindExtensionWindowWithContext();
5307 if (extensionWindow != nullptr && extensionWindow->startUIExtensionDestroyTimer_.compare_exchange_strong(
5308 startUIExtensionDestroyTimer, false)) {
5309 TLOGI(WmsLogTag::WMS_LIFE, "called");
5310 extensionWindow->SetUIExtensionDestroyComplete();
5311 extensionWindow->setUIExtensionDestroyCompleted_.store(false);
5312 }
5313 }
5314 }
5315
AddSetUIExtensionDestroyTimeoutCheck()5316 void WindowSessionImpl::AddSetUIExtensionDestroyTimeoutCheck()
5317 {
5318 const char* const where = __func__;
5319 auto task = [weakThis = wptr(this), where] {
5320 auto window = weakThis.promote();
5321 if (window == nullptr) {
5322 TLOGNE(WmsLogTag::WMS_LIFE, "%{public}s: window is nullptr", where);
5323 return;
5324 }
5325 if (window->setUIExtensionDestroyCompleted_.load()) {
5326 TLOGNI(WmsLogTag::WMS_LIFE, "%{public}s: already, persistentId=%{public}d", where,
5327 window->GetPersistentId());
5328 return;
5329 }
5330
5331 TLOGNI(WmsLogTag::WMS_LIFE, "%{public}s: timeout, persistentId=%{public}d", where, window->GetPersistentId());
5332 std::ostringstream oss;
5333 oss << "SetUIExtDestroy timeout uid: " << getuid();
5334 oss << ", windowName: " << window->GetWindowName();
5335 if (window->context_) {
5336 oss << ", bundleName: " << window->context_->GetBundleName();
5337 if (window->context_->GetApplicationInfo()) {
5338 oss << ", abilityName: " << window->context_->GetApplicationInfo()->name;
5339 }
5340 }
5341 SingletonContainer::Get<WindowInfoReporter>().ReportWindowException(
5342 static_cast<int32_t>(WindowDFXHelperType::WINDOW_TRANSPARENT_CHECK), getpid(), oss.str());
5343
5344 if (WindowHelper::IsUIExtensionWindow(window->GetType())) {
5345 window->NotifyExtensionTimeout(TimeoutErrorCode::SET_UIEXTENSION_DESTROY_TIMEOUT);
5346 }
5347 };
5348 handler_->PostTask(task, SET_UIEXTENSION_DESTROY_TIMEOUT_LISTENER_TASK_NAME + std::to_string(GetPersistentId()),
5349 SET_UIEXTENSION_DESTROY_TIMEOUT_TIME_MS, AppExecFwk::EventQueue::Priority::HIGH);
5350 startUIExtensionDestroyTimer_.store(true);
5351 }
5352
SetEnableDragBySystem(bool enableDrag)5353 WSError WindowSessionImpl::SetEnableDragBySystem(bool enableDrag)
5354 {
5355 TLOGE(WmsLogTag::WMS_LAYOUT, "enableDrag: %{public}d", enableDrag);
5356 property_->SetDragEnabled(enableDrag);
5357 return WSError::WS_OK;
5358 }
5359
SetDragActivated(bool dragActivated)5360 WSError WindowSessionImpl::SetDragActivated(bool dragActivated)
5361 {
5362 dragActivated_ = dragActivated;
5363 return WSError::WS_OK;
5364 }
5365
IsWindowDraggable()5366 bool WindowSessionImpl::IsWindowDraggable()
5367 {
5368 bool isDragEnabled = GetProperty()->GetDragEnabled();
5369 TLOGD(WmsLogTag::WMS_LAYOUT, "PersistentId: %{public}d, dragEnabled: %{public}d, dragActivate: %{public}d",
5370 GetPersistentId(), isDragEnabled, dragActivated_.load());
5371 return isDragEnabled && dragActivated_.load();
5372 }
5373
SetTargetAPIVersion(uint32_t targetAPIVersion)5374 void WindowSessionImpl::SetTargetAPIVersion(uint32_t targetAPIVersion)
5375 {
5376 targetAPIVersion_ = targetAPIVersion;
5377 }
5378
GetTargetAPIVersion() const5379 uint32_t WindowSessionImpl::GetTargetAPIVersion() const
5380 {
5381 return targetAPIVersion_;
5382 }
5383
GetTargetAPIVersionByApplicationInfo() const5384 uint32_t WindowSessionImpl::GetTargetAPIVersionByApplicationInfo() const
5385 {
5386 return (context_ != nullptr && context_->GetApplicationInfo() != nullptr) ?
5387 static_cast<uint32_t>(context_->GetApplicationInfo()->apiTargetVersion) % API_VERSION_MOD :
5388 INVALID_TARGET_API_VERSION;
5389 }
5390
SetLayoutTransform(const Transform & transform)5391 void WindowSessionImpl::SetLayoutTransform(const Transform& transform)
5392 {
5393 std::lock_guard<std::recursive_mutex> lock(transformMutex_);
5394 layoutTransform_ = transform;
5395 }
5396
GetLayoutTransform() const5397 Transform WindowSessionImpl::GetLayoutTransform() const
5398 {
5399 std::lock_guard<std::recursive_mutex> lock(transformMutex_);
5400 return layoutTransform_;
5401 }
5402
SetCurrentTransform(const Transform & transform)5403 void WindowSessionImpl::SetCurrentTransform(const Transform& transform)
5404 {
5405 std::lock_guard<std::mutex> lock(currentTransformMutex_);
5406 currentTransform_ = transform;
5407 }
5408
GetCurrentTransform() const5409 Transform WindowSessionImpl::GetCurrentTransform() const
5410 {
5411 std::lock_guard<std::mutex> lock(currentTransformMutex_);
5412 return currentTransform_;
5413 }
5414
RegisterWindowInspectorCallback()5415 void WindowSessionImpl::RegisterWindowInspectorCallback()
5416 {
5417 auto getWMSWindowListCallback = [weakThis = wptr(this)]() -> std::optional<WindowListInfo> {
5418 if (auto window = weakThis.promote()) {
5419 return std::make_optional<WindowListInfo>({
5420 window->GetWindowName(), window->GetWindowId(),
5421 static_cast<uint32_t>(window->GetType()), window->GetRect()
5422 });
5423 } else {
5424 return std::nullopt;
5425 }
5426 };
5427 WindowInspector::GetInstance().RegisterGetWMSWindowListCallback(GetWindowId(), std::move(getWMSWindowListCallback));
5428 }
5429
GetExtensionConfig(AAFwk::WantParams & want) const5430 void WindowSessionImpl::GetExtensionConfig(AAFwk::WantParams& want) const
5431 {
5432 want.SetParam(Extension::CROSS_AXIS_FIELD, AAFwk::Integer::Box(static_cast<int32_t>(crossAxisState_.load())));
5433 WindowType rootHostWindowType = (GetType() == WindowType::WINDOW_TYPE_UI_EXTENSION) ?
5434 GetRootHostWindowType() : GetType();
5435 want.SetParam(Extension::ROOT_HOST_WINDOW_TYPE_FIELD,
5436 AAFwk::Integer::Box(static_cast<int32_t>(rootHostWindowType)));
5437 }
5438
UpdateExtensionConfig(const std::shared_ptr<AAFwk::Want> & want)5439 void WindowSessionImpl::UpdateExtensionConfig(const std::shared_ptr<AAFwk::Want>& want)
5440 {
5441 if (want == nullptr) {
5442 TLOGE(WmsLogTag::WMS_UIEXT, "null want ptr");
5443 return;
5444 }
5445
5446 const auto& configParam = want->GetParams().GetWantParams(Extension::UIEXTENSION_CONFIG_FIELD);
5447 auto state = configParam.GetIntParam(Extension::CROSS_AXIS_FIELD, 0);
5448 if (IsValidCrossState(state)) {
5449 crossAxisState_ = static_cast<CrossAxisState>(state);
5450 }
5451 want->RemoveParam(Extension::UIEXTENSION_CONFIG_FIELD);
5452 auto rootHostWindowType =
5453 static_cast<WindowType>(configParam.GetIntParam(Extension::ROOT_HOST_WINDOW_TYPE_FIELD, 0));
5454 SetRootHostWindowType(rootHostWindowType);
5455 TLOGI(WmsLogTag::WMS_ATTRIBUTE, "CrossAxisState: %{public}d, rootHostWindowType: %{public}u, winId: %{public}u",
5456 state, rootHostWindowType, GetWindowId());
5457 }
5458
IsValidCrossState(int32_t state) const5459 bool WindowSessionImpl::IsValidCrossState(int32_t state) const
5460 {
5461 return state >= static_cast<int32_t>(CrossAxisState::STATE_INVALID) &&
5462 state < static_cast<int32_t>(CrossAxisState::STATE_END);
5463 }
5464
UpdateSubWindowLevel(uint32_t subWindowLevel)5465 void WindowSessionImpl::UpdateSubWindowLevel(uint32_t subWindowLevel)
5466 {
5467 std::vector<sptr<WindowSessionImpl>> subWindows;
5468 GetSubWidnows(GetPersistentId(), subWindows);
5469 for (auto& subWindow : subWindows) {
5470 if (subWindow != nullptr) {
5471 subWindow->UpdateSubWindowLevel(subWindowLevel + 1);
5472 }
5473 }
5474 }
5475
IsSubWindowMaximizeSupported() const5476 bool WindowSessionImpl::IsSubWindowMaximizeSupported() const
5477 {
5478 if (!WindowHelper::IsSubWindow(GetType())) {
5479 TLOGE(WmsLogTag::WMS_LAYOUT, "Maximize fail not subwindow");
5480 return false;
5481 }
5482 if (windowOption_ != nullptr) {
5483 return windowOption_->GetSubWindowMaximizeSupported();
5484 }
5485 return false;
5486 }
5487 } // namespace Rosen
5488 } // namespace OHOS
5489