Bug 1612239 - Remove nsIArray use from nsIMsgPluggableStore.changeKeywords(). r=mkmelin
authorBen Campbell <benc@thunderbird.net>
Tue, 19 May 2020 12:58:11 +0300
changeset 39181 bc7e17995e196b439e49de01b3774726b912b960
parent 39180 c827ec349a361a8f647744e23275fc414addf30d
child 39182 1f1357f245c172afa95cc491fb3b2f14d8fb067c
push id402
push userclokep@gmail.com
push dateMon, 29 Jun 2020 20:48:04 +0000
reviewersmkmelin
bugs1612239
Bug 1612239 - Remove nsIArray use from nsIMsgPluggableStore.changeKeywords(). r=mkmelin
mailnews/base/public/nsIMsgPluggableStore.idl
mailnews/local/src/nsLocalMailFolder.cpp
mailnews/local/src/nsMsgBrkMBoxStore.cpp
mailnews/local/src/nsMsgMaildirStore.cpp
--- a/mailnews/base/public/nsIMsgPluggableStore.idl
+++ b/mailnews/base/public/nsIMsgPluggableStore.idl
@@ -312,17 +312,17 @@ interface nsIMsgPluggableStore : nsISupp
   void changeFlags(in Array<nsIMsgDBHdr> aHdrArray, in unsigned long aFlags,
                    in boolean aSet);
   /**
    *Sets/Clears the passed keywords on the passed messages.
    * @param aHdrArray array of nsIMsgDBHdr's
    * @param aKeywords keywords to set/clear
    * @param aAdd true to add the keyword(s), false to remove.
    */
-  void changeKeywords(in nsIArray aHdrArray, in ACString aKeywords,
+  void changeKeywords(in Array<nsIMsgDBHdr> aHdrArray, in ACString aKeywords,
                       in boolean aAdd);
 
   /**
    * Identifies a specific type of store. Please use this only for legacy
    * bug fixes, and not as a method to change behavior!
    *
    * Typical values: "mbox", "maildir"
    */
--- a/mailnews/local/src/nsLocalMailFolder.cpp
+++ b/mailnews/local/src/nsLocalMailFolder.cpp
@@ -3509,17 +3509,19 @@ nsresult nsMsgLocalMailFolder::ChangeKey
   nsresult rv =
       (add) ? nsMsgDBFolder::AddKeywordsToMessages(aMessages, aKeywords)
             : nsMsgDBFolder::RemoveKeywordsFromMessages(aMessages, aKeywords);
 
   NS_ENSURE_SUCCESS(rv, rv);
   nsCOMPtr<nsIMsgPluggableStore> msgStore;
   GetMsgStore(getter_AddRefs(msgStore));
   NS_ENSURE_SUCCESS(rv, rv);
-  return msgStore->ChangeKeywords(aMessages, aKeywords, add);
+  nsTArray<RefPtr<nsIMsgDBHdr>> tmpHdrs;
+  MsgHdrsToTArray(aMessages, tmpHdrs);
+  return msgStore->ChangeKeywords(tmpHdrs, aKeywords, add);
 }
 
 NS_IMETHODIMP nsMsgLocalMailFolder::RemoveKeywordsFromMessages(
     nsIArray *aMessages, const nsACString &aKeywords) {
   return ChangeKeywordForMessages(aMessages, aKeywords, false /* remove */);
 }
 
 NS_IMETHODIMP nsMsgLocalMailFolder::UpdateNewMsgHdr(nsIMsgDBHdr *aOldHdr,
--- a/mailnews/local/src/nsMsgBrkMBoxStore.cpp
+++ b/mailnews/local/src/nsMsgBrkMBoxStore.cpp
@@ -806,22 +806,21 @@ void nsMsgBrkMBoxStore::SetDBValid(nsIMs
     folder->GetMsgDatabase(getter_AddRefs(db));
     if (db) SetSummaryFileValid(folder, db, true);
   }
 }
 
 NS_IMETHODIMP nsMsgBrkMBoxStore::ChangeFlags(
     const nsTArray<RefPtr<nsIMsgDBHdr>> &aHdrArray, uint32_t aFlags,
     bool aSet) {
+  if (aHdrArray.IsEmpty()) return NS_ERROR_INVALID_ARG;
+
   nsCOMPtr<nsIOutputStream> outputStream;
   nsCOMPtr<nsISeekableStream> seekableStream;
   int64_t restoreStreamPos;
-
-  if (aHdrArray.IsEmpty()) return NS_ERROR_INVALID_ARG;
-
   nsresult rv = GetOutputStream(aHdrArray[0], outputStream, seekableStream,
                                 restoreStreamPos);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIMsgDBHdr> msgHdr;
   for (auto msgHdr : aHdrArray) {
     // Seek to x-mozilla-status offset and rewrite value.
     rv = UpdateFolderFlag(msgHdr, aSet, aFlags, outputStream);
@@ -833,31 +832,27 @@ NS_IMETHODIMP nsMsgBrkMBoxStore::ChangeF
   if (restoreStreamPos != -1)
     seekableStream->Seek(nsISeekableStream::NS_SEEK_SET, restoreStreamPos);
   else if (outputStream)
     outputStream->Close();
   SetDBValid(aHdrArray[0]);
   return NS_OK;
 }
 
-NS_IMETHODIMP nsMsgBrkMBoxStore::ChangeKeywords(nsIArray *aHdrArray,
-                                                const nsACString &aKeywords,
-                                                bool aAdd) {
-  NS_ENSURE_ARG_POINTER(aHdrArray);
+NS_IMETHODIMP nsMsgBrkMBoxStore::ChangeKeywords(
+    const nsTArray<RefPtr<nsIMsgDBHdr>> &aHdrArray, const nsACString &aKeywords,
+    bool aAdd) {
+  if (aHdrArray.IsEmpty()) return NS_ERROR_INVALID_ARG;
+
   nsCOMPtr<nsIOutputStream> outputStream;
   nsCOMPtr<nsISeekableStream> seekableStream;
   int64_t restoreStreamPos;
 
-  uint32_t messageCount;
-  nsresult rv = aHdrArray->GetLength(&messageCount);
-  NS_ENSURE_SUCCESS(rv, rv);
-  if (!messageCount) return NS_ERROR_INVALID_ARG;
-  nsCOMPtr<nsIMsgDBHdr> firstHdr = do_QueryElementAt(aHdrArray, 0);
-  rv =
-      GetOutputStream(firstHdr, outputStream, seekableStream, restoreStreamPos);
+  nsresult rv = GetOutputStream(aHdrArray[0], outputStream, seekableStream,
+                                restoreStreamPos);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIInputStream> inputStream = do_QueryInterface(outputStream, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   mozilla::UniquePtr<nsLineBuffer<char>> lineBuffer(new nsLineBuffer<char>);
 
   // For each message, we seek to the beginning of the x-mozilla-status header,
@@ -872,37 +867,34 @@ NS_IMETHODIMP nsMsgBrkMBoxStore::ChangeK
   // we can't do anything until the folder is compacted and another
   // x-mozilla-keys header is added. In that case, we set a property
   // on the header, which the compaction code will check.
 
   nsTArray<nsCString> keywordArray;
   ParseString(aKeywords, ' ', keywordArray);
 
   nsCOMPtr<nsIMsgDBHdr> msgHdr;
-  for (uint32_t i = 0; i < messageCount; ++i)  // for each message
+  for (auto msgHdr : aHdrArray)  // for each message
   {
-    msgHdr = do_QueryElementAt(aHdrArray, i, &rv);
-    NS_ENSURE_SUCCESS(rv, rv);
     uint64_t messageOffset;
     msgHdr->GetMessageOffset(&messageOffset);
     uint32_t statusOffset = 0;
     (void)msgHdr->GetStatusOffset(&statusOffset);
     uint64_t desiredOffset = messageOffset + statusOffset;
 
     ChangeKeywordsHelper(msgHdr, desiredOffset, *lineBuffer, keywordArray, aAdd,
                          outputStream, seekableStream, inputStream);
   }
   lineBuffer.reset();
   if (restoreStreamPos != -1)
     seekableStream->Seek(nsISeekableStream::NS_SEEK_SET, restoreStreamPos);
   else if (outputStream)
     outputStream->Close();
-  if (messageCount > 0) {
-    msgHdr = do_QueryElementAt(aHdrArray, 0);
-    SetDBValid(msgHdr);
+  if (!aHdrArray.IsEmpty()) {
+    SetDBValid(aHdrArray[0]);
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP nsMsgBrkMBoxStore::GetStoreType(nsACString &aType) {
   aType.AssignLiteral("mbox");
   return NS_OK;
 }
--- a/mailnews/local/src/nsMsgMaildirStore.cpp
+++ b/mailnews/local/src/nsMsgMaildirStore.cpp
@@ -1315,41 +1315,34 @@ nsresult nsMsgMaildirStore::GetOutputStr
   nsCOMPtr<nsIFile> maildirFile;
   folderPath->Clone(getter_AddRefs(maildirFile));
   maildirFile->Append(NS_LITERAL_STRING("cur"));
   maildirFile->AppendNative(fileName);
 
   return MsgGetFileStream(maildirFile, getter_AddRefs(aOutputStream));
 }
 
-NS_IMETHODIMP nsMsgMaildirStore::ChangeKeywords(nsIArray *aHdrArray,
+NS_IMETHODIMP nsMsgMaildirStore::ChangeKeywords(const nsTArray<RefPtr<nsIMsgDBHdr>> &aHdrArray,
                                                 const nsACString &aKeywords,
                                                 bool aAdd) {
-  NS_ENSURE_ARG_POINTER(aHdrArray);
-  NS_ENSURE_ARG_POINTER(aHdrArray);
+  if (aHdrArray.IsEmpty()) return NS_ERROR_INVALID_ARG;
+
   nsCOMPtr<nsIOutputStream> outputStream;
   nsCOMPtr<nsISeekableStream> seekableStream;
 
-  uint32_t messageCount;
-  nsresult rv = aHdrArray->GetLength(&messageCount);
-  NS_ENSURE_SUCCESS(rv, rv);
-  if (!messageCount) return NS_ERROR_INVALID_ARG;
-
   mozilla::UniquePtr<nsLineBuffer<char>> lineBuffer(new nsLineBuffer<char>);
 
   nsTArray<nsCString> keywordArray;
   ParseString(aKeywords, ' ', keywordArray);
 
-  for (uint32_t i = 0; i < messageCount; ++i)  // for each message
+  for (auto message : aHdrArray)  // for each message
   {
-    nsCOMPtr<nsIMsgDBHdr> message = do_QueryElementAt(aHdrArray, i, &rv);
-    NS_ENSURE_SUCCESS(rv, rv);
     // get output stream for header
     nsCOMPtr<nsIOutputStream> outputStream;
-    rv = GetOutputStream(message, outputStream);
+    nsresult rv = GetOutputStream(message, outputStream);
     NS_ENSURE_SUCCESS(rv, rv);
     nsCOMPtr<nsIInputStream> inputStream = do_QueryInterface(outputStream, &rv);
     NS_ENSURE_SUCCESS(rv, rv);
     nsCOMPtr<nsISeekableStream> seekableStream(
         do_QueryInterface(inputStream, &rv));
     NS_ENSURE_SUCCESS(rv, rv);
     uint32_t statusOffset = 0;
     (void)message->GetStatusOffset(&statusOffset);