Commit c2c40e3d authored by Kathleen Brade's avatar Kathleen Brade Committed by Georg Koppen
Browse files

Bug #5856: Do not expose physical screen info via window & window.screen.

parent 8f556ceb
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -2070,9 +2070,13 @@ nsDOMWindowUtils::GetViewId(nsIDOMElement* aElement, nsViewID* aResult)
NS_IMETHODIMP
nsDOMWindowUtils::GetScreenPixelsPerCSSPixel(float* aScreenPixels)
{
  nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
  NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
  return window->GetDevicePixelRatio(aScreenPixels);
  // We don't call nsGlobalWindow::GetDevicePixelRatio() in case it is
  // being spoofed to satisfy the "privacy.resistFingerprinting" pref.
  nsPresContext* presContext = GetPresContext();
  *aScreenPixels = !presContext ? 1.0 :
    (float(nsPresContext::AppUnitsPerCSSPixel())/
     presContext->AppUnitsPerDevPixel());
  return NS_OK;
}

NS_IMETHODIMP
+36 −0
Original line number Diff line number Diff line
@@ -4971,6 +4971,12 @@ nsGlobalWindow::GetOuterSize(ErrorResult& aError)
{
  MOZ_ASSERT(IsOuterWindow());

  if (!IsChrome()) {
    CSSIntSize size;
    aError = GetInnerSize(size);
    return nsIntSize(size.width, size.height);
  }

  nsCOMPtr<nsIBaseWindow> treeOwnerAsWin = GetTreeOwnerWindow();
  if (!treeOwnerAsWin) {
    aError.Throw(NS_ERROR_FAILURE);
@@ -5102,6 +5108,11 @@ nsGlobalWindow::GetScreenXY(ErrorResult& aError)
{
  MOZ_ASSERT(IsOuterWindow());

  // For non-chrome callers, always return (0,0) to prevent fingerprinting.
  if (!IsChrome()) {
    return nsIntPoint(0, 0);
  }

  nsCOMPtr<nsIBaseWindow> treeOwnerAsWin = GetTreeOwnerWindow();
  if (!treeOwnerAsWin) {
    aError.Throw(NS_ERROR_FAILURE);
@@ -5166,6 +5177,9 @@ nsGlobalWindow::GetMozInnerScreenX(ErrorResult& aError)
{
  FORWARD_TO_OUTER_OR_THROW(GetMozInnerScreenX, (aError), aError, 0);

  // For non-chrome callers, always return 0 to prevent fingerprinting.
  if (!IsChrome()) return 0.0;

  nsRect r = GetInnerScreenRect();
  return nsPresContext::AppUnitsToFloatCSSPixels(r.x);
}
@@ -5184,6 +5198,9 @@ nsGlobalWindow::GetMozInnerScreenY(ErrorResult& aError)
{
  FORWARD_TO_OUTER_OR_THROW(GetMozInnerScreenY, (aError), aError, 0);

  // For non-chrome callers, always return 0 to prevent fingerprinting.
  if (!IsChrome()) return 0.0;

  nsRect r = GetInnerScreenRect();
  return nsPresContext::AppUnitsToFloatCSSPixels(r.y);
}
@@ -5202,6 +5219,11 @@ nsGlobalWindow::GetDevicePixelRatio(ErrorResult& aError)
{
  FORWARD_TO_OUTER_OR_THROW(GetDevicePixelRatio, (aError), aError, 0.0);

  // For non-chrome callers, always return 1.0 to prevent fingerprinting.
  if (!IsChrome()) {
    return 1.0;
  }

  if (!mDocShell) {
    return 1.0;
  }
@@ -5507,6 +5529,20 @@ nsGlobalWindow::SetScreenY(int32_t aScreenY)
  return rv.ErrorCode();
}

bool
nsGlobalWindow::IsChrome() const
{
  bool isChrome = false;

  if (mDocShell) {
    nsRefPtr<nsPresContext> presContext;
    mDocShell->GetPresContext(getter_AddRefs(presContext));
    isChrome = (presContext && presContext->IsChrome());
  }

  return isChrome;
}

// NOTE: Arguments to this function should have values scaled to
// CSS pixels, not device pixels.
void
+2 −0
Original line number Diff line number Diff line
@@ -599,6 +599,8 @@ public:
    return mIsChrome;
  }

  bool IsChrome() const;

  using nsPIDOMWindow::IsModalContentWindow;
  static bool IsModalContentWindow(JSContext* aCx, JSObject* aGlobal);

+49 −0
Original line number Diff line number Diff line
@@ -67,6 +67,9 @@ NS_IMPL_RELEASE_INHERITED(nsScreen, DOMEventTargetHelper)
int32_t
nsScreen::GetPixelDepth(ErrorResult& aRv)
{
  // For non-chrome callers, always return 24 to prevent fingerprinting.
  if (!IsChrome()) return 24;

  nsDeviceContext* context = GetDeviceContext();

  if (!context) {
@@ -110,6 +113,9 @@ nsScreen::GetDeviceContext()
nsresult
nsScreen::GetRect(nsRect& aRect)
{
  // For non-chrome callers, return window inner rect to prevent fingerprinting.
  if (!IsChrome()) return GetWindowInnerRect(aRect);

  nsDeviceContext *context = GetDeviceContext();

  if (!context) {
@@ -129,6 +135,9 @@ nsScreen::GetRect(nsRect& aRect)
nsresult
nsScreen::GetAvailRect(nsRect& aRect)
{
  // For non-chrome callers, return window inner rect to prevent fingerprinting.
  if (!IsChrome()) return GetWindowInnerRect(aRect);

  nsDeviceContext *context = GetDeviceContext();

  if (!context) {
@@ -372,3 +381,43 @@ nsScreen::FullScreenEventListener::HandleEvent(nsIDOMEvent* aEvent)

  return NS_OK;
}

bool
nsScreen::IsChrome()
{
  nsCOMPtr<nsPIDOMWindow> owner = GetOwner();
  if (owner && owner->GetDocShell()) {
    return owner->GetDocShell()->ItemType() == nsIDocShellTreeItem::typeChrome;
  }
  return false;
}

nsresult
nsScreen::GetDOMWindow(nsIDOMWindow **aResult)
{
  NS_ENSURE_ARG_POINTER(aResult);
  *aResult = NULL;

  nsCOMPtr<nsPIDOMWindow> owner = GetOwner();
  if (!owner)
    return NS_ERROR_FAILURE;

  nsCOMPtr<nsIDOMWindow> win = do_QueryInterface(owner);
  NS_ENSURE_STATE(win);
  win.swap(*aResult);

  return NS_OK;
}

nsresult
nsScreen::GetWindowInnerRect(nsRect& aRect)
{
  aRect.x = 0;
  aRect.y = 0;
  nsCOMPtr<nsIDOMWindow> win;
  nsresult rv = GetDOMWindow(getter_AddRefs(win));
  NS_ENSURE_SUCCESS(rv, rv);
  rv = win->GetInnerWidth(&aRect.width);
  NS_ENSURE_SUCCESS(rv, rv);
  return win->GetInnerHeight(&aRect.height);
}
+3 −0
Original line number Diff line number Diff line
@@ -130,6 +130,9 @@ protected:
  nsDeviceContext* GetDeviceContext();
  nsresult GetRect(nsRect& aRect);
  nsresult GetAvailRect(nsRect& aRect);
  bool IsChrome();
  nsresult GetDOMWindow(nsIDOMWindow **aResult);
  nsresult GetWindowInnerRect(nsRect& aRect);

  mozilla::dom::ScreenOrientation mOrientation;