2019-02-12 05:34:38 +03:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 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/. */
|
|
|
|
|
2022-06-07 07:29:48 +03:00
|
|
|
const lazy = {};
|
|
|
|
|
2022-09-23 21:11:48 +03:00
|
|
|
ChromeUtils.defineESModuleGetters(lazy, {
|
Bug 1792341 - Migrate more toolkit/modules consumers to use direct ES module import. r=Gijs,webdriver-reviewers,perftest-reviewers,necko-reviewers,geckoview-reviewers,preferences-reviewers,application-update-reviewers,pip-reviewers,credential-management-reviewers,sgalich,owlish,bytesized,AlexandruIonescu,whimboo,mconley,mixedpuppy
Mainly automated changes. Some manual ESLint fixes and whitespace cleanup.
Differential Revision: https://phabricator.services.mozilla.com/D158452
2022-10-18 14:21:26 +03:00
|
|
|
ContentDOMReference: "resource://gre/modules/ContentDOMReference.sys.mjs",
|
|
|
|
DeferredTask: "resource://gre/modules/DeferredTask.sys.mjs",
|
2022-09-23 21:11:48 +03:00
|
|
|
KEYBOARD_CONTROLS: "resource://gre/modules/PictureInPictureControls.sys.mjs",
|
Bug 1792365 - Convert toolkit/modules consumers to use ES module imports directly. r=webdriver-reviewers,perftest-reviewers,geckoview-reviewers,extension-reviewers,preferences-reviewers,desktop-theme-reviewers,application-update-reviewers,pip-reviewers,credential-management-reviewers,robwu,Gijs,sgalich,bytesized,AlexandruIonescu,dao,m_kato
Differential Revision: https://phabricator.services.mozilla.com/D158094
2022-09-29 09:52:34 +03:00
|
|
|
Rect: "resource://gre/modules/Geometry.sys.mjs",
|
2022-09-23 21:11:48 +03:00
|
|
|
TOGGLE_POLICIES: "resource://gre/modules/PictureInPictureControls.sys.mjs",
|
|
|
|
TOGGLE_POLICY_STRINGS:
|
|
|
|
"resource://gre/modules/PictureInPictureControls.sys.mjs",
|
|
|
|
});
|
2019-12-21 03:24:06 +03:00
|
|
|
|
2022-02-07 21:05:43 +03:00
|
|
|
const { WebVTT } = ChromeUtils.import("resource://gre/modules/vtt.jsm");
|
Bug 1792341 - Migrate more toolkit/modules consumers to use direct ES module import. r=Gijs,webdriver-reviewers,perftest-reviewers,necko-reviewers,geckoview-reviewers,preferences-reviewers,application-update-reviewers,pip-reviewers,credential-management-reviewers,sgalich,owlish,bytesized,AlexandruIonescu,whimboo,mconley,mixedpuppy
Mainly automated changes. Some manual ESLint fixes and whitespace cleanup.
Differential Revision: https://phabricator.services.mozilla.com/D158452
2022-10-18 14:21:26 +03:00
|
|
|
import { setTimeout, clearTimeout } from "resource://gre/modules/Timer.sys.mjs";
|
2022-09-23 21:11:48 +03:00
|
|
|
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
|
2019-04-05 18:51:58 +03:00
|
|
|
|
2022-06-07 07:29:48 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetters(lazy, {
|
2022-02-08 22:39:41 +03:00
|
|
|
NimbusFeatures: "resource://nimbus/ExperimentAPI.jsm",
|
|
|
|
});
|
|
|
|
|
2022-02-07 21:05:43 +03:00
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(
|
2022-06-07 07:29:48 +03:00
|
|
|
lazy,
|
2022-02-07 21:05:43 +03:00
|
|
|
"DISPLAY_TEXT_TRACKS_PREF",
|
|
|
|
"media.videocontrols.picture-in-picture.display-text-tracks.enabled",
|
|
|
|
false
|
|
|
|
);
|
2023-01-25 02:53:23 +03:00
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(
|
|
|
|
lazy,
|
|
|
|
"IMPROVED_CONTROLS_ENABLED_PREF",
|
|
|
|
"media.videocontrols.picture-in-picture.improved-video-controls.enabled",
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2019-04-05 18:51:58 +03:00
|
|
|
const TOGGLE_ENABLED_PREF =
|
|
|
|
"media.videocontrols.picture-in-picture.video-toggle.enabled";
|
2022-04-14 17:30:31 +03:00
|
|
|
const PIP_ENABLED_PREF = "media.videocontrols.picture-in-picture.enabled";
|
2019-04-25 06:35:29 +03:00
|
|
|
const TOGGLE_TESTING_PREF =
|
|
|
|
"media.videocontrols.picture-in-picture.video-toggle.testing";
|
2022-01-04 00:54:57 +03:00
|
|
|
const TOGGLE_VISIBILITY_THRESHOLD_PREF =
|
|
|
|
"media.videocontrols.picture-in-picture.video-toggle.visibility-threshold";
|
2022-04-06 17:33:54 +03:00
|
|
|
const TEXT_TRACK_FONT_SIZE =
|
|
|
|
"media.videocontrols.picture-in-picture.display-text-tracks.size";
|
2021-02-25 23:24:05 +03:00
|
|
|
|
2019-04-05 18:51:58 +03:00
|
|
|
const MOUSEMOVE_PROCESSING_DELAY_MS = 50;
|
2019-07-18 03:50:08 +03:00
|
|
|
const TOGGLE_HIDING_TIMEOUT_MS = 2000;
|
2022-02-23 04:10:17 +03:00
|
|
|
// If you change this, also change VideoControlsWidget.SEEK_TIME_SECS:
|
|
|
|
const SEEK_TIME_SECS = 5;
|
2022-03-23 22:47:47 +03:00
|
|
|
const EMPTIED_TIMEOUT_MS = 1000;
|
2019-04-05 18:51:58 +03:00
|
|
|
|
2020-10-23 01:04:55 +03:00
|
|
|
// The ToggleChild does not want to capture events from the PiP
|
|
|
|
// windows themselves. This set contains all currently open PiP
|
|
|
|
// players' content windows
|
|
|
|
var gPlayerContents = new WeakSet();
|
|
|
|
|
2019-04-25 06:35:29 +03:00
|
|
|
// To make it easier to write tests, we have a process-global
|
|
|
|
// WeakSet of all <video> elements that are being tracked for
|
|
|
|
// mouseover
|
|
|
|
var gWeakIntersectingVideosForTesting = new WeakSet();
|
2019-04-05 18:51:58 +03:00
|
|
|
|
2019-12-21 03:24:06 +03:00
|
|
|
// Overrides are expected to stay constant for the lifetime of a
|
|
|
|
// content process, so we set this as a lazy process global.
|
2020-09-11 23:14:09 +03:00
|
|
|
// See PictureInPictureToggleChild.getSiteOverrides for a
|
|
|
|
// sense of what the return types are.
|
2022-06-07 07:29:48 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(lazy, "gSiteOverrides", () => {
|
2020-09-11 23:14:09 +03:00
|
|
|
return PictureInPictureToggleChild.getSiteOverrides();
|
2019-12-21 03:24:06 +03:00
|
|
|
});
|
|
|
|
|
2022-06-07 07:29:48 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(lazy, "logConsole", () => {
|
2022-04-14 02:25:26 +03:00
|
|
|
return console.createInstance({
|
|
|
|
prefix: "PictureInPictureChild",
|
|
|
|
maxLogLevel: Services.prefs.getBoolPref(
|
|
|
|
"media.videocontrols.picture-in-picture.log",
|
|
|
|
false
|
|
|
|
)
|
|
|
|
? "Debug"
|
|
|
|
: "Error",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-03-23 23:01:00 +03:00
|
|
|
/**
|
|
|
|
* Creates and returns an instance of the PictureInPictureChildVideoWrapper class responsible
|
|
|
|
* for applying site-specific wrapper methods around the original video.
|
|
|
|
*
|
|
|
|
* The Picture-In-Picture add-on can use this to provide site-specific wrappers for
|
|
|
|
* sites that require special massaging to control.
|
|
|
|
* @param {Object} pipChild reference to PictureInPictureChild class calling this function
|
|
|
|
* @param {Element} originatingVideo
|
|
|
|
* The <video> element to wrap.
|
|
|
|
* @returns {PictureInPictureChildVideoWrapper} instance of PictureInPictureChildVideoWrapper
|
|
|
|
*/
|
|
|
|
function applyWrapper(pipChild, originatingVideo) {
|
|
|
|
let originatingDoc = originatingVideo.ownerDocument;
|
|
|
|
let originatingDocumentURI = originatingDoc.documentURI;
|
|
|
|
|
2022-06-07 07:29:48 +03:00
|
|
|
let overrides = lazy.gSiteOverrides.find(([matcher]) => {
|
2022-03-23 23:01:00 +03:00
|
|
|
return matcher.matches(originatingDocumentURI);
|
|
|
|
});
|
|
|
|
|
|
|
|
// gSiteOverrides is a list of tuples where the first element is the MatchPattern
|
|
|
|
// for a supported site and the second is the actual overrides object for it.
|
2022-04-07 02:00:41 +03:00
|
|
|
let wrapperPath = overrides ? overrides[1].videoWrapperScriptPath : null;
|
2022-03-23 23:01:00 +03:00
|
|
|
return new PictureInPictureChildVideoWrapper(
|
|
|
|
wrapperPath,
|
|
|
|
originatingVideo,
|
|
|
|
pipChild
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-23 21:11:48 +03:00
|
|
|
export class PictureInPictureLauncherChild extends JSWindowActorChild {
|
2020-10-08 16:51:24 +03:00
|
|
|
handleEvent(event) {
|
|
|
|
switch (event.type) {
|
|
|
|
case "MozTogglePictureInPicture": {
|
|
|
|
if (event.isTrusted) {
|
|
|
|
this.togglePictureInPicture(event.target);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
receiveMessage(message) {
|
|
|
|
switch (message.name) {
|
|
|
|
case "PictureInPicture:KeyToggle": {
|
|
|
|
this.keyToggle();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tells the parent to open a Picture-in-Picture window hosting
|
|
|
|
* a clone of the passed video. If we know about a pre-existing
|
|
|
|
* Picture-in-Picture window existing, this tells the parent to
|
|
|
|
* close it before opening the new one.
|
|
|
|
*
|
|
|
|
* @param {Element} video The <video> element to view in a Picture
|
|
|
|
* in Picture window.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves {undefined} Once the new Picture-in-Picture window
|
|
|
|
* has been requested.
|
|
|
|
*/
|
|
|
|
async togglePictureInPicture(video) {
|
2020-10-23 01:04:55 +03:00
|
|
|
if (video.isCloningElementVisually) {
|
2020-10-16 20:50:49 +03:00
|
|
|
// The only way we could have entered here for the same video is if
|
|
|
|
// we are toggling via the context menu, since we hide the inline
|
|
|
|
// Picture-in-Picture toggle when a video is being displayed in
|
|
|
|
// Picture-in-Picture. Turn off PiP in this case
|
|
|
|
const stopPipEvent = new this.contentWindow.CustomEvent(
|
|
|
|
"MozStopPictureInPicture",
|
|
|
|
{
|
|
|
|
bubbles: true,
|
|
|
|
detail: { reason: "context-menu" },
|
|
|
|
}
|
|
|
|
);
|
|
|
|
video.dispatchEvent(stopPipEvent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-23 07:05:20 +03:00
|
|
|
if (!PictureInPictureChild.videoWrapper) {
|
|
|
|
PictureInPictureChild.videoWrapper = applyWrapper(
|
|
|
|
PictureInPictureChild,
|
|
|
|
video
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-25 02:53:23 +03:00
|
|
|
let timestamp = undefined;
|
|
|
|
let scrubberPosition = undefined;
|
|
|
|
|
|
|
|
if (lazy.IMPROVED_CONTROLS_ENABLED_PREF) {
|
|
|
|
timestamp = PictureInPictureChild.videoWrapper.formatTimestamp(
|
2023-01-26 23:28:41 +03:00
|
|
|
PictureInPictureChild.videoWrapper.getCurrentTime(video),
|
|
|
|
PictureInPictureChild.videoWrapper.getDuration(video)
|
2023-01-25 02:53:23 +03:00
|
|
|
);
|
|
|
|
|
2023-01-26 23:28:41 +03:00
|
|
|
// Scrubber is hidden if undefined, so only set it to something else
|
|
|
|
// if the timestamp is not undefined.
|
2023-01-25 02:53:23 +03:00
|
|
|
scrubberPosition =
|
|
|
|
timestamp === undefined
|
|
|
|
? undefined
|
2023-01-26 23:28:41 +03:00
|
|
|
: PictureInPictureChild.videoWrapper.getCurrentTime(video) /
|
|
|
|
PictureInPictureChild.videoWrapper.getDuration(video);
|
2023-01-25 02:53:23 +03:00
|
|
|
}
|
2022-12-22 19:29:47 +03:00
|
|
|
|
2020-10-16 20:50:49 +03:00
|
|
|
// All other requests to toggle PiP should open a new PiP
|
|
|
|
// window
|
2022-06-07 07:29:48 +03:00
|
|
|
const videoRef = lazy.ContentDOMReference.get(video);
|
2020-10-08 16:51:24 +03:00
|
|
|
this.sendAsyncMessage("PictureInPicture:Request", {
|
|
|
|
isMuted: PictureInPictureChild.videoIsMuted(video),
|
|
|
|
playing: PictureInPictureChild.videoIsPlaying(video),
|
|
|
|
videoHeight: video.videoHeight,
|
|
|
|
videoWidth: video.videoWidth,
|
|
|
|
videoRef,
|
2022-06-07 07:29:48 +03:00
|
|
|
ccEnabled: lazy.DISPLAY_TEXT_TRACKS_PREF,
|
2022-03-13 03:45:01 +03:00
|
|
|
webVTTSubtitles: !!video.textTracks?.length,
|
2022-11-22 06:52:32 +03:00
|
|
|
scrubberPosition,
|
2022-12-22 19:29:47 +03:00
|
|
|
timestamp,
|
2020-10-08 16:51:24 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
/**
|
2022-03-11 03:29:05 +03:00
|
|
|
* The keyboard was used to attempt to open Picture-in-Picture. If a video is focused,
|
|
|
|
* select that video. Otherwise find the first playing video, or if none, the largest
|
|
|
|
* dimension video. We suspect this heuristic will handle most cases, though we
|
|
|
|
* might refine this later on. Note that we assume that this method will only be
|
|
|
|
* called for the focused document.
|
2020-10-08 16:51:24 +03:00
|
|
|
*/
|
|
|
|
keyToggle() {
|
2022-03-11 03:29:05 +03:00
|
|
|
let doc = this.document;
|
|
|
|
if (doc) {
|
|
|
|
let video = doc.activeElement;
|
2022-04-06 14:57:57 +03:00
|
|
|
if (!HTMLVideoElement.isInstance(video)) {
|
2021-02-19 04:51:25 +03:00
|
|
|
let listOfVideos = [...doc.querySelectorAll("video")].filter(
|
|
|
|
video => !isNaN(video.duration)
|
|
|
|
);
|
2020-10-08 16:51:24 +03:00
|
|
|
// Get the first non-paused video, otherwise the longest video. This
|
|
|
|
// fallback is designed to skip over "preview"-style videos on sidebars.
|
2022-03-11 03:29:05 +03:00
|
|
|
video =
|
2020-10-08 16:51:24 +03:00
|
|
|
listOfVideos.filter(v => !v.paused)[0] ||
|
|
|
|
listOfVideos.sort((a, b) => b.duration - a.duration)[0];
|
2022-03-11 03:29:05 +03:00
|
|
|
}
|
|
|
|
if (video) {
|
|
|
|
this.togglePictureInPicture(video);
|
2020-10-08 16:51:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 18:51:58 +03:00
|
|
|
/**
|
|
|
|
* The PictureInPictureToggleChild is responsible for displaying the overlaid
|
|
|
|
* Picture-in-Picture toggle over top of <video> elements that the mouse is
|
|
|
|
* hovering.
|
|
|
|
*/
|
2022-09-23 21:11:48 +03:00
|
|
|
export class PictureInPictureToggleChild extends JSWindowActorChild {
|
2020-03-03 21:40:25 +03:00
|
|
|
constructor() {
|
|
|
|
super();
|
2019-04-05 18:51:58 +03:00
|
|
|
// We need to maintain some state about various things related to the
|
|
|
|
// Picture-in-Picture toggles - however, for now, the same
|
|
|
|
// PictureInPictureToggleChild might be re-used for different documents.
|
|
|
|
// We keep the state stashed inside of this WeakMap, keyed on the document
|
|
|
|
// itself.
|
|
|
|
this.weakDocStates = new WeakMap();
|
2022-04-14 17:30:31 +03:00
|
|
|
this.toggleEnabled =
|
|
|
|
Services.prefs.getBoolPref(TOGGLE_ENABLED_PREF) &&
|
|
|
|
Services.prefs.getBoolPref(PIP_ENABLED_PREF);
|
2019-04-25 06:35:29 +03:00
|
|
|
this.toggleTesting = Services.prefs.getBoolPref(TOGGLE_TESTING_PREF, false);
|
2019-11-01 17:15:22 +03:00
|
|
|
|
|
|
|
// Bug 1570744 - JSWindowActorChild's cannot be used as nsIObserver's
|
|
|
|
// directly, so we create a new function here instead to act as our
|
|
|
|
// nsIObserver, which forwards the notification to the observe method.
|
|
|
|
this.observerFunction = (subject, topic, data) => {
|
|
|
|
this.observe(subject, topic, data);
|
|
|
|
};
|
|
|
|
Services.prefs.addObserver(TOGGLE_ENABLED_PREF, this.observerFunction);
|
2022-04-14 17:30:31 +03:00
|
|
|
Services.prefs.addObserver(PIP_ENABLED_PREF, this.observerFunction);
|
2020-09-08 19:59:55 +03:00
|
|
|
Services.cpmm.sharedData.addEventListener("change", this);
|
2019-04-05 18:51:58 +03:00
|
|
|
}
|
|
|
|
|
2020-10-21 02:27:28 +03:00
|
|
|
didDestroy() {
|
2019-11-14 21:38:56 +03:00
|
|
|
this.stopTrackingMouseOverVideos();
|
2019-11-01 17:15:22 +03:00
|
|
|
Services.prefs.removeObserver(TOGGLE_ENABLED_PREF, this.observerFunction);
|
2022-04-14 17:30:31 +03:00
|
|
|
Services.prefs.removeObserver(PIP_ENABLED_PREF, this.observerFunction);
|
2020-09-08 19:59:55 +03:00
|
|
|
Services.cpmm.sharedData.removeEventListener("change", this);
|
2021-03-12 23:38:55 +03:00
|
|
|
|
|
|
|
// remove the observer on the <video> element
|
|
|
|
let state = this.docState;
|
|
|
|
if (state?.intersectionObserver) {
|
|
|
|
state.intersectionObserver.disconnect();
|
|
|
|
}
|
2022-01-19 19:36:36 +03:00
|
|
|
|
|
|
|
// ensure the sandbox created by the video is destroyed
|
|
|
|
this.videoWrapper?.destroy();
|
|
|
|
this.videoWrapper = null;
|
|
|
|
|
2021-03-12 23:38:55 +03:00
|
|
|
// ensure we don't access the state
|
|
|
|
this.isDestroyed = true;
|
2019-08-13 16:19:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
observe(subject, topic, data) {
|
2020-07-22 19:02:13 +03:00
|
|
|
if (topic != "nsPref:changed") {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-13 16:19:06 +03:00
|
|
|
|
2022-04-14 17:30:31 +03:00
|
|
|
this.toggleEnabled =
|
|
|
|
Services.prefs.getBoolPref(TOGGLE_ENABLED_PREF) &&
|
|
|
|
Services.prefs.getBoolPref(PIP_ENABLED_PREF);
|
2020-07-22 19:02:13 +03:00
|
|
|
|
2020-10-26 22:16:53 +03:00
|
|
|
if (this.toggleEnabled) {
|
|
|
|
// We have enabled the Picture-in-Picture toggle, so we need to make
|
|
|
|
// sure we register all of the videos that might already be on the page.
|
|
|
|
this.contentWindow.requestIdleCallback(() => {
|
|
|
|
let videos = this.document.querySelectorAll("video");
|
|
|
|
for (let video of videos) {
|
|
|
|
this.registerVideo(video);
|
2020-07-22 19:02:13 +03:00
|
|
|
}
|
2020-10-26 22:16:53 +03:00
|
|
|
});
|
2019-08-13 16:19:06 +03:00
|
|
|
}
|
2019-08-01 19:53:02 +03:00
|
|
|
}
|
|
|
|
|
2019-04-05 18:51:58 +03:00
|
|
|
/**
|
|
|
|
* Returns the state for the current document referred to via
|
2019-11-01 17:15:22 +03:00
|
|
|
* this.document. If no such state exists, creates it, stores it
|
2019-04-05 18:51:58 +03:00
|
|
|
* and returns it.
|
|
|
|
*/
|
|
|
|
get docState() {
|
2021-03-12 23:38:55 +03:00
|
|
|
if (this.isDestroyed || !this.document) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-01 17:15:22 +03:00
|
|
|
let state = this.weakDocStates.get(this.document);
|
2021-03-12 23:38:55 +03:00
|
|
|
|
2022-01-04 00:54:57 +03:00
|
|
|
let visibilityThresholdPref = Services.prefs.getFloatPref(
|
|
|
|
TOGGLE_VISIBILITY_THRESHOLD_PREF,
|
2022-02-09 03:21:14 +03:00
|
|
|
"1.0"
|
2022-01-04 00:54:57 +03:00
|
|
|
);
|
|
|
|
|
2019-04-05 18:51:58 +03:00
|
|
|
if (!state) {
|
|
|
|
state = {
|
|
|
|
// A reference to the IntersectionObserver that's monitoring for videos
|
|
|
|
// to become visible.
|
|
|
|
intersectionObserver: null,
|
|
|
|
// A WeakSet of videos that are supposedly visible, according to the
|
|
|
|
// IntersectionObserver.
|
|
|
|
weakVisibleVideos: new WeakSet(),
|
|
|
|
// The number of videos that are supposedly visible, according to the
|
|
|
|
// IntersectionObserver
|
2019-04-15 04:09:12 +03:00
|
|
|
visibleVideosCount: 0,
|
2019-04-05 18:51:58 +03:00
|
|
|
// The DeferredTask that we'll arm every time a mousemove event occurs
|
|
|
|
// on a page where we have one or more visible videos.
|
|
|
|
mousemoveDeferredTask: null,
|
|
|
|
// A weak reference to the last video we displayed the toggle over.
|
|
|
|
weakOverVideo: null,
|
2019-05-02 20:51:30 +03:00
|
|
|
// True if the user is in the midst of clicking the toggle.
|
|
|
|
isClickingToggle: false,
|
|
|
|
// Set to the original target element on pointerdown if the user is clicking
|
|
|
|
// the toggle - this way, we can determine if a "click" event will need to be
|
|
|
|
// suppressed ("click" events don't fire if a "mouseup" occurs on a different
|
|
|
|
// element from the "pointerdown" / "mousedown" event).
|
|
|
|
clickedElement: null,
|
2019-07-18 03:50:08 +03:00
|
|
|
// This is a DeferredTask to hide the toggle after a period of mouse
|
|
|
|
// inactivity.
|
|
|
|
hideToggleDeferredTask: null,
|
2019-10-12 02:36:59 +03:00
|
|
|
// If we reach a point where we're tracking videos for mouse movements,
|
|
|
|
// then this will be true. If there are no videos worth tracking, then
|
|
|
|
// this is false.
|
|
|
|
isTrackingVideos: false,
|
2022-06-07 07:29:48 +03:00
|
|
|
togglePolicy: lazy.TOGGLE_POLICIES.DEFAULT,
|
2022-01-04 00:54:57 +03:00
|
|
|
toggleVisibilityThreshold: visibilityThresholdPref,
|
2020-12-04 22:43:54 +03:00
|
|
|
// The documentURI that has been checked with toggle policies and
|
|
|
|
// visibility thresholds for this document. Note that the documentURI
|
|
|
|
// might change for a document via the history API, so we remember
|
|
|
|
// the last checked documentURI to determine if we need to check again.
|
2020-08-08 00:47:01 +03:00
|
|
|
checkedPolicyDocumentURI: null,
|
2019-04-05 18:51:58 +03:00
|
|
|
};
|
2019-11-01 17:15:22 +03:00
|
|
|
this.weakDocStates.set(this.document, state);
|
2019-04-05 18:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2019-11-07 21:31:42 +03:00
|
|
|
/**
|
|
|
|
* Returns the video that the user was last hovering with the mouse if it
|
|
|
|
* still exists.
|
|
|
|
*
|
|
|
|
* @return {Element} the <video> element that the user was last hovering,
|
|
|
|
* or null if there was no such <video>, or the <video> no longer exists.
|
|
|
|
*/
|
|
|
|
getWeakOverVideo() {
|
|
|
|
let { weakOverVideo } = this.docState;
|
|
|
|
if (weakOverVideo) {
|
|
|
|
// Bug 800957 - Accessing weakrefs at the wrong time can cause us to
|
|
|
|
// throw NS_ERROR_XPC_BAD_CONVERT_NATIVE
|
|
|
|
try {
|
|
|
|
return weakOverVideo.get();
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-04-05 18:51:58 +03:00
|
|
|
handleEvent(event) {
|
2019-05-01 16:48:02 +03:00
|
|
|
if (!event.isTrusted) {
|
|
|
|
// We don't care about synthesized events that might be coming from
|
|
|
|
// content JS.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-23 01:04:55 +03:00
|
|
|
// Don't capture events from Picture-in-Picture content windows
|
|
|
|
if (gPlayerContents.has(this.contentWindow)) {
|
|
|
|
return;
|
2020-06-09 21:30:51 +03:00
|
|
|
}
|
|
|
|
|
2019-04-05 18:51:58 +03:00
|
|
|
switch (event.type) {
|
2022-12-06 19:20:13 +03:00
|
|
|
case "touchstart": {
|
|
|
|
// Even if this is a touch event, there may be subsequent click events.
|
|
|
|
// Suppress those events after selecting the toggle to prevent playback changes
|
|
|
|
// when opening the Picture-in-Picture window.
|
|
|
|
if (this.docState.isClickingToggle) {
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-09-08 19:59:55 +03:00
|
|
|
case "change": {
|
|
|
|
const { changedKeys } = event;
|
2020-09-11 23:14:09 +03:00
|
|
|
if (changedKeys.includes("PictureInPicture:SiteOverrides")) {
|
|
|
|
// For now we only update our cache if the site overrides change.
|
2020-09-08 19:59:55 +03:00
|
|
|
// the user will need to refresh the page for changes to apply.
|
|
|
|
try {
|
2022-06-07 07:29:48 +03:00
|
|
|
lazy.gSiteOverrides = PictureInPictureToggleChild.getSiteOverrides();
|
2020-09-08 19:59:55 +03:00
|
|
|
} catch (e) {
|
2020-09-11 23:14:09 +03:00
|
|
|
// Ignore resulting TypeError if gSiteOverrides is still unloaded
|
2020-09-08 19:59:55 +03:00
|
|
|
if (!(e instanceof TypeError)) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-09-05 00:06:26 +03:00
|
|
|
case "UAWidgetSetupOrChange": {
|
2019-04-05 18:51:58 +03:00
|
|
|
if (
|
|
|
|
this.toggleEnabled &&
|
2022-04-06 14:57:57 +03:00
|
|
|
this.contentWindow.HTMLVideoElement.isInstance(event.target) &&
|
2019-11-01 17:15:22 +03:00
|
|
|
event.target.ownerDocument == this.document
|
2019-04-05 18:51:58 +03:00
|
|
|
) {
|
|
|
|
this.registerVideo(event.target);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-08-13 21:29:29 +03:00
|
|
|
case "contextmenu": {
|
|
|
|
if (this.toggleEnabled) {
|
|
|
|
this.checkContextMenu(event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-10-01 21:09:43 +03:00
|
|
|
case "mouseout": {
|
|
|
|
this.onMouseOut(event);
|
|
|
|
break;
|
|
|
|
}
|
2022-03-31 21:45:03 +03:00
|
|
|
case "click":
|
|
|
|
if (event.detail == 0) {
|
|
|
|
let shadowRoot = event.originalTarget.containingShadowRoot;
|
|
|
|
let toggle = this.getToggleElement(shadowRoot);
|
|
|
|
if (event.originalTarget == toggle) {
|
|
|
|
this.startPictureInPicture(event, shadowRoot.host, toggle);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// fall through
|
2019-05-02 20:51:30 +03:00
|
|
|
case "mousedown":
|
|
|
|
case "pointerup":
|
2022-03-31 21:45:03 +03:00
|
|
|
case "mouseup": {
|
2019-05-02 20:51:30 +03:00
|
|
|
this.onMouseButtonEvent(event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "pointerdown": {
|
|
|
|
this.onPointerDown(event);
|
2019-04-05 18:51:58 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "mousemove": {
|
|
|
|
this.onMouseMove(event);
|
|
|
|
break;
|
|
|
|
}
|
2019-10-12 02:36:59 +03:00
|
|
|
case "pageshow": {
|
|
|
|
this.onPageShow(event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "pagehide": {
|
|
|
|
this.onPageHide(event);
|
|
|
|
break;
|
|
|
|
}
|
2019-04-05 18:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a <video> to the IntersectionObserver so that we know when it becomes
|
|
|
|
* visible.
|
|
|
|
*
|
|
|
|
* @param {Element} video The <video> element to register.
|
|
|
|
*/
|
|
|
|
registerVideo(video) {
|
|
|
|
let state = this.docState;
|
|
|
|
if (!state.intersectionObserver) {
|
|
|
|
let fn = this.onIntersection.bind(this);
|
2019-11-01 17:15:22 +03:00
|
|
|
state.intersectionObserver = new this.contentWindow.IntersectionObserver(
|
|
|
|
fn,
|
|
|
|
{
|
|
|
|
threshold: [0.0, 0.5],
|
|
|
|
}
|
|
|
|
);
|
2019-04-05 18:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
state.intersectionObserver.observe(video);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by the IntersectionObserver callback once a video becomes visible.
|
|
|
|
* This adds some fine-grained checking to ensure that a sufficient amount of
|
|
|
|
* the video is visible before we consider showing the toggles on it. For now,
|
|
|
|
* that means that the entirety of the video must be in the viewport.
|
|
|
|
*
|
|
|
|
* @param {IntersectionEntry} intersectionEntry An IntersectionEntry passed to
|
|
|
|
* the IntersectionObserver callback.
|
|
|
|
* @return bool Whether or not we should start tracking mousemove events for
|
|
|
|
* this registered video.
|
|
|
|
*/
|
|
|
|
worthTracking(intersectionEntry) {
|
2019-04-15 04:09:26 +03:00
|
|
|
return intersectionEntry.isIntersecting;
|
2019-04-05 18:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by the IntersectionObserver once a video crosses one of the
|
|
|
|
* thresholds dictated by the IntersectionObserver configuration.
|
|
|
|
*
|
|
|
|
* @param {Array<IntersectionEntry>} A collection of one or more
|
|
|
|
* IntersectionEntry's for <video> elements that might have entered or exited
|
|
|
|
* the viewport.
|
|
|
|
*/
|
|
|
|
onIntersection(entries) {
|
|
|
|
// The IntersectionObserver will also fire when a previously intersecting
|
|
|
|
// element is removed from the DOM. We know, however, that the node is
|
|
|
|
// still alive and referrable from the WeakSet because the
|
|
|
|
// IntersectionObserverEntry holds a strong reference to the video.
|
|
|
|
let state = this.docState;
|
2021-03-12 23:38:55 +03:00
|
|
|
if (!state) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-15 04:09:12 +03:00
|
|
|
let oldVisibleVideosCount = state.visibleVideosCount;
|
2019-04-05 18:51:58 +03:00
|
|
|
for (let entry of entries) {
|
|
|
|
let video = entry.target;
|
|
|
|
if (this.worthTracking(entry)) {
|
|
|
|
if (!state.weakVisibleVideos.has(video)) {
|
|
|
|
state.weakVisibleVideos.add(video);
|
2019-04-15 04:09:12 +03:00
|
|
|
state.visibleVideosCount++;
|
2019-04-25 06:35:29 +03:00
|
|
|
if (this.toggleTesting) {
|
|
|
|
gWeakIntersectingVideosForTesting.add(video);
|
|
|
|
}
|
2019-04-05 18:51:58 +03:00
|
|
|
}
|
|
|
|
} else if (state.weakVisibleVideos.has(video)) {
|
|
|
|
state.weakVisibleVideos.delete(video);
|
2019-04-15 04:09:12 +03:00
|
|
|
state.visibleVideosCount--;
|
2019-04-25 06:35:29 +03:00
|
|
|
if (this.toggleTesting) {
|
|
|
|
gWeakIntersectingVideosForTesting.delete(video);
|
|
|
|
}
|
2019-04-05 18:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 06:35:29 +03:00
|
|
|
// For testing, especially in debug or asan builds, we might not
|
|
|
|
// run this idle callback within an acceptable time. While we're
|
|
|
|
// testing, we'll bypass the idle callback performance optimization
|
|
|
|
// and run our callbacks as soon as possible during the next idle
|
|
|
|
// period.
|
2019-04-15 04:09:12 +03:00
|
|
|
if (!oldVisibleVideosCount && state.visibleVideosCount) {
|
2021-02-25 23:24:05 +03:00
|
|
|
if (this.toggleTesting || !this.contentWindow) {
|
2019-04-05 18:51:58 +03:00
|
|
|
this.beginTrackingMouseOverVideos();
|
2019-04-25 06:35:29 +03:00
|
|
|
} else {
|
2019-11-01 17:15:22 +03:00
|
|
|
this.contentWindow.requestIdleCallback(() => {
|
2019-04-25 06:35:29 +03:00
|
|
|
this.beginTrackingMouseOverVideos();
|
|
|
|
});
|
|
|
|
}
|
2019-04-15 04:09:12 +03:00
|
|
|
} else if (oldVisibleVideosCount && !state.visibleVideosCount) {
|
2021-02-25 23:24:05 +03:00
|
|
|
if (this.toggleTesting || !this.contentWindow) {
|
2019-04-05 18:51:58 +03:00
|
|
|
this.stopTrackingMouseOverVideos();
|
2019-04-25 06:35:29 +03:00
|
|
|
} else {
|
2019-11-01 17:15:22 +03:00
|
|
|
this.contentWindow.requestIdleCallback(() => {
|
2019-04-25 06:35:29 +03:00
|
|
|
this.stopTrackingMouseOverVideos();
|
|
|
|
});
|
|
|
|
}
|
2019-04-05 18:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 20:51:30 +03:00
|
|
|
addMouseButtonListeners() {
|
|
|
|
// We want to try to cancel the mouse events from continuing
|
|
|
|
// on into content if the user has clicked on the toggle, so
|
|
|
|
// we don't use the mozSystemGroup here, and add the listener
|
|
|
|
// to the parent target of the window, which in this case,
|
|
|
|
// is the windowRoot. Since this event listener is attached to
|
|
|
|
// part of the outer window, we need to also remove it in a
|
|
|
|
// pagehide event listener in the event that the page unloads
|
|
|
|
// before stopTrackingMouseOverVideos fires.
|
2019-11-01 17:15:22 +03:00
|
|
|
this.contentWindow.windowRoot.addEventListener("pointerdown", this, {
|
2019-05-02 20:51:30 +03:00
|
|
|
capture: true,
|
|
|
|
});
|
2019-11-01 17:15:22 +03:00
|
|
|
this.contentWindow.windowRoot.addEventListener("mousedown", this, {
|
2019-05-02 20:51:30 +03:00
|
|
|
capture: true,
|
|
|
|
});
|
2019-11-01 17:15:22 +03:00
|
|
|
this.contentWindow.windowRoot.addEventListener("mouseup", this, {
|
2019-05-02 20:51:30 +03:00
|
|
|
capture: true,
|
|
|
|
});
|
2019-11-01 17:15:22 +03:00
|
|
|
this.contentWindow.windowRoot.addEventListener("pointerup", this, {
|
2019-05-02 20:51:30 +03:00
|
|
|
capture: true,
|
|
|
|
});
|
2019-11-01 17:15:22 +03:00
|
|
|
this.contentWindow.windowRoot.addEventListener("click", this, {
|
|
|
|
capture: true,
|
|
|
|
});
|
|
|
|
this.contentWindow.windowRoot.addEventListener("mouseout", this, {
|
2019-10-01 21:09:43 +03:00
|
|
|
capture: true,
|
|
|
|
});
|
2022-12-06 19:20:13 +03:00
|
|
|
this.contentWindow.windowRoot.addEventListener("touchstart", this, {
|
|
|
|
capture: true,
|
|
|
|
});
|
2019-05-02 20:51:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
removeMouseButtonListeners() {
|
2020-10-21 02:27:28 +03:00
|
|
|
// This can be null when closing the tab, but the event
|
|
|
|
// listeners should be removed in that case already.
|
2021-02-25 23:24:05 +03:00
|
|
|
if (!this.contentWindow || !this.contentWindow.windowRoot) {
|
2020-10-21 02:27:28 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-01 17:15:22 +03:00
|
|
|
this.contentWindow.windowRoot.removeEventListener("pointerdown", this, {
|
2019-05-02 20:51:30 +03:00
|
|
|
capture: true,
|
|
|
|
});
|
2019-11-01 17:15:22 +03:00
|
|
|
this.contentWindow.windowRoot.removeEventListener("mousedown", this, {
|
2019-05-02 20:51:30 +03:00
|
|
|
capture: true,
|
|
|
|
});
|
2019-11-01 17:15:22 +03:00
|
|
|
this.contentWindow.windowRoot.removeEventListener("mouseup", this, {
|
2019-05-02 20:51:30 +03:00
|
|
|
capture: true,
|
|
|
|
});
|
2019-11-01 17:15:22 +03:00
|
|
|
this.contentWindow.windowRoot.removeEventListener("pointerup", this, {
|
2019-05-02 20:51:30 +03:00
|
|
|
capture: true,
|
|
|
|
});
|
2019-11-01 17:15:22 +03:00
|
|
|
this.contentWindow.windowRoot.removeEventListener("click", this, {
|
2019-05-02 20:51:30 +03:00
|
|
|
capture: true,
|
|
|
|
});
|
2019-11-01 17:15:22 +03:00
|
|
|
this.contentWindow.windowRoot.removeEventListener("mouseout", this, {
|
2019-10-01 21:09:43 +03:00
|
|
|
capture: true,
|
|
|
|
});
|
2022-12-06 19:20:13 +03:00
|
|
|
this.contentWindow.windowRoot.removeEventListener("touchstart", this, {
|
|
|
|
capture: true,
|
|
|
|
});
|
2019-05-02 20:51:30 +03:00
|
|
|
}
|
|
|
|
|
2019-04-05 18:51:58 +03:00
|
|
|
/**
|
|
|
|
* One of the challenges of displaying this toggle is that many sites put
|
|
|
|
* things over top of <video> elements, like custom controls, or images, or
|
|
|
|
* all manner of things that might intercept mouseevents that would normally
|
|
|
|
* fire directly on the <video>. In order to properly detect when the mouse
|
|
|
|
* is over top of one of the <video> elements in this situation, we currently
|
|
|
|
* add a mousemove event handler to the entire document, and stash the most
|
|
|
|
* recent mousemove that fires. At periodic intervals, that stashed mousemove
|
|
|
|
* event is checked to see if it's hovering over one of our registered
|
|
|
|
* <video> elements.
|
|
|
|
*
|
|
|
|
* This sort of thing will not be necessary once bug 1539652 is fixed.
|
|
|
|
*/
|
|
|
|
beginTrackingMouseOverVideos() {
|
|
|
|
let state = this.docState;
|
|
|
|
if (!state.mousemoveDeferredTask) {
|
2022-06-07 07:29:48 +03:00
|
|
|
state.mousemoveDeferredTask = new lazy.DeferredTask(() => {
|
2019-04-05 18:51:58 +03:00
|
|
|
this.checkLastMouseMove();
|
|
|
|
}, MOUSEMOVE_PROCESSING_DELAY_MS);
|
|
|
|
}
|
2019-11-01 17:15:22 +03:00
|
|
|
this.document.addEventListener("mousemove", this, {
|
2019-04-15 04:09:46 +03:00
|
|
|
mozSystemGroup: true,
|
|
|
|
capture: true,
|
|
|
|
});
|
2019-11-01 17:15:22 +03:00
|
|
|
this.contentWindow.addEventListener("pageshow", this, {
|
2019-10-12 02:36:59 +03:00
|
|
|
mozSystemGroup: true,
|
|
|
|
});
|
2019-11-01 17:15:22 +03:00
|
|
|
this.contentWindow.addEventListener("pagehide", this, {
|
2019-10-12 02:36:59 +03:00
|
|
|
mozSystemGroup: true,
|
|
|
|
});
|
2019-05-02 20:51:30 +03:00
|
|
|
this.addMouseButtonListeners();
|
2019-10-12 02:36:59 +03:00
|
|
|
state.isTrackingVideos = true;
|
2019-04-05 18:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If we no longer have any interesting videos in the viewport, we deregister
|
|
|
|
* the mousemove and click listeners, and also remove any toggles that might
|
|
|
|
* be on the page still.
|
|
|
|
*/
|
|
|
|
stopTrackingMouseOverVideos() {
|
|
|
|
let state = this.docState;
|
2020-02-06 20:32:28 +03:00
|
|
|
// We initialize `mousemoveDeferredTask` in `beginTrackingMouseOverVideos`.
|
|
|
|
// If it doesn't exist, that can't have happened. Nothing else ever sets
|
|
|
|
// this value (though we arm/disarm in various places). So we don't need
|
|
|
|
// to do anything else here and can return early.
|
|
|
|
if (!state.mousemoveDeferredTask) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-05 18:51:58 +03:00
|
|
|
state.mousemoveDeferredTask.disarm();
|
2019-11-01 17:15:22 +03:00
|
|
|
this.document.removeEventListener("mousemove", this, {
|
2019-04-15 04:09:46 +03:00
|
|
|
mozSystemGroup: true,
|
|
|
|
capture: true,
|
|
|
|
});
|
2021-02-25 23:24:05 +03:00
|
|
|
if (this.contentWindow) {
|
|
|
|
this.contentWindow.removeEventListener("pageshow", this, {
|
|
|
|
mozSystemGroup: true,
|
|
|
|
});
|
|
|
|
this.contentWindow.removeEventListener("pagehide", this, {
|
|
|
|
mozSystemGroup: true,
|
|
|
|
});
|
|
|
|
}
|
2019-05-02 20:51:30 +03:00
|
|
|
this.removeMouseButtonListeners();
|
2019-11-07 21:31:42 +03:00
|
|
|
let oldOverVideo = this.getWeakOverVideo();
|
2019-04-05 18:51:58 +03:00
|
|
|
if (oldOverVideo) {
|
|
|
|
this.onMouseLeaveVideo(oldOverVideo);
|
|
|
|
}
|
2019-10-12 02:36:59 +03:00
|
|
|
state.isTrackingVideos = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This pageshow event handler will get called if and when we complete a tab
|
|
|
|
* tear out or in. If we happened to be tracking videos before the tear
|
|
|
|
* occurred, we re-add the mouse event listeners so that they're attached to
|
|
|
|
* the right WindowRoot.
|
|
|
|
*
|
|
|
|
* @param {Event} event The pageshow event fired when completing a tab tear
|
|
|
|
* out or in.
|
|
|
|
*/
|
|
|
|
onPageShow(event) {
|
|
|
|
let state = this.docState;
|
|
|
|
if (state.isTrackingVideos) {
|
|
|
|
this.addMouseButtonListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This pagehide event handler will get called if and when we start a tab
|
|
|
|
* tear out or in. If we happened to be tracking videos before the tear
|
|
|
|
* occurred, we remove the mouse event listeners. We'll re-add them when the
|
|
|
|
* pageshow event fires.
|
|
|
|
*
|
|
|
|
* @param {Event} event The pagehide event fired when starting a tab tear
|
|
|
|
* out or in.
|
|
|
|
*/
|
|
|
|
onPageHide(event) {
|
|
|
|
let state = this.docState;
|
|
|
|
if (state.isTrackingVideos) {
|
|
|
|
this.removeMouseButtonListeners();
|
|
|
|
}
|
2019-04-05 18:51:58 +03:00
|
|
|
}
|
|
|
|
|
2019-04-15 04:09:46 +03:00
|
|
|
/**
|
2019-05-02 20:51:30 +03:00
|
|
|
* If we're tracking <video> elements, this pointerdown event handler is run anytime
|
|
|
|
* a pointerdown occurs on the document. This function is responsible for checking
|
2019-04-15 04:09:46 +03:00
|
|
|
* if the user clicked on the Picture-in-Picture toggle. It does this by first
|
|
|
|
* checking if the video is visible beneath the point that was clicked. Then
|
2019-05-02 20:51:30 +03:00
|
|
|
* it tests whether or not the pointerdown occurred within the rectangle of the
|
|
|
|
* toggle. If so, the event's propagation is stopped, and Picture-in-Picture is
|
|
|
|
* triggered.
|
2019-04-15 04:09:46 +03:00
|
|
|
*
|
|
|
|
* @param {Event} event The mousemove event.
|
|
|
|
*/
|
2019-05-02 20:51:30 +03:00
|
|
|
onPointerDown(event) {
|
2019-07-18 03:47:53 +03:00
|
|
|
// The toggle ignores non-primary mouse clicks.
|
|
|
|
if (event.button != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-07 21:31:42 +03:00
|
|
|
let video = this.getWeakOverVideo();
|
2019-04-15 04:09:46 +03:00
|
|
|
if (!video) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let shadowRoot = video.openOrClosedShadowRoot;
|
|
|
|
if (!shadowRoot) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-04 22:43:54 +03:00
|
|
|
let state = this.docState;
|
2022-04-27 17:39:00 +03:00
|
|
|
|
2022-11-19 01:18:36 +03:00
|
|
|
let overVideo = (() => {
|
2022-04-27 17:39:00 +03:00
|
|
|
let { clientX, clientY } = event;
|
|
|
|
let winUtils = this.contentWindow.windowUtils;
|
|
|
|
// We use winUtils.nodesFromRect instead of document.elementsFromPoint,
|
|
|
|
// since document.elementsFromPoint always flushes layout. The 1's in that
|
|
|
|
// function call are for the size of the rect that we want, which is 1x1.
|
|
|
|
//
|
|
|
|
// We pass the aOnlyVisible boolean argument to check that the video isn't
|
|
|
|
// occluded by anything visible at the point of mousedown. If it is, we'll
|
|
|
|
// ignore the mousedown.
|
|
|
|
let elements = winUtils.nodesFromRect(
|
|
|
|
clientX,
|
|
|
|
clientY,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
/* aOnlyVisible = */ true,
|
|
|
|
state.toggleVisibilityThreshold
|
|
|
|
);
|
|
|
|
|
|
|
|
for (let element of elements) {
|
2022-11-19 01:18:36 +03:00
|
|
|
if (element == video || element.containingShadowRoot == shadowRoot) {
|
|
|
|
return true;
|
2022-04-27 17:39:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 01:18:36 +03:00
|
|
|
return false;
|
2022-04-27 17:39:00 +03:00
|
|
|
})();
|
|
|
|
|
2022-11-19 01:18:36 +03:00
|
|
|
if (!overVideo) {
|
2019-04-15 04:09:46 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-19 01:18:36 +03:00
|
|
|
let toggle = this.getToggleElement(shadowRoot);
|
|
|
|
if (this.isMouseOverToggle(toggle, event)) {
|
|
|
|
state.isClickingToggle = true;
|
|
|
|
state.clickedElement = Cu.getWeakReference(event.originalTarget);
|
|
|
|
event.stopImmediatePropagation();
|
2019-07-02 21:08:16 +03:00
|
|
|
|
2022-11-19 01:18:36 +03:00
|
|
|
this.startPictureInPicture(event, video, toggle);
|
|
|
|
}
|
2022-03-31 21:45:03 +03:00
|
|
|
}
|
2019-07-02 21:08:16 +03:00
|
|
|
|
2022-03-31 21:45:03 +03:00
|
|
|
startPictureInPicture(event, video, toggle) {
|
|
|
|
Services.telemetry.keyedScalarAdd(
|
|
|
|
"pictureinpicture.opened_method",
|
|
|
|
"toggle",
|
|
|
|
1
|
|
|
|
);
|
|
|
|
let args = {
|
|
|
|
method: "toggle",
|
|
|
|
firstTimeToggle: (!Services.prefs.getBoolPref(
|
|
|
|
"media.videocontrols.picture-in-picture.video-toggle.has-used"
|
|
|
|
)).toString(),
|
|
|
|
};
|
|
|
|
Services.telemetry.recordEvent(
|
|
|
|
"pictureinpicture",
|
|
|
|
"opened_method",
|
|
|
|
"method",
|
|
|
|
null,
|
|
|
|
args
|
|
|
|
);
|
2019-05-02 20:51:30 +03:00
|
|
|
|
2022-03-31 21:45:03 +03:00
|
|
|
let pipEvent = new this.contentWindow.CustomEvent(
|
|
|
|
"MozTogglePictureInPicture",
|
|
|
|
{
|
|
|
|
bubbles: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
video.dispatchEvent(pipEvent);
|
|
|
|
|
|
|
|
// Since we've initiated Picture-in-Picture, we can go ahead and
|
|
|
|
// hide the toggle now.
|
|
|
|
this.onMouseLeaveVideo(video);
|
2019-04-15 04:09:46 +03:00
|
|
|
}
|
|
|
|
|
2019-05-02 20:51:30 +03:00
|
|
|
/**
|
|
|
|
* Called for mousedown, pointerup, mouseup and click events. If we
|
|
|
|
* detected that the user is clicking on the Picture-in-Picture toggle,
|
|
|
|
* these events are cancelled in the capture-phase before they reach
|
|
|
|
* content. The state for suppressing these events is cleared on the
|
|
|
|
* click event (unless the mouseup occurs on a different element from
|
|
|
|
* the mousedown, in which case, the state is cleared on mouseup).
|
|
|
|
*
|
|
|
|
* @param {Event} event A mousedown, pointerup, mouseup or click event.
|
|
|
|
*/
|
|
|
|
onMouseButtonEvent(event) {
|
2019-07-18 03:47:53 +03:00
|
|
|
// The toggle ignores non-primary mouse clicks.
|
|
|
|
if (event.button != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-02 20:51:30 +03:00
|
|
|
let state = this.docState;
|
|
|
|
if (state.isClickingToggle) {
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
|
|
|
|
// If this is a mouseup event, check to see if we have a record of what
|
|
|
|
// the original target was on pointerdown. If so, and if it doesn't match
|
|
|
|
// the mouseup original target, that means we won't get a click event, and
|
|
|
|
// we can clear the "clicking the toggle" state right away.
|
|
|
|
//
|
|
|
|
// Otherwise, we wait for the click event to do that.
|
|
|
|
let isMouseUpOnOtherElement =
|
|
|
|
event.type == "mouseup" &&
|
|
|
|
(!state.clickedElement ||
|
|
|
|
state.clickedElement.get() != event.originalTarget);
|
|
|
|
|
2023-01-11 04:26:26 +03:00
|
|
|
if (
|
|
|
|
isMouseUpOnOtherElement ||
|
|
|
|
event.type == "click" ||
|
|
|
|
// pointerup event still triggers after a touchstart event. We just need to detect
|
|
|
|
// the pointer type and determine if we got to this part of the code through a touch event.
|
|
|
|
event.pointerType == "touch"
|
|
|
|
) {
|
2019-05-02 20:51:30 +03:00
|
|
|
// The click is complete, so now we reset the state so that
|
|
|
|
// we stop suppressing these events.
|
|
|
|
state.isClickingToggle = false;
|
|
|
|
state.clickedElement = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 21:09:43 +03:00
|
|
|
/**
|
|
|
|
* Called on mouseout events to determine whether or not the mouse has
|
|
|
|
* exited the window.
|
|
|
|
*
|
|
|
|
* @param {Event} event The mouseout event.
|
|
|
|
*/
|
|
|
|
onMouseOut(event) {
|
|
|
|
if (!event.relatedTarget) {
|
|
|
|
// For mouseout events, if there's no relatedTarget (which normally
|
|
|
|
// maps to the element that the mouse entered into) then this means that
|
|
|
|
// we left the window.
|
2019-11-07 21:31:42 +03:00
|
|
|
let video = this.getWeakOverVideo();
|
2019-10-01 21:09:43 +03:00
|
|
|
if (!video) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.onMouseLeaveVideo(video);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 18:51:58 +03:00
|
|
|
/**
|
|
|
|
* Called for each mousemove event when we're tracking those events to
|
|
|
|
* determine if the cursor is hovering over a <video>.
|
|
|
|
*
|
|
|
|
* @param {Event} event The mousemove event.
|
|
|
|
*/
|
|
|
|
onMouseMove(event) {
|
|
|
|
let state = this.docState;
|
2019-07-18 03:50:08 +03:00
|
|
|
|
|
|
|
if (state.hideToggleDeferredTask) {
|
|
|
|
state.hideToggleDeferredTask.disarm();
|
|
|
|
state.hideToggleDeferredTask.arm();
|
|
|
|
}
|
|
|
|
|
2019-04-05 18:51:58 +03:00
|
|
|
state.lastMouseMoveEvent = event;
|
|
|
|
state.mousemoveDeferredTask.arm();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by the DeferredTask after MOUSEMOVE_PROCESSING_DELAY_MS
|
|
|
|
* milliseconds. Checked to see if that mousemove happens to be overtop of
|
|
|
|
* any interesting <video> elements that we want to display the toggle
|
|
|
|
* on. If so, puts the toggle on that video.
|
|
|
|
*/
|
|
|
|
checkLastMouseMove() {
|
|
|
|
let state = this.docState;
|
|
|
|
let event = state.lastMouseMoveEvent;
|
|
|
|
let { clientX, clientY } = event;
|
2022-06-07 07:29:48 +03:00
|
|
|
lazy.logConsole.debug("Visible videos count:", state.visibleVideosCount);
|
|
|
|
lazy.logConsole.debug("Tracking videos:", state.isTrackingVideos);
|
2019-11-01 17:15:22 +03:00
|
|
|
let winUtils = this.contentWindow.windowUtils;
|
2019-04-05 18:51:58 +03:00
|
|
|
// We use winUtils.nodesFromRect instead of document.elementsFromPoint,
|
|
|
|
// since document.elementsFromPoint always flushes layout. The 1's in that
|
|
|
|
// function call are for the size of the rect that we want, which is 1x1.
|
|
|
|
let elements = winUtils.nodesFromRect(
|
|
|
|
clientX,
|
|
|
|
clientY,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
true,
|
2019-04-15 04:08:12 +03:00
|
|
|
false,
|
2022-04-27 17:39:00 +03:00
|
|
|
/* aOnlyVisible = */ true
|
2019-04-15 04:08:12 +03:00
|
|
|
);
|
2019-04-05 18:51:58 +03:00
|
|
|
|
|
|
|
for (let element of elements) {
|
2022-06-07 07:29:48 +03:00
|
|
|
lazy.logConsole.debug("Element id under cursor:", element.id);
|
|
|
|
lazy.logConsole.debug(
|
2022-04-14 02:25:26 +03:00
|
|
|
"Node name of an element under cursor:",
|
|
|
|
element.nodeName
|
|
|
|
);
|
2022-06-07 07:29:48 +03:00
|
|
|
lazy.logConsole.debug(
|
2022-04-14 02:25:26 +03:00
|
|
|
"Supported <video> element:",
|
|
|
|
state.weakVisibleVideos.has(element)
|
|
|
|
);
|
2022-06-07 07:29:48 +03:00
|
|
|
lazy.logConsole.debug(
|
|
|
|
"PiP window is open:",
|
|
|
|
element.isCloningElementVisually
|
|
|
|
);
|
2022-04-14 02:25:26 +03:00
|
|
|
|
2022-04-27 17:39:00 +03:00
|
|
|
// Check for hovering over the video controls or so too, not only
|
|
|
|
// directly over the video.
|
|
|
|
for (let el = element; el; el = el.containingShadowRoot?.host) {
|
|
|
|
if (state.weakVisibleVideos.has(el) && !el.isCloningElementVisually) {
|
2022-06-07 07:29:48 +03:00
|
|
|
lazy.logConsole.debug("Found supported element");
|
2022-04-27 17:39:00 +03:00
|
|
|
this.onMouseOverVideo(el, event);
|
|
|
|
return;
|
|
|
|
}
|
2019-04-05 18:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 21:31:42 +03:00
|
|
|
let oldOverVideo = this.getWeakOverVideo();
|
2019-04-05 18:51:58 +03:00
|
|
|
if (oldOverVideo) {
|
|
|
|
this.onMouseLeaveVideo(oldOverVideo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called once it has been determined that the mouse is overtop of a video
|
|
|
|
* that is in the viewport.
|
|
|
|
*
|
|
|
|
* @param {Element} video The video the mouse is over.
|
|
|
|
*/
|
2019-04-15 04:09:26 +03:00
|
|
|
onMouseOverVideo(video, event) {
|
2019-11-07 21:31:42 +03:00
|
|
|
let oldOverVideo = this.getWeakOverVideo();
|
2019-04-15 04:09:26 +03:00
|
|
|
let shadowRoot = video.openOrClosedShadowRoot;
|
|
|
|
|
2021-03-12 23:38:55 +03:00
|
|
|
if (shadowRoot.firstChild && video != oldOverVideo) {
|
2020-11-09 20:10:28 +03:00
|
|
|
if (video.getTransformToViewport().a == -1) {
|
|
|
|
shadowRoot.firstChild.setAttribute("flipped", true);
|
|
|
|
} else {
|
|
|
|
shadowRoot.firstChild.removeAttribute("flipped");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 04:09:26 +03:00
|
|
|
// It seems from automated testing that if it's still very early on in the
|
|
|
|
// lifecycle of a <video> element, it might not yet have a shadowRoot,
|
|
|
|
// in which case, we can bail out here early.
|
|
|
|
if (!shadowRoot) {
|
|
|
|
if (oldOverVideo) {
|
|
|
|
// We also clear the hover state on the old video we were hovering,
|
|
|
|
// if there was one.
|
|
|
|
this.onMouseLeaveVideo(oldOverVideo);
|
|
|
|
}
|
|
|
|
|
2019-04-05 18:51:58 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-21 03:24:06 +03:00
|
|
|
let state = this.docState;
|
2020-07-22 19:02:13 +03:00
|
|
|
let toggle = this.getToggleElement(shadowRoot);
|
2019-07-18 03:50:08 +03:00
|
|
|
let controlsOverlay = shadowRoot.querySelector(".controlsOverlay");
|
2019-12-21 03:24:06 +03:00
|
|
|
|
2020-08-08 00:47:01 +03:00
|
|
|
if (state.checkedPolicyDocumentURI != this.document.documentURI) {
|
2022-06-07 07:29:48 +03:00
|
|
|
state.togglePolicy = lazy.TOGGLE_POLICIES.DEFAULT;
|
2019-12-21 03:24:06 +03:00
|
|
|
// We cache the matchers process-wide. We'll skip this while running tests to make that
|
|
|
|
// easier.
|
2020-09-11 23:14:09 +03:00
|
|
|
let siteOverrides = this.toggleTesting
|
|
|
|
? PictureInPictureToggleChild.getSiteOverrides()
|
2022-06-07 07:29:48 +03:00
|
|
|
: lazy.gSiteOverrides;
|
2019-12-21 03:24:06 +03:00
|
|
|
|
2022-01-04 00:54:57 +03:00
|
|
|
let visibilityThresholdPref = Services.prefs.getFloatPref(
|
|
|
|
TOGGLE_VISIBILITY_THRESHOLD_PREF,
|
2022-02-09 03:21:14 +03:00
|
|
|
"1.0"
|
2022-01-04 00:54:57 +03:00
|
|
|
);
|
|
|
|
|
2022-03-23 23:01:00 +03:00
|
|
|
if (!this.videoWrapper) {
|
|
|
|
this.videoWrapper = applyWrapper(this, video);
|
|
|
|
}
|
|
|
|
|
2019-12-21 03:24:06 +03:00
|
|
|
// Do we have any toggle overrides? If so, try to apply them.
|
2020-12-04 22:43:54 +03:00
|
|
|
for (let [override, { policy, visibilityThreshold }] of siteOverrides) {
|
|
|
|
if (
|
|
|
|
(policy || visibilityThreshold) &&
|
|
|
|
override.matches(this.document.documentURI)
|
|
|
|
) {
|
2022-03-23 23:01:00 +03:00
|
|
|
state.togglePolicy = this.videoWrapper?.shouldHideToggle(video)
|
2022-06-07 07:29:48 +03:00
|
|
|
? lazy.TOGGLE_POLICIES.HIDDEN
|
|
|
|
: policy || lazy.TOGGLE_POLICIES.DEFAULT;
|
2022-01-04 00:54:57 +03:00
|
|
|
state.toggleVisibilityThreshold =
|
|
|
|
visibilityThreshold || visibilityThresholdPref;
|
2019-12-21 03:24:06 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-08 00:47:01 +03:00
|
|
|
state.checkedPolicyDocumentURI = this.document.documentURI;
|
2019-12-21 03:24:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The built-in <video> controls are along the bottom, which would overlap the
|
|
|
|
// toggle if the override is set to BOTTOM, so we ignore overrides that set
|
|
|
|
// a policy of BOTTOM for <video> elements with controls.
|
|
|
|
if (
|
2022-06-07 07:29:48 +03:00
|
|
|
state.togglePolicy != lazy.TOGGLE_POLICIES.DEFAULT &&
|
|
|
|
!(state.togglePolicy == lazy.TOGGLE_POLICIES.BOTTOM && video.controls)
|
2019-12-21 03:24:06 +03:00
|
|
|
) {
|
2022-06-07 07:29:48 +03:00
|
|
|
toggle.setAttribute(
|
|
|
|
"policy",
|
|
|
|
lazy.TOGGLE_POLICY_STRINGS[state.togglePolicy]
|
|
|
|
);
|
2020-08-08 00:47:01 +03:00
|
|
|
} else {
|
|
|
|
toggle.removeAttribute("policy");
|
2019-12-21 03:24:06 +03:00
|
|
|
}
|
|
|
|
|
2022-06-07 07:29:48 +03:00
|
|
|
const nimbusExperimentVariables = lazy.NimbusFeatures.pictureinpicture.getAllVariables(
|
2022-02-08 22:39:41 +03:00
|
|
|
{ defaultValues: { title: null, message: false, showIconOnly: false } }
|
|
|
|
);
|
|
|
|
// nimbusExperimentVariables will be defaultValues when the experiment is disabled
|
|
|
|
if (nimbusExperimentVariables.title && nimbusExperimentVariables.message) {
|
|
|
|
let pipExplainer = shadowRoot.querySelector(".pip-explainer");
|
|
|
|
let pipLabel = shadowRoot.querySelector(".pip-label");
|
|
|
|
|
|
|
|
pipExplainer.innerText = nimbusExperimentVariables.message;
|
|
|
|
pipLabel.innerText = nimbusExperimentVariables.title;
|
|
|
|
} else if (nimbusExperimentVariables.showIconOnly) {
|
|
|
|
// We only want to show the PiP icon in this experiment scenario
|
|
|
|
let pipExpanded = shadowRoot.querySelector(".pip-expanded");
|
|
|
|
pipExpanded.style.display = "none";
|
2022-04-01 16:54:22 +03:00
|
|
|
let pipSmall = shadowRoot.querySelector(".pip-small");
|
|
|
|
pipSmall.style.opacity = "1";
|
|
|
|
|
|
|
|
let pipIcon = shadowRoot.querySelectorAll(".pip-icon")[1];
|
2022-02-08 22:39:41 +03:00
|
|
|
pipIcon.style.display = "block";
|
|
|
|
}
|
|
|
|
|
2019-07-18 03:50:08 +03:00
|
|
|
controlsOverlay.removeAttribute("hidetoggle");
|
|
|
|
|
|
|
|
// The hideToggleDeferredTask we create here is for automatically hiding
|
|
|
|
// the toggle after a period of no mousemove activity for
|
|
|
|
// TOGGLE_HIDING_TIMEOUT_MS. If the mouse moves, then the DeferredTask
|
|
|
|
// timer is reset.
|
|
|
|
//
|
|
|
|
// We disable the toggle hiding timeout during testing to reduce
|
|
|
|
// non-determinism from timers when testing the toggle.
|
|
|
|
if (!state.hideToggleDeferredTask && !this.toggleTesting) {
|
2022-06-07 07:29:48 +03:00
|
|
|
state.hideToggleDeferredTask = new lazy.DeferredTask(() => {
|
2019-07-18 03:50:08 +03:00
|
|
|
controlsOverlay.setAttribute("hidetoggle", true);
|
|
|
|
}, TOGGLE_HIDING_TIMEOUT_MS);
|
|
|
|
}
|
2019-04-15 04:09:26 +03:00
|
|
|
|
|
|
|
if (oldOverVideo) {
|
|
|
|
if (oldOverVideo == video) {
|
|
|
|
// If we're still hovering the old video, we might have entered or
|
|
|
|
// exited the toggle region.
|
|
|
|
this.checkHoverToggle(toggle, event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We had an old video that we were hovering, and we're not hovering
|
|
|
|
// it anymore. Let's leave it.
|
|
|
|
this.onMouseLeaveVideo(oldOverVideo);
|
|
|
|
}
|
|
|
|
|
2019-04-13 06:22:47 +03:00
|
|
|
state.weakOverVideo = Cu.getWeakReference(video);
|
2019-10-01 21:09:43 +03:00
|
|
|
controlsOverlay.classList.add("hovering");
|
2019-04-15 04:09:26 +03:00
|
|
|
|
2020-08-05 20:49:39 +03:00
|
|
|
if (
|
2022-06-07 07:29:48 +03:00
|
|
|
state.togglePolicy != lazy.TOGGLE_POLICIES.HIDDEN &&
|
2020-08-05 20:49:39 +03:00
|
|
|
!toggle.hasAttribute("hidden")
|
|
|
|
) {
|
|
|
|
Services.telemetry.scalarAdd("pictureinpicture.saw_toggle", 1);
|
2022-02-08 22:39:41 +03:00
|
|
|
const hasUsedPiP = Services.prefs.getBoolPref(
|
|
|
|
"media.videocontrols.picture-in-picture.video-toggle.has-used"
|
|
|
|
);
|
|
|
|
let args = {
|
|
|
|
firstTime: (!hasUsedPiP).toString(),
|
|
|
|
};
|
|
|
|
Services.telemetry.recordEvent(
|
|
|
|
"pictureinpicture",
|
|
|
|
"saw_toggle",
|
|
|
|
"toggle",
|
|
|
|
null,
|
|
|
|
args
|
|
|
|
);
|
2022-02-08 22:39:41 +03:00
|
|
|
// only record if this is the first time seeing the toggle
|
2022-02-08 22:39:41 +03:00
|
|
|
if (!hasUsedPiP) {
|
2022-06-07 07:29:48 +03:00
|
|
|
lazy.NimbusFeatures.pictureinpicture.recordExposureEvent();
|
2022-02-08 22:39:41 +03:00
|
|
|
}
|
2020-08-05 20:49:39 +03:00
|
|
|
}
|
|
|
|
|
2019-04-15 04:09:26 +03:00
|
|
|
// Now that we're hovering the video, we'll check to see if we're
|
|
|
|
// hovering the toggle too.
|
|
|
|
this.checkHoverToggle(toggle, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a mouse event is happening over a toggle element. If it is,
|
2019-10-01 21:09:43 +03:00
|
|
|
* sets the hovering class on it. Otherwise, it clears the hovering
|
|
|
|
* class.
|
2019-04-15 04:09:26 +03:00
|
|
|
*
|
|
|
|
* @param {Element} toggle The Picture-in-Picture toggle to check.
|
|
|
|
* @param {MouseEvent} event A MouseEvent to test.
|
|
|
|
*/
|
|
|
|
checkHoverToggle(toggle, event) {
|
2019-10-01 21:09:43 +03:00
|
|
|
toggle.classList.toggle("hovering", this.isMouseOverToggle(toggle, event));
|
2019-04-13 06:22:47 +03:00
|
|
|
}
|
2019-04-13 04:24:05 +03:00
|
|
|
|
2019-04-13 06:22:47 +03:00
|
|
|
/**
|
|
|
|
* Called once it has been determined that the mouse is no longer overlapping
|
|
|
|
* a video that we'd previously called onMouseOverVideo with.
|
|
|
|
*
|
|
|
|
* @param {Element} video The video that the mouse left.
|
|
|
|
*/
|
|
|
|
onMouseLeaveVideo(video) {
|
2019-04-15 04:09:26 +03:00
|
|
|
let state = this.docState;
|
|
|
|
let shadowRoot = video.openOrClosedShadowRoot;
|
|
|
|
|
|
|
|
if (shadowRoot) {
|
|
|
|
let controlsOverlay = shadowRoot.querySelector(".controlsOverlay");
|
2020-07-22 19:02:13 +03:00
|
|
|
let toggle = this.getToggleElement(shadowRoot);
|
2019-10-01 21:09:43 +03:00
|
|
|
controlsOverlay.classList.remove("hovering");
|
|
|
|
toggle.classList.remove("hovering");
|
2019-04-15 04:09:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
state.weakOverVideo = null;
|
2019-09-14 17:27:37 +03:00
|
|
|
|
|
|
|
if (!this.toggleTesting) {
|
|
|
|
state.hideToggleDeferredTask.disarm();
|
2019-10-01 21:09:43 +03:00
|
|
|
state.mousemoveDeferredTask.disarm();
|
2019-09-14 17:27:37 +03:00
|
|
|
}
|
|
|
|
|
2019-07-18 03:50:08 +03:00
|
|
|
state.hideToggleDeferredTask = null;
|
2019-04-15 04:09:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a reference to a Picture-in-Picture toggle element, determines
|
|
|
|
* if a MouseEvent event is occurring within its bounds.
|
|
|
|
*
|
|
|
|
* @param {Element} toggle The Picture-in-Picture toggle.
|
|
|
|
* @param {MouseEvent} event A MouseEvent to test.
|
|
|
|
*
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
isMouseOverToggle(toggle, event) {
|
|
|
|
let toggleRect = toggle.ownerGlobal.windowUtils.getBoundsWithoutFlushing(
|
2019-07-05 12:04:56 +03:00
|
|
|
toggle
|
2019-04-15 04:09:26 +03:00
|
|
|
);
|
2019-10-01 21:07:14 +03:00
|
|
|
|
2020-10-26 22:16:53 +03:00
|
|
|
// The way the toggle is currently implemented with
|
|
|
|
// absolute positioning, the root toggle element bounds don't actually
|
|
|
|
// contain all of the toggle child element bounds. Until we find a way to
|
|
|
|
// sort that out, we workaround the issue by having each clickable child
|
|
|
|
// elements of the toggle have a clicklable class, and then compute the
|
|
|
|
// smallest rect that contains all of their bounding rects and use that
|
|
|
|
// as the hitbox.
|
2022-06-07 07:29:48 +03:00
|
|
|
toggleRect = lazy.Rect.fromRect(toggleRect);
|
2020-10-26 22:16:53 +03:00
|
|
|
let clickableChildren = toggle.querySelectorAll(".clickable");
|
|
|
|
for (let child of clickableChildren) {
|
2022-06-07 07:29:48 +03:00
|
|
|
let childRect = lazy.Rect.fromRect(
|
2020-10-26 22:16:53 +03:00
|
|
|
child.ownerGlobal.windowUtils.getBoundsWithoutFlushing(child)
|
|
|
|
);
|
|
|
|
toggleRect.expandToContain(childRect);
|
2020-07-22 19:02:13 +03:00
|
|
|
}
|
|
|
|
|
2019-10-01 21:07:14 +03:00
|
|
|
// If the toggle has no dimensions, we're definitely not over it.
|
|
|
|
if (!toggleRect.width || !toggleRect.height) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-15 04:09:26 +03:00
|
|
|
let { clientX, clientY } = event;
|
2019-10-01 21:07:14 +03:00
|
|
|
|
2019-04-15 04:09:26 +03:00
|
|
|
return (
|
|
|
|
clientX >= toggleRect.left &&
|
|
|
|
clientX <= toggleRect.right &&
|
|
|
|
clientY >= toggleRect.top &&
|
|
|
|
clientY <= toggleRect.bottom
|
|
|
|
);
|
2019-04-05 18:51:58 +03:00
|
|
|
}
|
2019-04-25 06:35:29 +03:00
|
|
|
|
2019-08-13 21:29:29 +03:00
|
|
|
/**
|
|
|
|
* Checks a contextmenu event to see if the mouse is currently over the
|
|
|
|
* Picture-in-Picture toggle. If so, sends a message to the parent process
|
|
|
|
* to open up the Picture-in-Picture toggle context menu.
|
|
|
|
*
|
|
|
|
* @param {MouseEvent} event A contextmenu event.
|
|
|
|
*/
|
|
|
|
checkContextMenu(event) {
|
2019-11-07 21:31:42 +03:00
|
|
|
let video = this.getWeakOverVideo();
|
2019-08-13 21:29:29 +03:00
|
|
|
if (!video) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let shadowRoot = video.openOrClosedShadowRoot;
|
|
|
|
if (!shadowRoot) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-22 19:02:13 +03:00
|
|
|
let toggle = this.getToggleElement(shadowRoot);
|
2019-08-13 21:29:29 +03:00
|
|
|
if (this.isMouseOverToggle(toggle, event)) {
|
2022-02-16 15:18:11 +03:00
|
|
|
let devicePixelRatio = toggle.ownerGlobal.devicePixelRatio;
|
2019-11-01 17:15:22 +03:00
|
|
|
this.sendAsyncMessage("PictureInPicture:OpenToggleContextMenu", {
|
2022-02-16 15:18:11 +03:00
|
|
|
screenXDevPx: event.screenX * devicePixelRatio,
|
|
|
|
screenYDevPx: event.screenY * devicePixelRatio,
|
2019-08-13 21:29:29 +03:00
|
|
|
mozInputSource: event.mozInputSource,
|
|
|
|
});
|
2022-02-16 15:18:11 +03:00
|
|
|
event.stopImmediatePropagation();
|
|
|
|
event.preventDefault();
|
2019-08-13 21:29:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 19:02:13 +03:00
|
|
|
/**
|
|
|
|
* Returns the appropriate root element for the Picture-in-Picture toggle,
|
|
|
|
* depending on whether or not we're using the experimental toggle preference.
|
|
|
|
*
|
|
|
|
* @param {Element} shadowRoot The shadowRoot of the video element.
|
|
|
|
* @returns {Element} The toggle element.
|
|
|
|
*/
|
|
|
|
getToggleElement(shadowRoot) {
|
2020-10-26 22:16:53 +03:00
|
|
|
return shadowRoot.getElementById("pictureInPictureToggle");
|
2020-07-22 19:02:13 +03:00
|
|
|
}
|
|
|
|
|
2019-04-25 06:35:29 +03:00
|
|
|
/**
|
|
|
|
* This is a test-only function that returns true if a video is being tracked
|
|
|
|
* for mouseover events after having intersected the viewport.
|
|
|
|
*/
|
|
|
|
static isTracking(video) {
|
|
|
|
return gWeakIntersectingVideosForTesting.has(video);
|
|
|
|
}
|
2019-12-21 03:24:06 +03:00
|
|
|
|
|
|
|
/**
|
2020-09-11 23:14:09 +03:00
|
|
|
* Gets any Picture-in-Picture site-specific overrides stored in the
|
|
|
|
* sharedData struct, and returns them as an Array of two-element Arrays,
|
|
|
|
* where the first element is a MatchPattern and the second element is an
|
2023-02-24 17:00:17 +03:00
|
|
|
* object of the form { policy, disabledKeyboardControls } (where each property
|
2020-09-11 23:14:09 +03:00
|
|
|
* may be missing or undefined).
|
2019-12-21 03:24:06 +03:00
|
|
|
*
|
|
|
|
* @returns {Array<Array<2>>} Array of 2-element Arrays where the first element
|
2020-09-11 23:14:09 +03:00
|
|
|
* is a MatchPattern and the second element is an object with optional policy
|
2023-02-24 17:00:17 +03:00
|
|
|
* and/or disabledKeyboardControls properties.
|
2019-12-21 03:24:06 +03:00
|
|
|
*/
|
2020-09-11 23:14:09 +03:00
|
|
|
static getSiteOverrides() {
|
2019-12-21 03:24:06 +03:00
|
|
|
let result = [];
|
|
|
|
let patterns = Services.cpmm.sharedData.get(
|
2020-09-11 23:14:09 +03:00
|
|
|
"PictureInPicture:SiteOverrides"
|
2019-12-21 03:24:06 +03:00
|
|
|
);
|
|
|
|
for (let pattern in patterns) {
|
|
|
|
let matcher = new MatchPattern(pattern);
|
|
|
|
result.push([matcher, patterns[pattern]]);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2019-04-05 18:51:58 +03:00
|
|
|
}
|
2019-02-12 05:34:38 +03:00
|
|
|
|
2022-09-23 21:11:48 +03:00
|
|
|
export class PictureInPictureChild extends JSWindowActorChild {
|
2022-06-04 19:39:16 +03:00
|
|
|
#subtitlesEnabled = false;
|
2020-10-23 01:04:55 +03:00
|
|
|
// A weak reference to this PiP window's video element
|
|
|
|
weakVideo = null;
|
|
|
|
|
|
|
|
// A weak reference to this PiP window's content window
|
|
|
|
weakPlayerContent = null;
|
|
|
|
|
2022-02-07 21:05:43 +03:00
|
|
|
// A reference to current WebVTT track currently displayed on the content window
|
|
|
|
_currentWebVTTTrack = null;
|
|
|
|
|
|
|
|
observerFunction = null;
|
|
|
|
|
|
|
|
observe(subject, topic, data) {
|
2022-04-06 17:33:54 +03:00
|
|
|
if (topic != "nsPref:changed") {
|
2022-02-07 21:05:43 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:33:54 +03:00
|
|
|
switch (data) {
|
|
|
|
case "media.videocontrols.picture-in-picture.display-text-tracks.enabled": {
|
|
|
|
const originatingVideo = this.getWeakVideo();
|
|
|
|
let isTextTrackPrefEnabled = Services.prefs.getBoolPref(
|
|
|
|
"media.videocontrols.picture-in-picture.display-text-tracks.enabled"
|
|
|
|
);
|
2022-02-07 21:05:43 +03:00
|
|
|
|
2022-04-06 17:33:54 +03:00
|
|
|
// Enable or disable text track support
|
|
|
|
if (isTextTrackPrefEnabled) {
|
|
|
|
this.setupTextTracks(originatingVideo);
|
|
|
|
} else {
|
|
|
|
this.removeTextTracks(originatingVideo);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-02-07 21:05:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 23:06:01 +03:00
|
|
|
/**
|
|
|
|
* Creates a link element with a reference to the css stylesheet needed
|
|
|
|
* for text tracks responsive styling.
|
|
|
|
* @returns {Element} the link element containing text tracks stylesheet.
|
|
|
|
*/
|
|
|
|
createTextTracksStyleSheet() {
|
|
|
|
let headStyleElement = this.document.createElement("link");
|
|
|
|
headStyleElement.setAttribute("rel", "stylesheet");
|
|
|
|
headStyleElement.setAttribute(
|
|
|
|
"href",
|
|
|
|
"chrome://global/skin/pictureinpicture/texttracks.css"
|
|
|
|
);
|
|
|
|
headStyleElement.setAttribute("type", "text/css");
|
|
|
|
return headStyleElement;
|
|
|
|
}
|
|
|
|
|
2022-02-07 21:05:43 +03:00
|
|
|
/**
|
|
|
|
* Sets up Picture-in-Picture to support displaying text tracks from WebVTT
|
2022-02-10 18:24:07 +03:00
|
|
|
* or if WebVTT isn't supported we will register the caption change mutation observer if
|
|
|
|
* the site wrapper exists.
|
2022-02-07 21:05:43 +03:00
|
|
|
*
|
|
|
|
* If the originating video supports WebVTT, try to read the
|
|
|
|
* active track and cues. Display any active cues on the pip window
|
|
|
|
* right away if applicable.
|
|
|
|
*
|
|
|
|
* @param originatingVideo {Element|null}
|
|
|
|
* The <video> being displayed in Picture-in-Picture mode, or null if that <video> no longer exists.
|
|
|
|
*/
|
|
|
|
setupTextTracks(originatingVideo) {
|
|
|
|
const isWebVTTSupported = !!originatingVideo.textTracks?.length;
|
|
|
|
|
|
|
|
if (!isWebVTTSupported) {
|
2022-02-10 18:24:07 +03:00
|
|
|
this.setUpCaptionChangeListener(originatingVideo);
|
2022-02-07 21:05:43 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify active track for originating video
|
|
|
|
this.setActiveTextTrack(originatingVideo.textTracks);
|
|
|
|
|
|
|
|
if (!this._currentWebVTTTrack) {
|
2022-04-28 21:57:16 +03:00
|
|
|
// If WebVTT track is invalid, try using a video wrapper
|
|
|
|
this.setUpCaptionChangeListener(originatingVideo);
|
2022-02-07 21:05:43 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen for changes in tracks and active cues
|
|
|
|
originatingVideo.textTracks.addEventListener("change", this);
|
|
|
|
this._currentWebVTTTrack.addEventListener("cuechange", this.onCueChange);
|
|
|
|
|
|
|
|
const cues = this._currentWebVTTTrack.activeCues;
|
|
|
|
this.updateWebVTTTextTracksDisplay(cues);
|
|
|
|
}
|
|
|
|
|
2022-06-17 07:16:28 +03:00
|
|
|
/**
|
|
|
|
* Toggle the visibility of the subtitles in the PiP window
|
|
|
|
*/
|
|
|
|
toggleTextTracks() {
|
|
|
|
let textTracks = this.document.getElementById("texttracks");
|
|
|
|
textTracks.style.display =
|
|
|
|
textTracks.style.display === "none" ? "" : "none";
|
|
|
|
}
|
|
|
|
|
2022-02-07 21:05:43 +03:00
|
|
|
/**
|
|
|
|
* Removes existing text tracks on the Picture in Picture window.
|
|
|
|
*
|
|
|
|
* If the originating video supports WebVTT, clear references to active
|
|
|
|
* tracks and cues. No longer listen for any track or cue changes.
|
|
|
|
*
|
|
|
|
* @param originatingVideo {Element|null}
|
|
|
|
* The <video> being displayed in Picture-in-Picture mode, or null if that <video> no longer exists.
|
|
|
|
*/
|
|
|
|
removeTextTracks(originatingVideo) {
|
|
|
|
const isWebVTTSupported = !!originatingVideo.textTracks;
|
|
|
|
|
|
|
|
if (!isWebVTTSupported) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No longer listen for changes to tracks and active cues
|
|
|
|
originatingVideo.textTracks.removeEventListener("change", this);
|
|
|
|
this._currentWebVTTTrack?.removeEventListener(
|
|
|
|
"cuechange",
|
|
|
|
this.onCueChange
|
|
|
|
);
|
|
|
|
this._currentWebVTTTrack = null;
|
|
|
|
this.updateWebVTTTextTracksDisplay(null);
|
|
|
|
}
|
|
|
|
|
2022-03-11 03:52:01 +03:00
|
|
|
/**
|
|
|
|
* Moves the text tracks container position above the pip window's video controls
|
|
|
|
* if their positions visually overlap. Since pip controls are within the parent
|
|
|
|
* process, we determine if pip video controls and text tracks visually overlap by
|
|
|
|
* comparing their relative positions with DOMRect.
|
|
|
|
*
|
|
|
|
* If overlap is found, set attribute "overlap-video-controls" to move text tracks
|
|
|
|
* and define a new relative bottom position according to pip window size and the
|
|
|
|
* position of video controls.
|
|
|
|
* @param {Object} data args needed to determine if text tracks must be moved
|
|
|
|
*/
|
|
|
|
moveTextTracks(data) {
|
|
|
|
const {
|
|
|
|
isFullscreen,
|
|
|
|
isVideoControlsShowing,
|
|
|
|
playerBottomControlsDOMRect,
|
2022-11-22 06:52:32 +03:00
|
|
|
isScrubberShowing,
|
2022-03-11 03:52:01 +03:00
|
|
|
} = data;
|
|
|
|
let textTracks = this.document.getElementById("texttracks");
|
|
|
|
const originatingWindow = this.getWeakVideo().ownerGlobal;
|
|
|
|
const isReducedMotionEnabled = originatingWindow.matchMedia(
|
|
|
|
"(prefers-reduced-motion: reduce)"
|
|
|
|
).matches;
|
2022-04-14 22:49:28 +03:00
|
|
|
const textTracksFontScale = this.document
|
|
|
|
.querySelector(":root")
|
|
|
|
.style.getPropertyValue("--font-scale");
|
2022-03-11 03:52:01 +03:00
|
|
|
|
|
|
|
if (isFullscreen || isReducedMotionEnabled) {
|
|
|
|
textTracks.removeAttribute("overlap-video-controls");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isVideoControlsShowing) {
|
|
|
|
let playerVideoRect = textTracks.parentElement.getBoundingClientRect();
|
|
|
|
let isOverlap =
|
2022-04-14 22:49:28 +03:00
|
|
|
playerVideoRect.bottom - textTracksFontScale * playerVideoRect.height >
|
2022-03-11 03:52:01 +03:00
|
|
|
playerBottomControlsDOMRect.top;
|
|
|
|
|
|
|
|
if (isOverlap) {
|
2022-11-22 06:52:32 +03:00
|
|
|
const root = this.document.querySelector(":root");
|
|
|
|
if (isScrubberShowing) {
|
|
|
|
root.style.setProperty("--player-controls-scrubber-height", "30px");
|
|
|
|
} else {
|
|
|
|
root.style.setProperty("--player-controls-scrubber-height", "0px");
|
|
|
|
}
|
2022-03-11 03:52:01 +03:00
|
|
|
textTracks.setAttribute("overlap-video-controls", true);
|
|
|
|
} else {
|
|
|
|
textTracks.removeAttribute("overlap-video-controls");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
textTracks.removeAttribute("overlap-video-controls");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 21:05:43 +03:00
|
|
|
/**
|
|
|
|
* Updates the text content for the container that holds and displays text tracks
|
|
|
|
* on the pip window.
|
|
|
|
* @param textTrackCues {TextTrackCueList|null}
|
|
|
|
* Collection of TextTrackCue objects containing text displayed, or null if there is no cue to display.
|
|
|
|
*/
|
|
|
|
updateWebVTTTextTracksDisplay(textTrackCues) {
|
|
|
|
let pipWindowTracksContainer = this.document.getElementById("texttracks");
|
|
|
|
let playerVideo = this.document.getElementById("playervideo");
|
|
|
|
let playerVideoWindow = playerVideo.ownerGlobal;
|
|
|
|
|
|
|
|
// To prevent overlap with previous cues, clear all text from the pip window
|
|
|
|
pipWindowTracksContainer.replaceChildren();
|
|
|
|
|
|
|
|
if (!textTrackCues) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-04 19:39:16 +03:00
|
|
|
if (!this.isSubtitlesEnabled) {
|
|
|
|
this.isSubtitlesEnabled = true;
|
2023-01-25 18:14:28 +03:00
|
|
|
this.sendAsyncMessage("PictureInPicture:EnableSubtitlesButton");
|
2022-06-04 19:39:16 +03:00
|
|
|
}
|
|
|
|
|
2022-02-11 23:06:06 +03:00
|
|
|
let allCuesArray = [...textTrackCues];
|
|
|
|
// Re-order cues
|
|
|
|
this.getOrderedWebVTTCues(allCuesArray);
|
2022-02-07 21:05:43 +03:00
|
|
|
// Parse through WebVTT cue using vtt.js to ensure
|
|
|
|
// semantic markup like <b> and <i> tags are rendered.
|
|
|
|
allCuesArray.forEach(cue => {
|
|
|
|
let text = cue.text;
|
2022-05-06 23:31:31 +03:00
|
|
|
// Trim extra newlines and whitespaces
|
|
|
|
const re = /(\s*\n{2,}\s*)/g;
|
|
|
|
text = text.trim();
|
|
|
|
text = text.replace(re, "\n");
|
2022-02-07 21:05:43 +03:00
|
|
|
let cueTextNode = WebVTT.convertCueToDOMTree(playerVideoWindow, text);
|
|
|
|
let cueDiv = this.document.createElement("div");
|
|
|
|
cueDiv.appendChild(cueTextNode);
|
|
|
|
pipWindowTracksContainer.appendChild(cueDiv);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-11 23:06:06 +03:00
|
|
|
/**
|
|
|
|
* Re-orders list of multiple active cues to ensure cues are rendered in the correct order.
|
|
|
|
* How cues are ordered depends on the VTTCue.line value of the cue.
|
|
|
|
*
|
|
|
|
* If line is string "auto", we want to reverse the order of cues.
|
|
|
|
* Cues are read from top to bottom in a vtt file, but are inserted into a video from bottom to top.
|
|
|
|
* Ensure this order is followed.
|
|
|
|
*
|
|
|
|
* If line is an integer or percentage, we want to order cues according to numeric value.
|
|
|
|
* Assumptions:
|
|
|
|
* 1) all active cues are numeric
|
|
|
|
* 2) all active cues are in range 0..100
|
|
|
|
* 3) all actives cue are horizontal (no VTTCue.vertical)
|
|
|
|
* 4) all active cues with VTTCue.line integer have VTTCue.snapToLines = true
|
|
|
|
* 5) all active cues with VTTCue.line percentage have VTTCue.snapToLines = false
|
|
|
|
*
|
|
|
|
* vtt.jsm currently sets snapToLines to false if line is a percentage value, but
|
|
|
|
* cues are still ordered by line. In most cases, snapToLines is set to true by default,
|
|
|
|
* unless intentionally overridden.
|
|
|
|
* @param allCuesArray {Array<VTTCue>} array of active cues
|
|
|
|
*/
|
|
|
|
getOrderedWebVTTCues(allCuesArray) {
|
|
|
|
if (!allCuesArray || allCuesArray.length <= 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let allCuesHaveNumericLines = allCuesArray.find(cue => cue.line !== "auto");
|
|
|
|
|
|
|
|
if (allCuesHaveNumericLines) {
|
|
|
|
allCuesArray.sort((cue1, cue2) => cue1.line - cue2.line);
|
|
|
|
} else if (allCuesArray.length >= 2) {
|
|
|
|
allCuesArray.reverse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 01:04:55 +03:00
|
|
|
/**
|
|
|
|
* Returns a reference to the PiP's <video> element being displayed in Picture-in-Picture
|
|
|
|
* mode.
|
|
|
|
*
|
|
|
|
* @return {Element} The <video> being displayed in Picture-in-Picture mode, or null
|
|
|
|
* if that <video> no longer exists.
|
|
|
|
*/
|
|
|
|
getWeakVideo() {
|
|
|
|
if (this.weakVideo) {
|
|
|
|
// Bug 800957 - Accessing weakrefs at the wrong time can cause us to
|
|
|
|
// throw NS_ERROR_XPC_BAD_CONVERT_NATIVE
|
|
|
|
try {
|
|
|
|
return this.weakVideo.get();
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a reference to the inner window of the about:blank document that is
|
|
|
|
* cloning the originating <video> in the always-on-top player <xul:browser>.
|
|
|
|
*
|
|
|
|
* @return {Window} The inner window of the about:blank player <xul:browser>, or
|
|
|
|
* null if that window has been closed.
|
|
|
|
*/
|
|
|
|
getWeakPlayerContent() {
|
|
|
|
if (this.weakPlayerContent) {
|
|
|
|
// Bug 800957 - Accessing weakrefs at the wrong time can cause us to
|
|
|
|
// throw NS_ERROR_XPC_BAD_CONVERT_NATIVE
|
|
|
|
try {
|
|
|
|
return this.weakPlayerContent.get();
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the passed video happens to be the one that this
|
|
|
|
* content process is running in a Picture-in-Picture window.
|
|
|
|
*
|
|
|
|
* @param {Element} video The <video> element to check.
|
|
|
|
*
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
inPictureInPicture(video) {
|
|
|
|
return this.getWeakVideo() === video;
|
|
|
|
}
|
|
|
|
|
2019-04-04 22:04:47 +03:00
|
|
|
static videoIsPlaying(video) {
|
2020-08-04 00:02:31 +03:00
|
|
|
return !!(!video.paused && !video.ended && video.readyState > 2);
|
2019-04-04 22:04:47 +03:00
|
|
|
}
|
|
|
|
|
2019-11-05 00:14:50 +03:00
|
|
|
static videoIsMuted(video) {
|
2022-05-23 07:05:20 +03:00
|
|
|
return this.videoWrapper.isMuted(video);
|
2019-11-05 00:14:50 +03:00
|
|
|
}
|
|
|
|
|
2019-02-12 05:34:38 +03:00
|
|
|
handleEvent(event) {
|
|
|
|
switch (event.type) {
|
2019-10-01 19:20:51 +03:00
|
|
|
case "MozStopPictureInPicture": {
|
2020-10-23 01:04:55 +03:00
|
|
|
if (event.isTrusted && event.target === this.getWeakVideo()) {
|
2020-10-16 20:50:49 +03:00
|
|
|
const reason = event.detail?.reason || "video-el-remove";
|
|
|
|
this.closePictureInPicture({ reason });
|
2019-10-01 19:20:51 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-03-23 02:33:53 +03:00
|
|
|
case "pagehide": {
|
|
|
|
// The originating video's content document has unloaded,
|
|
|
|
// so close Picture-in-Picture.
|
2019-07-02 21:08:16 +03:00
|
|
|
this.closePictureInPicture({ reason: "pagehide" });
|
2019-03-23 02:33:53 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-09-12 23:47:04 +03:00
|
|
|
case "MozDOMFullscreen:Request": {
|
|
|
|
this.closePictureInPicture({ reason: "fullscreen" });
|
|
|
|
break;
|
|
|
|
}
|
2019-04-04 22:04:47 +03:00
|
|
|
case "play": {
|
2019-11-01 17:15:22 +03:00
|
|
|
this.sendAsyncMessage("PictureInPicture:Playing");
|
2019-04-04 22:04:47 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "pause": {
|
2019-11-01 17:15:22 +03:00
|
|
|
this.sendAsyncMessage("PictureInPicture:Paused");
|
2019-04-04 22:04:47 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-11-05 00:14:50 +03:00
|
|
|
case "volumechange": {
|
2020-10-23 01:04:55 +03:00
|
|
|
let video = this.getWeakVideo();
|
2019-11-07 21:31:42 +03:00
|
|
|
|
|
|
|
// Just double-checking that we received the event for the right
|
|
|
|
// video element.
|
|
|
|
if (video !== event.target) {
|
2022-11-15 00:27:47 +03:00
|
|
|
lazy.logConsole.error(
|
2019-11-07 21:31:42 +03:00
|
|
|
"PictureInPictureChild received volumechange for " +
|
|
|
|
"the wrong video!"
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-25 19:08:04 +03:00
|
|
|
if (this.constructor.videoIsMuted(video)) {
|
2019-11-05 00:14:50 +03:00
|
|
|
this.sendAsyncMessage("PictureInPicture:Muting");
|
|
|
|
} else {
|
|
|
|
this.sendAsyncMessage("PictureInPicture:Unmuting");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-12-05 19:59:04 +03:00
|
|
|
case "resize": {
|
|
|
|
let video = event.target;
|
2020-10-23 01:04:55 +03:00
|
|
|
if (this.inPictureInPicture(video)) {
|
2019-12-05 19:59:04 +03:00
|
|
|
this.sendAsyncMessage("PictureInPicture:Resize", {
|
|
|
|
videoHeight: video.videoHeight,
|
|
|
|
videoWidth: video.videoWidth,
|
|
|
|
});
|
|
|
|
}
|
2022-02-10 18:24:07 +03:00
|
|
|
this.setupTextTracks(video);
|
2019-12-05 19:59:04 +03:00
|
|
|
break;
|
|
|
|
}
|
2022-03-23 22:47:47 +03:00
|
|
|
case "emptied": {
|
2022-07-06 20:30:29 +03:00
|
|
|
this.isSubtitlesEnabled = false;
|
2022-03-23 22:47:47 +03:00
|
|
|
if (this.emptiedTimeout) {
|
|
|
|
clearTimeout(this.emptiedTimeout);
|
|
|
|
this.emptiedTimeout = null;
|
|
|
|
}
|
|
|
|
let video = this.getWeakVideo();
|
|
|
|
// We may want to keep the pip window open if the video
|
|
|
|
// is still in DOM. But if video src is no longer defined,
|
|
|
|
// close Picture-in-Picture.
|
|
|
|
this.emptiedTimeout = setTimeout(() => {
|
|
|
|
if (!video || !video.src) {
|
|
|
|
this.closePictureInPicture({ reason: "video-el-emptied" });
|
|
|
|
}
|
|
|
|
}, EMPTIED_TIMEOUT_MS);
|
|
|
|
break;
|
|
|
|
}
|
2022-02-07 21:05:43 +03:00
|
|
|
case "change": {
|
|
|
|
// Clear currently stored track data (webvtt support) before reading
|
|
|
|
// a new track.
|
|
|
|
if (this._currentWebVTTTrack) {
|
|
|
|
this._currentWebVTTTrack.removeEventListener(
|
|
|
|
"cuechange",
|
|
|
|
this.onCueChange
|
|
|
|
);
|
|
|
|
this._currentWebVTTTrack = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tracks = event.target;
|
|
|
|
this.setActiveTextTrack(tracks);
|
|
|
|
const isCurrentTrackAvailable = this._currentWebVTTTrack;
|
|
|
|
|
|
|
|
// If tracks are disabled or invalid while change occurs,
|
|
|
|
// remove text tracks from the pip window and stop here.
|
|
|
|
if (!isCurrentTrackAvailable || !tracks.length) {
|
|
|
|
this.updateWebVTTTextTracksDisplay(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._currentWebVTTTrack.addEventListener(
|
|
|
|
"cuechange",
|
|
|
|
this.onCueChange
|
|
|
|
);
|
|
|
|
const cues = this._currentWebVTTTrack.activeCues;
|
|
|
|
this.updateWebVTTTextTracksDisplay(cues);
|
|
|
|
break;
|
|
|
|
}
|
2022-12-22 19:29:47 +03:00
|
|
|
case "timeupdate":
|
|
|
|
case "durationchange": {
|
2023-01-26 23:28:41 +03:00
|
|
|
let video = this.getWeakVideo();
|
|
|
|
let currentTime = this.videoWrapper.getCurrentTime(video);
|
|
|
|
let duration = this.videoWrapper.getDuration(video);
|
2022-11-22 06:52:32 +03:00
|
|
|
let scrubberPosition = currentTime === 0 ? 0 : currentTime / duration;
|
2023-01-20 23:39:16 +03:00
|
|
|
let timestamp = this.videoWrapper.formatTimestamp(
|
|
|
|
currentTime,
|
|
|
|
duration
|
2022-12-22 19:29:47 +03:00
|
|
|
);
|
2023-01-20 23:39:16 +03:00
|
|
|
// There's no point in sending this message unless we have a
|
|
|
|
// reasonable timestamp.
|
2023-01-25 02:53:23 +03:00
|
|
|
if (timestamp !== undefined && lazy.IMPROVED_CONTROLS_ENABLED_PREF) {
|
2023-01-20 23:39:16 +03:00
|
|
|
this.sendAsyncMessage(
|
|
|
|
"PictureInPicture:SetTimestampAndScrubberPosition",
|
|
|
|
{
|
|
|
|
scrubberPosition,
|
|
|
|
timestamp,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2022-11-22 06:52:32 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-02-12 05:34:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-23 02:33:53 +03:00
|
|
|
/**
|
|
|
|
* Tells the parent to close a pre-existing Picture-in-Picture
|
|
|
|
* window.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*
|
|
|
|
* @resolves {undefined} Once the pre-existing Picture-in-Picture
|
|
|
|
* window has unloaded.
|
|
|
|
*/
|
2019-07-02 21:08:16 +03:00
|
|
|
async closePictureInPicture({ reason }) {
|
2020-10-23 01:04:55 +03:00
|
|
|
let video = this.getWeakVideo();
|
2019-11-07 21:31:42 +03:00
|
|
|
if (video) {
|
|
|
|
this.untrackOriginatingVideo(video);
|
2019-03-23 02:33:53 +03:00
|
|
|
}
|
2019-11-01 17:15:22 +03:00
|
|
|
this.sendAsyncMessage("PictureInPicture:Close", {
|
2019-07-02 21:08:16 +03:00
|
|
|
reason,
|
2019-02-12 05:34:38 +03:00
|
|
|
});
|
2019-03-23 02:32:50 +03:00
|
|
|
|
2019-11-07 21:31:42 +03:00
|
|
|
let playerContent = this.getWeakPlayerContent();
|
|
|
|
if (playerContent) {
|
|
|
|
if (!playerContent.closed) {
|
2019-04-25 06:35:09 +03:00
|
|
|
await new Promise(resolve => {
|
2019-11-07 21:31:42 +03:00
|
|
|
playerContent.addEventListener("unload", resolve, {
|
2019-04-25 06:35:09 +03:00
|
|
|
once: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-03-23 02:32:50 +03:00
|
|
|
// Nothing should be holding a reference to the Picture-in-Picture
|
|
|
|
// player window content at this point, but just in case, we'll
|
|
|
|
// clear the weak reference directly so nothing else can get a hold
|
|
|
|
// of it from this angle.
|
2020-10-23 01:04:55 +03:00
|
|
|
this.weakPlayerContent = null;
|
2019-03-23 02:32:50 +03:00
|
|
|
}
|
2019-02-12 05:34:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
receiveMessage(message) {
|
|
|
|
switch (message.name) {
|
|
|
|
case "PictureInPicture:SetupPlayer": {
|
2020-10-08 16:51:24 +03:00
|
|
|
const { videoRef } = message.data;
|
|
|
|
this.setupPlayer(videoRef);
|
2019-02-12 05:34:38 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-04-04 22:04:47 +03:00
|
|
|
case "PictureInPicture:Play": {
|
|
|
|
this.play();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "PictureInPicture:Pause": {
|
2020-11-19 03:09:00 +03:00
|
|
|
if (message.data && message.data.reason == "pip-closed") {
|
|
|
|
let video = this.getWeakVideo();
|
|
|
|
|
|
|
|
// Currently in Firefox srcObjects are MediaStreams. However, by spec a srcObject
|
|
|
|
// can be either a MediaStream, MediaSource or Blob. In case of future changes
|
|
|
|
// we do not want to pause MediaStream srcObjects and we want to maintain current
|
|
|
|
// behavior for non-MediaStream srcObjects.
|
2022-04-06 14:57:57 +03:00
|
|
|
if (video && MediaStream.isInstance(video.srcObject)) {
|
2020-11-19 03:09:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 22:04:47 +03:00
|
|
|
this.pause();
|
|
|
|
break;
|
|
|
|
}
|
2019-11-05 00:14:50 +03:00
|
|
|
case "PictureInPicture:Mute": {
|
|
|
|
this.mute();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "PictureInPicture:Unmute": {
|
|
|
|
this.unmute();
|
|
|
|
break;
|
|
|
|
}
|
2022-09-01 21:33:10 +03:00
|
|
|
case "PictureInPicture:SeekForward":
|
|
|
|
case "PictureInPicture:SeekBackward": {
|
|
|
|
let selectedTime;
|
|
|
|
let video = this.getWeakVideo();
|
|
|
|
let currentTime = this.videoWrapper.getCurrentTime(video);
|
|
|
|
if (message.name == "PictureInPicture:SeekBackward") {
|
|
|
|
selectedTime = currentTime - SEEK_TIME_SECS;
|
|
|
|
selectedTime = selectedTime >= 0 ? selectedTime : 0;
|
|
|
|
} else {
|
|
|
|
const maxtime = this.videoWrapper.getDuration(video);
|
|
|
|
selectedTime = currentTime + SEEK_TIME_SECS;
|
|
|
|
selectedTime = selectedTime <= maxtime ? selectedTime : maxtime;
|
|
|
|
}
|
|
|
|
this.videoWrapper.setCurrentTime(video, selectedTime);
|
|
|
|
break;
|
|
|
|
}
|
2020-01-26 09:21:23 +03:00
|
|
|
case "PictureInPicture:KeyDown": {
|
|
|
|
this.keyDown(message.data);
|
|
|
|
break;
|
|
|
|
}
|
2022-03-11 03:52:01 +03:00
|
|
|
case "PictureInPicture:EnterFullscreen":
|
|
|
|
case "PictureInPicture:ExitFullscreen": {
|
|
|
|
let textTracks = this.document.getElementById("texttracks");
|
|
|
|
if (textTracks) {
|
|
|
|
this.moveTextTracks(message.data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "PictureInPicture:ShowVideoControls":
|
|
|
|
case "PictureInPicture:HideVideoControls": {
|
|
|
|
let textTracks = this.document.getElementById("texttracks");
|
|
|
|
if (textTracks) {
|
|
|
|
this.moveTextTracks(message.data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-06-17 07:16:28 +03:00
|
|
|
case "PictureInPicture:ToggleTextTracks": {
|
|
|
|
this.toggleTextTracks();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "PictureInPicture:ChangeFontSizeTextTracks": {
|
|
|
|
this.setTextTrackFontSize();
|
|
|
|
break;
|
|
|
|
}
|
2022-11-22 06:52:32 +03:00
|
|
|
case "PictureInPicture:SetVideoTime": {
|
2023-01-10 01:04:55 +03:00
|
|
|
const { scrubberPosition, wasPlaying } = message.data;
|
|
|
|
this.setVideoTime(scrubberPosition, wasPlaying);
|
2022-11-22 06:52:32 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-02-12 05:34:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 06:52:32 +03:00
|
|
|
/**
|
|
|
|
* Set the current time of the video based of the position of the scrubber
|
|
|
|
* @param {Number} scrubberPosition A number between 0 and 1 representing the position of the scrubber
|
|
|
|
*/
|
2023-01-10 01:04:55 +03:00
|
|
|
setVideoTime(scrubberPosition, wasPlaying) {
|
2022-11-22 06:52:32 +03:00
|
|
|
const video = this.getWeakVideo();
|
|
|
|
let duration = this.videoWrapper.getDuration(video);
|
|
|
|
let currentTime = scrubberPosition * duration;
|
2023-01-10 01:04:55 +03:00
|
|
|
this.videoWrapper.setCurrentTime(video, currentTime, wasPlaying);
|
2022-11-22 06:52:32 +03:00
|
|
|
}
|
|
|
|
|
2022-02-07 21:05:43 +03:00
|
|
|
/**
|
|
|
|
* Updates this._currentWebVTTTrack if an active track is found
|
|
|
|
* for the originating video.
|
|
|
|
* @param {TextTrackList} textTrackList list of text tracks
|
|
|
|
*/
|
|
|
|
setActiveTextTrack(textTrackList) {
|
|
|
|
this._currentWebVTTTrack = null;
|
|
|
|
|
|
|
|
for (let i = 0; i < textTrackList.length; i++) {
|
|
|
|
let track = textTrackList[i];
|
|
|
|
let isCCText = track.kind === "subtitles" || track.kind === "captions";
|
2022-04-28 21:57:16 +03:00
|
|
|
if (isCCText && track.mode === "showing" && track.cues) {
|
2022-02-07 21:05:43 +03:00
|
|
|
this._currentWebVTTTrack = track;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 07:16:28 +03:00
|
|
|
/**
|
|
|
|
* Set the font size on the PiP window using the current font size value from
|
|
|
|
* the "media.videocontrols.picture-in-picture.display-text-tracks.size" pref
|
|
|
|
*/
|
2022-04-06 17:33:54 +03:00
|
|
|
setTextTrackFontSize() {
|
|
|
|
const fontSize = Services.prefs.getStringPref(
|
|
|
|
TEXT_TRACK_FONT_SIZE,
|
|
|
|
"medium"
|
|
|
|
);
|
|
|
|
const root = this.document.querySelector(":root");
|
|
|
|
if (fontSize === "small") {
|
|
|
|
root.style.setProperty("--font-scale", "0.03");
|
|
|
|
} else if (fontSize === "large") {
|
|
|
|
root.style.setProperty("--font-scale", "0.09");
|
|
|
|
} else {
|
|
|
|
root.style.setProperty("--font-scale", "0.06");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-23 02:33:53 +03:00
|
|
|
/**
|
|
|
|
* Keeps an eye on the originating video's document. If it ever
|
|
|
|
* goes away, this will cause the Picture-in-Picture window for any
|
|
|
|
* of its content to go away as well.
|
|
|
|
*/
|
|
|
|
trackOriginatingVideo(originatingVideo) {
|
2022-02-07 21:05:43 +03:00
|
|
|
this.observerFunction = (subject, topic, data) => {
|
|
|
|
this.observe(subject, topic, data);
|
|
|
|
};
|
|
|
|
Services.prefs.addObserver(
|
|
|
|
"media.videocontrols.picture-in-picture.display-text-tracks.enabled",
|
|
|
|
this.observerFunction
|
|
|
|
);
|
|
|
|
|
2019-03-23 02:33:53 +03:00
|
|
|
let originatingWindow = originatingVideo.ownerGlobal;
|
|
|
|
if (originatingWindow) {
|
|
|
|
originatingWindow.addEventListener("pagehide", this);
|
2019-04-04 22:04:47 +03:00
|
|
|
originatingVideo.addEventListener("play", this);
|
|
|
|
originatingVideo.addEventListener("pause", this);
|
2019-11-05 00:14:50 +03:00
|
|
|
originatingVideo.addEventListener("volumechange", this);
|
2019-12-05 19:59:04 +03:00
|
|
|
originatingVideo.addEventListener("resize", this);
|
2022-03-23 22:47:47 +03:00
|
|
|
originatingVideo.addEventListener("emptied", this);
|
2022-11-22 06:52:32 +03:00
|
|
|
originatingVideo.addEventListener("timeupdate", this);
|
2020-09-12 23:47:04 +03:00
|
|
|
|
2022-06-07 07:29:48 +03:00
|
|
|
if (lazy.DISPLAY_TEXT_TRACKS_PREF) {
|
2022-02-07 21:05:43 +03:00
|
|
|
this.setupTextTracks(originatingVideo);
|
|
|
|
}
|
|
|
|
|
2020-09-12 23:47:04 +03:00
|
|
|
let chromeEventHandler = originatingWindow.docShell.chromeEventHandler;
|
|
|
|
chromeEventHandler.addEventListener(
|
|
|
|
"MozDOMFullscreen:Request",
|
|
|
|
this,
|
|
|
|
true
|
|
|
|
);
|
2020-10-23 01:04:55 +03:00
|
|
|
chromeEventHandler.addEventListener(
|
|
|
|
"MozStopPictureInPicture",
|
|
|
|
this,
|
|
|
|
true
|
|
|
|
);
|
2019-03-23 02:33:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 18:24:07 +03:00
|
|
|
setUpCaptionChangeListener(originatingVideo) {
|
|
|
|
if (this.videoWrapper) {
|
|
|
|
this.videoWrapper.setCaptionContainerObserver(originatingVideo, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-23 02:33:53 +03:00
|
|
|
/**
|
|
|
|
* Stops tracking the originating video's document. This should
|
|
|
|
* happen once the Picture-in-Picture window goes away (or is about
|
|
|
|
* to go away), and we no longer care about hearing when the originating
|
|
|
|
* window's document unloads.
|
|
|
|
*/
|
|
|
|
untrackOriginatingVideo(originatingVideo) {
|
2022-02-07 21:05:43 +03:00
|
|
|
Services.prefs.removeObserver(
|
|
|
|
"media.videocontrols.picture-in-picture.display-text-tracks.enabled",
|
|
|
|
this.observerFunction
|
|
|
|
);
|
|
|
|
|
2019-03-23 02:33:53 +03:00
|
|
|
let originatingWindow = originatingVideo.ownerGlobal;
|
|
|
|
if (originatingWindow) {
|
|
|
|
originatingWindow.removeEventListener("pagehide", this);
|
2019-04-04 22:04:47 +03:00
|
|
|
originatingVideo.removeEventListener("play", this);
|
|
|
|
originatingVideo.removeEventListener("pause", this);
|
2019-11-05 00:14:50 +03:00
|
|
|
originatingVideo.removeEventListener("volumechange", this);
|
2019-12-05 19:59:04 +03:00
|
|
|
originatingVideo.removeEventListener("resize", this);
|
2022-03-23 22:47:47 +03:00
|
|
|
originatingVideo.removeEventListener("emptied", this);
|
2022-11-22 06:52:32 +03:00
|
|
|
originatingVideo.removeEventListener("timeupdate", this);
|
2020-09-12 23:47:04 +03:00
|
|
|
|
2022-06-07 07:29:48 +03:00
|
|
|
if (lazy.DISPLAY_TEXT_TRACKS_PREF) {
|
2022-02-07 21:05:43 +03:00
|
|
|
this.removeTextTracks(originatingVideo);
|
|
|
|
}
|
|
|
|
|
2020-09-12 23:47:04 +03:00
|
|
|
let chromeEventHandler = originatingWindow.docShell.chromeEventHandler;
|
|
|
|
chromeEventHandler.removeEventListener(
|
|
|
|
"MozDOMFullscreen:Request",
|
|
|
|
this,
|
|
|
|
true
|
|
|
|
);
|
2020-10-23 01:04:55 +03:00
|
|
|
chromeEventHandler.removeEventListener(
|
|
|
|
"MozStopPictureInPicture",
|
|
|
|
this,
|
|
|
|
true
|
|
|
|
);
|
2019-03-23 02:33:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs in an instance of PictureInPictureChild for the
|
|
|
|
* player window's content, and not the originating video
|
|
|
|
* content. Sets up the player so that it clones the originating
|
|
|
|
* video. If anything goes wrong during set up, a message is
|
|
|
|
* sent to the parent to close the Picture-in-Picture window.
|
|
|
|
*
|
2020-10-08 16:51:24 +03:00
|
|
|
* @param videoRef {ContentDOMReference}
|
|
|
|
* A reference to the video element that a Picture-in-Picture window
|
|
|
|
* is being created for
|
2019-03-23 02:33:53 +03:00
|
|
|
* @return {Promise}
|
|
|
|
* @resolves {undefined} Once the player window has been set up
|
|
|
|
* properly, or a pre-existing Picture-in-Picture window has gone
|
|
|
|
* away due to an unexpected error.
|
|
|
|
*/
|
2020-10-08 16:51:24 +03:00
|
|
|
async setupPlayer(videoRef) {
|
2022-06-07 07:29:48 +03:00
|
|
|
const video = await lazy.ContentDOMReference.resolve(videoRef);
|
2020-10-08 16:51:24 +03:00
|
|
|
|
2020-10-23 01:04:55 +03:00
|
|
|
this.weakVideo = Cu.getWeakReference(video);
|
|
|
|
let originatingVideo = this.getWeakVideo();
|
2019-02-12 05:34:38 +03:00
|
|
|
if (!originatingVideo) {
|
2019-03-23 02:32:50 +03:00
|
|
|
// If the video element has gone away before we've had a chance to set up
|
|
|
|
// Picture-in-Picture for it, tell the parent to close the Picture-in-Picture
|
|
|
|
// window.
|
2019-07-02 21:08:16 +03:00
|
|
|
await this.closePictureInPicture({ reason: "setup-failure" });
|
2019-03-23 02:32:50 +03:00
|
|
|
return;
|
2019-02-12 05:34:38 +03:00
|
|
|
}
|
|
|
|
|
2022-03-23 23:01:00 +03:00
|
|
|
this.videoWrapper = applyWrapper(this, originatingVideo);
|
2022-01-19 19:36:36 +03:00
|
|
|
|
2020-09-28 18:32:18 +03:00
|
|
|
let loadPromise = new Promise(resolve => {
|
|
|
|
this.contentWindow.addEventListener("load", resolve, {
|
|
|
|
once: true,
|
|
|
|
mozSystemGroup: true,
|
|
|
|
capture: true,
|
2019-02-12 05:34:38 +03:00
|
|
|
});
|
2020-09-28 18:32:18 +03:00
|
|
|
});
|
|
|
|
this.contentWindow.location.reload();
|
|
|
|
await loadPromise;
|
2019-02-12 05:34:38 +03:00
|
|
|
|
2020-06-09 21:30:51 +03:00
|
|
|
// We're committed to adding the video to this window now. Ensure we track
|
|
|
|
// the content window before we do so, so that the toggle actor can
|
|
|
|
// distinguish this new video we're creating from web-controlled ones.
|
2020-10-23 01:04:55 +03:00
|
|
|
this.weakPlayerContent = Cu.getWeakReference(this.contentWindow);
|
|
|
|
gPlayerContents.add(this.contentWindow);
|
2020-06-09 21:30:51 +03:00
|
|
|
|
2019-11-01 17:15:22 +03:00
|
|
|
let doc = this.document;
|
2019-10-10 16:57:48 +03:00
|
|
|
let playerVideo = doc.createElement("video");
|
2022-02-07 21:05:43 +03:00
|
|
|
playerVideo.id = "playervideo";
|
|
|
|
let textTracks = doc.createElement("div");
|
2019-10-10 16:57:48 +03:00
|
|
|
|
2019-08-21 19:48:49 +03:00
|
|
|
doc.body.style.overflow = "hidden";
|
|
|
|
doc.body.style.margin = "0";
|
|
|
|
|
2019-02-12 05:34:38 +03:00
|
|
|
// Force the player video to assume maximum height and width of the
|
|
|
|
// containing window
|
|
|
|
playerVideo.style.height = "100vh";
|
2022-02-03 00:54:36 +03:00
|
|
|
playerVideo.style.width = "100vw";
|
2020-03-16 17:41:15 +03:00
|
|
|
playerVideo.style.backgroundColor = "#000";
|
2019-02-12 05:34:38 +03:00
|
|
|
|
2022-02-07 21:05:43 +03:00
|
|
|
// Load text tracks container in the content process so that
|
|
|
|
// we can load text tracks without having to constantly
|
|
|
|
// access the parent process.
|
|
|
|
textTracks.id = "texttracks";
|
2022-03-11 03:52:01 +03:00
|
|
|
// When starting pip, player controls are expected to appear.
|
|
|
|
textTracks.setAttribute("overlap-video-controls", true);
|
2019-02-12 05:34:38 +03:00
|
|
|
doc.body.appendChild(playerVideo);
|
2022-02-07 21:05:43 +03:00
|
|
|
doc.body.appendChild(textTracks);
|
2022-02-22 23:06:01 +03:00
|
|
|
// Load text tracks stylesheet
|
|
|
|
let textTracksStyleSheet = this.createTextTracksStyleSheet();
|
|
|
|
doc.head.appendChild(textTracksStyleSheet);
|
2019-02-12 05:34:38 +03:00
|
|
|
|
2022-04-06 17:33:54 +03:00
|
|
|
this.setTextTrackFontSize();
|
|
|
|
|
2019-03-02 01:37:26 +03:00
|
|
|
originatingVideo.cloneElementVisually(playerVideo);
|
|
|
|
|
2020-11-09 20:10:28 +03:00
|
|
|
let shadowRoot = originatingVideo.openOrClosedShadowRoot;
|
|
|
|
if (originatingVideo.getTransformToViewport().a == -1) {
|
|
|
|
shadowRoot.firstChild.setAttribute("flipped", true);
|
|
|
|
playerVideo.style.transform = "scaleX(-1)";
|
|
|
|
}
|
|
|
|
|
2022-02-07 21:05:43 +03:00
|
|
|
this.onCueChange = this.onCueChange.bind(this);
|
2019-03-23 02:33:53 +03:00
|
|
|
this.trackOriginatingVideo(originatingVideo);
|
2019-02-12 05:34:38 +03:00
|
|
|
|
2019-11-01 17:15:22 +03:00
|
|
|
this.contentWindow.addEventListener(
|
2019-02-12 05:34:38 +03:00
|
|
|
"unload",
|
|
|
|
() => {
|
2020-10-23 01:04:55 +03:00
|
|
|
let video = this.getWeakVideo();
|
2019-11-07 21:31:42 +03:00
|
|
|
if (video) {
|
|
|
|
this.untrackOriginatingVideo(video);
|
|
|
|
video.stopCloningElementVisually();
|
2019-03-13 04:44:37 +03:00
|
|
|
}
|
2020-10-23 01:04:55 +03:00
|
|
|
this.weakVideo = null;
|
2019-02-12 05:34:38 +03:00
|
|
|
},
|
|
|
|
{ once: true }
|
|
|
|
);
|
|
|
|
}
|
2019-04-04 22:04:47 +03:00
|
|
|
|
|
|
|
play() {
|
2020-10-23 01:04:55 +03:00
|
|
|
let video = this.getWeakVideo();
|
2022-01-19 19:36:36 +03:00
|
|
|
if (video && this.videoWrapper) {
|
|
|
|
this.videoWrapper.play(video);
|
2019-04-04 22:04:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pause() {
|
2020-10-23 01:04:55 +03:00
|
|
|
let video = this.getWeakVideo();
|
2022-01-19 19:36:36 +03:00
|
|
|
if (video && this.videoWrapper) {
|
|
|
|
this.videoWrapper.pause(video);
|
2019-04-04 22:04:47 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-05 20:40:02 +03:00
|
|
|
|
2019-11-05 00:14:50 +03:00
|
|
|
mute() {
|
2020-10-23 01:04:55 +03:00
|
|
|
let video = this.getWeakVideo();
|
2022-01-19 19:36:36 +03:00
|
|
|
if (video && this.videoWrapper) {
|
|
|
|
this.videoWrapper.setMuted(video, true);
|
2019-11-05 00:14:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unmute() {
|
2020-10-23 01:04:55 +03:00
|
|
|
let video = this.getWeakVideo();
|
2022-01-19 19:36:36 +03:00
|
|
|
if (video && this.videoWrapper) {
|
|
|
|
this.videoWrapper.setMuted(video, false);
|
2019-11-05 00:14:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 21:05:43 +03:00
|
|
|
onCueChange(e) {
|
2022-06-07 07:29:48 +03:00
|
|
|
if (!lazy.DISPLAY_TEXT_TRACKS_PREF) {
|
2022-02-07 21:05:43 +03:00
|
|
|
this.updateWebVTTTextTracksDisplay(null);
|
|
|
|
} else {
|
|
|
|
const cues = this._currentWebVTTTrack.activeCues;
|
|
|
|
this.updateWebVTTTextTracksDisplay(cues);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 23:14:09 +03:00
|
|
|
/**
|
|
|
|
* This checks if a given keybinding has been disabled for the specific site
|
|
|
|
* currently being viewed.
|
|
|
|
*/
|
2023-02-24 17:00:17 +03:00
|
|
|
isKeyDisabled(key) {
|
2020-10-23 01:04:55 +03:00
|
|
|
const video = this.getWeakVideo();
|
2020-09-11 23:14:09 +03:00
|
|
|
if (!video) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const { documentURI } = video.ownerDocument;
|
|
|
|
if (!documentURI) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-02-24 17:00:17 +03:00
|
|
|
for (let [override, { disabledKeyboardControls }] of lazy.gSiteOverrides) {
|
|
|
|
if (
|
|
|
|
disabledKeyboardControls !== undefined &&
|
|
|
|
override.matches(documentURI)
|
|
|
|
) {
|
|
|
|
if (disabledKeyboardControls === lazy.KEYBOARD_CONTROLS.ALL) {
|
|
|
|
return true;
|
2020-09-11 23:14:09 +03:00
|
|
|
}
|
2023-02-24 17:00:17 +03:00
|
|
|
return !!(disabledKeyboardControls & key);
|
2020-09-11 23:14:09 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-24 17:00:17 +03:00
|
|
|
return false;
|
2020-09-11 23:14:09 +03:00
|
|
|
}
|
|
|
|
|
2020-01-26 09:21:23 +03:00
|
|
|
/**
|
|
|
|
* This reuses the keyHandler logic in the VideoControlsWidget
|
|
|
|
* https://searchfox.org/mozilla-central/rev/cfd1cc461f1efe0d66c2fdc17c024a203d5a2fd8/toolkit/content/widgets/videocontrols.js#1687-1810.
|
|
|
|
* There are future plans to eventually combine the two implementations.
|
|
|
|
*/
|
2020-09-11 23:14:09 +03:00
|
|
|
/* eslint-disable complexity */
|
2020-01-26 09:21:23 +03:00
|
|
|
keyDown({ altKey, shiftKey, metaKey, ctrlKey, keyCode }) {
|
2020-10-23 01:04:55 +03:00
|
|
|
let video = this.getWeakVideo();
|
2020-01-26 09:21:23 +03:00
|
|
|
if (!video) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var keystroke = "";
|
|
|
|
if (altKey) {
|
|
|
|
keystroke += "alt-";
|
|
|
|
}
|
|
|
|
if (shiftKey) {
|
|
|
|
keystroke += "shift-";
|
|
|
|
}
|
|
|
|
if (this.contentWindow.navigator.platform.startsWith("Mac")) {
|
|
|
|
if (metaKey) {
|
|
|
|
keystroke += "accel-";
|
|
|
|
}
|
|
|
|
if (ctrlKey) {
|
|
|
|
keystroke += "control-";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (metaKey) {
|
|
|
|
keystroke += "meta-";
|
|
|
|
}
|
|
|
|
if (ctrlKey) {
|
|
|
|
keystroke += "accel-";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (keyCode) {
|
|
|
|
case this.contentWindow.KeyEvent.DOM_VK_UP:
|
|
|
|
keystroke += "upArrow";
|
|
|
|
break;
|
|
|
|
case this.contentWindow.KeyEvent.DOM_VK_DOWN:
|
|
|
|
keystroke += "downArrow";
|
|
|
|
break;
|
|
|
|
case this.contentWindow.KeyEvent.DOM_VK_LEFT:
|
|
|
|
keystroke += "leftArrow";
|
|
|
|
break;
|
|
|
|
case this.contentWindow.KeyEvent.DOM_VK_RIGHT:
|
|
|
|
keystroke += "rightArrow";
|
|
|
|
break;
|
|
|
|
case this.contentWindow.KeyEvent.DOM_VK_HOME:
|
|
|
|
keystroke += "home";
|
|
|
|
break;
|
|
|
|
case this.contentWindow.KeyEvent.DOM_VK_END:
|
|
|
|
keystroke += "end";
|
|
|
|
break;
|
|
|
|
case this.contentWindow.KeyEvent.DOM_VK_SPACE:
|
|
|
|
keystroke += "space";
|
|
|
|
break;
|
2021-11-23 00:39:09 +03:00
|
|
|
case this.contentWindow.KeyEvent.DOM_VK_W:
|
|
|
|
keystroke += "w";
|
|
|
|
break;
|
2020-01-26 09:21:23 +03:00
|
|
|
}
|
|
|
|
|
2023-02-02 00:46:31 +03:00
|
|
|
const isVideoStreaming = this.videoWrapper.isLive(video);
|
2020-01-26 09:21:23 +03:00
|
|
|
var oldval, newval;
|
|
|
|
|
|
|
|
try {
|
|
|
|
switch (keystroke) {
|
|
|
|
case "space" /* Toggle Play / Pause */:
|
2023-02-24 17:00:17 +03:00
|
|
|
if (this.isKeyDisabled(lazy.KEYBOARD_CONTROLS.PLAY_PAUSE)) {
|
2020-09-11 23:14:09 +03:00
|
|
|
return;
|
|
|
|
}
|
2022-01-19 19:36:36 +03:00
|
|
|
|
|
|
|
if (
|
|
|
|
this.videoWrapper.getPaused(video) ||
|
|
|
|
this.videoWrapper.getEnded(video)
|
|
|
|
) {
|
|
|
|
this.videoWrapper.play(video);
|
2020-01-26 09:21:23 +03:00
|
|
|
} else {
|
2022-01-19 19:36:36 +03:00
|
|
|
this.videoWrapper.pause(video);
|
2020-01-26 09:21:23 +03:00
|
|
|
}
|
2022-01-19 19:36:36 +03:00
|
|
|
|
2020-01-26 09:21:23 +03:00
|
|
|
break;
|
2021-11-23 00:39:09 +03:00
|
|
|
case "accel-w" /* Close video */:
|
2023-02-24 17:00:17 +03:00
|
|
|
if (this.isKeyDisabled(lazy.KEYBOARD_CONTROLS.CLOSE)) {
|
2021-11-23 00:39:09 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.pause();
|
|
|
|
this.closePictureInPicture({ reason: "close-player-shortcut" });
|
|
|
|
break;
|
2020-01-26 09:21:23 +03:00
|
|
|
case "downArrow" /* Volume decrease */:
|
2023-02-24 17:00:17 +03:00
|
|
|
if (this.isKeyDisabled(lazy.KEYBOARD_CONTROLS.VOLUME)) {
|
2020-09-11 23:14:09 +03:00
|
|
|
return;
|
|
|
|
}
|
2022-01-19 19:36:36 +03:00
|
|
|
oldval = this.videoWrapper.getVolume(video);
|
|
|
|
this.videoWrapper.setVolume(video, oldval < 0.1 ? 0 : oldval - 0.1);
|
|
|
|
this.videoWrapper.setMuted(video, false);
|
2020-01-26 09:21:23 +03:00
|
|
|
break;
|
|
|
|
case "upArrow" /* Volume increase */:
|
2023-02-24 17:00:17 +03:00
|
|
|
if (this.isKeyDisabled(lazy.KEYBOARD_CONTROLS.VOLUME)) {
|
2020-09-11 23:14:09 +03:00
|
|
|
return;
|
|
|
|
}
|
2022-01-19 19:36:36 +03:00
|
|
|
oldval = this.videoWrapper.getVolume(video);
|
|
|
|
this.videoWrapper.setVolume(video, oldval > 0.9 ? 1 : oldval + 0.1);
|
|
|
|
this.videoWrapper.setMuted(video, false);
|
2020-01-26 09:21:23 +03:00
|
|
|
break;
|
|
|
|
case "accel-downArrow" /* Mute */:
|
2023-02-24 17:00:17 +03:00
|
|
|
if (this.isKeyDisabled(lazy.KEYBOARD_CONTROLS.MUTE_UNMUTE)) {
|
2020-09-11 23:14:09 +03:00
|
|
|
return;
|
|
|
|
}
|
2022-01-19 19:36:36 +03:00
|
|
|
this.videoWrapper.setMuted(video, true);
|
2020-01-26 09:21:23 +03:00
|
|
|
break;
|
|
|
|
case "accel-upArrow" /* Unmute */:
|
2023-02-24 17:00:17 +03:00
|
|
|
if (this.isKeyDisabled(lazy.KEYBOARD_CONTROLS.MUTE_UNMUTE)) {
|
2020-09-11 23:14:09 +03:00
|
|
|
return;
|
|
|
|
}
|
2022-01-19 19:36:36 +03:00
|
|
|
this.videoWrapper.setMuted(video, false);
|
2020-01-26 09:21:23 +03:00
|
|
|
break;
|
2022-02-23 04:10:17 +03:00
|
|
|
case "leftArrow": /* Seek back 5 seconds */
|
2020-01-26 09:21:23 +03:00
|
|
|
case "accel-leftArrow" /* Seek back 10% */:
|
2023-02-24 17:00:17 +03:00
|
|
|
if (
|
|
|
|
this.isKeyDisabled(lazy.KEYBOARD_CONTROLS.SEEK) ||
|
|
|
|
(isVideoStreaming &&
|
|
|
|
this.isKeyDisabled(lazy.KEYBOARD_CONTROLS.LIVE_SEEK))
|
|
|
|
) {
|
2020-01-26 09:21:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-19 19:36:36 +03:00
|
|
|
oldval = this.videoWrapper.getCurrentTime(video);
|
2020-01-26 09:21:23 +03:00
|
|
|
if (keystroke == "leftArrow") {
|
2022-02-23 04:10:17 +03:00
|
|
|
newval = oldval - SEEK_TIME_SECS;
|
2020-01-26 09:21:23 +03:00
|
|
|
} else {
|
2022-01-19 19:36:36 +03:00
|
|
|
newval = oldval - this.videoWrapper.getDuration(video) / 10;
|
2020-01-26 09:21:23 +03:00
|
|
|
}
|
2022-01-19 19:36:36 +03:00
|
|
|
this.videoWrapper.setCurrentTime(video, newval >= 0 ? newval : 0);
|
2020-01-26 09:21:23 +03:00
|
|
|
break;
|
2022-02-23 04:10:17 +03:00
|
|
|
case "rightArrow": /* Seek forward 5 seconds */
|
2020-01-26 09:21:23 +03:00
|
|
|
case "accel-rightArrow" /* Seek forward 10% */:
|
2023-02-24 17:00:17 +03:00
|
|
|
if (
|
|
|
|
this.isKeyDisabled(lazy.KEYBOARD_CONTROLS.SEEK) ||
|
|
|
|
(isVideoStreaming &&
|
|
|
|
this.isKeyDisabled(lazy.KEYBOARD_CONTROLS.LIVE_SEEK))
|
|
|
|
) {
|
2020-01-26 09:21:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-19 19:36:36 +03:00
|
|
|
oldval = this.videoWrapper.getCurrentTime(video);
|
|
|
|
var maxtime = this.videoWrapper.getDuration(video);
|
2020-01-26 09:21:23 +03:00
|
|
|
if (keystroke == "rightArrow") {
|
2022-02-23 04:10:17 +03:00
|
|
|
newval = oldval + SEEK_TIME_SECS;
|
2020-01-26 09:21:23 +03:00
|
|
|
} else {
|
|
|
|
newval = oldval + maxtime / 10;
|
|
|
|
}
|
2022-01-19 19:36:36 +03:00
|
|
|
let selectedTime = newval <= maxtime ? newval : maxtime;
|
|
|
|
this.videoWrapper.setCurrentTime(video, selectedTime);
|
2020-01-26 09:21:23 +03:00
|
|
|
break;
|
|
|
|
case "home" /* Seek to beginning */:
|
2023-02-24 17:00:17 +03:00
|
|
|
if (this.isKeyDisabled(lazy.KEYBOARD_CONTROLS.SEEK)) {
|
2020-09-11 23:14:09 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-01-26 09:21:23 +03:00
|
|
|
if (!isVideoStreaming) {
|
2022-01-19 19:36:36 +03:00
|
|
|
this.videoWrapper.setCurrentTime(video, 0);
|
2020-01-26 09:21:23 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "end" /* Seek to end */:
|
2023-02-24 17:00:17 +03:00
|
|
|
if (this.isKeyDisabled(lazy.KEYBOARD_CONTROLS.SEEK)) {
|
2020-09-11 23:14:09 +03:00
|
|
|
return;
|
|
|
|
}
|
2022-01-19 19:36:36 +03:00
|
|
|
|
|
|
|
let duration = this.videoWrapper.getDuration(video);
|
|
|
|
if (
|
|
|
|
!isVideoStreaming &&
|
|
|
|
this.videoWrapper.getCurrentTime(video) != duration
|
|
|
|
) {
|
|
|
|
this.videoWrapper.setCurrentTime(video, duration);
|
2020-01-26 09:21:23 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
/* ignore any exception from setting video.currentTime */
|
|
|
|
}
|
|
|
|
}
|
2022-06-04 19:39:16 +03:00
|
|
|
|
|
|
|
get isSubtitlesEnabled() {
|
|
|
|
return this.#subtitlesEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
set isSubtitlesEnabled(val) {
|
|
|
|
if (val) {
|
|
|
|
Services.telemetry.recordEvent(
|
|
|
|
"pictureinpicture",
|
|
|
|
"subtitles_shown",
|
|
|
|
"subtitles",
|
|
|
|
null,
|
|
|
|
{
|
|
|
|
webVTTSubtitles: (!!this.getWeakVideo().textTracks
|
|
|
|
?.length).toString(),
|
|
|
|
}
|
|
|
|
);
|
2022-07-06 20:30:29 +03:00
|
|
|
} else {
|
2023-01-25 18:14:28 +03:00
|
|
|
this.sendAsyncMessage("PictureInPicture:DisableSubtitlesButton");
|
2022-06-04 19:39:16 +03:00
|
|
|
}
|
|
|
|
this.#subtitlesEnabled = val;
|
|
|
|
}
|
2019-02-12 05:34:38 +03:00
|
|
|
}
|
2022-01-19 19:36:36 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The PictureInPictureChildVideoWrapper class handles providing a path to a script that
|
|
|
|
* defines a "site wrapper" for the original <video> (or other controls API provided
|
|
|
|
* by the site) to command it.
|
|
|
|
*
|
|
|
|
* This "site wrapper" provided to PictureInPictureChildVideoWrapper is a script file that
|
|
|
|
* defines a class called `PictureInPictureVideoWrapper` and exports it. These scripts can
|
|
|
|
* be found under "browser/extensions/pictureinpicture/video-wrappers" as part of the
|
|
|
|
* Picture-In-Picture addon.
|
|
|
|
*
|
|
|
|
* Site wrappers need to adhere to a specific interface to work properly with
|
|
|
|
* PictureInPictureChildVideoWrapper:
|
|
|
|
*
|
|
|
|
* - The "site wrapper" script must export a class called "PictureInPictureVideoWrapper"
|
|
|
|
* - Method names on a site wrapper class should match its caller's name
|
2022-04-22 23:46:11 +03:00
|
|
|
* (i.e: PictureInPictureChildVideoWrapper.play will only call `play` on a site-wrapper, if available)
|
2022-01-19 19:36:36 +03:00
|
|
|
*/
|
|
|
|
class PictureInPictureChildVideoWrapper {
|
|
|
|
#sandbox;
|
|
|
|
#siteWrapper;
|
2022-02-10 18:24:07 +03:00
|
|
|
#PictureInPictureChild;
|
2022-01-19 19:36:36 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a wrapper for the original <video>
|
|
|
|
*
|
|
|
|
* @param {String|null} videoWrapperScriptPath
|
|
|
|
* Path to a wrapper script from the Picture-in-Picture addon. If a wrapper isn't
|
|
|
|
* provided to the class, then we fallback on a default implementation for
|
|
|
|
* commanding the original <video>.
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* The original <video> we want to create a wrapper class for.
|
2022-04-22 23:46:11 +03:00
|
|
|
* @param {Object} pipChild
|
|
|
|
* Reference to PictureInPictureChild class calling this function.
|
2022-01-19 19:36:36 +03:00
|
|
|
*/
|
2022-04-22 23:46:11 +03:00
|
|
|
constructor(videoWrapperScriptPath, video, pipChild) {
|
2022-01-19 19:36:36 +03:00
|
|
|
this.#sandbox = videoWrapperScriptPath
|
|
|
|
? this.#createSandbox(videoWrapperScriptPath, video)
|
|
|
|
: null;
|
2022-04-22 23:46:11 +03:00
|
|
|
this.#PictureInPictureChild = pipChild;
|
2022-01-19 19:36:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles calling methods defined on the site wrapper class to perform video
|
|
|
|
* controls operations on the source video. If the method doesn't exist,
|
|
|
|
* or if an error is thrown while calling it, use a fallback implementation.
|
|
|
|
*
|
|
|
|
* @param {String} methodInfo.name
|
|
|
|
* The method name to call.
|
|
|
|
* @param {Array} methodInfo.args
|
|
|
|
* Arguments to pass to the site wrapper method being called.
|
|
|
|
* @param {Function} methodInfo.fallback
|
|
|
|
* A fallback function that's invoked when a method doesn't exist on the site
|
|
|
|
* wrapper class or an error is thrown while calling a method
|
|
|
|
* @param {Function} methodInfo.validateReturnVal
|
|
|
|
* Validates whether or not the return value of the wrapper method is correct.
|
|
|
|
* If this isn't provided or if it evaluates false for a return value, then
|
|
|
|
* return null.
|
|
|
|
*
|
|
|
|
* @returns The expected output of the wrapper function.
|
|
|
|
*/
|
|
|
|
#callWrapperMethod({ name, args = [], fallback = () => {}, validateRetVal }) {
|
|
|
|
try {
|
|
|
|
const wrappedMethod = this.#siteWrapper?.[name];
|
|
|
|
if (typeof wrappedMethod === "function") {
|
|
|
|
let retVal = wrappedMethod.call(this.#siteWrapper, ...args);
|
|
|
|
|
|
|
|
if (!validateRetVal) {
|
2022-11-15 00:27:47 +03:00
|
|
|
lazy.logConsole.error(
|
2022-01-19 19:36:36 +03:00
|
|
|
`No return value validator was provided for method ${name}(). Returning null.`
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validateRetVal(retVal)) {
|
2022-11-15 00:27:47 +03:00
|
|
|
lazy.logConsole.error(
|
2022-01-19 19:36:36 +03:00
|
|
|
`Calling method ${name}() returned an unexpected value: ${retVal}. Returning null.`
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2022-11-15 00:27:47 +03:00
|
|
|
lazy.logConsole.error(
|
|
|
|
`There was an error while calling ${name}(): `,
|
|
|
|
e.message
|
|
|
|
);
|
2022-01-19 19:36:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return fallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a sandbox with Xray vision to execute content code in an unprivileged
|
|
|
|
* context. This way, privileged code (PictureInPictureChild) can call into the
|
|
|
|
* sandbox to perform video controls operations on the originating video
|
|
|
|
* (content code) and still be protected from direct access by it.
|
|
|
|
*
|
|
|
|
* @param {String} videoWrapperScriptPath
|
|
|
|
* Path to a wrapper script from the Picture-in-Picture addon.
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* The source video element whose window to create a sandbox for.
|
|
|
|
*/
|
|
|
|
#createSandbox(videoWrapperScriptPath, video) {
|
|
|
|
const addonPolicy = WebExtensionPolicy.getByID(
|
|
|
|
"pictureinpicture@mozilla.org"
|
|
|
|
);
|
|
|
|
let wrapperScriptUrl = addonPolicy.getURL(videoWrapperScriptPath);
|
|
|
|
let originatingWin = video.ownerGlobal;
|
|
|
|
let originatingDoc = video.ownerDocument;
|
|
|
|
|
|
|
|
let sandbox = Cu.Sandbox([originatingDoc.nodePrincipal], {
|
|
|
|
sandboxName: "Picture-in-Picture video wrapper sandbox",
|
|
|
|
sandboxPrototype: originatingWin,
|
|
|
|
sameZoneAs: originatingWin,
|
|
|
|
wantXrays: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
Services.scriptloader.loadSubScript(wrapperScriptUrl, sandbox);
|
|
|
|
} catch (e) {
|
|
|
|
Cu.nukeSandbox(sandbox);
|
2022-11-15 00:27:47 +03:00
|
|
|
lazy.logConsole.error(
|
|
|
|
"Error loading wrapper script for Picture-in-Picture",
|
|
|
|
e
|
|
|
|
);
|
2022-01-19 19:36:36 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The prototype of the wrapper class instantiated from the sandbox with Xray
|
|
|
|
// vision is `Object` and not actually `PictureInPictureVideoWrapper`. But we
|
|
|
|
// need to be able to access methods defined on this class to perform site-specific
|
|
|
|
// video control operations otherwise we fallback to a default implementation.
|
|
|
|
// Because of this, we need to "waive Xray vision" by adding `.wrappedObject` to the
|
|
|
|
// end.
|
2022-05-25 19:08:04 +03:00
|
|
|
this.#siteWrapper = new sandbox.PictureInPictureVideoWrapper(
|
|
|
|
video
|
|
|
|
).wrappedJSObject;
|
2022-01-19 19:36:36 +03:00
|
|
|
|
|
|
|
return sandbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
#isBoolean(val) {
|
|
|
|
return typeof val === "boolean";
|
|
|
|
}
|
|
|
|
|
|
|
|
#isNumber(val) {
|
|
|
|
return typeof val === "number";
|
|
|
|
}
|
|
|
|
|
2022-04-22 23:46:11 +03:00
|
|
|
/**
|
|
|
|
* Destroys the sandbox for the site wrapper class
|
|
|
|
*/
|
2022-01-19 19:36:36 +03:00
|
|
|
destroy() {
|
|
|
|
if (this.#sandbox) {
|
|
|
|
Cu.nukeSandbox(this.#sandbox);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 18:24:07 +03:00
|
|
|
/**
|
|
|
|
* Function to display the captions on the PiP window
|
|
|
|
* @param text The captions to be shown on the PiP window
|
|
|
|
*/
|
|
|
|
updatePiPTextTracks(text) {
|
2022-06-04 19:39:16 +03:00
|
|
|
if (!this.#PictureInPictureChild.isSubtitlesEnabled && text) {
|
|
|
|
this.#PictureInPictureChild.isSubtitlesEnabled = true;
|
2022-06-17 07:16:28 +03:00
|
|
|
this.#PictureInPictureChild.sendAsyncMessage(
|
2023-01-25 18:14:28 +03:00
|
|
|
"PictureInPicture:EnableSubtitlesButton"
|
2022-06-17 07:16:28 +03:00
|
|
|
);
|
2022-06-04 19:39:16 +03:00
|
|
|
}
|
2022-02-10 18:24:07 +03:00
|
|
|
let pipWindowTracksContainer = this.#PictureInPictureChild.document.getElementById(
|
|
|
|
"texttracks"
|
|
|
|
);
|
|
|
|
pipWindowTracksContainer.textContent = text;
|
|
|
|
}
|
|
|
|
|
2022-01-19 19:36:36 +03:00
|
|
|
/* Video methods to be used for video controls from the PiP window. */
|
|
|
|
|
2022-04-22 23:46:11 +03:00
|
|
|
/**
|
|
|
|
* OVERRIDABLE - calls the play() method defined in the site wrapper script. Runs a fallback implementation
|
|
|
|
* if the method does not exist or if an error is thrown while calling it. This method is meant to handle video
|
|
|
|
* behaviour when a video is played.
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* The originating video source element
|
|
|
|
*/
|
2022-01-19 19:36:36 +03:00
|
|
|
play(video) {
|
|
|
|
return this.#callWrapperMethod({
|
|
|
|
name: "play",
|
|
|
|
args: [video],
|
|
|
|
fallback: () => video.play(),
|
|
|
|
validateRetVal: retVal => retVal == null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-22 23:46:11 +03:00
|
|
|
/**
|
|
|
|
* OVERRIDABLE - calls the pause() method defined in the site wrapper script. Runs a fallback implementation
|
|
|
|
* if the method does not exist or if an error is thrown while calling it. This method is meant to handle video
|
|
|
|
* behaviour when a video is paused.
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* The originating video source element
|
|
|
|
*/
|
2022-01-19 19:36:36 +03:00
|
|
|
pause(video) {
|
|
|
|
return this.#callWrapperMethod({
|
|
|
|
name: "pause",
|
|
|
|
args: [video],
|
|
|
|
fallback: () => video.pause(),
|
|
|
|
validateRetVal: retVal => retVal == null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-22 23:46:11 +03:00
|
|
|
/**
|
|
|
|
* OVERRIDABLE - calls the getPaused() method defined in the site wrapper script. Runs a fallback implementation
|
|
|
|
* if the method does not exist or if an error is thrown while calling it. This method is meant to determine if
|
|
|
|
* a video is paused or not.
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* The originating video source element
|
|
|
|
* @returns {Boolean} Boolean value true if paused, or false if video is still playing
|
|
|
|
*/
|
2022-01-19 19:36:36 +03:00
|
|
|
getPaused(video) {
|
|
|
|
return this.#callWrapperMethod({
|
|
|
|
name: "getPaused",
|
|
|
|
args: [video],
|
|
|
|
fallback: () => video.paused,
|
|
|
|
validateRetVal: retVal => this.#isBoolean(retVal),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-22 23:46:11 +03:00
|
|
|
/**
|
|
|
|
* OVERRIDABLE - calls the getEnded() method defined in the site wrapper script. Runs a fallback implementation
|
|
|
|
* if the method does not exist or if an error is thrown while calling it. This method is meant to determine if
|
|
|
|
* video playback or streaming has stopped.
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* The originating video source element
|
|
|
|
* @returns {Boolean} Boolean value true if the video has ended, or false if still playing
|
|
|
|
*/
|
2022-01-19 19:36:36 +03:00
|
|
|
getEnded(video) {
|
|
|
|
return this.#callWrapperMethod({
|
|
|
|
name: "getEnded",
|
|
|
|
args: [video],
|
|
|
|
fallback: () => video.ended,
|
|
|
|
validateRetVal: retVal => this.#isBoolean(retVal),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-22 23:46:11 +03:00
|
|
|
/**
|
|
|
|
* OVERRIDABLE - calls the getDuration() method defined in the site wrapper script. Runs a fallback implementation
|
|
|
|
* if the method does not exist or if an error is thrown while calling it. This method is meant to get the current
|
|
|
|
* duration of a video in seconds.
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* The originating video source element
|
|
|
|
* @returns {Number} Duration of the video in seconds
|
|
|
|
*/
|
2022-01-19 19:36:36 +03:00
|
|
|
getDuration(video) {
|
|
|
|
return this.#callWrapperMethod({
|
|
|
|
name: "getDuration",
|
|
|
|
args: [video],
|
|
|
|
fallback: () => video.duration,
|
|
|
|
validateRetVal: retVal => this.#isNumber(retVal),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-22 23:46:11 +03:00
|
|
|
/**
|
|
|
|
* OVERRIDABLE - calls the getCurrentTime() method defined in the site wrapper script. Runs a fallback implementation
|
|
|
|
* if the method does not exist or if an error is thrown while calling it. This method is meant to get the current
|
|
|
|
* time of a video in seconds.
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* The originating video source element
|
|
|
|
* @returns {Number} Current time of the video in seconds
|
|
|
|
*/
|
2022-01-19 19:36:36 +03:00
|
|
|
getCurrentTime(video) {
|
|
|
|
return this.#callWrapperMethod({
|
|
|
|
name: "getCurrentTime",
|
|
|
|
args: [video],
|
|
|
|
fallback: () => video.currentTime,
|
|
|
|
validateRetVal: retVal => this.#isNumber(retVal),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-22 23:46:11 +03:00
|
|
|
/**
|
|
|
|
* OVERRIDABLE - calls the setCurrentTime() method defined in the site wrapper script. Runs a fallback implementation
|
|
|
|
* if the method does not exist or if an error is thrown while calling it. This method is meant to set the current
|
|
|
|
* time of a video.
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* The originating video source element
|
|
|
|
* @param {Number} position
|
|
|
|
* The current playback time of the video
|
2023-01-10 01:04:55 +03:00
|
|
|
* @param {Boolean} wasPlaying
|
|
|
|
* True if the video was playing before seeking else false
|
2022-04-22 23:46:11 +03:00
|
|
|
*/
|
2023-01-10 01:04:55 +03:00
|
|
|
setCurrentTime(video, position, wasPlaying) {
|
2022-01-19 19:36:36 +03:00
|
|
|
return this.#callWrapperMethod({
|
|
|
|
name: "setCurrentTime",
|
2023-01-10 01:04:55 +03:00
|
|
|
args: [video, position, wasPlaying],
|
2022-01-19 19:36:36 +03:00
|
|
|
fallback: () => {
|
|
|
|
video.currentTime = position;
|
|
|
|
},
|
|
|
|
validateRetVal: retVal => retVal == null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-22 19:29:47 +03:00
|
|
|
/**
|
|
|
|
* Return hours, minutes, and seconds from seconds
|
|
|
|
* @param {Number} aSeconds
|
|
|
|
* The time in seconds
|
|
|
|
* @returns {String} Timestamp string
|
|
|
|
**/
|
|
|
|
timeFromSeconds(aSeconds) {
|
|
|
|
aSeconds = isNaN(aSeconds) ? 0 : Math.round(aSeconds);
|
|
|
|
let seconds = Math.floor(aSeconds % 60),
|
|
|
|
minutes = Math.floor((aSeconds / 60) % 60),
|
|
|
|
hours = Math.floor(aSeconds / 3600);
|
|
|
|
seconds = seconds < 10 ? "0" + seconds : seconds;
|
|
|
|
minutes = hours > 0 && minutes < 10 ? "0" + minutes : minutes;
|
|
|
|
return aSeconds < 3600
|
|
|
|
? `${minutes}:${seconds}`
|
|
|
|
: `${hours}:${minutes}:${seconds}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a timestamp from current time and total duration,
|
|
|
|
* output as a string in the form '0:00 / 0:00'
|
|
|
|
* @param {Number} aCurrentTime
|
|
|
|
* The current time in seconds
|
|
|
|
* @param {Number} aDuration
|
|
|
|
* The total duration in seconds
|
|
|
|
* @returns {String} Formatted timestamp
|
|
|
|
**/
|
|
|
|
formatTimestamp(aCurrentTime, aDuration) {
|
2023-01-20 23:39:16 +03:00
|
|
|
// We can't format numbers that can't be represented as decimal digits.
|
|
|
|
if (!Number.isFinite(aCurrentTime) || !Number.isFinite(aDuration)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2022-12-22 19:29:47 +03:00
|
|
|
return `${this.timeFromSeconds(aCurrentTime)} / ${this.timeFromSeconds(
|
|
|
|
aDuration
|
|
|
|
)}`;
|
|
|
|
}
|
|
|
|
|
2022-04-22 23:46:11 +03:00
|
|
|
/**
|
|
|
|
* OVERRIDABLE - calls the getVolume() method defined in the site wrapper script. Runs a fallback implementation
|
|
|
|
* if the method does not exist or if an error is thrown while calling it. This method is meant to get the volume
|
|
|
|
* value of a video.
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* The originating video source element
|
|
|
|
* @returns {Number} Volume of the video between 0 (muted) and 1 (loudest)
|
|
|
|
*/
|
2022-01-19 19:36:36 +03:00
|
|
|
getVolume(video) {
|
|
|
|
return this.#callWrapperMethod({
|
|
|
|
name: "getVolume",
|
|
|
|
args: [video],
|
|
|
|
fallback: () => video.volume,
|
|
|
|
validateRetVal: retVal => this.#isNumber(retVal),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-22 23:46:11 +03:00
|
|
|
/**
|
|
|
|
* OVERRIDABLE - calls the setVolume() method defined in the site wrapper script. Runs a fallback implementation
|
|
|
|
* if the method does not exist or if an error is thrown while calling it. This method is meant to set the volume
|
|
|
|
* value of a video.
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* The originating video source element
|
|
|
|
* @param {Number} volume
|
|
|
|
* Value between 0 (muted) and 1 (loudest)
|
|
|
|
*/
|
2022-01-19 19:36:36 +03:00
|
|
|
setVolume(video, volume) {
|
|
|
|
return this.#callWrapperMethod({
|
|
|
|
name: "setVolume",
|
|
|
|
args: [video, volume],
|
|
|
|
fallback: () => {
|
|
|
|
video.volume = volume;
|
|
|
|
},
|
|
|
|
validateRetVal: retVal => retVal == null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-23 07:05:20 +03:00
|
|
|
/**
|
|
|
|
* OVERRIDABLE - calls the isMuted() method defined in the site wrapper script. Runs a fallback implementation
|
|
|
|
* if the method does not exist or if an error is thrown while calling it. This method is meant to get the mute
|
|
|
|
* state a video.
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* The originating video source element
|
|
|
|
* @param {Boolean} shouldMute
|
|
|
|
* Boolean value true to mute the video, or false to unmute the video
|
|
|
|
*/
|
|
|
|
isMuted(video) {
|
|
|
|
return this.#callWrapperMethod({
|
|
|
|
name: "isMuted",
|
|
|
|
args: [video],
|
|
|
|
fallback: () => video.muted,
|
|
|
|
validateRetVal: retVal => this.#isBoolean(retVal),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-22 23:46:11 +03:00
|
|
|
/**
|
|
|
|
* OVERRIDABLE - calls the setMuted() method defined in the site wrapper script. Runs a fallback implementation
|
|
|
|
* if the method does not exist or if an error is thrown while calling it. This method is meant to mute or unmute
|
|
|
|
* a video.
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* The originating video source element
|
|
|
|
* @param {Boolean} shouldMute
|
|
|
|
* Boolean value true to mute the video, or false to unmute the video
|
|
|
|
*/
|
2022-02-10 00:24:46 +03:00
|
|
|
setMuted(video, shouldMute) {
|
2022-01-19 19:36:36 +03:00
|
|
|
return this.#callWrapperMethod({
|
|
|
|
name: "setMuted",
|
2022-02-10 00:24:46 +03:00
|
|
|
args: [video, shouldMute],
|
2022-01-19 19:36:36 +03:00
|
|
|
fallback: () => {
|
2022-02-10 00:24:46 +03:00
|
|
|
video.muted = shouldMute;
|
2022-01-19 19:36:36 +03:00
|
|
|
},
|
|
|
|
validateRetVal: retVal => retVal == null,
|
|
|
|
});
|
|
|
|
}
|
2022-02-10 18:24:07 +03:00
|
|
|
|
2022-04-22 23:46:11 +03:00
|
|
|
/**
|
|
|
|
* OVERRIDABLE - calls the setCaptionContainerObserver() method defined in the site wrapper script. Runs a fallback implementation
|
|
|
|
* if the method does not exist or if an error is thrown while calling it. This method is meant to listen for any cue changes in a
|
|
|
|
* video's caption container and execute a callback function responsible for updating the pip window's text tracks container whenever
|
|
|
|
* a cue change is triggered {@see updatePiPTextTracks()}.
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* The originating video source element
|
|
|
|
* @param {Function} callback
|
|
|
|
* The callback function to be executed when cue changes are detected
|
|
|
|
*/
|
|
|
|
setCaptionContainerObserver(video, callback) {
|
2022-02-10 18:24:07 +03:00
|
|
|
return this.#callWrapperMethod({
|
|
|
|
name: "setCaptionContainerObserver",
|
|
|
|
args: [
|
|
|
|
video,
|
|
|
|
text => {
|
|
|
|
this.updatePiPTextTracks(text);
|
|
|
|
},
|
|
|
|
],
|
|
|
|
fallback: () => {},
|
|
|
|
validateRetVal: retVal => retVal == null,
|
|
|
|
});
|
|
|
|
}
|
2022-03-23 23:01:00 +03:00
|
|
|
|
2022-04-22 23:46:11 +03:00
|
|
|
/**
|
|
|
|
* OVERRIDABLE - calls the shouldHideToggle() method defined in the site wrapper script. Runs a fallback implementation
|
|
|
|
* if the method does not exist or if an error is thrown while calling it. This method is meant to determine if the pip toggle
|
|
|
|
* for a video should be hidden by the site wrapper.
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* The originating video source element
|
|
|
|
* @returns {Boolean} Boolean value true if the pip toggle should be hidden by the site wrapper, or false if it should not
|
|
|
|
*/
|
2022-03-23 23:01:00 +03:00
|
|
|
shouldHideToggle(video) {
|
|
|
|
return this.#callWrapperMethod({
|
|
|
|
name: "shouldHideToggle",
|
|
|
|
args: [video],
|
|
|
|
fallback: () => false,
|
|
|
|
validateRetVal: retVal => this.#isBoolean(retVal),
|
|
|
|
});
|
|
|
|
}
|
2023-02-02 00:46:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* OVERRIDABLE - calls the isLive() method defined in the site wrapper script. Runs a fallback implementation
|
|
|
|
* if the method does not exist or if an error is thrown while calling it. This method is meant to get if the
|
|
|
|
* video is a live stream.
|
|
|
|
* @param {HTMLVideoElement} video
|
|
|
|
* The originating video source element
|
|
|
|
*/
|
|
|
|
isLive(video) {
|
|
|
|
return this.#callWrapperMethod({
|
|
|
|
name: "isLive",
|
|
|
|
args: [video],
|
|
|
|
fallback: () => video.duration === Infinity,
|
|
|
|
validateRetVal: retVal => this.#isBoolean(retVal),
|
|
|
|
});
|
|
|
|
}
|
2022-01-19 19:36:36 +03:00
|
|
|
}
|