Bug 603409 - Allow add-ons to opt out of metadata collection. r=dtownsend, a=blocking-final
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Sun, 12 Dec 2010 12:44:44 -0800
changeset 59131 d3ca4963f4b221a8f895fea2e95fd609e0ee515b
parent 59130 bdfc000c60fe8341c5ff555869a99672824d18dc
child 59132 90f6cd0ff3e7e6fae2a7d44c463882033375db36
push id1
push usershaver@mozilla.com
push dateTue, 04 Jan 2011 17:58:04 +0000
reviewersdtownsend, blocking-final
bugs603409
milestone2.0b8pre
Bug 603409 - Allow add-ons to opt out of metadata collection. r=dtownsend, a=blocking-final
toolkit/mozapps/extensions/AddonRepository.jsm
toolkit/mozapps/extensions/test/xpcshell/test_AddonRepository_cache.js
toolkit/mozapps/extensions/test/xpcshell/test_install.js
--- a/toolkit/mozapps/extensions/AddonRepository.jsm
+++ b/toolkit/mozapps/extensions/AddonRepository.jsm
@@ -528,17 +528,36 @@ var AddonRepository = {
     // Completely remove cache if caching is not enabled
     if (!this.cacheEnabled) {
       this._addons = null;
       this._pendingCallbacks = null;
       AddonDatabase.delete(aCallback);
       return;
     }
 
-    this.getAddonsByIDs(aIds, {
+    let enabledIds = aIds.filter(function(aId) {
+      let preference = "extensions." + aId + ".getAddons.cache.enabled";
+      let enabled = true;
+      try {
+        enabled = Services.prefs.getBoolPref(preference);
+      } catch(e) {
+        // If pref doesn't exist, default to enabled = true
+      }
+      return enabled;
+    });
+
+    // Completely remove cache if there are no add-ons to cache
+    if (enabledIds.length == 0) {
+      this._addons = null;
+      this._pendingCallbacks = null;
+      AddonDatabase.delete(aCallback);
+      return;
+    }
+
+    this.getAddonsByIDs(enabledIds, {
       searchSucceeded: function(aAddons) {
         self._addons = {};
         aAddons.forEach(function(aAddon) { self._addons[aAddon.id] = aAddon; });
         AddonDatabase.repopulate(aAddons, aCallback);
       },
       searchFailed: function() {
         WARN("Search failed when repopulating cache");
         if (aCallback)
@@ -559,18 +578,36 @@ var AddonRepository = {
    */
   cacheAddons: function(aIds, aCallback) {
     if (!this.cacheEnabled) {
       if (aCallback)
         aCallback();
       return;
     }
 
+    let enabledIds = aIds.filter(function(aId) {
+      let preference = "extensions." + aId + ".getAddons.cache.enabled";
+      let enabled = true;
+      try {
+        enabled = Services.prefs.getBoolPref(preference);
+      } catch(e) {
+        // If pref doesn't exist, default to enabled = true
+      }
+      return enabled;
+    });
+
+    // If there are no add-ons to cache, act as if caching is disabled
+    if (enabledIds.length == 0) {
+      if (aCallback)
+        aCallback();
+      return;
+    }
+
     let self = this;
-    this.getAddonsByIDs(aIds, {
+    this.getAddonsByIDs(enabledIds, {
       searchSucceeded: function(aAddons) {
         aAddons.forEach(function(aAddon) { self._addons[aAddon.id] = aAddon; });
         AddonDatabase.insertAddons(aAddons, aCallback);
       },
       searchFailed: function() {
         WARN("Search failed when adding add-ons to cache");
         if (aCallback)
           aCallback();
--- a/toolkit/mozapps/extensions/test/xpcshell/test_AddonRepository_cache.js
+++ b/toolkit/mozapps/extensions/test/xpcshell/test_AddonRepository_cache.js
@@ -20,16 +20,18 @@ const GETADDONS_FAILED             = BAS
 
 const FILE_DATABASE = "addons.sqlite";
 const ADDON_NAMES = ["test_AddonRepository_1",
                      "test_AddonRepository_2",
                      "test_AddonRepository_3"];
 const ADDON_IDS = ADDON_NAMES.map(function(aName) aName + "@tests.mozilla.org");
 const ADDON_FILES = ADDON_NAMES.map(do_get_addon);
 
+const PREF_ADDON0_CACHE_ENABLED = "extensions." + ADDON_IDS[0] + ".getAddons.cache.enabled";
+const PREF_ADDON1_CACHE_ENABLED = "extensions." + ADDON_IDS[1] + ".getAddons.cache.enabled";
 
 // Properties of an individual add-on that should be checked
 // Note: size and updateDate are checked separately
 const ADDON_PROPERTIES = ["id", "type", "name", "version", "creator",
                           "developers", "translators", "contributors",
                           "description", "fullDescription",
                           "developerComments", "eula", "iconURL",
                           "screenshots", "homepageURL", "supportURL",
@@ -460,17 +462,28 @@ function run_test_4() {
   });
 }
 
 // Tests repopulateCache when search returns results
 function run_test_5() {
   Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_RESULTS);
 
   AddonRepository.repopulateCache(ADDON_IDS, function() {
-    check_initialized_cache([true, true, true], run_test_6);
+    check_initialized_cache([true, true, true], run_test_5_1);
+  });
+}
+
+// Tests repopulateCache when caching is disabled for a single add-on
+function run_test_5_1() {
+  Services.prefs.setBoolPref(PREF_ADDON0_CACHE_ENABLED, false);
+
+  AddonRepository.repopulateCache(ADDON_IDS, function() {
+    // Reset pref for next test
+    Services.prefs.setBoolPref(PREF_ADDON0_CACHE_ENABLED, true);
+    check_initialized_cache([false, true, true], run_test_6);
   });
 }
 
 // Tests repopulateCache when caching is disabled
 function run_test_6() {
   check_database_exists(true);
   Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, false);
 
@@ -502,17 +515,28 @@ function run_test_8() {
   });
 }
 
 // Tests cacheAddons for a single add-on when search returns results
 function run_test_9() {
   Services.prefs.setCharPref(PREF_GETADDONS_BYIDS, GETADDONS_RESULTS);
 
   AddonRepository.cacheAddons([ADDON_IDS[0]], function() {
-    check_initialized_cache([true, false, false], run_test_10);
+    check_initialized_cache([true, false, false], run_test_9_1);
+  });
+}
+
+// Tests cacheAddons when caching is disabled for a single add-on
+function run_test_9_1() {
+  Services.prefs.setBoolPref(PREF_ADDON1_CACHE_ENABLED, false);
+
+  AddonRepository.cacheAddons(ADDON_IDS, function() {
+    // Reset pref for next test
+    Services.prefs.setBoolPref(PREF_ADDON1_CACHE_ENABLED, true);
+    check_initialized_cache([true, false, true], run_test_10);
   });
 }
 
 // Tests cacheAddons for multiple add-ons, some already in the cache,
 function run_test_10() {
   AddonRepository.cacheAddons(ADDON_IDS, function() {
     check_initialized_cache([true, true, true], run_test_11);
   });
--- a/toolkit/mozapps/extensions/test/xpcshell/test_install.js
+++ b/toolkit/mozapps/extensions/test/xpcshell/test_install.js
@@ -1169,36 +1169,64 @@ function run_test_18() {
 
                 AddonManager.getAddonByID("addon2@tests.mozilla.org", function(a2) {
                   do_check_false(a2.userDisabled);
                   do_check_true(a2.isActive);
 
                   a2.uninstall();
                   restartManager();
 
-                  run_test_19();
+                  run_test_18_1();
                 });
               }
             });
             aInstall.install();
           }, "application/x-xpinstall");
         });
       }
     });
     aInstall.install();
   }, "application/x-xpinstall");
 }
 
-// Checks that metadata is downloaded for new installs and is visible before and
-// after restart
-function run_test_19() {
+
+// Checks that metadata is not stored if the pref is set to false
+function run_test_18_1() {
   Services.prefs.setBoolPref("extensions.getAddons.cache.enabled", true);
   Services.prefs.setCharPref("extensions.getAddons.get.url",
                              "http://localhost:4444/data/test_install.xml");
 
+  Services.prefs.setBoolPref("extensions.addon2@tests.mozilla.org.getAddons.cache.enabled", false);
+
+  let url = "http://localhost:4444/addons/test_install2_1.xpi";
+  AddonManager.getInstallForURL(url, function(aInstall) {
+    aInstall.addListener({
+      onInstallEnded: function(aInstall, aAddon) {
+        do_check_neq(aAddon.fullDescription, "Repository description");
+
+        restartManager();
+
+        AddonManager.getAddonByID("addon2@tests.mozilla.org", function(a2) {
+          do_check_neq(a2.fullDescription, "Repository description");
+
+          a2.uninstall();
+          restartManager();
+
+          Services.prefs.setBoolPref("extensions.addon2@tests.mozilla.org.getAddons.cache.enabled", true);
+          run_test_19();
+        });
+      }
+    });
+    aInstall.install();
+  }, "application/x-xpinstall");
+}
+
+// Checks that metadata is downloaded for new installs and is visible before and
+// after restart
+function run_test_19() {
   let url = "http://localhost:4444/addons/test_install2_1.xpi";
   AddonManager.getInstallForURL(url, function(aInstall) {
     aInstall.addListener({
       onInstallEnded: function(aInstall, aAddon) {
         do_check_eq(aAddon.fullDescription, "Repository description");
 
         restartManager();