Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 37d309e

Browse files
Olli PettayOlli Pettay
authored andcommitted
Bug 1412485, disable legacy touch APIs on desktop, r=masayuki
Hiding document.createEvent("TouchEvent"), document.createTouch, document.createTouchList and ontouch* event handlers on desktop to follow what Chrome has done. This patch explicitly does not remove createTouch or createTouchList everywhere, although those seem to have been removing already on some other browsers. Devtools use TOUCHEVENTS_OVERRIDE_ENABLED for touch event testing, and this patch keeps the old behavior per discussion with devtools devs. Differential Revision: https://phabricator.services.mozilla.com/D22081 --HG-- extra : rebase_source : 562588a289632ba2f11db7f3ac8782c26c3b05f8
1 parent e7f3517 commit 37d309e

22 files changed

+297
-157
lines changed

dom/bindings/test/TestCodeGen.webidl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -879,21 +879,21 @@ interface TestInterface {
879879
readonly attribute boolean prefable4;
880880
[Pref="abc.def"]
881881
readonly attribute boolean prefable5;
882-
[Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
882+
[Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
883883
readonly attribute boolean prefable6;
884-
[Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
884+
[Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
885885
readonly attribute boolean prefable7;
886-
[Pref="ghi.jkl", Func="nsGenericHTMLElement::TouchEventsEnabled"]
886+
[Pref="ghi.jkl", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
887887
readonly attribute boolean prefable8;
888-
[Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
888+
[Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
889889
readonly attribute boolean prefable9;
890890
[Pref="abc.def"]
891891
void prefable10();
892-
[Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
892+
[Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
893893
void prefable11();
894894
[Pref="abc.def", Func="TestFuncControlledMember"]
895895
readonly attribute boolean prefable12;
896-
[Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
896+
[Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
897897
void prefable13();
898898
[Pref="abc.def", Func="TestFuncControlledMember"]
899899
readonly attribute boolean prefable14;
@@ -915,15 +915,15 @@ interface TestInterface {
915915
readonly attribute boolean conditionalOnSecureContext1;
916916
[SecureContext, Pref="abc.def"]
917917
readonly attribute boolean conditionalOnSecureContext2;
918-
[SecureContext, Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
918+
[SecureContext, Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
919919
readonly attribute boolean conditionalOnSecureContext3;
920920
[SecureContext, Pref="abc.def", Func="TestFuncControlledMember"]
921921
readonly attribute boolean conditionalOnSecureContext4;
922922
[SecureContext]
923923
void conditionalOnSecureContext5();
924924
[SecureContext, Pref="abc.def"]
925925
void conditionalOnSecureContext6();
926-
[SecureContext, Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
926+
[SecureContext, Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
927927
void conditionalOnSecureContext7();
928928
[SecureContext, Pref="abc.def", Func="TestFuncControlledMember"]
929929
void conditionalOnSecureContext8();
@@ -1188,7 +1188,7 @@ dictionary DictWithConditionalMembers {
11881188
long chromeOnlyMember;
11891189
[Func="TestFuncControlledMember"]
11901190
long funcControlledMember;
1191-
[ChromeOnly, Func="nsGenericHTMLElement::TouchEventsEnabled"]
1191+
[ChromeOnly, Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
11921192
long chromeOnlyFuncControlledMember;
11931193
};
11941194

dom/bindings/test/TestExampleGen.webidl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -715,21 +715,21 @@ interface TestExampleInterface {
715715
readonly attribute boolean prefable4;
716716
[Pref="abc.def"]
717717
readonly attribute boolean prefable5;
718-
[Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
718+
[Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
719719
readonly attribute boolean prefable6;
720-
[Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
720+
[Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
721721
readonly attribute boolean prefable7;
722-
[Pref="ghi.jkl", Func="nsGenericHTMLElement::TouchEventsEnabled"]
722+
[Pref="ghi.jkl", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
723723
readonly attribute boolean prefable8;
724-
[Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
724+
[Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
725725
readonly attribute boolean prefable9;
726726
[Pref="abc.def"]
727727
void prefable10();
728-
[Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
728+
[Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
729729
void prefable11();
730730
[Pref="abc.def", Func="TestFuncControlledMember"]
731731
readonly attribute boolean prefable12;
732-
[Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
732+
[Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
733733
void prefable13();
734734
[Pref="abc.def", Func="TestFuncControlledMember"]
735735
readonly attribute boolean prefable14;
@@ -749,15 +749,15 @@ interface TestExampleInterface {
749749
readonly attribute boolean conditionalOnSecureContext1;
750750
[SecureContext, Pref="abc.def"]
751751
readonly attribute boolean conditionalOnSecureContext2;
752-
[SecureContext, Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
752+
[SecureContext, Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
753753
readonly attribute boolean conditionalOnSecureContext3;
754754
[SecureContext, Pref="abc.def", Func="TestFuncControlledMember"]
755755
readonly attribute boolean conditionalOnSecureContext4;
756756
[SecureContext]
757757
void conditionalOnSecureContext5();
758758
[SecureContext, Pref="abc.def"]
759759
void conditionalOnSecureContext6();
760-
[SecureContext, Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
760+
[SecureContext, Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
761761
void conditionalOnSecureContext7();
762762
[SecureContext, Pref="abc.def", Func="TestFuncControlledMember"]
763763
void conditionalOnSecureContext8();

dom/bindings/test/TestJSImplGen.webidl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -733,21 +733,21 @@ interface TestJSImplInterface {
733733
readonly attribute boolean prefable4;
734734
[Pref="abc.def"]
735735
readonly attribute boolean prefable5;
736-
[Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
736+
[Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
737737
readonly attribute boolean prefable6;
738-
[Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
738+
[Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
739739
readonly attribute boolean prefable7;
740-
[Pref="ghi.jkl", Func="nsGenericHTMLElement::TouchEventsEnabled"]
740+
[Pref="ghi.jkl", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
741741
readonly attribute boolean prefable8;
742-
[Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
742+
[Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
743743
readonly attribute boolean prefable9;
744744
[Pref="abc.def"]
745745
void prefable10();
746-
[Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
746+
[Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
747747
void prefable11();
748748
[Pref="abc.def", Func="TestFuncControlledMember"]
749749
readonly attribute boolean prefable12;
750-
[Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
750+
[Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
751751
void prefable13();
752752
[Pref="abc.def", Func="TestFuncControlledMember"]
753753
readonly attribute boolean prefable14;
@@ -769,15 +769,15 @@ interface TestJSImplInterface {
769769
readonly attribute boolean conditionalOnSecureContext1;
770770
[SecureContext, Pref="abc.def"]
771771
readonly attribute boolean conditionalOnSecureContext2;
772-
[SecureContext, Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
772+
[SecureContext, Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
773773
readonly attribute boolean conditionalOnSecureContext3;
774774
[SecureContext, Pref="abc.def", Func="TestFuncControlledMember"]
775775
readonly attribute boolean conditionalOnSecureContext4;
776776
[SecureContext]
777777
void conditionalOnSecureContext5();
778778
[SecureContext, Pref="abc.def"]
779779
void conditionalOnSecureContext6();
780-
[SecureContext, Pref="abc.def", Func="nsGenericHTMLElement::TouchEventsEnabled"]
780+
[SecureContext, Pref="abc.def", Func="nsGenericHTMLElement::LegacyTouchAPIEnabled"]
781781
void conditionalOnSecureContext7();
782782
[SecureContext, Pref="abc.def", Func="TestFuncControlledMember"]
783783
void conditionalOnSecureContext8();

dom/events/EventDispatcher.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,8 +1277,9 @@ nsresult EventDispatcher::DispatchDOMEvent(nsISupports* aTarget,
12771277
return NS_NewDOMScrollAreaEvent(aOwner, aPresContext, nullptr);
12781278
}
12791279
if (aEventType.LowerCaseEqualsLiteral("touchevent") &&
1280-
TouchEvent::PrefEnabled(
1281-
nsContentUtils::GetDocShellForEventTarget(aOwner))) {
1280+
TouchEvent::LegacyAPIEnabled(
1281+
nsContentUtils::GetDocShellForEventTarget(aOwner),
1282+
aCallerType == CallerType::System)) {
12821283
return NS_NewDOMTouchEvent(aOwner, aPresContext, nullptr);
12831284
}
12841285
if (aEventType.LowerCaseEqualsLiteral("hashchangeevent")) {

dom/events/TouchEvent.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "mozilla/dom/Touch.h"
1010
#include "mozilla/dom/TouchListBinding.h"
1111
#include "mozilla/Preferences.h"
12+
#include "mozilla/StaticPrefs.h"
1213
#include "mozilla/TouchEvents.h"
1314
#include "nsContentUtils.h"
1415
#include "nsIDocShell.h"
@@ -274,6 +275,31 @@ bool TouchEvent::PrefEnabled(nsIDocShell* aDocShell) {
274275
return enabled;
275276
}
276277

278+
// static
279+
bool TouchEvent::LegacyAPIEnabled(JSContext* aCx, JSObject* aGlobal) {
280+
nsIPrincipal* principal = nsContentUtils::SubjectPrincipal(aCx);
281+
bool isSystem = principal && nsContentUtils::IsSystemPrincipal(principal);
282+
283+
nsIDocShell* docShell = nullptr;
284+
if (aGlobal) {
285+
nsGlobalWindowInner* win = xpc::WindowOrNull(aGlobal);
286+
if (win) {
287+
docShell = win->GetDocShell();
288+
}
289+
}
290+
return LegacyAPIEnabled(docShell, isSystem);
291+
}
292+
293+
// static
294+
bool TouchEvent::LegacyAPIEnabled(nsIDocShell* aDocShell,
295+
bool aCallerIsSystem) {
296+
return (aCallerIsSystem ||
297+
StaticPrefs::dom_w3c_touch_events_legacy_apis_enabled() ||
298+
(aDocShell && aDocShell->GetTouchEventsOverride() ==
299+
nsIDocShell::TOUCHEVENTS_OVERRIDE_ENABLED)) &&
300+
PrefEnabled(aDocShell);
301+
}
302+
277303
// static
278304
already_AddRefed<TouchEvent> TouchEvent::Constructor(
279305
const GlobalObject& aGlobal, const nsAString& aType,

dom/events/TouchEvent.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ class TouchEvent : public UIEvent {
9797
static bool PlatformSupportsTouch();
9898
static bool PrefEnabled(JSContext* aCx, JSObject* aGlobal);
9999
static bool PrefEnabled(nsIDocShell* aDocShell);
100+
static bool LegacyAPIEnabled(JSContext* aCx, JSObject* aGlobal);
101+
static bool LegacyAPIEnabled(nsIDocShell* aDocShell, bool aCallerIsSystem);
100102

101103
static already_AddRefed<TouchEvent> Constructor(const GlobalObject& aGlobal,
102104
const nsAString& aType,

dom/events/test/mochitest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ skip-if = toolkit == 'android' #CRASH_DUMP, RANDOM
179179
[test_focus_disabled.html]
180180
[test_focus_abspos.html]
181181
[test_legacy_event.html]
182+
[test_legacy_touch_api.html]
182183
[test_messageEvent.html]
183184
[test_messageEvent_init.html]
184185
[test_moz_mouse_pixel_scroll_event.html]

dom/events/test/test_all_synthetic_events.html

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -422,46 +422,55 @@
422422
},
423423
};
424424

425-
for (var name of Object.keys(kEventConstructors)) {
426-
if (!kEventConstructors[name].chromeOnly) {
427-
continue;
428-
}
429-
if (window[name]) {
430-
ok(false, name + " should be chrome only.");
425+
function test() {
426+
for (var name of Object.keys(kEventConstructors)) {
427+
if (!kEventConstructors[name].chromeOnly) {
428+
continue;
429+
}
430+
if (window[name]) {
431+
ok(false, name + " should be chrome only.");
432+
}
433+
window[name] = SpecialPowers.unwrap(SpecialPowers.wrap(window)[name]);
431434
}
432-
window[name] = SpecialPowers.unwrap(SpecialPowers.wrap(window)[name]);
433-
}
434435

435-
var props = Object.getOwnPropertyNames(window);
436-
for (var i = 0; i < props.length; i++) {
437-
// Assume that event object must be named as "FooBarEvent".
438-
if (!props[i].match(/^([A-Z][a-zA-Z]+)?Event$/)) {
439-
continue;
440-
}
441-
if (!kEventConstructors[props[i]]) {
442-
ok(false, "Unknown event found: " + props[i]);
443-
continue;
444-
}
445-
if (!kEventConstructors[props[i]].create) {
446-
todo(false, "Cannot create untrusted event of " + props[i]);
447-
continue;
448-
}
449-
ok(true, "Creating " + props[i] + "...");
450-
var event = kEventConstructors[props[i]].create("foo", {});
451-
if (!event) {
452-
ok(false, "Failed to create untrusted event: " + props[i]);
453-
continue;
454-
}
455-
if (typeof(event.getModifierState) == "function") {
456-
const kModifiers = [ "Shift", "Control", "Alt", "AltGr", "Meta", "CapsLock", "ScrollLock", "NumLock", "OS", "Fn", "FnLock", "Symbol", "SymbolLock" ];
457-
for (var j = 0; j < kModifiers.length; j++) {
458-
ok(true, "Calling " + props[i] + ".getModifierState(" + kModifiers[j] + ")...");
459-
var modifierState = event.getModifierState(kModifiers[j]);
460-
ok(true, props[i] + ".getModifierState(" + kModifiers[j] + ") = " + modifierState);
436+
var props = Object.getOwnPropertyNames(window);
437+
for (var i = 0; i < props.length; i++) {
438+
// Assume that event object must be named as "FooBarEvent".
439+
if (!props[i].match(/^([A-Z][a-zA-Z]+)?Event$/)) {
440+
continue;
441+
}
442+
if (!kEventConstructors[props[i]]) {
443+
ok(false, "Unknown event found: " + props[i]);
444+
continue;
445+
}
446+
if (!kEventConstructors[props[i]].create) {
447+
todo(false, "Cannot create untrusted event of " + props[i]);
448+
continue;
449+
}
450+
ok(true, "Creating " + props[i] + "...");
451+
var event = kEventConstructors[props[i]].create("foo", {});
452+
if (!event) {
453+
ok(false, "Failed to create untrusted event: " + props[i]);
454+
continue;
455+
}
456+
if (typeof(event.getModifierState) == "function") {
457+
const kModifiers = [ "Shift", "Control", "Alt", "AltGr", "Meta", "CapsLock", "ScrollLock", "NumLock", "OS", "Fn", "FnLock", "Symbol", "SymbolLock" ];
458+
for (var j = 0; j < kModifiers.length; j++) {
459+
ok(true, "Calling " + props[i] + ".getModifierState(" + kModifiers[j] + ")...");
460+
var modifierState = event.getModifierState(kModifiers[j]);
461+
ok(true, props[i] + ".getModifierState(" + kModifiers[j] + ") = " + modifierState);
462+
}
461463
}
462464
}
463465
}
464466

467+
SimpleTest.waitForExplicitFinish();
468+
SpecialPowers.pushPrefEnv(
469+
{"set": [["dom.w3c_touch_events.legacy_apis.enabled", true]]},
470+
function() {
471+
test();
472+
SimpleTest.finish();
473+
});
465474
</script>
466475
</pre>
467476
</body>

dom/events/test/test_bug1429572.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
var win;
1717
function start() {
18-
SpecialPowers.pushPrefEnv({"set": [["dom.w3c_touch_events.enabled", 1]]},
18+
SpecialPowers.pushPrefEnv({"set": [["dom.w3c_touch_events.enabled", 1],
19+
["dom.w3c_touch_events.legacy_apis.enabled", true]]},
1920
function() {
2021
ok(true, "Starting the test.");
2122
win = window.open("window_bug1429572.html", "testwindow",

0 commit comments

Comments
 (0)