diff --git a/interfaces/innerkits/wm/window_manager.h b/interfaces/innerkits/wm/window_manager.h index 9e9713ffedf85c1522988512b90d65909207bda5..f13d49654501ee4db05d7f729f9df66fb4d1dae1 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 { -WM_DECLARE_SINGLE_INSTANCE_BASE(WindowManager); -friend class WindowManagerAgent; -friend class WMSDeathRecipient; -friend class SSMDeathRecipient; +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..de5f7bd9547e03af503e3da0b524cc9f76415639 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 { -WM_DECLARE_SINGLE_INSTANCE_BASE(WindowManagerLite); -friend class WindowManagerAgentLite; -friend class WMSDeathRecipient; -friend class SSMDeathRecipient; +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..8259913332e28a4d7e924dbc202e80d14c83c2a8 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: + explicit SSMDeathRecipient(const int32_t userId = INVALID_USER_ID); void OnRemoteDied(const wptr& wptrDeath) override; + +private: + int32_t userId_; }; + class FoundationDeathRecipient : public IRemoteObject::DeathRecipient { public: + explicit 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 sptr GetInstance(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..7ec14783cb8c00762600c8677e8eac1757931281 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: + explicit SSMDeathRecipientLite(const int32_t userId = INVALID_USER_ID); void OnRemoteDied(const wptr& wptrDeath) override; + +private: + int32_t userId_; }; class FoundationDeathRecipientLite : public IRemoteObject::DeathRecipient { public: + explicit FoundationDeathRecipientLite(const int32_t userId = INVALID_USER_ID); void OnRemoteDied(const wptr& wptrDeath) override; + +private: + int32_t userId_; }; -class SessionManagerLite { -WM_DECLARE_SINGLE_INSTANCE_BASE(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/scene_session_manager.cpp b/window_scene/session_manager/src/scene_session_manager.cpp index dcfb44388f701d5dd3dc59b28db79af34b06f0f3..d08e8f2cad296a8a20e99f53478fb8f6d9e4fbee 100644 --- a/window_scene/session_manager/src/scene_session_manager.cpp +++ b/window_scene/session_manager/src/scene_session_manager.cpp @@ -2055,7 +2055,7 @@ void SceneSessionManager::SetSkipEventOnCastPlusInner(int32_t windowId, bool isS { TLOGI(WmsLogTag::WMS_ATTRIBUTE, "Wid: %{public}d, isSkip: %{public}d", windowId, isSkip); auto sceneSession = GetSceneSession(windowId); - if(sceneSession == nullptr) { + if (sceneSession == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "SceneSession is nullptr, Wid: %{public}d", windowId); return; } diff --git a/window_scene/session_manager/src/session_manager.cpp b/window_scene/session_manager/src/session_manager.cpp index f2d7aa4d73fc5a0563e40446b6a53a79d15035d2..0ea138bcb932064759edfa59d6166b1f13cec690 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,35 @@ 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 = 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 +190,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 +212,24 @@ __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 +238,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 +382,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,13 +442,11 @@ 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 +470,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..ff99c58722e88a57c7387187544d87144a533d8e 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) +{ + 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) +{ + 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) +{ + 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,38 @@ public: SessionManagerLite::GetInstance().DeleteSessionListener(listener); return ret; } - + private: static inline BrokerDelegator delegator_; }; -WM_IMPLEMENT_SINGLE_INSTANCE(SessionManagerLite) +SessionManagerLite& SessionManagerLite::GetInstance() +{ + static sptr 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 +171,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 +353,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_->GetSessionManagerServiceByUserId(userId_, remoteObject); + } if (errCode != ERR_NONE) { TLOGE(WmsLogTag::DEFAULT, "userId is illegal"); } @@ -365,7 +411,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 +429,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 +505,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,9 +525,11 @@ 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(); - mockSessionManagerServiceProxy_->RegisterSMSLiteRecoverListener(smsRecoverListener_); + TLOGD(WmsLogTag::WMS_RECOVER, "Register recover listener, userId_: %{public}d", userId_); + mockSessionManagerServiceProxy_->RegisterSMSLiteRecoverListener(smsRecoverListener_, userId_); IPCSkeleton::SetCallingIdentity(identity); } } @@ -489,7 +539,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 +557,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/BUILD.gn b/window_scene/test/unittest/BUILD.gn index 587e7e334921240f99ec080d6146248edf72abc9..61db84a37b6799e2feeb9b4d9dd9be23674b8330 100644 --- a/window_scene/test/unittest/BUILD.gn +++ b/window_scene/test/unittest/BUILD.gn @@ -1362,7 +1362,10 @@ ohos_unittest("ws_mock_session_manager_service_test") { include_dirs = [ "../mock" ] - sources = [ "mock_session_manager_service_test.cpp" ] + sources = [ + "${window_base_path}/window_scene/test/mock/mock_accesstoken_kit.cpp", + "mock_session_manager_service_test.cpp" + ] deps = [ ":ws_unittest_common", diff --git a/window_scene/test/unittest/mock_session_manager_service_test.cpp b/window_scene/test/unittest/mock_session_manager_service_test.cpp index ebc1648b8f43c4e5237db2535d8428d86a3cb9ff..87d5b75d9b494476b3aa889a0334aed10236ae24 100644 --- a/window_scene/test/unittest/mock_session_manager_service_test.cpp +++ b/window_scene/test/unittest/mock_session_manager_service_test.cpp @@ -21,6 +21,8 @@ #include "iremote_object_mocker.h" #include "scene_session_manager.h" +#include "session_manager_service_proxy.h" +#include "../mock/mock_accesstoken_kit.h" #include "wm_common.h" using namespace testing; @@ -35,6 +37,7 @@ public: NotifySCBSnapshotSkipByUserIdAndBundleNames, (int32_t, const std::vector&, const sptr&), (override)); + MOCK_METHOD(int32_t, GetUserIdByCallingUid, (), (override)); }; namespace { @@ -129,6 +132,391 @@ HWTEST(MockSessionManagerServiceTest, RecoverSCBSnapshotSkipByUserId, TestSize.L ret = mockMockSms.RecoverSCBSnapshotSkipByUserId(100); EXPECT_EQ(ERR_OK, ret); } + +/** + * @tc.name: SetSessionManagerServiceFailed + * @tc.desc: test the function of SetSessionManagerServiceFailed + * @tc.type: FUNC + */ +HWTEST(MockSessionManagerServiceTest, SetSessionManagerServiceFailed, TestSize.Level1) +{ + MockMockSessionManagerService mockMockSms; + sptr nullSessionManagerService = nullptr; + bool result = mockMockSms.SetSessionManagerService(nullSessionManagerService); + EXPECT_EQ(result, false); + + sptr mockSessionManagerService = sptr::MakeSptr(); + // Mock GetUserIdByCallingUid() 返回非法用户id -1 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(-1)); + result = mockMockSms.SetSessionManagerService(mockSessionManagerService); + EXPECT_EQ(result, false); +} + +/** + * @tc.name: GetSessionManagerService + * @tc.desc: test the function of GetSessionManagerService + * @tc.type: FUNC + */ +HWTEST(MockSessionManagerServiceTest, GetSessionManagerService, TestSize.Level1) +{ + sptr sessionManagerService; + MockMockSessionManagerService mockMockSms; + // 1. Mock GetUserIdByCallingUid() 返回非法用户id -1 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(-1)); + ErrCode resultCode = mockMockSms.GetSessionManagerService(sessionManagerService); + EXPECT_EQ(resultCode, ERR_INVALID_VALUE); + + // 1. Mock GetUserIdByCallingUid() 返回系统用户id 0 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(0)); + resultCode = mockMockSms.GetSessionManagerService(sessionManagerService); + EXPECT_EQ(resultCode, ERR_OK); +} + +/** + * @tc.name: GetSessionManagerServiceByUserId + * @tc.desc: test the function of GetSessionManagerServiceByUserId + * @tc.type: FUNC + */ +HWTEST(MockSessionManagerServiceTest, GetSessionManagerServiceByUserId, TestSize.Level1) +{ + int32_t userId = 100; + MockMockSessionManagerService mockMockSms; + sptr sessionManagerService; + // 1. Mock GetUserIdByCallingUid() 返回非法用户id -1 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(-1)); + ErrCode resultCode = mockMockSms.GetSessionManagerServiceByUserId(userId, sessionManagerService); + EXPECT_EQ(resultCode, ERR_INVALID_VALUE); + + // 2. Mock GetUserIdByCallingUid() 返回非系统用户id 200 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(200)); + resultCode = mockMockSms.GetSessionManagerServiceByUserId(userId, sessionManagerService); + EXPECT_EQ(resultCode, ERR_WOULD_BLOCK); + + // 2. Mock GetUserIdByCallingUid() 返回系统用户id 0 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(0)); + resultCode = mockMockSms.GetSessionManagerServiceByUserId(userId, sessionManagerService); + EXPECT_EQ(resultCode, ERR_OK); +} + +/** + * @tc.name: RegisterSMSRecoverListener + * @tc.desc: test the function of RegisterSMSRecoverListener + * @tc.type: FUNC + */ +HWTEST(MockSessionManagerServiceTest, RegisterSMSRecoverListener, TestSize.Level1) +{ + MockMockSessionManagerService mockMockSms; + sptr listener = nullptr; + int32_t userId = 100; + // 1. listener 为 nullptr + ErrCode resultCode = mockMockSms.RegisterSMSRecoverListener(listener, userId); + EXPECT_EQ(resultCode, ERR_INVALID_VALUE); + + // 2.Mock GetUserIdByCallingUid() 返回系统用户id 0 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(0)); + listener = sptr::MakeSptr(); + userId = INVALID_USER_ID; + resultCode = mockMockSms.RegisterSMSRecoverListener(listener, userId); + EXPECT_EQ(resultCode, ERR_OK); + + // 3.Mock GetUserIdByCallingUid() 返回系统用户id 0 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(0)); + listener = sptr::MakeSptr(); + userId = 100; + resultCode = mockMockSms.RegisterSMSRecoverListener(listener, userId); + EXPECT_EQ(resultCode, 4194312); + + // 4.Mock GetUserIdByCallingUid() 返回非系统用户id 100 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(100)); + listener = sptr::MakeSptr(); + userId = INVALID_USER_ID; + resultCode = mockMockSms.RegisterSMSRecoverListener(listener, userId); + EXPECT_EQ(resultCode, ERR_WOULD_BLOCK); +} + +/** + * @tc.name: UnregisterSMSRecoverListener + * @tc.desc: test the function of UnregisterSMSRecoverListener + * @tc.type: FUNC + */ +HWTEST(MockSessionManagerServiceTest, UnregisterSMSRecoverListener, TestSize.Level1) +{ + MockMockSessionManagerService mockMockSms; + // 1.Mock GetUserIdByCallingUid() 返回系统用户id 0 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(0)); + int32_t userId = INVALID_USER_ID; + ErrCode resultCode = mockMockSms.UnregisterSMSRecoverListener(userId); + EXPECT_EQ(resultCode, ERR_OK); + + // 2.Mock GetUserIdByCallingUid() 返回系统用户id 0 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(0)); + userId = 100; + resultCode = mockMockSms.UnregisterSMSRecoverListener(userId); + EXPECT_EQ(resultCode, 4194312); + + // 3.Mock GetUserIdByCallingUid() 返回非系统用户id 100 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(100)); + userId = INVALID_USER_ID; + resultCode = mockMockSms.UnregisterSMSRecoverListener(userId); + EXPECT_EQ(resultCode, ERR_OK); +} + +/** + * @tc.name: RegisterSMSLiteRecoverListener + * @tc.desc: test the function of RegisterSMSLiteRecoverListener + * @tc.type: FUNC + */ +HWTEST(MockSessionManagerServiceTest, RegisterSMSLiteRecoverListener, TestSize.Level1) +{ + MockMockSessionManagerService mockMockSms; + sptr listener = nullptr; + int32_t userId = 100; + // 1. listener 为 nullptr + ErrCode resultCode = mockMockSms.RegisterSMSLiteRecoverListener(listener, userId); + EXPECT_EQ(resultCode, ERR_INVALID_VALUE); + + // 2.Mock GetUserIdByCallingUid() 返回系统用户id 0 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(0)); + listener = sptr::MakeSptr(); + userId = INVALID_USER_ID; + resultCode = mockMockSms.RegisterSMSLiteRecoverListener(listener, userId); + EXPECT_EQ(resultCode, ERR_OK); + + // 3.Mock GetUserIdByCallingUid() 返回系统用户id 0 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(0)); + listener = sptr::MakeSptr(); + userId = 100; + resultCode = mockMockSms.RegisterSMSLiteRecoverListener(listener, userId); + EXPECT_EQ(resultCode, 4194312); + + // 4.Mock GetUserIdByCallingUid() 返回非系统用户id 100 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(100)); + listener = sptr::MakeSptr(); + userId = INVALID_USER_ID; + resultCode = mockMockSms.RegisterSMSLiteRecoverListener(listener, userId); + EXPECT_EQ(resultCode, ERR_WOULD_BLOCK); +} + +/** + * @tc.name: UnregisterSMSLiteRecoverListener + * @tc.desc: test the function of UnregisterSMSLiteRecoverListener + * @tc.type: FUNC + */ +HWTEST(MockSessionManagerServiceTest, UnregisterSMSLiteRecoverListener, TestSize.Level1) +{ + MockMockSessionManagerService mockMockSms; + // 1.Mock GetUserIdByCallingUid() 返回系统用户id 0 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(0)); + int32_t userId = INVALID_USER_ID; + ErrCode resultCode = mockMockSms.UnregisterSMSLiteRecoverListener(userId); + EXPECT_EQ(resultCode, ERR_OK); + + // 2.Mock GetUserIdByCallingUid() 返回系统用户id 0 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(0)); + userId = 100; + resultCode = mockMockSms.UnregisterSMSLiteRecoverListener(userId); + EXPECT_EQ(resultCode, 4194312); + + // 3.Mock GetUserIdByCallingUid() 返回非系统用户id 100 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(100)); + userId = INVALID_USER_ID; + resultCode = mockMockSms.UnregisterSMSLiteRecoverListener(userId); + EXPECT_EQ(resultCode, ERR_OK); +} + +/** + * @tc.name: NotifySceneBoardAvailable + * @tc.desc: test the function of NotifySceneBoardAvailable + * @tc.type: FUNC + */ +HWTEST(MockSessionManagerServiceTest, NotifySceneBoardAvailable, TestSize.Level1) +{ + // 1.Mock IsSystemCalling返回fasle + MockMockSessionManagerService mockMockSms; + MockAccesstokenKit::MockIsSystemApp(false); + MockAccesstokenKit::MockIsSACalling(false); + ErrCode resultCode = mockMockSms.NotifySceneBoardAvailable(); + EXPECT_EQ(resultCode, ERR_PERMISSION_DENIED); + + // 2. Mock IsSystemCalling返回true + MockAccesstokenKit::MockIsSystemApp(true); + MockAccesstokenKit::MockIsSACalling(true); + // Mock GetUserIdByCallingUid() 返回非法用户-1 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(-1)); + resultCode = mockMockSms.NotifySceneBoardAvailable(); + EXPECT_EQ(resultCode, ERR_INVALID_VALUE); + + // 2. Mock IsSystemCalling返回true + MockAccesstokenKit::MockIsSystemApp(true); + MockAccesstokenKit::MockIsSACalling(true); + // Mock GetUserIdByCallingUid() 返回合法用户100 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(100)); + resultCode = mockMockSms.NotifySceneBoardAvailable(); + EXPECT_EQ(resultCode, ERR_OK); +} + +/** + * @tc.name: NotifySceneBoardAvailableToSystemAppClient + * @tc.desc: test the function of NotifySceneBoardAvailableToSystemAppClient + * @tc.type: FUNC + */ +HWTEST(MockSessionManagerServiceTest, NotifySceneBoardAvailableToSystemAppClient, TestSize.Level1) +{ + int32_t userId = 100; + MockMockSessionManagerService mockMockSms; + mockMockSms.NotifySceneBoardAvailableToSystemAppClient(userId); +} + +/** + * @tc.name: NotifySceneBoardAvailableToClient + * @tc.desc: test the function of NotifySceneBoardAvailableToClient + * @tc.type: FUNC + */ +HWTEST(MockSessionManagerServiceTest, NotifySceneBoardAvailableToClient, TestSize.Level1) +{ + int32_t userId = 100; + MockMockSessionManagerService mockMockSms; + mockMockSms.NotifySceneBoardAvailableToClient(userId); +} + +/** + * @tc.name: NotifySceneBoardAvailableToSystemAppLiteClient + * @tc.desc: test the function of NotifySceneBoardAvailableToSystemAppLiteClient + * @tc.type: FUNC + */ +HWTEST(MockSessionManagerServiceTest, NotifySceneBoardAvailableToSystemAppLiteClient, TestSize.Level1) +{ + int32_t userId = 100; + MockMockSessionManagerService mockMockSms; + mockMockSms.NotifySceneBoardAvailableToSystemAppLiteClient(userId); +} + +/** + * @tc.name: NotifySceneBoardAvailableToLiteClient + * @tc.desc: test the function of NotifySceneBoardAvailableToLiteClient + * @tc.type: FUNC + */ +HWTEST(MockSessionManagerServiceTest, NotifySceneBoardAvailableToLiteClient, TestSize.Level1) +{ + int32_t userId = 100; + MockMockSessionManagerService mockMockSms; + mockMockSms.NotifySceneBoardAvailableToLiteClient(userId); +} + +/** + * @tc.name: NotifyWMSConnected + * @tc.desc: test the function of NotifyWMSConnected + * @tc.type: FUNC + */ +HWTEST(MockSessionManagerServiceTest, NotifyWMSConnected, TestSize.Level1) +{ + int32_t userId = 100; + int32_t screenId = 0; + bool isColdStart = false; + MockMockSessionManagerService mockMockSms; + mockMockSms.NotifyWMSConnected(userId, screenId, isColdStart); +} + +/** + * @tc.name: NotifyWMSConnectionChangedToClient + * @tc.desc: test the function of NotifyWMSConnectionChangedToClient + * @tc.type: FUNC + */ +HWTEST(MockSessionManagerServiceTest, NotifyWMSConnectionChangedToClient, TestSize.Level1) +{ + int32_t wmsUserId = 100; + int32_t screenId = 0; + bool isConnnected = true; + MockMockSessionManagerService mockMockSms; + mockMockSms.NotifyWMSConnectionChangedToClient(wmsUserId, screenId, isConnnected); +} + +/** + * @tc.name: NotifyWMSConnectionChangedToLiteClient + * @tc.desc: test the function of NotifyWMSConnectionChangedToLiteClient + * @tc.type: FUNC + */ +HWTEST(MockSessionManagerServiceTest, NotifyWMSConnectionChangedToLiteClient, TestSize.Level1) +{ + int32_t wmsUserId = 100; + int32_t screenId = 0; + bool isConnnected = true; + MockMockSessionManagerService mockMockSms; + mockMockSms.NotifyWMSConnectionChangedToLiteClient(wmsUserId, screenId, isConnnected); +} + +/** + * @tc.name: GetSceneSessionManagerCommon + * @tc.desc: test the function of GetSceneSessionManagerCommon + * @tc.type: FUNC + */ +HWTEST(MockSessionManagerServiceTest, GetSceneSessionManagerCommon, TestSize.Level1) +{ + sptr result; + bool isLite = false; + // Mock GetUserIdByCallingUid() 返回非法用户id -1 + MockMockSessionManagerService mockMockSms; + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(-1)); + ErrCode resultCode = mockMockSms.GetSceneSessionManagerCommon(100, result, isLite); + EXPECT_EQ(resultCode, ERR_INVALID_VALUE); + + // Mock GetUserIdByCallingUid() 返回非系统用户id 100 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(100)); + resultCode = mockMockSms.GetSceneSessionManagerCommon(200, result, isLite); + EXPECT_EQ(resultCode, ERR_WOULD_BLOCK); + + // Mock GetUserIdByCallingUid() 返回系统用户id 0 + EXPECT_CALL(mockMockSms, GetUserIdByCallingUid()) + .Times(1) + .WillOnce(Return(0)); + // Mock GetSessionManagerServiceByUserId() 返回nullptr 或者 传入不存在userId 99999 + resultCode = mockMockSms.GetSceneSessionManagerCommon(99999, result, isLite); + EXPECT_EQ(resultCode, ERR_INVALID_VALUE); +} } // namespace } // namespace Rosen } // namespace OHOS \ No newline at end of file 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..990798c3c6a4adc41e1deefad8d018e8ee603efd 100644 --- a/window_scene/test/unittest/session_manager_lite_test.cpp +++ b/window_scene/test/unittest/session_manager_lite_test.cpp @@ -23,6 +23,7 @@ #include "session_manager_service_recover_interface.h" #include "singleton_delegator.h" #include "window_manager_hilog.h" +#include "iremote_object_mocker.h" using namespace testing; using namespace testing::ext; @@ -46,13 +47,10 @@ void SessionManagerLiteTest::TearDownTestCase() {} void SessionManagerLiteTest::SetUp() { - sml_ = std::make_shared(); - ASSERT_NE(nullptr, sml_); } void SessionManagerLiteTest::TearDown() { - sml_ = nullptr; } namespace { @@ -63,13 +61,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 +79,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 +98,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 +119,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 +135,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 +154,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 +170,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 +186,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, sml); + + 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); + sptr 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..eb23cac34dc6a78540c3c05bc9e134e38b393a7a 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; + int32_t 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..f88f26b519901647fc68aedf819d6de0ce875a9a 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..bd2b3125cd792bce1ba2d64679b738f7d9ef323e 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,59 @@ 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 = 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 +504,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() @@ -540,40 +594,44 @@ void WindowAdapter::OnUserSwitch() bool WindowAdapter::InitSSMProxy() { std::lock_guard lock(mutex_); - if (!isProxyValid_) { - windowManagerServiceProxy_ = SessionManager::GetInstance().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; - } - 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(); }); - 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(); }); - isRegisteredUserSwitchListener_ = true; - } - isProxyValid_ = true; + if (isProxyValid_) { + return true; + } + windowManagerServiceProxy_ = SessionManager::GetInstance(userId_)->GetSceneSessionManagerProxy(); + if (!windowManagerServiceProxy_ || !windowManagerServiceProxy_->AsObject()) { + WLOGFE("Failed to get system scene session manager services"); + 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(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(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 +640,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..e147ce83cd4d0849871f0c6d65b1b2adb301b051 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,34 @@ WM_IMPLEMENT_SINGLE_INSTANCE(WindowAdapterLite) } \ } while(false) +WindowAdapterLite::WindowAdapterLite(const int32_t userId) : userId_(userId) {} + +WindowAdapterLite& WindowAdapterLite::GetInstance() +{ + static sptr 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) { @@ -133,30 +162,38 @@ WMError WindowAdapterLite::GetVisibilityWindowInfo(std::vector lock(mutex_); - if (!isProxyValid_) { - windowManagerServiceProxy_ = SessionManagerLite::GetInstance().GetSceneSessionManagerLiteProxy(); - if (!windowManagerServiceProxy_ || !windowManagerServiceProxy_->AsObject()) { - WLOGFE("Failed to get scene session manager lite proxy"); - return false; - } - wmsDeath_ = new (std::nothrow) WMSDeathRecipient(); - if (!wmsDeath_) { - WLOGFE("Failed to create death recipient WMSDeathRecipient"); - return false; - } - sptr remoteObject = windowManagerServiceProxy_->AsObject(); - if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(wmsDeath_)) { - WLOGFE("Failed to add death recipient"); - return false; - } - // U0 system user needs to subscribe OnUserSwitch event - int32_t clientUserId = GetUserIdByUid(getuid()); - if (clientUserId == SYSTEM_USERID && !isRegisteredUserSwitchListener_) { - SessionManagerLite::GetInstance().RegisterUserSwitchListener([this] { this->OnUserSwitch(); }); - isRegisteredUserSwitchListener_ = true; - } - isProxyValid_ = true; + if (isProxyValid_) { + return true; + } + 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(userId_); + if (!wmsDeath_) { + WLOGFE("Failed to create death recipient WMSDeathRecipient"); + return false; + } + sptr remoteObject = windowManagerServiceProxy_->AsObject(); + if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(wmsDeath_)) { + WLOGFE("Failed to add death recipient"); + return false; + } + // U0 system user needs to subscribe OnUserSwitch event + int32_t clientUserId = GetUserIdByUid(getuid()); + if (clientUserId == SYSTEM_USERID && !isRegisteredUserSwitchListener_) { + 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; return true; } @@ -179,6 +216,8 @@ void WindowAdapterLite::ClearWindowAdapter() windowManagerServiceProxy_ = nullptr; } +WMSDeathRecipient::WMSDeathRecipient(int32_t userId) : userId_(userId) {} + void WMSDeathRecipient::OnRemoteDied(const wptr& wptrDeath) { if (wptrDeath == nullptr) { @@ -190,9 +229,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 +322,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..ef17a8f3d71edc57642af31ca2c694279c51c655 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,7 +531,8 @@ 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()) { } @@ -547,10 +548,50 @@ 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 = 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 +614,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 +636,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 +656,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 +692,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 +713,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 +745,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 +766,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 +800,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 +812,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 +842,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 +873,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 +893,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 +925,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 +955,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 +996,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 +1025,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 +1066,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 +1096,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 +1138,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 +1167,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 +1208,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 +1228,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 +1260,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 +1282,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 +1319,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 +1341,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 +1378,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 +1403,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 +1446,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 +1544,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 +1591,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 +1616,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 +1650,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 +1684,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 +1694,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 +1708,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 +1717,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 +1726,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 +1745,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 +1754,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 +1763,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 +1773,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 +1782,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 +1811,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 +1835,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 +1867,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 +1889,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 +1924,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 +1957,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 +1975,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 +1995,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 +2014,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 +2050,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 +2082,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 +2095,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 +2103,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 +2112,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 +2122,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 +2132,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 +2142,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 +2151,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 +2160,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 +2169,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 +2178,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 +2187,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 +2196,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 +2206,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 +2217,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 +2235,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 +2245,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 +2254,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 +2406,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 +2467,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 +2475,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..f40c6af03a8592a2e1a02a25321719d0d0f59e8d 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,55 @@ 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; } +WindowManagerLite& WindowManagerLite::GetInstance() +{ + static sptr instance = new WindowManagerLite(); + return *instance; +} + +sptr WindowManagerLite::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 = &WindowManagerLite::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 WindowManagerLite(userId); + windowManagerLiteMap_.insert({ userId, instance }); + return instance; +} + +WMError WindowManagerLite::RemoveInstanceByUserId(const int userId) +{ + TLOGI(WmsLogTag::WMS_MULTI_USER, "remove instance userId: %{public}d", userId); + std::lock_guard lock(windowManagerLiteMapMutex_); + windowManagerLiteMap_.erase(userId); + return WMError::WM_OK; +} + WMError WindowManagerLite::RegisterFocusChangedListener(const sptr& listener) { if (listener == nullptr) { @@ -371,7 +414,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 +447,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 +466,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 +499,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 +520,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 +552,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 +565,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 +591,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 +633,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 +666,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 +689,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 +707,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 +739,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 +759,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 +794,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 +806,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 +816,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 +881,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 +890,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 +900,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 +935,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 +967,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 +999,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 +1034,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 +1047,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 +1059,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 +1068,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 +1083,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 +1119,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 +1136,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 +1151,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 +1168,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 +1199,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 +1281,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 +1299,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 diff --git a/wmserver/BUILD.gn b/wmserver/BUILD.gn index 0b55b6d033e430d08d76da83561734b0b7029e84..f47cf75b80b5d04fdea431f57b3663c369349a79 100644 --- a/wmserver/BUILD.gn +++ b/wmserver/BUILD.gn @@ -151,6 +151,7 @@ ohos_shared_library("sms") { deps = [ ":mock_session_manager_service_stub", + "${window_base_path}/dm:libdm", "${window_base_path}/utils:libwmutil", "${window_base_path}/utils:libwmutil_base", "${window_base_path}/window_scene/common:window_scene_common", @@ -170,6 +171,7 @@ ohos_shared_library("sms") { "hilog:libhilog", "image_framework:image_native", "ipc:ipc_single", + "os_account:os_account_innerkits", "safwk:system_ability_fwk", "samgr:samgr_proxy", ] diff --git a/wmserver/IMockSessionManagerInterface.idl b/wmserver/IMockSessionManagerInterface.idl index 7a0227abf95d3c2bc227bcd20a269c5f9132b1d6..8d23725731ea8f77b9da87b743f9b16ae5f34921 100644 --- a/wmserver/IMockSessionManagerInterface.idl +++ b/wmserver/IMockSessionManagerInterface.idl @@ -18,12 +18,15 @@ interface_token OHOS.IMockSessionManager; interface OHOS.Rosen.IMockSessionManagerInterface { [ipccode 0] void GetSessionManagerService([out] IRemoteObject sessionManageService); + void GetSessionManagerServiceByUserId([in] int userId, [out] IRemoteObject sessionManageService); void GetScreenSessionManagerLite([out] IRemoteObject screenSessionManagerLite); [oneway] void NotifySceneBoardAvailable(); - void RegisterSMSRecoverListener([in] IRemoteObject listener); - void UnregisterSMSRecoverListener(); - void RegisterSMSLiteRecoverListener([in] IRemoteObject listener); - void UnregisterSMSLiteRecoverListener(); + void RegisterSMSRecoverListener([in] IRemoteObject listener, [in] int userId); + void UnregisterSMSRecoverListener([in] int userId); + void RegisterSMSLiteRecoverListener([in] IRemoteObject listener, [in] int userId); + void UnregisterSMSLiteRecoverListener([in] int userId); void SetSnapshotSkipByUserIdAndBundleNames([in] int userId, [in] String[] bundleNameList); void SetSnapshotSkipByIdNamesMap([in] Map userIdAndBundleNames); + void GetSceneSessionManagerLiteByUserId([in] int userId, [out] IRemoteObject sceneSessionManagerLite); + void GetSceneSessionManagerByUserId([in] int userId, [out] IRemoteObject sceneSessionManager); } \ No newline at end of file diff --git a/wmserver/include/mock_session_manager_service.h b/wmserver/include/mock_session_manager_service.h index 0f94b31f39e6e06dce3f7a521456f6dc108e5c60..2095af0ee20057fafaa26d3e69534321a1c93a4b 100644 --- a/wmserver/include/mock_session_manager_service.h +++ b/wmserver/include/mock_session_manager_service.h @@ -19,6 +19,7 @@ #include #include +#include "dm_common.h" #include "mock_session_manager_interface_stub.h" #include "wm_single_instance.h" #include "zidl/session_manager_service_recover_interface.h" @@ -45,6 +46,7 @@ public: */ bool SetSessionManagerService(const sptr& sessionManagerService); ErrCode GetSessionManagerService(sptr& sessionManagerService) override; + ErrCode GetSessionManagerServiceByUserId(int32_t userId, sptr& sessionManagerService) override; void NotifyWMSConnected(int32_t userId, int32_t screenId, bool isColdStart); void NotifyNotKillService() {} @@ -52,12 +54,12 @@ public: * Window Recover */ ErrCode NotifySceneBoardAvailable() override; - ErrCode RegisterSMSRecoverListener(const sptr& listener) override; - ErrCode UnregisterSMSRecoverListener() override; - void UnregisterSMSRecoverListener(int32_t userId, int32_t pid); - ErrCode RegisterSMSLiteRecoverListener(const sptr& listener) override; - ErrCode UnregisterSMSLiteRecoverListener() override; - void UnregisterSMSLiteRecoverListener(int32_t userId, int32_t pid); + ErrCode RegisterSMSRecoverListener(const sptr& listener, int32_t userId) override; + ErrCode UnregisterSMSRecoverListener(int32_t userId) override; + void UnregisterSMSRecoverListener(int32_t clientUserId, int32_t displayId, int32_t pid); + ErrCode RegisterSMSLiteRecoverListener(const sptr& listener, int32_t userId) override; + ErrCode UnregisterSMSLiteRecoverListener(int32_t userId) override; + void UnregisterSMSLiteRecoverListener(int32_t clientUserId, int32_t displayId, int32_t pid); /* * Window Snapshot @@ -67,6 +69,11 @@ public: ErrCode SetSnapshotSkipByIdNamesMap(const std::unordered_map>& userIdAndBunldeNames) override; + ErrCode GetSceneSessionManagerLiteByUserId(int32_t userId, + sptr& sceneSessionManagerLite) override; + ErrCode GetSceneSessionManagerByUserId(int32_t userId, + sptr& sceneSessionManager) override; + protected: MockSessionManagerService(); virtual ~MockSessionManagerService() = default; @@ -92,10 +99,20 @@ private: sptr GetSessionManagerServiceByUserId(int32_t userId); void RemoveSessionManagerServiceByUserId(int32_t userId); bool RegisterMockSessionManagerService(); + ErrCode ValidateParameters(int32_t clientUserId, int32_t userId) const; + ErrCode GetForegroundOsAccountDisplayId(int32_t userId, DisplayId& displayId) const; + ErrCode NotifyWMSConnectionStatus(int32_t userId, const sptr& smsListener); + virtual int32_t GetUserIdByCallingUid(); std::map>* GetSMSRecoverListenerMap(int32_t userId); + std::map>* GetSystemAppSMSRecoverListenerMap( + int32_t displayId); std::map>* GetSMSLiteRecoverListenerMap(int32_t userId); + std::map>* GetSystemAppSMSLiteRecoverListenerMap( + int32_t displayId); void NotifySceneBoardAvailableToClient(int32_t userId); + void NotifySceneBoardAvailableToSystemAppClient(int32_t userId); void NotifySceneBoardAvailableToLiteClient(int32_t userId); + void NotifySceneBoardAvailableToSystemAppLiteClient(int32_t userId); void NotifyWMSConnectionChanged(int32_t wmsUserId, int32_t screenId, bool isConnected); void NotifyWMSConnectionChangedToClient(int32_t wmsUserId, int32_t screenId, bool isConnected); void NotifyWMSConnectionChangedToLiteClient(int32_t wmsUserId, int32_t screenId, bool isConnected); @@ -105,6 +122,8 @@ private: void ShowAceDumpHelp(std::string& dumpInfo); void ShowIllegalArgsInfo(std::string& dumpInfo); + ErrCode GetSceneSessionManagerCommon(int32_t userId, sptr& result, bool isLite); + /* * Window Snapshot */ @@ -118,13 +137,15 @@ private: std::vector>& userIdAndBunldeNames); sptr screenSessionManager_; - sptr sceneSessionManager_; + sptr defaultSceneSessionManager_; /* * Multi User */ - int32_t currentWMSUserId_; - int32_t currentScreenId_; + int32_t defaultWMSUserId_; + int32_t defaultScreenId_; + std::mutex userId2ScreenIdMapMutex_; + std::map userId2ScreenIdMap_; std::shared_mutex smsDeathRecipientMapLock_; std::map> smsDeathRecipientMap_; std::shared_mutex sessionManagerServiceMapLock_; @@ -140,6 +161,13 @@ private: std::shared_mutex smsLiteRecoverListenerLock_; std::map>> smsLiteRecoverListenerMap_; + std::shared_mutex systemAppSmsRecoverListenerLock_; + std::map>> + systemAppSmsRecoverListenerMap_; + std::shared_mutex systemAppSmsLiteRecoverListenerLock_; + std::map>> + systemAppSmsLiteRecoverListenerMap_; + /* * Window Snapshot */ diff --git a/wmserver/src/mock_session_manager_service.cpp b/wmserver/src/mock_session_manager_service.cpp index d81b89a67adc822aed5662cc9fcffa0191821c0f..fdb55616eb47d1ce3d9a9efc0155951659ed2731 100644 --- a/wmserver/src/mock_session_manager_service.cpp +++ b/wmserver/src/mock_session_manager_service.cpp @@ -30,6 +30,7 @@ #include #include +#include "os_account_manager.h" #include "window_manager_hilog.h" #include "unique_fd.h" #include "parameters.h" @@ -42,6 +43,7 @@ #include "session_manager_service_interface.h" #include "screen_session_manager_lite.h" #include "common/include/session_permission.h" +#include "display_manager.h" #define PATH_LEN 1024 #define O_RDWR 02 @@ -59,38 +61,28 @@ const std::string KEY_SCENE_BOARD_TEST_ENABLE = "persist.scb.testmode.enable"; const std::string SCENE_BOARD_BUNDLE_NAME = "com.ohos.sceneboard"; const std::string TEST_MODULE_NAME_SUFFIX = "_test"; const std::string BOOTEVENT_WMS_READY = "bootevent.wms.ready"; - -inline int32_t GetUserIdByCallingUid() -{ - int32_t uid = IPCSkeleton::GetCallingUid(); - TLOGD(WmsLogTag::WMS_MULTI_USER, "get calling uid(%{public}d)", uid); - if (uid <= INVALID_UID) { - TLOGE(WmsLogTag::WMS_MULTI_USER, "uid is illegal: %{public}d", uid); - return INVALID_USER_ID; - } - return GetUserIdByUid(uid); -} } // namespace class ClientListenerDeathRecipient : public IRemoteObject::DeathRecipient { public: - explicit ClientListenerDeathRecipient(int32_t userId, int32_t pid, bool isLite) - : userId_(userId), pid_(pid), isLite_(isLite) + explicit ClientListenerDeathRecipient(int32_t userId, int32_t displayId, int32_t pid, bool isLite) + : userId_(userId), displayId_(displayId), pid_(pid), isLite_(isLite) {} void OnRemoteDied(const wptr& wptrDeath) override { - TLOGD(WmsLogTag::WMS_RECOVER, "Client died, pid = %{public}d, isLite = %{public}d", pid_, isLite_); + TLOGI(WmsLogTag::WMS_RECOVER, "Client died, pid = %{public}d, isLite = %{public}d", pid_, isLite_); if (isLite_) { - MockSessionManagerService::GetInstance().UnregisterSMSLiteRecoverListener(userId_, pid_); + MockSessionManagerService::GetInstance().UnregisterSMSLiteRecoverListener(userId_, displayId_, pid_); } else { - MockSessionManagerService::GetInstance().UnregisterSMSRecoverListener(userId_, pid_); + MockSessionManagerService::GetInstance().UnregisterSMSRecoverListener(userId_, displayId_, pid_); } } private: int32_t userId_; + int32_t displayId_; int32_t pid_; bool isLite_; }; @@ -116,7 +108,7 @@ void MockSessionManagerService::SMSDeathRecipient::OnRemoteDied(const wptr& a bool MockSessionManagerService::SetSessionManagerService(const sptr& sessionManagerService) { + TLOGI(WmsLogTag::WMS_MULTI_USER, "sessionManagerService set start"); if (!sessionManagerService) { WLOGFE("sessionManagerService is nullptr"); return false; } - currentWMSUserId_ = GetUserIdByCallingUid(); - if (currentWMSUserId_ <= INVALID_USER_ID) { - TLOGE(WmsLogTag::WMS_MULTI_USER, "userId is illegal: %{public}d", currentWMSUserId_); + int32_t clientUserId = GetUserIdByCallingUid(); + if (clientUserId <= INVALID_USER_ID) { + TLOGE(WmsLogTag::WMS_MULTI_USER, "userId is illegal: %{public}d", clientUserId); return false; } { std::unique_lock lock(sessionManagerServiceMapLock_); - sessionManagerServiceMap_[currentWMSUserId_] = sessionManagerService; + sessionManagerServiceMap_[clientUserId] = sessionManagerService; } - RecoverSCBSnapshotSkipByUserId(currentWMSUserId_); - auto smsDeathRecipient = GetSMSDeathRecipientByUserId(currentWMSUserId_); + RecoverSCBSnapshotSkipByUserId(clientUserId); + auto smsDeathRecipient = GetSMSDeathRecipientByUserId(clientUserId); if (!smsDeathRecipient) { - smsDeathRecipient = new SMSDeathRecipient(currentWMSUserId_); + smsDeathRecipient = new SMSDeathRecipient(clientUserId); std::unique_lock lock(smsDeathRecipientMapLock_); if (!smsDeathRecipient) { TLOGE(WmsLogTag::WMS_MULTI_USER, "Failed to create death Recipient ptr smsDeathRecipient"); return false; } - smsDeathRecipientMap_[currentWMSUserId_] = smsDeathRecipient; + smsDeathRecipientMap_[clientUserId] = smsDeathRecipient; } if (sessionManagerService->IsProxyObject() && !sessionManagerService->AddDeathRecipient(smsDeathRecipient)) { TLOGE(WmsLogTag::WMS_MULTI_USER, "Failed to add death recipient"); @@ -277,14 +273,33 @@ ErrCode MockSessionManagerService::GetSessionManagerService(sptr& return ERR_INVALID_VALUE; } if (clientUserId == SYSTEM_USERID) { - TLOGD(WmsLogTag::WMS_MULTI_USER, "System user, return current sessionManagerService with %{public}d", - currentWMSUserId_); - clientUserId = currentWMSUserId_; + TLOGI(WmsLogTag::WMS_MULTI_USER, "System user, return default sessionManagerService with %{public}d", + defaultWMSUserId_); + clientUserId = defaultWMSUserId_; } sessionManagerService = GetSessionManagerServiceByUserId(clientUserId); return ERR_OK; } +ErrCode MockSessionManagerService::GetSessionManagerServiceByUserId(int32_t userId, + sptr& sessionManagerService) +{ + int32_t clientUserId = GetUserIdByCallingUid(); + TLOGI(WmsLogTag::WMS_MULTI_USER, "GetSessionManagerServiceByUserId, userId: %{public}d, clientUserId: %{public}d", + userId, clientUserId); + if (clientUserId <= INVALID_USER_ID) { + TLOGE(WmsLogTag::WMS_MULTI_USER, "userId is illegal: %{public}d", clientUserId); + return ERR_INVALID_VALUE; + } + if (clientUserId != SYSTEM_USERID) { + TLOGE(WmsLogTag::WMS_MULTI_USER, "clientUserId is not system user id : %{public}d", clientUserId); + return ERR_WOULD_BLOCK; + } + sessionManagerService = GetSessionManagerServiceByUserId(userId); + return ERR_OK; +} + + sptr MockSessionManagerService::GetSessionManagerServiceByUserId(int32_t userId) { std::shared_lock lock(sessionManagerServiceMapLock_); @@ -308,69 +323,58 @@ void MockSessionManagerService::RemoveSessionManagerServiceByUserId(int32_t user } } -ErrCode MockSessionManagerService::NotifySceneBoardAvailable() -{ - if (!SessionPermission::IsSystemCalling()) { - TLOGE(WmsLogTag::WMS_RECOVER, "permission denied"); - return ERR_PERMISSION_DENIED; - } - int32_t userId = GetUserIdByCallingUid(); - if (userId <= INVALID_USER_ID) { - TLOGE(WmsLogTag::WMS_RECOVER, "userId is illegal: %{public}d", userId); - return ERR_INVALID_VALUE; - } - TLOGI(WmsLogTag::WMS_RECOVER, "scene board is available with userId=%{public}d", userId); - - NotifySceneBoardAvailableToLiteClient(SYSTEM_USERID); - NotifySceneBoardAvailableToLiteClient(userId); - - NotifySceneBoardAvailableToClient(SYSTEM_USERID); - NotifySceneBoardAvailableToClient(userId); - return ERR_OK; -} - -ErrCode MockSessionManagerService::RegisterSMSRecoverListener(const sptr& listener) +ErrCode MockSessionManagerService::RegisterSMSRecoverListener(const sptr& listener, int32_t userId) { + TLOGI(WmsLogTag::WMS_RECOVER, "userId = %{public}d,", userId); if (listener == nullptr) { TLOGE(WmsLogTag::WMS_RECOVER, "listener is nullptr"); return ERR_INVALID_VALUE; } int32_t clientUserId = GetUserIdByCallingUid(); - if (clientUserId <= INVALID_USER_ID) { - TLOGE(WmsLogTag::WMS_RECOVER, "userId is illegal: %{public}d", clientUserId); - return ERR_INVALID_VALUE; + DisplayId displayId = DISPLAY_ID_INVALID; + // SYSTEM_USERID calling, userId = INVALID_USER_ID, set userId to defaultWMSUserId_ + if (clientUserId == SYSTEM_USERID && userId == INVALID_USER_ID) { + TLOGI(WmsLogTag::WMS_MULTI_USER, "system user, userId is INVALID_USER_ID, use defaultWMSUserId_: %{public}d", + defaultWMSUserId_); + userId = defaultWMSUserId_; + } + + ErrCode ret = ValidateParameters(clientUserId, userId); + if (ret != ERR_OK) { + return ret; } + + if (clientUserId == SYSTEM_USERID && userId != INVALID_USER_ID) { + ret = GetForegroundOsAccountDisplayId(userId, displayId); + if (ret != ERR_OK) { + return ret; + } + } else { + displayId = DEFAULT_SCREEN_ID; + } + int32_t pid = IPCSkeleton::GetCallingRealPid(); - TLOGI(WmsLogTag::WMS_RECOVER, "clientUserId = %{public}d, pid = %{public}d", clientUserId, pid); - sptr clientDeathListener = new ClientListenerDeathRecipient(clientUserId, pid, false); + TLOGI(WmsLogTag::WMS_RECOVER, "clientUserId = %{public}d, pid = %{public}d, displayId = %{public}" PRIu64, + clientUserId, pid, displayId); + sptr clientDeathListener = + new ClientListenerDeathRecipient(clientUserId, displayId, pid, false); listener->AddDeathRecipient(clientDeathListener); sptr smsListener; { - std::unique_lock lock(smsRecoverListenerLock_); smsListener = iface_cast(listener); - smsRecoverListenerMap_[clientUserId][pid] = smsListener; + if (clientUserId == SYSTEM_USERID) { + std::unique_lock lock(systemAppSmsRecoverListenerLock_); + systemAppSmsRecoverListenerMap_[displayId][pid] = smsListener; + } else { + std::unique_lock lock(smsRecoverListenerLock_); + smsRecoverListenerMap_[clientUserId][pid] = smsListener; + } } if (clientUserId != SYSTEM_USERID) { return ERR_WOULD_BLOCK; } - bool isWMSConnected = false; - { - std::lock_guard lock(wmsConnectionStatusLock_); - if (wmsConnectionStatusMap_.find(currentWMSUserId_) != wmsConnectionStatusMap_.end()) { - isWMSConnected = wmsConnectionStatusMap_[currentWMSUserId_]; - } - } - if (smsListener && isWMSConnected) { - auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_); - if (sessionManagerService == nullptr) { - TLOGE(WmsLogTag::WMS_RECOVER, "SessionManagerService is null"); - return ERR_DEAD_OBJECT; - } - TLOGI(WmsLogTag::WMS_RECOVER, "WMS ready,notify client"); - smsListener->OnWMSConnectionChanged(currentWMSUserId_, currentScreenId_, true, sessionManagerService); - } - return ERR_OK; + return NotifyWMSConnectionStatus(userId, smsListener); } std::map>* MockSessionManagerService::GetSMSRecoverListenerMap( @@ -383,100 +387,130 @@ std::map>* MockSessionManag return nullptr; } -ErrCode MockSessionManagerService::UnregisterSMSRecoverListener() +std::map>* MockSessionManagerService::GetSystemAppSMSRecoverListenerMap(int32_t displayId) { - int32_t clientUserId = GetUserIdByCallingUid(); - if (clientUserId <= INVALID_USER_ID) { - TLOGE(WmsLogTag::WMS_RECOVER, "userId is illegal: %{public}d", clientUserId); - return ERR_INVALID_VALUE; + auto iter = systemAppSmsRecoverListenerMap_.find(displayId); + if (iter != systemAppSmsRecoverListenerMap_.end()) { + return &iter->second; } - int32_t pid = IPCSkeleton::GetCallingRealPid(); - TLOGD(WmsLogTag::WMS_RECOVER, "clientUserId = %{public}d, pid = %{public}d", clientUserId, pid); - UnregisterSMSRecoverListener(clientUserId, pid); - return ERR_OK; + return nullptr; } -void MockSessionManagerService::UnregisterSMSRecoverListener(int32_t userId, int32_t pid) +ErrCode MockSessionManagerService::UnregisterSMSRecoverListener(int32_t userId) { - TLOGD(WmsLogTag::WMS_RECOVER, "userId = %{public}d, pid = %{public}d", userId, pid); - std::unique_lock lock(smsRecoverListenerLock_); - std::map>* smsRecoverListenerMap = - GetSMSRecoverListenerMap(userId); - if (!smsRecoverListenerMap) { - TLOGW(WmsLogTag::WMS_RECOVER, "smsRecoverListenerMap is null"); - return; + TLOGI(WmsLogTag::WMS_RECOVER, "userId = %{public}d,", userId); + + int32_t clientUserId = GetUserIdByCallingUid(); + DisplayId displayId = DISPLAY_ID_INVALID; + // SYSTEM_USERID calling, userId = INVALID_USER_ID, set userId to defaultWMSUserId_ + if (clientUserId == SYSTEM_USERID && userId == INVALID_USER_ID) { + TLOGI(WmsLogTag::WMS_MULTI_USER, "system user, userId is INVALID_USER_ID, use defaultWMSUserId_: %{public}d", + defaultWMSUserId_); + userId = defaultWMSUserId_; + } + + ErrCode ret = ValidateParameters(clientUserId, userId); + if (ret != ERR_OK) { + return ret; } - auto iter = smsRecoverListenerMap->find(pid); - if (iter != smsRecoverListenerMap->end()) { - smsRecoverListenerMap->erase(iter); + + if (clientUserId == SYSTEM_USERID && userId != INVALID_USER_ID) { + ret = GetForegroundOsAccountDisplayId(userId, displayId); + if (ret != ERR_OK) { + return ret; + } + } else { + displayId = DEFAULT_SCREEN_ID; } + + int32_t pid = IPCSkeleton::GetCallingRealPid(); + UnregisterSMSRecoverListener(clientUserId, displayId, pid); + return ERR_OK; } -void MockSessionManagerService::NotifySceneBoardAvailableToClient(int32_t userId) +void MockSessionManagerService::UnregisterSMSRecoverListener(int32_t clientUserId, int32_t displayId, int32_t pid) { - std::shared_lock lock(smsRecoverListenerLock_); - std::map>* smsRecoverListenerMap = - GetSMSRecoverListenerMap(userId); - if (!smsRecoverListenerMap) { - TLOGW(WmsLogTag::WMS_RECOVER, "smsRecoverListenerMap is null"); - return; - } - TLOGI(WmsLogTag::WMS_RECOVER, "userId=%{public}d, Remote process count = %{public}" PRIu64, userId, - static_cast(smsRecoverListenerMap->size())); - auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_); - if (sessionManagerService == nullptr) { - TLOGE(WmsLogTag::WMS_RECOVER, "SessionManagerService is null"); - return; - } - for (auto& iter : *smsRecoverListenerMap) { - if (iter.second != nullptr) { - TLOGI(WmsLogTag::WMS_RECOVER, "Call OnSessionManagerServiceRecover pid = %{public}d", iter.first); - iter.second->OnSessionManagerServiceRecover(sessionManagerService); + TLOGI(WmsLogTag::WMS_RECOVER, + "clientUserId = %{public}d, pid = %{public}d, displayId = %{public}d", clientUserId, pid, displayId); + if (clientUserId == SYSTEM_USERID) { + std::unique_lock lock(systemAppSmsRecoverListenerLock_); + std::map>* systemAppSmsRecoverListenerMap = + GetSystemAppSMSRecoverListenerMap(displayId); + if (!systemAppSmsRecoverListenerMap) { + TLOGE(WmsLogTag::WMS_RECOVER, "systemAppSmsRecoverListenerMap is null"); + return; + } + auto iter = systemAppSmsRecoverListenerMap->find(pid); + if (iter != systemAppSmsRecoverListenerMap->end()) { + systemAppSmsRecoverListenerMap->erase(iter); + } + } else { + std::unique_lock lock(smsRecoverListenerLock_); + std::map>* smsRecoverListenerMap = + GetSMSRecoverListenerMap(clientUserId); + if (!smsRecoverListenerMap) { + TLOGE(WmsLogTag::WMS_RECOVER, "smsRecoverListenerMap is null"); + return; + } + auto iter = smsRecoverListenerMap->find(pid); + if (iter != smsRecoverListenerMap->end()) { + smsRecoverListenerMap->erase(iter); } } } -ErrCode MockSessionManagerService::RegisterSMSLiteRecoverListener(const sptr& listener) +ErrCode MockSessionManagerService::RegisterSMSLiteRecoverListener(const sptr& listener, int32_t userId) { + TLOGI(WmsLogTag::WMS_RECOVER, "userId = %{public}d,", userId); if (listener == nullptr) { TLOGE(WmsLogTag::WMS_RECOVER, "Lite listener is nullptr"); return ERR_INVALID_VALUE; } + int32_t clientUserId = GetUserIdByCallingUid(); - if (clientUserId <= INVALID_USER_ID) { - TLOGE(WmsLogTag::WMS_RECOVER, "userId is illegal: %{public}d", clientUserId); - return ERR_INVALID_VALUE; + DisplayId displayId = DISPLAY_ID_INVALID; + // SYSTEM_USERID calling, userId = INVALID_USER_ID, set userId to defaultWMSUserId_ + if (clientUserId == SYSTEM_USERID && userId == INVALID_USER_ID) { + TLOGI(WmsLogTag::WMS_MULTI_USER, "system user, userId is INVALID_USER_ID, use defaultWMSUserId_: %{public}d", + defaultWMSUserId_); + userId = defaultWMSUserId_; + } + + ErrCode ret = ValidateParameters(clientUserId, userId); + if (ret != ERR_OK) { + return ret; + } + + if (clientUserId == SYSTEM_USERID && userId != INVALID_USER_ID) { + ret = GetForegroundOsAccountDisplayId(userId, displayId); + if (ret != ERR_OK) { + return ret; + } + } else { + displayId = DEFAULT_SCREEN_ID; } + int32_t pid = IPCSkeleton::GetCallingRealPid(); - TLOGI(WmsLogTag::WMS_RECOVER, "clientUserId = %{public}d, pid = %{public}d", clientUserId, pid); - sptr clientDeathListener = new ClientListenerDeathRecipient(clientUserId, pid, true); + TLOGI(WmsLogTag::WMS_RECOVER, "clientUserId = %{public}d, pid = %{public}d, displayId = %{public}" PRIu64, + clientUserId, pid, displayId); + sptr clientDeathListener = + new ClientListenerDeathRecipient(clientUserId, displayId, pid, true); listener->AddDeathRecipient(clientDeathListener); sptr smsListener; { - std::unique_lock lock(smsLiteRecoverListenerLock_); smsListener = iface_cast(listener); - smsLiteRecoverListenerMap_[clientUserId][pid] = smsListener; + if (clientUserId == SYSTEM_USERID) { + std::unique_lock lock(systemAppSmsLiteRecoverListenerLock_); + systemAppSmsLiteRecoverListenerMap_[displayId][pid] = smsListener; + } else { + std::unique_lock lock(smsLiteRecoverListenerLock_); + smsLiteRecoverListenerMap_[clientUserId][pid] = smsListener; + } } if (clientUserId != SYSTEM_USERID) { return ERR_WOULD_BLOCK; } - bool isWMSConnected = false; - { - std::lock_guard lock(wmsConnectionStatusLock_); - if (wmsConnectionStatusMap_.find(currentWMSUserId_) != wmsConnectionStatusMap_.end()) { - isWMSConnected = wmsConnectionStatusMap_[currentWMSUserId_]; - } - } - if (smsListener && isWMSConnected) { - auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_); - if (sessionManagerService == nullptr) { - TLOGE(WmsLogTag::WMS_RECOVER, "SessionManagerService is null"); - return ERR_DEAD_OBJECT; - } - TLOGD(WmsLogTag::WMS_MULTI_USER, "Lite wms is already connected, notify client"); - smsListener->OnWMSConnectionChanged(currentWMSUserId_, currentScreenId_, true, sessionManagerService); - } - return ERR_OK; + return NotifyWMSConnectionStatus(userId, smsListener); } std::map>* MockSessionManagerService::GetSMSLiteRecoverListenerMap( @@ -489,31 +523,180 @@ std::map>* MockSessionManag return nullptr; } -ErrCode MockSessionManagerService::UnregisterSMSLiteRecoverListener() +std::map>* MockSessionManagerService::GetSystemAppSMSLiteRecoverListenerMap(int32_t displayId) { + auto iter = systemAppSmsLiteRecoverListenerMap_.find(displayId); + if (iter != systemAppSmsLiteRecoverListenerMap_.end()) { + return &iter->second; + } + return nullptr; +} + +ErrCode MockSessionManagerService::UnregisterSMSLiteRecoverListener(int32_t userId) +{ + TLOGI(WmsLogTag::WMS_RECOVER, "userId = %{public}d,", userId); + int32_t clientUserId = GetUserIdByCallingUid(); - if (clientUserId <= INVALID_USER_ID) { - TLOGE(WmsLogTag::WMS_RECOVER, "userId is illegal: %{public}d", clientUserId); - return ERR_INVALID_VALUE; + DisplayId displayId = DISPLAY_ID_INVALID; + // SYSTEM_USERID calling, userId = INVALID_USER_ID, set userId to defaultWMSUserId_ + if (clientUserId == SYSTEM_USERID && userId == INVALID_USER_ID) { + TLOGI(WmsLogTag::WMS_MULTI_USER, "system user, userId is INVALID_USER_ID, use defaultWMSUserId_: %{public}d", + defaultWMSUserId_); + userId = defaultWMSUserId_; + } + + ErrCode ret = ValidateParameters(clientUserId, userId); + if (ret != ERR_OK) { + return ret; + } + + if (clientUserId == SYSTEM_USERID && userId != INVALID_USER_ID) { + ret = GetForegroundOsAccountDisplayId(userId, displayId); + if (ret != ERR_OK) { + return ret; + } + } else { + displayId = DEFAULT_SCREEN_ID; } + int32_t pid = IPCSkeleton::GetCallingRealPid(); - TLOGD(WmsLogTag::WMS_RECOVER, "clientUserId = %{public}d, pid = %{public}d", clientUserId, pid); - UnregisterSMSLiteRecoverListener(clientUserId, pid); + UnregisterSMSLiteRecoverListener(clientUserId, displayId, pid); return ERR_OK; } -void MockSessionManagerService::UnregisterSMSLiteRecoverListener(int32_t userId, int32_t pid) +void MockSessionManagerService::UnregisterSMSLiteRecoverListener(int32_t clientUserId, int32_t displayId, int32_t pid) { - TLOGD(WmsLogTag::WMS_RECOVER, "userId = %{public}d, pid = %{public}d", userId, pid); - std::unique_lock lock(smsLiteRecoverListenerLock_); - std::map>* smsLiteRecoverListenerMap = - GetSMSLiteRecoverListenerMap(userId); - if (!smsLiteRecoverListenerMap) { + TLOGI(WmsLogTag::WMS_RECOVER, + "clientUserId = %{public}d, pid = %{public}d, displayId = %{public}d", clientUserId, pid, displayId); + if (clientUserId == SYSTEM_USERID) { + std::unique_lock lock(systemAppSmsLiteRecoverListenerLock_); + std::map>* systemAppSmsLiteRecoverListenerMap = + GetSystemAppSMSLiteRecoverListenerMap(displayId); + if (!systemAppSmsLiteRecoverListenerMap) { + TLOGE(WmsLogTag::WMS_RECOVER, "systemAppSmsLiteRecoverListenerMap is null"); + return; + } + auto iter = systemAppSmsLiteRecoverListenerMap->find(pid); + if (iter != systemAppSmsLiteRecoverListenerMap->end()) { + systemAppSmsLiteRecoverListenerMap->erase(iter); + } + } else { + std::unique_lock lock(smsLiteRecoverListenerLock_); + std::map>* smsLiteRecoverListenerMap = + GetSMSLiteRecoverListenerMap(clientUserId); + if (!smsLiteRecoverListenerMap) { + TLOGE(WmsLogTag::WMS_RECOVER, "smsLiteRecoverListenerMap is null"); + return; + } + auto iter = smsLiteRecoverListenerMap->find(pid); + if (iter != smsLiteRecoverListenerMap->end()) { + smsLiteRecoverListenerMap->erase(iter); + } + } +} + +ErrCode MockSessionManagerService::NotifySceneBoardAvailable() +{ + if (!SessionPermission::IsSystemCalling()) { + TLOGE(WmsLogTag::WMS_RECOVER, "permission denied"); + return ERR_PERMISSION_DENIED; + } + int32_t userId = GetUserIdByCallingUid(); + if (userId <= INVALID_USER_ID) { + TLOGE(WmsLogTag::WMS_RECOVER, "userId is illegal: %{public}d", userId); + return ERR_INVALID_VALUE; + } + TLOGI(WmsLogTag::WMS_RECOVER, "scene board is available with userId=%{public}d", userId); + + NotifySceneBoardAvailableToSystemAppLiteClient(userId); + NotifySceneBoardAvailableToLiteClient(userId); + + NotifySceneBoardAvailableToSystemAppClient(userId); + NotifySceneBoardAvailableToClient(userId); + return ERR_OK; +} + +void MockSessionManagerService::NotifySceneBoardAvailableToSystemAppClient(int32_t userId) +{ + int32_t displayId = DEFAULT_SCREEN_ID; + { + std::lock_guard lock(userId2ScreenIdMapMutex_); + displayId = userId2ScreenIdMap_[userId]; + } + TLOGI(WmsLogTag::WMS_RECOVER, "userId = %{public}d, displayId = %{public}d", userId, displayId); + std::shared_lock lock(systemAppSmsRecoverListenerLock_); + std::map>* systemAppSmsRecoverListenerMap = + GetSystemAppSMSRecoverListenerMap(displayId); + if (!systemAppSmsRecoverListenerMap) { + TLOGE(WmsLogTag::WMS_RECOVER, "systemAppSmsRecoverListenerMap is null"); + return; + } + TLOGI(WmsLogTag::WMS_RECOVER, "userId=%{public}d, Remote process count = %{public}" PRIu64, SYSTEM_USERID, + static_cast(systemAppSmsRecoverListenerMap->size())); + auto sessionManagerService = GetSessionManagerServiceByUserId(userId); + if (sessionManagerService == nullptr) { + TLOGE(WmsLogTag::WMS_RECOVER, "SessionManagerService is null"); + return; + } + for (auto& iter : *systemAppSmsRecoverListenerMap) { + if (iter.second != nullptr) { + TLOGI(WmsLogTag::WMS_RECOVER, "Call OnSessionManagerServiceRecover pid = %{public}d", iter.first); + iter.second->OnSessionManagerServiceRecover(sessionManagerService); + } + } +} + +void MockSessionManagerService::NotifySceneBoardAvailableToClient(int32_t userId) +{ + std::shared_lock lock(smsRecoverListenerLock_); + std::map>* smsRecoverListenerMap = + GetSMSRecoverListenerMap(userId); + if (!smsRecoverListenerMap) { + TLOGE(WmsLogTag::WMS_RECOVER, "smsRecoverListenerMap is null"); return; } - auto iter = smsLiteRecoverListenerMap->find(pid); - if (iter != smsLiteRecoverListenerMap->end()) { - smsLiteRecoverListenerMap->erase(iter); + TLOGI(WmsLogTag::WMS_RECOVER, "userId=%{public}d, Remote process count = %{public}" PRIu64, userId, + static_cast(smsRecoverListenerMap->size())); + auto sessionManagerService = GetSessionManagerServiceByUserId(userId); + if (sessionManagerService == nullptr) { + TLOGE(WmsLogTag::WMS_RECOVER, "SessionManagerService is null"); + return; + } + for (auto& iter : *smsRecoverListenerMap) { + if (iter.second != nullptr) { + TLOGI(WmsLogTag::WMS_RECOVER, "Call OnSessionManagerServiceRecover pid = %{public}d", iter.first); + iter.second->OnSessionManagerServiceRecover(sessionManagerService); + } + } +} + +void MockSessionManagerService::NotifySceneBoardAvailableToSystemAppLiteClient(int32_t userId) +{ + int32_t displayId = DEFAULT_SCREEN_ID; + { + std::lock_guard lock(userId2ScreenIdMapMutex_); + displayId = userId2ScreenIdMap_[userId]; + } + TLOGI(WmsLogTag::WMS_RECOVER, "userId = %{public}d, displayId = %{public}d", userId, displayId); + std::shared_lock lock(systemAppSmsLiteRecoverListenerLock_); + std::map>* systemAppSmsLiteRecoverListenerMap = + GetSystemAppSMSLiteRecoverListenerMap(displayId); + if (!systemAppSmsLiteRecoverListenerMap) { + TLOGE(WmsLogTag::WMS_RECOVER, "systemAppSmsLiteRecoverListenerMap is null"); + return; + } + TLOGI(WmsLogTag::WMS_RECOVER, "userId=%{public}d, Remote process count = %{public}" PRIu64, SYSTEM_USERID, + static_cast(systemAppSmsLiteRecoverListenerMap->size())); + auto sessionManagerService = GetSessionManagerServiceByUserId(userId); + if (sessionManagerService == nullptr) { + TLOGE(WmsLogTag::WMS_RECOVER, "SessionManagerService is null"); + return; + } + for (auto& iter : *systemAppSmsLiteRecoverListenerMap) { + if (iter.second != nullptr) { + TLOGI(WmsLogTag::WMS_RECOVER, "Call OnSessionManagerServiceRecover Lite pid = %{public}d", iter.first); + iter.second->OnSessionManagerServiceRecover(sessionManagerService); + } } } @@ -523,20 +706,19 @@ void MockSessionManagerService::NotifySceneBoardAvailableToLiteClient(int32_t us std::map>* smsLiteRecoverListenerMap = GetSMSLiteRecoverListenerMap(userId); if (!smsLiteRecoverListenerMap) { + TLOGE(WmsLogTag::WMS_RECOVER, "smsLiteRecoverListenerMap is null"); return; } TLOGI(WmsLogTag::WMS_RECOVER, "userId=%{public}d, Remote process count = %{public}" PRIu64, userId, static_cast(smsLiteRecoverListenerMap->size())); - auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_); + auto sessionManagerService = GetSessionManagerServiceByUserId(userId); if (sessionManagerService == nullptr) { TLOGE(WmsLogTag::WMS_RECOVER, "SessionManagerService is null"); return; } for (auto& iter : *smsLiteRecoverListenerMap) { if (iter.second != nullptr) { - TLOGI(WmsLogTag::WMS_RECOVER, - "Call OnSessionManagerServiceRecover Lite pid = %{public}d, ref count = %{public}" PRId32, iter.first, - iter.second->GetSptrRefCount()); + TLOGI(WmsLogTag::WMS_RECOVER, "Call OnSessionManagerServiceRecover Lite pid = %{public}d", iter.first); iter.second->OnSessionManagerServiceRecover(sessionManagerService); } } @@ -546,9 +728,15 @@ void MockSessionManagerService::NotifyWMSConnected(int32_t userId, int32_t scree { TLOGI(WmsLogTag::WMS_MULTI_USER, "userId = %{public}d, screenId = %{public}d, isColdStart = %{public}d", userId, screenId, isColdStart); - currentScreenId_ = screenId; - currentWMSUserId_ = userId; - auto smsDeathRecipient = GetSMSDeathRecipientByUserId(currentWMSUserId_); + if (screenId == defaultScreenId_) { + defaultWMSUserId_ = userId; + TLOGI(WmsLogTag::WMS_MULTI_USER, "set defaultWMSUserId_ = %{public}d", defaultWMSUserId_); + } + { + std::lock_guard lock(userId2ScreenIdMapMutex_); + userId2ScreenIdMap_[userId] = screenId; + } + auto smsDeathRecipient = GetSMSDeathRecipientByUserId(userId); if (smsDeathRecipient != nullptr) { smsDeathRecipient->SetScreenId(screenId); } @@ -573,24 +761,24 @@ void MockSessionManagerService::NotifyWMSConnectionChanged(int32_t wmsUserId, in void MockSessionManagerService::NotifyWMSConnectionChangedToClient( int32_t wmsUserId, int32_t screenId, bool isConnected) { - std::shared_lock lock(smsRecoverListenerLock_); - std::map>* smsRecoverListenerMap = - GetSMSRecoverListenerMap(SYSTEM_USERID); - if (!smsRecoverListenerMap) { - TLOGW(WmsLogTag::WMS_MULTI_USER, "smsRecoverListenerMap is null"); + std::shared_lock lock(systemAppSmsRecoverListenerLock_); + std::map>* systemAppSmsRecoverListenerMap = + GetSystemAppSMSRecoverListenerMap(screenId); + if (!systemAppSmsRecoverListenerMap) { + TLOGE(WmsLogTag::WMS_MULTI_USER, "systemAppSmsRecoverListenerMap is null"); return; } - TLOGD(WmsLogTag::WMS_MULTI_USER, - "wmsUserId = %{public}d, isConnected = %{public}d, remote process count = %{public}zu", - wmsUserId, isConnected, smsRecoverListenerMap->size()); - auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_); + TLOGI(WmsLogTag::WMS_MULTI_USER, + "wmsUserId = %{public}d, isConnected = %{public}d, remote process count = %{public}zu", + wmsUserId, isConnected, systemAppSmsRecoverListenerMap->size()); + auto sessionManagerService = GetSessionManagerServiceByUserId(wmsUserId); if (sessionManagerService == nullptr) { TLOGE(WmsLogTag::WMS_RECOVER, "SessionManagerService is null"); return; } - for (auto& iter : *smsRecoverListenerMap) { + for (auto& iter : *systemAppSmsRecoverListenerMap) { if (iter.second != nullptr) { - TLOGD(WmsLogTag::WMS_MULTI_USER, "Call OnWMSConnectionChanged pid = %{public}d", iter.first); + TLOGI(WmsLogTag::WMS_MULTI_USER, "Call OnWMSConnectionChanged pid = %{public}d", iter.first); iter.second->OnWMSConnectionChanged(wmsUserId, screenId, isConnected, sessionManagerService); } } @@ -599,21 +787,24 @@ void MockSessionManagerService::NotifyWMSConnectionChangedToClient( void MockSessionManagerService::NotifyWMSConnectionChangedToLiteClient( int32_t wmsUserId, int32_t screenId, bool isConnected) { - std::shared_lock lock(smsLiteRecoverListenerLock_); - std::map>* smsLiteRecoverListenerMap = - GetSMSLiteRecoverListenerMap(SYSTEM_USERID); - if (!smsLiteRecoverListenerMap) { + std::shared_lock lock(systemAppSmsLiteRecoverListenerLock_); + std::map>* systemAppSmsLiteRecoverListenerMap = + GetSystemAppSMSLiteRecoverListenerMap(screenId); + if (!systemAppSmsLiteRecoverListenerMap) { + TLOGE(WmsLogTag::WMS_MULTI_USER, "systemAppSmsLiteRecoverListenerMap is null"); return; } - TLOGD(WmsLogTag::WMS_MULTI_USER, "wmsUserId = %{public}d, isConnected = %{public}d", wmsUserId, isConnected); - auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_); + TLOGI(WmsLogTag::WMS_MULTI_USER, + "wmsUserId = %{public}d, isConnected = %{public}d, remote process count = %{public}zu", + wmsUserId, isConnected, systemAppSmsLiteRecoverListenerMap->size()); + auto sessionManagerService = GetSessionManagerServiceByUserId(wmsUserId); if (sessionManagerService == nullptr) { TLOGE(WmsLogTag::WMS_RECOVER, "SessionManagerService is null"); return; } - for (auto& iter : *smsLiteRecoverListenerMap) { + for (auto& iter : *systemAppSmsLiteRecoverListenerMap) { if (iter.second != nullptr) { - TLOGD(WmsLogTag::WMS_MULTI_USER, + TLOGI(WmsLogTag::WMS_MULTI_USER, "Call OnWMSConnectionChanged Lite pid = %{public}d, ref count = %{public}d", iter.first, iter.second->GetSptrRefCount()); iter.second->OnWMSConnectionChanged(wmsUserId, screenId, isConnected, sessionManagerService); @@ -639,7 +830,7 @@ void MockSessionManagerService::ShowIllegalArgsInfo(std::string& dumpInfo) sptr MockSessionManagerService::GetSceneSessionManager() { - auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_); + auto sessionManagerService = GetSessionManagerServiceByUserId(defaultWMSUserId_); if (sessionManagerService == nullptr) { WLOGFE("SessionManagerService is null"); return nullptr; @@ -654,8 +845,8 @@ sptr MockSessionManagerService::GetSceneSessionManager() WLOGFW("Get scene session manager proxy failed, scene session manager service is null"); return sptr(nullptr); } - sceneSessionManager_ = remoteObject; - return sceneSessionManager_; + defaultSceneSessionManager_ = remoteObject; + return defaultSceneSessionManager_; } int MockSessionManagerService::DumpSessionInfo(const std::vector& args, std::string& dumpInfo) @@ -663,20 +854,24 @@ int MockSessionManagerService::DumpSessionInfo(const std::vector& a if (args.empty()) { return -1; // WMError::WM_ERROR_INVALID_PARAM; } - auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_); + auto sessionManagerService = GetSessionManagerServiceByUserId(defaultWMSUserId_); if (sessionManagerService == nullptr) { WLOGFE("sessionManagerService is nullptr"); return -1; } - if (!sceneSessionManager_) { + if (!defaultSceneSessionManager_) { WLOGFW("Get scene session manager ..."); GetSceneSessionManager(); - if (!sceneSessionManager_) { + if (!defaultSceneSessionManager_) { WLOGFW("Get scene session manager proxy failed, nullptr"); return -1; } } - sptr sceneSessionManagerProxy = iface_cast(sceneSessionManager_); + sptr sceneSessionManagerProxy = iface_cast(defaultSceneSessionManager_); + if (sceneSessionManagerProxy == nullptr) { + WLOGFW("sessionManagerServiceProxy is nullptr"); + return -1; + } WSError ret = sceneSessionManagerProxy->GetSessionDumpInfo(args, dumpInfo); if (ret != WSError::WS_OK) { WLOGFD("sessionManagerService set success!"); @@ -751,15 +946,15 @@ bool MockSessionManagerService::SMSDeathRecipient::IsSceneBoardTestMode() void MockSessionManagerService::GetProcessSurfaceNodeIdByPersistentId(const int32_t pid, const std::vector& windowIdList, std::vector& surfaceNodeIds) { - auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_); + auto sessionManagerService = GetSessionManagerServiceByUserId(defaultWMSUserId_); if (sessionManagerService == nullptr) { WLOGFE("sessionManagerService is nullptr"); return; } - if (!sceneSessionManager_) { + if (!defaultSceneSessionManager_) { WLOGFW("Get scene session manager ..."); GetSceneSessionManager(); - if (!sceneSessionManager_) { + if (!defaultSceneSessionManager_) { WLOGFW("Get scene session manager proxy failed, nullptr"); return; } @@ -768,11 +963,14 @@ void MockSessionManagerService::GetProcessSurfaceNodeIdByPersistentId(const int3 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "windowIdList is null, no need to get surfaceNodeId"); return; } - std::vector persistentIds; - for (uint64_t id : windowIdList) { - persistentIds.push_back(static_cast(id)); + std::vector persistentIds(windowIdList.size()); + std::transform(windowIdList.begin(), windowIdList.end(), + persistentIds.begin(), [](uint64_t id) { return static_cast(id); }); + sptr sceneSessionManagerProxy = iface_cast(defaultSceneSessionManager_); + if (sceneSessionManagerProxy == nullptr) { + WLOGFW("sessionManagerServiceProxy is nullptr"); + return; } - sptr sceneSessionManagerProxy = iface_cast(sceneSessionManager_); WMError ret = sceneSessionManagerProxy->GetProcessSurfaceNodeIdByPersistentId( pid, persistentIds, surfaceNodeIds); if (ret != WMError::WM_OK) { @@ -782,19 +980,19 @@ void MockSessionManagerService::GetProcessSurfaceNodeIdByPersistentId(const int3 void MockSessionManagerService::AddSkipSelfWhenShowOnVirtualScreenList(const std::vector& persistentIds) { - auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_); + auto sessionManagerService = GetSessionManagerServiceByUserId(defaultWMSUserId_); if (sessionManagerService == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "sessionManagerService is nullptr"); return; } - if (!sceneSessionManager_) { + if (!defaultSceneSessionManager_) { GetSceneSessionManager(); - if (!sceneSessionManager_) { + if (!defaultSceneSessionManager_) { TLOGW(WmsLogTag::WMS_ATTRIBUTE, "get scene session manager proxy failed, nullptr"); return; } } - sptr sceneSessionManagerProxy = iface_cast(sceneSessionManager_); + sptr sceneSessionManagerProxy = iface_cast(defaultSceneSessionManager_); if (sceneSessionManagerProxy == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "sessionManagerServiceProxy is nullptr"); return; @@ -807,19 +1005,19 @@ void MockSessionManagerService::AddSkipSelfWhenShowOnVirtualScreenList(const std void MockSessionManagerService::RemoveSkipSelfWhenShowOnVirtualScreenList(const std::vector& persistentIds) { - auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_); + auto sessionManagerService = GetSessionManagerServiceByUserId(defaultWMSUserId_); if (sessionManagerService == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "sessionManagerService is nullptr"); return; } - if (!sceneSessionManager_) { + if (!defaultSceneSessionManager_) { GetSceneSessionManager(); - if (!sceneSessionManager_) { + if (!defaultSceneSessionManager_) { TLOGW(WmsLogTag::WMS_ATTRIBUTE, "get scene session manager proxy failed, nullptr"); return; } } - sptr sceneSessionManagerProxy = iface_cast(sceneSessionManager_); + sptr sceneSessionManagerProxy = iface_cast(defaultSceneSessionManager_); if (sceneSessionManagerProxy == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "sessionManagerServiceProxy is nullptr"); return; @@ -837,19 +1035,19 @@ void MockSessionManagerService::SetScreenPrivacyWindowTagSwitch( TLOGI(WmsLogTag::WMS_ATTRIBUTE, "PrivacyWindowTags is empty"); return; } - auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_); + auto sessionManagerService = GetSessionManagerServiceByUserId(defaultWMSUserId_); if (sessionManagerService == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "sessionManagerService is nullptr"); return; } - if (!sceneSessionManager_) { + if (!defaultSceneSessionManager_) { GetSceneSessionManager(); - if (!sceneSessionManager_) { + if (!defaultSceneSessionManager_) { TLOGW(WmsLogTag::WMS_ATTRIBUTE, "get scene session manager proxy failed, nullptr"); return; } } - sptr sceneSessionManagerProxy = iface_cast(sceneSessionManager_); + sptr sceneSessionManagerProxy = iface_cast(defaultSceneSessionManager_); if (sceneSessionManagerProxy == nullptr) { TLOGE(WmsLogTag::WMS_ATTRIBUTE, "sessionManagerServiceProxy is nullptr"); return; @@ -880,6 +1078,62 @@ sptr MockSessionManagerService::GetSceneSessionManagerByUserId(in return remoteObject; } +ErrCode MockSessionManagerService::GetSceneSessionManagerCommon( + int32_t userId, + sptr& result, + bool isLite) +{ + int32_t clientUserId = GetUserIdByCallingUid(); + if (clientUserId <= INVALID_USER_ID) { + TLOGE(WmsLogTag::WMS_MULTI_USER, "userId is illegal: %{public}d", clientUserId); + return ERR_INVALID_VALUE; + } + + if (clientUserId != SYSTEM_USERID) { + return ERR_WOULD_BLOCK; + } + + auto sessionManagerService = GetSessionManagerServiceByUserId(userId); + if (sessionManagerService == nullptr) { + TLOGE(WmsLogTag::WMS_MULTI_USER, "sessionManagerService is nullptr, userId: %{public}d", userId); + return ERR_INVALID_VALUE; + } + + sptr sessionManagerServiceProxy = + iface_cast(sessionManagerService); + if (sessionManagerServiceProxy == nullptr) { + TLOGE(WmsLogTag::WMS_MULTI_USER, "sessionManagerServiceProxy is nullptr, userId: %{public}d", userId); + return ERR_DEAD_OBJECT; + } + + if (isLite) { + result = sessionManagerServiceProxy->GetSceneSessionManagerLite(); + } else { + result = sessionManagerServiceProxy->GetSceneSessionManager(); + } + + if (result == nullptr) { + TLOGE(WmsLogTag::WMS_MULTI_USER, + "Get scene session manager proxy failed, scene session manager service is null," + "userId: %{public}d, isLite: %{public}d", + userId, isLite); + return ERR_DEAD_OBJECT; + } + return ERR_OK; +} + +ErrCode MockSessionManagerService::GetSceneSessionManagerLiteByUserId(int32_t userId, + sptr& sceneSessionManagerLite) +{ + return GetSceneSessionManagerCommon(userId, sceneSessionManagerLite, true); +} + +ErrCode MockSessionManagerService::GetSceneSessionManagerByUserId(int32_t userId, + sptr& sceneSessionManager) +{ + return GetSceneSessionManagerCommon(userId, sceneSessionManager, false); +} + ErrCode MockSessionManagerService::RecoverSCBSnapshotSkipByUserId(int32_t userId) { std::unique_lock lock(userIdBundleNamesMapLock_); @@ -968,5 +1222,64 @@ ErrCode MockSessionManagerService::SetSnapshotSkipByIdNamesMapInner( } return ERR_OK; } + +ErrCode MockSessionManagerService::ValidateParameters(int32_t clientUserId, int32_t userId) const +{ + if (clientUserId <= INVALID_USER_ID) { + TLOGE(WmsLogTag::WMS_RECOVER, "clientUserId is illegal: %{public}d", clientUserId); + return ERR_INVALID_VALUE; + } + return ERR_OK; +} + +ErrCode MockSessionManagerService::GetForegroundOsAccountDisplayId(int32_t userId, DisplayId& displayId) const +{ + displayId = DISPLAY_ID_INVALID; + ErrCode err = AccountSA::OsAccountManager::GetForegroundOsAccountDisplayId(userId, displayId); + TLOGI(WmsLogTag::WMS_RECOVER, "displayId: %{public}" PRIu64, displayId); + if (err != ERR_OK) { + TLOGE(WmsLogTag::WMS_RECOVER, + "get user display failed, errorCode: %{public}d, userId %{public}d", err, userId); + } + return err; +} + +ErrCode MockSessionManagerService::NotifyWMSConnectionStatus(int32_t userId, + const sptr& smsListener) +{ + bool isWMSConnected = false; + { + std::lock_guard lock(wmsConnectionStatusLock_); + if (wmsConnectionStatusMap_.find(userId) != wmsConnectionStatusMap_.end()) { + isWMSConnected = wmsConnectionStatusMap_[userId]; + } + } + if (smsListener && isWMSConnected) { + auto sessionManagerService = GetSessionManagerServiceByUserId(userId); + if (sessionManagerService == nullptr) { + TLOGE(WmsLogTag::WMS_RECOVER, "SessionManagerService is null"); + return ERR_DEAD_OBJECT; + } + TLOGI(WmsLogTag::WMS_MULTI_USER, "wms is already connected, notify client"); + int32_t screenId = DEFAULT_SCREEN_ID; + { + std::lock_guard lock(userId2ScreenIdMapMutex_); + screenId = userId2ScreenIdMap_[userId]; + } + smsListener->OnWMSConnectionChanged(userId, screenId, true, sessionManagerService); + } + return ERR_OK; +} + +int32_t MockSessionManagerService::GetUserIdByCallingUid() +{ + int32_t uid = IPCSkeleton::GetCallingUid(); + TLOGI(WmsLogTag::WMS_MULTI_USER, "get calling uid(%{public}d)", uid); + if (uid <= INVALID_UID) { + TLOGE(WmsLogTag::WMS_MULTI_USER, "uid is illegal: %{public}d", uid); + return INVALID_USER_ID; + } + return GetUserIdByUid(uid); +} } // namespace Rosen } // namespace OHOS \ No newline at end of file