toolkit/devtools/webconsole/client.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/devtools/webconsole/client.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,400 @@
     1.4 +/* -*- js2-basic-offset: 2; indent-tabs-mode: nil; -*- */
     1.5 +/* vim: set 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 +
    1.12 +const {Cc, Ci, Cu} = require("chrome");
    1.13 +
    1.14 +loader.lazyImporter(this, "LongStringClient", "resource://gre/modules/devtools/dbg-client.jsm");
    1.15 +
    1.16 +/**
    1.17 + * A WebConsoleClient is used as a front end for the WebConsoleActor that is
    1.18 + * created on the server, hiding implementation details.
    1.19 + *
    1.20 + * @param object aDebuggerClient
    1.21 + *        The DebuggerClient instance we live for.
    1.22 + * @param object aResponse
    1.23 + *        The response packet received from the "startListeners" request sent to
    1.24 + *        the WebConsoleActor.
    1.25 + */
    1.26 +function WebConsoleClient(aDebuggerClient, aResponse)
    1.27 +{
    1.28 +  this._actor = aResponse.from;
    1.29 +  this._client = aDebuggerClient;
    1.30 +  this._longStrings = {};
    1.31 +  this.traits = aResponse.traits || {};
    1.32 +}
    1.33 +exports.WebConsoleClient = WebConsoleClient;
    1.34 +
    1.35 +WebConsoleClient.prototype = {
    1.36 +  _longStrings: null,
    1.37 +  traits: null,
    1.38 +
    1.39 +  get actor() { return this._actor; },
    1.40 +
    1.41 +  /**
    1.42 +   * Retrieve the cached messages from the server.
    1.43 +   *
    1.44 +   * @see this.CACHED_MESSAGES
    1.45 +   * @param array aTypes
    1.46 +   *        The array of message types you want from the server. See
    1.47 +   *        this.CACHED_MESSAGES for known types.
    1.48 +   * @param function aOnResponse
    1.49 +   *        The function invoked when the response is received.
    1.50 +   */
    1.51 +  getCachedMessages: function WCC_getCachedMessages(aTypes, aOnResponse)
    1.52 +  {
    1.53 +    let packet = {
    1.54 +      to: this._actor,
    1.55 +      type: "getCachedMessages",
    1.56 +      messageTypes: aTypes,
    1.57 +    };
    1.58 +    this._client.request(packet, aOnResponse);
    1.59 +  },
    1.60 +
    1.61 +  /**
    1.62 +   * Inspect the properties of an object.
    1.63 +   *
    1.64 +   * @param string aActor
    1.65 +   *        The WebConsoleObjectActor ID to send the request to.
    1.66 +   * @param function aOnResponse
    1.67 +   *        The function invoked when the response is received.
    1.68 +   */
    1.69 +  inspectObjectProperties:
    1.70 +  function WCC_inspectObjectProperties(aActor, aOnResponse)
    1.71 +  {
    1.72 +    let packet = {
    1.73 +      to: aActor,
    1.74 +      type: "inspectProperties",
    1.75 +    };
    1.76 +    this._client.request(packet, aOnResponse);
    1.77 +  },
    1.78 +
    1.79 +  /**
    1.80 +   * Evaluate a JavaScript expression.
    1.81 +   *
    1.82 +   * @param string aString
    1.83 +   *        The code you want to evaluate.
    1.84 +   * @param function aOnResponse
    1.85 +   *        The function invoked when the response is received.
    1.86 +   * @param object [aOptions={}]
    1.87 +   *        Options for evaluation:
    1.88 +   *
    1.89 +   *        - bindObjectActor: an ObjectActor ID. The OA holds a reference to
    1.90 +   *        a Debugger.Object that wraps a content object. This option allows
    1.91 +   *        you to bind |_self| to the D.O of the given OA, during string
    1.92 +   *        evaluation.
    1.93 +   *
    1.94 +   *        See: Debugger.Object.evalInGlobalWithBindings() for information
    1.95 +   *        about bindings.
    1.96 +   *
    1.97 +   *        Use case: the variable view needs to update objects and it does so
    1.98 +   *        by knowing the ObjectActor it inspects and binding |_self| to the
    1.99 +   *        D.O of the OA. As such, variable view sends strings like these for
   1.100 +   *        eval:
   1.101 +   *          _self["prop"] = value;
   1.102 +   *
   1.103 +   *        - frameActor: a FrameActor ID. The FA holds a reference to
   1.104 +   *        a Debugger.Frame. This option allows you to evaluate the string in
   1.105 +   *        the frame of the given FA.
   1.106 +   *
   1.107 +   *        - url: the url to evaluate the script as. Defaults to
   1.108 +   *        "debugger eval code".
   1.109 +   */
   1.110 +  evaluateJS: function WCC_evaluateJS(aString, aOnResponse, aOptions = {})
   1.111 +  {
   1.112 +    let packet = {
   1.113 +      to: this._actor,
   1.114 +      type: "evaluateJS",
   1.115 +      text: aString,
   1.116 +      bindObjectActor: aOptions.bindObjectActor,
   1.117 +      frameActor: aOptions.frameActor,
   1.118 +      url: aOptions.url,
   1.119 +    };
   1.120 +    this._client.request(packet, aOnResponse);
   1.121 +  },
   1.122 +
   1.123 +  /**
   1.124 +   * Autocomplete a JavaScript expression.
   1.125 +   *
   1.126 +   * @param string aString
   1.127 +   *        The code you want to autocomplete.
   1.128 +   * @param number aCursor
   1.129 +   *        Cursor location inside the string. Index starts from 0.
   1.130 +   * @param function aOnResponse
   1.131 +   *        The function invoked when the response is received.
   1.132 +   * @param string aFrameActor
   1.133 +   *        The id of the frame actor that made the call.
   1.134 +   */
   1.135 +  autocomplete: function WCC_autocomplete(aString, aCursor, aOnResponse, aFrameActor)
   1.136 +  {
   1.137 +    let packet = {
   1.138 +      to: this._actor,
   1.139 +      type: "autocomplete",
   1.140 +      text: aString,
   1.141 +      cursor: aCursor,
   1.142 +      frameActor: aFrameActor,
   1.143 +    };
   1.144 +    this._client.request(packet, aOnResponse);
   1.145 +  },
   1.146 +
   1.147 +  /**
   1.148 +   * Clear the cache of messages (page errors and console API calls).
   1.149 +   */
   1.150 +  clearMessagesCache: function WCC_clearMessagesCache()
   1.151 +  {
   1.152 +    let packet = {
   1.153 +      to: this._actor,
   1.154 +      type: "clearMessagesCache",
   1.155 +    };
   1.156 +    this._client.request(packet);
   1.157 +  },
   1.158 +
   1.159 +  /**
   1.160 +   * Get Web Console-related preferences on the server.
   1.161 +   *
   1.162 +   * @param array aPreferences
   1.163 +   *        An array with the preferences you want to retrieve.
   1.164 +   * @param function [aOnResponse]
   1.165 +   *        Optional function to invoke when the response is received.
   1.166 +   */
   1.167 +  getPreferences: function WCC_getPreferences(aPreferences, aOnResponse)
   1.168 +  {
   1.169 +    let packet = {
   1.170 +      to: this._actor,
   1.171 +      type: "getPreferences",
   1.172 +      preferences: aPreferences,
   1.173 +    };
   1.174 +    this._client.request(packet, aOnResponse);
   1.175 +  },
   1.176 +
   1.177 +  /**
   1.178 +   * Set Web Console-related preferences on the server.
   1.179 +   *
   1.180 +   * @param object aPreferences
   1.181 +   *        An object with the preferences you want to change.
   1.182 +   * @param function [aOnResponse]
   1.183 +   *        Optional function to invoke when the response is received.
   1.184 +   */
   1.185 +  setPreferences: function WCC_setPreferences(aPreferences, aOnResponse)
   1.186 +  {
   1.187 +    let packet = {
   1.188 +      to: this._actor,
   1.189 +      type: "setPreferences",
   1.190 +      preferences: aPreferences,
   1.191 +    };
   1.192 +    this._client.request(packet, aOnResponse);
   1.193 +  },
   1.194 +
   1.195 +  /**
   1.196 +   * Retrieve the request headers from the given NetworkEventActor.
   1.197 +   *
   1.198 +   * @param string aActor
   1.199 +   *        The NetworkEventActor ID.
   1.200 +   * @param function aOnResponse
   1.201 +   *        The function invoked when the response is received.
   1.202 +   */
   1.203 +  getRequestHeaders: function WCC_getRequestHeaders(aActor, aOnResponse)
   1.204 +  {
   1.205 +    let packet = {
   1.206 +      to: aActor,
   1.207 +      type: "getRequestHeaders",
   1.208 +    };
   1.209 +    this._client.request(packet, aOnResponse);
   1.210 +  },
   1.211 +
   1.212 +  /**
   1.213 +   * Retrieve the request cookies from the given NetworkEventActor.
   1.214 +   *
   1.215 +   * @param string aActor
   1.216 +   *        The NetworkEventActor ID.
   1.217 +   * @param function aOnResponse
   1.218 +   *        The function invoked when the response is received.
   1.219 +   */
   1.220 +  getRequestCookies: function WCC_getRequestCookies(aActor, aOnResponse)
   1.221 +  {
   1.222 +    let packet = {
   1.223 +      to: aActor,
   1.224 +      type: "getRequestCookies",
   1.225 +    };
   1.226 +    this._client.request(packet, aOnResponse);
   1.227 +  },
   1.228 +
   1.229 +  /**
   1.230 +   * Retrieve the request post data from the given NetworkEventActor.
   1.231 +   *
   1.232 +   * @param string aActor
   1.233 +   *        The NetworkEventActor ID.
   1.234 +   * @param function aOnResponse
   1.235 +   *        The function invoked when the response is received.
   1.236 +   */
   1.237 +  getRequestPostData: function WCC_getRequestPostData(aActor, aOnResponse)
   1.238 +  {
   1.239 +    let packet = {
   1.240 +      to: aActor,
   1.241 +      type: "getRequestPostData",
   1.242 +    };
   1.243 +    this._client.request(packet, aOnResponse);
   1.244 +  },
   1.245 +
   1.246 +  /**
   1.247 +   * Retrieve the response headers from the given NetworkEventActor.
   1.248 +   *
   1.249 +   * @param string aActor
   1.250 +   *        The NetworkEventActor ID.
   1.251 +   * @param function aOnResponse
   1.252 +   *        The function invoked when the response is received.
   1.253 +   */
   1.254 +  getResponseHeaders: function WCC_getResponseHeaders(aActor, aOnResponse)
   1.255 +  {
   1.256 +    let packet = {
   1.257 +      to: aActor,
   1.258 +      type: "getResponseHeaders",
   1.259 +    };
   1.260 +    this._client.request(packet, aOnResponse);
   1.261 +  },
   1.262 +
   1.263 +  /**
   1.264 +   * Retrieve the response cookies from the given NetworkEventActor.
   1.265 +   *
   1.266 +   * @param string aActor
   1.267 +   *        The NetworkEventActor ID.
   1.268 +   * @param function aOnResponse
   1.269 +   *        The function invoked when the response is received.
   1.270 +   */
   1.271 +  getResponseCookies: function WCC_getResponseCookies(aActor, aOnResponse)
   1.272 +  {
   1.273 +    let packet = {
   1.274 +      to: aActor,
   1.275 +      type: "getResponseCookies",
   1.276 +    };
   1.277 +    this._client.request(packet, aOnResponse);
   1.278 +  },
   1.279 +
   1.280 +  /**
   1.281 +   * Retrieve the response content from the given NetworkEventActor.
   1.282 +   *
   1.283 +   * @param string aActor
   1.284 +   *        The NetworkEventActor ID.
   1.285 +   * @param function aOnResponse
   1.286 +   *        The function invoked when the response is received.
   1.287 +   */
   1.288 +  getResponseContent: function WCC_getResponseContent(aActor, aOnResponse)
   1.289 +  {
   1.290 +    let packet = {
   1.291 +      to: aActor,
   1.292 +      type: "getResponseContent",
   1.293 +    };
   1.294 +    this._client.request(packet, aOnResponse);
   1.295 +  },
   1.296 +
   1.297 +  /**
   1.298 +   * Retrieve the timing information for the given NetworkEventActor.
   1.299 +   *
   1.300 +   * @param string aActor
   1.301 +   *        The NetworkEventActor ID.
   1.302 +   * @param function aOnResponse
   1.303 +   *        The function invoked when the response is received.
   1.304 +   */
   1.305 +  getEventTimings: function WCC_getEventTimings(aActor, aOnResponse)
   1.306 +  {
   1.307 +    let packet = {
   1.308 +      to: aActor,
   1.309 +      type: "getEventTimings",
   1.310 +    };
   1.311 +    this._client.request(packet, aOnResponse);
   1.312 +  },
   1.313 +
   1.314 +  /**
   1.315 +   * Send a HTTP request with the given data.
   1.316 +   *
   1.317 +   * @param string aData
   1.318 +   *        The details of the HTTP request.
   1.319 +   * @param function aOnResponse
   1.320 +   *        The function invoked when the response is received.
   1.321 +   */
   1.322 +  sendHTTPRequest: function WCC_sendHTTPRequest(aData, aOnResponse) {
   1.323 +    let packet = {
   1.324 +      to: this._actor,
   1.325 +      type: "sendHTTPRequest",
   1.326 +      request: aData
   1.327 +    };
   1.328 +    this._client.request(packet, aOnResponse);
   1.329 +  },
   1.330 +
   1.331 +  /**
   1.332 +   * Start the given Web Console listeners.
   1.333 +   *
   1.334 +   * @see this.LISTENERS
   1.335 +   * @param array aListeners
   1.336 +   *        Array of listeners you want to start. See this.LISTENERS for
   1.337 +   *        known listeners.
   1.338 +   * @param function aOnResponse
   1.339 +   *        Function to invoke when the server response is received.
   1.340 +   */
   1.341 +  startListeners: function WCC_startListeners(aListeners, aOnResponse)
   1.342 +  {
   1.343 +    let packet = {
   1.344 +      to: this._actor,
   1.345 +      type: "startListeners",
   1.346 +      listeners: aListeners,
   1.347 +    };
   1.348 +    this._client.request(packet, aOnResponse);
   1.349 +  },
   1.350 +
   1.351 +  /**
   1.352 +   * Stop the given Web Console listeners.
   1.353 +   *
   1.354 +   * @see this.LISTENERS
   1.355 +   * @param array aListeners
   1.356 +   *        Array of listeners you want to stop. See this.LISTENERS for
   1.357 +   *        known listeners.
   1.358 +   * @param function aOnResponse
   1.359 +   *        Function to invoke when the server response is received.
   1.360 +   */
   1.361 +  stopListeners: function WCC_stopListeners(aListeners, aOnResponse)
   1.362 +  {
   1.363 +    let packet = {
   1.364 +      to: this._actor,
   1.365 +      type: "stopListeners",
   1.366 +      listeners: aListeners,
   1.367 +    };
   1.368 +    this._client.request(packet, aOnResponse);
   1.369 +  },
   1.370 +
   1.371 +  /**
   1.372 +   * Return an instance of LongStringClient for the given long string grip.
   1.373 +   *
   1.374 +   * @param object aGrip
   1.375 +   *        The long string grip returned by the protocol.
   1.376 +   * @return object
   1.377 +   *         The LongStringClient for the given long string grip.
   1.378 +   */
   1.379 +  longString: function WCC_longString(aGrip)
   1.380 +  {
   1.381 +    if (aGrip.actor in this._longStrings) {
   1.382 +      return this._longStrings[aGrip.actor];
   1.383 +    }
   1.384 +
   1.385 +    let client = new LongStringClient(this._client, aGrip);
   1.386 +    this._longStrings[aGrip.actor] = client;
   1.387 +    return client;
   1.388 +  },
   1.389 +
   1.390 +  /**
   1.391 +   * Close the WebConsoleClient. This stops all the listeners on the server and
   1.392 +   * detaches from the console actor.
   1.393 +   *
   1.394 +   * @param function aOnResponse
   1.395 +   *        Function to invoke when the server response is received.
   1.396 +   */
   1.397 +  close: function WCC_close(aOnResponse)
   1.398 +  {
   1.399 +    this.stopListeners(null, aOnResponse);
   1.400 +    this._longStrings = null;
   1.401 +    this._client = null;
   1.402 +  },
   1.403 +};

mercurial