1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/client/dbg-client.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,2375 @@ 1.4 +/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ft=javascript ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +"use strict"; 1.11 +var Ci = Components.interfaces; 1.12 +var Cc = Components.classes; 1.13 +var Cu = Components.utils; 1.14 +var Cr = Components.results; 1.15 +// On B2G scope object misbehaves and we have to bind globals to `this` 1.16 +// in order to ensure theses variable to be visible in transport.js 1.17 +this.Ci = Ci; 1.18 +this.Cc = Cc; 1.19 +this.Cu = Cu; 1.20 +this.Cr = Cr; 1.21 + 1.22 +this.EXPORTED_SYMBOLS = ["DebuggerTransport", 1.23 + "DebuggerClient", 1.24 + "RootClient", 1.25 + "debuggerSocketConnect", 1.26 + "LongStringClient", 1.27 + "EnvironmentClient", 1.28 + "ObjectClient"]; 1.29 + 1.30 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.31 +Cu.import("resource://gre/modules/NetUtil.jsm"); 1.32 +Cu.import("resource://gre/modules/Services.jsm"); 1.33 +Cu.import("resource://gre/modules/Timer.jsm"); 1.34 + 1.35 +let promise = Cu.import("resource://gre/modules/devtools/deprecated-sync-thenables.js").Promise; 1.36 +const { defer, resolve, reject } = promise; 1.37 + 1.38 +XPCOMUtils.defineLazyServiceGetter(this, "socketTransportService", 1.39 + "@mozilla.org/network/socket-transport-service;1", 1.40 + "nsISocketTransportService"); 1.41 + 1.42 +XPCOMUtils.defineLazyModuleGetter(this, "console", 1.43 + "resource://gre/modules/devtools/Console.jsm"); 1.44 + 1.45 +XPCOMUtils.defineLazyModuleGetter(this, "devtools", 1.46 + "resource://gre/modules/devtools/Loader.jsm"); 1.47 + 1.48 +Object.defineProperty(this, "WebConsoleClient", { 1.49 + get: function () { 1.50 + return devtools.require("devtools/toolkit/webconsole/client").WebConsoleClient; 1.51 + }, 1.52 + configurable: true, 1.53 + enumerable: true 1.54 +}); 1.55 + 1.56 +Components.utils.import("resource://gre/modules/devtools/DevToolsUtils.jsm"); 1.57 +this.makeInfallible = DevToolsUtils.makeInfallible; 1.58 + 1.59 +let wantLogging = Services.prefs.getBoolPref("devtools.debugger.log"); 1.60 + 1.61 +function dumpn(str) 1.62 +{ 1.63 + if (wantLogging) { 1.64 + dump("DBG-CLIENT: " + str + "\n"); 1.65 + } 1.66 +} 1.67 + 1.68 +let loader = Cc["@mozilla.org/moz/jssubscript-loader;1"] 1.69 + .getService(Ci.mozIJSSubScriptLoader); 1.70 +loader.loadSubScript("resource://gre/modules/devtools/server/transport.js", this); 1.71 + 1.72 +/** 1.73 + * Add simple event notification to a prototype object. Any object that has 1.74 + * some use for event notifications or the observer pattern in general can be 1.75 + * augmented with the necessary facilities by passing its prototype to this 1.76 + * function. 1.77 + * 1.78 + * @param aProto object 1.79 + * The prototype object that will be modified. 1.80 + */ 1.81 +function eventSource(aProto) { 1.82 + /** 1.83 + * Add a listener to the event source for a given event. 1.84 + * 1.85 + * @param aName string 1.86 + * The event to listen for. 1.87 + * @param aListener function 1.88 + * Called when the event is fired. If the same listener 1.89 + * is added more than once, it will be called once per 1.90 + * addListener call. 1.91 + */ 1.92 + aProto.addListener = function (aName, aListener) { 1.93 + if (typeof aListener != "function") { 1.94 + throw TypeError("Listeners must be functions."); 1.95 + } 1.96 + 1.97 + if (!this._listeners) { 1.98 + this._listeners = {}; 1.99 + } 1.100 + 1.101 + this._getListeners(aName).push(aListener); 1.102 + }; 1.103 + 1.104 + /** 1.105 + * Add a listener to the event source for a given event. The 1.106 + * listener will be removed after it is called for the first time. 1.107 + * 1.108 + * @param aName string 1.109 + * The event to listen for. 1.110 + * @param aListener function 1.111 + * Called when the event is fired. 1.112 + */ 1.113 + aProto.addOneTimeListener = function (aName, aListener) { 1.114 + let l = (...args) => { 1.115 + this.removeListener(aName, l); 1.116 + aListener.apply(null, args); 1.117 + }; 1.118 + this.addListener(aName, l); 1.119 + }; 1.120 + 1.121 + /** 1.122 + * Remove a listener from the event source previously added with 1.123 + * addListener(). 1.124 + * 1.125 + * @param aName string 1.126 + * The event name used during addListener to add the listener. 1.127 + * @param aListener function 1.128 + * The callback to remove. If addListener was called multiple 1.129 + * times, all instances will be removed. 1.130 + */ 1.131 + aProto.removeListener = function (aName, aListener) { 1.132 + if (!this._listeners || !this._listeners[aName]) { 1.133 + return; 1.134 + } 1.135 + this._listeners[aName] = 1.136 + this._listeners[aName].filter(function (l) { return l != aListener }); 1.137 + }; 1.138 + 1.139 + /** 1.140 + * Returns the listeners for the specified event name. If none are defined it 1.141 + * initializes an empty list and returns that. 1.142 + * 1.143 + * @param aName string 1.144 + * The event name. 1.145 + */ 1.146 + aProto._getListeners = function (aName) { 1.147 + if (aName in this._listeners) { 1.148 + return this._listeners[aName]; 1.149 + } 1.150 + this._listeners[aName] = []; 1.151 + return this._listeners[aName]; 1.152 + }; 1.153 + 1.154 + /** 1.155 + * Notify listeners of an event. 1.156 + * 1.157 + * @param aName string 1.158 + * The event to fire. 1.159 + * @param arguments 1.160 + * All arguments will be passed along to the listeners, 1.161 + * including the name argument. 1.162 + */ 1.163 + aProto.notify = function () { 1.164 + if (!this._listeners) { 1.165 + return; 1.166 + } 1.167 + 1.168 + let name = arguments[0]; 1.169 + let listeners = this._getListeners(name).slice(0); 1.170 + 1.171 + for each (let listener in listeners) { 1.172 + try { 1.173 + listener.apply(null, arguments); 1.174 + } catch (e) { 1.175 + // Prevent a bad listener from interfering with the others. 1.176 + DevToolsUtils.reportException("notify event '" + name + "'", e); 1.177 + } 1.178 + } 1.179 + } 1.180 +} 1.181 + 1.182 +/** 1.183 + * Set of protocol messages that affect thread state, and the 1.184 + * state the actor is in after each message. 1.185 + */ 1.186 +const ThreadStateTypes = { 1.187 + "paused": "paused", 1.188 + "resumed": "attached", 1.189 + "detached": "detached" 1.190 +}; 1.191 + 1.192 +/** 1.193 + * Set of protocol messages that are sent by the server without a prior request 1.194 + * by the client. 1.195 + */ 1.196 +const UnsolicitedNotifications = { 1.197 + "consoleAPICall": "consoleAPICall", 1.198 + "eventNotification": "eventNotification", 1.199 + "fileActivity": "fileActivity", 1.200 + "lastPrivateContextExited": "lastPrivateContextExited", 1.201 + "logMessage": "logMessage", 1.202 + "networkEvent": "networkEvent", 1.203 + "networkEventUpdate": "networkEventUpdate", 1.204 + "newGlobal": "newGlobal", 1.205 + "newScript": "newScript", 1.206 + "newSource": "newSource", 1.207 + "tabDetached": "tabDetached", 1.208 + "tabListChanged": "tabListChanged", 1.209 + "reflowActivity": "reflowActivity", 1.210 + "addonListChanged": "addonListChanged", 1.211 + "tabNavigated": "tabNavigated", 1.212 + "pageError": "pageError", 1.213 + "documentLoad": "documentLoad", 1.214 + "enteredFrame": "enteredFrame", 1.215 + "exitedFrame": "exitedFrame", 1.216 + "appOpen": "appOpen", 1.217 + "appClose": "appClose", 1.218 + "appInstall": "appInstall", 1.219 + "appUninstall": "appUninstall" 1.220 +}; 1.221 + 1.222 +/** 1.223 + * Set of pause types that are sent by the server and not as an immediate 1.224 + * response to a client request. 1.225 + */ 1.226 +const UnsolicitedPauses = { 1.227 + "resumeLimit": "resumeLimit", 1.228 + "debuggerStatement": "debuggerStatement", 1.229 + "breakpoint": "breakpoint", 1.230 + "DOMEvent": "DOMEvent", 1.231 + "watchpoint": "watchpoint", 1.232 + "exception": "exception" 1.233 +}; 1.234 + 1.235 +/** 1.236 + * Creates a client for the remote debugging protocol server. This client 1.237 + * provides the means to communicate with the server and exchange the messages 1.238 + * required by the protocol in a traditional JavaScript API. 1.239 + */ 1.240 +this.DebuggerClient = function (aTransport) 1.241 +{ 1.242 + this._transport = aTransport; 1.243 + this._transport.hooks = this; 1.244 + 1.245 + // Map actor ID to client instance for each actor type. 1.246 + this._threadClients = new Map; 1.247 + this._addonClients = new Map; 1.248 + this._tabClients = new Map; 1.249 + this._tracerClients = new Map; 1.250 + this._consoleClients = new Map; 1.251 + 1.252 + this._pendingRequests = []; 1.253 + this._activeRequests = new Map; 1.254 + this._eventsEnabled = true; 1.255 + 1.256 + this.compat = new ProtocolCompatibility(this, []); 1.257 + this.traits = {}; 1.258 + 1.259 + this.request = this.request.bind(this); 1.260 + this.localTransport = this._transport.onOutputStreamReady === undefined; 1.261 + 1.262 + /* 1.263 + * As the first thing on the connection, expect a greeting packet from 1.264 + * the connection's root actor. 1.265 + */ 1.266 + this.mainRoot = null; 1.267 + this.expectReply("root", (aPacket) => { 1.268 + this.mainRoot = new RootClient(this, aPacket); 1.269 + this.notify("connected", aPacket.applicationType, aPacket.traits); 1.270 + }); 1.271 +} 1.272 + 1.273 +/** 1.274 + * A declarative helper for defining methods that send requests to the server. 1.275 + * 1.276 + * @param aPacketSkeleton 1.277 + * The form of the packet to send. Can specify fields to be filled from 1.278 + * the parameters by using the |args| function. 1.279 + * @param telemetry 1.280 + * The unique suffix of the telemetry histogram id. 1.281 + * @param before 1.282 + * The function to call before sending the packet. Is passed the packet, 1.283 + * and the return value is used as the new packet. The |this| context is 1.284 + * the instance of the client object we are defining a method for. 1.285 + * @param after 1.286 + * The function to call after the response is received. It is passed the 1.287 + * response, and the return value is considered the new response that 1.288 + * will be passed to the callback. The |this| context is the instance of 1.289 + * the client object we are defining a method for. 1.290 + */ 1.291 +DebuggerClient.requester = function (aPacketSkeleton, 1.292 + { telemetry, before, after }) { 1.293 + return DevToolsUtils.makeInfallible(function (...args) { 1.294 + let histogram, startTime; 1.295 + if (telemetry) { 1.296 + let transportType = this._transport.onOutputStreamReady === undefined 1.297 + ? "LOCAL_" 1.298 + : "REMOTE_"; 1.299 + let histogramId = "DEVTOOLS_DEBUGGER_RDP_" 1.300 + + transportType + telemetry + "_MS"; 1.301 + histogram = Services.telemetry.getHistogramById(histogramId); 1.302 + startTime = +new Date; 1.303 + } 1.304 + let outgoingPacket = { 1.305 + to: aPacketSkeleton.to || this.actor 1.306 + }; 1.307 + 1.308 + let maxPosition = -1; 1.309 + for (let k of Object.keys(aPacketSkeleton)) { 1.310 + if (aPacketSkeleton[k] instanceof DebuggerClient.Argument) { 1.311 + let { position } = aPacketSkeleton[k]; 1.312 + outgoingPacket[k] = aPacketSkeleton[k].getArgument(args); 1.313 + maxPosition = Math.max(position, maxPosition); 1.314 + } else { 1.315 + outgoingPacket[k] = aPacketSkeleton[k]; 1.316 + } 1.317 + } 1.318 + 1.319 + if (before) { 1.320 + outgoingPacket = before.call(this, outgoingPacket); 1.321 + } 1.322 + 1.323 + this.request(outgoingPacket, DevToolsUtils.makeInfallible(function (aResponse) { 1.324 + if (after) { 1.325 + let { from } = aResponse; 1.326 + aResponse = after.call(this, aResponse); 1.327 + if (!aResponse.from) { 1.328 + aResponse.from = from; 1.329 + } 1.330 + } 1.331 + 1.332 + // The callback is always the last parameter. 1.333 + let thisCallback = args[maxPosition + 1]; 1.334 + if (thisCallback) { 1.335 + thisCallback(aResponse); 1.336 + } 1.337 + 1.338 + if (histogram) { 1.339 + histogram.add(+new Date - startTime); 1.340 + } 1.341 + }.bind(this), "DebuggerClient.requester request callback")); 1.342 + 1.343 + }, "DebuggerClient.requester"); 1.344 +}; 1.345 + 1.346 +function args(aPos) { 1.347 + return new DebuggerClient.Argument(aPos); 1.348 +} 1.349 + 1.350 +DebuggerClient.Argument = function (aPosition) { 1.351 + this.position = aPosition; 1.352 +}; 1.353 + 1.354 +DebuggerClient.Argument.prototype.getArgument = function (aParams) { 1.355 + if (!(this.position in aParams)) { 1.356 + throw new Error("Bad index into params: " + this.position); 1.357 + } 1.358 + return aParams[this.position]; 1.359 +}; 1.360 + 1.361 +DebuggerClient.prototype = { 1.362 + /** 1.363 + * Connect to the server and start exchanging protocol messages. 1.364 + * 1.365 + * @param aOnConnected function 1.366 + * If specified, will be called when the greeting packet is 1.367 + * received from the debugging server. 1.368 + */ 1.369 + connect: function (aOnConnected) { 1.370 + this.addOneTimeListener("connected", (aName, aApplicationType, aTraits) => { 1.371 + this.traits = aTraits; 1.372 + if (aOnConnected) { 1.373 + aOnConnected(aApplicationType, aTraits); 1.374 + } 1.375 + }); 1.376 + 1.377 + this._transport.ready(); 1.378 + }, 1.379 + 1.380 + /** 1.381 + * Shut down communication with the debugging server. 1.382 + * 1.383 + * @param aOnClosed function 1.384 + * If specified, will be called when the debugging connection 1.385 + * has been closed. 1.386 + */ 1.387 + close: function (aOnClosed) { 1.388 + // Disable detach event notifications, because event handlers will be in a 1.389 + // cleared scope by the time they run. 1.390 + this._eventsEnabled = false; 1.391 + 1.392 + if (aOnClosed) { 1.393 + this.addOneTimeListener('closed', function (aEvent) { 1.394 + aOnClosed(); 1.395 + }); 1.396 + } 1.397 + 1.398 + const detachClients = (clientMap, next) => { 1.399 + const clients = clientMap.values(); 1.400 + const total = clientMap.size; 1.401 + let numFinished = 0; 1.402 + 1.403 + if (total == 0) { 1.404 + next(); 1.405 + return; 1.406 + } 1.407 + 1.408 + for (let client of clients) { 1.409 + let method = client instanceof WebConsoleClient ? "close" : "detach"; 1.410 + client[method](() => { 1.411 + if (++numFinished === total) { 1.412 + clientMap.clear(); 1.413 + next(); 1.414 + } 1.415 + }); 1.416 + } 1.417 + }; 1.418 + 1.419 + detachClients(this._consoleClients, () => { 1.420 + detachClients(this._threadClients, () => { 1.421 + detachClients(this._tabClients, () => { 1.422 + detachClients(this._addonClients, () => { 1.423 + this._transport.close(); 1.424 + this._transport = null; 1.425 + }); 1.426 + }); 1.427 + }); 1.428 + }); 1.429 + }, 1.430 + 1.431 + /* 1.432 + * This function exists only to preserve DebuggerClient's interface; 1.433 + * new code should say 'client.mainRoot.listTabs()'. 1.434 + */ 1.435 + listTabs: function (aOnResponse) { return this.mainRoot.listTabs(aOnResponse); }, 1.436 + 1.437 + /* 1.438 + * This function exists only to preserve DebuggerClient's interface; 1.439 + * new code should say 'client.mainRoot.listAddons()'. 1.440 + */ 1.441 + listAddons: function (aOnResponse) { return this.mainRoot.listAddons(aOnResponse); }, 1.442 + 1.443 + /** 1.444 + * Attach to a tab actor. 1.445 + * 1.446 + * @param string aTabActor 1.447 + * The actor ID for the tab to attach. 1.448 + * @param function aOnResponse 1.449 + * Called with the response packet and a TabClient 1.450 + * (which will be undefined on error). 1.451 + */ 1.452 + attachTab: function (aTabActor, aOnResponse) { 1.453 + if (this._tabClients.has(aTabActor)) { 1.454 + let cachedTab = this._tabClients.get(aTabActor); 1.455 + let cachedResponse = { 1.456 + cacheEnabled: cachedTab.cacheEnabled, 1.457 + javascriptEnabled: cachedTab.javascriptEnabled, 1.458 + traits: cachedTab.traits, 1.459 + }; 1.460 + setTimeout(() => aOnResponse(cachedResponse, cachedTab), 0); 1.461 + return; 1.462 + } 1.463 + 1.464 + let packet = { 1.465 + to: aTabActor, 1.466 + type: "attach" 1.467 + }; 1.468 + this.request(packet, (aResponse) => { 1.469 + let tabClient; 1.470 + if (!aResponse.error) { 1.471 + tabClient = new TabClient(this, aResponse); 1.472 + this._tabClients.set(aTabActor, tabClient); 1.473 + } 1.474 + aOnResponse(aResponse, tabClient); 1.475 + }); 1.476 + }, 1.477 + 1.478 + /** 1.479 + * Attach to an addon actor. 1.480 + * 1.481 + * @param string aAddonActor 1.482 + * The actor ID for the addon to attach. 1.483 + * @param function aOnResponse 1.484 + * Called with the response packet and a AddonClient 1.485 + * (which will be undefined on error). 1.486 + */ 1.487 + attachAddon: function DC_attachAddon(aAddonActor, aOnResponse) { 1.488 + let packet = { 1.489 + to: aAddonActor, 1.490 + type: "attach" 1.491 + }; 1.492 + this.request(packet, aResponse => { 1.493 + let addonClient; 1.494 + if (!aResponse.error) { 1.495 + addonClient = new AddonClient(this, aAddonActor); 1.496 + this._addonClients[aAddonActor] = addonClient; 1.497 + this.activeAddon = addonClient; 1.498 + } 1.499 + aOnResponse(aResponse, addonClient); 1.500 + }); 1.501 + }, 1.502 + 1.503 + /** 1.504 + * Attach to a Web Console actor. 1.505 + * 1.506 + * @param string aConsoleActor 1.507 + * The ID for the console actor to attach to. 1.508 + * @param array aListeners 1.509 + * The console listeners you want to start. 1.510 + * @param function aOnResponse 1.511 + * Called with the response packet and a WebConsoleClient 1.512 + * instance (which will be undefined on error). 1.513 + */ 1.514 + attachConsole: 1.515 + function (aConsoleActor, aListeners, aOnResponse) { 1.516 + let packet = { 1.517 + to: aConsoleActor, 1.518 + type: "startListeners", 1.519 + listeners: aListeners, 1.520 + }; 1.521 + 1.522 + this.request(packet, (aResponse) => { 1.523 + let consoleClient; 1.524 + if (!aResponse.error) { 1.525 + if (this._consoleClients.has(aConsoleActor)) { 1.526 + consoleClient = this._consoleClients.get(aConsoleActor); 1.527 + } else { 1.528 + consoleClient = new WebConsoleClient(this, aResponse); 1.529 + this._consoleClients.set(aConsoleActor, consoleClient); 1.530 + } 1.531 + } 1.532 + aOnResponse(aResponse, consoleClient); 1.533 + }); 1.534 + }, 1.535 + 1.536 + /** 1.537 + * Attach to a global-scoped thread actor for chrome debugging. 1.538 + * 1.539 + * @param string aThreadActor 1.540 + * The actor ID for the thread to attach. 1.541 + * @param function aOnResponse 1.542 + * Called with the response packet and a ThreadClient 1.543 + * (which will be undefined on error). 1.544 + * @param object aOptions 1.545 + * Configuration options. 1.546 + * - useSourceMaps: whether to use source maps or not. 1.547 + */ 1.548 + attachThread: function (aThreadActor, aOnResponse, aOptions={}) { 1.549 + if (this._threadClients.has(aThreadActor)) { 1.550 + setTimeout(() => aOnResponse({}, this._threadClients.get(aThreadActor)), 0); 1.551 + return; 1.552 + } 1.553 + 1.554 + let packet = { 1.555 + to: aThreadActor, 1.556 + type: "attach", 1.557 + options: aOptions 1.558 + }; 1.559 + this.request(packet, (aResponse) => { 1.560 + if (!aResponse.error) { 1.561 + var threadClient = new ThreadClient(this, aThreadActor); 1.562 + this._threadClients.set(aThreadActor, threadClient); 1.563 + } 1.564 + aOnResponse(aResponse, threadClient); 1.565 + }); 1.566 + }, 1.567 + 1.568 + /** 1.569 + * Attach to a trace actor. 1.570 + * 1.571 + * @param string aTraceActor 1.572 + * The actor ID for the tracer to attach. 1.573 + * @param function aOnResponse 1.574 + * Called with the response packet and a TraceClient 1.575 + * (which will be undefined on error). 1.576 + */ 1.577 + attachTracer: function (aTraceActor, aOnResponse) { 1.578 + if (this._tracerClients.has(aTraceActor)) { 1.579 + setTimeout(() => aOnResponse({}, this._tracerClients.get(aTraceActor)), 0); 1.580 + return; 1.581 + } 1.582 + 1.583 + let packet = { 1.584 + to: aTraceActor, 1.585 + type: "attach" 1.586 + }; 1.587 + this.request(packet, (aResponse) => { 1.588 + if (!aResponse.error) { 1.589 + var traceClient = new TraceClient(this, aTraceActor); 1.590 + this._tracerClients.set(aTraceActor, traceClient); 1.591 + } 1.592 + aOnResponse(aResponse, traceClient); 1.593 + }); 1.594 + }, 1.595 + 1.596 + /** 1.597 + * Release an object actor. 1.598 + * 1.599 + * @param string aActor 1.600 + * The actor ID to send the request to. 1.601 + * @param aOnResponse function 1.602 + * If specified, will be called with the response packet when 1.603 + * debugging server responds. 1.604 + */ 1.605 + release: DebuggerClient.requester({ 1.606 + to: args(0), 1.607 + type: "release" 1.608 + }, { 1.609 + telemetry: "RELEASE" 1.610 + }), 1.611 + 1.612 + /** 1.613 + * Send a request to the debugging server. 1.614 + * 1.615 + * @param aRequest object 1.616 + * A JSON packet to send to the debugging server. 1.617 + * @param aOnResponse function 1.618 + * If specified, will be called with the response packet when 1.619 + * debugging server responds. 1.620 + */ 1.621 + request: function (aRequest, aOnResponse) { 1.622 + if (!this.mainRoot) { 1.623 + throw Error("Have not yet received a hello packet from the server."); 1.624 + } 1.625 + if (!aRequest.to) { 1.626 + let type = aRequest.type || ""; 1.627 + throw Error("'" + type + "' request packet has no destination."); 1.628 + } 1.629 + 1.630 + this._pendingRequests.push({ to: aRequest.to, 1.631 + request: aRequest, 1.632 + onResponse: aOnResponse }); 1.633 + this._sendRequests(); 1.634 + }, 1.635 + 1.636 + /** 1.637 + * Send pending requests to any actors that don't already have an 1.638 + * active request. 1.639 + */ 1.640 + _sendRequests: function () { 1.641 + this._pendingRequests = this._pendingRequests.filter((request) => { 1.642 + if (this._activeRequests.has(request.to)) { 1.643 + return true; 1.644 + } 1.645 + 1.646 + this.expectReply(request.to, request.onResponse); 1.647 + this._transport.send(request.request); 1.648 + 1.649 + return false; 1.650 + }); 1.651 + }, 1.652 + 1.653 + /** 1.654 + * Arrange to hand the next reply from |aActor| to |aHandler|. 1.655 + * 1.656 + * DebuggerClient.prototype.request usually takes care of establishing 1.657 + * the handler for a given request, but in rare cases (well, greetings 1.658 + * from new root actors, is the only case at the moment) we must be 1.659 + * prepared for a "reply" that doesn't correspond to any request we sent. 1.660 + */ 1.661 + expectReply: function (aActor, aHandler) { 1.662 + if (this._activeRequests.has(aActor)) { 1.663 + throw Error("clashing handlers for next reply from " + uneval(aActor)); 1.664 + } 1.665 + this._activeRequests.set(aActor, aHandler); 1.666 + }, 1.667 + 1.668 + // Transport hooks. 1.669 + 1.670 + /** 1.671 + * Called by DebuggerTransport to dispatch incoming packets as appropriate. 1.672 + * 1.673 + * @param aPacket object 1.674 + * The incoming packet. 1.675 + * @param aIgnoreCompatibility boolean 1.676 + * Set true to not pass the packet through the compatibility layer. 1.677 + */ 1.678 + onPacket: function (aPacket, aIgnoreCompatibility=false) { 1.679 + let packet = aIgnoreCompatibility 1.680 + ? aPacket 1.681 + : this.compat.onPacket(aPacket); 1.682 + 1.683 + resolve(packet).then(aPacket => { 1.684 + if (!aPacket.from) { 1.685 + DevToolsUtils.reportException( 1.686 + "onPacket", 1.687 + new Error("Server did not specify an actor, dropping packet: " + 1.688 + JSON.stringify(aPacket))); 1.689 + return; 1.690 + } 1.691 + 1.692 + // If we have a registered Front for this actor, let it handle the packet 1.693 + // and skip all the rest of this unpleasantness. 1.694 + let front = this.getActor(aPacket.from); 1.695 + if (front) { 1.696 + front.onPacket(aPacket); 1.697 + return; 1.698 + } 1.699 + 1.700 + let onResponse; 1.701 + // See if we have a handler function waiting for a reply from this 1.702 + // actor. (Don't count unsolicited notifications or pauses as 1.703 + // replies.) 1.704 + if (this._activeRequests.has(aPacket.from) && 1.705 + !(aPacket.type in UnsolicitedNotifications) && 1.706 + !(aPacket.type == ThreadStateTypes.paused && 1.707 + aPacket.why.type in UnsolicitedPauses)) { 1.708 + onResponse = this._activeRequests.get(aPacket.from); 1.709 + this._activeRequests.delete(aPacket.from); 1.710 + } 1.711 + 1.712 + // Packets that indicate thread state changes get special treatment. 1.713 + if (aPacket.type in ThreadStateTypes && 1.714 + this._threadClients.has(aPacket.from)) { 1.715 + this._threadClients.get(aPacket.from)._onThreadState(aPacket); 1.716 + } 1.717 + // On navigation the server resumes, so the client must resume as well. 1.718 + // We achieve that by generating a fake resumption packet that triggers 1.719 + // the client's thread state change listeners. 1.720 + if (aPacket.type == UnsolicitedNotifications.tabNavigated && 1.721 + this._tabClients.has(aPacket.from) && 1.722 + this._tabClients.get(aPacket.from).thread) { 1.723 + let thread = this._tabClients.get(aPacket.from).thread; 1.724 + let resumption = { from: thread._actor, type: "resumed" }; 1.725 + thread._onThreadState(resumption); 1.726 + } 1.727 + // Only try to notify listeners on events, not responses to requests 1.728 + // that lack a packet type. 1.729 + if (aPacket.type) { 1.730 + this.notify(aPacket.type, aPacket); 1.731 + } 1.732 + 1.733 + if (onResponse) { 1.734 + onResponse(aPacket); 1.735 + } 1.736 + 1.737 + this._sendRequests(); 1.738 + }, ex => DevToolsUtils.reportException("onPacket handler", ex)); 1.739 + }, 1.740 + 1.741 + /** 1.742 + * Called by DebuggerTransport when the underlying stream is closed. 1.743 + * 1.744 + * @param aStatus nsresult 1.745 + * The status code that corresponds to the reason for closing 1.746 + * the stream. 1.747 + */ 1.748 + onClosed: function (aStatus) { 1.749 + this.notify("closed"); 1.750 + }, 1.751 + 1.752 + /** 1.753 + * Actor lifetime management, echos the server's actor pools. 1.754 + */ 1.755 + __pools: null, 1.756 + get _pools() { 1.757 + if (this.__pools) { 1.758 + return this.__pools; 1.759 + } 1.760 + this.__pools = new Set(); 1.761 + return this.__pools; 1.762 + }, 1.763 + 1.764 + addActorPool: function (pool) { 1.765 + this._pools.add(pool); 1.766 + }, 1.767 + removeActorPool: function (pool) { 1.768 + this._pools.delete(pool); 1.769 + }, 1.770 + getActor: function (actorID) { 1.771 + let pool = this.poolFor(actorID); 1.772 + return pool ? pool.get(actorID) : null; 1.773 + }, 1.774 + 1.775 + poolFor: function (actorID) { 1.776 + for (let pool of this._pools) { 1.777 + if (pool.has(actorID)) return pool; 1.778 + } 1.779 + return null; 1.780 + }, 1.781 + 1.782 + /** 1.783 + * Currently attached addon. 1.784 + */ 1.785 + activeAddon: null 1.786 +} 1.787 + 1.788 +eventSource(DebuggerClient.prototype); 1.789 + 1.790 +// Constants returned by `FeatureCompatibilityShim.onPacketTest`. 1.791 +const SUPPORTED = 1; 1.792 +const NOT_SUPPORTED = 2; 1.793 +const SKIP = 3; 1.794 + 1.795 +/** 1.796 + * This object provides an abstraction layer over all of our backwards 1.797 + * compatibility, feature detection, and shimming with regards to the remote 1.798 + * debugging prototcol. 1.799 + * 1.800 + * @param aFeatures Array 1.801 + * An array of FeatureCompatibilityShim objects 1.802 + */ 1.803 +function ProtocolCompatibility(aClient, aFeatures) { 1.804 + this._client = aClient; 1.805 + this._featuresWithUnknownSupport = new Set(aFeatures); 1.806 + this._featuresWithoutSupport = new Set(); 1.807 + 1.808 + this._featureDeferreds = Object.create(null) 1.809 + for (let f of aFeatures) { 1.810 + this._featureDeferreds[f.name] = defer(); 1.811 + } 1.812 +} 1.813 + 1.814 +ProtocolCompatibility.prototype = { 1.815 + /** 1.816 + * Returns a promise that resolves to true if the RDP supports the feature, 1.817 + * and is rejected otherwise. 1.818 + * 1.819 + * @param aFeatureName String 1.820 + * The name of the feature we are testing. 1.821 + */ 1.822 + supportsFeature: function (aFeatureName) { 1.823 + return this._featureDeferreds[aFeatureName].promise; 1.824 + }, 1.825 + 1.826 + /** 1.827 + * Force a feature to be considered unsupported. 1.828 + * 1.829 + * @param aFeatureName String 1.830 + * The name of the feature we are testing. 1.831 + */ 1.832 + rejectFeature: function (aFeatureName) { 1.833 + this._featureDeferreds[aFeatureName].reject(false); 1.834 + }, 1.835 + 1.836 + /** 1.837 + * Called for each packet received over the RDP from the server. Tests for 1.838 + * protocol features and shims packets to support needed features. 1.839 + * 1.840 + * @param aPacket Object 1.841 + * Packet received over the RDP from the server. 1.842 + */ 1.843 + onPacket: function (aPacket) { 1.844 + this._detectFeatures(aPacket); 1.845 + return this._shimPacket(aPacket); 1.846 + }, 1.847 + 1.848 + /** 1.849 + * For each of the features we don't know whether the server supports or not, 1.850 + * attempt to detect support based on the packet we just received. 1.851 + */ 1.852 + _detectFeatures: function (aPacket) { 1.853 + for (let feature of this._featuresWithUnknownSupport) { 1.854 + try { 1.855 + switch (feature.onPacketTest(aPacket)) { 1.856 + case SKIP: 1.857 + break; 1.858 + case SUPPORTED: 1.859 + this._featuresWithUnknownSupport.delete(feature); 1.860 + this._featureDeferreds[feature.name].resolve(true); 1.861 + break; 1.862 + case NOT_SUPPORTED: 1.863 + this._featuresWithUnknownSupport.delete(feature); 1.864 + this._featuresWithoutSupport.add(feature); 1.865 + this.rejectFeature(feature.name); 1.866 + break; 1.867 + default: 1.868 + DevToolsUtils.reportException( 1.869 + "PC__detectFeatures", 1.870 + new Error("Bad return value from `onPacketTest` for feature '" 1.871 + + feature.name + "'")); 1.872 + } 1.873 + } catch (ex) { 1.874 + DevToolsUtils.reportException("PC__detectFeatures", ex); 1.875 + } 1.876 + } 1.877 + }, 1.878 + 1.879 + /** 1.880 + * Go through each of the features that we know are unsupported by the current 1.881 + * server and attempt to shim support. 1.882 + */ 1.883 + _shimPacket: function (aPacket) { 1.884 + let extraPackets = []; 1.885 + 1.886 + let loop = function (aFeatures, aPacket) { 1.887 + if (aFeatures.length === 0) { 1.888 + for (let packet of extraPackets) { 1.889 + this._client.onPacket(packet, true); 1.890 + } 1.891 + return aPacket; 1.892 + } else { 1.893 + let replacePacket = function (aNewPacket) { 1.894 + return aNewPacket; 1.895 + }; 1.896 + let extraPacket = function (aExtraPacket) { 1.897 + extraPackets.push(aExtraPacket); 1.898 + return aPacket; 1.899 + }; 1.900 + let keepPacket = function () { 1.901 + return aPacket; 1.902 + }; 1.903 + let newPacket = aFeatures[0].translatePacket(aPacket, 1.904 + replacePacket, 1.905 + extraPacket, 1.906 + keepPacket); 1.907 + return resolve(newPacket).then(loop.bind(null, aFeatures.slice(1))); 1.908 + } 1.909 + }.bind(this); 1.910 + 1.911 + return loop([f for (f of this._featuresWithoutSupport)], 1.912 + aPacket); 1.913 + } 1.914 +}; 1.915 + 1.916 +/** 1.917 + * Interface defining what methods a feature compatibility shim should have. 1.918 + */ 1.919 +const FeatureCompatibilityShim = { 1.920 + // The name of the feature 1.921 + name: null, 1.922 + 1.923 + /** 1.924 + * Takes a packet and returns boolean (or promise of boolean) indicating 1.925 + * whether the server supports the RDP feature we are possibly shimming. 1.926 + */ 1.927 + onPacketTest: function (aPacket) { 1.928 + throw new Error("Not yet implemented"); 1.929 + }, 1.930 + 1.931 + /** 1.932 + * Takes a packet actually sent from the server and decides whether to replace 1.933 + * it with a new packet, create an extra packet, or keep it. 1.934 + */ 1.935 + translatePacket: function (aPacket, aReplacePacket, aExtraPacket, aKeepPacket) { 1.936 + throw new Error("Not yet implemented"); 1.937 + } 1.938 +}; 1.939 + 1.940 +/** 1.941 + * Creates a tab client for the remote debugging protocol server. This client 1.942 + * is a front to the tab actor created in the server side, hiding the protocol 1.943 + * details in a traditional JavaScript API. 1.944 + * 1.945 + * @param aClient DebuggerClient 1.946 + * The debugger client parent. 1.947 + * @param aForm object 1.948 + * The protocol form for this tab. 1.949 + */ 1.950 +function TabClient(aClient, aForm) { 1.951 + this.client = aClient; 1.952 + this._actor = aForm.from; 1.953 + this._threadActor = aForm.threadActor; 1.954 + this.javascriptEnabled = aForm.javascriptEnabled; 1.955 + this.cacheEnabled = aForm.cacheEnabled; 1.956 + this.thread = null; 1.957 + this.request = this.client.request; 1.958 + this.traits = aForm.traits || {}; 1.959 +} 1.960 + 1.961 +TabClient.prototype = { 1.962 + get actor() { return this._actor }, 1.963 + get _transport() { return this.client._transport; }, 1.964 + 1.965 + /** 1.966 + * Attach to a thread actor. 1.967 + * 1.968 + * @param object aOptions 1.969 + * Configuration options. 1.970 + * - useSourceMaps: whether to use source maps or not. 1.971 + * @param function aOnResponse 1.972 + * Called with the response packet and a ThreadClient 1.973 + * (which will be undefined on error). 1.974 + */ 1.975 + attachThread: function(aOptions={}, aOnResponse) { 1.976 + if (this.thread) { 1.977 + setTimeout(() => aOnResponse({}, this.thread), 0); 1.978 + return; 1.979 + } 1.980 + 1.981 + let packet = { 1.982 + to: this._threadActor, 1.983 + type: "attach", 1.984 + options: aOptions 1.985 + }; 1.986 + this.request(packet, (aResponse) => { 1.987 + if (!aResponse.error) { 1.988 + this.thread = new ThreadClient(this, this._threadActor); 1.989 + this.client._threadClients.set(this._threadActor, this.thread); 1.990 + } 1.991 + aOnResponse(aResponse, this.thread); 1.992 + }); 1.993 + }, 1.994 + 1.995 + /** 1.996 + * Detach the client from the tab actor. 1.997 + * 1.998 + * @param function aOnResponse 1.999 + * Called with the response packet. 1.1000 + */ 1.1001 + detach: DebuggerClient.requester({ 1.1002 + type: "detach" 1.1003 + }, { 1.1004 + before: function (aPacket) { 1.1005 + if (this.thread) { 1.1006 + this.thread.detach(); 1.1007 + } 1.1008 + return aPacket; 1.1009 + }, 1.1010 + after: function (aResponse) { 1.1011 + this.client._tabClients.delete(this.actor); 1.1012 + return aResponse; 1.1013 + }, 1.1014 + telemetry: "TABDETACH" 1.1015 + }), 1.1016 + 1.1017 + /** 1.1018 + * Reload the page in this tab. 1.1019 + */ 1.1020 + reload: DebuggerClient.requester({ 1.1021 + type: "reload" 1.1022 + }, { 1.1023 + telemetry: "RELOAD" 1.1024 + }), 1.1025 + 1.1026 + /** 1.1027 + * Navigate to another URL. 1.1028 + * 1.1029 + * @param string url 1.1030 + * The URL to navigate to. 1.1031 + */ 1.1032 + navigateTo: DebuggerClient.requester({ 1.1033 + type: "navigateTo", 1.1034 + url: args(0) 1.1035 + }, { 1.1036 + telemetry: "NAVIGATETO" 1.1037 + }), 1.1038 + 1.1039 + /** 1.1040 + * Reconfigure the tab actor. 1.1041 + * 1.1042 + * @param object aOptions 1.1043 + * A dictionary object of the new options to use in the tab actor. 1.1044 + * @param function aOnResponse 1.1045 + * Called with the response packet. 1.1046 + */ 1.1047 + reconfigure: DebuggerClient.requester({ 1.1048 + type: "reconfigure", 1.1049 + options: args(0) 1.1050 + }, { 1.1051 + telemetry: "RECONFIGURETAB" 1.1052 + }), 1.1053 +}; 1.1054 + 1.1055 +eventSource(TabClient.prototype); 1.1056 + 1.1057 +function AddonClient(aClient, aActor) { 1.1058 + this._client = aClient; 1.1059 + this._actor = aActor; 1.1060 + this.request = this._client.request; 1.1061 +} 1.1062 + 1.1063 +AddonClient.prototype = { 1.1064 + get actor() { return this._actor; }, 1.1065 + get _transport() { return this._client._transport; }, 1.1066 + 1.1067 + /** 1.1068 + * Detach the client from the addon actor. 1.1069 + * 1.1070 + * @param function aOnResponse 1.1071 + * Called with the response packet. 1.1072 + */ 1.1073 + detach: DebuggerClient.requester({ 1.1074 + type: "detach" 1.1075 + }, { 1.1076 + after: function(aResponse) { 1.1077 + if (this._client.activeAddon === this._client._addonClients[this.actor]) { 1.1078 + this._client.activeAddon = null 1.1079 + } 1.1080 + delete this._client._addonClients[this.actor]; 1.1081 + return aResponse; 1.1082 + }, 1.1083 + telemetry: "ADDONDETACH" 1.1084 + }) 1.1085 +}; 1.1086 + 1.1087 +/** 1.1088 + * A RootClient object represents a root actor on the server. Each 1.1089 + * DebuggerClient keeps a RootClient instance representing the root actor 1.1090 + * for the initial connection; DebuggerClient's 'listTabs' and 1.1091 + * 'listChildProcesses' methods forward to that root actor. 1.1092 + * 1.1093 + * @param aClient object 1.1094 + * The client connection to which this actor belongs. 1.1095 + * @param aGreeting string 1.1096 + * The greeting packet from the root actor we're to represent. 1.1097 + * 1.1098 + * Properties of a RootClient instance: 1.1099 + * 1.1100 + * @property actor string 1.1101 + * The name of this child's root actor. 1.1102 + * @property applicationType string 1.1103 + * The application type, as given in the root actor's greeting packet. 1.1104 + * @property traits object 1.1105 + * The traits object, as given in the root actor's greeting packet. 1.1106 + */ 1.1107 +function RootClient(aClient, aGreeting) { 1.1108 + this._client = aClient; 1.1109 + this.actor = aGreeting.from; 1.1110 + this.applicationType = aGreeting.applicationType; 1.1111 + this.traits = aGreeting.traits; 1.1112 +} 1.1113 + 1.1114 +RootClient.prototype = { 1.1115 + constructor: RootClient, 1.1116 + 1.1117 + /** 1.1118 + * List the open tabs. 1.1119 + * 1.1120 + * @param function aOnResponse 1.1121 + * Called with the response packet. 1.1122 + */ 1.1123 + listTabs: DebuggerClient.requester({ type: "listTabs" }, 1.1124 + { telemetry: "LISTTABS" }), 1.1125 + 1.1126 + /** 1.1127 + * List the installed addons. 1.1128 + * 1.1129 + * @param function aOnResponse 1.1130 + * Called with the response packet. 1.1131 + */ 1.1132 + listAddons: DebuggerClient.requester({ type: "listAddons" }, 1.1133 + { telemetry: "LISTADDONS" }), 1.1134 + 1.1135 + /* 1.1136 + * Methods constructed by DebuggerClient.requester require these forwards 1.1137 + * on their 'this'. 1.1138 + */ 1.1139 + get _transport() { return this._client._transport; }, 1.1140 + get request() { return this._client.request; } 1.1141 +}; 1.1142 + 1.1143 +/** 1.1144 + * Creates a thread client for the remote debugging protocol server. This client 1.1145 + * is a front to the thread actor created in the server side, hiding the 1.1146 + * protocol details in a traditional JavaScript API. 1.1147 + * 1.1148 + * @param aClient DebuggerClient|TabClient 1.1149 + * The parent of the thread (tab for tab-scoped debuggers, DebuggerClient 1.1150 + * for chrome debuggers). 1.1151 + * @param aActor string 1.1152 + * The actor ID for this thread. 1.1153 + */ 1.1154 +function ThreadClient(aClient, aActor) { 1.1155 + this._parent = aClient; 1.1156 + this.client = aClient instanceof DebuggerClient ? aClient : aClient.client; 1.1157 + this._actor = aActor; 1.1158 + this._frameCache = []; 1.1159 + this._scriptCache = {}; 1.1160 + this._pauseGrips = {}; 1.1161 + this._threadGrips = {}; 1.1162 + this.request = this.client.request; 1.1163 +} 1.1164 + 1.1165 +ThreadClient.prototype = { 1.1166 + _state: "paused", 1.1167 + get state() { return this._state; }, 1.1168 + get paused() { return this._state === "paused"; }, 1.1169 + 1.1170 + _pauseOnExceptions: false, 1.1171 + _ignoreCaughtExceptions: false, 1.1172 + _pauseOnDOMEvents: null, 1.1173 + 1.1174 + _actor: null, 1.1175 + get actor() { return this._actor; }, 1.1176 + 1.1177 + get compat() { return this.client.compat; }, 1.1178 + get _transport() { return this.client._transport; }, 1.1179 + 1.1180 + _assertPaused: function (aCommand) { 1.1181 + if (!this.paused) { 1.1182 + throw Error(aCommand + " command sent while not paused. Currently " + this._state); 1.1183 + } 1.1184 + }, 1.1185 + 1.1186 + /** 1.1187 + * Resume a paused thread. If the optional aLimit parameter is present, then 1.1188 + * the thread will also pause when that limit is reached. 1.1189 + * 1.1190 + * @param [optional] object aLimit 1.1191 + * An object with a type property set to the appropriate limit (next, 1.1192 + * step, or finish) per the remote debugging protocol specification. 1.1193 + * Use null to specify no limit. 1.1194 + * @param function aOnResponse 1.1195 + * Called with the response packet. 1.1196 + */ 1.1197 + _doResume: DebuggerClient.requester({ 1.1198 + type: "resume", 1.1199 + resumeLimit: args(0) 1.1200 + }, { 1.1201 + before: function (aPacket) { 1.1202 + this._assertPaused("resume"); 1.1203 + 1.1204 + // Put the client in a tentative "resuming" state so we can prevent 1.1205 + // further requests that should only be sent in the paused state. 1.1206 + this._state = "resuming"; 1.1207 + 1.1208 + if (this._pauseOnExceptions) { 1.1209 + aPacket.pauseOnExceptions = this._pauseOnExceptions; 1.1210 + } 1.1211 + if (this._ignoreCaughtExceptions) { 1.1212 + aPacket.ignoreCaughtExceptions = this._ignoreCaughtExceptions; 1.1213 + } 1.1214 + if (this._pauseOnDOMEvents) { 1.1215 + aPacket.pauseOnDOMEvents = this._pauseOnDOMEvents; 1.1216 + } 1.1217 + return aPacket; 1.1218 + }, 1.1219 + after: function (aResponse) { 1.1220 + if (aResponse.error) { 1.1221 + // There was an error resuming, back to paused state. 1.1222 + this._state = "paused"; 1.1223 + } 1.1224 + return aResponse; 1.1225 + }, 1.1226 + telemetry: "RESUME" 1.1227 + }), 1.1228 + 1.1229 + /** 1.1230 + * Reconfigure the thread actor. 1.1231 + * 1.1232 + * @param object aOptions 1.1233 + * A dictionary object of the new options to use in the thread actor. 1.1234 + * @param function aOnResponse 1.1235 + * Called with the response packet. 1.1236 + */ 1.1237 + reconfigure: DebuggerClient.requester({ 1.1238 + type: "reconfigure", 1.1239 + options: args(0) 1.1240 + }, { 1.1241 + telemetry: "RECONFIGURETHREAD" 1.1242 + }), 1.1243 + 1.1244 + /** 1.1245 + * Resume a paused thread. 1.1246 + */ 1.1247 + resume: function (aOnResponse) { 1.1248 + this._doResume(null, aOnResponse); 1.1249 + }, 1.1250 + 1.1251 + /** 1.1252 + * Step over a function call. 1.1253 + * 1.1254 + * @param function aOnResponse 1.1255 + * Called with the response packet. 1.1256 + */ 1.1257 + stepOver: function (aOnResponse) { 1.1258 + this._doResume({ type: "next" }, aOnResponse); 1.1259 + }, 1.1260 + 1.1261 + /** 1.1262 + * Step into a function call. 1.1263 + * 1.1264 + * @param function aOnResponse 1.1265 + * Called with the response packet. 1.1266 + */ 1.1267 + stepIn: function (aOnResponse) { 1.1268 + this._doResume({ type: "step" }, aOnResponse); 1.1269 + }, 1.1270 + 1.1271 + /** 1.1272 + * Step out of a function call. 1.1273 + * 1.1274 + * @param function aOnResponse 1.1275 + * Called with the response packet. 1.1276 + */ 1.1277 + stepOut: function (aOnResponse) { 1.1278 + this._doResume({ type: "finish" }, aOnResponse); 1.1279 + }, 1.1280 + 1.1281 + /** 1.1282 + * Interrupt a running thread. 1.1283 + * 1.1284 + * @param function aOnResponse 1.1285 + * Called with the response packet. 1.1286 + */ 1.1287 + interrupt: DebuggerClient.requester({ 1.1288 + type: "interrupt" 1.1289 + }, { 1.1290 + telemetry: "INTERRUPT" 1.1291 + }), 1.1292 + 1.1293 + /** 1.1294 + * Enable or disable pausing when an exception is thrown. 1.1295 + * 1.1296 + * @param boolean aFlag 1.1297 + * Enables pausing if true, disables otherwise. 1.1298 + * @param function aOnResponse 1.1299 + * Called with the response packet. 1.1300 + */ 1.1301 + pauseOnExceptions: function (aPauseOnExceptions, 1.1302 + aIgnoreCaughtExceptions, 1.1303 + aOnResponse) { 1.1304 + this._pauseOnExceptions = aPauseOnExceptions; 1.1305 + this._ignoreCaughtExceptions = aIgnoreCaughtExceptions; 1.1306 + 1.1307 + // If the debuggee is paused, we have to send the flag via a reconfigure 1.1308 + // request. 1.1309 + if (this.paused) { 1.1310 + this.reconfigure({ 1.1311 + pauseOnExceptions: aPauseOnExceptions, 1.1312 + ignoreCaughtExceptions: aIgnoreCaughtExceptions 1.1313 + }, aOnResponse); 1.1314 + return; 1.1315 + } 1.1316 + // Otherwise send the flag using a standard resume request. 1.1317 + this.interrupt(aResponse => { 1.1318 + if (aResponse.error) { 1.1319 + // Can't continue if pausing failed. 1.1320 + aOnResponse(aResponse); 1.1321 + return; 1.1322 + } 1.1323 + this.resume(aOnResponse); 1.1324 + }); 1.1325 + }, 1.1326 + 1.1327 + /** 1.1328 + * Enable pausing when the specified DOM events are triggered. Disabling 1.1329 + * pausing on an event can be realized by calling this method with the updated 1.1330 + * array of events that doesn't contain it. 1.1331 + * 1.1332 + * @param array|string events 1.1333 + * An array of strings, representing the DOM event types to pause on, 1.1334 + * or "*" to pause on all DOM events. Pass an empty array to 1.1335 + * completely disable pausing on DOM events. 1.1336 + * @param function onResponse 1.1337 + * Called with the response packet in a future turn of the event loop. 1.1338 + */ 1.1339 + pauseOnDOMEvents: function (events, onResponse) { 1.1340 + this._pauseOnDOMEvents = events; 1.1341 + // If the debuggee is paused, the value of the array will be communicated in 1.1342 + // the next resumption. Otherwise we have to force a pause in order to send 1.1343 + // the array. 1.1344 + if (this.paused) { 1.1345 + setTimeout(() => onResponse({}), 0); 1.1346 + return; 1.1347 + } 1.1348 + this.interrupt(response => { 1.1349 + // Can't continue if pausing failed. 1.1350 + if (response.error) { 1.1351 + onResponse(response); 1.1352 + return; 1.1353 + } 1.1354 + this.resume(onResponse); 1.1355 + }); 1.1356 + }, 1.1357 + 1.1358 + /** 1.1359 + * Send a clientEvaluate packet to the debuggee. Response 1.1360 + * will be a resume packet. 1.1361 + * 1.1362 + * @param string aFrame 1.1363 + * The actor ID of the frame where the evaluation should take place. 1.1364 + * @param string aExpression 1.1365 + * The expression that will be evaluated in the scope of the frame 1.1366 + * above. 1.1367 + * @param function aOnResponse 1.1368 + * Called with the response packet. 1.1369 + */ 1.1370 + eval: DebuggerClient.requester({ 1.1371 + type: "clientEvaluate", 1.1372 + frame: args(0), 1.1373 + expression: args(1) 1.1374 + }, { 1.1375 + before: function (aPacket) { 1.1376 + this._assertPaused("eval"); 1.1377 + // Put the client in a tentative "resuming" state so we can prevent 1.1378 + // further requests that should only be sent in the paused state. 1.1379 + this._state = "resuming"; 1.1380 + return aPacket; 1.1381 + }, 1.1382 + after: function (aResponse) { 1.1383 + if (aResponse.error) { 1.1384 + // There was an error resuming, back to paused state. 1.1385 + this._state = "paused"; 1.1386 + } 1.1387 + return aResponse; 1.1388 + }, 1.1389 + telemetry: "CLIENTEVALUATE" 1.1390 + }), 1.1391 + 1.1392 + /** 1.1393 + * Detach from the thread actor. 1.1394 + * 1.1395 + * @param function aOnResponse 1.1396 + * Called with the response packet. 1.1397 + */ 1.1398 + detach: DebuggerClient.requester({ 1.1399 + type: "detach" 1.1400 + }, { 1.1401 + after: function (aResponse) { 1.1402 + this.client._threadClients.delete(this.actor); 1.1403 + this._parent.thread = null; 1.1404 + return aResponse; 1.1405 + }, 1.1406 + telemetry: "THREADDETACH" 1.1407 + }), 1.1408 + 1.1409 + /** 1.1410 + * Request to set a breakpoint in the specified location. 1.1411 + * 1.1412 + * @param object aLocation 1.1413 + * The source location object where the breakpoint will be set. 1.1414 + * @param function aOnResponse 1.1415 + * Called with the thread's response. 1.1416 + */ 1.1417 + setBreakpoint: function ({ url, line, column, condition }, aOnResponse) { 1.1418 + // A helper function that sets the breakpoint. 1.1419 + let doSetBreakpoint = function (aCallback) { 1.1420 + const location = { 1.1421 + url: url, 1.1422 + line: line, 1.1423 + column: column 1.1424 + }; 1.1425 + 1.1426 + let packet = { 1.1427 + to: this._actor, 1.1428 + type: "setBreakpoint", 1.1429 + location: location, 1.1430 + condition: condition 1.1431 + }; 1.1432 + this.client.request(packet, function (aResponse) { 1.1433 + // Ignoring errors, since the user may be setting a breakpoint in a 1.1434 + // dead script that will reappear on a page reload. 1.1435 + if (aOnResponse) { 1.1436 + let root = this.client.mainRoot; 1.1437 + let bpClient = new BreakpointClient( 1.1438 + this.client, 1.1439 + aResponse.actor, 1.1440 + location, 1.1441 + root.traits.conditionalBreakpoints ? condition : undefined 1.1442 + ); 1.1443 + aOnResponse(aResponse, bpClient); 1.1444 + } 1.1445 + if (aCallback) { 1.1446 + aCallback(); 1.1447 + } 1.1448 + }.bind(this)); 1.1449 + }.bind(this); 1.1450 + 1.1451 + // If the debuggee is paused, just set the breakpoint. 1.1452 + if (this.paused) { 1.1453 + doSetBreakpoint(); 1.1454 + return; 1.1455 + } 1.1456 + // Otherwise, force a pause in order to set the breakpoint. 1.1457 + this.interrupt(function (aResponse) { 1.1458 + if (aResponse.error) { 1.1459 + // Can't set the breakpoint if pausing failed. 1.1460 + aOnResponse(aResponse); 1.1461 + return; 1.1462 + } 1.1463 + doSetBreakpoint(this.resume.bind(this)); 1.1464 + }.bind(this)); 1.1465 + }, 1.1466 + 1.1467 + /** 1.1468 + * Release multiple thread-lifetime object actors. If any pause-lifetime 1.1469 + * actors are included in the request, a |notReleasable| error will return, 1.1470 + * but all the thread-lifetime ones will have been released. 1.1471 + * 1.1472 + * @param array actors 1.1473 + * An array with actor IDs to release. 1.1474 + */ 1.1475 + releaseMany: DebuggerClient.requester({ 1.1476 + type: "releaseMany", 1.1477 + actors: args(0), 1.1478 + }, { 1.1479 + telemetry: "RELEASEMANY" 1.1480 + }), 1.1481 + 1.1482 + /** 1.1483 + * Promote multiple pause-lifetime object actors to thread-lifetime ones. 1.1484 + * 1.1485 + * @param array actors 1.1486 + * An array with actor IDs to promote. 1.1487 + */ 1.1488 + threadGrips: DebuggerClient.requester({ 1.1489 + type: "threadGrips", 1.1490 + actors: args(0) 1.1491 + }, { 1.1492 + telemetry: "THREADGRIPS" 1.1493 + }), 1.1494 + 1.1495 + /** 1.1496 + * Return the event listeners defined on the page. 1.1497 + * 1.1498 + * @param aOnResponse Function 1.1499 + * Called with the thread's response. 1.1500 + */ 1.1501 + eventListeners: DebuggerClient.requester({ 1.1502 + type: "eventListeners" 1.1503 + }, { 1.1504 + telemetry: "EVENTLISTENERS" 1.1505 + }), 1.1506 + 1.1507 + /** 1.1508 + * Request the loaded sources for the current thread. 1.1509 + * 1.1510 + * @param aOnResponse Function 1.1511 + * Called with the thread's response. 1.1512 + */ 1.1513 + getSources: DebuggerClient.requester({ 1.1514 + type: "sources" 1.1515 + }, { 1.1516 + telemetry: "SOURCES" 1.1517 + }), 1.1518 + 1.1519 + _doInterrupted: function (aAction, aError) { 1.1520 + if (this.paused) { 1.1521 + aAction(); 1.1522 + return; 1.1523 + } 1.1524 + this.interrupt(function (aResponse) { 1.1525 + if (aResponse) { 1.1526 + aError(aResponse); 1.1527 + return; 1.1528 + } 1.1529 + aAction(); 1.1530 + this.resume(); 1.1531 + }.bind(this)); 1.1532 + }, 1.1533 + 1.1534 + /** 1.1535 + * Clear the thread's source script cache. A scriptscleared event 1.1536 + * will be sent. 1.1537 + */ 1.1538 + _clearScripts: function () { 1.1539 + if (Object.keys(this._scriptCache).length > 0) { 1.1540 + this._scriptCache = {} 1.1541 + this.notify("scriptscleared"); 1.1542 + } 1.1543 + }, 1.1544 + 1.1545 + /** 1.1546 + * Request frames from the callstack for the current thread. 1.1547 + * 1.1548 + * @param aStart integer 1.1549 + * The number of the youngest stack frame to return (the youngest 1.1550 + * frame is 0). 1.1551 + * @param aCount integer 1.1552 + * The maximum number of frames to return, or null to return all 1.1553 + * frames. 1.1554 + * @param aOnResponse function 1.1555 + * Called with the thread's response. 1.1556 + */ 1.1557 + getFrames: DebuggerClient.requester({ 1.1558 + type: "frames", 1.1559 + start: args(0), 1.1560 + count: args(1) 1.1561 + }, { 1.1562 + telemetry: "FRAMES" 1.1563 + }), 1.1564 + 1.1565 + /** 1.1566 + * An array of cached frames. Clients can observe the framesadded and 1.1567 + * framescleared event to keep up to date on changes to this cache, 1.1568 + * and can fill it using the fillFrames method. 1.1569 + */ 1.1570 + get cachedFrames() { return this._frameCache; }, 1.1571 + 1.1572 + /** 1.1573 + * true if there are more stack frames available on the server. 1.1574 + */ 1.1575 + get moreFrames() { 1.1576 + return this.paused && (!this._frameCache || this._frameCache.length == 0 1.1577 + || !this._frameCache[this._frameCache.length - 1].oldest); 1.1578 + }, 1.1579 + 1.1580 + /** 1.1581 + * Ensure that at least aTotal stack frames have been loaded in the 1.1582 + * ThreadClient's stack frame cache. A framesadded event will be 1.1583 + * sent when the stack frame cache is updated. 1.1584 + * 1.1585 + * @param aTotal number 1.1586 + * The minimum number of stack frames to be included. 1.1587 + * @param aCallback function 1.1588 + * Optional callback function called when frames have been loaded 1.1589 + * @returns true if a framesadded notification should be expected. 1.1590 + */ 1.1591 + fillFrames: function (aTotal, aCallback=noop) { 1.1592 + this._assertPaused("fillFrames"); 1.1593 + if (this._frameCache.length >= aTotal) { 1.1594 + return false; 1.1595 + } 1.1596 + 1.1597 + let numFrames = this._frameCache.length; 1.1598 + 1.1599 + this.getFrames(numFrames, aTotal - numFrames, (aResponse) => { 1.1600 + if (aResponse.error) { 1.1601 + aCallback(aResponse); 1.1602 + return; 1.1603 + } 1.1604 + 1.1605 + for each (let frame in aResponse.frames) { 1.1606 + this._frameCache[frame.depth] = frame; 1.1607 + } 1.1608 + 1.1609 + // If we got as many frames as we asked for, there might be more 1.1610 + // frames available. 1.1611 + this.notify("framesadded"); 1.1612 + 1.1613 + aCallback(aResponse); 1.1614 + }); 1.1615 + 1.1616 + return true; 1.1617 + }, 1.1618 + 1.1619 + /** 1.1620 + * Clear the thread's stack frame cache. A framescleared event 1.1621 + * will be sent. 1.1622 + */ 1.1623 + _clearFrames: function () { 1.1624 + if (this._frameCache.length > 0) { 1.1625 + this._frameCache = []; 1.1626 + this.notify("framescleared"); 1.1627 + } 1.1628 + }, 1.1629 + 1.1630 + /** 1.1631 + * Return a ObjectClient object for the given object grip. 1.1632 + * 1.1633 + * @param aGrip object 1.1634 + * A pause-lifetime object grip returned by the protocol. 1.1635 + */ 1.1636 + pauseGrip: function (aGrip) { 1.1637 + if (aGrip.actor in this._pauseGrips) { 1.1638 + return this._pauseGrips[aGrip.actor]; 1.1639 + } 1.1640 + 1.1641 + let client = new ObjectClient(this.client, aGrip); 1.1642 + this._pauseGrips[aGrip.actor] = client; 1.1643 + return client; 1.1644 + }, 1.1645 + 1.1646 + /** 1.1647 + * Get or create a long string client, checking the grip client cache if it 1.1648 + * already exists. 1.1649 + * 1.1650 + * @param aGrip Object 1.1651 + * The long string grip returned by the protocol. 1.1652 + * @param aGripCacheName String 1.1653 + * The property name of the grip client cache to check for existing 1.1654 + * clients in. 1.1655 + */ 1.1656 + _longString: function (aGrip, aGripCacheName) { 1.1657 + if (aGrip.actor in this[aGripCacheName]) { 1.1658 + return this[aGripCacheName][aGrip.actor]; 1.1659 + } 1.1660 + 1.1661 + let client = new LongStringClient(this.client, aGrip); 1.1662 + this[aGripCacheName][aGrip.actor] = client; 1.1663 + return client; 1.1664 + }, 1.1665 + 1.1666 + /** 1.1667 + * Return an instance of LongStringClient for the given long string grip that 1.1668 + * is scoped to the current pause. 1.1669 + * 1.1670 + * @param aGrip Object 1.1671 + * The long string grip returned by the protocol. 1.1672 + */ 1.1673 + pauseLongString: function (aGrip) { 1.1674 + return this._longString(aGrip, "_pauseGrips"); 1.1675 + }, 1.1676 + 1.1677 + /** 1.1678 + * Return an instance of LongStringClient for the given long string grip that 1.1679 + * is scoped to the thread lifetime. 1.1680 + * 1.1681 + * @param aGrip Object 1.1682 + * The long string grip returned by the protocol. 1.1683 + */ 1.1684 + threadLongString: function (aGrip) { 1.1685 + return this._longString(aGrip, "_threadGrips"); 1.1686 + }, 1.1687 + 1.1688 + /** 1.1689 + * Clear and invalidate all the grip clients from the given cache. 1.1690 + * 1.1691 + * @param aGripCacheName 1.1692 + * The property name of the grip cache we want to clear. 1.1693 + */ 1.1694 + _clearObjectClients: function (aGripCacheName) { 1.1695 + for each (let grip in this[aGripCacheName]) { 1.1696 + grip.valid = false; 1.1697 + } 1.1698 + this[aGripCacheName] = {}; 1.1699 + }, 1.1700 + 1.1701 + /** 1.1702 + * Invalidate pause-lifetime grip clients and clear the list of current grip 1.1703 + * clients. 1.1704 + */ 1.1705 + _clearPauseGrips: function () { 1.1706 + this._clearObjectClients("_pauseGrips"); 1.1707 + }, 1.1708 + 1.1709 + /** 1.1710 + * Invalidate thread-lifetime grip clients and clear the list of current grip 1.1711 + * clients. 1.1712 + */ 1.1713 + _clearThreadGrips: function () { 1.1714 + this._clearObjectClients("_threadGrips"); 1.1715 + }, 1.1716 + 1.1717 + /** 1.1718 + * Handle thread state change by doing necessary cleanup and notifying all 1.1719 + * registered listeners. 1.1720 + */ 1.1721 + _onThreadState: function (aPacket) { 1.1722 + this._state = ThreadStateTypes[aPacket.type]; 1.1723 + this._clearFrames(); 1.1724 + this._clearPauseGrips(); 1.1725 + aPacket.type === ThreadStateTypes.detached && this._clearThreadGrips(); 1.1726 + this.client._eventsEnabled && this.notify(aPacket.type, aPacket); 1.1727 + }, 1.1728 + 1.1729 + /** 1.1730 + * Return an EnvironmentClient instance for the given environment actor form. 1.1731 + */ 1.1732 + environment: function (aForm) { 1.1733 + return new EnvironmentClient(this.client, aForm); 1.1734 + }, 1.1735 + 1.1736 + /** 1.1737 + * Return an instance of SourceClient for the given source actor form. 1.1738 + */ 1.1739 + source: function (aForm) { 1.1740 + if (aForm.actor in this._threadGrips) { 1.1741 + return this._threadGrips[aForm.actor]; 1.1742 + } 1.1743 + 1.1744 + return this._threadGrips[aForm.actor] = new SourceClient(this, aForm); 1.1745 + }, 1.1746 + 1.1747 + /** 1.1748 + * Request the prototype and own properties of mutlipleObjects. 1.1749 + * 1.1750 + * @param aOnResponse function 1.1751 + * Called with the request's response. 1.1752 + * @param actors [string] 1.1753 + * List of actor ID of the queried objects. 1.1754 + */ 1.1755 + getPrototypesAndProperties: DebuggerClient.requester({ 1.1756 + type: "prototypesAndProperties", 1.1757 + actors: args(0) 1.1758 + }, { 1.1759 + telemetry: "PROTOTYPESANDPROPERTIES" 1.1760 + }) 1.1761 +}; 1.1762 + 1.1763 +eventSource(ThreadClient.prototype); 1.1764 + 1.1765 +/** 1.1766 + * Creates a tracing profiler client for the remote debugging protocol 1.1767 + * server. This client is a front to the trace actor created on the 1.1768 + * server side, hiding the protocol details in a traditional 1.1769 + * JavaScript API. 1.1770 + * 1.1771 + * @param aClient DebuggerClient 1.1772 + * The debugger client parent. 1.1773 + * @param aActor string 1.1774 + * The actor ID for this thread. 1.1775 + */ 1.1776 +function TraceClient(aClient, aActor) { 1.1777 + this._client = aClient; 1.1778 + this._actor = aActor; 1.1779 + this._activeTraces = new Set(); 1.1780 + this._waitingPackets = new Map(); 1.1781 + this._expectedPacket = 0; 1.1782 + this.request = this._client.request; 1.1783 +} 1.1784 + 1.1785 +TraceClient.prototype = { 1.1786 + get actor() { return this._actor; }, 1.1787 + get tracing() { return this._activeTraces.size > 0; }, 1.1788 + 1.1789 + get _transport() { return this._client._transport; }, 1.1790 + 1.1791 + /** 1.1792 + * Detach from the trace actor. 1.1793 + */ 1.1794 + detach: DebuggerClient.requester({ 1.1795 + type: "detach" 1.1796 + }, { 1.1797 + after: function (aResponse) { 1.1798 + this._client._tracerClients.delete(this.actor); 1.1799 + return aResponse; 1.1800 + }, 1.1801 + telemetry: "TRACERDETACH" 1.1802 + }), 1.1803 + 1.1804 + /** 1.1805 + * Start a new trace. 1.1806 + * 1.1807 + * @param aTrace [string] 1.1808 + * An array of trace types to be recorded by the new trace. 1.1809 + * 1.1810 + * @param aName string 1.1811 + * The name of the new trace. 1.1812 + * 1.1813 + * @param aOnResponse function 1.1814 + * Called with the request's response. 1.1815 + */ 1.1816 + startTrace: DebuggerClient.requester({ 1.1817 + type: "startTrace", 1.1818 + name: args(1), 1.1819 + trace: args(0) 1.1820 + }, { 1.1821 + after: function (aResponse) { 1.1822 + if (aResponse.error) { 1.1823 + return aResponse; 1.1824 + } 1.1825 + 1.1826 + if (!this.tracing) { 1.1827 + this._waitingPackets.clear(); 1.1828 + this._expectedPacket = 0; 1.1829 + } 1.1830 + this._activeTraces.add(aResponse.name); 1.1831 + 1.1832 + return aResponse; 1.1833 + }, 1.1834 + telemetry: "STARTTRACE" 1.1835 + }), 1.1836 + 1.1837 + /** 1.1838 + * End a trace. If a name is provided, stop the named 1.1839 + * trace. Otherwise, stop the most recently started trace. 1.1840 + * 1.1841 + * @param aName string 1.1842 + * The name of the trace to stop. 1.1843 + * 1.1844 + * @param aOnResponse function 1.1845 + * Called with the request's response. 1.1846 + */ 1.1847 + stopTrace: DebuggerClient.requester({ 1.1848 + type: "stopTrace", 1.1849 + name: args(0) 1.1850 + }, { 1.1851 + after: function (aResponse) { 1.1852 + if (aResponse.error) { 1.1853 + return aResponse; 1.1854 + } 1.1855 + 1.1856 + this._activeTraces.delete(aResponse.name); 1.1857 + 1.1858 + return aResponse; 1.1859 + }, 1.1860 + telemetry: "STOPTRACE" 1.1861 + }) 1.1862 +}; 1.1863 + 1.1864 +/** 1.1865 + * Grip clients are used to retrieve information about the relevant object. 1.1866 + * 1.1867 + * @param aClient DebuggerClient 1.1868 + * The debugger client parent. 1.1869 + * @param aGrip object 1.1870 + * A pause-lifetime object grip returned by the protocol. 1.1871 + */ 1.1872 +function ObjectClient(aClient, aGrip) 1.1873 +{ 1.1874 + this._grip = aGrip; 1.1875 + this._client = aClient; 1.1876 + this.request = this._client.request; 1.1877 +} 1.1878 + 1.1879 +ObjectClient.prototype = { 1.1880 + get actor() { return this._grip.actor }, 1.1881 + get _transport() { return this._client._transport; }, 1.1882 + 1.1883 + valid: true, 1.1884 + 1.1885 + get isFrozen() this._grip.frozen, 1.1886 + get isSealed() this._grip.sealed, 1.1887 + get isExtensible() this._grip.extensible, 1.1888 + 1.1889 + getDefinitionSite: DebuggerClient.requester({ 1.1890 + type: "definitionSite" 1.1891 + }, { 1.1892 + before: function (aPacket) { 1.1893 + if (this._grip.class != "Function") { 1.1894 + throw new Error("getDefinitionSite is only valid for function grips."); 1.1895 + } 1.1896 + return aPacket; 1.1897 + } 1.1898 + }), 1.1899 + 1.1900 + /** 1.1901 + * Request the names of a function's formal parameters. 1.1902 + * 1.1903 + * @param aOnResponse function 1.1904 + * Called with an object of the form: 1.1905 + * { parameterNames:[<parameterName>, ...] } 1.1906 + * where each <parameterName> is the name of a parameter. 1.1907 + */ 1.1908 + getParameterNames: DebuggerClient.requester({ 1.1909 + type: "parameterNames" 1.1910 + }, { 1.1911 + before: function (aPacket) { 1.1912 + if (this._grip["class"] !== "Function") { 1.1913 + throw new Error("getParameterNames is only valid for function grips."); 1.1914 + } 1.1915 + return aPacket; 1.1916 + }, 1.1917 + telemetry: "PARAMETERNAMES" 1.1918 + }), 1.1919 + 1.1920 + /** 1.1921 + * Request the names of the properties defined on the object and not its 1.1922 + * prototype. 1.1923 + * 1.1924 + * @param aOnResponse function Called with the request's response. 1.1925 + */ 1.1926 + getOwnPropertyNames: DebuggerClient.requester({ 1.1927 + type: "ownPropertyNames" 1.1928 + }, { 1.1929 + telemetry: "OWNPROPERTYNAMES" 1.1930 + }), 1.1931 + 1.1932 + /** 1.1933 + * Request the prototype and own properties of the object. 1.1934 + * 1.1935 + * @param aOnResponse function Called with the request's response. 1.1936 + */ 1.1937 + getPrototypeAndProperties: DebuggerClient.requester({ 1.1938 + type: "prototypeAndProperties" 1.1939 + }, { 1.1940 + telemetry: "PROTOTYPEANDPROPERTIES" 1.1941 + }), 1.1942 + 1.1943 + /** 1.1944 + * Request the property descriptor of the object's specified property. 1.1945 + * 1.1946 + * @param aName string The name of the requested property. 1.1947 + * @param aOnResponse function Called with the request's response. 1.1948 + */ 1.1949 + getProperty: DebuggerClient.requester({ 1.1950 + type: "property", 1.1951 + name: args(0) 1.1952 + }, { 1.1953 + telemetry: "PROPERTY" 1.1954 + }), 1.1955 + 1.1956 + /** 1.1957 + * Request the prototype of the object. 1.1958 + * 1.1959 + * @param aOnResponse function Called with the request's response. 1.1960 + */ 1.1961 + getPrototype: DebuggerClient.requester({ 1.1962 + type: "prototype" 1.1963 + }, { 1.1964 + telemetry: "PROTOTYPE" 1.1965 + }), 1.1966 + 1.1967 + /** 1.1968 + * Request the display string of the object. 1.1969 + * 1.1970 + * @param aOnResponse function Called with the request's response. 1.1971 + */ 1.1972 + getDisplayString: DebuggerClient.requester({ 1.1973 + type: "displayString" 1.1974 + }, { 1.1975 + telemetry: "DISPLAYSTRING" 1.1976 + }), 1.1977 + 1.1978 + /** 1.1979 + * Request the scope of the object. 1.1980 + * 1.1981 + * @param aOnResponse function Called with the request's response. 1.1982 + */ 1.1983 + getScope: DebuggerClient.requester({ 1.1984 + type: "scope" 1.1985 + }, { 1.1986 + before: function (aPacket) { 1.1987 + if (this._grip.class !== "Function") { 1.1988 + throw new Error("scope is only valid for function grips."); 1.1989 + } 1.1990 + return aPacket; 1.1991 + }, 1.1992 + telemetry: "SCOPE" 1.1993 + }) 1.1994 +}; 1.1995 + 1.1996 +/** 1.1997 + * A LongStringClient provides a way to access "very long" strings from the 1.1998 + * debugger server. 1.1999 + * 1.2000 + * @param aClient DebuggerClient 1.2001 + * The debugger client parent. 1.2002 + * @param aGrip Object 1.2003 + * A pause-lifetime long string grip returned by the protocol. 1.2004 + */ 1.2005 +function LongStringClient(aClient, aGrip) { 1.2006 + this._grip = aGrip; 1.2007 + this._client = aClient; 1.2008 + this.request = this._client.request; 1.2009 +} 1.2010 + 1.2011 +LongStringClient.prototype = { 1.2012 + get actor() { return this._grip.actor; }, 1.2013 + get length() { return this._grip.length; }, 1.2014 + get initial() { return this._grip.initial; }, 1.2015 + get _transport() { return this._client._transport; }, 1.2016 + 1.2017 + valid: true, 1.2018 + 1.2019 + /** 1.2020 + * Get the substring of this LongString from aStart to aEnd. 1.2021 + * 1.2022 + * @param aStart Number 1.2023 + * The starting index. 1.2024 + * @param aEnd Number 1.2025 + * The ending index. 1.2026 + * @param aCallback Function 1.2027 + * The function called when we receive the substring. 1.2028 + */ 1.2029 + substring: DebuggerClient.requester({ 1.2030 + type: "substring", 1.2031 + start: args(0), 1.2032 + end: args(1) 1.2033 + }, { 1.2034 + telemetry: "SUBSTRING" 1.2035 + }), 1.2036 +}; 1.2037 + 1.2038 +/** 1.2039 + * A SourceClient provides a way to access the source text of a script. 1.2040 + * 1.2041 + * @param aClient ThreadClient 1.2042 + * The thread client parent. 1.2043 + * @param aForm Object 1.2044 + * The form sent across the remote debugging protocol. 1.2045 + */ 1.2046 +function SourceClient(aClient, aForm) { 1.2047 + this._form = aForm; 1.2048 + this._isBlackBoxed = aForm.isBlackBoxed; 1.2049 + this._isPrettyPrinted = aForm.isPrettyPrinted; 1.2050 + this._activeThread = aClient; 1.2051 + this._client = aClient.client; 1.2052 +} 1.2053 + 1.2054 +SourceClient.prototype = { 1.2055 + get _transport() this._client._transport, 1.2056 + get isBlackBoxed() this._isBlackBoxed, 1.2057 + get isPrettyPrinted() this._isPrettyPrinted, 1.2058 + get actor() this._form.actor, 1.2059 + get request() this._client.request, 1.2060 + get url() this._form.url, 1.2061 + 1.2062 + /** 1.2063 + * Black box this SourceClient's source. 1.2064 + * 1.2065 + * @param aCallback Function 1.2066 + * The callback function called when we receive the response from the server. 1.2067 + */ 1.2068 + blackBox: DebuggerClient.requester({ 1.2069 + type: "blackbox" 1.2070 + }, { 1.2071 + telemetry: "BLACKBOX", 1.2072 + after: function (aResponse) { 1.2073 + if (!aResponse.error) { 1.2074 + this._isBlackBoxed = true; 1.2075 + if (this._activeThread) { 1.2076 + this._activeThread.notify("blackboxchange", this); 1.2077 + } 1.2078 + } 1.2079 + return aResponse; 1.2080 + } 1.2081 + }), 1.2082 + 1.2083 + /** 1.2084 + * Un-black box this SourceClient's source. 1.2085 + * 1.2086 + * @param aCallback Function 1.2087 + * The callback function called when we receive the response from the server. 1.2088 + */ 1.2089 + unblackBox: DebuggerClient.requester({ 1.2090 + type: "unblackbox" 1.2091 + }, { 1.2092 + telemetry: "UNBLACKBOX", 1.2093 + after: function (aResponse) { 1.2094 + if (!aResponse.error) { 1.2095 + this._isBlackBoxed = false; 1.2096 + if (this._activeThread) { 1.2097 + this._activeThread.notify("blackboxchange", this); 1.2098 + } 1.2099 + } 1.2100 + return aResponse; 1.2101 + } 1.2102 + }), 1.2103 + 1.2104 + /** 1.2105 + * Get a long string grip for this SourceClient's source. 1.2106 + */ 1.2107 + source: function (aCallback) { 1.2108 + let packet = { 1.2109 + to: this._form.actor, 1.2110 + type: "source" 1.2111 + }; 1.2112 + this._client.request(packet, aResponse => { 1.2113 + this._onSourceResponse(aResponse, aCallback) 1.2114 + }); 1.2115 + }, 1.2116 + 1.2117 + /** 1.2118 + * Pretty print this source's text. 1.2119 + */ 1.2120 + prettyPrint: function (aIndent, aCallback) { 1.2121 + const packet = { 1.2122 + to: this._form.actor, 1.2123 + type: "prettyPrint", 1.2124 + indent: aIndent 1.2125 + }; 1.2126 + this._client.request(packet, aResponse => { 1.2127 + if (!aResponse.error) { 1.2128 + this._isPrettyPrinted = true; 1.2129 + this._activeThread._clearFrames(); 1.2130 + this._activeThread.notify("prettyprintchange", this); 1.2131 + } 1.2132 + this._onSourceResponse(aResponse, aCallback); 1.2133 + }); 1.2134 + }, 1.2135 + 1.2136 + /** 1.2137 + * Stop pretty printing this source's text. 1.2138 + */ 1.2139 + disablePrettyPrint: function (aCallback) { 1.2140 + const packet = { 1.2141 + to: this._form.actor, 1.2142 + type: "disablePrettyPrint" 1.2143 + }; 1.2144 + this._client.request(packet, aResponse => { 1.2145 + if (!aResponse.error) { 1.2146 + this._isPrettyPrinted = false; 1.2147 + this._activeThread._clearFrames(); 1.2148 + this._activeThread.notify("prettyprintchange", this); 1.2149 + } 1.2150 + this._onSourceResponse(aResponse, aCallback); 1.2151 + }); 1.2152 + }, 1.2153 + 1.2154 + _onSourceResponse: function (aResponse, aCallback) { 1.2155 + if (aResponse.error) { 1.2156 + aCallback(aResponse); 1.2157 + return; 1.2158 + } 1.2159 + 1.2160 + if (typeof aResponse.source === "string") { 1.2161 + aCallback(aResponse); 1.2162 + return; 1.2163 + } 1.2164 + 1.2165 + let { contentType, source } = aResponse; 1.2166 + let longString = this._activeThread.threadLongString(source); 1.2167 + longString.substring(0, longString.length, function (aResponse) { 1.2168 + if (aResponse.error) { 1.2169 + aCallback(aResponse); 1.2170 + return; 1.2171 + } 1.2172 + 1.2173 + aCallback({ 1.2174 + source: aResponse.substring, 1.2175 + contentType: contentType 1.2176 + }); 1.2177 + }); 1.2178 + } 1.2179 +}; 1.2180 + 1.2181 +/** 1.2182 + * Breakpoint clients are used to remove breakpoints that are no longer used. 1.2183 + * 1.2184 + * @param aClient DebuggerClient 1.2185 + * The debugger client parent. 1.2186 + * @param aActor string 1.2187 + * The actor ID for this breakpoint. 1.2188 + * @param aLocation object 1.2189 + * The location of the breakpoint. This is an object with two properties: 1.2190 + * url and line. 1.2191 + * @param aCondition string 1.2192 + * The conditional expression of the breakpoint 1.2193 + */ 1.2194 +function BreakpointClient(aClient, aActor, aLocation, aCondition) { 1.2195 + this._client = aClient; 1.2196 + this._actor = aActor; 1.2197 + this.location = aLocation; 1.2198 + this.request = this._client.request; 1.2199 + 1.2200 + // The condition property should only exist if it's a truthy value 1.2201 + if (aCondition) { 1.2202 + this.condition = aCondition; 1.2203 + } 1.2204 +} 1.2205 + 1.2206 +BreakpointClient.prototype = { 1.2207 + 1.2208 + _actor: null, 1.2209 + get actor() { return this._actor; }, 1.2210 + get _transport() { return this._client._transport; }, 1.2211 + 1.2212 + /** 1.2213 + * Remove the breakpoint from the server. 1.2214 + */ 1.2215 + remove: DebuggerClient.requester({ 1.2216 + type: "delete" 1.2217 + }, { 1.2218 + telemetry: "DELETE" 1.2219 + }), 1.2220 + 1.2221 + /** 1.2222 + * Determines if this breakpoint has a condition 1.2223 + */ 1.2224 + hasCondition: function() { 1.2225 + let root = this._client.mainRoot; 1.2226 + // XXX bug 990137: We will remove support for client-side handling of 1.2227 + // conditional breakpoints 1.2228 + if (root.traits.conditionalBreakpoints) { 1.2229 + return "condition" in this; 1.2230 + } else { 1.2231 + return "conditionalExpression" in this; 1.2232 + } 1.2233 + }, 1.2234 + 1.2235 + /** 1.2236 + * Get the condition of this breakpoint. Currently we have to 1.2237 + * support locally emulated conditional breakpoints until the 1.2238 + * debugger servers are updated (see bug 990137). We used a 1.2239 + * different property when moving it server-side to ensure that we 1.2240 + * are testing the right code. 1.2241 + */ 1.2242 + getCondition: function() { 1.2243 + let root = this._client.mainRoot; 1.2244 + if (root.traits.conditionalBreakpoints) { 1.2245 + return this.condition; 1.2246 + } else { 1.2247 + return this.conditionalExpression; 1.2248 + } 1.2249 + }, 1.2250 + 1.2251 + /** 1.2252 + * Set the condition of this breakpoint 1.2253 + */ 1.2254 + setCondition: function(gThreadClient, aCondition) { 1.2255 + let root = this._client.mainRoot; 1.2256 + let deferred = promise.defer(); 1.2257 + 1.2258 + if (root.traits.conditionalBreakpoints) { 1.2259 + let info = { 1.2260 + url: this.location.url, 1.2261 + line: this.location.line, 1.2262 + column: this.location.column, 1.2263 + condition: aCondition 1.2264 + }; 1.2265 + 1.2266 + // Remove the current breakpoint and add a new one with the 1.2267 + // condition. 1.2268 + this.remove(aResponse => { 1.2269 + if (aResponse && aResponse.error) { 1.2270 + deferred.reject(aResponse); 1.2271 + return; 1.2272 + } 1.2273 + 1.2274 + gThreadClient.setBreakpoint(info, (aResponse, aNewBreakpoint) => { 1.2275 + if (aResponse && aResponse.error) { 1.2276 + deferred.reject(aResponse); 1.2277 + } else { 1.2278 + deferred.resolve(aNewBreakpoint); 1.2279 + } 1.2280 + }); 1.2281 + }); 1.2282 + } else { 1.2283 + // The property shouldn't even exist if the condition is blank 1.2284 + if(aCondition === "") { 1.2285 + delete this.conditionalExpression; 1.2286 + } 1.2287 + else { 1.2288 + this.conditionalExpression = aCondition; 1.2289 + } 1.2290 + deferred.resolve(this); 1.2291 + } 1.2292 + 1.2293 + return deferred.promise; 1.2294 + } 1.2295 +}; 1.2296 + 1.2297 +eventSource(BreakpointClient.prototype); 1.2298 + 1.2299 +/** 1.2300 + * Environment clients are used to manipulate the lexical environment actors. 1.2301 + * 1.2302 + * @param aClient DebuggerClient 1.2303 + * The debugger client parent. 1.2304 + * @param aForm Object 1.2305 + * The form sent across the remote debugging protocol. 1.2306 + */ 1.2307 +function EnvironmentClient(aClient, aForm) { 1.2308 + this._client = aClient; 1.2309 + this._form = aForm; 1.2310 + this.request = this._client.request; 1.2311 +} 1.2312 + 1.2313 +EnvironmentClient.prototype = { 1.2314 + 1.2315 + get actor() this._form.actor, 1.2316 + get _transport() { return this._client._transport; }, 1.2317 + 1.2318 + /** 1.2319 + * Fetches the bindings introduced by this lexical environment. 1.2320 + */ 1.2321 + getBindings: DebuggerClient.requester({ 1.2322 + type: "bindings" 1.2323 + }, { 1.2324 + telemetry: "BINDINGS" 1.2325 + }), 1.2326 + 1.2327 + /** 1.2328 + * Changes the value of the identifier whose name is name (a string) to that 1.2329 + * represented by value (a grip). 1.2330 + */ 1.2331 + assign: DebuggerClient.requester({ 1.2332 + type: "assign", 1.2333 + name: args(0), 1.2334 + value: args(1) 1.2335 + }, { 1.2336 + telemetry: "ASSIGN" 1.2337 + }) 1.2338 +}; 1.2339 + 1.2340 +eventSource(EnvironmentClient.prototype); 1.2341 + 1.2342 +/** 1.2343 + * Connects to a debugger server socket and returns a DebuggerTransport. 1.2344 + * 1.2345 + * @param aHost string 1.2346 + * The host name or IP address of the debugger server. 1.2347 + * @param aPort number 1.2348 + * The port number of the debugger server. 1.2349 + */ 1.2350 +this.debuggerSocketConnect = function (aHost, aPort) 1.2351 +{ 1.2352 + let s = socketTransportService.createTransport(null, 0, aHost, aPort, null); 1.2353 + // By default the CONNECT socket timeout is very long, 65535 seconds, 1.2354 + // so that if we race to be in CONNECT state while the server socket is still 1.2355 + // initializing, the connection is stuck in connecting state for 18.20 hours! 1.2356 + s.setTimeout(Ci.nsISocketTransport.TIMEOUT_CONNECT, 2); 1.2357 + 1.2358 + // openOutputStream may throw NS_ERROR_NOT_INITIALIZED if we hit some race 1.2359 + // where the nsISocketTransport gets shutdown in between its instantiation and 1.2360 + // the call to this method. 1.2361 + let transport; 1.2362 + try { 1.2363 + transport = new DebuggerTransport(s.openInputStream(0, 0, 0), 1.2364 + s.openOutputStream(0, 0, 0)); 1.2365 + } catch(e) { 1.2366 + DevToolsUtils.reportException("debuggerSocketConnect", e); 1.2367 + throw e; 1.2368 + } 1.2369 + return transport; 1.2370 +} 1.2371 + 1.2372 +/** 1.2373 + * Takes a pair of items and returns them as an array. 1.2374 + */ 1.2375 +function pair(aItemOne, aItemTwo) { 1.2376 + return [aItemOne, aItemTwo]; 1.2377 +} 1.2378 +function noop() {}