Verified Commit 1caba48f authored by ma1's avatar ma1 Committed by Pier Angelo Vendrame
Browse files

Bug 41631: Prevent weird initial window dimensions caused by subpixel computations

parent 2f67626c
Loading
Loading
Loading
Loading
+59 −43
Original line number Diff line number Diff line
@@ -22,16 +22,29 @@ const kPrefLetterboxingTesting =
  "privacy.resistFingerprinting.letterboxing.testing";
const kTopicDOMWindowOpened = "domwindowopened";

var logConsole;
function log(msg) {
  if (!logConsole) {
    logConsole = console.createInstance({
XPCOMUtils.defineLazyGetter(this, "logConsole", () =>
  console.createInstance({
    prefix: "RFPHelper.jsm",
    maxLogLevelPref: "privacy.resistFingerprinting.jsmloglevel",
    });
  })
);

function log(...args) {
  logConsole.log(...args);
}

  logConsole.log(msg);
function forEachWindow(callback) {
  const windowList = Services.wm.getEnumerator("navigator:browser");
  while (windowList.hasMoreElements()) {
    const win = windowList.getNext();
    if (win.gBrowser && !win.closed) {
      try {
        callback(win);
      } catch (e) {
        logConsole.error(e);
      }
    }
  }
}

class _RFPHelper {
@@ -158,7 +171,11 @@ class _RFPHelper {
  _handleResistFingerprintingChanged() {
    if (Services.prefs.getBoolPref(kPrefResistFingerprinting)) {
      this._addRFPObservers();
      Services.ww.registerNotification(this);
      forEachWindow(win => this._attachWindow(win));
    } else {
      forEachWindow(win => this._detachWindow(win));
      Services.ww.unregisterNotification(this);
      this._removeRFPObservers();
    }
  }
@@ -295,13 +312,11 @@ class _RFPHelper {
  }

  _handleLetterboxingPrefChanged() {
    if (Services.prefs.getBoolPref(kPrefLetterboxing, false)) {
      Services.ww.registerNotification(this);
      this._attachAllWindows();
    } else {
      this._detachAllWindows();
      Services.ww.unregisterNotification(this);
    }
    this.letterboxingEnabled = Services.prefs.getBoolPref(
      kPrefLetterboxing,
      false
    );
    forEachWindow(win => this._updateSizeForTabsInWindow(win));
  }

  // The function to parse the dimension set from the pref value. The pref value
@@ -402,11 +417,13 @@ class _RFPHelper {
    let logPrefix = `_roundContentSize[${Math.random()}]`;
    log(logPrefix);
    let win = aBrowser.ownerGlobal;

    let browserContainer = aBrowser
      .getTabBrowser()
      .getBrowserContainer(aBrowser);
    let browserParent = aBrowser.parentElement;
    browserParent.classList.remove("exclude-letterboxing");

    let [
      [contentWidth, contentHeight],
      [parentWidth, parentHeight],
@@ -419,6 +436,22 @@ class _RFPHelper {
      ])
    );

    if (!win._rfpSizeOffset) {
      const BASELINE_ROUNDING = 10;
      const offset = s =>
        s - Math.round(s / BASELINE_ROUNDING) * BASELINE_ROUNDING;

      win._rfpSizeOffset = {
        width: offset(parentWidth),
        height: offset(parentHeight),
      };
      log(
        `${logPrefix} Window size offsets %o (from %s, %s)`,
        win._rfpSizeOffset,
        parentWidth,
        parentHeight
      );
    }
    log(
      `${logPrefix} contentWidth=${contentWidth} contentHeight=${contentHeight} parentWidth=${parentWidth} parentHeight=${parentHeight} containerWidth=${containerWidth} containerHeight=${containerHeight}${
        isNewTab ? " (new tab)." : "."
@@ -437,6 +470,16 @@ class _RFPHelper {
      });

      let result;

      if (!this.letterboxingEnabled) {
        const offset = win._rfpSizeOffset;
        result = r(aWidth - offset.width, aHeight - offset.height);
        log(
          `${logPrefix} Letterboxing disabled, applying baseline rounding offsets: (${aWidth}, ${aHeight}) => ${result.width} x ${result.height})`
        );
        return result;
      }

      log(`${logPrefix} roundDimensions(${aWidth}, ${aHeight})`);
      // If the set is empty, we will round the content with the default
      // stepping size.
@@ -554,7 +597,6 @@ class _RFPHelper {

  _updateSizeForTabsInWindow(aWindow) {
    let tabBrowser = aWindow.gBrowser;

    tabBrowser.tabpanels?.classList.add("letterboxing");

    for (let tab of tabBrowser.tabs) {
@@ -585,19 +627,7 @@ class _RFPHelper {
    this._updateSizeForTabsInWindow(aWindow);
  }

  _attachAllWindows() {
    let windowList = Services.wm.getEnumerator("navigator:browser");

    while (windowList.hasMoreElements()) {
      let win = windowList.getNext();

      if (win.closed || !win.gBrowser) {
        continue;
      }

      this._attachWindow(win);
    }
  }

  _detachWindow(aWindow) {
    let tabBrowser = aWindow.gBrowser;
@@ -616,20 +646,6 @@ class _RFPHelper {
    }
  }

  _detachAllWindows() {
    let windowList = Services.wm.getEnumerator("navigator:browser");

    while (windowList.hasMoreElements()) {
      let win = windowList.getNext();

      if (win.closed || !win.gBrowser) {
        continue;
      }

      this._detachWindow(win);
    }
  }

  _handleDOMWindowOpened(win) {
    let self = this;