Bug 1701789 - Restore UserProximityEvent r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D110172
This commit is contained in:
Kagami Sascha Rosylight 2021-03-30 20:44:47 +00:00
Родитель f01ba326dd
Коммит b046eaa662
16 изменённых файлов: 107 добавлений и 0 удалений

Просмотреть файл

@ -29,6 +29,7 @@ DEPRECATED_OPERATION(ImageBitmapRenderingContext_TransferImageBitmap)
DEPRECATED_OPERATION(WindowContentUntrusted)
DEPRECATED_OPERATION(MotionEvent)
DEPRECATED_OPERATION(OrientationEvent)
DEPRECATED_OPERATION(ProximityEvent)
DEPRECATED_OPERATION(AmbientLightEvent)
DEPRECATED_OPERATION(IDBOpenDBOptions_StorageType)
DEPRECATED_OPERATION(MozBoxOrInlineBoxDisplay)

Просмотреть файл

@ -301,6 +301,8 @@ void EventListenerManager::AddEventListenerInternal(
EnableDevice(eDeviceOrientation);
} else if (aTypeAtom == nsGkAtoms::onabsolutedeviceorientation) {
EnableDevice(eAbsoluteDeviceOrientation);
} else if (aTypeAtom == nsGkAtoms::onuserproximity) {
EnableDevice(eUserProximity);
} else if (aTypeAtom == nsGkAtoms::ondevicelight) {
EnableDevice(eDeviceLight);
} else if (aTypeAtom == nsGkAtoms::ondevicemotion) {
@ -484,6 +486,7 @@ bool EventListenerManager::IsDeviceType(EventMessage aEventMessage) {
case eAbsoluteDeviceOrientation:
case eDeviceMotion:
case eDeviceLight:
case eUserProximity:
#if defined(MOZ_WIDGET_ANDROID)
case eOrientationChange:
#endif
@ -519,6 +522,9 @@ void EventListenerManager::EnableDevice(EventMessage aEventMessage) {
window->EnableDeviceSensor(SENSOR_ORIENTATION);
#endif
break;
case eUserProximity:
window->EnableDeviceSensor(SENSOR_PROXIMITY);
break;
case eDeviceLight:
window->EnableDeviceSensor(SENSOR_LIGHT);
break;
@ -564,6 +570,9 @@ void EventListenerManager::DisableDevice(EventMessage aEventMessage) {
window->DisableDeviceSensor(SENSOR_LINEAR_ACCELERATION);
window->DisableDeviceSensor(SENSOR_GYROSCOPE);
break;
case eUserProximity:
window->DisableDeviceSensor(SENSOR_PROXIMITY);
break;
case eDeviceLight:
window->DisableDeviceSensor(SENSOR_LIGHT);
break;

Просмотреть файл

@ -326,6 +326,8 @@ WINDOW_ONLY_EVENT(deviceorientation, eDeviceOrientation, EventNameType_None,
eBasicEventClass)
WINDOW_ONLY_EVENT(absolutedeviceorientation, eAbsoluteDeviceOrientation,
EventNameType_None, eBasicEventClass)
WINDOW_ONLY_EVENT(userproximity, eUserProximity, EventNameType_None,
eBasicEventClass)
WINDOW_ONLY_EVENT(devicelight, eDeviceLight, EventNameType_None,
eBasicEventClass)
WINDOW_ONLY_EVENT(vrdisplayactivate, eVRDisplayActivate, EventNameType_None,

Просмотреть файл

@ -29,6 +29,10 @@ function hasOrientationListeners() {
dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_GAME_ROTATION_VECTOR, window);
}
function hasProximityListeners() {
return dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_PROXIMITY, window);
}
function hasMotionListeners() {
return dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_ACCELERATION, window) ||
dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_LINEAR_ACCELERATION, window) ||
@ -113,6 +117,7 @@ async function start() {
});
await test_event_presence("device.sensors.ambientLight.enabled", hasLightListeners, "devicelight");
await test_event_presence("device.sensors.proximity.enabled", hasProximityListeners, "userproximity");
await test_event_presence("device.sensors.motion.enabled", hasMotionListeners, "devicemotion");
await test_event_presence("device.sensors.orientation.enabled", hasOrientationListeners, "deviceorientation");

Просмотреть файл

@ -22,11 +22,14 @@ SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [
["device.sensors.orientation.enabled", false],
["device.sensors.motion.enabled", false],
["device.sensors.proximity.enabled", false],
["device.sensors.ambientLight.enabled", false]
]}, () => {
is("UserProximityEvent" in window, false, "UserProximityEvent does not exist");
is("DeviceLightEvent" in window, false, "DeviceLightEvent does not exist");
is("DeviceOrientationEvent" in window, false, "DeviceOrientationEvent does not exist");
is("DeviceMotionEvent" in window, false, "DeviceMotionEvent does not exist");
is("onuserproximity" in window, false, "onuserproximity does not exist");
SimpleTest.finish();
});

Просмотреть файл

@ -23,11 +23,20 @@ SpecialPowers.pushPrefEnv({"set": [
["device.sensors.enabled", true],
["device.sensors.orientation.enabled", true],
["device.sensors.motion.enabled", true],
["device.sensors.proximity.enabled", true],
["device.sensors.ambientLight.enabled", true]
]}, () => {
let receivedEvent;
document.addEventListener("hello", function(e) { receivedEvent = e; }, true);
// UserProximityEvent
e = new UserProximityEvent("hello", {near: true});
is(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event should not be trusted");
is(e.near, true, "near should be true");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
// DeviceLightEvent
e = new DeviceLightEvent("hello", {value: 1} );
is(e.type, "hello", "Wrong event type!");

Просмотреть файл

@ -20,6 +20,7 @@
#include "mozilla/dom/DeviceOrientationEvent.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/Event.h"
#include "mozilla/dom/UserProximityEvent.h"
#include "mozilla/ErrorResult.h"
#include <cmath>
@ -92,6 +93,7 @@ NS_IMETHODIMP nsDeviceSensorData::GetZ(double* aZ) {
NS_IMPL_ISUPPORTS(nsDeviceSensors, nsIDeviceSensors)
nsDeviceSensors::nsDeviceSensors() {
mIsUserProximityNear = false;
mLastDOMMotionEventTime = TimeStamp::Now();
for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
@ -327,6 +329,8 @@ void nsDeviceSensors::Notify(const mozilla::hal::SensorData& aSensorData) {
const Orientation orient = RotationVectorToOrientation(x, y, z, w);
FireDOMOrientationEvent(target, orient.alpha, orient.beta, orient.gamma,
Orientation::kRelative);
} else if (type == nsIDeviceSensorData::TYPE_PROXIMITY) {
MaybeFireDOMUserProximityEvent(target, x, z);
} else if (type == nsIDeviceSensorData::TYPE_LIGHT) {
FireDOMLightEvent(target, x);
}
@ -348,6 +352,29 @@ void nsDeviceSensors::FireDOMLightEvent(mozilla::dom::EventTarget* aTarget,
aTarget->DispatchEvent(*event);
}
void nsDeviceSensors::MaybeFireDOMUserProximityEvent(
mozilla::dom::EventTarget* aTarget, double aValue, double aMax) {
bool near = (aValue < aMax);
if (mIsUserProximityNear != near) {
mIsUserProximityNear = near;
FireDOMUserProximityEvent(aTarget, mIsUserProximityNear);
}
}
void nsDeviceSensors::FireDOMUserProximityEvent(
mozilla::dom::EventTarget* aTarget, bool aNear) {
UserProximityEventInit init;
init.mBubbles = true;
init.mCancelable = false;
init.mNear = aNear;
RefPtr<UserProximityEvent> event =
UserProximityEvent::Constructor(aTarget, u"userproximity"_ns, init);
event->SetTrusted(true);
aTarget->DispatchEvent(*event);
}
void nsDeviceSensors::FireDOMOrientationEvent(EventTarget* aTarget,
double aAlpha, double aBeta,
double aGamma, bool aIsAbsolute) {
@ -496,6 +523,14 @@ bool nsDeviceSensors::IsSensorAllowedByPref(uint32_t aType,
doc->WarnOnceAbout(DeprecatedOperations::eOrientationEvent);
}
break;
case nsIDeviceSensorData::TYPE_PROXIMITY:
// checks "device.sensors.proximity.enabled" pref
if (!StaticPrefs::device_sensors_proximity_enabled()) {
return false;
} else if (doc) {
doc->WarnOnceAbout(DeprecatedOperations::eProximityEvent, true);
}
break;
case nsIDeviceSensorData::TYPE_LIGHT:
// checks "device.sensors.ambientLight.enabled" pref
if (!StaticPrefs::device_sensors_ambientLight_enabled()) {

Просмотреть файл

@ -45,6 +45,12 @@ class nsDeviceSensors : public nsIDeviceSensors,
void FireDOMLightEvent(mozilla::dom::EventTarget* aTarget, double value);
void MaybeFireDOMUserProximityEvent(mozilla::dom::EventTarget* aTarget,
double aValue, double aMax);
void FireDOMUserProximityEvent(mozilla::dom::EventTarget* aTarget,
bool aNear);
void FireDOMOrientationEvent(mozilla::dom::EventTarget* target, double aAlpha,
double aBeta, double aGamma, bool aIsAbsolute);
@ -59,6 +65,7 @@ class nsDeviceSensors : public nsIDeviceSensors,
bool IsSensorAllowedByPref(uint32_t aType, nsIDOMWindow* aWindow);
mozilla::TimeStamp mLastDOMMotionEventTime;
bool mIsUserProximityNear;
mozilla::Maybe<DeviceAccelerationInit> mLastAcceleration;
mozilla::Maybe<DeviceAccelerationInit> mLastAccelerationIncludingGravity;
mozilla::Maybe<DeviceRotationRateInit> mLastRotationRate;

Просмотреть файл

@ -0,0 +1,20 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/.
*/
[Pref="device.sensors.proximity.enabled", Func="nsGlobalWindowInner::DeviceSensorsEnabled",
Exposed=Window]
interface UserProximityEvent : Event
{
constructor(DOMString type,
optional UserProximityEventInit eventInitDict = {});
readonly attribute boolean near;
};
dictionary UserProximityEventInit : EventInit
{
boolean near = false;
};

Просмотреть файл

@ -492,6 +492,8 @@ partial interface Window {
attribute EventHandler ondevicemotion;
attribute EventHandler ondeviceorientation;
attribute EventHandler onabsolutedeviceorientation;
[Pref="device.sensors.proximity.enabled"]
attribute EventHandler onuserproximity;
attribute EventHandler ondevicelight;
void dump(DOMString str);

Просмотреть файл

@ -334,6 +334,9 @@ with Files("UIEvent.webidl"):
with Files("URL.webidl"):
BUG_COMPONENT = ("Core", "Audio/Video")
with Files("UserProximityEvent.webidl"):
BUG_COMPONENT = ("Core", "DOM: UI Events & Focus Handling")
with Files("VTT*"):
BUG_COMPONENT = ("Core", "Audio/Video")
@ -1106,6 +1109,7 @@ GENERATED_EVENTS_WEBIDL_FILES = [
"TCPSocketEvent.webidl",
"TrackEvent.webidl",
"UDPMessageEvent.webidl",
"UserProximityEvent.webidl",
"WebGLContextEvent.webidl",
"XRInputSourceEvent.webidl",
"XRInputSourcesChangeEvent.webidl",

Просмотреть файл

@ -20,6 +20,7 @@ namespace hal {
enum SensorType {
SENSOR_ORIENTATION = 0,
SENSOR_ACCELERATION = 1,
SENSOR_PROXIMITY = 2,
SENSOR_LINEAR_ACCELERATION = 3,
SENSOR_GYROSCOPE = 4,
SENSOR_LIGHT = 5,

Просмотреть файл

@ -1473,6 +1473,12 @@
value: true
mirror: always
# KaiOS-only, see https://bugzilla.mozilla.org/show_bug.cgi?id=1699707#c10
- name: device.sensors.proximity.enabled
type: bool
value: false
mirror: always
- name: device.sensors.test.events
type: bool
value: false

Просмотреть файл

@ -382,6 +382,7 @@ NS_EVENT_MESSAGE(eOpen)
NS_EVENT_MESSAGE(eDeviceOrientation)
NS_EVENT_MESSAGE(eAbsoluteDeviceOrientation)
NS_EVENT_MESSAGE(eDeviceMotion)
NS_EVENT_MESSAGE(eUserProximity)
NS_EVENT_MESSAGE(eDeviceLight)
#if defined(MOZ_WIDGET_ANDROID)
NS_EVENT_MESSAGE(eOrientationChange)

Просмотреть файл

@ -1886,6 +1886,7 @@ STATIC_ATOMS = [
Atom("ondeviceorientation", "ondeviceorientation"),
Atom("onabsolutedeviceorientation", "onabsolutedeviceorientation"),
Atom("onmozorientationchange", "onmozorientationchange"),
Atom("onuserproximity", "onuserproximity"),
# light sensor support
Atom("ondevicelight", "ondevicelight"),
# MediaDevices device change event

Просмотреть файл

@ -19,6 +19,7 @@ interface nsIDeviceSensorData : nsISupports
// and [2, 1] if absolute orientation (compass heading) is required.
const unsigned long TYPE_ORIENTATION = 0;
const unsigned long TYPE_ACCELERATION = 1;
const unsigned long TYPE_PROXIMITY = 2;
const unsigned long TYPE_LINEAR_ACCELERATION = 3;
const unsigned long TYPE_GYROSCOPE = 4;
const unsigned long TYPE_LIGHT = 5;