Files
palemoon27/toolkit/devtools/framework/target.js
T
roytam1 a7bc0406ee import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1155006: Fix unified build sensitivities in js/src/jit. r=shu (6e24e1af1)
- Bug 1162766 - Fix more bad implicit constructors in js. r=evilpie (39961b06d)
- Bug 1151606 - Stream atoms instead of raw pointers for native functions in tracked optimizations. (r=djvj) (7641ee9d6)
- pointer style (540728104)
- Bug 1154997 - Deal with self-hosted builtins when stringifying tracked optimization type info. (r=djvj) (92f9a54e6)
- pointer style (45742d820)
- Bug 1154115 - Rewrite the JSAPI profiling API to use a FrameHandle, as to avoid multiple lookups in JitcodeGlobalTable. (r=djvj) (4d202ba9e)
- Bug 1119023 - Timeline in new perf tool should filter out markers, r=jsantell (6fc1a8bbe)
- Bug 1132755 - Allocations tree has a bunch of columns that don't make sense, r=jsantell (1ae9ee7e2)
- Bug 1142744 - Fix tests broken by bug 1132755, r=me (cc495f72d)
- Bug 1133058 - OptionsView button, when clicked, should have an 'open' attribute. r=vp (65a78d896)
- Bug 1132765 - Pass through performance memory options for 'probability' and 'maxLogLength' from the front to the memory actor. r=vp (f9bbbe098)
- Bug 1141817 - Fix yield statement to correctly return memory actor state so that the performance tool can poll for allocations during recording. r=vp (2ddf7d528)
- Bug 1141817 - Followup to fix additional intermittents like bug 1132370, r=vp (eab962f01)
- Bug 1142748 - Use a single configuration for starting/stopping recordings, r=jsantell (0181b319a)
- bit of Bug 879008 - New UI for the sampling Profiler (32c4d0fe8)
- Bug 1123815 - Merge gum into fx-team to enable the Performance++ tool, r=me (84aabbd61)
- Bug 1143933 - Expose raw JIT optimization information in performance front end. r=vp,shu (f68a6df50)
- Bug 1143915 - Allow multiple calls to memory and timeline actor's start methods, to return the local start time from the actor. r=vp (028ac4187)
- Bug 978948 - Add animation generator support for setTimeout in the canvas debugger. r=vp (42d623452)
- Bug 985488 - Allow canvas debugger to time out and stop recording frames. Canvas debugger 'wait' style now matches other media styles. Update labels in canvas debugger to explicitly state that it's waiting for rAF cycles, rather than appearing as if something went wrong. r=vporof (b4670d843)
- Bug 1144163 - Add a rulers highlighter; added unit test. r=pbrosset (5811a67d0)
- Bug 1144163 - Add a rulers highlighter; added highlighter. r=pbrosset (779f88bdd)
- Bug 1144163 - Add a rulers highlighter; added gcli command and button. r=pbrosset (d0d13da51)
- Bug 1110550 - Enable performance overview graphs to rerender and change on devtools theme switch. r=vp (bd91ca7cf)
- Bug 1149630 - Performance graphs should inherit from a common graph and be similarly styled. r=vporof (481c841f1)
- Bug 1150733 - Correctly internationalize jit samples label. r=vporof, r=flod (b5612d1a6)
- Bug 1137518 - FlameGraph's destroy function should be async, r=jsantell (f103e4c15)
- Bug 1137503 - Avoid potential infinite loops in `findOptimalTickInterval` functions, r=jsantell (95df6c04a)
- Bug 1121194 - Support vertical panning for the flamegraph in the new performance tool, r=jsantell (06241b5b2)
- Bug 1121180 - Support dark theme in flamecharts for the performance tool. r=vp (c76abe237)
-  Bug 1059308 - Add Target.isTabActor to tell if the remote tab actor supports attach/detach requests. r=jryans (e03dcef93)
- Bug 1132370 - Wrong State: Expected 'attached', but current state is 'detached', r=jsantell (e884e8db9)
- No Bug - Fix documentation for _startMemory and _stopMemory in performance/modules/front.js, r=me DONTBUILD (d79090b31)
- Bug 1147656 - Remove duplicate profiler defaults from the front end and just use on the server. r=vp (35c015dd0)
- Bug 1046234 - Add more DevTools Telemetry measures (display size etc) r=pbrosset, r=gijs (a235681b4)
- actually package telemetry.js (e8f3a58a4)
- Bug 1077464 - Wire console.profile/profileEnd to the new performance tool. Move most of the recording-model logic from the front end into the PerformanceFront and PerformanceActorConnection so it can manage recordings without the front end being viewed. r=vp,jryans,pbrosset (eef8e18c3)
- Bug 1144363 - Fix this._telemetry is undefined in gDevTools. r=bgrins (ba7d02902)
- init telemetry, missing parts of Bug 866642 (1e70df975)
- do not use sysctl.h on Linux anymore, since it is not provided by recent glibc (b2467d7ce)
- clean up some telemetry issues of histogram, parts of  Bug 974171 (d30c8d0ad)
- move devtools to browser - part 1 (9a856f452)
- Bug 1291423: Explicitly qualify the destructor call that we invoke in Maybe::reset. r=Waldo (944904a7d)
- Bug 1148075 - Dynamically add XUL commands for the debugger frontend. r=vporof (60bc91f8f)
- Bug 1147945 - Let the profiler's buffer size and sample rate be configurable via prefs. r=vp (acebcbdd9)
- Bug 1124326 - Improve packageDir support for Cordova. r=ochameau (4b736580a)
- Bug 1124326 - Support Cordova w/o build file. r=ochameau (d4b50aeae)
- Bug 1134029 - Fix 'Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIURI.host]' timeouts, r=jsantell (18d16a5d0)
- Bug 1147806 - Content frame filtering is confused when profiling FxOS, r=jsantell (b3c62c552)
- Bug 1108843 - Generalize platform data in call tree view when platform data is hidden. r=vporof (354553ed7)
- Bug 1138928 - Display only function name and file, instead of full url, in flame graphs. r=vp (4169689c1)
- Bug 1152605 - Should not show host names for chrome URIs. r=vporof (c6dcf9e78)
- Bug 1147604 - Inverted call trees should list (root) as leaves. r=jsantell (01768267f)
- Bug 1075450 - Disable some Awesomebar actions for private windows r=mak (21d5586e7)
- Bug 1120616 - Part 1: Implement filter styles in rule view r=bgrins (b66ee0282)
- Bug 1120616 - Part 2: Add unit tests for filter styles in rule view r=bgrins (2892503d8)
- Bug 1120616 - Part 3: Adjust the styles in the computed view's filter style search r=bgrins (41f8fae1b)
- Bug 1120616 - Part 4: Add textbox context menu for rule and computed view r=bgrins (ff3f868ad)
- Bug 1120616 - Part 5: Refactor style inspector tests to use synthesizeKeys r=bgrins (41db021d7)
- Bug 1102219 - Part 5: Replace more `String.prototype.contains` with `String.prototype.includes` in chrome code. r=till (86ed03588)
- Bug 1154018 - Check to see that nsIURI's host exists when parsing location for framenodes, and cache failures. r=vp (9494d52e7)
- Bug 1160691 - Optimize FrameUtils.isContent and FrameUtils.parseLocation. (r=jsantell) (09118fd5d)
- Bug 1154115 - Make the performance devtool handle the new profiler JSON format. (r=jsantell,vporof) (e3e5be7a4)
- Bug 1059308 - Make frame selection button to work in browser toolbox. r=jryans,past (30fe6e61e)
- Bug 1059308 - Fix tests to support chrome actor. r=jryans (01cf3926c)
- Bug 1147042 - Rename attachProcess to getProcess. r=ochameau (0393ffb80)
- Bug 1145824 - Profiler actor and performance tools now handle passing in a startTime to filter out SPS profiles on platform rather than client. r=vp,fitzgen (f225116ba)
- Bug 1157718 - Do not use Array.prototype.includes in production code that leaves nightly in performance tool. r=fitzgen (ff06d284e)
- Bug 1140728 - Rename 'Memory' to 'Allocations' in the new performance tool. r=jsantell (f584e720f)
- Bug 1137500 - Always wait for the overview to be rendered in tests after a recording finishes, unless otherwise specified, r=jsantell (59825e179)
- Bug 1137487 - AbstractCanvasGraph's destroy function should be async, r=jsantell (a17ae00b5)
- Bug 1132758 - Performance feature visibility now based on a per recording-basis, dependent on features enabled and server support. r=vp (0d080a7c2)
- Bug 1147035 - Make DeveloperToolbar.jsm use the gBrowser.contentDocumentAsCPOW shortcut. r=past. (251eff125)
- Bug 1151168 - Don't flush profiled threads that are pending deletion on JS shutdown and don't delete expired markers when resetting the profile buffer. (r=djvj) (90721313a)
2020-08-29 08:02:13 +08:00

799 lines
23 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const {Cc, Ci, Cu} = require("chrome");
const {Promise: promise} = require("resource://gre/modules/Promise.jsm");
const EventEmitter = require("devtools/toolkit/event-emitter");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "DebuggerServer",
"resource://gre/modules/devtools/dbg-server.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "DebuggerClient",
"resource://gre/modules/devtools/dbg-client.jsm");
const targets = new WeakMap();
const promiseTargets = new WeakMap();
/**
* Functions for creating Targets
*/
exports.TargetFactory = {
/**
* Construct a Target
* @param {XULTab} tab
* The tab to use in creating a new target.
*
* @return A target object
*/
forTab: function TF_forTab(tab) {
let target = targets.get(tab);
if (target == null) {
target = new TabTarget(tab);
targets.set(tab, target);
}
return target;
},
/**
* Return a promise of a Target for a remote tab.
* @param {Object} options
* The options object has the following properties:
* {
* form: the remote protocol form of a tab,
* client: a DebuggerClient instance
* (caller owns this and is responsible for closing),
* chrome: true if the remote target is the whole process
* }
*
* @return A promise of a target object
*/
forRemoteTab: function TF_forRemoteTab(options) {
let targetPromise = promiseTargets.get(options);
if (targetPromise == null) {
let target = new TabTarget(options);
targetPromise = target.makeRemote().then(() => target);
promiseTargets.set(options, targetPromise);
}
return targetPromise;
},
/**
* Creating a target for a tab that is being closed is a problem because it
* allows a leak as a result of coming after the close event which normally
* clears things up. This function allows us to ask if there is a known
* target for a tab without creating a target
* @return true/false
*/
isKnownTab: function TF_isKnownTab(tab) {
return targets.has(tab);
},
/**
* Construct a Target
* @param {nsIDOMWindow} window
* The chromeWindow to use in creating a new target
* @return A target object
*/
forWindow: function TF_forWindow(window) {
let target = targets.get(window);
if (target == null) {
target = new WindowTarget(window);
targets.set(window, target);
}
return target;
},
/**
* Get all of the targets known to the local browser instance
* @return An array of target objects
*/
allTargets: function TF_allTargets() {
let windows = [];
let wm = Cc["@mozilla.org/appshell/window-mediator;1"]
.getService(Ci.nsIWindowMediator);
let en = wm.getXULWindowEnumerator(null);
while (en.hasMoreElements()) {
windows.push(en.getNext());
}
return windows.map(function(window) {
return TargetFactory.forWindow(window);
});
},
};
/**
* The 'version' property allows the developer tools equivalent of browser
* detection. Browser detection is evil, however while we don't know what we
* will need to detect in the future, it is an easy way to postpone work.
* We should be looking to use the support features added in bug 1069673
* in place of version where possible.
*/
function getVersion() {
// FIXME: return something better
return 20;
}
/**
* A Target represents something that we can debug. Targets are generally
* read-only. Any changes that you wish to make to a target should be done via
* a Tool that attaches to the target. i.e. a Target is just a pointer saying
* "the thing to debug is over there".
*
* Providing a generalized abstraction of a web-page or web-browser (available
* either locally or remotely) is beyond the scope of this class (and maybe
* also beyond the scope of this universe) However Target does attempt to
* abstract some common events and read-only properties common to many Tools.
*
* Supported read-only properties:
* - name, isRemote, url
*
* Target extends EventEmitter and provides support for the following events:
* - close: The target window has been closed. All tools attached to this
* target should close. This event is not currently cancelable.
* - navigate: The target window has navigated to a different URL
*
* Optional events:
* - will-navigate: The target window will navigate to a different URL
* - hidden: The target is not visible anymore (for TargetTab, another tab is selected)
* - visible: The target is visible (for TargetTab, tab is selected)
*
* Comparing Targets: 2 instances of a Target object can point at the same
* thing, so t1 !== t2 and t1 != t2 even when they represent the same object.
* To compare to targets use 't1.equals(t2)'.
*/
function Target() {
throw new Error("Use TargetFactory.newXXX or Target.getXXX to create a Target in place of 'new Target()'");
}
Object.defineProperty(Target.prototype, "version", {
get: getVersion,
enumerable: true
});
/**
* A TabTarget represents a page living in a browser tab. Generally these will
* be web pages served over http(s), but they don't have to be.
*/
function TabTarget(tab) {
EventEmitter.decorate(this);
this.destroy = this.destroy.bind(this);
this._handleThreadState = this._handleThreadState.bind(this);
this.on("thread-resumed", this._handleThreadState);
this.on("thread-paused", this._handleThreadState);
// Only real tabs need initialization here. Placeholder objects for remote
// targets will be initialized after a makeRemote method call.
if (tab && !["client", "form", "chrome"].every(tab.hasOwnProperty, tab)) {
this._tab = tab;
this._setupListeners();
} else {
this._form = tab.form;
this._client = tab.client;
this._chrome = tab.chrome;
}
// Default isTabActor to true if not explicitely specified
this._isTabActor = typeof(tab.isTabActor) == "boolean" ? tab.isTabActor : true;
}
TabTarget.prototype = {
_webProgressListener: null,
/**
* Returns a promise for the protocol description from the root actor.
* Used internally with `target.actorHasMethod`. Takes advantage of
* caching if definition was fetched previously with the corresponding
* actor information. Must be a remote target.
*
* @return {Promise}
* {
* "category": "actor",
* "typeName": "longstractor",
* "methods": [{
* "name": "substring",
* "request": {
* "type": "substring",
* "start": {
* "_arg": 0,
* "type": "primitive"
* },
* "end": {
* "_arg": 1,
* "type": "primitive"
* }
* },
* "response": {
* "substring": {
* "_retval": "primitive"
* }
* }
* }],
* "events": {}
* }
*/
getActorDescription: function (actorName) {
if (!this.client) {
throw new Error("TabTarget#getActorDescription() can only be called on remote tabs.");
}
let deferred = promise.defer();
if (this._protocolDescription && this._protocolDescription.types[actorName]) {
deferred.resolve(this._protocolDescription.types[actorName]);
} else {
this.client.mainRoot.protocolDescription(description => {
this._protocolDescription = description;
deferred.resolve(description.types[actorName]);
});
}
return deferred.promise;
},
/**
* Returns a boolean indicating whether or not the specific actor
* type exists. Must be a remote target.
*
* @param {String} actorName
* @return {Boolean}
*/
hasActor: function (actorName) {
if (!this.client) {
throw new Error("TabTarget#hasActor() can only be called on remote tabs.");
}
if (this.form) {
return !!this.form[actorName + "Actor"];
}
return false;
},
/**
* Queries the protocol description to see if an actor has
* an available method. The actor must already be lazily-loaded,
* so this is for use inside of tool. Returns a promise that
* resolves to a boolean. Must be a remote target.
*
* @param {String} actorName
* @param {String} methodName
* @return {Promise}
*/
actorHasMethod: function (actorName, methodName) {
if (!this.client) {
throw new Error("TabTarget#actorHasMethod() can only be called on remote tabs.");
}
return this.getActorDescription(actorName).then(desc => {
if (desc && desc.methods) {
return !!desc.methods.find(method => method.name === methodName);
}
return false;
});
},
/**
* Returns a trait from the root actor.
*
* @param {String} traitName
* @return {Mixed}
*/
getTrait: function (traitName) {
if (!this.client) {
throw new Error("TabTarget#getTrait() can only be called on remote tabs.");
}
// If the targeted actor exposes traits and has a defined value for this traits,
// override the root actor traits
if (this.form.traits && traitName in this.form.traits) {
return this.form.traits[traitName];
}
return this.client.traits[traitName];
},
get version() { return getVersion(); },
get tab() {
return this._tab;
},
get form() {
return this._form;
},
get root() {
return this._root;
},
get client() {
return this._client;
},
// Tells us if we are debugging content document
// or if we are debugging chrome stuff.
// Allows to controls which features are available against
// a chrome or a content document.
get chrome() {
return this._chrome;
},
// Tells us if the related actor implements TabActor interface
// and requires to call `attach` request before being used
// and `detach` during cleanup
get isTabActor() {
return this._isTabActor;
},
get window() {
// XXX - this is a footgun for e10s - there .contentWindow will be null,
// and even though .contentWindowAsCPOW *might* work, it will not work
// in all contexts. Consumers of .window need to be refactored to not
// rely on this.
if (Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
Cu.reportError("The .window getter on devtools' |target| object isn't e10s friendly!\n"
+ Error().stack);
}
// Be extra careful here, since this may be called by HS_getHudByWindow
// during shutdown.
if (this._tab && this._tab.linkedBrowser) {
return this._tab.linkedBrowser.contentWindow;
}
return null;
},
get name() {
if (this._tab && this._tab.linkedBrowser.contentDocument) {
return this._tab.linkedBrowser.contentDocument.title
} else if (this.isAddon) {
return this._form.name;
} else {
return this._form.title;
}
},
get url() {
return this._tab ? this._tab.linkedBrowser.currentURI.spec :
this._form.url;
},
get isRemote() {
return !this.isLocalTab;
},
get isAddon() {
return !!(this._form && this._form.actor &&
this._form.actor.match(/conn\d+\.addon\d+/));
},
get isLocalTab() {
return !!this._tab;
},
get isMultiProcess() {
return !this.window;
},
get isThreadPaused() {
return !!this._isThreadPaused;
},
/**
* Adds remote protocol capabilities to the target, so that it can be used
* for tools that support the Remote Debugging Protocol even for local
* connections.
*/
makeRemote: function TabTarget_makeRemote() {
if (this._remote) {
return this._remote.promise;
}
this._remote = promise.defer();
if (this.isLocalTab) {
// Since a remote protocol connection will be made, let's start the
// DebuggerServer here, once and for all tools.
if (!DebuggerServer.initialized) {
DebuggerServer.init();
DebuggerServer.addBrowserActors();
}
this._client = new DebuggerClient(DebuggerServer.connectPipe());
// A local TabTarget will never perform chrome debugging.
this._chrome = false;
}
this._setupRemoteListeners();
let attachTab = () => {
this._client.attachTab(this._form.actor, (aResponse, aTabClient) => {
if (!aTabClient) {
this._remote.reject("Unable to attach to the tab");
return;
}
this.activeTab = aTabClient;
this.threadActor = aResponse.threadActor;
this._remote.resolve(null);
});
};
if (this.isLocalTab) {
this._client.connect((aType, aTraits) => {
this._client.listTabs(aResponse => {
this._root = aResponse;
if (this.window) {
let windowUtils = this.window
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
let outerWindow = windowUtils.outerWindowID;
aResponse.tabs.some((tab) => {
if (tab.outerWindowID === outerWindow) {
this._form = tab;
return true;
}
return false;
});
}
if (!this._form) {
this._form = aResponse.tabs[aResponse.selected];
}
attachTab();
});
});
} else if (this.isTabActor) {
// In the remote debugging case, the protocol connection will have been
// already initialized in the connection screen code.
attachTab();
} else {
// AddonActor and chrome debugging on RootActor doesn't inherits from TabActor and
// doesn't need to be attached.
this._remote.resolve(null);
}
return this._remote.promise;
},
/**
* Listen to the different events.
*/
_setupListeners: function TabTarget__setupListeners() {
this._webProgressListener = new TabWebProgressListener(this);
this.tab.linkedBrowser.addProgressListener(this._webProgressListener);
this.tab.addEventListener("TabClose", this);
this.tab.parentNode.addEventListener("TabSelect", this);
this.tab.ownerDocument.defaultView.addEventListener("unload", this);
},
/**
* Teardown event listeners.
*/
_teardownListeners: function TabTarget__teardownListeners() {
if (this._webProgressListener) {
this._webProgressListener.destroy();
}
this._tab.ownerDocument.defaultView.removeEventListener("unload", this);
this._tab.removeEventListener("TabClose", this);
this._tab.parentNode.removeEventListener("TabSelect", this);
},
/**
* Setup listeners for remote debugging, updating existing ones as necessary.
*/
_setupRemoteListeners: function TabTarget__setupRemoteListeners() {
this.client.addListener("closed", this.destroy);
this._onTabDetached = (aType, aPacket) => {
// We have to filter message to ensure that this detach is for this tab
if (aPacket.from == this._form.actor) {
this.destroy();
}
};
this.client.addListener("tabDetached", this._onTabDetached);
this._onTabNavigated = (aType, aPacket) => {
let event = Object.create(null);
event.url = aPacket.url;
event.title = aPacket.title;
event.nativeConsoleAPI = aPacket.nativeConsoleAPI;
event.isFrameSwitching = aPacket.isFrameSwitching;
// Send any stored event payload (DOMWindow or nsIRequest) for backwards
// compatibility with non-remotable tools.
if (aPacket.state == "start") {
event._navPayload = this._navRequest;
this.emit("will-navigate", event);
this._navRequest = null;
} else {
event._navPayload = this._navWindow;
this.emit("navigate", event);
this._navWindow = null;
}
};
this.client.addListener("tabNavigated", this._onTabNavigated);
this._onFrameUpdate = (aType, aPacket) => {
this.emit("frame-update", aPacket);
};
this.client.addListener("frameUpdate", this._onFrameUpdate);
},
/**
* Teardown listeners for remote debugging.
*/
_teardownRemoteListeners: function TabTarget__teardownRemoteListeners() {
this.client.removeListener("closed", this.destroy);
this.client.removeListener("tabNavigated", this._onTabNavigated);
this.client.removeListener("tabDetached", this._onTabDetached);
this.client.removeListener("frameUpdate", this._onFrameUpdate);
},
/**
* Handle tabs events.
*/
handleEvent: function (event) {
switch (event.type) {
case "TabClose":
case "unload":
this.destroy();
break;
case "TabSelect":
if (this.tab.selected) {
this.emit("visible", event);
} else {
this.emit("hidden", event);
}
break;
}
},
/**
* Handle script status.
*/
_handleThreadState: function(event) {
switch (event) {
case "thread-resumed":
this._isThreadPaused = false;
break;
case "thread-paused":
this._isThreadPaused = true;
break;
}
},
/**
* Target is not alive anymore.
*/
destroy: function() {
// If several things call destroy then we give them all the same
// destruction promise so we're sure to destroy only once
if (this._destroyer) {
return this._destroyer.promise;
}
this._destroyer = promise.defer();
// Before taking any action, notify listeners that destruction is imminent.
this.emit("close");
// First of all, do cleanup tasks that pertain to both remoted and
// non-remoted targets.
this.off("thread-resumed", this._handleThreadState);
this.off("thread-paused", this._handleThreadState);
if (this._tab) {
this._teardownListeners();
}
let cleanupAndResolve = () => {
this._cleanup();
this._destroyer.resolve(null);
};
// If this target was not remoted, the promise will be resolved before the
// function returns.
if (this._tab && !this._client) {
cleanupAndResolve();
} else if (this._client) {
// If, on the other hand, this target was remoted, the promise will be
// resolved after the remote connection is closed.
this._teardownRemoteListeners();
if (this.isLocalTab) {
// We started with a local tab and created the client ourselves, so we
// should close it.
this._client.close(cleanupAndResolve);
} else {
// The client was handed to us, so we are not responsible for closing
// it. We just need to detach from the tab, if already attached.
if (this.activeTab) {
// |detach| may fail if the connection is already dead, so proceed
// cleanup directly after this.
this.activeTab.detach();
cleanupAndResolve();
} else {
cleanupAndResolve();
}
}
}
return this._destroyer.promise;
},
/**
* Clean up references to what this target points to.
*/
_cleanup: function TabTarget__cleanup() {
if (this._tab) {
targets.delete(this._tab);
} else {
promiseTargets.delete(this._form);
}
this.activeTab = null;
this._client = null;
this._tab = null;
this._form = null;
this._remote = null;
},
toString: function() {
return 'TabTarget:' + (this._tab ? this._tab : (this._form && this._form.actor));
},
};
/**
* WebProgressListener for TabTarget.
*
* @param object aTarget
* The TabTarget instance to work with.
*/
function TabWebProgressListener(aTarget) {
this.target = aTarget;
}
TabWebProgressListener.prototype = {
target: null,
QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISupportsWeakReference]),
onStateChange: function TWPL_onStateChange(progress, request, flag, status) {
let isStart = flag & Ci.nsIWebProgressListener.STATE_START;
let isDocument = flag & Ci.nsIWebProgressListener.STATE_IS_DOCUMENT;
let isNetwork = flag & Ci.nsIWebProgressListener.STATE_IS_NETWORK;
let isRequest = flag & Ci.nsIWebProgressListener.STATE_IS_REQUEST;
// Skip non-interesting states.
if (!isStart || !isDocument || !isRequest || !isNetwork) {
return;
}
// emit event if the top frame is navigating
if (progress.isTopLevel) {
// Emit the event if the target is not remoted or store the payload for
// later emission otherwise.
if (this.target._client) {
this.target._navRequest = request;
} else {
this.target.emit("will-navigate", request);
}
}
},
onProgressChange: function() {},
onSecurityChange: function() {},
onStatusChange: function() {},
onLocationChange: function TWPL_onLocationChange(webProgress, request, URI, flags) {
if (this.target &&
!(flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT)) {
let window = webProgress.DOMWindow;
// Emit the event if the target is not remoted or store the payload for
// later emission otherwise.
if (this.target._client) {
this.target._navWindow = window;
} else {
this.target.emit("navigate", window);
}
}
},
/**
* Destroy the progress listener instance.
*/
destroy: function TWPL_destroy() {
if (this.target.tab) {
try {
this.target.tab.linkedBrowser.removeProgressListener(this);
} catch (ex) {
// This can throw when a tab crashes in e10s.
}
}
this.target._webProgressListener = null;
this.target._navRequest = null;
this.target._navWindow = null;
this.target = null;
}
};
/**
* A WindowTarget represents a page living in a xul window or panel. Generally
* these will have a chrome: URL
*/
function WindowTarget(window) {
EventEmitter.decorate(this);
this._window = window;
this._setupListeners();
}
WindowTarget.prototype = {
get version() { return getVersion(); },
get window() {
return this._window;
},
get name() {
return this._window.document.title;
},
get url() {
return this._window.document.location.href;
},
get isRemote() {
return false;
},
get isLocalTab() {
return false;
},
get isThreadPaused() {
return !!this._isThreadPaused;
},
/**
* Listen to the different events.
*/
_setupListeners: function() {
this._handleThreadState = this._handleThreadState.bind(this);
this.on("thread-paused", this._handleThreadState);
this.on("thread-resumed", this._handleThreadState);
},
_handleThreadState: function(event) {
switch (event) {
case "thread-resumed":
this._isThreadPaused = false;
break;
case "thread-paused":
this._isThreadPaused = true;
break;
}
},
/**
* Target is not alive anymore.
*/
destroy: function() {
if (!this._destroyed) {
this._destroyed = true;
this.off("thread-paused", this._handleThreadState);
this.off("thread-resumed", this._handleThreadState);
this.emit("close");
targets.delete(this._window);
this._window = null;
}
return promise.resolve(null);
},
toString: function() {
return 'WindowTarget:' + this.window;
},
};