michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 sw=2 sts=2 et: */ michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: dump("############################### browserElementPanning.js loaded\n"); michael@0: michael@0: let { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components; michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/Geometry.jsm"); michael@0: michael@0: var global = this; michael@0: michael@0: const kObservedEvents = [ michael@0: "BEC:ShownModalPrompt", michael@0: "Activity:Success", michael@0: "Activity:Error" michael@0: ]; michael@0: michael@0: const ContentPanning = { michael@0: // Are we listening to touch or mouse events? michael@0: watchedEventsType: '', michael@0: michael@0: // Are mouse events being delivered to this content along with touch michael@0: // events, in violation of spec? michael@0: hybridEvents: false, michael@0: michael@0: init: function cp_init() { michael@0: // If APZ is enabled, we do active element handling in C++ michael@0: // (see widget/xpwidgets/ActiveElementManager.h), and panning michael@0: // itself in APZ, so we don't need to handle any touch events here. michael@0: if (docShell.asyncPanZoomEnabled === false) { michael@0: this._setupListenersForPanning(); michael@0: } michael@0: michael@0: addEventListener("unload", michael@0: this._unloadHandler.bind(this), michael@0: /* useCapture = */ false, michael@0: /* wantsUntrusted = */ false); michael@0: michael@0: addMessageListener("Viewport:Change", this._recvViewportChange.bind(this)); michael@0: addMessageListener("Gesture:DoubleTap", this._recvDoubleTap.bind(this)); michael@0: addEventListener("visibilitychange", this._handleVisibilityChange.bind(this)); michael@0: kObservedEvents.forEach((topic) => { michael@0: Services.obs.addObserver(this, topic, false); michael@0: }); michael@0: }, michael@0: michael@0: _setupListenersForPanning: function cp_setupListenersForPanning() { michael@0: var events; michael@0: try { michael@0: content.document.createEvent('TouchEvent'); michael@0: events = ['touchstart', 'touchend', 'touchmove']; michael@0: this.watchedEventsType = 'touch'; michael@0: #ifdef MOZ_WIDGET_GONK michael@0: // The gonk widget backend does not deliver mouse events per michael@0: // spec. Third-party content isn't exposed to this behavior, michael@0: // but that behavior creates some extra work for us here. michael@0: let appInfo = Cc["@mozilla.org/xre/app-info;1"]; michael@0: let isParentProcess = michael@0: !appInfo || appInfo.getService(Ci.nsIXULRuntime) michael@0: .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT; michael@0: this.hybridEvents = isParentProcess; michael@0: #endif michael@0: } catch(e) { michael@0: // Touch events aren't supported, so fall back on mouse. michael@0: events = ['mousedown', 'mouseup', 'mousemove']; michael@0: this.watchedEventsType = 'mouse'; michael@0: } michael@0: michael@0: let els = Cc["@mozilla.org/eventlistenerservice;1"] michael@0: .getService(Ci.nsIEventListenerService); michael@0: michael@0: events.forEach(function(type) { michael@0: // Using the system group for mouse/touch events to avoid michael@0: // missing events if .stopPropagation() has been called. michael@0: els.addSystemEventListener(global, type, michael@0: this.handleEvent.bind(this), michael@0: /* useCapture = */ false); michael@0: }.bind(this)); michael@0: }, michael@0: michael@0: handleEvent: function cp_handleEvent(evt) { michael@0: // Ignore events targeting a