Bug 774633 - Move the call to SetInitialPrincipalToSubject into nsAppShellService::RegisterTopLevelWindow. r=jst
authorBobby Holley <bobbyholley@gmail.com>
Thu, 23 Aug 2012 16:44:52 -0700
changeset 105280 35334e8206322bbb4a591c02ee680c98ab5b2d8c
parent 105279 6bd07fd5147dcb6c133ff36fd468e43fdc544f0b
child 105281 d4d36e3b948f7b8527247433c2e1effb2de71016
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewersjst
bugs774633
milestone17.0a1
Bug 774633 - Move the call to SetInitialPrincipalToSubject into nsAppShellService::RegisterTopLevelWindow. r=jst This means that we get the correct principal on the window before sending notifying any consumers about the window's creation.
embedding/components/windowwatcher/src/nsWindowWatcher.cpp
xpfe/appshell/src/nsAppShellService.cpp
--- a/embedding/components/windowwatcher/src/nsWindowWatcher.cpp
+++ b/embedding/components/windowwatcher/src/nsWindowWatcher.cpp
@@ -873,16 +873,20 @@ nsWindowWatcher::OpenWindowInternal(nsID
     // 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.
     nsCOMPtr<nsPIDOMWindow> newWindow = do_QueryInterface(*_retval);
 #ifdef DEBUG
     nsCOMPtr<nsPIDOMWindow> newDebugWindow = do_GetInterface(newDocShell);
     NS_ASSERTION(newWindow == newDebugWindow, "Different windows??");
 #endif
+    // The principal of the initial about:blank document gets set up in
+    // nsWindowWatcher::AddWindow. Make sure to call it. In the common case
+    // this call already happened when the window was created, but
+    // SetInitialPrincipalToSubject is safe to call multiple times.
     if (newWindow) {
       newWindow->SetInitialPrincipalToSubject();
     }
   }
 
   if (uriToLoad && aNavigate) { // get the script principal and pass it to docshell
     JSContextAutoPopper contextGuard;
 
--- a/xpfe/appshell/src/nsAppShellService.cpp
+++ b/xpfe/appshell/src/nsAppShellService.cpp
@@ -14,16 +14,17 @@
 #include "nsIObserver.h"
 #include "nsIXPConnect.h"
 #include "nsIJSContextStack.h"
 
 #include "nsIWindowMediator.h"
 #include "nsIWindowWatcher.h"
 #include "nsPIWindowWatcher.h"
 #include "nsIDOMWindow.h"
+#include "nsPIDOMWindow.h"
 #include "nsWebShellWindow.h"
 
 #include "nsIEnumerator.h"
 #include "nsCRT.h"
 #include "prprf.h"    
 
 #include "nsWidgetsCID.h"
 #include "nsIRequestObserver.h"
@@ -445,37 +446,34 @@ nsAppShellService::GetApplicationProvide
 }
 
 /*
  * Register a new top level window (created elsewhere)
  */
 NS_IMETHODIMP
 nsAppShellService::RegisterTopLevelWindow(nsIXULWindow* aWindow)
 {
+  nsCOMPtr<nsIDocShell> docShell;
+  aWindow->GetDocShell(getter_AddRefs(docShell));
+  nsCOMPtr<nsPIDOMWindow> domWindow(do_GetInterface(docShell));
+  domWindow->SetInitialPrincipalToSubject();
+
   // tell the window mediator about the new window
   nsCOMPtr<nsIWindowMediator> mediator
     ( do_GetService(NS_WINDOWMEDIATOR_CONTRACTID) );
   NS_ASSERTION(mediator, "Couldn't get window mediator.");
 
   if (mediator)
     mediator->RegisterWindow(aWindow);
 
   // tell the window watcher about the new window
   nsCOMPtr<nsPIWindowWatcher> wwatcher ( do_GetService(NS_WINDOWWATCHER_CONTRACTID) );
   NS_ASSERTION(wwatcher, "No windowwatcher?");
-  if (wwatcher) {
-    nsCOMPtr<nsIDocShell> docShell;
-    aWindow->GetDocShell(getter_AddRefs(docShell));
-    NS_ASSERTION(docShell, "Window has no docshell");
-    if (docShell) {
-      nsCOMPtr<nsIDOMWindow> domWindow(do_GetInterface(docShell));
-      NS_ASSERTION(domWindow, "Couldn't get DOM window.");
-      if (domWindow)
-        wwatcher->AddWindow(domWindow, 0);
-    }
+  if (wwatcher && domWindow) {
+    wwatcher->AddWindow(domWindow, 0);
   }
 
   // an ongoing attempt to quit is stopped by a newly opened window
   nsCOMPtr<nsIObserverService> obssvc =
     do_GetService("@mozilla.org/observer-service;1");
   NS_ASSERTION(obssvc, "Couldn't get observer service.");
 
   if (obssvc)