Bug 1741452 - Use nsWeakPtr in mHwndRootWindowMap r=jrmuizel

Use nsWeakPtr instead of RefPtr<nsBaseWidget>, since nsBaseWidget::~nsBaseWidget() and nsWindow::~nsWindow() seem to expect that a widget might be released without calling Destroy().

Differential Revision: https://phabricator.services.mozilla.com/D135512
This commit is contained in:
sotaro 2022-01-12 00:12:18 +00:00
parent caf7db3528
commit 7963675e93
2 changed files with 19 additions and 8 deletions

View File

@ -382,7 +382,8 @@ void WinWindowOcclusionTracker::Enable(nsBaseWidget* aWindow, HWND aHwnd) {
return;
}
mHwndRootWindowMap.emplace(aHwnd, aWindow);
nsWeakPtr weak = do_GetWeakReference(aWindow);
mHwndRootWindowMap.emplace(aHwnd, weak);
RefPtr<Runnable> runnable = WrapRunnable(
RefPtr<WindowOcclusionCalculator>(
@ -623,9 +624,13 @@ void WinWindowOcclusionTracker::UpdateOcclusionState(
} else if (aShowAllWindows) {
occlState = OcclusionState::VISIBLE;
}
it->second->NotifyOcclusionState(occlState);
if (it->second->SizeMode() != nsSizeMode_Minimized) {
nsCOMPtr<nsIWidget> widget = do_QueryReferent(it->second);
if (!widget) {
continue;
}
auto* baseWidget = static_cast<nsBaseWidget*>(widget.get());
baseWidget->NotifyOcclusionState(occlState);
if (baseWidget->SizeMode() != nsSizeMode_Minimized) {
mNumVisibleRootWindows++;
}
}
@ -688,11 +693,16 @@ void WinWindowOcclusionTracker::MarkNonIconicWindowsOccluded() {
// Set all visible root windows as occluded. If not visible,
// set them as hidden.
for (auto& [hwnd, window] : mHwndRootWindowMap) {
auto state = (window->SizeMode() == nsSizeMode_Minimized)
for (auto& [hwnd, weak] : mHwndRootWindowMap) {
nsCOMPtr<nsIWidget> widget = do_QueryReferent(weak);
if (!widget) {
continue;
}
auto* baseWidget = static_cast<nsBaseWidget*>(widget.get());
auto state = (baseWidget->SizeMode() == nsSizeMode_Minimized)
? OcclusionState::HIDDEN
: OcclusionState::OCCLUDED;
window->NotifyOcclusionState(state);
baseWidget->NotifyOcclusionState(state);
}
}

View File

@ -12,6 +12,7 @@
#include <unordered_set>
#include <vector>
#include "nsIWeakReferenceUtils.h"
#include "mozilla/ThreadSafeWeakPtr.h"
#include "mozilla/widget/WindowOcclusionState.h"
#include "mozilla/widget/WinEventObserver.h"
@ -284,7 +285,7 @@ class WinWindowOcclusionTracker final : public DisplayStatusListener,
// Map of HWND to widget. Maintained on main thread, and used to send
// occlusion state notifications to Windows from
// mRootWindowHwndsOcclusionState.
std::unordered_map<HWND, nsBaseWidget*> mHwndRootWindowMap;
std::unordered_map<HWND, nsWeakPtr> mHwndRootWindowMap;
// This is set by UpdateOcclusionState(). It is currently only used by tests.
int mNumVisibleRootWindows = 0;