2013-03-11 23:50:42 -07:00
|
|
|
/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ft=javascript 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";
|
|
|
|
|
2013-05-13 00:01:00 -07:00
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
|
2013-03-11 23:50:42 -07:00
|
|
|
|
2013-08-14 20:45:34 -07:00
|
|
|
const NET_STRINGS_URI = "chrome://browser/locale/devtools/netmonitor.properties";
|
|
|
|
const LISTENERS = [ "NetworkActivity" ];
|
|
|
|
const NET_PREFS = { "NetworkMonitor.saveRequestAndResponseBodies": true };
|
|
|
|
|
|
|
|
// The panel's window global is an EventEmitter firing the following events:
|
|
|
|
const EVENTS = {
|
|
|
|
// When the monitored target begins and finishes navigating.
|
|
|
|
TARGET_WILL_NAVIGATE: "NetMonitor:TargetWillNavigate",
|
|
|
|
TARGET_DID_NAVIGATE: "NetMonitor:TargetNavigate",
|
|
|
|
|
|
|
|
// When a network event is received.
|
|
|
|
// See https://developer.mozilla.org/docs/Tools/Web_Console/remoting for
|
|
|
|
// more information about what each packet is supposed to deliver.
|
|
|
|
NETWORK_EVENT: "NetMonitor:NetworkEvent",
|
|
|
|
|
|
|
|
// When request headers begin and finish receiving.
|
|
|
|
UPDATING_REQUEST_HEADERS: "NetMonitor:NetworkEventUpdating:RequestHeaders",
|
|
|
|
RECEIVED_REQUEST_HEADERS: "NetMonitor:NetworkEventUpdated:RequestHeaders",
|
|
|
|
|
|
|
|
// When request cookies begin and finish receiving.
|
|
|
|
UPDATING_REQUEST_COOKIES: "NetMonitor:NetworkEventUpdating:RequestCookies",
|
|
|
|
RECEIVED_REQUEST_COOKIES: "NetMonitor:NetworkEventUpdated:RequestCookies",
|
|
|
|
|
|
|
|
// When request post data begins and finishes receiving.
|
|
|
|
UPDATING_REQUEST_POST_DATA: "NetMonitor:NetworkEventUpdating:RequestPostData",
|
|
|
|
RECEIVED_REQUEST_POST_DATA: "NetMonitor:NetworkEventUpdated:RequestPostData",
|
|
|
|
|
|
|
|
// When response headers begin and finish receiving.
|
|
|
|
UPDATING_RESPONSE_HEADERS: "NetMonitor:NetworkEventUpdating:ResponseHeaders",
|
|
|
|
RECEIVED_RESPONSE_HEADERS: "NetMonitor:NetworkEventUpdated:ResponseHeaders",
|
|
|
|
|
|
|
|
// When response cookies begin and finish receiving.
|
|
|
|
UPDATING_RESPONSE_COOKIES: "NetMonitor:NetworkEventUpdating:ResponseCookies",
|
|
|
|
RECEIVED_RESPONSE_COOKIES: "NetMonitor:NetworkEventUpdated:ResponseCookies",
|
|
|
|
|
|
|
|
// When event timings begin and finish receiving.
|
|
|
|
UPDATING_EVENT_TIMINGS: "NetMonitor:NetworkEventUpdating:EventTimings",
|
|
|
|
RECEIVED_EVENT_TIMINGS: "NetMonitor:NetworkEventUpdated:EventTimings",
|
|
|
|
|
|
|
|
// When response content begins, updates and finishes receiving.
|
|
|
|
STARTED_RECEIVING_RESPONSE: "NetMonitor:NetworkEventUpdating:ResponseStart",
|
|
|
|
UPDATING_RESPONSE_CONTENT: "NetMonitor:NetworkEventUpdating:ResponseContent",
|
|
|
|
RECEIVED_RESPONSE_CONTENT: "NetMonitor:NetworkEventUpdated:ResponseContent",
|
|
|
|
|
|
|
|
// When the request post params are displayed in the UI.
|
|
|
|
REQUEST_POST_PARAMS_DISPLAYED: "NetMonitor:RequestPostParamsAvailable",
|
|
|
|
|
|
|
|
// When the response body is displayed in the UI.
|
2013-12-02 08:34:47 -08:00
|
|
|
RESPONSE_BODY_DISPLAYED: "NetMonitor:ResponseBodyAvailable",
|
|
|
|
|
2014-02-13 07:26:09 -08:00
|
|
|
// When the html response preview is displayed in the UI.
|
|
|
|
RESPONSE_HTML_PREVIEW_DISPLAYED: "NetMonitor:ResponseHtmlPreviewAvailable",
|
|
|
|
|
2014-02-14 11:22:06 -08:00
|
|
|
// When the image response thumbnail is displayed in the UI.
|
|
|
|
RESPONSE_IMAGE_THUMBNAIL_DISPLAYED: "NetMonitor:ResponseImageThumbnailAvailable",
|
|
|
|
|
|
|
|
// When a tab is selected in the NetworkDetailsView and subsequently rendered.
|
2013-12-02 08:34:47 -08:00
|
|
|
TAB_UPDATED: "NetMonitor:TabUpdated",
|
|
|
|
|
2014-01-31 22:37:53 -08:00
|
|
|
// Fired when Sidebar has finished being populated.
|
2013-12-02 08:34:47 -08:00
|
|
|
SIDEBAR_POPULATED: "NetMonitor:SidebarPopulated",
|
|
|
|
|
2014-01-31 22:37:53 -08:00
|
|
|
// Fired when NetworkDetailsView has finished being populated.
|
2013-12-02 08:34:47 -08:00
|
|
|
NETWORKDETAILSVIEW_POPULATED: "NetMonitor:NetworkDetailsViewPopulated",
|
|
|
|
|
2014-01-31 22:37:53 -08:00
|
|
|
// Fired when CustomRequestView has finished being populated.
|
|
|
|
CUSTOMREQUESTVIEW_POPULATED: "NetMonitor:CustomRequestViewPopulated",
|
|
|
|
|
|
|
|
// Fired when charts have been displayed in the PerformanceStatisticsView.
|
|
|
|
PLACEHOLDER_CHARTS_DISPLAYED: "NetMonitor:PlaceholderChartsDisplayed",
|
|
|
|
PRIMED_CACHE_CHART_DISPLAYED: "NetMonitor:PrimedChartsDisplayed",
|
2014-03-07 04:14:53 -08:00
|
|
|
EMPTY_CACHE_CHART_DISPLAYED: "NetMonitor:EmptyChartsDisplayed",
|
|
|
|
|
|
|
|
// Fired once the NetMonitorController establishes a connection to the debug
|
|
|
|
// target.
|
|
|
|
CONNECTED: "connected",
|
2014-01-31 22:37:53 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Descriptions for what this frontend is currently doing.
|
|
|
|
const ACTIVITY_TYPE = {
|
|
|
|
// Standing by and handling requests normally.
|
|
|
|
NONE: 0,
|
|
|
|
|
|
|
|
// Forcing the target to reload with cache enabled or disabled.
|
|
|
|
RELOAD: {
|
|
|
|
WITH_CACHE_ENABLED: 1,
|
|
|
|
WITH_CACHE_DISABLED: 2
|
|
|
|
},
|
|
|
|
|
|
|
|
// Enabling or disabling the cache without triggering a reload.
|
|
|
|
ENABLE_CACHE: 3,
|
|
|
|
DISABLE_CACHE: 4
|
2013-12-02 08:34:47 -08:00
|
|
|
};
|
2013-08-14 20:45:34 -07:00
|
|
|
|
2013-03-11 23:50:42 -07:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource:///modules/devtools/SideMenuWidget.jsm");
|
|
|
|
Cu.import("resource:///modules/devtools/VariablesView.jsm");
|
2013-10-01 13:17:20 -07:00
|
|
|
Cu.import("resource:///modules/devtools/VariablesViewController.jsm");
|
2013-03-11 23:50:42 -07:00
|
|
|
Cu.import("resource:///modules/devtools/ViewHelpers.jsm");
|
|
|
|
|
2013-10-24 10:08:17 -07:00
|
|
|
const require = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}).devtools.require;
|
2014-01-31 22:37:53 -08:00
|
|
|
const promise = Cu.import("resource://gre/modules/Promise.jsm", {}).Promise;
|
2014-02-25 20:22:05 -08:00
|
|
|
const EventEmitter = require("devtools/toolkit/event-emitter");
|
2013-10-24 10:08:17 -07:00
|
|
|
const Editor = require("devtools/sourceeditor/editor");
|
2014-02-14 11:22:06 -08:00
|
|
|
const {Tooltip} = require("devtools/shared/widgets/Tooltip");
|
2013-10-24 10:08:17 -07:00
|
|
|
|
2014-01-31 22:37:53 -08:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Chart",
|
|
|
|
"resource:///modules/devtools/Chart.jsm");
|
|
|
|
|
2014-03-29 10:01:37 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Curl",
|
|
|
|
"resource:///modules/devtools/Curl.jsm");
|
|
|
|
|
2014-01-31 22:37:53 -08:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Task",
|
|
|
|
"resource://gre/modules/Task.jsm");
|
|
|
|
|
2013-05-30 11:46:36 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "PluralForm",
|
|
|
|
"resource://gre/modules/PluralForm.jsm");
|
|
|
|
|
2014-01-31 22:37:53 -08:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "DevToolsUtils",
|
|
|
|
"resource://gre/modules/devtools/DevToolsUtils.jsm");
|
|
|
|
|
2014-03-29 10:01:37 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "clipboardHelper",
|
|
|
|
"@mozilla.org/widget/clipboardhelper;1", "nsIClipboardHelper");
|
2013-08-03 03:29:48 -07:00
|
|
|
|
|
|
|
Object.defineProperty(this, "NetworkHelper", {
|
|
|
|
get: function() {
|
2014-03-29 10:01:37 -07:00
|
|
|
return require("devtools/toolkit/webconsole/network-helper");
|
2013-08-03 03:29:48 -07:00
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true
|
|
|
|
});
|
2013-03-11 23:50:42 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Object defining the network monitor controller components.
|
|
|
|
*/
|
|
|
|
let NetMonitorController = {
|
|
|
|
/**
|
|
|
|
* Initializes the view.
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
* A promise that is resolved when the monitor finishes startup.
|
|
|
|
*/
|
|
|
|
startupNetMonitor: function() {
|
2013-07-10 01:40:51 -07:00
|
|
|
if (this._startup) {
|
|
|
|
return this._startup;
|
2013-03-11 23:50:42 -07:00
|
|
|
}
|
|
|
|
|
2013-07-10 01:40:51 -07:00
|
|
|
NetMonitorView.initialize();
|
2013-03-11 23:50:42 -07:00
|
|
|
|
2013-07-10 01:40:51 -07:00
|
|
|
// Startup is synchronous, for now.
|
2013-07-11 00:12:20 -07:00
|
|
|
return this._startup = promise.resolve();
|
2013-03-11 23:50:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroys the view and disconnects the monitor client from the server.
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
* A promise that is resolved when the monitor finishes shutdown.
|
|
|
|
*/
|
|
|
|
shutdownNetMonitor: function() {
|
2013-07-10 01:40:51 -07:00
|
|
|
if (this._shutdown) {
|
|
|
|
return this._shutdown;
|
2013-03-11 23:50:42 -07:00
|
|
|
}
|
|
|
|
|
2013-07-10 01:40:51 -07:00
|
|
|
NetMonitorView.destroy();
|
|
|
|
this.TargetEventsHandler.disconnect();
|
|
|
|
this.NetworkEventsHandler.disconnect();
|
|
|
|
this.disconnect();
|
2013-03-11 23:50:42 -07:00
|
|
|
|
2013-07-10 01:40:51 -07:00
|
|
|
// Shutdown is synchronous, for now.
|
2013-07-11 00:12:20 -07:00
|
|
|
return this._shutdown = promise.resolve();
|
2013-03-11 23:50:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiates remote or chrome network monitoring based on the current target,
|
|
|
|
* wiring event handlers as necessary.
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
* A promise that is resolved when the monitor finishes connecting.
|
|
|
|
*/
|
|
|
|
connect: function() {
|
|
|
|
if (this._connection) {
|
2013-07-10 01:40:51 -07:00
|
|
|
return this._connection;
|
2013-03-11 23:50:42 -07:00
|
|
|
}
|
|
|
|
|
2013-07-11 00:12:20 -07:00
|
|
|
let deferred = promise.defer();
|
2013-07-10 01:40:51 -07:00
|
|
|
this._connection = deferred.promise;
|
2013-03-11 23:50:42 -07:00
|
|
|
|
|
|
|
let target = this._target;
|
|
|
|
let { client, form } = target;
|
|
|
|
if (target.chrome) {
|
|
|
|
this._startChromeMonitoring(client, form.consoleActor, deferred.resolve);
|
|
|
|
} else {
|
|
|
|
this._startMonitoringTab(client, form, deferred.resolve);
|
|
|
|
}
|
|
|
|
|
2014-03-07 04:14:53 -08:00
|
|
|
return deferred.promise.then((result) => {
|
|
|
|
window.emit(EVENTS.CONNECTED);
|
|
|
|
return result;
|
|
|
|
});
|
2013-03-11 23:50:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnects the debugger client and removes event handlers as necessary.
|
|
|
|
*/
|
|
|
|
disconnect: function() {
|
|
|
|
// When debugging local or a remote instance, the connection is closed by
|
|
|
|
// the RemoteTarget.
|
|
|
|
this._connection = null;
|
|
|
|
this.client = null;
|
|
|
|
this.tabClient = null;
|
|
|
|
this.webConsoleClient = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up a monitoring session.
|
|
|
|
*
|
|
|
|
* @param DebuggerClient aClient
|
|
|
|
* The debugger client.
|
|
|
|
* @param object aTabGrip
|
|
|
|
* The remote protocol grip of the tab.
|
|
|
|
* @param function aCallback
|
|
|
|
* A function to invoke once the client attached to the console client.
|
|
|
|
*/
|
|
|
|
_startMonitoringTab: function(aClient, aTabGrip, aCallback) {
|
|
|
|
if (!aClient) {
|
|
|
|
Cu.reportError("No client found!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.client = aClient;
|
|
|
|
|
|
|
|
aClient.attachTab(aTabGrip.actor, (aResponse, aTabClient) => {
|
|
|
|
if (!aTabClient) {
|
|
|
|
Cu.reportError("No tab client found!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.tabClient = aTabClient;
|
|
|
|
|
|
|
|
aClient.attachConsole(aTabGrip.consoleActor, LISTENERS, (aResponse, aWebConsoleClient) => {
|
|
|
|
if (!aWebConsoleClient) {
|
|
|
|
Cu.reportError("Couldn't attach to console: " + aResponse.error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.webConsoleClient = aWebConsoleClient;
|
|
|
|
this.webConsoleClient.setPreferences(NET_PREFS, () => {
|
|
|
|
this.TargetEventsHandler.connect();
|
|
|
|
this.NetworkEventsHandler.connect();
|
|
|
|
|
|
|
|
if (aCallback) {
|
|
|
|
aCallback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up a chrome monitoring session.
|
|
|
|
*
|
|
|
|
* @param DebuggerClient aClient
|
|
|
|
* The debugger client.
|
|
|
|
* @param object aConsoleActor
|
|
|
|
* The remote protocol grip of the chrome debugger.
|
|
|
|
* @param function aCallback
|
|
|
|
* A function to invoke once the client attached to the console client.
|
|
|
|
*/
|
|
|
|
_startChromeMonitoring: function(aClient, aConsoleActor, aCallback) {
|
|
|
|
if (!aClient) {
|
|
|
|
Cu.reportError("No client found!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.client = aClient;
|
|
|
|
|
|
|
|
aClient.attachConsole(aConsoleActor, LISTENERS, (aResponse, aWebConsoleClient) => {
|
|
|
|
if (!aWebConsoleClient) {
|
|
|
|
Cu.reportError("Couldn't attach to console: " + aResponse.error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.webConsoleClient = aWebConsoleClient;
|
|
|
|
this.webConsoleClient.setPreferences(NET_PREFS, () => {
|
|
|
|
this.TargetEventsHandler.connect();
|
|
|
|
this.NetworkEventsHandler.connect();
|
|
|
|
|
|
|
|
if (aCallback) {
|
|
|
|
aCallback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-01-31 22:37:53 -08:00
|
|
|
/**
|
|
|
|
* Gets the activity currently performed by the frontend.
|
|
|
|
* @return number
|
|
|
|
*/
|
|
|
|
getCurrentActivity: function() {
|
|
|
|
return this._currentActivity || ACTIVITY_TYPE.NONE;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Triggers a specific "activity" to be performed by the frontend. This can be,
|
|
|
|
* for example, triggering reloads or enabling/disabling cache.
|
|
|
|
*
|
|
|
|
* @param number aType
|
|
|
|
* The activity type. See the ACTIVITY_TYPE const.
|
|
|
|
* @return object
|
|
|
|
* A promise resolved once the activity finishes and the frontend
|
|
|
|
* is back into "standby" mode.
|
|
|
|
*/
|
|
|
|
triggerActivity: function(aType) {
|
|
|
|
// Puts the frontend into "standby" (when there's no particular activity).
|
|
|
|
let standBy = () => {
|
|
|
|
this._currentActivity = ACTIVITY_TYPE.NONE;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Waits for a series of "navigation start" and "navigation stop" events.
|
|
|
|
let waitForNavigation = () => {
|
|
|
|
let deferred = promise.defer();
|
|
|
|
this._target.once("will-navigate", () => {
|
|
|
|
this._target.once("navigate", () => {
|
|
|
|
deferred.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Reconfigures the tab, optionally triggering a reload.
|
|
|
|
let reconfigureTab = aOptions => {
|
|
|
|
let deferred = promise.defer();
|
|
|
|
this._target.activeTab.reconfigure(aOptions, deferred.resolve);
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Reconfigures the tab and waits for the target to finish navigating.
|
|
|
|
let reconfigureTabAndWaitForNavigation = aOptions => {
|
|
|
|
aOptions.performReload = true;
|
|
|
|
let navigationFinished = waitForNavigation();
|
|
|
|
return reconfigureTab(aOptions).then(() => navigationFinished);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aType == ACTIVITY_TYPE.RELOAD.WITH_CACHE_ENABLED) {
|
|
|
|
this._currentActivity = ACTIVITY_TYPE.ENABLE_CACHE;
|
|
|
|
this._target.once("will-navigate", () => this._currentActivity = aType);
|
|
|
|
return reconfigureTabAndWaitForNavigation({ cacheEnabled: true }).then(standBy);
|
|
|
|
}
|
|
|
|
if (aType == ACTIVITY_TYPE.RELOAD.WITH_CACHE_DISABLED) {
|
|
|
|
this._currentActivity = ACTIVITY_TYPE.DISABLE_CACHE;
|
|
|
|
this._target.once("will-navigate", () => this._currentActivity = aType);
|
|
|
|
return reconfigureTabAndWaitForNavigation({ cacheEnabled: false }).then(standBy);
|
|
|
|
}
|
|
|
|
if (aType == ACTIVITY_TYPE.ENABLE_CACHE) {
|
|
|
|
this._currentActivity = aType;
|
|
|
|
return reconfigureTab({ cacheEnabled: true, performReload: false }).then(standBy);
|
|
|
|
}
|
|
|
|
if (aType == ACTIVITY_TYPE.DISABLE_CACHE) {
|
|
|
|
this._currentActivity = aType;
|
|
|
|
return reconfigureTab({ cacheEnabled: false, performReload: false }).then(standBy);
|
|
|
|
}
|
|
|
|
this._currentActivity = ACTIVITY_TYPE.NONE;
|
|
|
|
return promise.reject(new Error("Invalid activity type"));
|
|
|
|
},
|
|
|
|
|
2014-03-07 04:14:53 -08:00
|
|
|
/**
|
|
|
|
* Getter that tells if the server supports sending custom network requests.
|
|
|
|
* @type boolean
|
|
|
|
*/
|
|
|
|
get supportsCustomRequest() {
|
|
|
|
return this.webConsoleClient &&
|
|
|
|
(this.webConsoleClient.traits.customNetworkRequest ||
|
|
|
|
!this._target.isApp);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter that tells if the server can do network performance statistics.
|
|
|
|
* @type boolean
|
|
|
|
*/
|
|
|
|
get supportsPerfStats() {
|
|
|
|
return this.tabClient &&
|
|
|
|
(this.tabClient.traits.reconfigure || !this._target.isApp);
|
|
|
|
},
|
|
|
|
|
2013-03-11 23:50:42 -07:00
|
|
|
_startup: null,
|
|
|
|
_shutdown: null,
|
|
|
|
_connection: null,
|
2014-01-31 22:37:53 -08:00
|
|
|
_currentActivity: null,
|
2013-03-11 23:50:42 -07:00
|
|
|
client: null,
|
|
|
|
tabClient: null,
|
|
|
|
webConsoleClient: null
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Functions handling target-related lifetime events.
|
|
|
|
*/
|
|
|
|
function TargetEventsHandler() {
|
|
|
|
this._onTabNavigated = this._onTabNavigated.bind(this);
|
|
|
|
this._onTabDetached = this._onTabDetached.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
TargetEventsHandler.prototype = {
|
|
|
|
get target() NetMonitorController._target,
|
|
|
|
get webConsoleClient() NetMonitorController.webConsoleClient,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen for events emitted by the current tab target.
|
|
|
|
*/
|
|
|
|
connect: function() {
|
|
|
|
dumpn("TargetEventsHandler is connecting...");
|
|
|
|
this.target.on("close", this._onTabDetached);
|
|
|
|
this.target.on("navigate", this._onTabNavigated);
|
|
|
|
this.target.on("will-navigate", this._onTabNavigated);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove events emitted by the current tab target.
|
|
|
|
*/
|
|
|
|
disconnect: function() {
|
|
|
|
if (!this.target) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dumpn("TargetEventsHandler is disconnecting...");
|
|
|
|
this.target.off("close", this._onTabDetached);
|
|
|
|
this.target.off("navigate", this._onTabNavigated);
|
|
|
|
this.target.off("will-navigate", this._onTabNavigated);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called for each location change in the monitored tab.
|
|
|
|
*
|
|
|
|
* @param string aType
|
|
|
|
* Packet type.
|
|
|
|
* @param object aPacket
|
|
|
|
* Packet received from the server.
|
|
|
|
*/
|
|
|
|
_onTabNavigated: function(aType, aPacket) {
|
2013-05-24 10:53:10 -07:00
|
|
|
switch (aType) {
|
|
|
|
case "will-navigate": {
|
|
|
|
// Reset UI.
|
|
|
|
NetMonitorView.RequestsMenu.reset();
|
2014-02-13 07:26:09 -08:00
|
|
|
NetMonitorView.Sidebar.toggle(false);
|
2013-05-24 10:53:10 -07:00
|
|
|
|
2014-01-31 22:37:53 -08:00
|
|
|
// Switch to the default network traffic inspector view.
|
|
|
|
if (NetMonitorController.getCurrentActivity() == ACTIVITY_TYPE.NONE) {
|
|
|
|
NetMonitorView.showNetworkInspectorView();
|
|
|
|
}
|
|
|
|
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.TARGET_WILL_NAVIGATE);
|
2013-05-24 10:53:10 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "navigate": {
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.TARGET_DID_NAVIGATE);
|
2013-05-24 10:53:10 -07:00
|
|
|
break;
|
|
|
|
}
|
2013-03-11 23:50:42 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the monitored tab is closed.
|
|
|
|
*/
|
|
|
|
_onTabDetached: function() {
|
|
|
|
NetMonitorController.shutdownNetMonitor();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Functions handling target network events.
|
|
|
|
*/
|
|
|
|
function NetworkEventsHandler() {
|
|
|
|
this._onNetworkEvent = this._onNetworkEvent.bind(this);
|
|
|
|
this._onNetworkEventUpdate = this._onNetworkEventUpdate.bind(this);
|
|
|
|
this._onRequestHeaders = this._onRequestHeaders.bind(this);
|
|
|
|
this._onRequestCookies = this._onRequestCookies.bind(this);
|
|
|
|
this._onRequestPostData = this._onRequestPostData.bind(this);
|
|
|
|
this._onResponseHeaders = this._onResponseHeaders.bind(this);
|
|
|
|
this._onResponseCookies = this._onResponseCookies.bind(this);
|
|
|
|
this._onResponseContent = this._onResponseContent.bind(this);
|
|
|
|
this._onEventTimings = this._onEventTimings.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkEventsHandler.prototype = {
|
|
|
|
get client() NetMonitorController._target.client,
|
|
|
|
get webConsoleClient() NetMonitorController.webConsoleClient,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to the current target client.
|
|
|
|
*/
|
|
|
|
connect: function() {
|
|
|
|
dumpn("NetworkEventsHandler is connecting...");
|
|
|
|
this.client.addListener("networkEvent", this._onNetworkEvent);
|
|
|
|
this.client.addListener("networkEventUpdate", this._onNetworkEventUpdate);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect from the client.
|
|
|
|
*/
|
|
|
|
disconnect: function() {
|
|
|
|
if (!this.client) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dumpn("NetworkEventsHandler is disconnecting...");
|
|
|
|
this.client.removeListener("networkEvent", this._onNetworkEvent);
|
|
|
|
this.client.removeListener("networkEventUpdate", this._onNetworkEventUpdate);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The "networkEvent" message type handler.
|
|
|
|
*
|
|
|
|
* @param string aType
|
|
|
|
* Message type.
|
|
|
|
* @param object aPacket
|
|
|
|
* The message received from the server.
|
|
|
|
*/
|
|
|
|
_onNetworkEvent: function(aType, aPacket) {
|
2014-03-07 04:14:53 -08:00
|
|
|
if (aPacket.from != this.webConsoleClient.actor) {
|
|
|
|
// Skip events from different console actors.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-24 10:53:10 -07:00
|
|
|
let { actor, startedDateTime, method, url, isXHR } = aPacket.eventActor;
|
|
|
|
NetMonitorView.RequestsMenu.addRequest(actor, startedDateTime, method, url, isXHR);
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.NETWORK_EVENT);
|
2013-03-11 23:50:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The "networkEventUpdate" message type handler.
|
|
|
|
*
|
|
|
|
* @param string aType
|
|
|
|
* Message type.
|
|
|
|
* @param object aPacket
|
|
|
|
* The message received from the server.
|
|
|
|
*/
|
|
|
|
_onNetworkEventUpdate: function(aType, aPacket) {
|
|
|
|
let actor = aPacket.from;
|
2014-03-07 04:14:53 -08:00
|
|
|
if (!NetMonitorView.RequestsMenu.getItemByValue(actor)) {
|
|
|
|
// Skip events from unknown actors.
|
|
|
|
return;
|
|
|
|
}
|
2013-03-11 23:50:42 -07:00
|
|
|
|
|
|
|
switch (aPacket.updateType) {
|
|
|
|
case "requestHeaders":
|
|
|
|
this.webConsoleClient.getRequestHeaders(actor, this._onRequestHeaders);
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.UPDATING_REQUEST_HEADERS);
|
2013-03-11 23:50:42 -07:00
|
|
|
break;
|
|
|
|
case "requestCookies":
|
|
|
|
this.webConsoleClient.getRequestCookies(actor, this._onRequestCookies);
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.UPDATING_REQUEST_COOKIES);
|
2013-03-11 23:50:42 -07:00
|
|
|
break;
|
|
|
|
case "requestPostData":
|
|
|
|
this.webConsoleClient.getRequestPostData(actor, this._onRequestPostData);
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.UPDATING_REQUEST_POST_DATA);
|
2013-03-11 23:50:42 -07:00
|
|
|
break;
|
|
|
|
case "responseHeaders":
|
|
|
|
this.webConsoleClient.getResponseHeaders(actor, this._onResponseHeaders);
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.UPDATING_RESPONSE_HEADERS);
|
2013-03-11 23:50:42 -07:00
|
|
|
break;
|
|
|
|
case "responseCookies":
|
|
|
|
this.webConsoleClient.getResponseCookies(actor, this._onResponseCookies);
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.UPDATING_RESPONSE_COOKIES);
|
2013-03-11 23:50:42 -07:00
|
|
|
break;
|
|
|
|
case "responseStart":
|
|
|
|
NetMonitorView.RequestsMenu.updateRequest(aPacket.from, {
|
|
|
|
httpVersion: aPacket.response.httpVersion,
|
|
|
|
status: aPacket.response.status,
|
|
|
|
statusText: aPacket.response.statusText,
|
|
|
|
headersSize: aPacket.response.headersSize
|
|
|
|
});
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.STARTED_RECEIVING_RESPONSE);
|
2013-03-11 23:50:42 -07:00
|
|
|
break;
|
|
|
|
case "responseContent":
|
|
|
|
NetMonitorView.RequestsMenu.updateRequest(aPacket.from, {
|
|
|
|
contentSize: aPacket.contentSize,
|
|
|
|
mimeType: aPacket.mimeType
|
|
|
|
});
|
|
|
|
this.webConsoleClient.getResponseContent(actor, this._onResponseContent);
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.UPDATING_RESPONSE_CONTENT);
|
2013-03-11 23:50:42 -07:00
|
|
|
break;
|
|
|
|
case "eventTimings":
|
|
|
|
NetMonitorView.RequestsMenu.updateRequest(aPacket.from, {
|
|
|
|
totalTime: aPacket.totalTime
|
|
|
|
});
|
|
|
|
this.webConsoleClient.getEventTimings(actor, this._onEventTimings);
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.UPDATING_EVENT_TIMINGS);
|
2013-03-11 23:50:42 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles additional information received for a "requestHeaders" packet.
|
|
|
|
*
|
|
|
|
* @param object aResponse
|
|
|
|
* The message received from the server.
|
|
|
|
*/
|
|
|
|
_onRequestHeaders: function(aResponse) {
|
|
|
|
NetMonitorView.RequestsMenu.updateRequest(aResponse.from, {
|
|
|
|
requestHeaders: aResponse
|
|
|
|
});
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.RECEIVED_REQUEST_HEADERS);
|
2013-03-11 23:50:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles additional information received for a "requestCookies" packet.
|
|
|
|
*
|
|
|
|
* @param object aResponse
|
|
|
|
* The message received from the server.
|
|
|
|
*/
|
|
|
|
_onRequestCookies: function(aResponse) {
|
|
|
|
NetMonitorView.RequestsMenu.updateRequest(aResponse.from, {
|
|
|
|
requestCookies: aResponse
|
|
|
|
});
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.RECEIVED_REQUEST_COOKIES);
|
2013-03-11 23:50:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles additional information received for a "requestPostData" packet.
|
|
|
|
*
|
|
|
|
* @param object aResponse
|
|
|
|
* The message received from the server.
|
|
|
|
*/
|
|
|
|
_onRequestPostData: function(aResponse) {
|
|
|
|
NetMonitorView.RequestsMenu.updateRequest(aResponse.from, {
|
|
|
|
requestPostData: aResponse
|
|
|
|
});
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.RECEIVED_REQUEST_POST_DATA);
|
2013-03-11 23:50:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles additional information received for a "responseHeaders" packet.
|
|
|
|
*
|
|
|
|
* @param object aResponse
|
|
|
|
* The message received from the server.
|
|
|
|
*/
|
|
|
|
_onResponseHeaders: function(aResponse) {
|
|
|
|
NetMonitorView.RequestsMenu.updateRequest(aResponse.from, {
|
|
|
|
responseHeaders: aResponse
|
|
|
|
});
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.RECEIVED_RESPONSE_HEADERS);
|
2013-03-11 23:50:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles additional information received for a "responseCookies" packet.
|
|
|
|
*
|
|
|
|
* @param object aResponse
|
|
|
|
* The message received from the server.
|
|
|
|
*/
|
|
|
|
_onResponseCookies: function(aResponse) {
|
|
|
|
NetMonitorView.RequestsMenu.updateRequest(aResponse.from, {
|
|
|
|
responseCookies: aResponse
|
|
|
|
});
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.RECEIVED_RESPONSE_COOKIES);
|
2013-03-11 23:50:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles additional information received for a "responseContent" packet.
|
|
|
|
*
|
|
|
|
* @param object aResponse
|
|
|
|
* The message received from the server.
|
|
|
|
*/
|
|
|
|
_onResponseContent: function(aResponse) {
|
|
|
|
NetMonitorView.RequestsMenu.updateRequest(aResponse.from, {
|
|
|
|
responseContent: aResponse
|
|
|
|
});
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.RECEIVED_RESPONSE_CONTENT);
|
2013-03-11 23:50:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles additional information received for a "eventTimings" packet.
|
|
|
|
*
|
|
|
|
* @param object aResponse
|
|
|
|
* The message received from the server.
|
|
|
|
*/
|
2013-05-13 00:01:00 -07:00
|
|
|
_onEventTimings: function(aResponse) {
|
2013-03-11 23:50:42 -07:00
|
|
|
NetMonitorView.RequestsMenu.updateRequest(aResponse.from, {
|
|
|
|
eventTimings: aResponse
|
|
|
|
});
|
2013-08-14 20:45:34 -07:00
|
|
|
window.emit(EVENTS.RECEIVED_EVENT_TIMINGS);
|
2013-04-11 11:11:46 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the full text of a LongString.
|
|
|
|
*
|
|
|
|
* @param object | string aStringGrip
|
|
|
|
* The long string grip containing the corresponding actor.
|
|
|
|
* If you pass in a plain string (by accident or because you're lazy),
|
|
|
|
* then a promise of the same string is simply returned.
|
|
|
|
* @return object Promise
|
|
|
|
* A promise that is resolved when the full string contents
|
|
|
|
* are available, or rejected if something goes wrong.
|
|
|
|
*/
|
2013-05-13 00:01:00 -07:00
|
|
|
getString: function(aStringGrip) {
|
2013-04-11 11:11:46 -07:00
|
|
|
// Make sure this is a long string.
|
|
|
|
if (typeof aStringGrip != "object" || aStringGrip.type != "longString") {
|
2013-07-11 00:12:20 -07:00
|
|
|
return promise.resolve(aStringGrip); // Go home string, you're drunk.
|
2013-04-11 11:11:46 -07:00
|
|
|
}
|
|
|
|
// Fetch the long string only once.
|
|
|
|
if (aStringGrip._fullText) {
|
|
|
|
return aStringGrip._fullText.promise;
|
|
|
|
}
|
|
|
|
|
2013-07-11 00:12:20 -07:00
|
|
|
let deferred = aStringGrip._fullText = promise.defer();
|
2013-04-11 11:11:46 -07:00
|
|
|
let { actor, initial, length } = aStringGrip;
|
|
|
|
let longStringClient = this.webConsoleClient.longString(aStringGrip);
|
|
|
|
|
2013-07-10 01:40:51 -07:00
|
|
|
longStringClient.substring(initial.length, length, aResponse => {
|
2013-04-11 11:11:46 -07:00
|
|
|
if (aResponse.error) {
|
|
|
|
Cu.reportError(aResponse.error + ": " + aResponse.message);
|
|
|
|
deferred.reject(aResponse);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
deferred.resolve(initial + aResponse.substring);
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
2013-03-11 23:50:42 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Localization convenience methods.
|
|
|
|
*/
|
|
|
|
let L10N = new ViewHelpers.L10N(NET_STRINGS_URI);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcuts for accessing various network monitor preferences.
|
|
|
|
*/
|
|
|
|
let Prefs = new ViewHelpers.Prefs("devtools.netmonitor", {
|
2013-04-22 11:12:17 -07:00
|
|
|
networkDetailsWidth: ["Int", "panes-network-details-width"],
|
2014-01-31 22:37:53 -08:00
|
|
|
networkDetailsHeight: ["Int", "panes-network-details-height"],
|
2014-02-11 06:25:21 -08:00
|
|
|
statistics: ["Bool", "statistics"],
|
|
|
|
filters: ["Json", "filters"]
|
2013-03-11 23:50:42 -07:00
|
|
|
});
|
|
|
|
|
2013-06-05 23:53:09 -07:00
|
|
|
/**
|
|
|
|
* Returns true if this is document is in RTL mode.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
XPCOMUtils.defineLazyGetter(window, "isRTL", function() {
|
|
|
|
return window.getComputedStyle(document.documentElement, null).direction == "rtl";
|
|
|
|
});
|
|
|
|
|
2013-03-11 23:50:42 -07:00
|
|
|
/**
|
|
|
|
* Convenient way of emitting events from the panel window.
|
|
|
|
*/
|
|
|
|
EventEmitter.decorate(this);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Preliminary setup for the NetMonitorController object.
|
|
|
|
*/
|
|
|
|
NetMonitorController.TargetEventsHandler = new TargetEventsHandler();
|
|
|
|
NetMonitorController.NetworkEventsHandler = new NetworkEventsHandler();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export some properties to the global scope for easier access.
|
|
|
|
*/
|
|
|
|
Object.defineProperties(window, {
|
2013-04-11 11:11:46 -07:00
|
|
|
"gNetwork": {
|
|
|
|
get: function() NetMonitorController.NetworkEventsHandler
|
2013-03-11 23:50:42 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-01-31 22:37:53 -08:00
|
|
|
/**
|
|
|
|
* Makes sure certain properties are available on all objects in a data store.
|
|
|
|
*
|
|
|
|
* @param array aDataStore
|
|
|
|
* A list of objects for which to check the availability of properties.
|
|
|
|
* @param array aMandatoryFields
|
|
|
|
* A list of strings representing properties of objects in aDataStore.
|
|
|
|
* @return object
|
|
|
|
* A promise resolved when all objects in aDataStore contain the
|
|
|
|
* properties defined in aMandatoryFields.
|
|
|
|
*/
|
|
|
|
function whenDataAvailable(aDataStore, aMandatoryFields) {
|
|
|
|
let deferred = promise.defer();
|
|
|
|
|
|
|
|
let interval = setInterval(() => {
|
|
|
|
if (aDataStore.every(item => aMandatoryFields.every(field => field in item))) {
|
|
|
|
clearInterval(interval);
|
|
|
|
clearTimeout(timer);
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
}, WDA_DEFAULT_VERIFY_INTERVAL);
|
|
|
|
|
|
|
|
let timer = setTimeout(() => {
|
|
|
|
clearInterval(interval);
|
|
|
|
deferred.reject(new Error("Timed out while waiting for data"));
|
|
|
|
}, WDA_DEFAULT_GIVE_UP_TIMEOUT);
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
const WDA_DEFAULT_VERIFY_INTERVAL = 50; // ms
|
|
|
|
const WDA_DEFAULT_GIVE_UP_TIMEOUT = 2000; // ms
|
|
|
|
|
2013-03-11 23:50:42 -07:00
|
|
|
/**
|
|
|
|
* Helper method for debugging.
|
|
|
|
* @param string
|
|
|
|
*/
|
|
|
|
function dumpn(str) {
|
|
|
|
if (wantLogging) {
|
|
|
|
dump("NET-FRONTEND: " + str + "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let wantLogging = Services.prefs.getBoolPref("devtools.debugger.log");
|