Bug 1833854 - Part 2: Common up GCSchedulingTunables invariant checks r=sfink
[gecko.git] / mobile / android / actors / MediaControlDelegateChild.sys.mjs
blob1db32b33f057901fcc1d2a803c9fe3c1c042061f
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/. */
5 import { GeckoViewActorChild } from "resource://gre/modules/GeckoViewActorChild.sys.mjs";
7 const lazy = {};
9 ChromeUtils.defineESModuleGetters(lazy, {
10   MediaUtils: "resource://gre/modules/MediaUtils.sys.mjs",
11   setTimeout: "resource://gre/modules/Timer.sys.mjs",
12 });
14 export class MediaControlDelegateChild extends GeckoViewActorChild {
15   handleEvent(aEvent) {
16     debug`handleEvent: ${aEvent.type}`;
18     switch (aEvent.type) {
19       case "MozDOMFullscreen:Entered":
20       case "MozDOMFullscreen:Exited":
21         this.handleFullscreenChanged(true);
22         break;
23     }
24   }
26   async handleFullscreenChanged(retry) {
27     debug`handleFullscreenChanged`;
29     const element = this.document.fullscreenElement;
30     const mediaElement = lazy.MediaUtils.findMediaElement(element);
32     if (element && !mediaElement) {
33       // Non-media element fullscreen.
34       debug`No fullscreen media element found.`;
35     }
37     const activated = await this.eventDispatcher.sendRequestForResult({
38       type: "GeckoView:MediaSession:Fullscreen",
39       metadata: lazy.MediaUtils.getMetadata(mediaElement) ?? {},
40       enabled: !!element,
41     });
42     if (activated) {
43       return;
44     }
45     if (retry && element) {
46       // When media session is going to active, we have a race condition of
47       // full screen event because media session will be activated by full
48       // screen event.
49       // So we retry to call media session delegate for this situation.
50       lazy.setTimeout(() => {
51         this.handleFullscreenChanged(false);
52       }, 100);
53     }
54   }
57 const { debug } = MediaControlDelegateChild.initLogging(
58   "MediaControlDelegateChild"