diff --git a/window_scene/intention_event/service/anr_manager/include/anr_manager.h b/window_scene/intention_event/service/anr_manager/include/anr_manager.h index 21bc23dcb448cb6f83f05c6ca4075dd09fb38dca..7e0bf439aec2cb36db72f034ee426df6c9fe3ef6 100644 --- a/window_scene/intention_event/service/anr_manager/include/anr_manager.h +++ b/window_scene/intention_event/service/anr_manager/include/anr_manager.h @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -59,6 +60,7 @@ private: std::atomic_int32_t anrTimerCount_ { 0 }; std::mutex mtx_; std::unordered_map applicationMap_; + std::shared_mutex applicationMapMutex_; std::function anrObserver_; std::function appInfoGetter_; }; diff --git a/window_scene/intention_event/service/anr_manager/src/anr_manager.cpp b/window_scene/intention_event/service/anr_manager/src/anr_manager.cpp index c34d5e8c129af0a767200257599cdff3552c9ef1..3f6c06042a33bd85f6c4aeaedbc419f3a3c8510f 100644 --- a/window_scene/intention_event/service/anr_manager/src/anr_manager.cpp +++ b/window_scene/intention_event/service/anr_manager/src/anr_manager.cpp @@ -93,7 +93,7 @@ void ANRManager::MarkProcessed(int32_t eventId, int32_t persistentId) bool ANRManager::IsANRTriggered(int32_t persistentId) { - std::lock_guard guard(mtx_); + std::shared_lock lock(applicationMapMutex_); if (DelayedSingleton::GetInstance()->CheckAnrStatus(persistentId)) { WLOGFE("Application not respond, persistentId:%{public}d -> pid:%{public}d, bundleName:%{public}s", persistentId, applicationMap_[persistentId].pid, applicationMap_[persistentId].bundleName.c_str()); @@ -105,9 +105,13 @@ bool ANRManager::IsANRTriggered(int32_t persistentId) void ANRManager::OnSessionLost(int32_t persistentId) { CALL_DEBUG_ENTER; - std::lock_guard guard(mtx_); - WLOGFD("Disconnect session, persistentId:%{public}d -> pid:%{public}d, bundleName:%{public}s", - persistentId, applicationMap_[persistentId].pid, applicationMap_[persistentId].bundleName.c_str()); + { + std::shared_lock lock(applicationMapMutex_); + if (applicationMap_.find(persistentId) != applicationMap_.end()) { + WLOGFD("Disconnect session, persistentId:%{public}d -> pid:%{public}d, bundleName:%{public}s", persistentId, + applicationMap_[persistentId].pid, applicationMap_[persistentId].bundleName.c_str()); + } + } RemoveTimers(persistentId); RemovePersistentId(persistentId); } @@ -115,16 +119,20 @@ void ANRManager::OnSessionLost(int32_t persistentId) void ANRManager::OnBackground(int32_t persistentId) { CALL_DEBUG_ENTER; - std::lock_guard guard(mtx_); - WLOGFD("Background session, persistentId:%{public}d -> pid:%{public}d, bundleName:%{public}s", - persistentId, applicationMap_[persistentId].pid, applicationMap_[persistentId].bundleName.c_str()); + { + std::shared_lock lock(applicationMapMutex_); + if (applicationMap_.find(persistentId) != applicationMap_.end()) { + WLOGFD("Background session, persistentId:%{public}d -> pid:%{public}d, bundleName:%{public}s", persistentId, + applicationMap_[persistentId].pid, applicationMap_[persistentId].bundleName.c_str()); + } + } RemoveTimers(persistentId); RemovePersistentId(persistentId); } void ANRManager::SetApplicationInfo(int32_t persistentId, int32_t pid, const std::string& bundleName) { - std::lock_guard guard(mtx_); + std::unique_lock lock(applicationMapMutex_); WLOGFD("PersistentId:%{public}d -> pid:%{public}d, bundleName:%{public}s", persistentId, pid, bundleName.c_str()); applicationMap_[persistentId] = { pid, bundleName }; @@ -140,10 +148,13 @@ void ANRManager::SetAnrObserver(std::function anrObserver) ANRManager::AppInfo ANRManager::GetAppInfoByPersistentId(int32_t persistentId) { - if (applicationMap_.find(persistentId) != applicationMap_.end()) { - WLOGFD("PersistentId:%{public}d -> pid:%{public}d, bundleName:%{public}s", - persistentId, applicationMap_[persistentId].pid, applicationMap_[persistentId].bundleName.c_str()); - return applicationMap_[persistentId]; + { + std::shared_lock lock(applicationMapMutex_); + if (applicationMap_.find(persistentId) != applicationMap_.end()) { + WLOGFD("PersistentId:%{public}d -> pid:%{public}d, bundleName:%{public}s", + persistentId, applicationMap_[persistentId].pid, applicationMap_[persistentId].bundleName.c_str()); + return applicationMap_[persistentId]; + } } WLOGFD("No application matches persistentId:%{public}d", persistentId); return ANRManager::AppInfo(); @@ -162,7 +173,10 @@ void ANRManager::RemoveTimers(int32_t persistentId) void ANRManager::RemovePersistentId(int32_t persistentId) { WLOGFD("RemovePersistentId:%{public}d", persistentId); - applicationMap_.erase(persistentId); + { + std::unique_lock lock(applicationMapMutex_); + applicationMap_.erase(persistentId); + } DelayedSingleton::GetInstance()->OnSessionLost(persistentId); } diff --git a/window_scene/session/host/include/scene_session.h b/window_scene/session/host/include/scene_session.h index 8f2fafee7589ef9be9cf94094caa581caaf02387..f883cb9cd78a8e4c75a6b2333440ee9f18feaee5 100644 --- a/window_scene/session/host/include/scene_session.h +++ b/window_scene/session/host/include/scene_session.h @@ -265,8 +265,10 @@ protected: + to_string(rect.posX_) + ", " + to_string(rect.posY_) + "]"; } + mutable std::shared_mutex sessionChangeCallbackMutex_; sptr specificCallback_ = nullptr; sptr sessionChangeCallback_ = nullptr; + mutable std::shared_mutex moveDragControllerMutex_; sptr moveDragController_ = nullptr; private: @@ -295,8 +297,8 @@ private: NotifySessionRectChangeFunc sessionRectChangeFunc_; static wptr enterSession_; static std::mutex enterSessionMutex_; - mutable std::mutex sessionChangeCbMutex_; int32_t collaboratorType_ = CollaboratorType::DEFAULT_TYPE; + mutable std::shared_mutex selfTokenMutex_; sptr selfToken_ = nullptr; WSRect lastSafeRect = { 0, 0, 0, 0 }; std::vector> subSession_; diff --git a/window_scene/session/host/include/session.h b/window_scene/session/host/include/session.h index 3a72762049ac0532a454c7acc729615d92038cec..6fd6a38aed7b4aef4181cf3cc5223b31f5a804cb 100644 --- a/window_scene/session/host/include/session.h +++ b/window_scene/session/host/include/session.h @@ -497,6 +497,7 @@ protected: float pivotY_ = 0.0f; mutable std::mutex dialogVecMutex_; std::vector> dialogVec_; + mutable std::shared_mutex parentSessionMutex_; sptr parentSession_; sptr windowEventChannel_; diff --git a/window_scene/session/host/src/main_session.cpp b/window_scene/session/host/src/main_session.cpp index 5863e4d62bd2cedb8a6cbf4f8215640bd4a7bf18..f449ba6d4e0bb2f6c5c0b665fdb1d17dff7f46c2 100644 --- a/window_scene/session/host/src/main_session.cpp +++ b/window_scene/session/host/src/main_session.cpp @@ -37,10 +37,13 @@ MainSession::MainSession(const SessionInfo& info, const sptrRenameSnapshotFromOldPersistentId(info.persistentId_); } - moveDragController_ = new (std::nothrow) MoveDragController(GetPersistentId()); - if (moveDragController_ != nullptr && specificCallback != nullptr && + { + std::unique_lock lock(moveDragControllerMutex_); + moveDragController_ = new (std::nothrow) MoveDragController(GetPersistentId()); + if (moveDragController_ != nullptr && specificCallback != nullptr && specificCallback->onWindowInputPidChangeCallback_ != nullptr) { - moveDragController_->SetNotifyWindowPidChangeCallback(specificCallback_->onWindowInputPidChangeCallback_); + moveDragController_->SetNotifyWindowPidChangeCallback(specificCallback->onWindowInputPidChangeCallback_); + } } SetMoveDragCallback(); std::string key = GetRatioPreferenceKey(); diff --git a/window_scene/session/host/src/scene_session.cpp b/window_scene/session/host/src/scene_session.cpp index e4df97130a8627be0fef17a20892f3908a05577b..165445f22e32713a1f26836d14f7842a88ba3e2e 100644 --- a/window_scene/session/host/src/scene_session.cpp +++ b/window_scene/session/host/src/scene_session.cpp @@ -305,7 +305,7 @@ WSError SceneSession::OnSessionEvent(SessionEvent event) void SceneSession::RegisterSessionChangeCallback(const sptr& sessionChangeCallback) { - std::lock_guard guard(sessionChangeCbMutex_); + std::unique_lock lock(sessionChangeCallbackMutex_); sessionChangeCallback_ = sessionChangeCallback; } @@ -652,6 +652,7 @@ WSError SceneSession::BindDialogSessionTarget(const sptr& sceneSes TLOGE(WmsLogTag::WMS_DIALOG, "dialog session is null"); return WSError::WS_ERROR_NULLPTR; } + std::shared_lock lock(sessionChangeCallbackMutex_); if (sessionChangeCallback_ != nullptr && sessionChangeCallback_->onBindDialogTarget_) { TLOGI(WmsLogTag::WMS_DIALOG, "id: %{public}d", sceneSession->GetPersistentId()); sessionChangeCallback_->onBindDialogTarget_(sceneSession); @@ -672,6 +673,7 @@ WSError SceneSession::SetSystemBarProperty(WindowType type, SystemBarProperty sy return WSError::WS_ERROR_NULLPTR; } property->SetSystemBarProperty(type, systemBarProperty); + std::shared_lock lock(sessionChangeCallbackMutex_); if (sessionChangeCallback_ != nullptr && sessionChangeCallback_->OnSystemBarPropertyChange_) { sessionChangeCallback_->OnSystemBarPropertyChange_(property->GetSystemBarProperty()); } @@ -729,6 +731,7 @@ WSError SceneSession::OnNeedAvoid(bool status) WSError SceneSession::OnShowWhenLocked(bool showWhenLocked) { WLOGFD("SceneSession ShowWhenLocked status:%{public}d", static_cast(showWhenLocked)); + std::shared_lock lock(sessionChangeCallbackMutex_); if (sessionChangeCallback_ != nullptr && sessionChangeCallback_->OnShowWhenLocked_) { sessionChangeCallback_->OnShowWhenLocked_(showWhenLocked); } @@ -1156,6 +1159,7 @@ WSError SceneSession::TransferPointerEvent(const std::shared_ptr lock(moveDragControllerMutex_); if (!moveDragController_) { WLOGE("moveDragController_ is null"); return Session::TransferPointerEvent(pointerEvent, needNotifyClient); @@ -1366,6 +1370,7 @@ bool SceneSession::FixRectByAspectRatio(WSRect& rect) void SceneSession::SetMoveDragCallback() { + std::shared_lock lock(moveDragControllerMutex_); if (moveDragController_) { MoveDragCallback callBack = [this](const SizeChangeReason& reason) { this->OnMoveDragCallback(reason); @@ -1376,6 +1381,11 @@ void SceneSession::SetMoveDragCallback() void SceneSession::OnMoveDragCallback(const SizeChangeReason& reason) { + std::shared_lock lock(moveDragControllerMutex_); + if (!moveDragController_) { + WLOGE("moveDragController_ is null"); + return; + } WSRect rect = moveDragController_->GetTargetRect(); WLOGFD("OnMoveDragCallback rect: [%{public}d, %{public}d, %{public}u, %{public}u], reason : %{public}d", rect.posX_, rect.posY_, rect.width_, rect.height_, reason); @@ -1679,6 +1689,7 @@ WSError SceneSession::UpdateWindowAnimationFlag(bool needDefaultAnimationFlag) void SceneSession::SetWindowAnimationFlag(bool needDefaultAnimationFlag) { needDefaultAnimationFlag_ = needDefaultAnimationFlag; + std::shared_lock lock(sessionChangeCallbackMutex_); if (sessionChangeCallback_ && sessionChangeCallback_->onWindowAnimationFlagChange_) { sessionChangeCallback_->onWindowAnimationFlagChange_(needDefaultAnimationFlag); } @@ -1704,6 +1715,7 @@ bool SceneSession::IsAppSession() const void SceneSession::NotifyIsCustomAnimationPlaying(bool isPlaying) { WLOGFI("id %{public}d %{public}u", GetPersistentId(), isPlaying); + std::shared_lock lock(sessionChangeCallbackMutex_); if (sessionChangeCallback_ != nullptr && sessionChangeCallback_->onIsCustomAnimationPlaying_) { sessionChangeCallback_->onIsCustomAnimationPlaying_(isPlaying); } @@ -1777,6 +1789,7 @@ void SceneSession::NotifyTouchOutside() WLOGFD("Notify sessionStage TouchOutside"); sessionStage_->NotifyTouchOutside(); } + std::shared_lock lock(sessionChangeCallbackMutex_); if (sessionChangeCallback_ && sessionChangeCallback_->OnTouchOutside_) { WLOGFD("Notify sessionChangeCallback TouchOutside"); sessionChangeCallback_->OnTouchOutside_(); @@ -1794,6 +1807,7 @@ void SceneSession::NotifyWindowVisibility() bool SceneSession::CheckOutTouchOutsideRegister() { + std::shared_lock lock(sessionChangeCallbackMutex_); if (sessionChangeCallback_ && sessionChangeCallback_->OnTouchOutside_) { return true; } @@ -1804,6 +1818,7 @@ void SceneSession::SetRequestedOrientation(Orientation orientation) { WLOGFI("id: %{public}d orientation: %{public}u", GetPersistentId(), static_cast(orientation)); GetSessionProperty()->SetRequestedOrientation(orientation); + std::shared_lock lock(sessionChangeCallbackMutex_); if (sessionChangeCallback_ && sessionChangeCallback_->OnRequestedOrientationChange_) { sessionChangeCallback_->OnRequestedOrientationChange_(static_cast(orientation)); } @@ -1818,6 +1833,7 @@ void SceneSession::NotifyForceHideChange(bool hide) return; } property->SetForceHide(hide); + std::shared_lock lock(sessionChangeCallbackMutex_); if (sessionChangeCallback_ && sessionChangeCallback_->OnForceHideChange_) { sessionChangeCallback_->OnForceHideChange_(hide); } @@ -1877,11 +1893,13 @@ void SceneSession::SetAbilitySessionInfo(std::shared_ptr selfToken) { + std::unique_lock lock(selfTokenMutex_); selfToken_ = selfToken; } sptr SceneSession::GetSelfToken() const { + std::shared_lock lock(selfTokenMutex_); return selfToken_; } @@ -2256,6 +2274,7 @@ std::vector> SceneSession::GetSubSession() const WSRect SceneSession::GetSessionTargetRect() const { WSRect rect; + std::shared_lock lock(moveDragControllerMutex_); if (moveDragController_) { rect = moveDragController_->GetTargetRect(); } else { @@ -2266,6 +2285,7 @@ WSRect SceneSession::GetSessionTargetRect() const void SceneSession::SetWindowDragHotAreaListener(const NotifyWindowDragHotAreaFunc& func) { + std::shared_lock lock(moveDragControllerMutex_); if (moveDragController_) { moveDragController_->SetWindowDragHotAreaFunc(func); } @@ -2358,6 +2378,7 @@ bool SceneSession::IsDirtyWindow() void SceneSession::NotifyUILostFocus() { + std::shared_lock lock(moveDragControllerMutex_); if (moveDragController_) { moveDragController_->OnLostFocus(); } diff --git a/window_scene/session/host/src/session.cpp b/window_scene/session/host/src/session.cpp index b885669546c6314620ac290e39fea55bd675685f..ab7228c01acf1b8220eb9c03f61b661efc70234c 100644 --- a/window_scene/session/host/src/session.cpp +++ b/window_scene/session/host/src/session.cpp @@ -51,8 +51,11 @@ static bool g_enableForceUIFirst = system::GetParameter("window.forceUIFirst.ena Session::Session(const SessionInfo& info) : sessionInfo_(info) { - property_ = new WindowSessionProperty(); - property_->SetWindowType(static_cast(info.windowType_)); + { + std::unique_lock lock(propertyMutex_); + property_ = new WindowSessionProperty(); + property_->SetWindowType(static_cast(info.windowType_)); + } auto runner = AppExecFwk::EventRunner::GetMainEventRunner(); mainHandler_ = std::make_shared(runner); @@ -782,6 +785,7 @@ __attribute__((no_sanitize("cfi"))) WSError Session::Connect(const sptr lock(propertyMutex_); if (property_ && property_->GetIsNeedUpdateWindowMode() && property) { property->SetIsNeedUpdateWindowMode(true); property->SetWindowMode(property_->GetWindowMode()); @@ -1341,6 +1345,7 @@ void Session::SetParentSession(const sptr& session) WLOGFW("Session is nullptr"); return; } + std::unique_lock lock(parentSessionMutex_); parentSession_ = session; TLOGD(WmsLogTag::WMS_SUB, "[WMSDialog][WMSSub]Set parent success, parentId: %{public}d, id: %{public}d", session->GetPersistentId(), GetPersistentId()); @@ -1348,6 +1353,7 @@ void Session::SetParentSession(const sptr& session) sptr Session::GetParentSession() const { + std::shared_lock lock(parentSessionMutex_); return parentSession_; } @@ -1505,11 +1511,14 @@ void Session::NotifyPointerEventToRs(int32_t pointAction) WSError Session::HandleSubWindowClick(int32_t action) { - if (parentSession_ && parentSession_->CheckDialogOnForeground()) { - TLOGD(WmsLogTag::WMS_DIALOG, "Its main window has dialog on foreground, id: %{public}d", GetPersistentId()); - return WSError::WS_ERROR_INVALID_PERMISSION; + { + std::shared_lock lock(parentSessionMutex_); + if (parentSession_ && parentSession_->CheckDialogOnForeground()) { + TLOGD(WmsLogTag::WMS_DIALOG, "Its main window has dialog on foreground, id: %{public}d", GetPersistentId()); + return WSError::WS_ERROR_INVALID_PERMISSION; + } } - + std::shared_lock lock(propertyMutex_); bool raiseEnabled = property_->GetRaiseEnabled() && (action == MMI::PointerEvent::POINTER_ACTION_DOWN || action == MMI::PointerEvent::POINTER_ACTION_BUTTON_DOWN); if (raiseEnabled) { @@ -1543,6 +1552,7 @@ WSError Session::TransferPointerEvent(const std::shared_ptr& return ret; } } else if (GetWindowType() == WindowType::WINDOW_TYPE_DIALOG) { + std::shared_lock lock(parentSessionMutex_); if (parentSession_ && parentSession_->CheckDialogOnForeground() && isPointDown) { parentSession_->HandlePointDownDialog(); if (!IsTopDialog()) { @@ -1918,6 +1928,7 @@ WSError Session::UpdateWindowMode(WindowMode mode) { WLOGFD("Session update window mode, id: %{public}d, mode: %{public}d", GetPersistentId(), static_cast(mode)); + std::shared_lock lock(propertyMutex_); if (property_ == nullptr) { WLOGFD("id: %{public}d property is nullptr", persistentId_); return WSError::WS_ERROR_NULLPTR; @@ -2121,6 +2132,7 @@ WSError Session::UpdateMaximizeMode(bool isMaximize) } else if (GetWindowMode() == WindowMode::WINDOW_MODE_FULLSCREEN) { mode = MaximizeMode::MODE_FULL_FILL; } + std::shared_lock lock(propertyMutex_); property_->SetMaximizeMode(mode); return sessionStage_->UpdateMaximizeMode(mode); } diff --git a/window_scene/session/host/src/sub_session.cpp b/window_scene/session/host/src/sub_session.cpp index bdbc9c2e1545b0d8ed521fb35f7899543914d2be..cd0684cc001ca9bd34756e2f90d15f0eac923b43 100644 --- a/window_scene/session/host/src/sub_session.cpp +++ b/window_scene/session/host/src/sub_session.cpp @@ -30,7 +30,10 @@ constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "SubSes SubSession::SubSession(const SessionInfo& info, const sptr& specificCallback) : SceneSession(info, specificCallback) { - moveDragController_ = new (std::nothrow) MoveDragController(GetPersistentId()); + { + std::unique_lock lock(moveDragControllerMutex_); + moveDragController_ = new (std::nothrow) MoveDragController(GetPersistentId()); + } SetMoveDragCallback(); TLOGD(WmsLogTag::WMS_LIFE, "Create SubSession"); } @@ -111,6 +114,7 @@ WSError SubSession::Reconnect(const sptr& sessionStage, const spt WSError SubSession::ProcessPointDownSession(int32_t posX, int32_t posY) { const auto& id = GetPersistentId(); + std::shared_lock lock(parentSessionMutex_); WLOGFI("id: %{public}d, type: %{public}d", id, GetWindowType()); if (parentSession_ && parentSession_->CheckDialogOnForeground()) { WLOGFI("Has dialog foreground, id: %{public}d, type: %{public}d", id, GetWindowType()); @@ -132,6 +136,7 @@ WSError SubSession::TransferKeyEvent(const std::shared_ptr& keyEv WLOGFE("KeyEvent is nullptr"); return WSError::WS_ERROR_NULLPTR; } + std::shared_lock lock(parentSessionMutex_); if (parentSession_ && parentSession_->CheckDialogOnForeground()) { TLOGD(WmsLogTag::WMS_DIALOG, "Its main window has dialog on foreground, not transfer pointer event"); return WSError::WS_ERROR_INVALID_PERMISSION; @@ -143,6 +148,7 @@ WSError SubSession::TransferKeyEvent(const std::shared_ptr& keyEv int32_t SubSession::GetMissionId() const { + std::shared_lock lock(parentSessionMutex_); return parentSession_ != nullptr ? parentSession_->GetPersistentId() : SceneSession::GetMissionId(); } diff --git a/window_scene/session/host/src/system_session.cpp b/window_scene/session/host/src/system_session.cpp index ae7554948357454395f16f2df7b3ef693e0c5172..aae60be314170584995abeaa01ec8450942cc4ec 100644 --- a/window_scene/session/host/src/system_session.cpp +++ b/window_scene/session/host/src/system_session.cpp @@ -192,6 +192,7 @@ WSError SystemSession::ProcessPointDownSession(int32_t posX, int32_t posY) const auto& id = GetPersistentId(); const auto& type = GetWindowType(); WLOGFI("id: %{public}d, type: %{public}d", id, type); + std::shared_lock lock(parentSessionMutex_); if (parentSession_ && parentSession_->CheckDialogOnForeground()) { WLOGFI("Parent has dialog foreground, id: %{public}d, type: %{public}d", id, type); parentSession_->HandlePointDownDialog(); @@ -219,6 +220,7 @@ WSError SystemSession::TransferKeyEvent(const std::shared_ptr& ke if (keyEvent->GetKeyCode() == MMI::KeyEvent::KEYCODE_BACK) { return WSError::WS_ERROR_INVALID_PERMISSION; } + std::shared_lock lock(parentSessionMutex_); if (parentSession_ && parentSession_->CheckDialogOnForeground() && !IsTopDialog()) { return WSError::WS_ERROR_INVALID_PERMISSION; @@ -268,6 +270,7 @@ WSError SystemSession::NotifyClientToUpdateRect(std::shared_ptr r int32_t SystemSession::GetMissionId() const { + std::shared_lock lock(parentSessionMutex_); return parentSession_ != nullptr ? parentSession_->GetPersistentId() : SceneSession::GetMissionId(); }