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 #include <optional>
20
21 #include <common/rs_common_def.h>
22 #include <ipc_skeleton.h>
23 #include <hisysevent.h>
24 #ifdef IMF_ENABLE
25 #include <input_method_controller.h>
26 #endif // IMF_ENABLE
27 #include <transaction/rs_interfaces.h>
28 #include <transaction/rs_transaction.h>
29
30 #include "anr_handler.h"
31 #include "color_parser.h"
32 #include "display_manager.h"
33 #include "interfaces/include/ws_common.h"
34 #include "session_permission.h"
35 #include "key_event.h"
36 #include "session/container/include/window_event_channel.h"
37 #include "session_manager/include/session_manager.h"
38 #include "vsync_station.h"
39 #include "window_adapter.h"
40 #include "window_manager_hilog.h"
41 #include "window_helper.h"
42 #include "color_parser.h"
43 #include "singleton_container.h"
44 #include "perform_reporter.h"
45
46
47 namespace OHOS {
48 namespace Rosen {
49 namespace {
50 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowSessionImpl"};
51 }
52
53 std::unordered_map<ColorSpace, GraphicColorGamut> WindowSessionImpl::colorSpaceConvertMap = {
54 {ColorSpace::COLOR_SPACE_DEFAULT, GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB},
55 {ColorSpace::COLOR_SPACE_WIDE_GAMUT, GraphicColorGamut::GRAPHIC_COLOR_GAMUT_DCI_P3},
56 };
57 std::map<int32_t, std::vector<sptr<IWindowLifeCycle>>> WindowSessionImpl::lifecycleListeners_;
58 std::map<int32_t, std::vector<sptr<IWindowChangeListener>>> WindowSessionImpl::windowChangeListeners_;
59 std::map<int32_t, std::vector<sptr<IAvoidAreaChangedListener>>> WindowSessionImpl::avoidAreaChangeListeners_;
60 std::map<int32_t, std::vector<sptr<IDialogDeathRecipientListener>>> WindowSessionImpl::dialogDeathRecipientListeners_;
61 std::map<int32_t, std::vector<sptr<IDialogTargetTouchListener>>> WindowSessionImpl::dialogTargetTouchListener_;
62 std::map<int32_t, std::vector<sptr<IOccupiedAreaChangeListener>>> WindowSessionImpl::occupiedAreaChangeListeners_;
63 std::map<int32_t, std::vector<sptr<IScreenshotListener>>> WindowSessionImpl::screenshotListeners_;
64 std::map<int32_t, std::vector<sptr<ITouchOutsideListener>>> WindowSessionImpl::touchOutsideListeners_;
65 std::recursive_mutex WindowSessionImpl::globalMutex_;
66 std::map<std::string, std::pair<int32_t, sptr<WindowSessionImpl>>> WindowSessionImpl::windowSessionMap_;
67 std::map<int32_t, std::vector<sptr<WindowSessionImpl>>> WindowSessionImpl::subWindowSessionMap_;
68
69 #define CALL_LIFECYCLE_LISTENER(windowLifecycleCb, listeners) \
70 do { \
71 for (auto& listener : (listeners)) { \
72 if (listener != nullptr) { \
73 listener->windowLifecycleCb(); \
74 } \
75 } \
76 } while (0)
77
78 #define CALL_LIFECYCLE_LISTENER_WITH_PARAM(windowLifecycleCb, listeners, param) \
79 do { \
80 for (auto& listener : (listeners)) { \
81 if (listener != nullptr) { \
82 listener->windowLifecycleCb(param); \
83 } \
84 } \
85 } while (0)
86
87 #define CALL_UI_CONTENT(uiContentCb) \
88 do { \
89 if (uiContent_ != nullptr) { \
90 uiContent_->uiContentCb(); \
91 } \
92 } while (0)
93
WindowSessionImpl(const sptr<WindowOption> & option)94 WindowSessionImpl::WindowSessionImpl(const sptr<WindowOption>& option)
95 {
96 WLOGFD("WindowSessionImpl");
97 property_ = new (std::nothrow) WindowSessionProperty();
98 if (property_ == nullptr) {
99 WLOGFE("Property is null");
100 return;
101 }
102
103 property_->SetWindowName(option->GetWindowName());
104 property_->SetRequestRect(option->GetWindowRect());
105 property_->SetWindowType(option->GetWindowType());
106 property_->SetFocusable(option->GetFocusable());
107 property_->SetTouchable(option->GetTouchable());
108 property_->SetDisplayId(option->GetDisplayId());
109 property_->SetParentId(option->GetParentId());
110 property_->SetTurnScreenOn(option->IsTurnScreenOn());
111 property_->SetKeepScreenOn(option->IsKeepScreenOn());
112 property_->SetWindowMode(option->GetWindowMode());
113 property_->SetWindowFlags(option->GetWindowFlags());
114 surfaceNode_ = CreateSurfaceNode(property_->GetWindowName(), option->GetWindowType());
115 handler_ = std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::GetMainEventRunner());
116 }
117
CreateSurfaceNode(std::string name,WindowType type)118 RSSurfaceNode::SharedPtr WindowSessionImpl::CreateSurfaceNode(std::string name, WindowType type)
119 {
120 struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
121 rsSurfaceNodeConfig.SurfaceNodeName = name;
122 RSSurfaceNodeType rsSurfaceNodeType = RSSurfaceNodeType::DEFAULT;
123 switch (type) {
124 case WindowType::WINDOW_TYPE_BOOT_ANIMATION:
125 case WindowType::WINDOW_TYPE_POINTER:
126 rsSurfaceNodeType = RSSurfaceNodeType::SELF_DRAWING_WINDOW_NODE;
127 break;
128 case WindowType::WINDOW_TYPE_APP_MAIN_WINDOW:
129 rsSurfaceNodeType = RSSurfaceNodeType::APP_WINDOW_NODE;
130 break;
131 default:
132 rsSurfaceNodeType = RSSurfaceNodeType::DEFAULT;
133 break;
134 }
135 return RSSurfaceNode::Create(rsSurfaceNodeConfig, rsSurfaceNodeType);
136 }
137
~WindowSessionImpl()138 WindowSessionImpl::~WindowSessionImpl()
139 {
140 WLOGFD("~WindowSessionImpl, id: %{public}d", GetPersistentId());
141 Destroy(false);
142 }
143
GetWindowId() const144 uint32_t WindowSessionImpl::GetWindowId() const
145 {
146 return static_cast<uint32_t>(GetPersistentId()) & 0xffffffff; // 0xffffffff: to get low 32 bits
147 }
148
GetParentId() const149 int32_t WindowSessionImpl::GetParentId() const
150 {
151 // 0xffffffff: to get low 32 bits
152 uint32_t parentID = static_cast<uint32_t>(property_->GetParentPersistentId()) & 0x7fffffff;
153 return static_cast<int32_t>(parentID);
154 }
155
IsWindowSessionInvalid() const156 bool WindowSessionImpl::IsWindowSessionInvalid() const
157 {
158 bool res = ((hostSession_ == nullptr) || (GetPersistentId() == INVALID_SESSION_ID) ||
159 (state_ == WindowState::STATE_DESTROYED));
160 if (res) {
161 WLOGW("already destroyed or not created! id: %{public}d state_: %{public}u", GetPersistentId(), state_);
162 }
163 return res;
164 }
165
GetPersistentId() const166 int32_t WindowSessionImpl::GetPersistentId() const
167 {
168 if (property_) {
169 return property_->GetPersistentId();
170 }
171 return INVALID_SESSION_ID;
172 }
173
GetProperty() const174 sptr<WindowSessionProperty> WindowSessionImpl::GetProperty() const
175 {
176 return property_;
177 }
178
GetSystemSessionConfig() const179 SystemSessionConfig WindowSessionImpl::GetSystemSessionConfig() const
180 {
181 return windowSystemConfig_;
182 }
183
GetHostSession() const184 sptr<ISession> WindowSessionImpl::GetHostSession() const
185 {
186 return hostSession_;
187 }
188
GetColorSpaceFromSurfaceGamut(GraphicColorGamut colorGamut)189 ColorSpace WindowSessionImpl::GetColorSpaceFromSurfaceGamut(GraphicColorGamut colorGamut)
190 {
191 for (std::pair<ColorSpace, GraphicColorGamut> p: colorSpaceConvertMap) {
192 if (p.second == colorGamut) {
193 return p.first;
194 }
195 }
196 WLOGFE("try to get not exist ColorSpace");
197
198 return ColorSpace::COLOR_SPACE_DEFAULT;
199 }
200
GetSurfaceGamutFromColorSpace(ColorSpace colorSpace)201 GraphicColorGamut WindowSessionImpl::GetSurfaceGamutFromColorSpace(ColorSpace colorSpace)
202 {
203 if (colorSpaceConvertMap.count(colorSpace)) {
204 return colorSpaceConvertMap[colorSpace];
205 }
206 WLOGFE("try to get not exist colorGamut");
207 return GRAPHIC_COLOR_GAMUT_SRGB;
208 }
209
IsSupportWideGamut()210 bool WindowSessionImpl::IsSupportWideGamut()
211 {
212 return true;
213 }
214
SetColorSpace(ColorSpace colorSpace)215 void WindowSessionImpl::SetColorSpace(ColorSpace colorSpace)
216 {
217 auto colorGamut = GetSurfaceGamutFromColorSpace(colorSpace);
218 surfaceNode_->SetColorSpace(colorGamut);
219 }
220
GetColorSpace()221 ColorSpace WindowSessionImpl::GetColorSpace()
222 {
223 GraphicColorGamut colorGamut = surfaceNode_->GetColorSpace();
224 return GetColorSpaceFromSurfaceGamut(colorGamut);
225 }
226
WindowSessionCreateCheck()227 WMError WindowSessionImpl::WindowSessionCreateCheck()
228 {
229 if (!property_) {
230 return WMError::WM_ERROR_NULLPTR;
231 }
232 const auto& name = property_->GetWindowName();
233 // check window name, same window names are forbidden
234 if (windowSessionMap_.find(name) != windowSessionMap_.end()) {
235 WLOGFE("WindowName(%{public}s) already exists.", name.c_str());
236 return WMError::WM_ERROR_REPEAT_OPERATION;
237 }
238
239 // check if camera floating window is already exists
240 if (property_->GetWindowType() == WindowType::WINDOW_TYPE_FLOAT_CAMERA) {
241 for (const auto& item : windowSessionMap_) {
242 if (item.second.second && item.second.second->property_ &&
243 item.second.second->property_->GetWindowType() == WindowType::WINDOW_TYPE_FLOAT_CAMERA) {
244 WLOGFE("Camera floating window is already exists.");
245 return WMError::WM_ERROR_REPEAT_OPERATION;
246 }
247 }
248 uint32_t accessTokenId = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
249 property_->SetAccessTokenId(accessTokenId);
250 WLOGI("Create camera float window, TokenId = %{public}u", accessTokenId);
251 }
252 return WMError::WM_OK;
253 }
254
Create(const std::shared_ptr<AbilityRuntime::Context> & context,const sptr<Rosen::ISession> & iSession)255 WMError WindowSessionImpl::Create(const std::shared_ptr<AbilityRuntime::Context>& context,
256 const sptr<Rosen::ISession>& iSession)
257 {
258 return WMError::WM_OK;
259 }
260
Connect()261 WMError WindowSessionImpl::Connect()
262 {
263 if (hostSession_ == nullptr) {
264 WLOGFE("Session is null!");
265 return WMError::WM_ERROR_NULLPTR;
266 }
267 sptr<ISessionStage> iSessionStage(this);
268 auto windowEventChannel = new (std::nothrow) WindowEventChannel(iSessionStage);
269 sptr<IWindowEventChannel> iWindowEventChannel(windowEventChannel);
270 sptr<IRemoteObject> token = context_ ? context_->GetToken() : nullptr;
271 if (token) {
272 property_->SetTokenState(true);
273 }
274 auto ret = hostSession_->Connect(
275 iSessionStage, iWindowEventChannel, surfaceNode_, windowSystemConfig_, property_, token);
276 WLOGFI("Window Connect [name:%{public}s, id:%{public}d, type:%{public}u], ret:%{public}u",
277 property_->GetWindowName().c_str(), GetPersistentId(), property_->GetWindowType(), ret);
278 return static_cast<WMError>(ret);
279 }
280
Show(uint32_t reason,bool withAnimation)281 WMError WindowSessionImpl::Show(uint32_t reason, bool withAnimation)
282 {
283 WLOGFI("Window Show [name:%{public}s, id:%{public}d, type:%{public}u], reason:%{public}u state:%{pubic}u",
284 property_->GetWindowName().c_str(), GetPersistentId(), property_->GetWindowType(), reason, state_);
285 if (IsWindowSessionInvalid()) {
286 WLOGFE("session is invalid");
287 return WMError::WM_ERROR_INVALID_WINDOW;
288 }
289 if (state_ == WindowState::STATE_SHOWN) {
290 WLOGFD("window session is alreay shown [name:%{public}s, id:%{public}d, type: %{public}u]",
291 property_->GetWindowName().c_str(), GetPersistentId(), property_->GetWindowType());
292 return WMError::WM_OK;
293 }
294
295 WSError ret = hostSession_->Foreground(property_);
296 // delete after replace WSError with WMError
297 WMError res = static_cast<WMError>(ret);
298 if (res == WMError::WM_OK) {
299 NotifyAfterForeground();
300 state_ = WindowState::STATE_SHOWN;
301 requestState_ = WindowState::STATE_SHOWN;
302 } else {
303 NotifyForegroundFailed(res);
304 }
305 return res;
306 }
307
Hide(uint32_t reason,bool withAnimation,bool isFromInnerkits)308 WMError WindowSessionImpl::Hide(uint32_t reason, bool withAnimation, bool isFromInnerkits)
309 {
310 WLOGFI("id:%{public}d Hide, reason:%{public}u, state:%{public}u",
311 GetPersistentId(), reason, state_);
312 if (IsWindowSessionInvalid()) {
313 WLOGFE("session is invalid");
314 return WMError::WM_ERROR_INVALID_WINDOW;
315 }
316 if (state_ == WindowState::STATE_HIDDEN || state_ == WindowState::STATE_CREATED) {
317 WLOGFD("window session is alreay hidden [name:%{public}s, id:%{public}d, type: %{public}u]",
318 property_->GetWindowName().c_str(), GetPersistentId(), property_->GetWindowType());
319 NotifyBackgroundFailed(WMError::WM_DO_NOTHING);
320 return WMError::WM_OK;
321 }
322 NotifyAfterBackground();
323 state_ = WindowState::STATE_HIDDEN;
324 requestState_ = WindowState::STATE_HIDDEN;
325 return WMError::WM_OK;
326 }
327
Destroy(bool needClearListener)328 WMError WindowSessionImpl::Destroy(bool needClearListener)
329 {
330 WLOGFI("Id:%{public}d Destroy, state_:%{public}u", GetPersistentId(), state_);
331 if (IsWindowSessionInvalid()) {
332 WLOGFE("session is invalid");
333 return WMError::WM_ERROR_INVALID_WINDOW;
334 }
335 hostSession_->Disconnect();
336 NotifyBeforeDestroy(GetWindowName());
337 if (needClearListener) {
338 ClearListenersById(GetPersistentId());
339 }
340 {
341 std::lock_guard<std::recursive_mutex> lock(mutex_);
342 state_ = WindowState::STATE_DESTROYED;
343 requestState_ = WindowState::STATE_DESTROYED;
344 }
345 if (hostSession_ != nullptr) {
346 hostSession_ = nullptr;
347 }
348 windowSessionMap_.erase(property_->GetWindowName());
349 DelayedSingleton<ANRHandler>::GetInstance()->ClearDestroyedPersistentId(GetPersistentId());
350 return WMError::WM_OK;
351 }
352
Destroy()353 WMError WindowSessionImpl::Destroy()
354 {
355 return Destroy(true);
356 }
357
SetActive(bool active)358 WSError WindowSessionImpl::SetActive(bool active)
359 {
360 WLOGFD("active status: %{public}d", active);
361 if (active) {
362 NotifyAfterActive();
363 } else {
364 NotifyAfterInactive();
365 }
366 return WSError::WS_OK;
367 }
368
UpdateRect(const WSRect & rect,SizeChangeReason reason)369 WSError WindowSessionImpl::UpdateRect(const WSRect& rect, SizeChangeReason reason)
370 {
371 // delete after replace ws_common.h with wm_common.h
372 auto wmReason = static_cast<WindowSizeChangeReason>(reason);
373 Rect wmRect = { rect.posX_, rect.posY_, rect.width_, rect.height_ };
374 if (!GetRect().IsUninitializedRect()) {
375 // 50 session动画阈值
376 int widthRange = 50;
377 int heightRange = 50;
378 if (std::abs((int)(GetRect().width_) - (int)(wmRect.width_)) > widthRange ||
379 std::abs((int)(GetRect().height_) - (int)(wmRect.height_)) > heightRange) {
380 wmReason = WindowSizeChangeReason::MAXIMIZE;
381 }
382 }
383 auto preRect = GetRect();
384 if (preRect.width_ == wmRect.height_ && preRect.height_ == wmRect.width_) {
385 wmReason = WindowSizeChangeReason::ROTATION;
386 }
387 property_->SetWindowRect(wmRect);
388 auto task = [this, wmReason, wmRect, preRect]() mutable {
389 RSTransaction::FlushImplicitTransaction();
390 RSAnimationTimingProtocol protocol;
391 protocol.SetDuration(600);
392 auto curve = RSAnimationTimingCurve::CreateCubicCurve(0.2, 0.0, 0.2, 1.0);
393 RSNode::OpenImplicitAnimation(protocol, curve);
394 if ((wmRect != preRect) || (wmReason != lastSizeChangeReason_)) {
395 NotifySizeChange(wmRect, wmReason);
396 lastSizeChangeReason_ = wmReason;
397 }
398 UpdateViewportConfig(wmRect, wmReason);
399 RSNode::CloseImplicitAnimation();
400 RSTransaction::FlushImplicitTransaction();
401 postTaskDone_ = true;
402 };
403 if (handler_ != nullptr && wmReason == WindowSizeChangeReason::ROTATION) {
404 postTaskDone_ = false;
405 handler_->PostTask(task);
406 } else {
407 if ((wmRect != preRect) || (wmReason != lastSizeChangeReason_) || !postTaskDone_) {
408 NotifySizeChange(wmRect, wmReason);
409 lastSizeChangeReason_ = wmReason;
410 postTaskDone_ = true;
411 }
412 UpdateViewportConfig(wmRect, wmReason);
413 }
414 WLOGFI("update rect [%{public}d, %{public}d, %{public}u, %{public}u], reason:%{public}u", rect.posX_, rect.posY_,
415 rect.width_, rect.height_, wmReason);
416 return WSError::WS_OK;
417 }
418
UpdateFocus(bool isFocused)419 WSError WindowSessionImpl::UpdateFocus(bool isFocused)
420 {
421 WLOGFD("Report update focus: %{public}u", isFocused);
422 if (isFocused) {
423 HiSysEventWrite(
424 OHOS::HiviewDFX::HiSysEvent::Domain::WINDOW_MANAGER,
425 "FOCUS_WINDOW",
426 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
427 "PID", getpid(),
428 "UID", getuid());
429 NotifyAfterFocused();
430 } else {
431 NotifyAfterUnfocused();
432 }
433 return WSError::WS_OK;
434 }
435
UpdateWindowMode(WindowMode mode)436 WSError WindowSessionImpl::UpdateWindowMode(WindowMode mode)
437 {
438 return WSError::WS_OK;
439 }
440
UpdateViewportConfig(const Rect & rect,WindowSizeChangeReason reason)441 void WindowSessionImpl::UpdateViewportConfig(const Rect& rect, WindowSizeChangeReason reason)
442 {
443 std::lock_guard<std::recursive_mutex> lock(mutex_);
444 if (uiContent_ == nullptr) {
445 WLOGFE("uiContent_ is null!");
446 return;
447 }
448 Ace::ViewportConfig config;
449 config.SetSize(rect.width_, rect.height_);
450 config.SetPosition(rect.posX_, rect.posY_);
451 auto display = SingletonContainer::Get<DisplayManager>().GetDisplayById(property_->GetDisplayId());
452 if (display == nullptr || display->GetDisplayInfo() == nullptr) {
453 WLOGFE("display is null!");
454 return;
455 }
456 float density = display->GetDisplayInfo()->GetVirtualPixelRatio();
457 config.SetDensity(density);
458 uiContent_->UpdateViewportConfig(config, reason);
459 WLOGFD("Id:%{public}d, windowRect:[%{public}d, %{public}d, %{public}u, %{public}u]",
460 GetPersistentId(), rect.posX_, rect.posY_, rect.width_, rect.height_);
461 }
462
GetFloatingWindowParentId()463 int32_t WindowSessionImpl::GetFloatingWindowParentId()
464 {
465 if (context_.get() == nullptr) {
466 return INVALID_SESSION_ID;
467 }
468
469 for (const auto& winPair : windowSessionMap_) {
470 if (winPair.second.second && WindowHelper::IsMainWindow(winPair.second.second->GetType()) &&
471 winPair.second.second->GetProperty() &&
472 context_.get() == winPair.second.second->GetContext().get()) {
473 WLOGFD("Find parent, [parentName: %{public}s, selfPersistentId: %{public}d]",
474 winPair.second.second->GetProperty()->GetWindowName().c_str(), GetPersistentId());
475 return winPair.second.second->GetProperty()->GetPersistentId();
476 }
477 }
478 return INVALID_SESSION_ID;
479 }
480
GetRect() const481 Rect WindowSessionImpl::GetRect() const
482 {
483 return property_->GetWindowRect();
484 }
485
UpdateTitleButtonVisibility()486 void WindowSessionImpl::UpdateTitleButtonVisibility()
487 {
488 if (uiContent_ == nullptr || !IsDecorEnable()) {
489 return;
490 }
491 auto modeSupportInfo = property_->GetModeSupportInfo();
492 bool hideSplitButton = !(modeSupportInfo & WindowModeSupport::WINDOW_MODE_SUPPORT_SPLIT_PRIMARY);
493 // not support fullscreen in split and floating mode, or not support float in fullscreen mode
494 bool hideMaximizeButton = (!(modeSupportInfo & WindowModeSupport::WINDOW_MODE_SUPPORT_FULLSCREEN) &&
495 (GetMode() == WindowMode::WINDOW_MODE_FLOATING || WindowHelper::IsSplitWindowMode(GetMode()))) ||
496 (!(modeSupportInfo & WindowModeSupport::WINDOW_MODE_SUPPORT_FLOATING) &&
497 GetMode() == WindowMode::WINDOW_MODE_FULLSCREEN);
498 WLOGFI("[hideSplit, hideMaximize]: [%{public}d, %{public}d]", hideSplitButton, hideMaximizeButton);
499 uiContent_->HideWindowTitleButton(hideSplitButton, hideMaximizeButton, false);
500 }
501
SetUIContent(const std::string & contentInfo,NativeEngine * engine,NativeValue * storage,bool isdistributed,AppExecFwk::Ability * ability)502 WMError WindowSessionImpl::SetUIContent(const std::string& contentInfo,
503 NativeEngine* engine, NativeValue* storage, bool isdistributed, AppExecFwk::Ability* ability)
504 {
505 WLOGFD("SetUIContent: %{public}s state:%{public}u", contentInfo.c_str(), state_);
506 if (uiContent_) {
507 uiContent_->Destroy();
508 }
509 std::unique_ptr<Ace::UIContent> uiContent;
510 if (ability != nullptr) {
511 uiContent = Ace::UIContent::Create(ability);
512 } else {
513 uiContent = Ace::UIContent::Create(context_.get(), engine);
514 }
515 if (uiContent == nullptr) {
516 WLOGFE("fail to SetUIContent id: %{public}d", GetPersistentId());
517 return WMError::WM_ERROR_NULLPTR;
518 }
519 if (isdistributed) {
520 uiContent->Restore(this, contentInfo, storage);
521 } else {
522 uiContent->Initialize(this, contentInfo, storage);
523 }
524 // make uiContent available after Initialize/Restore
525 uiContent_ = std::move(uiContent);
526
527 uint32_t version = 0;
528 if ((context_ != nullptr) && (context_->GetApplicationInfo() != nullptr)) {
529 version = context_->GetApplicationInfo()->apiCompatibleVersion;
530 }
531 // 10 ArkUI new framework support after API10
532 if (version < 10) {
533 SetLayoutFullScreenByApiVersion(isIgnoreSafeArea_);
534 if (!isSystembarPropertiesSet_) {
535 SetSystemBarProperty(WindowType::WINDOW_TYPE_STATUS_BAR, SystemBarProperty());
536 }
537 } else if (isIgnoreSafeAreaNeedNotify_) {
538 SetLayoutFullScreenByApiVersion(isIgnoreSafeArea_);
539 isIgnoreSafeAreaNeedNotify_ = false;
540 }
541
542 UpdateDecorEnable(true);
543 if (state_ == WindowState::STATE_SHOWN) {
544 // UIContent may be nullptr when show window, need to notify again when window is shown
545 uiContent_->Foreground();
546 UpdateTitleButtonVisibility();
547 }
548 UpdateViewportConfig(GetRect(), WindowSizeChangeReason::UNDEFINED);
549 WLOGFD("notify uiContent window size change end");
550 return WMError::WM_OK;
551 }
552
UpdateDecorEnable(bool needNotify)553 void WindowSessionImpl::UpdateDecorEnable(bool needNotify)
554 {
555 if (needNotify) {
556 if (uiContent_ != nullptr) {
557 uiContent_->UpdateWindowMode(GetMode(), IsDecorEnable());
558 WLOGFD("Notify uiContent window mode change end");
559 }
560 NotifyModeChange(GetMode(), IsDecorEnable());
561 }
562 }
563
NotifyModeChange(WindowMode mode,bool hasDeco)564 void WindowSessionImpl::NotifyModeChange(WindowMode mode, bool hasDeco)
565 {
566 auto windowChangeListeners = GetListeners<IWindowChangeListener>();
567 for (auto& listener : windowChangeListeners) {
568 if (listener.GetRefPtr() != nullptr) {
569 listener.GetRefPtr()->OnModeChange(mode, hasDeco);
570 }
571 }
572 if (hostSession_) {
573 property_->SetWindowMode(mode);
574 property_->SetDecorEnable(hasDeco);
575 }
576 UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_MODE);
577 UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_DECOR_ENABLE);
578 }
579
GetSurfaceNode() const580 std::shared_ptr<RSSurfaceNode> WindowSessionImpl::GetSurfaceNode() const
581 {
582 return surfaceNode_;
583 }
584
GetContext() const585 const std::shared_ptr<AbilityRuntime::Context> WindowSessionImpl::GetContext() const
586 {
587 return context_;
588 }
589
GetRequestRect() const590 Rect WindowSessionImpl::GetRequestRect() const
591 {
592 return property_->GetRequestRect();
593 }
594
GetType() const595 WindowType WindowSessionImpl::GetType() const
596 {
597 return property_->GetWindowType();
598 }
599
GetWindowName() const600 const std::string& WindowSessionImpl::GetWindowName() const
601 {
602 return property_->GetWindowName();
603 }
604
GetWindowState() const605 WindowState WindowSessionImpl::GetWindowState() const
606 {
607 return state_;
608 }
609
GetRequestWindowState() const610 WindowState WindowSessionImpl::GetRequestWindowState() const
611 {
612 return requestState_;
613 }
614
SetFocusable(bool isFocusable)615 WMError WindowSessionImpl::SetFocusable(bool isFocusable)
616 {
617 WLOGFD("set focusable");
618 if (IsWindowSessionInvalid()) {
619 return WMError::WM_ERROR_INVALID_WINDOW;
620 }
621 property_->SetFocusable(isFocusable);
622 return UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_FOCUSABLE);
623 }
624
GetFocusable() const625 bool WindowSessionImpl::GetFocusable() const
626 {
627 return property_->GetFocusable();
628 }
629
SetTouchable(bool isTouchable)630 WMError WindowSessionImpl::SetTouchable(bool isTouchable)
631 {
632 WLOGFD("set touchable");
633 if (IsWindowSessionInvalid()) {
634 return WMError::WM_ERROR_INVALID_WINDOW;
635 }
636 property_->SetTouchable(isTouchable);
637 return UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_TOUCHABLE);
638 }
639
SetResizeByDragEnabled(bool dragEnabled)640 WMError WindowSessionImpl::SetResizeByDragEnabled(bool dragEnabled)
641 {
642 WLOGFD("set dragEnabled");
643 if (IsWindowSessionInvalid()) {
644 return WMError::WM_ERROR_INVALID_WINDOW;
645 }
646
647 property_->SetDragEnabled(dragEnabled);
648 return UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_DRAGENABLED);
649 }
650
SetRaiseByClickEnabled(bool raiseEnabled)651 WMError WindowSessionImpl::SetRaiseByClickEnabled(bool raiseEnabled)
652 {
653 WLOGFD("set raiseEnabled");
654 if (IsWindowSessionInvalid()) {
655 return WMError::WM_ERROR_INVALID_WINDOW;
656 }
657
658 property_->SetRaiseEnabled(raiseEnabled);
659 return UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_RAISEENABLED);
660 }
661
GetTouchable() const662 bool WindowSessionImpl::GetTouchable() const
663 {
664 return property_->GetTouchable();
665 }
666
SetWindowType(WindowType type)667 WMError WindowSessionImpl::SetWindowType(WindowType type)
668 {
669 property_->SetWindowType(type);
670 UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_OTHER_PROPS);
671 return WMError::WM_OK;
672 }
673
SetBrightness(float brightness)674 WMError WindowSessionImpl::SetBrightness(float brightness)
675 {
676 if ((brightness < MINIMUM_BRIGHTNESS &&
677 std::fabs(brightness - UNDEFINED_BRIGHTNESS) >= std::numeric_limits<float>::min()) ||
678 brightness > MAXIMUM_BRIGHTNESS) {
679 WLOGFE("invalid brightness value: %{public}f", brightness);
680 return WMError::WM_ERROR_INVALID_PARAM;
681 }
682 if (!WindowHelper::IsAppWindow(GetType())) {
683 WLOGFE("non app window does not support set brightness, type: %{public}u", GetType());
684 return WMError::WM_ERROR_INVALID_TYPE;
685 }
686 if (!property_) {
687 return WMError::WM_ERROR_NULLPTR;
688 }
689 property_->SetBrightness(brightness);
690 if (state_ == WindowState::STATE_SHOWN) {
691 return UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_SET_BRIGHTNESS);
692 }
693 return WMError::WM_OK;
694 }
695
GetBrightness() const696 float WindowSessionImpl::GetBrightness() const
697 {
698 return property_->GetBrightness();
699 }
700
SetRequestedOrientation(Orientation orientation)701 void WindowSessionImpl::SetRequestedOrientation(Orientation orientation)
702 {
703 if (property_->GetRequestedOrientation() == orientation) {
704 return;
705 }
706 property_->SetRequestedOrientation(orientation);
707 if (state_ == WindowState::STATE_SHOWN) {
708 UpdateProperty(WSPropertyChangeAction::ACTION_UPDATE_ORIENTATION);
709 }
710 }
711
GetContentInfo()712 std::string WindowSessionImpl::GetContentInfo()
713 {
714 WLOGFD("GetContentInfo");
715 if (uiContent_ == nullptr) {
716 WLOGFE("fail to GetContentInfo id: %{public}d", GetPersistentId());
717 return "";
718 }
719 return uiContent_->GetContentInfo();
720 }
721
GetUIContent() const722 Ace::UIContent* WindowSessionImpl::GetUIContent() const
723 {
724 return uiContent_.get();
725 }
726
OnNewWant(const AAFwk::Want & want)727 void WindowSessionImpl::OnNewWant(const AAFwk::Want& want)
728 {
729 WLOGFI("Window [name:%{public}s, id:%{public}d]",
730 property_->GetWindowName().c_str(), GetPersistentId());
731 if (uiContent_ != nullptr) {
732 uiContent_->OnNewWant(want);
733 }
734 }
735
SetAPPWindowLabel(const std::string & label)736 WMError WindowSessionImpl::SetAPPWindowLabel(const std::string& label)
737 {
738 if (uiContent_ == nullptr) {
739 WLOGFE("uicontent is empty");
740 return WMError::WM_ERROR_NULLPTR;
741 }
742 uiContent_->SetAppWindowTitle(label);
743 WLOGI("Set app window label success, label : %{public}s", label.c_str());
744 return WMError::WM_OK;
745 }
746
SetAPPWindowIcon(const std::shared_ptr<Media::PixelMap> & icon)747 WMError WindowSessionImpl::SetAPPWindowIcon(const std::shared_ptr<Media::PixelMap>& icon)
748 {
749 if (icon == nullptr) {
750 WLOGFE("window icon is empty");
751 return WMError::WM_ERROR_NULLPTR;
752 }
753 if (uiContent_ == nullptr) {
754 WLOGFE("uicontent is empty");
755 return WMError::WM_ERROR_NULLPTR;
756 }
757 uiContent_->SetAppWindowIcon(icon);
758 WLOGI("Set app window icon success");
759 return WMError::WM_OK;
760 }
761
RegisterLifeCycleListener(const sptr<IWindowLifeCycle> & listener)762 WMError WindowSessionImpl::RegisterLifeCycleListener(const sptr<IWindowLifeCycle>& listener)
763 {
764 WLOGFD("Start register");
765 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
766 return RegisterListener(lifecycleListeners_[GetPersistentId()], listener);
767 }
768
RegisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener> & listener)769 WMError WindowSessionImpl::RegisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener>& listener)
770 {
771 WLOGFD("Start register");
772 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
773 return RegisterListener(occupiedAreaChangeListeners_[GetPersistentId()], listener);
774 }
775
UnregisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener> & listener)776 WMError WindowSessionImpl::UnregisterOccupiedAreaChangeListener(const sptr<IOccupiedAreaChangeListener>& listener)
777 {
778 WLOGFD("Start unregister");
779 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
780 return UnregisterListener(occupiedAreaChangeListeners_[GetPersistentId()], listener);
781 }
782
UnregisterLifeCycleListener(const sptr<IWindowLifeCycle> & listener)783 WMError WindowSessionImpl::UnregisterLifeCycleListener(const sptr<IWindowLifeCycle>& listener)
784 {
785 WLOGFD("Start unregister");
786 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
787 return UnregisterListener(lifecycleListeners_[GetPersistentId()], listener);
788 }
789
RegisterWindowChangeListener(const sptr<IWindowChangeListener> & listener)790 WMError WindowSessionImpl::RegisterWindowChangeListener(const sptr<IWindowChangeListener>& listener)
791 {
792 WLOGFD("Start register");
793 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
794 return RegisterListener(windowChangeListeners_[GetPersistentId()], listener);
795 }
796
UnregisterWindowChangeListener(const sptr<IWindowChangeListener> & listener)797 WMError WindowSessionImpl::UnregisterWindowChangeListener(const sptr<IWindowChangeListener>& listener)
798 {
799 WLOGFD("Start register");
800 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
801 return UnregisterListener(windowChangeListeners_[GetPersistentId()], listener);
802 }
803
804 template<typename T>
GetListeners()805 EnableIfSame<T, IWindowLifeCycle, std::vector<sptr<IWindowLifeCycle>>> WindowSessionImpl::GetListeners()
806 {
807 std::vector<sptr<IWindowLifeCycle>> lifecycleListeners;
808 {
809 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
810 for (auto& listener : lifecycleListeners_[GetPersistentId()]) {
811 lifecycleListeners.push_back(listener);
812 }
813 }
814 return lifecycleListeners;
815 }
816
817 template<typename T>
GetListeners()818 EnableIfSame<T, IWindowChangeListener, std::vector<sptr<IWindowChangeListener>>> WindowSessionImpl::GetListeners()
819 {
820 std::vector<sptr<IWindowChangeListener>> windowChangeListeners;
821 {
822 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
823 for (auto& listener : windowChangeListeners_[GetPersistentId()]) {
824 windowChangeListeners.push_back(listener);
825 }
826 }
827 return windowChangeListeners;
828 }
829
830 template<typename T>
GetListeners()831 EnableIfSame<T, IOccupiedAreaChangeListener, std::vector<sptr<IOccupiedAreaChangeListener>>> WindowSessionImpl::GetListeners()
832 {
833 std::vector<sptr<IOccupiedAreaChangeListener>> occupiedAreaChangeListeners;
834 {
835 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
836 for (auto& listener : occupiedAreaChangeListeners_[GetPersistentId()]) {
837 occupiedAreaChangeListeners.push_back(listener);
838 }
839 }
840 return occupiedAreaChangeListeners;
841 }
842
843 template<typename T>
RegisterListener(std::vector<sptr<T>> & holder,const sptr<T> & listener)844 WMError WindowSessionImpl::RegisterListener(std::vector<sptr<T>>& holder, const sptr<T>& listener)
845 {
846 if (listener == nullptr) {
847 WLOGFE("listener is nullptr");
848 return WMError::WM_ERROR_NULLPTR;
849 }
850 if (std::find(holder.begin(), holder.end(), listener) != holder.end()) {
851 WLOGFE("Listener already registered");
852 return WMError::WM_OK;
853 }
854 holder.emplace_back(listener);
855 return WMError::WM_OK;
856 }
857
858 template<typename T>
UnregisterListener(std::vector<sptr<T>> & holder,const sptr<T> & listener)859 WMError WindowSessionImpl::UnregisterListener(std::vector<sptr<T>>& holder, const sptr<T>& listener)
860 {
861 if (listener == nullptr) {
862 WLOGFE("listener could not be null");
863 return WMError::WM_ERROR_NULLPTR;
864 }
865 holder.erase(std::remove_if(holder.begin(), holder.end(),
866 [listener](sptr<T> registeredListener) {
867 return registeredListener == listener;
868 }), holder.end());
869 return WMError::WM_OK;
870 }
871
872 template<typename T>
ClearUselessListeners(std::map<int32_t,T> & listeners,int32_t persistentId)873 void WindowSessionImpl::ClearUselessListeners(std::map<int32_t, T>& listeners, int32_t persistentId)
874 {
875 listeners.erase(persistentId);
876 }
877
ClearListenersById(int32_t persistentId)878 void WindowSessionImpl::ClearListenersById(int32_t persistentId)
879 {
880 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
881 ClearUselessListeners(lifecycleListeners_, persistentId);
882 ClearUselessListeners(windowChangeListeners_, persistentId);
883 ClearUselessListeners(avoidAreaChangeListeners_, persistentId);
884 ClearUselessListeners(dialogDeathRecipientListeners_, persistentId);
885 ClearUselessListeners(dialogTargetTouchListener_, persistentId);
886 ClearUselessListeners(screenshotListeners_, persistentId);
887 }
888
RegisterWindowDestroyedListener(const NotifyNativeWinDestroyFunc & func)889 void WindowSessionImpl::RegisterWindowDestroyedListener(const NotifyNativeWinDestroyFunc& func)
890 {
891 notifyNativeFunc_ = std::move(func);
892 }
893
SetInputEventConsumer(const std::shared_ptr<IInputEventConsumer> & inputEventConsumer)894 void WindowSessionImpl::SetInputEventConsumer(const std::shared_ptr<IInputEventConsumer>& inputEventConsumer)
895 {
896 std::lock_guard<std::recursive_mutex> lock(mutex_);
897 inputEventConsumer_ = inputEventConsumer;
898 }
899
NotifyAfterForeground(bool needNotifyListeners,bool needNotifyUiContent)900 void WindowSessionImpl::NotifyAfterForeground(bool needNotifyListeners, bool needNotifyUiContent)
901 {
902 if (needNotifyListeners) {
903 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
904 CALL_LIFECYCLE_LISTENER(AfterForeground, lifecycleListeners);
905 }
906 if (needNotifyUiContent) {
907 CALL_UI_CONTENT(Foreground);
908 }
909 }
910
NotifyAfterBackground(bool needNotifyListeners,bool needNotifyUiContent)911 void WindowSessionImpl::NotifyAfterBackground(bool needNotifyListeners, bool needNotifyUiContent)
912 {
913 if (needNotifyListeners) {
914 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
915 CALL_LIFECYCLE_LISTENER(AfterBackground, lifecycleListeners);
916 }
917 if (needNotifyUiContent) {
918 CALL_UI_CONTENT(Background);
919 }
920 }
921
NotifyAfterFocused()922 void WindowSessionImpl::NotifyAfterFocused()
923 {
924 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
925 CALL_LIFECYCLE_LISTENER(AfterFocused, lifecycleListeners);
926 CALL_UI_CONTENT(Focus);
927 }
928
NotifyAfterUnfocused(bool needNotifyUiContent)929 void WindowSessionImpl::NotifyAfterUnfocused(bool needNotifyUiContent)
930 {
931 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
932 // use needNotifyUinContent to separate ui content callbacks
933 CALL_LIFECYCLE_LISTENER(AfterUnfocused, lifecycleListeners);
934 if (needNotifyUiContent) {
935 CALL_UI_CONTENT(UnFocus);
936 }
937 }
938
NotifyBeforeDestroy(std::string windowName)939 void WindowSessionImpl::NotifyBeforeDestroy(std::string windowName)
940 {
941 std::lock_guard<std::recursive_mutex> lock(mutex_);
942 std::shared_ptr<Ace::UIContent> uiContent = std::move(uiContent_);
943 auto task = [uiContent]() {
944 if (uiContent != nullptr) {
945 uiContent->Destroy();
946 WLOGFD("NotifyBeforeDestroy: uiContent destroy success");
947 }
948 };
949 if (handler_) {
950 handler_->PostTask(task);
951 } else {
952 task();
953 }
954
955 if (notifyNativeFunc_) {
956 notifyNativeFunc_(windowName);
957 }
958 }
959
NotifyAfterActive()960 void WindowSessionImpl::NotifyAfterActive()
961 {
962 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
963 CALL_LIFECYCLE_LISTENER(AfterActive, lifecycleListeners);
964 }
965
NotifyAfterInactive()966 void WindowSessionImpl::NotifyAfterInactive()
967 {
968 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
969 CALL_LIFECYCLE_LISTENER(AfterInactive, lifecycleListeners);
970 }
971
NotifyForegroundFailed(WMError ret)972 void WindowSessionImpl::NotifyForegroundFailed(WMError ret)
973 {
974 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
975 CALL_LIFECYCLE_LISTENER_WITH_PARAM(ForegroundFailed, lifecycleListeners, static_cast<int32_t>(ret));
976 }
977
NotifyBackgroundFailed(WMError ret)978 void WindowSessionImpl::NotifyBackgroundFailed(WMError ret)
979 {
980 auto lifecycleListeners = GetListeners<IWindowLifeCycle>();
981 CALL_LIFECYCLE_LISTENER_WITH_PARAM(BackgroundFailed, lifecycleListeners, static_cast<int32_t>(ret));
982 }
983
MarkProcessed(int32_t eventId)984 WSError WindowSessionImpl::MarkProcessed(int32_t eventId)
985 {
986 if (hostSession_ == nullptr) {
987 WLOGFE("hostSession is nullptr");
988 return WSError::WS_DO_NOTHING;
989 }
990 return hostSession_->MarkProcessed(eventId);
991 }
992
RegisterDialogDeathRecipientListener(const sptr<IDialogDeathRecipientListener> & listener)993 void WindowSessionImpl::RegisterDialogDeathRecipientListener(const sptr<IDialogDeathRecipientListener>& listener)
994 {
995 WLOGFD("Start register DialogDeathRecipientListener");
996 if (listener == nullptr) {
997 WLOGFE("listener is nullptr");
998 return;
999 }
1000 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
1001 RegisterListener(dialogDeathRecipientListeners_[GetPersistentId()], listener);
1002 }
1003
UnregisterDialogDeathRecipientListener(const sptr<IDialogDeathRecipientListener> & listener)1004 void WindowSessionImpl::UnregisterDialogDeathRecipientListener(const sptr<IDialogDeathRecipientListener>& listener)
1005 {
1006 WLOGFD("Start unregister DialogDeathRecipientListener");
1007 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
1008 UnregisterListener(dialogDeathRecipientListeners_[GetPersistentId()], listener);
1009 }
1010
RegisterDialogTargetTouchListener(const sptr<IDialogTargetTouchListener> & listener)1011 WMError WindowSessionImpl::RegisterDialogTargetTouchListener(const sptr<IDialogTargetTouchListener>& listener)
1012 {
1013 WLOGFD("Start register DialogTargetTouchListener");
1014 if (listener == nullptr) {
1015 WLOGFE("listener is nullptr");
1016 return WMError::WM_ERROR_NULLPTR;
1017 }
1018 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
1019 return RegisterListener(dialogTargetTouchListener_[GetPersistentId()], listener);
1020 }
1021
UnregisterDialogTargetTouchListener(const sptr<IDialogTargetTouchListener> & listener)1022 WMError WindowSessionImpl::UnregisterDialogTargetTouchListener(const sptr<IDialogTargetTouchListener>& listener)
1023 {
1024 WLOGFD("Start unregister DialogTargetTouchListener");
1025 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
1026 return UnregisterListener(dialogTargetTouchListener_[GetPersistentId()], listener);
1027 }
1028
RegisterScreenshotListener(const sptr<IScreenshotListener> & listener)1029 WMError WindowSessionImpl::RegisterScreenshotListener(const sptr<IScreenshotListener>& listener)
1030 {
1031 WLOGFD("Start register ScreenshotListener");
1032 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
1033 return RegisterListener(screenshotListeners_[GetPersistentId()], listener);
1034 }
1035
UnregisterScreenshotListener(const sptr<IScreenshotListener> & listener)1036 WMError WindowSessionImpl::UnregisterScreenshotListener(const sptr<IScreenshotListener>& listener)
1037 {
1038 WLOGFD("Start unregister ScreenshotListener");
1039 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
1040 return UnregisterListener(screenshotListeners_[GetPersistentId()], listener);
1041 }
1042
1043 template<typename T>
1044 EnableIfSame<T, IDialogDeathRecipientListener, std::vector<sptr<IDialogDeathRecipientListener>>> WindowSessionImpl::
GetListeners()1045 GetListeners()
1046 {
1047 std::vector<sptr<IDialogDeathRecipientListener>> dialogDeathRecipientListener;
1048 {
1049 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
1050 for (auto& listener : dialogDeathRecipientListeners_[GetPersistentId()]) {
1051 dialogDeathRecipientListener.push_back(listener);
1052 }
1053 }
1054 return dialogDeathRecipientListener;
1055 }
1056
1057 template<typename T>
1058 EnableIfSame<T, IDialogTargetTouchListener,
GetListeners()1059 std::vector<sptr<IDialogTargetTouchListener>>> WindowSessionImpl::GetListeners()
1060 {
1061 std::vector<sptr<IDialogTargetTouchListener>> dialogTargetTouchListener;
1062 {
1063 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
1064 for (auto& listener : dialogTargetTouchListener_[GetPersistentId()]) {
1065 dialogTargetTouchListener.push_back(listener);
1066 }
1067 }
1068 return dialogTargetTouchListener;
1069 }
1070
1071 template<typename T>
GetListeners()1072 EnableIfSame<T, IScreenshotListener, std::vector<sptr<IScreenshotListener>>> WindowSessionImpl::GetListeners()
1073 {
1074 std::vector<sptr<IScreenshotListener>> screenshotListeners;
1075 {
1076 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
1077 for (auto& listener : screenshotListeners_[GetPersistentId()]) {
1078 screenshotListeners.push_back(listener);
1079 }
1080 }
1081 return screenshotListeners;
1082 }
1083
NotifyDestroy()1084 WSError WindowSessionImpl::NotifyDestroy()
1085 {
1086 auto dialogDeathRecipientListener = GetListeners<IDialogDeathRecipientListener>();
1087 for (auto& listener : dialogDeathRecipientListener) {
1088 if (listener != nullptr) {
1089 listener->OnDialogDeathRecipient();
1090 }
1091 }
1092 return WSError::WS_OK;
1093 }
1094
NotifyTouchDialogTarget()1095 void WindowSessionImpl::NotifyTouchDialogTarget()
1096 {
1097 auto dialogTargetTouchListener = GetListeners<IDialogTargetTouchListener>();
1098 for (auto& listener : dialogTargetTouchListener) {
1099 if (listener != nullptr) {
1100 listener->OnDialogTargetTouch();
1101 }
1102 }
1103 }
1104
NotifyScreenshot()1105 void WindowSessionImpl::NotifyScreenshot()
1106 {
1107 auto screenshotListeners = GetListeners<IScreenshotListener>();
1108 for (auto& listener : screenshotListeners) {
1109 if (listener != nullptr) {
1110 listener->OnScreenshot();
1111 }
1112 }
1113 }
1114
NotifySizeChange(Rect rect,WindowSizeChangeReason reason)1115 void WindowSessionImpl::NotifySizeChange(Rect rect, WindowSizeChangeReason reason)
1116 {
1117 auto windowChangeListeners = GetListeners<IWindowChangeListener>();
1118 for (auto& listener : windowChangeListeners) {
1119 if (listener != nullptr) {
1120 listener->OnSizeChange(rect, reason);
1121 }
1122 }
1123 }
1124
RegisterAvoidAreaChangeListener(sptr<IAvoidAreaChangedListener> & listener)1125 WMError WindowSessionImpl::RegisterAvoidAreaChangeListener(sptr<IAvoidAreaChangedListener>& listener)
1126 {
1127 WLOGFD("Start register");
1128 if (listener == nullptr) {
1129 WLOGFE("listener is nullptr");
1130 return WMError::WM_ERROR_NULLPTR;
1131 }
1132
1133 auto persistentId = GetPersistentId();
1134 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
1135 WMError ret = RegisterListener(avoidAreaChangeListeners_[persistentId], listener);
1136 if (ret != WMError::WM_OK) {
1137 return ret;
1138 }
1139 if (avoidAreaChangeListeners_[persistentId].size() == 1) {
1140 ret = SingletonContainer::Get<WindowAdapter>().UpdateSessionAvoidAreaListener(persistentId, true);
1141 }
1142 return ret;
1143 }
1144
UnregisterAvoidAreaChangeListener(sptr<IAvoidAreaChangedListener> & listener)1145 WMError WindowSessionImpl::UnregisterAvoidAreaChangeListener(sptr<IAvoidAreaChangedListener>& listener)
1146 {
1147 WLOGFD("Start unregister");
1148 auto persistentId = GetPersistentId();
1149 if (listener == nullptr) {
1150 WLOGFE("listener is nullptr");
1151 return WMError::WM_ERROR_NULLPTR;
1152 }
1153 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
1154 WMError ret = UnregisterListener(avoidAreaChangeListeners_[persistentId], listener);
1155 if (ret != WMError::WM_OK) {
1156 return ret;
1157 }
1158 if (avoidAreaChangeListeners_[persistentId].empty()) {
1159 ret = SingletonContainer::Get<WindowAdapter>().UpdateSessionAvoidAreaListener(persistentId, false);
1160 }
1161 return ret;
1162 }
1163
1164 template<typename T>
1165 EnableIfSame<T, IAvoidAreaChangedListener,
GetListeners()1166 std::vector<sptr<IAvoidAreaChangedListener>>> WindowSessionImpl::GetListeners()
1167 {
1168 std::vector<sptr<IAvoidAreaChangedListener>> windowChangeListeners;
1169 {
1170 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
1171 for (auto& listener : avoidAreaChangeListeners_[GetPersistentId()]) {
1172 windowChangeListeners.push_back(listener);
1173 }
1174 }
1175 return windowChangeListeners;
1176 }
1177
NotifyAvoidAreaChange(const sptr<AvoidArea> & avoidArea,AvoidAreaType type)1178 void WindowSessionImpl::NotifyAvoidAreaChange(const sptr<AvoidArea>& avoidArea, AvoidAreaType type)
1179 {
1180 auto avoidAreaChangeListeners = GetListeners<IAvoidAreaChangedListener>();
1181 for (auto& listener : avoidAreaChangeListeners) {
1182 if (listener != nullptr) {
1183 listener->OnAvoidAreaChanged(*avoidArea, type);
1184 }
1185 }
1186 }
1187
UpdateAvoidArea(const sptr<AvoidArea> & avoidArea,AvoidAreaType type)1188 WSError WindowSessionImpl::UpdateAvoidArea(const sptr<AvoidArea>& avoidArea, AvoidAreaType type)
1189 {
1190 WLOGI("UpdateAvoidArea, id:%{public}d", GetPersistentId());
1191 NotifyAvoidAreaChange(avoidArea, type);
1192 return WSError::WS_OK;
1193 }
1194
RegisterTouchOutsideListener(const sptr<ITouchOutsideListener> & listener)1195 WMError WindowSessionImpl::RegisterTouchOutsideListener(const sptr<ITouchOutsideListener>& listener)
1196 {
1197 WLOGFD("Start register");
1198 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
1199 return RegisterListener(touchOutsideListeners_[GetPersistentId()], listener);
1200 }
1201
UnregisterTouchOutsideListener(const sptr<ITouchOutsideListener> & listener)1202 WMError WindowSessionImpl::UnregisterTouchOutsideListener(const sptr<ITouchOutsideListener>& listener)
1203 {
1204 WLOGFD("Start unregister");
1205 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
1206 return UnregisterListener(touchOutsideListeners_[GetPersistentId()], listener);
1207 }
1208
1209 template<typename T>
GetListeners()1210 EnableIfSame<T, ITouchOutsideListener, std::vector<sptr<ITouchOutsideListener>>> WindowSessionImpl::GetListeners()
1211 {
1212 std::vector<sptr<ITouchOutsideListener>> windowChangeListeners;
1213 {
1214 std::lock_guard<std::recursive_mutex> lock(globalMutex_);
1215 for (auto& listener : touchOutsideListeners_[GetPersistentId()]) {
1216 windowChangeListeners.push_back(listener);
1217 }
1218 }
1219 return windowChangeListeners;
1220 }
1221
NotifyTouchOutside()1222 WSError WindowSessionImpl::NotifyTouchOutside()
1223 {
1224 auto touchOutsideListeners = GetListeners<ITouchOutsideListener>();
1225 for (auto& listener : touchOutsideListeners) {
1226 if (listener != nullptr) {
1227 listener->OnTouchOutside();
1228 }
1229 }
1230 return WSError::WS_OK;
1231 }
1232
NotifyPointerEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)1233 void WindowSessionImpl::NotifyPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
1234 {
1235 std::shared_ptr<IInputEventConsumer> inputEventConsumer;
1236 {
1237 std::lock_guard<std::recursive_mutex> lock(mutex_);
1238 inputEventConsumer = inputEventConsumer_;
1239 }
1240 if (inputEventConsumer != nullptr) {
1241 WLOGFD("Transfer pointer event to inputEventConsumer");
1242 (void)inputEventConsumer->OnInputEvent(pointerEvent);
1243 } else if (uiContent_ != nullptr) {
1244 WLOGFD("Transfer pointer event to uiContent");
1245 (void)uiContent_->ProcessPointerEvent(pointerEvent);
1246 } else {
1247 WLOGFW("pointerEvent is not consumed, windowId: %{public}u", GetWindowId());
1248 pointerEvent->MarkProcessed();
1249 }
1250 }
1251
NotifyKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,bool & isConsumed)1252 void WindowSessionImpl::NotifyKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool& isConsumed)
1253 {
1254 if (keyEvent == nullptr) {
1255 WLOGFE("keyEvent is nullptr");
1256 return;
1257 }
1258
1259 bool inputMethodHasProcessed = false;
1260 #ifdef IMF_ENABLE
1261 bool isKeyboardEvent = IsKeyboardEvent(keyEvent);
1262 if (isKeyboardEvent) {
1263 WLOGD("dispatch keyEvent to input method");
1264 inputMethodHasProcessed = MiscServices::InputMethodController::GetInstance()->DispatchKeyEvent(keyEvent);
1265 }
1266 #endif // IMF_ENABLE
1267 if (inputMethodHasProcessed) {
1268 WLOGFD("input method has processed key event");
1269 return;
1270 }
1271 std::shared_ptr<IInputEventConsumer> inputEventConsumer;
1272 {
1273 std::lock_guard<std::recursive_mutex> lock(mutex_);
1274 inputEventConsumer = inputEventConsumer_;
1275 }
1276 if (inputEventConsumer != nullptr) {
1277 WLOGD("Transfer key event to inputEventConsumer");
1278 (void)inputEventConsumer->OnInputEvent(keyEvent);
1279 } else if (uiContent_) {
1280 isConsumed = uiContent_->ProcessKeyEvent(keyEvent);
1281 if (!isConsumed && keyEvent->GetKeyCode() == MMI::KeyEvent::KEYCODE_ESCAPE &&
1282 property_->GetWindowMode() == WindowMode::WINDOW_MODE_FULLSCREEN &&
1283 property_->GetMaximizeMode() == MaximizeMode::MODE_FULL_FILL) {
1284 WLOGI("recover from fullscreen cause KEYCODE_ESCAPE");
1285 Recover();
1286 }
1287 }
1288 }
1289
IsKeyboardEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent) const1290 bool WindowSessionImpl::IsKeyboardEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent) const
1291 {
1292 int32_t keyCode = keyEvent->GetKeyCode();
1293 bool isKeyFN = (keyCode == MMI::KeyEvent::KEYCODE_FN);
1294 bool isKeyBack = (keyCode == MMI::KeyEvent::KEYCODE_BACK);
1295 bool isKeyboard = (keyCode >= MMI::KeyEvent::KEYCODE_0 && keyCode <= MMI::KeyEvent::KEYCODE_NUMPAD_RIGHT_PAREN);
1296 WLOGI("isKeyFN: %{public}d, isKeyboard: %{public}d", isKeyFN, isKeyboard);
1297 return (isKeyFN || isKeyboard || isKeyBack);
1298 }
1299
RequestVsync(const std::shared_ptr<VsyncCallback> & vsyncCallback)1300 void WindowSessionImpl::RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback)
1301 {
1302 std::lock_guard<std::recursive_mutex> lock(mutex_);
1303 if (state_ == WindowState::STATE_DESTROYED) {
1304 WLOGFE("Receive vsync request failed, window is destroyed");
1305 return;
1306 }
1307 VsyncStation::GetInstance().RequestVsync(vsyncCallback);
1308 }
1309
GetVSyncPeriod()1310 int64_t WindowSessionImpl::GetVSyncPeriod()
1311 {
1312 std::lock_guard<std::recursive_mutex> lock(mutex_);
1313 return VsyncStation::GetInstance().GetVSyncPeriod();
1314 }
1315
UpdateProperty(WSPropertyChangeAction action)1316 WMError WindowSessionImpl::UpdateProperty(WSPropertyChangeAction action)
1317 {
1318 WLOGFD("UpdateProperty, action:%{public}u", action);
1319 if (IsWindowSessionInvalid()) {
1320 WLOGFE("session is invalid");
1321 return WMError::WM_ERROR_INVALID_WINDOW;
1322 }
1323 return SessionManager::GetInstance().UpdateProperty(property_, action);
1324 }
1325
Find(const std::string & name)1326 sptr<Window> WindowSessionImpl::Find(const std::string& name)
1327 {
1328 auto iter = windowSessionMap_.find(name);
1329 if (iter == windowSessionMap_.end()) {
1330 return nullptr;
1331 }
1332 return iter->second.second;
1333 }
1334
SetAceAbilityHandler(const sptr<IAceAbilityHandler> & handler)1335 void WindowSessionImpl::SetAceAbilityHandler(const sptr<IAceAbilityHandler>& handler)
1336 {
1337 if (handler == nullptr) {
1338 WLOGE("ace ability handler is nullptr");
1339 }
1340 std::lock_guard<std::recursive_mutex> lock(mutex_);
1341 aceAbilityHandler_ = handler;
1342 }
1343
SetBackgroundColor(const std::string & color)1344 WMError WindowSessionImpl::SetBackgroundColor(const std::string& color)
1345 {
1346 if (IsWindowSessionInvalid()) {
1347 WLOGFE("session is invalid");
1348 return WMError::WM_ERROR_INVALID_WINDOW;
1349 }
1350 uint32_t colorValue;
1351 if (ColorParser::Parse(color, colorValue)) {
1352 WLOGD("SetBackgroundColor: window: %{public}s, value: [%{public}s, %{public}u]",
1353 GetWindowName().c_str(), color.c_str(), colorValue);
1354 return SetBackgroundColor(colorValue);
1355 }
1356 WLOGFE("invalid color string: %{public}s", color.c_str());
1357 return WMError::WM_ERROR_INVALID_PARAM;
1358 }
1359
SetBackgroundColor(uint32_t color)1360 WMError WindowSessionImpl::SetBackgroundColor(uint32_t color)
1361 {
1362 WLOGFD("Report set bg color: %{public}u", GetWindowId());
1363
1364 // 0xff000000: ARGB style, means Opaque color.
1365 const bool isAlphaZero = !(color & 0xff000000);
1366 std::string bundleName;
1367 std::string abilityName;
1368 if ((context_ != nullptr) && (context_->GetApplicationInfo() != nullptr)) {
1369 bundleName = context_->GetBundleName();
1370 abilityName = context_->GetApplicationInfo()->name;
1371 }
1372
1373 if (isAlphaZero && WindowHelper::IsMainWindow(GetType())) {
1374 auto& reportInstance = SingletonContainer::Get<WindowInfoReporter>();
1375 reportInstance.ReportZeroOpacityInfoImmediately(bundleName, abilityName);
1376 }
1377
1378 if (uiContent_ != nullptr) {
1379 uiContent_->SetBackgroundColor(color);
1380 return WMError::WM_OK;
1381 }
1382 if (aceAbilityHandler_ != nullptr) {
1383 aceAbilityHandler_->SetBackgroundColor(color);
1384 return WMError::WM_OK;
1385 }
1386 WLOGFE("FA mode could not set bg color: %{public}u", GetWindowId());
1387 return WMError::WM_ERROR_INVALID_OPERATION;
1388 }
1389
GetSubWindow(int parentId)1390 std::vector<sptr<Window>> WindowSessionImpl::GetSubWindow(int parentId)
1391 {
1392 auto iter = subWindowSessionMap_.find(parentId);
1393 if (iter == subWindowSessionMap_.end()) {
1394 return std::vector<sptr<Window>>();
1395 }
1396 return std::vector<sptr<Window>>(subWindowSessionMap_[parentId].begin(), subWindowSessionMap_[parentId].end());
1397 }
1398
GetBackgroundColor() const1399 uint32_t WindowSessionImpl::GetBackgroundColor() const
1400 {
1401 if (uiContent_ != nullptr) {
1402 return uiContent_->GetBackgroundColor();
1403 }
1404 WLOGD("uiContent is nullptr, windowId: %{public}u, use FA mode", GetWindowId());
1405 if (aceAbilityHandler_ != nullptr) {
1406 return aceAbilityHandler_->GetBackgroundColor();
1407 }
1408 WLOGFE("FA mode does not get bg color: %{public}u", GetWindowId());
1409 return 0xffffffff; // means no background color been set, default color is white
1410 }
1411
SetLayoutFullScreenByApiVersion(bool status)1412 WMError WindowSessionImpl::SetLayoutFullScreenByApiVersion(bool status)
1413 {
1414 return WMError::WM_OK;
1415 }
1416
SetWindowGravity(WindowGravity gravity,uint32_t percent)1417 WMError WindowSessionImpl::SetWindowGravity(WindowGravity gravity, uint32_t percent)
1418 {
1419 return SessionManager::GetInstance().SetSessionGravity(GetPersistentId(),
1420 static_cast<SessionGravity>(gravity), percent);
1421 }
1422
SetSystemBarProperty(WindowType type,const SystemBarProperty & property)1423 WMError WindowSessionImpl::SetSystemBarProperty(WindowType type, const SystemBarProperty& property)
1424 {
1425 return WMError::WM_OK;
1426 }
1427
NotifyOccupiedAreaChangeInfo(sptr<OccupiedAreaChangeInfo> info)1428 void WindowSessionImpl::NotifyOccupiedAreaChangeInfo(sptr<OccupiedAreaChangeInfo> info)
1429 {
1430 WLOGFD("NotifyOccupiedAreaChangeInfo, safeHeight: %{public}u "
1431 "occupied rect: x %{public}u, y %{public}u, w %{public}u, h %{public}u",
1432 info->safeHeight_, info->rect_.posX_, info->rect_.posY_, info->rect_.width_, info->rect_.height_);
1433 auto occupiedAreaChangeListeners = GetListeners<IOccupiedAreaChangeListener>();
1434 for (auto& listener : occupiedAreaChangeListeners) {
1435 if (listener != nullptr) {
1436 listener->OnSizeChange(info);
1437 }
1438 }
1439 }
1440
DumpSessionElementInfo(const std::vector<std::string> & params)1441 void WindowSessionImpl::DumpSessionElementInfo(const std::vector<std::string>& params)
1442 {
1443 WLOGFD("DumpSessionElementInfo");
1444 }
1445 } // namespace Rosen
1446 } // namespace OHOS
1447