Bug 1369319 - Part 1: Block device sensor events when 'privacy.resistFingerprinting' is true. r?bz,arthuredelstein draft
authorTim Huang <tihuang@mozilla.com>
Tue, 20 Jun 2017 11:10:06 +0800
changeset 600613 13c78a04352dbb6841bff338f6e3769c6e2b4a0e
parent 596561 d39cd452b52bf82fa4a717172a62d62ab9e5366f
child 600614 34d67d08051bb868b59d73d12c14bf1a300585e4
push id65805
push userbmo:tihuang@mozilla.com
push dateTue, 27 Jun 2017 16:26:06 +0000
reviewersbz, arthuredelstein
bugs1369319
milestone56.0a1
Bug 1369319 - Part 1: Block device sensor events when 'privacy.resistFingerprinting' is true. r?bz,arthuredelstein This patch adds a nsDeviceSensors::IsSensorEventBlocked() function to check whether device sensor events should be blocked and replaces original 'device.sensors.enabled' check with this function. This function will not only check 'privacy.resistFingerprinting' but also check 'device.sensors.enabled'. MozReview-Commit-ID: 7NZiBHWN6y6
dom/system/nsDeviceSensors.cpp
dom/system/nsDeviceSensors.h
--- a/dom/system/nsDeviceSensors.cpp
+++ b/dom/system/nsDeviceSensors.cpp
@@ -2,16 +2,17 @@
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/Hal.h"
 #include "mozilla/HalSensor.h"
 
+#include "nsContentUtils.h"
 #include "nsDeviceSensors.h"
 
 #include "nsIDOMEvent.h"
 #include "nsIDOMWindow.h"
 #include "nsPIDOMWindow.h"
 #include "nsIDOMDocument.h"
 #include "nsIScriptObjectPrincipal.h"
 #include "nsIServiceManager.h"
@@ -123,17 +124,17 @@ nsDeviceSensors::~nsDeviceSensors()
 
   for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
     delete mWindowListeners[i];
   }
 }
 
 NS_IMETHODIMP nsDeviceSensors::HasWindowListener(uint32_t aType, nsIDOMWindow *aWindow, bool *aRetVal)
 {
-  if (!mEnabled)
+  if (AreSensorEventsDisabled(aWindow))
     *aRetVal = false;
   else
     *aRetVal = mWindowListeners[aType]->IndexOf(aWindow) != NoIndex;
 
   return NS_OK;
 }
 
 class DeviceSensorTestEvent : public Runnable
@@ -164,17 +165,17 @@ private:
   RefPtr<nsDeviceSensors> mTarget;
   uint32_t mType;
 };
 
 static bool sTestSensorEvents = false;
 
 NS_IMETHODIMP nsDeviceSensors::AddWindowListener(uint32_t aType, nsIDOMWindow *aWindow)
 {
-  if (!mEnabled)
+  if (AreSensorEventsDisabled(aWindow))
     return NS_OK;
 
   if (mWindowListeners[aType]->IndexOf(aWindow) != NoIndex)
     return NS_OK;
 
   if (!IsSensorEnabled(aType)) {
     RegisterSensorObserver((SensorType)aType, this);
   }
@@ -562,8 +563,24 @@ nsDeviceSensors::FireDOMMotionEvent(nsID
   bool defaultActionEnabled = true;
   target->DispatchEvent(event, &defaultActionEnabled);
 
   mLastRotationRate.reset();
   mLastAccelerationIncludingGravity.reset();
   mLastAcceleration.reset();
   mLastDOMMotionEventTime = TimeStamp::Now();
 }
+
+bool
+nsDeviceSensors::AreSensorEventsDisabled(nsIDOMWindow* aWindow)
+{
+  if (!mEnabled) {
+    return true;
+  }
+
+  nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aWindow);
+
+  if (!window) {
+    return false;
+  }
+
+  return nsContentUtils::ShouldResistFingerprinting(window->GetDocShell());
+}
--- a/dom/system/nsDeviceSensors.h
+++ b/dom/system/nsDeviceSensors.h
@@ -69,16 +69,18 @@ private:
                           double z);
 
   bool mEnabled;
 
   inline bool IsSensorEnabled(uint32_t aType) {
     return mWindowListeners[aType]->Length() > 0;
   }
 
+  bool AreSensorEventsDisabled(nsIDOMWindow* aWindow);
+
   mozilla::TimeStamp mLastDOMMotionEventTime;
   bool mIsUserProximityNear;
   mozilla::Maybe<DeviceAccelerationInit> mLastAcceleration;
   mozilla::Maybe<DeviceAccelerationInit> mLastAccelerationIncludingGravity;
   mozilla::Maybe<DeviceRotationRateInit> mLastRotationRate;
 };
 
 #endif