Bug 490602 Address book import leaks due to incorrect reference counting of proxy pointers. r/sr=bienvenu
authorMark Banner <bugzilla@standard8.plus.com>
Thu, 30 Apr 2009 09:44:14 +0100
changeset 2516 8e72da55519104f7c343448c2ee2daa6529f588d
parent 2515 1ddfa4eb317074dd368a6756dd3247cb62d6b7f4
child 2517 3c908fd109226622140a2c22a30ac257b04e2323
push idunknown
push userunknown
push dateunknown
bugs490602
Bug 490602 Address book import leaks due to incorrect reference counting of proxy pointers. r/sr=bienvenu
mailnews/addrbook/src/nsAbLDIFService.cpp
mailnews/addrbook/src/nsAbLDIFService.h
mailnews/import/src/nsImportAddressBooks.cpp
--- a/mailnews/addrbook/src/nsAbLDIFService.cpp
+++ b/mailnews/addrbook/src/nsAbLDIFService.cpp
@@ -92,17 +92,16 @@ static unsigned char b642nib[0x80] = {
     0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
     0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
 };
 
 NS_IMETHODIMP nsAbLDIFService::ImportLDIFFile(nsIAddrDatabase *aDb, nsIFile *aSrc, PRBool aStoreLocAsHome, PRUint32 *aProgress)
 {
   NS_ENSURE_ARG_POINTER(aSrc);
   NS_ENSURE_ARG_POINTER(aDb);
-  mDatabase = aDb;
 
   mStoreLocAsHome = aStoreLocAsHome;
 
   char buf[1024];
   char* pBuf = &buf[0];
   PRInt32 startPos = 0;
   PRUint32 len = 0;
   nsVoidArray listPosArray;   // where each list/group starts in ldif file
@@ -122,34 +121,34 @@ NS_IMETHODIMP nsAbLDIFService::ImportLDI
   {
     if (NS_SUCCEEDED(inputStream->Read(pBuf, sizeof(buf), &len)) && len > 0)
     {
       startPos = 0;
 
       while (NS_SUCCEEDED(GetLdifStringRecord(buf, len, startPos)))
       {
         if (mLdifLine.Find("groupOfNames") == -1)
-          AddLdifRowToDatabase(PR_FALSE);
+          AddLdifRowToDatabase(aDb, PR_FALSE);
         else
         {
           //keep file position for mailing list
           listPosArray.AppendElement((void*)savedStartPos);
           listSizeArray.AppendElement((void*)(filePos + startPos-savedStartPos));
           ClearLdifRecordBuffer();
         }
         savedStartPos = filePos + startPos;
       }
       filePos += len;
       if (aProgress)
         *aProgress = (PRUint32)filePos;
     }
   }
   //last row
   if (!mLdifLine.IsEmpty() && mLdifLine.Find("groupOfNames") == -1)
-    AddLdifRowToDatabase(PR_FALSE); 
+    AddLdifRowToDatabase(aDb, PR_FALSE); 
 
   // mail Lists
   PRInt32 i, pos;
   PRUint32 size;
   PRInt32 listTotal = listPosArray.Count();
   char *listBuf;
   ClearLdifRecordBuffer();  // make sure the buffer is clean
 
@@ -169,17 +168,17 @@ NS_IMETHODIMP nsAbLDIFService::ImportLDI
       if (NS_SUCCEEDED(inputStream->Read(listBuf, size, &len)) && len > 0)
       {
         startPos = 0;
 
         while (NS_SUCCEEDED(GetLdifStringRecord(listBuf, len, startPos)))
         {
           if (mLdifLine.Find("groupOfNames") != -1)
           {
-            AddLdifRowToDatabase(PR_TRUE);
+            AddLdifRowToDatabase(aDb, PR_TRUE);
             if (NS_SUCCEEDED(seekableStream->Seek(nsISeekableStream::NS_SEEK_SET, 0)))
               break;
           }
         }
       }
     PR_FREEIF(listBuf);
     }
   }
@@ -366,394 +365,397 @@ nsresult nsAbLDIFService::GetLdifStringR
 
   if ((stopPos == len) && (mLFCount > 1) || (mCRCount > 2 && mLFCount) ||
       (!mLFCount && mCRCount > 1))
     return NS_OK;
 
   return NS_ERROR_FAILURE;
 }
 
-void nsAbLDIFService::AddLdifRowToDatabase(PRBool bIsList)
+void nsAbLDIFService::AddLdifRowToDatabase(nsIAddrDatabase *aDatabase,
+                                           PRBool bIsList)
 {
   // If no data to process then reset CR/LF counters and return.
   if (mLdifLine.IsEmpty())
   {
     mLFCount = 0;
     mCRCount = 0;
     return;
   }
 
   nsCOMPtr <nsIMdbRow> newRow;
-  if (mDatabase)
+  if (aDatabase)
   {
     if (bIsList)
-      mDatabase->GetNewListRow(getter_AddRefs(newRow)); 
+      aDatabase->GetNewListRow(getter_AddRefs(newRow)); 
     else
-      mDatabase->GetNewRow(getter_AddRefs(newRow)); 
+      aDatabase->GetNewRow(getter_AddRefs(newRow)); 
 
     if (!newRow)
       return;
   }
   else
     return;
 
   char* cursor = ToNewCString(mLdifLine); 
   char* saveCursor = cursor;  /* keep for deleting */ 
   char* line = 0; 
   char* typeSlot = 0; 
   char* valueSlot = 0; 
   int length = 0;  // the length  of an ldif attribute
   while ( (line = str_getline(&cursor)) != nsnull)
   {
     if ( str_parse_line(line, &typeSlot, &valueSlot, &length) == 0) {
-      AddLdifColToDatabase(newRow, typeSlot, valueSlot, bIsList);
+      AddLdifColToDatabase(aDatabase, newRow, typeSlot, valueSlot, bIsList);
     }
     else
       continue; // parse error: continue with next loop iteration
   }
   nsMemory::Free(saveCursor);
-  mDatabase->AddCardRowToDB(newRow);    
+  aDatabase->AddCardRowToDB(newRow);    
 
   if (bIsList)
-    mDatabase->AddListDirNode(newRow);
+    aDatabase->AddListDirNode(newRow);
         
   // Clear buffer for next record
   ClearLdifRecordBuffer();
 }
 
-void nsAbLDIFService::AddLdifColToDatabase(nsIMdbRow* newRow, char* typeSlot, char* valueSlot, PRBool bIsList)
+void nsAbLDIFService::AddLdifColToDatabase(nsIAddrDatabase *aDatabase,
+                                           nsIMdbRow* newRow, char* typeSlot,
+                                           char* valueSlot, PRBool bIsList)
 {
   nsCAutoString colType(typeSlot);
   nsCAutoString column(valueSlot);
 
   // 4.x exports attributes like "givenname", 
   // mozilla does "givenName" to be compliant with RFC 2798
   ToLowerCase(colType);
 
   mdb_u1 firstByte = (mdb_u1)(colType.get())[0];
   switch ( firstByte )
   {
   case 'b':
     if (colType.EqualsLiteral("birthyear"))
-      mDatabase->AddBirthYear(newRow, column.get());
+      aDatabase->AddBirthYear(newRow, column.get());
     else if (colType.EqualsLiteral("birthmonth"))
-      mDatabase->AddBirthMonth(newRow, column.get());
+      aDatabase->AddBirthMonth(newRow, column.get());
     else if (colType.EqualsLiteral("birthday"))
-      mDatabase->AddBirthDay(newRow, column.get());
+      aDatabase->AddBirthDay(newRow, column.get());
     break; // 'b'
 
   case 'c':
     if (colType.EqualsLiteral("cn") || colType.EqualsLiteral("commonname"))
     {
       if (bIsList)
-        mDatabase->AddListName(newRow, column.get());
+        aDatabase->AddListName(newRow, column.get());
       else
-        mDatabase->AddDisplayName(newRow, column.get());
+        aDatabase->AddDisplayName(newRow, column.get());
     }
     else if (colType.EqualsLiteral("c") || colType.EqualsLiteral("countryname"))
     {
       if (mStoreLocAsHome )
-        mDatabase->AddHomeCountry(newRow, column.get());
+        aDatabase->AddHomeCountry(newRow, column.get());
       else
-        mDatabase->AddWorkCountry(newRow, column.get());
+        aDatabase->AddWorkCountry(newRow, column.get());
     }
 
     else if (colType.EqualsLiteral("cellphone") )
-      mDatabase->AddCellularNumber(newRow, column.get());
+      aDatabase->AddCellularNumber(newRow, column.get());
 
     else if (colType.EqualsLiteral("carphone"))
-      mDatabase->AddCellularNumber(newRow, column.get());
+      aDatabase->AddCellularNumber(newRow, column.get());
         
     else if (colType.EqualsLiteral("custom1"))
-      mDatabase->AddCustom1(newRow, column.get());
+      aDatabase->AddCustom1(newRow, column.get());
         
     else if (colType.EqualsLiteral("custom2"))
-      mDatabase->AddCustom2(newRow, column.get());
+      aDatabase->AddCustom2(newRow, column.get());
         
     else if (colType.EqualsLiteral("custom3"))
-      mDatabase->AddCustom3(newRow, column.get());
+      aDatabase->AddCustom3(newRow, column.get());
         
     else if (colType.EqualsLiteral("custom4"))
-      mDatabase->AddCustom4(newRow, column.get());
+      aDatabase->AddCustom4(newRow, column.get());
         
     else if (colType.EqualsLiteral("company"))
-      mDatabase->AddCompany(newRow, column.get());
+      aDatabase->AddCompany(newRow, column.get());
     break; // 'c'
 
   case 'd':
     if (colType.EqualsLiteral("description"))
     {
       if (bIsList)
-        mDatabase->AddListDescription(newRow, column.get());
+        aDatabase->AddListDescription(newRow, column.get());
       else
-        mDatabase->AddNotes(newRow, column.get());
+        aDatabase->AddNotes(newRow, column.get());
     }
 
     else if (colType.EqualsLiteral("department"))
-      mDatabase->AddDepartment(newRow, column.get());
+      aDatabase->AddDepartment(newRow, column.get());
 
     else if (colType.EqualsLiteral("displayname"))
     {
       if (bIsList)
-        mDatabase->AddListName(newRow, column.get());
+        aDatabase->AddListName(newRow, column.get());
       else
-        mDatabase->AddDisplayName(newRow, column.get());
+        aDatabase->AddDisplayName(newRow, column.get());
     }
     break; // 'd'
 
   case 'f':
 
     if (colType.EqualsLiteral("fax") ||
         colType.EqualsLiteral("facsimiletelephonenumber"))
-      mDatabase->AddFaxNumber(newRow, column.get());
+      aDatabase->AddFaxNumber(newRow, column.get());
     break; // 'f'
 
   case 'g':
     if (colType.EqualsLiteral("givenname"))
-      mDatabase->AddFirstName(newRow, column.get());
+      aDatabase->AddFirstName(newRow, column.get());
 
     break; // 'g'
 
   case 'h':
     if (colType.EqualsLiteral("homephone"))
-      mDatabase->AddHomePhone(newRow, column.get());
+      aDatabase->AddHomePhone(newRow, column.get());
 
     else if (colType.EqualsLiteral("homestreet"))
-      mDatabase->AddHomeAddress(newRow, column.get());
+      aDatabase->AddHomeAddress(newRow, column.get());
 
     else if (colType.EqualsLiteral("homeurl"))
-      mDatabase->AddWebPage2(newRow, column.get());
+      aDatabase->AddWebPage2(newRow, column.get());
     break; // 'h'
 
   case 'l':
     if (colType.EqualsLiteral("l") || colType.EqualsLiteral("locality"))
     {
       if (mStoreLocAsHome)
-        mDatabase->AddHomeCity(newRow, column.get());
+        aDatabase->AddHomeCity(newRow, column.get());
       else
-        mDatabase->AddWorkCity(newRow, column.get());
+        aDatabase->AddWorkCity(newRow, column.get());
     }
 
     break; // 'l'
 
   case 'm':
     if (colType.EqualsLiteral("mail"))
-      mDatabase->AddPrimaryEmail(newRow, column.get());
+      aDatabase->AddPrimaryEmail(newRow, column.get());
 
     else if (colType.EqualsLiteral("member") && bIsList)
-      mDatabase->AddLdifListMember(newRow, column.get());
+      aDatabase->AddLdifListMember(newRow, column.get());
 
     else if (colType.EqualsLiteral("mobile"))
-      mDatabase->AddCellularNumber(newRow, column.get());
+      aDatabase->AddCellularNumber(newRow, column.get());
 
     else if (colType.EqualsLiteral("mozilla_aimscreenname"))
-      mDatabase->AddAimScreenName(newRow, column.get());
+      aDatabase->AddAimScreenName(newRow, column.get());
 
     else if (colType.EqualsLiteral("mozillacustom1"))
-      mDatabase->AddCustom1(newRow, column.get());
+      aDatabase->AddCustom1(newRow, column.get());
         
     else if (colType.EqualsLiteral("mozillacustom2"))
-      mDatabase->AddCustom2(newRow, column.get());
+      aDatabase->AddCustom2(newRow, column.get());
         
     else if (colType.EqualsLiteral("mozillacustom3"))
-      mDatabase->AddCustom3(newRow, column.get());
+      aDatabase->AddCustom3(newRow, column.get());
         
     else if (colType.EqualsLiteral("mozillacustom4"))
-      mDatabase->AddCustom4(newRow, column.get());
+      aDatabase->AddCustom4(newRow, column.get());
 
     else if (colType.EqualsLiteral("mozillahomecountryname"))
-      mDatabase->AddHomeCountry(newRow, column.get());
+      aDatabase->AddHomeCountry(newRow, column.get());
 
     else if (colType.EqualsLiteral("mozillahomelocalityname"))
-      mDatabase->AddHomeCity(newRow, column.get());
+      aDatabase->AddHomeCity(newRow, column.get());
 
     else if (colType.EqualsLiteral("mozillahomestate"))
-      mDatabase->AddHomeState(newRow, column.get());
+      aDatabase->AddHomeState(newRow, column.get());
 
     else if (colType.EqualsLiteral("mozillahomestreet"))
-      mDatabase->AddHomeAddress(newRow, column.get());
+      aDatabase->AddHomeAddress(newRow, column.get());
 
     else if (colType.EqualsLiteral("mozillahomestreet2"))
-      mDatabase->AddHomeAddress2(newRow, column.get());
+      aDatabase->AddHomeAddress2(newRow, column.get());
 
     else if (colType.EqualsLiteral("mozillahomepostalcode"))
-      mDatabase->AddHomeZipCode(newRow, column.get());
+      aDatabase->AddHomeZipCode(newRow, column.get());
 
     else if (colType.EqualsLiteral("mozillahomeurl"))
-      mDatabase->AddWebPage2(newRow, column.get());
+      aDatabase->AddWebPage2(newRow, column.get());
 
     else if (colType.EqualsLiteral("mozillanickname"))
     {
       if (bIsList)
-        mDatabase->AddListNickName(newRow, column.get());
+        aDatabase->AddListNickName(newRow, column.get());
       else
-        mDatabase->AddNickName(newRow, column.get());
+        aDatabase->AddNickName(newRow, column.get());
     }
 
     else if (colType.EqualsLiteral("mozillasecondemail"))
-      mDatabase->Add2ndEmail(newRow, column.get());
+      aDatabase->Add2ndEmail(newRow, column.get());
 
     else if (colType.EqualsLiteral("mozillausehtmlmail"))
     {
       ToLowerCase(column);
       if (-1 != column.Find("true"))
-        mDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::html);
+        aDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::html);
       else if (-1 != column.Find("false"))
-        mDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::plaintext);
+        aDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::plaintext);
       else
-        mDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::unknown);
+        aDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::unknown);
     }
 
     else if (colType.EqualsLiteral("mozillaworkstreet2"))
-      mDatabase->AddWorkAddress2(newRow, column.get());
+      aDatabase->AddWorkAddress2(newRow, column.get());
 
     else if (colType.EqualsLiteral("mozillaworkurl"))
-      mDatabase->AddWebPage1(newRow, column.get());
+      aDatabase->AddWebPage1(newRow, column.get());
 
     break; // 'm'
 
   case 'n':
     if (colType.EqualsLiteral("notes"))
-      mDatabase->AddNotes(newRow, column.get());
+      aDatabase->AddNotes(newRow, column.get());
 
     else if (colType.EqualsLiteral("nscpaimscreenname") || 
              colType.EqualsLiteral("nsaimid"))
-      mDatabase->AddAimScreenName(newRow, column.get());
+      aDatabase->AddAimScreenName(newRow, column.get());
 
     break; // 'n'
 
   case 'o':
     if (colType.EqualsLiteral("objectclass"))
       break;
 
     else if (colType.EqualsLiteral("ou") || colType.EqualsLiteral("orgunit"))
-      mDatabase->AddDepartment(newRow, column.get());
+      aDatabase->AddDepartment(newRow, column.get());
 
     else if (colType.EqualsLiteral("o")) // organization
-      mDatabase->AddCompany(newRow, column.get());
+      aDatabase->AddCompany(newRow, column.get());
 
     break; // 'o'
 
   case 'p':
     if (colType.EqualsLiteral("postalcode"))
     {
       if (mStoreLocAsHome)
-        mDatabase->AddHomeZipCode(newRow, column.get());
+        aDatabase->AddHomeZipCode(newRow, column.get());
       else
-        mDatabase->AddWorkZipCode(newRow, column.get());
+        aDatabase->AddWorkZipCode(newRow, column.get());
     }
 
     else if (colType.EqualsLiteral("postofficebox"))
     {
       nsCAutoString workAddr1, workAddr2;
       SplitCRLFAddressField(column, workAddr1, workAddr2);
-      mDatabase->AddWorkAddress(newRow, workAddr1.get());
-      mDatabase->AddWorkAddress2(newRow, workAddr2.get());
+      aDatabase->AddWorkAddress(newRow, workAddr1.get());
+      aDatabase->AddWorkAddress2(newRow, workAddr2.get());
     }
     else if (colType.EqualsLiteral("pager") || colType.EqualsLiteral("pagerphone"))
-      mDatabase->AddPagerNumber(newRow, column.get());
+      aDatabase->AddPagerNumber(newRow, column.get());
 
     break; // 'p'
 
   case 'r':
     if (colType.EqualsLiteral("region"))
     {
       if (mStoreLocAsHome)
-        mDatabase->AddWorkState(newRow, column.get());
+        aDatabase->AddWorkState(newRow, column.get());
       else
-        mDatabase->AddWorkState(newRow, column.get());
+        aDatabase->AddWorkState(newRow, column.get());
     }
 
     break; // 'r'
 
   case 's':
     if (colType.EqualsLiteral("sn") || colType.EqualsLiteral("surname"))
-      mDatabase->AddLastName(newRow, column.get());
+      aDatabase->AddLastName(newRow, column.get());
 
     else if (colType.EqualsLiteral("street"))
-      mDatabase->AddWorkAddress(newRow, column.get());
+      aDatabase->AddWorkAddress(newRow, column.get());
 
     else if (colType.EqualsLiteral("streetaddress"))
     {
       nsCAutoString addr1, addr2;
       SplitCRLFAddressField(column, addr1, addr2);
       if (mStoreLocAsHome)
       {
-        mDatabase->AddHomeAddress(newRow, addr1.get());
-        mDatabase->AddHomeAddress2(newRow, addr2.get());
+        aDatabase->AddHomeAddress(newRow, addr1.get());
+        aDatabase->AddHomeAddress2(newRow, addr2.get());
       }
       else
       {
-        mDatabase->AddWorkAddress(newRow, addr1.get());
-        mDatabase->AddWorkAddress2(newRow, addr2.get());
+        aDatabase->AddWorkAddress(newRow, addr1.get());
+        aDatabase->AddWorkAddress2(newRow, addr2.get());
       }
     }
     else if (colType.EqualsLiteral("st"))
     {
     if (mStoreLocAsHome)
-      mDatabase->AddHomeState(newRow, column.get());
+      aDatabase->AddHomeState(newRow, column.get());
     else
-      mDatabase->AddWorkState(newRow, column.get());
+      aDatabase->AddWorkState(newRow, column.get());
     }
         
     break; // 's'
 
   case 't':
     if (colType.EqualsLiteral("title"))
-      mDatabase->AddJobTitle(newRow, column.get());
+      aDatabase->AddJobTitle(newRow, column.get());
 
     else if (colType.EqualsLiteral("telephonenumber") )
     {
-      mDatabase->AddWorkPhone(newRow, column.get());
+      aDatabase->AddWorkPhone(newRow, column.get());
     }
 
     break; // 't'
 
   case 'u':
 
     if (colType.EqualsLiteral("uniquemember") && bIsList)
-      mDatabase->AddLdifListMember(newRow, column.get());
+      aDatabase->AddLdifListMember(newRow, column.get());
 
     break; // 'u'
 
   case 'w':
     if (colType.EqualsLiteral("workurl"))
-      mDatabase->AddWebPage1(newRow, column.get());
+      aDatabase->AddWebPage1(newRow, column.get());
 
     break; // 'w'
 
   case 'x':
     if (colType.EqualsLiteral("xmozillanickname"))
     {
       if (bIsList)
-        mDatabase->AddListNickName(newRow, column.get());
+        aDatabase->AddListNickName(newRow, column.get());
       else
-        mDatabase->AddNickName(newRow, column.get());
+        aDatabase->AddNickName(newRow, column.get());
     }
 
     else if (colType.EqualsLiteral("xmozillausehtmlmail"))
     {
       ToLowerCase(column);
       if (-1 != column.Find("true"))
-        mDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::html);
+        aDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::html);
       else if (-1 != column.Find("false"))
-        mDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::plaintext);
+        aDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::plaintext);
       else
-        mDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::unknown);
+        aDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::unknown);
     }
 
     break; // 'x'
 
   case 'z':
     if (colType.EqualsLiteral("zip")) // alias for postalcode
     {
       if (mStoreLocAsHome)
-        mDatabase->AddHomeZipCode(newRow, column.get());
+        aDatabase->AddHomeZipCode(newRow, column.get());
       else
-        mDatabase->AddWorkZipCode(newRow, column.get());
+        aDatabase->AddWorkZipCode(newRow, column.get());
     }
 
     break; // 'z'
 
   default:
     break; // default
   }
 }
--- a/mailnews/addrbook/src/nsAbLDIFService.h
+++ b/mailnews/addrbook/src/nsAbLDIFService.h
@@ -50,21 +50,21 @@ public:
   NS_DECL_NSIABLDIFSERVICE
 
   nsAbLDIFService();
   virtual ~nsAbLDIFService();
 private:
   nsresult        str_parse_line(char *line, char **type, char **value, int *vlen) const;
   char *          str_getline(char **next) const;
   nsresult        GetLdifStringRecord(char* buf, PRInt32 len, PRInt32& stopPos);
-  void            AddLdifRowToDatabase(PRBool aIsList);
-  void            AddLdifColToDatabase(nsIMdbRow* newRow, char* typeSlot, char* valueSlot, PRBool bIsList);
+  void AddLdifRowToDatabase(nsIAddrDatabase *aDatabase, PRBool aIsList);
+  void AddLdifColToDatabase(nsIAddrDatabase *aDatabase, nsIMdbRow* newRow,
+                            char* typeSlot, char* valueSlot, PRBool bIsList);
   void            ClearLdifRecordBuffer();
   void            SplitCRLFAddressField(nsCString &inputAddress, nsCString &outputLine1, nsCString &outputLine2) const;
 
   PRBool          mStoreLocAsHome;
   nsCString       mLdifLine;
   PRInt32         mLFCount;
   PRInt32         mCRCount;
-  nsCOMPtr<nsIAddrDatabase> mDatabase;
 };
 
 #endif
--- a/mailnews/import/src/nsImportAddressBooks.cpp
+++ b/mailnews/import/src/nsImportAddressBooks.cpp
@@ -762,17 +762,17 @@ void AddressThreadData::DriverAbort()
     // FIXME: Do whatever is necessary to abort what has already been imported!
   }
   else
     abort = PR_TRUE;
   DriverDelete();
 }
 
 
-nsIAddrDatabase *GetAddressBookFromUri(const char *pUri)
+already_AddRefed<nsIAddrDatabase> GetAddressBookFromUri(const char *pUri)
 {
   nsIAddrDatabase *  pDatabase = nsnull;
   if (pUri) {
     nsresult rv;
     nsCOMPtr<nsIProxyObjectManager> proxyObjectManager =
       do_GetService(NS_XPCOMPROXY_CONTRACTID, &rv);
     if (NS_FAILED(rv))
       return nsnull;
@@ -800,17 +800,18 @@ nsIAddrDatabase *GetAddressBookFromUri(c
           directory->GetDatabase(&pDatabase);
       }
     }
   }
 
   return pDatabase;
 }
 
-nsIAddrDatabase *GetAddressBook( const PRUnichar *name, PRBool makeNew)
+already_AddRefed<nsIAddrDatabase> GetAddressBook(const PRUnichar *name,
+                                                 PRBool makeNew)
 {
   nsresult      rv = NS_OK;
 
   if (!makeNew) {
     // FIXME: How do I get the list of address books and look for a
     // specific name.  Major bogosity!
     // For now, assume we didn't find anything with that name
   }
@@ -826,22 +827,22 @@ nsIAddrDatabase *GetAddressBook( const P
     do_GetService(NS_XPCOMPROXY_CONTRACTID, &rv);
   if (NS_FAILED(rv))
     return nsnull;
 
   nsCOMPtr<nsIAbManager> abMan = do_GetService(NS_ABMANAGER_CONTRACTID, &rv);
   if (NS_FAILED(rv))
     return nsnull;
 
-  nsIAbManager *abManager = nsnull;
+  nsCOMPtr<nsIAbManager> abManager;
   rv = proxyObjectManager->GetProxyForObject(NS_PROXY_TO_MAIN_THREAD,
                                              NS_GET_IID(nsIAbManager),
                                              abMan,
                                              NS_PROXY_SYNC,
-                                             (void**)&abManager);
+                                             getter_AddRefs(abManager));
 
   if (NS_SUCCEEDED(rv))
     rv = abManager->GetUserProfileDirectory(getter_AddRefs(dbPath));
   if (NS_SUCCEEDED(rv)) {
     // Create a new address book file - we don't care what the file
     // name is, as long as it's unique
     rv = dbPath->Append(NS_LITERAL_STRING("impab.mab"));
         if (NS_SUCCEEDED(rv)) {
@@ -850,95 +851,91 @@ nsIAddrDatabase *GetAddressBook( const P
           if (NS_SUCCEEDED(rv)) {
             IMPORT_LOG0( "Getting the address database factory\n");
 
             nsCOMPtr<nsIAddrDatabase> addrDatabaseFactory =
               do_GetService(NS_ADDRDATABASE_CONTRACTID, &rv);
             if (NS_FAILED(rv))
               return nsnull;
 
-            nsIAddrDatabase *addrDBFactory = nsnull;
+            nsCOMPtr<nsIAddrDatabase> addrDBFactory;
             rv = proxyObjectManager->GetProxyForObject(NS_PROXY_TO_MAIN_THREAD,
                                                        NS_GET_IID(nsIAddrDatabase),
                                                        addrDatabaseFactory,
                                                        NS_PROXY_SYNC,
-                                                       (void**)&addrDBFactory);
+                                                       getter_AddRefs(addrDBFactory));
             if (NS_SUCCEEDED(rv) && addrDBFactory) {
-          IMPORT_LOG0( "Opening the new address book\n");
-        rv = addrDBFactory->Open( dbPath, PR_TRUE, PR_TRUE, &pDatabase);
+              IMPORT_LOG0( "Opening the new address book\n");
+              nsCOMPtr<nsIAddrDatabase> nonProxyDatabase;
+              rv = addrDBFactory->Open(dbPath, PR_TRUE, PR_TRUE,
+                                       getter_AddRefs(nonProxyDatabase));
+              if (nonProxyDatabase)
+                rv = proxyObjectManager->GetProxyForObject(NS_PROXY_TO_MAIN_THREAD,
+                                                           NS_GET_IID(nsIAddrDatabase),
+                                                           nonProxyDatabase,
+                                                           NS_PROXY_SYNC,
+                                                           (void**)&pDatabase);
+                
             }
           }
         }
   }
   if (NS_FAILED(rv)) {
     IMPORT_LOG0( "Failed to get the user profile directory from the address book session\n");
   }
 
 
   if (pDatabase) {
     // We made a database, add it to the UI?!?!?!?!?!?!
     // This is major bogosity again!  Why doesn't the address book
     // just handle this properly for me?  Uggggg...
 
-    nsCOMPtr<nsIRDFService> mainRdfService =
-      do_GetService(kRDFServiceCID, &rv);
-    if (NS_FAILED(rv))
-      return nsnull;
-
-    nsIRDFService *rdfService = nsnull;
+    nsCOMPtr<nsIAbDirectory> nonProxyParentDir;
+    abManager->GetDirectory(NS_LITERAL_CSTRING(kAllDirectoryRoot),
+                            getter_AddRefs(nonProxyParentDir));
+    nsCOMPtr<nsIAbDirectory> parentDir;
+    /*
+     * TODO
+     * This may not be required in the future since the
+     * primary listeners of the nsIAbDirectory will be
+     * RDF directory datasource which propagates events to
+     * RDF Observers. In the future the RDF directory datasource
+     * will proxy the observers because asynchronous directory
+     * implementations, such as LDAP, will assert results from
+     * a thread other than the UI thread.
+     *
+     */
     rv = proxyObjectManager->GetProxyForObject(NS_PROXY_TO_MAIN_THREAD,
-                                               NS_GET_IID(nsIRDFService),
-                                               mainRdfService,
-                                               NS_PROXY_SYNC,
-                                               (void**)&rdfService);
-    if (NS_SUCCEEDED(rv)) {
-      nsCOMPtr<nsIRDFResource>  parentResource;
-      rv = rdfService->GetResource(NS_LITERAL_CSTRING(kAllDirectoryRoot),
-                                   getter_AddRefs(parentResource));
-      nsCOMPtr<nsIAbDirectory> parentDir;
-      /*
-       * TODO
-       * This may not be required in the future since the
-       * primary listeners of the nsIAbDirectory will be
-       * RDF directory datasource which propagates events to
-       * RDF Observers. In the future the RDF directory datasource
-       * will proxy the observers because asynchronous directory
-       * implementations, such as LDAP, will assert results from
-       * a thread other than the UI thread.
-       *
-       */
-      rv = proxyObjectManager->GetProxyForObject(NS_PROXY_TO_MAIN_THREAD,
-                                                 NS_GET_IID(nsIAbDirectory),
-                                                 parentResource,
-                                                 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
-                                                 getter_AddRefs(parentDir));
-      if (parentDir)
+                                               NS_GET_IID(nsIAbDirectory),
+                                               nonProxyParentDir,
+                                               NS_PROXY_SYNC | NS_PROXY_ALWAYS,
+                                               getter_AddRefs(parentDir));
+    if (parentDir)
+    {
+      nsCAutoString URI("moz-abmdbdirectory://");
+      nsCAutoString leafName;
+      rv = dbPath->GetNativeLeafName(leafName);
+      if (NS_FAILED(rv))
+        IMPORT_LOG0( "*** Error: Unable to get name of database file\n");
+      else
       {
-        nsCAutoString URI("moz-abmdbdirectory://");
-        nsCAutoString leafName;
-        rv = dbPath->GetNativeLeafName(leafName);
-        if (NS_FAILED(rv)) {
-          IMPORT_LOG0( "*** Error: Unable to get name of database file\n");
-        }
-        else {
-          URI.Append(leafName);
-          rv = parentDir->CreateDirectoryByURI(nsDependentString(name), URI);
-          if (NS_FAILED(rv))
-            IMPORT_LOG0( "*** Error: Unable to create address book directory\n");
-        }
+        URI.Append(leafName);
+        rv = parentDir->CreateDirectoryByURI(nsDependentString(name), URI);
+        if (NS_FAILED(rv))
+          IMPORT_LOG0( "*** Error: Unable to create address book directory\n");
       }
+    }
 
-      if (NS_SUCCEEDED(rv))
-        IMPORT_LOG0( "Added new address book to the UI\n");
-      else
-        IMPORT_LOG0( "*** Error: An error occurred while adding the address book to the UI\n");
-    }
+    if (NS_SUCCEEDED(rv))
+      IMPORT_LOG0( "Added new address book to the UI\n");
+    else
+      IMPORT_LOG0( "*** Error: An error occurred while adding the address book to the UI\n");
   }
 
-  return( pDatabase);
+  return pDatabase;
 }
 
 void nsImportGenericAddressBooks::ReportError(const PRUnichar *pName,
                                               nsString *pStream,
                                               nsIStringBundle* aBundle)
 {
   if (!pStream)
     return;