Bug 711263 - Don't register AddonsReconciler unless addons engine is enabled; r=philikon
authorGregory Szorc <gps@mozilla.com>
Mon, 19 Dec 2011 17:13:33 -0800
changeset 84654 c53e7d91cd502dc27a18d1d5a6f0755f1037ac02
parent 84653 f98c57415d8d08e9e4c404b7b2753827131e3231
child 84655 0885bdf12669511be360a8ad77ff35b69a899f80
push id519
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 00:38:35 +0000
treeherdermozilla-beta@788ea1ef610b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersphilikon
bugs711263
milestone11.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 711263 - Don't register AddonsReconciler unless addons engine is enabled; r=philikon
services/sync/modules/addonsreconciler.js
services/sync/tests/unit/test_addons_engine.js
services/sync/tests/unit/test_addons_reconciler.js
services/sync/tests/unit/test_addons_store.js
services/sync/tests/unit/test_addons_tracker.js
--- a/services/sync/modules/addonsreconciler.js
+++ b/services/sync/modules/addonsreconciler.js
@@ -48,16 +48,17 @@
  * hopefully ported.
  */
 
 "use strict";
 
 const Cu = Components.utils;
 
 Cu.import("resource://services-sync/log4moz.js");
+Cu.import("resource://services-sync/engines.js");
 Cu.import("resource://services-sync/util.js");
 Cu.import("resource://gre/modules/AddonManager.jsm");
 
 const DEFAULT_STATE_FILE = "addonsreconciler";
 
 const CHANGE_INSTALLED   = 1;
 const CHANGE_UNINSTALLED = 2;
 const CHANGE_ENABLED     = 3;
@@ -146,21 +147,19 @@ const EXPORTED_SYMBOLS = ["AddonsReconci
  * events will occur immediately. However, we still see disabling events and
  * heed them like they were normal. In the end, the state is proper.
  */
 function AddonsReconciler() {
   this._log = Log4Moz.repository.getLogger("Sync.AddonsReconciler");
   let level = Svc.Prefs.get("log.logger.addonsreconciler", "Debug");
   this._log.level = Log4Moz.Level[level];
 
-  AddonManager.addAddonListener(this);
-  AddonManager.addInstallListener(this);
-  this._listening = true;
-
-  Svc.Obs.add("xpcom-shutdown", this.stopListening.bind(this));
+  Svc.Obs.add("weave:engine:start-tracking", this.startListening, this);
+  Svc.Obs.add("weave:engine:stop-tracking", this.stopListening, this);
+  Svc.Obs.add("xpcom-shutdown", this.stopListening, this);
 };
 AddonsReconciler.prototype = {
   /** Flag indicating whether we are listening to AddonManager events. */
   _listening: false,
 
   /** Whether state has been loaded from a file.
    *
    * State is loaded on demand if an operation requires it.
@@ -325,31 +324,50 @@ AddonsReconciler.prototype = {
         return false;
       } else {
         return true;
       }
     }.bind(this));
   },
 
   /**
+   * Tells the instance to start listening for AddonManager changes.
+   *
+   * This is typically called automatically when Sync is loaded.
+   */
+  startListening: function startListening() {
+    let engine = Engines.get("addons");
+    if (!engine || !engine.enabled || this._listening) {
+      return;
+    }
+
+    this._log.info("Registering as Add-on Manager listener.");
+    AddonManager.addAddonListener(this);
+    AddonManager.addInstallListener(this);
+    this._listening = true;
+  },
+
+  /**
    * Tells the instance to stop listening for AddonManager changes.
    *
    * The reconciler should always be listening. This should only be called when
    * the instance is being destroyed.
    *
    * This function will get called automatically on XPCOM shutdown. However, it
    * is a best practice to call it yourself.
    */
   stopListening: function stopListening() {
-    if (this._listening) {
-      this._log.debug("Stopping listening and removing AddonManager listeners.");
-      AddonManager.removeInstallListener(this);
-      AddonManager.removeAddonListener(this);
-      this._listening = false;
+    if (!this._listening) {
+      return;
     }
+
+    this._log.debug("Stopping listening and removing AddonManager listeners.");
+    AddonManager.removeInstallListener(this);
+    AddonManager.removeAddonListener(this);
+    this._listening = false;
   },
 
   /**
    * Refreshes the global state of add-ons by querying the AddonManager.
    */
   refreshGlobalState: function refreshGlobalState(callback) {
     this._log.info("Refreshing global state from AddonManager.");
     this._ensureStateLoaded();
--- a/services/sync/tests/unit/test_addons_engine.js
+++ b/services/sync/tests/unit/test_addons_engine.js
@@ -132,10 +132,15 @@ add_test(function test_get_changed_ids()
   do_check_eq(0, Object.keys(changes).length);
 
   advance_test();
 });
 
 function run_test() {
   initTestLogging("Trace");
   Log4Moz.repository.getLogger("Sync.Engine.Addons").level = Log4Moz.Level.Trace;
+  Log4Moz.repository.getLogger("Sync.AddonsRepository").level =
+    Log4Moz.Level.Trace;
+
+  reconciler.startListening();
+
   advance_test();
 }
--- a/services/sync/tests/unit/test_addons_reconciler.js
+++ b/services/sync/tests/unit/test_addons_reconciler.js
@@ -1,32 +1,38 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
-Cu.import("resource://services-sync/addonsreconciler.js");
+Cu.import("resource://services-sync/AddonsReconciler.js");
+Cu.import("resource://services-sync/engines/addons.js");
 Cu.import("resource://gre/modules/AddonManager.jsm");
 
 loadAddonTestFunctions();
 startupManager();
 
 function run_test() {
   initTestLogging("Trace");
   Log4Moz.repository.getLogger("Sync.AddonsReconciler").level = Log4Moz.Level.Trace;
+  Log4Moz.repository.getLogger("Sync.AddonsReconciler").level =
+    Log4Moz.Level.Trace;
+
+  Svc.Prefs.set("engine.addons", true);
+  Engines.register(AddonsEngine);
 
   run_next_test();
 }
 
 add_test(function test_defaults() {
   _("Ensure new objects have reasonable defaults.");
 
   let reconciler = new AddonsReconciler();
 
-  do_check_true(reconciler._listening);
+  do_check_false(reconciler._listening);
   do_check_eq("object", typeof(reconciler.addons));
   do_check_eq(0, Object.keys(reconciler.addons).length);
   do_check_eq(0, reconciler._changes.length);
   do_check_eq(0, reconciler._listeners.length);
 
   run_next_test();
 });
 
@@ -46,16 +52,17 @@ add_test(function test_load_state_empty_
     run_next_test();
   });
 });
 
 add_test(function test_install_detection() {
   _("Ensure that add-on installation results in appropriate side-effects.");
 
   let reconciler = new AddonsReconciler();
+  reconciler.startListening();
 
   let before = new Date();
   let addon = installAddon("test_bootstrap1_1");
   let after = new Date();
 
   do_check_eq(1, Object.keys(reconciler.addons).length);
   do_check_true(addon.id in reconciler.addons);
   let record = reconciler.addons[addon.id];
@@ -85,16 +92,18 @@ add_test(function test_install_detection
 
   run_next_test();
 });
 
 add_test(function test_uninstall_detection() {
   _("Ensure that add-on uninstallation results in appropriate side-effects.");
 
   let reconciler = new AddonsReconciler();
+  reconciler.startListening();
+
   reconciler._addons = {};
   reconciler._changes = [];
 
   let addon = installAddon("test_bootstrap1_1");
   let id = addon.id;
   let guid = addon.syncGUID;
 
   reconciler._changes = [];
--- a/services/sync/tests/unit/test_addons_store.js
+++ b/services/sync/tests/unit/test_addons_store.js
@@ -61,16 +61,20 @@ function createAndStartHTTPServer(port) 
     _("Error: " + Utils.exceptionStr(ex));
     do_throw(ex);
   }
 }
 
 function run_test() {
   initTestLogging("Trace");
   Log4Moz.repository.getLogger("Sync.Engine.Addons").level = Log4Moz.Level.Trace;
+  Log4Moz.repository.getLogger("Sync.AddonsRepository").level =
+    Log4Moz.Level.Trace;
+
+  reconciler.startListening();
 
   run_next_test();
 }
 
 add_test(function test_get_all_ids() {
   _("Ensures that getAllIDs() returns an appropriate set.");
 
   engine._refreshReconcilerState();
--- a/services/sync/tests/unit/test_addons_tracker.js
+++ b/services/sync/tests/unit/test_addons_tracker.js
@@ -6,16 +6,17 @@
 Cu.import("resource://services-sync/engines/addons.js");
 Cu.import("resource://services-sync/constants.js");
 Cu.import("resource://services-sync/util.js");
 Cu.import("resource://gre/modules/AddonManager.jsm");
 
 loadAddonTestFunctions();
 startupManager();
 Svc.Prefs.set("addons.ignoreRepositoryChecking", true);
+Svc.Prefs.set("engine.addons", true);
 
 Engines.register(AddonsEngine);
 let engine     = Engines.get("addons");
 let reconciler = engine._reconciler;
 let store      = engine._store;
 let tracker    = engine._tracker;
 
 const addon1ID = "addon1@tests.mozilla.org";
@@ -34,16 +35,18 @@ function cleanup_and_advance() {
   cb.wait();
 
   run_next_test();
 }
 
 function run_test() {
   initTestLogging("Trace");
   Log4Moz.repository.getLogger("Sync.Engine.Addons").level = Log4Moz.Level.Trace;
+  Log4Moz.repository.getLogger("Sync.AddonsReconciler").level =
+    Log4Moz.Level.Trace;
 
   cleanup_and_advance();
 }
 
 add_test(function test_empty() {
   _("Verify the tracker is empty to start with.");
 
   do_check_eq(0, Object.keys(tracker.changedIDs).length);
@@ -62,16 +65,18 @@ add_test(function test_not_tracking() {
   do_check_eq(0, tracker.score);
 
   cleanup_and_advance();
 });
 
 add_test(function test_track_install() {
   _("Ensure that installing an add-on notifies tracker.");
 
+  reconciler.startListening();
+
   Svc.Obs.notify("weave:engine:start-tracking");
 
   do_check_eq(0, tracker.score);
   let addon = installAddon("test_bootstrap1_1");
   let changed = tracker.changedIDs;
 
   do_check_eq(1, Object.keys(changed).length);
   do_check_true(addon.syncGUID in changed);
@@ -79,16 +84,18 @@ add_test(function test_track_install() {
 
   uninstallAddon(addon);
   cleanup_and_advance();
 });
 
 add_test(function test_track_uninstall() {
   _("Ensure that uninstalling an add-on notifies tracker.");
 
+  reconciler.startListening();
+
   let addon = installAddon("test_bootstrap1_1");
   let guid = addon.syncGUID;
   do_check_eq(0, tracker.score);
 
   Svc.Obs.notify("weave:engine:start-tracking");
 
   uninstallAddon(addon);
   let changed = tracker.changedIDs;
@@ -97,16 +104,18 @@ add_test(function test_track_uninstall()
   do_check_eq(SCORE_INCREMENT_XLARGE, tracker.score);
 
   cleanup_and_advance();
 });
 
 add_test(function test_track_user_disable() {
   _("Ensure that tracker sees disabling of add-on");
 
+  reconciler.startListening();
+
   let addon = installAddon("test_bootstrap1_1");
   do_check_false(addon.userDisabled);
   do_check_false(addon.appDisabled);
   do_check_true(addon.isActive);
 
   Svc.Obs.notify("weave:engine:start-tracking");
   do_check_eq(0, tracker.score);
 
@@ -138,16 +147,18 @@ add_test(function test_track_user_disabl
 
   uninstallAddon(addon);
   cleanup_and_advance();
 });
 
 add_test(function test_track_enable() {
   _("Ensure that enabling a disabled add-on notifies tracker.");
 
+  reconciler.startListening();
+
   let addon = installAddon("test_bootstrap1_1");
   addon.userDisabled = true;
   store._sleep(0);
 
   do_check_eq(0, tracker.score);
 
   Svc.Obs.notify("weave:engine:start-tracking");
   addon.userDisabled = false;