From fe4e09853e46156e533d71a939650676605543bd Mon Sep 17 00:00:00 2001 From: "Olli.Pettay%helsinki.fi" Date: Fri, 19 May 2006 10:26:44 +0000 Subject: [PATCH] Bug 338122, Crash [@ nsIMEStateManager::IsActive] when window gets removed on mousedown at button, r+sr=roc git-svn-id: svn://10.0.0.236/trunk@197990 18797224-902f-48f8-a5cc-f745e15eee43 --- .../events/src/nsEventStateManager.cpp | 4 ++-- .../content/events/src/nsIMEStateManager.cpp | 16 ++++++-------- .../html/content/src/nsGenericHTMLElement.cpp | 2 +- mozilla/layout/base/nsDocumentViewer.cpp | 14 +++++++++---- mozilla/layout/base/nsPresContext.cpp | 21 +++++-------------- mozilla/layout/base/nsPresContext.h | 14 ++++++++++--- mozilla/layout/base/nsPresShell.cpp | 3 ++- .../layout/forms/nsComboboxControlFrame.cpp | 2 +- mozilla/layout/generic/nsGfxScrollFrame.cpp | 4 ++-- mozilla/layout/html/tests/Makefile.in | 1 + mozilla/layout/printing/nsPrintEngine.cpp | 3 ++- mozilla/layout/style/nsCSSDataBlock.cpp | 6 +++--- mozilla/layout/style/nsCSSRules.cpp | 2 +- mozilla/layout/style/nsStyleUtil.cpp | 11 ++-------- 14 files changed, 49 insertions(+), 54 deletions(-) diff --git a/mozilla/content/events/src/nsEventStateManager.cpp b/mozilla/content/events/src/nsEventStateManager.cpp index 95eaa2ef499..daac2ebc25b 100644 --- a/mozilla/content/events/src/nsEventStateManager.cpp +++ b/mozilla/content/events/src/nsEventStateManager.cpp @@ -4091,7 +4091,7 @@ nsEventStateManager::SendFocusBlur(nsPresContext* aPresContext, { // Keep a ref to presShell since dispatching the DOM event may cause // the document to be destroyed. - nsCOMPtr presShell = aPresContext->PresShell(); + nsCOMPtr presShell = aPresContext->GetPresShell(); if (!presShell) return NS_OK; @@ -4546,7 +4546,7 @@ void nsEventStateManager::EnsureDocument(nsPresContext* aPresContext) { if (!mDocument) - EnsureDocument(aPresContext->PresShell()); + mDocument = aPresContext->Document(); } void diff --git a/mozilla/content/events/src/nsIMEStateManager.cpp b/mozilla/content/events/src/nsIMEStateManager.cpp index 2edcecaf32c..5ee366a8448 100755 --- a/mozilla/content/events/src/nsIMEStateManager.cpp +++ b/mozilla/content/events/src/nsIMEStateManager.cpp @@ -161,8 +161,7 @@ nsresult nsIMEStateManager::OnActivate(nsPresContext* aPresContext) { NS_ENSURE_ARG_POINTER(aPresContext); - NS_ENSURE_TRUE(aPresContext->GetDocument(), NS_ERROR_FAILURE); - sActiveWindow = aPresContext->GetDocument()->GetWindow(); + sActiveWindow = aPresContext->Document()->GetWindow(); NS_ENSURE_TRUE(sActiveWindow, NS_ERROR_FAILURE); sActiveWindow = sActiveWindow->GetPrivateRoot(); return NS_OK; @@ -172,10 +171,9 @@ nsresult nsIMEStateManager::OnDeactivate(nsPresContext* aPresContext) { NS_ENSURE_ARG_POINTER(aPresContext); - NS_ENSURE_TRUE(aPresContext->GetDocument(), NS_ERROR_FAILURE); - NS_ENSURE_TRUE(aPresContext->GetDocument()->GetWindow(), NS_ERROR_FAILURE); + NS_ENSURE_TRUE(aPresContext->Document()->GetWindow(), NS_ERROR_FAILURE); if (sActiveWindow == - aPresContext->GetDocument()->GetWindow()->GetPrivateRoot()) + aPresContext->Document()->GetWindow()->GetPrivateRoot()) sActiveWindow = nsnull; return NS_OK; } @@ -183,8 +181,8 @@ nsIMEStateManager::OnDeactivate(nsPresContext* aPresContext) PRBool nsIMEStateManager::IsActive(nsPresContext* aPresContext) { - NS_ENSURE_TRUE(aPresContext->GetDocument(), PR_FALSE); - nsPIDOMWindow* window = aPresContext->GetDocument()->GetWindow(); + NS_ENSURE_ARG_POINTER(aPresContext); + nsPIDOMWindow* window = aPresContext->Document()->GetWindow(); NS_ENSURE_TRUE(window, PR_FALSE); if (!sActiveWindow || sActiveWindow != window->GetPrivateRoot()) { // This root window is not active. @@ -201,10 +199,8 @@ nsIMEStateManager::IsActive(nsPresContext* aPresContext) nsIFocusController* nsIMEStateManager::GetFocusController(nsPresContext* aPresContext) { - if (!aPresContext->GetDocument()) - return nsnull; nsCOMPtr container = - aPresContext->GetDocument()->GetContainer(); + aPresContext->Document()->GetContainer(); nsCOMPtr windowPrivate = do_GetInterface(container); return windowPrivate ? windowPrivate->GetRootFocusController() : nsnull; diff --git a/mozilla/content/html/content/src/nsGenericHTMLElement.cpp b/mozilla/content/html/content/src/nsGenericHTMLElement.cpp index b70f79442dc..2439b889ea9 100644 --- a/mozilla/content/html/content/src/nsGenericHTMLElement.cpp +++ b/mozilla/content/html/content/src/nsGenericHTMLElement.cpp @@ -2660,7 +2660,7 @@ nsGenericHTMLElement::MapBackgroundInto(const nsMappedAttributes* aAttributes, // as well as elements with _baseHref set. We need to be able // to get to the element somehow, or store the base URI in the // attributes. - nsIDocument* doc = aData->mPresContext->GetDocument(); + nsIDocument* doc = aData->mPresContext->Document(); nsCOMPtr uri; nsresult rv = nsContentUtils::NewURIWithDocumentCharset( getter_AddRefs(uri), spec, doc, doc->GetBaseURI()); diff --git a/mozilla/layout/base/nsDocumentViewer.cpp b/mozilla/layout/base/nsDocumentViewer.cpp index 00c7f5129aa..b03a24a8d55 100644 --- a/mozilla/layout/base/nsDocumentViewer.cpp +++ b/mozilla/layout/base/nsDocumentViewer.cpp @@ -809,13 +809,15 @@ DocumentViewerImpl::InitInternal(nsIWidget* aParentWidget, if (aParentWidget && !mPresContext) { // Create presentation context if (GetIsCreatingPrintPreview()) - mPresContext = new nsPresContext(nsPresContext::eContext_PrintPreview); + mPresContext = + new nsPresContext(mDocument, nsPresContext::eContext_PrintPreview); else if (mIsPageMode) { //Presentation context already created in SetPageMode which is calling this method } else - mPresContext = new nsPresContext(nsPresContext::eContext_Galley); + mPresContext = + new nsPresContext(mDocument, nsPresContext::eContext_Galley); NS_ENSURE_TRUE(mPresContext, NS_ERROR_OUT_OF_MEMORY); nsresult rv = mPresContext->Init(aDeviceContext); @@ -1934,7 +1936,7 @@ DocumentViewerImpl::Show(void) } NS_ASSERTION(!mPresContext, "Shouldn't have a prescontext if we have no shell!"); - mPresContext = new nsPresContext(nsPresContext::eContext_Galley); + mPresContext = new nsPresContext(mDocument, nsPresContext::eContext_Galley); NS_ENSURE_TRUE(mPresContext, NS_ERROR_OUT_OF_MEMORY); rv = mPresContext->Init(mDeviceContext); @@ -4292,12 +4294,16 @@ NS_IMETHODIMP DocumentViewerImpl::SetPageMode(PRBool aPageMode, nsIPrintSettings mViewManager = nsnull; mWindow = nsnull; + NS_ENSURE_STATE(mDocument); if (aPageMode) { - mPresContext = new nsPresContext(nsPresContext::eContext_PageLayout); + mPresContext = + new nsPresContext(mDocument, nsPresContext::eContext_PageLayout); + NS_ENSURE_TRUE(mPresContext, NS_ERROR_OUT_OF_MEMORY); mPresContext->SetPaginatedScrolling(PR_TRUE); mPresContext->SetPrintSettings(aPrintSettings); nsresult rv = mPresContext->Init(mDeviceContext); + NS_ENSURE_SUCCESS(rv, rv); } InitInternal(mParentWidget, nsnull, mDeviceContext, bounds, PR_TRUE, PR_FALSE, PR_FALSE); mViewManager->EnableRefresh(NS_VMREFRESH_NO_SYNC); diff --git a/mozilla/layout/base/nsPresContext.cpp b/mozilla/layout/base/nsPresContext.cpp index 0d867861bee..93f02bfa4bc 100644 --- a/mozilla/layout/base/nsPresContext.cpp +++ b/mozilla/layout/base/nsPresContext.cpp @@ -154,8 +154,8 @@ static NS_DEFINE_CID(kSelectionImageService, NS_SELECTIONIMAGESERVICE_CID); // NOTE! nsPresContext::operator new() zeroes out all members, so don't // bother initializing members to 0. -nsPresContext::nsPresContext(nsPresContextType aType) - : mType(aType), mTextZoom(1.0), +nsPresContext::nsPresContext(nsIDocument* aDocument, nsPresContextType aType) + : mType(aType), mDocument(aDocument), mTextZoom(1.0), mPageSize(-1, -1), mIsRootPaginatedDocument(PR_FALSE), mCanPaginatedScroll(PR_FALSE), mViewportStyleOverflow(NS_STYLE_OVERFLOW_AUTO, NS_STYLE_OVERFLOW_AUTO), @@ -217,6 +217,7 @@ nsPresContext::nsPresContext(nsPresContextType aType) mImageAnimationMode = imgIContainer::kNormalAnimMode; mNeverAnimate = PR_FALSE; } + NS_ASSERTION(mDocument, "Null document"); } nsPresContext::~nsPresContext() @@ -1170,7 +1171,7 @@ nsPresContext::SetBidi(PRUint32 aSource, PRBool aForceReflow) NS_ASSERTION(!(aForceReflow && (GetBidi() == 0)), "ForceReflow on new prescontext"); - GetDocument()->SetBidiOptions(aSource); + Document()->SetBidiOptions(aSource); if (IBMBIDI_TEXTDIRECTION_RTL == GET_BIDI_OPTION_DIRECTION(aSource) || IBMBIDI_NUMERAL_HINDI == GET_BIDI_OPTION_NUMERAL(aSource)) { SetBidiEnabled(PR_TRUE); @@ -1195,7 +1196,7 @@ nsPresContext::SetBidi(PRUint32 aSource, PRBool aForceReflow) PRUint32 nsPresContext::GetBidi() const { - return GetDocument()->GetBidiOptions(); + return Document()->GetBidiOptions(); } #endif //IBMBIDI @@ -1311,18 +1312,6 @@ nsPresContext::EnsureVisible(PRBool aUnsuppressFocus) return PR_FALSE; } -nsresult -NS_NewPresContext(nsPresContext::nsPresContextType aType, - nsPresContext** aInstancePtrResult) -{ - nsPresContext *context = new nsPresContext(aType); - if (!context) - return NS_ERROR_OUT_OF_MEMORY; - - NS_ADDREF(*aInstancePtrResult = context); - return NS_OK; -} - #ifdef MOZ_REFLOW_PERF void nsPresContext::CountReflows(const char * aName, diff --git a/mozilla/layout/base/nsPresContext.h b/mozilla/layout/base/nsPresContext.h index be720b48bd0..4a6574c3f6e 100644 --- a/mozilla/layout/base/nsPresContext.h +++ b/mozilla/layout/base/nsPresContext.h @@ -60,6 +60,7 @@ #include "nsIPrintSettings.h" #include "nsPropertyTable.h" #include "nsLayoutAtoms.h" +#include "nsIDocument.h" #ifdef IBMBIDI class nsBidiPresUtils; #endif // IBMBIDI @@ -69,7 +70,6 @@ struct nsRect; class imgIRequest; class nsIContent; -class nsIDocument; class nsIFontMetrics; class nsIFrame; class nsFrameManager; @@ -148,7 +148,7 @@ public: eContext_PageLayout // paginated & editable. }; - nsPresContext(nsPresContextType aType) NS_HIDDEN; + nsPresContext(nsIDocument* aDocument, nsPresContextType aType) NS_HIDDEN; /** * Initialize the presentation context from a particular device. @@ -175,7 +175,14 @@ public: nsIPresShell* GetPresShell() const { return mShell; } - nsIDocument* GetDocument() const { return GetPresShell()->GetDocument(); } + nsIDocument* Document() const + { + NS_ASSERTION(!mShell || !mShell->GetDocument() || + mShell->GetDocument() == mDocument, + "nsPresContext doesn't have the same document as nsPresShell!"); + return mDocument; + } + nsIViewManager* GetViewManager() { return GetPresShell()->GetViewManager(); } #ifdef _IMPL_NS_LAYOUT nsStyleSet* StyleSet() { return GetPresShell()->StyleSet(); } @@ -666,6 +673,7 @@ protected: nsPresContextType mType; nsIPresShell* mShell; // [WEAK] + nsCOMPtr mDocument; nsIDeviceContext* mDeviceContext; // [STRONG] could be weak, but // better safe than sorry. // Cannot reintroduce cycles diff --git a/mozilla/layout/base/nsPresShell.cpp b/mozilla/layout/base/nsPresShell.cpp index f9c5aa18232..ec785566d0c 100644 --- a/mozilla/layout/base/nsPresShell.cpp +++ b/mozilla/layout/base/nsPresShell.cpp @@ -7217,7 +7217,8 @@ PresShell::VerifyIncrementalReflow() nsIPresShell* sh; // Create a presentation context to view the new frame tree - NS_IF_ADDREF(cx = new nsPresContext(mPresContext->IsPaginated() ? + NS_IF_ADDREF(cx = new nsPresContext(mDocument, + mPresContext->IsPaginated() ? nsPresContext::eContext_PrintPreview : nsPresContext::eContext_Galley)); diff --git a/mozilla/layout/forms/nsComboboxControlFrame.cpp b/mozilla/layout/forms/nsComboboxControlFrame.cpp index b9d3c84adb2..9c4bffb63f8 100644 --- a/mozilla/layout/forms/nsComboboxControlFrame.cpp +++ b/mozilla/layout/forms/nsComboboxControlFrame.cpp @@ -1580,7 +1580,7 @@ nsComboboxControlFrame::HandleRedisplayTextEvent() // ActuallyDisplayText, since that flushes out the content sink by // calling SetText on a DOM node with aNotify set to true. See bug // 289730. - GetPresContext()->GetDocument()-> + GetPresContext()->Document()-> FlushPendingNotifications(Flush_ContentAndNotify); // Redirect frame insertions during this method (see GetContentInsertionFrame()) diff --git a/mozilla/layout/generic/nsGfxScrollFrame.cpp b/mozilla/layout/generic/nsGfxScrollFrame.cpp index c4af84d6a9f..e64f2156ae3 100644 --- a/mozilla/layout/generic/nsGfxScrollFrame.cpp +++ b/mozilla/layout/generic/nsGfxScrollFrame.cpp @@ -1654,7 +1654,7 @@ nsGfxScrollFrameInner::CreateAnonymousContent(nsISupportsArray& aAnonymousChildr } nsNodeInfoManager *nodeInfoManager = - presContext->GetDocument()->NodeInfoManager(); + presContext->Document()->NodeInfoManager(); nsCOMPtr nodeInfo; nodeInfoManager->GetNodeInfo(nsXULAtoms::scrollbar, nsnull, kNameSpaceID_XUL, getter_AddRefs(nodeInfo)); @@ -2105,7 +2105,7 @@ nsGfxScrollFrameInner::IsLTR() const if (mIsRoot) { // If we're the root scrollframe, we need the root element's style data. nsPresContext *presContext = mOuter->GetPresContext(); - nsIDocument *document = presContext->GetDocument(); + nsIDocument *document = presContext->Document(); nsIContent *root = document->GetRootContent(); // But for HTML we want the body element. diff --git a/mozilla/layout/html/tests/Makefile.in b/mozilla/layout/html/tests/Makefile.in index 25b235c80bf..f9b08842e8d 100644 --- a/mozilla/layout/html/tests/Makefile.in +++ b/mozilla/layout/html/tests/Makefile.in @@ -52,6 +52,7 @@ REQUIRES = xpcom \ gfx \ widget \ locale \ + dom \ $(NULL) SIMPLE_PROGRAMS = $(CPPSRCS:.cpp=$(BIN_SUFFIX)) diff --git a/mozilla/layout/printing/nsPrintEngine.cpp b/mozilla/layout/printing/nsPrintEngine.cpp index 7d4f6bde08f..184a6712a6e 100644 --- a/mozilla/layout/printing/nsPrintEngine.cpp +++ b/mozilla/layout/printing/nsPrintEngine.cpp @@ -2300,7 +2300,8 @@ nsPrintEngine::ReflowPrintObject(nsPrintObject * aPO) } // create the PresContext - aPO->mPresContext = new nsPresContext(mIsCreatingPrintPreview ? + aPO->mPresContext = new nsPresContext(aPO->mDocument, + mIsCreatingPrintPreview ? nsPresContext::eContext_PrintPreview: nsPresContext::eContext_Print); NS_ENSURE_TRUE(aPO->mPresContext, NS_ERROR_OUT_OF_MEMORY); diff --git a/mozilla/layout/style/nsCSSDataBlock.cpp b/mozilla/layout/style/nsCSSDataBlock.cpp index 97b8778b3c7..977e6d09fa3 100644 --- a/mozilla/layout/style/nsCSSDataBlock.cpp +++ b/mozilla/layout/style/nsCSSDataBlock.cpp @@ -198,7 +198,7 @@ nsCSSCompressedDataBlock::MapRuleInfoInto(nsRuleData *aRuleData) const if ((iProp == eCSSProperty_background_image || iProp == eCSSProperty_list_style_image) && val->GetUnit() == eCSSUnit_URL) { - val->StartImageLoad(aRuleData->mPresContext->GetDocument(), + val->StartImageLoad(aRuleData->mPresContext->Document(), iProp == eCSSProperty_background_image); } *target = *val; @@ -243,7 +243,7 @@ nsCSSCompressedDataBlock::MapRuleInfoInto(nsRuleData *aRuleData) const l; l = l->mNext) if (l->mValue.GetUnit() == eCSSUnit_URL) l->mValue.StartImageLoad( - aRuleData->mPresContext->GetDocument()); + aRuleData->mPresContext->Document()); } else if (iProp == eCSSProperty_cursor) { for (nsCSSValueList* l = ValueListAtCursor(cursor); l; l = l->mNext) @@ -254,7 +254,7 @@ nsCSSCompressedDataBlock::MapRuleInfoInto(nsRuleData *aRuleData) const l->mValue.GetArrayValue()->Item(0); if (val.GetUnit() == eCSSUnit_URL) val.StartImageLoad( - aRuleData->mPresContext->GetDocument()); + aRuleData->mPresContext->Document()); } } // fall through diff --git a/mozilla/layout/style/nsCSSRules.cpp b/mozilla/layout/style/nsCSSRules.cpp index e5b6e4f5979..2c40cc930d8 100644 --- a/mozilla/layout/style/nsCSSRules.cpp +++ b/mozilla/layout/style/nsCSSRules.cpp @@ -1200,7 +1200,7 @@ nsCSSDocumentRule::DeleteRule(PRUint32 aIndex) NS_IMETHODIMP_(PRBool) nsCSSDocumentRule::UseForPresentation(nsPresContext* aPresContext) { - nsIURI *docURI = aPresContext->GetDocument()->GetDocumentURI(); + nsIURI *docURI = aPresContext->Document()->GetDocumentURI(); nsCAutoString docURISpec; if (docURI) docURI->GetSpec(docURISpec); diff --git a/mozilla/layout/style/nsStyleUtil.cpp b/mozilla/layout/style/nsStyleUtil.cpp index fcefb9eccf6..cf17ba2e16e 100644 --- a/mozilla/layout/style/nsStyleUtil.cpp +++ b/mozilla/layout/style/nsStyleUtil.cpp @@ -459,10 +459,7 @@ PRBool nsStyleUtil::IsHTMLLink(nsIContent *aContent, nsIAtom *aTag, nsPresContex linkState = eLinkState_NotLink; } if (linkState != eLinkState_NotLink) { - nsIDocument* doc = aPresContext->GetDocument(); - if (doc) { - doc->AddStyleRelevantLink(aContent, hrefURI); - } + aPresContext->Document()->AddStyleRelevantLink(aContent, hrefURI); } link->SetLinkState(linkState); } @@ -501,11 +498,7 @@ PRBool nsStyleUtil::IsSimpleXlink(nsIContent *aContent, nsPresContext *aPresCont // no link handler? then all links are unvisited *aState = eLinkState_Unvisited; } - nsIDocument* doc = aPresContext->GetDocument(); - if (doc) { - doc->AddStyleRelevantLink(aContent, absURI); - } - + aPresContext->Document()->AddStyleRelevantLink(aContent, absURI); rv = PR_TRUE; } }