Bug 1694390 - Make TabManager a class. r=webdriver-reviewers,jdescottes,whimboo

Differential Revision: https://phabricator.services.mozilla.com/D191332
This commit is contained in:
Alexandra Borovova 2023-10-25 13:02:54 +00:00
Родитель fdf02cbb40
Коммит 1a322fdc55
1 изменённых файлов: 33 добавлений и 26 удалений

Просмотреть файл

@ -11,10 +11,14 @@ ChromeUtils.defineESModuleGetters(lazy, {
MobileTabBrowser: "chrome://remote/content/shared/MobileTabBrowser.sys.mjs",
});
// Maps browser's permanentKey to uuid: WeakMap.<Object, string>
const browserUniqueIds = new WeakMap();
class TabManagerClass {
#browserUniqueIds;
constructor() {
// Maps browser's permanentKey to uuid: WeakMap.<Object, string>
this.#browserUniqueIds = new WeakMap();
}
export var TabManager = {
/**
* Retrieve all the browser elements from tabs as contained in open windows.
*
@ -35,11 +39,11 @@ export var TabManager = {
}
return browsers;
},
}
get windows() {
return Services.wm.getEnumerator(null);
},
}
/**
* Array of unique browser ids (UUIDs) for all content browsers of all
@ -66,7 +70,7 @@ export var TabManager = {
}
return browserIds;
},
}
/**
* Get the <code>&lt;xul:browser&gt;</code> for the specified tab.
@ -83,7 +87,7 @@ export var TabManager = {
}
return null;
},
}
/**
* Return the tab browser for the specified chrome window.
@ -102,7 +106,7 @@ export var TabManager = {
}
return null;
},
}
/**
* Create a new tab.
@ -153,7 +157,7 @@ export var TabManager = {
}
return tab;
},
}
/**
* Retrieve the browser element corresponding to the provided unique id,
@ -179,7 +183,7 @@ export var TabManager = {
}
}
return null;
},
}
/**
* Retrieve the browsing context corresponding to the provided unique id.
@ -196,7 +200,7 @@ export var TabManager = {
}
return BrowsingContext.get(id);
},
}
/**
* Retrieve the unique id for the given xul browser element. The id is a
@ -215,11 +219,11 @@ export var TabManager = {
}
const key = browserElement.permanentKey;
if (!browserUniqueIds.has(key)) {
browserUniqueIds.set(key, lazy.generateUUID());
if (!this.#browserUniqueIds.has(key)) {
this.#browserUniqueIds.set(key, lazy.generateUUID());
}
return browserUniqueIds.get(key);
},
return this.#browserUniqueIds.get(key);
}
/**
* Retrieve the id of a Browsing Context.
@ -243,7 +247,7 @@ export var TabManager = {
}
return browsingContext.id.toString();
},
}
/**
* Get the navigable for the given browsing context.
@ -271,7 +275,7 @@ export var TabManager = {
}
return browsingContext;
},
}
getTabCount() {
let count = 0;
@ -281,7 +285,7 @@ export var TabManager = {
count += tabsLength ? tabsLength : 1;
}
return count;
},
}
/**
* Retrieve the tab owning a Browsing Context.
@ -300,7 +304,7 @@ export var TabManager = {
const tabBrowser = this.getTabBrowser(browser.ownerGlobal);
return tabBrowser.getTabForBrowser(browser);
},
}
/**
* Retrieve the list of tabs for a given window.
@ -320,14 +324,14 @@ export var TabManager = {
return tabBrowser.tabs;
}
return [];
},
}
getWindowForTab(tab) {
// `.linkedBrowser.ownerGlobal` works both with Firefox Desktop and Mobile.
// Other accessors (eg `.ownerGlobal` or `.browser.ownerGlobal`) fail on one
// of the platforms.
return tab.linkedBrowser.ownerGlobal;
},
}
/**
* Check if the given argument is a valid canonical browsing context and was not
@ -344,7 +348,7 @@ export var TabManager = {
CanonicalBrowsingContext.isInstance(browsingContext) &&
!browsingContext.isDiscarded
);
},
}
/**
* Remove the given tab.
@ -360,7 +364,7 @@ export var TabManager = {
const ownerWindow = this.getWindowForTab(tab);
const tabBrowser = this.getTabBrowser(ownerWindow);
await tabBrowser.removeTab(tab);
},
}
/**
* Select the given tab.
@ -386,9 +390,12 @@ export var TabManager = {
const selected = new lazy.EventPromise(ownerWindow, "TabSelect");
tabBrowser.selectedTab = tab;
return selected;
},
}
supportsTabs() {
return lazy.AppInfo.isAndroid || lazy.AppInfo.isFirefox;
},
};
}
}
// Expose a shared singleton.
export const TabManager = new TabManagerClass();