Bug 774633 - Factor nsWindowWatcher call to SetOpenerScriptPrincipal into a method on nsGlobalWindow. r=jst

This doesn't change any functionality in the code. Note that the name is currently
a bit of a misnomer, but we change that in the next patch when we rip out the
arguments.
This commit is contained in:
Bobby Holley 2012-08-23 16:44:52 -07:00
parent e42cad6359
commit 6e122ec16c
4 changed files with 42 additions and 22 deletions

View File

@ -1548,6 +1548,39 @@ nsGlobalWindow::SetOpenerScriptPrincipal(nsIPrincipal* aPrincipal)
}
}
void
nsGlobalWindow::SetInitialPrincipalToSubject(nsIDocShellTreeItem* aItem,
nsIDOMWindow* aParent)
{
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
MOZ_ASSERT(ssm);
nsresult rv;
nsCOMPtr<nsIPrincipal> newWindowPrincipal;
rv = ssm->GetSubjectPrincipal(getter_AddRefs(newWindowPrincipal));
MOZ_ASSERT(NS_SUCCEEDED(rv));
if (!newWindowPrincipal && aParent) {
nsCOMPtr<nsIScriptObjectPrincipal> sop(do_QueryInterface(aParent));
if (sop) {
newWindowPrincipal = sop->GetPrincipal();
}
}
bool isSystem;
rv = ssm->IsSystemPrincipal(newWindowPrincipal, &isSystem);
if (NS_FAILED(rv) || isSystem) {
// Don't pass this principal along to content windows
int32_t itemType;
rv = aItem->GetItemType(&itemType);
if (NS_FAILED(rv) || itemType != nsIDocShellTreeItem::typeChrome) {
newWindowPrincipal = nullptr;
}
}
SetOpenerScriptPrincipal(newWindowPrincipal);
}
nsIPrincipal*
nsGlobalWindow::GetOpenerScriptPrincipal()
{

View File

@ -343,6 +343,8 @@ public:
virtual NS_HIDDEN_(void) SetChromeEventHandler(nsIDOMEventTarget* aChromeEventHandler);
virtual NS_HIDDEN_(void) SetOpenerScriptPrincipal(nsIPrincipal* aPrincipal);
virtual NS_HIDDEN_(void) SetInitialPrincipalToSubject(nsIDocShellTreeItem* aItem,
nsIDOMWindow* aParent);
virtual NS_HIDDEN_(nsIPrincipal*) GetOpenerScriptPrincipal();
virtual NS_HIDDEN_(PopupControlState) PushPopupControlState(PopupControlState state, bool aForce) const;

View File

@ -24,6 +24,7 @@
class nsIIdleObserver;
class nsIPrincipal;
class nsIDocShellTreeItem; // XXX - Temporary! Goes away in the next patch
// Popup control state enum. The values in this enum must go from most
// permissive to least permissive so that it's safe to push state in
@ -48,8 +49,8 @@ class nsIArray;
class nsPIWindowRoot;
#define NS_PIDOMWINDOW_IID \
{0x66660102, 0xd875, 0x47e2, \
{0xa1, 0xf7, 0x12, 0xbc, 0x83, 0xc9, 0x93, 0xa9}}
{ 0x0c5763c6, 0x5e87, 0x4f6f, \
{ 0xa2, 0xef, 0xcf, 0x4d, 0xeb, 0xd1, 0xbc, 0xc3 } }
class nsPIDOMWindow : public nsIDOMWindowInternal
{
@ -286,6 +287,9 @@ public:
// preloaded into it when it was created, or created by
// CreateAboutBlankContentViewer()).
virtual void SetOpenerScriptPrincipal(nsIPrincipal* aPrincipal) = 0;
virtual void SetInitialPrincipalToSubject(nsIDocShellTreeItem* aItem,
nsIDOMWindow* aParent) = 0;
// Ask this window who opened it.
virtual nsIPrincipal* GetOpenerScriptPrincipal() = 0;

View File

@ -873,32 +873,13 @@ nsWindowWatcher::OpenWindowInternal(nsIDOMWindow *aParent,
// the JS stack, just use the principal of our parent window. In those
// cases we do _not_ set the parent window principal as the owner of the
// load--since we really don't know who the owner is, just leave it null.
nsIPrincipal* newWindowPrincipal = subjectPrincipal;
if (!newWindowPrincipal && aParent) {
nsCOMPtr<nsIScriptObjectPrincipal> sop(do_QueryInterface(aParent));
if (sop) {
newWindowPrincipal = sop->GetPrincipal();
}
}
bool isSystem;
rv = sm->IsSystemPrincipal(newWindowPrincipal, &isSystem);
if (NS_FAILED(rv) || isSystem) {
// Don't pass this principal along to content windows
int32_t itemType;
rv = newDocShellItem->GetItemType(&itemType);
if (NS_FAILED(rv) || itemType != nsIDocShellTreeItem::typeChrome) {
newWindowPrincipal = nullptr;
}
}
nsCOMPtr<nsPIDOMWindow> newWindow = do_QueryInterface(*_retval);
#ifdef DEBUG
nsCOMPtr<nsPIDOMWindow> newDebugWindow = do_GetInterface(newDocShell);
NS_ASSERTION(newWindow == newDebugWindow, "Different windows??");
#endif
if (newWindow) {
newWindow->SetOpenerScriptPrincipal(newWindowPrincipal);
newWindow->SetInitialPrincipalToSubject(newDocShellItem, aParent);
}
}