From d72258e637e954674a5695f85326a05e3a9e9ada Mon Sep 17 00:00:00 2001 From: Geoff Lankow Date: Wed, 18 Jul 2012 01:24:51 +1200 Subject: [PATCH] Bug 767320 - Cache results of Addon.hasResource() to try avoid unnecessary IO; r=Unfocused --- toolkit/mozapps/extensions/XPIProvider.jsm | 20 ++++++++++++++++--- .../test/xpcshell/test_getresource.js | 13 ++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/toolkit/mozapps/extensions/XPIProvider.jsm b/toolkit/mozapps/extensions/XPIProvider.jsm index 7215981676b..e1140cd0416 100644 --- a/toolkit/mozapps/extensions/XPIProvider.jsm +++ b/toolkit/mozapps/extensions/XPIProvider.jsm @@ -3842,6 +3842,9 @@ var XPIProvider = { if (aAddon._installLocation.locked) throw new Error("Cannot uninstall addons from locked install locations"); + if ("_hasResourceCache" in aAddon) + aAddon._hasResourceCache = new Map(); + // Inactive add-ons don't require a restart to uninstall let requiresRestart = this.uninstallRequiresRestart(aAddon); @@ -5710,8 +5713,10 @@ XPIProvider.DBAddonInternal = DBAddonInternal; function createWrapper(aAddon) { if (!aAddon) return null; - if (!aAddon._wrapper) + if (!aAddon._wrapper) { + aAddon._hasResourceCache = new Map(); aAddon._wrapper = new AddonWrapper(aAddon); + } return aAddon._wrapper; } @@ -6106,6 +6111,9 @@ function AddonWrapper(aAddon) { }; this.hasResource = function(aPath) { + if (aAddon._hasResourceCache.has(aPath)) + return aAddon._hasResourceCache.get(aPath); + let bundle = aAddon._sourceBundle.clone(); // Bundle may not exist any more if the addon has just been uninstalled, @@ -6113,6 +6121,7 @@ function AddonWrapper(aAddon) { try { var isDir = bundle.isDirectory(); } catch (e) { + aAddon._hasResourceCache.set(aPath, false); return false; } @@ -6122,16 +6131,21 @@ function AddonWrapper(aAddon) { bundle.append(aPart); }); } - return bundle.exists(); + let result = bundle.exists(); + aAddon._hasResourceCache.set(aPath, result); + return result; } let zipReader = Cc["@mozilla.org/libjar/zip-reader;1"]. createInstance(Ci.nsIZipReader); try { zipReader.open(bundle); - return zipReader.hasEntry(aPath); + let result = zipReader.hasEntry(aPath); + aAddon._hasResourceCache.set(aPath, result); + return result; } catch (e) { + aAddon._hasResourceCache.set(aPath, false); return false; } finally { diff --git a/toolkit/mozapps/extensions/test/xpcshell/test_getresource.js b/toolkit/mozapps/extensions/test/xpcshell/test_getresource.js index 67a093dc896..33c72182dbd 100644 --- a/toolkit/mozapps/extensions/test/xpcshell/test_getresource.js +++ b/toolkit/mozapps/extensions/test/xpcshell/test_getresource.js @@ -75,12 +75,17 @@ function run_test() { do_check_true(false); } - restartManager(); + AddonManager.getInstallForFile(do_get_addon("test_getresource"), function(aInstall) { + do_check_false(a1.hasResource("icon.png")); + do_check_true(aInstall.addon.hasResource("icon.png")); - AddonManager.getAddonByID("addon1@tests.mozilla.org", function(newa1) { - do_check_eq(newa1, null); + restartManager(); - do_test_finished(); + AddonManager.getAddonByID("addon1@tests.mozilla.org", function(newa1) { + do_check_eq(newa1, null); + + do_test_finished(); + }); }); }); });