Files
palemoon27/toolkit/devtools/webconsole/client.js
T
roytam1 b9843e0358 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1167459 - Skip rendering function name nodes if there's no name available (e.g. for C++ pseudoframes), r=jsantell (4a69ed224)
- missing bits Bug 1102219 - Part 4: Replace String.prototype.contains with `String.prototype.includes` in chrome code. r=till (73cd2d2b1)
- Bug 1165045 - Don't create nodes with empty text in the call tree, r=jsantell (b013aa82d)
- Bug 1166122 - Fix regression in the call tree caused by bug 1165045, r=jsantell (ee3f16901)
- Bug 1167975 - CallView._displaySelf sets this.document just because other functions use it; it should pass it as an argument instead, r=jsantell (5ef560c4f)
- Bug 1122662 - Resize graphs when window resizes;r=vporof (25c108e4e)
- Bug 1164784 - Eliminate CSS duplication with perf tool record button r=jsantell (43c9bb999)
- Bug 1150761 - Rename the performance tool's details view names to better describe the data visualizations. r=vp (04ceb6a37)
- Bug 1144424 - Rename '{self,total} allocations' to '{self,total} sampled allocations' in the performance tool. r=jsantell (ae79ad54f)
- Bug 1069910 - Add tooltips explaining what each column in the profiler's tree view represents; r=jsantell (8756f88b6)
- Bug 1107849 - Define a min/max width for the performance panel sidebar. r=vporof (e1769e831)
- Bug 11663354 - A locked recording button should appear disabled in the performance tool. r=vp (4a359d39e)
- Bug 1023546 - DevTools - Support HDPI resolutions for Windows. r=bgrins (ef1a3ecb8)
- Bug 1168125 - Cleanup performance xul and css, r=jsantell (8ec794e46)
- Bug 1168125 - Replace the waterfall view with a tree, r=jsantell (ea76514fe)
- Bug 1168125 - Add marker folding logic, r=jsantell (1d3748d2a)
- Bug 862341 Part 1: Move the network request storage from the console frontend to the console client so the netmonitor can reuse it. r=vporof (d29fb2b73)
- remove gre from resource path (126b00df1)
- Bug 943306 - Allow persisting console input history between sessions;r=past (146ebb486)
- Bug 1134845 - Add clearHistory jsterm helper to remove persisted console input history. r=past (22237e95b)
- Bug 1143497 - Offer a way to extend WebConsole commands. r=bgrins (84e2d2957)
- Bug 1125205 - Display console API messages from shared or service workers to the web console, r=past (b4b701a2c)
- Bug 1169342 - Remove nsIDOMDeviceStorage. Cleanup nsDOMDeviceStorage event wrappers. r=dhylands (41338e16f)
- Bug 1151610 - Manage the case where two extensions fight over the same command. r=bgrins (63f9d2064)
- Bug 862341 Part 2: Display cached network requests in the web console. r=vporof (83c0e7263)
- Bug 1144211 - Improve code coverage of camera mochitests. r=mikeh (ba9f3de89)
- Bug 1152500 - Fix how stop recording may be handled out-of-order. r=dhylands (d8bdd379c)
- Bug 862341 Part 3: Display cached network requests in the network panel. r=vporof (a1a6f151d)
- Bug 862341 Part 4: Start recording network requests when the toolbox opens. r=vporof (7a2bdf847)
- Bug 1151499 - Correct the FM playable state. r=baku (8af26fff2)
- Bug 1180347 - Split media.useAudioChannelService to support turning the service on without turning the Firefox OS specific APIs on; r=baku (3fa29291a)
- Bug 862341 Part 5: Tests. r=vporof (82fb944c6)
2021-05-29 22:53:37 +08:00

619 lines
18 KiB
JavaScript

/* -*- js-indent-level: 2; indent-tabs-mode: nil -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const {Cc, Ci, Cu} = require("chrome");
const DevToolsUtils = require("devtools/toolkit/DevToolsUtils");
const EventEmitter = require("devtools/toolkit/event-emitter");
loader.lazyImporter(this, "LongStringClient", "resource://gre/modules/devtools/dbg-client.jsm");
/**
* A WebConsoleClient is used as a front end for the WebConsoleActor that is
* created on the server, hiding implementation details.
*
* @param object aDebuggerClient
* The DebuggerClient instance we live for.
* @param object aResponse
* The response packet received from the "startListeners" request sent to
* the WebConsoleActor.
*/
function WebConsoleClient(aDebuggerClient, aResponse)
{
this._actor = aResponse.from;
this._client = aDebuggerClient;
this._longStrings = {};
this.traits = aResponse.traits || {};
this.events = [];
this._networkRequests = new Map();
this.pendingEvaluationResults = new Map();
this.onEvaluationResult = this.onEvaluationResult.bind(this);
this.onNetworkEvent = this._onNetworkEvent.bind(this);
this.onNetworkEventUpdate = this._onNetworkEventUpdate.bind(this);
this._client.addListener("evaluationResult", this.onEvaluationResult);
this._client.addListener("networkEvent", this.onNetworkEvent);
this._client.addListener("networkEventUpdate", this.onNetworkEventUpdate);
EventEmitter.decorate(this);
}
exports.WebConsoleClient = WebConsoleClient;
WebConsoleClient.prototype = {
_longStrings: null,
traits: null,
/**
* Holds the network requests currently displayed by the Web Console. Each key
* represents the connection ID and the value is network request information.
* @private
* @type object
*/
_networkRequests: null,
getNetworkRequest(actorId) {
return this._networkRequests.get(actorId);
},
hasNetworkRequest(actorId) {
return this._networkRequests.has(actorId);
},
removeNetworkRequest(actorId) {
this._networkRequests.delete(actorId);
},
getNetworkEvents() {
return this._networkRequests.values();
},
get actor() { return this._actor; },
/**
* The "networkEvent" message type handler. We redirect any message to
* the UI for displaying.
*
* @private
* @param string type
* Message type.
* @param object packet
* The message received from the server.
*/
_onNetworkEvent: function (type, packet)
{
if (packet.from == this._actor) {
let actor = packet.eventActor;
let networkInfo = {
_type: "NetworkEvent",
timeStamp: actor.timeStamp,
node: null,
actor: actor.actor,
discardRequestBody: true,
discardResponseBody: true,
startedDateTime: actor.startedDateTime,
request: {
url: actor.url,
method: actor.method,
},
isXHR: actor.isXHR,
response: {},
timings: {},
updates: [], // track the list of network event updates
private: actor.private,
fromCache: actor.fromCache
};
this._networkRequests.set(actor.actor, networkInfo);
this.emit("networkEvent", networkInfo);
}
},
/**
* The "networkEventUpdate" message type handler. We redirect any message to
* the UI for displaying.
*
* @private
* @param string type
* Message type.
* @param object packet
* The message received from the server.
*/
_onNetworkEventUpdate: function (type, packet)
{
let networkInfo = this.getNetworkRequest(packet.from);
if (!networkInfo) {
return;
}
networkInfo.updates.push(packet.updateType);
switch (packet.updateType) {
case "requestHeaders":
networkInfo.request.headersSize = packet.headersSize;
break;
case "requestPostData":
networkInfo.discardRequestBody = packet.discardRequestBody;
networkInfo.request.bodySize = packet.dataSize;
break;
case "responseStart":
networkInfo.response.httpVersion = packet.response.httpVersion;
networkInfo.response.status = packet.response.status;
networkInfo.response.statusText = packet.response.statusText;
networkInfo.response.headersSize = packet.response.headersSize;
networkInfo.response.remoteAddress = packet.response.remoteAddress;
networkInfo.response.remotePort = packet.response.remotePort;
networkInfo.discardResponseBody = packet.response.discardResponseBody;
break;
case "responseContent":
networkInfo.response.content = {
mimeType: packet.mimeType,
};
networkInfo.response.bodySize = packet.contentSize;
networkInfo.response.transferredSize = packet.transferredSize;
networkInfo.discardResponseBody = packet.discardResponseBody;
break;
case "eventTimings":
networkInfo.totalTime = packet.totalTime;
break;
case "securityInfo":
networkInfo.securityInfo = packet.state;
break;
}
this.emit("networkEventUpdate", {
packet: packet,
networkInfo
});
},
/**
* Retrieve the cached messages from the server.
*
* @see this.CACHED_MESSAGES
* @param array types
* The array of message types you want from the server. See
* this.CACHED_MESSAGES for known types.
* @param function aOnResponse
* The function invoked when the response is received.
*/
getCachedMessages: function WCC_getCachedMessages(types, aOnResponse)
{
let packet = {
to: this._actor,
type: "getCachedMessages",
messageTypes: types,
};
this._client.request(packet, aOnResponse);
},
/**
* Inspect the properties of an object.
*
* @param string aActor
* The WebConsoleObjectActor ID to send the request to.
* @param function aOnResponse
* The function invoked when the response is received.
*/
inspectObjectProperties:
function WCC_inspectObjectProperties(aActor, aOnResponse)
{
let packet = {
to: aActor,
type: "inspectProperties",
};
this._client.request(packet, aOnResponse);
},
/**
* Evaluate a JavaScript expression.
*
* @param string aString
* The code you want to evaluate.
* @param function aOnResponse
* The function invoked when the response is received.
* @param object [aOptions={}]
* Options for evaluation:
*
* - bindObjectActor: an ObjectActor ID. The OA holds a reference to
* a Debugger.Object that wraps a content object. This option allows
* you to bind |_self| to the D.O of the given OA, during string
* evaluation.
*
* See: Debugger.Object.evalInGlobalWithBindings() for information
* about bindings.
*
* Use case: the variable view needs to update objects and it does so
* by knowing the ObjectActor it inspects and binding |_self| to the
* D.O of the OA. As such, variable view sends strings like these for
* eval:
* _self["prop"] = value;
*
* - frameActor: a FrameActor ID. The FA holds a reference to
* a Debugger.Frame. This option allows you to evaluate the string in
* the frame of the given FA.
*
* - url: the url to evaluate the script as. Defaults to
* "debugger eval code".
*
* - selectedNodeActor: the NodeActor ID of the current selection in the
* Inspector, if such a selection exists. This is used by helper functions
* that can reference the currently selected node in the Inspector, like
* $0.
*/
evaluateJS: function WCC_evaluateJS(aString, aOnResponse, aOptions = {})
{
let packet = {
to: this._actor,
type: "evaluateJS",
text: aString,
bindObjectActor: aOptions.bindObjectActor,
frameActor: aOptions.frameActor,
url: aOptions.url,
selectedNodeActor: aOptions.selectedNodeActor,
};
this._client.request(packet, aOnResponse);
},
/**
* Evaluate a JavaScript expression asynchronously.
* See evaluateJS for parameter and response information.
*/
evaluateJSAsync: function(aString, aOnResponse, aOptions = {})
{
// Pre-37 servers don't support async evaluation.
if (!this.traits.evaluateJSAsync) {
this.evaluateJS(aString, aOnResponse, aOptions);
return;
}
let packet = {
to: this._actor,
type: "evaluateJSAsync",
text: aString,
bindObjectActor: aOptions.bindObjectActor,
frameActor: aOptions.frameActor,
url: aOptions.url,
selectedNodeActor: aOptions.selectedNodeActor,
};
this._client.request(packet, response => {
// Null check this in case the client has been detached while waiting
// for a response.
if (this.pendingEvaluationResults) {
this.pendingEvaluationResults.set(response.resultID, aOnResponse);
}
});
},
/**
* Handler for the actors's unsolicited evaluationResult packet.
*/
onEvaluationResult: function(aNotification, aPacket) {
// Find the associated callback based on this ID, and fire it.
// In a sync evaluation, this would have already been called in
// direct response to the client.request function.
let onResponse = this.pendingEvaluationResults.get(aPacket.resultID);
if (onResponse) {
onResponse(aPacket);
this.pendingEvaluationResults.delete(aPacket.resultID);
} else {
DevToolsUtils.reportException("onEvaluationResult",
"No response handler for an evaluateJSAsync result (resultID: " + aPacket.resultID + ")");
}
},
/**
* Autocomplete a JavaScript expression.
*
* @param string aString
* The code you want to autocomplete.
* @param number aCursor
* Cursor location inside the string. Index starts from 0.
* @param function aOnResponse
* The function invoked when the response is received.
* @param string aFrameActor
* The id of the frame actor that made the call.
*/
autocomplete: function WCC_autocomplete(aString, aCursor, aOnResponse, aFrameActor)
{
let packet = {
to: this._actor,
type: "autocomplete",
text: aString,
cursor: aCursor,
frameActor: aFrameActor,
};
this._client.request(packet, aOnResponse);
},
/**
* Clear the cache of messages (page errors and console API calls).
*/
clearMessagesCache: function WCC_clearMessagesCache()
{
let packet = {
to: this._actor,
type: "clearMessagesCache",
};
this._client.request(packet);
},
/**
* Get Web Console-related preferences on the server.
*
* @param array aPreferences
* An array with the preferences you want to retrieve.
* @param function [aOnResponse]
* Optional function to invoke when the response is received.
*/
getPreferences: function WCC_getPreferences(aPreferences, aOnResponse)
{
let packet = {
to: this._actor,
type: "getPreferences",
preferences: aPreferences,
};
this._client.request(packet, aOnResponse);
},
/**
* Set Web Console-related preferences on the server.
*
* @param object aPreferences
* An object with the preferences you want to change.
* @param function [aOnResponse]
* Optional function to invoke when the response is received.
*/
setPreferences: function WCC_setPreferences(aPreferences, aOnResponse)
{
let packet = {
to: this._actor,
type: "setPreferences",
preferences: aPreferences,
};
this._client.request(packet, aOnResponse);
},
/**
* Retrieve the request headers from the given NetworkEventActor.
*
* @param string aActor
* The NetworkEventActor ID.
* @param function aOnResponse
* The function invoked when the response is received.
*/
getRequestHeaders: function WCC_getRequestHeaders(aActor, aOnResponse)
{
let packet = {
to: aActor,
type: "getRequestHeaders",
};
this._client.request(packet, aOnResponse);
},
/**
* Retrieve the request cookies from the given NetworkEventActor.
*
* @param string aActor
* The NetworkEventActor ID.
* @param function aOnResponse
* The function invoked when the response is received.
*/
getRequestCookies: function WCC_getRequestCookies(aActor, aOnResponse)
{
let packet = {
to: aActor,
type: "getRequestCookies",
};
this._client.request(packet, aOnResponse);
},
/**
* Retrieve the request post data from the given NetworkEventActor.
*
* @param string aActor
* The NetworkEventActor ID.
* @param function aOnResponse
* The function invoked when the response is received.
*/
getRequestPostData: function WCC_getRequestPostData(aActor, aOnResponse)
{
let packet = {
to: aActor,
type: "getRequestPostData",
};
this._client.request(packet, aOnResponse);
},
/**
* Retrieve the response headers from the given NetworkEventActor.
*
* @param string aActor
* The NetworkEventActor ID.
* @param function aOnResponse
* The function invoked when the response is received.
*/
getResponseHeaders: function WCC_getResponseHeaders(aActor, aOnResponse)
{
let packet = {
to: aActor,
type: "getResponseHeaders",
};
this._client.request(packet, aOnResponse);
},
/**
* Retrieve the response cookies from the given NetworkEventActor.
*
* @param string aActor
* The NetworkEventActor ID.
* @param function aOnResponse
* The function invoked when the response is received.
*/
getResponseCookies: function WCC_getResponseCookies(aActor, aOnResponse)
{
let packet = {
to: aActor,
type: "getResponseCookies",
};
this._client.request(packet, aOnResponse);
},
/**
* Retrieve the response content from the given NetworkEventActor.
*
* @param string aActor
* The NetworkEventActor ID.
* @param function aOnResponse
* The function invoked when the response is received.
*/
getResponseContent: function WCC_getResponseContent(aActor, aOnResponse)
{
let packet = {
to: aActor,
type: "getResponseContent",
};
this._client.request(packet, aOnResponse);
},
/**
* Retrieve the timing information for the given NetworkEventActor.
*
* @param string aActor
* The NetworkEventActor ID.
* @param function aOnResponse
* The function invoked when the response is received.
*/
getEventTimings: function WCC_getEventTimings(aActor, aOnResponse)
{
let packet = {
to: aActor,
type: "getEventTimings",
};
this._client.request(packet, aOnResponse);
},
/**
* Retrieve the security information for the given NetworkEventActor.
*
* @param string aActor
* The NetworkEventActor ID.
* @param function aOnResponse
* The function invoked when the response is received.
*/
getSecurityInfo: function WCC_getSecurityInfo(aActor, aOnResponse)
{
let packet = {
to: aActor,
type: "getSecurityInfo",
};
this._client.request(packet, aOnResponse);
},
/**
* Send a HTTP request with the given data.
*
* @param string aData
* The details of the HTTP request.
* @param function aOnResponse
* The function invoked when the response is received.
*/
sendHTTPRequest: function WCC_sendHTTPRequest(aData, aOnResponse) {
let packet = {
to: this._actor,
type: "sendHTTPRequest",
request: aData
};
this._client.request(packet, aOnResponse);
},
/**
* Start the given Web Console listeners.
*
* @see this.LISTENERS
* @param array aListeners
* Array of listeners you want to start. See this.LISTENERS for
* known listeners.
* @param function aOnResponse
* Function to invoke when the server response is received.
*/
startListeners: function WCC_startListeners(aListeners, aOnResponse)
{
let packet = {
to: this._actor,
type: "startListeners",
listeners: aListeners,
};
this._client.request(packet, aOnResponse);
},
/**
* Stop the given Web Console listeners.
*
* @see this.LISTENERS
* @param array aListeners
* Array of listeners you want to stop. See this.LISTENERS for
* known listeners.
* @param function aOnResponse
* Function to invoke when the server response is received.
*/
stopListeners: function WCC_stopListeners(aListeners, aOnResponse)
{
let packet = {
to: this._actor,
type: "stopListeners",
listeners: aListeners,
};
this._client.request(packet, aOnResponse);
},
/**
* Return an instance of LongStringClient for the given long string grip.
*
* @param object aGrip
* The long string grip returned by the protocol.
* @return object
* The LongStringClient for the given long string grip.
*/
longString: function WCC_longString(aGrip)
{
if (aGrip.actor in this._longStrings) {
return this._longStrings[aGrip.actor];
}
let client = new LongStringClient(this._client, aGrip);
this._longStrings[aGrip.actor] = client;
return client;
},
/**
* Close the WebConsoleClient. This stops all the listeners on the server and
* detaches from the console actor.
*
* @param function aOnResponse
* Function to invoke when the server response is received.
*/
detach: function WCC_detach(aOnResponse)
{
this._client.removeListener("evaluationResult", this.onEvaluationResult);
this._client.removeListener("networkEvent", this.onNetworkEvent);
this._client.removeListener("networkEventUpdate", this.onNetworkEventUpdate);
this.stopListeners(null, aOnResponse);
this._longStrings = null;
this._client = null;
this.pendingEvaluationResults.clear();
this.pendingEvaluationResults = null;
this.clearNetworkRequests();
this._networkRequests = null;
},
clearNetworkRequests: function () {
this._networkRequests.clear();
}
};