Bug 759446 - Use rest arguments where possible in the Add-ons Manager. r=Unfocused

This commit is contained in:
Luis Ares 2012-06-20 21:12:11 +12:00
parent 598b532f6c
commit 391e841c33
4 changed files with 27 additions and 39 deletions

View File

@ -77,11 +77,9 @@ const DEFAULT_PROVIDERS = [
* @param aCallback
* The callback method to call
*/
function safeCall(aCallback) {
var args = Array.slice(arguments, 1);
function safeCall(aCallback, ...aArgs) {
try {
aCallback.apply(null, args);
aCallback.apply(null, aArgs);
}
catch (e) {
WARN("Exception calling callback", e);
@ -102,14 +100,12 @@ function safeCall(aCallback) {
* @return the return value from the provider or dflt if the provider does not
* implement method or throws an error
*/
function callProvider(aProvider, aMethod, aDefault) {
function callProvider(aProvider, aMethod, aDefault, ...aArgs) {
if (!(aMethod in aProvider))
return aDefault;
var args = Array.slice(arguments, 3);
try {
return aProvider[aMethod].apply(aProvider, args);
return aProvider[aMethod].apply(aProvider, aArgs);
}
catch (e) {
ERROR("Exception calling provider " + aMethod, e);
@ -1078,17 +1074,16 @@ var AddonManagerInternal = {
* @param aMethod
* The method on the listeners to call
*/
callManagerListeners: function AMI_callManagerListeners(aMethod) {
callManagerListeners: function AMI_callManagerListeners(aMethod, ...aArgs) {
if (!aMethod || typeof aMethod != "string")
throw Components.Exception("aMethod must be a non-empty string",
Cr.NS_ERROR_INVALID_ARG);
var args = Array.slice(arguments, 1);
let managerListeners = this.managerListeners.slice(0);
for (let listener of managerListeners) {
try {
if (aMethod in listener)
listener[aMethod].apply(listener, args);
listener[aMethod].apply(listener, aArgs);
}
catch (e) {
WARN("AddonManagerListener threw exception when calling " + aMethod, e);
@ -1106,7 +1101,7 @@ var AddonManagerInternal = {
* An optional array of extra InstallListeners to also call
* @return false if any of the listeners returned false, true otherwise
*/
callInstallListeners: function AMI_callInstallListeners(aMethod, aExtraListeners) {
callInstallListeners: function AMI_callInstallListeners(aMethod, aExtraListeners, ...aArgs) {
if (!aMethod || typeof aMethod != "string")
throw Components.Exception("aMethod must be a non-empty string",
Cr.NS_ERROR_INVALID_ARG);
@ -1121,12 +1116,11 @@ var AddonManagerInternal = {
listeners = aExtraListeners.concat(this.installListeners);
else
listeners = this.installListeners.slice(0);
let args = Array.slice(arguments, 2);
for (let listener of listeners) {
try {
if (aMethod in listener) {
if (listener[aMethod].apply(listener, args) === false)
if (listener[aMethod].apply(listener, aArgs) === false)
result = false;
}
}
@ -1144,17 +1138,16 @@ var AddonManagerInternal = {
* @param aMethod
* The method on the listeners to call
*/
callAddonListeners: function AMI_callAddonListeners(aMethod) {
callAddonListeners: function AMI_callAddonListeners(aMethod, ...aArgs) {
if (!aMethod || typeof aMethod != "string")
throw Components.Exception("aMethod must be a non-empty string",
Cr.NS_ERROR_INVALID_ARG);
var args = Array.slice(arguments, 1);
let addonListeners = this.addonListeners.slice(0);
for (let listener of addonListeners) {
try {
if (aMethod in listener)
listener[aMethod].apply(listener, args);
listener[aMethod].apply(listener, aArgs);
}
catch (e) {
WARN("AddonListener threw exception when calling " + aMethod, e);
@ -1951,13 +1944,13 @@ var AddonManagerPrivate = {
AddonManagerInternal.updateAddonRepositoryData(aCallback);
},
callInstallListeners: function AMP_callInstallListeners(aMethod) {
callInstallListeners: function AMP_callInstallListeners(...aArgs) {
return AddonManagerInternal.callInstallListeners.apply(AddonManagerInternal,
arguments);
aArgs);
},
callAddonListeners: function AMP_callAddonListeners(aMethod) {
AddonManagerInternal.callAddonListeners.apply(AddonManagerInternal, arguments);
callAddonListeners: function AMP_callAddonListeners(...aArgs) {
AddonManagerInternal.callAddonListeners.apply(AddonManagerInternal, aArgs);
},
AddonAuthor: AddonAuthor,

View File

@ -5280,13 +5280,12 @@ UpdateChecker.prototype = {
* @param aMethod
* The method to call on the listener
*/
callListener: function(aMethod) {
callListener: function(aMethod, ...aArgs) {
if (!(aMethod in this.listener))
return;
let args = Array.slice(arguments, 1);
try {
this.listener[aMethod].apply(this.listener, args);
this.listener[aMethod].apply(this.listener, aArgs);
}
catch (e) {
LOG("Exception calling UpdateListener method " + aMethod + ": " + e);

View File

@ -283,8 +283,8 @@ var gEventManager = {
"onUninstalled", "onInstalled", "onOperationCancelled",
"onUpdateAvailable", "onUpdateFinished", "onCompatibilityUpdateAvailable",
"onPropertyChanged"].forEach(function(aEvent) {
self[aEvent] = function() {
self.delegateAddonEvent(aEvent, Array.splice(arguments, 0));
self[aEvent] = function(...aArgs) {
self.delegateAddonEvent(aEvent, aArgs);
};
});
@ -292,8 +292,8 @@ var gEventManager = {
"onDownloadProgress", "onDownloadCancelled", "onInstallStarted",
"onInstallEnded", "onInstallFailed", "onInstallCancelled",
"onExternalInstall"].forEach(function(aEvent) {
self[aEvent] = function() {
self.delegateInstallEvent(aEvent, Array.splice(arguments, 0));
self[aEvent] = function(...aArgs) {
self.delegateInstallEvent(aEvent, aArgs);
};
});

View File

@ -132,10 +132,9 @@ registerCleanupFunction(function() {
});
});
function log_exceptions(aCallback) {
function log_exceptions(aCallback, ...aArgs) {
try {
var args = Array.slice(arguments, 1);
return aCallback.apply(null, args);
return aCallback.apply(null, aArgs);
}
catch (e) {
info("Exception thrown: " + e);
@ -372,12 +371,11 @@ function wait_for_window_open(aCallback) {
});
}
function get_string(aName) {
function get_string(aName, ...aArgs) {
var bundle = Services.strings.createBundle("chrome://mozapps/locale/extensions/extensions.properties");
if (arguments.length == 1)
if (aArgs.length == 0)
return bundle.GetStringFromName(aName);
var args = Array.slice(arguments, 1);
return bundle.formatStringFromName(aName, args, args.length);
return bundle.formatStringFromName(aName, aArgs, aArgs.length);
}
function formatDate(aDate) {
@ -923,9 +921,7 @@ MockProvider.prototype = {
*
* @param aCallback Callback to eventually call
*/
_delayCallback: function MP_delayCallback(aCallback) {
var params = Array.splice(arguments, 1);
_delayCallback: function MP_delayCallback(aCallback, ...aArgs) {
if (!this.useAsyncCallbacks) {
aCallback.apply(null, params);
return;
@ -938,7 +934,7 @@ MockProvider.prototype = {
var self = this;
timer.initWithCallback(function() {
self.callbackTimers.splice(pos, 1);
aCallback.apply(null, params);
aCallback.apply(null, aArgs);
}, this.apiDelay, timer.TYPE_ONE_SHOT);
}
};