diff --git a/devtools/server/actors/targets/browsing-context.js b/devtools/server/actors/targets/browsing-context.js index c1a51667a32f..efa9a943c771 100644 --- a/devtools/server/actors/targets/browsing-context.js +++ b/devtools/server/actors/targets/browsing-context.js @@ -1346,25 +1346,6 @@ const browsingContextTargetPrototype = { isTopLevel: isTopLevel, id: getWindowID(window), }); - - // TODO bug 997119: move that code to ThreadActor by listening to - // window-ready - const threadActor = this.threadActor; - if (isTopLevel && threadActor.state != "detached") { - this.sources.reset(); - threadActor.clearDebuggees(); - threadActor.dbg.enable(); - threadActor.maybePauseOnExceptions(); - // Update the global no matter if the debugger is on or off, - // otherwise the global will be wrong when enabled later. - threadActor.global = window; - } - - // Refresh the debuggee list when a new window object appears (top window or - // iframe). - if (threadActor.attached) { - threadActor.dbg.addDebuggees(); - } }, _windowDestroyed(window, id = null, isFrozen = false) { @@ -1417,16 +1398,6 @@ const browsingContextTargetPrototype = { return; } - // Proceed normally only if the debuggee is not paused. - // TODO bug 997119: move that code to ThreadActor by listening to - // will-navigate - const threadActor = this.threadActor; - if (threadActor.state == "paused") { - threadActor.unsafeSynchronize(Promise.resolve(threadActor.doResume())); - threadActor.dbg.disable(); - } - threadActor.disableAllBreakpoints(); - this.emit("tabNavigated", { url: newURI, nativeConsoleAPI: true, @@ -1461,12 +1432,6 @@ const browsingContextTargetPrototype = { return; } - // TODO bug 997119: move that code to ThreadActor by listening to navigate - const threadActor = this.threadActor; - if (threadActor.state == "running") { - threadActor.dbg.enable(); - } - this.emit("tabNavigated", { url: this.url, title: this.title, diff --git a/devtools/server/actors/thread.js b/devtools/server/actors/thread.js index be756167301c..3074b6f9b746 100644 --- a/devtools/server/actors/thread.js +++ b/devtools/server/actors/thread.js @@ -138,6 +138,13 @@ const ThreadActor = ActorClassWithSpec(threadSpec, { this._onOpeningRequest = this._onOpeningRequest.bind(this); this._onNewDebuggee = this._onNewDebuggee.bind(this); this._eventBreakpointListener = this._eventBreakpointListener.bind(this); + this._onWindowReady = this._onWindowReady.bind(this); + this._onWillNavigate = this._onWillNavigate.bind(this); + this._onNavigate = this._onNavigate.bind(this); + + this._parent.on("window-ready", this._onWindowReady); + this._parent.on("will-navigate", this._onWillNavigate); + this._parent.on("navigate", this._onNavigate); this._debuggerNotificationObserver = new DebuggerNotificationObserver(); @@ -282,6 +289,10 @@ const ThreadActor = ActorClassWithSpec(threadSpec, { } catch (e) {} } + this._parent.off("window-ready", this._onWindowReady); + this._parent.off("will-navigate", this._onWillNavigate); + this._parent.off("navigate", this._onNavigate); + this.sources.off("newSource", this.onNewSourceEvent); this.clearDebuggees(); this.conn.removeActorPool(this._threadLifetimePool); @@ -1752,6 +1763,43 @@ const ThreadActor = ActorClassWithSpec(threadSpec, { return {}; }, + _onWindowReady: function({ isTopLevel, window }) { + if (isTopLevel && this.state != "detached") { + this.sources.reset(); + this.clearDebuggees(); + this.dbg.enabled = true; + this.maybePauseOnExceptions(); + // Update the global no matter if the debugger is on or off, + // otherwise the global will be wrong when enabled later. + this.global = window; + } + + // Refresh the debuggee list when a new window object appears (top window or + // iframe). + if (this.attached) { + this.dbg.addDebuggees(); + } + }, + + _onWillNavigate: function({ isTopLevel }) { + if (!isTopLevel) { + return; + } + + // Proceed normally only if the debuggee is not paused. + if (this.state == "paused") { + this.unsafeSynchronize(Promise.resolve(this.doResume())); + this.dbg.enabled = false; + } + this.disableAllBreakpoints(); + }, + + _onNavigate: function() { + if (this.state == "running") { + this.dbg.enabled = true; + } + }, + // JS Debugger API hooks. /** diff --git a/devtools/server/startup/worker.js b/devtools/server/startup/worker.js index f8a62f56c29a..87cdffc87166 100644 --- a/devtools/server/startup/worker.js +++ b/devtools/server/startup/worker.js @@ -37,6 +37,7 @@ this.rpc = function(method, ...params) { loadSubScript("resource://devtools/shared/worker/loader.js"); var defer = worker.require("devtools/shared/defer"); +var EventEmitter = worker.require("devtools/shared/event-emitter"); var { ActorPool } = worker.require("devtools/server/actors/common"); var { ThreadActor } = worker.require("devtools/server/actors/thread"); var { WebConsoleActor } = worker.require("devtools/server/actors/webconsole"); @@ -97,6 +98,8 @@ this.addEventListener("message", function(event) { }, }; + EventEmitter.decorate(parent); + const threadActor = new ThreadActor(parent, global); pool.addActor(threadActor); diff --git a/devtools/server/tests/unit/testactors.js b/devtools/server/tests/unit/testactors.js index b94a5d328a56..55202e190de1 100644 --- a/devtools/server/tests/unit/testactors.js +++ b/devtools/server/tests/unit/testactors.js @@ -16,6 +16,8 @@ const { const { TabSources } = require("devtools/server/actors/utils/TabSources"); const makeDebugger = require("devtools/server/actors/utils/make-debugger"); +const noop = () => {}; + var gTestGlobals = new Set(); DebuggerServer.addTestGlobal = function(global) { gTestGlobals.add(global); @@ -122,6 +124,8 @@ function TestTargetActor(connection, global) { TestTargetActor.prototype = { constructor: TestTargetActor, actorPrefix: "TestTargetActor", + on: noop, + off: noop, get window() { return this._global;