From 1b13fa7226dbc5cea12d036fbe57fbada00aa090 Mon Sep 17 00:00:00 2001 From: "bzbarsky%mit.edu" Date: Thu, 9 Feb 2006 16:23:32 +0000 Subject: [PATCH] More usage of already_AddRefed. Fix leaks too. Bug 325378, r=sicking, sr=jst git-svn-id: svn://10.0.0.236/trunk@189480 18797224-902f-48f8-a5cc-f745e15eee43 --- .../layout/forms/nsComboboxControlFrame.cpp | 56 +++++--------- mozilla/layout/forms/nsListControlFrame.cpp | 73 ++++++------------- mozilla/layout/forms/nsListControlFrame.h | 10 ++- 3 files changed, 46 insertions(+), 93 deletions(-) diff --git a/mozilla/layout/forms/nsComboboxControlFrame.cpp b/mozilla/layout/forms/nsComboboxControlFrame.cpp index 82cf9c9e869..4a0643781d4 100644 --- a/mozilla/layout/forms/nsComboboxControlFrame.cpp +++ b/mozilla/layout/forms/nsComboboxControlFrame.cpp @@ -636,52 +636,30 @@ nsComboboxControlFrame::PositionDropdown(nsPresContext* aPresContext, // Returns the nsIDOMHTMLOptionElement for a given index // in the select's collection //--------------------------------------------------------- -static nsIDOMHTMLOptionElement* -GetOption(nsIDOMHTMLOptionsCollection& aCollection, PRInt32 aIndex) +static already_AddRefed +GetOption(nsIDOMHTMLOptionsCollection* aCollection, PRInt32 aIndex) { - nsIDOMNode* node = nsnull; - if (NS_SUCCEEDED(aCollection.Item(aIndex, &node))) { - if (nsnull != node) { - nsIDOMHTMLOptionElement* option = nsnull; - node->QueryInterface(NS_GET_IID(nsIDOMHTMLOptionElement), (void**)&option); - NS_RELEASE(node); - return option; - } - } - return nsnull; -} -//--------------------------------------------------------- -// for a given piece of content it returns nsIDOMHTMLSelectElement object -// or null -//--------------------------------------------------------- -static nsIDOMHTMLSelectElement* -GetSelect(nsIContent * aContent) -{ - nsIDOMHTMLSelectElement* selectElement = nsnull; - nsresult result = aContent->QueryInterface(NS_GET_IID(nsIDOMHTMLSelectElement), - (void**)&selectElement); - if (NS_SUCCEEDED(result) && selectElement) { - return selectElement; - } else { - return nsnull; + nsIDOMHTMLOptionElement* option = nsnull; + + nsCOMPtr node; + if (NS_SUCCEEDED(aCollection->Item(aIndex, getter_AddRefs(node))) && node) { + CallQueryInterface(node, &option); } + + return option; } //--------------------------------------------------------- //--------------------------------------------------------- // This returns the collection for nsIDOMHTMLSelectElement or // the nsIContent object is the select is null (AddRefs) //--------------------------------------------------------- -static nsIDOMHTMLOptionsCollection* -GetOptions(nsIContent * aContent, nsIDOMHTMLSelectElement* aSelect = nsnull) +static already_AddRefed +GetOptions(nsIContent * aContent) { nsIDOMHTMLOptionsCollection* options = nsnull; - if (!aSelect) { - nsCOMPtr selectElement = getter_AddRefs(GetSelect(aContent)); - if (selectElement) { - selectElement->GetOptions(&options); // AddRefs (1) - } - } else { - aSelect->GetOptions(&options); // AddRefs (1) + nsCOMPtr selectElement = do_QueryInterface(aContent); + if (selectElement) { + selectElement->GetOptions(&options); // AddRefs (1) } return options; } @@ -704,13 +682,13 @@ nsComboboxControlFrame::ReflowItems(nsPresContext* aPresContext, nscoord maxWidth = 0; //nsIRenderingContext * rc = aReflowState.rendContext; nsresult rv = NS_ERROR_FAILURE; - nsCOMPtr options = getter_AddRefs(GetOptions(mContent)); + nsCOMPtr options = GetOptions(mContent); if (options) { PRUint32 numOptions; options->GetLength(&numOptions); //printf("--- Num of Items %d ---\n", numOptions); for (PRUint32 i=0;i optionElement = getter_AddRefs(GetOption(*options, i)); + nsCOMPtr optionElement = GetOption(options, i); if (optionElement) { nsAutoString text; optionElement->GetLabel(text); @@ -1043,7 +1021,7 @@ nsComboboxControlFrame::Reflow(nsPresContext* aPresContext, printSize("CW", aReflowState.mComputedWidth); printSize("CH", aReflowState.mComputedHeight); - nsCOMPtr optionsTemp = getter_AddRefs(GetOptions(mContent)); + nsCOMPtr optionsTemp = GetOptions(mContent); PRUint32 numOptions; optionsTemp->GetLength(&numOptions); printSize("NO", (nscoord)numOptions); diff --git a/mozilla/layout/forms/nsListControlFrame.cpp b/mozilla/layout/forms/nsListControlFrame.cpp index fd2cb15fab8..1886f9ec3a0 100644 --- a/mozilla/layout/forms/nsListControlFrame.cpp +++ b/mozilla/layout/forms/nsListControlFrame.cpp @@ -1201,8 +1201,7 @@ nsListControlFrame::InitSelectionRange(PRInt32 aClickedIndex) GetSelectedIndex(&selectedIndex); if (selectedIndex >= 0) { // Get the end of the contiguous selection - nsCOMPtr options = - getter_AddRefs(GetOptions(mContent)); + nsCOMPtr options = GetOptions(mContent); NS_ASSERTION(options, "Collection of options is null!"); PRUint32 numOptions; options->GetLength(&numOptions); @@ -1210,7 +1209,8 @@ nsListControlFrame::InitSelectionRange(PRInt32 aClickedIndex) // Push i to one past the last selected index in the group for (i=selectedIndex+1; i < numOptions; i++) { PRBool selected; - GetOption(options, i)->GetSelected(&selected); + nsCOMPtr option = GetOption(options, i); + option->GetSelected(&selected); if (!selected) { break; } @@ -1505,24 +1505,6 @@ nsListControlFrame::GetMultiple(nsIDOMHTMLSelectElement* aSelect) const } -//--------------------------------------------------------- -// for a given piece of content it returns nsIDOMHTMLSelectElement object -// or null -//--------------------------------------------------------- -nsIDOMHTMLSelectElement* -nsListControlFrame::GetSelect(nsIContent * aContent) -{ - nsIDOMHTMLSelectElement* selectElement = nsnull; - nsresult result = aContent->QueryInterface(NS_GET_IID(nsIDOMHTMLSelectElement), - (void**)&selectElement); - if (NS_SUCCEEDED(result) && selectElement) { - return selectElement; - } else { - return nsnull; - } -} - - //--------------------------------------------------------- // Returns the nsIContent object in the collection // for a given index (AddRefs) @@ -1531,7 +1513,8 @@ already_AddRefed nsListControlFrame::GetOptionAsContent(nsIDOMHTMLOptionsCollection* aCollection, PRInt32 aIndex) { nsIContent * content = nsnull; - nsCOMPtr optionElement = getter_AddRefs(GetOption(aCollection, aIndex)); + nsCOMPtr optionElement = GetOption(aCollection, + aIndex); NS_ASSERTION(optionElement != nsnull, "could not get option element by index!"); @@ -1550,8 +1533,7 @@ already_AddRefed nsListControlFrame::GetOptionContent(PRInt32 aIndex) const { - nsCOMPtr options = - getter_AddRefs(GetOptions(mContent)); + nsCOMPtr options = GetOptions(mContent); NS_ASSERTION(options.get() != nsnull, "Collection of options is null!"); if (options) { @@ -1561,20 +1543,15 @@ nsListControlFrame::GetOptionContent(PRInt32 aIndex) const } //--------------------------------------------------------- -// This returns the collection for nsIDOMHTMLSelectElement or -// the nsIContent object is the select is null (AddRefs) +// This returns the options collection for aContent, if any //--------------------------------------------------------- -nsIDOMHTMLOptionsCollection* -nsListControlFrame::GetOptions(nsIContent * aContent, nsIDOMHTMLSelectElement* aSelect) +already_AddRefed +nsListControlFrame::GetOptions(nsIContent * aContent) { nsIDOMHTMLOptionsCollection* options = nsnull; - if (!aSelect) { - nsCOMPtr selectElement = getter_AddRefs(GetSelect(aContent)); - if (selectElement) { - selectElement->GetOptions(&options); // AddRefs (1) - } - } else { - aSelect->GetOptions(&options); // AddRefs (1) + nsCOMPtr selectElement = do_QueryInterface(aContent); + if (selectElement) { + selectElement->GetOptions(&options); // AddRefs (1) } return options; @@ -1584,7 +1561,7 @@ nsListControlFrame::GetOptions(nsIContent * aContent, nsIDOMHTMLSelectElement* a // Returns the nsIDOMHTMLOptionElement for a given index // in the select's collection //--------------------------------------------------------- -nsIDOMHTMLOptionElement* +already_AddRefed nsListControlFrame::GetOption(nsIDOMHTMLOptionsCollection* aCollection, PRInt32 aIndex) { @@ -1744,8 +1721,7 @@ nsListControlFrame::GetOptionText(PRInt32 aIndex, nsAString & aStr) { aStr.SetLength(0); nsresult rv = NS_ERROR_FAILURE; - nsCOMPtr options = - getter_AddRefs(GetOptions(mContent)); + nsCOMPtr options = GetOptions(mContent); if (options) { PRUint32 numOptions; @@ -1754,8 +1730,8 @@ nsListControlFrame::GetOptionText(PRInt32 aIndex, nsAString & aStr) if (numOptions == 0) { rv = NS_OK; } else { - nsCOMPtr optionElement( - getter_AddRefs(GetOption(options, aIndex))); + nsCOMPtr optionElement = + GetOption(options, aIndex); if (optionElement) { #if 0 // This is for turning off labels Bug 4050 nsAutoString text; @@ -1807,10 +1783,9 @@ NS_IMETHODIMP nsListControlFrame::GetNumberOfOptions(PRInt32* aNumOptions) { if (mContent != nsnull) { - nsCOMPtr options = - getter_AddRefs(GetOptions(mContent)); + nsCOMPtr options = GetOptions(mContent); - if (nsnull == options) { + if (!options) { *aNumOptions = 0; } else { PRUint32 length = 0; @@ -1931,14 +1906,12 @@ nsListControlFrame::SetOptionsSelectedFromFrame(PRInt32 aStartIndex, PRBool nsListControlFrame::ToggleOptionSelectedFromFrame(PRInt32 aIndex) { - nsCOMPtr options = - getter_AddRefs(GetOptions(mContent)); + nsCOMPtr options = GetOptions(mContent); NS_ASSERTION(options, "No options"); if (!options) { return PR_FALSE; } - nsCOMPtr option( - getter_AddRefs(GetOption(options, aIndex))); + nsCOMPtr option = GetOption(options, aIndex); NS_ASSERTION(option, "No option"); if (!option) { return PR_FALSE; @@ -2966,8 +2939,7 @@ nsListControlFrame::KeyPress(nsIDOMEvent* aKeyEvent) keyEvent->GetShiftKey(&isShift); // now make sure there are options or we are wasting our time - nsCOMPtr options = - getter_AddRefs(GetOptions(mContent)); + nsCOMPtr options = GetOptions(mContent); NS_ENSURE_TRUE(options, NS_ERROR_FAILURE); PRUint32 numOptions = 0; @@ -3139,7 +3111,8 @@ nsListControlFrame::KeyPress(nsIDOMEvent* aKeyEvent) PRUint32 i; for (i = 0; i < numOptions; i++) { PRUint32 index = (i + startIndex) % numOptions; - nsCOMPtr optionElement(getter_AddRefs(GetOption(options, index))); + nsCOMPtr optionElement = + GetOption(options, index); if (optionElement) { nsAutoString text; if (NS_OK == optionElement->GetText(text)) { diff --git a/mozilla/layout/forms/nsListControlFrame.h b/mozilla/layout/forms/nsListControlFrame.h index 7191df67516..748ee027ee2 100644 --- a/mozilla/layout/forms/nsListControlFrame.h +++ b/mozilla/layout/forms/nsListControlFrame.h @@ -180,10 +180,12 @@ public: nsresult KeyPress(nsIDOMEvent* aKeyEvent); // Static Methods - static nsIDOMHTMLSelectElement* GetSelect(nsIContent * aContent); - static nsIDOMHTMLOptionsCollection* GetOptions(nsIContent * aContent, nsIDOMHTMLSelectElement* aSelect = nsnull); - static nsIDOMHTMLOptionElement* GetOption(nsIDOMHTMLOptionsCollection* aOptions, PRInt32 aIndex); - static already_AddRefed GetOptionAsContent(nsIDOMHTMLOptionsCollection* aCollection,PRInt32 aIndex); + static already_AddRefed + GetOptions(nsIContent * aContent); + static already_AddRefed + GetOption(nsIDOMHTMLOptionsCollection* aOptions, PRInt32 aIndex); + static already_AddRefed + GetOptionAsContent(nsIDOMHTMLOptionsCollection* aCollection,PRInt32 aIndex); static void ComboboxFocusSet();