diff --git a/remote/domains/parent/Target.jsm b/remote/domains/parent/Target.jsm index e7ceeb1a4c0c..823001eeb74f 100644 --- a/remote/domains/parent/Target.jsm +++ b/remote/domains/parent/Target.jsm @@ -46,6 +46,7 @@ class Target extends Domain { browserContextId: target.id, targetId: target.id, type: target.type, + url: target.url, }, }); } diff --git a/remote/targets/Targets.jsm b/remote/targets/Targets.jsm index 338dd6916553..ae2f7e5dffdc 100644 --- a/remote/targets/Targets.jsm +++ b/remote/targets/Targets.jsm @@ -52,8 +52,13 @@ class Targets { clear() { for (const target of this) { - this.disconnect(target.browser); + // The main process target doesn't have a `browser` and so would fail here. + // Ignore it here, and instead destroy it individually right after this. + if (target != this.mainProcessTarget) { + this.disconnect(target.browser); + } } + this._targets.clear(); if (this.mainProcessTarget) { this.mainProcessTarget.disconnect(); this.mainProcessTarget = null; @@ -80,6 +85,7 @@ class Targets { getMainProcessTarget() { if (!this.mainProcessTarget) { this.mainProcessTarget = new MainProcessTarget(this); + this._targets.set(this.mainProcessTarget.id, this.mainProcessTarget); this.emit("connect", this.mainProcessTarget); } return this.mainProcessTarget; diff --git a/remote/test/browser/browser_target.js b/remote/test/browser/browser_target.js index 98050ed38d24..1240094ce9d2 100644 --- a/remote/test/browser/browser_target.js +++ b/remote/test/browser/browser_target.js @@ -20,14 +20,31 @@ add_task(async function() { const {Target} = client; ok("Target" in client, "Target domain is available"); - const targetCreatedForAlreadyOpenedTab = Target.targetCreated(); + const onTargetsCreated = new Promise(resolve => { + let gotTabTarget = false, gotMainTarget = false; + const unsubscribe = Target.targetCreated(event => { + if (event.targetInfo.type == "page" && + event.targetInfo.url == gBrowser.selectedBrowser.currentURI.spec) { + info("Got the current tab target"); + gotTabTarget = true; + } + if (event.targetInfo.type == "browser") { + info("Got the browser target"); + gotMainTarget = true; + } + if (gotTabTarget && gotMainTarget) { + unsubscribe(); + resolve(); + } + }); + }); // Instruct the server to fire Target.targetCreated events Target.setDiscoverTargets({ discover: true }); // Calling `setDiscoverTargets` will dispatch `targetCreated` event for all - // already opened tabs - await targetCreatedForAlreadyOpenedTab; + // already opened tabs and the browser target. + await onTargetsCreated; // Create a new target so that the test runs against a fresh new tab const targetCreated = Target.targetCreated(); diff --git a/remote/test/browser/browser_target_close.js b/remote/test/browser/browser_target_close.js index d9c5c98b3e4e..8bc040609281 100644 --- a/remote/test/browser/browser_target_close.js +++ b/remote/test/browser/browser_target_close.js @@ -14,14 +14,26 @@ add_task(async function() { info("Setup Target domain"); const { Target } = client; - const targetCreated = Target.targetCreated(); + + // Wait for all Target.targetCreated event. One for each tab, plus the one + // for the main process target. + const targetsCreated = new Promise(resolve => { + let targets = 0; + const unsubscribe = Target.targetCreated(event => { + if (++targets >= gBrowser.tabs.length + 1) { + unsubscribe(); + resolve(); + } + }); + }); Target.setDiscoverTargets({ discover: true }); - await targetCreated; + await targetsCreated; info("Create a new tab and wait for the target to be created"); const otherTargetCreated = Target.targetCreated(); const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URI); const {targetInfo} = await otherTargetCreated; + is(targetInfo.type, "page"); const onTabClose = BrowserTestUtils.waitForEvent(tab, "TabClose"); const targetDestroyed = Target.targetDestroyed();