From 781a1e03d547dd830144c9340a1e53552fcf6510 Mon Sep 17 00:00:00 2001 From: Geoff Lankow Date: Wed, 12 Feb 2020 23:16:44 +0000 Subject: [PATCH] Bug 1614265 - Move escapedVCardToAbCard from nsIAbManager to nsIMsgVCardService. r=mkmelin Differential Revision: https://phabricator.services.mozilla.com/D62393 --HG-- extra : moz-landing-system : lando --- mail/components/MessengerContentHandler.jsm | 4 +- mail/components/addrbook/content/abCard.js | 6 +- mail/test/browser/message-window/browser.ini | 1 - .../addrbook/jsaddrbook/AddrBookManager.jsm | 3 - mailnews/addrbook/public/nsIAbManager.idl | 9 -- .../addrbook/public/nsIMsgVCardService.idl | 13 +- mailnews/addrbook/src/nsAbContentHandler.cpp | 15 ++- mailnews/addrbook/src/nsMsgVCardService.cpp | 127 ++++++++++++++++++ mailnews/addrbook/test/unit/test_bug387403.js | 8 +- mailnews/addrbook/test/unit/xpcshell.ini | 1 - mailnews/import/test/unit/xpcshell.ini | 2 - mailnews/import/vcard/src/nsVCardAddress.cpp | 9 +- .../addrbook/content/abCardOverlay.js | 4 +- 13 files changed, 167 insertions(+), 35 deletions(-) diff --git a/mail/components/MessengerContentHandler.jsm b/mail/components/MessengerContentHandler.jsm index 1e43da1913..778d194462 100644 --- a/mail/components/MessengerContentHandler.jsm +++ b/mail/components/MessengerContentHandler.jsm @@ -417,7 +417,9 @@ MailDefaultHandler.prototype = { inputStream, inputStream.available() ); - let card = MailServices.ab.escapedVCardToAbCard(data); + let card = Cc["@mozilla.org/addressbook/msgvcardservice;1"] + .getService(Ci.nsIMsgVCardService) + .escapedVCardToAbCard(data); Services.ww.openWindow( null, "chrome://messenger/content/addressbook/abNewCardDialog.xhtml", diff --git a/mail/components/addrbook/content/abCard.js b/mail/components/addrbook/content/abCard.js index 432d8f0683..f09ee2195b 100644 --- a/mail/components/addrbook/content/abCard.js +++ b/mail/components/addrbook/content/abCard.js @@ -157,9 +157,9 @@ function OnLoadNewCard() { if ("escapedVCardStr" in window.arguments[0]) { // hide non vcard values HideNonVcardFields(); - gEditCard.card = MailServices.ab.escapedVCardToAbCard( - window.arguments[0].escapedVCardStr - ); + gEditCard.card = Cc["@mozilla.org/addressbook/msgvcardservice;1"] + .getService(Ci.nsIMsgVCardService) + .escapedVCardToAbCard(window.arguments[0].escapedVCardStr); } if ("titleProperty" in window.arguments[0]) { diff --git a/mail/test/browser/message-window/browser.ini b/mail/test/browser/message-window/browser.ini index 63214dd0e3..eb83578e74 100644 --- a/mail/test/browser/message-window/browser.ini +++ b/mail/test/browser/message-window/browser.ini @@ -18,5 +18,4 @@ skip-if = os == "linux" || os = "mac" [browser_emlSubject.js] [browser_messageSidebar.js] [browser_vcardActions.js] -fail-if = true [browser_viewPlaintext.js] diff --git a/mailnews/addrbook/jsaddrbook/AddrBookManager.jsm b/mailnews/addrbook/jsaddrbook/AddrBookManager.jsm index d713ccca93..d747c0c14c 100644 --- a/mailnews/addrbook/jsaddrbook/AddrBookManager.jsm +++ b/mailnews/addrbook/jsaddrbook/AddrBookManager.jsm @@ -299,9 +299,6 @@ AddrBookManager.prototype = { } return false; }, - escapedVCardToAbCard(escapedVCardStr) { - throw Cr.NS_ERROR_NOT_IMPLEMENTED; - }, generateUUID(directoryId, localId) { return `${directoryId}#${localId}`; }, diff --git a/mailnews/addrbook/public/nsIAbManager.idl b/mailnews/addrbook/public/nsIAbManager.idl index 39ff513e3f..59cd695afb 100644 --- a/mailnews/addrbook/public/nsIAbManager.idl +++ b/mailnews/addrbook/public/nsIAbManager.idl @@ -149,15 +149,6 @@ interface nsIAbManager : nsISupports */ boolean mailListNameExists(in wstring name); - /** - * Translates an escaped vcard string into a nsIAbCard. - * - * @param escapedVCardStr The string containing the vcard. - * - * @return A card containing the translated vcard data. - */ - nsIAbCard escapedVCardToAbCard(in string escapedVCardStr); - /** * Generates a UUID from a (directory ID, local ID) tuple. * diff --git a/mailnews/addrbook/public/nsIMsgVCardService.idl b/mailnews/addrbook/public/nsIMsgVCardService.idl index e3e3411cab..c9721de584 100644 --- a/mailnews/addrbook/public/nsIMsgVCardService.idl +++ b/mailnews/addrbook/public/nsIMsgVCardService.idl @@ -5,6 +5,8 @@ #include "nsISupports.idl" +interface nsIAbCard; + %{C++ #include "nsVCardObj.h" %} @@ -13,7 +15,7 @@ [ptr] native VObjectIterator_ptr(VObjectIterator); [ptr] native const_char_ptr(const char); -[uuid(8b6ae917-676d-4f1f-bbad-2ecc9be0d9b1)] +[scriptable, builtinclass, uuid(8b6ae917-676d-4f1f-bbad-2ecc9be0d9b1)] interface nsIMsgVCardService : nsISupports { [noscript, notxpcom] void cleanVObject(in VObject_ptr o); [noscript, notxpcom] VObject_ptr nextVObjectInList(in VObject_ptr o); @@ -26,4 +28,13 @@ interface nsIMsgVCardService : nsISupports { [noscript, notxpcom] long moreIteration(in VObjectIterator_ptr i); [noscript, notxpcom] const_char_ptr vObjectName(in VObject_ptr o); [noscript, notxpcom] charPtr vObjectAnyValue(in VObject_ptr o); + + /** + * Translates an escaped vcard string into a nsIAbCard. + * + * @param escapedVCardStr The string containing the vcard. + * + * @return A card containing the translated vcard data. + */ + nsIAbCard escapedVCardToAbCard(in string escapedVCardStr); }; diff --git a/mailnews/addrbook/src/nsAbContentHandler.cpp b/mailnews/addrbook/src/nsAbContentHandler.cpp index 10c3c314ea..cbc1329984 100644 --- a/mailnews/addrbook/src/nsAbContentHandler.cpp +++ b/mailnews/addrbook/src/nsAbContentHandler.cpp @@ -19,9 +19,9 @@ #include "nsMsgUtils.h" #include "nsIMsgVCardService.h" #include "nsIAbCard.h" -#include "nsIAbManager.h" #include "nsVCard.h" #include "nsIChannel.h" +#include "nsIMsgVCardService.h" // // nsAbContentHandler // @@ -70,12 +70,13 @@ nsAbContentHandler::HandleContent(const char *aContentType, nsCOMPtr parentWindow = nsPIDOMWindowOuter::From(domWindow); - nsCOMPtr ab = do_GetService(NS_ABMANAGER_CONTRACTID, &rv); + nsCOMPtr vCardService = + do_GetService(NS_MSGVCARDSERVICE_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr cardFromVCard; - rv = ab->EscapedVCardToAbCard(unescapedData.get(), - getter_AddRefs(cardFromVCard)); + rv = vCardService->EscapedVCardToAbCard(unescapedData.get(), + getter_AddRefs(cardFromVCard)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr ifptr = @@ -156,11 +157,13 @@ nsAbContentHandler::OnStreamComplete(nsIStreamLoader *aLoader, vCard.Adopt( vCardService->WriteMemoryVObjects(0, &len, vObj.get(), false)); - nsCOMPtr ab = do_GetService(NS_ABMANAGER_CONTRACTID, &rv); + nsCOMPtr vCardService = + do_GetService(NS_MSGVCARDSERVICE_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr cardFromVCard; - rv = ab->EscapedVCardToAbCard(vCard.get(), getter_AddRefs(cardFromVCard)); + rv = vCardService->EscapedVCardToAbCard(vCard.get(), + getter_AddRefs(cardFromVCard)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr domWindow = do_GetInterface(aContext); diff --git a/mailnews/addrbook/src/nsMsgVCardService.cpp b/mailnews/addrbook/src/nsMsgVCardService.cpp index ddf59a2f08..647514895c 100644 --- a/mailnews/addrbook/src/nsMsgVCardService.cpp +++ b/mailnews/addrbook/src/nsMsgVCardService.cpp @@ -3,6 +3,10 @@ * 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/. */ +#include "nsIAbCard.h" +#include "nsAbBaseCID.h" +#include "nsString.h" +#include "nsMsgUtils.h" #include "nsMsgVCardService.h" #include "nsVCard.h" #include "prmem.h" @@ -64,3 +68,126 @@ NS_IMETHODIMP_(char *) nsMsgVCardService::VObjectAnyValue(VObject *o) { if (retval) PL_strcpy(retval, (char *)vObjectAnyValue(o)); return retval; } + +char *getCString(VObject *vObj) { + if (VALUE_TYPE(vObj) == VCVT_USTRINGZ) + return fakeCString(vObjectUStringZValue(vObj)); + if (VALUE_TYPE(vObj) == VCVT_STRINGZ) + return PL_strdup(vObjectStringZValue(vObj)); + return NULL; +} + +static void convertNameValue(VObject *vObj, nsIAbCard *aCard) { + const char *cardPropName = NULL; + + // if the vCard property is not a root property then we need to determine its + // exact property. a good example of this is VCTelephoneProp, this prop has + // four objects underneath it: fax, work and home and cellular. + if (PL_strcasecmp(VCCityProp, vObjectName(vObj)) == 0) + cardPropName = kWorkCityProperty; + else if (PL_strcasecmp(VCTelephoneProp, vObjectName(vObj)) == 0) { + if (isAPropertyOf(vObj, VCFaxProp)) + cardPropName = kFaxProperty; + else if (isAPropertyOf(vObj, VCWorkProp)) + cardPropName = kWorkPhoneProperty; + else if (isAPropertyOf(vObj, VCHomeProp)) + cardPropName = kHomePhoneProperty; + else if (isAPropertyOf(vObj, VCCellularProp)) + cardPropName = kCellularProperty; + else if (isAPropertyOf(vObj, VCPagerProp)) + cardPropName = kPagerProperty; + else + return; + } else if (PL_strcasecmp(VCEmailAddressProp, vObjectName(vObj)) == 0) + cardPropName = kPriEmailProperty; + else if (PL_strcasecmp(VCFamilyNameProp, vObjectName(vObj)) == 0) + cardPropName = kLastNameProperty; + else if (PL_strcasecmp(VCFullNameProp, vObjectName(vObj)) == 0) + cardPropName = kDisplayNameProperty; + else if (PL_strcasecmp(VCGivenNameProp, vObjectName(vObj)) == 0) + cardPropName = kFirstNameProperty; + else if (PL_strcasecmp(VCOrgNameProp, vObjectName(vObj)) == 0) + cardPropName = kCompanyProperty; + else if (PL_strcasecmp(VCOrgUnitProp, vObjectName(vObj)) == 0) + cardPropName = kDepartmentProperty; + else if (PL_strcasecmp(VCPostalCodeProp, vObjectName(vObj)) == 0) + cardPropName = kWorkZipCodeProperty; + else if (PL_strcasecmp(VCRegionProp, vObjectName(vObj)) == 0) + cardPropName = kWorkStateProperty; + else if (PL_strcasecmp(VCStreetAddressProp, vObjectName(vObj)) == 0) + cardPropName = kWorkAddressProperty; + else if (PL_strcasecmp(VCPostalBoxProp, vObjectName(vObj)) == 0) + cardPropName = kWorkAddress2Property; + else if (PL_strcasecmp(VCCountryNameProp, vObjectName(vObj)) == 0) + cardPropName = kWorkCountryProperty; + else if (PL_strcasecmp(VCTitleProp, vObjectName(vObj)) == 0) + cardPropName = kJobTitleProperty; + else if (PL_strcasecmp(VCUseHTML, vObjectName(vObj)) == 0) + cardPropName = kPreferMailFormatProperty; + else if (PL_strcasecmp(VCNoteProp, vObjectName(vObj)) == 0) + cardPropName = kNotesProperty; + else if (PL_strcasecmp(VCURLProp, vObjectName(vObj)) == 0) + cardPropName = kWorkWebPageProperty; + else + return; + + if (!VALUE_TYPE(vObj)) return; + + char *cardPropValue = getCString(vObj); + if (PL_strcmp(cardPropName, kPreferMailFormatProperty)) { + aCard->SetPropertyAsAUTF8String(cardPropName, + nsDependentCString(cardPropValue)); + } else { + if (!PL_strcmp(cardPropValue, "TRUE")) + aCard->SetPropertyAsUint32(cardPropName, nsIAbPreferMailFormat::html); + else if (!PL_strcmp(cardPropValue, "FALSE")) + aCard->SetPropertyAsUint32(cardPropName, + nsIAbPreferMailFormat::plaintext); + else + aCard->SetPropertyAsUint32(cardPropName, nsIAbPreferMailFormat::unknown); + } + PR_FREEIF(cardPropValue); + return; +} + +static void convertFromVObject(VObject *vObj, nsIAbCard *aCard) { + if (vObj) { + VObjectIterator t; + + convertNameValue(vObj, aCard); + + initPropIterator(&t, vObj); + while (moreIteration(&t)) { + VObject *nextObject = nextVObject(&t); + convertFromVObject(nextObject, aCard); + } + } + return; +} + +NS_IMETHODIMP nsMsgVCardService::EscapedVCardToAbCard( + const char *aEscapedVCardStr, nsIAbCard **aCard) { + NS_ENSURE_ARG_POINTER(aEscapedVCardStr); + NS_ENSURE_ARG_POINTER(aCard); + + nsCOMPtr cardFromVCard = + do_CreateInstance(NS_ABCARDPROPERTY_CONTRACTID); + if (!cardFromVCard) return NS_ERROR_FAILURE; + + // aEscapedVCardStr will be "" the first time, before you have a vCard + if (*aEscapedVCardStr != '\0') { + nsCString unescapedData; + MsgUnescapeString(nsDependentCString(aEscapedVCardStr), 0, unescapedData); + + VObject *vObj = parse_MIME(unescapedData.get(), unescapedData.Length()); + if (vObj) { + convertFromVObject(vObj, cardFromVCard); + + cleanVObject(vObj); + } else + NS_WARNING("Parse of vCard failed"); + } + + cardFromVCard.forget(aCard); + return NS_OK; +} diff --git a/mailnews/addrbook/test/unit/test_bug387403.js b/mailnews/addrbook/test/unit/test_bug387403.js index e7cb63abd2..4cadc4e3af 100644 --- a/mailnews/addrbook/test/unit/test_bug387403.js +++ b/mailnews/addrbook/test/unit/test_bug387403.js @@ -6,7 +6,9 @@ function run_test() { // Before bug 387403 this would hang, eating up all the memory until it // crashed. - MailServices.ab.escapedVCardToAbCard( - "begin:vcard\nfn;quoted-printable:Xxxx=C5=82xx Xxx\nn;quoted-printable:Xxx;Xxxx=C5=82xx \nadr;quoted-printable;quoted-printable;dom:;;xx. Xxxxxxxxxxxx X;Xxxxxx=C3=3" - ); + Cc["@mozilla.org/addressbook/msgvcardservice;1"] + .getService(Ci.nsIMsgVCardService) + .escapedVCardToAbCard( + "begin:vcard\nfn;quoted-printable:Xxxx=C5=82xx Xxx\nn;quoted-printable:Xxx;Xxxx=C5=82xx \nadr;quoted-printable;quoted-printable;dom:;;xx. Xxxxxxxxxxxx X;Xxxxxx=C3=3" + ); } diff --git a/mailnews/addrbook/test/unit/xpcshell.ini b/mailnews/addrbook/test/unit/xpcshell.ini index 47114df856..5bf3b8ecc0 100644 --- a/mailnews/addrbook/test/unit/xpcshell.ini +++ b/mailnews/addrbook/test/unit/xpcshell.ini @@ -5,7 +5,6 @@ support-files = data/* [test_basic_nsIAbCard.js] [test_basic_nsIAbDirectory.js] [test_bug387403.js] -fail-if = true [test_bug448165.js] [test_bug534822.js] fail-if = true diff --git a/mailnews/import/test/unit/xpcshell.ini b/mailnews/import/test/unit/xpcshell.ini index 5d33ad0afc..d0823a2b46 100644 --- a/mailnews/import/test/unit/xpcshell.ini +++ b/mailnews/import/test/unit/xpcshell.ini @@ -7,7 +7,6 @@ support-files = resources/* [test_bug_437556.js] [test_csv_GetSample.js] [test_becky_addressbook.js] -fail-if = true run-if = os == 'win' [test_becky_filters.js] run-if = os == 'win' @@ -19,6 +18,5 @@ run-if = os == 'win' [test_shiftjis_csv.js] [test_utf16_csv.js] [test_vcard_import.js] -fail-if = true [test_winmail.js] run-if = os == 'win' diff --git a/mailnews/import/vcard/src/nsVCardAddress.cpp b/mailnews/import/vcard/src/nsVCardAddress.cpp index b21f7a0363..b3ed9bf0f0 100644 --- a/mailnews/import/vcard/src/nsVCardAddress.cpp +++ b/mailnews/import/vcard/src/nsVCardAddress.cpp @@ -8,11 +8,11 @@ #include "nsVCardAddress.h" #include "nsIAbCard.h" -#include "nsIAbManager.h" #include "nsIAbDirectory.h" #include "nsIFile.h" #include "nsIInputStream.h" #include "nsILineInputStream.h" +#include "nsIMsgVCardService.h" #include "plstr.h" #include "msgCore.h" @@ -51,7 +51,8 @@ nsresult nsVCardAddress::ImportAddresses(bool *pAbort, const char16_t *pName, nsCOMPtr lineStream(do_QueryInterface(inputStream, &rv)); NS_ENSURE_SUCCESS(rv, rv); - nsCOMPtr ab = do_GetService(NS_ABMANAGER_CONTRACTID, &rv); + nsCOMPtr vCardService = + do_GetService(NS_MSGVCARDSERVICE_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); bool more = true; @@ -61,8 +62,8 @@ nsresult nsVCardAddress::ImportAddresses(bool *pAbort, const char16_t *pName, if (NS_SUCCEEDED(rv) && !record.IsEmpty()) { // Parse the vCard and build an nsIAbCard from it nsCOMPtr cardFromVCard; - rv = - ab->EscapedVCardToAbCard(record.get(), getter_AddRefs(cardFromVCard)); + rv = vCardService->EscapedVCardToAbCard(record.get(), + getter_AddRefs(cardFromVCard)); NS_ENSURE_SUCCESS(rv, rv); nsIAbCard *outCard; diff --git a/suite/mailnews/components/addrbook/content/abCardOverlay.js b/suite/mailnews/components/addrbook/content/abCardOverlay.js index d964e5ed3e..58eb3e0838 100644 --- a/suite/mailnews/components/addrbook/content/abCardOverlay.js +++ b/suite/mailnews/components/addrbook/content/abCardOverlay.js @@ -116,7 +116,9 @@ function OnLoadNewCard() if ("escapedVCardStr" in window.arguments[0]) { // hide non vcard values HideNonVcardFields(); - gEditCard.card = MailServices.ab.escapedVCardToAbCard(window.arguments[0].escapedVCardStr); + gEditCard.card = Cc["@mozilla.org/addressbook/msgvcardservice;1"] + .getService(Ci.nsIMsgVCardService) + .escapedVCardToAbCard(window.arguments[0].escapedVCardStr); } if ("titleProperty" in window.arguments[0])