From 44323325c810c820c1e7700500aa7bb1914b5202 Mon Sep 17 00:00:00 2001 From: Blair McBride Date: Fri, 25 Oct 2013 14:22:32 +1300 Subject: [PATCH] Bug 697314 - Defer importing xpinstall permissions until necessary. r=Mossop --HG-- rename : toolkit/mozapps/extensions/test/xpcshell/test_bug578467.js => toolkit/mozapps/extensions/test/xpcshell/test_permissions_prefs.js --- browser/components/preferences/permissions.js | 3 + toolkit/mozapps/extensions/XPIProvider.jsm | 34 +++++---- .../test/xpcshell/test_bug578467.js | 37 ---------- .../test/xpcshell/test_permissions_prefs.js | 71 +++++++++++++++++++ .../test/xpcshell/xpcshell-shared.ini | 2 +- 5 files changed, 96 insertions(+), 51 deletions(-) delete mode 100644 toolkit/mozapps/extensions/test/xpcshell/test_bug578467.js create mode 100644 toolkit/mozapps/extensions/test/xpcshell/test_permissions_prefs.js diff --git a/browser/components/preferences/permissions.js b/browser/components/preferences/permissions.js index 7ad74b992318..272526c4931d 100644 --- a/browser/components/preferences/permissions.js +++ b/browser/components/preferences/permissions.js @@ -6,6 +6,8 @@ const nsIPermissionManager = Components.interfaces.nsIPermissionManager; const nsICookiePermission = Components.interfaces.nsICookiePermission; +const NOTIFICATION_FLUSH_PERMISSIONS = "flush-pending-permissions"; + function Permission(host, rawHost, type, capability, perm) { this.host = host; @@ -183,6 +185,7 @@ var gPermissionManager = { var os = Components.classes["@mozilla.org/observer-service;1"] .getService(Components.interfaces.nsIObserverService); + os.notifyObservers(null, NOTIFICATION_FLUSH_PERMISSIONS, this._type); os.addObserver(this, "perm-changed", false); this._loadPermissions(); diff --git a/toolkit/mozapps/extensions/XPIProvider.jsm b/toolkit/mozapps/extensions/XPIProvider.jsm index 59f6011d63d3..ee3edf105c3f 100644 --- a/toolkit/mozapps/extensions/XPIProvider.jsm +++ b/toolkit/mozapps/extensions/XPIProvider.jsm @@ -102,6 +102,7 @@ const KEY_APP_SYSTEM_LOCAL = "app-system-local"; const KEY_APP_SYSTEM_SHARE = "app-system-share"; const KEY_APP_SYSTEM_USER = "app-system-user"; +const NOTIFICATION_FLUSH_PERMISSIONS = "flush-pending-permissions"; const XPI_PERMISSION = "install"; const RDFURI_INSTALL_MANIFEST_ROOT = "urn:mozilla:install-manifest"; @@ -1952,6 +1953,7 @@ var XPIProvider = { Services.prefs.addObserver(PREF_EM_MIN_COMPAT_APP_VERSION, this, false); Services.prefs.addObserver(PREF_EM_MIN_COMPAT_PLATFORM_VERSION, this, false); + Services.obs.addObserver(this, NOTIFICATION_FLUSH_PERMISSIONS, false); let flushCaches = this.checkForChanges(aAppChanged, aOldAppVersion, aOldPlatformVersion); @@ -3322,10 +3324,6 @@ var XPIProvider = { aOldPlatformVersion) { LOG("checkForChanges"); - // Import the website installation permissions if the application has changed - if (aAppChanged !== false) - this.importPermissions(); - // Keep track of whether and why we need to open and update the database at // startup time. let updateReasons = []; @@ -3521,6 +3519,7 @@ var XPIProvider = { if (aUri.schemeIs("chrome") || aUri.schemeIs("file")) return true; + this.importPermissions(); let permission = Services.perms.testPermission(aUri, XPI_PERMISSION); if (permission == Ci.nsIPermissionManager.DENY_ACTION) @@ -3849,15 +3848,24 @@ var XPIProvider = { * @see nsIObserver */ observe: function XPI_observe(aSubject, aTopic, aData) { - switch (aData) { - case PREF_EM_MIN_COMPAT_APP_VERSION: - case PREF_EM_MIN_COMPAT_PLATFORM_VERSION: - this.minCompatibleAppVersion = Prefs.getCharPref(PREF_EM_MIN_COMPAT_APP_VERSION, - null); - this.minCompatiblePlatformVersion = Prefs.getCharPref(PREF_EM_MIN_COMPAT_PLATFORM_VERSION, - null); - this.updateAddonAppDisabledStates(); - break; + if (aTopic == NOTIFICATION_FLUSH_PERMISSIONS) { + if (!aData || aData == XPI_PERMISSION) { + this.importPermissions(); + } + return; + } + + if (aTopic == "nsPref:changed") { + switch (aData) { + case PREF_EM_MIN_COMPAT_APP_VERSION: + case PREF_EM_MIN_COMPAT_PLATFORM_VERSION: + this.minCompatibleAppVersion = Prefs.getCharPref(PREF_EM_MIN_COMPAT_APP_VERSION, + null); + this.minCompatiblePlatformVersion = Prefs.getCharPref(PREF_EM_MIN_COMPAT_PLATFORM_VERSION, + null); + this.updateAddonAppDisabledStates(); + break; + } } }, diff --git a/toolkit/mozapps/extensions/test/xpcshell/test_bug578467.js b/toolkit/mozapps/extensions/test/xpcshell/test_bug578467.js deleted file mode 100644 index 16817f1c7160..000000000000 --- a/toolkit/mozapps/extensions/test/xpcshell/test_bug578467.js +++ /dev/null @@ -1,37 +0,0 @@ -/* Any copyright is dedicated to the Public Domain. - * http://creativecommons.org/publicdomain/zero/1.0/ - */ - -// Tests that xpinstall.[whitelist|blacklist].add preferences are emptied when -// converted into permissions on startup with new profile - -const PREF_XPI_WHITELIST_PERMISSIONS = "xpinstall.whitelist.add"; -const PREF_XPI_BLACKLIST_PERMISSIONS = "xpinstall.blacklist.add"; - -function run_test() { - createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9"); - - // Create own preferences to test - Services.prefs.setCharPref("xpinstall.whitelist.add.EMPTY", ""); - Services.prefs.setCharPref("xpinstall.whitelist.add.TEST", "whitelist.example.com"); - Services.prefs.setCharPref("xpinstall.blacklist.add.EMPTY", ""); - Services.prefs.setCharPref("xpinstall.blacklist.add.TEST", "blacklist.example.com"); - - // Get list of preferences to check - var whitelistPreferences = Services.prefs.getChildList(PREF_XPI_WHITELIST_PERMISSIONS, {}); - var blacklistPreferences = Services.prefs.getChildList(PREF_XPI_BLACKLIST_PERMISSIONS, {}); - var preferences = whitelistPreferences.concat(blacklistPreferences); - - startupManager(); - - // Check preferences were emptied - preferences.forEach(function(aPreference) { - try { - do_check_eq(Services.prefs.getCharPref(aPreference), ""); - } - catch (e) { - // Successfully emptied - } - }); -} - diff --git a/toolkit/mozapps/extensions/test/xpcshell/test_permissions_prefs.js b/toolkit/mozapps/extensions/test/xpcshell/test_permissions_prefs.js new file mode 100644 index 000000000000..c7c31dfd764c --- /dev/null +++ b/toolkit/mozapps/extensions/test/xpcshell/test_permissions_prefs.js @@ -0,0 +1,71 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +// Tests that xpinstall.[whitelist|blacklist].add preferences are emptied when +// converted into permissions. + +const PREF_XPI_WHITELIST_PERMISSIONS = "xpinstall.whitelist.add"; +const PREF_XPI_BLACKLIST_PERMISSIONS = "xpinstall.blacklist.add"; + +function do_check_permission_prefs(preferences) { + // Check preferences were emptied + for (let pref of preferences) { + try { + do_check_eq(Services.prefs.getCharPref(pref), ""); + } + catch (e) { + // Successfully emptied + } + } +} + +function clear_imported_preferences_cache() { + let scope = Components.utils.import("resource://gre/modules/PermissionsUtils.jsm", {}); + scope.gImportedPrefBranches.clear(); +} + +function run_test() { + createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9"); + + // Create own preferences to test + Services.prefs.setCharPref("xpinstall.whitelist.add.EMPTY", ""); + Services.prefs.setCharPref("xpinstall.whitelist.add.TEST", "whitelist.example.com"); + Services.prefs.setCharPref("xpinstall.blacklist.add.EMPTY", ""); + Services.prefs.setCharPref("xpinstall.blacklist.add.TEST", "blacklist.example.com"); + + // Get list of preferences to check + var whitelistPreferences = Services.prefs.getChildList(PREF_XPI_WHITELIST_PERMISSIONS, {}); + var blacklistPreferences = Services.prefs.getChildList(PREF_XPI_BLACKLIST_PERMISSIONS, {}); + var preferences = whitelistPreferences.concat(blacklistPreferences); + + startupManager(); + + // Permissions are imported lazily - act as thought we're checking an install, + // to trigger on-deman importing of the permissions. + let url = Services.io.newURI("http://example.com/file.xpi", null, null); + AddonManager.isInstallAllowed("application/x-xpinstall", url); + do_check_permission_prefs(preferences); + + + // Import can also be triggerred by an observer notification by any other area + // of code, such as a permissions management UI. + + // First, request to flush all permissions + clear_imported_preferences_cache(); + Services.prefs.setCharPref("xpinstall.whitelist.add.TEST2", "whitelist2.example.com"); + Services.obs.notifyObservers(null, "flush-pending-permissions", "install"); + do_check_permission_prefs(preferences); + + // Then, request to flush just install permissions + clear_imported_preferences_cache(); + Services.prefs.setCharPref("xpinstall.whitelist.add.TEST3", "whitelist3.example.com"); + Services.obs.notifyObservers(null, "flush-pending-permissions", ""); + do_check_permission_prefs(preferences); + + // And a request to flush some other permissions sholdn't flush install permissions + clear_imported_preferences_cache(); + Services.prefs.setCharPref("xpinstall.whitelist.add.TEST4", "whitelist4.example.com"); + Services.obs.notifyObservers(null, "flush-pending-permissions", "lolcats"); + do_check_eq(Services.prefs.getCharPref("xpinstall.whitelist.add.TEST4"), "whitelist4.example.com"); +} diff --git a/toolkit/mozapps/extensions/test/xpcshell/xpcshell-shared.ini b/toolkit/mozapps/extensions/test/xpcshell/xpcshell-shared.ini index 7571de2fbed3..541da136a1d9 100644 --- a/toolkit/mozapps/extensions/test/xpcshell/xpcshell-shared.ini +++ b/toolkit/mozapps/extensions/test/xpcshell/xpcshell-shared.ini @@ -122,7 +122,6 @@ fail-if = os == "android" [test_bug569138.js] [test_bug570173.js] [test_bug576735.js] -[test_bug578467.js] [test_bug587088.js] [test_bug594058.js] [test_bug595081.js] @@ -205,6 +204,7 @@ skip-if = os == "android" [test_migrate_max_version.js] [test_onPropertyChanged_appDisabled.js] [test_permissions.js] +[test_permissions_prefs.js] [test_plugins.js] [test_pluginchange.js] [test_pluginBlocklistCtp.js]