commit fa996469071a1442386f0313b278cf07ccb4d14c Author: Arthur Edelstein arthuredelstein@gmail.com Date: Wed Oct 12 02:28:31 2016 +0000
Bug 19459: Size new windows to 1000x1000 or nearest 200x100 --- browser/base/content/browser.js | 10 +++- xpfe/appshell/nsXULWindow.cpp | 108 ++++++++++++++++++++++++++++++++++------ xpfe/appshell/nsXULWindow.h | 2 + 3 files changed, 102 insertions(+), 18 deletions(-)
diff --git a/browser/base/content/browser.js b/browser/base/content/browser.js index 41d72ae..e5fb01c 100644 --- a/browser/base/content/browser.js +++ b/browser/base/content/browser.js @@ -999,8 +999,14 @@ var gBrowserInit = { // have been initialized. Services.obs.notifyObservers(window, "browser-window-before-show", "");
- // Set a sane starting width/height for all resolutions on new profiles. - if (!document.documentElement.hasAttribute("width")) { + if (gPrefService.getBoolPref("privacy.resistFingerprinting")) { + // With fingerprinting resistance enabled, code elsewhere + // is going to generate rounded window dimensions. Make sure + // we don't have a maximized window that can interfere with + // that, as observed in http://trac.torproject.org/18175 + document.documentElement.setAttribute("sizemode", "normal"); + } else if (!document.documentElement.hasAttribute("width")) { + // Set a sane starting width/height for all resolutions on new profiles. let defaultWidth; let defaultHeight;
diff --git a/xpfe/appshell/nsXULWindow.cpp b/xpfe/appshell/nsXULWindow.cpp index 99289a0..c523e31 100644 --- a/xpfe/appshell/nsXULWindow.cpp +++ b/xpfe/appshell/nsXULWindow.cpp @@ -1007,7 +1007,70 @@ NS_IMETHODIMP nsXULWindow::EnsureAuthPrompter() } return mAuthPrompter ? NS_OK : NS_ERROR_FAILURE; } - + +// Rounds window size to 1000x1000, or, if there isn't enough available +// screen space, to a multiple of 200x100. +NS_IMETHODIMP nsXULWindow::ResizeToRoundedDimensions() +{ + NS_ENSURE_STATE(mPrimaryContentShell); + nsCOMPtr<nsIBaseWindow> shellWindow(do_QueryInterface(mPrimaryContentShell)); + NS_ENSURE_STATE(shellWindow); + int32_t windowWidth, windowHeight; + int32_t availWidthCSS, availHeightCSS; + int32_t contentWidth, contentHeight; + double devicePerCSSPixels = 1.0; + shellWindow->GetUnscaledDevicePixelsPerCSSPixel(&devicePerCSSPixels); + GetSize(&windowWidth, &windowHeight); // device pixels + NS_ENSURE_TRUE(GetAvailScreenSize(&availWidthCSS, &availHeightCSS), + NS_ERROR_FAILURE); + int32_t availWidth = NSToIntRound(devicePerCSSPixels * + availWidthCSS); // device pixels + int32_t availHeight = NSToIntRound(devicePerCSSPixels * + availHeightCSS); // device pixels + shellWindow->GetSize(&contentWidth, &contentHeight); // device pixels + // Useful for debugging: + //printf("\nscaling factor: %f\n", devicePerCSSPixels); + //printf("window size: %d x %d\n", windowWidth, windowHeight); + //printf("avail screen size: %d x %d\n", availWidth, availHeight); + //printf("primary content shell: %d x %d\n", contentWidth, contentHeight); + int32_t chromeWidth = windowWidth - contentWidth; + int32_t chromeHeight = windowHeight - contentHeight; + int maxInnerWidth = Preferences::GetInt("privacy.window.maxInnerWidth", + INT_MAX); + int maxInnerHeight = Preferences::GetInt("privacy.window.maxInnerHeight", + INT_MAX); + int32_t availForContentWidthCSS = + std::min(maxInnerWidth, + NSToIntRound((0.95 * availWidth - chromeWidth) / + devicePerCSSPixels)); + int32_t availForContentHeightCSS = + std::min(maxInnerHeight, + NSToIntRound((0.95 * availHeight - chromeHeight) / + devicePerCSSPixels)); + int32_t targetContentWidth = + NSToIntRound(devicePerCSSPixels * + std::min(1000, availForContentWidthCSS - + (availForContentWidthCSS % 200))); + int32_t targetContentHeight = + NSToIntRound(devicePerCSSPixels * + std::min(1000, availForContentHeightCSS - + (availForContentHeightCSS % 100))); + SizeShellTo(mPrimaryContentShell, + targetContentWidth, targetContentHeight); + mIgnoreXULSize = true; + mIgnoreXULSizeMode = true; + // Useful for debugging: + //printf("target content size: %d, %d\n", + // targetContentWidth, targetContentHeight); + //GetSize(&windowWidth, &windowHeight); + //GetAvailScreenSize(&availWidth, &availHeight); + //shellWindow->GetSize(&contentWidth, &contentHeight); // device pixels + //printf("\nwindow size: %d x %d\n", windowWidth, windowHeight); + //printf("avail screen size: %d x %d\n", availWidth, availHeight); + //printf("primary content shell: %d x %d\n", contentWidth, contentHeight); + return NS_OK; +} + void nsXULWindow::OnChromeLoaded() { nsresult rv = EnsureContentTreeOwner(); @@ -1037,6 +1100,13 @@ void nsXULWindow::OnChromeLoaded() } }
+ // If we have a content browser object, and fingerprinting resistance + // is active, then set to a rounded size. + if (mPrimaryContentShell && + Preferences::GetBool("privacy.resistFingerprinting", false)) { + ResizeToRoundedDimensions(); + } + bool positionSet = !mIgnoreXULPosition; nsCOMPtr<nsIXULWindow> parentWindow(do_QueryReferent(mParentWindow)); #if defined(XP_UNIX) && !defined(XP_MACOSX) @@ -1137,6 +1207,21 @@ bool nsXULWindow::LoadPositionFromXUL() return gotPosition; }
+bool nsXULWindow::GetAvailScreenSize(int32_t* availWidth, int32_t* availHeight) +{ + nsCOMPtr<nsIDOMWindow> domWindow; + GetWindowDOMWindow(getter_AddRefs(domWindow)); + if (nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(domWindow)) { + nsCOMPtr<nsIDOMScreen> screen = window->GetScreen(); + if (screen) { + screen->GetAvailWidth(availWidth); + screen->GetAvailHeight(availHeight); + return true; + } + } + return false; +} + bool nsXULWindow::LoadSizeFromXUL() { bool gotSize = false; @@ -1181,21 +1266,12 @@ bool nsXULWindow::LoadSizeFromXUL() }
if (gotSize) { - // constrain to screen size - nsCOMPtr<nsIDOMWindow> domWindow; - GetWindowDOMWindow(getter_AddRefs(domWindow)); - if (nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(domWindow)) { - nsCOMPtr<nsIDOMScreen> screen = window->GetScreen(); - if (screen) { - int32_t screenWidth; - int32_t screenHeight; - screen->GetAvailWidth(&screenWidth); - screen->GetAvailHeight(&screenHeight); - if (specWidth > screenWidth) - specWidth = screenWidth; - if (specHeight > screenHeight) - specHeight = screenHeight; - } + int32_t screenWidth, screenHeight; + if (GetAvailScreenSize(&screenWidth, &screenHeight)) { + if (specWidth > screenWidth) + specWidth = screenWidth; + if (specHeight > screenHeight) + specHeight = screenHeight; }
mIntrinsicallySized = false; diff --git a/xpfe/appshell/nsXULWindow.h b/xpfe/appshell/nsXULWindow.h index 882e614..f959e11 100644 --- a/xpfe/appshell/nsXULWindow.h +++ b/xpfe/appshell/nsXULWindow.h @@ -88,6 +88,7 @@ protected: NS_IMETHOD EnsurePrimaryContentTreeOwner(); NS_IMETHOD EnsurePrompter(); NS_IMETHOD EnsureAuthPrompter(); + NS_IMETHOD ResizeToRoundedDimensions();
void OnChromeLoaded(); void StaggerPosition(int32_t &aRequestedX, int32_t &aRequestedY, @@ -120,6 +121,7 @@ protected: void SetContentScrollbarVisibility(bool aVisible); bool GetContentScrollbarVisibility(); void PersistentAttributesDirty(uint32_t aDirtyFlags); + bool GetAvailScreenSize(int32_t* availWidth, int32_t* availHeight);
nsChromeTreeOwner* mChromeTreeOwner; nsContentTreeOwner* mContentTreeOwner;
tbb-commits@lists.torproject.org