From 16e6354db14f934ce5e6abfa538c6b49cbd80e30 Mon Sep 17 00:00:00 2001 From: Dave Townsend Date: Wed, 9 May 2012 14:54:02 -0700 Subject: [PATCH] Bug 740612: Inject values into bootstrap scope before running bootstrap.js and properly handle errors. r=Unfocused --- toolkit/mozapps/extensions/XPIProvider.jsm | 21 +++--- .../test/addons/test_bug740612_1/bootstrap.js | 1 + .../test/addons/test_bug740612_1/install.rdf | 24 +++++++ .../test/addons/test_bug740612_2/bootstrap.js | 23 +++++++ .../test/addons/test_bug740612_2/install.rdf | 24 +++++++ .../test/xpcshell/test_bug740612.js | 69 +++++++++++++++++++ .../extensions/test/xpcshell/xpcshell.ini | 1 + 7 files changed, 152 insertions(+), 11 deletions(-) create mode 100644 toolkit/mozapps/extensions/test/addons/test_bug740612_1/bootstrap.js create mode 100644 toolkit/mozapps/extensions/test/addons/test_bug740612_1/install.rdf create mode 100644 toolkit/mozapps/extensions/test/addons/test_bug740612_2/bootstrap.js create mode 100644 toolkit/mozapps/extensions/test/addons/test_bug740612_2/install.rdf create mode 100644 toolkit/mozapps/extensions/test/xpcshell/test_bug740612.js diff --git a/toolkit/mozapps/extensions/XPIProvider.jsm b/toolkit/mozapps/extensions/XPIProvider.jsm index c4b86b106cd..2afa51a5c8c 100644 --- a/toolkit/mozapps/extensions/XPIProvider.jsm +++ b/toolkit/mozapps/extensions/XPIProvider.jsm @@ -3597,6 +3597,16 @@ var XPIProvider = { createInstance(Ci.mozIJSSubScriptLoader); try { + // Copy the reason values from the global object into the bootstrap scope. + for (let name in BOOTSTRAP_REASONS) + this.bootstrapScopes[aId][name] = BOOTSTRAP_REASONS[name]; + + // Add other stuff that extensions want. + const features = [ "Worker", "ChromeWorker" ]; + + for (let feature of features) + this.bootstrapScopes[aId][feature] = gGlobalScope[feature]; + // As we don't want our caller to control the JS version used for the // bootstrap file, we run loadSubScript within the context of the // sandbox with the latest JS version set explicitly. @@ -3614,17 +3624,6 @@ var XPIProvider = { catch (e) { WARN("Error loading bootstrap.js for " + aId, e); } - - // Copy the reason values from the global object into the bootstrap scope. - for (let name in BOOTSTRAP_REASONS) - this.bootstrapScopes[aId][name] = BOOTSTRAP_REASONS[name]; - - - // Add other stuff that extensions want. - const features = [ "Worker", "ChromeWorker" ]; - - for each (let feature in features) - this.bootstrapScopes[aId][feature] = gGlobalScope[feature]; }, /** diff --git a/toolkit/mozapps/extensions/test/addons/test_bug740612_1/bootstrap.js b/toolkit/mozapps/extensions/test/addons/test_bug740612_1/bootstrap.js new file mode 100644 index 00000000000..6703e7f7d7e --- /dev/null +++ b/toolkit/mozapps/extensions/test/addons/test_bug740612_1/bootstrap.js @@ -0,0 +1 @@ +const APP_STARTUP = 1; diff --git a/toolkit/mozapps/extensions/test/addons/test_bug740612_1/install.rdf b/toolkit/mozapps/extensions/test/addons/test_bug740612_1/install.rdf new file mode 100644 index 00000000000..b2316273f91 --- /dev/null +++ b/toolkit/mozapps/extensions/test/addons/test_bug740612_1/install.rdf @@ -0,0 +1,24 @@ + + + + + + bug740612_1@tests.mozilla.org + 1.0 + true + + + Test Bootstrap 1 + Test Description + + + + xpcshell@tests.mozilla.org + 1 + 1 + + + + + diff --git a/toolkit/mozapps/extensions/test/addons/test_bug740612_2/bootstrap.js b/toolkit/mozapps/extensions/test/addons/test_bug740612_2/bootstrap.js new file mode 100644 index 00000000000..2ad48145318 --- /dev/null +++ b/toolkit/mozapps/extensions/test/addons/test_bug740612_2/bootstrap.js @@ -0,0 +1,23 @@ +Components.utils.import("resource://gre/modules/Services.jsm"); + +const VERSION = "1.0"; + +function install(data, reason) { + Services.prefs.setIntPref("bootstraptest.installed_version", VERSION); + Services.prefs.setIntPref("bootstraptest.install_reason", reason); +} + +function startup(data, reason) { + Services.prefs.setIntPref("bootstraptest.active_version", VERSION); + Services.prefs.setIntPref("bootstraptest.startup_reason", reason); +} + +function shutdown(data, reason) { + Services.prefs.setIntPref("bootstraptest.active_version", 0); + Services.prefs.setIntPref("bootstraptest.shutdown_reason", reason); +} + +function uninstall(data, reason) { + Services.prefs.setIntPref("bootstraptest.installed_version", 0); + Services.prefs.setIntPref("bootstraptest.uninstall_reason", reason); +} diff --git a/toolkit/mozapps/extensions/test/addons/test_bug740612_2/install.rdf b/toolkit/mozapps/extensions/test/addons/test_bug740612_2/install.rdf new file mode 100644 index 00000000000..ff4d613ef2f --- /dev/null +++ b/toolkit/mozapps/extensions/test/addons/test_bug740612_2/install.rdf @@ -0,0 +1,24 @@ + + + + + + bug740612_2@tests.mozilla.org + 1.0 + true + + + Test Bootstrap 2 + Test Description + + + + xpcshell@tests.mozilla.org + 1 + 1 + + + + + diff --git a/toolkit/mozapps/extensions/test/xpcshell/test_bug740612.js b/toolkit/mozapps/extensions/test/xpcshell/test_bug740612.js new file mode 100644 index 00000000000..294c8f216f3 --- /dev/null +++ b/toolkit/mozapps/extensions/test/xpcshell/test_bug740612.js @@ -0,0 +1,69 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +// This verifies that attempts to override the global values fails but doesn't +// destroy the world with it +createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2"); + +const profileDir = gProfD.clone(); +profileDir.append("extensions"); + +function getActiveVersion() { + return Services.prefs.getIntPref("bootstraptest.active_version"); +} + +function getInstalledVersion() { + return Services.prefs.getIntPref("bootstraptest.installed_version"); +} + +function manuallyInstall(aXPIFile, aInstallLocation, aID) { + if (TEST_UNPACKED) { + let dir = aInstallLocation.clone(); + dir.append(aID); + dir.create(AM_Ci.nsIFile.DIRECTORY_TYPE, 0755); + let zip = AM_Cc["@mozilla.org/libjar/zip-reader;1"]. + createInstance(AM_Ci.nsIZipReader); + zip.open(aXPIFile); + let entries = zip.findEntries(null); + while (entries.hasMore()) { + let entry = entries.getNext(); + let target = dir.clone(); + entry.split("/").forEach(function(aPart) { + target.append(aPart); + }); + zip.extract(entry, target); + } + zip.close(); + + return dir; + } + else { + let target = aInstallLocation.clone(); + target.append(aID + ".xpi"); + aXPIFile.copyTo(target.parent, target.leafName); + return target; + } +} + +function run_test() { + do_test_pending(); + + manuallyInstall(do_get_addon("test_bug740612_1"), profileDir, + "bug740612_1@tests.mozilla.org"); + manuallyInstall(do_get_addon("test_bug740612_2"), profileDir, + "bug740612_2@tests.mozilla.org"); + + startupManager(); + + AddonManager.getAddonsByIDs(["bug740612_1@tests.mozilla.org", + "bug740612_2@tests.mozilla.org"], + function([a1, a2]) { + do_check_neq(a1, null); + do_check_neq(a2, null); + do_check_eq(getInstalledVersion(), "1.0"); + do_check_eq(getActiveVersion(), "1.0"); + + do_test_finished(); + }); +} diff --git a/toolkit/mozapps/extensions/test/xpcshell/xpcshell.ini b/toolkit/mozapps/extensions/test/xpcshell/xpcshell.ini index aa14e3b7d31..0f865afbab2 100644 --- a/toolkit/mozapps/extensions/test/xpcshell/xpcshell.ini +++ b/toolkit/mozapps/extensions/test/xpcshell/xpcshell.ini @@ -128,6 +128,7 @@ fail-if = os == "android" [test_bug655254.js] [test_bug659772.js] [test_bug675371.js] +[test_bug740612.js] [test_cacheflush.js] [test_checkcompatibility.js] [test_ChromeManifestParser.js]