Bug 1594889 - Remove xpidl [array] use in nsIMsgTraitService. r=mkmelin draft
authorBen Campbell <benc@thunderbird.net>
Wed, 27 Nov 2019 12:22:07 +1300
changeset 82250 1be215d94d6eda6a2c02ace5c43b3e033e3ab333
parent 82247 2cfb1d544ba0442fce04865f94804fd79ddeb30e
child 82251 c482ceef88d5dd6d84d939f2059e49cc673a4945
push id9942
push userbenc@thunderbird.net
push dateThu, 05 Dec 2019 00:52:33 +0000
treeherdertry-comm-central@c482ceef88d5 [default view] [failures only]
reviewersmkmelin
bugs1594889
Bug 1594889 - Remove xpidl [array] use in nsIMsgTraitService. r=mkmelin
mailnews/base/search/public/nsIMsgTraitService.idl
mailnews/base/search/src/MsgTraitService.jsm
mailnews/base/test/unit/test_nsMsgTraitService.js
mailnews/extensions/bayesian-spam-filter/src/nsBayesianFilter.cpp
mailnews/extensions/bayesian-spam-filter/test/unit/test_traitAliases.js
--- a/mailnews/base/search/public/nsIMsgTraitService.idl
+++ b/mailnews/base/search/public/nsIMsgTraitService.idl
@@ -130,28 +130,28 @@ interface nsIMsgTraitService : nsISuppor
 
   /**
    * Get an array of "pro" traits to be analyzed by the bayesian code. This is
    * a "pro" trait of messages that match the trait.
    * Only enabled traits are returned.
    * This should return the same number of indices as the corresponding call to
    * getEnabledAntiIndices().
    *
-   * @return array of trait internal indices for "pro" trait to analyze
+   * @return an array of trait internal indices for "pro" trait to analyze
    */
   Array<unsigned long> getEnabledProIndices();
 
   /**
    * Get an array of "anti" traits to be analyzed by the bayesian code. This is
    * a "anti" trait of messages that do not match the trait.
    * Only enabled traits are returned.
    * This should return the same number of indices as the corresponding call to
    * getEnabledProIndices().
    *
-   * @return array of trait internal indices for "anti" trait to analyze
+   * @return an array of trait internal indices for "anti" trait to analyze
    */
   Array<unsigned long> getEnabledAntiIndices();
 
   /**
    * Add a trait as an alias of another trait. An alias is a trait whose
    * counts will be combined with the aliased trait. This allows multiple sets
    * of corpus data to be used to provide information on a single message
    * characteristic, while allowing each individual set of corpus data to
@@ -169,15 +169,14 @@ interface nsIMsgTraitService : nsISuppor
    * @param aTraitAlias  the internal identifier for the alias to remove
    */
   void removeAlias(in unsigned long aTraitIndex, in unsigned long aTraitAlias);
 
   /**
    * Get an array of trait aliases for a trait index, if any
    *
    * @param aTraitIndex  the internal identifier for the aliased trait
-   * @param aLength      length of array of aliases
-   * @param aAliases     array of internal identifiers for aliases
+   *
+   * @return an array of internal identifiers for aliases
    */
-  void getAliases(in unsigned long aTraitIndex, out unsigned long aLength,
-                  [retval, array, size_is(aLength)] out unsigned long aAliases);
+  Array<unsigned long> getAliases(in unsigned long aTraitIndex);
 
 };
--- a/mailnews/base/search/src/MsgTraitService.jsm
+++ b/mailnews/base/search/src/MsgTraitService.jsm
@@ -144,25 +144,24 @@ MsgTraitService.prototype = {
     }
     let location = aliases.indexOf(aTraitAliasIndex.toString());
     if (location != -1) {
       aliases.splice(location, 1);
       traitsBranch.setCharPref("aliases." + aTraitIndex, aliases.join());
     }
   },
 
-  getAliases(aTraitIndex, aLength) {
+  getAliases(aTraitIndex) {
     let aliasesString = traitsBranch.getCharPref("aliases." + aTraitIndex, "");
     let aliases;
     if (aliasesString.length) {
       aliases = aliasesString.split(",");
     } else {
       aliases = [];
     }
-    aLength.value = aliases.length;
     return aliases;
   },
 };
 
 // initialization
 
 _init();
 
--- a/mailnews/base/test/unit/test_nsMsgTraitService.js
+++ b/mailnews/base/test/unit/test_nsMsgTraitService.js
@@ -64,31 +64,31 @@ function run_test() {
   Assert.equal(proArray[1], proIndex);
   Assert.equal(antiArray[1], antiIndex);
 
   // check of aliases
   // add three random aliases
   ts.addAlias(1, 501);
   ts.addAlias(1, 502);
   ts.addAlias(1, 601);
-  let aliases = ts.getAliases(1, {});
+  let aliases = ts.getAliases(1);
   Assert.equal(aliases[0], 501);
   Assert.equal(aliases[1], 502);
   Assert.equal(aliases[2], 601);
 
   // remove the middle one
   ts.removeAlias(1, 502);
-  aliases = ts.getAliases(1, {});
+  aliases = ts.getAliases(1);
   Assert.equal(aliases.length, 2);
   Assert.equal(aliases[0], 501);
   Assert.equal(aliases[1], 601);
 
   // try to add an existing value
   ts.addAlias(1, 501);
-  aliases = ts.getAliases(1, {});
+  aliases = ts.getAliases(1);
   Assert.equal(aliases.length, 2);
   Assert.equal(aliases[0], 501);
   Assert.equal(aliases[1], 601);
 
   // now let's make sure this got saved in preferences
   Assert.equal(
     proId,
     Services.prefs.getCharPref("mailnews.traits.id." + proIndex)
--- a/mailnews/extensions/bayesian-spam-filter/src/nsBayesianFilter.cpp
+++ b/mailnews/extensions/bayesian-spam-filter/src/nsBayesianFilter.cpp
@@ -1359,33 +1359,27 @@ void nsBayesianFilter::classifyMessage(
   /* this part is similar to the Graham algorithm with some adjustments. */
   uint32_t traitCount = aProTraits.Length();
 
   // pro message counts per trait index
   AutoTArray<uint32_t, kTraitAutoCapacity> numProMessages;
   // anti message counts per trait index
   AutoTArray<uint32_t, kTraitAutoCapacity> numAntiMessages;
   // array of pro aliases per trait index
-  AutoTArray<uint32_t*, kTraitAutoCapacity> proAliasArrays;
-  // number of pro aliases per trait index
-  AutoTArray<uint32_t, kTraitAutoCapacity> proAliasesLengths;
+  AutoTArray<nsTArray<uint32_t>, kTraitAutoCapacity> proAliasArrays;
   // array of anti aliases per trait index
-  AutoTArray<uint32_t*, kTraitAutoCapacity> antiAliasArrays;
-  // number of anti aliases per trait index
-  AutoTArray<uint32_t, kTraitAutoCapacity> antiAliasesLengths;
+  AutoTArray<nsTArray<uint32_t>, kTraitAutoCapacity> antiAliasArrays;
   // construct the outgoing listener arrays
   AutoTArray<uint32_t, kTraitAutoCapacity> traits;
   AutoTArray<uint32_t, kTraitAutoCapacity> percents;
   if (traitCount > kTraitAutoCapacity) {
     traits.SetCapacity(traitCount);
     percents.SetCapacity(traitCount);
     numProMessages.SetCapacity(traitCount);
     numAntiMessages.SetCapacity(traitCount);
-    proAliasesLengths.SetCapacity(traitCount);
-    antiAliasesLengths.SetCapacity(traitCount);
     proAliasArrays.SetCapacity(traitCount);
     antiAliasArrays.SetCapacity(traitCount);
   }
 
   nsresult rv;
   nsCOMPtr<nsIMsgTraitService> traitService(
       do_GetService("@mozilla.org/msg-trait-service;1", &rv));
   if (NS_FAILED(rv)) {
@@ -1394,71 +1388,67 @@ void nsBayesianFilter::classifyMessage(
             ("Failed to get trait service"));
   }
 
   // get aliases and message counts for the pro and anti traits
   for (uint32_t traitIndex = 0; traitIndex < traitCount; traitIndex++) {
     nsresult rv;
 
     // pro trait
-    uint32_t proAliasesLength = 0;
-    uint32_t* proAliases = nullptr;
+    nsTArray<uint32_t> proAliases;
     uint32_t proTrait = aProTraits[traitIndex];
     if (traitService) {
-      rv = traitService->GetAliases(proTrait, &proAliasesLength, &proAliases);
+      rv = traitService->GetAliases(proTrait, proAliases);
       if (NS_FAILED(rv)) {
         NS_ERROR("trait service failed to get aliases");
         MOZ_LOG(BayesianFilterLogModule, LogLevel::Error,
                 ("trait service failed to get aliases"));
       }
     }
-    proAliasesLengths.AppendElement(proAliasesLength);
     proAliasArrays.AppendElement(proAliases);
     uint32_t proMessageCount = mCorpus.getMessageCount(proTrait);
-    for (uint32_t aliasIndex = 0; aliasIndex < proAliasesLength; aliasIndex++)
+    for (uint32_t aliasIndex = 0; aliasIndex < proAliases.Length(); aliasIndex++)
       proMessageCount += mCorpus.getMessageCount(proAliases[aliasIndex]);
     numProMessages.AppendElement(proMessageCount);
 
     // anti trait
-    uint32_t antiAliasesLength = 0;
-    uint32_t* antiAliases = nullptr;
+    nsTArray<uint32_t> antiAliases;
     uint32_t antiTrait = aAntiTraits[traitIndex];
     if (traitService) {
       rv =
-          traitService->GetAliases(antiTrait, &antiAliasesLength, &antiAliases);
+          traitService->GetAliases(antiTrait, antiAliases);
       if (NS_FAILED(rv)) {
         NS_ERROR("trait service failed to get aliases");
         MOZ_LOG(BayesianFilterLogModule, LogLevel::Error,
                 ("trait service failed to get aliases"));
       }
     }
-    antiAliasesLengths.AppendElement(antiAliasesLength);
     antiAliasArrays.AppendElement(antiAliases);
     uint32_t antiMessageCount = mCorpus.getMessageCount(antiTrait);
-    for (uint32_t aliasIndex = 0; aliasIndex < antiAliasesLength; aliasIndex++)
+    for (uint32_t aliasIndex = 0; aliasIndex < antiAliases.Length(); aliasIndex++)
       antiMessageCount += mCorpus.getMessageCount(antiAliases[aliasIndex]);
     numAntiMessages.AppendElement(antiMessageCount);
   }
 
   for (uint32_t i = 0; i < tokenCount; ++i) {
     Token& token = tokens[i];
     CorpusToken* t = mCorpus.get(token.mWord);
     if (!t) continue;
     for (uint32_t traitIndex = 0; traitIndex < traitCount; traitIndex++) {
       uint32_t iProCount = mCorpus.getTraitCount(t, aProTraits[traitIndex]);
       // add in any counts for aliases to proTrait
-      for (uint32_t aliasIndex = 0; aliasIndex < proAliasesLengths[traitIndex];
+      for (uint32_t aliasIndex = 0; aliasIndex < proAliasArrays[traitIndex].Length();
            aliasIndex++)
         iProCount +=
             mCorpus.getTraitCount(t, proAliasArrays[traitIndex][aliasIndex]);
       double proCount = static_cast<double>(iProCount);
 
       uint32_t iAntiCount = mCorpus.getTraitCount(t, aAntiTraits[traitIndex]);
       // add in any counts for aliases to antiTrait
-      for (uint32_t aliasIndex = 0; aliasIndex < antiAliasesLengths[traitIndex];
+      for (uint32_t aliasIndex = 0; aliasIndex < antiAliasArrays[traitIndex].Length();
            aliasIndex++)
         iAntiCount +=
             mCorpus.getTraitCount(t, antiAliasArrays[traitIndex][aliasIndex]);
       double antiCount = static_cast<double>(iAntiCount);
 
       double prob, denom;
       // Prevent a divide by zero error by setting defaults for prob
 
@@ -1626,20 +1616,16 @@ void nsBayesianFilter::classifyMessage(
                    : nsMsgJunkStatus(nsIJunkMailPlugin::GOOD),
             proPercent);
     }
 
     if (aTraitListener) {
       traits.AppendElement(aProTraits[traitIndex]);
       percents.AppendElement(proPercent);
     }
-
-    // free aliases arrays returned from XPCOM
-    if (proAliasesLengths[traitIndex]) free(proAliasArrays[traitIndex]);
-    if (antiAliasesLengths[traitIndex]) free(antiAliasArrays[traitIndex]);
   }
 
   if (aTraitListener)
     aTraitListener->OnMessageTraitsClassified(messageURI, traits, percents);
 
   delete[] tokens;
   // reuse mAnalysisStore without clearing memory
   mNextAnalysisIndex = 1;
--- a/mailnews/extensions/bayesian-spam-filter/test/unit/test_traitAliases.js
+++ b/mailnews/extensions/bayesian-spam-filter/test/unit/test_traitAliases.js
@@ -137,18 +137,18 @@ function startCommand() {
 
   gTest = tests.shift();
 
   // classify message
   var antiArray = [kAntiTrait];
   var proArray = [kProTrait];
 
   // remove any existing aliases
-  let proAliases = traitService.getAliases(kProTrait, {});
-  let antiAliases = traitService.getAliases(kAntiTrait, {});
+  let proAliases = traitService.getAliases(kProTrait);
+  let antiAliases = traitService.getAliases(kAntiTrait);
   let proAlias;
   let antiAlias;
   while ((proAlias = proAliases.pop())) {
     traitService.removeAlias(kProTrait, proAlias);
   }
   while ((antiAlias = antiAliases.pop())) {
     traitService.removeAlias(kAntiTrait, antiAlias);
   }