|
1 /* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 "use strict"; |
|
7 |
|
8 const { Cc, Ci, Cu, Cr } = require("chrome"); |
|
9 const { Promise: promise } = Cu.import("resource://gre/modules/Promise.jsm", {}); |
|
10 const EventEmitter = require("devtools/toolkit/event-emitter"); |
|
11 const { DevToolsUtils } = Cu.import("resource://gre/modules/devtools/DevToolsUtils.jsm", {}); |
|
12 |
|
13 function DebuggerPanel(iframeWindow, toolbox) { |
|
14 this.panelWin = iframeWindow; |
|
15 this._toolbox = toolbox; |
|
16 this._destroyer = null; |
|
17 |
|
18 this._view = this.panelWin.DebuggerView; |
|
19 this._controller = this.panelWin.DebuggerController; |
|
20 this._view._hostType = this._toolbox.hostType; |
|
21 this._controller._target = this.target; |
|
22 this._controller._toolbox = this._toolbox; |
|
23 |
|
24 this.handleHostChanged = this.handleHostChanged.bind(this); |
|
25 this.highlightWhenPaused = this.highlightWhenPaused.bind(this); |
|
26 this.unhighlightWhenResumed = this.unhighlightWhenResumed.bind(this); |
|
27 |
|
28 EventEmitter.decorate(this); |
|
29 }; |
|
30 |
|
31 exports.DebuggerPanel = DebuggerPanel; |
|
32 |
|
33 DebuggerPanel.prototype = { |
|
34 /** |
|
35 * Open is effectively an asynchronous constructor. |
|
36 * |
|
37 * @return object |
|
38 * A promise that is resolved when the Debugger completes opening. |
|
39 */ |
|
40 open: function() { |
|
41 let targetPromise; |
|
42 |
|
43 // Local debugging needs to make the target remote. |
|
44 if (!this.target.isRemote) { |
|
45 targetPromise = this.target.makeRemote(); |
|
46 } else { |
|
47 targetPromise = promise.resolve(this.target); |
|
48 } |
|
49 |
|
50 return targetPromise |
|
51 .then(() => this._controller.startupDebugger()) |
|
52 .then(() => this._controller.connect()) |
|
53 .then(() => { |
|
54 this._toolbox.on("host-changed", this.handleHostChanged); |
|
55 this.target.on("thread-paused", this.highlightWhenPaused); |
|
56 this.target.on("thread-resumed", this.unhighlightWhenResumed); |
|
57 this.isReady = true; |
|
58 this.emit("ready"); |
|
59 return this; |
|
60 }) |
|
61 .then(null, function onError(aReason) { |
|
62 DevToolsUtils.reportException("DebuggerPanel.prototype.open", aReason); |
|
63 }); |
|
64 }, |
|
65 |
|
66 // DevToolPanel API |
|
67 |
|
68 get target() this._toolbox.target, |
|
69 |
|
70 destroy: function() { |
|
71 // Make sure this panel is not already destroyed. |
|
72 if (this._destroyer) { |
|
73 return this._destroyer; |
|
74 } |
|
75 |
|
76 this.target.off("thread-paused", this.highlightWhenPaused); |
|
77 this.target.off("thread-resumed", this.unhighlightWhenResumed); |
|
78 |
|
79 return this._destroyer = this._controller.shutdownDebugger().then(() => { |
|
80 this.emit("destroyed"); |
|
81 }); |
|
82 }, |
|
83 |
|
84 // DebuggerPanel API |
|
85 |
|
86 addBreakpoint: function(aLocation, aOptions) { |
|
87 return this._controller.Breakpoints.addBreakpoint(aLocation, aOptions); |
|
88 }, |
|
89 |
|
90 removeBreakpoint: function(aLocation) { |
|
91 return this._controller.Breakpoints.removeBreakpoint(aLocation); |
|
92 }, |
|
93 |
|
94 handleHostChanged: function() { |
|
95 this._view.handleHostChanged(this._toolbox.hostType); |
|
96 }, |
|
97 |
|
98 highlightWhenPaused: function() { |
|
99 this._toolbox.highlightTool("jsdebugger"); |
|
100 |
|
101 // Also raise the toolbox window if it is undocked or select the |
|
102 // corresponding tab when toolbox is docked. |
|
103 this._toolbox.raise(); |
|
104 }, |
|
105 |
|
106 unhighlightWhenResumed: function() { |
|
107 this._toolbox.unhighlightTool("jsdebugger"); |
|
108 } |
|
109 }; |