MobileTabBrowser.sys.mjs (2077B)
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 const lazy = {}; 6 7 ChromeUtils.defineESModuleGetters(lazy, { 8 GeckoViewTabUtil: "resource://gre/modules/GeckoViewTestUtils.sys.mjs", 9 10 windowManager: "chrome://remote/content/shared/WindowManager.sys.mjs", 11 }); 12 13 // GeckoView shim for Desktop's gBrowser 14 export class MobileTabBrowser { 15 constructor(window) { 16 this.window = window; 17 } 18 19 get tabs() { 20 return [this.window.tab]; 21 } 22 23 get selectedTab() { 24 return this.window.tab; 25 } 26 27 set selectedTab(tab) { 28 if (tab != this.selectedTab) { 29 throw new Error("GeckoView only supports a single tab"); 30 } 31 32 // Synthesize a custom TabSelect event to indicate that a tab has been 33 // selected even when we don't change it. 34 const event = this.window.CustomEvent("TabSelect", { 35 bubbles: true, 36 cancelable: false, 37 detail: { 38 previousTab: this.selectedTab, 39 }, 40 }); 41 this.window.document.dispatchEvent(event); 42 } 43 44 get selectedBrowser() { 45 return this.selectedTab.linkedBrowser; 46 } 47 48 addEventListener() { 49 this.window.addEventListener(...arguments); 50 } 51 52 /** 53 * Create a new tab. 54 * 55 * @param {string} uriString 56 * The URI string to load within the newly opened tab. 57 * 58 * @returns {Promise<Tab>} 59 * The created tab. 60 * @throws {Error} 61 * Throws an error if the tab cannot be created. 62 */ 63 addTab(uriString) { 64 return lazy.GeckoViewTabUtil.createNewTab(uriString); 65 } 66 67 getTabForBrowser(browser) { 68 if (browser != this.selectedBrowser) { 69 throw new Error("GeckoView only supports a single tab"); 70 } 71 72 return this.selectedTab; 73 } 74 75 removeEventListener() { 76 this.window.removeEventListener(...arguments); 77 } 78 79 removeTab(tab) { 80 if (tab != this.selectedTab) { 81 throw new Error("GeckoView only supports a single tab"); 82 } 83 84 return lazy.windowManager.closeWindow(this.window); 85 } 86 }