Bug 785225 - Part 1: Don't rely on Engines singleton in AddonsReconciler; r=rnewman
authorGregory Szorc <gps@mozilla.com>
Wed, 29 Aug 2012 14:43:40 -0700
changeset 111048 e10e5cf8fafa03f6a391f36f2defd109479b84e1
parent 111047 b78552875522d395c649e41eb3389fbd6be5d5ec
child 111049 bc3e055a34a1bd859433af0d5eac54a9f8777aae
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersrnewman
bugs785225
milestone18.0a1
Bug 785225 - Part 1: Don't rely on Engines singleton in AddonsReconciler; r=rnewman
services/sync/modules/addonsreconciler.js
services/sync/modules/engines/addons.js
--- a/services/sync/modules/addonsreconciler.js
+++ b/services/sync/modules/addonsreconciler.js
@@ -15,17 +15,16 @@
  * hopefully ported.
  */
 
 "use strict";
 
 const Cu = Components.utils;
 
 Cu.import("resource://services-common/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;
@@ -114,18 +113,16 @@ 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];
 
-  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.
    *
@@ -296,18 +293,17 @@ AddonsReconciler.prototype = {
   },
 
   /**
    * 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) {
+    if (this._listening) {
       return;
     }
 
     this._log.info("Registering as Add-on Manager listener.");
     AddonManager.addAddonListener(this);
     AddonManager.addInstallListener(this);
     this._listening = true;
   },
--- a/services/sync/modules/engines/addons.js
+++ b/services/sync/modules/engines/addons.js
@@ -652,16 +652,20 @@ function AddonsTracker(name) {
   Tracker.call(this, name);
 
   Svc.Obs.add("weave:engine:start-tracking", this);
   Svc.Obs.add("weave:engine:stop-tracking", this);
 }
 AddonsTracker.prototype = {
   __proto__: Tracker.prototype,
 
+  get engine() {
+    return Engines.get("addons");
+  },
+
   get reconciler() {
     return Engines.get("addons")._reconciler;
   },
 
   get store() {
     return Engines.get("addons")._store;
   },
 
@@ -684,17 +688,22 @@ AddonsTracker.prototype = {
 
     this.addChangedID(addon.guid, date.getTime() / 1000);
     this.score += SCORE_INCREMENT_XLARGE;
   },
 
   observe: function(subject, topic, data) {
     switch (topic) {
       case "weave:engine:start-tracking":
+        if (this.engine.enabled) {
+          this.reconciler.startListening();
+        }
+
         this.reconciler.addChangeListener(this);
         break;
 
       case "weave:engine:stop-tracking":
         this.reconciler.removeChangeListener(this);
+        this.reconciler.stopListening();
         break;
     }
   }
 };