Skip to content
Permalink
Browse files
Bug 19459: Size new windows to 1000x1000 or nearest 200x100
  • Loading branch information
arthuredelstein committed Nov 1, 2016
1 parent 08136fa commit a30069f4259fbfddec414a405cd3ff78e30a9e2c
Showing with 102 additions and 18 deletions.
  1. +8 −2 browser/base/content/browser.js
  2. +92 −16 xpfe/appshell/nsXULWindow.cpp
  3. +2 −0 xpfe/appshell/nsXULWindow.h
@@ -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;

@@ -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;
@@ -88,6 +88,7 @@ friend class nsContentTreeOwner;
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 @@ friend class nsContentTreeOwner;
void SetContentScrollbarVisibility(bool aVisible);
bool GetContentScrollbarVisibility();
void PersistentAttributesDirty(uint32_t aDirtyFlags);
bool GetAvailScreenSize(int32_t* availWidth, int32_t* availHeight);

nsChromeTreeOwner* mChromeTreeOwner;
nsContentTreeOwner* mContentTreeOwner;

0 comments on commit a30069f

Please sign in to comment.