Bug 1362774 - make fingerprint resistance not resize window before onDOMContentLoaded and detect browser windows better, r?timhuang,bz draft
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Fri, 20 Jul 2018 17:32:30 +0100
changeset 821972 eb42d0318a35ed63442c7b18c74033a486f0b71b
parent 820804 4f12d77b4f9b6adaf06615c1c8cdc14de836dc1a
child 821973 29bdcc8f7ed3e301094dd0ec51491255fbb17664
push id117240
push userbmo:gijskruitbosch+bugs@gmail.com
push dateTue, 24 Jul 2018 13:13:26 +0000
reviewerstimhuang, bz
bugs1362774
milestone63.0a1
Bug 1362774 - make fingerprint resistance not resize window before onDOMContentLoaded and detect browser windows better, r?timhuang,bz This changes the fingerprint resistance code to set mIgnoreXULSize and mIgnoreXULSizeMode even when there isn't yet a primary content container, if this is a browser window. It also removes some dead code and reorders some other logic wrt mIgnoreXULSizeMode to make it more obvious what that code is doing, and renames the containing method to distinguish it more clearly from some other Load...Attributes methods. MozReview-Commit-ID: LJqRWeta6XV
xpfe/appshell/nsXULWindow.cpp
xpfe/appshell/nsXULWindow.h
--- a/xpfe/appshell/nsXULWindow.cpp
+++ b/xpfe/appshell/nsXULWindow.cpp
@@ -1106,19 +1106,16 @@ NS_IMETHODIMP nsXULWindow::ForceRoundedD
     &targetContentHeight
   );
 
   targetContentWidth = NSToIntRound(targetContentWidth * devicePerCSSPixels);
   targetContentHeight = NSToIntRound(targetContentHeight * devicePerCSSPixels);
 
   SetPrimaryContentSize(targetContentWidth, targetContentHeight);
 
-  mIgnoreXULSize = true;
-  mIgnoreXULSizeMode = true;
-
   return NS_OK;
 }
 
 void nsXULWindow::OnChromeLoaded()
 {
   nsresult rv = EnsureContentTreeOwner();
 
   if (NS_SUCCEEDED(rv)) {
@@ -1365,67 +1362,53 @@ nsXULWindow::SetSpecifiedSize(int32_t aS
     SetSize(aSpecWidth, aSpecHeight, false);
   }
 }
 
 /* Miscellaneous persistent attributes are attributes named in the
    |persist| attribute, other than size and position. Those are special
    because it's important to load those before one of the misc
    attributes (sizemode) and they require extra processing. */
-bool nsXULWindow::LoadMiscPersistentAttributesFromXUL()
+bool nsXULWindow::UpdateWindowStateFromMiscXULAttributes()
 {
   bool     gotState = false;
 
   /* There are no misc attributes of interest to the hidden window.
      It's especially important not to try to validate that window's
      size or position, because some platforms (Mac OS X) need to
      make it visible and offscreen. */
   if (mIsHiddenWindow)
     return false;
 
   nsCOMPtr<dom::Element> windowElement = GetWindowDOMElement();
   NS_ENSURE_TRUE(windowElement, false);
 
   nsAutoString stateString;
-
-  // sizemode
-  windowElement->GetAttribute(MODE_ATTRIBUTE, stateString);
   nsSizeMode sizeMode = nsSizeMode_Normal;
-  /* ignore request to minimize, to not confuse novices
-  if (stateString.Equals(SIZEMODE_MINIMIZED))
-    sizeMode = nsSizeMode_Minimized;
-  */
-  if (!mIgnoreXULSizeMode &&
-      (stateString.Equals(SIZEMODE_MAXIMIZED) || stateString.Equals(SIZEMODE_FULLSCREEN))) {
-    /* Honor request to maximize only if the window is sizable.
-       An unsizable, unmaximizable, yet maximized window confuses
-       Windows OS and is something of a travesty, anyway. */
-    if (mChromeFlags & nsIWebBrowserChrome::CHROME_WINDOW_RESIZE) {
-      mIntrinsicallySized = false;
-
-      if (stateString.Equals(SIZEMODE_MAXIMIZED))
-        sizeMode = nsSizeMode_Maximized;
-      else
-        sizeMode = nsSizeMode_Fullscreen;
-    }
-  }
-
-  // If we are told to ignore the size mode attribute update the
-  // document so the attribute and window are in sync.
+
+  // If we are told to ignore the size mode attribute, force
+  // normal sizemode.
   if (mIgnoreXULSizeMode) {
-    nsAutoString sizeString;
-    if (sizeMode == nsSizeMode_Maximized)
-      sizeString.Assign(SIZEMODE_MAXIMIZED);
-    else if (sizeMode == nsSizeMode_Fullscreen)
-      sizeString.Assign(SIZEMODE_FULLSCREEN);
-    else if (sizeMode == nsSizeMode_Normal)
-      sizeString.Assign(SIZEMODE_NORMAL);
-    if (!sizeString.IsEmpty()) {
-      ErrorResult rv;
-      windowElement->SetAttribute(MODE_ATTRIBUTE, sizeString, rv);
+    windowElement->SetAttribute(MODE_ATTRIBUTE, NS_LITERAL_STRING("normal"), IgnoreErrors());
+  } else {
+    // Otherwise, read sizemode from DOM and, if the window is resizable,
+    // set it later.
+    windowElement->GetAttribute(MODE_ATTRIBUTE, stateString);
+    if ((stateString.Equals(SIZEMODE_MAXIMIZED) || stateString.Equals(SIZEMODE_FULLSCREEN))) {
+      /* Honor request to maximize only if the window is sizable.
+         An unsizable, unmaximizable, yet maximized window confuses
+         Windows OS and is something of a travesty, anyway. */
+      if (mChromeFlags & nsIWebBrowserChrome::CHROME_WINDOW_RESIZE) {
+        mIntrinsicallySized = false;
+
+        if (stateString.Equals(SIZEMODE_MAXIMIZED))
+          sizeMode = nsSizeMode_Maximized;
+        else
+          sizeMode = nsSizeMode_Fullscreen;
+      }
     }
   }
 
   if (sizeMode == nsSizeMode_Fullscreen) {
     nsCOMPtr<mozIDOMWindowProxy> ourWindow;
     GetWindowDOMWindow(getter_AddRefs(ourWindow));
     auto* piWindow = nsPIDOMWindowOuter::From(ourWindow);
     piWindow->SetFullScreen(true);
@@ -2515,26 +2498,36 @@ nsXULWindow::LoadPersistentWindowState()
 void
 nsXULWindow::SizeShell()
 {
   AutoRestore<bool> sizingShellFromXUL(mSizingShellFromXUL);
   mSizingShellFromXUL = true;
 
   int32_t specWidth = -1, specHeight = -1;
   bool gotSize = false;
-  bool isContent = false;
-
-  GetHasPrimaryContent(&isContent);
+
+  nsCOMPtr<dom::Element> windowElement = GetWindowDOMElement();
+  nsAutoString windowType;
+  if (windowElement) {
+    windowElement->GetAttribute(WINDOWTYPE_ATTRIBUTE, windowType);
+  }
 
   CSSIntSize windowDiff = GetOuterToInnerSizeDifferenceInCSSPixels(mWindow);
 
-  // If this window has a primary content and fingerprinting resistance is
-  // enabled, we enforce this window to rounded dimensions.
-  if (isContent && nsContentUtils::ShouldResistFingerprinting()) {
-    ForceRoundedDimensions();
+  // If we're using fingerprint resistance, we're going to resize the window
+  // once we have primary content.
+  if (nsContentUtils::ShouldResistFingerprinting() &&
+      windowType.EqualsLiteral("navigator:browser")) {
+    // Once we've got primary content, force dimensions.
+    if (mPrimaryContentShell || mPrimaryTabParent) {
+      ForceRoundedDimensions();
+    }
+    // Always avoid setting size/sizemode on this window.
+    mIgnoreXULSize = true;
+    mIgnoreXULSizeMode = true;
   } else if (!mIgnoreXULSize) {
     gotSize = LoadSizeFromXUL(specWidth, specHeight);
     specWidth += windowDiff.width;
     specHeight += windowDiff.height;
   }
 
   bool positionSet = !mIgnoreXULPosition;
   nsCOMPtr<nsIXULWindow> parentWindow(do_QueryReferent(mParentWindow));
@@ -2580,17 +2573,17 @@ nsXULWindow::SizeShell()
   }
 
   // Now that we have set the window's final size, we can re-do its
   // positioning so that it is properly constrained to the screen.
   if (positionSet) {
     LoadPositionFromXUL(specWidth, specHeight);
   }
 
-  LoadMiscPersistentAttributesFromXUL();
+  UpdateWindowStateFromMiscXULAttributes();
 
   if (mChromeLoaded && mCenterAfterLoad && !positionSet &&
       mWindow->SizeMode() == nsSizeMode_Normal) {
     Center(parentWindow, parentWindow ? false : true, false);
   }
 }
 
 NS_IMETHODIMP nsXULWindow::GetXULBrowserWindow(nsIXULBrowserWindow * *aXULBrowserWindow)
--- a/xpfe/appshell/nsXULWindow.h
+++ b/xpfe/appshell/nsXULWindow.h
@@ -100,17 +100,17 @@ protected:
    void ApplyChromeFlags();
    void SizeShell();
    void OnChromeLoaded();
    void StaggerPosition(int32_t &aRequestedX, int32_t &aRequestedY,
                         int32_t aSpecWidth, int32_t aSpecHeight);
    bool       LoadPositionFromXUL(int32_t aSpecWidth, int32_t aSpecHeight);
    bool       LoadSizeFromXUL(int32_t& aSpecWidth, int32_t& aSpecHeight);
    void       SetSpecifiedSize(int32_t aSpecWidth, int32_t aSpecHeight);
-   bool       LoadMiscPersistentAttributesFromXUL();
+   bool       UpdateWindowStateFromMiscXULAttributes();
    void       SyncAttributesToWidget();
    NS_IMETHOD SavePersistentAttributes();
 
    bool NeedsTooltipListener();
    void AddTooltipSupport();
    void RemoveTooltipSupport();
 
    NS_IMETHOD GetWindowDOMWindow(mozIDOMWindowProxy** aDOMWindow);