pictureInPicture.js (2890B)
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 "use strict"; 6 7 /* global AppConstants, ChromeUtils, ExtensionAPI, Services */ 8 9 ChromeUtils.defineESModuleGetters(this, { 10 KEYBOARD_CONTROLS: "resource://gre/modules/PictureInPictureControls.sys.mjs", 11 TOGGLE_POLICIES: "resource://gre/modules/PictureInPictureControls.sys.mjs", 12 }); 13 14 const TOGGLE_ENABLED_PREF = 15 "media.videocontrols.picture-in-picture.video-toggle.enabled"; 16 17 /** 18 * This API is expected to be running in the parent process. 19 */ 20 this.pictureInPictureParent = class extends ExtensionAPI { 21 /** 22 * Override ExtensionAPI with PiP override's specific API 23 * Relays the site overrides to this extension's child process 24 * 25 * @returns {object} returns the necessary API structure required to manage sharedData in PictureInPictureParent 26 */ 27 getAPI() { 28 return { 29 pictureInPictureParent: { 30 setOverrides(overrides) { 31 // The Picture-in-Picture toggle is only implemented for Desktop, so make 32 // this a no-op for non-Desktop builds. 33 if (AppConstants.platform == "android") { 34 return; 35 } 36 37 Services.ppmm.sharedData.set( 38 "PictureInPicture:SiteOverrides", 39 overrides 40 ); 41 }, 42 }, 43 }; 44 } 45 }; 46 47 /** 48 * This API is expected to be running in a content process - specifically, 49 * the WebExtension content process that the background scripts run in. We 50 * split these out so that they can return values synchronously to the 51 * background scripts. 52 */ 53 this.pictureInPictureChild = class extends ExtensionAPI { 54 /** 55 * Override ExtensionAPI with PiP override's specific API 56 * Clone constants into the Picture-in-Picture child process 57 * 58 * @param {ExtensionContext} context the context of our extension 59 * @returns {object} returns the necessary API structure required to get data from PictureInPictureChild 60 */ 61 getAPI(context) { 62 return { 63 pictureInPictureChild: { 64 getKeyboardControls() { 65 // The Picture-in-Picture toggle is only implemented for Desktop, so make 66 // this return nothing for non-Desktop builds. 67 if (AppConstants.platform == "android") { 68 return Cu.cloneInto({}, context.cloneScope); 69 } 70 71 return Cu.cloneInto(KEYBOARD_CONTROLS, context.cloneScope); 72 }, 73 getPolicies() { 74 // The Picture-in-Picture toggle is only implemented for Desktop, so make 75 // this return nothing for non-Desktop builds. 76 if (AppConstants.platform == "android") { 77 return Cu.cloneInto({}, context.cloneScope); 78 } 79 80 return Cu.cloneInto(TOGGLE_POLICIES, context.cloneScope); 81 }, 82 }, 83 }; 84 } 85 };