ported from UXP: No issue - Modernize nsWindowWatcher (ce7568ee)

This commit is contained in:
2026-02-26 09:32:46 +08:00
parent 39e62d7cb3
commit b2800f2c18
3 changed files with 23 additions and 85 deletions
@@ -4,8 +4,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//#define USEWEAKREFS // (haven't quite figured that out yet)
#include "nsWindowWatcher.h"
#include "nsAutoWindowStateHelper.h"
@@ -71,10 +69,6 @@
#include "nsIXULBrowserWindow.h"
#include "nsGlobalWindow.h"
#ifdef USEWEAKREFS
#include "nsIWeakReference.h"
#endif
using namespace mozilla;
using namespace mozilla::dom;
@@ -88,20 +82,9 @@ struct nsWatcherWindowEntry
{
nsWatcherWindowEntry(mozIDOMWindowProxy* aWindow, nsIWebBrowserChrome* aChrome)
: mChrome(nullptr)
: mWindow(do_GetWeakReference(aWindow))
, mChrome(do_GetWeakReference(aChrome))
{
#ifdef USEWEAKREFS
mWindow = do_GetWeakReference(aWindow);
#else
mWindow = aWindow;
#endif
nsCOMPtr<nsISupportsWeakReference> supportsweak(do_QueryInterface(aChrome));
if (supportsweak) {
supportsweak->GetWeakReference(getter_AddRefs(mChromeWeak));
} else {
mChrome = aChrome;
mChromeWeak = nullptr;
}
ReferenceSelf();
}
~nsWatcherWindowEntry() {}
@@ -110,13 +93,8 @@ struct nsWatcherWindowEntry
void Unlink();
void ReferenceSelf();
#ifdef USEWEAKREFS
nsCOMPtr<nsIWeakReference> mWindow;
#else // still not an owning ref
mozIDOMWindowProxy* mWindow;
#endif
nsIWebBrowserChrome* mChrome;
nsWeakPtr mChromeWeak;
nsWeakPtr mWindow;
nsWeakPtr mChrome;
// each struct is in a circular, doubly-linked list
nsWatcherWindowEntry* mYounger; // next younger in sequence
nsWatcherWindowEntry* mOlder;
@@ -220,24 +198,17 @@ nsWatcherWindowEnumerator::GetNext(nsISupports** aResult)
*aResult = nullptr;
#ifdef USEWEAKREFS
while (mCurrentPosition) {
CallQueryReferent(mCurrentPosition->mWindow, aResult);
if (*aResult) {
nsCOMPtr<mozIDOMWindowProxy> window =
do_QueryReferent(mCurrentPosition->mWindow);
if (window) {
CallQueryInterface(window, aResult);
mCurrentPosition = FindNext();
break;
} else { // window is gone!
mWindowWatcher->RemoveWindow(mCurrentPosition);
return NS_OK;
}
}
NS_IF_ADDREF(*aResult);
#else
if (mCurrentPosition) {
CallQueryInterface(mCurrentPosition->mWindow, aResult);
mCurrentPosition = FindNext();
}
#endif
return NS_OK;
return NS_ERROR_FAILURE;
}
nsWatcherWindowEntry*
@@ -1483,14 +1454,7 @@ nsWindowWatcher::AddWindow(mozIDOMWindowProxy* aWindow, nsIWebBrowserChrome* aCh
// its chrome mapping and return
info = FindWindowEntry(aWindow);
if (info) {
nsCOMPtr<nsISupportsWeakReference> supportsweak(
do_QueryInterface(aChrome));
if (supportsweak) {
supportsweak->GetWeakReference(getter_AddRefs(info->mChromeWeak));
} else {
info->mChrome = aChrome;
info->mChromeWeak = nullptr;
}
info->mChrome = do_GetWeakReference(aChrome);
return NS_OK;
}
@@ -1542,38 +1506,18 @@ nsWindowWatcher::FindWindowEntry(mozIDOMWindowProxy* aWindow)
// find the corresponding nsWatcherWindowEntry
nsWatcherWindowEntry* info;
nsWatcherWindowEntry* listEnd;
#ifdef USEWEAKREFS
nsresult rv;
bool found;
#endif
info = mOldestWindow;
listEnd = 0;
#ifdef USEWEAKREFS
rv = NS_OK;
found = false;
while (info != listEnd && NS_SUCCEEDED(rv)) {
nsCOMPtr<mozIDOMWindowProxy> infoWindow(do_QueryReferent(info->mWindow));
if (!infoWindow) { // clean up dangling reference, while we're here
rv = RemoveWindow(info);
} else if (infoWindow.get() == aWindow) {
return info;
}
info = info->mYounger;
listEnd = mOldestWindow;
}
return 0;
#else
while (info != listEnd) {
if (info->mWindow == aWindow) {
nsCOMPtr<mozIDOMWindowProxy> window = do_QueryReferent(info->mWindow);
if (window && window == aWindow) {
return info;
}
info = info->mYounger;
listEnd = mOldestWindow;
}
return 0;
#endif
}
nsresult
@@ -1599,16 +1543,11 @@ nsWindowWatcher::RemoveWindow(nsWatcherWindowEntry* aInfo)
// send notifications.
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
if (os) {
#ifdef USEWEAKREFS
nsCOMPtr<nsISupports> domwin(do_QueryReferent(aInfo->mWindow));
if (domwin) {
os->NotifyObservers(domwin, "domwindowclosed", 0);
nsCOMPtr<mozIDOMWindowProxy> window = do_QueryReferent(aInfo->mWindow);
if (window) {
nsCOMPtr<nsISupports> domwin(do_QueryInterface(window));
os->NotifyObservers(domwin, "domwindowclosed", nullptr);
}
// else bummer. since the window is gone, there's nothing to notify with.
#else
nsCOMPtr<nsISupports> domwin(do_QueryInterface(aInfo->mWindow));
os->NotifyObservers(domwin, "domwindowclosed", 0);
#endif
}
delete aInfo;
@@ -1627,12 +1566,8 @@ nsWindowWatcher::GetChromeForWindow(mozIDOMWindowProxy* aWindow,
MutexAutoLock lock(mListLock);
nsWatcherWindowEntry* info = FindWindowEntry(aWindow);
if (info) {
if (info->mChromeWeak) {
return info->mChromeWeak->QueryReferent(
NS_GET_IID(nsIWebBrowserChrome), reinterpret_cast<void**>(aResult));
}
*aResult = info->mChrome;
NS_IF_ADDREF(*aResult);
nsCOMPtr<nsIWebBrowserChrome> chrome = do_QueryReferent(info->mChrome);
chrome.forget(aResult);
}
return NS_OK;
}
+1
View File
@@ -108,6 +108,7 @@ NS_INTERFACE_MAP_BEGIN(nsContentTreeOwner)
NS_INTERFACE_MAP_ENTRY(nsIWebBrowserChrome3)
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
NS_INTERFACE_MAP_ENTRY(nsIWindowProvider)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
// NOTE: This is using aggregation because there are some properties and
// method on nsIBaseWindow (which we implement) and on
// nsIEmbeddingSiteWindow (which we also implement) that have the same name.
+3 -1
View File
@@ -18,6 +18,7 @@
#include "nsIInterfaceRequestorUtils.h"
#include "nsIWebBrowserChrome3.h"
#include "nsIWindowProvider.h"
#include "nsWeakReference.h"
class nsXULWindow;
class nsSiteWindow;
@@ -26,7 +27,8 @@ class nsContentTreeOwner final : public nsIDocShellTreeOwner,
public nsIBaseWindow,
public nsIInterfaceRequestor,
public nsIWebBrowserChrome3,
public nsIWindowProvider
public nsIWindowProvider,
public nsSupportsWeakReference
{
friend class nsXULWindow;
friend class nsSiteWindow;