Bug 1590637 - Reload address book after migration. r=mkmelin a=jorgk
authorGeoff Lankow <geoff@darktrojan.net>
Thu, 24 Oct 2019 11:30:54 +0200
changeset 37212 1acfb376f132d28c5082b73a5b737e6dbe27b444
parent 37211 0649668a52e0ac68c987c542c2afc9cb7cd18012
child 37213 28e678851cfa4e43dbca06e3492898cc72686cb6
push id395
push userclokep@gmail.com
push dateMon, 02 Dec 2019 19:38:57 +0000
reviewersmkmelin, jorgk
bugs1590637
Bug 1590637 - Reload address book after migration. r=mkmelin a=jorgk
mail/base/modules/MailMigrator.jsm
mailnews/addrbook/jsaddrbook/AddrBookDirectory.jsm
mailnews/addrbook/src/nsAbManager.cpp
mailnews/addrbook/test/unit/test_migration6.js
mailnews/addrbook/test/unit/xpcshell-jsaddrbook.ini
--- a/mail/base/modules/MailMigrator.jsm
+++ b/mail/base/modules/MailMigrator.jsm
@@ -675,18 +675,17 @@ var MailMigrator = {
    * Perform any migration work that needs to occur after the Account Wizard
    * has had a chance to appear.
    */
   migratePostAccountWizard() {
     this.migrateToClearTypeFonts();
   },
 
   /**
-   * Migrate address books from Mork to JS/SQLite. This must happen before
-   * address book start-up and without causing it.
+   * Migrate address books from Mork to JS/SQLite.
    *
    * All Mork address books found in the prefs are converted to JS/SQLite
    * address books and the prefs updated. Migrated Mork files in the profile
    * are renamed with the extension ".mab.bak" to avoid confusion.
    */
   async _migrateAddressBooks() {
     async function migrateBook(fileName, notFoundThrows = true) {
       let oldFile = profileDir.clone();
@@ -807,16 +806,18 @@ var MailMigrator = {
             "jsaddrbook://$1.sqlite"
           );
           Services.prefs.setStringPref(prefName, uri);
         }
       } catch (ex) {
         Cu.reportError(ex);
       }
     }
+
+    Services.obs.notifyObservers(null, "addrbook-reload");
   },
 
   /**
    * Perform any migration work that needs to occur once the user profile has
    * been loaded.
    */
   async migrateAtProfileStartup() {
     await this._migrateAddressBooks();
--- a/mailnews/addrbook/jsaddrbook/AddrBookDirectory.jsm
+++ b/mailnews/addrbook/jsaddrbook/AddrBookDirectory.jsm
@@ -122,17 +122,16 @@ var connections = new Map();
 var closeObserver = {
   observe() {
     for (let connection of connections.values()) {
       connection.asyncClose();
     }
     connections.clear();
   },
 };
-Services.obs.addObserver(closeObserver, "addrbook-reload");
 Services.obs.addObserver(closeObserver, "quit-application");
 
 /**
  * Opens an SQLite connection to `file`, caches the connection, and upgrades
  * the database schema if necessary.
  */
 function openConnectionTo(file) {
   let connection = connections.get(file.path);
--- a/mailnews/addrbook/src/nsAbManager.cpp
+++ b/mailnews/addrbook/src/nsAbManager.cpp
@@ -179,17 +179,17 @@ nsresult nsAbManager::GetRootDirectory(n
   // We cache the top level AB to ensure that nsIAbDirectory items are not
   // created and dumped every time GetDirectories is called. This was causing
   // performance problems, especially with the content policy on messages
   // with lots of urls.
   nsresult rv;
 
   if (!mCacheTopLevelAb) {
     nsCOMPtr<nsIAbDirectory> rootAddressBook(
-        do_GetService(NS_ABDIRECTORY_CONTRACTID, &rv));
+        do_CreateInstance(NS_ABDIRECTORY_CONTRACTID, &rv));
     NS_ENSURE_SUCCESS(rv, rv);
     mCacheTopLevelAb = rootAddressBook;
   }
 
   NS_IF_ADDREF(*aResult = mCacheTopLevelAb);
   return NS_OK;
 }
 
new file mode 100644
--- /dev/null
+++ b/mailnews/addrbook/test/unit/test_migration6.js
@@ -0,0 +1,55 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/*
+ * Test auto-migration from Mork address books to JS/SQLite address books.
+ *
+ * This test profile has multiple additional address books to migrate.
+ * The address book manager should *not* think one or more of them is still a
+ * Mork address book after migration (bug 1590637).
+ */
+add_task(async function() {
+  // Copy address books to be migrated into the profile.
+
+  copyABFile("data/cardForEmail.mab", "abook-1.mab");
+  copyABFile("data/collect.mab", "abook-2.mab");
+  Services.prefs.setStringPref("ldap_2.servers.Test1.description", "Test 1");
+  Services.prefs.setIntPref("ldap_2.servers.Test1.dirType", 2);
+  Services.prefs.setStringPref("ldap_2.servers.Test1.filename", "abook-1.mab");
+  Services.prefs.setStringPref("ldap_2.servers.Test2.description", "Test 2");
+  Services.prefs.setIntPref("ldap_2.servers.Test2.dirType", 2);
+  Services.prefs.setStringPref("ldap_2.servers.Test2.filename", "abook-2.mab");
+
+  // Do the migration.
+
+  await MailMigrator._migrateAddressBooks();
+
+  // Check new files have been created, and old ones renamed.
+
+  checkFileExists("abook.sqlite", true);
+  checkFileExists("abook.mab", false);
+  checkFileExists("abook.mab.bak", false);
+  checkFileExists("history.sqlite", true);
+  checkFileExists("history.mab", false);
+  checkFileExists("history.mab.bak", false);
+  checkFileExists("abook-1.sqlite", true);
+  checkFileExists("abook-1.mab", false);
+  checkFileExists("abook-1.mab.bak", true);
+  checkFileExists("abook-2.sqlite", true);
+  checkFileExists("abook-2.mab", false);
+  checkFileExists("abook-2.mab.bak", true);
+
+  // Check the new address books.
+
+  let directories = [...MailServices.ab.directories];
+  equal(directories.length, 4);
+  equal(directories[0].URI, "jsaddrbook://abook-1.sqlite");
+  equal(directories[1].URI, "jsaddrbook://abook-2.sqlite");
+  equal(directories[2].URI, "jsaddrbook://abook.sqlite");
+  equal(directories[3].URI, "jsaddrbook://history.sqlite");
+
+  // This will fail if we try to read an SQLite file with the Mork code.
+
+  directories.map(directory => directory.childCards);
+});
--- a/mailnews/addrbook/test/unit/xpcshell-jsaddrbook.ini
+++ b/mailnews/addrbook/test/unit/xpcshell-jsaddrbook.ini
@@ -11,8 +11,10 @@ head = head_migration.js
 [test_migration2.js]
 head = head_migration.js
 [test_migration3.js]
 head = head_migration.js
 [test_migration4.js]
 head = head_migration.js
 [test_migration5.js]
 head = head_migration.js
+[test_migration6.js]
+head = head_migration.js