diff --git a/wm/include/window_adapter.h b/wm/include/window_adapter.h index 4f511464dd06b7a721398996fa8f857913983e22..bcbd6edf4256f3acbc939f6fbdca6ba39b481352 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: + WMSDeathRecipinet(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 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..c082ec2534fa22e38a7d59f94128c2fece12c745 100644 --- a/wm/include/window_adapter_lite.h +++ b/wm/include/window_adapter_lite.h @@ -29,11 +29,14 @@ 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: using WMSConnectionChangedCallbackFunc = std::function; virtual void GetFocusWindowInfo(FocusChangeInfo& focusInfo, DisplayId displayId = DEFAULT_DISPLAY_ID); @@ -62,13 +65,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 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..1c7878e5c0bff18b7fb2b175b888072151b5edb5 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,68 @@ WM_IMPLEMENT_SINGLE_INSTANCE(WindowAdapter) } \ } while (false) +WMSDeathRecipient::WMSDeathRecipient(const int32_t userId) : userId_(userId) {} + +void WMSDeathRecipient::OnRemoteDied(const wptr& wptrDeath) +{ + TLOGI(WmsLogTag::WMS_RECOVER, "wms died"); + sptr object = wptrDeath.promote(); + if(object == nullptr) { + TLOGE(WmsLogTag::WMS_RECOVER, "invalid user: %d", userId_); + return; + } + WindowAdapter::GetInstance(userId_)->ClearWindowAdapter(); +} + +WindowAdapter::WindowAdapter(const int32_t userId) : userId_(userId) {} + +WindowAdapter& WindowAdapter::GetInstance() +{ + static sptr instance = nullptr; + static std::mutex mtx; + if(instance == nullptr) { + std::lock_guard lock(mtx); + if(instance == nullptr) { + instance = new WindowAdapter(); + } + } + return *instance; +} + +sptr WindowAdapter::GetInstance(const int32_t userId) +{ + sptr instance = nullptr; + + if(userId <= INVALID_USER_ID) { + TLOGI(WmsLogTag::WMS_MULTI_USER, "use singleton mode"); + instance = &WindowAdapter::GetInstance(); + return instance; + } + // multi-instance mode + { + std::lock_guard lock(windowAdapterMapMutex_); + auto iter = windowAdapterMap_.find(userId); + fi(iter != windowAdapterMap_.end()) { + return iter->second; + } + } + TLOGI(WmsLogTag::WMS_MULTI_USER, "create new instanceuserId: %d", userId); + instance = new WindowAdapter(userId, instance); + return instance; +} + +WindowAdapter::~WindowAdapter() +{ + TLOGI(WmsLogTag::WMS_SCB, "destroyed"); + 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) { @@ -456,7 +519,7 @@ WMError WindowAdapter::RegisterWMSConnectionChangedListener(const WMSConnectionC WMError WindowAdapter::UnregisterWMSConnectionChangedListener() { TLOGI(WmsLogTag::WMS_MULTI_USER, "unregister wms connection changed listener"); - return SessionManager::GetInstance().UnregisterWMSConnectionChangedListener(); + return SessionManager::GetInstance(userId_)->UnregisterWMSConnectionChangedListener(); } void WindowAdapter::WindowManagerAndSessionRecover() @@ -541,12 +604,12 @@ bool WindowAdapter::InitSSMProxy() { std::lock_guard lock(mutex_); if (!isProxyValid_) { - windowManagerServiceProxy_ = SessionManager::GetInstance().GetSceneSessionManagerProxy(); + windowManagerServiceProxy_ = SessionManager::GetInstance(userId_)->GetSceneSessionManagerProxy(); if (!windowManagerServiceProxy_ || !windowManagerServiceProxy_->AsObject()) { WLOGFE("Failed to get system scene session manager services"); return false; } - wmsDeath_ = new (std::nothrow) WMSDeathRecipient(); + wmsDeath_ = sprt::MakeSptr(userId_); if (!wmsDeath_) { WLOGFE("Failed to create death Recipient ptr WMSDeathRecipient"); return false; @@ -557,14 +620,20 @@ bool WindowAdapter::InitSSMProxy() return false; } if (!recoverInitialized_) { - SessionManager::GetInstance().RegisterWindowManagerRecoverCallbackFunc( - [this] { this->WindowManagerAndSessionRecover(); }); + SessionManager::GetInstance(userId_)->RegisterWindowManagerRecoverCallbackFunc( + weakThis = wptr(this) + auto windowAdapter = weakThis.promote(); + if(!windowAdapter) { + TLOGE(WmsLogTag::WMS_SCB, "window adapter is null"); + return; + }); + windowAdapter->WindowManagerAndSessionRecover(); recoverInitialized_ = true; } // U0 system user needs to subscribe OnUserSwitch event int32_t clientUserId = GetUserIdByUid(getuid()); if (clientUserId == SYSTEM_USERID && !isRegisteredUserSwitchListener_) { - SessionManager::GetInstance().RegisterUserSwitchListener([this]() { this->OnUserSwitch(); }); + SessionManager::GetInstance(userId_)->RegisterUserSwitchListener([this]() { this->OnUserSwitch(); }); isRegisteredUserSwitchListener_ = true; } isProxyValid_ = true; @@ -574,6 +643,7 @@ bool WindowAdapter::InitSSMProxy() 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 +652,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..9e898d386f1e1c514d43d53fd96f8cfd66967fc3 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> WindowAdapter::windowAdapterLiteMap_ = {}; +std::mutex WindowAdapterLite::windowAdapterLiteMapMutex_; #define INIT_PROXY_CHECK_RETURN(ret) \ do { \ @@ -51,6 +52,43 @@ WM_IMPLEMENT_SINGLE_INSTANCE(WindowAdapterLite) } \ } while(false) +WindowAdapterLite::WindowAdapterLite(const int32_t userId) : userId_(userId) {} + +WindowAdapterLite& WindowAdapterLite::GetInstance() +{ + static sptr instance = nullptr; + static std::mutex mtx; + if(instance == nullptr) { + std::lock_guard lock(mtx); + if(instance == nullptr) { + instance = new WindowAdapterLite(); + } + } + return *instance; +} + +sptr WindowAdapterLite::GetInstance(const int32_t userId) +{ + sptr instance = nullptr; + + if(userId <= INVALID_USER_ID) { + TLOGI(WmsLogTag::WMS_MULTI_USER, "use singleton mode"); + instance = &WindowAdapterLite::GetInstance(); + return instance; + } + // multi-instance mode + { + std::lock_guard lock(windowAdapterLiteMapMutex_); + auto iter = windowAdapterLiteMap_.find(userId); + fi(iter != windowAdapterLiteMap_.end()) { + return iter->second; + } + } + TLOGI(WmsLogTag::WMS_MULTI_USER, "create new instanceuserId: %d", userId); + instance = new WindowAdapterLite(userId, instance); + return instance; +} + WMError WindowAdapterLite::RegisterWindowManagerAgent(WindowManagerAgentType type, const sptr& windowManagerAgent) { @@ -134,12 +172,12 @@ bool WindowAdapterLite::InitSSMProxy() { std::lock_guard lock(mutex_); if (!isProxyValid_) { - windowManagerServiceProxy_ = SessionManagerLite::GetInstance().GetSceneSessionManagerLiteProxy(); + windowManagerServiceProxy_ = SessionManagerLite::GetInstance(userId_)->GetSceneSessionManagerLiteProxy(); if (!windowManagerServiceProxy_ || !windowManagerServiceProxy_->AsObject()) { WLOGFE("Failed to get scene session manager lite proxy"); return false; } - wmsDeath_ = new (std::nothrow) WMSDeathRecipient(); + wmsDeath_ = new (std::nothrow) WMSDeathRecipient(userId_); if (!wmsDeath_) { WLOGFE("Failed to create death recipient WMSDeathRecipient"); return false; @@ -152,7 +190,15 @@ bool WindowAdapterLite::InitSSMProxy() // U0 system user needs to subscribe OnUserSwitch event int32_t clientUserId = GetUserIdByUid(getuid()); if (clientUserId == SYSTEM_USERID && !isRegisteredUserSwitchListener_) { - SessionManagerLite::GetInstance().RegisterUserSwitchListener([this] { this->OnUserSwitch(); }); + SessionManagerLite::GetInstance(userId_)->RegisterUserSwitchListener([weakThis = wptr(this)] + { + auto windowAdapter = weakThis.promote(); + if(!windowAdapter) { + TLOGE(WmsLogTag::WMS_SCB, "window adapter is mull"); + return; + } + windowAdapter->OnUserSwitch(); + }); isRegisteredUserSwitchListener_ = true; } isProxyValid_ = true; @@ -179,6 +225,8 @@ void WindowAdapterLite::ClearWindowAdapter() windowManagerServiceProxy_ = nullptr; } +WMSDeathRecipient::WMSDeathRecipient(int32_t userId) : userId_(userId) {} + void WMSDeathRecipient::OnRemoteDied(const wptr& wptrDeath) { if (wptrDeath == nullptr) { @@ -190,9 +238,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 +331,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)