Bug 425551 - "Fix async DOM events from keeping link elements alive past unlink". r+sr=sicking, a=beltzner.

This commit is contained in:
2008-04-11 10:29:06 -07:00
parent 388dde8ab4
commit 87036333c7
34 changed files with 73 additions and 62 deletions

View File

@@ -1063,45 +1063,6 @@ private:
NS_DEFINE_STATIC_IID_ACCESSOR(nsIDocument, NS_IDOCUMENT_IID)
/**
* Helper class to automatically handle batching of document updates. This
* class will call BeginUpdate on construction and EndUpdate on destruction on
* the given document with the given update type. The document could be null,
* in which case no updates will be called. The constructor also takes a
* boolean that can be set to false to prevent notifications.
*/
class mozAutoDocUpdate
{
public:
mozAutoDocUpdate(nsIDocument* aDocument, nsUpdateType aUpdateType,
PRBool aNotify) :
mDocument(aNotify ? aDocument : nsnull),
mUpdateType(aUpdateType)
{
if (mDocument) {
mDocument->BeginUpdate(mUpdateType);
}
}
~mozAutoDocUpdate()
{
if (mDocument) {
mDocument->EndUpdate(mUpdateType);
}
}
private:
nsCOMPtr<nsIDocument> mDocument;
nsUpdateType mUpdateType;
};
#define MOZ_AUTO_DOC_UPDATE_PASTE2(tok,line) tok##line
#define MOZ_AUTO_DOC_UPDATE_PASTE(tok,line) \
MOZ_AUTO_DOC_UPDATE_PASTE2(tok,line)
#define MOZ_AUTO_DOC_UPDATE(doc,type,notify) \
mozAutoDocUpdate MOZ_AUTO_DOC_UPDATE_PASTE(_autoDocUpdater_, __LINE__) \
(doc,type,notify)
/**
* mozAutoSubtreeModified batches DOM mutations so that a DOMSubtreeModified
* event is dispatched, if necessary, when the outermost mozAutoSubtreeModified

View File

@@ -102,6 +102,7 @@ EXPORTS = \
nsStubImageDecoderObserver.h \
nsStubMutationObserver.h \
nsTextFragment.h \
mozAutoDocUpdate.h \
$(NULL)
CPPSRCS = \

View File

@@ -52,9 +52,6 @@ public:
if (mDocument) {
mDocument->BeginUpdate(mUpdateType);
}
else {
nsContentUtils::AddScriptBlocker();
}
}
~mozAutoDocUpdate()
@@ -62,9 +59,6 @@ public:
if (mDocument) {
mDocument->EndUpdate(mUpdateType);
}
else {
nsContentUtils::RemoveScriptBlocker();
}
}
private:

View File

@@ -642,6 +642,7 @@ nsAttrAndChildArray::Clear()
ATTRS(mImpl)[i].~InternalAttr();
}
nsAutoScriptBlocker scriptBlocker;
PRUint32 end = slotCount * ATTRSIZE + ChildCount();
for (i = slotCount * ATTRSIZE; i < end; ++i) {
nsIContent* child = static_cast<nsIContent*>(mImpl->mBuffer[i]);

View File

@@ -93,6 +93,7 @@
#include "nsThreadUtils.h"
#include "nsPresShellIterator.h"
#include "nsPIDOMWindow.h"
#include "mozAutoDocUpdate.h"
PRLogModuleInfo* gContentSinkLogModuleInfo;

View File

@@ -162,6 +162,8 @@ static NS_DEFINE_CID(kXTFServiceCID, NS_XTFSERVICE_CID);
#include "nsIScriptError.h"
#include "nsIConsoleService.h"
#include "mozAutoDocUpdate.h"
const char kLoadAsData[] = "loadAsData";
static const char kJSStackContractID[] = "@mozilla.org/js/xpc/ContextStack;1";

View File

@@ -160,6 +160,8 @@ static NS_DEFINE_CID(kDOMEventGroupCID, NS_DOMEVENTGROUP_CID);
#include "nsFrameLoader.h"
#include "mozAutoDocUpdate.h"
#ifdef MOZ_LOGGING
// so we can get logging even in release builds
#define FORCE_PR_LOG 1
@@ -831,6 +833,8 @@ nsDocument::~nsDocument()
// links one by one
DestroyLinkMap();
nsAutoScriptBlocker scriptBlocker;
PRInt32 indx; // must be signed
PRUint32 count = mChildren.ChildCount();
for (indx = PRInt32(count) - 1; indx >= 0; --indx) {
@@ -1085,6 +1089,8 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsDocument)
// from the doc.
tmp->DestroyLinkMap();
nsAutoScriptBlocker scriptBlocker;
// Unlink the mChildren nsAttrAndChildArray.
for (PRInt32 indx = PRInt32(tmp->mChildren.ChildCount()) - 1;
indx >= 0; --indx) {

View File

@@ -62,6 +62,7 @@
#include "nsNodeUtils.h"
#include "nsBindingManager.h"
#include "nsCCUncollectableMarker.h"
#include "mozAutoDocUpdate.h"
#include "pldhash.h"
#include "prprf.h"

View File

@@ -138,6 +138,8 @@
#include "nsCycleCollectionParticipant.h"
#include "nsCCUncollectableMarker.h"
#include "mozAutoDocUpdate.h"
#ifdef MOZ_SVG
PRBool NS_SVG_TestFeature(const nsAString &fstr);
#endif /* MOZ_SVG */
@@ -2043,7 +2045,7 @@ nsGenericElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
NS_PRECONDITION(!IsNativeAnonymous() || aBindingParent == this,
"Native anonymous content must have itself as its "
"own binding parent");
if (!aBindingParent && aParent) {
aBindingParent = aParent->GetBindingParent();
}
@@ -3465,13 +3467,17 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsGenericElement)
// Unlink child content (and unbind our subtree).
{
PRUint32 i;
PRUint32 kids = tmp->mAttrsAndChildren.ChildCount();
for (i = kids; i > 0; i--) {
// We could probably do a non-deep unbind here when IsInDoc is false
// for better performance.
tmp->mAttrsAndChildren.ChildAt(i-1)->UnbindFromTree();
tmp->mAttrsAndChildren.RemoveChildAt(i-1);
PRUint32 childCount = tmp->mAttrsAndChildren.ChildCount();
if (childCount) {
// Don't allow script to run while we're unbinding everything.
nsAutoScriptBlocker scriptBlocker;
while (childCount-- > 0) {
// Once we have XPCOMGC we shouldn't need to call UnbindFromTree.
// We could probably do a non-deep unbind here when IsInDoc is false
// for better performance.
tmp->mAttrsAndChildren.ChildAt(childCount)->UnbindFromTree();
tmp->mAttrsAndChildren.RemoveChildAt(childCount);
}
}
}

View File

@@ -77,6 +77,8 @@
#include "nsEventDispatcher.h"
#include "nsDOMClassInfo.h"
#include "mozAutoDocUpdate.h"
#ifdef DEBUG_chb
static void PrintReqURL(imgIRequest* req) {
if (!req) {

View File

@@ -83,6 +83,7 @@
#include "nsFrameLoader.h"
#include "nsObjectLoadingContent.h"
#include "mozAutoDocUpdate.h"
static NS_DEFINE_CID(kCPluginManagerCID, NS_PLUGINMANAGER_CID);

View File

@@ -66,6 +66,7 @@ public:
NS_IMETHOD Run();
nsresult PostDOMEvent();
nsresult RunDOMEventWhenSafe();
nsCOMPtr<nsIDOMNode> mEventNode;
nsCOMPtr<nsIDOMEvent> mEvent;

View File

@@ -41,6 +41,7 @@
#include "nsIDOMDocument.h"
#include "nsIDOMDocumentEvent.h"
#include "nsIDOMEventTarget.h"
#include "nsContentUtils.h"
NS_IMETHODIMP nsPLDOMEvent::Run()
{
@@ -76,3 +77,8 @@ nsresult nsPLDOMEvent::PostDOMEvent()
{
return NS_DispatchToCurrentThread(this);
}
nsresult nsPLDOMEvent::RunDOMEventWhenSafe()
{
return nsContentUtils::AddScriptRunner(this) ? NS_OK : NS_ERROR_FAILURE;
}

View File

@@ -110,6 +110,7 @@
#include "nsEventDispatcher.h"
#include "nsLayoutUtils.h"
#include "nsContentCreatorFunctions.h"
#include "mozAutoDocUpdate.h"
class nsINodeInfo;
class nsIDOMNodeList;

View File

@@ -87,6 +87,8 @@
#include "nsUnicharUtils.h"
#include "nsEventDispatcher.h"
#include "mozAutoDocUpdate.h"
static const int NS_FORM_CONTROL_LIST_HASHTABLE_SIZE = 16;
class nsFormControlList;

View File

@@ -42,6 +42,7 @@
#include "nsPresContext.h"
#include "nsMappedAttributes.h"
#include "nsRuleData.h"
#include "mozAutoDocUpdate.h"
class nsHTMLHeadingElement : public nsGenericHTMLElement,
public nsIDOMHTMLHeadingElement

View File

@@ -111,6 +111,8 @@
#include "nsImageLoadingContent.h"
#include "nsIDOMWindowInternal.h"
#include "mozAutoDocUpdate.h"
// XXX align=left, hspace, vspace, border? other nav4 attrs
static NS_DEFINE_CID(kXULControllersCID, NS_XULCONTROLLERS_CID);

View File

@@ -209,10 +209,6 @@ nsHTMLLinkElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
UpdateStyleSheetInternal(nsnull);
// XXXbz we really shouldn't fire the event until after we've finished with
// the outermost BindToTree... In particular, this can effectively cause us
// to reenter this code, or for some part of the document to become unbound
// inside the event!
CreateAndDispatchEvent(aDocument, NS_LITERAL_STRING("DOMLinkAdded"));
return rv;
@@ -243,10 +239,8 @@ nsHTMLLinkElement::UnbindFromTree(PRBool aDeep, PRBool aNullParent)
mLinkState = eLinkState_Unknown;
}
// XXXbz we really shouldn't fire the event until after we've finished with
// the outermost UnbindFromTree... In particular, this can effectively cause
// us to reenter this code, or to be bound to a different tree inside the
// event!
// Once we have XPCOMGC we shouldn't need to call UnbindFromTree during Unlink
// and so this messy event dispatch can go away.
CreateAndDispatchEvent(oldDoc, NS_LITERAL_STRING("DOMLinkRemoved"));
nsGenericHTMLElement::UnbindFromTree(aDeep, aNullParent);
UpdateStyleSheetInternal(oldDoc);
@@ -276,7 +270,13 @@ nsHTMLLinkElement::CreateAndDispatchEvent(nsIDocument* aDoc,
nsRefPtr<nsPLDOMEvent> event = new nsPLDOMEvent(this, aEventName);
if (event) {
event->PostDOMEvent();
// If we have script blockers on the stack then we want to run as soon as
// they are removed. Otherwise punt the runable to the event loop as we
// don't know when it will be safe to run script.
if (nsContentUtils::IsSafeToRunScript())
event->PostDOMEvent();
else
event->RunDOMEventWhenSafe();
}
}

View File

@@ -69,6 +69,7 @@
#include "nsIDocument.h"
#include "nsIDOMDocument.h"
#include "nsContentCreatorFunctions.h"
#include "mozAutoDocUpdate.h"
/**
* Implementation of &lt;option&gt;

View File

@@ -75,6 +75,7 @@
#include "nsLayoutErrors.h"
#include "nsStubMutationObserver.h"
#include "nsDOMError.h"
#include "mozAutoDocUpdate.h"
static NS_DEFINE_CID(kXULControllersCID, NS_XULCONTROLLERS_CID);

View File

@@ -121,6 +121,7 @@
#include "nsIElementObserver.h"
#include "nsNodeInfoManager.h"
#include "nsContentCreatorFunctions.h"
#include "mozAutoDocUpdate.h"
#ifdef NS_DEBUG
static PRLogModuleInfo* gSinkLogModuleInfo;

View File

@@ -140,6 +140,7 @@
#include "nsIEditorStyleSheets.h"
#include "nsIInlineSpellChecker.h"
#include "nsRange.h"
#include "mozAutoDocUpdate.h"
#define NS_MAX_DOCUMENT_WRITE_DEPTH 20

View File

@@ -51,6 +51,7 @@
#include "nsIPresShell.h"
#include "nsPresContext.h"
#include "nsDOMClassInfoID.h"
#include "mozAutoDocUpdate.h"
//----------------------------------------------------------------------
// nsISupports methods:

View File

@@ -65,6 +65,7 @@ NS_IMPL_NS_NEW_SVG_ELEMENT(Use)
NS_IMPL_CYCLE_COLLECTION_CLASS(nsSVGUseElement)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsSVGUseElement,
nsSVGUseElementBase)
nsAutoScriptBlocker scriptBlocker;
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOriginal)
tmp->DestroyAnonymousContent();
tmp->RemoveListener();

View File

@@ -345,6 +345,8 @@ nsXBLBinding::InstallAnonymousContent(nsIContent* aAnonParent, nsIContent* aElem
nsIDocument* doc = aElement->GetCurrentDoc();
PRBool allowScripts = AllowScripts();
nsAutoScriptBlocker scriptBlocker;
PRUint32 childCount = aAnonParent->GetChildCount();
for (PRUint32 i = 0; i < childCount; i++) {
nsIContent *child = aAnonParent->GetChildAt(i);
@@ -1139,6 +1141,7 @@ nsXBLBinding::ChangeDocument(nsIDocument* aOldDocument, nsIDocument* aNewDocumen
nsCOMPtr<nsIXULDocument> xuldoc(do_QueryInterface(aOldDocument));
#endif
nsAutoScriptBlocker scriptBlocker;
anonymous->UnbindFromTree(); // Kill it.
#ifdef MOZ_XUL

View File

@@ -37,6 +37,7 @@
* ***** END LICENSE BLOCK ***** */
#include "nsXBLInsertionPoint.h"
#include "nsContentUtils.h"
nsXBLInsertionPoint::nsXBLInsertionPoint(nsIContent* aParentElement,
PRUint32 aIndex,
@@ -125,6 +126,8 @@ nsXBLInsertionPoint::UnbindDefaultContent()
// Hold a strong ref while doing this, just in case
nsCOMPtr<nsIContent> defContent = mDefaultContent;
nsAutoScriptBlocker scriptBlocker;
// Unbind the _kids_ of the default content, not just the default content
// itself, since they are bound to some other parent. Basically we want to
// undo the mess that InstallAnonymousContent created.

View File

@@ -141,6 +141,7 @@ class nsXBLInsertionPointEntry {
public:
~nsXBLInsertionPointEntry() {
if (mDefaultContent) {
nsAutoScriptBlocker scriptBlocker;
// mDefaultContent is a sort of anonymous content within the XBL
// document, and we own and manage it. Unhook it here, since we're going
// away.
@@ -248,6 +249,7 @@ NS_IMPL_CYCLE_COLLECTION_CLASS(nsXBLInsertionPointEntry)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_NATIVE(nsXBLInsertionPointEntry)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mInsertionParent)
if (tmp->mDefaultContent) {
nsAutoScriptBlocker scriptBlocker;
// mDefaultContent is a sort of anonymous content within the XBL
// document, and we own and manage it. Unhook it here, since we're going
// away.
@@ -1285,6 +1287,7 @@ nsXBLPrototypeBinding::ConstructInsertionTable(nsIContent* aContent)
// in situations where no content ends up being placed at the insertion point.
PRUint32 defaultCount = child->GetChildCount();
if (defaultCount > 0) {
nsAutoScriptBlocker scriptBlocker;
// Annotate the insertion point with our default content.
xblIns->SetDefaultContent(child);

View File

@@ -96,6 +96,7 @@
#include "nsNodeUtils.h"
#include "nsIScriptGlobalObject.h"
#include "nsEventDispatcher.h"
#include "mozAutoDocUpdate.h"
#ifdef MOZ_SVG
#include "nsGUIEvent.h"

View File

@@ -62,6 +62,7 @@
#include "nsIProgrammingLanguage.h"
#include "nsIXPConnect.h"
#include "nsXTFWeakTearoff.h"
#include "mozAutoDocUpdate.h"
nsXTFElementWrapper::nsXTFElementWrapper(nsINodeInfo* aNodeInfo,
nsIXTFElement* aXTFElement)

View File

@@ -151,6 +151,7 @@
#include "nsXBLBinding.h"
#include "nsEventDispatcher.h"
#include "nsPresShellIterator.h"
#include "mozAutoDocUpdate.h"
/**
* Three bits are used for XUL Element's lazy state.

View File

@@ -66,6 +66,7 @@
#include "nsContentUtils.h"
#include "nsAttrName.h"
#include "nsNodeUtils.h"
#include "mozAutoDocUpdate.h"
#include "jsapi.h"
#include "pldhash.h"

View File

@@ -75,6 +75,7 @@
#include "nsIContent.h"
#include "nsDisplayList.h"
#include "nsNodeUtils.h"
#include "mozAutoDocUpdate.h"
// masks for mEdgeVisibility
#define LEFT_VIS 0x0001

View File

@@ -76,6 +76,7 @@
#include "nsContentUtils.h"
#include "nsContentErrors.h"
#include "mozAutoDocUpdate.h"
#define NS_IF_CLONE(member_) \
PR_BEGIN_MACRO \

View File

@@ -74,6 +74,7 @@
#include "nsContentUtils.h"
#include "nsIJSContextStack.h"
#include "nsIScriptSecurityManager.h"
#include "mozAutoDocUpdate.h"
// -------------------------------
// Style Rule List for the DOM