Bug 686941: Make nsIDocument::mCachedRootElement a weak pointer. r=smaug

This commit is contained in:
Kyle Huey 2011-09-29 12:06:35 -04:00
Родитель 0ba68de373
Коммит 99f348a3f5
2 изменённых файлов: 12 добавлений и 13 удалений

Просмотреть файл

@ -527,13 +527,8 @@ public:
/**
* Return the root element for this document.
*/
Element *GetRootElement() const
{
return (mCachedRootElement &&
mCachedRootElement->GetNodeParent() == this) ?
reinterpret_cast<Element*>(mCachedRootElement.get()) :
GetRootElementInternal();
}
Element *GetRootElement() const;
protected:
virtual Element *GetRootElementInternal() const = 0;
@ -1644,9 +1639,7 @@ protected:
nsIDocument* mParentDocument;
// A reference to the element last returned from GetRootElement().
// This should be an Element, but that would force us to pull in
// Element.h and therefore nsIContent.h.
nsCOMPtr<nsINode> mCachedRootElement;
mozilla::dom::Element* mCachedRootElement;
// We'd like these to be nsRefPtrs, but that'd require us to include
// additional headers that we don't want to expose.

Просмотреть файл

@ -1825,7 +1825,6 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(nsDocument)
}
// Traverse all nsIDocument pointer members.
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mCachedRootElement)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mSecurityInfo)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mDisplayDocument)
@ -1906,7 +1905,7 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsDocument)
tmp->mFirstChild = nsnull;
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mXPathEvaluatorTearoff)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mCachedRootElement)
tmp->mCachedRootElement = nsnull; // Avoid a dangling pointer
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mDisplayDocument)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mFirstBaseNodeWithHref)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mDOMImplementation)
@ -3396,6 +3395,13 @@ nsDocument::NodeName(nsAString& aNodeName)
aNodeName.AssignLiteral("#document");
}
Element*
nsIDocument::GetRootElement() const
{
return (mCachedRootElement && mCachedRootElement->GetNodeParent() == this) ?
mCachedRootElement : GetRootElementInternal();
}
Element*
nsDocument::GetRootElementInternal() const
{
@ -3405,7 +3411,7 @@ nsDocument::GetRootElementInternal() const
for (i = mChildren.ChildCount(); i > 0; --i) {
nsIContent* child = mChildren.ChildAt(i - 1);
if (child->IsElement()) {
const_cast<nsDocument*>(this)->mCachedRootElement = child;
const_cast<nsDocument*>(this)->mCachedRootElement = child->AsElement();
return child->AsElement();
}
}