Bug 1433193 - Follow-up: misc. code improvements in nsMsgSend.cpp. r=bz
authorJorg K <jorgk@jorgk.com>
Wed, 31 Jan 2018 01:30:30 +0100
changeset 31006 22d78d1bbe82d8a2038cbb104386c8e7363c13b0
parent 31005 4007d551bf7a114ab2ec74d4ab823bbb926a1757
child 31007 702ee21341981524ced85e7510a7a9dc67adc3ff
push id383
push userclokep@gmail.com
push dateMon, 07 May 2018 21:52:48 +0000
reviewersbz
bugs1433193
Bug 1433193 - Follow-up: misc. code improvements in nsMsgSend.cpp. r=bz
mailnews/compose/src/nsMsgSend.cpp
mailnews/compose/src/nsMsgSend.h
--- a/mailnews/compose/src/nsMsgSend.cpp
+++ b/mailnews/compose/src/nsMsgSend.cpp
@@ -1254,51 +1254,44 @@ mime_write_message_body(nsIMsgSend *stat
 nsresult
 mime_encoder_output_fn(const char *buf, int32_t size, void *closure)
 {
   nsMsgComposeAndSend *state = (nsMsgComposeAndSend *) closure;
   return mime_write_message_body (state, (char *) buf, (uint32_t)size);
 }
 
 nsresult
-nsMsgComposeAndSend::GetEmbeddedObjectInfo(nsIDOMNode *node, nsMsgAttachmentData *attachment, bool *acceptObject)
+nsMsgComposeAndSend::GetEmbeddedObjectInfo(Element *domElement, nsMsgAttachmentData *attachment, bool *acceptObject)
 {
-  NS_ENSURE_ARG_POINTER(node);
+  NS_ENSURE_ARG_POINTER(domElement);
   NS_ENSURE_ARG_POINTER(attachment);
   NS_ENSURE_ARG_POINTER(acceptObject);
 
   // GetEmbeddedObjectInfo will determine if we need to attach the source of the
   // embedded object with the message. The decision is made automatically unless
   // the attribute moz-do-not-send has been set to true or false.
   nsresult rv = NS_OK;
 
   // Reset this structure to null!
   *acceptObject = false;
 
-  // We're only interested in body, image, link and anchors which are all
-  // elements.
-  nsCOMPtr<Element> domElement = do_QueryInterface(node);
-  if (!domElement)
-    return NS_OK;
-
   bool isImage = false;
   nsAutoString mozDoNotSendAttr;
   domElement->GetAttribute(NS_LITERAL_STRING(ATTR_MOZ_DO_NOT_SEND), mozDoNotSendAttr);
 
   // Only empty or moz-do-not-send="false" may be accepted later.
   if (!(mozDoNotSendAttr.IsEmpty() || mozDoNotSendAttr.LowerCaseEqualsLiteral("false")))
     return NS_OK;
 
-  // Now, we know the types of objects this node can be, so we will do
-  // our query interface here and see what we come up with
-  nsCOMPtr<Element> nodeAsElement = do_QueryInterface(node);
-  RefPtr<HTMLImageElement>  image  = HTMLImageElement::FromContentOrNull(nodeAsElement);
-  RefPtr<HTMLLinkElement>   link   = HTMLLinkElement::FromContentOrNull(nodeAsElement);
-  RefPtr<HTMLAnchorElement> anchor = HTMLAnchorElement::FromContentOrNull(nodeAsElement);
-  RefPtr<HTMLBodyElement>   body   = HTMLBodyElement::FromContentOrNull(nodeAsElement);
+  // We're only interested in body, image, link and anchors which are all
+  // elements. Let's see what we have.
+  RefPtr<HTMLImageElement>  image  = HTMLImageElement::FromContent(domElement);
+  RefPtr<HTMLLinkElement>   link   = HTMLLinkElement::FromContent(domElement);
+  RefPtr<HTMLAnchorElement> anchor = HTMLAnchorElement::FromContent(domElement);
+  RefPtr<HTMLBodyElement>   body   = HTMLBodyElement::FromContent(domElement);
 
   // First, try to see if the body as a background image
   if (body)
   {
     nsString tUrl;
     body->GetBackground(tUrl);
     if (tUrl.IsEmpty())
       return NS_OK;
@@ -1320,20 +1313,18 @@ nsMsgComposeAndSend::GetEmbeddedObjectIn
       return NS_OK;
 
     nsAutoCString turlC;
     CopyUTF16toUTF8(tUrl, turlC);
     if (NS_FAILED(nsMsgNewURL(getter_AddRefs(attachment->m_url), turlC)))
     {
       // Well, the first time failed...which means we probably didn't get
       // the full path name...
-      nsCOMPtr<nsINode> node2 = do_QueryInterface(node);
-
       nsAutoCString spec;
-      rv = node2->OwnerDoc()->GetDocumentURI()->GetSpec(spec);
+      rv = image->OwnerDoc()->GetDocumentURI()->GetSpec(spec);
       NS_ENSURE_SUCCESS(rv, rv);
 
       // Ok, now get the path to the root doc and tack on the name we
       // got from the GetSrc() call....
       NS_ConvertUTF8toUTF16 workURL(spec);
 
       int32_t loc = workURL.RFindChar('/');
       if (loc >= 0)
@@ -1455,30 +1446,30 @@ nsMsgComposeAndSend::GetMultipartRelated
     if (count > 0)
     {
       // preallocate space for part numbers
       m_partNumbers.SetLength(count);
       // Let parse the list to count the number of valid objects. BTW, we can remove the others from the list
       RefPtr<nsMsgAttachmentData> attachment(new nsMsgAttachmentData);
 
       int32_t i;
-      nsCOMPtr<nsIDOMNode> node;
+      nsCOMPtr<Element> domElement;
 
       for (i = count - 1, count = 0; i >= 0; i --)
       {
         // Reset this structure to null!
 
         // now we need to get the element in the array and do the magic
         // to process this element.
         //
-        node = do_QueryElementAt(mEmbeddedObjectList, i, &rv);
+        domElement = do_QueryElementAt(mEmbeddedObjectList, i, &rv);
         bool acceptObject = false;
-        if (node)
+        if (domElement)
         {
-          rv = GetEmbeddedObjectInfo(node, attachment, &acceptObject);
+          rv = GetEmbeddedObjectInfo(domElement, attachment, &acceptObject);
         }
         else // outlook import case
         {
           nsCOMPtr<nsIMsgEmbeddedImageData> imageData =
             do_QueryElementAt(mEmbeddedObjectList, i, &rv);
           if (!imageData)
             continue;
           acceptObject = true;
@@ -1640,18 +1631,18 @@ nsMsgComposeAndSend::GetBodyFromEditor()
 }
 
 //
 // This is the routine that does the magic of generating the body and the
 // attachments for the multipart/related email message.
 //
 typedef struct
 {
-  nsIDOMNode    *node;
-  char          *url;
+  Element *element;
+  char *url;
 } domSaveStruct;
 
 nsresult
 nsMsgComposeAndSend::ProcessMultipartRelated(int32_t *aMailboxCount, int32_t *aNewsCount)
 {
   uint32_t                  multipartCount = GetMultipartRelatedCount();
   nsresult                  rv = NS_OK;
   uint32_t                  i;
@@ -1669,34 +1660,34 @@ nsMsgComposeAndSend::ProcessMultipartRel
   if (multipartCount > 0)
   {
     domSaveArray = (domSaveStruct *)PR_MALLOC(sizeof(domSaveStruct) * multipartCount);
     if (!domSaveArray)
       return NS_ERROR_MIME_MPART_ATTACHMENT_ERROR;
     memset(domSaveArray, 0, sizeof(domSaveStruct) * multipartCount);
   }
 
-  for (i = mPreloadedAttachmentCount; i < (mPreloadedAttachmentCount + multipartCount);)
+  i = mPreloadedAttachmentCount;
+  while (i < mPreloadedAttachmentCount + multipartCount)
   {
     // Ok, now we need to get the element in the array and do the magic
     // to process this element.
     //
 
     locCount++;
-    nsCOMPtr<nsIDOMNode> node = do_QueryElementAt(mEmbeddedObjectList, locCount);
-    if (node)
+    nsCOMPtr<Element> domElement = do_QueryElementAt(mEmbeddedObjectList, locCount);
+    if (domElement)
     {
       bool acceptObject = false;
-      rv = GetEmbeddedObjectInfo(node, attachment, &acceptObject);
+      rv = GetEmbeddedObjectInfo(domElement, attachment, &acceptObject);
       NS_ENSURE_SUCCESS(rv, NS_ERROR_MIME_MPART_ATTACHMENT_ERROR);
       if (!acceptObject)
         continue;
       nsString nodeValue;
-      nsCOMPtr<nsINode> node2 = do_QueryInterface(node);
-      node2->GetNodeValue(nodeValue);
+      domElement->GetNodeValue(nodeValue);
       LossyCopyUTF16toASCII(nodeValue, m_attachments[i]->m_contentId);
     }
     else
     {
       nsCOMPtr<nsIMsgEmbeddedImageData> imageData = do_QueryElementAt(mEmbeddedObjectList, locCount, &rv);
       if (!imageData)
         return NS_ERROR_MIME_MPART_ATTACHMENT_ERROR;
       imageData->GetUri(getter_AddRefs(attachment->m_url));
@@ -1711,17 +1702,17 @@ nsMsgComposeAndSend::ProcessMultipartRel
     m_attachments[i]->mMHTMLPart = true;
 
     m_attachments[i]->mDeleteFile = true;
     m_attachments[i]->m_done = false;
     m_attachments[i]->SetMimeDeliveryState(this);
     m_attachments[i]->mNodeIndex = locCount;
 
     j++;
-    domSaveArray[j].node = node;
+    domSaveArray[j].element = domElement;
 
     // check if we have alreay attached this object, don't need to attach it twice
     duplicateOf = -1;
     for (k = mPreloadedAttachmentCount; k < i; k++)
     {
       bool isEqual = false;
       NS_ASSERTION(attachment->m_url, "null attachment url!");
       if (attachment->m_url)
@@ -1804,30 +1795,29 @@ nsMsgComposeAndSend::ProcessMultipartRel
       m_attachments[i]->SetMimeDeliveryState(nullptr);
     }
 
     //
     // Ok, while we are here, we should whack the DOM with the generated
     // Content-ID for this object. This will be necessary for generating
     // the HTML we need.
     //
-    nsString domURL;
-    if (!m_attachments[duplicateOf == -1 ? i : duplicateOf]->m_contentId.IsEmpty())
+    if (domSaveArray[j].element &&
+        !m_attachments[duplicateOf == -1 ? i : duplicateOf]->m_contentId.IsEmpty())
     {
       nsString   newSpec(NS_LITERAL_STRING("cid:"));
       newSpec.AppendASCII(m_attachments[duplicateOf == -1 ? i : duplicateOf]->m_contentId.get());
 
-      // Now, we know the types of objects this node can be, so we will do
-      // our query interface here and see what we come up with
-      nsCOMPtr<Element> nodeAsElement = do_QueryInterface(domSaveArray[j].node);
-      RefPtr<HTMLImageElement>  image  = HTMLImageElement::FromContentOrNull(nodeAsElement);
-      RefPtr<HTMLLinkElement>   link   = HTMLLinkElement::FromContentOrNull(nodeAsElement);
-      RefPtr<HTMLAnchorElement> anchor = HTMLAnchorElement::FromContentOrNull(nodeAsElement);
-      RefPtr<HTMLBodyElement>   body   = HTMLBodyElement::FromContentOrNull(nodeAsElement);
-
+      // We know the types of objects this element can be, let's see what we come up with.
+      RefPtr<HTMLImageElement>  image  = HTMLImageElement::FromContent(domSaveArray[j].element);
+      RefPtr<HTMLLinkElement>   link   = HTMLLinkElement::FromContent(domSaveArray[j].element);
+      RefPtr<HTMLAnchorElement> anchor = HTMLAnchorElement::FromContent(domSaveArray[j].element);
+      RefPtr<HTMLBodyElement>   body   = HTMLBodyElement::FromContent(domSaveArray[j].element);
+
+      nsString domURL;
       IgnoredErrorResult rv2;
       if (anchor)
       {
         anchor->GetHref(domURL);
         anchor->SetHref(newSpec, rv2);
       }
       else if (link)
       {
@@ -1854,26 +1844,24 @@ nsMsgComposeAndSend::ProcessMultipartRel
   rv = GetBodyFromEditor();
 
   //
   // Ok, now we need to un-whack the DOM or we have a screwed up document on
   // Send failure.
   //
   for (i = 0; i < multipartCount; i++)
   {
-    if ( (!domSaveArray[i].node) || (!domSaveArray[i].url) )
+    if (!domSaveArray[i].element || !domSaveArray[i].url)
       continue;
 
-    // Now, we know the types of objects this node can be, so we will do
-    // our query interface here and see what we come up with
-    nsCOMPtr<Element> nodeAsElement = do_QueryInterface(domSaveArray[i].node);
-    RefPtr<HTMLImageElement>  image  = HTMLImageElement::FromContentOrNull(nodeAsElement);
-    RefPtr<HTMLLinkElement>   link   = HTMLLinkElement::FromContentOrNull(nodeAsElement);
-    RefPtr<HTMLAnchorElement> anchor = HTMLAnchorElement::FromContentOrNull(nodeAsElement);
-    RefPtr<HTMLBodyElement>   body   = HTMLBodyElement::FromContentOrNull(nodeAsElement);
+    // We know the types of objects this element can be, let's see what we come up with.
+    RefPtr<HTMLImageElement>  image  = HTMLImageElement::FromContent(domSaveArray[i].element);
+    RefPtr<HTMLLinkElement>   link   = HTMLLinkElement::FromContent(domSaveArray[i].element);
+    RefPtr<HTMLAnchorElement> anchor = HTMLAnchorElement::FromContent(domSaveArray[i].element);
+    RefPtr<HTMLBodyElement>   body   = HTMLBodyElement::FromContent(domSaveArray[i].element);
 
       // STRING USE WARNING: hoisting the following conversion might save code-space, since it happens along every path
 
     IgnoredErrorResult rv2;
     if (anchor) {
       anchor->SetHref(NS_ConvertASCIItoUTF16(domSaveArray[i].url), rv2);
     } else if (link) {
       link->SetHref(NS_ConvertASCIItoUTF16(domSaveArray[i].url), rv2);
--- a/mailnews/compose/src/nsMsgSend.h
+++ b/mailnews/compose/src/nsMsgSend.h
@@ -113,17 +113,16 @@
 #include "msgCore.h"
 #include "prprf.h"
 #include "nsIOutputStream.h"
 #include "nsMsgMessageFlags.h"
 #include "nsIURL.h"
 #include "nsMsgAttachmentHandler.h"
 #include "nsMsgCompFields.h"
 #include "nsIMsgSendListener.h"
-#include "nsIDOMNode.h"
 #include "nsIEditor.h"
 #include "nsIUrlListener.h"
 #include "nsIMsgStatusFeedback.h"
 #include "nsIMsgIdentity.h"
 #include "nsIMsgHdr.h"
 #include "nsIMsgIdentity.h"
 #include "nsWeakReference.h"
 #include "nsPIDOMWindow.h"
@@ -156,16 +155,17 @@
 
 //
 // Forward declarations...
 //
 class nsMsgSendPart;
 class nsMsgCopy;
 class nsIPrompt;
 class nsIInterfaceRequestor;
+class mozilla::dom::Element;
 
 namespace mozilla {
 namespace mailnews {
 class MimeEncoder;
 }
 }
 
 class nsMsgComposeAndSend : public nsIMsgSend,
@@ -250,17 +250,17 @@ public:
   nsresult    HackAttachments(nsIArray *attachments,
                               nsIArray *preloaded_attachments);
   nsresult    CountCompFieldAttachments();
   nsresult    AddCompFieldLocalAttachments();
   nsresult    AddCompFieldRemoteAttachments(uint32_t  aStartLocation, int32_t *aMailboxCount, int32_t *aNewsCount);
 
   // Deal with multipart related data
   nsresult    ProcessMultipartRelated(int32_t *aMailboxCount, int32_t *aNewsCount);
-  nsresult    GetEmbeddedObjectInfo(nsIDOMNode *node, nsMsgAttachmentData *attachment, bool *acceptObject);
+  nsresult    GetEmbeddedObjectInfo(mozilla::dom::Element *domElement, nsMsgAttachmentData *attachment, bool *acceptObject);
   uint32_t    GetMultipartRelatedCount(bool forceToBeCalculated = false);
   nsCOMPtr<nsIArray> mEmbeddedObjectList; // it's initialized when calling GetMultipartRelatedCount
 
   // Body processing
   nsresult    SnarfAndCopyBody(const nsACString &attachment1_body,
                                const char  *attachment1_type);
 
   int32_t     PreProcessPart(nsMsgAttachmentHandler  *ma,