tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

MediaControlDelegateChild.sys.mjs (1827B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 import { GeckoViewActorChild } from "resource://gre/modules/GeckoViewActorChild.sys.mjs";
      6 
      7 const lazy = {};
      8 
      9 ChromeUtils.defineESModuleGetters(lazy, {
     10  MediaUtils: "resource://gre/modules/MediaUtils.sys.mjs",
     11  setTimeout: "resource://gre/modules/Timer.sys.mjs",
     12 });
     13 
     14 export class MediaControlDelegateChild extends GeckoViewActorChild {
     15  handleEvent(aEvent) {
     16    debug`handleEvent: ${aEvent.type}`;
     17 
     18    switch (aEvent.type) {
     19      case "MozDOMFullscreen:Entered":
     20      case "MozDOMFullscreen:Exited":
     21        this.handleFullscreenChanged(true);
     22        break;
     23    }
     24  }
     25 
     26  async handleFullscreenChanged(retry) {
     27    debug`handleFullscreenChanged`;
     28 
     29    const element = this.document.fullscreenElement;
     30    const mediaElement = lazy.MediaUtils.findMediaElement(element);
     31 
     32    if (element && !mediaElement) {
     33      // Non-media element fullscreen.
     34      debug`No fullscreen media element found.`;
     35    }
     36 
     37    const activated = await this.sendQuery(
     38      "GeckoView:MediaSession:Fullscreen",
     39      {
     40        metadata: lazy.MediaUtils.getMetadata(mediaElement) ?? {},
     41        enabled: !!element,
     42      }
     43    );
     44    if (activated) {
     45      return;
     46    }
     47    if (retry && element) {
     48      // When media session is going to active, we have a race condition of
     49      // full screen event because media session will be activated by full
     50      // screen event.
     51      // So we retry to call media session delegate for this situation.
     52      lazy.setTimeout(() => {
     53        this.handleFullscreenChanged(false);
     54      }, 100);
     55    }
     56  }
     57 }
     58 
     59 const { debug } = MediaControlDelegateChild.initLogging(
     60  "MediaControlDelegateChild"
     61 );