Bug 642102 - Instrumentation code should use services modules; r=bienvenu
authorMark Banner <bugzilla@standard8.plus.com>
Thu, 07 Apr 2011 18:13:20 -0700
changeset 7524 827b6f63e9db7199464ff9dfd11b0ba81bc9ec54
parent 7523 ab3d7bdaa533bdf8ea6a3c2820528dc877a52065
child 7527 c3227fb51b3fbe9b3cca311ffa795bc4328ce25a
push idunknown
push userunknown
push dateunknown
reviewersbienvenu
bugs642102
Bug 642102 - Instrumentation code should use services modules; r=bienvenu
mail/base/modules/mailInstrumentation.js
--- a/mail/base/modules/mailInstrumentation.js
+++ b/mail/base/modules/mailInstrumentation.js
@@ -50,19 +50,16 @@ const Cu = Components.utils;
 const nsIMFNService = Ci.nsIMsgFolderNotificationService;
 var gMFNService = Cc["@mozilla.org/messenger/msgnotificationservice;1"]
                      .getService(nsIMFNService);
 
 Cu.import("resource:///modules/IOUtils.js");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource:///modules/mailServices.js");
 
-var gPrefBranch = Cc["@mozilla.org/preferences-service;1"]
-                     .getService(Ci.nsIPrefService).getBranch(null);
-
 /* :::::::: The Module ::::::::::::::: */
 
 var mailInstrumentationManager =
 {
   // JS object containing the current state object
   _currentState: null,
 
   /**
@@ -87,40 +84,40 @@ var mailInstrumentationManager =
   msgAdded: function (aMsg) {
     gMFNService.removeListener(this);
     this._mfnListener = false;
     mailInstrumentationManager.addEvent("msgDownloaded", true);
   },
 
   _accountsChanged: function() {
     // check if there are at least two accounts - one is local folders account
-    if (gPrefBranch.getCharPref("mail.accountmanager.accounts").indexOf(',') > 0) {
+    if (Services.prefs.getCharPref("mail.accountmanager.accounts").indexOf(',') > 0) {
       mailInstrumentationManager.addEvent("accountAdded", true);
       this._removeObserver("mail.accountmanager.accounts",
                            this._accountsChanged);
 
     }
   },
   _smtpServerAdded: function() {
     mailInstrumentationManager.addEvent("smtpServerAdded", true);
     this._removeObserver("mail.smtpservers", _smtpServerAdded);
   },
   _userOptedIn: function() {
     try {
-      if (gPrefBranch.getBoolPref("mail.instrumentation.userOptedIn"))
+      if (Services.prefs.getBoolPref("mail.instrumentation.userOptedIn"))
         mailInstrumentationManager._postStateObject();
     } catch (ex) {logException(ex);}
   },
 
   /**
    * Loads the last saved state. This should only be called by
    * _init and a unit test.
    */
   _loadState: function minst_loadState() {
-    let data = gPrefBranch.getCharPref("mail.instrumentation.lastNotificationSent");
+    let data = Services.prefs.getCharPref("mail.instrumentation.lastNotificationSent");
     if (data) {
       try {
         // parse the session state into JS objects
         this._currentState = JSON.parse(data);
         return;
       } catch (ex) {}
     }
     this._currentState = this._createStateObject();
@@ -129,29 +126,27 @@ var mailInstrumentationManager =
   /**
    * Writes the state object to disk.
    */
   _postStateObject: function minst_postStateObject() {
     // This will throw an exception if no account is set up, so we
     // wrap the whole thing.
     try {
       if (!this._currentState.userEmailHash.length) {
-        let acctMgr = Cc["@mozilla.org/messenger/account-manager;1"]
-                        .getService(Ci.nsIMsgAccountManager);
-        let email = acctMgr.defaultAccount.defaultIdentity.email;
+        let email = MailServices.accounts.defaultAccount.defaultIdentity.email;
         this._currentState.userEmailHash = this._hashEmailAddress(email);
       }
       let data = JSON.stringify(this._currentState);
       dump("data to post = " + data + "\n");
       // post data only if state changed since last write.
       if (data == this._lastStateString)
         return;
 
       this._lastStateString = data;
-      let userOptedIn = gPrefBranch.getBoolPref("mail.instrumentation.userOptedIn");
+      let userOptedIn = Services.prefs.getBoolPref("mail.instrumentation.userOptedIn");
       if (userOptedIn)
         this._postData();
     } catch (ex) {logException(ex);}
   },
 
   /**
    * @return an empty state object that can be populated with window states.
    */
@@ -183,31 +178,31 @@ var mailInstrumentationManager =
     ch.update(byteArray, byteArray.length);
     let hashedData = ch.finish(false);
     return this._bytesAsHex(hashedData);
   },
 
   _postData: function minst_postData() {
     let req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
                 .createInstance(Ci.nsIXMLHttpRequest);
-    let url = gPrefBranch.getCharPref("mail.instrumentation.postUrl");
+    let url = Services.prefs.getCharPref("mail.instrumentation.postUrl");
     if (!url.length)
       return;
     let dataToPost = this._lastStateString;
     req.open("POST", url, true);
     req.onerror = this._onError;
     req.onload = this._onLoad;
     req.send(dataToPost);
   },
   _onError: function minst_onError(e) {
     logException(e);
   },
   _onLoad: function minst_onLoad() {
-    gPrefBranch.setCharPref("mail.instrumentation.lastNotificationSent",
-                            this._lastStateString);
+    Services.prefs.setCharPref("mail.instrumentation.lastNotificationSent",
+                               this._lastStateString);
   },
   // keeps track of whether or not we've removed the observer for a given
   // pref name.
   _prefsObserved : {},
   _addObserver : function(pref, observer) {
     Services.prefs.addObserver(pref, observer, false);
     this._prefsObserved[pref] = true;
   },
@@ -219,49 +214,45 @@ var mailInstrumentationManager =
   },
 /* ........ Public API ................*/
   /**
    * This is called to initialize the instrumentation.
    */
   init: function minst_init() {
     // If we're done with instrumentation, or this is not a first run,
     // we should just return immediately.
-    if (!gPrefBranch.getBoolPref("mail.instrumentation.askUser"))
+    if (!Services.prefs.getBoolPref("mail.instrumentation.askUser"))
       return;
-    let am = Cc["@mozilla.org/messenger/account-manager;1"]
-               .getService(Ci.nsIMsgAccountManager);
-    if (am.accounts.Count() > 0)
+    if (MailServices.accounts.accounts.Count() > 0)
       return;
 
     this._loadState();
-    let os = Cc["@mozilla.org/observer-service;1"]
-              .getService(Ci.nsIObserverService);
-    os.addObserver(this, "mail:composeSendSucceeded", false);
-    os.addObserver(this, "mail:setAsDefault", false);
-    this._addObserver("mail.accountmanager.accounts", this._accountsChanged);
-    this._addObserver("mail.instrumentation.userOptedIn", this._userOptedIn);
-    this._addObserver("mail.smtpservers", this._smtpServerAdded);
+    Services.obs.addObserver(this, "mail:composeSendSucceeded", false);
+    Services.obs.addObserver(this, "mail:setAsDefault", false);
+    Services.prefs.addObserver("mail.accountmanager.accounts",
+                               this._accountsChanged, false);
+    Services.prefs.addObserver("mail.instrumentation.userOptedIn",
+                               this._userOptedIn, false);
+    Services.prefs.addObserver("mail.smtpservers", this._smtpServerAdded, false);
     gMFNService.addListener(this, nsIMFNService.msgAdded);
     this._observersRegistered = true;
     this._mfnListener = true;
   },
   uninit: function() {
     if (!this._observersRegistered)
       return;
     let os = Cc["@mozilla.org/observer-service;1"]
               .getService(Ci.nsIObserverService);
-    os.removeObserver(this, "mail:composeSendSucceeded");
-    os.removeObserver(this, "mail:setAsDefault");
+    Services.obs.removeObserver(this, "mail:composeSendSucceeded");
+    Services.obs.removeObserver(this, "mail:setAsDefault");
     if (this._mfnListener)
       gMFNService.removeListener(this);
-    this._removeObserver("mail.accountmanager.accounts",
-                         this._accountsChanged);
-    this._removeObserver("mail.instrumentation.userOptedIn",
-                         this._userOptedIn);
-    this._removeObserver("mail.smtpservers", this._smtpServerAdded);
+    Services.prefs.removeObserver("mail.accountmanager.accounts", this);
+    Services.prefs.removeObserver("mail.instrumentation.userOptedIn", this);
+    Services.prefs.removeObserver("mail.smtpservers", this);
   },
   /**
    * This adds an event to the current state, if it doesn't exist.
    */
   addEvent: function minst_addEvent(aEventKey, aData) {
     try {
       if (!(aEventKey in this._currentState.events)) {
         let newEvent = new Object;