Bug 1340972 - Part 19: Replace error-prone Addref()/Release() with using smart pointers in mailnews/import. r=aceman
authorJorg K <jorgk@jorgk.com>
Sat, 16 Sep 2017 21:46:51 +0200
changeset 29729 0c7ce1b538aaf1c071e8b3cda1ec52d724deeb16
parent 29728 3687aa6c1fc587cb8ad0d35f4666ed1685db5a9f
child 29730 e16fc2c7ad6821c182b9f1c52a7cbae3e8a79ac5
push id378
push userclokep@gmail.com
push dateMon, 13 Nov 2017 18:45:35 +0000
reviewersaceman
bugs1340972
Bug 1340972 - Part 19: Replace error-prone Addref()/Release() with using smart pointers in mailnews/import. r=aceman
mailnews/import/becky/src/nsBeckyStringBundle.cpp
mailnews/import/becky/src/nsBeckyStringBundle.h
mailnews/import/outlook/src/nsOutlookCompose.cpp
mailnews/import/outlook/src/nsOutlookCompose.h
mailnews/import/outlook/src/nsOutlookImport.cpp
mailnews/import/outlook/src/nsOutlookMail.cpp
mailnews/import/outlook/src/nsOutlookStringBundle.cpp
mailnews/import/outlook/src/nsOutlookStringBundle.h
mailnews/import/src/nsImportABDescriptor.cpp
mailnews/import/src/nsImportAddressBooks.cpp
mailnews/import/src/nsImportFieldMap.cpp
mailnews/import/src/nsImportMail.cpp
mailnews/import/src/nsImportMailboxDescriptor.cpp
mailnews/import/src/nsImportService.cpp
mailnews/import/src/nsImportService.h
mailnews/import/text/src/nsTextAddress.cpp
mailnews/import/text/src/nsTextAddress.h
mailnews/import/text/src/nsTextImport.cpp
mailnews/import/vcard/src/nsVCardImport.cpp
mailnews/import/winlivemail/nsWMImport.cpp
mailnews/import/winlivemail/nsWMStringBundle.cpp
mailnews/import/winlivemail/nsWMStringBundle.h
--- a/mailnews/import/becky/src/nsBeckyStringBundle.cpp
+++ b/mailnews/import/becky/src/nsBeckyStringBundle.cpp
@@ -11,37 +11,35 @@
 #include "nsIURI.h"
 #include "nsServiceManagerUtils.h"
 #include "nsXPCOMCIDInternal.h"
 
 #include "nsBeckyStringBundle.h"
 
 #define BECKY_MESSAGES_URL "chrome://messenger/locale/beckyImportMsgs.properties"
 
-nsIStringBundle *nsBeckyStringBundle::mBundle = nullptr;
+nsCOMPtr<nsIStringBundle> nsBeckyStringBundle::mBundle = nullptr;
 
-nsIStringBundle *
+void
 nsBeckyStringBundle::GetStringBundle(void)
 {
   if (mBundle)
-    return mBundle;
+    return;
 
   nsresult rv;
   nsCOMPtr<nsIStringBundleService> bundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
   if (NS_SUCCEEDED(rv) && bundleService)
-    rv = bundleService->CreateBundle(BECKY_MESSAGES_URL, &mBundle);
-
-  return mBundle;
+    rv = bundleService->CreateBundle(BECKY_MESSAGES_URL, getter_AddRefs(mBundle));
 }
 
 void
 nsBeckyStringBundle::EnsureStringBundle(void)
 {
   if (!mBundle)
-    (void) GetStringBundle();
+    GetStringBundle();
 }
 
 char16_t *
 nsBeckyStringBundle::GetStringByName(const char *aName)
 {
   EnsureStringBundle();
 
   if (mBundle) {
@@ -65,10 +63,10 @@ nsBeckyStringBundle::FormatStringFromNam
                                        params,
                                        length,
                                        _retval);
 }
 
 void
 nsBeckyStringBundle::Cleanup(void)
 {
-  NS_IF_RELEASE(mBundle);
+  mBundle = nullptr;
 }
--- a/mailnews/import/becky/src/nsBeckyStringBundle.h
+++ b/mailnews/import/becky/src/nsBeckyStringBundle.h
@@ -10,21 +10,21 @@ class nsIStringBundle;
 
 class nsBeckyStringBundle final {
 public:
   static char16_t *GetStringByName(const char *name);
   static nsresult FormatStringFromName(const char *name,
                                        const char16_t **params,
                                        uint32_t length,
                                        nsAString& _retval);
-  static nsIStringBundle * GetStringBundle(void); // don't release
+  static void GetStringBundle(void);
   static void EnsureStringBundle(void);
   static void Cleanup(void);
 private:
-  static nsIStringBundle *mBundle;
+  static nsCOMPtr<nsIStringBundle> mBundle;
 };
 
 #define BECKYIMPORT_NAME                     2000
 #define BECKYIMPORT_DESCRIPTION              2001
 #define BECKYIMPORT_MAILBOX_SUCCESS          2002
 #define BECKYIMPORT_MAILBOX_BADPARAM         2003
 #define BECKYIMPORT_MAILBOX_CONVERTERROR     2004
 #define BECKYIMPORT_ADDRESS_SUCCESS          2005
--- a/mailnews/import/outlook/src/nsOutlookCompose.cpp
+++ b/mailnews/import/outlook/src/nsOutlookCompose.cpp
@@ -114,17 +114,16 @@ private:
 //////////////////////////////////////////////////////////////////////////////////////////////////
 
 // First off, a listener
 class OutlookSendListener : public nsIMsgSendListener
 {
 public:
   OutlookSendListener() {
     m_done = false;
-    m_location = nullptr;
   }
 
   // nsISupports interface
   NS_DECL_THREADSAFE_ISUPPORTS
 
   /* void OnStartSending (in string aMsgID, in uint32_t aMsgSize); */
   NS_IMETHOD OnStartSending(const char *aMsgID, uint32_t aMsgSize) {return NS_OK;}
 
@@ -144,23 +143,23 @@ public:
 
    /* void OnSendNotPerformed */
    NS_IMETHOD OnSendNotPerformed(const char *aMsgID, nsresult aStatus) {return NS_OK;}
 
   /* void OnGetDraftFolderURI (); */
   NS_IMETHOD OnGetDraftFolderURI(const char *aFolderURI) {return NS_OK;}
 
   static nsresult CreateSendListener(nsIMsgSendListener **ppListener);
-  void Reset() { m_done = false; NS_IF_RELEASE(m_location);}
+  void Reset() { m_done = false; m_location = nullptr; }
 
 public:
-  virtual ~OutlookSendListener() { NS_IF_RELEASE(m_location); }
+  virtual ~OutlookSendListener() {}
 
   bool m_done;
-  nsIFile * m_location;
+  nsCOMPtr<nsIFile> m_location;
 };
 
 NS_IMPL_ISUPPORTS(OutlookSendListener, nsIMsgSendListener)
 
 nsresult OutlookSendListener::CreateSendListener(nsIMsgSendListener **ppListener)
 {
   NS_ENSURE_ARG_POINTER(ppListener);
   NS_ADDREF(*ppListener = new OutlookSendListener());
@@ -178,71 +177,65 @@ nsresult OutlookSendListener::CreateSend
 #define hackEndW u"\015\012end"
 #define hackCRLFA "crlf"
 #define hackCRLFW u"crlf"
 #define hackAmpersandA "amp"
 #define hackAmpersandW u"amp"
 
 nsOutlookCompose::nsOutlookCompose()
 {
-  m_pListener = nullptr;
-  m_pMsgFields = nullptr;
-
   m_optimizationBuffer = new char[FILE_IO_BUFFER_SIZE];
 }
 
 nsOutlookCompose::~nsOutlookCompose()
 {
-  NS_IF_RELEASE(m_pListener);
-  NS_IF_RELEASE(m_pMsgFields);
   if (m_pIdentity) {
     nsresult rv = m_pIdentity->ClearAllValues();
     NS_ASSERTION(NS_SUCCEEDED(rv),"failed to clear values");
     if (NS_FAILED(rv))
       return;
   }
   delete[] m_optimizationBuffer;
 }
 
-nsIMsgIdentity * nsOutlookCompose::m_pIdentity = nullptr;
+nsCOMPtr<nsIMsgIdentity> nsOutlookCompose::m_pIdentity = nullptr;
 
 nsresult nsOutlookCompose::CreateIdentity(void)
 {
   if (m_pIdentity)
     return NS_OK;
 
   nsresult rv;
   nsCOMPtr<nsIMsgAccountManager> accMgr =
     do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
-  rv = accMgr->CreateIdentity(&m_pIdentity);
+  rv = accMgr->CreateIdentity(getter_AddRefs(m_pIdentity));
   nsString name;
   name.AssignLiteral("Import Identity");
   if (m_pIdentity) {
     m_pIdentity->SetFullName(name);
     m_pIdentity->SetEmail(NS_LITERAL_CSTRING("import@service.invalid"));
   }
   return rv;
 }
 
 void nsOutlookCompose::ReleaseIdentity()
 {
-  NS_IF_RELEASE(m_pIdentity);
+  m_pIdentity = nullptr;
 }
 
 nsresult nsOutlookCompose::CreateComponents(void)
 {
   nsresult rv = NS_OK;
 
-  NS_IF_RELEASE(m_pMsgFields);
   if (!m_pListener && NS_SUCCEEDED(rv))
-    rv = OutlookSendListener::CreateSendListener(&m_pListener);
+    rv = OutlookSendListener::CreateSendListener(getter_AddRefs(m_pListener));
 
   if (NS_SUCCEEDED(rv)) {
-      rv = CallCreateInstance(kMsgCompFieldsCID, &m_pMsgFields);
+      m_pMsgFields = do_CreateInstance(kMsgCompFieldsCID, &rv);
     if (NS_SUCCEEDED(rv) && m_pMsgFields) {
       // IMPORT_LOG0("nsOutlookCompose - CreateComponents succeeded\n");
       m_pMsgFields->SetForcePlainText(false);
       return NS_OK;
     }
   }
 
   return NS_ERROR_FAILURE;
@@ -320,17 +313,17 @@ nsresult nsOutlookCompose::ComposeTheMes
                         m_pMsgFields,                 // message fields
                         msg.BodyIsHtml() ? "text/html" : "text/plain",
                         bodyA,                        // body pointer
                         mode == nsIMsgSend::nsMsgSaveAsDraft,
                         pAttach,                      // local attachments
                         embeddedObjects,
                         m_pListener);                 // listener
 
-  OutlookSendListener *pListen = (OutlookSendListener *)m_pListener;
+  OutlookSendListener *pListen = static_cast<OutlookSendListener *>(m_pListener.get());
   if (NS_FAILED(rv)) {
     IMPORT_LOG1("*** Error, CreateAndSendMessage FAILED: 0x%lx\n", rv);
   }
   else {
     // wait for the listener to get done!
     int32_t abortCnt = 0;
     int32_t cnt = 0;
     int32_t sleepCnt = 1;
--- a/mailnews/import/outlook/src/nsOutlookCompose.h
+++ b/mailnews/import/outlook/src/nsOutlookCompose.h
@@ -46,19 +46,19 @@ private:
 
   // Bug 593907
   void HackBody(const wchar_t* orig, size_t origLen, nsString& hack);
   void UnhackBody(nsCString& body);
   bool GenerateHackSequence(const wchar_t* body, size_t origLen);
   // End Bug 593907
 
 private:
-  nsIMsgSendListener *  m_pListener;
-  nsIMsgCompFields *    m_pMsgFields;
-  static nsIMsgIdentity *    m_pIdentity;
+  nsCOMPtr<nsIMsgSendListener> m_pListener;
+  nsCOMPtr<nsIMsgCompFields> m_pMsgFields;
+  static nsCOMPtr<nsIMsgIdentity> m_pIdentity;
   char* m_optimizationBuffer;
   nsCOMPtr<nsIImportService>  m_pImportService;
 
   // Bug 593907
   nsString m_hackedPostfix;
   // End Bug 593907
 };
 
--- a/mailnews/import/outlook/src/nsOutlookImport.cpp
+++ b/mailnews/import/outlook/src/nsOutlookImport.cpp
@@ -199,67 +199,66 @@ NS_IMETHODIMP nsOutlookImport::GetImport
   NS_PRECONDITION(ppInterface != nullptr, "null ptr");
   if (! ppInterface)
     return NS_ERROR_NULL_POINTER;
 
   *ppInterface = nullptr;
   nsresult  rv;
   if (!strcmp(pImportType, "mail")) {
     // create the nsIImportMail interface and return it!
-    nsIImportMail *  pMail = nullptr;
-    nsIImportGeneric *pGeneric = nullptr;
-    rv = ImportOutlookMailImpl::Create(&pMail);
+    nsCOMPtr<nsIImportMail> pMail;
+    nsCOMPtr<nsIImportGeneric> pGeneric;
+    rv = ImportOutlookMailImpl::Create(getter_AddRefs(pMail));
     if (NS_SUCCEEDED(rv)) {
       nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
       if (NS_SUCCEEDED(rv)) {
-        rv = impSvc->CreateNewGenericMail(&pGeneric);
+        rv = impSvc->CreateNewGenericMail(getter_AddRefs(pGeneric));
         if (NS_SUCCEEDED(rv)) {
           pGeneric->SetData("mailInterface", pMail);
           nsString name;
           nsOutlookStringBundle::GetStringByID(OUTLOOKIMPORT_NAME, name);
           nsCOMPtr<nsISupportsString> nameString (do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv));
           if (NS_SUCCEEDED(rv)) {
             nameString->SetData(name);
             pGeneric->SetData("name", nameString);
-            rv = pGeneric->QueryInterface(kISupportsIID, (void **)ppInterface);
+            nsCOMPtr<nsISupports> pInterface(do_QueryInterface(pGeneric));
+            pInterface.forget(ppInterface);
           }
         }
       }
     }
-    NS_IF_RELEASE(pMail);
-    NS_IF_RELEASE(pGeneric);
     return rv;
   }
 
   if (!strcmp(pImportType, "addressbook")) {
     // create the nsIImportAddressBook interface and return it!
-    nsIImportAddressBooks *  pAddress = nullptr;
-    nsIImportGeneric *    pGeneric = nullptr;
-    rv = ImportOutlookAddressImpl::Create(&pAddress);
+    nsCOMPtr<nsIImportAddressBooks> pAddress;
+    nsCOMPtr<nsIImportGeneric> pGeneric;
+    rv = ImportOutlookAddressImpl::Create(getter_AddRefs(pAddress));
     if (NS_SUCCEEDED(rv)) {
       nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
       if (NS_SUCCEEDED(rv)) {
-        rv = impSvc->CreateNewGenericAddressBooks(&pGeneric);
+        rv = impSvc->CreateNewGenericAddressBooks(getter_AddRefs(pGeneric));
         if (NS_SUCCEEDED(rv)) {
           pGeneric->SetData("addressInterface", pAddress);
-          rv = pGeneric->QueryInterface(kISupportsIID, (void **)ppInterface);
+          nsCOMPtr<nsISupports> pInterface(do_QueryInterface(pGeneric));
+          pInterface.forget(ppInterface);
         }
       }
     }
-    NS_IF_RELEASE(pAddress);
-    NS_IF_RELEASE(pGeneric);
     return rv;
   }
 
   if (!strcmp(pImportType, "settings")) {
-    nsIImportSettings *pSettings = nullptr;
-    rv = nsOutlookSettings::Create(&pSettings);
-    if (NS_SUCCEEDED(rv))
-      pSettings->QueryInterface(kISupportsIID, (void **)ppInterface);
-    NS_IF_RELEASE(pSettings);
+    nsCOMPtr<nsIImportSettings> pSettings;
+    rv = nsOutlookSettings::Create(getter_AddRefs(pSettings));
+    if (NS_SUCCEEDED(rv)) {
+      nsCOMPtr<nsISupports> pInterface(do_QueryInterface(pSettings));
+      pInterface.forget(ppInterface);
+    }
     return rv;
   }
 
   return NS_ERROR_NOT_AVAILABLE;
 }
 
 /////////////////////////////////////////////////////////////////////////////////
 nsresult ImportOutlookMailImpl::Create(nsIImportMail** aImport)
--- a/mailnews/import/outlook/src/nsOutlookMail.cpp
+++ b/mailnews/import/outlook/src/nsOutlookMail.cpp
@@ -155,36 +155,33 @@ nsresult nsOutlookMail::GetMailFolders(n
     if ((pFolder = m_storeList.GetItem(i))) {
       if (!m_mapi.GetStoreFolders(pFolder->GetCBEntryID(), pFolder->GetEntryID(), m_folderList, 1)) {
         IMPORT_LOG1("GetStoreFolders for index %d failed.\n", i);
       }
     }
   }
 
   // Create the mailbox descriptors for the list of folders
-  nsIImportMailboxDescriptor *  pID;
-  nsISupports *          pInterface;
+  nsCOMPtr<nsIImportMailboxDescriptor> pID;
   nsString            name;
   nsString            uniName;
 
   for (i = 0; i < m_folderList.GetSize(); i++) {
     pFolder = m_folderList.GetItem(i);
-    rv = impSvc->CreateNewMailboxDescriptor(&pID);
+    rv = impSvc->CreateNewMailboxDescriptor(getter_AddRefs(pID));
     if (NS_SUCCEEDED(rv)) {
       pID->SetDepth(pFolder->GetDepth());
       pID->SetIdentifier(i);
 
       pFolder->GetDisplayName(name);
       pID->SetDisplayName(name.get());
 
       pID->SetSize(1000);
-      rv = pID->QueryInterface(kISupportsIID, (void **) &pInterface);
+      nsCOMPtr<nsISupports> pInterface(do_QueryInterface(pID));
       array->AppendElement(pInterface, false);
-      pInterface->Release();
-      pID->Release();
     }
   }
   array.forget(pArray);
   return NS_OK;
 }
 
 bool nsOutlookMail::IsAddressBookNameUnique(nsString& name, nsString& list)
 {
@@ -258,35 +255,32 @@ nsresult nsOutlookMail::GetAddressBooks(
     if ((pFolder = m_storeList.GetItem(i))) {
       if (!m_mapi.GetStoreAddressFolders(pFolder->GetCBEntryID(), pFolder->GetEntryID(), m_addressList)) {
         IMPORT_LOG1("GetStoreFolders for index %d failed.\n", i);
       }
     }
   }
 
   // Create the mailbox descriptors for the list of folders
-  nsIImportABDescriptor *      pID;
-  nsISupports *          pInterface;
+  nsCOMPtr<nsIImportABDescriptor> pID;
   nsString            name;
   nsString            list;
 
   for (i = 0; i < m_addressList.GetSize(); i++) {
     pFolder = m_addressList.GetItem(i);
     if (!pFolder->IsStore()) {
-      rv = impSvc->CreateNewABDescriptor(&pID);
+      rv = impSvc->CreateNewABDescriptor(getter_AddRefs(pID));
       if (NS_SUCCEEDED(rv)) {
         pID->SetIdentifier(i);
         pFolder->GetDisplayName(name);
         MakeAddressBookNameUnique(name, list);
         pID->SetPreferredName(name);
         pID->SetSize(100);
-        rv = pID->QueryInterface(kISupportsIID, (void **) &pInterface);
+        nsCOMPtr<nsISupports> pInterface(do_QueryInterface(pID));
         array->AppendElement(pInterface, false);
-        pInterface->Release();
-        pID->Release();
       }
     }
   }
   array.forget(pArray);
   return NS_OK;
 }
 
 void nsOutlookMail::OpenMessageStore(CMapiFolder *pNextFolder)
--- a/mailnews/import/outlook/src/nsOutlookStringBundle.cpp
+++ b/mailnews/import/outlook/src/nsOutlookStringBundle.cpp
@@ -9,48 +9,41 @@
 #include "nsIStringBundle.h"
 #include "nsOutlookStringBundle.h"
 #include "nsIServiceManager.h"
 #include "nsIURI.h"
 #include "mozilla/Services.h"
 
 #define OUTLOOK_MSGS_URL       "chrome://messenger/locale/outlookImportMsgs.properties"
 
-nsIStringBundle *  nsOutlookStringBundle::m_pBundle = nullptr;
+nsCOMPtr<nsIStringBundle> nsOutlookStringBundle::m_pBundle = nullptr;
 
-nsIStringBundle *nsOutlookStringBundle::GetStringBundle(void)
+void nsOutlookStringBundle::GetStringBundle(void)
 {
   if (m_pBundle)
-    return m_pBundle;
-
-  char*        propertyURL = OUTLOOK_MSGS_URL;
-  nsIStringBundle*  sBundle = nullptr;
+    return;
 
   nsCOMPtr<nsIStringBundleService> sBundleService =
     mozilla::services::GetStringBundleService();
   if (sBundleService) {
-    sBundleService->CreateBundle(propertyURL, &sBundle);
+    sBundleService->CreateBundle(OUTLOOK_MSGS_URL, getter_AddRefs(m_pBundle));
   }
-
-  m_pBundle = sBundle;
-
-  return sBundle;
 }
 
 void nsOutlookStringBundle::GetStringByID(int32_t stringID, nsString& result)
 {
   char16_t *ptrv = GetStringByID(stringID);
   result = ptrv;
   FreeString(ptrv);
 }
 
 char16_t *nsOutlookStringBundle::GetStringByID(int32_t stringID)
 {
   if (m_pBundle)
-    m_pBundle = GetStringBundle();
+    GetStringBundle();
 
   if (m_pBundle) {
     nsAutoString str;
     nsresult rv = m_pBundle->GetStringFromID(stringID, str);
 
     if (NS_SUCCEEDED(rv))
       return ToNewUnicode(str);
   }
@@ -60,12 +53,10 @@ char16_t *nsOutlookStringBundle::GetStri
   resultString.AppendInt(stringID);
   resultString.AppendLiteral("?]");
 
   return ToNewUnicode(resultString);
 }
 
 void nsOutlookStringBundle::Cleanup(void)
 {
-  if (m_pBundle)
-    m_pBundle->Release();
   m_pBundle = nullptr;
 }
--- a/mailnews/import/outlook/src/nsOutlookStringBundle.h
+++ b/mailnews/import/outlook/src/nsOutlookStringBundle.h
@@ -9,21 +9,21 @@
 #include "nsString.h"
 
 class nsIStringBundle;
 
 class nsOutlookStringBundle {
 public:
   static char16_t     * GetStringByID(int32_t stringID);
   static void GetStringByID(int32_t stringID, nsString& result);
-  static nsIStringBundle * GetStringBundle(void); // don't release
+  static void GetStringBundle(void);
   static void FreeString(char16_t *pStr) { NS_Free(pStr);}
   static void Cleanup(void);
 private:
-  static nsIStringBundle * m_pBundle;
+  static nsCOMPtr<nsIStringBundle> m_pBundle;
 };
 
 
 
 #define OUTLOOKIMPORT_NAME                     2000
 #define OUTLOOKIMPORT_DESCRIPTION              2010
 #define OUTLOOKIMPORT_MAILBOX_SUCCESS          2002
 #define OUTLOOKIMPORT_MAILBOX_BADPARAM         2003
--- a/mailnews/import/src/nsImportABDescriptor.cpp
+++ b/mailnews/import/src/nsImportABDescriptor.cpp
@@ -9,24 +9,18 @@
 
 ////////////////////////////////////////////////////////////////////////
 
 nsresult nsImportABDescriptor::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
 {
   if (aOuter)
     return NS_ERROR_NO_AGGREGATION;
 
-  nsImportABDescriptor *it = new nsImportABDescriptor();
-  if (it == nullptr)
-    return NS_ERROR_OUT_OF_MEMORY;
-
-  NS_ADDREF(it);
-  nsresult rv = it->QueryInterface(aIID, aResult);
-  NS_RELEASE(it);
-  return rv;
+  RefPtr<nsImportABDescriptor> it = new nsImportABDescriptor();
+  return it->QueryInterface(aIID, aResult);
 }
 
 NS_IMPL_ISUPPORTS(nsImportABDescriptor, nsIImportABDescriptor)
 
 nsImportABDescriptor::nsImportABDescriptor()
   : mId(0), mRef(0), mSize(0), mImport(true)
 {
 }
--- a/mailnews/import/src/nsImportAddressBooks.cpp
+++ b/mailnews/import/src/nsImportAddressBooks.cpp
@@ -67,109 +67,88 @@ private:
   void  GetDefaultFieldMap(void);
 
 public:
   static void  SetLogs(nsString& success, nsString& error, nsISupportsString *pSuccess, nsISupportsString *pError);
   static void ReportError(const char16_t *pName, nsString *pStream,
                           nsIStringBundle *aBundle);
 
 private:
-  nsIImportAddressBooks *    m_pInterface;
-  nsCOMPtr<nsIArray> m_Books;
-  nsCOMArray<nsIAddrDatabase> m_DBs;
+  nsCOMPtr<nsIImportAddressBooks> m_pInterface;
+  nsCOMPtr<nsIArray>              m_Books;
+  nsCOMArray<nsIAddrDatabase>     m_DBs;
   nsCOMPtr <nsIFile>              m_pLocation;
-  nsIImportFieldMap *      m_pFieldMap;
-  bool              m_autoFind;
-  char16_t *          m_description;
-  bool              m_gotLocation;
-  bool              m_found;
-  bool              m_userVerify;
-  nsISupportsString *    m_pSuccessLog;
-  nsISupportsString *    m_pErrorLog;
-  uint32_t          m_totalSize;
-  bool              m_doImport;
-  AddressThreadData *      m_pThreadData;
-  char *            m_pDestinationUri;
-  nsCOMPtr<nsIStringBundle>   m_stringBundle;
+  nsCOMPtr<nsIImportFieldMap>     m_pFieldMap;
+  bool                            m_autoFind;
+  char16_t *                      m_description;
+  bool                            m_gotLocation;
+  bool                            m_found;
+  bool                            m_userVerify;
+  nsCOMPtr<nsISupportsString>     m_pSuccessLog;
+  nsCOMPtr<nsISupportsString>     m_pErrorLog;
+  uint32_t                        m_totalSize;
+  bool                            m_doImport;
+  AddressThreadData *             m_pThreadData;
+  nsCString                       m_pDestinationUri;
+  nsCOMPtr<nsIStringBundle>       m_stringBundle;
 };
 
 class AddressThreadData {
 public:
   bool              driverAlive;
   bool              threadAlive;
   bool              abort;
   bool              fatalError;
   uint32_t          currentTotal;
   uint32_t          currentSize;
-  nsIArray         *books;
-  nsCOMArray<nsIAddrDatabase>* dBs;
-  nsCOMPtr<nsIAbLDIFService> ldifService;
-  nsIImportAddressBooks *    addressImport;
-  nsIImportFieldMap *      fieldMap;
-  nsISupportsString *    successLog;
-  nsISupportsString *    errorLog;
-  char *            pDestinationUri;
-    nsIStringBundle*            stringBundle;
+  nsCOMPtr<nsIArray>              books;
+  nsCOMArray<nsIAddrDatabase>*    dBs;
+  nsCOMPtr<nsIAbLDIFService>      ldifService;
+  nsCOMPtr<nsIImportAddressBooks> addressImport;
+  nsCOMPtr<nsIImportFieldMap>     fieldMap;
+  nsCOMPtr<nsISupportsString>     successLog;
+  nsCOMPtr<nsISupportsString>     errorLog;
+  nsCString                       pDestinationUri;
+  nsCOMPtr<nsIStringBundle>       stringBundle;
 
   AddressThreadData();
   ~AddressThreadData();
 };
 
 
 nsresult NS_NewGenericAddressBooks(nsIImportGeneric** aImportGeneric)
 {
     NS_PRECONDITION(aImportGeneric != nullptr, "null ptr");
     if (! aImportGeneric)
         return NS_ERROR_NULL_POINTER;
 
-  nsImportGenericAddressBooks *pGen = new nsImportGenericAddressBooks();
-
-  if (pGen == nullptr)
-    return NS_ERROR_OUT_OF_MEMORY;
-
-  NS_ADDREF(pGen);
-  nsresult rv = pGen->QueryInterface(NS_GET_IID(nsIImportGeneric), (void **)aImportGeneric);
-  NS_RELEASE(pGen);
-
-    return rv;
+  RefPtr<nsImportGenericAddressBooks> pGen = new nsImportGenericAddressBooks();
+  return pGen->QueryInterface(NS_GET_IID(nsIImportGeneric), (void **)aImportGeneric);
 }
 
 nsImportGenericAddressBooks::nsImportGenericAddressBooks()
 {
-  m_pInterface = nullptr;
-  m_pSuccessLog = nullptr;
-  m_pErrorLog = nullptr;
   m_totalSize = 0;
   m_doImport = false;
   m_pThreadData = nullptr;
-  m_pDestinationUri = nullptr;
-  m_pFieldMap = nullptr;
 
   m_autoFind = false;
   m_description = nullptr;
   m_gotLocation = false;
   m_found = false;
   m_userVerify = false;
 
   nsImportStringBundle::GetStringBundle(IMPORT_MSGS_URL, getter_AddRefs(m_stringBundle));
 }
 
 
 nsImportGenericAddressBooks::~nsImportGenericAddressBooks()
 {
-  if (m_pDestinationUri)
-    NS_Free(m_pDestinationUri);
-
   if (m_description)
     NS_Free(m_description);
-
-  NS_IF_RELEASE(m_pFieldMap);
-  NS_IF_RELEASE(m_pInterface);
-  NS_IF_RELEASE(m_pSuccessLog);
-  NS_IF_RELEASE(m_pErrorLog);
 }
 
 
 
 NS_IMPL_ISUPPORTS(nsImportGenericAddressBooks, nsIImportGeneric)
 
 
 NS_IMETHODIMP nsImportGenericAddressBooks::GetData(const char *dataId, nsISupports **_retval)
@@ -191,20 +170,20 @@ NS_IMETHODIMP nsImportGenericAddressBook
     if (!m_pLocation)
       GetDefaultLocation();
     if (!m_Books)
       GetDefaultBooks();
     *_retval = m_Books;
   }
 
   if (!PL_strcasecmp(dataId, "addressDestination")) {
-    if (m_pDestinationUri) {
+    if (!m_pDestinationUri.IsEmpty()) {
       nsCOMPtr<nsISupportsCString> abString = do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
       NS_ENSURE_SUCCESS(rv, rv);
-      abString->SetData(nsDependentCString(m_pDestinationUri));
+      abString->SetData(m_pDestinationUri);
       abString.forget(_retval);
     }
   }
 
   if (!PL_strcasecmp(dataId, "fieldMap")) {
     if (m_pFieldMap) {
       NS_ADDREF(*_retval = m_pFieldMap);
     }
@@ -255,23 +234,22 @@ NS_IMETHODIMP nsImportGenericAddressBook
 
 NS_IMETHODIMP nsImportGenericAddressBooks::SetData(const char *dataId, nsISupports *item)
 {
   NS_PRECONDITION(dataId != nullptr, "null ptr");
   if (!dataId)
     return NS_ERROR_NULL_POINTER;
 
   if (!PL_strcasecmp(dataId, "addressInterface")) {
-    NS_IF_RELEASE(m_pInterface);
     if (item)
-      item->QueryInterface(NS_GET_IID(nsIImportAddressBooks), (void **) &m_pInterface);
+      m_pInterface = do_QueryInterface(item);
   }
   if (!PL_strcasecmp(dataId, "addressBooks")) {
     if (item)
-      item->QueryInterface(NS_GET_IID(nsIArray), (void **) &m_Books);
+      m_Books = do_QueryInterface(item);
   }
 
   if (!PL_strcasecmp(dataId, "addressLocation")) {
     m_pLocation = nullptr;
 
     if (item) {
       nsresult rv;
       m_pLocation = do_QueryInterface(item, &rv);
@@ -281,30 +259,24 @@ NS_IMETHODIMP nsImportGenericAddressBook
     if (m_pInterface)
       m_pInterface->SetSampleLocation(m_pLocation);
   }
 
   if (!PL_strcasecmp(dataId, "addressDestination")) {
     if (item) {
       nsCOMPtr<nsISupportsCString> abString = do_QueryInterface(item);
       if (abString) {
-        if (m_pDestinationUri)
-          NS_Free(m_pDestinationUri);
-        m_pDestinationUri = nullptr;
-                nsAutoCString tempUri;
-                abString->GetData(tempUri);
-                m_pDestinationUri = ToNewCString(tempUri);
+        abString->GetData(m_pDestinationUri);
       }
     }
   }
 
   if (!PL_strcasecmp(dataId, "fieldMap")) {
-    NS_IF_RELEASE(m_pFieldMap);
     if (item)
-      item->QueryInterface(NS_GET_IID(nsIImportFieldMap), (void **) &m_pFieldMap);
+      m_pFieldMap = do_QueryInterface(item);
   }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP nsImportGenericAddressBooks::GetStatus(const char *statusKind, int32_t *_retval)
 {
   NS_PRECONDITION(statusKind != nullptr, "null ptr");
@@ -385,38 +357,35 @@ void nsImportGenericAddressBooks::GetDef
   }
 }
 
 void nsImportGenericAddressBooks::GetDefaultFieldMap(void)
 {
   if (!m_pInterface || !m_pLocation)
     return;
 
-  NS_IF_RELEASE(m_pFieldMap);
-
   nsresult  rv;
   nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
   if (NS_FAILED(rv)) {
     IMPORT_LOG0("*** Unable to get nsIImportService.\n");
     return;
   }
 
-  rv = impSvc->CreateNewFieldMap(&m_pFieldMap);
+  rv = impSvc->CreateNewFieldMap(getter_AddRefs(m_pFieldMap));
   if (NS_FAILED(rv))
     return;
 
   int32_t  sz = 0;
   rv = m_pFieldMap->GetNumMozFields(&sz);
   if (NS_SUCCEEDED(rv))
     rv = m_pFieldMap->DefaultFieldMap(sz);
   if (NS_SUCCEEDED(rv))
     rv = m_pInterface->InitFieldMap(m_pFieldMap);
   if (NS_FAILED(rv)) {
     IMPORT_LOG0("*** Error: Unable to initialize field map\n");
-    NS_IF_RELEASE(m_pFieldMap);
   }
 }
 
 
 NS_IMETHODIMP nsImportGenericAddressBooks::WantsProgress(bool *_retval)
 {
   NS_PRECONDITION(_retval != nullptr, "null ptr");
   NS_ENSURE_ARG_POINTER(_retval);
@@ -612,64 +581,53 @@ NS_IMETHODIMP nsImportGenericAddressBook
       (needsFieldMap && !m_pFieldMap)) {
     nsImportStringBundle::GetStringByID(IMPORT_ERROR_AB_NOTINITIALIZED,
                                         m_stringBundle, error);
     SetLogs(success, error, successLog, errorLog);
     *_retval = false;
     return NS_OK;
   }
 
-  NS_IF_RELEASE(m_pSuccessLog);
-  NS_IF_RELEASE(m_pErrorLog);
   m_pSuccessLog = successLog;
   m_pErrorLog = errorLog;
-  NS_IF_ADDREF(m_pSuccessLog);
-  NS_IF_ADDREF(m_pErrorLog);
-
 
   // create the info need to drive address book import. We're
   // not going to create a new thread for this since address books
   // don't tend to be large, and import is rare.
   m_pThreadData = new AddressThreadData();
   m_pThreadData->books = m_Books;
-  NS_ADDREF(m_Books);
   m_pThreadData->addressImport = m_pInterface;
-  NS_ADDREF(m_pInterface);
   m_pThreadData->fieldMap = m_pFieldMap;
-  NS_IF_ADDREF(m_pFieldMap);
   m_pThreadData->errorLog = m_pErrorLog;
-  NS_IF_ADDREF(m_pErrorLog);
   m_pThreadData->successLog = m_pSuccessLog;
-  NS_IF_ADDREF(m_pSuccessLog);
-  if (m_pDestinationUri)
-    m_pThreadData->pDestinationUri = strdup(m_pDestinationUri);
+  m_pThreadData->pDestinationUri = m_pDestinationUri;
 
   uint32_t count = 0;
   m_Books->GetLength(&count);
   // Create/obtain any address books that we need here, so that we don't need
   // to do so inside the import thread which would just proxy the create
   // operations back to the main thread anyway.
-  nsCOMPtr<nsIAddrDatabase> db = GetAddressBookFromUri(m_pDestinationUri);
+  nsCOMPtr<nsIAddrDatabase> db = GetAddressBookFromUri(m_pDestinationUri.get());
   for (uint32_t i = 0; i < count; ++i)
   {
     nsCOMPtr<nsIImportABDescriptor> book = do_QueryElementAt(m_Books, i);
     if (book)
     {
       if (!db)
       {
         nsString name;
         book->GetPreferredName(name);
         db = GetAddressBook(name.get(), true);
       }
       m_DBs.AppendObject(db);
     }
   }
   m_pThreadData->dBs = &m_DBs;
 
-  NS_IF_ADDREF(m_pThreadData->stringBundle = m_stringBundle);
+  m_pThreadData->stringBundle = m_stringBundle;
 
   nsresult rv;
   m_pThreadData->ldifService = do_GetService(NS_ABLDIFSERVICE_CONTRACTID, &rv);
 
   ImportAddressThread(m_pThreadData);
   delete m_pThreadData;
   m_pThreadData = nullptr;
   *_retval = true;
@@ -744,37 +702,20 @@ NS_IMETHODIMP nsImportGenericAddressBook
 AddressThreadData::AddressThreadData()
 {
   fatalError = false;
   driverAlive = true;
   threadAlive = true;
   abort = false;
   currentTotal = 0;
   currentSize = 0;
-  books = nullptr;
-  addressImport = nullptr;
-  successLog = nullptr;
-  errorLog = nullptr;
-  pDestinationUri = nullptr;
-  fieldMap = nullptr;
-  stringBundle = nullptr;
-  ldifService = nullptr;
 }
 
 AddressThreadData::~AddressThreadData()
 {
-  if (pDestinationUri)
-    NS_Free(pDestinationUri);
-
-  NS_IF_RELEASE(books);
-  NS_IF_RELEASE(addressImport);
-  NS_IF_RELEASE(errorLog);
-  NS_IF_RELEASE(successLog);
-  NS_IF_RELEASE(fieldMap);
-  NS_IF_RELEASE(stringBundle);
 }
 
 void nsImportGenericAddressBooks::ReportError(const char16_t *pName,
                                               nsString *pStream,
                                               nsIStringBundle* aBundle)
 {
   if (!pStream)
     return;
--- a/mailnews/import/src/nsImportFieldMap.cpp
+++ b/mailnews/import/src/nsImportFieldMap.cpp
@@ -14,24 +14,18 @@
 
 ////////////////////////////////////////////////////////////////////////
 
 nsresult nsImportFieldMap::Create(nsIStringBundle *aBundle, nsISupports *aOuter, REFNSIID aIID, void **aResult)
 {
   if (aOuter)
     return NS_ERROR_NO_AGGREGATION;
 
-  nsImportFieldMap *it = new nsImportFieldMap(aBundle);
-  if (it == nullptr)
-    return NS_ERROR_OUT_OF_MEMORY;
-
-  NS_ADDREF(it);
-  nsresult rv = it->QueryInterface(aIID, aResult);
-  NS_RELEASE(it);
-  return rv;
+  RefPtr<nsImportFieldMap> it = new nsImportFieldMap(aBundle);
+  return it->QueryInterface(aIID, aResult);
 }
 
 NS_IMPL_ISUPPORTS(nsImportFieldMap, nsIImportFieldMap)
 
 NS_IMETHODIMP nsImportFieldMap::GetSkipFirstRecord(bool *result)
 {
   NS_ENSURE_ARG_POINTER(result);
   *result = m_skipFirstRecord;
--- a/mailnews/import/src/nsImportMail.cpp
+++ b/mailnews/import/src/nsImportMail.cpp
@@ -79,52 +79,52 @@ private:
   void  GetDefaultDestination(void);
   void  GetMailboxName(uint32_t index, nsISupportsString *pStr);
 
 public:
   static void  SetLogs(nsString& success, nsString& error, nsISupportsString *pSuccess, nsISupportsString *pError);
   static void ReportError(int32_t id, const char16_t *pName, nsString *pStream, nsIStringBundle* aBundle);
 
 private:
-  nsString      m_pName;  // module name that created this interface
-  nsIMsgFolder *    m_pDestFolder;
-  bool          m_deleteDestFolder;
-  bool          m_createdFolder;
-  nsCOMPtr <nsIFile> m_pSrcLocation;
-  bool          m_gotLocation;
-  bool          m_found;
-  bool          m_userVerify;
-  nsIImportMail *m_pInterface;
-  nsIArray *  m_pMailboxes;
-  nsISupportsString *m_pSuccessLog;
-  nsISupportsString *m_pErrorLog;
-  uint32_t      m_totalSize;
-  bool          m_doImport;
-  ImportThreadData *  m_pThreadData;
-    bool          m_performingMigration;
-  nsCOMPtr<nsIStringBundle> m_stringBundle;
+  nsString                    m_pName;  // module name that created this interface
+  nsCOMPtr<nsIMsgFolder>      m_pDestFolder;
+  bool                        m_deleteDestFolder;
+  bool                        m_createdFolder;
+  nsCOMPtr<nsIFile>           m_pSrcLocation;
+  bool                        m_gotLocation;
+  bool                        m_found;
+  bool                        m_userVerify;
+  nsCOMPtr<nsIImportMail>     m_pInterface;
+  nsCOMPtr<nsIArray>          m_pMailboxes;
+  nsCOMPtr<nsISupportsString> m_pSuccessLog;
+  nsCOMPtr<nsISupportsString> m_pErrorLog;
+  uint32_t                    m_totalSize;
+  bool                        m_doImport;
+  ImportThreadData *          m_pThreadData;
+  bool                        m_performingMigration;
+  nsCOMPtr<nsIStringBundle>   m_stringBundle;
 };
 
 class ImportThreadData {
 public:
   bool            driverAlive;
   bool            threadAlive;
   bool            abort;
   bool            fatalError;
   uint32_t        currentTotal;
   uint32_t        currentSize;
-  nsIMsgFolder *      destRoot;
-  bool            ownsDestRoot;
-  nsIArray *boxes;
-  nsIImportMail *      mailImport;
-  nsISupportsString *  successLog;
-  nsISupportsString *  errorLog;
-  uint32_t        currentMailbox;
-    bool            performingMigration;
-  nsIStringBundle *stringBundle;
+  nsCOMPtr<nsIMsgFolder>      destRoot;
+  bool                        ownsDestRoot;
+  nsCOMPtr<nsIArray>          boxes;
+  nsCOMPtr<nsIImportMail>     mailImport;
+  nsCOMPtr<nsISupportsString> successLog;
+  nsCOMPtr<nsISupportsString> errorLog;
+  uint32_t                    currentMailbox;
+  bool                        performingMigration;
+  nsCOMPtr<nsIStringBundle>   stringBundle;
 
   ImportThreadData();
   ~ImportThreadData();
   void DriverDelete();
   void ThreadDelete();
   void DriverAbort();
 };
 
@@ -143,68 +143,49 @@ nsresult ProxyCreateSubfolder(nsIMsgFold
 nsresult ProxyForceDBClosed(nsIMsgFolder *aFolder);
 
 nsresult NS_NewGenericMail(nsIImportGeneric** aImportGeneric)
 {
     NS_PRECONDITION(aImportGeneric != nullptr, "null ptr");
     if (! aImportGeneric)
         return NS_ERROR_NULL_POINTER;
 
-  nsImportGenericMail *pGen = new nsImportGenericMail();
-
-  if (pGen == nullptr)
-    return NS_ERROR_OUT_OF_MEMORY;
-
-  NS_ADDREF(pGen);
-  nsresult rv = pGen->QueryInterface(NS_GET_IID(nsIImportGeneric), (void **)aImportGeneric);
-  NS_RELEASE(pGen);
-
-    return rv;
+  RefPtr<nsImportGenericMail> pGen = new nsImportGenericMail();
+  return pGen->QueryInterface(NS_GET_IID(nsIImportGeneric), (void **)aImportGeneric);
 }
 
 nsImportGenericMail::nsImportGenericMail()
 {
   m_found = false;
   m_userVerify = false;
   m_gotLocation = false;
-  m_pInterface = nullptr;
-  m_pMailboxes = nullptr;
-  m_pSuccessLog = nullptr;
-  m_pErrorLog = nullptr;
   m_totalSize = 0;
   m_doImport = false;
   m_pThreadData = nullptr;
 
   m_pDestFolder = nullptr;
   m_deleteDestFolder = false;
   m_createdFolder = false;
-    m_performingMigration = false;
+  m_performingMigration = false;
 
   nsresult rv = nsImportStringBundle::GetStringBundle(IMPORT_MSGS_URL, getter_AddRefs(m_stringBundle));
   if (NS_FAILED(rv))
     IMPORT_LOG0("Failed to get string bundle for Importing Mail");
 }
 
 
 nsImportGenericMail::~nsImportGenericMail()
 {
   if (m_pThreadData) {
     m_pThreadData->DriverAbort();
     m_pThreadData = nullptr;
   }
-
-  NS_IF_RELEASE(m_pDestFolder);
-  NS_IF_RELEASE(m_pInterface);
-  NS_IF_RELEASE(m_pMailboxes);
-  NS_IF_RELEASE(m_pSuccessLog);
-  NS_IF_RELEASE(m_pErrorLog);
 }
 
 
-
 NS_IMPL_ISUPPORTS(nsImportGenericMail, nsIImportGeneric)
 
 
 NS_IMETHODIMP nsImportGenericMail::GetData(const char *dataId, nsISupports **_retval)
 {
   nsresult rv = NS_OK;
   NS_ENSURE_ARG_POINTER(_retval);
 
@@ -256,57 +237,53 @@ NS_IMETHODIMP nsImportGenericMail::GetDa
 NS_IMETHODIMP nsImportGenericMail::SetData(const char *dataId, nsISupports *item)
 {
   nsresult rv = NS_OK;
   NS_PRECONDITION(dataId != nullptr, "null ptr");
   if (!dataId)
     return NS_ERROR_NULL_POINTER;
 
   if (!PL_strcasecmp(dataId, "mailInterface")) {
-    NS_IF_RELEASE(m_pInterface);
     if (item)
-      item->QueryInterface(NS_GET_IID(nsIImportMail), (void **) &m_pInterface);
+      m_pInterface = do_QueryInterface(item);
   }
   if (!PL_strcasecmp(dataId, "mailBoxes")) {
-    NS_IF_RELEASE(m_pMailboxes);
     if (item)
-      item->QueryInterface(NS_GET_IID(nsIArray), (void **) &m_pMailboxes);
+      m_pMailboxes = do_QueryInterface(item);
   }
 
   if (!PL_strcasecmp(dataId, "mailLocation")) {
-    NS_IF_RELEASE(m_pMailboxes);
     m_pSrcLocation = nullptr;
     if (item) {
       nsresult rv;
-      nsCOMPtr <nsIFile> location = do_QueryInterface(item, &rv);
+      nsCOMPtr<nsIFile> location = do_QueryInterface(item, &rv);
       NS_ENSURE_SUCCESS(rv,rv);
       m_pSrcLocation = location;
     }
   }
 
   if (!PL_strcasecmp(dataId, "mailDestination")) {
-    NS_IF_RELEASE(m_pDestFolder);
     if (item)
-      item->QueryInterface(NS_GET_IID(nsIMsgFolder), (void **) &m_pDestFolder);
+      m_pDestFolder = do_QueryInterface(item);
     m_deleteDestFolder = false;
   }
 
   if (!PL_strcasecmp(dataId, "name")) {
-    nsCOMPtr<nsISupportsString> nameString;
     if (item) {
-      item->QueryInterface(NS_GET_IID(nsISupportsString), getter_AddRefs(nameString));
-      rv = nameString->GetData(m_pName);
+      nsCOMPtr<nsISupportsString> nameString = do_QueryInterface(item, &rv);
+      if (NS_SUCCEEDED(rv))
+        rv = nameString->GetData(m_pName);
     }
   }
 
   if (!PL_strcasecmp(dataId, "migration")) {
-    nsCOMPtr<nsISupportsPRBool> migrationString;
     if (item) {
-      item->QueryInterface(NS_GET_IID(nsISupportsPRBool), getter_AddRefs(migrationString));
-      rv = migrationString->GetData(&m_performingMigration);
+      nsCOMPtr<nsISupportsPRBool> migrationString = do_QueryInterface(item, &rv);
+      if (NS_SUCCEEDED(rv))
+        rv = migrationString->GetData(&m_performingMigration);
     }
   }
   return rv;
 }
 
 NS_IMETHODIMP nsImportGenericMail::GetStatus(const char *statusKind, int32_t *_retval)
 {
   NS_PRECONDITION(statusKind != nullptr, "null ptr");
@@ -346,17 +323,17 @@ void nsImportGenericMail::GetDefaultLoca
     m_pSrcLocation = pLoc;
 }
 
 void nsImportGenericMail::GetDefaultMailboxes(void)
 {
   if (!m_pInterface || m_pMailboxes || !m_pSrcLocation)
     return;
 
-  m_pInterface->FindMailboxes(m_pSrcLocation, &m_pMailboxes);
+  m_pInterface->FindMailboxes(m_pSrcLocation, getter_AddRefs(m_pMailboxes));
 }
 
 void nsImportGenericMail::GetDefaultDestination(void)
 {
   if (m_pDestFolder)
     return;
   if (!m_pInterface)
     return;
@@ -476,41 +453,31 @@ NS_IMETHODIMP nsImportGenericMail::Begin
     return NS_OK;
   }
 
   if (m_pThreadData) {
     m_pThreadData->DriverAbort();
     m_pThreadData = nullptr;
   }
 
-  NS_IF_RELEASE(m_pSuccessLog);
-  NS_IF_RELEASE(m_pErrorLog);
   m_pSuccessLog = successLog;
   m_pErrorLog = errorLog;
-  NS_IF_ADDREF(m_pSuccessLog);
-  NS_IF_ADDREF(m_pErrorLog);
-
 
   // kick off the thread to do the import!!!!
   m_pThreadData = new ImportThreadData();
   m_pThreadData->boxes = m_pMailboxes;
-  NS_ADDREF(m_pMailboxes);
   m_pThreadData->mailImport = m_pInterface;
-  NS_ADDREF(m_pInterface);
   m_pThreadData->errorLog = m_pErrorLog;
-  NS_IF_ADDREF(m_pErrorLog);
   m_pThreadData->successLog = m_pSuccessLog;
-  NS_IF_ADDREF(m_pSuccessLog);
 
   m_pThreadData->ownsDestRoot = m_deleteDestFolder;
   m_pThreadData->destRoot = m_pDestFolder;
-    m_pThreadData->performingMigration = m_performingMigration;
-  NS_IF_ADDREF(m_pDestFolder);
+  m_pThreadData->performingMigration = m_performingMigration;
 
-  NS_IF_ADDREF(m_pThreadData->stringBundle = m_stringBundle);
+  m_pThreadData->stringBundle = m_stringBundle;
 
   PRThread *pThread = PR_CreateThread(PR_USER_THREAD, &ImportMailThread, m_pThreadData,
                   PR_PRIORITY_NORMAL,
                   PR_LOCAL_THREAD,
                   PR_UNJOINABLE_THREAD,
                   0);
   if (!pThread) {
     m_pThreadData->ThreadDelete();
@@ -635,31 +602,20 @@ ImportThreadData::ImportThreadData()
   fatalError = false;
   driverAlive = true;
   threadAlive = true;
   abort = false;
   currentTotal = 0;
   currentSize = 0;
   destRoot = nullptr;
   ownsDestRoot = false;
-  boxes = nullptr;
-  mailImport = nullptr;
-  successLog = nullptr;
-  errorLog = nullptr;
-  stringBundle = nullptr;
 }
 
 ImportThreadData::~ImportThreadData()
 {
-  NS_IF_RELEASE(destRoot);
-  NS_IF_RELEASE(boxes);
-  NS_IF_RELEASE(mailImport);
-  NS_IF_RELEASE(errorLog);
-  NS_IF_RELEASE(successLog);
-  NS_IF_RELEASE(stringBundle);
 }
 
 void ImportThreadData::DriverDelete(void)
 {
   driverAlive = false;
   if (!driverAlive && !threadAlive)
     delete this;
 }
--- a/mailnews/import/src/nsImportMailboxDescriptor.cpp
+++ b/mailnews/import/src/nsImportMailboxDescriptor.cpp
@@ -12,24 +12,18 @@
 
 
 
 nsresult nsImportMailboxDescriptor::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
 {
   if (aOuter)
     return NS_ERROR_NO_AGGREGATION;
 
-  nsImportMailboxDescriptor *it = new nsImportMailboxDescriptor();
-  if (it == nullptr)
-    return NS_ERROR_OUT_OF_MEMORY;
-
-  NS_ADDREF(it);
-  nsresult rv = it->QueryInterface(aIID, aResult);
-  NS_RELEASE(it);
-  return rv;
+  RefPtr<nsImportMailboxDescriptor> it = new nsImportMailboxDescriptor();
+  return it->QueryInterface(aIID, aResult);
 }
 
 NS_IMPL_ISUPPORTS(nsImportMailboxDescriptor, nsIImportMailboxDescriptor)
 
 nsImportMailboxDescriptor::nsImportMailboxDescriptor()
 {
   m_import = true;
   m_size = 0;
--- a/mailnews/import/src/nsImportService.cpp
+++ b/mailnews/import/src/nsImportService.cpp
@@ -152,17 +152,17 @@ NS_IMETHODIMP nsImportService::GetModule
     return NS_ERROR_FAILURE;
   int32_t  cnt = m_pModules->GetCount();
   ImportModuleDesc *pDesc;
   for (int32_t i = 0; i < cnt; i++) {
     pDesc = m_pModules->GetModuleDesc(i);
     if (!pDesc)
       return NS_ERROR_FAILURE;
     if (pDesc->GetCID().Equals(cid)) {
-      *ppModule = pDesc->GetModule();
+      pDesc->GetModule(ppModule);
 
       IMPORT_LOG0("* nsImportService::GetSpecificModule - attempted to load module\n");
 
       if (*ppModule == nullptr)
         return NS_ERROR_FAILURE;
       return NS_OK;
     }
   }
@@ -365,17 +365,17 @@ NS_IMETHODIMP nsImportService::GetModule
     return NS_ERROR_FAILURE;
 
   ImportModuleDesc *  pDesc;
   int32_t  count = 0;
   for (int32_t i = 0; i < m_pModules->GetCount(); i++) {
     pDesc = m_pModules->GetModuleDesc(i);
     if (pDesc->SupportsThings(filter)) {
       if (count == index) {
-        *_retval = pDesc->GetModule();
+        pDesc->GetModule(_retval);
         break;
       }
       else
         count++;
     }
   }
   if (! (*_retval))
     return NS_ERROR_FAILURE;
@@ -432,75 +432,53 @@ nsresult nsImportService::LoadModuleInfo
     m_pModules = new nsImportModuleList();
 
   // load the component and get all of the info we need from it....
   // then call AddModule
   nsresult  rv;
 
   nsCID        clsId;
   clsId.Parse(pClsId);
-  nsIImportModule *  module;
-  rv = CallCreateInstance(clsId, &module);
+  nsCOMPtr<nsIImportModule> module = do_CreateInstance(clsId, &rv);
   if (NS_FAILED(rv)) return rv;
 
   nsString  theTitle;
   nsString  theDescription;
   rv = module->GetName(getter_Copies(theTitle));
   if (NS_FAILED(rv))
     theTitle.AssignLiteral("Unknown");
 
   rv = module->GetDescription(getter_Copies(theDescription));
   if (NS_FAILED(rv))
     theDescription.AssignLiteral("Unknown description");
 
   // call the module to get the info we need
   m_pModules->AddModule(clsId, pSupports, theTitle.get(), theDescription.get());
 
-  module->Release();
-
   return NS_OK;
 }
 
-
-nsIImportModule *ImportModuleDesc::GetModule(bool keepLoaded)
+// XXX This should return already_AddRefed.
+void ImportModuleDesc::GetModule(nsIImportModule **_retval)
 {
-  if (m_pModule)
+  if (!m_pModule)
   {
-    m_pModule->AddRef();
-    return m_pModule;
+    nsresult  rv;
+    m_pModule = do_CreateInstance(m_cid, &rv);
+    if (NS_FAILED(rv))
+      m_pModule = nullptr;
   }
 
-  nsresult  rv;
-  rv = CallCreateInstance(m_cid, &m_pModule);
-  if (NS_FAILED(rv))
-  {
-    m_pModule = nullptr;
-    return nullptr;
-  }
-
-  if (keepLoaded)
-  {
-    m_pModule->AddRef();
-    return m_pModule;
-  }
-  else
-  {
-    nsIImportModule *pModule = m_pModule;
-    m_pModule = nullptr;
-    return pModule;
-  }
+  NS_IF_ADDREF(*_retval = m_pModule);
+  return;
 }
 
 void ImportModuleDesc::ReleaseModule(void)
 {
-  if (m_pModule)
-  {
-    m_pModule->Release();
-    m_pModule = nullptr;
-  }
+  m_pModule = nullptr;
 }
 
 bool ImportModuleDesc::SupportsThings(const char *pThings)
 {
   if (!pThings || !*pThings)
     return true;
 
   nsCString thing(pThings);
--- a/mailnews/import/src/nsImportService.h
+++ b/mailnews/import/src/nsImportService.h
@@ -51,27 +51,27 @@ public:
   void  SetDescription(const char16_t *pDesc) { m_description = pDesc;}
   void  SetSupports(const char *pSupports) { m_supports = pSupports;}
 
   nsCID      GetCID(void) { return m_cid;}
   const char16_t *GetName(void) { return m_name.get();}
   const char16_t *GetDescription(void) { return m_description.get();}
   const char *  GetSupports(void) { return m_supports.get();}
 
-  nsIImportModule *  GetModule(bool keepLoaded = false); // Adds ref
+  void        GetModule(nsIImportModule **);
   void        ReleaseModule(void);
 
   bool        SupportsThings(const char *pThings);
 
 private:
-    nsCID m_cid;
+  nsCID m_cid;
   nsString m_name;
   nsString m_description;
   nsCString m_supports;
-  nsIImportModule *m_pModule;
+  nsCOMPtr<nsIImportModule> m_pModule;
 };
 
 class nsImportModuleList {
 public:
   nsImportModuleList() { m_pList = nullptr; m_alloc = 0; m_count = 0;}
   ~nsImportModuleList() { ClearList(); }
 
   void  AddModule(const nsCID& cid, const char *pSupports, const char16_t *pName, const char16_t *pDesc);
--- a/mailnews/import/text/src/nsTextAddress.cpp
+++ b/mailnews/import/text/src/nsTextAddress.cpp
@@ -22,26 +22,22 @@
 #include "plstr.h"
 #include "msgCore.h"
 #include <algorithm>
 
 #define kWhitespace    " \t\b\r\n"
 
 nsTextAddress::nsTextAddress()
 {
-    m_database = nullptr;
-    m_fieldMap = nullptr;
-    m_LFCount = 0;
-    m_CRCount = 0;
+  m_LFCount = 0;
+  m_CRCount = 0;
 }
 
 nsTextAddress::~nsTextAddress()
 {
-    NS_IF_RELEASE(m_database);
-    NS_IF_RELEASE(m_fieldMap);
 }
 
 nsresult nsTextAddress::GetUnicharLineStreamForFile(nsIFile *aFile,
                                                     nsIInputStream *aInputStream,
                                                     nsIUnicharLineInputStream **aStream)
 {
   nsAutoCString charset;
   nsresult rv = MsgDetectCharsetFromFile(aFile, charset);
@@ -59,22 +55,18 @@ nsresult nsTextAddress::GetUnicharLineSt
   }
 
   return CallQueryInterface(converterStream, aStream);
 }
 
 nsresult nsTextAddress::ImportAddresses(bool *pAbort, const char16_t *pName, nsIFile *pSrc, nsIAddrDatabase *pDb, nsIImportFieldMap *fieldMap, nsString& errors, uint32_t *pProgress)
 {
   // Open the source file for reading, read each line and process it!
-  NS_IF_RELEASE(m_database);
-  NS_IF_RELEASE(m_fieldMap);
   m_database = pDb;
   m_fieldMap = fieldMap;
-  NS_ADDREF(m_fieldMap);
-  NS_ADDREF(m_database);
 
   nsCOMPtr<nsIInputStream> inputStream;
   nsresult rv = NS_NewLocalFileInputStream(getter_AddRefs(inputStream), pSrc);
   if (NS_FAILED(rv)) {
     IMPORT_LOG0("*** Error opening address file for reading\n");
     return rv;
   }
 
--- a/mailnews/import/text/src/nsTextAddress.h
+++ b/mailnews/import/text/src/nsTextAddress.h
@@ -41,17 +41,17 @@ private:
   static nsresult ReadRecord(nsIUnicharLineInputStream *pSrc, nsAString &aLine, bool *aMore);
   static nsresult GetUnicharLineStreamForFile(nsIFile *aFile,
                                               nsIInputStream *aInputStream,
                                               nsIUnicharLineInputStream **aStream);
 
   char16_t m_delim;
   int32_t m_LFCount;
   int32_t m_CRCount;
-  nsIAddrDatabase *m_database;
-  nsIImportFieldMap *m_fieldMap;
-  nsCOMPtr<nsIImportService> m_pService;
+  nsCOMPtr<nsIAddrDatabase>   m_database;
+  nsCOMPtr<nsIImportFieldMap> m_fieldMap;
+  nsCOMPtr<nsIImportService>  m_pService;
 };
 
 
 
 #endif /* nsTextAddress_h__ */
 
--- a/mailnews/import/text/src/nsTextImport.cpp
+++ b/mailnews/import/text/src/nsTextImport.cpp
@@ -164,31 +164,30 @@ NS_IMETHODIMP nsTextImport::GetImportInt
   NS_ENSURE_ARG_POINTER(pImportType);
   NS_ENSURE_ARG_POINTER(ppInterface);
 
   *ppInterface = nullptr;
   nsresult rv;
 
   if (!strcmp(pImportType, "addressbook")) {
     // create the nsIImportMail interface and return it!
-    nsIImportAddressBooks * pAddress = nullptr;
-    nsIImportGeneric * pGeneric = nullptr;
-    rv = ImportAddressImpl::Create(&pAddress, m_stringBundle);
+    nsCOMPtr<nsIImportAddressBooks> pAddress;
+    nsCOMPtr<nsIImportGeneric> pGeneric;
+    rv = ImportAddressImpl::Create(getter_AddRefs(pAddress), m_stringBundle);
     if (NS_SUCCEEDED(rv)) {
       nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
       if (NS_SUCCEEDED(rv)) {
-        rv = impSvc->CreateNewGenericAddressBooks(&pGeneric);
+        rv = impSvc->CreateNewGenericAddressBooks(getter_AddRefs(pGeneric));
         if (NS_SUCCEEDED(rv)) {
           pGeneric->SetData("addressInterface", pAddress);
-          rv = pGeneric->QueryInterface(kISupportsIID, (void **)ppInterface);
+          nsCOMPtr<nsISupports> pInterface(do_QueryInterface(pGeneric));
+          pInterface.forget(ppInterface);
         }
       }
     }
-    NS_IF_RELEASE(pAddress);
-    NS_IF_RELEASE(pGeneric);
     return rv;
   }
   return NS_ERROR_NOT_AVAILABLE;
 }
 
 /////////////////////////////////////////////////////////////////////////////////
 
 
@@ -290,35 +289,33 @@ NS_IMETHODIMP ImportAddressImpl::FindAdd
     return rv;
   }
 
   int32_t idx = name.RFindChar('.');
   if ((idx != -1) && (idx > 0) && ((name.Length() - idx - 1) < 5)) {
     name.SetLength(idx);
   }
 
-  nsCOMPtr<nsIImportABDescriptor>  desc;
-  nsISupports * pInterface;
+  nsCOMPtr<nsIImportABDescriptor> desc;
 
   nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
   if (NS_FAILED(rv)) {
     IMPORT_LOG0("*** Failed to obtain the import service\n");
     return rv;
   }
 
   rv = impSvc->CreateNewABDescriptor(getter_AddRefs(desc));
   if (NS_SUCCEEDED(rv)) {
     int64_t sz = 0;
     pLoc->GetFileSize(&sz);
     desc->SetPreferredName(name);
     desc->SetSize((uint32_t) sz);
     desc->SetAbFile(m_fileLoc);
-    rv = desc->QueryInterface(kISupportsIID, (void **) &pInterface);
+    nsCOMPtr<nsISupports> pInterface(do_QueryInterface(desc));
     array->AppendElement(pInterface, false);
-    pInterface->Release();
   }
   if (NS_FAILED(rv)) {
     IMPORT_LOG0("*** Error creating address book descriptor for text import\n");
     return rv;
   }
   array.forget(ppArray);
   return NS_OK;
 }
--- a/mailnews/import/vcard/src/nsVCardImport.cpp
+++ b/mailnews/import/vcard/src/nsVCardImport.cpp
@@ -140,32 +140,31 @@ NS_IMETHODIMP nsVCardImport::GetImportIn
     const char *pImportType, nsISupports **ppInterface)
 {
   NS_ENSURE_ARG_POINTER(pImportType);
   NS_ENSURE_ARG_POINTER(ppInterface);
   *ppInterface = nullptr;
   if (!strcmp(pImportType, "addressbook")) {
     nsresult rv;
     // create the nsIImportMail interface and return it!
-    nsIImportAddressBooks *pAddress = nullptr;
-    nsIImportGeneric *pGeneric = nullptr;
-    rv = ImportVCardAddressImpl::Create(&pAddress, m_stringBundle);
+    nsCOMPtr<nsIImportAddressBooks> pAddress;
+    nsCOMPtr<nsIImportGeneric> pGeneric;
+    rv = ImportVCardAddressImpl::Create(getter_AddRefs(pAddress), m_stringBundle);
     if (NS_SUCCEEDED(rv)) {
       nsCOMPtr<nsIImportService> impSvc(
           do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
       if (NS_SUCCEEDED(rv)) {
-        rv = impSvc->CreateNewGenericAddressBooks(&pGeneric);
+        rv = impSvc->CreateNewGenericAddressBooks(getter_AddRefs(pGeneric));
         if (NS_SUCCEEDED(rv)) {
           pGeneric->SetData("addressInterface", pAddress);
-          rv = pGeneric->QueryInterface(kISupportsIID, (void **)ppInterface);
+          nsCOMPtr<nsISupports> pInterface(do_QueryInterface(pGeneric));
+          pInterface.forget(ppInterface);
         }
       }
     }
-    NS_IF_RELEASE(pAddress);
-    NS_IF_RELEASE(pGeneric);
     return rv;
   }
   return NS_ERROR_NOT_AVAILABLE;
 }
 
 nsresult ImportVCardAddressImpl::Create(
     nsIImportAddressBooks** aImport, nsIStringBundle* aStringBundle)
 {
--- a/mailnews/import/winlivemail/nsWMImport.cpp
+++ b/mailnews/import/winlivemail/nsWMImport.cpp
@@ -134,22 +134,22 @@ NS_IMETHODIMP nsWMImport::GetImportInter
 {
   NS_ENSURE_ARG_POINTER(pImportType);
   NS_ENSURE_ARG_POINTER(ppInterface);
 
   *ppInterface = nullptr;
   nsresult rv;
 
   if (!strcmp(pImportType, "settings")) {
-    nsIImportSettings *pSettings = nullptr;
-    rv = nsWMSettings::Create(&pSettings);
+    nsCOMPtr<nsIImportSettings> pSettings;
+    rv = nsWMSettings::Create(getter_AddRefs(pSettings));
     if (NS_SUCCEEDED(rv)) {
-      pSettings->QueryInterface(kISupportsIID, (void **)ppInterface);
+      nsCOMPtr<nsISupports> pInterface(do_QueryInterface(pSettings));
+      pInterface.forget(ppInterface);
     }
-    NS_IF_RELEASE(pSettings);
     return rv;
   }
 
   return NS_ERROR_NOT_AVAILABLE;
 }
 
 /////////////////////////////////////////////////////////////////////////////////
 nsresult ImportWMMailImpl::Create(nsIImportMail** aImport)
--- a/mailnews/import/winlivemail/nsWMStringBundle.cpp
+++ b/mailnews/import/winlivemail/nsWMStringBundle.cpp
@@ -9,48 +9,43 @@
 #include "nsIStringBundle.h"
 #include "nsWMStringBundle.h"
 #include "nsIServiceManager.h"
 #include "nsIURI.h"
 #include "mozilla/Services.h"
 
 #define WM_MSGS_URL       "chrome://messenger/locale/wmImportMsgs.properties"
 
-nsIStringBundle *  nsWMStringBundle::m_pBundle = nullptr;
+nsCOMPtr<nsIStringBundle> nsWMStringBundle::m_pBundle = nullptr;
 
-nsIStringBundle *nsWMStringBundle::GetStringBundle(void)
+void nsWMStringBundle::GetStringBundle(void)
 {
   if (m_pBundle)
-    return m_pBundle;
+    return;
 
-  char*        propertyURL = WM_MSGS_URL;
-  nsIStringBundle*  sBundle = nullptr;
+  char*  propertyURL = WM_MSGS_URL;
 
   nsCOMPtr<nsIStringBundleService> sBundleService =
     mozilla::services::GetStringBundleService();
   if (sBundleService) {
-    sBundleService->CreateBundle(propertyURL, &sBundle);
+    sBundleService->CreateBundle(propertyURL, getter_AddRefs(m_pBundle));
   }
-
-  m_pBundle = sBundle;
-
-  return sBundle;
 }
 
 void nsWMStringBundle::GetStringByID(int32_t stringID, nsString& result)
 {
   char16_t *ptrv = GetStringByID(stringID);
   result = ptrv;
   FreeString(ptrv);
 }
 
 char16_t *nsWMStringBundle::GetStringByID(int32_t stringID)
 {
   if (!m_pBundle)
-    m_pBundle = GetStringBundle();
+    GetStringBundle();
 
   if (m_pBundle) {
     nsAutoString str;
     nsresult rv = m_pBundle->GetStringFromID(stringID, str);
 
     if (NS_SUCCEEDED(rv))
       return ToNewUnicode(str);
   }
@@ -60,12 +55,10 @@ char16_t *nsWMStringBundle::GetStringByI
   resultString.AppendInt(stringID);
   resultString.AppendLiteral("?]");
 
   return ToNewUnicode(resultString);
 }
 
 void nsWMStringBundle::Cleanup(void)
 {
-  if (m_pBundle)
-    m_pBundle->Release();
   m_pBundle = nullptr;
 }
--- a/mailnews/import/winlivemail/nsWMStringBundle.h
+++ b/mailnews/import/winlivemail/nsWMStringBundle.h
@@ -8,22 +8,22 @@
 #include "nsString.h"
 
 class nsIStringBundle;
 
 class nsWMStringBundle {
 public:
   static char16_t     *    GetStringByID(int32_t stringID);
   static void          GetStringByID(int32_t stringID, nsString& result);
-  static nsIStringBundle *  GetStringBundle(void); // don't release
+  static void          GetStringBundle(void);
   static void          FreeString(char16_t *pStr) { NS_Free(pStr);}
   static void          Cleanup(void);
 
 private:
-  static nsIStringBundle *  m_pBundle;
+  static nsCOMPtr<nsIStringBundle> m_pBundle;
 };
 
 
 
 #define WMIMPORT_NAME                     2000
 #define WMIMPORT_DESCRIPTION              2001
 #define WMIMPORT_MAILBOX_SUCCESS          2002
 #define WMIMPORT_MAILBOX_BADPARAM         2003