michael@0: /* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: const { Cc, Ci, Cu, Cr } = require("chrome"); michael@0: const { Promise: promise } = Cu.import("resource://gre/modules/Promise.jsm", {}); michael@0: const EventEmitter = require("devtools/toolkit/event-emitter"); michael@0: const { DevToolsUtils } = Cu.import("resource://gre/modules/devtools/DevToolsUtils.jsm", {}); michael@0: michael@0: function DebuggerPanel(iframeWindow, toolbox) { michael@0: this.panelWin = iframeWindow; michael@0: this._toolbox = toolbox; michael@0: this._destroyer = null; michael@0: michael@0: this._view = this.panelWin.DebuggerView; michael@0: this._controller = this.panelWin.DebuggerController; michael@0: this._view._hostType = this._toolbox.hostType; michael@0: this._controller._target = this.target; michael@0: this._controller._toolbox = this._toolbox; michael@0: michael@0: this.handleHostChanged = this.handleHostChanged.bind(this); michael@0: this.highlightWhenPaused = this.highlightWhenPaused.bind(this); michael@0: this.unhighlightWhenResumed = this.unhighlightWhenResumed.bind(this); michael@0: michael@0: EventEmitter.decorate(this); michael@0: }; michael@0: michael@0: exports.DebuggerPanel = DebuggerPanel; michael@0: michael@0: DebuggerPanel.prototype = { michael@0: /** michael@0: * Open is effectively an asynchronous constructor. michael@0: * michael@0: * @return object michael@0: * A promise that is resolved when the Debugger completes opening. michael@0: */ michael@0: open: function() { michael@0: let targetPromise; michael@0: michael@0: // Local debugging needs to make the target remote. michael@0: if (!this.target.isRemote) { michael@0: targetPromise = this.target.makeRemote(); michael@0: } else { michael@0: targetPromise = promise.resolve(this.target); michael@0: } michael@0: michael@0: return targetPromise michael@0: .then(() => this._controller.startupDebugger()) michael@0: .then(() => this._controller.connect()) michael@0: .then(() => { michael@0: this._toolbox.on("host-changed", this.handleHostChanged); michael@0: this.target.on("thread-paused", this.highlightWhenPaused); michael@0: this.target.on("thread-resumed", this.unhighlightWhenResumed); michael@0: this.isReady = true; michael@0: this.emit("ready"); michael@0: return this; michael@0: }) michael@0: .then(null, function onError(aReason) { michael@0: DevToolsUtils.reportException("DebuggerPanel.prototype.open", aReason); michael@0: }); michael@0: }, michael@0: michael@0: // DevToolPanel API michael@0: michael@0: get target() this._toolbox.target, michael@0: michael@0: destroy: function() { michael@0: // Make sure this panel is not already destroyed. michael@0: if (this._destroyer) { michael@0: return this._destroyer; michael@0: } michael@0: michael@0: this.target.off("thread-paused", this.highlightWhenPaused); michael@0: this.target.off("thread-resumed", this.unhighlightWhenResumed); michael@0: michael@0: return this._destroyer = this._controller.shutdownDebugger().then(() => { michael@0: this.emit("destroyed"); michael@0: }); michael@0: }, michael@0: michael@0: // DebuggerPanel API michael@0: michael@0: addBreakpoint: function(aLocation, aOptions) { michael@0: return this._controller.Breakpoints.addBreakpoint(aLocation, aOptions); michael@0: }, michael@0: michael@0: removeBreakpoint: function(aLocation) { michael@0: return this._controller.Breakpoints.removeBreakpoint(aLocation); michael@0: }, michael@0: michael@0: handleHostChanged: function() { michael@0: this._view.handleHostChanged(this._toolbox.hostType); michael@0: }, michael@0: michael@0: highlightWhenPaused: function() { michael@0: this._toolbox.highlightTool("jsdebugger"); michael@0: michael@0: // Also raise the toolbox window if it is undocked or select the michael@0: // corresponding tab when toolbox is docked. michael@0: this._toolbox.raise(); michael@0: }, michael@0: michael@0: unhighlightWhenResumed: function() { michael@0: this._toolbox.unhighlightTool("jsdebugger"); michael@0: } michael@0: };