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
--- 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