Back to index

lightning-sunbird  0.9+nobinonly
Classes | Functions
nsFormSubmission.cpp File Reference
#include "nsIFormSubmission.h"
#include "nsPresContext.h"
#include "nsCOMPtr.h"
#include "nsIForm.h"
#include "nsILinkHandler.h"
#include "nsIDocument.h"
#include "nsHTMLAtoms.h"
#include "nsIHTMLDocument.h"
#include "nsIFormControl.h"
#include "nsIScriptGlobalObject.h"
#include "nsIDOMHTMLFormElement.h"
#include "nsDOMError.h"
#include "nsGenericHTMLElement.h"
#include "nsISaveAsCharset.h"
#include "nsIFile.h"
#include "nsDirectoryServiceDefs.h"
#include "nsIFormProcessor.h"
#include "nsIURI.h"
#include "nsNetUtil.h"
#include "nsLinebreakConverter.h"
#include "nsICharsetConverterManager.h"
#include "nsICharsetAlias.h"
#include "nsEscape.h"
#include "nsUnicharUtils.h"
#include "nsIMultiplexInputStream.h"
#include "nsIMIMEInputStream.h"
#include "nsIConsoleService.h"
#include "nsIScriptError.h"
#include "nsIStringBundle.h"
#include "nsBidiUtils.h"

Go to the source code of this file.

Classes

class  nsFormSubmission
 Helper superclass implementation of nsIFormSubmission, providing common methods that most of the specific implementations need and use. More...
class  nsFSURLEncoded
class  nsFSMultipartFormData
 Handle multipart/form-data encoding, which does files as well as normal inputs. More...
class  nsFSTextPlain

Functions

static NS_DEFINE_CID (kFormProcessorCID, NS_FORMPROCESSOR_CID)
static NS_DEFINE_CID (kCharsetAliasCID, NS_CHARSETALIAS_CID)
static nsresult SendJSWarning (nsIContent *aContent, const char *aWarningName)
 Send a warning to the JS console.
static nsresult SendJSWarning (nsIContent *aContent, const char *aWarningName, const nsAFlatString &aWarningArg1)
 Send a warning to the JS console.
static nsresult SendJSWarning (nsIContent *aContent, const char *aWarningName, const PRUnichar **aWarningArgs, PRUint32 aWarningArgsLen)
 Send a warning to the JS console.
static void HandleMailtoSubject (nsCString &aPath)
nsresult GetSubmissionFromForm (nsGenericHTMLElement *aForm, nsPresContext *aPresContext, nsIFormSubmission **aFormSubmission)
 Get a submission object based on attributes in the form (ENCTYPE and METHOD)

Function Documentation

nsresult GetSubmissionFromForm ( nsGenericHTMLElement aForm,
nsPresContext aPresContext,
nsIFormSubmission **  aFormSubmission 
)

Get a submission object based on attributes in the form (ENCTYPE and METHOD)

Parameters:
aFormthe form to get a submission object based on
aPresContextthe presentation context
aFormSubmissionthe form submission object (out param)

Definition at line 1135 of file nsFormSubmission.cpp.

{
  nsresult rv = NS_OK;

  //
  // Get all the information necessary to encode the form data
  //

  // Get BIDI options
  PRUint8 ctrlsModAtSubmit = 0;
  PRUint32 bidiOptions = aPresContext->GetBidi();
  ctrlsModAtSubmit = GET_BIDI_OPTION_CONTROLSTEXTMODE(bidiOptions);

  // Get encoding type (default: urlencoded)
  PRInt32 enctype = NS_FORM_ENCTYPE_URLENCODED;
  nsFormSubmission::GetEnumAttr(aForm, nsHTMLAtoms::enctype, &enctype);

  // Get method (default: GET)
  PRInt32 method = NS_FORM_METHOD_GET;
  nsFormSubmission::GetEnumAttr(aForm, nsHTMLAtoms::method, &method);

  // Get charset
  nsCAutoString charset;
  nsFormSubmission::GetSubmitCharset(aForm, ctrlsModAtSubmit, charset);

  // Get unicode encoder
  nsCOMPtr<nsISaveAsCharset> encoder;
  nsFormSubmission::GetEncoder(aForm, aPresContext, charset,
                               getter_AddRefs(encoder));

  // Get form processor
  nsCOMPtr<nsIFormProcessor> formProcessor =
    do_GetService(kFormProcessorCID, &rv);

  //
  // Choose encoder
  //
  // If enctype=multipart/form-data and method=post, do multipart
  // Else do URL encoded
  // NOTE:
  // The rule used to be, if enctype=multipart/form-data, do multipart
  // Else do URL encoded
  if (method == NS_FORM_METHOD_POST &&
      enctype == NS_FORM_ENCTYPE_MULTIPART) {
    *aFormSubmission = new nsFSMultipartFormData(charset, encoder,
                                                 formProcessor, bidiOptions);
  } else if (method == NS_FORM_METHOD_POST &&
             enctype == NS_FORM_ENCTYPE_TEXTPLAIN) {
    *aFormSubmission = new nsFSTextPlain(charset, encoder,
                                         formProcessor, bidiOptions);
  } else {
    if (enctype == NS_FORM_ENCTYPE_MULTIPART ||
        enctype == NS_FORM_ENCTYPE_TEXTPLAIN) {
      nsAutoString enctypeStr;
      aForm->GetAttr(kNameSpaceID_None, nsHTMLAtoms::enctype, enctypeStr);
      SendJSWarning(aForm, "ForgotPostWarning", PromiseFlatString(enctypeStr));
    }
    *aFormSubmission = new nsFSURLEncoded(charset, encoder,
                                          formProcessor, bidiOptions, method);
  }
  NS_ENSURE_TRUE(*aFormSubmission, NS_ERROR_OUT_OF_MEMORY);
  NS_ADDREF(*aFormSubmission);


  // This ASSUMES that all encodings above inherit from nsFormSubmission, which
  // they currently do.  If that changes, change this too.
  NS_STATIC_CAST(nsFormSubmission*, *aFormSubmission)->Init();

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void HandleMailtoSubject ( nsCString aPath) [static]

Definition at line 409 of file nsFormSubmission.cpp.

                                      {

  // Walk through the string and see if we have a subject already.
  PRBool hasSubject = PR_FALSE;
  PRBool hasParams = PR_FALSE;
  PRInt32 paramSep = aPath.FindChar('?');
  while (paramSep != kNotFound && paramSep < (PRInt32)aPath.Length()) {
    hasParams = PR_TRUE;

    // Get the end of the name at the = op.  If it is *after* the next &,
    // assume that someone made a parameter without an = in it
    PRInt32 nameEnd = aPath.FindChar('=', paramSep+1);
    PRInt32 nextParamSep = aPath.FindChar('&', paramSep+1);
    if (nextParamSep == kNotFound) {
      nextParamSep = aPath.Length();
    }

    // If the = op is after the &, this parameter is a name without value.
    // If there is no = op, same thing.
    if (nameEnd == kNotFound || nextParamSep < nameEnd) {
      nameEnd = nextParamSep;
    }

    if (nameEnd != kNotFound) {
      if (Substring(aPath, paramSep+1, nameEnd-(paramSep+1)) ==
          NS_LITERAL_CSTRING("subject")) {
        hasSubject = PR_TRUE;
        break;
      }
    }

    paramSep = nextParamSep;
  }

  // If there is no subject, append a preformed subject to the mailto line
  if (!hasSubject) {
    if (hasParams) {
      aPath.Append('&');
    } else {
      aPath.Append('?');
    }

    // Get the default subject
    nsXPIDLString brandName;
    nsresult rv =
      nsContentUtils::GetLocalizedString(nsContentUtils::eBRAND_PROPERTIES,
                                         "brandShortName", brandName);
    if (NS_FAILED(rv))
      return;
    const PRUnichar *formatStrings[] = { brandName.get() };
    nsXPIDLString subjectStr;
    rv = nsContentUtils::FormatLocalizedString(
                                           nsContentUtils::eFORMS_PROPERTIES,
                                           "DefaultFormSubject",
                                           formatStrings,
                                           NS_ARRAY_LENGTH(formatStrings),
                                           subjectStr);
    if (NS_FAILED(rv))
      return;
    aPath.AppendLiteral("subject=");
    nsCString subjectStrEscaped;
    aPath.Append(NS_EscapeURL(NS_ConvertUTF16toUTF8(subjectStr), esc_Query,
                              subjectStrEscaped));
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static NS_DEFINE_CID ( kFormProcessorCID  ,
NS_FORMPROCESSOR_CID   
) [static]
static NS_DEFINE_CID ( kCharsetAliasCID  ,
NS_CHARSETALIAS_CID   
) [static]
static NS_INTERFACE_MAP_END nsresult SendJSWarning ( nsIContent aContent,
const char *  aWarningName 
) [static]

Send a warning to the JS console.

Parameters:
aContentthe content the warning is about
aWarningNamethe internationalized name of the warning within layout/html/forms/src/HtmlProperties.js

Definition at line 1096 of file nsFormSubmission.cpp.

{
  return SendJSWarning(aContent, aWarningName, nsnull, 0);
}

Here is the caller graph for this function:

static nsresult SendJSWarning ( nsIContent aContent,
const char *  aWarningName,
const nsAFlatString aWarningArg1 
) [static]

Send a warning to the JS console.

Parameters:
aContentthe content the warning is about
aWarningNamethe internationalized name of the warning within layout/html/forms/src/HtmlProperties.js
aWarningArg1an argument to replace a S in the warning

Definition at line 1103 of file nsFormSubmission.cpp.

{
  const PRUnichar* formatStrings[1] = { aWarningArg1.get() };
  return SendJSWarning(aContent, aWarningName, formatStrings, 1);
}

Here is the call graph for this function:

static nsresult SendJSWarning ( nsIContent aContent,
const char *  aWarningName,
const PRUnichar **  aWarningArgs,
PRUint32  aWarningArgsLen 
) [static]

Send a warning to the JS console.

Parameters:
aContentthe content the warning is about
aWarningNamethe internationalized name of the warning within layout/html/forms/src/HtmlProperties.js
aWarningArgsan array of strings to replace S's in the warning
aWarningArgsLenthe number of strings in the array

Definition at line 1112 of file nsFormSubmission.cpp.

{
  // Get the document URL to use as the filename

  nsIDocument* document = aContent->GetDocument();
  nsIURI *documentURI = nsnull;
  if (document) {
    documentURI = document->GetDocumentURI();
    NS_ENSURE_TRUE(documentURI, NS_ERROR_UNEXPECTED);
  }

  return nsContentUtils::ReportToConsole(nsContentUtils::eFORMS_PROPERTIES,
                                         aWarningName,
                                         aWarningArgs, aWarningArgsLen,
                                         documentURI,
                                         EmptyString(), 0, 0,
                                         nsIScriptError::warningFlag,
                                         "HTML");
}

Here is the call graph for this function: