diff --git a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.cpp b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.cpp index 2ce9dfbb6ed13566591d196c6320cd8ad3395450..3a9b80998f6c346c931dfa8d48229e04b3028500 100644 --- a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.cpp +++ b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.cpp @@ -410,7 +410,7 @@ JsSceneSession::JsSceneSession(napi_env env, const sptr& session) { auto sessionchangeCallback = sptr::MakeSptr(); session->RegisterSessionChangeCallback(sessionchangeCallback); - sessionchangeCallback->clearCallbackFunc_ = [weakThis = wptr(this)](bool needRemove) { + session->RegisterClearCallbackMapCallback([weakThis = wptr(this)](bool needRemove) { if (!needRemove) { TLOGND(WmsLogTag::WMS_LIFE, "clearCallbackFunc needRemove is false"); return; @@ -421,7 +421,7 @@ JsSceneSession::JsSceneSession(napi_env env, const sptr& session) return; } jsSceneSession->ClearCbMap(); - }; + }); sessionchangeCallback_ = sessionchangeCallback; taskScheduler_ = std::make_shared(env); @@ -1536,26 +1536,21 @@ void JsSceneSession::ProcessIsCustomAnimationPlaying() void JsSceneSession::ProcessShowWhenLockedRegister() { - auto sessionchangeCallback = sessionchangeCallback_.promote(); - if (sessionchangeCallback == nullptr) { - WLOGFE("sessionchangeCallback is nullptr"); + auto session = weakSession_.promote(); + if (session == nullptr) { + TLOGE(WmsLogTag::WMS_LIFE, "session is nullptr, id:%{public}d", persistentId_); return; } - sessionchangeCallback->OnShowWhenLocked_ = [weakThis = wptr(this)](bool showWhenLocked) { + const char* const where = __func__; + session->RegisterShowWhenLockedCallback([weakThis = wptr(this), where](bool showWhenLocked) { auto jsSceneSession = weakThis.promote(); if (!jsSceneSession) { - TLOGE(WmsLogTag::WMS_LIFE, "ProcessShowWhenLockedRegister jsSceneSession is null"); + TLOGNE(WmsLogTag::WMS_LIFE, "%{public}s jsSceneSession is null", where); return; } jsSceneSession->OnShowWhenLocked(showWhenLocked); - }; - auto session = weakSession_.promote(); - if (session == nullptr) { - WLOGFE("session is nullptr, id:%{public}d", persistentId_); - return; - } - sessionchangeCallback->OnShowWhenLocked_(session->GetShowWhenLockedFlagValue()); - WLOGFD("success"); + }); + TLOGD(WmsLogTag::WMS_LIFE, "success"); } void JsSceneSession::ProcessRequestedOrientationChange() @@ -1577,20 +1572,21 @@ void JsSceneSession::ProcessRequestedOrientationChange() void JsSceneSession::ProcessForceHideChangeRegister() { - auto sessionchangeCallback = sessionchangeCallback_.promote(); - if (sessionchangeCallback == nullptr) { - WLOGFE("sessionchangeCallback is nullptr"); + auto session = weakSession_.promote(); + if (session == nullptr) { + TLOGE(WmsLogTag::WMS_LIFE, "session is nullptr, id:%{public}d", persistentId_); return; } - sessionchangeCallback->OnForceHideChange_ = [weakThis = wptr(this)](bool hide) { + const char* const where = __func__; + session->RegisterForceHideChangeCallback([weakThis = wptr(this), where](bool hide) { auto jsSceneSession = weakThis.promote(); if (!jsSceneSession) { - TLOGE(WmsLogTag::WMS_LIFE, "ProcessForceHideChangeRegister jsSceneSession is null"); + TLOGNE(WmsLogTag::WMS_LIFE, "%{public}s jsSceneSession is null", where); return; } jsSceneSession->OnForceHideChange(hide); - }; - WLOGFD("success"); + }); + TLOGD(WmsLogTag::WMS_LIFE, "success"); } void JsSceneSession::OnForceHideChange(bool hide) diff --git a/window_scene/session/host/include/scene_session.h b/window_scene/session/host/include/scene_session.h index 84e3f032ecce325ac5ea0af40b323d89642acb88..11551f4509918df671bd15d6d5087f63f7931207 100644 --- a/window_scene/session/host/include/scene_session.h +++ b/window_scene/session/host/include/scene_session.h @@ -126,11 +126,8 @@ public: NotifySessionEventFunc OnSessionEvent_; NotifyIsCustomAnimationPlayingCallback onIsCustomAnimationPlaying_; NotifyWindowAnimationFlagChangeFunc onWindowAnimationFlagChange_; - NotifyShowWhenLockedFunc OnShowWhenLocked_; NotifyRaiseAboveTargetFunc onRaiseAboveTarget_; - NotifyForceHideChangeFunc OnForceHideChange_; NotifyTouchOutsideFunc OnTouchOutside_; - ClearCallbackMapFunc clearCallbackFunc_; NotifyLandscapeMultiWindowSessionFunc onSetLandscapeMultiWindowFunc_; NotifyLayoutFullScreenChangeFunc onLayoutFullScreenChangeFunc_; NotifyDefaultDensityEnabledFunc onDefaultDensityEnabledFunc_; @@ -401,6 +398,9 @@ public: */ void ClearJsSceneSessionCbMap(bool needRemove); // ONLY Accessed on OS_sceneSession thread void ClearSpecificSessionCbMap(); + void RegisterShowWhenLockedCallback(NotifyShowWhenLockedFunc&& callback); + void RegisterForceHideChangeCallback(NotifyForceHideChangeFunc&& callback); + void RegisterClearCallbackMapCallback(ClearCallbackMapFunc&& callback); void SendPointerEventToUI(std::shared_ptr pointerEvent); bool SendKeyEventToUI(std::shared_ptr keyEvent, bool isPreImeEvent = false); @@ -571,6 +571,13 @@ protected: */ NotifyPrepareClosePiPSessionFunc onPrepareClosePiPSession_; + /* + * Window Lifecycle + */ + NotifyShowWhenLockedFunc onShowWhenLockedFunc_; + NotifyForceHideChangeFunc onForceHideChangeFunc_; + ClearCallbackMapFunc clearCallbackMapFunc_; + private: void NotifyAccessibilityVisibilityChange(); void CalculateCombinedExtWindowFlags(); diff --git a/window_scene/session/host/include/session.h b/window_scene/session/host/include/session.h index 9568609b299bf4f748e9515ce033a495537e92b9..99bb41b4d7ddecf098984bee2e61307baf1dceb2 100644 --- a/window_scene/session/host/include/session.h +++ b/window_scene/session/host/include/session.h @@ -127,7 +127,7 @@ public: friend class HidumpController; using Task = std::function; explicit Session(const SessionInfo& info); - virtual ~Session() = default; + virtual ~Session(); bool isKeyboardPanelEnabled_ = false; void SetEventHandler(const std::shared_ptr& handler, const std::shared_ptr& exportHandler = nullptr); diff --git a/window_scene/session/host/src/scene_session.cpp b/window_scene/session/host/src/scene_session.cpp index 2b9115481e0b559ed653d7eea3be40642363b914..772e8b8d4cf54a146d0d29d7cf39266906f22828 100644 --- a/window_scene/session/host/src/scene_session.cpp +++ b/window_scene/session/host/src/scene_session.cpp @@ -375,14 +375,57 @@ void SceneSession::ClearSpecificSessionCbMap() void SceneSession::ClearJsSceneSessionCbMap(bool needRemove) { - if (sessionChangeCallback_ && sessionChangeCallback_->clearCallbackFunc_) { + if (clearCallbackMapFunc_) { TLOGD(WmsLogTag::WMS_LIFE, "id: %{public}d, needRemove: %{public}d", GetPersistentId(), needRemove); - sessionChangeCallback_->clearCallbackFunc_(needRemove); + clearCallbackMapFunc_(needRemove); } else { TLOGE(WmsLogTag::WMS_LIFE, "get callback failed, id: %{public}d", GetPersistentId()); } } +void SceneSession::RegisterShowWhenLockedCallback(NotifyShowWhenLockedFunc&& callback) +{ + const char* const where = __func__; + auto task = [weakThis = wptr(this), callback = std::move(callback), where] { + auto session = weakThis.promote(); + if (!session) { + TLOGNE(WmsLogTag::WMS_LIFE, "%{public}s session is nullptr", where); + return; + } + session->onShowWhenLockedFunc_ = std::move(callback); + session->onShowWhenLockedFunc_(session->GetShowWhenLockedFlagValue()); + }; + PostTask(task, where); +} + +void SceneSession::RegisterForceHideChangeCallback(NotifyForceHideChangeFunc&& callback) +{ + const char* const where = __func__; + auto task = [weakThis = wptr(this), callback = std::move(callback), where] { + auto session = weakThis.promote(); + if (!session) { + TLOGNE(WmsLogTag::WMS_LIFE, "%{public}s session is nullptr", where); + return; + } + session->onForceHideChangeFunc_ = std::move(callback); + }; + PostTask(task, where); +} + +void SceneSession::RegisterClearCallbackMapCallback(ClearCallbackMapFunc&& callback) +{ + const char* const where = __func__; + auto task = [weakThis = wptr(this), callback = std::move(callback), where] { + auto session = weakThis.promote(); + if (!session) { + TLOGNE(WmsLogTag::WMS_LIFE, "%{public}s session is nullptr", where); + return; + } + session->clearCallbackMapFunc_ = std::move(callback); + }; + PostTask(task, where); +} + WSError SceneSession::Disconnect(bool isFromClient, const std::string& identityToken) { if (isFromClient && SessionHelper::IsMainWindow(GetWindowType())) { @@ -1427,8 +1470,8 @@ WSError SceneSession::OnNeedAvoid(bool status) WSError SceneSession::OnShowWhenLocked(bool showWhenLocked) { WLOGFD("SceneSession ShowWhenLocked status:%{public}d", static_cast(showWhenLocked)); - if (sessionChangeCallback_ != nullptr && sessionChangeCallback_->OnShowWhenLocked_) { - sessionChangeCallback_->OnShowWhenLocked_(showWhenLocked); + if (onShowWhenLockedFunc_) { + onShowWhenLockedFunc_(showWhenLocked); } return WSError::WS_OK; } @@ -3148,8 +3191,8 @@ void SceneSession::NotifyForceHideChange(bool hide) return; } property->SetForceHide(hide); - if (sessionChangeCallback_ && sessionChangeCallback_->OnForceHideChange_) { - sessionChangeCallback_->OnForceHideChange_(hide); + if (onForceHideChangeFunc_) { + onForceHideChangeFunc_(hide); } SetForceTouchable(!hide); if (hide) { @@ -5245,11 +5288,8 @@ void SceneSession::UnregisterSessionChangeListeners() session->sessionChangeCallback_->OnSessionEvent_ = nullptr; session->sessionChangeCallback_->onIsCustomAnimationPlaying_ = nullptr; session->sessionChangeCallback_->onWindowAnimationFlagChange_ = nullptr; - session->sessionChangeCallback_->OnShowWhenLocked_ = nullptr; session->sessionChangeCallback_->onRaiseAboveTarget_ = nullptr; - session->sessionChangeCallback_->OnForceHideChange_ = nullptr; session->sessionChangeCallback_->OnTouchOutside_ = nullptr; - session->sessionChangeCallback_->clearCallbackFunc_ = nullptr; session->sessionChangeCallback_->onSetLandscapeMultiWindowFunc_ = nullptr; session->sessionChangeCallback_->onLayoutFullScreenChangeFunc_ = nullptr; session->sessionChangeCallback_->onRestoreMainWindowFunc_ = nullptr; diff --git a/window_scene/session/host/src/session.cpp b/window_scene/session/host/src/session.cpp index 9484c921d3fcc22d8c9ba1ee723bc748002ad251..f6b893e77cec689249bab9ad6057fcd981c47d22 100644 --- a/window_scene/session/host/src/session.cpp +++ b/window_scene/session/host/src/session.cpp @@ -102,6 +102,16 @@ Session::Session(const SessionInfo& info) : sessionInfo_(info) } } +Session::~Session() +{ + TLOGI(WmsLogTag::WMS_LIFE, "id:%{public}d", GetPersistentId()); + if (mainHandler_) { + mainHandler_->PostTask([surfaceNode = std::move(surfaceNode_)]() mutable { + // do nothing + }); + } +} + void Session::SetEventHandler(const std::shared_ptr& handler, const std::shared_ptr& exportHandler) { diff --git a/window_scene/test/unittest/scene_session_test2.cpp b/window_scene/test/unittest/scene_session_test2.cpp index fcfbc5ba6c15126774251b9971195b971cf3f8eb..7f2e4777c5f81e6c056d14314d82557629d33b2f 100644 --- a/window_scene/test/unittest/scene_session_test2.cpp +++ b/window_scene/test/unittest/scene_session_test2.cpp @@ -1086,7 +1086,7 @@ HWTEST_F(SceneSessionTest2, NotifyForceHideChange, Function | SmallTest | Level2 auto func = [sceneSession](bool hide) { sceneSession->SetPrivacyMode(hide); }; - sceneSession->sessionChangeCallback_->OnForceHideChange_ = func; + sceneSession->onForceHideChangeFunc_ = func; EXPECT_NE(nullptr, &func); sceneSession->NotifyForceHideChange(true); diff --git a/window_scene/test/unittest/scene_session_test3.cpp b/window_scene/test/unittest/scene_session_test3.cpp index 01c06ba908f2ed4c1e54e1db537345f5e3c15e14..6edc275fb3327943f3f9f262ef269f1304d219c1 100644 --- a/window_scene/test/unittest/scene_session_test3.cpp +++ b/window_scene/test/unittest/scene_session_test3.cpp @@ -309,7 +309,7 @@ HWTEST_F(SceneSessionTest3, ClearSpecificSessionCbMap1, Function | SmallTest | L sptr sessionChangeCallback = new (std::nothrow) MainSession::SessionChangeCallback(); - sessionChangeCallback->clearCallbackFunc_ = [](bool) {}; + sceneSession->clearCallbackMapFunc_ = [](bool) {}; sceneSession->sessionChangeCallback_ = sessionChangeCallback; sceneSession->ClearSpecificSessionCbMap(); }