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/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* helper object for APIs that deal with DOMRequest and need to release them properly
|
|
|
|
* when the window goes out of scope
|
|
|
|
*/
|
|
|
|
const Cu = Components.utils;
|
|
|
|
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
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.DOMRequestIpcHelper = function DOMRequestIpcHelper() {
|
2012-03-06 11:50:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
DOMRequestIpcHelper.prototype = {
|
|
|
|
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 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;
|
|
|
|
},
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
_getRandomId: function() {
|
|
|
|
return Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator).generateUUID().toString();
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
2012-11-06 18:32:09 -08:00
|
|
|
if (aTopic !== "inner-window-destroyed") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-06 11:50:58 -08:00
|
|
|
let wId = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
|
|
|
|
if (wId == this.innerWindowID) {
|
|
|
|
Services.obs.removeObserver(this, "inner-window-destroyed");
|
|
|
|
this._requests = [];
|
|
|
|
this._window = null;
|
2012-04-11 21:01:49 -07:00
|
|
|
this.removeMessageListener();
|
2012-03-06 11:50:58 -08:00
|
|
|
if(this.uninit)
|
|
|
|
this.uninit();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-04-11 21:01:49 -07:00
|
|
|
initRequests: function initRequests() {
|
2012-03-06 11:50:58 -08:00
|
|
|
this._requests = [];
|
2012-04-11 21:01:49 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
initMessageListener: function initMessageListener(aMessages) {
|
|
|
|
this._messages = aMessages;
|
2012-05-25 10:37:45 -07:00
|
|
|
this._messages.forEach(function(msgName) {
|
2012-04-11 21:01:49 -07:00
|
|
|
cpmm.addMessageListener(msgName, this);
|
2012-05-25 10:37:45 -07:00
|
|
|
}, this);
|
2012-04-11 21:01:49 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
initHelper: function(aWindow, aMessages) {
|
|
|
|
this.initMessageListener(aMessages);
|
|
|
|
this.initRequests();
|
|
|
|
this._id = this._getRandomId();
|
|
|
|
Services.obs.addObserver(this, "inner-window-destroyed", false);
|
2012-03-06 11:50:58 -08:00
|
|
|
this._window = aWindow;
|
|
|
|
let util = this._window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
this.innerWindowID = util.currentInnerWindowID;
|
2012-04-11 21:01:49 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
removeMessageListener: function removeMessageListener() {
|
2012-05-25 10:37:45 -07:00
|
|
|
this._messages.forEach(function(msgName) {
|
|
|
|
cpmm.removeMessageListener(msgName, this);
|
|
|
|
}, this);
|
2012-04-11 21:01:49 -07:00
|
|
|
this._messages = null;
|
2012-03-06 11:50:58 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
createRequest: function() {
|
2012-04-10 22:55:54 -07:00
|
|
|
return Services.DOMRequest.createRequest(this._window);
|
2012-03-06 11:50:58 -08:00
|
|
|
}
|
|
|
|
}
|