client.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. "use strict";
  6. const DevToolsUtils = require("devtools/shared/DevToolsUtils");
  7. const EventEmitter = require("devtools/shared/event-emitter");
  8. const promise = require("promise");
  9. const defer = require("devtools/shared/defer");
  10. const {LongStringClient} = require("devtools/shared/client/main");
  11. /**
  12. * A WebConsoleClient is used as a front end for the WebConsoleActor that is
  13. * created on the server, hiding implementation details.
  14. *
  15. * @param object debuggerClient
  16. * The DebuggerClient instance we live for.
  17. * @param object response
  18. * The response packet received from the "startListeners" request sent to
  19. * the WebConsoleActor.
  20. */
  21. function WebConsoleClient(debuggerClient, response) {
  22. this._actor = response.from;
  23. this._client = debuggerClient;
  24. this._longStrings = {};
  25. this.traits = response.traits || {};
  26. this.events = [];
  27. this._networkRequests = new Map();
  28. this.pendingEvaluationResults = new Map();
  29. this.onEvaluationResult = this.onEvaluationResult.bind(this);
  30. this.onNetworkEvent = this._onNetworkEvent.bind(this);
  31. this.onNetworkEventUpdate = this._onNetworkEventUpdate.bind(this);
  32. this._client.addListener("evaluationResult", this.onEvaluationResult);
  33. this._client.addListener("networkEvent", this.onNetworkEvent);
  34. this._client.addListener("networkEventUpdate", this.onNetworkEventUpdate);
  35. EventEmitter.decorate(this);
  36. }
  37. exports.WebConsoleClient = WebConsoleClient;
  38. WebConsoleClient.prototype = {
  39. _longStrings: null,
  40. traits: null,
  41. /**
  42. * Holds the network requests currently displayed by the Web Console. Each key
  43. * represents the connection ID and the value is network request information.
  44. * @private
  45. * @type object
  46. */
  47. _networkRequests: null,
  48. getNetworkRequest(actorId) {
  49. return this._networkRequests.get(actorId);
  50. },
  51. hasNetworkRequest(actorId) {
  52. return this._networkRequests.has(actorId);
  53. },
  54. removeNetworkRequest(actorId) {
  55. this._networkRequests.delete(actorId);
  56. },
  57. getNetworkEvents() {
  58. return this._networkRequests.values();
  59. },
  60. get actor() {
  61. return this._actor;
  62. },
  63. /**
  64. * The "networkEvent" message type handler. We redirect any message to
  65. * the UI for displaying.
  66. *
  67. * @private
  68. * @param string type
  69. * Message type.
  70. * @param object packet
  71. * The message received from the server.
  72. */
  73. _onNetworkEvent: function (type, packet) {
  74. if (packet.from == this._actor) {
  75. let actor = packet.eventActor;
  76. let networkInfo = {
  77. _type: "NetworkEvent",
  78. timeStamp: actor.timeStamp,
  79. node: null,
  80. actor: actor.actor,
  81. discardRequestBody: true,
  82. discardResponseBody: true,
  83. startedDateTime: actor.startedDateTime,
  84. request: {
  85. url: actor.url,
  86. method: actor.method,
  87. },
  88. isXHR: actor.isXHR,
  89. cause: actor.cause,
  90. response: {},
  91. timings: {},
  92. // track the list of network event updates
  93. updates: [],
  94. private: actor.private,
  95. fromCache: actor.fromCache,
  96. fromServiceWorker: actor.fromServiceWorker
  97. };
  98. this._networkRequests.set(actor.actor, networkInfo);
  99. this.emit("networkEvent", networkInfo);
  100. }
  101. },
  102. /**
  103. * The "networkEventUpdate" message type handler. We redirect any message to
  104. * the UI for displaying.
  105. *
  106. * @private
  107. * @param string type
  108. * Message type.
  109. * @param object packet
  110. * The message received from the server.
  111. */
  112. _onNetworkEventUpdate: function (type, packet) {
  113. let networkInfo = this.getNetworkRequest(packet.from);
  114. if (!networkInfo) {
  115. return;
  116. }
  117. networkInfo.updates.push(packet.updateType);
  118. switch (packet.updateType) {
  119. case "requestHeaders":
  120. networkInfo.request.headersSize = packet.headersSize;
  121. break;
  122. case "requestPostData":
  123. networkInfo.discardRequestBody = packet.discardRequestBody;
  124. networkInfo.request.bodySize = packet.dataSize;
  125. break;
  126. case "responseStart":
  127. networkInfo.response.httpVersion = packet.response.httpVersion;
  128. networkInfo.response.status = packet.response.status;
  129. networkInfo.response.statusText = packet.response.statusText;
  130. networkInfo.response.headersSize = packet.response.headersSize;
  131. networkInfo.response.remoteAddress = packet.response.remoteAddress;
  132. networkInfo.response.remotePort = packet.response.remotePort;
  133. networkInfo.discardResponseBody = packet.response.discardResponseBody;
  134. break;
  135. case "responseContent":
  136. networkInfo.response.content = {
  137. mimeType: packet.mimeType,
  138. };
  139. networkInfo.response.bodySize = packet.contentSize;
  140. networkInfo.response.transferredSize = packet.transferredSize;
  141. networkInfo.discardResponseBody = packet.discardResponseBody;
  142. break;
  143. case "eventTimings":
  144. networkInfo.totalTime = packet.totalTime;
  145. break;
  146. case "securityInfo":
  147. networkInfo.securityInfo = packet.state;
  148. break;
  149. }
  150. this.emit("networkEventUpdate", {
  151. packet: packet,
  152. networkInfo
  153. });
  154. },
  155. /**
  156. * Retrieve the cached messages from the server.
  157. *
  158. * @see this.CACHED_MESSAGES
  159. * @param array types
  160. * The array of message types you want from the server. See
  161. * this.CACHED_MESSAGES for known types.
  162. * @param function onResponse
  163. * The function invoked when the response is received.
  164. */
  165. getCachedMessages: function (types, onResponse) {
  166. let packet = {
  167. to: this._actor,
  168. type: "getCachedMessages",
  169. messageTypes: types,
  170. };
  171. this._client.request(packet, onResponse);
  172. },
  173. /**
  174. * Inspect the properties of an object.
  175. *
  176. * @param string actor
  177. * The WebConsoleObjectActor ID to send the request to.
  178. * @param function onResponse
  179. * The function invoked when the response is received.
  180. */
  181. inspectObjectProperties: function (actor, onResponse) {
  182. let packet = {
  183. to: actor,
  184. type: "inspectProperties",
  185. };
  186. this._client.request(packet, onResponse);
  187. },
  188. /**
  189. * Evaluate a JavaScript expression.
  190. *
  191. * @param string string
  192. * The code you want to evaluate.
  193. * @param function onResponse
  194. * The function invoked when the response is received.
  195. * @param object [options={}]
  196. * Options for evaluation:
  197. *
  198. * - bindObjectActor: an ObjectActor ID. The OA holds a reference to
  199. * a Debugger.Object that wraps a content object. This option allows
  200. * you to bind |_self| to the D.O of the given OA, during string
  201. * evaluation.
  202. *
  203. * See: Debugger.Object.executeInGlobalWithBindings() for information
  204. * about bindings.
  205. *
  206. * Use case: the variable view needs to update objects and it does so
  207. * by knowing the ObjectActor it inspects and binding |_self| to the
  208. * D.O of the OA. As such, variable view sends strings like these for
  209. * eval:
  210. * _self["prop"] = value;
  211. *
  212. * - frameActor: a FrameActor ID. The FA holds a reference to
  213. * a Debugger.Frame. This option allows you to evaluate the string in
  214. * the frame of the given FA.
  215. *
  216. * - url: the url to evaluate the script as. Defaults to
  217. * "debugger eval code".
  218. *
  219. * - selectedNodeActor: the NodeActor ID of the current
  220. * selection in the Inspector, if such a selection
  221. * exists. This is used by helper functions that can
  222. * reference the currently selected node in the Inspector,
  223. * like $0.
  224. */
  225. evaluateJS: function (string, onResponse, options = {}) {
  226. let packet = {
  227. to: this._actor,
  228. type: "evaluateJS",
  229. text: string,
  230. bindObjectActor: options.bindObjectActor,
  231. frameActor: options.frameActor,
  232. url: options.url,
  233. selectedNodeActor: options.selectedNodeActor,
  234. selectedObjectActor: options.selectedObjectActor,
  235. };
  236. this._client.request(packet, onResponse);
  237. },
  238. /**
  239. * Evaluate a JavaScript expression asynchronously.
  240. * See evaluateJS for parameter and response information.
  241. */
  242. evaluateJSAsync: function (string, onResponse, options = {}) {
  243. // Pre-37 servers don't support async evaluation.
  244. if (!this.traits.evaluateJSAsync) {
  245. this.evaluateJS(string, onResponse, options);
  246. return;
  247. }
  248. let packet = {
  249. to: this._actor,
  250. type: "evaluateJSAsync",
  251. text: string,
  252. bindObjectActor: options.bindObjectActor,
  253. frameActor: options.frameActor,
  254. url: options.url,
  255. selectedNodeActor: options.selectedNodeActor,
  256. selectedObjectActor: options.selectedObjectActor,
  257. };
  258. this._client.request(packet, response => {
  259. // Null check this in case the client has been detached while waiting
  260. // for a response.
  261. if (this.pendingEvaluationResults) {
  262. this.pendingEvaluationResults.set(response.resultID, onResponse);
  263. }
  264. });
  265. },
  266. /**
  267. * Handler for the actors's unsolicited evaluationResult packet.
  268. */
  269. onEvaluationResult: function (notification, packet) {
  270. // The client on the main thread can receive notification packets from
  271. // multiple webconsole actors: the one on the main thread and the ones
  272. // on worker threads. So make sure we should be handling this request.
  273. if (packet.from !== this._actor) {
  274. return;
  275. }
  276. // Find the associated callback based on this ID, and fire it.
  277. // In a sync evaluation, this would have already been called in
  278. // direct response to the client.request function.
  279. let onResponse = this.pendingEvaluationResults.get(packet.resultID);
  280. if (onResponse) {
  281. onResponse(packet);
  282. this.pendingEvaluationResults.delete(packet.resultID);
  283. } else {
  284. DevToolsUtils.reportException("onEvaluationResult",
  285. "No response handler for an evaluateJSAsync result (resultID: " +
  286. packet.resultID + ")");
  287. }
  288. },
  289. /**
  290. * Autocomplete a JavaScript expression.
  291. *
  292. * @param string string
  293. * The code you want to autocomplete.
  294. * @param number cursor
  295. * Cursor location inside the string. Index starts from 0.
  296. * @param function onResponse
  297. * The function invoked when the response is received.
  298. * @param string frameActor
  299. * The id of the frame actor that made the call.
  300. */
  301. autocomplete: function (string, cursor, onResponse, frameActor) {
  302. let packet = {
  303. to: this._actor,
  304. type: "autocomplete",
  305. text: string,
  306. cursor: cursor,
  307. frameActor: frameActor,
  308. };
  309. this._client.request(packet, onResponse);
  310. },
  311. /**
  312. * Clear the cache of messages (page errors and console API calls).
  313. */
  314. clearMessagesCache: function () {
  315. let packet = {
  316. to: this._actor,
  317. type: "clearMessagesCache",
  318. };
  319. this._client.request(packet);
  320. },
  321. /**
  322. * Get Web Console-related preferences on the server.
  323. *
  324. * @param array preferences
  325. * An array with the preferences you want to retrieve.
  326. * @param function [onResponse]
  327. * Optional function to invoke when the response is received.
  328. */
  329. getPreferences: function (preferences, onResponse) {
  330. let packet = {
  331. to: this._actor,
  332. type: "getPreferences",
  333. preferences: preferences,
  334. };
  335. this._client.request(packet, onResponse);
  336. },
  337. /**
  338. * Set Web Console-related preferences on the server.
  339. *
  340. * @param object preferences
  341. * An object with the preferences you want to change.
  342. * @param function [onResponse]
  343. * Optional function to invoke when the response is received.
  344. */
  345. setPreferences: function (preferences, onResponse) {
  346. let packet = {
  347. to: this._actor,
  348. type: "setPreferences",
  349. preferences: preferences,
  350. };
  351. this._client.request(packet, onResponse);
  352. },
  353. /**
  354. * Retrieve the request headers from the given NetworkEventActor.
  355. *
  356. * @param string actor
  357. * The NetworkEventActor ID.
  358. * @param function onResponse
  359. * The function invoked when the response is received.
  360. */
  361. getRequestHeaders: function (actor, onResponse) {
  362. let packet = {
  363. to: actor,
  364. type: "getRequestHeaders",
  365. };
  366. this._client.request(packet, onResponse);
  367. },
  368. /**
  369. * Retrieve the request cookies from the given NetworkEventActor.
  370. *
  371. * @param string actor
  372. * The NetworkEventActor ID.
  373. * @param function onResponse
  374. * The function invoked when the response is received.
  375. */
  376. getRequestCookies: function (actor, onResponse) {
  377. let packet = {
  378. to: actor,
  379. type: "getRequestCookies",
  380. };
  381. this._client.request(packet, onResponse);
  382. },
  383. /**
  384. * Retrieve the request post data from the given NetworkEventActor.
  385. *
  386. * @param string actor
  387. * The NetworkEventActor ID.
  388. * @param function onResponse
  389. * The function invoked when the response is received.
  390. */
  391. getRequestPostData: function (actor, onResponse) {
  392. let packet = {
  393. to: actor,
  394. type: "getRequestPostData",
  395. };
  396. this._client.request(packet, onResponse);
  397. },
  398. /**
  399. * Retrieve the response headers from the given NetworkEventActor.
  400. *
  401. * @param string actor
  402. * The NetworkEventActor ID.
  403. * @param function onResponse
  404. * The function invoked when the response is received.
  405. */
  406. getResponseHeaders: function (actor, onResponse) {
  407. let packet = {
  408. to: actor,
  409. type: "getResponseHeaders",
  410. };
  411. this._client.request(packet, onResponse);
  412. },
  413. /**
  414. * Retrieve the response cookies from the given NetworkEventActor.
  415. *
  416. * @param string actor
  417. * The NetworkEventActor ID.
  418. * @param function onResponse
  419. * The function invoked when the response is received.
  420. */
  421. getResponseCookies: function (actor, onResponse) {
  422. let packet = {
  423. to: actor,
  424. type: "getResponseCookies",
  425. };
  426. this._client.request(packet, onResponse);
  427. },
  428. /**
  429. * Retrieve the response content from the given NetworkEventActor.
  430. *
  431. * @param string actor
  432. * The NetworkEventActor ID.
  433. * @param function onResponse
  434. * The function invoked when the response is received.
  435. */
  436. getResponseContent: function (actor, onResponse) {
  437. let packet = {
  438. to: actor,
  439. type: "getResponseContent",
  440. };
  441. this._client.request(packet, onResponse);
  442. },
  443. /**
  444. * Retrieve the timing information for the given NetworkEventActor.
  445. *
  446. * @param string actor
  447. * The NetworkEventActor ID.
  448. * @param function onResponse
  449. * The function invoked when the response is received.
  450. */
  451. getEventTimings: function (actor, onResponse) {
  452. let packet = {
  453. to: actor,
  454. type: "getEventTimings",
  455. };
  456. this._client.request(packet, onResponse);
  457. },
  458. /**
  459. * Retrieve the security information for the given NetworkEventActor.
  460. *
  461. * @param string actor
  462. * The NetworkEventActor ID.
  463. * @param function onResponse
  464. * The function invoked when the response is received.
  465. */
  466. getSecurityInfo: function (actor, onResponse) {
  467. let packet = {
  468. to: actor,
  469. type: "getSecurityInfo",
  470. };
  471. this._client.request(packet, onResponse);
  472. },
  473. /**
  474. * Send a HTTP request with the given data.
  475. *
  476. * @param string data
  477. * The details of the HTTP request.
  478. * @param function onResponse
  479. * The function invoked when the response is received.
  480. */
  481. sendHTTPRequest: function (data, onResponse) {
  482. let packet = {
  483. to: this._actor,
  484. type: "sendHTTPRequest",
  485. request: data
  486. };
  487. this._client.request(packet, onResponse);
  488. },
  489. /**
  490. * Start the given Web Console listeners.
  491. *
  492. * @see this.LISTENERS
  493. * @param array listeners
  494. * Array of listeners you want to start. See this.LISTENERS for
  495. * known listeners.
  496. * @param function onResponse
  497. * Function to invoke when the server response is received.
  498. */
  499. startListeners: function (listeners, onResponse) {
  500. let packet = {
  501. to: this._actor,
  502. type: "startListeners",
  503. listeners: listeners,
  504. };
  505. this._client.request(packet, onResponse);
  506. },
  507. /**
  508. * Stop the given Web Console listeners.
  509. *
  510. * @see this.LISTENERS
  511. * @param array listeners
  512. * Array of listeners you want to stop. See this.LISTENERS for
  513. * known listeners.
  514. * @param function onResponse
  515. * Function to invoke when the server response is received.
  516. */
  517. stopListeners: function (listeners, onResponse) {
  518. let packet = {
  519. to: this._actor,
  520. type: "stopListeners",
  521. listeners: listeners,
  522. };
  523. this._client.request(packet, onResponse);
  524. },
  525. /**
  526. * Return an instance of LongStringClient for the given long string grip.
  527. *
  528. * @param object grip
  529. * The long string grip returned by the protocol.
  530. * @return object
  531. * The LongStringClient for the given long string grip.
  532. */
  533. longString: function (grip) {
  534. if (grip.actor in this._longStrings) {
  535. return this._longStrings[grip.actor];
  536. }
  537. let client = new LongStringClient(this._client, grip);
  538. this._longStrings[grip.actor] = client;
  539. return client;
  540. },
  541. /**
  542. * Close the WebConsoleClient. This stops all the listeners on the server and
  543. * detaches from the console actor.
  544. *
  545. * @param function onResponse
  546. * Function to invoke when the server response is received.
  547. */
  548. detach: function (onResponse) {
  549. this._client.removeListener("evaluationResult", this.onEvaluationResult);
  550. this._client.removeListener("networkEvent", this.onNetworkEvent);
  551. this._client.removeListener("networkEventUpdate",
  552. this.onNetworkEventUpdate);
  553. this.stopListeners(null, onResponse);
  554. this._longStrings = null;
  555. this._client = null;
  556. this.pendingEvaluationResults.clear();
  557. this.pendingEvaluationResults = null;
  558. this.clearNetworkRequests();
  559. this._networkRequests = null;
  560. },
  561. clearNetworkRequests: function () {
  562. this._networkRequests.clear();
  563. },
  564. /**
  565. * Fetches the full text of a LongString.
  566. *
  567. * @param object | string stringGrip
  568. * The long string grip containing the corresponding actor.
  569. * If you pass in a plain string (by accident or because you're lazy),
  570. * then a promise of the same string is simply returned.
  571. * @return object Promise
  572. * A promise that is resolved when the full string contents
  573. * are available, or rejected if something goes wrong.
  574. */
  575. getString: function (stringGrip) {
  576. // Make sure this is a long string.
  577. if (typeof stringGrip != "object" || stringGrip.type != "longString") {
  578. // Go home string, you're drunk.
  579. return promise.resolve(stringGrip);
  580. }
  581. // Fetch the long string only once.
  582. if (stringGrip._fullText) {
  583. return stringGrip._fullText.promise;
  584. }
  585. let deferred = stringGrip._fullText = defer();
  586. let { initial, length } = stringGrip;
  587. let longStringClient = this.longString(stringGrip);
  588. longStringClient.substring(initial.length, length, response => {
  589. if (response.error) {
  590. DevToolsUtils.reportException("getString",
  591. response.error + ": " + response.message);
  592. deferred.reject(response);
  593. return;
  594. }
  595. deferred.resolve(initial + response.substring);
  596. });
  597. return deferred.promise;
  598. }
  599. };