Bug 1614846 - Followup. Use MailServices to get accountmanager in Enigmail code. r=mkmelin DONTBUILD
authorBen Campbell <benc@thunderbird.net>
Mon, 30 Mar 2020 13:56:58 +0300
changeset 38625 3c0f617d39f6181ff23253053a36111786a15fb9
parent 38624 40b4770d124eb592eb2e4e4c9410ef50cd60baa0
child 38626 d318ed3f234768fa67bd40121f87eca9e8443f33
push id400
push userclokep@gmail.com
push dateMon, 04 May 2020 18:56:09 +0000
reviewersmkmelin
bugs1614846
Bug 1614846 - Followup. Use MailServices to get accountmanager in Enigmail code. r=mkmelin DONTBUILD
mail/extensions/openpgp/content/modules/autoSetup.jsm
mail/extensions/openpgp/content/modules/configBackup.jsm
mail/extensions/openpgp/content/modules/filters.jsm
mail/extensions/openpgp/content/modules/funcs.jsm
mail/extensions/openpgp/content/ui/enigmailCommon.js
mail/extensions/openpgp/content/ui/enigmailKeygen.js
--- a/mail/extensions/openpgp/content/modules/autoSetup.jsm
+++ b/mail/extensions/openpgp/content/modules/autoSetup.jsm
@@ -42,19 +42,21 @@ const { EnigmailTimer } = ChromeUtils.im
   "chrome://openpgp/content/modules/timer.jsm"
 );
 const { EnigmailStreams } = ChromeUtils.import(
   "chrome://openpgp/content/modules/streams.jsm"
 );
 const { EnigmailGpg } = ChromeUtils.import(
   "chrome://openpgp/content/modules/gpg.jsm"
 );
+const { MailServices } = ChromeUtils.import(
+  "resource:///modules/MailServices.jsm"
+);
 
 // Interfaces
-const nsIMsgAccountManager = Ci.nsIMsgAccountManager;
 const nsIMessenger = Ci.nsIMessenger;
 const nsIMsgMessageService = Ci.nsIMsgMessageService;
 
 /**
  * the determined setup type
  */
 var gDeterminedSetupType = {
   value: EnigmailConstants.AUTOSETUP_NOT_INITIALIZED,
@@ -82,39 +84,35 @@ var EnigmailAutoSetup = {
     gDeterminedSetupType = {
       value: EnigmailConstants.AUTOSETUP_NOT_INITIALIZED,
     };
 
     return new Promise(async (resolve, reject) => {
       EnigmailLog.DEBUG("autoSetup.jsm: determinePreviousInstallType()\n");
 
       try {
-        let msgAccountManager = Cc[
-          "@mozilla.org/messenger/account-manager;1"
-        ].getService(nsIMsgAccountManager);
         let returnMsgValue = {
           value: EnigmailConstants.AUTOSETUP_NO_HEADER,
         };
 
-        var accounts = msgAccountManager.accounts;
+        let accounts = MailServices.accounts.accounts;
 
         let msgHeaders = [];
 
         // If no account, except Local Folders is configured
         if (accounts.length <= 1) {
           gDeterminedSetupType.value = EnigmailConstants.AUTOSETUP_NO_ACCOUNT;
           resolve(gDeterminedSetupType);
           return;
         }
 
         // Iterate through each account
 
-        for (var i = 0; i < accounts.length; i++) {
-          var account = accounts.queryElementAt(i, Ci.nsIMsgAccount);
-          var accountMsgServer = account.incomingServer;
+        for (let account of accounts) {
+          let accountMsgServer = account.incomingServer;
           EnigmailLog.DEBUG(
             `autoSetup.jsm: determinePreviousInstallType: scanning account "${accountMsgServer.prettyName}"\n`
           );
 
           let msgFolderArr = [];
 
           try {
             getMsgFolders(account.incomingServer.rootFolder, msgFolderArr);
@@ -366,24 +364,19 @@ var EnigmailAutoSetup = {
    * @param {Number} timeoutValue: number of miliseconds to wait before starting
    *                               the process
    */
   createKeyForAllAccounts(timeoutValue = 1000) {
     EnigmailLog.DEBUG("autoSetup.jsm: createKeyForAllAccounts()\n");
     let self = this;
 
     EnigmailTimer.setTimeout(async function() {
-      let msgAccountManager = Cc[
-        "@mozilla.org/messenger/account-manager;1"
-      ].getService(nsIMsgAccountManager);
-      let accounts = msgAccountManager.accounts;
       let createdKeys = [];
 
-      for (let i = 0; i < accounts.length; i++) {
-        let account = accounts.queryElementAt(i, Ci.nsIMsgAccount);
+      for (let account of MailServices.accounts.accounts) {
         let id = account.defaultIdentity;
 
         if (id && id.email) {
           let keyId = await self.createAutocryptKey(id.fullName, id.email);
           EnigmailLog.DEBUG(
             `autoSetup.jsm: createKeyForAllAccounts: created key ${keyId}\n`
           );
           if (keyId) {
@@ -462,22 +455,17 @@ var EnigmailAutoSetup = {
       }
     });
   },
 
   /**
    * Configure Enigmail to use existing keys
    */
   applyExistingKeys() {
-    let msgAccountManager = Cc[
-      "@mozilla.org/messenger/account-manager;1"
-    ].getService(nsIMsgAccountManager);
-    let identities = msgAccountManager.allIdentities;
-
-    for (let id of identities) {
+    for (let id of MailServices.accounts.allIdentities) {
       if (id.email) {
         let keyObj = EnigmailKeyRing.getSecretKeyByEmail(id.email);
         if (keyObj) {
           EnigmailLog.DEBUG(
             `autoSetup.jsm: applyExistingKeys: found key ${keyObj.keyId}\n`
           );
           id.setBoolAttribute("enablePgp", true);
           id.setCharAttribute("pgpkeyId", "0x" + keyObj.fpr);
--- a/mail/extensions/openpgp/content/modules/configBackup.jsm
+++ b/mail/extensions/openpgp/content/modules/configBackup.jsm
@@ -10,16 +10,19 @@ const { EnigmailLog } = ChromeUtils.impo
   "chrome://openpgp/content/modules/log.jsm"
 );
 const { EnigmailFiles } = ChromeUtils.import(
   "chrome://openpgp/content/modules/files.jsm"
 );
 const { EnigmailPrefs } = ChromeUtils.import(
   "chrome://openpgp/content/modules/prefs.jsm"
 );
+const { MailServices } = ChromeUtils.import(
+  "resource:///modules/MailServices.jsm"
+);
 
 const TYPE_BOOL = 1;
 const TYPE_CHAR = 2;
 const TYPE_INT = 3;
 
 const IdentityPref = {
   enablePgp: TYPE_BOOL,
   pgpkeyId: TYPE_CHAR,
@@ -30,36 +33,27 @@ const IdentityPref = {
   defaultEncryptionPolicy: TYPE_INT,
   openPgpUrlName: TYPE_CHAR,
   pgpMimeMode: TYPE_BOOL,
   attachPgpKey: TYPE_BOOL,
   autoEncryptDrafts: TYPE_BOOL,
 };
 
 var EnigmailConfigBackup = {
-  getAccountManager() {
-    let amService = Cc["@mozilla.org/messenger/account-manager;1"].getService(
-      Ci.nsIMsgAccountManager
-    );
-    return amService;
-  },
-
   /**
    * itereate over all identities and execute a callback function for each found element
    *
    * @param callbackFunc  function  - the callback for each identity
    *                  The function takes the identity as 1st argument, i.e.
    *                    callbackFunc(nsIMsgIdentity)
    * @return  - undefined
    */
   forAllIdentitites(callbackFunc) {
-    let amService = this.getAccountManager();
-
-    amService.LoadAccounts(); // ensure accounts are really loaded
-    for (let id of amService.allIdentities) {
+    MailServices.accounts.LoadAccounts(); // ensure accounts are really loaded
+    for (let id of MailServices.accounts.allIdentities) {
       try {
         callbackFunc(id);
       } catch (ex) {
         EnigmailLog.DEBUG(
           "configBackup.jsm: forAllIdentitites: exception " +
             ex.toString() +
             "\n"
         );
@@ -190,18 +184,17 @@ var EnigmailConfigBackup = {
       this.forAllIdentitites(setIdentityPref);
 
       for (let i in prefObj.mailIdentities) {
         if (!("foundMatchingEmail" in prefObj.mailIdentities[i])) {
           returnObj.unmatchedIds.push(prefObj.mailIdentities[i].identityName);
         }
       }
 
-      let am = this.getAccountManager();
-      am.saveAccountInfo();
+      MailServices.accounts.saveAccountInfo();
       EnigmailPrefs.savePrefs();
     } catch (ex) {
       EnigmailLog.ERROR(
         "configBackup.jsm: restorePrefs - exception " + ex.toString() + "\n"
       );
       return returnObj;
     }
 
--- a/mail/extensions/openpgp/content/modules/filters.jsm
+++ b/mail/extensions/openpgp/content/modules/filters.jsm
@@ -41,16 +41,19 @@ const { EnigmailData } = ChromeUtils.imp
 const { jsmime } = ChromeUtils.import("resource:///modules/jsmime.jsm");
 const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
 const { EnigmailMime } = ChromeUtils.import(
   "chrome://openpgp/content/modules/mime.jsm"
 );
 const { EnigmailCompat } = ChromeUtils.import(
   "chrome://openpgp/content/modules/compat.jsm"
 );
+const { MailServices } = ChromeUtils.import(
+  "resource:///modules/MailServices.jsm"
+);
 
 const getDialog = EnigmailLazy.loader("enigmail/dialog.jsm", "EnigmailDialog");
 
 var gNewMailListenerInitiated = false;
 
 /**
  * filter action for creating a decrypted version of the mail and
  * deleting the original mail at the same time
@@ -365,21 +368,17 @@ function shutdownNewMailListener() {
       "@mozilla.org/messenger/msgnotificationservice;1"
     ].getService(Ci.nsIMsgFolderNotificationService);
     notificationService.removeListener(newMailListener);
     gNewMailListenerInitiated = false;
   }
 }
 
 function getIdentityForSender(senderEmail, msgServer) {
-  let accountManager = Cc[
-    "@mozilla.org/messenger/account-manager;1"
-  ].getService(Ci.nsIMsgAccountManager);
-
-  let identities = accountManager.getIdentitiesForServer(msgServer);
+  let identities = MailServices.accounts.getIdentitiesForServer(msgServer);
 
   for (let i = 0; i < identities.length; i++) {
     let id = identities.queryElementAt(i, Ci.nsIMsgIdentity);
     if (id.email.toLowerCase() === senderEmail.toLowerCase()) {
       return id;
     }
   }
 
--- a/mail/extensions/openpgp/content/modules/funcs.jsm
+++ b/mail/extensions/openpgp/content/modules/funcs.jsm
@@ -466,22 +466,18 @@ var EnigmailFuncs = {
 
   /**
    * Get a list of all own email addresses, taken from all identities
    * and all reply-to addresses
    */
   getOwnEmailAddresses() {
     let ownEmails = {};
 
-    let am = Cc["@mozilla.org/messenger/account-manager;1"].getService(
-      Ci.nsIMsgAccountManager
-    );
-
     // Determine all sorts of own email addresses
-    for (let id of am.allIdentities) {
+    for (let id of MailServices.accounts.allIdentities) {
       if (id.email && id.email.length > 0) {
         ownEmails[this.getBaseEmail(id.email.toLowerCase())] = 1;
       }
       if (id.replyTo && id.replyTo.length > 0) {
         try {
           let replyEmails = this.stripEmail(id.replyTo)
             .toLowerCase()
             .split(/,/);
--- a/mail/extensions/openpgp/content/ui/enigmailCommon.js
+++ b/mail/extensions/openpgp/content/ui/enigmailCommon.js
@@ -100,18 +100,16 @@ const ENIG_STRINGBUNDLE_CONTRACTID = "@m
 const ENIG_LOCAL_FILE_CONTRACTID = "@mozilla.org/file/local;1";
 const ENIG_DIRSERVICE_CONTRACTID = "@mozilla.org/file/directory_service;1";
 const ENIG_MIME_CONTRACTID = "@mozilla.org/mime;1";
 const ENIG_WMEDIATOR_CONTRACTID =
   "@mozilla.org/rdf/datasource;1?name=window-mediator";
 const ENIG_ASS_CONTRACTID = "@mozilla.org/appshell/appShellService;1";
 const ENIG_LOCALE_SVC_CONTRACTID = "@mozilla.org/intl/nslocaleservice;1";
 const ENIG_DATE_FORMAT_CONTRACTID = "@mozilla.org/intl/scriptabledateformat;1";
-const ENIG_ACCOUNT_MANAGER_CONTRACTID =
-  "@mozilla.org/messenger/account-manager;1";
 const ENIG_THREAD_MANAGER_CID = "@mozilla.org/thread-manager;1";
 const ENIG_SIMPLEURI_CONTRACTID = "@mozilla.org/network/simple-uri;1";
 
 const ENIG_STANDARD_URL_CONTRACTID = "@mozilla.org/network/standard-url;1";
 const ENIG_SCRIPTABLEINPUTSTREAM_CONTRACTID =
   "@mozilla.org/scriptableinputstream;1";
 const ENIG_BINARYINPUTSTREAM_CONTRACTID = "@mozilla.org/binaryinputstream;1";
 const ENIG_SAVEASCHARSET_CONTRACTID = "@mozilla.org/intl/saveascharset;1";
--- a/mail/extensions/openpgp/content/ui/enigmailKeygen.js
+++ b/mail/extensions/openpgp/content/ui/enigmailKeygen.js
@@ -10,37 +10,36 @@
 
 // modules
 /* global EnigmailData: false, EnigmailLog: false, EnigmailLocale: false, EnigmailGpg: false, EnigmailKeyEditor: false */
 /* global EnigmailOS: false, EnigmailPrefs: false, EnigmailApp: false, EnigmailKeyRing: false */
 /* global EnigmailDialog: false, EnigmailFuncs: false */
 
 // from enigmailCommon.js:
 /* global EnigGetWindowOptions: false, EnigConfirm: false, EnigGetString: false, GetEnigmailSvc: false */
-/* global EnigLongAlert: false, EnigAlert: false, EnigInitCommon: false, ENIG_ACCOUNT_MANAGER_CONTRACTID: false */
+/* global EnigLongAlert: false, EnigAlert: false, EnigInitCommon: false */
 /* global EnigGetPref: false, EnigSetPref: false, EnigSavePrefs: false, EnigFilePicker: false, EnigGetFilePath: false */
 /* global EnigmailWindows: false, EnigCreateRevokeCert: false */
 
 // Initialize enigmailCommon
 EnigInitCommon("enigmailKeygen");
 
-var gAccountManager = Cc[ENIG_ACCOUNT_MANAGER_CONTRACTID].getService(
-  Ci.nsIMsgAccountManager
-);
-
 var EnigmailCryptoAPI = ChromeUtils.import(
   "chrome://openpgp/content/modules/cryptoAPI.jsm"
 ).EnigmailCryptoAPI;
 var { EnigmailFiles } = ChromeUtils.import(
   "chrome://openpgp/content/modules/files.jsm"
 );
 var OpenPGPMasterpass = ChromeUtils.import(
   "chrome://openpgp/content/modules/masterpass.jsm"
 ).OpenPGPMasterpass;
 var { RNP } = ChromeUtils.import("chrome://openpgp/content/modules/rnp.jsm");
+const { MailServices } = ChromeUtils.import(
+  "resource:///modules/MailServices.jsm"
+);
 
 var gUserIdentityList;
 var gUserIdentityListPopup;
 
 var gKeygenRequest;
 var gAllData = "";
 var gGeneratedKey = null;
 var gUsedId;
@@ -316,26 +315,26 @@ function onNoExpiry() {
   expireInput.disabled = noExpiry.checked;
   timeScale.disabled = noExpiry.checked;
 }
 
 function getCurrentIdentity() {
   var item = gUserIdentityList.selectedItem;
   var identityKey = item.getAttribute("id");
 
-  var identity = gAccountManager.getIdentity(identityKey);
+  var identity = MailServices.accounts.getIdentity(identityKey);
 
   return identity;
 }
 
 function fillIdentityListPopup() {
   EnigmailLog.DEBUG("enigmailKeygen.js: fillIdentityListPopup\n");
 
   try {
-    var identities = gAccountManager.allIdentities;
+    var identities = MailServices.accounts.allIdentities;
 
     EnigmailLog.DEBUG(
       "enigmailKeygen.js: fillIdentityListPopup: " + identities + "\n"
     );
 
     // Default identity
     let defIdentity = EnigmailFuncs.getDefaultIdentity();
 
@@ -351,17 +350,17 @@ function fillIdentityListPopup() {
 
       EnigmailLog.DEBUG("id.valid=" + identity.valid + "\n");
       if (!identity.valid || !identity.email) {
         continue;
       }
 
       var serverSupports, inServer;
       // Gecko >= 20
-      serverSupports = gAccountManager.getServersForIdentity(identity);
+      serverSupports = MailServices.accounts.getServersForIdentity(identity);
       if (serverSupports.length > 0) {
         inServer = serverSupports.queryElementAt(0, Ci.nsIMsgIncomingServer);
       }
 
       if (inServer) {
         var accountName = " - " + inServer.prettyName;
 
         EnigmailLog.DEBUG(