summaryrefslogtreecommitdiffstats
path: root/remote/shared/MobileTabBrowser.sys.mjs
blob: fd8e78e45ada3f8d818e17fbf7dc77926221739d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* 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/. */

import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  windowManager: "chrome://remote/content/shared/WindowManager.sys.mjs",
});

XPCOMUtils.defineLazyModuleGetters(lazy, {
  GeckoViewTabUtil: "resource://gre/modules/GeckoViewTestUtils.jsm",
});

// GeckoView shim for Desktop's gBrowser
export class MobileTabBrowser {
  constructor(window) {
    this.window = window;
  }

  get tabs() {
    return [this.window.tab];
  }

  get selectedTab() {
    return this.window.tab;
  }

  set selectedTab(tab) {
    if (tab != this.selectedTab) {
      throw new Error("GeckoView only supports a single tab");
    }

    // Synthesize a custom TabSelect event to indicate that a tab has been
    // selected even when we don't change it.
    const event = this.window.CustomEvent("TabSelect", {
      bubbles: true,
      cancelable: false,
      detail: {
        previousTab: this.selectedTab,
      },
    });
    this.window.document.dispatchEvent(event);
  }

  get selectedBrowser() {
    return this.selectedTab.linkedBrowser;
  }

  addEventListener() {
    this.window.addEventListener(...arguments);
  }

  /**
   * Create a new tab.
   *
   * @param url URL to load within the newly opended tab.
   *
   * @return {Promise<Tab>} The created tab.
   * @throws {Error} Throws an error if the tab cannot be created.
   */
  addTab(url) {
    return lazy.GeckoViewTabUtil.createNewTab(url);
  }

  getTabForBrowser(browser) {
    if (browser != this.selectedBrowser) {
      throw new Error("GeckoView only supports a single tab");
    }

    return this.selectedTab;
  }

  removeEventListener() {
    this.window.removeEventListener(...arguments);
  }

  removeTab(tab) {
    if (tab != this.selectedTab) {
      throw new Error("GeckoView only supports a single tab");
    }

    return lazy.windowManager.closeWindow(this.window);
  }
}