mirror of
https://github.com/openharmony/windowmanager.git
synced 2026-07-19 17:08:11 -04:00
!1158 ensure lifecycle callback thread safety
Merge pull request !1158 from 邢亚楠/split_start_dev
This commit is contained in:
+54
-38
@@ -77,20 +77,20 @@ class WindowImpl : public Window {
|
||||
using ListenerTaskCallback = std::function<void()>;
|
||||
using EventHandler = OHOS::AppExecFwk::EventHandler;
|
||||
using Priority = OHOS::AppExecFwk::EventQueue::Priority;
|
||||
#define CALL_LIFECYCLE_LISTENER(windowLifecycleCb) \
|
||||
do { \
|
||||
for (auto& listener : lifecycleListeners_) { \
|
||||
if (listener != nullptr) { \
|
||||
listener->windowLifecycleCb(); \
|
||||
} \
|
||||
} \
|
||||
#define CALL_LIFECYCLE_LISTENER(windowLifecycleCb, listeners) \
|
||||
do { \
|
||||
for (auto& listener : (listeners)) { \
|
||||
if (listener != nullptr) { \
|
||||
listener->windowLifecycleCb(); \
|
||||
} \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define CALL_UI_CONTENT(uiContentCb) \
|
||||
do { \
|
||||
if (uiContent_ != nullptr) { \
|
||||
uiContent_->uiContentCb(); \
|
||||
} \
|
||||
#define CALL_UI_CONTENT(uiContentCb) \
|
||||
do { \
|
||||
if (uiContent_ != nullptr) { \
|
||||
uiContent_->uiContentCb(); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
public:
|
||||
@@ -242,38 +242,50 @@ public:
|
||||
void PostListenerTask(ListenerTaskCallback &&callback, Priority priority = Priority::LOW,
|
||||
const std::string taskName = "");
|
||||
private:
|
||||
inline void NotifyAfterForeground()
|
||||
inline std::vector<sptr<IWindowLifeCycle>> GetLifecycleListeners()
|
||||
{
|
||||
PostListenerTask([this]() {
|
||||
CALL_LIFECYCLE_LISTENER(AfterForeground);
|
||||
CALL_UI_CONTENT(Foreground);
|
||||
std::vector<sptr<IWindowLifeCycle>> lifecycleListeners;
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(mutex_);
|
||||
lifecycleListeners = lifecycleListeners_;
|
||||
}
|
||||
return lifecycleListeners;
|
||||
}
|
||||
inline void NotifyAfterForeground(bool needNotifyUiContent = true)
|
||||
{
|
||||
auto lifecycleListeners = GetLifecycleListeners();
|
||||
PostListenerTask([this, lifecycleListeners, needNotifyUiContent]() {
|
||||
CALL_LIFECYCLE_LISTENER(AfterForeground, lifecycleListeners);
|
||||
if (needNotifyUiContent) {
|
||||
CALL_UI_CONTENT(Foreground);
|
||||
}
|
||||
});
|
||||
}
|
||||
inline void NotifyAfterBackground()
|
||||
{
|
||||
PostListenerTask([this]() {
|
||||
CALL_LIFECYCLE_LISTENER(AfterBackground);
|
||||
auto lifecycleListeners = GetLifecycleListeners();
|
||||
PostListenerTask([this, lifecycleListeners]() {
|
||||
CALL_LIFECYCLE_LISTENER(AfterBackground, lifecycleListeners);
|
||||
CALL_UI_CONTENT(Background);
|
||||
});
|
||||
}
|
||||
inline void NotifyAfterFocused()
|
||||
{
|
||||
PostListenerTask([this]() {
|
||||
CALL_LIFECYCLE_LISTENER(AfterFocused);
|
||||
auto lifecycleListeners = GetLifecycleListeners();
|
||||
PostListenerTask([this, lifecycleListeners]() {
|
||||
CALL_LIFECYCLE_LISTENER(AfterFocused, lifecycleListeners);
|
||||
CALL_UI_CONTENT(Focus);
|
||||
});
|
||||
}
|
||||
inline void NotifyAfterUnfocused()
|
||||
inline void NotifyAfterUnfocused(bool needNotifyUiContent = true)
|
||||
{
|
||||
PostListenerTask([this]() {
|
||||
CALL_LIFECYCLE_LISTENER(AfterUnfocused);
|
||||
CALL_UI_CONTENT(UnFocus);
|
||||
});
|
||||
}
|
||||
inline void NotifyListenerAfterUnfocused()
|
||||
{
|
||||
PostListenerTask([this]() {
|
||||
CALL_LIFECYCLE_LISTENER(AfterUnfocused);
|
||||
auto lifecycleListeners = GetLifecycleListeners();
|
||||
// use needNotifyUinContent to separate ui content callbacks
|
||||
PostListenerTask([this, lifecycleListeners, needNotifyUiContent]() {
|
||||
CALL_LIFECYCLE_LISTENER(AfterUnfocused, lifecycleListeners);
|
||||
if (needNotifyUiContent) {
|
||||
CALL_UI_CONTENT(UnFocus);
|
||||
}
|
||||
});
|
||||
}
|
||||
inline void NotifyBeforeDestroy(std::string windowName)
|
||||
@@ -299,26 +311,30 @@ private:
|
||||
}
|
||||
inline void NotifyAfterActive()
|
||||
{
|
||||
PostListenerTask([this]() {
|
||||
CALL_LIFECYCLE_LISTENER(AfterActive);
|
||||
auto lifecycleListeners = GetLifecycleListeners();
|
||||
PostListenerTask([lifecycleListeners]() {
|
||||
CALL_LIFECYCLE_LISTENER(AfterActive, lifecycleListeners);
|
||||
});
|
||||
}
|
||||
inline void NotifyAfterInactive()
|
||||
{
|
||||
PostListenerTask([this]() {
|
||||
CALL_LIFECYCLE_LISTENER(AfterInactive);
|
||||
auto lifecycleListeners = GetLifecycleListeners();
|
||||
PostListenerTask([lifecycleListeners]() {
|
||||
CALL_LIFECYCLE_LISTENER(AfterInactive, lifecycleListeners);
|
||||
});
|
||||
}
|
||||
inline void NotifyForegroundFailed()
|
||||
{
|
||||
PostListenerTask([this]() {
|
||||
CALL_LIFECYCLE_LISTENER(ForegroundFailed);
|
||||
auto lifecycleListeners = GetLifecycleListeners();
|
||||
PostListenerTask([lifecycleListeners]() {
|
||||
CALL_LIFECYCLE_LISTENER(ForegroundFailed, lifecycleListeners);
|
||||
});
|
||||
}
|
||||
inline void NotifyForegroundInvalidWindowMode()
|
||||
{
|
||||
PostListenerTask([this]() {
|
||||
CALL_LIFECYCLE_LISTENER(ForegroundInvalidMode);
|
||||
auto lifecycleListeners = GetLifecycleListeners();
|
||||
PostListenerTask([lifecycleListeners]() {
|
||||
CALL_LIFECYCLE_LISTENER(ForegroundInvalidMode, lifecycleListeners);
|
||||
});
|
||||
}
|
||||
void DestroyFloatingWindow();
|
||||
|
||||
@@ -1002,7 +1002,7 @@ WMError WindowImpl::Show(uint32_t reason, bool withAnimation)
|
||||
WLOGFI("window is already shown id: %{public}u, raise to top", property_->GetWindowId());
|
||||
SingletonContainer::Get<WindowAdapter>().ProcessPointDown(property_->GetWindowId());
|
||||
}
|
||||
CALL_LIFECYCLE_LISTENER(AfterForeground);
|
||||
NotifyAfterUnfocused(false);
|
||||
return WMError::WM_OK;
|
||||
}
|
||||
WMError ret = PreProcessShow(reason, withAnimation);
|
||||
@@ -2147,7 +2147,7 @@ void WindowImpl::ConsumePointerEvent(std::shared_ptr<MMI::PointerEvent>& pointer
|
||||
return;
|
||||
}
|
||||
if (!WindowHelper::IsPointInTargetRect(pointerItem.GetDisplayX(), pointerItem.GetDisplayY(), GetRect())) {
|
||||
NotifyListenerAfterUnfocused();
|
||||
NotifyAfterUnfocused(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user