From 79032612b90a1c08f1c5d84e0dab2b1d5c70badc Mon Sep 17 00:00:00 2001 From: "jst%mozilla.org" Date: Sat, 13 Jan 2007 01:08:43 +0000 Subject: [PATCH] Fixing popup blocker bug 354973. r=mano@mozilla.com, sr=peterv@propagandism.org git-svn-id: svn://10.0.0.236/trunk@218261 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/browser/base/content/browser.js | 35 ++++++------------- .../src/browser/BrowserWindowController.mm | 14 +++----- .../events/src/nsDOMPopupBlockedEvent.cpp | 17 ++++----- .../idl/events/nsIDOMPopupBlockedEvent.idl | 15 ++++++-- mozilla/dom/src/base/nsGlobalWindow.cpp | 20 +++++------ mozilla/toolkit/content/widgets/browser.xml | 21 ++++++----- mozilla/widget/public/nsGUIEvent.h | 5 +-- 7 files changed, 57 insertions(+), 70 deletions(-) diff --git a/mozilla/browser/base/content/browser.js b/mozilla/browser/base/content/browser.js index 8e5cff3844e..2dc6ce80845 100644 --- a/mozilla/browser/base/content/browser.js +++ b/mozilla/browser/base/content/browser.js @@ -529,14 +529,12 @@ const gPopupBlockerObserver = { var label = bundle_browser.getFormattedString("popupShowPopupPrefix", [popupURIspec]); menuitem.setAttribute("label", label); - menuitem.setAttribute("requestingWindowURI", pageReport[i].requestingWindowURI.spec); menuitem.setAttribute("popupWindowURI", popupURIspec); menuitem.setAttribute("popupWindowFeatures", pageReport[i].popupWindowFeatures); -#ifndef MOZILLA_1_8_BRANCH -# bug 314700 menuitem.setAttribute("popupWindowName", pageReport[i].popupWindowName); -#endif menuitem.setAttribute("oncommand", "gPopupBlockerObserver.showBlockedPopup(event);"); + menuitem.requestingWindow = pageReport[i].requestingWindow; + menuitem.requestingDocument = pageReport[i].requestingDocument; aEvent.target.appendChild(menuitem); } } @@ -561,30 +559,17 @@ const gPopupBlockerObserver = { showBlockedPopup: function (aEvent) { - var requestingWindow = aEvent.target.getAttribute("requestingWindowURI"); - var requestingWindowURI = - Components.classes["@mozilla.org/network/io-service;1"] - .getService(Components.interfaces.nsIIOService) - .newURI(requestingWindow, null, null); + var target = aEvent.target; + var popupWindowURI = target.getAttribute("popupWindowURI"); + var features = target.getAttribute("popupWindowFeatures"); + var name = target.getAttribute("popupWindowName"); - var popupWindowURI = aEvent.target.getAttribute("popupWindowURI"); - var features = aEvent.target.getAttribute("popupWindowFeatures"); -#ifndef MOZILLA_1_8_BRANCH -# bug 314700 - var name = aEvent.target.getAttribute("popupWindowName"); -#endif + var dwi = target.requestingWindow; - var shell = findChildShell(null, gBrowser.selectedBrowser.docShell, - requestingWindowURI); - if (shell) { - var ifr = shell.QueryInterface(Components.interfaces.nsIInterfaceRequestor); - var dwi = ifr.getInterface(Components.interfaces.nsIDOMWindowInternal); -#ifdef MOZILLA_1_8_BRANCH -# bug 314700 - dwi.open(popupWindowURI, "", features); -#else + // If we have a requesting window and the requesting document is + // still the current document, open the popup. + if (dwi && dwi.document == target.requestingDocument) { dwi.open(popupWindowURI, name, features); -#endif } }, diff --git a/mozilla/camino/src/browser/BrowserWindowController.mm b/mozilla/camino/src/browser/BrowserWindowController.mm index 1bba2dbe0d9..ab13df26881 100644 --- a/mozilla/camino/src/browser/BrowserWindowController.mm +++ b/mozilla/camino/src/browser/BrowserWindowController.mm @@ -1912,9 +1912,10 @@ enum BWCOpenDest { - (void)whitelistAndShowPopup:(nsIDOMPopupBlockedEvent*)aPopupBlockedEvent { + nsCOMPtr requestingWindow; + aPopupBlockedEvent->GetRequestingWindow(getter_AddRefs(requestingWindow)); // get the URIs for the popup window, and it's parent document - nsCOMPtr requestingWindowURI, popupWindowURI; - aPopupBlockedEvent->GetRequestingWindowURI(getter_AddRefs(requestingWindowURI)); + nsCOMPtr popupWindowURI; aPopupBlockedEvent->GetPopupWindowURI(getter_AddRefs(popupWindowURI)); nsAutoString windowName, features; @@ -1930,15 +1931,10 @@ enum BWCOpenDest { #endif // find the docshell for the blocked popup window, in order to show it - nsCOMPtr popupWinDocShell = [[mBrowserView getBrowserView] findDocShellForURI:requestingWindowURI]; - if (!popupWinDocShell) + if (!requestingWindow) return; - nsCOMPtr domWin = do_GetInterface(popupWinDocShell); - if (!domWin) - return; - - nsCOMPtr piDomWin = do_QueryInterface(domWin); + nsCOMPtr piDomWin = do_QueryInterface(requestingWindow); if (!piDomWin) return; diff --git a/mozilla/content/events/src/nsDOMPopupBlockedEvent.cpp b/mozilla/content/events/src/nsDOMPopupBlockedEvent.cpp index 8f2e89a6e7b..1a986cad26e 100644 --- a/mozilla/content/events/src/nsDOMPopupBlockedEvent.cpp +++ b/mozilla/content/events/src/nsDOMPopupBlockedEvent.cpp @@ -62,7 +62,6 @@ nsDOMPopupBlockedEvent::~nsDOMPopupBlockedEvent() if (mEventIsInternal) { if (mEvent->eventStructType == NS_POPUPBLOCKED_EVENT) { nsPopupBlockedEvent* event = NS_STATIC_CAST(nsPopupBlockedEvent*, mEvent); - NS_IF_RELEASE(event->mRequestingWindowURI); NS_IF_RELEASE(event->mPopupWindowURI); } } @@ -79,7 +78,7 @@ NS_INTERFACE_MAP_END_INHERITING(nsDOMEvent) NS_IMETHODIMP nsDOMPopupBlockedEvent::InitPopupBlockedEvent(const nsAString & aTypeArg, PRBool aCanBubbleArg, PRBool aCancelableArg, - nsIURI *aRequestingWindowURI, + nsIDOMWindow *aRequestingWindow, nsIURI *aPopupWindowURI, const nsAString & aPopupWindowName, const nsAString & aPopupWindowFeatures) @@ -92,9 +91,8 @@ nsDOMPopupBlockedEvent::InitPopupBlockedEvent(const nsAString & aTypeArg, case NS_POPUPBLOCKED_EVENT: { nsPopupBlockedEvent* event = NS_STATIC_CAST(nsPopupBlockedEvent*, mEvent); - event->mRequestingWindowURI = aRequestingWindowURI; + event->mRequestingWindow = do_GetWeakReference(aRequestingWindow); event->mPopupWindowURI = aPopupWindowURI; - NS_IF_ADDREF(event->mRequestingWindowURI); NS_IF_ADDREF(event->mPopupWindowURI); event->mPopupWindowFeatures = aPopupWindowFeatures; event->mPopupWindowName = aPopupWindowName; @@ -108,16 +106,15 @@ nsDOMPopupBlockedEvent::InitPopupBlockedEvent(const nsAString & aTypeArg, } NS_IMETHODIMP -nsDOMPopupBlockedEvent::GetRequestingWindowURI(nsIURI **aRequestingWindowURI) +nsDOMPopupBlockedEvent::GetRequestingWindow(nsIDOMWindow **aRequestingWindow) { - NS_ENSURE_ARG_POINTER(aRequestingWindowURI); if (mEvent->eventStructType == NS_POPUPBLOCKED_EVENT) { nsPopupBlockedEvent* event = NS_STATIC_CAST(nsPopupBlockedEvent*, mEvent); - *aRequestingWindowURI = event->mRequestingWindowURI; - NS_IF_ADDREF(*aRequestingWindowURI); - return NS_OK; + CallQueryReferent(event->mRequestingWindow.get(), aRequestingWindow); + } else { + *aRequestingWindow = 0; } - *aRequestingWindowURI = 0; + return NS_OK; // Don't throw an exception } diff --git a/mozilla/dom/public/idl/events/nsIDOMPopupBlockedEvent.idl b/mozilla/dom/public/idl/events/nsIDOMPopupBlockedEvent.idl index f6c256f9680..2962f7eabde 100644 --- a/mozilla/dom/public/idl/events/nsIDOMPopupBlockedEvent.idl +++ b/mozilla/dom/public/idl/events/nsIDOMPopupBlockedEvent.idl @@ -44,10 +44,19 @@ interface nsIURI; * posted when a popup window is blocked. */ -[scriptable, uuid(bb57e9bb-0c89-459c-977d-5cbc1831bdf5)] +[scriptable, uuid(05be571f-c3ea-4959-a340-c57b1591ae4b)] interface nsIDOMPopupBlockedEvent : nsIDOMEvent { - readonly attribute nsIURI requestingWindowURI; + /** + * The window object that attempted to open the blocked popup + * (i.e. the window object on which open() was called). + */ + readonly attribute nsIDOMWindow requestingWindow; + + + /** + * The URI of the window that was blocked. + */ readonly attribute nsIURI popupWindowURI; /** @@ -65,7 +74,7 @@ interface nsIDOMPopupBlockedEvent : nsIDOMEvent void initPopupBlockedEvent(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, - in nsIURI requestingWindowURI, + in nsIDOMWindow requestingWindow, in nsIURI popupWindowURI, in DOMString popupWindowName, in DOMString popupWindowFeatures); diff --git a/mozilla/dom/src/base/nsGlobalWindow.cpp b/mozilla/dom/src/base/nsGlobalWindow.cpp index a7828529aea..e428dddeacd 100644 --- a/mozilla/dom/src/base/nsGlobalWindow.cpp +++ b/mozilla/dom/src/base/nsGlobalWindow.cpp @@ -4235,19 +4235,23 @@ PRBool IsPopupBlocked(nsIDOMDocument* aDoc) static void FirePopupBlockedEvent(nsIDOMDocument* aDoc, - nsIURI *aRequestingURI, nsIURI *aPopupURI, + nsIDOMWindow *aRequestingWindow, nsIURI *aPopupURI, const nsAString &aPopupWindowName, const nsAString &aPopupWindowFeatures) { if (aDoc) { - // Fire a "DOMPopupBlocked" event so that the UI can hear about blocked popups. + // Fire a "DOMPopupBlocked" event so that the UI can hear about + // blocked popups. nsCOMPtr docEvent(do_QueryInterface(aDoc)); nsCOMPtr event; - docEvent->CreateEvent(NS_LITERAL_STRING("PopupBlockedEvents"), getter_AddRefs(event)); + docEvent->CreateEvent(NS_LITERAL_STRING("PopupBlockedEvents"), + getter_AddRefs(event)); if (event) { nsCOMPtr pbev(do_QueryInterface(event)); pbev->InitPopupBlockedEvent(NS_LITERAL_STRING("DOMPopupBlocked"), - PR_TRUE, PR_TRUE, aRequestingURI, aPopupURI, aPopupWindowName, aPopupWindowFeatures); + PR_TRUE, PR_TRUE, aRequestingWindow, + aPopupURI, aPopupWindowName, + aPopupWindowFeatures); nsCOMPtr privateEvent(do_QueryInterface(event)); privateEvent->SetTrusted(PR_TRUE); @@ -4373,12 +4377,7 @@ nsGlobalWindow::FireAbuseEvents(PRBool aBlocked, PRBool aWindow, nsCOMPtr topDoc; topWindow->GetDocument(getter_AddRefs(topDoc)); - nsCOMPtr requestingURI; nsCOMPtr popupURI; - nsCOMPtr webNav = - do_GetInterface((nsIScriptGlobalObject *)this); - if (webNav) - webNav->GetCurrentURI(getter_AddRefs(requestingURI)); // build the URI of the would-have-been popup window // (see nsWindowWatcher::URIfromURL) @@ -4416,7 +4415,8 @@ nsGlobalWindow::FireAbuseEvents(PRBool aBlocked, PRBool aWindow, // fire an event chock full of informative URIs if (aBlocked) - FirePopupBlockedEvent(topDoc, requestingURI, popupURI, aPopupWindowName, aPopupWindowFeatures); + FirePopupBlockedEvent(topDoc, this, popupURI, aPopupWindowName, + aPopupWindowFeatures); if (aWindow) FirePopupWindowEvent(topDoc); } diff --git a/mozilla/toolkit/content/widgets/browser.xml b/mozilla/toolkit/content/widgets/browser.xml index d42791ebc85..5cde1747740 100644 --- a/mozilla/toolkit/content/widgets/browser.xml +++ b/mozilla/toolkit/content/widgets/browser.xml @@ -424,8 +424,10 @@ if (this.pageReport) { var i = 0; while (i < this.pageReport.length) { - if (this.findChildShell(this.docShell, - this.pageReport[i].requestingWindowURI)) + // Filter out irrelevant reports. + if (this.pageReport[i].requestingWindow && + (this.pageReport[i].requestingWindow.document == + this.pageReport[i].requestingDocument)) i++; else this.pageReport.splice(i, 1); @@ -481,20 +483,17 @@ this.pageReport = new Array(); } -#ifdef MOZILLA_1_8_BRANCH -# bug 314700 - var obj = { requestingWindowURI: evt.requestingWindowURI, - popupWindowURI: evt.popupWindowURI, - popupWindowFeatures: evt.popupWindowFeatures }; -#else - var obj = { requestingWindowURI: evt.requestingWindowURI, + var obj = { requestingWindow: evt.requestingWindow, + // Record the current document in the requesting window + // before it can change. + requestingDocument: evt.requestingWindow.document, popupWindowURI: evt.popupWindowURI, popupWindowFeatures: evt.popupWindowFeatures, popupWindowName: evt.popupWindowName }; -#endif + this.pageReport.push(obj); this.pageReport.reported = false; - this.updatePageReport() + this.updatePageReport(); ]]> diff --git a/mozilla/widget/public/nsGUIEvent.h b/mozilla/widget/public/nsGUIEvent.h index 804fca97e38..23d13234450 100644 --- a/mozilla/widget/public/nsGUIEvent.h +++ b/mozilla/widget/public/nsGUIEvent.h @@ -54,6 +54,7 @@ #include "nsCOMPtr.h" #include "nsIAtom.h" #include "nsIDOMKeyEvent.h" +#include "nsWeakPtr.h" class nsIRenderingContext; class nsIRegion; @@ -885,11 +886,11 @@ class nsPopupBlockedEvent : public nsEvent public: nsPopupBlockedEvent(PRBool isTrusted, PRUint32 msg) : nsEvent(isTrusted, msg, NS_POPUPBLOCKED_EVENT), - mRequestingWindowURI(nsnull), mPopupWindowURI(nsnull) + mPopupWindowURI(nsnull) { } - nsIURI* mRequestingWindowURI; // owning reference + nsWeakPtr mRequestingWindow; nsIURI* mPopupWindowURI; // owning reference nsString mPopupWindowFeatures; nsString mPopupWindowName;