Bug 788378 - Add guards to the AddonManager/AddonManagerInternal getters and setters, to ensure they're not used before startup/after shutdown. r=Mossop

This commit is contained in:
Blair McBride 2012-09-18 16:49:42 +12:00
parent aa4ebfe7cf
commit f3f5026492
3 changed files with 115 additions and 40 deletions

View File

@ -1932,14 +1932,26 @@ var AddonManagerInternal = {
},
get addonTypes() {
if (!gStarted)
throw Components.Exception("AddonManager is not initialized",
Cr.NS_ERROR_NOT_INITIALIZED);
return this.typesProxy;
},
get autoUpdateDefault() {
if (!gStarted)
throw Components.Exception("AddonManager is not initialized",
Cr.NS_ERROR_NOT_INITIALIZED);
return gAutoUpdateDefault;
},
set autoUpdateDefault(aValue) {
if (!gStarted)
throw Components.Exception("AddonManager is not initialized",
Cr.NS_ERROR_NOT_INITIALIZED);
aValue = !!aValue;
if (aValue != gAutoUpdateDefault)
Services.prefs.setBoolPref(PREF_EM_AUTOUPDATE_DEFAULT, aValue);
@ -1947,10 +1959,18 @@ var AddonManagerInternal = {
},
get checkCompatibility() {
if (!gStarted)
throw Components.Exception("AddonManager is not initialized",
Cr.NS_ERROR_NOT_INITIALIZED);
return gCheckCompatibility;
},
set checkCompatibility(aValue) {
if (!gStarted)
throw Components.Exception("AddonManager is not initialized",
Cr.NS_ERROR_NOT_INITIALIZED);
aValue = !!aValue;
if (aValue != gCheckCompatibility) {
if (!aValue)
@ -1962,10 +1982,18 @@ var AddonManagerInternal = {
},
get strictCompatibility() {
if (!gStarted)
throw Components.Exception("AddonManager is not initialized",
Cr.NS_ERROR_NOT_INITIALIZED);
return gStrictCompatibility;
},
set strictCompatibility(aValue) {
if (!gStarted)
throw Components.Exception("AddonManager is not initialized",
Cr.NS_ERROR_NOT_INITIALIZED);
aValue = !!aValue;
if (aValue != gStrictCompatibility)
Services.prefs.setBoolPref(PREF_EM_STRICT_COMPATIBILITY, aValue);
@ -1973,14 +2001,26 @@ var AddonManagerInternal = {
},
get checkUpdateSecurityDefault() {
if (!gStarted)
throw Components.Exception("AddonManager is not initialized",
Cr.NS_ERROR_NOT_INITIALIZED);
return gCheckUpdateSecurityDefault;
},
get checkUpdateSecurity() {
if (!gStarted)
throw Components.Exception("AddonManager is not initialized",
Cr.NS_ERROR_NOT_INITIALIZED);
return gCheckUpdateSecurity;
},
set checkUpdateSecurity(aValue) {
if (!gStarted)
throw Components.Exception("AddonManager is not initialized",
Cr.NS_ERROR_NOT_INITIALIZED);
aValue = !!aValue;
if (aValue != gCheckUpdateSecurity) {
if (aValue != gCheckUpdateSecurityDefault)
@ -1992,10 +2032,18 @@ var AddonManagerInternal = {
},
get updateEnabled() {
if (!gStarted)
throw Components.Exception("AddonManager is not initialized",
Cr.NS_ERROR_NOT_INITIALIZED);
return gUpdateEnabled;
},
set updateEnabled(aValue) {
if (!gStarted)
throw Components.Exception("AddonManager is not initialized",
Cr.NS_ERROR_NOT_INITIALIZED);
aValue = !!aValue;
if (aValue != gUpdateEnabled)
Services.prefs.setBoolPref(PREF_EM_UPDATE_ENABLED, aValue);
@ -2003,6 +2051,10 @@ var AddonManagerInternal = {
},
get hotfixID() {
if (!gStarted)
throw Components.Exception("AddonManager is not initialized",
Cr.NS_ERROR_NOT_INITIALIZED);
return gHotfixID;
},
};
@ -2340,6 +2392,10 @@ var AddonManager = {
* @return true if the addon should auto-update, false otherwise.
*/
shouldAutoUpdate: function AM_shouldAutoUpdate(aAddon) {
if (!gStarted)
throw Components.Exception("AddonManager is not initialized",
Cr.NS_ERROR_NOT_INITIALIZED);
if (!aAddon || typeof aAddon != "object")
throw Components.Exception("aAddon must be specified",
Cr.NS_ERROR_INVALID_ARG);

View File

@ -4,57 +4,75 @@
// Verify that API functions fail if the Add-ons Manager isn't initialised.
const IGNORE = ["escapeAddonURI", "shouldAutoUpdate", "getStartupChanges",
"addTypeListener", "removeTypeListener",
"addAddonListener", "removeAddonListener",
"addInstallListener", "removeInstallListener",
"addManagerListener", "removeManagerListener"];
const IGNORE = {
funcs: ["escapeAddonURI", "getStartupChanges", "addTypeListener",
"removeTypeListener", "addAddonListener", "removeAddonListener",
"addInstallListener", "removeInstallListener", "addManagerListener",
"removeManagerListener"],
getters: [],
setters: []
};
const IGNORE_PRIVATE = ["AddonAuthor", "AddonCompatibilityOverride",
"AddonScreenshot", "AddonType", "startup", "shutdown",
"registerProvider", "unregisterProvider",
"addStartupChange", "removeStartupChange"];
const IGNORE_PRIVATE = {
funcs: ["AddonAuthor", "AddonCompatibilityOverride", "AddonScreenshot",
"AddonType", "startup", "shutdown", "registerProvider",
"unregisterProvider", "addStartupChange", "removeStartupChange"],
getters: [],
setters: []
};
function test_functions() {
for (let prop in AddonManager) {
if (typeof AddonManager[prop] != "function")
continue;
if (IGNORE.indexOf(prop) != -1)
continue;
try {
do_print("AddonManager." + prop);
AddonManager[prop]();
do_throw(prop + " did not throw an exception");
}
catch (e) {
if (e.result != Components.results.NS_ERROR_NOT_INITIALIZED)
do_throw(prop + " threw an unexpected exception: " + e);
}
}
function test_functions(aObjName, aIgnore) {
let obj = this[aObjName];
for (let prop in obj) {
let desc = Object.getOwnPropertyDescriptor(obj, prop);
for (let prop in AddonManagerPrivate) {
if (typeof AddonManagerPrivate[prop] != "function")
continue;
if (IGNORE_PRIVATE.indexOf(prop) != -1)
continue;
if (typeof desc.value == "function") {
if (aIgnore.funcs.indexOf(prop) != -1)
continue;
try {
do_print("AddonManagerPrivate." + prop);
AddonManagerPrivate[prop]();
do_throw(prop + " did not throw an exception");
}
catch (e) {
if (e.result != Components.results.NS_ERROR_NOT_INITIALIZED)
do_throw(prop + " threw an unexpected exception: " + e);
try {
do_print(aObjName + "." + prop + "()");
obj[prop]();
do_throw(prop + " did not throw an exception");
}
catch (e) {
if (e.result != Components.results.NS_ERROR_NOT_INITIALIZED)
do_throw(prop + " threw an unexpected exception: " + e);
}
} else {
if (typeof desc.get == "function" && aIgnore.getters.indexOf(prop) == -1) {
do_print(aObjName + "." + prop + " getter");
try {
let temp = obj[prop];
do_throw(prop + " did not throw an exception");
}
catch (e) {
if (e.result != Components.results.NS_ERROR_NOT_INITIALIZED)
do_throw(prop + " threw an unexpected exception: " + e);
}
}
if (typeof desc.set == "function" && aIgnore.setters.indexOf(prop) == -1) {
do_print(aObjName + "." + prop + " setter");
try {
obj[prop] = "i am the walrus";
do_throw(prop + " did not throw an exception");
}
catch (e) {
if (e.result != Components.results.NS_ERROR_NOT_INITIALIZED)
do_throw(prop + " threw an unexpected exception: " + e);
}
}
}
}
}
function run_test() {
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
test_functions();
test_functions("AddonManager", IGNORE);
test_functions("AddonManagerPrivate", IGNORE_PRIVATE);
startupManager();
shutdownManager();
test_functions();
test_functions("AddonManager", IGNORE);
test_functions("AddonManagerPrivate", IGNORE_PRIVATE);
}

View File

@ -18,6 +18,7 @@ function run_test() {
testserver.start(4444);
do_test_pending();
startupManager();
run_test_1();
}