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

View File

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

View File

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

View File

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