diff --git a/interfaces/innerkits/wm/window_manager.h b/interfaces/innerkits/wm/window_manager.h index 9e9713ffedf85c1522988512b90d65909207bda5..6897f34271db13ba4e7306cc447284b28390f8dd 100644 --- a/interfaces/innerkits/wm/window_manager.h +++ b/interfaces/innerkits/wm/window_manager.h @@ -16,19 +16,23 @@ #ifndef OHOS_ROSEN_WINDOW_MANAGER_H #define OHOS_ROSEN_WINDOW_MANAGER_H +#include #include #include #include +#include #include -#include -#include "wm_single_instance.h" -#include "wm_common.h" + #include "dm_common.h" #include "focus_change_info.h" -#include "window_visibility_info.h" -#include "window_drawing_content_info.h" + #include "window.h" +#include "window_drawing_content_info.h" #include "window_pid_visibility_info.h" +#include "window_visibility_info.h" +#include "wm_common.h" +#include "wm_single_instance.h" + namespace OHOS { namespace Rosen { @@ -612,12 +616,15 @@ public: * * @brief WindowManager used to manage window. */ -class WindowManager { +class WindowManager : public RefBase{ WM_DECLARE_SINGLE_INSTANCE_BASE(WindowManager); friend class WindowManagerAgent; friend class WMSDeathRecipient; friend class SSMDeathRecipient; public: + static sptr GetInstance(const int32_t userId); + static WMError RemoveInstanceByUserId(const int32_t userId); + /** * @brief Register WMS connection status changed listener. * @attention Callable only by u0 system user. A process only supports successful registration once. @@ -1350,9 +1357,17 @@ public: WMError RemoveSessionBlackList( const std::unordered_set& bundleNames, const std::unordered_set& privacyWindowTags); + ~WindowManager() override; + private: - WindowManager(); - ~WindowManager(); + /** + * multi user and multi screen + */ + WindowManager(const int32_t userId = INVALID_USER_ID); + int32_t userId_; + static std::unordered_map> windowManagerMap_; + static std::mutex windowManagerMapMutex_; + std::recursive_mutex mutex_; class Impl; std::unique_ptr pImpl_; diff --git a/interfaces/innerkits/wm/window_manager_lite.h b/interfaces/innerkits/wm/window_manager_lite.h index 10359312a4b9d7907eba35dd92fbbc56e6f01ea5..08e9f542e7cc78d2bae590bd3cab9de0cd62a712 100644 --- a/interfaces/innerkits/wm/window_manager_lite.h +++ b/interfaces/innerkits/wm/window_manager_lite.h @@ -35,12 +35,15 @@ namespace Rosen { * * @brief WindowManagerLite used to manage window. */ -class WindowManagerLite { +class WindowManagerLite : public RefBase{ WM_DECLARE_SINGLE_INSTANCE_BASE(WindowManagerLite); friend class WindowManagerAgentLite; friend class WMSDeathRecipient; friend class SSMDeathRecipient; public: + static sptr GetInstance(const int32_t userId); + static WMError RemoveInstanceByUserId(const int32_t userId); + /** * @brief Register focus changed listener. * @@ -365,14 +368,23 @@ public: */ WMError SendPointerEventForHover(const std::shared_ptr& pointerEvent); + ~WindowManagerLite() override; + private: - WindowManagerLite(); - ~WindowManagerLite(); + WindowManagerLite(const int32_t userId = INVALID_USER_ID); + std::recursive_mutex mutex_; class Impl; std::unique_ptr pImpl_; bool destroyed_ = false; + /** + * Multi user and multi screen + */ + int32_t userId_; + static std::unordered_map> windowManagerLiteMap_; + static std::mutex windowManagerLiteMapMutex_; + void UpdateFocusStatus(uint32_t windowId, const sptr& abilityToken, WindowType windowType, DisplayId displayId, bool focused) const; void UpdateFocusChangeInfo(const sptr& focusChangeInfo, bool focused) const; diff --git a/window_scene/session_manager/include/session_manager.h b/window_scene/session_manager/include/session_manager.h index 8db368fa90939df8295b2dfc89d193be57561995..074597f512c7f24153c4fee65db37b2e9eec39f3 100644 --- a/window_scene/session_manager/include/session_manager.h +++ b/window_scene/session_manager/include/session_manager.h @@ -21,25 +21,37 @@ #include "imock_session_manager_interface.h" #include "session_manager_service_interface.h" +#include "wm_common.h" #include "wm_single_instance.h" #include "zidl/scene_session_manager_interface.h" namespace OHOS::Rosen { class SSMDeathRecipient : public IRemoteObject::DeathRecipient { public: + SSMDeathRecipient(const int32_t userId=INVALID_USER_ID); void OnRemoteDied(const wptr& wptrDeath) override; + +private: + int32_t userId_; }; + class FoundationDeathRecipient : public IRemoteObject::DeathRecipient { public: + FoundationDeathRecipient(int32_t userId = INVALID_USER_ID); void OnRemoteDied(const wptr& wptrDeath) override; + +private: + int32_t userId_; }; -class SessionManager { - WM_DECLARE_SINGLE_INSTANCE_BASE(SessionManager); +class SessionManager : public RefBase { +WM_DECLARE_SINGLE_INSTANCE_BASE(SessionManager); public: + static sptrGetInstance(const int32_t userId); + void ClearSessionManagerProxy(); - void Clear(); + void RemoveSSMDeathRecipient(); sptr GetSceneSessionManagerProxy(); void OnFoundationDied(); @@ -62,20 +74,15 @@ public: void RegisterWindowManagerRecoverCallbackFunc(const WindowManagerRecoverCallbackFunc& callbackFunc); void RecoverSessionManagerService(const sptr& sessionManagerService); -protected: - SessionManager() = default; - virtual ~SessionManager(); + ~SessionManager() override; private: + SessionManager(const int32_t userId = INVALID_USER_ID); void InitSessionManagerServiceProxy(); WMError InitMockSMSProxy(); void InitSceneSessionManagerProxy(); - - /* - * Multi User - */ - void OnWMSConnectionChangedCallback(int32_t userId, int32_t screenId, bool isConnected, bool isCallbackRegistered); - void OnUserSwitch(const sptr& sessionManagerService); + void RemoveDeathRecipientFromProxy(const sptr& proxy, + const sptr& recipient); /* * Window Recover @@ -83,25 +90,36 @@ private: void RegisterSMSRecoverListener(); void UnregisterSMSRecoverListener(); - std::recursive_mutex mutex_; + sptr foundationDeath_ = nullptr; + bool isFoundationListenerRegistered_ = false; + std::mutex foundationListenerRegisterdMutex_; sptr mockSessionManagerServiceProxy_ = nullptr; - sptr sessionManagerServiceProxy_ = nullptr; + std::mutex mockSessionManagerServiceMutex_; + + sptr sceneSessionManagerDeath_=nullptr; sptr sceneSessionManagerProxy_ = nullptr; - bool isRecoverListenerRegistered_ = false; + std::mutex sceneSessionManagerMutex_; + + sptr sessionManagerServiceProxy_ = nullptr; + std::mutex sessionManagerServiceMutex_; + sptr smsRecoverListener_ = nullptr; - sptr ssmDeath_ = nullptr; - sptr foundationDeath_ = nullptr; - bool isFoundationListenerRegistered_ = false; - // above guarded by mutex_ + bool isRecoverListenerRegistered_ = false; + std::mutex recoverListenerMutex_; - std::recursive_mutex recoverMutex_; WindowManagerRecoverCallbackFunc windowManagerRecoverFunc_ = nullptr; - // above guarded by recoverMutex_ + std::mutex recoverCallbackMutex_; /* - * Multi User + * Multi User and multi screen */ + void OnWMSConnectionChangedCallback(int32_t userId, int32_t screenId, bool isConnected, bool isCallbackRegistered); + void OnUserSwitch(const sptr& sessionManagerService); + int32_t userId_; + static std::unordered_map> sessionManagerMap_; + static std::mutex sessionManagerMapMutex_; UserSwitchCallbackFunc userSwitchCallbackFunc_ = nullptr; + std::mutex wmsConnectionMutex_; bool isWMSConnected_ = false; int32_t currentWMSUserId_ = INVALID_USER_ID; diff --git a/window_scene/session_manager/include/session_manager_lite.h b/window_scene/session_manager/include/session_manager_lite.h index e298d579bc4e379a1f0e7e5c0086cb01d634282d..4e277f1c2c2f464a4fd0b82258259b3eb95d211c 100644 --- a/window_scene/session_manager/include/session_manager_lite.h +++ b/window_scene/session_manager/include/session_manager_lite.h @@ -17,29 +17,56 @@ #define OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_LITE_H #include +#include #include #include "imock_session_manager_interface.h" #include "session_manager_service_interface.h" +#include "session_manager_service_recover_interface.h" #include "wm_common.h" #include "wm_single_instance.h" #include "zidl/scene_session_manager_lite_interface.h" #include "zidl/screen_session_manager_lite_interface.h" namespace OHOS::Rosen { +class SessionManagerLite; +class SessionManagerServiceLiteRecoverListener : public IRemoteStub { +public: + explicit SessionManagerServiceLiteRecoverListener(sptr sml); + int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; + void OnSessionManagerServiceRecover(const sptr& sessionManagerService) override; + void OnWMSConnectionChanged(int32_t userId, + int32_t screenId, + bool isConnected, + const sptr& sessionManagerService) override; + +private: + sptr sessionManagerLite_; +}; + class SSMDeathRecipientLite : public IRemoteObject::DeathRecipient { public: + SSMDeathRecipientLite(const int32_t userId = INVALID_USER_ID); void OnRemoteDied(const wptr& wptrDeath) override; + +private: + int32_t userId_; }; class FoundationDeathRecipientLite : public IRemoteObject::DeathRecipient { public: + FoundationDeathRecipientLite(const int32_t userId = INVALID_USER_ID); void OnRemoteDied(const wptr& wptrDeath) override; + +private: + int32_t userId_; }; -class SessionManagerLite { +class SessionManagerLite : public RefBase{ WM_DECLARE_SINGLE_INSTANCE_BASE(SessionManagerLite); public: + static sptr GetInstance(const int32_t userId); + void ClearSessionManagerProxy(); void Clear(); @@ -70,8 +97,8 @@ public: void RecoverSessionManagerService(const sptr& sessionManagerService); protected: - SessionManagerLite() = default; - virtual ~SessionManagerLite(); + SessionManagerLite(const int32_t userId = INVALID_USER_ID); + ~SessionManagerLite() override; private: void InitSessionManagerServiceProxy(); @@ -84,8 +111,11 @@ private: WMError InitMockSMSProxy(); /* - * Multi User + * Multi User and multi screen */ + int32_t userId_; + static std::unordered_map> sessionManagerLiteMap_; + static std::mutex sessionManagerLiteMapMutex_; void OnUserSwitch(const sptr& sessionManagerService); void OnWMSConnectionChangedCallback(int32_t userId, int32_t screenId, bool isConnected, bool isCallbackRegistered); diff --git a/window_scene/session_manager/src/session_manager.cpp b/window_scene/session_manager/src/session_manager.cpp index f2d7aa4d73fc5a0563e40446b6a53a79d15035d2..1361353f524382bf90bf9d8308370635c9ea2315 100644 --- a/window_scene/session_manager/src/session_manager.cpp +++ b/window_scene/session_manager/src/session_manager.cpp @@ -20,7 +20,7 @@ #include #include "session_manager_service_recover_interface.h" -#include "singleton_delegator.h" + #include "window_manager_hilog.h" #include "session_manager_lite.h" @@ -28,10 +28,13 @@ namespace OHOS::Rosen { namespace { constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_DISPLAY, "SessionManager" }; } +std::unordered_map>SessionManager::sessionManagerMap_= {}; +std::mutex SessionManager::sessionManagerMapMutex_; class SessionManagerServiceRecoverListener : public IRemoteStub { public: - SessionManagerServiceRecoverListener() = default; + explicit SessionManagerServiceRecoverListener(sptr sessionManager) + : sessionManager_(sessionManager) {} int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override @@ -73,22 +76,31 @@ public: void OnSessionManagerServiceRecover(const sptr& sessionManagerService) override { - SessionManager::GetInstance().Clear(); - SessionManager::GetInstance().ClearSessionManagerProxy(); + if (sessionManager_ == nullptr) { + TLOGE(WmsLogTag::WMS_RECOVER, "sessionManager_ is null"); + return; + } + sessionManager_->RemoveSSMDeathRecipient(); + sessionManager_->ClearSessionManagerProxy(); auto sms = iface_cast(sessionManagerService); - SessionManager::GetInstance().RecoverSessionManagerService(sms); + sessionManager_->RecoverSessionManagerService(sms); } void OnWMSConnectionChanged( int32_t userId, int32_t screenId, bool isConnected, const sptr& sessionManagerService) override { + if (sessionManager_ == nullptr) { + TLOGE(WmsLogTag::WMS_RECOVER, "failed to get sessionManager_"); + return; + } auto sms = iface_cast(sessionManagerService); - SessionManager::GetInstance().OnWMSConnectionChanged(userId, screenId, isConnected, sms); + sessionManager_->OnWMSConnectionChanged(userId, screenId, isConnected, sms); } -}; -WM_IMPLEMENT_SINGLE_INSTANCE(SessionManager) +private: + sptr sessionManager_=nullptr; +}; SessionManager::~SessionManager() { @@ -99,7 +111,42 @@ SessionManager::~SessionManager() if (remoteObject) { remoteObject->RemoveDeathRecipient(foundationDeath_); } - TLOGI(WmsLogTag::WMS_LIFE, "destroyed"); + TLOGI(WmsLogTag::WMS_SCB, "destroyed userId: %{public}d", userId_); +} + +SessionManager::SessionManager(const int32_t userId) : userId_(userId) {} + +SessionManager& SessionManager::GetInstance() +{ + static sptr instance = nullptr; + static std::mutex mtx; + if (instance == nullptr) { + std::lock_guard lock(mtx); + if (instance == nullptr) { + instance = new SessionManager(); + } + } + return *instance; +} + +sptr SessionManager::GetInstance(const int32_t userId) +{ + sptr instance = nullptr; + + if (userId <= INVALID_USER_ID) { + instance = &SessionManager::GetInstance(); + return instance; + } + // multi-instance mode + std::lock_guard lock(sessionManagerMapMutex_); + auto iter = sessionManagerMap_.find(userId); + if(iter != sessionManagerMap_.end()){ + return iter->second; + } + TLOGI(WmsLogTag::WMS_MULTI_USER, "create new instance userId: %{public}d", userId); + instance = new SessionManager(userId); + sessionManagerMap_.insert({ userId, instance }); + return instance; } void SessionManager::OnWMSConnectionChangedCallback( @@ -150,19 +197,21 @@ void SessionManager::OnWMSConnectionChanged( void SessionManager::ClearSessionManagerProxy() { - WLOGFI("enter!"); - std::lock_guard lock(mutex_); - if (sessionManagerServiceProxy_ != nullptr) { - int refCount = sessionManagerServiceProxy_->GetSptrRefCount(); - WLOGFI("sessionManagerServiceProxy GetSptrRefCount : %{public}d", refCount); - sessionManagerServiceProxy_ = nullptr; + TLOGI(WmsLogTag::WMS_SCB, "begin clear proxy"); + { + std::lock_guard lock(sessionManagerServiceMutex_); + if (sessionManagerServiceProxy_ != nullptr) { + int refCount = sessionManagerServiceProxy_->GetSptrRefCount(); + TLOGI(WmsLogTag::WMS_SCB, "ref count: %{public}d", refCount); + sessionManagerServiceProxy_ = nullptr; + } } + std::lock_guard lock(sceneSessionManagerMutex_); sceneSessionManagerProxy_ = nullptr; } __attribute__((no_sanitize("cfi"))) sptr SessionManager::GetSceneSessionManagerProxy() { - std::lock_guard lock(mutex_); InitSessionManagerServiceProxy(); InitSceneSessionManagerProxy(); return sceneSessionManagerProxy_; @@ -170,19 +219,26 @@ __attribute__((no_sanitize("cfi"))) sptr SessionManager::G void SessionManager::InitSessionManagerServiceProxy() { - if (sessionManagerServiceProxy_) { - return; + { + std::lock_guard lock(sessionManagerServiceMutex_); + if (sessionManagerServiceProxy_) { + return; + } } if (InitMockSMSProxy() != WMError::WM_OK) { - WLOGFE("Init mock session manager service proxy failed"); + TLOGE(WmsLogTag::WMS_SCB, "init failed"); return; } RegisterSMSRecoverListener(); - sessionManagerServiceProxy_ = SessionManagerLite::GetInstance().GetSessionManagerServiceProxy(); + auto proxy = SessionManagerLite::GetInstance(userId_)->GetSessionManagerServiceProxy(); + { + std::lock_guard lock(sessionManagerServiceMutex_); + sessionManagerServiceProxy_ = proxy; + } if (!sessionManagerServiceProxy_) { - WLOGFE("sessionManagerServiceProxy is nullptr"); + TLOGE(WmsLogTag::WMS_SCB, "failed to get sms proxy"); } } @@ -191,106 +247,143 @@ WMError SessionManager::InitMockSMSProxy() sptr systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (!systemAbilityManager) { - WLOGFE("Failed to get system ability mgr."); + TLOGE(WmsLogTag::WMS_SCB, "get mgr failed"); return WMError::WM_ERROR_NULLPTR; } sptr remoteObject = systemAbilityManager->GetSystemAbility(WINDOW_MANAGER_SERVICE_ID); if (!remoteObject) { - WLOGFE("Remote object is nullptr"); + TLOGE(WmsLogTag::WMS_SCB, "Remote object is nullptr"); return WMError::WM_ERROR_NULLPTR; } - mockSessionManagerServiceProxy_ = iface_cast(remoteObject); - if (!mockSessionManagerServiceProxy_) { - WLOGFW("Get mock session manager service proxy failed, nullptr"); - return WMError::WM_ERROR_NULLPTR; + { + std::lock_guard lock(mockSessionManagerServiceMutex_); + mockSessionManagerServiceProxy_ = iface_cast(remoteObject); + if (!mockSessionManagerServiceProxy_) { + TLOGE(WmsLogTag::WMS_SCB, "get mock proxy failed"); + return WMError::WM_ERROR_NULLPTR; + } } - if (GetUserIdByUid(getuid()) != SYSTEM_USERID || isFoundationListenerRegistered_) { + if (GetUserIdByUid(getuid()) != SYSTEM_USERID) { return WMError::WM_OK; } + { + std::lock_guard lock(foundationListenerRegisterdMutex_); + if (isFoundationListenerRegistered_) { + return WMError::WM_OK; + } + } if (!foundationDeath_) { - foundationDeath_ = sptr::MakeSptr(); + foundationDeath_ = sptr::MakeSptr(userId_); } if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(foundationDeath_)) { - WLOGFE("Failed to add death recipient"); + TLOGE(WmsLogTag::WMS_SCB, "Failed to add death recipient"); return WMError::WM_ERROR_IPC_FAILED; } + std::lock_guard lock(foundationListenerRegisterdMutex_); isFoundationListenerRegistered_ = true; return WMError::WM_OK; } __attribute__((no_sanitize("cfi"))) void SessionManager::InitSceneSessionManagerProxy() { - if (sceneSessionManagerProxy_) { - return; - } - if (!sessionManagerServiceProxy_) { - WLOGFE("sessionManagerServiceProxy is nullptr"); - return; + TLOGI(WmsLogTag::WMS_SCB, "init ssm proxy"); + { + std::lock_guard lock(sceneSessionManagerMutex_); + if(sceneSessionManagerProxy_) { + return; + } } - - sptr remoteObject = sessionManagerServiceProxy_->GetSceneSessionManager(); - if (!remoteObject) { - WLOGFW("Get scene session manager proxy failed, null"); - return; + sptr remoteObject = nullptr; + { + std::lock_guard lock(sessionManagerServiceMutex_); + if(sessionManagerServiceProxy_ == nullptr) { + TLOGE(WmsLogTag::WMS_SCB, "get sms proxy failed"); + return; + } + remoteObject = sessionManagerServiceProxy_->GetSceneSessionManager(); + if(remoteObject == nullptr){ + TLOGE(WmsLogTag::WMS_SCB, "get sms proxy failed"); + return; + } } - sceneSessionManagerProxy_ = iface_cast(remoteObject); - if (sceneSessionManagerProxy_) { - ssmDeath_ = sptr::MakeSptr(); - if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(ssmDeath_)) { - WLOGFE("Failed to add death recipient"); + sceneSessionManagerDeath_ = sptr::MakeSptr(userId_); + { + std::lock_guard lock(sceneSessionManagerMutex_); + sceneSessionManagerProxy_ = iface_cast(remoteObject); + if (sceneSessionManagerProxy_ == nullptr) { + TLOGE(WmsLogTag::WMS_SCB, "Get scene session manager proxy failed"); return; } - } else { - WLOGFW("Get scene session manager proxy failed, iface_cast null"); + } + if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(sceneSessionManagerDeath_)) { + TLOGE(WmsLogTag::WMS_SCB, "failed to add death recipient"); + return; } } void SessionManager::RegisterSMSRecoverListener() { - std::lock_guard lock(mutex_); - if (!isRecoverListenerRegistered_) { - if (!mockSessionManagerServiceProxy_) { - TLOGE(WmsLogTag::WMS_RECOVER, "mockSessionManagerServiceProxy is null"); + { + std::lock_guard lock(recoverListenerMutex_); + if (isRecoverListenerRegistered_){ + TLOGI(WmsLogTag::WMS_RECOVER, "listener has been registered"); + return; + } + } + { + std::lock_guard lock(mockSessionManagerServiceMutex_); + if (mockSessionManagerServiceProxy_ == nullptr){ + TLOGE(WmsLogTag::WMS_SCB, "Get mock sms proxy failed"); return; } + } + TLOGI(WmsLogTag::WMS_RECOVER, "begin register listener"); + sptr sm(this); + smsRecoverListener_ = sptr::MakeSptr(sm); + { + TLOGD(WmsLogTag::WMS_RECOVER, "Register recover listener, userId_: %{public}d", userId_); + std::lock_guard lock(mockSessionManagerServiceMutex_); + mockSessionManagerServiceProxy_->RegisterSMSRecoverListener(smsRecoverListener_, userId_); + } + { + std::lock_guard lock(recoverListenerMutex_); isRecoverListenerRegistered_ = true; - TLOGD(WmsLogTag::WMS_RECOVER, "Register recover listener"); - smsRecoverListener_ = sptr::MakeSptr(); - std::string identity = IPCSkeleton::ResetCallingIdentity(); - mockSessionManagerServiceProxy_->RegisterSMSRecoverListener(smsRecoverListener_); - IPCSkeleton::SetCallingIdentity(identity); } + std::string identity = IPCSkeleton::ResetCallingIdentity(); + IPCSkeleton::SetCallingIdentity(identity); } void SessionManager::UnregisterSMSRecoverListener() { - std::lock_guard lock(mutex_); - isRecoverListenerRegistered_ = false; - if (mockSessionManagerServiceProxy_) { - mockSessionManagerServiceProxy_->UnregisterSMSRecoverListener(); + TLOGI(WmsLogTag::WMS_RECOVER, "begin unregister listener"); + { + std::lock_guard lock(recoverListenerMutex_); + isRecoverListenerRegistered_ = false; } + TLOGD(WmsLogTag::WMS_RECOVER, "UnRegister recover listener, userId_: %{public}d", userId_); + mockSessionManagerServiceProxy_->UnregisterSMSRecoverListener(userId_); } void SessionManager::RegisterWindowManagerRecoverCallbackFunc(const WindowManagerRecoverCallbackFunc& callbackFunc) { - std::lock_guard lock(recoverMutex_); + std::lock_guard lock(recoverCallbackMutex_); windowManagerRecoverFunc_ = callbackFunc; } void SessionManager::RecoverSessionManagerService(const sptr& sessionManagerService) { + TLOGI(WmsLogTag::WMS_RECOVER, "recover sms proxy"); { - std::lock_guard lock(mutex_); + std::lock_guard lock(sessionManagerServiceMutex_); sessionManagerServiceProxy_ = sessionManagerService; } { - std::lock_guard lock(recoverMutex_); - TLOGI(WmsLogTag::WMS_RECOVER, "Run recover"); - if (windowManagerRecoverFunc_ != nullptr) { - TLOGD(WmsLogTag::WMS_RECOVER, "windowManagerRecover"); + std::lock_guard lock(recoverCallbackMutex_) + if (windowManagerRecoverFunc_) { + TLOGD(WmsLogTag::WMS_RECOVER, "begin recover"); windowManagerRecoverFunc_(); } } @@ -298,42 +391,54 @@ void SessionManager::RecoverSessionManagerService(const sptr& sessionManagerService) { - TLOGI(WmsLogTag::WMS_MULTI_USER, "User switched"); - Clear(); + TLOGI(WmsLogTag::WMS_MULTI_USER, "begin user switch"); + RemoveSSMDeathRecipient(); ClearSessionManagerProxy(); { - std::lock_guard lock(mutex_); + std::lock_guard lock(sessionManagerServiceMutex_); sessionManagerServiceProxy_ = sessionManagerService; + } InitSceneSessionManagerProxy(); - if (!sceneSessionManagerProxy_) { - TLOGE(WmsLogTag::WMS_MULTI_USER, "sceneSessionManagerProxy is null"); + { + std::lock_guard lock(sceneSessionManagerMutex_); + if (sceneSessionManagerProxy_ == nullptr) { + TLOGE(WmsLogTag::WMS_MULTI_USER, "init ssm proxy failed"); return; } } if (userSwitchCallbackFunc_) { - TLOGD(WmsLogTag::WMS_MULTI_USER, "User switch callback"); + TLOGD(WmsLogTag::WMS_MULTI_USER, "run user switch callback"); userSwitchCallbackFunc_(); } } -void SessionManager::Clear() +void SessionManager::RemoveSSMDeathRecipient() { - std::lock_guard lock(mutex_); - if ((sceneSessionManagerProxy_ != nullptr) && (sceneSessionManagerProxy_->AsObject() != nullptr)) { - sceneSessionManagerProxy_->AsObject()->RemoveDeathRecipient(ssmDeath_); + TLOGI(WmsLogTag::WMS_SCB, "begin remove ssm death recipient"); + sptr remoteObject = nullptr; + { + std::lock_guard lock(sceneSessionManagerMutex_); + if (sceneSessionManagerProxy_){ + remoteObject = sceneSessionManagerProxy_->AsObject(); + } + } + if(remoteObject) { + remoteObject->RemoveDeathRecipient(sceneSessionManagerDeath_); } } WMError SessionManager::RegisterWMSConnectionChangedListener(const WMSConnectionChangedCallbackFunc& callbackFunc) { - TLOGD(WmsLogTag::WMS_MULTI_USER, "in"); + TLOGD(WmsLogTag::WMS_MULTI_USER, "register wms connection changed listener"); + auto ret = WMError::WM_OK; + bool isWMSAlreadyConnected = false; + int32_t userId = INVALID_USER_ID; + int32_t screenId = DEFAULT_SCREEN_ID; + if (callbackFunc == nullptr) { TLOGE(WmsLogTag::WMS_MULTI_USER, "callbackFunc is null"); return WMError::WM_ERROR_NULLPTR; } - bool isWMSAlreadyConnected = false; - int32_t userId = INVALID_USER_ID; - int32_t screenId = DEFAULT_SCREEN_ID; { // The mutex ensures the timing of the following variable states in multiple threads std::lock_guard lock(wmsConnectionMutex_); @@ -346,14 +451,12 @@ WMError SessionManager::RegisterWMSConnectionChangedListener(const WMSConnection TLOGI(WmsLogTag::WMS_MULTI_USER, "WMS already connected, notify immediately"); OnWMSConnectionChangedCallback(userId, screenId, true, true); } - { - std::lock_guard lock(mutex_); - auto ret = InitMockSMSProxy(); - if (ret != WMError::WM_OK) { - TLOGE(WmsLogTag::WMS_MULTI_USER, "Init mock session manager service failed"); - return ret; - } - } + + ret = InitMockSMSProxy(); + if (ret != WMError::WM_OK) { + TLOGE(WmsLogTag::WMS_MULTI_USER, "Init mock session manager service failed"); + return ret; + } RegisterSMSRecoverListener(); return WMError::WM_OK; } @@ -376,29 +479,45 @@ void SessionManager::RegisterUserSwitchListener(const UserSwitchCallbackFunc& ca void SessionManager::OnFoundationDied() { - TLOGI(WmsLogTag::WMS_RECOVER, "enter"); + TLOGI(WmsLogTag::WMS_SCB, "begin clear"); + { + std::lock_guard lock(foundationListenerRegisterdMutex_); + isFoundationListenerRegistered_ = false; + } { std::lock_guard lock(wmsConnectionMutex_); isWMSConnected_ = false; } - std::lock_guard lock(mutex_); - isFoundationListenerRegistered_ = false; - isRecoverListenerRegistered_ = false; + { + std::lock_guard lock(recoverListenerMutex_); + isRecoverListenerRegistered_ = false; + } + { + std::lock_guard lock(sessionManagerServiceMutex_); + sessionManagerServiceProxy_ = nullptr; + } + { + std::lock_guard lock(sceneSessionManagerMutex_); + sceneSessionManagerProxy_ = nullptr; + } + std::lock_guard lock(mockSessionManagerServiceMutex_); mockSessionManagerServiceProxy_ = nullptr; - sessionManagerServiceProxy_ = nullptr; - sceneSessionManagerProxy_ = nullptr; } +FoundationDeathRecipient::FoundationDeathRecipient(int32_t userId) : userId_(userId) {} + void FoundationDeathRecipient::OnRemoteDied(const wptr& wptrDeath) { - TLOGI(WmsLogTag::WMS_RECOVER, "Foundation died"); - SessionManager::GetInstance().OnFoundationDied(); + TLOGI(WmsLogTag::WMS_RECOVER, "mock sms proxy died, userId_: %{public}d", userId_"); + SessionManager::GetInstance(userId_)->OnFoundationDied(); } +SSMDeathRecipient::SSMDeathRecipient(int32_t userId) : userId_(userId) {} + void SSMDeathRecipient::OnRemoteDied(const wptr& wptrDeath) { - WLOGI("ssm OnRemoteDied"); - SessionManager::GetInstance().Clear(); - SessionManager::GetInstance().ClearSessionManagerProxy(); + TLOGI(WmsLogTag::WMS_RECOVER, "ssm proxy died, userId_: %{public}d", userId_"); + SessionManager::GetInstance(userId_)->RemoveSSMDeathRecipient(); + SessionManager::GetInstance(userId_)->ClearSessionManagerProxy(); } } // namespace OHOS::Rosen diff --git a/window_scene/session_manager/src/session_manager_lite.cpp b/window_scene/session_manager/src/session_manager_lite.cpp index bfa0e313d57cd40d5818ba0b05f47bd0ad22fafe..e1eccdaa22629a3a45ac7fa0d136dc81da497407 100644 --- a/window_scene/session_manager/src/session_manager_lite.cpp +++ b/window_scene/session_manager/src/session_manager_lite.cpp @@ -19,7 +19,6 @@ #include #include -#include "session_manager_service_recover_interface.h" #include "scene_session_manager_lite_proxy.h" #include "window_manager_hilog.h" @@ -27,65 +26,81 @@ namespace OHOS::Rosen { namespace { constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "SessionManagerLite" }; } +std::unordered_map> SessionManagerLite::sessionManagerLiteMap_ = {}; +std::mutex SessionManagerLite::sessionManagerLiteMapMutex_; -class SessionManagerServiceLiteRecoverListener : public IRemoteStub { -public: - SessionManagerServiceLiteRecoverListener() = default; +SessionManagerServiceLiteRecoverListener::SessionManagerServiceLiteRecoverListener(sptr sml) + : sessionManagerLite_(sml) +{ +} - int32_t OnRemoteRequest( - uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override - { - if (data.ReadInterfaceToken() != GetDescriptor()) { - TLOGE(WmsLogTag::WMS_RECOVER, "InterfaceToken check failed"); - return ERR_TRANSACTION_FAILED; +int32_t SessionManagerServiceLiteRecoverListener::OnRemoteRequest(uint32_t code, + MessageParcel& data, + MessageParcel& reply, + MessageOption& option) override +{ + if (data.ReadInterfaceToken() != GetDescriptor()) { + TLOGE(WmsLogTag::WMS_RECOVER, "InterfaceToken check failed"); + return ERR_TRANSACTION_FAILED; + } + auto msgId = static_cast(code); + switch (msgId) { + case SessionManagerServiceRecoverMessage::TRANS_ID_ON_SESSION_MANAGER_SERVICE_RECOVER: { + auto sessionManagerService = data.ReadRemoteObject(); + // Even if sessionManagerService is null, the recovery process is still required. + OnSessionManagerServiceRecover(sessionManagerService); + break; } - auto msgId = static_cast(code); - switch (msgId) { - case SessionManagerServiceRecoverMessage::TRANS_ID_ON_SESSION_MANAGER_SERVICE_RECOVER: { - auto sessionManagerService = data.ReadRemoteObject(); - // Even if sessionManagerService is null, the recovery process is still required. - OnSessionManagerServiceRecover(sessionManagerService); - break; + case SessionManagerServiceRecoverMessage::TRANS_ID_ON_WMS_CONNECTION_CHANGED: { + int32_t userId = INVALID_USER_ID; + int32_t screenId = DEFAULT_SCREEN_ID; + bool isConnected = false; + if (!data.ReadInt32(userId) || !data.ReadInt32(screenId) || !data.ReadBool(isConnected)) { + TLOGE(WmsLogTag::WMS_MULTI_USER, "Read data failed in lite!"); + return ERR_TRANSACTION_FAILED; } - case SessionManagerServiceRecoverMessage::TRANS_ID_ON_WMS_CONNECTION_CHANGED: { - int32_t userId = INVALID_USER_ID; - int32_t screenId = DEFAULT_SCREEN_ID; - bool isConnected = false; - if (!data.ReadInt32(userId) || !data.ReadInt32(screenId) || !data.ReadBool(isConnected)) { - TLOGE(WmsLogTag::WMS_MULTI_USER, "Read data failed in lite!"); - return ERR_TRANSACTION_FAILED; - } - if (isConnected) { - // Even if data.ReadRemoteObject() is null, the WMS connection still needs to be notified. - OnWMSConnectionChanged(userId, screenId, isConnected, data.ReadRemoteObject()); - } else { - OnWMSConnectionChanged(userId, screenId, isConnected, nullptr); - } - break; + if (isConnected) { + // Even if data.ReadRemoteObject() is null, the WMS connection still needs to be notified. + OnWMSConnectionChanged(userId, screenId, isConnected, data.ReadRemoteObject()); + } else { + OnWMSConnectionChanged(userId, screenId, isConnected, nullptr); } - default: - TLOGW(WmsLogTag::WMS_RECOVER, "unknown transaction code %{public}d", code); - return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + break; } - return ERR_NONE; + default: + TLOGW(WmsLogTag::WMS_RECOVER, "unknown transaction code %{public}d", code); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } + return ERR_NONE; +} - void OnSessionManagerServiceRecover(const sptr& sessionManagerService) override - { - SessionManagerLite::GetInstance().Clear(); - SessionManagerLite::GetInstance().ClearSessionManagerProxy(); - - auto sms = iface_cast(sessionManagerService); - SessionManagerLite::GetInstance().RecoverSessionManagerService(sms); +void SessionManagerServiceLiteRecoverListener::OnSessionManagerServiceRecover( + const sptr& sessionManagerService) override +{ + if (sessionManagerLite_ == nullptr) { + TLOGE(WmsLogTag::WMS_MULTI_USER, "failed to get sessionManagerLite_"); + return; } + sessionManagerLite_->clear(); + sessionManagerLite_->ClearSessionManagerProxy(); - void OnWMSConnectionChanged( - int32_t userId, int32_t screenId, bool isConnected, const sptr& sessionManagerService) override - { - auto sms = iface_cast(sessionManagerService); - SessionManagerLite::GetInstance().OnWMSConnectionChanged(userId, screenId, isConnected, sms); + auto sms = iface_cast(sessionManagerService); + sessionManagerLite_->RecoverSessionManagerService(sms); +} + +void SessionManagerServiceLiteRecoverListener::OnWMSConnectionChanged( + int32_t userId, + int32_t screenId, + bool isConnected, + const sptr& sessionManagerService) override +{ + if (sessionManagerLite_ == nullptr) { + TLOGE(WmsLogTag::WMS_MULTI_USER, "failed to get sessionManagerLite_"); + return; } -}; + auto sms = iface_cast(sessionManagerService); + sessionManagerLite_->OnWMSConnectionChanged(userId, screenId, isConnected, sms); +} class SceneSessionManagerLiteProxyMock : public SceneSessionManagerLiteProxy { public: @@ -114,12 +129,45 @@ public: SessionManagerLite::GetInstance().DeleteSessionListener(listener); return ret; } - + private: static inline BrokerDelegator delegator_; }; -WM_IMPLEMENT_SINGLE_INSTANCE(SessionManagerLite) +SessionManagerLite& SessionManagerLite::GetInstance() +{ + static sptr instance = nullptr; + static std::mutex mtx; + if (instance == nullptr) { + std::lock_guard lock(mtx); + if (instance == nullptr) { + instance = new SessionManagerLite(); + } + } + return *instance; +} + +sptr SessionManagerLite::GetInstance(const int32_t userId) +{ + sptr instance = nullptr; + + if (userId <= INVALID_USER_ID) { + instance = &SessionManagerLite::GetInstance(); + return instance; + } + //multi-instance mode + std::lock_guard lock(sessionManagerLiteMapMutex_); + auto iter = sessionManagerLiteMap_.find(userId); + if (iter != sessionManagerLiteMap_.end()) { + return iter->second; + } + TLOGI(WmsLogTag::WMS_MULTI_USER, "create new instance userId: %{public}d", userId); + instance = new SessionManagerLite(userId); + sessionManagerLiteMap_.insert({userId, instance}); + return instance; +} + +SessionManagerLite::SessionManagerLite(const int32_t userId) : userId_(userId) {} SessionManagerLite::~SessionManagerLite() { @@ -130,7 +178,7 @@ SessionManagerLite::~SessionManagerLite() if (remoteObject) { remoteObject->RemoveDeathRecipient(foundationDeath_); } - TLOGI(WmsLogTag::WMS_LIFE, "destroyed"); + TLOGI(WmsLogTag::WMS_SCB, "destroyed userId: %{public}d", userId_); } void SessionManagerLite::ClearSessionManagerProxy() @@ -312,7 +360,12 @@ void SessionManagerLite::InitSessionManagerServiceProxy() } RegisterSMSRecoverListener(); sptr remoteObject = nullptr; - int32_t errCode = mockSessionManagerServiceProxy_->GetSessionManagerService(remoteObject); + int32_t errCode = ERR_NONE; + if (userId_ == INVALID_USER_ID) { + errCode = mockSessionManagerServiceProxy_->GetSessionManagerService(remoteObject); + } else { + errCode = mockSessionManagerServiceProxy_->GetSessionManagerService(userId_, remoteObject); + } if (errCode != ERR_NONE) { TLOGE(WmsLogTag::DEFAULT, "userId is illegal"); } @@ -365,7 +418,7 @@ void SessionManagerLite::InitSceneSessionManagerLiteProxy() } sceneSessionManagerLiteProxy_ = iface_cast(remoteObject); if (sceneSessionManagerLiteProxy_) { - ssmDeath_ = sptr::MakeSptr(); + ssmDeath_ = sptr::MakeSptr(userId_); if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(ssmDeath_)) { WLOGFE("Failed to add death recipient"); return; @@ -383,11 +436,13 @@ void SessionManagerLite::Clear() } } +SSMDeathRecipientLite::SSMDeathRecipientLite(const int32_t userId) : userId_(userId) {} + void SSMDeathRecipientLite::OnRemoteDied(const wptr& wptrDeath) { - WLOGI("ssm OnRemoteDied"); - SessionManagerLite::GetInstance().Clear(); - SessionManagerLite::GetInstance().ClearSessionManagerProxy(); + TLOGI(WmsLogTag::WMS_SCB, "ssm lite OnRemoteDied"); + SessionManagerLite::GetInstance(userId_)->Clear(); + SessionManagerLite::GetInstance(userId_)->ClearSessionManagerProxy(); } WMError SessionManagerLite::RegisterWMSConnectionChangedListener(const WMSConnectionChangedCallbackFunc& callbackFunc) @@ -457,7 +512,7 @@ WMError SessionManagerLite::InitMockSMSProxy() return WMError::WM_OK; } if (!foundationDeath_) { - foundationDeath_ = sptr::MakeSptr(); + foundationDeath_ = sptr::MakeSptr(userId_); } if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(foundationDeath_)) { TLOGE(WmsLogTag::WMS_MULTI_USER, "Failed to add death recipient"); @@ -477,8 +532,10 @@ void SessionManagerLite::RegisterSMSRecoverListener() } recoverListenerRegistered_ = true; TLOGD(WmsLogTag::WMS_RECOVER, "Register recover listener"); - smsRecoverListener_ = sptr::MakeSptr(); + sptr sml(this); + smsRecoverListener_ = sptr::MakeSptr(sml); std::string identity = IPCSkeleton::ResetCallingIdentity(); + TLOGD(WmsLogTag::WMS_RECOVER, "Register recover listener, userId: %{public}d", userId_); mockSessionManagerServiceProxy_->RegisterSMSLiteRecoverListener(smsRecoverListener_); IPCSkeleton::SetCallingIdentity(identity); } @@ -489,7 +546,8 @@ void SessionManagerLite::UnregisterSMSRecoverListener() std::lock_guard lock(mutex_); recoverListenerRegistered_ = false; if (mockSessionManagerServiceProxy_) { - mockSessionManagerServiceProxy_->UnregisterSMSLiteRecoverListener(); + TLOGD(WmsLogTag::WMS_RECOVER, "Unregister recover listener, userId: %{public}d", userId_); + mockSessionManagerServiceProxy_->UnregisterSMSLiteRecoverListener(userId_); } } @@ -506,10 +564,12 @@ void SessionManagerLite::OnWMSConnectionChangedCallback( } } +FoundationDeathRecipientLite::FoundationDeathRecipientLite(const int32_t userId) : userId_(userId) {} + void FoundationDeathRecipientLite::OnRemoteDied(const wptr& wptrDeath) { TLOGI(WmsLogTag::WMS_RECOVER, "Foundation died"); - SessionManagerLite::GetInstance().OnFoundationDied(); + SessionManagerLite::GetInstance(userId_)->OnFoundationDied(); } void SessionManagerLite::OnFoundationDied() diff --git a/window_scene/test/unittest/multi_user/session_manager_lite_test.cpp b/window_scene/test/unittest/multi_user/session_manager_lite_test.cpp index c965c173a79986465c4755b7ed7744a6cd23a106..6bb7cf6f6659d65840f0558b7b1a9c9e27165cbe 100644 --- a/window_scene/test/unittest/multi_user/session_manager_lite_test.cpp +++ b/window_scene/test/unittest/multi_user/session_manager_lite_test.cpp @@ -233,14 +233,9 @@ HWTEST_F(SessionManagerLiteTest, RegisterUserSwitchListener, Function | SmallTes HWTEST_F(SessionManagerLiteTest, UnregisterWMSConnectionChangedListener, Function | SmallTest | Level2) { ASSERT_NE(nullptr, sml_); - - sml_->mockSessionManagerServiceProxy_ = nullptr; - auto ret = sml_->UnregisterWMSConnectionChangedListener(); - ASSERT_EQ(WMError::WM_OK, ret); - sptr remoteObject = sptr::MakeSptr(); sml_->mockSessionManagerServiceProxy_ = iface_cast(remoteObject); - ret = sml_->UnregisterWMSConnectionChangedListener(); + auto ret = sml_->UnregisterWMSConnectionChangedListener(); ASSERT_EQ(WMError::WM_OK, ret); } } // namespace Rosen diff --git a/window_scene/test/unittest/multi_user/session_manager_test.cpp b/window_scene/test/unittest/multi_user/session_manager_test.cpp index 3e48443880dfafd39b6028af7edbea9253c2c47b..48cf9f3054af6c8601c6ff9d47cb0cb47404a9d0 100644 --- a/window_scene/test/unittest/multi_user/session_manager_test.cpp +++ b/window_scene/test/unittest/multi_user/session_manager_test.cpp @@ -51,7 +51,7 @@ void SessionManagerTest::TearDown() HWTEST_F(SessionManagerTest, GetSceneSessionManagerProxy, Function | SmallTest | Level2) { ASSERT_NE(nullptr, sm_); - sm_->Clear(); + sm_->RemoveSSMDeathRecipient(); sm_->ClearSessionManagerProxy(); auto sceneSessionManagerProxy = sm_->GetSceneSessionManagerProxy(); ASSERT_NE(nullptr, sceneSessionManagerProxy); @@ -208,14 +208,9 @@ HWTEST_F(SessionManagerTest, RegisterWMSConnectionChangedListener1, Function | S HWTEST_F(SessionManagerTest, UnregisterWMSConnectionChangedListener, Function | SmallTest | Level2) { ASSERT_NE(nullptr, sm_); - - sm_->mockSessionManagerServiceProxy_ = nullptr; - auto ret = sm_->UnregisterWMSConnectionChangedListener(); - ASSERT_EQ(WMError::WM_OK, ret); - sptr remoteObject = sptr::MakeSptr(); sm_->mockSessionManagerServiceProxy_ = iface_cast(remoteObject); - ret = sm_->UnregisterWMSConnectionChangedListener(); + auto ret = sm_->UnregisterWMSConnectionChangedListener(); ASSERT_EQ(WMError::WM_OK, ret); } } // namespace Rosen diff --git a/window_scene/test/unittest/session_manager_lite_test.cpp b/window_scene/test/unittest/session_manager_lite_test.cpp index 59d29b6003d570ba1d3e73f93e724aa475fba3ac..38b7dc88df88d061026fbbdc6752116f9fa43003 100644 --- a/window_scene/test/unittest/session_manager_lite_test.cpp +++ b/window_scene/test/unittest/session_manager_lite_test.cpp @@ -46,13 +46,10 @@ void SessionManagerLiteTest::TearDownTestCase() {} void SessionManagerLiteTest::SetUp() { - sml_ = std::make_shared(); - ASSERT_NE(nullptr, sml_); } void SessionManagerLiteTest::TearDown() { - sml_ = nullptr; } namespace { @@ -63,13 +60,14 @@ namespace { */ HWTEST_F(SessionManagerLiteTest, RecoverSessionManagerService, TestSize.Level1) { - ASSERT_NE(nullptr, sml_); + int32_t userId = -1; + auto sml = SessionManagerLite::GetInstance(userId); bool funcInvoked = false; - sml_->RecoverSessionManagerService(nullptr); + sml->RecoverSessionManagerService(nullptr); ASSERT_EQ(funcInvoked, false); - sml_->userSwitchCallbackFunc_ = [&]() { funcInvoked = true; }; - sml_->RecoverSessionManagerService(nullptr); + sml->userSwitchCallbackFunc_ = [&]() { funcInvoked = true; }; + sml->RecoverSessionManagerService(nullptr); ASSERT_EQ(funcInvoked, true); } @@ -80,14 +78,16 @@ HWTEST_F(SessionManagerLiteTest, RecoverSessionManagerService, TestSize.Level1) */ HWTEST_F(SessionManagerLiteTest, ReregisterSessionListener, TestSize.Level1) { - ASSERT_NE(nullptr, sml_); - sml_->ReregisterSessionListener(); - ASSERT_EQ(nullptr, sml_->sceneSessionManagerLiteProxy_); - - sml_->recoverListenerRegistered_ = true; - sml_->GetSceneSessionManagerLiteProxy(); - sml_->ReregisterSessionListener(); - ASSERT_NE(nullptr, sml_->sceneSessionManagerLiteProxy_); + int32_t userId = -1; + auto sml = SessionManagerLite::GetInstance(userId); + ASSERT_NE(nullptr, sml); + sml->ReregisterSessionListener(); + ASSERT_EQ(nullptr, sml->sceneSessionManagerLiteProxy_); + + sml->recoverListenerRegistered_ = true; + sml->GetSceneSessionManagerLiteProxy(); + sml->ReregisterSessionListener(); + ASSERT_NE(nullptr, sml->sceneSessionManagerLiteProxy_); } /** @@ -97,16 +97,18 @@ HWTEST_F(SessionManagerLiteTest, ReregisterSessionListener, TestSize.Level1) */ HWTEST_F(SessionManagerLiteTest, OnRemoteDied1, TestSize.Level1) { - ASSERT_NE(nullptr, sml_); + int32_t userId = -1; + auto sml = SessionManagerLite::GetInstance(userId); + ASSERT_NE(nullptr, sml); FoundationDeathRecipient foundationDeathRecipient; wptr wptrDeath; foundationDeathRecipient.OnRemoteDied(wptrDeath); - ASSERT_EQ(false, sml_->isWMSConnected_); - ASSERT_EQ(false, sml_->isFoundationListenerRegistered_); - ASSERT_EQ(false, sml_->recoverListenerRegistered_); - ASSERT_EQ(nullptr, sml_->mockSessionManagerServiceProxy_); - ASSERT_EQ(nullptr, sml_->sessionManagerServiceProxy_); - ASSERT_EQ(nullptr, sml_->sceneSessionManagerLiteProxy_); + ASSERT_EQ(false, sml->isWMSConnected_); + ASSERT_EQ(false, sml->isFoundationListenerRegistered_); + ASSERT_EQ(false, sml->recoverListenerRegistered_); + ASSERT_EQ(nullptr, sml->mockSessionManagerServiceProxy_); + ASSERT_EQ(nullptr, sml->sessionManagerServiceProxy_); + ASSERT_EQ(nullptr, sml->sceneSessionManagerLiteProxy_); } /** @@ -116,11 +118,13 @@ HWTEST_F(SessionManagerLiteTest, OnRemoteDied1, TestSize.Level1) */ HWTEST_F(SessionManagerLiteTest, OnRemoteDied2, TestSize.Level1) { - ASSERT_NE(nullptr, sml_); + int32_t userId = -1; + auto sml = SessionManagerLite::GetInstance(userId); + ASSERT_NE(nullptr, sml); SSMDeathRecipient sSMDeathRecipient; wptr wptrDeath; sSMDeathRecipient.OnRemoteDied(wptrDeath); - ASSERT_EQ(nullptr, sml_->sessionManagerServiceProxy_); + ASSERT_EQ(nullptr, sml->sessionManagerServiceProxy_); } /** @@ -130,14 +134,16 @@ HWTEST_F(SessionManagerLiteTest, OnRemoteDied2, TestSize.Level1) */ HWTEST_F(SessionManagerLiteTest, OnFoundationDied, TestSize.Level1) { - ASSERT_NE(nullptr, sml_); - sml_->OnFoundationDied(); - ASSERT_EQ(false, sml_->isWMSConnected_); - ASSERT_EQ(false, sml_->isFoundationListenerRegistered_); - ASSERT_EQ(false, sml_->recoverListenerRegistered_); - ASSERT_EQ(nullptr, sml_->mockSessionManagerServiceProxy_); - ASSERT_EQ(nullptr, sml_->sessionManagerServiceProxy_); - ASSERT_EQ(nullptr, sml_->sceneSessionManagerLiteProxy_); + int32_t userId = -1; + auto sml = SessionManagerLite::GetInstance(userId); + ASSERT_NE(nullptr, sml); + sml->OnFoundationDied(); + ASSERT_EQ(false, sml->isWMSConnected_); + ASSERT_EQ(false, sml->isFoundationListenerRegistered_); + ASSERT_EQ(false, sml->recoverListenerRegistered_); + ASSERT_EQ(nullptr, sml->mockSessionManagerServiceProxy_); + ASSERT_EQ(nullptr, sml->sessionManagerServiceProxy_); + ASSERT_EQ(nullptr, sml->sceneSessionManagerLiteProxy_); } /** @@ -147,11 +153,13 @@ HWTEST_F(SessionManagerLiteTest, OnFoundationDied, TestSize.Level1) */ HWTEST_F(SessionManagerLiteTest, RegisterSMSRecoverListener1, TestSize.Level1) { - ASSERT_NE(nullptr, sml_); - sml_->recoverListenerRegistered_ = false; - sml_->mockSessionManagerServiceProxy_ = nullptr; - sml_->RegisterSMSRecoverListener(); - ASSERT_EQ(sml_->recoverListenerRegistered_, false); + int32_t userId = -1; + auto sml = SessionManagerLite::GetInstance(userId); + ASSERT_NE(nullptr, sml); + sml->recoverListenerRegistered_ = false; + sml->mockSessionManagerServiceProxy_ = nullptr; + sml->RegisterSMSRecoverListener(); + ASSERT_EQ(sml->recoverListenerRegistered_, false); } /** @@ -161,11 +169,13 @@ HWTEST_F(SessionManagerLiteTest, RegisterSMSRecoverListener1, TestSize.Level1) */ HWTEST_F(SessionManagerLiteTest, RegisterSMSRecoverListener2, TestSize.Level1) { - ASSERT_NE(nullptr, sml_); - sml_->recoverListenerRegistered_ = false; - sml_->InitMockSMSProxy(); - sml_->RegisterSMSRecoverListener(); - ASSERT_EQ(sml_->recoverListenerRegistered_, true); + int32_t userId = -1; + auto sml = SessionManagerLite::GetInstance(userId); + ASSERT_NE(nullptr, sml); + sml->recoverListenerRegistered_ = false; + sml->InitMockSMSProxy(); + sml->RegisterSMSRecoverListener(); + ASSERT_EQ(sml->recoverListenerRegistered_, true); } /** @@ -175,10 +185,119 @@ HWTEST_F(SessionManagerLiteTest, RegisterSMSRecoverListener2, TestSize.Level1) */ HWTEST_F(SessionManagerLiteTest, InitMockSMSProxy, TestSize.Level1) { - ASSERT_NE(nullptr, sml_); - sml_->InitMockSMSProxy(); - sml_->InitMockSMSProxy(); - ASSERT_NE(sml_->foundationDeath_, nullptr); + int32_t userId = -1; + auto sml = SessionManagerLite::GetInstance(userId); + ASSERT_NE(nullptr, sml); + sml->InitMockSMSProxy(); + sml->InitMockSMSProxy(); + ASSERT_NE(sml->foundationDeath_, nullptr); +} + +/** + * @tc.name: GetInstance + * @tc.desc: normal function + * @tc.type: FUNC + */ +HWTEST_F(SessionManagerLiteTest, GetInstance, TestSize.Level1) +{ + sptr instance = nullptr; + int32_t userId; + + userId = -1; + instance = SessionManagerLite::GetInstance(userId); + ASSERT_NE(nullptr, instance); + + userId = 101; + instance = SessionManagerLite::GetInstance(userId); + ASSERT_NE(nullptr, instance); + + // branch overried + instance = SessionManagerLite::GetInstance(userId); + ASSERT_NE(nullptr, instance); +} + +/** + * @tc.name: SessionManagerServiceLiteRecoverListener::OnSessionManagerServiceRecover + * @tc.desc: normal function + * @tc.type: FUNC + */ +HWTEST_F(SessionManagerLiteTest, SessionManagerServiceLiteRecoverListener1, TestSize.Level1) +{ + int32_t userId = 101; + sptr sml = SessionManagerLite::GetInstance(userId); + ASSERT_NE(nullptr, instance); + + sptr listener = new SessionManagerServiceLiteRecoverListener(sml); + sptr service = sptr::MakeSptr(); + listener->OnSessionManagerServiceRecover(service); +} + +/** + * @tc.name: SessionManagerServiceLiteRecoverListener::OnWMSConnectionChanged + * @tc.desc: normal function + * @tc.type: FUNC + */ +HWTEST_F(SessionManagerLiteTest, SessionManagerServiceLiteRecoverListener2, TestSize.Level1) +{ + int32_t userId = 100; + int32_t screenId = 101; + bool isConnected = false; + + sptr service = sptr::MakeSptr(); + sptr listener1 = new SessionManagerServiceLiteRecoverListener(nullptr); + ASSERT_NE(nullptr, listener1); + listener1->OnWMSConnectionChanged(userId, screenId, isConnected, service); + + sptr sml = SessionManagerLite::GetInstance(userId); + sptr listener2 = new SessionManagerServiceLiteRecoverListener(sml); + ASSERT_NE(nullptr, listener2); + listener2->OnWMSConnectionChanged(userId, screenId, isConnected, service); +} + +/** + * @tc.name: SessionManagerServiceLiteRecoverListener::OnRemoteRequest + * @tc.desc: normal function + * @tc.type: FUNC + */ +HWTEST_F(SessionManagerLiteTest, SessionManagerServiceLiteRecoverListener3, TestSize.Level1) +{ + OHOS::MessageParcel data; + OHOS::MessageParcel reply; + OHOS::MessageOption option; + IPCObjectStub iPCObjectStub; + uint32_t code; + int32_t userId = 101; + + sptr sml = SessionManagerLite::GetInstance(userId); + stpr listener = new SessionManagerServiceLiteRecoverListener(sml); + ASSERT_NE(nullptr, listener); + + code = static_cast(OHOS::Rosen::ISessionManagerServiceRecoverListener:: + SessionManagerServiceRecoverMessage::TRANS_ID_ON_SESSION_MANAGER_SERVICE_RECOVER); + auto ret = listener->OnRemoteRequest(code, data, reply, option); + ASSERT_NE(ret, 0); + + code = static_cast(OHOS::Rosen::ISessionManagerServiceRecoverListener:: + SessionManagerServiceRecoverMessage::TRANS_ID_ON_WMS_CONNECTION_CHANGED); + ret = listener->OnRemoteRequest(code, data, reply, option); + ASSERT_NE(ret, 0); + + code = 10; + ret = listener->OnRemoteRequest(code, data, reply, option); + ASSERT_NE(ret, 0); +} + +/** + * @tc.name: SSMDeathRecipientLite::OnRemoteDied + * @tc.desc: normal function + * @tc.type: FUNC + */ +HWTEST_F(SessionManagerLiteTest, OnRemoteDied3, TestSize.Level1) +{ + sptr recipient = new SSMDeathRecipientLite(); + ASSERT_NE(nullptr, recipient); + wptr wptrDeath; + recipient->OnRemoteDied(wptrDeath); } } // namespace } // namespace Rosen diff --git a/window_scene/test/unittest/session_manager_test.cpp b/window_scene/test/unittest/session_manager_test.cpp index 8bb188413817b1343b7b6b54e9596550b538797e..31718551de974482c83a203d499c643e1008471e 100644 --- a/window_scene/test/unittest/session_manager_test.cpp +++ b/window_scene/test/unittest/session_manager_test.cpp @@ -160,6 +160,41 @@ HWTEST_F(SessionManagerTest, RegisterWindowManagerRecoverCallbackFunc, TestSize. sm_->RegisterWindowManagerRecoverCallbackFunc(testFunc); ASSERT_NE(sm_->windowManagerRecoverFunc_, nullptr); } + +/** + * @tc.name: SessionManager::GetInstance() + * @tc.desc: normal function + * @tc.type: FUNC + */ +HWTEST_F(SessionManagerTest, GetInsance, TestSize.Level1) +{ + sptr instance = nullptr; + userId = -1; + instance = SessionManager::GetInstance(userId); + ASSERT_NE(nullptr, instance); + + userId = 101; + instance = SessionManager::GetInstance(userId); + ASSERT_NE(nullptr, instance); + + // branch overried + instance = SessionManager::GetInstance(userId); + ASSERT_NE(nullptr, instance); +} + +/** + * @tc.name: RemoveSSMDeathRecipient + * @tc.desc: normal function + * @tc.type: FUNC + */ +HWTEST_F(SessionManagerTest, RemoveSSMDeathRecipient, TestSize.Level1) +{ + ASSERT_NE(nullptr, sm_); + auto proxy = sm_->GetSceneSessionManagerProxy(); + ASSERT_NE(nullptr, proxy); + sm_->RemoveSSMDeathRecipient(); +} + } // namespace } // namespace Rosen } // namespace OHOS \ No newline at end of file diff --git a/wm/include/window_adapter.h b/wm/include/window_adapter.h index 4f511464dd06b7a721398996fa8f857913983e22..44e732c0bb908bcca02e9145f1338258c22187c6 100644 --- a/wm/include/window_adapter.h +++ b/wm/include/window_adapter.h @@ -18,25 +18,30 @@ #include #include - #include "common/include/window_session_property.h" -#include "window.h" -#include "zidl/window_interface.h" #include "singleton_delegator.h" +#include "window.h" #include "window_property.h" #include "wm_single_instance.h" +#include "zidl/window_interface.h" #include "zidl/window_manager_interface.h" namespace OHOS { namespace Rosen { class WMSDeathRecipient : public IRemoteObject::DeathRecipient { public: + WMSDeathRecipient(const int32_t userId = INVALID_USER_ID); virtual void OnRemoteDied(const wptr& wptrDeath) override; + +private: + int32_t userId_; }; -class WindowAdapter { -WM_DECLARE_SINGLE_INSTANCE(WindowAdapter); +class WindowAdapter : public RefBase { +WM_DECLARE_SINGLE_INSTANCE_BASE(WindowAdapter); public: + static sptr GetInstance(const int32_t userId); + using SessionRecoverCallbackFunc = std::function; using UIEffectRecoverCallbackFunc = std::function; using WMSConnectionChangedCallbackFunc = std::function; @@ -216,15 +221,21 @@ public: */ WMError GetPiPSettingSwitchStatus(bool& switchStatus); + ~WindowAdapter() override; + private: static inline SingletonDelegator delegator; bool InitWMSProxy(); bool InitSSMProxy(); /* - * Multi User + * Multi user and multi screen */ + explicit WindowAdapter(const int32_t userId = INVALID_USER_ID); void OnUserSwitch(); + int32_t userId_; + static std::unordered_map> windowAdapterMap_; + static std::mutex windowAdapterMapMutex_; /* * Window Recover diff --git a/wm/include/window_adapter_lite.h b/wm/include/window_adapter_lite.h index 1b7676388414cce61433c8e382c8b2221aadeadf..2c68b355354bab260695e45c815112c5c45ecd48 100644 --- a/wm/include/window_adapter_lite.h +++ b/wm/include/window_adapter_lite.h @@ -29,12 +29,17 @@ namespace OHOS { namespace Rosen { class WMSDeathRecipient : public IRemoteObject::DeathRecipient { public: + WMSDeathRecipient(const int32_t userId = INVALID_USER_ID); virtual void OnRemoteDied(const wptr& wptrDeath) override; +private: + int32_t userId_; }; -class WindowAdapterLite { -WM_DECLARE_SINGLE_INSTANCE(WindowAdapterLite); +class WindowAdapterLite : public RefBase{ +WM_DECLARE_SINGLE_INSTANCE_BASE(WindowAdapterLite); public: + static sptr GetInstance(const int32_t userId); + using WMSConnectionChangedCallbackFunc = std::function; virtual void GetFocusWindowInfo(FocusChangeInfo& focusInfo, DisplayId displayId = DEFAULT_DISPLAY_ID); virtual WMError RegisterWindowManagerAgent(WindowManagerAgentType type, @@ -62,13 +67,19 @@ public: virtual WMError ListWindowInfo(const WindowInfoOption& windowInfoOption, std::vector>& infos); virtual WMError SendPointerEventForHover(const std::shared_ptr& pointerEvent); + ~WindowAdapterLite() = default; + private: + WindowAdapterLite(const int32_t userId = INVALID_USER_ID); static inline SingletonDelegator delegator; bool InitSSMProxy(); /* - * Multi User + * Multi user and multi screen */ + int32_t userId_; + static std::unordered_map> windowAdapterLiteMap_; + static std::mutex windowAdapterLiteMapMutex_; void OnUserSwitch(); void ReregisterWindowManagerLiteAgent(); diff --git a/wm/src/window_adapter.cpp b/wm/src/window_adapter.cpp index 43ff070c62226117fd6fc4fb3136a6596ec99adf..9d40b52705f632e517a06972291761d35bb6011d 100644 --- a/wm/src/window_adapter.cpp +++ b/wm/src/window_adapter.cpp @@ -16,24 +16,25 @@ #include "window_adapter.h" #include #include -#include #include -#include "window_manager.h" -#include "window_manager_proxy.h" -#include "window_manager_hilog.h" -#include "wm_common.h" +#include +#include +#include "focus_change_info.h" #include "scene_board_judgement.h" #include "session_manager.h" -#include "focus_change_info.h" -#include +#include "window_manager.h" +#include "window_manager_hilog.h" +#include "window_manager_proxy.h" #include "window_session_impl.h" +#include "wm_common.h" namespace OHOS { namespace Rosen { namespace { constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowAdapter"}; } -WM_IMPLEMENT_SINGLE_INSTANCE(WindowAdapter) +std::unordered_map> WindowAdapter::windowAdapterMap_ = {}; +std::mutex WindowAdapter::windowAdapterMapMutex_; #define INIT_PROXY_CHECK_RETURN(ret) \ do { \ @@ -66,6 +67,66 @@ WM_IMPLEMENT_SINGLE_INSTANCE(WindowAdapter) } \ } while (false) +WMSDeathRecipient::WMSDeathRecipient(const int32_t userId) : userId_(userId) {} + +void WMSDeathRecipient::OnRemoteDied(const wptr& wptrDeath) +{ + TLOGI(WmsLogTag::WMS_RECOVER, "wms died"); + sptr object = wptrDeath.promote(); + if (object == nullptr) { + TLOGE(WmsLogTag::WMS_RECOVER, "invalid user: %d", userId_); + return; + } + WindowAdapter::GetInstance(userId_)->ClearWindowAdapter(); +} + +WindowAdapter::WindowAdapter(const int32_t userId) : userId_(userId) {} + +WindowAdapter& WindowAdapter::GetInstance() +{ + static sptr instance = nullptr; + static std::mutex mtx; + if (instance == nullptr) { + std::lock_guard lock(mtx); + if (instance == nullptr) { + instance = new WindowAdapter(); + } + } + return *instance; +} + +sptr WindowAdapter::GetInstance(const int32_t userId) +{ + sptr instance = nullptr; + + if (userId <= INVALID_USER_ID) { + instance = &WindowAdapter::GetInstance(); + return instance; + } + // multi-instance mode + std::lock_guard lock(windowAdapterMapMutex_); + auto iter = windowAdapterMap_.find(userId); + if (iter != windowAdapterMap_.end()) { + return iter->second; + } + TLOGI(WmsLogTag::WMS_MULTI_USER, "create new instance userId: %{public}d", userId); + instance = new WindowAdapter(userId); + windowAdapterMap_.insert({userId, instance}); + return instance; +} + +WindowAdapter::~WindowAdapter() +{ + TLOGI(WmsLogTag::WMS_SCB, "destroyed, userId: %{public}d", userId_); + sptr remoteObject = nullptr; + if (windowManagerServiceProxy_) { + remoteObject = windowManagerServiceProxy_->AsObject(); + } + if (remoteObject) { + remoteObject->RemoveDeathRecipient(wmsDeath_); + } +} + WMError WindowAdapter::CreateWindow(sptr& window, sptr& windowProperty, std::shared_ptr surfaceNode, uint32_t& windowId, const sptr& token) { @@ -450,13 +511,13 @@ void WindowAdapter::UnregisterSessionRecoverCallbackFunc(int32_t persistentId) WMError WindowAdapter::RegisterWMSConnectionChangedListener(const WMSConnectionChangedCallbackFunc& callbackFunc) { TLOGI(WmsLogTag::WMS_MULTI_USER, "RegisterWMSConnectionChangedListener in"); - return SessionManager::GetInstance().RegisterWMSConnectionChangedListener(callbackFunc); + return SessionManager::GetInstance(userId_)->RegisterWMSConnectionChangedListener(callbackFunc); } WMError WindowAdapter::UnregisterWMSConnectionChangedListener() { TLOGI(WmsLogTag::WMS_MULTI_USER, "unregister wms connection changed listener"); - return SessionManager::GetInstance().UnregisterWMSConnectionChangedListener(); + return SessionManager::GetInstance(userId_)->UnregisterWMSConnectionChangedListener(); } void WindowAdapter::WindowManagerAndSessionRecover() @@ -541,39 +602,42 @@ bool WindowAdapter::InitSSMProxy() { std::lock_guard lock(mutex_); if (!isProxyValid_) { - windowManagerServiceProxy_ = SessionManager::GetInstance().GetSceneSessionManagerProxy(); + windowManagerServiceProxy_ = SessionManager::GetInstance(userId_)->GetSceneSessionManagerProxy(); if (!windowManagerServiceProxy_ || !windowManagerServiceProxy_->AsObject()) { WLOGFE("Failed to get system scene session manager services"); return false; } - wmsDeath_ = new (std::nothrow) WMSDeathRecipient(); - if (!wmsDeath_) { - WLOGFE("Failed to create death Recipient ptr WMSDeathRecipient"); - return false; - } + wmsDeath_ = sptr::MakeSptr(userId_); sptr remoteObject = windowManagerServiceProxy_->AsObject(); if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(wmsDeath_)) { WLOGFE("Failed to add death recipient"); return false; } if (!recoverInitialized_) { - SessionManager::GetInstance().RegisterWindowManagerRecoverCallbackFunc( - [this] { this->WindowManagerAndSessionRecover(); }); + SessionManager::GetInstance(userId_)->RegisterWindowManagerRecoverCallbackFunc([weakThis = wptr(this)] { + auto windowAdapter = weakThis.promote(); + if (!windowAdapter) { + TLOGE(WmsLogTag::WMS_SCB, "window adapter is null"); + return; + } + windowAdapter->WindowManagerAndSessionRecover(); + }); recoverInitialized_ = true; } // U0 system user needs to subscribe OnUserSwitch event int32_t clientUserId = GetUserIdByUid(getuid()); if (clientUserId == SYSTEM_USERID && !isRegisteredUserSwitchListener_) { - SessionManager::GetInstance().RegisterUserSwitchListener([this]() { this->OnUserSwitch(); }); + SessionManager::GetInstance(userId_)->RegisterUserSwitchListener([this]() { this->OnUserSwitch(); }); isRegisteredUserSwitchListener_ = true; } isProxyValid_ = true; - } + } return true; } void WindowAdapter::ClearWindowAdapter() { + TLOGI(WmsLogTag::WMS_RECOVER, "begin clear"); std::lock_guard lock(mutex_); if ((windowManagerServiceProxy_ != nullptr) && (windowManagerServiceProxy_->AsObject() != nullptr)) { windowManagerServiceProxy_->AsObject()->RemoveDeathRecipient(wmsDeath_); @@ -582,22 +646,6 @@ void WindowAdapter::ClearWindowAdapter() windowManagerServiceProxy_ = nullptr; } -void WMSDeathRecipient::OnRemoteDied(const wptr& wptrDeath) -{ - if (wptrDeath == nullptr) { - WLOGFE("wptrDeath is null"); - return; - } - - sptr object = wptrDeath.promote(); - if (!object) { - WLOGFE("object is null"); - return; - } - WLOGI("wms OnRemoteDied"); - SingletonContainer::Get().ClearWindowAdapter(); -} - WMError WindowAdapter::GetTopWindowId(uint32_t mainWinId, uint32_t& topWinId) { INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR); diff --git a/wm/src/window_adapter_lite.cpp b/wm/src/window_adapter_lite.cpp index 42795a5fb44acfd0edfc019a6a67b439f1ea477b..91a7749defd58e9e4684ae5f040ac4470bc41634 100644 --- a/wm/src/window_adapter_lite.cpp +++ b/wm/src/window_adapter_lite.cpp @@ -25,7 +25,8 @@ namespace Rosen { namespace { constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowAdapterLite"}; } -WM_IMPLEMENT_SINGLE_INSTANCE(WindowAdapterLite) +std::unordered_map> WindowAdapterLite::windowAdapterLiteMap_ = {}; +std::mutex WindowAdapterLite::windowAdapterLiteMapMutex_; #define INIT_PROXY_CHECK_RETURN(ret) \ do { \ @@ -51,6 +52,41 @@ WM_IMPLEMENT_SINGLE_INSTANCE(WindowAdapterLite) } \ } while(false) +WindowAdapterLite::WindowAdapterLite(const int32_t userId) : userId_(userId) {} + +WindowAdapterLite& WindowAdapterLite::GetInstance() +{ + static sptr instance = nullptr; + static std::mutex mtx; + if (instance == nullptr) { + std::lock_guard lock(mtx); + if (instance == nullptr) { + instance = new WindowAdapterLite(); + } + } + return *instance; +} + +sptr WindowAdapterLite::GetInstance(const int32_t userId) +{ + sptr instance = nullptr; + + if (userId <= INVALID_USER_ID) { + instance = &WindowAdapterLite::GetInstance(); + return instance; + } + // multi-instance mode + std::lock_guard lock(windowAdapterLiteMapMutex_); + auto iter = windowAdapterLiteMap_.find(userId); + if (iter != windowAdapterLiteMap_.end()) { + return iter->second; + } + TLOGI(WmsLogTag::WMS_MULTI_USER, "create new instance userId: %{public}d", userId); + instance = new WindowAdapterLite(userId); + windowAdapterLiteMap_.insert({userId, instance}); + return instance; +} + WMError WindowAdapterLite::RegisterWindowManagerAgent(WindowManagerAgentType type, const sptr& windowManagerAgent) { @@ -134,12 +170,12 @@ bool WindowAdapterLite::InitSSMProxy() { std::lock_guard lock(mutex_); if (!isProxyValid_) { - windowManagerServiceProxy_ = SessionManagerLite::GetInstance().GetSceneSessionManagerLiteProxy(); + windowManagerServiceProxy_ = SessionManagerLite::GetInstance(userId_)->GetSceneSessionManagerLiteProxy(); if (!windowManagerServiceProxy_ || !windowManagerServiceProxy_->AsObject()) { WLOGFE("Failed to get scene session manager lite proxy"); return false; } - wmsDeath_ = new (std::nothrow) WMSDeathRecipient(); + wmsDeath_ = new (std::nothrow) WMSDeathRecipient(userId_); if (!wmsDeath_) { WLOGFE("Failed to create death recipient WMSDeathRecipient"); return false; @@ -152,7 +188,14 @@ bool WindowAdapterLite::InitSSMProxy() // U0 system user needs to subscribe OnUserSwitch event int32_t clientUserId = GetUserIdByUid(getuid()); if (clientUserId == SYSTEM_USERID && !isRegisteredUserSwitchListener_) { - SessionManagerLite::GetInstance().RegisterUserSwitchListener([this] { this->OnUserSwitch(); }); + SessionManagerLite::GetInstance(userId_)->RegisterUserSwitchListener([weakThis = wptr(this)] { + auto windowAdapter = weakThis.promote(); + if (!windowAdapter) { + TLOGE(WmsLogTag::WMS_SCB, "window adapter is null"); + return; + } + windowAdapter->OnUserSwitch(); + }); isRegisteredUserSwitchListener_ = true; } isProxyValid_ = true; @@ -179,6 +222,8 @@ void WindowAdapterLite::ClearWindowAdapter() windowManagerServiceProxy_ = nullptr; } +WMSDeathRecipient::WMSDeathRecipient(int32_t userId) : userId_(userId) {} + void WMSDeathRecipient::OnRemoteDied(const wptr& wptrDeath) { if (wptrDeath == nullptr) { @@ -190,9 +235,9 @@ void WMSDeathRecipient::OnRemoteDied(const wptr& wptrDeath) WLOGFE("object is null"); return; } - WLOGI("wms OnRemoteDied"); - SingletonContainer::Get().ClearWindowAdapter(); - SingletonContainer::Get().ClearSessionManagerProxy(); + TLOGI(WmsLogTag::WMS_SCB, "wms lite OnRemoteDied"); + WindowAdapterLite::GetInstance(userId_)->ClearWindowAdapter(); + SessionManagerLite::GetInstance(userId_)->ClearSessionManagerProxy(); } void WindowAdapterLite::GetFocusWindowInfo(FocusChangeInfo& focusInfo, DisplayId displayId) @@ -283,13 +328,13 @@ WMError WindowAdapterLite::RaiseWindowToTop(int32_t persistentId) WMError WindowAdapterLite::RegisterWMSConnectionChangedListener(const WMSConnectionChangedCallbackFunc& callbackFunc) { TLOGD(WmsLogTag::WMS_MULTI_USER, "register listener"); - return SessionManagerLite::GetInstance().RegisterWMSConnectionChangedListener(callbackFunc); + return SessionManagerLite::GetInstance(userId_)->RegisterWMSConnectionChangedListener(callbackFunc); } WMError WindowAdapterLite::UnregisterWMSConnectionChangedListener() { TLOGD(WmsLogTag::WMS_MULTI_USER, "unregister wms connection changed listener"); - return SessionManagerLite::GetInstance().UnregisterWMSConnectionChangedListener(); + return SessionManagerLite::GetInstance(userId_)->UnregisterWMSConnectionChangedListener(); } WMError WindowAdapterLite::GetWindowStyleType(WindowStyleType& windowStyleType) diff --git a/wm/src/window_manager.cpp b/wm/src/window_manager.cpp index e37e59d04f758c913f1abc68069921f2840814f7..a21533537c86b7e6dd31f289863edee820c61f5b 100644 --- a/wm/src/window_manager.cpp +++ b/wm/src/window_manager.cpp @@ -40,8 +40,8 @@ public: int32_t CheckWindowId(int32_t windowId) const override; }; } - -WM_IMPLEMENT_SINGLE_INSTANCE(WindowManager) +std::unordered_map> WindowManager::windowManagerMap_ = {}; +std::mutex WindowManager::windowManagerMapMutex_; class WindowManager::Impl { public: @@ -531,14 +531,15 @@ void WindowManager::Impl::NotifyWMSWindowDestroyed(const WindowLifeCycleInfo& li wmsWindowDestroyedListener->OnWindowDestroyed(lifeCycleInfo, jsWindowNapiValue); } -WindowManager::WindowManager() : pImpl_(std::make_unique()) +WindowManager::WindowManager(const int32_t userId) : userId_(userId), + pImpl_(std::make_unique(mutex_)) { } int32_t WindowChecker::CheckWindowId(int32_t windowId) const { int32_t pid = INVALID_PID; - WMError ret = SingletonContainer::Get().CheckWindowId(windowId, pid); + WMError ret = WindowAdapter::GetInstance(userId_)->CheckWindowId(windowId, pid); if (ret != WMError::WM_OK) { WLOGFE("Window(%{public}d) do not allow styles to be set", windowId); } @@ -547,10 +548,57 @@ int32_t WindowChecker::CheckWindowId(int32_t windowId) const WindowManager::~WindowManager() { + TLOGI(WmsLogTag::WMS_SCB, "destroyed"); std::lock_guard lock(mutex_); destroyed_ = true; } +WindowManager& WindowManager::GetInstance() +{ + static sptr instance = nullptr; + static std::mutex mtx; + if (instance == nullptr) { + std::lock_guard lock(mtx); + if(instance == nullptr){ + instance = new WindowManager(); + } + } + return *instance; +} + +sptr WindowManager::GetInstance(const int32_t userId) +{ + /** + * Only system applications or services with a userId of 0 are allowed to communicate + * with multiple WMS-Servers and are permitted to listen for WMS connection status. + */ + sptr instance = nullptr; + int32_t clientUserId = GetUserIdByUid(getuid()); + if (clientUserId != SYSTEM_USERID || userId <= INVALID_USER_ID) { + instance = &WindowManager::GetInstance(); + return instance; + } + + //multi-instance mode + std::lock_guard lock(windowManagerMapMutex_); + auto iter = windowManagerMap_.find(userId); + if (iter != windowManagerMap_.end()) { + return iter->second; + } + TLOGI(WmsLogTag::WMS_MULTI_USER, "create new instance userId: %{public}d", userId); + instance = new WindowManager(userId); + windowManagerMap_.insert({ userId, instance }); + return instance; +} + +WMError WindowManager::RemoveInstanceByUserId(const int32_t userId) +{ + TLOGI(WmsLogTag::WMS_MULTI_USER, "remove instance userId: %{public}d", userId); + std:lock_guard lock(windowManagerMapMutex_); + windowManagerMap_.erase(userId); + return WMError::WM_OK; +} + WMError WindowManager::RegisterWMSConnectionChangedListener(const sptr& listener) { int32_t clientUserId = GetUserIdByUid(getuid()); @@ -573,9 +621,14 @@ WMError WindowManager::RegisterWMSConnectionChangedListener(const sptrwmsConnectionChangedListener_ = listener; } - auto ret = WindowAdapter::GetInstance().RegisterWMSConnectionChangedListener( - [this](int32_t userId, int32_t screenId, bool isConnected) { - this->OnWMSConnectionChanged(userId, screenId, isConnected); + auto ret = WindowAdapter::GetInstance(userId_)->RegisterWMSConnectionChangedListener( + [weakThis = wptr(this)](int32_t userId, int32_t screenId, bool isConnected) { + auto windowManager = weakThis.promote(); + if (!windowManager) { + TLOGE(WmsLogTag::WMS_SCB, "window adapter is null"); + return; + } + windowManager->OnWMSConnectionChanged(userId, screenId, isConnected); }); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_MULTI_USER, "Register failed: error = %{public}d", static_cast(ret)); @@ -590,7 +643,7 @@ WMError WindowManager::UnregisterWMSConnectionChangedListener() TLOGI(WmsLogTag::WMS_MULTI_USER, "Unregister enter"); std::unique_lock lock(pImpl_->listenerMutex_); pImpl_->wmsConnectionChangedListener_ = nullptr; - WindowAdapter::GetInstance().UnregisterWMSConnectionChangedListener(); + WindowAdapter::GetInstance(userId_)->UnregisterWMSConnectionChangedListener(); return WMError::WM_OK; } @@ -610,7 +663,7 @@ WMError WindowManager::RegisterFocusChangedListener(const sptrfocusChangedListenerAgent_; } - ret = WindowAdapter::GetInstance().RegisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_FOCUS, focusChangedListenerAgentBack); { std::unique_lock lock(pImpl_->listenerMutex_); @@ -646,7 +699,7 @@ WMError WindowManager::UnregisterFocusChangedListener(const sptrfocusChangedListeners_.erase(iter); WMError ret = WMError::WM_OK; if (pImpl_->focusChangedListeners_.empty() && pImpl_->focusChangedListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_FOCUS, pImpl_->focusChangedListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->focusChangedListenerAgent_ = nullptr; @@ -667,7 +720,7 @@ WMError WindowManager::RegisterWindowModeChangedListener(const sptrwindowModeListenerAgent_ == nullptr) { pImpl_->windowModeListenerAgent_ = new WindowManagerAgent(); } - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_MODE, pImpl_->windowModeListenerAgent_); if (ret != WMError::WM_OK) { TLOGW(WmsLogTag::WMS_MAIN, "RegisterWindowManagerAgent failed!"); @@ -699,7 +752,7 @@ WMError WindowManager::UnregisterWindowModeChangedListener(const sptrwindowModeListeners_.erase(iter); WMError ret = WMError::WM_OK; if (pImpl_->windowModeListeners_.empty() && pImpl_->windowModeListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_MODE, pImpl_->windowModeListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->windowModeListenerAgent_ = nullptr; @@ -720,7 +773,7 @@ WMError WindowManager::RegisterSystemBarChangedListener(const sptrsystemBarChangedListenerAgent_ == nullptr) { pImpl_->systemBarChangedListenerAgent_ = new WindowManagerAgent(); } - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_SYSTEM_BAR, pImpl_->systemBarChangedListenerAgent_); if (ret != WMError::WM_OK) { WLOGFW("RegisterWindowManagerAgent failed!"); @@ -754,7 +807,7 @@ WMError WindowManager::UnregisterSystemBarChangedListener(const sptrsystemBarChangedListeners_.erase(iter); WMError ret = WMError::WM_OK; if (pImpl_->systemBarChangedListeners_.empty() && pImpl_->systemBarChangedListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_SYSTEM_BAR, pImpl_->systemBarChangedListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->systemBarChangedListenerAgent_ = nullptr; @@ -766,19 +819,19 @@ WMError WindowManager::UnregisterSystemBarChangedListener(const sptr().MinimizeAllAppWindows(displayId); + return WindowAdapter::GetInstance(userId_)->MinimizeAllAppWindows(displayId); } WMError WindowManager::ToggleShownStateForAllAppWindows() { WLOGFD("ToggleShownStateForAllAppWindows"); - return SingletonContainer::Get().ToggleShownStateForAllAppWindows(); + return WindowAdapter::GetInstance(userId_)->ToggleShownStateForAllAppWindows(); } WMError WindowManager::SetWindowLayoutMode(WindowLayoutMode mode) { WLOGFD("set window layout mode: %{public}u", mode); - WMError ret = SingletonContainer::Get().SetWindowLayoutMode(mode); + WMError ret = WindowAdapter::GetInstance(userId_)->SetWindowLayoutMode(mode); if (ret != WMError::WM_OK) { WLOGFE("set layout mode failed"); } @@ -796,7 +849,7 @@ WMError WindowManager::RegisterWindowUpdateListener(const sptrwindowUpdateListenerAgent_ == nullptr) { pImpl_->windowUpdateListenerAgent_ = new WindowManagerAgent(); } - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_UPDATE, pImpl_->windowUpdateListenerAgent_); if (ret != WMError::WM_OK) { WLOGFW("RegisterWindowManagerAgent failed!"); @@ -827,7 +880,7 @@ WMError WindowManager::UnregisterWindowUpdateListener(const sptrwindowUpdateListeners_.erase(iter); WMError ret = WMError::WM_OK; if (pImpl_->windowUpdateListeners_.empty() && pImpl_->windowUpdateListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_UPDATE, pImpl_->windowUpdateListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->windowUpdateListenerAgent_ = nullptr; @@ -847,7 +900,7 @@ WMError WindowManager::RegisterVisibilityChangedListener(const sptrwindowVisibilityListenerAgent_ == nullptr) { pImpl_->windowVisibilityListenerAgent_ = new WindowManagerAgent(); } - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY, pImpl_->windowVisibilityListenerAgent_); if (ret != WMError::WM_OK) { @@ -879,7 +932,7 @@ WMError WindowManager::UnregisterVisibilityChangedListener(const sptrwindowVisibilityListeners_.empty() && pImpl_->windowVisibilityListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY, pImpl_->windowVisibilityListenerAgent_); if (ret == WMError::WM_OK) { @@ -909,7 +962,7 @@ WMError WindowManager::RegisterDisplayIdChangedListener(const sptr(windowInfoKey); } - ret = SingletonContainer::Get().RegisterWindowPropertyChangeAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowPropertyChangeAgent( WindowInfoKey::DISPLAY_ID, interestInfo, pImpl_->windowPropertyChangeAgent_); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "RegisterWindowPropertyChangeAgent failed!"); @@ -950,7 +1003,7 @@ WMError WindowManager::UnregisterDisplayIdChangedListener(const sptrwindowDisplayIdChangeListeners_.empty() && pImpl_->windowPropertyChangeAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowPropertyChangeAgent(WindowInfoKey::DISPLAY_ID, + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowPropertyChangeAgent(WindowInfoKey::DISPLAY_ID, interestInfo, pImpl_->windowPropertyChangeAgent_); if (ret == WMError::WM_OK) { pImpl_->windowPropertyChangeAgent_ = nullptr; @@ -979,7 +1032,7 @@ WMError WindowManager::RegisterRectChangedListener(const sptr(windowInfoKey); } - ret = SingletonContainer::Get().RegisterWindowPropertyChangeAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowPropertyChangeAgent( WindowInfoKey::WINDOW_RECT, interestInfo, pImpl_->windowPropertyChangeAgent_); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "RegisterWindowPropertyChangeAgent failed!"); @@ -1020,7 +1073,7 @@ WMError WindowManager::UnregisterRectChangedListener(const sptrwindowRectChangeListeners_.empty() && pImpl_->windowPropertyChangeAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowPropertyChangeAgent(WindowInfoKey::WINDOW_RECT, + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowPropertyChangeAgent(WindowInfoKey::WINDOW_RECT, interestInfo, pImpl_->windowPropertyChangeAgent_); if (ret == WMError::WM_OK) { pImpl_->windowPropertyChangeAgent_ = nullptr; @@ -1050,7 +1103,7 @@ WMError WindowManager::RegisterWindowModeChangedListenerForPropertyChange( } interestInfo |= static_cast(windowInfoKey); } - ret = SingletonContainer::Get().RegisterWindowPropertyChangeAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowPropertyChangeAgent( WindowInfoKey::WINDOW_MODE, interestInfo, pImpl_->windowPropertyChangeAgent_); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "RegisterWindowPropertyChangeAgent failed!"); @@ -1092,7 +1145,7 @@ WMError WindowManager::UnregisterWindowModeChangedListenerForPropertyChange( } WMError ret = WMError::WM_OK; if (pImpl_->windowModeChangeListeners_.empty() && pImpl_->windowPropertyChangeAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowPropertyChangeAgent(WindowInfoKey::WINDOW_MODE, + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowPropertyChangeAgent(WindowInfoKey::WINDOW_MODE, interestInfo, pImpl_->windowPropertyChangeAgent_); if (ret == WMError::WM_OK) { pImpl_->windowPropertyChangeAgent_ = nullptr; @@ -1121,7 +1174,7 @@ WMError WindowManager::RegisterFloatingScaleChangedListener(const sptr(windowInfoKey); } - ret = SingletonContainer::Get().RegisterWindowPropertyChangeAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowPropertyChangeAgent( WindowInfoKey::FLOATING_SCALE, interestInfo, pImpl_->windowPropertyChangeAgent_); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "RegisterWindowPropertyChangeAgent failed!"); @@ -1162,7 +1215,7 @@ WMError WindowManager::UnregisterFloatingScaleChangedListener(const sptrfloatingScaleChangeListeners_.empty() && pImpl_->windowPropertyChangeAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowPropertyChangeAgent( + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowPropertyChangeAgent( WindowInfoKey::FLOATING_SCALE, interestInfo, pImpl_->windowPropertyChangeAgent_); if (ret == WMError::WM_OK) { pImpl_->windowPropertyChangeAgent_ = nullptr; @@ -1182,7 +1235,7 @@ WMError WindowManager::RegisterVisibilityStateChangedListener(const sptrwindowVisibilityStateListenerAgent_ == nullptr) { pImpl_->windowVisibilityStateListenerAgent_ = new WindowManagerAgent(); } - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY, pImpl_->windowVisibilityStateListenerAgent_); if (ret != WMError::WM_OK) { @@ -1214,7 +1267,7 @@ WMError WindowManager::UnregisterVisibilityStateChangedListener(const sptrwindowVisibilityStateListeners_.empty() && pImpl_->windowVisibilityStateListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY, pImpl_->windowVisibilityStateListenerAgent_); if (ret == WMError::WM_OK) { @@ -1236,7 +1289,7 @@ WMError WindowManager::RegisterCameraFloatWindowChangedListener(const sptrcameraFloatWindowChangedListenerAgent_ == nullptr) { pImpl_->cameraFloatWindowChangedListenerAgent_ = new WindowManagerAgent(); } - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_FLOAT, pImpl_->cameraFloatWindowChangedListenerAgent_); if (ret != WMError::WM_OK) { @@ -1273,7 +1326,7 @@ WMError WindowManager::UnregisterCameraFloatWindowChangedListener( WMError ret = WMError::WM_OK; if (pImpl_->cameraFloatWindowChangedListeners_.empty() && pImpl_->cameraFloatWindowChangedListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_FLOAT, pImpl_->cameraFloatWindowChangedListenerAgent_); if (ret == WMError::WM_OK) { @@ -1295,7 +1348,7 @@ WMError WindowManager::RegisterWaterMarkFlagChangedListener(const sptrwaterMarkFlagChangeAgent_ == nullptr) { pImpl_->waterMarkFlagChangeAgent_ = new WindowManagerAgent(); } - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WATER_MARK_FLAG, pImpl_->waterMarkFlagChangeAgent_); if (ret != WMError::WM_OK) { @@ -1332,7 +1385,7 @@ WMError WindowManager::UnregisterWaterMarkFlagChangedListener(const sptrwaterMarkFlagChangeListeners_.empty() && pImpl_->waterMarkFlagChangeAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WATER_MARK_FLAG, pImpl_->waterMarkFlagChangeAgent_); if (ret == WMError::WM_OK) { @@ -1357,7 +1410,7 @@ WMError WindowManager::RegisterGestureNavigationEnabledChangedListener( pImpl_->gestureNavigationEnabledAgent_ = new (std::nothrow)WindowManagerAgent(); } if (pImpl_->gestureNavigationEnabledAgent_ != nullptr) { - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_GESTURE_NAVIGATION_ENABLED, pImpl_->gestureNavigationEnabledAgent_); } else { @@ -1400,7 +1453,7 @@ WMError WindowManager::UnregisterGestureNavigationEnabledChangedListener( WMError ret = WMError::WM_OK; if (pImpl_->gestureNavigationEnabledListeners_.empty() && pImpl_->gestureNavigationEnabledAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_GESTURE_NAVIGATION_ENABLED, pImpl_->gestureNavigationEnabledAgent_); if (ret == WMError::WM_OK) { @@ -1498,7 +1551,7 @@ WMError WindowManager::RegisterWindowPidVisibilityChangedListener( pImpl_->windowPidVisibilityListenerAgent_ = sptr::MakeSptr(); } - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_PID_VISIBILITY, pImpl_->windowPidVisibilityListenerAgent_); if (ret != WMError::WM_OK) { @@ -1545,7 +1598,7 @@ WMError WindowManager::UnregisterWindowPidVisibilityChangedListener( } std::unique_lock lock(pImpl_->visibilityListenerAgentListenerMutex_); if (isListenEmpty && pImpl_->windowPidVisibilityListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_PID_VISIBILITY, pImpl_->windowPidVisibilityListenerAgent_); if (ret == WMError::WM_OK) { @@ -1570,7 +1623,7 @@ WMError WindowManager::NotifyDisplayInfoChange(const sptr& token, void WindowManager::GetFocusWindowInfo(FocusChangeInfo& focusInfo, DisplayId displayId) { - SingletonContainer::Get().GetFocusWindowInfo(focusInfo, displayId); + WindowAdapter::GetInstance(userId_)->GetFocusWindowInfo(focusInfo, displayId); } void WindowManager::OnWMSConnectionChanged(int32_t userId, int32_t screenId, bool isConnected) const @@ -1604,7 +1657,7 @@ void WindowManager::UpdateWindowModeTypeInfo(WindowModeType type) const WMError WindowManager::GetWindowModeType(WindowModeType& windowModeType) const { - WMError ret = SingletonContainer::Get().GetWindowModeType(windowModeType); + WMError ret = WindowAdapter::GetInstance(userId_)->GetWindowModeType(windowModeType); if (ret != WMError::WM_OK) { WLOGFE("get window mode type failed"); } @@ -1638,7 +1691,7 @@ void WindowManager::UpdateWindowDrawingContentInfo( WMError WindowManager::GetAccessibilityWindowInfo(std::vector>& infos) const { - WMError ret = SingletonContainer::Get().GetAccessibilityWindowInfo(infos); + WMError ret = WindowAdapter::GetInstance(userId_)->GetAccessibilityWindowInfo(infos); if (ret != WMError::WM_OK) { WLOGFE("get window info failed"); } @@ -1648,7 +1701,7 @@ WMError WindowManager::GetAccessibilityWindowInfo(std::vector>& infos) const { - WMError ret = SingletonContainer::Get().GetUnreliableWindowInfo(windowId, infos); + WMError ret = WindowAdapter::GetInstance(userId_)->GetUnreliableWindowInfo(windowId, infos); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::DEFAULT, "get unreliable window info failed"); } @@ -1662,7 +1715,7 @@ WMError WindowManager::ListWindowInfo(const WindowInfoOption& windowInfoOption, static_cast(windowInfoOption.windowInfoFilterOption), static_cast(windowInfoOption.windowInfoTypeOption), windowInfoOption.displayId, windowInfoOption.windowId); - WMError ret = SingletonContainer::Get().ListWindowInfo(windowInfoOption, infos); + WMError ret = WindowAdapter::GetInstance(userId_)->ListWindowInfo(windowInfoOption, infos); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "failed"); } @@ -1671,7 +1724,7 @@ WMError WindowManager::ListWindowInfo(const WindowInfoOption& windowInfoOption, WMError WindowManager::GetAllWindowLayoutInfo(DisplayId displayId, std::vector>& infos) const { - WMError ret = SingletonContainer::Get().GetAllWindowLayoutInfo(displayId, infos); + WMError ret = WindowAdapter::GetInstance(userId_)->GetAllWindowLayoutInfo(displayId, infos); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "failed"); } @@ -1680,17 +1733,17 @@ WMError WindowManager::GetAllWindowLayoutInfo(DisplayId displayId, std::vector().GetGlobalWindowMode(displayId, globalWinMode); + return WindowAdapter::GetInstance(userId_)->GetGlobalWindowMode(displayId, globalWinMode); } WMError WindowManager::GetTopNavDestinationName(int32_t windowId, std::string& topNavDestName) const { - return SingletonContainer::Get().GetTopNavDestinationName(windowId, topNavDestName); + return WindowAdapter::GetInstance(userId_)->GetTopNavDestinationName(windowId, topNavDestName); } WMError WindowManager::GetVisibilityWindowInfo(std::vector>& infos) const { - WMError ret = SingletonContainer::Get().GetVisibilityWindowInfo(infos); + WMError ret = WindowAdapter::GetInstance(userId_)->GetVisibilityWindowInfo(infos); if (ret != WMError::WM_OK) { WLOGFE("get window visibility info failed"); } @@ -1699,7 +1752,7 @@ WMError WindowManager::GetVisibilityWindowInfo(std::vector& infos) { - WMError ret = SingletonContainer::Get().DumpSessionAll(infos); + WMError ret = WindowAdapter::GetInstance(userId_)->DumpSessionAll(infos); if (ret != WMError::WM_OK) { WLOGFE("dump session all failed"); } @@ -1708,7 +1761,7 @@ WMError WindowManager::DumpSessionAll(std::vector& infos) WMError WindowManager::DumpSessionWithId(int32_t persistentId, std::vector& infos) { - WMError ret = SingletonContainer::Get().DumpSessionWithId(persistentId, infos); + WMError ret = WindowAdapter::GetInstance(userId_)->DumpSessionWithId(persistentId, infos); if (ret != WMError::WM_OK) { WLOGFE("dump session with id failed"); } @@ -1717,7 +1770,7 @@ WMError WindowManager::DumpSessionWithId(int32_t persistentId, std::vector& uiContentRemoteObj) { - WMError ret = SingletonContainer::Get().GetUIContentRemoteObj(windowId, uiContentRemoteObj); + WMError ret = WindowAdapter::GetInstance(userId_)->GetUIContentRemoteObj(windowId, uiContentRemoteObj); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "Failed to get UIContentRemoteObj. PersistentId=%{public}d; ret=%{public}u", windowId, static_cast(ret)); @@ -1727,7 +1780,7 @@ WMError WindowManager::GetUIContentRemoteObj(int32_t windowId, sptr().SetGestureNavigationEnabled(enable); + WMError ret = WindowAdapter::GetInstance(userId_)->SetGestureNavigationEnabled(enable); if (ret != WMError::WM_OK) { WLOGFE("set gesture navigation enabled failed"); } @@ -1736,7 +1789,7 @@ WMError WindowManager::SetGestureNavigationEnabled(bool enable) const WMError WindowManager::NotifyWindowExtensionVisibilityChange(int32_t pid, int32_t uid, bool visible) { - WMError ret = SingletonContainer::Get().NotifyWindowExtensionVisibilityChange(pid, uid, visible); + WMError ret = WindowAdapter::GetInstance(userId_)->NotifyWindowExtensionVisibilityChange(pid, uid, visible); if (ret != WMError::WM_OK) { WLOGFE("notify WindowExtension visibility change failed"); } @@ -1765,7 +1818,7 @@ void WindowManager::NotifyWindowPidVisibilityChanged(const sptr().RaiseWindowToTop(persistentId); + WMError ret = WindowAdapter::GetInstance(userId_)->RaiseWindowToTop(persistentId); if (ret != WMError::WM_OK) { WLOGFE("raise window to top failed"); } @@ -1789,7 +1842,7 @@ WMError WindowManager::RegisterDrawingContentChangedListener(const sptrwindowDrawingContentListenerAgent_ == nullptr) { pImpl_->windowDrawingContentListenerAgent_ = new WindowManagerAgent(); } - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_DRAWING_STATE, pImpl_->windowDrawingContentListenerAgent_); if (ret != WMError::WM_OK) { @@ -1821,7 +1874,7 @@ WMError WindowManager::UnregisterDrawingContentChangedListener(const sptrwindowDrawingContentListeners_.empty() && pImpl_->windowDrawingContentListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_DRAWING_STATE, pImpl_->windowDrawingContentListenerAgent_); if (ret == WMError::WM_OK) { @@ -1843,7 +1896,7 @@ WMError WindowManager::RegisterWindowSystemBarPropertyChangedListener( if (pImpl_->windowSystemBarPropertyChangeAgent_ == nullptr) { pImpl_->windowSystemBarPropertyChangeAgent_ = new WindowManagerAgent(); } - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_STATUS_BAR_PROPERTY, pImpl_->windowSystemBarPropertyChangeAgent_); if (ret != WMError::WM_OK) { @@ -1878,7 +1931,7 @@ WMError WindowManager::UnregisterWindowSystemBarPropertyChangedListener( } WMError ret = WMError::WM_OK; if (pImpl_->windowSystemBarPropertyChangeAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_STATUS_BAR_PROPERTY, pImpl_->windowSystemBarPropertyChangeAgent_); if (ret == WMError::WM_OK) { @@ -1911,7 +1964,7 @@ void WindowManager::Impl::NotifyWindowSystemBarPropertyChange( WMError WindowManager::ShiftAppWindowFocus(int32_t sourcePersistentId, int32_t targetPersistentId) { - WMError ret = SingletonContainer::Get().ShiftAppWindowFocus(sourcePersistentId, targetPersistentId); + WMError ret = WindowAdapter::GetInstance(userId_)->ShiftAppWindowFocus(sourcePersistentId, targetPersistentId); if (ret != WMError::WM_OK) { WLOGFE("shift application window focus failed"); } @@ -1929,7 +1982,7 @@ WMError WindowManager::RegisterVisibleWindowNumChangedListener(const sptrvisibleWindowNumChangedListenerAgent_ == nullptr) { pImpl_->visibleWindowNumChangedListenerAgent_ = new WindowManagerAgent(); } - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_VISIBLE_WINDOW_NUM, pImpl_->visibleWindowNumChangedListenerAgent_); if (ret != WMError::WM_OK) { @@ -1949,7 +2002,7 @@ WMError WindowManager::RegisterVisibleWindowNumChangedListener(const sptr& pixelMap) { - return SingletonContainer::Get().GetSnapshotByWindowId(windowId, pixelMap); + return WindowAdapter::GetInstance(userId_)->GetSnapshotByWindowId(windowId, pixelMap); } WMError WindowManager::UnregisterVisibleWindowNumChangedListener(const sptr& listener) @@ -1968,7 +2021,7 @@ WMError WindowManager::UnregisterVisibleWindowNumChangedListener(const sptrvisibleWindowNumChangedListeners_.empty() && pImpl_->visibleWindowNumChangedListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_VISIBLE_WINDOW_NUM, pImpl_->visibleWindowNumChangedListenerAgent_); if (ret == WMError::WM_OK) { @@ -2004,7 +2057,7 @@ WMError WindowManager::RegisterWindowStyleChangedListener(const sptrwindowStyleListeners_.push_back(listener); } WMError ret = WMError::WM_OK; - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_STYLE, pImpl_->windowStyleListenerAgent_); if (ret != WMError::WM_OK) { TLOGW(WmsLogTag::WMS_MAIN, "RegisterWindowManagerAgent failed!"); @@ -2036,7 +2089,7 @@ WMError WindowManager::UnregisterWindowStyleChangedListener(const sptrwindowStyleListeners_.empty() && pImpl_->windowStyleListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapter::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_STYLE, pImpl_->windowStyleListenerAgent_); if (ret == WMError::WM_OK) { std::unique_lock lock(pImpl_->listenerMutex_); @@ -2049,7 +2102,7 @@ WMError WindowManager::UnregisterWindowStyleChangedListener(const sptr().GetWindowStyleType(styleType) == WMError::WM_OK) { + if (WindowAdapter::GetInstance(userId_)->GetWindowStyleType(styleType) == WMError::WM_OK) { return styleType; } return styleType; @@ -2057,7 +2110,7 @@ WindowStyleType WindowManager::GetWindowStyleType() WMError WindowManager::SkipSnapshotForAppProcess(int32_t pid, bool skip) { - WMError ret = SingletonContainer::Get().SkipSnapshotForAppProcess(pid, skip); + WMError ret = WindowAdapter::GetInstance(userId_)->SkipSnapshotForAppProcess(pid, skip); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "skip failed"); } @@ -2066,7 +2119,7 @@ WMError WindowManager::SkipSnapshotForAppProcess(int32_t pid, bool skip) WMError WindowManager::SetProcessWatermark(int32_t pid, const std::string& watermarkName, bool isEnabled) { - WMError ret = SingletonContainer::Get().SetProcessWatermark(pid, watermarkName, isEnabled); + WMError ret = WindowAdapter::GetInstance(userId_)->SetProcessWatermark(pid, watermarkName, isEnabled); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "failed"); } @@ -2076,7 +2129,7 @@ WMError WindowManager::SetProcessWatermark(int32_t pid, const std::string& water WMError WindowManager::GetWindowIdsByCoordinate(DisplayId displayId, int32_t windowNumber, int32_t x, int32_t y, std::vector& windowIds) const { - WMError ret = SingletonContainer::Get().GetWindowIdsByCoordinate( + WMError ret = WindowAdapter::GetInstance(userId_)->GetWindowIdsByCoordinate( displayId, windowNumber, x, y, windowIds); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::DEFAULT, "get windowIds by coordinate failed"); @@ -2086,7 +2139,7 @@ WMError WindowManager::GetWindowIdsByCoordinate(DisplayId displayId, int32_t win WMError WindowManager::UpdateScreenLockStatusForApp(const std::string& bundleName, bool isRelease) { - WMError ret = SingletonContainer::Get().UpdateScreenLockStatusForApp(bundleName, isRelease); + WMError ret = WindowAdapter::GetInstance(userId_)->UpdateScreenLockStatusForApp(bundleName, isRelease); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "update screen lock status failed"); } @@ -2096,7 +2149,7 @@ WMError WindowManager::UpdateScreenLockStatusForApp(const std::string& bundleNam WMError WindowManager::GetDisplayIdByWindowId(const std::vector& windowIds, std::unordered_map& windowDisplayIdMap) { - WMError ret = SingletonContainer::Get().GetDisplayIdByWindowId(windowIds, windowDisplayIdMap); + WMError ret = WindowAdapter::GetInstance(userId_)->GetDisplayIdByWindowId(windowIds, windowDisplayIdMap); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "failed"); } @@ -2105,7 +2158,7 @@ WMError WindowManager::GetDisplayIdByWindowId(const std::vector& windo WMError WindowManager::SetGlobalDragResizeType(DragResizeType dragResizeType) { - WMError ret = SingletonContainer::Get().SetGlobalDragResizeType(dragResizeType); + WMError ret = WindowAdapter::GetInstance(userId_)->SetGlobalDragResizeType(dragResizeType); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::DEFAULT, "failed"); } @@ -2114,7 +2167,7 @@ WMError WindowManager::SetGlobalDragResizeType(DragResizeType dragResizeType) WMError WindowManager::GetGlobalDragResizeType(DragResizeType& dragResizeType) { - WMError ret = SingletonContainer::Get().GetGlobalDragResizeType(dragResizeType); + WMError ret = WindowAdapter::GetInstance(userId_)->GetGlobalDragResizeType(dragResizeType); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::DEFAULT, "failed"); } @@ -2123,7 +2176,7 @@ WMError WindowManager::GetGlobalDragResizeType(DragResizeType& dragResizeType) WMError WindowManager::SetAppDragResizeType(const std::string& bundleName, DragResizeType dragResizeType) { - WMError ret = SingletonContainer::Get().SetAppDragResizeType(bundleName, dragResizeType); + WMError ret = WindowAdapter::GetInstance(userId_)->SetAppDragResizeType(bundleName, dragResizeType); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::DEFAULT, "failed"); } @@ -2132,7 +2185,7 @@ WMError WindowManager::SetAppDragResizeType(const std::string& bundleName, DragR WMError WindowManager::GetAppDragResizeType(const std::string& bundleName, DragResizeType& dragResizeType) { - WMError ret = SingletonContainer::Get().GetAppDragResizeType(bundleName, dragResizeType); + WMError ret = WindowAdapter::GetInstance(userId_)->GetAppDragResizeType(bundleName, dragResizeType); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::DEFAULT, "failed"); } @@ -2141,7 +2194,7 @@ WMError WindowManager::GetAppDragResizeType(const std::string& bundleName, DragR WMError WindowManager::SetAppKeyFramePolicy(const std::string& bundleName, const KeyFramePolicy& keyFramePolicy) { - WMError ret = SingletonContainer::Get().SetAppKeyFramePolicy(bundleName, keyFramePolicy); + WMError ret = WindowAdapter::GetInstance(userId_)->SetAppKeyFramePolicy(bundleName, keyFramePolicy); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::DEFAULT, "failed"); } @@ -2150,7 +2203,7 @@ WMError WindowManager::SetAppKeyFramePolicy(const std::string& bundleName, const WMError WindowManager::ShiftAppWindowPointerEvent(int32_t sourceWindowId, int32_t targetWindowId, int32_t fingerId) { - WMError ret = SingletonContainer::Get().ShiftAppWindowPointerEvent( + WMError ret = WindowAdapter::GetInstance(userId_)->ShiftAppWindowPointerEvent( sourceWindowId, targetWindowId, fingerId); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_PC, "failed"); @@ -2160,7 +2213,7 @@ WMError WindowManager::ShiftAppWindowPointerEvent(int32_t sourceWindowId, int32_ WMError WindowManager::NotifyScreenshotEvent(ScreenshotEventType type) { - WMError ret = SingletonContainer::Get().NotifyScreenshotEvent(type); + WMError ret = WindowAdapter::GetInstance(userId_)->NotifyScreenshotEvent(type); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "failed"); } @@ -2171,7 +2224,7 @@ WMError WindowManager::SetStartWindowBackgroundColor( const std::string& moduleName, const std::string& abilityName, uint32_t color) { int32_t uid = static_cast(getuid()); - WMError ret = SingletonContainer::Get().SetStartWindowBackgroundColor( + WMError ret = WindowAdapter::GetInstance(userId_)->SetStartWindowBackgroundColor( moduleName, abilityName, color, uid); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_PATTERN, "failed"); @@ -2189,7 +2242,7 @@ WMError WindowManager::RequestFocus(int32_t persistentId, bool isFocused, TLOGE(WmsLogTag::WMS_FOCUS, "could not find focus reason"); return WMError::WM_ERROR_INVALID_PARAM; } - WMError ret = SingletonContainer::Get().RequestFocusStatusBySA(persistentId, + WMError ret = WindowAdapter::GetInstance(userId_)->RequestFocusStatusBySA(persistentId, isFocused, byForeground, static_cast(curReason)); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_FOCUS, "failed"); @@ -2199,7 +2252,7 @@ WMError WindowManager::RequestFocus(int32_t persistentId, bool isFocused, WMError WindowManager::MinimizeByWindowId(const std::vector& windowIds) { - WMError ret = SingletonContainer::Get().MinimizeByWindowId(windowIds); + WMError ret = WindowAdapter::GetInstance(userId_)->MinimizeByWindowId(windowIds); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_LIFE, "failed"); } @@ -2208,7 +2261,7 @@ WMError WindowManager::MinimizeByWindowId(const std::vector& windowIds) WMError WindowManager::SetForegroundWindowNum(uint32_t windowNum) { - WMError ret = SingletonContainer::Get().SetForegroundWindowNum(windowNum); + WMError ret = WindowAdapter::GetInstance(userId_)->SetForegroundWindowNum(windowNum); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_PC, "failed"); } @@ -2360,7 +2413,7 @@ WMError WindowManager::AnimateTo(int32_t windowId, WindowAnimationProperty anima { TLOGI(WmsLogTag::WMS_ANIMATION, "In, windowId: %{public}d, targetScale: %{public}f, animationOption: %{public}s", windowId, animationProperty.targetScale, animationOption.ToString().c_str()); - WMError ret = SingletonContainer::Get().AnimateTo(windowId, animationProperty, animationOption); + WMError ret = WindowAdapter::GetInstance(userId_)->AnimateTo(windowId, animationProperty, animationOption); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ANIMATION, "Failed, errorCode: %{public}d", static_cast(ret)); } @@ -2421,7 +2474,7 @@ WMError WindowManager::AddSessionBlackList( const std::unordered_set& bundleNames, const std::unordered_set& privacyWindowTags) { TLOGI(WmsLogTag::WMS_ATTRIBUTE, "in"); - auto ret = SingletonContainer::Get().AddSessionBlackList(bundleNames, privacyWindowTags); + auto ret = WindowAdapter::GetInstance(userId_)->AddSessionBlackList(bundleNames, privacyWindowTags); return ret; } @@ -2429,7 +2482,7 @@ WMError WindowManager::RemoveSessionBlackList( const std::unordered_set& bundleNames, const std::unordered_set& privacyWindowTags) { TLOGI(WmsLogTag::WMS_ATTRIBUTE, "in"); - auto ret = SingletonContainer::Get(). + auto ret = WindowAdapter::GetInstance(userId_)-> RemoveSessionBlackList(bundleNames, privacyWindowTags); return ret; } diff --git a/wm/src/window_manager_lite.cpp b/wm/src/window_manager_lite.cpp index 75f62e1ae7e70d83c5940c784dfbf6007408e624..0ce7697b936eb2268f17b16beb9fd6ac64048055 100644 --- a/wm/src/window_manager_lite.cpp +++ b/wm/src/window_manager_lite.cpp @@ -29,8 +29,8 @@ namespace Rosen { namespace { constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowManagerLite"}; } - -WM_IMPLEMENT_SINGLE_INSTANCE(WindowManagerLite) +std::unordered_map> WindowManagerLite::windowManagerLiteMap_ = {}; +std::mutex_ WindowManagerLite::windowManagerLiteMapMutex_; class WindowManagerLite::Impl { public: @@ -58,6 +58,10 @@ public: void NotifyAccessibilityWindowInfo(const std::vector>& infos, WindowUpdateType type); + /** + * Compatible with the singleton pattern, ensuring that calls can + * still be made through SingletonContainer::Get + */ static inline SingletonDelegator delegator_; std::recursive_mutex& mutex_; @@ -350,16 +354,68 @@ void WindowManagerLite::Impl::UpdatePiPWindowStateChanged(const std::string& bun } } -WindowManagerLite::WindowManagerLite() : pImpl_(std::make_unique(mutex_)) -{ -} +WindowManagerLite::WindowManagerLite(const int32_t userId) + : pImpl_(std::make_unique(mutex_)),userId_(userId){} WindowManagerLite::~WindowManagerLite() { + TLOGI(WmsLogTag::WMS_SCB, "window manager lite destroyed"); std::lock_guard lock(mutex_); destroyed_ = true; } +WindowAdapterLite& WindowAdapterLite::GetInstance() +{ + static sptr instance = nullptr; + static std::mutex mtx; + if (instance == nullptr) { + std::lock_guard lock(mtx); + if(instance == nullptr){ + instance = new WindowManagerLite(); + } + } + return *instance; +} + +WindowAdapterLite& WindowAdapterLite::GetInstance(const int32_t userId) +{ + /** + * Only system applications or services with a userId of 0 are allowed to communicate + * with multiple WMS-Servers and are permitted to listen for WMS connection status. + */ + sptr instance = nullptr; + int32_t clientUserId = GetUserIdByUid(getuid()); + if (clientUserId != SYSTEM_USERID || userId <= INVALID_USER_ID) { + TLOGI(WmsLogTag::WMS_MULTI_USER, "user singleton mode"); + instance = &WindowAdapterLite::GetInstance(); + return instance; + } + + //multi-instance mode + { + std:lock_guard lock(windowManagerLiteMapMutex_); + auto iter = windowManagerLiteMap_.find(userId); + if (iter != windowManagerLiteMap_.end()) { + return iter->second; + } + } + TLOGI(WmsLogTag::WMS_MULTI_USER, "create new instance userId:%{public}d", userId); + instance = new WindowAdapterLite(userId); + { + std::lock_guard lock(windowManagerLiteMapMutex_); + windowManagerLiteMap_.insert({userId,instance}); + } + return instance; +} + +WMError WindowManagerLite::RemoveInstanceByUserId(const int userId) +{ + TLOGI(WmsLogTag::WMS_MULTI_USER, "create new instance userId:%{public}d", userId); + std:lock_guard lock(windowManagerLiteMapMutex_); + windowManagerLiteMap_.erase(userId); + return WMError::OK; +} + WMError WindowManagerLite::RegisterFocusChangedListener(const sptr& listener) { if (listener == nullptr) { @@ -371,7 +427,7 @@ WMError WindowManagerLite::RegisterFocusChangedListener(const sptrfocusChangedListenerAgent_ == nullptr) { pImpl_->focusChangedListenerAgent_ = new (std::nothrow) WindowManagerAgentLite(); - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapterLite::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_FOCUS, pImpl_->focusChangedListenerAgent_); } if (ret != WMError::WM_OK) { @@ -404,7 +460,7 @@ WMError WindowManagerLite::UnregisterFocusChangedListener(const sptrfocusChangedListeners_.erase(iter); WMError ret = WMError::WM_OK; if (pImpl_->focusChangedListeners_.empty() && pImpl_->focusChangedListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapterLite::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_FOCUS, pImpl_->focusChangedListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->focusChangedListenerAgent_ = nullptr; @@ -423,7 +479,7 @@ WMError WindowManagerLite::RegisterVisibilityChangedListener(const sptrwindowVisibilityListenerAgent_ == nullptr) { pImpl_->windowVisibilityListenerAgent_ = new (std::nothrow) WindowManagerAgentLite(); - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapterLite::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY, pImpl_->windowVisibilityListenerAgent_); } @@ -456,7 +512,7 @@ WMError WindowManagerLite::UnregisterVisibilityChangedListener(const sptrwindowVisibilityListeners_.empty() && pImpl_->windowVisibilityListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapterLite::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY, pImpl_->windowVisibilityListenerAgent_); if (ret == WMError::WM_OK) { @@ -477,7 +533,7 @@ WMError WindowManagerLite::RegisterVisibilityStateChangedListener(const sptrwindowVisibilityStateListenerAgent_ == nullptr) { pImpl_->windowVisibilityStateListenerAgent_ = new WindowManagerAgentLite(); } - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapterLite::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY, pImpl_->windowVisibilityStateListenerAgent_); if (ret != WMError::WM_OK) { @@ -509,7 +565,7 @@ WMError WindowManagerLite::UnregisterVisibilityStateChangedListener(const sptrwindowVisibilityStateListeners_.empty() && pImpl_->windowVisibilityStateListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapterLite::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY, pImpl_->windowVisibilityStateListenerAgent_); if (ret == WMError::WM_OK) { @@ -522,7 +578,7 @@ WMError WindowManagerLite::UnregisterVisibilityStateChangedListener(const sptr().GetFocusWindowInfo(focusInfo, displayId); + WindowAdapterLite::GetInstance(userId_)->GetFocusWindowInfo(focusInfo, displayId); } void WindowManagerLite::UpdateFocusChangeInfo(const sptr& focusChangeInfo, bool focused) const @@ -548,7 +604,7 @@ void WindowManagerLite::UpdateWindowVisibilityInfo( WMError WindowManagerLite::GetVisibilityWindowInfo(std::vector>& infos) const { - WMError ret = SingletonContainer::Get().GetVisibilityWindowInfo(infos); + WMError ret = WindowAdapterLite::GetInstance(userId_)->GetVisibilityWindowInfo(infos); if (ret != WMError::WM_OK) { WLOGFE("get window visibility info failed"); } @@ -590,7 +646,7 @@ WMError WindowManagerLite::RegisterDrawingContentChangedListener(const sptrwindowDrawingContentListenerAgent_ == nullptr) { pImpl_->windowDrawingContentListenerAgent_ = new (std::nothrow) WindowManagerAgentLite(); - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapterLite::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_DRAWING_STATE, pImpl_->windowDrawingContentListenerAgent_); } @@ -623,7 +679,7 @@ WMError WindowManagerLite::UnregisterDrawingContentChangedListener(const sptrwindowDrawingContentListeners_.empty() && pImpl_->windowDrawingContentListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapterLite::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_DRAWING_STATE, pImpl_->windowDrawingContentListenerAgent_); if (ret == WMError::WM_OK) { @@ -646,7 +702,7 @@ void WindowManagerLite::NotifyAccessibilityWindowInfo(const std::vector().GetWindowModeType(windowModeType); + WMError ret = WindowAdapterLite::GetInstance(userId_)->GetWindowModeType(windowModeType); if (ret != WMError::WM_OK) { WLOGFE("get window visibility info failed"); } @@ -664,7 +720,7 @@ WMError WindowManagerLite::RegisterWindowModeChangedListener(const sptrwindowModeListenerAgent_ == nullptr) { pImpl_->windowModeListenerAgent_ = new (std::nothrow) WindowManagerAgentLite(); } - WMError ret = SingletonContainer::Get().RegisterWindowManagerAgent( + WMError ret = WindowAdapterLite::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_MODE, pImpl_->windowModeListenerAgent_); if (ret != WMError::WM_OK) { TLOGW(WmsLogTag::WMS_MAIN, "RegisterWindowManagerAgent failed!"); @@ -696,7 +752,7 @@ WMError WindowManagerLite::UnregisterWindowModeChangedListener(const sptrwindowModeListeners_.erase(iter); WMError ret = WMError::WM_OK; if (pImpl_->windowModeListeners_.empty() && pImpl_->windowModeListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapterLite::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_MODE, pImpl_->windowModeListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->windowModeListenerAgent_ = nullptr; @@ -716,7 +772,7 @@ WMError WindowManagerLite::RegisterCameraWindowChangedListener(const sptrcameraWindowChangedListenerAgent_ == nullptr) { pImpl_->cameraWindowChangedListenerAgent_ = new WindowManagerAgentLite(); } - WMError ret = SingletonContainer::Get().RegisterWindowManagerAgent( + WMError ret = WindowAdapterLite::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_WINDOW, pImpl_->cameraWindowChangedListenerAgent_); if (ret != WMError::WM_OK) { TLOGW(WmsLogTag::WMS_SYSTEM, "RegisterWindowManagerAgent failed!"); @@ -751,7 +807,7 @@ WMError WindowManagerLite::UnregisterCameraWindowChangedListener(const sptrcameraWindowChangedListeners_.empty() && pImpl_->cameraWindowChangedListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapterLite::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_WINDOW, pImpl_->cameraWindowChangedListenerAgent_); if (ret == WMError::WM_OK) { @@ -763,7 +819,7 @@ WMError WindowManagerLite::UnregisterCameraWindowChangedListener(const sptr().RaiseWindowToTop(persistentId); + WMError ret = WindowAdapterLite::GetInstance(userId_)->RaiseWindowToTop(persistentId); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_SYSTEM, "raise window to top failed."); } @@ -773,12 +829,12 @@ WMError WindowManagerLite::RaiseWindowToTop(int32_t persistentId) WMError WindowManagerLite::GetMainWindowInfos(int32_t topNum, std::vector& topNInfo) { TLOGI(WmsLogTag::WMS_MAIN, "Get main window info lite"); - return SingletonContainer::Get().GetMainWindowInfos(topNum, topNInfo); + return WindowAdapterLite::GetInstance(userId_)->GetMainWindowInfos(topNum, topNInfo); } WMError WindowManagerLite::GetCallingWindowInfo(CallingWindowInfo& callingWindowInfo) { - return SingletonContainer::Get().GetCallingWindowInfo(callingWindowInfo); + return WindowAdapterLite::GetInstance(userId_)->GetCallingWindowInfo(callingWindowInfo); } WMError WindowManagerLite::RegisterWMSConnectionChangedListener(const sptr& listener) @@ -838,7 +894,7 @@ WMError WindowManagerLite::GetAllMainWindowInfos(std::vector& in TLOGE(WmsLogTag::WMS_MAIN, "infos is not empty."); return WMError::WM_ERROR_INVALID_PARAM; } - return SingletonContainer::Get().GetAllMainWindowInfos(infos); + return WindowAdapterLite::GetInstance(userId_)->GetAllMainWindowInfos(infos); } WMError WindowManagerLite::ClearMainSessions(const std::vector& persistentIds) @@ -847,7 +903,7 @@ WMError WindowManagerLite::ClearMainSessions(const std::vector& persist TLOGW(WmsLogTag::WMS_MAIN, "Clear main Session failed, persistentIds is empty."); return WMError::WM_OK; } - return SingletonContainer::Get().ClearMainSessions(persistentIds); + return WindowAdapterLite::GetInstance(userId_)->ClearMainSessions(persistentIds); } WMError WindowManagerLite::ClearMainSessions(const std::vector& persistentIds, @@ -857,7 +913,7 @@ WMError WindowManagerLite::ClearMainSessions(const std::vector& persist TLOGW(WmsLogTag::WMS_MAIN, "Clear main Session failed, persistentIds is empty."); return WMError::WM_OK; } - return SingletonContainer::Get().ClearMainSessions(persistentIds, clearFailedIds); + return WindowAdapterLite::GetInstance(userId_)->ClearMainSessions(persistentIds, clearFailedIds); } WMError WindowManagerLite::NotifyWindowStyleChange(WindowStyleType type) @@ -892,7 +948,7 @@ WMError WindowManagerLite::RegisterWindowStyleChangedListener(const sptrwindowStyleListeners_.push_back(listener); } WMError ret = WMError::WM_OK; - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapterLite::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_STYLE, pImpl_->windowStyleListenerAgent_); if (ret != WMError::WM_OK) { TLOGW(WmsLogTag::WMS_MAIN, "RegisterWindowManagerAgent failed!"); @@ -924,7 +980,7 @@ WMError WindowManagerLite::UnregisterWindowStyleChangedListener(const sptrwindowStyleListeners_.empty() && pImpl_->windowStyleListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapterLite::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_STYLE, pImpl_->windowStyleListenerAgent_); if (ret == WMError::WM_OK) { std::lock_guard lock(pImpl_->mutex_); @@ -956,7 +1012,7 @@ WMError WindowManagerLite::RegisterCallingWindowDisplayChangedListener( pImpl_->callingDisplayChangedListeners_.emplace_back(listener); } WMError ret = WMError::WM_OK; - ret = SingletonContainer::Get().RegisterWindowManagerAgent( + ret = WindowAdapterLite::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CALLING_DISPLAY, pImpl_->callingDisplayListenerAgent_); if (ret != WMError::WM_OK) { TLOGW(WmsLogTag::WMS_KEYBOARD, "Register agent failed!"); @@ -991,7 +1047,7 @@ WMError WindowManagerLite::UnregisterCallingWindowDisplayChangedListener( } WMError ret = WMError::WM_OK; if (pImpl_->callingDisplayChangedListeners_.empty() && pImpl_->callingDisplayListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapterLite::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CALLING_DISPLAY, pImpl_->callingDisplayListenerAgent_); if (ret == WMError::WM_OK) { std::lock_guard lock(pImpl_->mutex_); @@ -1004,7 +1060,7 @@ WMError WindowManagerLite::UnregisterCallingWindowDisplayChangedListener( WindowStyleType WindowManagerLite::GetWindowStyleType() { WindowStyleType styleType; - if (SingletonContainer::Get().GetWindowStyleType(styleType) == WMError::WM_OK) { + if (WindowAdapterLite::GetInstance(userId_)->GetWindowStyleType(styleType) == WMError::WM_OK) { return styleType; } return styleType; @@ -1016,7 +1072,7 @@ WMError WindowManagerLite::TerminateSessionByPersistentId(int32_t persistentId) TLOGE(WmsLogTag::WMS_LIFE, "persistentId is invalid."); return WMError::WM_ERROR_INVALID_PARAM; } - return SingletonContainer::Get().TerminateSessionByPersistentId(persistentId); + return WindowAdapterLite::GetInstance(userId_)->TerminateSessionByPersistentId(persistentId); } WMError WindowManagerLite::CloseTargetFloatWindow(const std::string& bundleName) @@ -1025,7 +1081,7 @@ WMError WindowManagerLite::CloseTargetFloatWindow(const std::string& bundleName) TLOGE(WmsLogTag::WMS_MULTI_WINDOW, "bundleName is empty."); return WMError::WM_ERROR_INVALID_PARAM; } - return SingletonContainer::Get().CloseTargetFloatWindow(bundleName); + return WindowAdapterLite::GetInstance(userId_)->CloseTargetFloatWindow(bundleName); } WMError WindowManagerLite::RegisterPiPStateChangedListener(const sptr& listener) @@ -1040,7 +1096,7 @@ WMError WindowManagerLite::RegisterPiPStateChangedListener(const sptrpipStateChangedListenerAgent_ == nullptr) { pImpl_->pipStateChangedListenerAgent_ = new WindowManagerAgentLite(); } - WMError ret = SingletonContainer::Get().RegisterWindowManagerAgent( + WMError ret = WindowAdapterLite::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_PIP, pImpl_->pipStateChangedListenerAgent_); if (ret != WMError::WM_OK) { TLOGW(WmsLogTag::WMS_PIP, "RegisterWindowManagerAgent failed!"); @@ -1076,7 +1132,7 @@ WMError WindowManagerLite::UnregisterPiPStateChangedListener(const sptrpipStateChangedListeners_.empty() && pImpl_->pipStateChangedListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapterLite::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_PIP, pImpl_->pipStateChangedListenerAgent_); if (ret == WMError::WM_OK) { @@ -1093,12 +1149,12 @@ WMError WindowManagerLite::CloseTargetPiPWindow(const std::string& bundleName) return WMError::WM_ERROR_INVALID_PARAM; } TLOGD(WmsLogTag::WMS_PIP, "bundleName:%{public}s", bundleName.c_str()); - return SingletonContainer::Get().CloseTargetPiPWindow(bundleName); + return WindowAdapterLite::GetInstance(userId_)->CloseTargetPiPWindow(bundleName); } WMError WindowManagerLite::GetCurrentPiPWindowInfo(std::string& bundleName) { - return SingletonContainer::Get().GetCurrentPiPWindowInfo(bundleName); + return WindowAdapterLite::GetInstance(userId_)->GetCurrentPiPWindowInfo(bundleName); } void WindowManagerLite::UpdatePiPWindowStateChanged(const std::string& bundleName, bool isForeground) const @@ -1108,7 +1164,7 @@ void WindowManagerLite::UpdatePiPWindowStateChanged(const std::string& bundleNam WMError WindowManagerLite::GetAccessibilityWindowInfo(std::vector>& infos) const { - WMError ret = SingletonContainer::Get().GetAccessibilityWindowInfo(infos); + WMError ret = WindowAdapterLite::GetInstance(userId_)->GetAccessibilityWindowInfo(infos); if (ret != WMError::WM_OK) { WLOGFE("get window info failed"); } @@ -1125,7 +1181,7 @@ WMError WindowManagerLite::RegisterWindowUpdateListener(const sptrwindowUpdateListenerAgent_ == nullptr) { pImpl_->windowUpdateListenerAgent_ = new WindowManagerAgentLite(); } - WMError ret = SingletonContainer::Get().RegisterWindowManagerAgent( + WMError ret = WindowAdapterLite::GetInstance(userId_)->RegisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_UPDATE, pImpl_->windowUpdateListenerAgent_); if (ret != WMError::WM_OK) { WLOGFW("RegisterWindowManagerAgent failed!"); @@ -1156,7 +1212,7 @@ WMError WindowManagerLite::UnregisterWindowUpdateListener(const sptrwindowUpdateListeners_.erase(iter); WMError ret = WMError::WM_OK; if (pImpl_->windowUpdateListeners_.empty() && pImpl_->windowUpdateListenerAgent_ != nullptr) { - ret = SingletonContainer::Get().UnregisterWindowManagerAgent( + ret = WindowAdapterLite::GetInstance(userId_)->UnregisterWindowManagerAgent( WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_UPDATE, pImpl_->windowUpdateListenerAgent_); if (ret == WMError::WM_OK) { pImpl_->windowUpdateListenerAgent_ = nullptr; @@ -1238,7 +1294,7 @@ WMError WindowManagerLite::ListWindowInfo(const WindowInfoOption& windowInfoOpti static_cast(windowInfoOption.windowInfoFilterOption), static_cast(windowInfoOption.windowInfoTypeOption), windowInfoOption.displayId, windowInfoOption.windowId); - WMError ret = SingletonContainer::Get().ListWindowInfo(windowInfoOption, infos); + WMError ret = WindowAdapterLite::GetInstance(userId_)->ListWindowInfo(windowInfoOption, infos); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "failed"); } @@ -1256,7 +1312,7 @@ WMError WindowManagerLite::SendPointerEventForHover(const std::shared_ptr().SendPointerEventForHover(pointerEvent); + WMError ret = WindowAdapterLite::GetInstance(userId_)->SendPointerEventForHover(pointerEvent); if (ret != WMError::WM_OK) { TLOGE(WmsLogTag::WMS_EVENT, "send failed"); } diff --git a/wm/test/unittest/window_adapter_lite_test.cpp b/wm/test/unittest/window_adapter_lite_test.cpp index eaa16cd7ef2386c2bd3f142be4022971c802132d..5b053668a2796d5651d22a5859831bfb70e5f7eb 100644 --- a/wm/test/unittest/window_adapter_lite_test.cpp +++ b/wm/test/unittest/window_adapter_lite_test.cpp @@ -290,6 +290,41 @@ HWTEST_F(WindowAdapterLiteTest, SendPointerEventForHover, Function | SmallTest | auto err = windowAdapterLite_->SendPointerEventForHover(pointerEvent); EXPECT_EQ(WMError::WM_ERROR_INVALID_PERMISSION, err); } + +/** + * @tc.name: GetInstance + * @tc.desc: sptr + * @tc.type: FUNC + */ +HWTEST_F(WindowAdapterLiteTest, GetInstance, Function | SmallTest | Level2) +{ + sptr instance = nullptr; + int32_t userId = 100; + instance = WindowAdapterLite::GetInstance(userId); + ASSERT_NE(instance, nullptr); + + userId = -1; + instance = WindowAdapterLite::GetInstance(userId); + ASSERT_NE(instance, nullptr); + + userId = -2; + instance = WindowAdapterLite::GetInstance(userId); + ASSERT_NE(instance, nullptr); +} + +/** + * @tc.name: WMSDeathRecipient::OnRemoteDied + * @tc.desc: WindowAdapterLite/OnRemoteDied + * @tc.type: FUNC + */ +HWTEST_F(WindowAdapterLiteTest, OnRemoteDied2, TestSize.Level1) +{ + sptr wmSDeathRecipient = sptr::MakeSptr(); + ASSERT_NE(wmSDeathRecipient, nullptr); + sptr service = sptr::MakeSptr(); + wptr wptrDeath = wptr(service); + wmSDeathRecipient->OnRemoteDied(wptrDeath); +} } // namespace } // namespace Rosen } // namespace OHOS \ No newline at end of file diff --git a/wm/test/unittest/window_adapter_test.cpp b/wm/test/unittest/window_adapter_test.cpp index 9196a00c87a4b6074c978e04e8721cd8e7f304e8..e503d33534df689b6c78d420feda6f40e82f8e04 100644 --- a/wm/test/unittest/window_adapter_test.cpp +++ b/wm/test/unittest/window_adapter_test.cpp @@ -1231,6 +1231,58 @@ HWTEST_F(WindowAdapterTest, UseImplicitAnimation, TestSize.Level1) WindowAdapter windowAdapter; ASSERT_EQ(WMError::WM_ERROR_INVALID_SESSION, windowAdapter.UseImplicitAnimation(0, true)); } + +/** + * @tc.name: WMSDeathRecipient + * @tc.desc: normal func + * @tc.type: FUNC + */ +HWTEST_F(WindowAdapterTest, WMSDeathRecipient, TestSize.Level1) +{ + auto wmsDeath_ = sptr::MakeSptr(); + ASSERT_NE(wmsDeath_, nullptr); + + sptr token = nullptr; + wmsDeath_->OnRemoteDied(wptr(token)); + + token = sptr::MakeSptr(); + wmsDeath_->OnRemoteDied(wptr(token)); +} + +/** + * @tc.name: GetInstance + * @tc.desc: normal function + * @tc.type: FUNC + */ +HWTEST_F(WindowAdapterTest, GetInstance, TestSize.Level1) +{ + sptr instance = nullptr; + int32_t userId = -1; + instance = WindowAdapter::GetInstance(userId); + ASSERT_NE(instance, nullptr); + + userId = 101; + instance = WindowAdapter::GetInstance(userId); + ASSERT_NE(instance, nullptr); + + // branch overried + instance = WindowAdapter::GetInstance(userId); + ASSERT_NE(instance, nullptr); +} + +/** + * @tc.name: InitSSMProxy + * @tc.desc: normal function + * @tc.type: FUNC + */ +HWTEST_F(WindowAdapterTest, InitSSMProxy, TestSize.Level1) +{ + int32_t userId = -1; + sptr instance = WindowAdapter::GetInstance(userId); + ASSERT_NE(instance, nullptr); + + ASSERT_NE(true, instance->InitWMSProxy()); +} } // namespace } // namespace Rosen } // namespace OHOS \ No newline at end of file diff --git a/wm/test/unittest/window_manager_lite_test.cpp b/wm/test/unittest/window_manager_lite_test.cpp index e330377e88917564b17679e4af32abe47aba0e73..d98c1c2a4635bd9b56782d1c94b63280b0c06ae7 100644 --- a/wm/test/unittest/window_manager_lite_test.cpp +++ b/wm/test/unittest/window_manager_lite_test.cpp @@ -1610,6 +1610,38 @@ HWTEST_F(WindowManagerLiteTest, SendPointerEventForHover, Function | SmallTest | ret = windowManager.SendPointerEventForHover(pointerEvent); EXPECT_EQ(ret, WMError::WM_ERROR_INVALID_PERMISSION); } + +/** + * @tc.name: GetInstance + * @tc.desc: normal function + * @tc.type: FUNC + */ +HWTEST_F(WindowManagerLiteTest, GetInstance, TestSize.Level1) +{ + sptr instance = nullptr; + int32_t userId = -1; + instance = WindowManagerLite::GetInstance(userId); + ASSERT_NE(instance, nullptr); + + userId = 101; + instance = WindowManagerLite::GetInstance(userId); + ASSERT_NE(instance, nullptr); + + // branch overried + instance = WindowManagerLite::GetInstance(userId); + ASSERT_NE(instance, nullptr); +} + +/** + * @tc.name: RemoveInstanceByUserId + * @tc.desc: normal function + * @tc.type: FUNC + */ +HWTEST_F(WindowManagerLiteTest, RemoveInstanceByUserId, TestSize.Level1) +{ + int32_t userId = 101; + ASSERT_EQ(WMError::WM_OK, WindowManagerLite::RemoveInstanceByUserId(userId)); +} } } // namespace } // namespace OHOS::Rosen \ No newline at end of file diff --git a/wm/test/unittest/window_manager_test.cpp b/wm/test/unittest/window_manager_test.cpp index 12e7f6e3d997116fdd22a5df5b94da0b31f182f4..1389b814212d56150ba7a66113f535aee54e5e5c 100644 --- a/wm/test/unittest/window_manager_test.cpp +++ b/wm/test/unittest/window_manager_test.cpp @@ -2522,6 +2522,38 @@ HWTEST_F(WindowManagerTest, RemoveSessionBlackList01, TestSize.Level1) ret = WindowManager::GetInstance().RemoveSessionBlackList(bundleNames, privacyWindowTags); EXPECT_EQ(WMError::WM_ERROR_INVALID_PERMISSION, ret); } + +/** + * @tc.name: GetInstance + * @tc.desc: normal function + * @tc.type: FUNC + */ +HWTEST_F(WindowManagerTest, GetInstance, TestSize.Level1) +{ + sptr instance = nullptr; + int32_t userId = -1; + instance = WindowManager::GetInstance(userId); + ASSERT_NE(instance, nullptr); + + userId = 101; + instance = WindowManager::GetInstance(userId); + ASSERT_NE(instance, nullptr); + + // branch overried + instance = WindowManager::GetInstance(userId); + ASSERT_NE(instance, nullptr); +} + +/** + * @tc.name: RemoveInstanceByUserId + * @tc.desc: normal function + * @tc.type: FUNC + */ +HWTEST_F(WindowManagerTest, RemoveInstanceByUserId, TestSize.Level1) +{ + int32_t userId = 101; + ASSERT_EQ(WMError::WM_OK, WindowManager::RemoveInstanceByUserId(userId)); +} } } // namespace } // namespace Rosen