2012-03-06 19:50:58 +00:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
/**
|
2013-07-08 21:55:42 +00:00
|
|
|
* Helper object for APIs that deal with DOMRequests and need to release them
|
|
|
|
* when the window goes out of scope.
|
|
|
|
*/
|
|
|
|
const Cu = Components.utils;
|
2012-03-06 19:50:58 +00:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.EXPORTED_SYMBOLS = ["DOMRequestIpcHelper"];
|
2012-03-06 19:50:58 +00:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2012-08-27 14:13:02 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsIMessageListenerManager");
|
2012-03-06 19:50:58 +00:00
|
|
|
|
2013-07-08 21:55:42 +00:00
|
|
|
/**
|
|
|
|
* We use DOMRequestIpcHelperMessageListener to avoid leaking objects which
|
|
|
|
* "inherit" from DOMRequestIpcHelper.
|
|
|
|
*
|
|
|
|
* The issue is that the message manager will hold a strong ref to the message
|
|
|
|
* listener we register with it. But we don't want to hold a strong ref to the
|
|
|
|
* DOMRequestIpcHelper object, because that object may be arbitrarily large.
|
|
|
|
*
|
|
|
|
* So instead the message manager holds a strong ref to the
|
|
|
|
* DOMRequestIpcHelperMessageListener, and that holds a /weak/ ref to its
|
|
|
|
* DOMRequestIpcHelper.
|
|
|
|
*
|
|
|
|
* Additionally, we want to unhook all of these message listeners when the
|
|
|
|
* appropriate window is destroyed. We use DOMRequestIpcHelperMessageListener
|
|
|
|
* for this, too.
|
|
|
|
*/
|
|
|
|
this.DOMRequestIpcHelperMessageListener = function(aHelper, aWindow, aMessages) {
|
|
|
|
this._weakHelper = Cu.getWeakReference(aHelper);
|
|
|
|
|
|
|
|
this._messages = aMessages;
|
|
|
|
this._messages.forEach(function(msgName) {
|
|
|
|
cpmm.addMessageListener(msgName, this);
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
Services.obs.addObserver(this, "inner-window-destroyed", /* weakRef */ true);
|
|
|
|
|
|
|
|
// aWindow may be null; in that case, the DOMRequestIpcHelperMessageListener
|
|
|
|
// is not tied to a particular window and lives forever.
|
|
|
|
if (aWindow) {
|
|
|
|
let util = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
this._innerWindowID = util.currentInnerWindowID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DOMRequestIpcHelperMessageListener.prototype = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIMessageListener,
|
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference]),
|
|
|
|
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
if (aTopic !== "inner-window-destroyed") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let wId = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
|
|
|
|
if (wId != this._innerWindowID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.destroy();
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(aMsg) {
|
|
|
|
let helper = this._weakHelper.get();
|
|
|
|
if (helper) {
|
|
|
|
helper.receiveMessage(aMsg);
|
|
|
|
} else {
|
|
|
|
this.destroy();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
2013-07-11 21:13:42 +00:00
|
|
|
// DOMRequestIpcHelper.destroy() calls back into this function.
|
|
|
|
if (this._destroyed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._destroyed = true;
|
|
|
|
|
2013-07-08 21:55:42 +00:00
|
|
|
Services.obs.removeObserver(this, "inner-window-destroyed");
|
|
|
|
|
|
|
|
this._messages.forEach(function(msgName) {
|
|
|
|
cpmm.removeMessageListener(msgName, this);
|
|
|
|
}, this);
|
|
|
|
this._messages = null;
|
|
|
|
|
|
|
|
let helper = this._weakHelper.get();
|
|
|
|
if (helper) {
|
|
|
|
helper.destroyDOMRequestHelper();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.DOMRequestIpcHelper = function DOMRequestIpcHelper() {
|
2012-03-06 19:50:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DOMRequestIpcHelper.prototype = {
|
2013-07-08 21:55:42 +00:00
|
|
|
/**
|
|
|
|
* An object which "inherits" from DOMRequestIpcHelper and declares its own
|
|
|
|
* queryInterface method MUST implement Ci.nsISupportsWeakReference.
|
|
|
|
*/
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference]),
|
|
|
|
|
|
|
|
initDOMRequestHelper: function(aWindow, aMessages) {
|
|
|
|
this._DOMRequestIpcHelperMessageListener =
|
|
|
|
new DOMRequestIpcHelperMessageListener(this, aWindow, aMessages);
|
|
|
|
|
|
|
|
this._window = aWindow;
|
|
|
|
this._requests = [];
|
|
|
|
this._id = this._getRandomId();
|
|
|
|
|
|
|
|
if (this._window) {
|
|
|
|
// We don't use this.innerWindowID, but other classes rely on it.
|
|
|
|
let util = this._window.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
this.innerWindowID = util.currentInnerWindowID;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-03-06 19:50:58 +00:00
|
|
|
getRequestId: function(aRequest) {
|
|
|
|
let id = "id" + this._getRandomId();
|
|
|
|
this._requests[id] = aRequest;
|
|
|
|
return id;
|
|
|
|
},
|
|
|
|
|
|
|
|
getRequest: function(aId) {
|
|
|
|
if (this._requests[aId])
|
|
|
|
return this._requests[aId];
|
|
|
|
},
|
|
|
|
|
|
|
|
removeRequest: function(aId) {
|
|
|
|
if (this._requests[aId])
|
|
|
|
delete this._requests[aId];
|
|
|
|
},
|
|
|
|
|
2012-03-20 15:59:38 +00:00
|
|
|
takeRequest: function(aId) {
|
|
|
|
if (!this._requests[aId])
|
|
|
|
return null;
|
|
|
|
let request = this._requests[aId];
|
|
|
|
delete this._requests[aId];
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
2012-03-06 19:50:58 +00:00
|
|
|
_getRandomId: function() {
|
|
|
|
return Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator).generateUUID().toString();
|
|
|
|
},
|
|
|
|
|
2013-07-08 21:55:42 +00:00
|
|
|
destroyDOMRequestHelper: function() {
|
|
|
|
// This function is re-entrant --
|
|
|
|
// DOMRequestIpcHelperMessageListener.destroy() calls back into this
|
|
|
|
// function, and this.uninit() may also call it.
|
|
|
|
if (this._destroyed) {
|
2012-11-07 02:32:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-07-08 21:55:42 +00:00
|
|
|
this._destroyed = true;
|
2012-11-07 02:32:09 +00:00
|
|
|
|
2013-07-08 21:55:42 +00:00
|
|
|
this._DOMRequestIpcHelperMessageListener.destroy();
|
2012-03-06 19:50:58 +00:00
|
|
|
this._requests = [];
|
2013-07-08 21:55:42 +00:00
|
|
|
this._window = null;
|
2012-04-12 04:01:49 +00:00
|
|
|
|
2013-07-08 21:55:42 +00:00
|
|
|
if(this.uninit) {
|
|
|
|
this.uninit();
|
|
|
|
}
|
2012-03-06 19:50:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
createRequest: function() {
|
2012-04-11 05:55:54 +00:00
|
|
|
return Services.DOMRequest.createRequest(this._window);
|
2012-03-06 19:50:58 +00:00
|
|
|
}
|
|
|
|
}
|