Web Console remoting

  • Revision slug: Tools/Web_Console/remoting
  • Revision title: Web Console remoting
  • Revision id: 458875
  • Created:
  • Creator: mihaisucan
  • Is current revision? No
  • Comment add the new protocol messages from bug 731311 (sendHTTPRequest) for firefox 25

Revision Content

Introduction

This document describes the way Web Console remoting works. The Web Console is split between a client with its user interface, and the server which has listeners for all the things that happen in the tab. For communication between the server and the client we use the Remote Debugging Protocol. This architecture allows you to connect a Web Console client instance to a server running on B2G, Fennec or some other Firefox instance.

To better understand the architecture of the Web Console we recommend learning about the debugger architecture.

The remote Web Console is a feature introduced in Firefox 18. This document describes the latest protocol, with changes that have been made since then.

The WebConsoleActor and the WebConsoleClient

The WebConsoleActor lives in dbg-webconsole-actors.js, in the toolkit/devtools/webconsole folder.

The WebConsoleClient lives in WebConsoleClient.jsm (in toolkit/devtools/webconsole) and it is used by the Web Console when working with the Web Console actor.

To see how the debugger is used in the Web Console code, look in browser/devtools/webconsole/webconsole.js, and search for WebConsoleConnectionProxy.

The new Web Console actors are:

  • The WebConsoleActor allows JS evaluation, autocomplete, start/stop listeners, etc.
  • The NetworkEventActor is used for each new network request. The client can request further network event details - like response body or request headers.

To attach to the WebConsoleActor, follow these steps:

connectToServer() // the usual
listTabs()
pickTheTabYouWant()
debuggerClient.attachConsole(tab.consoleActor, listeners, onAttachConsole)

The listeners argument is an array which specifies listeners you want to start in the web console. These can be: page errors, window.console API messages, network activity, and file activity. For example:

["PageError", "ConsoleAPI", "NetworkActivity", "FileActivity"]

The Web Console actor does not start any listeners by default. The client has the option to start each listener when needed. This approach allows for lower resource usage on the server - this is a potential issue if the server runs on devices with fewer resources.

The onAttachConsole callback receives a new instance of the WebConsoleClient object. This object provides methods that abstract away protocol packets, things like startListeners(), stopListeners(), etc.

Protocol packets look as follows:

{
  "to": "root",
  "type": "listTabs"
}
{
  "from": "root",
  "consoleActor": "conn0.console9",
  "selected": 2,
  "tabs": [
    {
      "actor": "conn0.tab2",
      "consoleActor": "conn0.console7",
      "title": "",
      "url": "https://tbpl.mozilla.org/?tree=Fx-Team"
    },
// ...
  ]
}

Notice that the consoleActor is also available as a global actor. When you attach to the global consoleActor you receive all of the network requests, page errors, and the other events from all of the tabs and windows, including chrome errors and network events. This actor is used for the Browser Console implementation and for debugging remote Firefox/B2G instances.

startListeners(listeners, onResponse)

The new startListeners packet:

{
  "to": "conn0.console9",
  "type": "startListeners",
  "listeners": [
    "PageError",
    "ConsoleAPI",
    "NetworkActivity",
    "FileActivity"
  ]
}

The reply is:

{
  "startedListeners": [
    "PageError",
    "ConsoleAPI",
    "NetworkActivity",
    "FileActivity"
  ],
  "nativeConsoleAPI": true,
  "from": "conn0.console9"
}

The reply tells which listeners were started and it includes a flag nativeConsoleAPI which tells if the window.console object was overridden by the scripts in the page or not.

Tab navigation

To listen to the tab navigation events you also need to attach to the tab actor for the given tab. The tabNavigated notification comes from tab actors.

Prior to Firefox 20 the Web Console actor provided a LocationChange listener, with an associated locationChanged notification. This is no longer the case: we have made changes to allow the Web Console client to reuse the tabNavigated notification (bug 792062).

When page navigation starts the following packet is sent from the tab actor:

{
  "from": tabActor,
  "type": "tabNavigated",
  "state": "start",
  "url": newURL,
  "nativeConsoleAPI": true
}

The nativeConsoleAPI property tells if the window.console object is native or not for the top level window object for the given tab - this is always true when navigation starts. When navigation stops the following packet is sent:

{
  "from": tabActor,
  "type": "tabNavigated",
  "state": "stop",
  "url": newURL,
  "title": newTitle,
  "nativeConsoleAPI": true|false
}

getCachedMessages(types, onResponse)

The webConsoleClient.getCachedMessages(types, onResponse) method sends the following packet to the server:

{
  "to": "conn0.console9",
  "type": "getCachedMessages",
  "messageTypes": [
    "PageError",
    "ConsoleAPI"
  ]
}

The getCachedMessages packet allows one to retrieve the cached messages from before the Web Console was open. You can only get cached messages for page errors and console API calls. The reply looks like this:

{
  "messages": [ ... ],
  "from": "conn0.console9"
}

Each message in the array is of the same type as when we send typical page errors and console API calls. These will be explained in the following sections of this document.

Actor preferences

To allow the Web Console to configure logging options while it is running, we have added the setPreferences packet:

{
  "to": "conn0.console9",
  "type": "setPreferences",
  "preferences": {
    "NetworkMonitor.saveRequestAndResponseBodies": false
  }
}

Reply:

{
  "updated": [
    "NetworkMonitor.saveRequestAndResponseBodies"
  ],
  "from": "conn0.console10"
}

For convenience you can use webConsoleClient.setPreferences(prefs, onResponse). The prefs argument is an object like { prefName: prefValue, ... }.

In Firefox 25 we added the getPreferences request packet:

{
  "to": "conn0.console34",
  "type": "getPreferences",
  "preferences": [
    "NetworkMonitor.saveRequestAndResponseBodies"
  ]
}

Reply packet:

{
  "preferences": {
    "NetworkMonitor.saveRequestAndResponseBodies": false
  },
  "from": "conn0.console34"
}

You can also use the webConsoleClient.getPreferences(prefs, onResponse). The prefs argument is an array of preferences you want to get their values for, by name.

Private browsing

The Browser Console can be used while the user has private windows open. Each page error, console API message and network request is annotated with a private flag. Private messages are cleared whenever the last private window is closed. The console actor provides the lastPrivateContextExited notification:

{
  "from": "conn0.console19",
  "type": "lastPrivateContextExited"
}

This notification is sent only when your client is attached to the global console actor, it does not make sense for tab console actors.

This notification has been introduced in Firefox 24.

Send HTTP requests

Starting with Firefox 25 you can send an HTTP request using the console actor:

{
  "to": "conn0.console9",
  "type": "sendHTTPRequest",
  "request": {
    "url": "http://localhost",
    "method": "GET",
    "headers": [
      {
        name: "Header-name",
        value: "header value",
      },
      // ...
    ],
  },
}

The response packet is a network event actor grip:

{
  "to": "conn0.console9",
  "eventActor": {
    "actor": "conn0.netEvent14",
    "startedDateTime": "2013-08-26T19:50:03.699Z",
    "url": "http://localhost",
    "method": "GET"
    "isXHR": true,
    "private": false
  }
}

You can also use the webConsoleClient.sendHTTPRequest(request, onResponse) method. The request argument is the same as the request object in the above example request packet.

Page errors

Page errors come from the nsIConsoleService. Each allowed page error is an nsIScriptError object.

The pageError packet is:

{
  "from": "conn0.console9",
  "type": "pageError",
  "pageError": {
    "errorMessage": "ReferenceError: foo is not defined",
    "sourceName": "http://localhost/~mihai/mozilla/test.js",
    "lineText": "",
    "lineNumber": 6,
    "columnNumber": 0,
    "category": "content javascript",
    "timeStamp": 1347294508210,
    "error": false,
    "warning": false,
    "exception": true,
    "strict": false,
    "private": false,
  }
}

The packet is similar to nsIScriptError - for simplicity. We only removed several unneeded properties and changed how flags work.

The private flag tells if the error comes from a private window/tab (added in Firefox 24).

Starting with Firefox 24 the errorMessage and lineText properties can be long string actor grips if the string is very long.

Console API messages

The window.console API calls send internal messages throughout Gecko which allow us to do whatever we want for each call. The Web Console actor sends these messages to the remote debugging client.

We use the ObjectActor from dbg-script-actors.js without a ThreadActor, to avoid slowing down the page scripts - the debugger deoptimizes JavaScript execution in the target page. The lifetime of object actors in the Web Console is different than the lifetime of these objects in the debugger - which is usually per pause or per thread. The Web Console manages the lifetime of ObjectActors manually.

Prior to Firefox 23 we used a different actor (WebConsoleObjectActor) for working with JavaScript objects through the protocol. In bug 783499 we did a number of changes that allowed us to reuse the ObjectActor from the debugger.

Console API messages come through the nsIOb serverService - the console object implementation lives in dom/base/ConsoleAPI.js.

For each console message we receive in the server, we send the following consoleAPICall packet to the client:

{
  "from": "conn0.console9",
  "type": "consoleAPICall",
  "message": {
    "level": "error",
    "filename": "http://localhost/~mihai/mozilla/test.html",
    "lineNumber": 149,
    "functionName": "",
    "timeStamp": 1347302713771,
    "private": false,
    "arguments": [
      "error omg aloha ",
      {
        "type": "object",
        "className": "HTMLBodyElement",
        "actor": "conn0.consoleObj20"
      },
      " 960 739 3.141592653589793 %a",
      "zuzu",
      { "type": "null" },
      { "type": "undefined" }
    ]
  }
}

Similar to how we send the page errors, here we send the actual console event received from the nsIObserverService. We change the arguments array - we create ObjectActor instances for each object passed as an argument - and, lastly, we remove some unneeded properties (like window IDs). In the case of long strings we use the LongStringActor. The Web Console can then inspect the arguments.

The private flag tells if the console API call comes from a private window/tab (added in Firefox 24).

We have small variations for the object, depending on the console API call method - just like there are small differences in the console event object received from the observer service. To see these differences please look in the Console API implementation: dom/base/ConsoleAPI.js.

JavaScript evaluation

The evaluateJS request and response packets

The Web Console client provides the evaluateJS(requestId, string, onResponse) method which sends the following packet:

{
  "to": "conn0.console9",
  "type": "evaluateJS",
  "text": "document",
  "bindObjectActor": null,
  "frameActor": null,
  "url": null,
}

The bindObjectActor property is an optional ObjectActor ID that points to a Debugger.Object. This option allows you to bind _self to the Debugger.Object of the given object actor, during string evaluation. See evalInGlobalWithBindings() for information about bindings.

The variable view needs to update objects and it does so by binding _self to the Debugger.Object of the ObjectActor that is being viewed. As such, variable view sends strings like these for evaluation:

  _self["prop"] = value;

The frameActor property is an optional 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.

The url property is an optional URL to evaluate the script as (new in Firefox 25). The default source URL for evaluation is "debugger eval code".

The response packet:

{
  "from": "conn0.console9",
  "input": "document",
  "result": {
    "type": "object",
    "className": "HTMLDocument",
    "actor": "conn0.consoleObj20"
    "extensible": true,
    "frozen": false,
    "sealed": false
  },
  "timestamp": 1347306273605,
  "exception": null,
  "exceptionMessage": null,
  "helperResult": null
}
  • exception holds the JSON-ification of the exception thrown during evaluation.
  • exceptionMessage holds the exception.toString() result.
  • result has the result ObjectActor instance.
  • helperResult is anything that might come from a JSTerm helper result, JSON stuff (not content objects!).

In Firefox 23: we renamed the error and errorMessage properties to exception and exceptionMessage respectively, to avoid conflict with the default properties used when protocol errors occur.

Autocomplete and more

The autocomplete request packet:

{
  "to": "conn0.console9",
  "type": "autocomplete",
  "text": "d",
  "cursor": 1
}

The response packet:

{
  "from": "conn0.console9",
  "matches": [
    "decodeURI",
    "decodeURIComponent",
    "defaultStatus",
    "devicePixelRatio",
    "disableExternalCapture",
    "dispatchEvent",
    "doMyXHR",
    "document",
    "dump"
  ],
  "matchProp": "d"
}

There's also the clearMessagesCache request packet that has no response. This clears the console API calls cache and should clear the page errors cache - see bug 717611.

Network logging

The networkEvent packet

Whenever a new network request starts being logged the networkEvent packet is sent:

{
  "from": "conn0.console10",
  "type": "networkEvent",
  "eventActor": {
    "actor": "conn0.netEvent14",
    "startedDateTime": "2012-09-17T19:50:03.699Z",
    "url": "http://localhost/~mihai/mozilla/test2.css",
    "method": "GET"
    "isXHR": false,
    "private": false
  }
}

This packet is used to inform the Web Console of a new network event. For each request a new NetworkEventActor instance is created. The isXHR flag indicates if the request was initiated via an XMLHttpRequest instance, that is: the nsIHttpChannel's notification is of an nsIXMLHttpRequest interface.

The private flag tells if the network request comes from a private window/tab (added in Firefox 24).

The NetworkEventActor

The new network event actor stores further request and response information.

The networkEventUpdate packet

The Web Console UI needs to be kept up-to-date when changes happen, when new stuff is added. The new networkEventUpdate packet is sent for this purpose. Examples:

{
  "from": "conn0.netEvent14",
  "type": "networkEventUpdate",
  "updateType": "requestHeaders",
  "headers": 10,
  "headersSize": 425
},
{
  "from": "conn0.netEvent14",
  "type": "networkEventUpdate",
  "updateType": "requestCookies",
  "cookies": 0
},
{
  "from": "conn0.netEvent14",
  "type": "networkEventUpdate",
  "updateType": "requestPostData",
  "dataSize": 1024,
  "discardRequestBody": false
},
{
  "from": "conn0.netEvent14",
  "type": "networkEventUpdate",
  "updateType": "responseStart",
  "response": {
    "httpVersion": "HTTP/1.1",
    "status": "304",
    "statusText": "Not Modified",
    "headersSize": 194,
    "discardResponseBody": true
  }
},
{
  "from": "conn0.netEvent14",
  "type": "networkEventUpdate",
  "updateType": "eventTimings",
  "totalTime": 1
},
{
  "from": "conn0.netEvent14",
  "type": "networkEventUpdate",
  "updateType": "responseHeaders",
  "headers": 6,
  "headersSize": 194
},
{
  "from": "conn0.netEvent14",
  "type": "networkEventUpdate",
  "updateType": "responseCookies",
  "cookies": 0
},
{
  "from": "conn0.netEvent14",
  "type": "networkEventUpdate",
  "updateType": "responseContent",
  "mimeType": "text/css",
  "contentSize": 0,
  "discardResponseBody": true
}

Actual headers, cookies, and bodies are not sent.

The getRequestHeaders and other packets

To get more details about a network event you can use the following packet requests (and replies).

The getRequestHeaders packet:

{
  "to": "conn0.netEvent15",
  "type": "getRequestHeaders"
}
{
  "from": "conn0.netEvent15",
  "headers": [
    {
      "name": "Host",
      "value": "localhost"
    }, ...
  ],
  "headersSize": 350
}

The getRequestCookies packet:

{
  "to": "conn0.netEvent15",
  "type": "getRequestCookies"
}
{
  "from": "conn0.netEvent15",
  "cookies": []
}

The getResponseHeaders packet:

{
  "to": "conn0.netEvent15",
  "type": "getResponseHeaders"
}
{
  "from": "conn0.netEvent15",
  "headers": [
    {
      "name": "Date",
      "value": "Mon, 17 Sep 2012 20:05:27 GMT"
    }, ...
  ],
  "headersSize": 320
}

The getResponseCookies packet:

{
  "to": "conn0.netEvent15",
  "type": "getResponseCookies"
}
{
  "from": "conn0.netEvent15",
  "cookies": []
}

Starting with Firefox 19: for all of the header and cookie values in the above packets we use LongStringActor grips when the value is very long. This helps us avoid using too much of the network bandwidth.

The getRequestPostData packet:

{
  "to": "conn0.netEvent15",
  "type": "getRequestPostData"
}
{
  "from": "conn0.netEvent15",
  "postData": { text: "foobar" },
  "postDataDiscarded": false
}

The getResponseContent packet:

{
  "to": "conn0.netEvent15",
  "type": "getResponseContent"
}
{
  "from": "conn0.netEvent15",
  "content": {
    "mimeType": "text/css",
    "text": "\n@import \"test.css\";\n\n.foobar { color: green }\n\n"
  },
  "contentDiscarded": false
}

The request and response content text value is most commonly sent using a LongStringActor grip. For very short request/response bodies we send the raw text.

Starting with Firefox 19: for non-text response types we send the content in base64 encoding (again, most likely a LongStringActor grip). To tell the difference just check if response.content.encoding == "base64".

The getEventTimings packet:

{
  "to": "conn0.netEvent15",
  "type": "getEventTimings"
}
{
  "from": "conn0.netEvent15",
  "timings": {
    "blocked": 0,
    "dns": 0,
    "connect": 0,
    "send": 0,
    "wait": 16,
    "receive": 0
  },
  "totalTime": 16
}

The fileActivity packet

When a file load is observed the following fileActivity packet is sent to the client:

{
  "from": "conn0.console9",
  "type": "fileActivity",
  "uri": "file:///home/mihai/public_html/mozilla/test2.css"
}

History

Protocol changes by Firefox version:

  • Firefox 18: initial version.
  • Firefox 19: bug 787981 - added LongStringActor usage in several places.
  • Firefox 20: bug 792062 - removed locationChanged packet and updated the tabNavigated packet for tab actors.
  • Firefox 23: bug 783499 - removed the WebConsoleObjectActor. Now the Web Console uses the JavaScript debugger API and the ObjectActor.
  • Firefox 23: added the bindObjectActor and frameActor options to the evaluateJS request packet.
  • Firefox 24: new private flags for the console actor notifications, bug 874061. Also added the lastPrivateContextExited notification for the global console actor.
  • Firefox 24: new isXHR flag for the networkEvent notification, bug 859046.
  • Firefox 24: removed the message property from the pageError packet notification, bug 877773. The lineText and errorMessage properties can be long string actors now.
  • Firefox 25: added the url option to the evaluateJS request packet.
  • Firefox 25: added the getPreferences and sendHTTPRequest request packets to the console actor, bug 886067 and bug 731311.

Conclusions

As of this writing, this document is a dense summary of the work we did in bug 768096 and subsequent changes. We try to keep this document up-to-date. We hope this is helpful for you.

If you make changes to the Web Console server please update this document. Thank you!

Revision Source

<h2 id="Introduction">Introduction</h2>
<p>This document describes the way Web Console remoting works. The Web Console is split between a client with its user interface, and the server which has listeners for all the things that happen in the tab. For communication between the server and the client we use the <a href="https://wiki.mozilla.org/Remote_Debugging_Protocol" title="https://wiki.mozilla.org/Remote_Debugging_Protocol">Remote Debugging Protocol</a>. This architecture allows you to connect a Web Console client instance to a server running on B2G, Fennec or some other Firefox instance.</p>
<p>To better understand the architecture of the Web Console we recommend learning about the <a href="https://wiki.mozilla.org/Debugger_Architecture" title="https://wiki.mozilla.org/Debugger_Architecture">debugger architecture</a>.</p>
<div class="note">
  <p>The remote Web Console is a feature introduced in Firefox 18. This document describes the latest protocol, with changes that have been made since then.</p>
</div>
<h2 id="The_WebConsoleActor_and_the_WebConsoleClient">The <code>WebConsoleActor</code> and the <code>WebConsoleClient</code></h2>
<p>The <code>WebConsoleActor</code> lives in <a href="http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/webconsole/dbg-webconsole-actors.js" title="http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/webconsole/dbg-webconsole-actors.js"><code>dbg-webconsole-actors.js</code></a>, in the <a href="http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/webconsole/" title="http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/webconsole/"><code>toolkit/devtools/webconsole</code></a> folder.</p>
<p>The <code>WebConsoleClient</code> lives in <a href="http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/webconsole/WebConsoleClient.jsm" title="http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/webconsole/WebConsoleClient.jsm"><code>WebConsoleClient.jsm</code></a> (in <a href="http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/webconsole/" title="http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/webconsole/"><code>toolkit/devtools/webconsole</code></a>) and it is used by the Web Console when working with the Web Console actor.</p>
<p>To see how the debugger is used in the Web Console code, look in <a href="http://mxr.mozilla.org/mozilla-central/source/browser/devtools/webconsole/webconsole.js" title="http://mxr.mozilla.org/mozilla-central/source/browser/devtools/webconsole/webconsole.js"><code>browser/devtools/webconsole/webconsole.js</code></a>, and search for <code>WebConsoleConnectionProxy</code>.</p>
<p>The new Web Console actors are:</p>
<ul>
  <li>The <code>WebConsoleActor</code> allows JS evaluation, autocomplete, start/stop listeners, etc.</li>
  <li>The <code>NetworkEventActor</code> is used for each new network request. The client can request further network event details - like response body or request headers.</li>
</ul>
<p>To attach to the <code>WebConsoleActor</code>, follow these steps:</p>
<pre class="brush:js;">
connectToServer() // the usual
listTabs()
pickTheTabYouWant()
debuggerClient.attachConsole(tab.consoleActor, listeners, onAttachConsole)
</pre>
<p>The <code>listeners</code> argument is an array which specifies listeners you want to start in the web console. These can be: page errors, <code>window.console</code> API messages, network activity, and file activity. For example:</p>
<pre class="brush:js;">
["PageError", "ConsoleAPI", "NetworkActivity", "FileActivity"]</pre>
<div class="note">
  <p>The Web Console actor does not start any listeners by default. The client has the option to start each listener when needed. This approach allows for lower resource usage on the server - this is a potential issue if the server runs on devices with fewer resources.</p>
</div>
<p>The <code>onAttachConsole</code> callback receives a new instance of the <code>WebConsoleClient</code> object. This object provides methods that abstract away protocol packets, things like <code>startListeners(), stopListeners()</code>, etc.</p>
<p>Protocol packets look as follows:</p>
<pre class="brush:json;">
{
  "to": "root",
  "type": "listTabs"
}
{
  "from": "root",
  "consoleActor": "conn0.console9",
  "selected": 2,
  "tabs": [
    {
      "actor": "conn0.tab2",
      "consoleActor": "conn0.console7",
      "title": "",
      "url": "https://tbpl.mozilla.org/?tree=Fx-Team"
    },
// ...
  ]
}
</pre>
<p>Notice that the <code>consoleActor</code> is also available as a <strong>global actor</strong>. When you attach to the global <code>consoleActor</code> you receive all of the network requests, page errors, and the other events from all of the tabs and windows, including chrome errors and network events. This actor is used for the Browser Console implementation and for debugging remote Firefox/B2G instances.</p>
<h3 id="startListeners(listeners.2C_onResponse)"><code>startListeners(listeners, onResponse)</code></h3>
<p>The new <code>startListeners</code> packet:</p>
<pre class="brush:json;">
{
  "to": "conn0.console9",
  "type": "startListeners",
  "listeners": [
    "PageError",
    "ConsoleAPI",
    "NetworkActivity",
    "FileActivity"
  ]
}
</pre>
<p>The reply is:</p>
<pre class="brush:json;">
{
  "startedListeners": [
    "PageError",
    "ConsoleAPI",
    "NetworkActivity",
    "FileActivity"
  ],
  "nativeConsoleAPI": true,
  "from": "conn0.console9"
}
</pre>
<p>The reply tells which listeners were started and it includes a flag <code>nativeConsoleAPI</code> which tells if the <code>window.console</code> object was overridden by the scripts in the page or not.</p>
<h3 id="Tab_navigation">Tab navigation</h3>
<p>To listen to the tab navigation events you also need to attach to the tab actor for the given tab. The <code>tabNavigated</code> notification comes from tab actors.</p>
<div class="warning">
  <p>Prior to Firefox 20 the Web Console actor provided a <code>LocationChange</code> listener, with an associated <code>locationChanged</code> notification. This is no longer the case: we have made changes to allow the Web Console client to reuse the <code>tabNavigated</code> notification (<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=792062" title="https://bugzilla.mozilla.org/show_bug.cgi?id=792062">bug 792062</a>).</p>
</div>
<p>When page navigation starts the following packet is sent from the tab actor:</p>
<pre class="brush: json">
<code class="brush: json">{
  "from": tabActor,
  "type": "tabNavigated",
  "state": "start",
  "url": newURL,
  "nativeConsoleAPI": true
}</code>
</pre>
<p>The <code>nativeConsoleAPI</code> property tells if the <code>window.console</code> object is native or not for the top level window object for the given tab - this is always <code>true</code> when navigation starts. When navigation stops the following packet is sent:</p>
<pre class="brush: json">
<code>{
  "from": tabActor,
  "type": "tabNavigated",
  "state": "stop",
  "url": newURL,
  "title": newTitle,
  "nativeConsoleAPI": true|false
}</code></pre>
<h3 id="getCachedMessages(types.2C_onResponse)"><code>getCachedMessages(types, onResponse)</code></h3>
<p>The <code>webConsoleClient.getCachedMessages(types, onResponse)</code> method sends the following packet to the server:</p>
<pre class="brush:json;">
{
  "to": "conn0.console9",
  "type": "getCachedMessages",
  "messageTypes": [
    "PageError",
    "ConsoleAPI"
  ]
}
</pre>
<p>The <code>getCachedMessages</code> packet allows one to retrieve the cached messages from before the Web Console was open. You can only get cached messages for page errors and console API calls. The reply looks like this:</p>
<pre class="brush:json;">
{
  "messages": [ ... ],
  "from": "conn0.console9"
}
</pre>
<p>Each message in the array is of the same type as when we send typical page errors and console API calls. These will be explained in the following sections of this document.</p>
<h3 id="Actor_preferences">Actor preferences</h3>
<p>To allow the Web Console to configure logging options while it is running, we have added the <code>setPreferences</code> packet:</p>
<pre class="brush:json;">
{
  "to": "conn0.console9",
  "type": "setPreferences",
  "preferences": {
    "NetworkMonitor.saveRequestAndResponseBodies": false
  }
}
</pre>
<p>Reply:</p>
<pre class="brush:json;">
{
  "updated": [
    "NetworkMonitor.saveRequestAndResponseBodies"
  ],
  "from": "conn0.console10"
}
</pre>
<p>For convenience you can use <code>webConsoleClient.setPreferences(prefs, onResponse)</code>. The <code>prefs</code> argument is an object like <code>{ prefName: prefValue, ... }</code>.</p>
<p>In Firefox 25 we added the <code>getPreferences</code> request packet:</p>
<pre class="brush:json;">
{
  "to": "conn0.console34",
  "type": "getPreferences",
  "preferences": [
    "NetworkMonitor.saveRequestAndResponseBodies"
  ]
}
</pre>
<p>Reply packet:</p>
<pre class="brush:json;">
{
  "preferences": {
    "NetworkMonitor.saveRequestAndResponseBodies": false
  },
  "from": "conn0.console34"
}
</pre>
<p>You can also use the <code>webConsoleClient.getPreferences(prefs, onResponse)</code>. The <code>prefs</code> argument is an array of preferences you want to get their values for, by name.</p>
<h3 id="Private_browsing">Private browsing</h3>
<p>The Browser Console can be used while the user has private windows open. Each page error, console API message and network request is annotated with a <code>private</code> flag. Private messages are cleared whenever the last private window is closed. The console actor provides the <code>lastPrivateContextExited</code> notification:</p>
<pre class="brush:json;">
{
  "from": "conn0.console19",
  "type": "lastPrivateContextExited"
}
</pre>
<p>This notification is sent only when your client is attached to the global console actor, it does not make sense for tab console actors.</p>
<div class="note">
  <p>This notification has been introduced in Firefox 24.</p>
</div>
<h3 id="Send_HTTP_requests">Send HTTP requests</h3>
<p>Starting with Firefox 25 you can send an HTTP request using the console actor:</p>
<pre class="brush:json;">
{
  "to": "conn0.console9",
  "type": "sendHTTPRequest",
  "request": {
    "url": "http://localhost",
    "method": "GET",
    "headers": [
      {
        name: "Header-name",
        value: "header value",
      },
      // ...
    ],
  },
}
</pre>
<p>The response packet is a network event actor grip:</p>
<pre class="brush:json;">
{
  "to": "conn0.console9",
  "eventActor": {
    "actor": "conn0.netEvent14",
    "startedDateTime": "2013-08-26T19:50:03.699Z",
    "url": "http://localhost",
    "method": "GET"
    "isXHR": true,
    "private": false
  }
}
</pre>
<p>You can also use the <code>webConsoleClient.sendHTTPRequest(request, onResponse)</code> method. The <code>request</code> argument is the same as the <code>request</code> object in the above example request packet.</p>
<h2 id="Page_errors">Page errors</h2>
<p>Page errors come from the <a href="/en-US/docs/XPCOM_Interface_Reference/nsIConsoleService" title="/en-US/docs/XPCOM_Interface_Reference/nsIConsoleService"><code>nsIConsoleService</code></a>. Each allowed page error is an <a href="/en-US/docs/XPCOM_Interface_Reference/nsIScriptError" title="/en-US/docs/XPCOM_Interface_Reference/nsIScriptError"><code>nsIScriptError</code></a> object.</p>
<p>The <code>pageError</code> packet is:</p>
<pre class="brush:json;">
{
  "from": "conn0.console9",
  "type": "pageError",
  "pageError": {
    "errorMessage": "ReferenceError: foo is not defined",
    "sourceName": "http://localhost/~mihai/mozilla/test.js",
    "lineText": "",
    "lineNumber": 6,
    "columnNumber": 0,
    "category": "content javascript",
    "timeStamp": 1347294508210,
    "error": false,
    "warning": false,
    "exception": true,
    "strict": false,
    "private": false,
  }
}
</pre>
<p>The packet is similar to <code>nsIScriptError</code> - for simplicity. We only removed several unneeded properties and changed how flags work.</p>
<p>The <code>private</code> flag tells if the error comes from a private window/tab (added in Firefox 24).</p>
<p>Starting with Firefox 24 the <code>errorMessage</code> and <code>lineText</code> properties can be long string actor grips if the string is very long.</p>
<h2 id="Console_API_messages">Console API messages</h2>
<p>The <a href="/en-US/docs/DOM/console" title="/en-US/docs/DOM/console"><code>window.console</code> API</a> calls send internal messages throughout Gecko which allow us to do whatever we want for each call. The Web Console actor sends these messages to the remote debugging client.</p>
<p>We use the <code>ObjectActor</code> from <a href="https://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/debugger/server/dbg-script-actors.js" title="https://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/debugger/server/dbg-script-actors.js">dbg-script-actors.js</a> without a <code>ThreadActor</code>, to avoid slowing down the page scripts - the debugger deoptimizes JavaScript execution in the target page. The <a href="https://wiki.mozilla.org/Remote_Debugging_Protocol#Grip_Lifetimes" title="https://wiki.mozilla.org/Remote_Debugging_Protocol#Grip_Lifetimes">lifetime of object actors</a> in the Web Console is different than the lifetime of these objects in the debugger - which is usually per pause or per thread. The Web Console manages the lifetime of <code>ObjectActors</code> manually.</p>
<div class="warning">
  <p>Prior to Firefox 23 we used a different actor (<code>WebConsoleObjectActor</code>) for working with JavaScript objects through the protocol. In <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=783499" title="https://bugzilla.mozilla.org/show_bug.cgi?id=783499">bug 783499</a> we did a number of changes that allowed us to reuse the <code>ObjectActor</code> from the debugger.</p>
</div>
<p>Console API messages come through the <a href="/en-US/docs/XPCOM_Interface_Reference/nsIObserverService" title="/en-US/docs/XPCOM_Interface_Reference/nsIObserverService"><code>nsIOb<span style="display: none;">&nbsp;</span>serverService</code></a> - the console object implementation lives in <a href="http://mxr.mozilla.org/mozilla-central/source/dom/base/ConsoleAPI.js" title="http://mxr.mozilla.org/mozilla-central/source/dom/base/ConsoleAPI.js"><code>dom/base/ConsoleAPI.js</code></a>.</p>
<p>For each console message we receive in the server, we send the following <code>consoleAPICall</code> packet to the client:</p>
<pre class="brush:json;">
{
  "from": "conn0.console9",
  "type": "consoleAPICall",
  "message": {
    "level": "error",
    "filename": "http://localhost/~mihai/mozilla/test.html",
    "lineNumber": 149,
    "functionName": "",
    "timeStamp": 1347302713771,
    "private": false,
    "arguments": [
      "error omg aloha ",
      {
        "type": "object",
        "className": "HTMLBodyElement",
        "actor": "conn0.consoleObj20"
      },
      " 960 739 3.141592653589793 %a",
      "zuzu",
      { "type": "null" },
      { "type": "undefined" }
    ]
  }
}
</pre>
<p>Similar to how we send the page errors, here we send the actual console event received from the <code>nsIObserverService</code>. We change the <code>arguments</code> array - we create <code>ObjectActor</code> instances for each object passed as an argument - and, lastly, we remove some unneeded properties (like window IDs). In the case of long strings we use the <code>LongStringActor</code>. The Web Console can then inspect the arguments.</p>
<p>The <code>private</code> flag tells if the console API call comes from a private window/tab (added in Firefox 24).</p>
<p>We have small variations for the object, depending on the console API call method - just like there are small differences in the console event object received from the observer service. To see these differences please look in the Console API implementation: <a href="http://mxr.mozilla.org/mozilla-central/source/dom/base/ConsoleAPI.js" title="http://mxr.mozilla.org/mozilla-central/source/dom/base/ConsoleAPI.js">dom/base/ConsoleAPI.js</a>.</p>
<h3 id="JavaScript_evaluation">JavaScript evaluation</h3>
<h4 id="The_evaluateJS_request_and_response_packets">The <code>evaluateJS</code> request and response packets</h4>
<p>The Web Console client provides the <code>evaluateJS(requestId, string, onResponse)</code> method which sends the following packet:</p>
<pre class="brush:json;">
{
  "to": "conn0.console9",
  "type": "evaluateJS",
  "text": "document",
  "bindObjectActor": null,
  "frameActor": null,
  "url": null,
}
</pre>
<p>The <code>bindObjectActor</code> property is an optional <code>ObjectActor</code> ID that points to a <code>Debugger.Object</code>. This option allows you to bind <code>_self</code> to the <code>Debugger.Object</code> of the given object actor, during string evaluation. See <a href="https://developer.mozilla.org/en-US/docs/SpiderMonkey/JS_Debugger_API_Reference/Debugger.Object#evalInGlobalWithBindings%28%29"><code>evalInGlobalWithBindings()</code></a> for information about bindings.</p>
<div class="note">
  <p>The variable view needs to update objects and it does so by binding <code>_self</code> to the <code>Debugger.Object</code> of the <code>ObjectActor</code> that is being viewed. As such, variable view sends strings like these for evaluation:</p>
  <pre class="brush:js;">
  _self["prop"] = value;
</pre>
</div>
<p>The <code>frameActor</code> property is an optional <code>FrameActor</code> ID. The FA holds a reference to a <a href="https://developer.mozilla.org/en-US/docs/SpiderMonkey/JS_Debugger_API_Reference/Debugger.Frame"><code>Debugger.Frame</code></a>. This option allows you to evaluate the string in the frame of the given FA.</p>
<p>The <code>url</code> property is an optional URL to evaluate the script as (new in Firefox 25). The default source URL for evaluation is "debugger eval code".</p>
<p>The response packet:</p>
<pre class="brush:json;">
{
  "from": "conn0.console9",
  "input": "document",
  "result": {
    "type": "object",
    "className": "HTMLDocument",
    "actor": "conn0.consoleObj20"
    "extensible": true,
    "frozen": false,
    "sealed": false
  },
  "timestamp": 1347306273605,
  "exception": null,
  "exceptionMessage": null,
  "helperResult": null
}
</pre>
<ul>
  <li><code>exception</code> holds the JSON-ification of the exception thrown during evaluation.</li>
  <li><code>exceptionMessage</code> holds the <code>exception.toString()</code> result.</li>
  <li><code>result</code> has the result <code>ObjectActor</code> instance.</li>
  <li><code>helperResult</code> is anything that might come from a JSTerm helper result, JSON stuff (not content objects!).</li>
</ul>
<div class="warning">
  <p>In Firefox 23: we renamed the <code>error</code> and <code>errorMessage</code> properties to <code>exception</code> and <code>exceptionMessage</code> respectively, to avoid conflict with the default properties used when protocol errors occur.</p>
</div>
<h3 id="Autocomplete_and_more">Autocomplete and more</h3>
<p>The <code>autocomplete</code> request packet:</p>
<pre class="brush:json;">
{
  "to": "conn0.console9",
  "type": "autocomplete",
  "text": "d",
  "cursor": 1
}
</pre>
<p>The response packet:</p>
<pre class="brush:json;">
{
  "from": "conn0.console9",
  "matches": [
    "decodeURI",
    "decodeURIComponent",
    "defaultStatus",
    "devicePixelRatio",
    "disableExternalCapture",
    "dispatchEvent",
    "doMyXHR",
    "document",
    "dump"
  ],
  "matchProp": "d"
}
</pre>
<p>There's also the <code>clearMessagesCache</code> request packet that has no response. This clears the console API calls cache and should clear the page errors cache - see <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=717611">bug 717611</a>.</p>
<h2 id="Network_logging">Network logging</h2>
<h3 id="The_networkEvent_packet">The <code>networkEvent</code> packet</h3>
<p>Whenever a new network request starts being logged the <code>networkEvent</code> packet is sent:</p>
<pre class="brush:json;">
{
  "from": "conn0.console10",
  "type": "networkEvent",
  "eventActor": {
    "actor": "conn0.netEvent14",
    "startedDateTime": "2012-09-17T19:50:03.699Z",
    "url": "http://localhost/~mihai/mozilla/test2.css",
    "method": "GET"
    "isXHR": false,
    "private": false
  }
}
</pre>
<p>This packet is used to inform the Web Console of a new network event. For each request a new <code>NetworkEventActor</code> instance is created. The <code>isXHR</code> flag indicates if the request was initiated via an <a href="/en-US/docs/Web/API/XMLHttpRequest" title="/en-US/docs/Web/API/XMLHttpRequest">XMLHttpRequest</a> instance, that is: the <a href="/en-US/docs/XPCOM_Interface_Reference/nsIHttpChannel" title="/en-US/docs/">nsIHttpChannel</a>'s notification is of an <a href="/en-US/docs/nsIXMLHttpRequest" title="/en-US/docs/nsIXMLHttpRequest">nsIXMLHttpRequest</a> interface.</p>
<p>The <code>private</code> flag tells if the network request comes from a private window/tab (added in Firefox 24).</p>
<h3 id="The_NetworkEventActor">The <code>NetworkEventActor</code></h3>
<p>The new network event actor stores further request and response information.</p>
<h4 id="The_networkEventUpdate_packet">The <code>networkEventUpdate</code> packet</h4>
<p>The Web Console UI needs to be kept up-to-date when changes happen, when new stuff is added. The new <code>networkEventUpdate</code> packet is sent for this purpose. Examples:</p>
<pre class="brush:json;">
{
  "from": "conn0.netEvent14",
  "type": "networkEventUpdate",
  "updateType": "requestHeaders",
  "headers": 10,
  "headersSize": 425
},
{
  "from": "conn0.netEvent14",
  "type": "networkEventUpdate",
  "updateType": "requestCookies",
  "cookies": 0
},
{
  "from": "conn0.netEvent14",
  "type": "networkEventUpdate",
  "updateType": "requestPostData",
  "dataSize": 1024,
  "discardRequestBody": false
},
{
  "from": "conn0.netEvent14",
  "type": "networkEventUpdate",
  "updateType": "responseStart",
  "response": {
    "httpVersion": "HTTP/1.1",
    "status": "304",
    "statusText": "Not Modified",
    "headersSize": 194,
    "discardResponseBody": true
  }
},
{
  "from": "conn0.netEvent14",
  "type": "networkEventUpdate",
  "updateType": "eventTimings",
  "totalTime": 1
},
{
  "from": "conn0.netEvent14",
  "type": "networkEventUpdate",
  "updateType": "responseHeaders",
  "headers": 6,
  "headersSize": 194
},
{
  "from": "conn0.netEvent14",
  "type": "networkEventUpdate",
  "updateType": "responseCookies",
  "cookies": 0
},
{
  "from": "conn0.netEvent14",
  "type": "networkEventUpdate",
  "updateType": "responseContent",
  "mimeType": "text/css",
  "contentSize": 0,
  "discardResponseBody": true
}
</pre>
<p>Actual headers, cookies, and bodies are not sent.</p>
<h4 id="The_getRequestHeaders_and_other_packets">The <code>getRequestHeaders</code> and other packets</h4>
<p>To get more details about a network event you can use the following packet requests (and replies).</p>
<p>The <code>getRequestHeaders</code> packet:</p>
<pre class="brush:json;">
{
  "to": "conn0.netEvent15",
  "type": "getRequestHeaders"
}
{
  "from": "conn0.netEvent15",
  "headers": [
    {
      "name": "Host",
      "value": "localhost"
    }, ...
  ],
  "headersSize": 350
}
</pre>
<p>The <code>getRequestCookies</code> packet:</p>
<pre class="brush:json;">
{
  "to": "conn0.netEvent15",
  "type": "getRequestCookies"
}
{
  "from": "conn0.netEvent15",
  "cookies": []
}
</pre>
<p>The <code>getResponseHeaders</code> packet:</p>
<pre class="brush:json;">
{
  "to": "conn0.netEvent15",
  "type": "getResponseHeaders"
}
{
  "from": "conn0.netEvent15",
  "headers": [
    {
      "name": "Date",
      "value": "Mon, 17 Sep 2012 20:05:27 GMT"
    }, ...
  ],
  "headersSize": 320
}
</pre>
<p>The <code>getResponseCookies</code> packet:</p>
<pre class="brush:json;">
{
  "to": "conn0.netEvent15",
  "type": "getResponseCookies"
}
{
  "from": "conn0.netEvent15",
  "cookies": []
}
</pre>
<div class="note">
  <p>Starting with Firefox 19: for all of the header and cookie values in the above packets we use <a href="https://wiki.mozilla.org/Remote_Debugging_Protocol#Objects" title="https://wiki.mozilla.org/Remote_Debugging_Protocol#Objects"><code>LongStringActor</code> grips</a> when the value is very long. This helps us avoid using too much of the network bandwidth.</p>
</div>
<p>The <code>getRequestPostData</code> packet:</p>
<pre class="brush:json;">
{
  "to": "conn0.netEvent15",
  "type": "getRequestPostData"
}
{
  "from": "conn0.netEvent15",
  "postData": { text: "foobar" },
  "postDataDiscarded": false
}</pre>
<p>The <code>getResponseContent</code> packet:</p>
<pre class="brush:json;">
{
  "to": "conn0.netEvent15",
  "type": "getResponseContent"
}
{
  "from": "conn0.netEvent15",
  "content": {
    "mimeType": "text/css",
    "text": "\n@import \"test.css\";\n\n.foobar { color: green }\n\n"
  },
  "contentDiscarded": false
}
</pre>
<p>The request and response content text value is most commonly sent using a <code>LongStringActor</code> grip. For very short request/response bodies we send the raw text.</p>
<div class="note">
  <p>Starting with Firefox 19: for non-text response types we send the content in base64 encoding (again, most likely a <code>LongStringActor</code> grip). To tell the difference just check if <code>response.content.encoding == "base64"</code>.</p>
</div>
<p>The <code>getEventTimings</code> packet:</p>
<pre class="brush:json;">
{
  "to": "conn0.netEvent15",
  "type": "getEventTimings"
}
{
  "from": "conn0.netEvent15",
  "timings": {
    "blocked": 0,
    "dns": 0,
    "connect": 0,
    "send": 0,
    "wait": 16,
    "receive": 0
  },
  "totalTime": 16
}
</pre>
<h3 id="The_fileActivity_packet">The <code>fileActivity</code> packet</h3>
<p>When a file load is observed the following <code>fileActivity</code> packet is sent to the client:</p>
<pre class="brush:json;">
{
  "from": "conn0.console9",
  "type": "fileActivity",
  "uri": "file:///home/mihai/public_html/mozilla/test2.css"
}
</pre>
<h2 id="History">History</h2>
<p>Protocol changes by Firefox version:</p>
<ul>
  <li>Firefox 18: initial version.</li>
  <li>Firefox 19: <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=787981" title="https://bugzilla.mozilla.org/show_bug.cgi?id=787981">bug 787981</a> - added <code>LongStringActor</code> usage in several places.</li>
  <li>Firefox 20: <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=792062" title="https://bugzilla.mozilla.org/show_bug.cgi?id=792062">bug 792062</a> - removed <code>locationChanged</code> packet and updated the <code>tabNavigated</code> packet for tab actors.</li>
  <li>Firefox 23: <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=783499" title="https://bugzilla.mozilla.org/show_bug.cgi?id=783499">bug 783499</a> - removed the <code>WebConsoleObjectActor</code>. Now the Web Console uses the JavaScript debugger API and the <code>ObjectActor</code>.</li>
  <li>Firefox 23: added the <code>bindObjectActor</code> and <code>frameActor</code> options to the <code>evaluateJS</code> request packet.</li>
  <li>Firefox 24: new <code>private</code> flags for the console actor notifications, <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=874061">bug 874061</a>. Also added the <code>lastPrivateContextExited</code> notification for the global console actor.</li>
  <li>Firefox 24: new <code>isXHR</code> flag for the <code>networkEvent</code> notification, <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=859046">bug 859046</a>.</li>
  <li>Firefox 24: removed the <code>message</code> property from the <code>pageError</code> packet notification, <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=877773">bug 877773</a>. The <code>lineText</code> and <code>errorMessage</code> properties can be long string actors now.</li>
  <li>Firefox 25: added the <code>url</code> option to the <code>evaluateJS</code> request packet.</li>
  <li>Firefox 25: added the <code>getPreferences</code> and <code>sendHTTPRequest</code> request packets to the console actor, <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=886067" title="Bug 886067 - Netmonitor displays request sizes as '0 KB' after opening Console">bug 886067</a> and <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=731311" title="Bug 731311 - Network monitor should allow to replay and edit requests">bug 731311</a>.</li>
</ul>
<h2 id="Conclusions">Conclusions</h2>
<p>As of this writing, this document is a dense summary of the work we did in <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=768096" title="https://bugzilla.mozilla.org/show_bug.cgi?id=768096">bug 768096</a> and subsequent changes. We try to keep this document up-to-date. We hope this is helpful for you.</p>
<p>If you make changes to the Web Console server please update this document. Thank you!</p>
Revert to this revision