Bug 904479 - Added createPromiseWithId() that returns id of resolver r=kanru,nsm

This commit is contained in:
Nick Robson 2015-08-19 14:53:22 -07:00
parent 1045493205
commit f3e110d556
13 changed files with 79 additions and 137 deletions

View File

@ -316,7 +316,7 @@ WebappsRegistry.prototype = {
}
this.addMessageListeners(["Webapps:GetLocalizationResource:Return"]);
return this.createPromise((aResolve, aReject) => {
return this.createPromiseWithId((aResolverId) => {
cpmm.sendAsyncMessage("Webapps:GetLocalizationResource", {
manifestURL: manifestURL,
lang: aLanguage,
@ -325,10 +325,7 @@ WebappsRegistry.prototype = {
dataType: aType,
oid: this._id,
topId: this._topId,
requestID: this.getPromiseResolverId({
resolve: aResolve,
reject: aReject
})
requestID: aResolverId
});
});
},
@ -609,7 +606,7 @@ WebappsApplication.prototype = {
connect: function(aKeyword, aRules) {
this.addMessageListeners(["Webapps:Connect:Return:OK",
"Webapps:Connect:Return:KO"]);
return this.createPromise(function (aResolve, aReject) {
return this.createPromiseWithId((aResolverId) => {
let from = this._window.location.origin + this._window.location.pathname;
cpmm.sendAsyncMessage("Webapps:Connect", {
keyword: aKeyword,
@ -618,27 +615,21 @@ WebappsApplication.prototype = {
pubPageURL: from,
outerWindowID: this._id,
topWindowID: this._topId,
requestID: this.getPromiseResolverId({
resolve: aResolve,
reject: aReject
})
requestID: aResolverId
});
}.bind(this));
});
},
getConnections: function() {
this.addMessageListeners("Webapps:GetConnections:Return:OK");
return this.createPromise(function (aResolve, aReject) {
return this.createPromiseWithId((aResolverId) => {
cpmm.sendAsyncMessage("Webapps:GetConnections", {
manifestURL: this.manifestURL,
outerWindowID: this._id,
topWindowID: this._topId,
requestID: this.getPromiseResolverId({
resolve: aResolve,
reject: aReject
})
requestID: aResolverId
});
}.bind(this));
});
},
addReceipt: function(receipt) {
@ -689,22 +680,19 @@ WebappsApplication.prototype = {
export: function() {
this.addMessageListeners(["Webapps:Export:Return"]);
return this.createPromise((aResolve, aReject) => {
return this.createPromiseWithId((aResolverId) => {
cpmm.sendAsyncMessage("Webapps:Export",
{ manifestURL: this.manifestURL,
oid: this._id,
topId: this._topId,
requestID: this.getPromiseResolverId({
resolve: aResolve,
reject: aReject
})
requestID: aResolverId
});
});
},
getLocalizedValue: function(aProperty, aLang, aEntryPoint) {
this.addMessageListeners(["Webapps:GetLocalizedValue:Return"]);
return this.createPromise((aResolve, aReject) => {
return this.createPromiseWithId((aResolverId) => {
cpmm.sendAsyncMessage("Webapps:GetLocalizedValue",
{ manifestURL: this.manifestURL,
oid: this._id,
@ -712,10 +700,7 @@ WebappsApplication.prototype = {
property: aProperty,
lang: aLang,
entryPoint: aEntryPoint,
requestID: this.getPromiseResolverId({
resolve: aResolve,
reject: aReject
})
requestID: aResolverId
});
});
},
@ -958,19 +943,16 @@ WebappsApplicationMgmt.prototype = {
},
getIcon: function(aApp, aIconID, aEntryPoint) {
return this.createPromise(function(aResolve, aReject) {
return this.createPromiseWithId((aResolverId) => {
cpmm.sendAsyncMessage("Webapps:GetIcon", {
oid: this._id,
topId: this._topId,
manifestURL: aApp.manifestURL,
iconID: aIconID,
entryPoint: aEntryPoint,
requestID: this.getPromiseResolverId({
resolve: aResolve,
reject: aReject
})
requestID: aResolverId
});
}.bind(this));
});
},
getNotInstalled: function() {
@ -988,29 +970,25 @@ WebappsApplicationMgmt.prototype = {
import: function(aBlob) {
let principal = this._window.document.nodePrincipal;
return this.createPromise((aResolve, aReject) => {
return this.createPromiseWithId((aResolverId) => {
cpmm.sendAsyncMessage("Webapps:Import",
{ blob: aBlob,
oid: this._id,
topId: this._topId,
requestID: this.getPromiseResolverId({
resolve: aResolve,
reject: aReject
})}, null, principal);
requestID: aResolverId
}, null, principal);
});
},
extractManifest: function(aBlob) {
let principal = this._window.document.nodePrincipal;
return this.createPromise((aResolve, aReject) => {
return this.createPromiseWithId((aResolverId) => {
cpmm.sendAsyncMessage("Webapps:ExtractManifest",
{ blob: aBlob,
oid: this._id,
topId: this._topId,
requestID: this.getPromiseResolverId({
resolve: aResolve,
reject: aReject
})}, null, principal);
requestID: aResolverId
}, null, principal);
});
},

View File

@ -298,6 +298,17 @@ DOMRequestIpcHelper.prototype = {
return new this._window.Promise(aPromiseInit);
},
/**
* createPromiseWithId() creates a new Promise, accepting a callback
* which is immediately called with the generated resolverId.
*/
createPromiseWithId: function(aCallback) {
return this.createPromise(function(aResolve, aReject) {
let resolverId = this.getPromiseResolverId({ resolve: aResolve, reject: aReject });
aCallback(resolverId);
}.bind(this));
},
forEachRequest: function(aCallback) {
if (!this._requests) {
return;

View File

@ -408,6 +408,18 @@
ok(promise instanceof Promise, "Returned a Promise");
promise.then(next);
},
function() {
info("== Test createPromiseWithId()");
var _resolverId;
var promise = dummy.createPromiseWithId(function(resolverId) {
_resolverId = resolverId;
});
var resolver = dummy.getPromiseResolver(_resolverId);
ok(promise instanceof Promise, "Returned a Promise");
ok(typeof _resolverId === "string", "resolverId is a string");
ok(resolver != null, "resolverId is a valid id");
next();
},
function() {
info("== Test getResolver()");
var id;

View File

@ -52,36 +52,30 @@ EngineeringModeAPI.prototype = {
// This returns a Promise<DOMString>
getValue: function getValue(aName) {
debug("getValue " + aName);
let promiseInit = function(resolve, reject) {
debug("promise init called for getValue " + aName);
let resolverId = this.getPromiseResolverId({resolve: resolve,
reject: reject });
debug("promise init " + resolverId);
let promiseInit = function(aResolverId) {
debug("promise init called for getValue " + aName + " has resolverId " + aResolverId);
cpmm.sendAsyncMessage("EngineeringMode:GetValue", {
requestId: resolverId,
requestId: aResolverId,
name: aName
});
}.bind(this);
return this.createPromise(promiseInit);
return this.createPromiseWithId(promiseInit);
},
// This returns a Promise<void>
setValue: function setValue(aName, aValue) {
debug("setValue " + aName + ' as ' + aValue );
let promiseInit = function(resolve, reject) {
debug("promise init called for setValue " + aName);
let resolverId = this.getPromiseResolverId({resolve: resolve,
reject: reject });
debug("promise init " + resolverId);
let promiseInit = function(aResolverId) {
debug("promise init called for getValue " + aName + " has resolverId " + aResolverId);
cpmm.sendAsyncMessage("EngineeringMode:SetValue", {
requestId: resolverId,
requestId: aResolverId,
name: aName,
value: aValue
});
}.bind(this);
return this.createPromise(promiseInit);
return this.createPromiseWithId(promiseInit);
},
set onmessage(aHandler) {

View File

@ -394,7 +394,7 @@ MozInputMethod.prototype = {
},
removeInput: function(inputId) {
return this._sendPromise(function(resolverId) {
return this.createPromiseWithId(function(resolverId) {
let appId = this._window.document.nodePrincipal.appId;
cpmm.sendAsyncMessage('InputRegistry:Remove', {
@ -435,14 +435,6 @@ MozInputMethod.prototype = {
if (!this._isSystem) {
throw new this._window.Error("Should have 'input-manage' permssion.");
}
},
_sendPromise: function(callback) {
let self = this;
return this.createPromise(function(resolve, reject) {
let resolverId = self.getPromiseResolverId({ resolve: resolve, reject: reject });
callback(resolverId);
});
}
};
@ -786,14 +778,13 @@ MozInputContext.prototype = {
_sendPromise: function(callback) {
let self = this;
return this._ipcHelper.createPromise(function(resolve, reject) {
let resolverId = self._ipcHelper.getPromiseResolverId({ resolve: resolve, reject: reject });
return this._ipcHelper.createPromiseWithId(function(aResolverId) {
if (!WindowMap.isActive(self._window)) {
self._ipcHelper.removePromiseResolver(resolverId);
self._ipcHelper.removePromiseResolver(aResolverId);
reject('Input method is not active.');
return;
}
callback(resolverId);
callback(aResolverId);
});
}
};

View File

@ -37,11 +37,8 @@ NfcCallback.prototype = {
_requestId: null,
_createPromise: function _createPromise() {
this.promise = this.createPromise((aResolve, aReject) => {
this._requestId = btoa(this.getPromiseResolverId({
resolve: aResolve,
reject: aReject
}));
this.promise = this.createPromiseWithId((aResolverId) => {
this._requestId = btoa(aResolverId);
});
},

View File

@ -104,10 +104,9 @@ PresentationDeviceInfoManager.prototype = {
getAll: function() {
log("getAll");
let self = this;
return this.createPromise(function(aResolve, aReject) {
let resolverId = self.getPromiseResolverId({ resolve: aResolve, reject: aReject });
return this.createPromiseWithId(function(aResolverId) {
cpmm.sendAsyncMessage("PresentationDeviceInfoManager:GetAll", {
requestId: resolverId,
requestId: aResolverId,
});
});
},

View File

@ -46,9 +46,8 @@ RequestSyncManager.prototype = {
sendMessage: function(aMsg, aObj) {
let self = this;
return this.createPromise(function(aResolve, aReject) {
aObj.requestID =
self.getPromiseResolverId({ resolve: aResolve, reject: aReject });
return this.createPromiseWithId(function(aResolverId) {
aObj.requestID = aResolverId;
cpmm.sendAsyncMessage(aMsg, aObj, null,
self._window.document.nodePrincipal);
});

View File

@ -68,9 +68,8 @@ RequestSyncScheduler.prototype = {
sendMessage: function(aMsg, aObj) {
let self = this;
return this.createPromise(function(aResolve, aReject) {
aObj.requestID =
self.getPromiseResolverId({ resolve: aResolve, reject: aReject });
return this.createPromiseWithId(function(aResolverId) {
aObj.requestID = aResolverId;
cpmm.sendAsyncMessage(aMsg, aObj, null,
self._window.document.nodePrincipal);
});

View File

@ -146,18 +146,6 @@ function ResourceStatsManager() {
ResourceStatsManager.prototype = {
__proto__: DOMRequestIpcHelper.prototype,
_getPromise: function(aCallback) {
let self = this;
return this.createPromise(function(aResolve, aReject) {
let resolverId = self.getPromiseResolverId({
resolve: aResolve,
reject: aReject
});
aCallback(resolverId);
});
},
// Check time range.
_checkTimeRange: function(aStart, aEnd) {
if (DEBUG) {
@ -180,7 +168,7 @@ ResourceStatsManager.prototype = {
// Create Promise.
let self = this;
return this._getPromise(function(aResolverId) {
return this.createPromiseWithId(function(aResolverId) {
self.cpmm.sendAsyncMessage("ResourceStats:GetStats", {
resolverId: aResolverId,
type: self.type,
@ -198,7 +186,7 @@ ResourceStatsManager.prototype = {
// Create Promise.
let self = this;
return this._getPromise(function(aResolverId) {
return this.createPromiseWithId(function(aResolverId) {
self.cpmm.sendAsyncMessage("ResourceStats:ClearStats", {
resolverId: aResolverId,
type: self.type,
@ -213,7 +201,7 @@ ResourceStatsManager.prototype = {
clearAllStats: function() {
// Create Promise.
let self = this;
return this._getPromise(function(aResolverId) {
return this.createPromiseWithId(function(aResolverId) {
self.cpmm.sendAsyncMessage("ResourceStats:ClearAllStats", {
resolverId: aResolverId,
type: self.type,
@ -239,7 +227,7 @@ ResourceStatsManager.prototype = {
// Create Promise.
let self = this;
return this._getPromise(function(aResolverId) {
return this.createPromiseWithId(function(aResolverId) {
self.cpmm.sendAsyncMessage("ResourceStats:AddAlarm", {
resolverId: aResolverId,
type: self.type,
@ -255,7 +243,7 @@ ResourceStatsManager.prototype = {
getAlarms: function(aStatsOptions) {
// Create Promise.
let self = this;
return this._getPromise(function(aResolverId) {
return this.createPromiseWithId(function(aResolverId) {
self.cpmm.sendAsyncMessage("ResourceStats:GetAlarms", {
resolverId: aResolverId,
type: self.type,
@ -268,7 +256,7 @@ ResourceStatsManager.prototype = {
removeAlarm: function(aAlarmId) {
// Create Promise.
let self = this;
return this._getPromise(function(aResolverId) {
return this.createPromiseWithId(function(aResolverId) {
self.cpmm.sendAsyncMessage("ResourceStats:RemoveAlarm", {
resolverId: aResolverId,
type: self.type,
@ -281,7 +269,7 @@ ResourceStatsManager.prototype = {
removeAllAlarms: function() {
// Create Promise.
let self = this;
return this._getPromise(function(aResolverId) {
return this.createPromiseWithId(function(aResolverId) {
self.cpmm.sendAsyncMessage("ResourceStats:RemoveAllAlarms", {
resolverId: aResolverId,
type: self.type,
@ -293,7 +281,7 @@ ResourceStatsManager.prototype = {
getAvailableComponents: function() {
// Create Promise.
let self = this;
return this._getPromise(function(aResolverId) {
return this.createPromiseWithId(function(aResolverId) {
self.cpmm.sendAsyncMessage("ResourceStats:GetComponents", {
resolverId: aResolverId,
type: self.type,

View File

@ -62,12 +62,8 @@ PromiseHelpersSubclass.prototype = {
callback(resolverId);
};
return this.createPromise((resolve, reject) => {
let resolverId = this.getPromiseResolverId({
resolve: resolve,
reject: reject
});
ctxCallback(resolverId);
return this.createPromiseWithId((aResolverId) => {
ctxCallback(aResolverId);
});
},

View File

@ -225,7 +225,7 @@ SystemUpdateManager.prototype = {
},
getProviders: function() {
return this._sendPromise(function(aResolverId) {
return this.createPromiseWithId(function(aResolverId) {
cpmm.sendAsyncMessage("SystemUpdate:GetProviders", {
requestId: aResolverId,
});
@ -233,7 +233,7 @@ SystemUpdateManager.prototype = {
},
getActiveProvider: function() {
return this._sendPromise(function(aResolverId) {
return this.createPromiseWithId(function(aResolverId) {
cpmm.sendAsyncMessage("SystemUpdate:GetActiveProvider", {
requestId: aResolverId,
});
@ -241,21 +241,12 @@ SystemUpdateManager.prototype = {
},
setActiveProvider: function(aUuid) {
return this._sendPromise(function(aResolverId) {
return this.createPromiseWithId(function(aResolverId) {
cpmm.sendAsyncMessage("SystemUpdate:SetActiveProvider", {
requestId: aResolverId,
uuid: aUuid
});
});
},
_sendPromise: function(aCallback) {
let self = this;
return this.createPromise(function(aResolve, aReject) {
let resolverId = self.getPromiseResolverId({resolve: aResolve,
reject: aReject});
aCallback(resolverId);
});
}
};

View File

@ -40,19 +40,6 @@ TetheringManager.prototype = {
this.initDOMRequestHelper(aWindow, messages);
},
_getPromise: function(aCallback) {
let self = this;
return this.createPromise(function(aResolve, aReject) {
let resolverId = self.getPromiseResolverId({
resolve: aResolve,
reject: aReject
});
aCallback(resolverId);
});
},
// TODO : aMessage format may be different after supporting bt/usb.
// for now, use wifi format first.
receiveMessage: function(aMessage) {
@ -77,7 +64,7 @@ TetheringManager.prototype = {
let self = this;
switch (aType) {
case TETHERING_TYPE_WIFI:
return this._getPromise(function(aResolverId) {
return this.createPromiseWithId(function(aResolverId) {
let data = { resolverId: aResolverId, enabled: aEnabled, config: aConfig };
cpmm.sendAsyncMessage("WifiManager:setWifiTethering", { data: data});
});
@ -85,7 +72,7 @@ TetheringManager.prototype = {
case TETHERING_TYPE_USB:
default:
debug("tethering type(" + aType + ") doesn't support");
return this._getPromise(function(aResolverId) {
return this.createPromiseWithId(function(aResolverId) {
self.takePromiseResolver(aResolverId).reject();
});
}