Bug 1612241 - Replace idl nsIArray usage with Array<T> in nsIAbBooleanExpression.idl. r=benc
authorGeoff Lankow <geoff@darktrojan.net>
Mon, 11 May 2020 22:30:38 +1200
changeset 39150 b49e6cda4e016e78743e2a50ec5293a60433484e
parent 39149 c2aeb710f2113c569ad196aae868330a00198b7b
child 39151 43d2da70f9d379bed1bb5f8dd2ebd0b16715b48d
push id402
push userclokep@gmail.com
push dateMon, 29 Jun 2020 20:48:04 +0000
reviewersbenc
bugs1612241
Bug 1612241 - Replace idl nsIArray usage with Array<T> in nsIAbBooleanExpression.idl. r=benc
mailnews/addrbook/public/nsIAbBooleanExpression.idl
mailnews/addrbook/src/nsAbBoolExprToLDAPFilter.cpp
mailnews/addrbook/src/nsAbBoolExprToLDAPFilter.h
mailnews/addrbook/src/nsAbBooleanExpression.cpp
mailnews/addrbook/src/nsAbBooleanExpression.h
mailnews/addrbook/src/nsAbDirectoryQuery.cpp
mailnews/addrbook/src/nsAbDirectoryQuery.h
mailnews/addrbook/src/nsAbOutlookDirectory.cpp
mailnews/addrbook/src/nsAbQueryStringToExpression.cpp
--- a/mailnews/addrbook/public/nsIAbBooleanExpression.idl
+++ b/mailnews/addrbook/public/nsIAbBooleanExpression.idl
@@ -1,16 +1,15 @@
 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsISupports.idl"
 
-interface nsIArray;
 typedef long nsAbBooleanConditionType;
 
 /**
  * Condition types
  *
  * Constants defining the types of condition
  * to obtain a boolean result of TRUE or FALSE
  *
@@ -112,11 +111,11 @@ interface nsIAbBooleanExpression: nsISup
     attribute nsAbBooleanOperationType operation;
 
     /**
      * List of peer expressions
      *
      * e1 [op] e2 [op] .... en
      *
      */
-    attribute nsIArray expressions;
+    attribute Array<nsISupports> expressions;
 };
 
--- a/mailnews/addrbook/src/nsAbBoolExprToLDAPFilter.cpp
+++ b/mailnews/addrbook/src/nsAbBoolExprToLDAPFilter.cpp
@@ -1,18 +1,17 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsIAbLDAPAttributeMap.h"
 #include "nsAbBoolExprToLDAPFilter.h"
 #include "nsString.h"
-#include "nsIArray.h"
-#include "nsArrayUtils.h"
+#include "nsQueryObject.h"
 
 const int nsAbBoolExprToLDAPFilter::TRANSLATE_CARD_PROPERTY = 1 << 0;
 const int nsAbBoolExprToLDAPFilter::ALLOW_NON_CONVERTABLE_CARD_PROPERTY = 1
                                                                           << 1;
 
 nsresult nsAbBoolExprToLDAPFilter::Convert(nsIAbLDAPAttributeMap* map,
                                            nsIAbBooleanExpression* expression,
                                            nsCString& filter, int flags) {
@@ -22,22 +21,21 @@ nsresult nsAbBoolExprToLDAPFilter::Conve
 
   filter = f;
   return rv;
 }
 
 nsresult nsAbBoolExprToLDAPFilter::FilterExpression(
     nsIAbLDAPAttributeMap* map, nsIAbBooleanExpression* expression,
     nsCString& filter, int flags) {
-  nsCOMPtr<nsIArray> childExpressions;
-  nsresult rv = expression->GetExpressions(getter_AddRefs(childExpressions));
+  nsTArray<RefPtr<nsISupports>> childExpressions;
+  nsresult rv = expression->GetExpressions(childExpressions);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  uint32_t count;
-  rv = childExpressions->GetLength(&count);
+  uint32_t count = childExpressions.Length();
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (count == 0) return NS_OK;
 
   nsAbBooleanOperationType operation;
   rv = expression->GetOperation(&operation);
   NS_ENSURE_SUCCESS(rv, rv);
 
@@ -48,18 +46,18 @@ nsresult nsAbBoolExprToLDAPFilter::Filte
    * ldap attributes using the card:nsIAbCard interface.
    *
    * So we need to check that we are not creating a condition
    * filter against this expression otherwise we will end up with an invalid
    * filter equal to "(|)".
    */
 
   if (count == 1) {
-    nsCOMPtr<nsIAbBooleanConditionString> childCondition(
-        do_QueryElementAt(childExpressions, 1, &rv));
+    nsCOMPtr<nsIAbBooleanConditionString> childCondition =
+        do_QueryInterface(childExpressions[0], &rv);
     if (NS_SUCCEEDED(rv)) {
       nsCString name;
       rv = childCondition->GetName(getter_Copies(name));
       NS_ENSURE_SUCCESS(rv, rv);
 
       if (name.EqualsLiteral("card:nsIAbCard")) return NS_OK;
     }
   }
@@ -82,35 +80,32 @@ nsresult nsAbBoolExprToLDAPFilter::Filte
     default:
       break;
   }
   filter.Append(')');
 
   return rv;
 }
 
-nsresult nsAbBoolExprToLDAPFilter::FilterExpressions(nsIAbLDAPAttributeMap* map,
-                                                     nsIArray* expressions,
-                                                     nsCString& filter,
-                                                     int flags) {
-  uint32_t count;
-  nsresult rv = expressions->GetLength(&count);
-  NS_ENSURE_SUCCESS(rv, rv);
+nsresult nsAbBoolExprToLDAPFilter::FilterExpressions(
+    nsIAbLDAPAttributeMap* map, nsTArray<RefPtr<nsISupports>>& expressions,
+    nsCString& filter, int flags) {
+  nsresult rv;
 
   nsCOMPtr<nsIAbBooleanConditionString> childCondition;
   nsCOMPtr<nsIAbBooleanExpression> childExpression;
-  for (uint32_t i = 0; i < count; i++) {
-    childCondition = do_QueryElementAt(expressions, i, &rv);
+  for (auto expression : expressions) {
+    childCondition = do_QueryObject(expression, &rv);
     if (NS_SUCCEEDED(rv)) {
       rv = FilterCondition(map, childCondition, filter, flags);
       NS_ENSURE_SUCCESS(rv, rv);
       continue;
     }
 
-    childExpression = do_QueryElementAt(expressions, i, &rv);
+    childExpression = do_QueryObject(expression, &rv);
     if (NS_SUCCEEDED(rv)) {
       rv = FilterExpression(map, childExpression, filter, flags);
       NS_ENSURE_SUCCESS(rv, rv);
       continue;
     }
   }
 
   return rv;
--- a/mailnews/addrbook/src/nsAbBoolExprToLDAPFilter.h
+++ b/mailnews/addrbook/src/nsAbBoolExprToLDAPFilter.h
@@ -21,16 +21,16 @@ class nsAbBoolExprToLDAPFilter {
                           nsIAbBooleanExpression* expression, nsCString& filter,
                           int flags = TRANSLATE_CARD_PROPERTY);
 
  protected:
   static nsresult FilterExpression(nsIAbLDAPAttributeMap* map,
                                    nsIAbBooleanExpression* expression,
                                    nsCString& filter, int flags);
   static nsresult FilterExpressions(nsIAbLDAPAttributeMap* map,
-                                    nsIArray* expressions, nsCString& filter,
-                                    int flags);
+                                    nsTArray<RefPtr<nsISupports>>& expressions,
+                                    nsCString& filter, int flags);
   static nsresult FilterCondition(nsIAbLDAPAttributeMap* map,
                                   nsIAbBooleanConditionString* condition,
                                   nsCString& filter, int flags);
 };
 
 #endif
--- a/mailnews/addrbook/src/nsAbBooleanExpression.cpp
+++ b/mailnews/addrbook/src/nsAbBooleanExpression.cpp
@@ -79,29 +79,20 @@ NS_IMETHODIMP nsAbBooleanExpression::Get
 }
 NS_IMETHODIMP nsAbBooleanExpression::SetOperation(
     nsAbBooleanOperationType aOperation) {
   mOperation = aOperation;
 
   return NS_OK;
 }
 
-/* attribute nsIArray expressions; */
-NS_IMETHODIMP nsAbBooleanExpression::GetExpressions(nsIArray **aExpressions) {
-  if (!aExpressions) return NS_ERROR_NULL_POINTER;
-
-  if (!mExpressions) {
-    mExpressions = do_CreateInstance(NS_ARRAY_CONTRACTID);
-
-    if (!mExpressions) return NS_ERROR_OUT_OF_MEMORY;
-  }
-
-  NS_ADDREF(*aExpressions = mExpressions);
+/* attribute Array<nsISupports> expressions; */
+NS_IMETHODIMP nsAbBooleanExpression::GetExpressions(
+    nsTArray<RefPtr<nsISupports>> &aExpressions) {
+  aExpressions = mExpressions.Clone();
   return NS_OK;
 }
 
-NS_IMETHODIMP nsAbBooleanExpression::SetExpressions(nsIArray *aExpressions) {
-  if (!aExpressions) return NS_ERROR_NULL_POINTER;
-
-  mExpressions = aExpressions;
-
+NS_IMETHODIMP nsAbBooleanExpression::SetExpressions(
+    const nsTArray<RefPtr<nsISupports>> &aExpressions) {
+  mExpressions = aExpressions.Clone();
   return NS_OK;
 }
--- a/mailnews/addrbook/src/nsAbBooleanExpression.h
+++ b/mailnews/addrbook/src/nsAbBooleanExpression.h
@@ -30,12 +30,12 @@ class nsAbBooleanExpression : public nsI
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSIABBOOLEANEXPRESSION
 
   nsAbBooleanExpression();
 
  protected:
   virtual ~nsAbBooleanExpression();
   nsAbBooleanOperationType mOperation;
-  nsCOMPtr<nsIArray> mExpressions;
+  nsTArray<RefPtr<nsISupports>> mExpressions;
 };
 
 #endif
--- a/mailnews/addrbook/src/nsAbDirectoryQuery.cpp
+++ b/mailnews/addrbook/src/nsAbDirectoryQuery.cpp
@@ -10,16 +10,17 @@
 #include "nsAbBooleanExpression.h"
 #include "nsArrayUtils.h"
 #include "nsComponentManagerUtils.h"
 #include "nsString.h"
 #include "nsUnicharUtils.h"
 #include "nsIAbDirSearchListener.h"
 #include "nsISimpleEnumerator.h"
 #include "nsMsgUtils.h"
+#include "nsQueryObject.h"
 
 NS_IMPL_ISUPPORTS(nsAbDirectoryQuerySimpleBooleanExpression,
                   nsIAbBooleanExpression)
 
 nsAbDirectoryQuerySimpleBooleanExpression::
     nsAbDirectoryQuerySimpleBooleanExpression()
     : mOperation(nsIAbBooleanOperationTypes::AND) {}
 
@@ -43,48 +44,33 @@ NS_IMETHODIMP nsAbDirectoryQuerySimpleBo
 
   mOperation = aOperation;
 
   return NS_OK;
 }
 
 /* attribute nsIArray expressions; */
 NS_IMETHODIMP nsAbDirectoryQuerySimpleBooleanExpression::GetExpressions(
-    nsIArray** aExpressions) {
-  if (!aExpressions) return NS_ERROR_NULL_POINTER;
-
-  if (!mExpressions) {
-    mExpressions = do_CreateInstance(NS_ARRAY_CONTRACTID);
-    if (!mExpressions) return NS_ERROR_OUT_OF_MEMORY;
-  }
-
-  NS_ADDREF(*aExpressions = mExpressions);
+    nsTArray<RefPtr<nsISupports>>& aExpressions) {
+  aExpressions = mExpressions.Clone();
   return NS_OK;
 }
 
 NS_IMETHODIMP nsAbDirectoryQuerySimpleBooleanExpression::SetExpressions(
-    nsIArray* aExpressions) {
-  if (!aExpressions) return NS_ERROR_NULL_POINTER;
-
+    const nsTArray<RefPtr<nsISupports>>& aExpressions) {
   // Ensure all the items are of the right type.
   nsresult rv;
-  uint32_t count;
-  rv = aExpressions->GetLength(&count);
-  NS_ENSURE_SUCCESS(rv, rv);
-
   nsCOMPtr<nsIAbBooleanConditionString> queryExpression;
-
-  for (uint32_t i = 0; i < count; ++i) {
-    queryExpression = do_QueryElementAt(aExpressions, i, &rv);
+  for (auto expression : aExpressions) {
+    queryExpression = do_QueryInterface(expression, &rv);
     if (NS_FAILED(rv)) return NS_ERROR_ILLEGAL_VALUE;
   }
 
   // Values ok, so we can just save and return.
-  mExpressions = aExpressions;
-
+  mExpressions = aExpressions.Clone();
   return NS_OK;
 }
 
 NS_IMPL_ISUPPORTS(nsAbDirectoryQueryArguments, nsIAbDirectoryQueryArguments)
 
 nsAbDirectoryQueryArguments::nsAbDirectoryQueryArguments()
     : mQuerySubDirectories(true) {}
 
@@ -324,38 +310,37 @@ nsresult nsAbDirectoryQuery::matchCard(n
 }
 
 nsresult nsAbDirectoryQuery::matchCardExpression(
     nsIAbCard* card, nsIAbBooleanExpression* expression, bool* result) {
   nsAbBooleanOperationType operation;
   nsresult rv = expression->GetOperation(&operation);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCOMPtr<nsIArray> childExpressions;
-  rv = expression->GetExpressions(getter_AddRefs(childExpressions));
+  nsTArray<RefPtr<nsISupports>> childExpressions;
+  rv = expression->GetExpressions(childExpressions);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  uint32_t count;
-  rv = childExpressions->GetLength(&count);
+  uint32_t count = childExpressions.Length();
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (operation == nsIAbBooleanOperationTypes::NOT && count > 1)
     return NS_ERROR_FAILURE;
 
   bool value = *result = false;
   nsCOMPtr<nsIAbBooleanConditionString> childCondition;
   nsCOMPtr<nsIAbBooleanExpression> childExpression;
 
   for (uint32_t i = 0; i < count; i++) {
-    childCondition = do_QueryElementAt(childExpressions, i, &rv);
+    childCondition = do_QueryObject(childExpressions[i], &rv);
     if (NS_SUCCEEDED(rv)) {
       rv = matchCardCondition(card, childCondition, &value);
       NS_ENSURE_SUCCESS(rv, rv);
     } else {
-      childExpression = do_QueryElementAt(childExpressions, i, &rv);
+      childExpression = do_QueryObject(childExpressions[i], &rv);
       if (NS_SUCCEEDED(rv)) {
         rv = matchCardExpression(card, childExpression, &value);
         NS_ENSURE_SUCCESS(rv, rv);
       } else
         return NS_ERROR_FAILURE;
     }
     if (operation == nsIAbBooleanOperationTypes::OR && value)
       break;
--- a/mailnews/addrbook/src/nsAbDirectoryQuery.h
+++ b/mailnews/addrbook/src/nsAbDirectoryQuery.h
@@ -20,17 +20,17 @@ class nsAbDirectoryQuerySimpleBooleanExp
   NS_DECL_NSIABBOOLEANEXPRESSION
 
   nsAbDirectoryQuerySimpleBooleanExpression();
 
  private:
   virtual ~nsAbDirectoryQuerySimpleBooleanExpression();
 
  public:
-  nsCOMPtr<nsIArray> mExpressions;
+  nsTArray<RefPtr<nsISupports>> mExpressions;
   nsAbBooleanOperationType mOperation;
 };
 
 class nsAbDirectoryQueryArguments : public nsIAbDirectoryQueryArguments {
  public:
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSIABDIRECTORYQUERYARGUMENTS
 
--- a/mailnews/addrbook/src/nsAbOutlookDirectory.cpp
+++ b/mailnews/addrbook/src/nsAbOutlookDirectory.cpp
@@ -653,55 +653,53 @@ static nsresult BuildRestriction(nsIAbBo
 static nsresult BuildRestriction(nsIAbBooleanExpression *aLevel,
                                  SRestriction &aRestriction) {
   if (!aLevel) {
     return NS_ERROR_NULL_POINTER;
   }
   aRestriction.rt = RES_COMMENT;
   nsresult retCode = NS_OK;
   nsAbBooleanOperationType operationType = 0;
-  uint32_t nbExpressions = 0;
-  nsCOMPtr<nsIArray> expressions;
+  nsTArray<RefPtr<nsISupports>> expressions;
 
   retCode = aLevel->GetOperation(&operationType);
   NS_ENSURE_SUCCESS(retCode, retCode);
-  retCode = aLevel->GetExpressions(getter_AddRefs(expressions));
+  retCode = aLevel->GetExpressions(expressions);
   NS_ENSURE_SUCCESS(retCode, retCode);
-  retCode = expressions->GetLength(&nbExpressions);
-  NS_ENSURE_SUCCESS(retCode, retCode);
+  uint32_t nbExpressions = expressions.Length();
   if (nbExpressions == 0) {
     PRINTF(("Error, no expressions.\n"));
     return NS_OK;
   }
   if (operationType == nsIAbBooleanOperationTypes::NOT && nbExpressions != 1) {
     PRINTF(("Error, unary operation NOT with multiple operands.\n"));
     return NS_OK;
   }
   LPSRestriction restrictionArray = new SRestriction[nbExpressions];
   uint32_t realNbExpressions = 0;
   bool skipItem = false;
   uint32_t i = 0;
 
-  nsCOMPtr<nsIAbBooleanConditionString> condition;
-  nsCOMPtr<nsIAbBooleanExpression> subExpression;
+  RefPtr<nsIAbBooleanConditionString> condition;
+  RefPtr<nsIAbBooleanExpression> subExpression;
 
   for (i = 0; i < nbExpressions; ++i) {
-    condition = do_QueryElementAt(expressions, i, &retCode);
+    condition = do_QueryObject(expressions[i], &retCode);
 
     if (NS_SUCCEEDED(retCode)) {
       retCode = BuildRestriction(condition, *restrictionArray, skipItem);
       if (NS_SUCCEEDED(retCode)) {
         if (!skipItem) {
           ++restrictionArray;
           ++realNbExpressions;
         }
       } else
         PRINTF(("Cannot build restriction for item %d %08x.\n", i, retCode));
     } else {
-      subExpression = do_QueryElementAt(expressions, i, &retCode);
+      subExpression = do_QueryObject(expressions[i], &retCode);
 
       if (NS_SUCCEEDED(retCode)) {
         retCode = BuildRestriction(subExpression, *restrictionArray);
         if (NS_SUCCEEDED(retCode)) {
           if (restrictionArray->rt != RES_COMMENT) {
             ++restrictionArray;
             ++realNbExpressions;
           }
--- a/mailnews/addrbook/src/nsAbQueryStringToExpression.cpp
+++ b/mailnews/addrbook/src/nsAbQueryStringToExpression.cpp
@@ -8,17 +8,16 @@
 #include "nsComponentManagerUtils.h"
 #include "nsServiceManagerUtils.h"
 #include "nsCOMPtr.h"
 #include "nsString.h"
 #include "nsITextToSubURI.h"
 #include "nsAbBooleanExpression.h"
 #include "nsAbBaseCID.h"
 #include "plstr.h"
-#include "nsIMutableArray.h"
 
 /**
  * This code parses the query expression passed in as an addressbook URI.
  * The expression takes the form:
  * (BOOL1(FIELD1,OP1,VALUE1)..(FIELDn,OPn,VALUEn)(BOOL2(FIELD1,OP1,VALUE1)...)...)
  *
  * BOOLn   A boolean operator joining subsequent terms delimited by ().
  *         For possible values see CreateBooleanExpression().
@@ -103,28 +102,26 @@ nsresult nsAbQueryStringToExpression::Pa
   (*index)++;
 
   return NS_OK;
 }
 
 nsresult nsAbQueryStringToExpression::ParseExpressions(
     const char** index, nsIAbBooleanExpression* expression) {
   nsresult rv;
-  nsCOMPtr<nsIMutableArray> expressions(
-      do_CreateInstance(NS_ARRAY_CONTRACTID, &rv));
-  if (NS_FAILED(rv)) return NS_ERROR_OUT_OF_MEMORY;
+  nsTArray<RefPtr<nsISupports>> expressions;
 
   // Case: ")(*)(*)....(*))"
   // printf ("Case: )(*)(*)....(*)): %s\n", *index);
   while (**index == '(') {
     nsCOMPtr<nsISupports> childExpression;
     rv = ParseExpression(index, getter_AddRefs(childExpression));
     NS_ENSURE_SUCCESS(rv, rv);
 
-    expressions->AppendElement(childExpression);
+    expressions.AppendElement(childExpression);
   }
 
   if (**index == 0) return NS_ERROR_FAILURE;
 
   // Case: "))"
   // printf ("Case: )): %s\n", *index);
 
   if (**index != ')') return NS_ERROR_FAILURE;