From c82a6bc1942142db5fb9850dd7521d3eecdb9e9d Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Wed, 31 Mar 2010 08:39:31 -0400 Subject: [PATCH] Bug 253889: DeCOMtaminate nsIPresShell - GetCaret(). r=roc --HG-- extra : rebase_source : 2a05050d8cbb10dd3bb763d5d5a158cc5f3dd814 --- accessible/src/base/nsCaretAccessible.cpp | 3 +-- content/events/src/nsContentEventHandler.cpp | 12 +++------ dom/base/nsFocusManager.cpp | 6 ++--- editor/libeditor/base/nsEditor.cpp | 11 ++++---- .../libeditor/base/nsEditorEventListener.cpp | 6 ++--- editor/libeditor/text/nsPlaintextEditor.cpp | 3 +-- layout/base/nsDisplayList.cpp | 6 ++--- layout/base/nsIPresShell.h | 4 +-- layout/base/nsPresShell.cpp | 21 ++++++---------- layout/forms/nsTextControlFrame.cpp | 22 +++++++--------- layout/generic/nsSelection.cpp | 25 ++++++------------- layout/xul/base/src/nsXULPopupManager.cpp | 3 +-- 12 files changed, 45 insertions(+), 77 deletions(-) diff --git a/accessible/src/base/nsCaretAccessible.cpp b/accessible/src/base/nsCaretAccessible.cpp index 1e2c3062f0ca..45dc72415d2b 100644 --- a/accessible/src/base/nsCaretAccessible.cpp +++ b/accessible/src/base/nsCaretAccessible.cpp @@ -322,8 +322,7 @@ nsCaretAccessible::GetCaretRect(nsIWidget **aOutWidget) nsCoreUtils::GetPresShellFor(lastNodeWithCaret); NS_ENSURE_TRUE(presShell, caretRect); - nsRefPtr caret; - presShell->GetCaret(getter_AddRefs(caret)); + nsRefPtr caret = presShell->GetCaret(); NS_ENSURE_TRUE(caret, caretRect); nsCOMPtr caretSelection(do_QueryReferent(mLastUsedSelection)); diff --git a/content/events/src/nsContentEventHandler.cpp b/content/events/src/nsContentEventHandler.cpp index 04626598f3e8..0f545985d9af 100644 --- a/content/events/src/nsContentEventHandler.cpp +++ b/content/events/src/nsContentEventHandler.cpp @@ -135,10 +135,8 @@ nsContentEventHandler::Init(nsQueryContentEvent* aEvent) NS_ENSURE_SUCCESS(rv, NS_ERROR_NOT_AVAILABLE); aEvent->mReply.mHasSelection = !isCollapsed; - nsRefPtr caret; - rv = mPresShell->GetCaret(getter_AddRefs(caret)); - NS_ENSURE_SUCCESS(rv, rv); - NS_ASSERTION(caret, "GetCaret succeeded, but the result is null"); + nsRefPtr caret = mPresShell->GetCaret(); + NS_ASSERTION(caret, "GetCaret returned null"); nsRect r; nsIFrame* frame = caret->GetGeometry(mSelection, &r); @@ -663,10 +661,8 @@ nsContentEventHandler::OnQueryCaretRect(nsQueryContentEvent* aEvent) if (NS_FAILED(rv)) return rv; - nsRefPtr caret; - rv = mPresShell->GetCaret(getter_AddRefs(caret)); - NS_ENSURE_SUCCESS(rv, rv); - NS_ASSERTION(caret, "GetCaret succeeded, but the result is null"); + nsRefPtr caret = mPresShell->GetCaret(); + NS_ASSERTION(caret, "GetCaret returned null"); // When the selection is collapsed and the queried offset is current caret // position, we should return the "real" caret rect. diff --git a/dom/base/nsFocusManager.cpp b/dom/base/nsFocusManager.cpp index 879d8dc30a50..89a6b79de96d 100644 --- a/dom/base/nsFocusManager.cpp +++ b/dom/base/nsFocusManager.cpp @@ -1899,8 +1899,7 @@ nsFocusManager::SetCaretVisible(nsIPresShell* aPresShell, // When browsing with caret, make sure caret is visible after new focus // Return early if there is no caret. This can happen for the testcase // for bug 308025 where a window is closed in a blur handler. - nsRefPtr caret; - aPresShell->GetCaret(getter_AddRefs(caret)); + nsRefPtr caret = aPresShell->GetCaret(); if (!caret) return NS_OK; @@ -2054,8 +2053,7 @@ nsFocusManager::GetSelectionLocation(nsIDocument* aDocument, if (newCaretFrame && newCaretContent) { // If the caret is exactly at the same position of the new frame, // then we can use the newCaretFrame and newCaretContent for our position - nsRefPtr caret; - aPresShell->GetCaret(getter_AddRefs(caret)); + nsRefPtr caret = aPresShell->GetCaret(); nsRect caretRect; nsIFrame *frame = caret->GetGeometry(domSelection, &caretRect); if (frame) { diff --git a/editor/libeditor/base/nsEditor.cpp b/editor/libeditor/base/nsEditor.cpp index 1ad208dc6529..61948c1d503a 100644 --- a/editor/libeditor/base/nsEditor.cpp +++ b/editor/libeditor/base/nsEditor.cpp @@ -1981,10 +1981,9 @@ nsEditor::QueryComposition(nsTextEventReply* aReply) if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; nsCOMPtr ps = do_QueryReferent(mPresShellWeak); if (!ps) return NS_ERROR_NOT_INITIALIZED; - nsRefPtr caretP; - result = ps->GetCaret(getter_AddRefs(caretP)); - - if (NS_SUCCEEDED(result) && caretP) { + nsRefPtr caretP = ps->GetCaret(); + + if (caretP) { if (aReply) { caretP->SetCaretDOMSelection(selection); @@ -4363,10 +4362,10 @@ nsresult nsEditor::EndUpdateViewBatch() GetPresShell(getter_AddRefs(presShell)); if (presShell) - presShell->GetCaret(getter_AddRefs(caret)); + caret = presShell->GetCaret(); StCaretHider caretHider(caret); - + PRUint32 flags = 0; GetFlags(&flags); diff --git a/editor/libeditor/base/nsEditorEventListener.cpp b/editor/libeditor/base/nsEditorEventListener.cpp index 1af694ad6d56..0595ebfeb32c 100644 --- a/editor/libeditor/base/nsEditorEventListener.cpp +++ b/editor/libeditor/base/nsEditorEventListener.cpp @@ -870,8 +870,7 @@ nsEditorEventListener::Focus(nsIDOMEvent* aEvent) nsCOMPtr presShell = GetPresShell(); if (presShell) { - nsRefPtr caret; - presShell->GetCaret(getter_AddRefs(caret)); + nsRefPtr caret = presShell->GetCaret(); if (caret) { caret->SetIgnoreUserModify(PR_FALSE); if (selection) { @@ -942,8 +941,7 @@ nsEditorEventListener::Blur(nsIDOMEvent* aEvent) nsCOMPtr presShell = GetPresShell(); if (presShell) { - nsRefPtr caret; - presShell->GetCaret(getter_AddRefs(caret)); + nsRefPtr caret = presShell->GetCaret(); if (caret) { caret->SetIgnoreUserModify(PR_TRUE); } diff --git a/editor/libeditor/text/nsPlaintextEditor.cpp b/editor/libeditor/text/nsPlaintextEditor.cpp index 6ab657559dda..b8defc3db979 100644 --- a/editor/libeditor/text/nsPlaintextEditor.cpp +++ b/editor/libeditor/text/nsPlaintextEditor.cpp @@ -1576,8 +1576,7 @@ nsPlaintextEditor::SetCompositionString(const nsAString& aCompositionString, nsI nsresult result = GetSelection(getter_AddRefs(selection)); if (NS_FAILED(result)) return result; - nsRefPtr caretP; - ps->GetCaret(getter_AddRefs(caretP)); + nsRefPtr caretP = ps->GetCaret(); // We should return caret position if it is possible. Because this event // dispatcher always expects to be returned the correct caret position. diff --git a/layout/base/nsDisplayList.cpp b/layout/base/nsDisplayList.cpp index 3572eb85a9d0..cf792bb5c13a 100644 --- a/layout/base/nsDisplayList.cpp +++ b/layout/base/nsDisplayList.cpp @@ -182,8 +182,7 @@ nsDisplayListBuilder::IsMovingFrame(nsIFrame* aFrame) nsCaret * nsDisplayListBuilder::GetCaret() { - nsRefPtr caret; - CurrentPresShellState()->mPresShell->GetCaret(getter_AddRefs(caret)); + nsRefPtr caret = CurrentPresShellState()->mPresShell->GetCaret(); return caret; } @@ -202,8 +201,7 @@ nsDisplayListBuilder::EnterPresShell(nsIFrame* aReferenceFrame, if (!mBuildCaret) return; - nsRefPtr caret; - state->mPresShell->GetCaret(getter_AddRefs(caret)); + nsRefPtr caret = state->mPresShell->GetCaret(); state->mCaretFrame = caret->GetCaretFrame(); if (state->mCaretFrame) { diff --git a/layout/base/nsIPresShell.h b/layout/base/nsIPresShell.h index 805f0accec10..6e60f6790b41 100644 --- a/layout/base/nsIPresShell.h +++ b/layout/base/nsIPresShell.h @@ -585,14 +585,14 @@ public: /** * Get the caret, if it exists. AddRefs it. */ - NS_IMETHOD GetCaret(nsCaret **aOutCaret) = 0; + virtual NS_HIDDEN_(already_AddRefed) GetCaret() = 0; /** * Invalidate the caret's current position if it's outside of its frame's * boundaries. This function is useful if you're batching selection * notifications and might remove the caret's frame out from under it. */ - NS_IMETHOD_(void) MaybeInvalidateCaretPosition() = 0; + virtual NS_HIDDEN_(void) MaybeInvalidateCaretPosition() = 0; /** * Set the current caret to a new caret. To undo this, call RestoreCaret. diff --git a/layout/base/nsPresShell.cpp b/layout/base/nsPresShell.cpp index c1e3ad7dc551..8edee8efdaaf 100644 --- a/layout/base/nsPresShell.cpp +++ b/layout/base/nsPresShell.cpp @@ -811,8 +811,8 @@ public: NS_IMETHOD_(void) ClearMouseCapture(nsIView* aView); // caret handling - NS_IMETHOD GetCaret(nsCaret **aOutCaret); - NS_IMETHOD_(void) MaybeInvalidateCaretPosition(); + virtual NS_HIDDEN_(already_AddRefed) GetCaret(); + virtual NS_HIDDEN_(void) MaybeInvalidateCaretPosition(); NS_IMETHOD SetCaretEnabled(PRBool aInEnable); NS_IMETHOD SetCaretReadOnly(PRBool aReadOnly); NS_IMETHOD GetCaretEnabled(PRBool *aOutEnabled); @@ -2784,17 +2784,14 @@ PresShell::NotifyDestroyingFrame(nsIFrame* aFrame) return NS_OK; } -// note that this can return a null caret, but NS_OK -NS_IMETHODIMP PresShell::GetCaret(nsCaret **outCaret) +already_AddRefed PresShell::GetCaret() { - NS_ENSURE_ARG_POINTER(outCaret); - - *outCaret = mCaret; - NS_IF_ADDREF(*outCaret); - return NS_OK; + nsCaret* caret = mCaret; + NS_IF_ADDREF(caret); + return caret; } -NS_IMETHODIMP_(void) PresShell::MaybeInvalidateCaretPosition() +void PresShell::MaybeInvalidateCaretPosition() { if (mCaret) { mCaret->InvalidateOutsideCaret(); @@ -6635,9 +6632,7 @@ PresShell::PrepareToUseCaretPosition(nsIWidget* aEventWidget, nsIntPoint& aTarge nsresult rv; // check caret visibility - nsRefPtr caret; - rv = GetCaret(getter_AddRefs(caret)); - NS_ENSURE_SUCCESS(rv, PR_FALSE); + nsRefPtr caret = GetCaret(); NS_ENSURE_TRUE(caret, PR_FALSE); PRBool caretVisible = PR_FALSE; diff --git a/layout/forms/nsTextControlFrame.cpp b/layout/forms/nsTextControlFrame.cpp index 803ed1a79974..9e747145342e 100644 --- a/layout/forms/nsTextControlFrame.cpp +++ b/layout/forms/nsTextControlFrame.cpp @@ -689,9 +689,8 @@ nsTextInputSelectionImpl::SetCaretReadOnly(PRBool aReadOnly) nsCOMPtr shell = do_QueryReferent(mPresShellWeak, &result); if (shell) { - nsRefPtr caret; - if (NS_SUCCEEDED(shell->GetCaret(getter_AddRefs(caret)))) - { + nsRefPtr caret = shell->GetCaret(); + if (caret) { nsISelection* domSel = mFrameSelection-> GetSelection(nsISelectionController::SELECTION_NORMAL); if (domSel) @@ -716,9 +715,8 @@ nsTextInputSelectionImpl::GetCaretVisible(PRBool *_retval) nsCOMPtr shell = do_QueryReferent(mPresShellWeak, &result); if (shell) { - nsRefPtr caret; - if (NS_SUCCEEDED(shell->GetCaret(getter_AddRefs(caret)))) - { + nsRefPtr caret = shell->GetCaret(); + if (caret) { nsISelection* domSel = mFrameSelection-> GetSelection(nsISelectionController::SELECTION_NORMAL); if (domSel) @@ -736,9 +734,8 @@ nsTextInputSelectionImpl::SetCaretVisibilityDuringSelection(PRBool aVisibility) nsCOMPtr shell = do_QueryReferent(mPresShellWeak, &result); if (shell) { - nsRefPtr caret; - if (NS_SUCCEEDED(shell->GetCaret(getter_AddRefs(caret)))) - { + nsRefPtr caret = shell->GetCaret(); + if (caret) { nsISelection* domSel = mFrameSelection-> GetSelection(nsISelectionController::SELECTION_NORMAL); if (domSel) @@ -1649,9 +1646,9 @@ nsTextControlFrame::CreateAnonymousContent(nsTArray& aElements) getter_AddRefs(domSelection))) && domSelection) { nsCOMPtr selPriv(do_QueryInterface(domSelection)); - nsRefPtr caret; + nsRefPtr caret = shell->GetCaret(); nsCOMPtr listener; - if (NS_SUCCEEDED(shell->GetCaret(getter_AddRefs(caret))) && caret) { + if (caret) { listener = do_QueryInterface(caret); if (listener) { selPriv->AddSelectionListener(listener); @@ -1883,8 +1880,7 @@ void nsTextControlFrame::SetFocus(PRBool aOn, PRBool aRepaint) if (!ourSel) return; nsIPresShell* presShell = PresContext()->GetPresShell(); - nsRefPtr caret; - presShell->GetCaret(getter_AddRefs(caret)); + nsRefPtr caret = presShell->GetCaret(); if (!caret) return; caret->SetCaretDOMSelection(ourSel); diff --git a/layout/generic/nsSelection.cpp b/layout/generic/nsSelection.cpp index 8f1bd5017a1b..6e9aa235265a 100644 --- a/layout/generic/nsSelection.cpp +++ b/layout/generic/nsSelection.cpp @@ -788,15 +788,12 @@ nsFrameSelection::FetchDesiredX(nscoord &aDesiredX) //the x position requested b return NS_OK; } - nsRefPtr caret; - nsresult result = mShell->GetCaret(getter_AddRefs(caret)); - if (NS_FAILED(result)) - return result; + nsRefPtr caret = mShell->GetCaret(); if (!caret) return NS_ERROR_NULL_POINTER; PRInt8 index = GetIndexFromSelectionType(nsISelectionController::SELECTION_NORMAL); - result = caret->SetCaretDOMSelection(mDomSelections[index]); + nsresult result = caret->SetCaretDOMSelection(mDomSelections[index]); if (NS_FAILED(result)) return result; @@ -2086,7 +2083,6 @@ nsFrameSelection::CommonPageMove(PRBool aForward, // expected behavior for PageMove is to scroll AND move the caret // and remain relative position of the caret in view. see Bug 4302. - nsresult result; //get the frame from the scrollable view nsIFrame* scrolledFrame = aScrollableFrame->GetScrolledFrame(); @@ -2098,12 +2094,9 @@ nsFrameSelection::CommonPageMove(PRBool aForward, nsISelection* domSel = GetSelection(nsISelectionController::SELECTION_NORMAL); if (!domSel) return; - - nsRefPtr caret; - result = mShell->GetCaret(getter_AddRefs(caret)); - if (NS_FAILED(result)) - return; - + + nsRefPtr caret = mShell->GetCaret(); + nsRect caretPos; nsIFrame* caretFrame = caret->GetGeometry(domSel, &caretPos); if (!caretFrame) @@ -4195,9 +4188,8 @@ nsTypedSelection::GetPrimaryFrameForFocusNode(nsIFrame **aReturnFrame, PRInt32 * nsFrameSelection::HINT hint = mFrameSelection->GetHint(); if (aVisual) { - nsRefPtr caret; - nsresult result = presShell->GetCaret(getter_AddRefs(caret)); - if (NS_FAILED(result) || !caret) + nsRefPtr caret = presShell->GetCaret(); + if (!caret) return NS_ERROR_FAILURE; PRUint8 caretBidiLevel = mFrameSelection->GetCaretBidiLevel(); @@ -5558,8 +5550,7 @@ nsTypedSelection::ScrollIntoView(SelectionRegion aRegion, result = GetPresShell(getter_AddRefs(presShell)); if (NS_FAILED(result) || !presShell) return result; - nsRefPtr caret; - presShell->GetCaret(getter_AddRefs(caret)); + nsRefPtr caret = presShell->GetCaret(); if (caret) { // Now that text frame character offsets are always valid (though not diff --git a/layout/xul/base/src/nsXULPopupManager.cpp b/layout/xul/base/src/nsXULPopupManager.cpp index 7a82b481415e..06be999d6d56 100644 --- a/layout/xul/base/src/nsXULPopupManager.cpp +++ b/layout/xul/base/src/nsXULPopupManager.cpp @@ -552,8 +552,7 @@ CheckCaretDrawingState() { if (!presShell) return; - nsRefPtr caret; - presShell->GetCaret(getter_AddRefs(caret)); + nsRefPtr caret = presShell->GetCaret(); if (!caret) return; caret->CheckCaretDrawingState();