Bug 455742: outparamdel nsIForm r+sr=jst
authorTaras Glek <tglek@mozilla.com>
Thu, 09 Oct 2008 16:31:40 -0700
changeset 20237 51c533bbac70bd11d41679eceec3ba68ba84d2e5
parent 20236 98b7850b016840a123a6b5f589f9176361be925d
child 20238 92e24ee719b84021eb107886afdbb5e56022522b
push idunknown
push userunknown
push dateunknown
bugs455742
milestone1.9.1b2pre
Bug 455742: outparamdel nsIForm r+sr=jst
content/base/src/nsContentUtils.cpp
content/html/content/public/nsIForm.h
content/html/content/src/nsHTMLFormElement.cpp
dom/src/base/nsDOMClassInfo.cpp
--- a/content/base/src/nsContentUtils.cpp
+++ b/content/base/src/nsContentUtils.cpp
@@ -1960,17 +1960,17 @@ nsContentUtils::GenerateStateKey(nsICont
           //
           index = htmlDocument->GetNumFormsSynchronous() - 1;
         }
         if (index > -1) {
           KeyAppendInt(index, aKey);
 
           // Append the index of the control in the form
           nsCOMPtr<nsIForm> form(do_QueryInterface(formElement));
-          form->IndexOfControl(control, &index);
+          index = form->IndexOfControl(control);
 
           if (index > -1) {
             KeyAppendInt(index, aKey);
             generatedUniqueKey = PR_TRUE;
           }
         }
 
         // Append the form name
--- a/content/html/content/public/nsIForm.h
+++ b/content/html/content/public/nsIForm.h
@@ -98,17 +98,17 @@ public:
   NS_IMETHOD GetElementAt(PRInt32 aIndex, nsIFormControl** aElement) const = 0;
 
   /**
    * Get the number of elements in form.elements
    *
    * @param aCount the number of elements
    * @return NS_OK if there was an element at that position, -1 otherwise
    */
-  NS_IMETHOD GetElementCount(PRUint32* aCount) const = 0;
+  NS_IMETHOD_(PRUint32) GetElementCount() const = 0;
 
   /**
    * Remove an element from this form's list of elements
    *
    * @param aElement the element to remove
    * @param aNotify If true, send nsIDocumentObserver notifications as needed.
    * @return NS_OK if the element was successfully removed.
    */
@@ -133,25 +133,24 @@ public:
    * Resolve a name in the scope of the form object, this means find
    * form controls in this form with the correct value in the name
    * attribute.
    *
    * @param aElement the element to remove
    * @param aName the name or id of the element to remove
    * @return NS_OK if the element was successfully removed.
    */
-  NS_IMETHOD ResolveName(const nsAString& aName,
-                         nsISupports **aResult) = 0;
+  NS_IMETHOD_(already_AddRefed<nsISupports>) ResolveName(const nsAString& aName) = 0;
 
   /**
    * Get the index of the given control within form.elements.
    * @param aControl the control to find the index of
    * @param aIndex the index [OUT]
    */
-  NS_IMETHOD IndexOfControl(nsIFormControl* aControl, PRInt32* aIndex) = 0;
+  NS_IMETHOD_(PRInt32) IndexOfControl(nsIFormControl* aControl) = 0;
 
   /**
    * Flag the form to know that a button or image triggered scripted form
    * submission. In that case the form will defer the submission until the
    * script handler returns and the return value is known.
    */ 
   NS_IMETHOD OnSubmitClickBegin() = 0;
   NS_IMETHOD OnSubmitClickEnd() = 0;
--- a/content/html/content/src/nsHTMLFormElement.cpp
+++ b/content/html/content/src/nsHTMLFormElement.cpp
@@ -164,24 +164,23 @@ public:
   NS_DECL_NSIWEBPROGRESSLISTENER
 
   // nsIForm
   NS_IMETHOD AddElement(nsIFormControl* aElement,
                         PRBool aNotify);
   NS_IMETHOD AddElementToTable(nsIFormControl* aChild,
                                const nsAString& aName);
   NS_IMETHOD GetElementAt(PRInt32 aIndex, nsIFormControl** aElement) const;
-  NS_IMETHOD GetElementCount(PRUint32* aCount) const;
+  NS_IMETHOD_(PRUint32) GetElementCount() const;
   NS_IMETHOD RemoveElement(nsIFormControl* aElement,
                            PRBool aNotify);
   NS_IMETHOD RemoveElementFromTable(nsIFormControl* aElement,
                                     const nsAString& aName);
-  NS_IMETHOD ResolveName(const nsAString& aName,
-                         nsISupports** aReturn);
-  NS_IMETHOD IndexOfControl(nsIFormControl* aControl, PRInt32* aIndex);
+  NS_IMETHOD_(already_AddRefed<nsISupports>) ResolveName(const nsAString& aName);
+  NS_IMETHOD_(PRInt32) IndexOfControl(nsIFormControl* aControl);
   NS_IMETHOD OnSubmitClickBegin();
   NS_IMETHOD OnSubmitClickEnd();
   NS_IMETHOD FlushPendingSubmission();
   NS_IMETHOD ForgetPendingSubmission();
   NS_IMETHOD GetActionURL(nsIURI** aActionURL);
   NS_IMETHOD GetSortedControls(nsTArray<nsIFormControl*>& aControls) const;
   NS_IMETHOD_(nsIFormControl*) GetDefaultSubmitElement() const;
   NS_IMETHOD_(PRBool) HasSingleTextControl() const;
@@ -289,18 +288,17 @@ protected:
    *        submit should be cancelled.
    */
   nsresult NotifySubmitObservers(nsIURI* aActionURL, PRBool* aCancelSubmit,
                                  PRBool aEarlyNotify);
 
   /**
    * Just like ResolveName(), but takes an arg for whether to flush
    */
-  nsresult DoResolveName(const nsAString& aName, PRBool aFlushContent,
-                         nsISupports** aReturn);
+  already_AddRefed<nsISupports> DoResolveName(const nsAString& aName, PRBool aFlushContent);
 
   //
   // Data members
   //
   /** The list of controls (form.elements as well as stuff not in elements) */
   nsRefPtr<nsFormControlList> mControls;
   /** The currently selected radio button of each group */
   nsInterfaceHashtable<nsStringCaseInsensitiveHashKey,nsIDOMHTMLInputElement> mSelectedRadioButtons;
@@ -921,18 +919,17 @@ nsHTMLFormElement::DoSubmitOrReset(nsEve
   }
   return rv;
 }
 
 nsresult
 nsHTMLFormElement::DoReset()
 {
   // JBK walk the elements[] array instead of form frame controls - bug 34297
-  PRUint32 numElements;
-  GetElementCount(&numElements);
+  PRUint32 numElements = GetElementCount();
   for (PRUint32 elementX = 0; (elementX < numElements); elementX++) {
     nsCOMPtr<nsIFormControl> controlNode;
     GetElementAt(elementX, getter_AddRefs(controlNode));
     if (controlNode) {
       controlNode->Reset();
     }
   }
 
@@ -1214,21 +1211,22 @@ nsHTMLFormElement::WalkFormElements(nsIF
     sortedControls[i]->SubmitNamesValues(aFormSubmission, aSubmitElement);
   }
 
   return NS_OK;
 }
 
 // nsIForm
 
-NS_IMETHODIMP
-nsHTMLFormElement::GetElementCount(PRUint32* aCount) const 
+NS_IMETHODIMP_(PRUint32)
+nsHTMLFormElement::GetElementCount() const 
 {
-  mControls->GetLength(aCount); 
-  return NS_OK;
+  PRUint32 count = nsnull;
+  mControls->GetLength(&count); 
+  return count;
 }
 
 NS_IMETHODIMP 
 nsHTMLFormElement::GetElementAt(PRInt32 aIndex,
                                 nsIFormControl** aFormControl) const 
 {
   // Converting to unsigned int will handle negative indices.
   if (PRUint32(aIndex) >= mControls->mElements.Length()) {
@@ -1525,30 +1523,29 @@ nsHTMLFormElement::RemoveElement(nsIForm
 
 NS_IMETHODIMP
 nsHTMLFormElement::RemoveElementFromTable(nsIFormControl* aElement,
                                           const nsAString& aName)
 {
   return mControls->RemoveElementFromTable(aElement, aName);
 }
 
-NS_IMETHODIMP
-nsHTMLFormElement::ResolveName(const nsAString& aName,
-                               nsISupports **aResult)
+NS_IMETHODIMP_(already_AddRefed<nsISupports>)
+nsHTMLFormElement::ResolveName(const nsAString& aName)
 {
-  return DoResolveName(aName, PR_TRUE, aResult);
+  return DoResolveName(aName, PR_TRUE);
 }
 
-nsresult
+already_AddRefed<nsISupports>
 nsHTMLFormElement::DoResolveName(const nsAString& aName,
-                                 PRBool aFlushContent,
-                                 nsISupports **aResult)
+                                 PRBool aFlushContent)
 {
-  mControls->NamedItemInternal(aName, aFlushContent, aResult);
-  return NS_OK;
+  nsISupports *result = nsnull;
+  mControls->NamedItemInternal(aName, aFlushContent, &result);
+  return result;
 }
 
 NS_IMETHODIMP
 nsHTMLFormElement::OnSubmitClickBegin()
 {
   mDeferSubmission = PR_TRUE;
 
   // Prepare to run NotifySubmitObservers early before the
@@ -1805,20 +1802,21 @@ NS_IMETHODIMP
 nsHTMLFormElement::OnSecurityChange(nsIWebProgress* aWebProgress,
                                     nsIRequest* aRequest,
                                     PRUint32 state)
 {
   NS_NOTREACHED("notification excluded in AddProgressListener(...)");
   return NS_OK;
 }
  
-NS_IMETHODIMP
-nsHTMLFormElement::IndexOfControl(nsIFormControl* aControl, PRInt32* aIndex)
+NS_IMETHODIMP_(PRInt32)
+nsHTMLFormElement::IndexOfControl(nsIFormControl* aControl)
 {
-  return mControls->IndexOfControl(aControl, aIndex);
+  PRInt32 index = nsnull;
+  return mControls->IndexOfControl(aControl, &index) == NS_OK ? index : nsnull;
 }
 
 NS_IMETHODIMP
 nsHTMLFormElement::SetCurrentRadioButton(const nsAString& aName,
                                          nsIDOMHTMLInputElement* aRadio)
 {
   NS_ENSURE_TRUE(mSelectedRadioButtons.Put(aName, aRadio),
                  NS_ERROR_OUT_OF_MEMORY);
@@ -1845,18 +1843,18 @@ nsHTMLFormElement::GetPositionInGroup(ns
 
   nsAutoString name;
   aRadio->GetName(name);
   if (name.IsEmpty()) {
     return NS_OK;
   }
 
   nsCOMPtr<nsISupports> itemWithName;
-  nsresult rv = ResolveName(name, getter_AddRefs(itemWithName));
-  NS_ENSURE_SUCCESS(rv, rv);
+  itemWithName = ResolveName(name);
+  NS_ENSURE_TRUE(itemWithName, NS_ERROR_FAILURE);
   nsCOMPtr<nsIDOMNodeList> radioNodeList(do_QueryInterface(itemWithName));
 
   // XXX If ResolveName could return an nsContentList instead then we 
   //     could get an nsContentList instead of using this hacky upcast
   nsBaseContentList *radioGroup =
     static_cast<nsBaseContentList *>((nsIDOMNodeList *)radioNodeList);
   NS_ASSERTION(radioGroup, "No such radio group in this container");
   if (!radioGroup) {
@@ -1887,18 +1885,17 @@ nsHTMLFormElement::GetNextRadioButton(co
   nsCOMPtr<nsIDOMHTMLInputElement> currentRadio;
   if (aFocusedRadio) {
     currentRadio = aFocusedRadio;
   }
   else {
     mSelectedRadioButtons.Get(aName, getter_AddRefs(currentRadio));
   }
 
-  nsCOMPtr<nsISupports> itemWithName;
-  ResolveName(aName, getter_AddRefs(itemWithName));
+  nsCOMPtr<nsISupports> itemWithName = ResolveName(aName);
   nsCOMPtr<nsIDOMNodeList> radioNodeList(do_QueryInterface(itemWithName));
 
   // XXX If ResolveName could return an nsContentList instead then we 
   //     could get an nsContentList instead of using this hacky upcast
 
   nsBaseContentList *radioGroup =
     static_cast<nsBaseContentList *>((nsIDOMNodeList *)radioNodeList);
   if (!radioGroup) {
@@ -1954,18 +1951,17 @@ nsHTMLFormElement::WalkRadioGroup(const 
   PRBool stopIterating = PR_FALSE;
 
   if (aName.IsEmpty()) {
     //
     // XXX If the name is empty, it's not stored in the control list.  There
     // *must* be a more efficient way to do this.
     //
     nsCOMPtr<nsIFormControl> control;
-    PRUint32 len = 0;
-    GetElementCount(&len);
+    PRUint32 len = GetElementCount();
     for (PRUint32 i=0; i<len; i++) {
       GetElementAt(i, getter_AddRefs(control));
       if (control->GetType() == NS_FORM_INPUT_RADIO) {
         nsCOMPtr<nsIContent> controlContent(do_QueryInterface(control));
         if (controlContent) {
           if (controlContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::name,
                                           EmptyString(), eCaseMatters)) {
             aVisitor->Visit(control, &stopIterating);
@@ -1976,17 +1972,18 @@ nsHTMLFormElement::WalkRadioGroup(const 
         }
       }
     }
   } else {
     //
     // Get the control / list of controls from the form using form["name"]
     //
     nsCOMPtr<nsISupports> item;
-    rv = DoResolveName(aName, aFlushContent, getter_AddRefs(item));
+    item = DoResolveName(aName, aFlushContent);
+    rv = item ? NS_OK : NS_ERROR_FAILURE;
 
     if (item) {
       //
       // If it's just a lone radio button, then select it.
       //
       nsCOMPtr<nsIFormControl> formControl(do_QueryInterface(item));
       if (formControl) {
         if (formControl->GetType() == NS_FORM_INPUT_RADIO) {
--- a/dom/src/base/nsDOMClassInfo.cpp
+++ b/dom/src/base/nsDOMClassInfo.cpp
@@ -8789,21 +8789,19 @@ nsHTMLElementSH::NewResolve(nsIXPConnect
 
 // HTMLFormElement helper
 
 // static
 nsresult
 nsHTMLFormElementSH::FindNamedItem(nsIForm *aForm, JSString *str,
                                    nsISupports **aResult)
 {
-  *aResult = nsnull;
-
   nsDependentJSString name(str);
 
-  aForm->ResolveName(name, aResult);
+  *aResult = aForm->ResolveName(name).get();
 
   if (!*aResult) {
     nsCOMPtr<nsIContent> content(do_QueryInterface(aForm));
     nsCOMPtr<nsIDOMHTMLFormElement> form_element(do_QueryInterface(aForm));
 
     nsCOMPtr<nsIHTMLDocument> html_doc =
       do_QueryInterface(content->GetDocument());
 
@@ -8904,33 +8902,31 @@ nsHTMLFormElementSH::NewEnumerate(nsIXPC
       if (!form) {
         *statep = JSVAL_NULL;
         return NS_ERROR_UNEXPECTED;
       }
 
       *statep = INT_TO_JSVAL(0);
 
       if (idp) {
-        PRUint32 count = 0;
-        form->GetElementCount(&count);
+        PRUint32 count = form->GetElementCount();
 
         *idp = INT_TO_JSVAL(count);
       }
 
       break;
     }
   case JSENUMERATE_NEXT:
     {
       nsCOMPtr<nsIForm> form(do_QueryWrappedNative(wrapper));
       NS_ENSURE_TRUE(form, NS_ERROR_FAILURE);
 
       PRInt32 index = (PRInt32)JSVAL_TO_INT(*statep);
 
-      PRUint32 count = 0;
-      form->GetElementCount(&count);
+      PRUint32 count = form->GetElementCount();
 
       if ((PRUint32)index < count) {
         nsCOMPtr<nsIFormControl> controlNode;
         form->GetElementAt(index, getter_AddRefs(controlNode));
         NS_ENSURE_TRUE(controlNode, NS_ERROR_FAILURE);
 
         nsCOMPtr<nsIDOMElement> domElement = do_QueryInterface(controlNode);
         NS_ENSURE_TRUE(domElement, NS_ERROR_FAILURE);