Bug 1274146 part 2 - Redirect fullscreenchange event to video controls via a custom event. r?gijs draft
authorXidorn Quan <me@upsuper.org>
Fri, 20 May 2016 14:52:27 +1000
changeset 369049 cba00d196afa272e8a05b0c3d9442d97d63b1b72
parent 369048 2a62afde538e826d58b7d0118fba18aa376dabf2
child 521441 d5e30fad745f723b96d4aa1b6a9c0760531d9611
push id18714
push userxquan@mozilla.com
push dateFri, 20 May 2016 04:53:04 +0000
reviewersgijs
bugs1274146
milestone49.0a1
Bug 1274146 part 2 - Redirect fullscreenchange event to video controls via a custom event. r?gijs Handler for fullscreenchange event attached normally may affect or be affected by other event handlers from the content. More specifically, a handler listening on "fullscreenchange" event on a target would stop any handler on the same target listening on "mozfullscreenchange" from being invoked. The main idea of this patch is to use a system event listener to avoid affecting event handling in content. MozReview-Commit-ID: FWVuREsDK9a
browser/base/content/content.js
toolkit/content/widgets/videocontrols.xml
--- a/browser/base/content/content.js
+++ b/browser/base/content/content.js
@@ -1474,8 +1474,31 @@ let OfflineApps = {
     }
   },
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
                                          Ci.nsISupportsWeakReference]),
 };
 
 addEventListener("MozApplicationManifest", OfflineApps, false);
 addMessageListener("OfflineApps:StartFetching", OfflineApps);
+
+var FullscreenListener = {
+  init: function() {
+    this._fullscreenElement = null;
+    // Register a system event listener so that it would not affect
+    // the event handling in the content.
+    Services.els.addSystemEventListener(global, "fullscreenchange", this, true);
+  },
+  handleEvent: function(evt) {
+    let targetFullscreenElement = evt.target.fullscreenElement;
+    let elem = targetFullscreenElement || this._fullscreenElement;
+    if (elem && elem instanceof content.HTMLVideoElement && elem.controls) {
+      this._fullscreenElement = targetFullscreenElement;
+      let nodes = Cc["@mozilla.org/inspector/dom-utils;1"]
+                  .getService(Ci.inIDOMUtils).getChildrenForNode(elem, true);
+      // Dispatch a fullscreen change notice to the controls.
+      Array.prototype.find.call(nodes, node => {
+        return node.tagName == "videocontrols";
+      }).dispatchEvent(new content.CustomEvent("mediafullscreenchange"));
+    }
+  }
+};
+FullscreenListener.init();
--- a/toolkit/content/widgets/videocontrols.xml
+++ b/toolkit/content/widgets/videocontrols.xml
@@ -1520,17 +1520,17 @@
                     addListener(this.playButton, "command", this.togglePause);
                     addListener(this.fullscreenButton, "command", this.toggleFullscreen);
                     addListener(this.clickToPlay, "click", this.clickToPlayClickHandler);
                     addListener(this.controlsSpacer, "click", this.clickToPlayClickHandler);
                     addListener(this.controlsSpacer, "dblclick", this.toggleFullscreen);
 
                     addListener(this.videocontrols, "resizevideocontrols", this.adjustControlSize);
                     addListener(this.videocontrols, "transitionend", this.onTransitionEnd);
-                    addListener(this.video.ownerDocument, "mozfullscreenchange", this.onFullscreenChange);
+                    addListener(this.videocontrols, "mediafullscreenchange", this.onFullscreenChange);
                     addListener(this.video, "keypress", this.keyHandler);
 
                     addListener(this.videocontrols, "dragstart", function(event) {
                         event.preventDefault(); //prevent dragging of controls image (bug 517114)
                     });
 
                     this.log("--- videocontrols initialized ---");
                 }