From 76dfb3183c831dc675622229e12e893385b3f178 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Wed, 5 Sep 2012 11:32:06 -0700 Subject: [PATCH] Bug 774633 - Fold SetOpenerScriptPrincipal into SetInitialPrincipalToSubject, and make it indempotent and callable without an existing document. r=jst There's no reason it has to fail if there's no mDoc, since any document is promptly blown away with the new about:blank document. The indempotence is important because we want to be able to call this method unconditionally in OpenJSWindowInternal (since we may not have gone through RegisterTopLevelWindow) without worrying about whether we've called it already. --- dom/base/nsGlobalWindow.cpp | 72 ++++++++++++++++++------------------- dom/base/nsGlobalWindow.h | 1 - dom/base/nsPIDOMWindow.h | 8 ++--- 3 files changed, 36 insertions(+), 45 deletions(-) diff --git a/dom/base/nsGlobalWindow.cpp b/dom/base/nsGlobalWindow.cpp index 2c8871b6b2b1..7e7c2815cd05 100644 --- a/dom/base/nsGlobalWindow.cpp +++ b/dom/base/nsGlobalWindow.cpp @@ -1507,45 +1507,11 @@ nsGlobalWindow::WouldReuseInnerWindow(nsIDocument *aNewDocument) return false; } -void -nsGlobalWindow::SetOpenerScriptPrincipal(nsIPrincipal* aPrincipal) -{ - FORWARD_TO_OUTER_VOID(SetOpenerScriptPrincipal, (aPrincipal)); - - if (mDoc) { - if (!mDoc->IsInitialDocument()) { - // We have a document already, and it's not the original one. Bail out. - return; - } - -#ifdef DEBUG - // We better have an about:blank document loaded at this point. Otherwise, - // something is really weird. - nsCOMPtr uri; - mDoc->NodePrincipal()->GetURI(getter_AddRefs(uri)); - NS_ASSERTION(uri && NS_IsAboutBlank(uri) && - NS_IsAboutBlank(mDoc->GetDocumentURI()), - "Unexpected original document"); -#endif - - GetDocShell()->CreateAboutBlankContentViewer(aPrincipal); - mDoc->SetIsInitialDocument(true); - - nsCOMPtr shell; - GetDocShell()->GetPresShell(getter_AddRefs(shell)); - - if (shell && !shell->DidInitialReflow()) { - // Ensure that if someone plays with this document they will get - // layout happening. - nsRect r = shell->GetPresContext()->GetVisibleArea(); - shell->InitialReflow(r.width, r.height); - } - } -} - void nsGlobalWindow::SetInitialPrincipalToSubject() { + FORWARD_TO_OUTER_VOID(SetInitialPrincipalToSubject, ()); + // First, grab the subject principal. These methods never fail. nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager(); nsCOMPtr newWindowPrincipal, systemPrincipal; @@ -1566,8 +1532,38 @@ nsGlobalWindow::SetInitialPrincipalToSubject() } } - // Set the initial about:blank document up with the correct principal. - SetOpenerScriptPrincipal(newWindowPrincipal); + // If there's an existing document, bail if it either: + if (mDoc) { + // (a) is not an initial about:blank document, or + if (!mDoc->IsInitialDocument()) + return; + // (b) already has the correct principal. + if (mDoc->NodePrincipal() == newWindowPrincipal) + return; + +#ifdef DEBUG + // If we have a document loaded at this point, it had better be about:blank. + // Otherwise, something is really weird. + nsCOMPtr uri; + mDoc->NodePrincipal()->GetURI(getter_AddRefs(uri)); + NS_ASSERTION(uri && NS_IsAboutBlank(uri) && + NS_IsAboutBlank(mDoc->GetDocumentURI()), + "Unexpected original document"); +#endif + } + + GetDocShell()->CreateAboutBlankContentViewer(newWindowPrincipal); + mDoc->SetIsInitialDocument(true); + + nsCOMPtr shell; + GetDocShell()->GetPresShell(getter_AddRefs(shell)); + + if (shell && !shell->DidInitialReflow()) { + // Ensure that if someone plays with this document they will get + // layout happening. + nsRect r = shell->GetPresContext()->GetVisibleArea(); + shell->InitialReflow(r.width, r.height); + } } PopupControlState diff --git a/dom/base/nsGlobalWindow.h b/dom/base/nsGlobalWindow.h index 060e4d12561e..47e2d99ad65b 100644 --- a/dom/base/nsGlobalWindow.h +++ b/dom/base/nsGlobalWindow.h @@ -342,7 +342,6 @@ public: virtual NS_HIDDEN_(void) SetIsBackground(bool aIsBackground); virtual NS_HIDDEN_(void) SetChromeEventHandler(nsIDOMEventTarget* aChromeEventHandler); - virtual NS_HIDDEN_(void) SetOpenerScriptPrincipal(nsIPrincipal* aPrincipal); virtual NS_HIDDEN_(void) SetInitialPrincipalToSubject(); virtual NS_HIDDEN_(PopupControlState) PushPopupControlState(PopupControlState state, bool aForce) const; diff --git a/dom/base/nsPIDOMWindow.h b/dom/base/nsPIDOMWindow.h index 40345f956d61..ae484cf55a23 100644 --- a/dom/base/nsPIDOMWindow.h +++ b/dom/base/nsPIDOMWindow.h @@ -280,12 +280,8 @@ public: return win->mIsHandlingResizeEvent; } - // Tell this window who opened it. This only has an effect if there is - // either no document currently in the window or if the document is the - // original document this window came with (an about:blank document either - // preloaded into it when it was created, or created by - // CreateAboutBlankContentViewer()). - virtual void SetOpenerScriptPrincipal(nsIPrincipal* aPrincipal) = 0; + // Set the window up with an about:blank document with the current subject + // principal. virtual void SetInitialPrincipalToSubject() = 0; virtual PopupControlState PushPopupControlState(PopupControlState aState,