2012-03-06 23:50:58 +04: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-10-05 01:59:48 +04:00
|
|
|
* Helper object for APIs that deal with DOMRequests and Promises.
|
|
|
|
* It allows objects inheriting from it to create and keep track of DOMRequests
|
|
|
|
* and Promises objects in the common scenario where requests are created in
|
|
|
|
* the child, handed out to content and delivered to the parent within an async
|
|
|
|
* message (containing the identifiers of these requests). The parent may send
|
|
|
|
* messages back as answers to different requests and the child will use this
|
|
|
|
* helper to get the right request object. This helper also takes care of
|
|
|
|
* releasing the requests objects when the window goes out of scope.
|
|
|
|
*
|
|
|
|
* DOMRequestIPCHelper also deals with message listeners, allowing to add them
|
|
|
|
* to the child side of frame and process message manager and removing them
|
|
|
|
* when needed.
|
2013-07-09 01:55:42 +04:00
|
|
|
*/
|
2012-10-31 20:13:28 +04:00
|
|
|
var EXPORTED_SYMBOLS = ["DOMRequestIpcHelper"];
|
2012-03-06 23:50:58 +04:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2012-03-06 23:50:58 +04:00
|
|
|
|
2013-10-05 01:59:48 +04:00
|
|
|
function DOMRequestIpcHelper() {
|
|
|
|
// _listeners keeps a list of messages for which we added a listener and the
|
|
|
|
// kind of listener that we added (strong or weak). It's an object of this
|
|
|
|
// form:
|
|
|
|
// {
|
|
|
|
// "message1": true,
|
|
|
|
// "messagen": false
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// where each property is the name of the message and its value is a boolean
|
2013-12-03 00:39:04 +04:00
|
|
|
// that indicates if the listener is weak or not.
|
2013-10-05 01:59:48 +04:00
|
|
|
this._listeners = null;
|
|
|
|
this._requests = null;
|
|
|
|
this._window = null;
|
2013-07-09 01:55:42 +04:00
|
|
|
}
|
|
|
|
|
2013-10-05 01:59:48 +04:00
|
|
|
DOMRequestIpcHelper.prototype = {
|
|
|
|
/**
|
2013-12-03 00:39:04 +04:00
|
|
|
* An object which "inherits" from DOMRequestIpcHelper and declares its own
|
|
|
|
* queryInterface method MUST implement Ci.nsISupportsWeakReference.
|
2013-10-05 01:59:48 +04:00
|
|
|
*/
|
2013-10-18 14:58:24 +04:00
|
|
|
QueryInterface: ChromeUtils.generateQI([
|
|
|
|
Ci.nsISupportsWeakReference,
|
|
|
|
Ci.nsIObserver,
|
|
|
|
]),
|
2013-07-09 01:55:42 +04:00
|
|
|
|
2013-10-05 01:59:48 +04:00
|
|
|
/**
|
|
|
|
* 'aMessages' is expected to be an array of either:
|
|
|
|
* - objects of this form:
|
|
|
|
* {
|
|
|
|
* name: "messageName",
|
2013-12-03 00:39:04 +04:00
|
|
|
* weakRef: false
|
2013-10-05 01:59:48 +04:00
|
|
|
* }
|
2013-12-03 00:39:04 +04:00
|
|
|
* where 'name' is the message identifier and 'weakRef' a boolean
|
|
|
|
* indicating if the listener should be a weak referred one or not.
|
2013-10-05 01:59:48 +04:00
|
|
|
*
|
|
|
|
* - or only strings containing the message name, in which case the listener
|
2013-12-03 00:39:04 +04:00
|
|
|
* will be added as a strong reference by default.
|
2013-10-05 01:59:48 +04:00
|
|
|
*/
|
2019-09-02 14:22:27 +03:00
|
|
|
addMessageListeners(aMessages) {
|
2013-10-05 01:59:48 +04:00
|
|
|
if (!aMessages) {
|
2013-07-09 01:55:42 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-05 01:59:48 +04:00
|
|
|
if (!this._listeners) {
|
|
|
|
this._listeners = {};
|
2013-07-09 01:55:42 +04:00
|
|
|
}
|
|
|
|
|
2013-10-05 01:59:48 +04:00
|
|
|
if (!Array.isArray(aMessages)) {
|
|
|
|
aMessages = [aMessages];
|
2013-07-09 01:55:42 +04:00
|
|
|
}
|
2013-10-05 01:59:48 +04:00
|
|
|
|
|
|
|
aMessages.forEach(aMsg => {
|
|
|
|
let name = aMsg.name || aMsg;
|
|
|
|
// If the listener is already set and it is of the same type we just
|
2014-06-20 06:51:05 +04:00
|
|
|
// increase the count and bail out. If it is not of the same type,
|
|
|
|
// we throw an exception.
|
2013-10-05 01:59:48 +04:00
|
|
|
if (this._listeners[name] != undefined) {
|
2014-06-20 06:51:05 +04:00
|
|
|
if (!!aMsg.weakRef == this._listeners[name].weakRef) {
|
|
|
|
this._listeners[name].count++;
|
2013-10-05 01:59:48 +04:00
|
|
|
return;
|
|
|
|
} else {
|
2020-05-05 20:41:36 +03:00
|
|
|
throw Components.Exception("", Cr.NS_ERROR_FAILURE);
|
2013-10-05 01:59:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 22:19:56 +03:00
|
|
|
aMsg.weakRef
|
|
|
|
? Services.cpmm.addWeakMessageListener(name, this)
|
|
|
|
: Services.cpmm.addMessageListener(name, this);
|
2014-06-20 06:51:05 +04:00
|
|
|
this._listeners[name] = {
|
|
|
|
weakRef: !!aMsg.weakRef,
|
|
|
|
count: 1,
|
|
|
|
};
|
2013-10-05 01:59:48 +04:00
|
|
|
});
|
2013-07-09 01:55:42 +04:00
|
|
|
},
|
|
|
|
|
2013-10-05 01:59:48 +04:00
|
|
|
/**
|
|
|
|
* 'aMessages' is expected to be a string or an array of strings containing
|
|
|
|
* the message names of the listeners to be removed.
|
|
|
|
*/
|
2019-09-02 14:22:27 +03:00
|
|
|
removeMessageListeners(aMessages) {
|
2013-10-05 01:59:48 +04:00
|
|
|
if (!this._listeners || !aMessages) {
|
2013-07-12 01:13:42 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-07-09 01:55:42 +04:00
|
|
|
|
2013-10-05 01:59:48 +04:00
|
|
|
if (!Array.isArray(aMessages)) {
|
|
|
|
aMessages = [aMessages];
|
2013-07-09 01:55:42 +04:00
|
|
|
}
|
|
|
|
|
2013-10-05 01:59:48 +04:00
|
|
|
aMessages.forEach(aName => {
|
|
|
|
if (this._listeners[aName] == undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-20 06:51:05 +04:00
|
|
|
// Only remove the listener really when we don't have anybody that could
|
|
|
|
// be waiting on a message.
|
|
|
|
if (!--this._listeners[aName].count) {
|
|
|
|
this._listeners[aName].weakRef
|
2018-03-01 22:19:56 +03:00
|
|
|
? Services.cpmm.removeWeakMessageListener(aName, this)
|
|
|
|
: Services.cpmm.removeMessageListener(aName, this);
|
2014-06-20 06:51:05 +04:00
|
|
|
delete this._listeners[aName];
|
|
|
|
}
|
2013-10-05 01:59:48 +04:00
|
|
|
});
|
|
|
|
},
|
2012-03-06 23:50:58 +04:00
|
|
|
|
2013-07-09 01:55:42 +04:00
|
|
|
/**
|
2013-10-05 01:59:48 +04:00
|
|
|
* Initialize the helper adding the corresponding listeners to the messages
|
|
|
|
* provided as the second parameter.
|
|
|
|
*
|
|
|
|
* 'aMessages' is expected to be an array of either:
|
|
|
|
*
|
|
|
|
* - objects of this form:
|
|
|
|
* {
|
|
|
|
* name: 'messageName',
|
2013-12-03 00:39:04 +04:00
|
|
|
* weakRef: false
|
2013-10-05 01:59:48 +04:00
|
|
|
* }
|
2013-12-03 00:39:04 +04:00
|
|
|
* where 'name' is the message identifier and 'weakRef' a boolean
|
|
|
|
* indicating if the listener should be a weak referred one or not.
|
2013-10-05 01:59:48 +04:00
|
|
|
*
|
|
|
|
* - or only strings containing the message name, in which case the listener
|
2013-12-03 00:39:04 +04:00
|
|
|
* will be added as a strong referred one by default.
|
2013-07-09 01:55:42 +04:00
|
|
|
*/
|
2019-09-02 14:22:27 +03:00
|
|
|
initDOMRequestHelper(aWindow, aMessages) {
|
2013-11-20 09:33:18 +04:00
|
|
|
// Query our required interfaces to force a fast fail if they are not
|
|
|
|
// provided. These calls will throw if the interface is not available.
|
|
|
|
this.QueryInterface(Ci.nsISupportsWeakReference);
|
|
|
|
this.QueryInterface(Ci.nsIObserver);
|
|
|
|
|
2013-10-05 01:59:48 +04:00
|
|
|
if (aMessages) {
|
|
|
|
this.addMessageListeners(aMessages);
|
|
|
|
}
|
2013-07-09 01:55:42 +04:00
|
|
|
|
|
|
|
this._id = this._getRandomId();
|
|
|
|
|
2013-10-05 01:59:48 +04:00
|
|
|
this._window = aWindow;
|
2013-07-09 01:55:42 +04:00
|
|
|
if (this._window) {
|
|
|
|
// We don't use this.innerWindowID, but other classes rely on it.
|
2018-07-25 02:47:41 +03:00
|
|
|
let util = this._window.windowUtils;
|
2013-07-09 01:55:42 +04:00
|
|
|
this.innerWindowID = util.currentInnerWindowID;
|
|
|
|
}
|
2013-10-05 01:59:48 +04:00
|
|
|
|
2013-10-18 14:58:24 +04:00
|
|
|
this._destroyed = false;
|
|
|
|
|
2013-11-20 09:33:18 +04:00
|
|
|
Services.obs.addObserver(
|
|
|
|
this,
|
|
|
|
"inner-window-destroyed",
|
|
|
|
/* weak-ref */ true
|
|
|
|
);
|
2013-10-05 01:59:48 +04:00
|
|
|
},
|
|
|
|
|
2019-09-02 14:22:27 +03:00
|
|
|
destroyDOMRequestHelper() {
|
2013-10-18 14:58:24 +04:00
|
|
|
if (this._destroyed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._destroyed = true;
|
|
|
|
|
2013-10-05 01:59:48 +04:00
|
|
|
Services.obs.removeObserver(this, "inner-window-destroyed");
|
|
|
|
|
|
|
|
if (this._listeners) {
|
|
|
|
Object.keys(this._listeners).forEach(aName => {
|
2018-03-01 22:19:56 +03:00
|
|
|
this._listeners[aName].weakRef
|
|
|
|
? Services.cpmm.removeWeakMessageListener(aName, this)
|
|
|
|
: Services.cpmm.removeMessageListener(aName, this);
|
2013-10-05 01:59:48 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this._listeners = null;
|
|
|
|
this._requests = null;
|
2013-10-18 14:58:24 +04:00
|
|
|
|
|
|
|
// Objects inheriting from DOMRequestIPCHelper may have an uninit function.
|
|
|
|
if (this.uninit) {
|
|
|
|
this.uninit();
|
|
|
|
}
|
2013-11-28 11:18:08 +04:00
|
|
|
|
|
|
|
this._window = null;
|
2013-10-05 01:59:48 +04:00
|
|
|
},
|
|
|
|
|
2019-09-02 14:22:27 +03:00
|
|
|
observe(aSubject, aTopic, aData) {
|
2013-10-05 01:59:48 +04:00
|
|
|
if (aTopic !== "inner-window-destroyed") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let wId = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
|
|
|
|
if (wId != this.innerWindowID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.destroyDOMRequestHelper();
|
2013-07-09 01:55:42 +04:00
|
|
|
},
|
|
|
|
|
2019-09-02 14:22:27 +03:00
|
|
|
getRequestId(aRequest) {
|
2013-10-05 01:59:48 +04:00
|
|
|
if (!this._requests) {
|
|
|
|
this._requests = {};
|
|
|
|
}
|
|
|
|
|
2012-03-06 23:50:58 +04:00
|
|
|
let id = "id" + this._getRandomId();
|
|
|
|
this._requests[id] = aRequest;
|
|
|
|
return id;
|
|
|
|
},
|
|
|
|
|
2019-09-02 14:22:27 +03:00
|
|
|
getPromiseResolverId(aPromiseResolver) {
|
2013-08-10 05:11:11 +04:00
|
|
|
// Delegates to getRequest() since the lookup table is agnostic about
|
|
|
|
// storage.
|
|
|
|
return this.getRequestId(aPromiseResolver);
|
|
|
|
},
|
|
|
|
|
2019-09-02 14:22:27 +03:00
|
|
|
getRequest(aId) {
|
2013-10-05 01:59:48 +04:00
|
|
|
if (this._requests && this._requests[aId]) {
|
2012-03-06 23:50:58 +04:00
|
|
|
return this._requests[aId];
|
2013-10-05 01:59:48 +04:00
|
|
|
}
|
2012-03-06 23:50:58 +04:00
|
|
|
},
|
|
|
|
|
2019-09-02 14:22:27 +03:00
|
|
|
getPromiseResolver(aId) {
|
2013-08-10 05:11:11 +04:00
|
|
|
// Delegates to getRequest() since the lookup table is agnostic about
|
|
|
|
// storage.
|
|
|
|
return this.getRequest(aId);
|
|
|
|
},
|
|
|
|
|
2019-09-02 14:22:27 +03:00
|
|
|
removeRequest(aId) {
|
2013-10-05 01:59:48 +04:00
|
|
|
if (this._requests && this._requests[aId]) {
|
2012-03-06 23:50:58 +04:00
|
|
|
delete this._requests[aId];
|
2013-10-05 01:59:48 +04:00
|
|
|
}
|
2012-03-06 23:50:58 +04:00
|
|
|
},
|
|
|
|
|
2019-09-02 14:22:27 +03:00
|
|
|
removePromiseResolver(aId) {
|
2013-08-10 05:11:11 +04:00
|
|
|
// Delegates to getRequest() since the lookup table is agnostic about
|
|
|
|
// storage.
|
|
|
|
this.removeRequest(aId);
|
|
|
|
},
|
|
|
|
|
2019-09-02 14:22:27 +03:00
|
|
|
takeRequest(aId) {
|
2013-10-05 01:59:48 +04:00
|
|
|
if (!this._requests || !this._requests[aId]) {
|
2012-03-20 19:59:38 +04:00
|
|
|
return null;
|
2013-10-05 01:59:48 +04:00
|
|
|
}
|
2012-03-20 19:59:38 +04:00
|
|
|
let request = this._requests[aId];
|
|
|
|
delete this._requests[aId];
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
2019-09-02 14:22:27 +03:00
|
|
|
takePromiseResolver(aId) {
|
2013-08-10 05:11:11 +04:00
|
|
|
// Delegates to getRequest() since the lookup table is agnostic about
|
|
|
|
// storage.
|
|
|
|
return this.takeRequest(aId);
|
|
|
|
},
|
|
|
|
|
2019-09-02 14:22:27 +03:00
|
|
|
_getRandomId() {
|
2013-10-05 01:59:48 +04:00
|
|
|
return Cc["@mozilla.org/uuid-generator;1"]
|
|
|
|
.getService(Ci.nsIUUIDGenerator)
|
|
|
|
.generateUUID()
|
|
|
|
.toString();
|
2012-03-06 23:50:58 +04:00
|
|
|
},
|
|
|
|
|
2019-09-02 14:22:27 +03:00
|
|
|
createRequest() {
|
2014-10-17 13:24:00 +04:00
|
|
|
// If we don't have a valid window object, throw.
|
|
|
|
if (!this._window) {
|
|
|
|
Cu.reportError(
|
|
|
|
"DOMRequestHelper trying to create a DOMRequest without a valid window, failing."
|
|
|
|
);
|
2020-05-05 20:41:36 +03:00
|
|
|
throw Components.Exception("", Cr.NS_ERROR_FAILURE);
|
2014-10-17 13:24:00 +04:00
|
|
|
}
|
2012-04-11 09:55:54 +04:00
|
|
|
return Services.DOMRequest.createRequest(this._window);
|
2013-08-10 05:11:11 +04: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.
|
|
|
|
*/
|
2019-09-02 14:22:27 +03:00
|
|
|
createPromise(aPromiseInit) {
|
2014-10-17 13:24:00 +04:00
|
|
|
// If we don't have a valid window object, throw.
|
|
|
|
if (!this._window) {
|
|
|
|
Cu.reportError(
|
|
|
|
"DOMRequestHelper trying to create a Promise without a valid window, failing."
|
|
|
|
);
|
2020-05-05 20:41:36 +03:00
|
|
|
throw Components.Exception("", Cr.NS_ERROR_FAILURE);
|
2014-10-17 13:24:00 +04:00
|
|
|
}
|
2013-08-10 05:11:11 +04:00
|
|
|
return new this._window.Promise(aPromiseInit);
|
2013-08-13 09:07:03 +04:00
|
|
|
},
|
|
|
|
|
2015-08-20 00:53:22 +03:00
|
|
|
/**
|
|
|
|
* createPromiseWithId() creates a new Promise, accepting a callback
|
|
|
|
* which is immediately called with the generated resolverId.
|
|
|
|
*/
|
2019-09-02 14:22:27 +03:00
|
|
|
createPromiseWithId(aCallback) {
|
2015-08-20 00:53:22 +03:00
|
|
|
return this.createPromise((aResolve, aReject) => {
|
|
|
|
let resolverId = this.getPromiseResolverId({
|
|
|
|
resolve: aResolve,
|
|
|
|
reject: aReject,
|
|
|
|
});
|
|
|
|
aCallback(resolverId);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-09-02 14:22:27 +03:00
|
|
|
forEachRequest(aCallback) {
|
2013-10-05 01:59:48 +04:00
|
|
|
if (!this._requests) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(this._requests).forEach(aKey => {
|
|
|
|
if (this.getRequest(aKey) instanceof this._window.DOMRequest) {
|
|
|
|
aCallback(aKey);
|
2013-08-13 09:07:03 +04:00
|
|
|
}
|
2013-10-05 01:59:48 +04:00
|
|
|
});
|
2013-08-13 09:07:03 +04:00
|
|
|
},
|
|
|
|
|
2019-09-02 14:22:27 +03:00
|
|
|
forEachPromiseResolver(aCallback) {
|
2013-10-05 01:59:48 +04:00
|
|
|
if (!this._requests) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(this._requests).forEach(aKey => {
|
|
|
|
if (
|
|
|
|
"resolve" in this.getPromiseResolver(aKey) &&
|
|
|
|
"reject" in this.getPromiseResolver(aKey)
|
|
|
|
) {
|
|
|
|
aCallback(aKey);
|
2013-08-13 09:07:03 +04:00
|
|
|
}
|
2013-10-05 01:59:48 +04:00
|
|
|
});
|
2013-08-13 09:07:03 +04:00
|
|
|
},
|
2012-03-06 23:50:58 +04:00
|
|
|
};
|