diff --git a/remote/cdp/domains/parent/Target.sys.mjs b/remote/cdp/domains/parent/Target.sys.mjs index 5f0229ce4d10..e1d6d64b9f6f 100644 --- a/remote/cdp/domains/parent/Target.sys.mjs +++ b/remote/cdp/domains/parent/Target.sys.mjs @@ -19,23 +19,31 @@ ChromeUtils.defineESModuleGetters(lazy, { let browserContextIds = 1; export class Target extends Domain { + #browserContextIds; + constructor(session) { super(session); + this.#browserContextIds = new Set(); + this._onTargetCreated = this._onTargetCreated.bind(this); this._onTargetDestroyed = this._onTargetDestroyed.bind(this); } getBrowserContexts() { - return { - browserContextIds: [], - }; + const browserContextIds = lazy.ContextualIdentityService.getPublicUserContextIds().filter( + id => this.#browserContextIds.has(id) + ); + + return { browserContextIds }; } createBrowserContext() { const identity = lazy.ContextualIdentityService.create( "remote-agent-" + browserContextIds++ ); + + this.#browserContextIds.add(identity.userContextId); return { browserContextId: identity.userContextId }; } @@ -44,6 +52,8 @@ export class Target extends Domain { lazy.ContextualIdentityService.remove(browserContextId); lazy.ContextualIdentityService.closeContainerTabs(browserContextId); + + this.#browserContextIds.delete(browserContextId); } getTargets() { diff --git a/remote/cdp/test/browser/target/browser_browserContext.js b/remote/cdp/test/browser/target/browser_browserContext.js index 5c2c25d640f7..9fe4e0f3dc7c 100644 --- a/remote/cdp/test/browser/target/browser_browserContext.js +++ b/remote/cdp/test/browser/target/browser_browserContext.js @@ -12,8 +12,29 @@ add_task(async function({ CDP }) { const { Target } = client; await getDiscoveredTargets(Target); + // Test if Target.getBrowserContexts is empty before creatinga ny + const { + browserContextIds: browserContextIdsBefore, + } = await Target.getBrowserContexts(); + + is( + browserContextIdsBefore.length, + 0, + "No browser context is open by default" + ); + const { browserContextId } = await Target.createBrowserContext(); + // Test if Target.getBrowserContexts includes the context we just created + const { browserContextIds } = await Target.getBrowserContexts(); + + is(browserContextIds.length, 1, "Got expected length of browser contexts"); + is( + browserContextIds[0], + browserContextId, + "Got expected browser context id from getBrowserContexts" + ); + const targetCreated = Target.targetCreated(); const { targetId } = await Target.createTarget({ browserContextId }); ok(!!targetId, "Target.createTarget returns a non-empty target id"); @@ -34,6 +55,17 @@ add_task(async function({ CDP }) { // Releasing the browser context is going to remove the tab opened when calling createTarget await Target.disposeBrowserContext({ browserContextId }); + // Test if Target.getBrowserContexts now is empty + const { + browserContextIds: browserContextIdsAfter, + } = await Target.getBrowserContexts(); + + is( + browserContextIdsAfter.length, + 0, + "After closing all browser contexts none is available anymore" + ); + await client.close(); info("The client is closed"); });