2012-03-06 11:50:58 -08: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-08-09 18:11:11 -07:00
|
|
|
* Helper object for APIs that deal with DOMRequests and Promises and need to
|
|
|
|
* release them when the window goes out of scope.
|
2013-07-08 14:55:42 -07:00
|
|
|
*/
|
|
|
|
const Cu = Components.utils;
|
2012-03-06 11:50:58 -08:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = ["DOMRequestIpcHelper"];
|
2012-03-06 11:50:58 -08:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2012-08-27 07:13:02 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsIMessageListenerManager");
|
2012-03-06 11:50:58 -08:00
|
|
|
|
2013-07-08 14:55:42 -07: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) {
|
2013-08-08 11:36:56 -07:00
|
|
|
cpmm.addWeakMessageListener(msgName, this);
|
2013-07-08 14:55:42 -07:00
|
|
|
}, 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 14:13:42 -07:00
|
|
|
// DOMRequestIpcHelper.destroy() calls back into this function.
|
|
|
|
if (this._destroyed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._destroyed = true;
|
|
|
|
|
2013-07-08 14:55:42 -07:00
|
|
|
Services.obs.removeObserver(this, "inner-window-destroyed");
|
|
|
|
|
|
|
|
this._messages.forEach(function(msgName) {
|
2013-08-08 11:36:56 -07:00
|
|
|
cpmm.removeWeakMessageListener(msgName, this);
|
2013-07-08 14:55:42 -07:00
|
|
|
}, this);
|
|
|
|
this._messages = null;
|
|
|
|
|
|
|
|
let helper = this._weakHelper.get();
|
|
|
|
if (helper) {
|
|
|
|
helper.destroyDOMRequestHelper();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.DOMRequestIpcHelper = function DOMRequestIpcHelper() {
|
2012-03-06 11:50:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
DOMRequestIpcHelper.prototype = {
|
2013-07-08 14:55:42 -07: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;
|
2013-08-09 09:32:02 -07:00
|
|
|
this._requests = {};
|
2013-07-08 14:55:42 -07:00
|
|
|
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 11:50:58 -08:00
|
|
|
getRequestId: function(aRequest) {
|
|
|
|
let id = "id" + this._getRandomId();
|
|
|
|
this._requests[id] = aRequest;
|
|
|
|
return id;
|
|
|
|
},
|
|
|
|
|
2013-08-09 18:11:11 -07:00
|
|
|
getPromiseResolverId: function(aPromiseResolver) {
|
|
|
|
// Delegates to getRequest() since the lookup table is agnostic about
|
|
|
|
// storage.
|
|
|
|
return this.getRequestId(aPromiseResolver);
|
|
|
|
},
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
getRequest: function(aId) {
|
|
|
|
if (this._requests[aId])
|
|
|
|
return this._requests[aId];
|
|
|
|
},
|
|
|
|
|
2013-08-09 18:11:11 -07:00
|
|
|
getPromiseResolver: function(aId) {
|
|
|
|
// Delegates to getRequest() since the lookup table is agnostic about
|
|
|
|
// storage.
|
|
|
|
return this.getRequest(aId);
|
|
|
|
},
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
removeRequest: function(aId) {
|
|
|
|
if (this._requests[aId])
|
|
|
|
delete this._requests[aId];
|
|
|
|
},
|
|
|
|
|
2013-08-09 18:11:11 -07:00
|
|
|
removePromiseResolver: function(aId) {
|
|
|
|
// Delegates to getRequest() since the lookup table is agnostic about
|
|
|
|
// storage.
|
|
|
|
this.removeRequest(aId);
|
|
|
|
},
|
|
|
|
|
2012-03-20 08:59:38 -07:00
|
|
|
takeRequest: function(aId) {
|
|
|
|
if (!this._requests[aId])
|
|
|
|
return null;
|
|
|
|
let request = this._requests[aId];
|
|
|
|
delete this._requests[aId];
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
2013-08-09 18:11:11 -07:00
|
|
|
takePromiseResolver: function(aId) {
|
|
|
|
// Delegates to getRequest() since the lookup table is agnostic about
|
|
|
|
// storage.
|
|
|
|
return this.takeRequest(aId);
|
|
|
|
},
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
_getRandomId: function() {
|
|
|
|
return Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator).generateUUID().toString();
|
|
|
|
},
|
|
|
|
|
2013-07-08 14:55:42 -07: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-06 18:32:09 -08:00
|
|
|
return;
|
|
|
|
}
|
2013-07-08 14:55:42 -07:00
|
|
|
this._destroyed = true;
|
2012-11-06 18:32:09 -08:00
|
|
|
|
2013-07-08 14:55:42 -07:00
|
|
|
this._DOMRequestIpcHelperMessageListener.destroy();
|
2013-08-09 09:32:02 -07:00
|
|
|
this._requests = {};
|
2013-07-08 14:55:42 -07:00
|
|
|
this._window = null;
|
2012-04-11 21:01:49 -07:00
|
|
|
|
2013-07-08 14:55:42 -07:00
|
|
|
if(this.uninit) {
|
|
|
|
this.uninit();
|
|
|
|
}
|
2012-03-06 11:50:58 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
createRequest: function() {
|
2012-04-10 22:55:54 -07:00
|
|
|
return Services.DOMRequest.createRequest(this._window);
|
2013-08-09 18:11:11 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* createPromise() creates a new Promise, with `aPromiseInit` as the
|
|
|
|
* PromiseInit callback. The promise constructor is obtained from the
|
|
|
|
* reference to window owned by this DOMRequestIPCHelper.
|
|
|
|
*/
|
|
|
|
createPromise: function(aPromiseInit) {
|
|
|
|
return new this._window.Promise(aPromiseInit);
|
2013-08-12 22:07:03 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
forEachRequest: function(aCallback) {
|
|
|
|
Object.keys(this._requests).forEach(function(k) {
|
|
|
|
if (this.getRequest(k) instanceof this._window.DOMRequest) {
|
|
|
|
aCallback(k);
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
forEachPromiseResolver: function(aCallback) {
|
|
|
|
Object.keys(this._requests).forEach(function(k) {
|
|
|
|
if (this.getPromiseResolver(k) instanceof this._window.PromiseResolver) {
|
|
|
|
aCallback(k);
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
},
|
2012-03-06 11:50:58 -08:00
|
|
|
}
|