Bug 1508054 - reduce exposed API of nsMsgHdr. r=jorgk
authoraceman <acelists@atlas.sk>
Sat, 17 Nov 2018 14:29:00 +0100
changeset 33742 0a1e5f9bc3135ad07009ca682ae8b43f82992d58
parent 33741 237d8227bd3fbbcbbf2ff62f376f255a8666c5f2
child 33743 aa07a415e6471232454abc757e172b11693baa08
push id388
push userclokep@gmail.com
push dateMon, 28 Jan 2019 20:54:56 +0000
reviewersjorgk
bugs1508054
Bug 1508054 - reduce exposed API of nsMsgHdr. r=jorgk
mailnews/db/msgdb/public/nsMsgHdr.h
mailnews/db/msgdb/src/nsMsgDatabase.cpp
mailnews/db/msgdb/src/nsMsgHdr.cpp
--- a/mailnews/db/msgdb/public/nsMsgHdr.h
+++ b/mailnews/db/msgdb/public/nsMsgHdr.h
@@ -15,71 +15,80 @@
 
 class nsMsgDatabase;
 class nsIMsgThread;
 
 class nsMsgHdr : public nsIMsgDBHdr {
 public:
     NS_DECL_NSIMSGDBHDR
     friend class nsMsgDatabase;
-    friend class nsMsgPropertyEnumerator; // accesses m_mdb
+    friend class nsImapMailDatabase;
+    friend class nsMsgPropertyEnumerator;
+    friend class nsMsgThread;
+
     ////////////////////////////////////////////////////////////////////////////
     ////////////////////////////////////////////////////////////////////////////
     // nsMsgHdr methods:
     nsMsgHdr(nsMsgDatabase *db, nsIMdbRow *dbRow);
 
-    virtual nsresult    GetRawFlags(uint32_t *result);
-    void                Init();
-    virtual nsresult    InitCachedValues();
-    virtual nsresult    InitFlags();
-    void                ClearCachedValues() {m_initedValues = 0;}
-
     NS_DECL_ISUPPORTS
 
-    nsIMdbRow   *GetMDBRow() {return m_mdbRow;}
-    bool        IsParentOf(nsIMsgDBHdr *possibleChild);
-    bool        IsAncestorOf(nsIMsgDBHdr *possibleChild);
-    bool        IsAncestorKilled(uint32_t ancestorsToCheck);
-    void        ReparentInThread(nsIMsgThread *thread);
-
     size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOfFun) const
     {
       return m_references.ShallowSizeOfExcludingThis(aMallocSizeOfFun);
     }
     size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOfFun) const
     {
       return aMallocSizeOfFun(this) + SizeOfExcludingThis(aMallocSizeOfFun);
     }
 
 protected:
-  virtual ~nsMsgHdr();
+    nsIMdbRow*       GetMDBRow() { return m_mdbRow; }
+    void             ReleaseMDBRow() { NS_IF_RELEASE(m_mdbRow); }
+    nsMsgDatabase*   GetMdb() { return m_mdb; }
+    void             ClearCachedValues() { m_initedValues = 0; }
+
+    virtual nsresult GetRawFlags(uint32_t *result);
+
+    bool             IsParentOf(nsIMsgDBHdr *possibleChild);
+    bool             IsAncestorOf(nsIMsgDBHdr *possibleChild);
+
+private:
+    virtual ~nsMsgHdr();
+
+    void             Init();
+    virtual nsresult InitFlags();
+    virtual nsresult InitCachedValues();
+
+    bool             IsAncestorKilled(uint32_t ancestorsToCheck);
+    void             ReparentInThread(nsIMsgThread *thread);
+
     nsresult SetStringColumn(const char *str, mdb_token token);
     nsresult SetUInt32Column(uint32_t value, mdb_token token);
     nsresult GetUInt32Column(mdb_token token, uint32_t *pvalue, uint32_t defaultValue = 0);
     nsresult SetUInt64Column(uint64_t value, mdb_token token);
     nsresult GetUInt64Column(mdb_token token, uint64_t *pvalue, uint64_t defaultValue = 0);
 
     // reference and threading stuff.
     nsresult ParseReferences(const char *references);
     const char* GetNextReference(const char *startNextRef, nsCString &reference,
                                  bool acceptNonDelimitedReferences);
 
     nsMsgKey    m_threadId;
-    nsMsgKey    m_messageKey;   // news: article number, mail mbox offset, imap uid...
+    nsMsgKey    m_messageKey;   // news: article number, local mail: key, imap: uid...
     nsMsgKey    m_threadParent; // message this is a reply to, in thread.
     PRTime      m_date;
     uint32_t    m_messageSize;  // lines for news articles, bytes for mail messages
-    uint32_t    m_statusOffset; // offset in a local mail message of the mozilla status hdr
+    uint32_t    m_statusOffset; // offset in a local mail message of the x-mozilla-status header
     uint32_t    m_flags;
     // avoid parsing references every time we want one
     nsTArray<nsCString> m_references;
     nsMsgPriorityValue  m_priority;
 
     // nsMsgHdrs will have to know what db and row they belong to, since they are really
     // just a wrapper around the msg row in the mdb. This could cause problems,
     // though I hope not.
     nsMsgDatabase *m_mdb;
     nsIMdbRow     *m_mdbRow;
     uint32_t      m_initedValues;
 };
 
 #endif
-
--- a/mailnews/db/msgdb/src/nsMsgDatabase.cpp
+++ b/mailnews/db/msgdb/src/nsMsgDatabase.cpp
@@ -2004,24 +2004,24 @@ NS_IMETHODIMP nsMsgDatabase::DeleteHeade
   if (commit)
     Commit(nsMsgDBCommitType::kLargeCommit);      // ### dmb is this a good time to commit?
   return ret;
 }
 
 NS_IMETHODIMP
 nsMsgDatabase::UndoDelete(nsIMsgDBHdr *aMsgHdr)
 {
-    if (aMsgHdr)
-    {
-        nsMsgHdr* msgHdr = static_cast<nsMsgHdr*>(aMsgHdr);  // closed system, so this is ok
-        // force deleted flag, so SetHdrFlag won't bail out because  deleted flag isn't set
-        msgHdr->m_flags |= nsMsgMessageFlags::Expunged;
-        SetHdrFlag(msgHdr, false, nsMsgMessageFlags::Expunged); // clear deleted flag in db
-    }
-    return NS_OK;
+  if (aMsgHdr)
+  {
+    // Force deleted flag, so SetHdrFlag won't bail out because deleted flag isn't set.
+    uint32_t result;
+    aMsgHdr->OrFlags(nsMsgMessageFlags::Expunged, &result);
+    SetHdrFlag(aMsgHdr, false, nsMsgMessageFlags::Expunged);  // Clear deleted flag in db.
+  }
+  return NS_OK;
 }
 
 nsresult nsMsgDatabase::RemoveHeaderFromThread(nsMsgHdr *msgHdr)
 {
   if (!msgHdr)
     return NS_ERROR_NULL_POINTER;
   nsresult ret = NS_OK;
   nsCOMPtr <nsIMsgThread> thread ;
@@ -2054,17 +2054,17 @@ nsresult nsMsgDatabase::RemoveHeaderFrom
   if (UseCorrectThreading())
     RemoveMsgRefsFromHash(msgHdr);
   nsIMdbRow* row = msgHdr->GetMDBRow();
   if (row)
   {
     ret = m_mdbAllMsgHeadersTable->CutRow(GetEnv(), row);
     row->CutAllColumns(GetEnv());
   }
-  msgHdr->m_initedValues = 0; // invalidate cached values.
+  msgHdr->ClearCachedValues();
   return ret;
 }
 
 nsresult nsMsgDatabase::IsRead(nsMsgKey key, bool *pRead)
 {
   nsCOMPtr <nsIMsgDBHdr> msgHdr;
 
   nsresult rv = GetMsgHdrForKey(key, getter_AddRefs(msgHdr));
@@ -3447,18 +3447,20 @@ NS_IMETHODIMP nsMsgDatabase::CreateNewHd
   return err;
 }
 
 NS_IMETHODIMP nsMsgDatabase::AddNewHdrToDB(nsIMsgDBHdr *newHdr, bool notify)
 {
   NS_ENSURE_ARG_POINTER(newHdr);
   nsMsgHdr* hdr = static_cast<nsMsgHdr*>(newHdr);          // closed system, cast ok
   bool newThread;
-  bool hasKey;
-  ContainsKey(hdr->m_messageKey, &hasKey);
+  bool hasKey = false;
+  nsMsgKey msgKey = nsMsgKey_None;
+  (void)hdr->GetMessageKey(&msgKey);
+  (void)ContainsKey(msgKey, &hasKey);
   if (hasKey)
   {
     NS_ERROR("adding hdr that already exists");
     return NS_ERROR_FAILURE;
   }
   nsresult err = ThreadNewHdr(hdr, newThread);
   // we thread header before we add it to the all headers table
   // so that subject and reference threading will work (otherwise,
@@ -3523,17 +3525,17 @@ NS_IMETHODIMP nsMsgDatabase::CopyHdrFrom
     if (!destRow)
       return NS_ERROR_UNEXPECTED;
 
     err = destRow->SetRow(GetEnv(), sourceRow);
     if (NS_SUCCEEDED(err))
     {
       // we may have gotten the header from a cache - calling SetRow
       // basically invalidates any cached values, so invalidate them.
-      destMsgHdr->m_initedValues = 0;
+      destMsgHdr->ClearCachedValues();
       if(addHdrToDB)
         err = AddNewHdrToDB(destMsgHdr, true);
       if (NS_SUCCEEDED(err) && newHdr)
         *newHdr = destMsgHdr;
     }
   }
   return err;
 }
@@ -4762,17 +4764,18 @@ nsresult nsMsgDatabase::AddNewThread(nsM
 {
 
   if (!msgHdr)
     return NS_ERROR_NULL_POINTER;
 
   nsMsgThread *threadHdr = nullptr;
 
   nsCString subject;
-  nsMsgKey threadKey = msgHdr->m_messageKey;
+  nsMsgKey threadKey;
+  msgHdr->GetMessageKey(&threadKey);
   // can't have a thread with key 1 since that's the table id of the all msg hdr table,
   // so give it kTableKeyForThreadOne (0xfffffffe).
   if (threadKey == kAllMsgHdrsTableKey)
     threadKey = kTableKeyForThreadOne;
 
   nsresult err = msgHdr->GetSubject(getter_Copies(subject));
 
   err = CreateNewThread(threadKey, subject.get(), &threadHdr);
@@ -4991,29 +4994,27 @@ nsresult nsMsgDatabase::DumpContents()
   RefPtr<nsMsgKeyArray> keys = new nsMsgKeyArray;
   if (!keys)
     return NS_ERROR_OUT_OF_MEMORY;
   nsresult rv = ListAllKeys(keys);
   uint32_t numKeys;
   keys->GetLength(&numKeys);
   for (i = 0; i < numKeys; i++) {
     key = keys->m_keys[i];
-    nsIMsgDBHdr *msg = NULL;
-    rv = GetMsgHdrForKey(key, &msg);
-    nsMsgHdr* msgHdr = static_cast<nsMsgHdr*>(msg);      // closed system, cast ok
+    nsCOMPtr<nsIMsgDBHdr> msgHdr;
+    rv = GetMsgHdrForKey(key, getter_AddRefs(msgHdr));
     if (NS_SUCCEEDED(rv))
     {
       nsCString author;
       nsCString subject;
 
       msgHdr->GetMessageKey(&key);
       msgHdr->GetAuthor(getter_Copies(author));
       msgHdr->GetSubject(getter_Copies(subject));
       printf("hdr key = %u, author = %s subject = %s\n", key, author.get(), subject.get());
-      NS_RELEASE(msgHdr);
     }
   }
   nsTArray<nsMsgKey> threads;
   rv = ListAllThreads(&threads);
   for ( i = 0; i < threads.Length(); i++)
   {
     key = threads[i];
     printf("thread key = %u\n", key);
@@ -5793,34 +5794,35 @@ mdb_pos nsMsgDatabase::FindInsertIndexIn
 }
 NS_IMETHODIMP
 nsMsgDatabase::UpdateHdrInCache(const char *aSearchFolderUri, nsIMsgDBHdr *aHdr, bool aAdd)
 {
   nsCOMPtr <nsIMdbTable> table;
   nsresult err = GetSearchResultsTable(aSearchFolderUri, true, getter_AddRefs(table));
   NS_ENSURE_SUCCESS(err, err);
   nsMsgKey key;
-  aHdr->GetMessageKey(&key);
+  err = aHdr->GetMessageKey(&key);
   nsMsgHdr* msgHdr = static_cast<nsMsgHdr*>(aHdr);  // closed system, so this is ok
-  if (NS_SUCCEEDED(err) && m_mdbStore && msgHdr->m_mdbRow)
+  nsIMdbRow* hdrRow = msgHdr->GetMDBRow();
+  if (NS_SUCCEEDED(err) && m_mdbStore && hdrRow)
   {
     if (!aAdd)
     {
-      table->CutRow(m_mdbEnv, msgHdr->m_mdbRow);
+      table->CutRow(m_mdbEnv, hdrRow);
     }
     else
     {
       mdbOid rowId;
-      msgHdr->m_mdbRow->GetOid(m_mdbEnv, &rowId);
+      hdrRow->GetOid(m_mdbEnv, &rowId);
       mdb_pos insertPos = FindInsertIndexInSortedTable(table, rowId.mOid_Id);
       uint32_t rowCount;
       table->GetCount(m_mdbEnv, &rowCount);
-      table->AddRow(m_mdbEnv, msgHdr->m_mdbRow);
+      table->AddRow(m_mdbEnv, hdrRow);
       mdb_pos newPos;
-      table->MoveRow(m_mdbEnv, msgHdr->m_mdbRow, rowCount, insertPos, &newPos);
+      table->MoveRow(m_mdbEnv, hdrRow, rowCount, insertPos, &newPos);
     }
   }
 
 //  if (aAdd)
  // if we need to add this hdr, we need to insert it in key order.
   return NS_OK;
 }
 NS_IMETHODIMP
--- a/mailnews/db/msgdb/src/nsMsgHdr.cpp
+++ b/mailnews/db/msgdb/src/nsMsgHdr.cpp
@@ -1020,17 +1020,17 @@ nsMsgPropertyEnumerator::nsMsgPropertyEn
      mNextColumn(NULL_MORK_COLUMN)
 {
   RefPtr<nsMsgDatabase> mdb;
   nsCOMPtr<nsIMdbRow> mdbRow;
 
   if (aHdr &&
       (mdbRow = aHdr->GetMDBRow()) &&
       (m_hdr = aHdr) &&
-      (mdb = aHdr->m_mdb) &&
+      (mdb = aHdr->GetMdb()) &&
       (m_mdbEnv = mdb->m_mdbEnv) &&
       (m_mdbStore = mdb->m_mdbStore))
   {
     mdbRow->GetRowCellCursor(m_mdbEnv, -1, getter_AddRefs(mRowCellCursor));
   }
 }
 
 nsMsgPropertyEnumerator::~nsMsgPropertyEnumerator()