Bug 1603649 - Port bug 1594288: Replace use of nsIDocShellTreeItem.findChildWithName. rs=bustage-fix
authorMagnus Melin <mkmelin+mozilla@iki.fi>
Mon, 16 Dec 2019 11:39:13 +0200
changeset 37751 f301cabc38200c413c463fc029bb76a7d1f12ec8
parent 37750 028ba2e01818daee2d1f08e2340b701b8cf16fb2
child 37752 449906178fe3e7d4b6317ba0d5f058d723607ce3
push id397
push userclokep@gmail.com
push dateMon, 10 Feb 2020 21:16:13 +0000
reviewersbustage-fix
bugs1603649, 1594288
Bug 1603649 - Port bug 1594288: Replace use of nsIDocShellTreeItem.findChildWithName. rs=bustage-fix
mailnews/base/src/nsMessenger.cpp
mailnews/base/src/nsMsgPrintEngine.cpp
mailnews/base/src/nsMsgWindow.cpp
mailnews/compose/src/nsMsgComposeService.cpp
--- a/mailnews/base/src/nsMessenger.cpp
+++ b/mailnews/base/src/nsMessenger.cpp
@@ -14,16 +14,17 @@
 #include "nsLocalFile.h"
 #include "nsDirectoryServiceDefs.h"
 #include "nsQuickSort.h"
 #include "nsNativeCharsetUtils.h"
 #include "nsIMutableArray.h"
 #include "mozilla/Path.h"
 #include "mozilla/Services.h"
 #include "mozilla/dom/LoadURIOptionsBinding.h"
+#include "nsQueryObject.h"
 
 // necko
 #include "nsMimeTypes.h"
 #include "nsIURL.h"
 #include "nsIPrompt.h"
 #include "nsIStreamListener.h"
 #include "nsIStreamConverterService.h"
 #include "nsNetUtil.h"
@@ -224,46 +225,43 @@ NS_IMETHODIMP nsMessenger::SetWindow(moz
     mMsgWindow = aMsgWindow;
     mWindow = aWin;
 
     rv = mailSession->AddFolderListener(this, nsIFolderListener::removed);
     NS_ENSURE_SUCCESS(rv, rv);
 
     NS_ENSURE_TRUE(aWin, NS_ERROR_FAILURE);
     nsCOMPtr<nsPIDOMWindowOuter> win = nsPIDOMWindowOuter::From(aWin);
-
-    nsIDocShell *docShell = win->GetDocShell();
-    nsCOMPtr<nsIDocShellTreeItem> docShellAsItem(docShell);
-    NS_ENSURE_TRUE(docShellAsItem, NS_ERROR_FAILURE);
-
-    nsCOMPtr<nsIDocShellTreeItem> rootDocShellAsItem;
-    docShellAsItem->GetInProcessSameTypeRootTreeItem(
-        getter_AddRefs(rootDocShellAsItem));
-
-    nsCOMPtr<nsIDocShellTreeItem> childAsItem;
-    rv = rootDocShellAsItem->FindChildWithName(NS_LITERAL_STRING("messagepane"),
-                                               true, false, nullptr, nullptr,
-                                               getter_AddRefs(childAsItem));
-
-    mDocShell = do_QueryInterface(childAsItem);
-    if (NS_SUCCEEDED(rv) && mDocShell) {
+    nsIDocShell *rootShell = win->GetDocShell();
+    nsTArray<RefPtr<nsIDocShell>> docShells;
+    rootShell->GetAllDocShellsInSubtree(
+        nsIDocShell::typeContent, nsIDocShell::ENUMERATE_FORWARDS, docShells);
+    for (auto &docShell : docShells) {
+      nsCOMPtr<nsIDocShellTreeItem> child = do_QueryObject(docShell);
+      bool childNameEquals = false;
+      child->NameEquals(NS_LITERAL_STRING("messagepane"), &childNameEquals);
+      if (childNameEquals) {
+        mDocShell = do_QueryInterface(child);
+        break;
+      }
+    }
+    if (mDocShell) {
       // Important! Clear out mCurrentDisplayCharset so we reset a default
       // charset on mDocshell the next time we try to load something into it.
       mCurrentDisplayCharset = "";
 
       if (aMsgWindow)
         aMsgWindow->GetTransactionManager(getter_AddRefs(mTxnMgr));
     }
 
-    // we don't always have a message pane, like in the addressbook
+    // We don't always have a message pane, like in the addressbook
     // so if we don't have a docshell, use the one for the app window.
     // we do this so OpenURL() will work.
-    if (!mDocShell) mDocShell = docShell;
-  }  // if aWin
-  else {
+    if (!mDocShell) mDocShell = rootShell;
+  } else {
     // Remove the folder listener if we added it, i.e. if mWindow is non-null
     if (mWindow) {
       rv = mailSession->RemoveFolderListener(this);
       NS_ENSURE_SUCCESS(rv, rv);
     }
 
     mWindow = nullptr;
   }
--- a/mailnews/base/src/nsMsgPrintEngine.cpp
+++ b/mailnews/base/src/nsMsgPrintEngine.cpp
@@ -3,16 +3,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /**
  * nsMsgPrintEngine.cpp provides a DocShell container for use in printing.
  */
 
 #include "nscore.h"
 #include "nsCOMPtr.h"
+#include "nsQueryObject.h"
 
 #include "nsIComponentManager.h"
 
 #include "nsISupports.h"
 
 #include "nsIURI.h"
 
 #include "nsPIDOMWindow.h"
@@ -211,30 +212,31 @@ nsMsgPrintEngine::SetWindow(mozIDOMWindo
 
   mWindow = aWin;
 
   NS_ENSURE_TRUE(mWindow, NS_ERROR_FAILURE);
   nsCOMPtr<nsPIDOMWindowOuter> window = nsPIDOMWindowOuter::From(mWindow);
 
   window->GetDocShell()->SetAppType(nsIDocShell::APP_TYPE_MAIL);
 
-  nsCOMPtr<nsIDocShellTreeItem> docShellAsItem = window->GetDocShell();
-  NS_ENSURE_TRUE(docShellAsItem, NS_ERROR_FAILURE);
-
-  nsCOMPtr<nsIDocShellTreeItem> rootAsItem;
-  docShellAsItem->GetInProcessSameTypeRootTreeItem(getter_AddRefs(rootAsItem));
-
-  nsCOMPtr<nsIDocShellTreeItem> childItem;
-  rootAsItem->FindChildWithName(NS_LITERAL_STRING("content"), true, false,
-                                nullptr, nullptr, getter_AddRefs(childItem));
-
-  mDocShell = do_QueryInterface(childItem);
-
-  if (mDocShell) SetupObserver();
-
+  nsIDocShell *rootShell = window->GetDocShell();
+  nsTArray<RefPtr<nsIDocShell>> docShells;
+  rootShell->GetAllDocShellsInSubtree(
+      nsIDocShell::typeContent, nsIDocShell::ENUMERATE_FORWARDS, docShells);
+  for (auto &docShell : docShells) {
+    nsCOMPtr<nsIDocShellTreeItem> child = do_QueryObject(docShell);
+    bool childNameEquals = false;
+    child->NameEquals(NS_LITERAL_STRING("content"), &childNameEquals);
+    if (childNameEquals) {
+      mDocShell = do_QueryInterface(child);
+      break;
+    }
+  }
+  NS_ENSURE_TRUE(mDocShell, NS_ERROR_FAILURE);
+  SetupObserver();
   return NS_OK;
 }
 
 /* void setParentWindow (in mozIDOMWindowProxy ptr); */
 NS_IMETHODIMP nsMsgPrintEngine::SetParentWindow(mozIDOMWindowProxy *ptr) {
   mParentWindow = ptr;
   return NS_OK;
 }
--- a/mailnews/base/src/nsMsgWindow.cpp
+++ b/mailnews/base/src/nsMsgWindow.cpp
@@ -31,16 +31,17 @@
 #include "nsMsgContentPolicy.h"
 #include "nsComponentManagerUtils.h"
 #include "nsServiceManagerUtils.h"
 #include "nsIAuthPrompt.h"
 #include "nsMsgUtils.h"
 #include "mozilla/TransactionManager.h"
 #include "mozilla/dom/LoadURIOptionsBinding.h"
 #include "mozilla/Components.h"
+#include "nsQueryObject.h"
 
 NS_IMPL_ISUPPORTS(nsMsgWindow, nsIMsgWindow, nsIURIContentListener,
                   nsISupportsWeakReference, nsIMsgWindowTest)
 
 nsMsgWindow::nsMsgWindow() {
   mCharsetOverride = false;
   m_stopped = false;
 }
@@ -65,27 +66,39 @@ NS_IMETHODIMP nsMsgWindow::GetMessageWin
   *aDocShell = nullptr;
   nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mMessageWindowDocShellWeak));
   if (!docShell) {
     // if we don't have a docshell, then we need to look up the message pane
     // docshell
     nsCOMPtr<nsIDocShell> rootShell(do_QueryReferent(mRootDocShellWeak));
     if (rootShell) {
       nsCOMPtr<nsIDocShellTreeItem> msgDocShellItem;
-      if (rootShell)
-        rootShell->FindChildWithName(NS_LITERAL_STRING("messagepane"), true,
-                                     false, nullptr, nullptr,
-                                     getter_AddRefs(msgDocShellItem));
-      NS_ENSURE_TRUE(msgDocShellItem, NS_ERROR_FAILURE);
-      docShell = do_QueryInterface(msgDocShellItem);
+      if (rootShell) {
+        nsTArray<RefPtr<nsIDocShell>> docShells;
+        rootShell->GetAllDocShellsInSubtree(nsIDocShell::typeContent,
+                                            nsIDocShell::ENUMERATE_FORWARDS,
+                                            docShells);
+        for (auto &ds : docShells) {
+          nsCOMPtr<nsIDocShellTreeItem> child = do_QueryObject(ds);
+          bool childNameEquals = false;
+          child->NameEquals(NS_LITERAL_STRING("messagepane"), &childNameEquals);
+          if (childNameEquals) {
+            docShell = do_QueryInterface(child);
+            break;
+          }
+        }
+      }
+      NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
+
       // we don't own mMessageWindowDocShell so don't try to keep a reference to
       // it!
       mMessageWindowDocShellWeak = do_GetWeakReference(docShell);
     }
   }
+  NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
   docShell.forget(aDocShell);
   return NS_OK;
 }
 
 NS_IMETHODIMP nsMsgWindow::CloseWindow() {
   nsCOMPtr<nsIURILoader> dispatcher = mozilla::components::URILoader::Service();
   if (dispatcher)  // on shut down it's possible dispatcher will be null.
     dispatcher->UnRegisterContentListener(this);
--- a/mailnews/compose/src/nsMsgComposeService.cpp
+++ b/mailnews/compose/src/nsMsgComposeService.cpp
@@ -44,16 +44,17 @@
 #include "mozilla/dom/Selection.h"
 #include "nsUTF8Utils.h"
 #include "mozilla/intl/LineBreaker.h"
 #include "mozilla/Services.h"
 #include "mimemoz2.h"
 #include "nsIArray.h"
 #include "nsArrayUtils.h"
 #include "nsIURIMutator.h"
+#include "nsQueryObject.h"
 
 #ifdef MSGCOMP_TRACE_PERFORMANCE
 #  include "mozilla/Logging.h"
 #  include "nsIMsgHdr.h"
 #  include "nsIMsgMessageService.h"
 #  include "nsMsgUtils.h"
 #endif
 
@@ -238,30 +239,36 @@ nsMsgComposeService::GetOrigWindowSelect
   bool replyQuotingSelection;
   rv = prefs->GetBoolPref(PREF_MAILNEWS_REPLY_QUOTING_SELECTION,
                           &replyQuotingSelection);
   NS_ENSURE_SUCCESS(rv, rv);
   if (!replyQuotingSelection) return NS_ERROR_ABORT;
 
   // Now delve down in to the message to get the HTML representation of the
   // selection
-  nsCOMPtr<nsIDocShell> rootDocShell;
-  rv = aMsgWindow->GetRootDocShell(getter_AddRefs(rootDocShell));
+  nsCOMPtr<nsIDocShell> rootShell;
+  rv = aMsgWindow->GetRootDocShell(getter_AddRefs(rootShell));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCOMPtr<nsIDocShellTreeItem> childAsItem;
-  rv = rootDocShell->FindChildWithName(NS_LITERAL_STRING("messagepane"), true,
-                                       false, nullptr, nullptr,
-                                       getter_AddRefs(childAsItem));
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsCOMPtr<nsIDocShell> messagePaneDocShell;
+  nsTArray<RefPtr<nsIDocShell>> docShells;
+  rootShell->GetAllDocShellsInSubtree(
+      nsIDocShell::typeContent, nsIDocShell::ENUMERATE_FORWARDS, docShells);
+  for (auto &docShell : docShells) {
+    nsCOMPtr<nsIDocShellTreeItem> child = do_QueryObject(docShell);
+    bool childNameEquals = false;
+    child->NameEquals(NS_LITERAL_STRING("messagepane"), &childNameEquals);
+    if (childNameEquals) {
+      messagePaneDocShell = do_QueryInterface(child);
+      break;
+    }
+  }
+  NS_ENSURE_TRUE(messagePaneDocShell, NS_ERROR_FAILURE);
 
-  nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(childAsItem, &rv));
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  nsCOMPtr<mozIDOMWindowProxy> domWindow(do_GetInterface(childAsItem));
+  nsCOMPtr<mozIDOMWindowProxy> domWindow(do_GetInterface(messagePaneDocShell));
   NS_ENSURE_TRUE(domWindow, NS_ERROR_FAILURE);
   nsCOMPtr<nsPIDOMWindowOuter> privateWindow =
       nsPIDOMWindowOuter::From(domWindow);
   RefPtr<mozilla::dom::Selection> sel = privateWindow->GetSelection();
   NS_ENSURE_TRUE(sel, NS_ERROR_FAILURE);
 
   bool requireMultipleWords = true;
   nsAutoCString charsOnlyIf;
@@ -301,17 +308,17 @@ nsMsgComposeService::GetOrigWindowSelect
 
     if (!charsOnlyIf.IsEmpty()) {
       if (MsgFindCharInSet(selPlain, charsOnlyIf.get()) < 0)
         return NS_ERROR_ABORT;
     }
   }
 
   nsCOMPtr<nsIContentViewer> contentViewer;
-  rv = docShell->GetContentViewer(getter_AddRefs(contentViewer));
+  rv = messagePaneDocShell->GetContentViewer(getter_AddRefs(contentViewer));
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<mozilla::dom::Document> domDocument(contentViewer->GetDocument());
 
   nsCOMPtr<nsIDocumentEncoder> docEncoder = do_createHTMLCopyEncoder();
 
   rv = docEncoder->Init(domDocument, NS_LITERAL_STRING("text/html"), 0);
   NS_ENSURE_SUCCESS(rv, rv);