From a3914ac40b6a512d3f1a3948b99f730c33ab8030 Mon Sep 17 00:00:00 2001 From: Ben Campbell Date: Thu, 26 Sep 2019 11:16:06 +1200 Subject: [PATCH] Bug 1562158 - Remove xpidl [array] use from nsIMsgTraitService. r=jorgk --- .../base/search/public/nsIMsgTraitService.idl | 27 ++++++++----- mailnews/base/search/src/nsMsgTraitService.js | 16 +++++--- .../base/test/unit/test_nsMsgTraitService.js | 13 +++---- mailnews/base/util/nsMsgDBFolder.cpp | 39 +++++++++---------- 4 files changed, 53 insertions(+), 42 deletions(-) diff --git a/mailnews/base/search/public/nsIMsgTraitService.idl b/mailnews/base/search/public/nsIMsgTraitService.idl index 987288f674..390ab4553d 100644 --- a/mailnews/base/search/public/nsIMsgTraitService.idl +++ b/mailnews/base/search/public/nsIMsgTraitService.idl @@ -129,17 +129,26 @@ interface nsIMsgTraitService : nsISupports ACString getAntiId(in ACString id); /** - * get an array of traits to be analyzed by the bayesian code. This is - * a pair of traits: a "pro" trait of messages that match the trait (and is - * set enabled) and an "anti" trait of messages that do not match the trait. + * 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(). * - * @param count length of proIndices and antiIndices arrays - * @param proIndices trait internal index for "pro" trait to analyze - * @param antiIndices trait internal index for corresponding "anti" traits + * @return array of trait internal indices for "pro" trait to analyze */ - void getEnabledIndices(out unsigned long count, - [array, size_is(count)] out unsigned long proIndices, - [array, size_is(count)] out unsigned long antiIndices); + Array 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 + */ + Array getEnabledAntiIndices(); /** * Add a trait as an alias of another trait. An alias is a trait whose diff --git a/mailnews/base/search/src/nsMsgTraitService.js b/mailnews/base/search/src/nsMsgTraitService.js index 83049faf26..954ba8fcf3 100644 --- a/mailnews/base/search/src/nsMsgTraitService.js +++ b/mailnews/base/search/src/nsMsgTraitService.js @@ -103,18 +103,24 @@ nsMsgTraitService.prototype = { return _traits[aId].antiId; }, - getEnabledIndices(aCount, aProIndices, aAntiIndices) { + getEnabledProIndices() { let proIndices = []; - let antiIndices = []; for (let id in _traits) { if (_traits[id].enabled) { proIndices.push(_traits[id].index); + } + } + return proIndices; + }, + + getEnabledAntiIndices() { + let antiIndices = []; + for (let id in _traits) { + if (_traits[id].enabled) { antiIndices.push(_traits[_traits[id].antiId].index); } } - aCount.value = proIndices.length; - aProIndices.value = proIndices; - aAntiIndices.value = antiIndices; + return antiIndices; }, addAlias(aTraitIndex, aTraitAliasIndex) { diff --git a/mailnews/base/test/unit/test_nsMsgTraitService.js b/mailnews/base/test/unit/test_nsMsgTraitService.js index 4fb2c6620f..f5e6869f06 100644 --- a/mailnews/base/test/unit/test_nsMsgTraitService.js +++ b/mailnews/base/test/unit/test_nsMsgTraitService.js @@ -57,13 +57,12 @@ function run_test() { Assert.ok(ts.getEnabled(proId)); ts.setAntiId(proId, antiId); Assert.equal(antiId, ts.getAntiId(proId)); - var proArray = {}; - var antiArray = {}; - ts.getEnabledIndices({}, proArray, antiArray); - Assert.equal(proArray.value.length, 2); - Assert.equal(antiArray.value.length, 2); - Assert.equal(proArray.value[1], proIndex); - Assert.equal(antiArray.value[1], antiIndex); + let proArray = ts.getEnabledProIndices(); + let antiArray = ts.getEnabledAntiIndices(); + Assert.equal(proArray.length, 2); + Assert.equal(antiArray.length, 2); + Assert.equal(proArray[1], proIndex); + Assert.equal(antiArray[1], antiIndex); // check of aliases // add three random aliases diff --git a/mailnews/base/util/nsMsgDBFolder.cpp b/mailnews/base/util/nsMsgDBFolder.cpp index e4c500d168..490e13e553 100644 --- a/mailnews/base/util/nsMsgDBFolder.cpp +++ b/mailnews/base/util/nsMsgDBFolder.cpp @@ -2090,16 +2090,16 @@ nsresult nsMsgDBFolder::SpamFilterClassifyMessage( do_GetService("@mozilla.org/msg-trait-service;1", &rv)); NS_ENSURE_SUCCESS(rv, rv); - uint32_t count; - uint32_t *proIndices; - uint32_t *antiIndices; - rv = traitService->GetEnabledIndices(&count, &proIndices, &antiIndices); + nsTArray proIndices; + rv = traitService->GetEnabledProIndices(proIndices); + NS_ENSURE_SUCCESS(rv, rv); + nsTArray antiIndices; + rv = traitService->GetEnabledAntiIndices(antiIndices); NS_ENSURE_SUCCESS(rv, rv); rv = aJunkMailPlugin->ClassifyTraitsInMessage( - aURI, count, proIndices, antiIndices, this, aMsgWindow, this); - free(proIndices); - free(antiIndices); + aURI, proIndices.Length(), proIndices.Elements(), antiIndices.Elements(), + this, aMsgWindow, this); return rv; } @@ -2114,17 +2114,16 @@ nsresult nsMsgDBFolder::SpamFilterClassifyMessages( do_GetService("@mozilla.org/msg-trait-service;1", &rv)); NS_ENSURE_SUCCESS(rv, rv); - uint32_t count; - uint32_t *proIndices; - uint32_t *antiIndices; - rv = traitService->GetEnabledIndices(&count, &proIndices, &antiIndices); + nsTArray proIndices; + rv = traitService->GetEnabledProIndices(proIndices); + NS_ENSURE_SUCCESS(rv, rv); + nsTArray antiIndices; + rv = traitService->GetEnabledAntiIndices(antiIndices); NS_ENSURE_SUCCESS(rv, rv); - rv = aJunkMailPlugin->ClassifyTraitsInMessages(aURICount, aURIArray, count, - proIndices, antiIndices, this, - aMsgWindow, this); - free(proIndices); - free(antiIndices); + rv = aJunkMailPlugin->ClassifyTraitsInMessages( + aURICount, aURIArray, proIndices.Length(), proIndices.Elements(), + antiIndices.Elements(), this, aMsgWindow, this); return rv; } @@ -2386,12 +2385,12 @@ nsMsgDBFolder::CallFilterPlugins(nsIMsgWindow *aMsgWindow, bool *aFiltersRun) { do_GetService("@mozilla.org/msg-trait-service;1", &rv)); NS_ENSURE_SUCCESS(rv, rv); - uint32_t count = 0, *proIndices, *antiIndices; - rv = traitService->GetEnabledIndices(&count, &proIndices, &antiIndices); + nsTArray proIndices; + rv = traitService->GetEnabledProIndices(proIndices); bool filterForOther = false; // We just skip this on failure, since it is rarely used. if (NS_SUCCEEDED(rv)) { - for (uint32_t i = 0; i < count; ++i) { + for (uint32_t i = 0; i < proIndices.Length(); ++i) { // The trait service determines which traits are globally enabled or // disabled. If a trait is enabled, it can still be made inactive // on a particular folder using an inherited property. To do that, @@ -2415,8 +2414,6 @@ nsMsgDBFolder::CallFilterPlugins(nsIMsgWindow *aMsgWindow, bool *aFiltersRun) { break; } } - free(proIndices); - free(antiIndices); } // clang-format off