Bug 1594896 - Remove xpidl [array] use in nsISMimeJSHelper. r=mkmelin
authorBen Campbell <benc@thunderbird.net>
Fri, 29 Nov 2019 16:10:37 +1300
changeset 28306 0211c8540f8473d9e538d27e39f4a922cbf033a3
parent 28305 55d957f1d6befeb94b0806a45340d96d25fb4eef
child 28307 247abc27cb5243b3cc136046593c2bb5a62dad64
push id16759
push usergeoff@darktrojan.net
push dateTue, 03 Dec 2019 22:20:21 +0000
treeherdercomm-central@247abc27cb52 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmkmelin
bugs1594896
Bug 1594896 - Remove xpidl [array] use in nsISMimeJSHelper. r=mkmelin
mail/extensions/smime/content/msgCompSMIMEOverlay.js
mailnews/extensions/smime/content/msgCompSecurityInfo.js
mailnews/extensions/smime/public/nsISMimeJSHelper.idl
mailnews/extensions/smime/src/nsSMimeJSHelper.cpp
suite/extensions/smime/content/msgCompSMIMEOverlay.js
--- a/mail/extensions/smime/content/msgCompSMIMEOverlay.js
+++ b/mail/extensions/smime/content/msgCompSMIMEOverlay.js
@@ -263,32 +263,31 @@ var SecurityController = {
 
       default:
         return false;
     }
   },
 };
 
 function onComposerSendMessage() {
-  let missingCount = {};
-  let emailAddresses = {};
+  let emailAddresses = [];
 
   try {
     if (!gMsgCompose.compFields.composeSecure.requireEncryptMessage) {
       return;
     }
 
     Cc["@mozilla.org/messenger-smime/smimejshelper;1"]
       .createInstance(Ci.nsISMimeJSHelper)
-      .getNoCertAddresses(gMsgCompose.compFields, missingCount, emailAddresses);
+      .getNoCertAddresses(gMsgCompose.compFields, emailAddresses);
   } catch (e) {
     return;
   }
 
-  if (missingCount.value > 0) {
+  if (emailAddresses.length > 0) {
     // The rules here: If the current identity has a directoryServer set, then
     // use that, otherwise, try the global preference instead.
 
     let autocompleteDirectory;
 
     // Does the current identity override the global preference?
     if (gCurrentIdentity.overrideGlobalPref) {
       autocompleteDirectory = gCurrentIdentity.directoryServer;
--- a/mailnews/extensions/smime/content/msgCompSecurityInfo.js
+++ b/mailnews/extensions/smime/content/msgCompSecurityInfo.js
@@ -44,19 +44,17 @@ function onLoad() {
   var canEncrypt = {};
 
   var allow_ldap_cert_fetching = params.smFields.requireEncryptMessage;
 
   while (true) {
     try {
       helper.getRecipientCertsInfo(
         params.compFields,
-        gCount,
         gEmailAddresses,
-        {}, // certStatusSummaries - provide no useful info anymore
         gCertIssuedInfos,
         gCertExpiresInfos,
         gCerts,
         canEncrypt
       );
     } catch (e) {
       dump(e);
       return;
--- a/mailnews/extensions/smime/public/nsISMimeJSHelper.idl
+++ b/mailnews/extensions/smime/public/nsISMimeJSHelper.idl
@@ -17,57 +17,47 @@ interface nsIX509Cert;
 interface nsISMimeJSHelper : nsISupports
 {
   /**
    * Obtains detailed information about the certificate availability
    * status of email recipients.
    *
    * @param compFields - Attributes of the composed message
    *
-   * @param count - The number of entries in returned arrays
-   *
    * @param emailAddresses - The list of all recipient email addresses
    *
-   * @param certVerification - The verification/validity status of recipient certs
-   *
    * @param certIssuedInfos - If a recipient cert was found, when has it been issued?
    *
    * @param certExpiredInfos - If a recipient cert was found, when will it expire?
    *
    * @param certs - The recipient certificates, which can contain null for not found
    *
    * @param canEncrypt - whether valid certificates have been found for all recipients
    *
    * @exception NS_ERROR_FAILURE - unexptected failure
    *
    * @exception NS_ERROR_OUT_OF_MEMORY - could not create the out list
    *
    * @exception NS_ERROR_INVALID_ARG
    */
   void getRecipientCertsInfo(in nsIMsgCompFields compFields,
-                             out unsigned long count,
-                             [array, size_is(count)] out wstring emailAddresses,
-                             [array, size_is(count)] out long certVerification,
-                             [array, size_is(count)] out wstring certIssuedInfos,
-                             [array, size_is(count)] out wstring certExpiresInfos,
-                             [array, size_is(count)] out nsIX509Cert certs,
+                             out Array<AString> emailAddresses,
+                             out Array<AString> certIssuedInfos,
+                             out Array<AString> certExpiresInfos,
+                             out Array<nsIX509Cert> certs,
                              out boolean canEncrypt);
 
   /**
    * Obtains a list of email addresses where valid email recipient certificates
    * are not yet available.
    *
    * @param compFields - Attributes of the composed message
    *
-   * @param count - The number of returned email addresses
-   *
-   * @param emailAddresses - The list of email addresses without valid certs
+   * @returns The list of email addresses without valid certs
    *
    * @exception NS_ERROR_FAILURE - unexptected failure
    *
    * @exception NS_ERROR_OUT_OF_MEMORY - could not create the out list
    *
    * @exception NS_ERROR_INVALID_ARG
    */
-  void getNoCertAddresses(in nsIMsgCompFields compFields,
-                          out unsigned long count,
-                          [array, size_is(count)] out wstring emailAddresses);
+  Array<AString> getNoCertAddresses(in nsIMsgCompFields compFields);
 };
--- a/mailnews/extensions/smime/src/nsSMimeJSHelper.cpp
+++ b/mailnews/extensions/smime/src/nsSMimeJSHelper.cpp
@@ -20,242 +20,112 @@ using namespace mozilla::mailnews;
 
 NS_IMPL_ISUPPORTS(nsSMimeJSHelper, nsISMimeJSHelper)
 
 nsSMimeJSHelper::nsSMimeJSHelper() {}
 
 nsSMimeJSHelper::~nsSMimeJSHelper() {}
 
 NS_IMETHODIMP nsSMimeJSHelper::GetRecipientCertsInfo(
-    nsIMsgCompFields *compFields, uint32_t *count, char16_t ***emailAddresses,
-    int32_t **certVerification, char16_t ***certIssuedInfos,
-    char16_t ***certExpiresInfos, nsIX509Cert ***certs, bool *canEncrypt) {
-  NS_ENSURE_ARG_POINTER(count);
-  *count = 0;
-
-  NS_ENSURE_ARG_POINTER(emailAddresses);
-  NS_ENSURE_ARG_POINTER(certVerification);
-  NS_ENSURE_ARG_POINTER(certIssuedInfos);
-  NS_ENSURE_ARG_POINTER(certExpiresInfos);
-  NS_ENSURE_ARG_POINTER(certs);
+    nsIMsgCompFields *compFields, nsTArray<nsString> &emailAddresses,
+    nsTArray<nsString> &certIssuedInfos, nsTArray<nsString> &certExpiresInfos,
+    nsTArray<RefPtr<nsIX509Cert>> &certs, bool *canEncrypt) {
   NS_ENSURE_ARG_POINTER(canEncrypt);
 
-  NS_ENSURE_ARG_POINTER(compFields);
-
   nsTArray<nsCString> mailboxes;
   nsresult rv = getMailboxList(compFields, mailboxes);
   NS_ENSURE_SUCCESS(rv, rv);
 
   uint32_t mailbox_count = mailboxes.Length();
 
+  emailAddresses.ClearAndRetainStorage();
+  certIssuedInfos.ClearAndRetainStorage();
+  certExpiresInfos.ClearAndRetainStorage();
+  certs.ClearAndRetainStorage();
+  emailAddresses.SetCapacity(mailbox_count);
+  certIssuedInfos.SetCapacity(mailbox_count);
+  certExpiresInfos.SetCapacity(mailbox_count);
+  certs.SetCapacity(mailbox_count);
+
   nsCOMPtr<nsIX509CertDB> certdb = do_GetService(NS_X509CERTDB_CONTRACTID);
 
-  *count = mailbox_count;
   *canEncrypt = false;
   rv = NS_OK;
-
-  if (mailbox_count) {
-    nsCOMPtr<nsIMsgComposeSecure> composeSecure =
-        do_CreateInstance(NS_MSGCOMPOSESECURE_CONTRACTID, &rv);
-    NS_ENSURE_SUCCESS(rv, rv);
+  bool found_blocker = false;
 
-    char16_t **outEA = static_cast<char16_t **>(
-        moz_xmalloc(mailbox_count * sizeof(char16_t *)));
-    int32_t *outCV =
-        static_cast<int32_t *>(moz_xmalloc(mailbox_count * sizeof(int32_t)));
-    char16_t **outCII = static_cast<char16_t **>(
-        moz_xmalloc(mailbox_count * sizeof(char16_t *)));
-    char16_t **outCEI = static_cast<char16_t **>(
-        moz_xmalloc(mailbox_count * sizeof(char16_t *)));
-    nsIX509Cert **outCerts = static_cast<nsIX509Cert **>(
-        moz_xmalloc(mailbox_count * sizeof(nsIX509Cert *)));
+  nsCOMPtr<nsIMsgComposeSecure> composeSecure =
+      do_CreateInstance(NS_MSGCOMPOSESECURE_CONTRACTID, &rv);
+  NS_ENSURE_SUCCESS(rv, rv);
 
-    if (!outEA || !outCV || !outCII || !outCEI || !outCerts) {
-      free(outEA);
-      free(outCV);
-      free(outCII);
-      free(outCEI);
-      free(outCerts);
-      rv = NS_ERROR_OUT_OF_MEMORY;
-    } else {
-      char16_t **iEA = outEA;
-      int32_t *iCV = outCV;
-      char16_t **iCII = outCII;
-      char16_t **iCEI = outCEI;
-      nsIX509Cert **iCert = outCerts;
-
-      bool found_blocker = false;
-      bool memory_failure = false;
+  for (uint32_t i = 0; i < mailbox_count; ++i) {
+    const nsCString &email = mailboxes[i];
+    nsCOMPtr<nsIX509Cert> cert;
+    nsString certIssuedInfo;
+    nsString certExpiresInfo;
 
-      for (uint32_t i = 0; i < mailbox_count;
-           ++i, ++iEA, ++iCV, ++iCII, ++iCEI, ++iCert) {
-        *iCert = nullptr;
-        *iCV = 0;
-        *iCII = nullptr;
-        *iCEI = nullptr;
-
-        if (memory_failure) {
-          *iEA = nullptr;
-          continue;
-        }
-
-        nsCString &email = mailboxes[i];
-        *iEA = ToNewUnicode(NS_ConvertUTF8toUTF16(email));
-        if (!*iEA) {
-          memory_failure = true;
-          continue;
-        }
+    nsCString email_lowercase;
+    ToLowerCase(email, email_lowercase);
 
-        nsCString email_lowercase;
-        ToLowerCase(email, email_lowercase);
-
-        nsCOMPtr<nsIX509Cert> cert;
-        if (NS_SUCCEEDED(composeSecure->FindCertByEmailAddress(
-                email_lowercase, false, getter_AddRefs(cert)))) {
-          cert.forget(iCert);
-
-          nsCOMPtr<nsIX509CertValidity> validity;
-          rv = (*iCert)->GetValidity(getter_AddRefs(validity));
-
-          if (NS_SUCCEEDED(rv)) {
-            nsString id, ed;
-
-            if (NS_SUCCEEDED(validity->GetNotBeforeLocalDay(id))) {
-              *iCII = ToNewUnicode(id);
-              if (!*iCII) {
-                memory_failure = true;
-                continue;
-              }
-            }
+    if (NS_SUCCEEDED(composeSecure->FindCertByEmailAddress(
+            email_lowercase, false, getter_AddRefs(cert)))) {
+      nsCOMPtr<nsIX509CertValidity> validity;
+      rv = cert->GetValidity(getter_AddRefs(validity));
+      if (NS_SUCCEEDED(rv)) {
+        validity->GetNotBeforeLocalDay(certIssuedInfo);
+        validity->GetNotAfterLocalDay(certExpiresInfo);
+      }
+    } else {
+      found_blocker = true;
+    }
+    emailAddresses.AppendElement(NS_ConvertUTF8toUTF16(email));
+    certIssuedInfos.AppendElement(certIssuedInfo);
+    certExpiresInfos.AppendElement(certExpiresInfo);
+    certs.AppendElement(cert);
+  }
+  if (mailbox_count > 0 && !found_blocker) {
+    *canEncrypt = true;
+  }
 
-            if (NS_SUCCEEDED(validity->GetNotAfterLocalDay(ed))) {
-              *iCEI = ToNewUnicode(ed);
-              if (!*iCEI) {
-                memory_failure = true;
-                continue;
-              }
-            }
-          }
-        } else {
-          found_blocker = true;
-        }
-      }
-
-      if (memory_failure) {
-        NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(mailbox_count, outEA);
-        NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(mailbox_count, outCII);
-        NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(mailbox_count, outCEI);
-        NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(mailbox_count, outCerts);
-        free(outCV);
-        rv = NS_ERROR_OUT_OF_MEMORY;
-      } else {
-        if (mailbox_count > 0 && !found_blocker) {
-          *canEncrypt = true;
-        }
-
-        *emailAddresses = outEA;
-        *certVerification = outCV;
-        *certIssuedInfos = outCII;
-        *certExpiresInfos = outCEI;
-        *certs = outCerts;
-      }
-    }
-  }
-  return rv;
+  return NS_OK;
 }
 
-NS_IMETHODIMP nsSMimeJSHelper::GetNoCertAddresses(nsIMsgCompFields *compFields,
-                                                  uint32_t *count,
-                                                  char16_t ***emailAddresses) {
-  NS_ENSURE_ARG_POINTER(count);
-  *count = 0;
-
-  NS_ENSURE_ARG_POINTER(emailAddresses);
-
+NS_IMETHODIMP nsSMimeJSHelper::GetNoCertAddresses(
+    nsIMsgCompFields *compFields, nsTArray<nsString> &emailAddresses) {
   NS_ENSURE_ARG_POINTER(compFields);
+  emailAddresses.ClearAndRetainStorage();
 
   nsTArray<nsCString> mailboxes;
   nsresult rv = getMailboxList(compFields, mailboxes);
   NS_ENSURE_SUCCESS(rv, rv);
 
   uint32_t mailbox_count = mailboxes.Length();
 
   if (!mailbox_count) {
-    *count = 0;
-    *emailAddresses = nullptr;
     return NS_OK;
   }
 
+  emailAddresses.SetCapacity(mailbox_count);
+
   nsCOMPtr<nsIMsgComposeSecure> composeSecure =
       do_CreateInstance(NS_MSGCOMPOSESECURE_CONTRACTID, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  uint32_t missing_count = 0;
-  bool *haveCert = new bool[mailbox_count];
-  if (!haveCert) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
-  rv = NS_OK;
+  for (uint32_t i = 0; i < mailbox_count; ++i) {
+    nsCString email_lowercase;
+    ToLowerCase(mailboxes[i], email_lowercase);
 
-  if (mailbox_count) {
-    for (uint32_t i = 0; i < mailbox_count; ++i) {
-      haveCert[i] = false;
-
-      nsCString email_lowercase;
-      ToLowerCase(mailboxes[i], email_lowercase);
-
-      nsCOMPtr<nsIX509Cert> cert;
-      if (NS_SUCCEEDED(composeSecure->FindCertByEmailAddress(
-              email_lowercase, true, getter_AddRefs(cert))))
-        haveCert[i] = true;
-
-      if (!haveCert[i]) ++missing_count;
+    nsCOMPtr<nsIX509Cert> cert;
+    if (NS_FAILED(composeSecure->FindCertByEmailAddress(
+            email_lowercase, true, getter_AddRefs(cert)))) {
+      // No cert found for this address.
+      emailAddresses.AppendElement(NS_ConvertUTF8toUTF16(mailboxes[i]));
     }
   }
 
-  *count = missing_count;
-
-  if (missing_count) {
-    char16_t **outEA = static_cast<char16_t **>(
-        moz_xmalloc(missing_count * sizeof(char16_t *)));
-    if (!outEA) {
-      rv = NS_ERROR_OUT_OF_MEMORY;
-    } else {
-      char16_t **iEA = outEA;
-
-      bool memory_failure = false;
-
-      for (uint32_t i = 0; i < mailbox_count; ++i) {
-        if (!haveCert[i]) {
-          if (memory_failure) {
-            *iEA = nullptr;
-          } else {
-            *iEA = ToNewUnicode(NS_ConvertUTF8toUTF16(mailboxes[i]));
-            if (!*iEA) {
-              memory_failure = true;
-            }
-          }
-          ++iEA;
-        }
-      }
-
-      if (memory_failure) {
-        NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(missing_count, outEA);
-        rv = NS_ERROR_OUT_OF_MEMORY;
-      } else {
-        *emailAddresses = outEA;
-      }
-    }
-  } else {
-    *emailAddresses = nullptr;
-  }
-
-  delete[] haveCert;
-  return rv;
+  return NS_OK;
 }
 
 nsresult nsSMimeJSHelper::getMailboxList(nsIMsgCompFields *compFields,
                                          nsTArray<nsCString> &mailboxes) {
   if (!compFields) return NS_ERROR_INVALID_ARG;
 
   nsresult res;
   nsString to, cc, bcc, ng;
--- a/suite/extensions/smime/content/msgCompSMIMEOverlay.js
+++ b/suite/extensions/smime/content/msgCompSMIMEOverlay.js
@@ -254,36 +254,34 @@ var SecurityController =
       default:
         return false;
     }
   }
 };
 
 function onComposerSendMessage()
 {
-  let missingCount = new Object();
   let emailAddresses = new Object();
 
   try
   {
     if (!gMsgCompose.compFields.composeSecure.requireEncryptMessage)
       return;
 
     Cc["@mozilla.org/messenger-smime/smimejshelper;1"]
       .createInstance(Ci.nsISMimeJSHelper)
       .getNoCertAddresses(gMsgCompose.compFields,
-                                  missingCount,
                                   emailAddresses);
   }
   catch (e)
   {
     return;
   }
 
-  if (missingCount.value > 0)
+  if (emailAddresses.length > 0)
   {
     // The rules here: If the current identity has a directoryServer set, then
     // use that, otherwise, try the global preference instead.
 
     let autocompleteDirectory;
 
     // Does the current identity override the global preference?
     if (gCurrentIdentity.overrideGlobalPref)