From 2e299693a9633a56ad9bf06f3c720f2687645e68 Mon Sep 17 00:00:00 2001 From: "tbogard%aol.net" Date: Sun, 12 Dec 1999 10:15:26 +0000 Subject: [PATCH] Implemented changes to nsIDocShellTreeItem and nsIDocShellTreeNode. FindItemWithName and FindChildWithName. Work to fix bug 21287. r=shaver a=shaver. git-svn-id: svn://10.0.0.236/trunk@55921 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/docshell/base/nsDocShell.cpp | 104 ++++++++++++++++++++++----- mozilla/docshell/base/nsDocShell.h | 6 +- 2 files changed, 90 insertions(+), 20 deletions(-) diff --git a/mozilla/docshell/base/nsDocShell.cpp b/mozilla/docshell/base/nsDocShell.cpp index 0ae958b4d4f..d5de6b61384 100644 --- a/mozilla/docshell/base/nsDocShell.cpp +++ b/mozilla/docshell/base/nsDocShell.cpp @@ -60,7 +60,9 @@ nsDocShell::nsDocShell() : mContentListener(nsnull), mItemType(typeContent), mMarginWidth(0), - mMarginHeight(0) + mMarginHeight(0), + mParent(nsnull), + mTreeOwner(nsnull) { NS_INIT_REFCNT(); } @@ -552,19 +554,76 @@ NS_IMETHODIMP nsDocShell::GetSameTypeRootTreeItem(nsIDocShellTreeItem** aRootTre return NS_OK; } +NS_IMETHODIMP nsDocShell::FindItemWithName(const PRUnichar *aName, + nsIDocShellTreeItem* aRequestor, nsIDocShellTreeItem **_retval) +{ + NS_ENSURE_ARG(aName); + NS_ENSURE_ARG_POINTER(_retval); + + *_retval = nsnull; // if we don't find one, we return NS_OK and a null result + + // First we check our children making sure not to ask a child if it + // is the aRequestor. + NS_ENSURE_SUCCESS(FindChildWithName(aName, PR_TRUE, aRequestor, _retval), + NS_ERROR_FAILURE); + if(*_retval) + return NS_OK; + + // Second if we have a parent and it isn't the requestor then we should ask + // it to do the search. If it is the requestor we should just stop here + // and let the parent do the rest. + // If we don't have a parent, then we should ask the docShellTreeOwner to do + // the search. + if(mParent) + { + if(mParent == aRequestor) + return NS_OK; + + PRInt32 parentType; + mParent->GetItemType(&parentType); + if(parentType == mItemType) + { + NS_ENSURE_SUCCESS(mParent->FindItemWithName(aName, + NS_STATIC_CAST(nsIDocShellTreeItem*, this), _retval), + NS_ERROR_FAILURE); + return NS_OK; + } + // If the parent isn't of the same type fall through and ask tree owner. + } + + if(mTreeOwner) + { + NS_ENSURE_SUCCESS(mTreeOwner->FindItemWithName(aName, + NS_STATIC_CAST(nsIDocShellTreeItem*, this), _retval), + NS_ERROR_FAILURE); + } + + return NS_OK; +} + NS_IMETHODIMP nsDocShell::GetTreeOwner(nsIDocShellTreeOwner** aTreeOwner) { NS_ENSURE_ARG_POINTER(aTreeOwner); - //XXXIMPL Implement this! - NS_WARN_IF_FALSE(PR_FALSE, "Not Implemented"); + *aTreeOwner = mTreeOwner; + NS_IF_ADDREF(*aTreeOwner); return NS_OK; } NS_IMETHODIMP nsDocShell::SetTreeOwner(nsIDocShellTreeOwner* aTreeOwner) { - //XXXIMPL Implement this! - NS_WARN_IF_FALSE(PR_FALSE, "Not Implemented"); + mTreeOwner = aTreeOwner; // Weak reference per API + + PRInt32 i, n = mChildren.Count(); + for(i = 0; i < n; i++) + { + nsIDocShellTreeItem* child = (nsIDocShellTreeItem*) mChildren.ElementAt(i); // doesn't addref the result + NS_ENSURE_TRUE(child, NS_ERROR_FAILURE); + PRInt32 childType = ~mItemType; // Set it to not us in case the get fails + child->GetItemType(&childType); // We don't care if this fails, if it does we won't set the owner + if(childType == mItemType) + child->SetTreeOwner(aTreeOwner); + } return NS_OK; } @@ -649,17 +708,19 @@ NS_IMETHODIMP nsDocShell::GetChildAt(PRInt32 aIndex, nsIDocShellTreeItem** aChil return NS_OK; } -/* depth-first search for a child shell with aName */ -NS_IMETHODIMP nsDocShell::FindChildWithName(const PRUnichar *aName, nsIDocShellTreeItem **_retval) +NS_IMETHODIMP nsDocShell::FindChildWithName(const PRUnichar *aName, + PRBool aRecurse, nsIDocShellTreeItem* aRequestor, + nsIDocShellTreeItem **_retval) { NS_ENSURE_ARG(aName); NS_ENSURE_ARG_POINTER(_retval); *_retval = nsnull; // if we don't find one, we return NS_OK and a null result + nsAutoString name(aName); nsXPIDLString childName; PRInt32 i, n = mChildren.Count(); - for (i = 0; i < n; i++) + for(i = 0; i < n; i++) { nsIDocShellTreeItem* child = (nsIDocShellTreeItem*) mChildren.ElementAt(i); // doesn't addref the result NS_ENSURE_TRUE(child, NS_ERROR_FAILURE); @@ -670,22 +731,28 @@ NS_IMETHODIMP nsDocShell::FindChildWithName(const PRUnichar *aName, nsIDocShellT NS_ADDREF(child); break; } - PRInt32 childType; - child->GetItemType(&childType); - if(childType == mItemType) //Only ask it to check children if it is same type + if(aRecurse && (aRequestor != child)) // Only ask the child if it isn't the requestor { - // See if child contains the shell with the given name - nsCOMPtr childAsNode = do_QueryInterface(child); - if(child) + PRInt32 childType; + child->GetItemType(&childType); + + if(childType == mItemType) //Only ask it to check children if it is same type { - NS_ENSURE_SUCCESS(childAsNode->FindChildWithName(name.GetUnicode(), _retval), NS_ERROR_FAILURE); + // See if child contains the shell with the given name + nsCOMPtr childAsNode(do_QueryInterface(child)); + if(child) + { + NS_ENSURE_SUCCESS(childAsNode->FindChildWithName(aName, PR_TRUE, + NS_STATIC_CAST(nsIDocShellTreeItem*, this), _retval), + NS_ERROR_FAILURE); + } } } - if (*_retval) // found it - break; + if(*_retval) // found it + return NS_OK; } - return NS_OK; + return NS_OK; } //***************************************************************************** @@ -904,6 +971,7 @@ NS_IMETHODIMP nsDocShell::GetParentWidget(nsIWidget** parentWidget) NS_ENSURE_ARG_POINTER(parentWidget); *parentWidget = mParentWidget; + NS_IF_ADDREF(*parentWidget); return NS_OK; } diff --git a/mozilla/docshell/base/nsDocShell.h b/mozilla/docshell/base/nsDocShell.h index a10a69caa8a..7d449f3f1bc 100644 --- a/mozilla/docshell/base/nsDocShell.h +++ b/mozilla/docshell/base/nsDocShell.h @@ -33,8 +33,10 @@ #include "nsIScrollableView.h" #include "nsIContentViewer.h" #include "nsIPref.h" +#include "nsVoidArray.h" #include "nsCDocShell.h" +#include "nsIDocShellTreeOwner.h" #include "nsIContentViewerContainer.h" #include "nsIDocumentLoader.h" @@ -46,7 +48,6 @@ #include "nsDSURIContentListener.h" -#include "nsVoidArray.h" class nsDocShellInitInfo { @@ -151,7 +152,8 @@ protected: parent thus a cycle. A weak reference would work, but not required as the interface states a requirement to zero out the parent when the parent is releasing the interface.*/ - nsIDocShellTreeItem* mParent; + nsIDocShellTreeItem* mParent; // Weak Reference + nsIDocShellTreeOwner* mTreeOwner; // Weak Reference };