summaryrefslogtreecommitdiffstats
path: root/devtools/shared/touch/simulator.js
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /devtools/shared/touch/simulator.js
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'devtools/shared/touch/simulator.js')
-rw-r--r--devtools/shared/touch/simulator.js77
1 files changed, 77 insertions, 0 deletions
diff --git a/devtools/shared/touch/simulator.js b/devtools/shared/touch/simulator.js
new file mode 100644
index 000000000..0e6d29282
--- /dev/null
+++ b/devtools/shared/touch/simulator.js
@@ -0,0 +1,77 @@
+/* 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/. */
+"use strict";
+
+var promise = require("promise");
+var defer = require("devtools/shared/defer");
+var Services = require("Services");
+
+const FRAME_SCRIPT =
+ "resource://devtools/shared/touch/simulator-content.js";
+
+var trackedBrowsers = new WeakMap();
+var savedTouchEventsEnabled =
+ Services.prefs.getIntPref("dom.w3c_touch_events.enabled");
+
+/**
+ * Simulate touch events for platforms where they aren't generally available.
+ * Defers to the `simulator-content.js` frame script to perform the real work.
+ */
+function TouchEventSimulator(browser) {
+ // Returns an already instantiated simulator for this browser
+ let simulator = trackedBrowsers.get(browser);
+ if (simulator) {
+ return simulator;
+ }
+
+ let mm = browser.frameLoader.messageManager;
+ mm.loadFrameScript(FRAME_SCRIPT, true);
+
+ simulator = {
+ enabled: false,
+
+ start() {
+ if (this.enabled) {
+ return promise.resolve({ isReloadNeeded: false });
+ }
+ this.enabled = true;
+
+ let deferred = defer();
+ let isReloadNeeded =
+ Services.prefs.getIntPref("dom.w3c_touch_events.enabled") != 1;
+ Services.prefs.setIntPref("dom.w3c_touch_events.enabled", 1);
+ let onStarted = () => {
+ mm.removeMessageListener("TouchEventSimulator:Started", onStarted);
+ deferred.resolve({ isReloadNeeded });
+ };
+ mm.addMessageListener("TouchEventSimulator:Started", onStarted);
+ mm.sendAsyncMessage("TouchEventSimulator:Start");
+ return deferred.promise;
+ },
+
+ stop() {
+ if (!this.enabled) {
+ return promise.resolve();
+ }
+ this.enabled = false;
+
+ let deferred = defer();
+ Services.prefs.setIntPref("dom.w3c_touch_events.enabled",
+ savedTouchEventsEnabled);
+ let onStopped = () => {
+ mm.removeMessageListener("TouchEventSimulator:Stopped", onStopped);
+ deferred.resolve();
+ };
+ mm.addMessageListener("TouchEventSimulator:Stopped", onStopped);
+ mm.sendAsyncMessage("TouchEventSimulator:Stop");
+ return deferred.promise;
+ }
+ };
+
+ trackedBrowsers.set(browser, simulator);
+
+ return simulator;
+}
+
+exports.TouchEventSimulator = TouchEventSimulator;