Bug 1874920 - [bidi] Avoid using internal user context id in bidi modules r=webdriver-reviewers,whimboo

Depends on D200182

Differential Revision: https://phabricator.services.mozilla.com/D200610
This commit is contained in:
Julian Descottes 2024-02-06 13:34:44 +00:00
parent 4620282750
commit 191a9767e8
6 changed files with 47 additions and 47 deletions

View File

@ -13,6 +13,8 @@ ChromeUtils.defineESModuleGetters(lazy, {
generateUUID: "chrome://remote/content/shared/UUID.sys.mjs",
TabManager: "chrome://remote/content/shared/TabManager.sys.mjs",
TabSession: "chrome://remote/content/cdp/sessions/TabSession.sys.mjs",
UserContextManager:
"chrome://remote/content/shared/UserContextManager.sys.mjs",
windowManager: "chrome://remote/content/shared/WindowManager.sys.mjs",
});
@ -131,7 +133,9 @@ export class Target extends Domain {
const onTarget = targetList.once("target-created");
const tab = await lazy.TabManager.addTab({
focus: true,
userContextId: browserContextId,
userContextId:
// Bug 1878649: Use UserContextManager ids consistently in CDP.
lazy.UserContextManager.getIdByInternalId(browserContextId),
window,
});

View File

@ -11,6 +11,8 @@ ChromeUtils.defineESModuleGetters(lazy, {
EventPromise: "chrome://remote/content/shared/Sync.sys.mjs",
generateUUID: "chrome://remote/content/shared/UUID.sys.mjs",
MobileTabBrowser: "chrome://remote/content/shared/MobileTabBrowser.sys.mjs",
UserContextManager:
"chrome://remote/content/shared/UserContextManager.sys.mjs",
});
class TabManagerClass {
@ -143,8 +145,8 @@ class TabManagerClass {
* The reference tab after which the new tab will be added. If no
* reference tab is provided, the new tab will be added after all the
* other tabs.
* @param {number} options.userContextId
* The user context (container) id.
* @param {string=} options.userContextId
* A user context id from UserContextManager.
* @param {window=} options.window
* The window where the new tab will open. Defaults to Services.wm.getMostRecentWindow
* if no window is provided. Will be ignored if referenceTab is provided.
@ -153,7 +155,7 @@ class TabManagerClass {
let {
focus = false,
referenceTab = null,
userContextId,
userContextId = null,
window = Services.wm.getMostRecentWindow(null),
} = options;
@ -169,10 +171,11 @@ class TabManagerClass {
}
const tabBrowser = this.getTabBrowser(window);
const tab = await tabBrowser.addTab("about:blank", {
index,
triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
userContextId,
userContextId: lazy.UserContextManager.getInternalIdById(userContextId),
});
if (focus) {

View File

@ -13,6 +13,9 @@ ChromeUtils.defineESModuleGetters(lazy, {
generateUUID: "chrome://remote/content/shared/UUID.sys.mjs",
});
const DEFAULT_CONTEXT_ID = "default";
const DEFAULT_INTERNAL_ID = 0;
/**
* A UserContextManager instance keeps track of all public user contexts and
* maps their internal platform.
@ -24,9 +27,6 @@ export class UserContextManagerClass {
#contextualIdentityListener;
#userContextIds;
DEFAULT_CONTEXT_ID = "default";
DEFAULT_INTERNAL_ID = 0;
constructor() {
// Map from internal ids (numbers) from the ContextualIdentityService to
// opaque UUIDs (string).
@ -34,7 +34,7 @@ export class UserContextManagerClass {
// The default user context is always using 0 as internal user context id
// and should be exposed as "default" instead of a randomly generated id.
this.#userContextIds.set(this.DEFAULT_INTERNAL_ID, this.DEFAULT_CONTEXT_ID);
this.#userContextIds.set(DEFAULT_INTERNAL_ID, DEFAULT_CONTEXT_ID);
// Register other (non-default) public contexts.
lazy.ContextualIdentityService.getPublicIdentities().forEach(identity =>
@ -55,6 +55,16 @@ export class UserContextManagerClass {
this.#userContextIds = null;
}
/**
* Retrieve the user context id corresponding to the default user context.
*
* @returns {string}
* The default user context id.
*/
get defaultUserContextId() {
return DEFAULT_CONTEXT_ID;
}
/**
* Creates a new user context.
*
@ -93,8 +103,14 @@ export class UserContextManagerClass {
* If `browsingContext` is not a CanonicalBrowsingContext instance.
*/
getIdByBrowsingContext(browsingContext) {
if (!CanonicalBrowsingContext.isInstance(browsingContext)) {
throw new TypeError(
`Expected browsingContext to be a CanonicalBrowsingContext, got ${browsingContext}`
);
}
return this.getIdByInternalId(
this.getInternalIdByBrowsingContext(browsingContext)
browsingContext.originAttributes.userContextId
);
}
@ -115,29 +131,6 @@ export class UserContextManagerClass {
return null;
}
/**
* Retrieve the internal user context id corresponding to the provided
* browsing context.
*
* @param {BrowsingContext} browsingContext
* The browsing context to get the internal user context id from.
*
* @returns {string}
* The corresponding internal user context id.
*
* @throws {TypeError}
* If `browsingContext` is not a CanonicalBrowsingContext instance.
*/
getInternalIdByBrowsingContext(browsingContext) {
if (!CanonicalBrowsingContext.isInstance(browsingContext)) {
throw new TypeError(
`Expected browsingContext to be a CanonicalBrowsingContext, got ${browsingContext}`
);
}
return browsingContext.originAttributes.userContextId;
}
/**
* Retrieve the internal id corresponding to the provided user
* context id.

View File

@ -13,6 +13,8 @@ ChromeUtils.defineESModuleGetters(lazy, {
generateUUID: "chrome://remote/content/shared/UUID.sys.mjs",
TabManager: "chrome://remote/content/shared/TabManager.sys.mjs",
TimedPromise: "chrome://remote/content/marionette/sync.sys.mjs",
UserContextManager:
"chrome://remote/content/shared/UserContextManager.sys.mjs",
waitForObserverTopic: "chrome://remote/content/marionette/sync.sys.mjs",
});
@ -218,7 +220,8 @@ class WindowManager {
{
private: isPrivate,
resolveOnContentBrowserCreated,
userContextId: userContextId !== null ? userContextId : undefined,
userContextId:
lazy.UserContextManager.getInternalIdById(userContextId),
}
)
);

View File

@ -107,7 +107,7 @@ class BrowserModule extends Module {
`Expected "userContext" to be a string, got ${userContextId}`
);
if (userContextId === lazy.UserContextManager.DEFAULT_CONTEXT_ID) {
if (userContextId === lazy.UserContextManager.defaultUserContextId) {
throw new lazy.error.InvalidArgumentError(
`Default user context cannot be removed`
);

View File

@ -488,12 +488,10 @@ class BrowsingContextModule extends Module {
}
}
let internalUserContextId = lazy.UserContextManager.DEFAULT_INTERNAL_ID;
let userContext = lazy.UserContextManager.defaultUserContextId;
if (referenceContext !== null) {
internalUserContextId =
lazy.UserContextManager.getInternalIdByBrowsingContext(
referenceContext
);
userContext =
lazy.UserContextManager.getIdByBrowsingContext(referenceContext);
}
if (userContextId !== null) {
@ -502,18 +500,17 @@ class BrowsingContextModule extends Module {
lazy.pprint`Expected "userContext" to be a string, got ${userContextId}`
);
internalUserContextId =
lazy.UserContextManager.getInternalIdById(userContextId);
if (internalUserContextId === null) {
if (!lazy.UserContextManager.hasUserContextId(userContextId)) {
throw new lazy.error.NoSuchUserContextError(
`User Context with id ${userContextId} was not found`
);
}
userContext = userContextId;
if (
lazy.AppInfo.isAndroid &&
userContextId != lazy.UserContextManager.DEFAULT_CONTEXT_ID
userContext != lazy.UserContextManager.defaultUserContextId
) {
throw new lazy.error.UnsupportedOperationError(
`browsingContext.create with non-default "userContext" not supported for ${lazy.AppInfo.name}`
@ -538,7 +535,7 @@ class BrowsingContextModule extends Module {
case "window":
const newWindow = await lazy.windowManager.openBrowserWindow({
focus: !background,
userContextId: internalUserContextId,
userContextId: userContext,
});
browser = lazy.TabManager.getTabBrowser(newWindow).selectedBrowser;
break;
@ -559,7 +556,7 @@ class BrowsingContextModule extends Module {
const tab = await lazy.TabManager.addTab({
focus: !background,
referenceTab,
userContextId: internalUserContextId,
userContextId: userContext,
});
browser = lazy.TabManager.getBrowserForTab(tab);
}