Bug 1444525: Extract the logic to see if we're a top-level chrome window. r?bz draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Sat, 10 Mar 2018 02:42:13 +0100
changeset 765782 4114a9737082370e6388629eaa7b39f74ae60622
parent 765781 c0d8ae1950da571ea2bc8c5c385badae3ba07170
child 765783 c7ddfe43244bb903dda762ba8276f285a88264fe
push id102161
push userbmo:emilio@crisal.io
push dateSat, 10 Mar 2018 07:39:34 +0000
reviewersbz
bugs1444525
milestone60.0a1
Bug 1444525: Extract the logic to see if we're a top-level chrome window. r?bz MozReview-Commit-ID: C9x7AwYR99n
dom/xul/XULDocument.cpp
dom/xul/XULDocument.h
--- a/dom/xul/XULDocument.cpp
+++ b/dom/xul/XULDocument.cpp
@@ -2626,16 +2626,37 @@ XULDocument::ResumeWalk()
 
     mStillWalking = false;
     if (mPendingSheets == 0) {
         rv = DoneWalking();
     }
     return rv;
 }
 
+already_AddRefed<nsIXULWindow>
+XULDocument::GetXULWindowIfToplevelChrome() const
+{
+    nsCOMPtr<nsIDocShellTreeItem> item = GetDocShell();
+    if (!item) {
+        return nullptr;
+    }
+    nsCOMPtr<nsIDocShellTreeOwner> owner;
+    item->GetTreeOwner(getter_AddRefs(owner));
+    nsCOMPtr<nsIXULWindow> xulWin = do_GetInterface(owner);
+    if (!xulWin) {
+        return nullptr;
+    }
+    nsCOMPtr<nsIDocShell> xulWinShell;
+    xulWin->GetDocShell(getter_AddRefs(xulWinShell));
+    if (!SameCOMIdentity(xulWinShell, item)) {
+        return nullptr;
+    }
+    return xulWin.forget();
+}
+
 nsresult
 XULDocument::DoneWalking()
 {
     NS_PRECONDITION(mPendingSheets == 0, "there are sheets to be loaded");
     NS_PRECONDITION(!mStillWalking, "walk not done");
 
     // XXXldb This is where we should really be setting the chromehidden
     // attribute.
@@ -2667,27 +2688,19 @@ XULDocument::DoneWalking()
             static_cast<nsIDocument*>(this),
             NS_LITERAL_STRING("MozBeforeInitialXULLayout"),
             true,
             false);
 
         // Before starting layout, check whether we're a toplevel chrome
         // window.  If we are, setup some state so that we don't have to restyle
         // the whole tree after StartLayout.
-        if (nsCOMPtr<nsIDocShellTreeItem> item = GetDocShell()) {
-            nsCOMPtr<nsIDocShellTreeOwner> owner;
-            item->GetTreeOwner(getter_AddRefs(owner));
-            if (nsCOMPtr<nsIXULWindow> xulWin = do_GetInterface(owner)) {
-                nsCOMPtr<nsIDocShell> xulWinShell;
-                xulWin->GetDocShell(getter_AddRefs(xulWinShell));
-                if (SameCOMIdentity(xulWinShell, item)) {
-                    // We're the chrome document!
-                    xulWin->BeforeStartLayout();
-                }
-            }
+        if (nsCOMPtr<nsIXULWindow> win = GetXULWindowIfToplevelChrome()) {
+            // We're the chrome document!
+            win->BeforeStartLayout();
         }
 
         StartLayout();
 
         if (mIsWritingFastLoad && IsChromeURI(mDocumentURI))
             nsXULPrototypeCache::GetInstance()->WritePrototype(mMasterPrototype);
 
         NS_ASSERTION(mDelayFrameLoaderInitialization,
--- a/dom/xul/XULDocument.h
+++ b/dom/xul/XULDocument.h
@@ -194,16 +194,20 @@ public:
                  ErrorResult& aRv);
     using nsDocument::GetBoxObjectFor;
     void LoadOverlay(const nsAString& aURL, nsIObserver* aObserver,
                      ErrorResult& aRv);
 
 protected:
     virtual ~XULDocument();
 
+    // Returns the associated XUL window if this is a top-level chrome document,
+    // null otherwise.
+    already_AddRefed<nsIXULWindow> GetXULWindowIfToplevelChrome() const;
+
     // Implementation methods
     friend nsresult
     (::NS_NewXULDocument(nsIDocument** aResult));
 
     nsresult Init(void) override;
     nsresult StartLayout(void);
 
     nsresult GetViewportSize(int32_t* aWidth, int32_t* aHeight);