content/base/src/nsFormData.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/src/nsFormData.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,146 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "nsFormData.h"
     1.9 +#include "nsIVariant.h"
    1.10 +#include "nsIInputStream.h"
    1.11 +#include "nsIDOMFile.h"
    1.12 +#include "mozilla/dom/HTMLFormElement.h"
    1.13 +#include "mozilla/dom/FormDataBinding.h"
    1.14 +
    1.15 +using namespace mozilla;
    1.16 +using namespace mozilla::dom;
    1.17 +
    1.18 +nsFormData::nsFormData(nsISupports* aOwner)
    1.19 +  : nsFormSubmission(NS_LITERAL_CSTRING("UTF-8"), nullptr)
    1.20 +  , mOwner(aOwner)
    1.21 +{
    1.22 +  SetIsDOMBinding();
    1.23 +}
    1.24 +
    1.25 +// -------------------------------------------------------------------------
    1.26 +// nsISupports
    1.27 +
    1.28 +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(nsFormData, mOwner)
    1.29 +NS_IMPL_CYCLE_COLLECTING_ADDREF(nsFormData)
    1.30 +NS_IMPL_CYCLE_COLLECTING_RELEASE(nsFormData)
    1.31 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsFormData)
    1.32 +  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
    1.33 +  NS_INTERFACE_MAP_ENTRY(nsIDOMFormData)
    1.34 +  NS_INTERFACE_MAP_ENTRY(nsIXHRSendable)
    1.35 +  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMFormData)
    1.36 +NS_INTERFACE_MAP_END
    1.37 +
    1.38 +// -------------------------------------------------------------------------
    1.39 +// nsFormSubmission
    1.40 +nsresult
    1.41 +nsFormData::GetEncodedSubmission(nsIURI* aURI,
    1.42 +                                 nsIInputStream** aPostDataStream)
    1.43 +{
    1.44 +  NS_NOTREACHED("Shouldn't call nsFormData::GetEncodedSubmission");
    1.45 +  return NS_OK;
    1.46 +}
    1.47 +
    1.48 +void
    1.49 +nsFormData::Append(const nsAString& aName, const nsAString& aValue)
    1.50 +{
    1.51 +  AddNameValuePair(aName, aValue);
    1.52 +}
    1.53 +
    1.54 +void
    1.55 +nsFormData::Append(const nsAString& aName, nsIDOMBlob* aBlob,
    1.56 +                   const Optional<nsAString>& aFilename)
    1.57 +{
    1.58 +  nsString filename;
    1.59 +  if (aFilename.WasPassed()) {
    1.60 +    filename = aFilename.Value();
    1.61 +  } else {
    1.62 +    filename.SetIsVoid(true);
    1.63 +  }
    1.64 +  AddNameFilePair(aName, aBlob, filename);
    1.65 +}
    1.66 +
    1.67 +// -------------------------------------------------------------------------
    1.68 +// nsIDOMFormData
    1.69 +
    1.70 +NS_IMETHODIMP
    1.71 +nsFormData::Append(const nsAString& aName, nsIVariant* aValue)
    1.72 +{
    1.73 +  uint16_t dataType;
    1.74 +  nsresult rv = aValue->GetDataType(&dataType);
    1.75 +  NS_ENSURE_SUCCESS(rv, rv);
    1.76 +
    1.77 +  if (dataType == nsIDataType::VTYPE_INTERFACE ||
    1.78 +      dataType == nsIDataType::VTYPE_INTERFACE_IS) {
    1.79 +    nsCOMPtr<nsISupports> supports;
    1.80 +    nsID *iid;
    1.81 +    rv = aValue->GetAsInterface(&iid, getter_AddRefs(supports));
    1.82 +    NS_ENSURE_SUCCESS(rv, rv);
    1.83 +
    1.84 +    nsMemory::Free(iid);
    1.85 +
    1.86 +    nsCOMPtr<nsIDOMBlob> domBlob = do_QueryInterface(supports);
    1.87 +    if (domBlob) {
    1.88 +      Optional<nsAString> temp;
    1.89 +      Append(aName, domBlob, temp);
    1.90 +      return NS_OK;
    1.91 +    }
    1.92 +  }
    1.93 +
    1.94 +  char16_t* stringData = nullptr;
    1.95 +  uint32_t stringLen = 0;
    1.96 +  rv = aValue->GetAsWStringWithSize(&stringLen, &stringData);
    1.97 +  NS_ENSURE_SUCCESS(rv, rv);
    1.98 +
    1.99 +  nsString valAsString;
   1.100 +  valAsString.Adopt(stringData, stringLen);
   1.101 +
   1.102 +  Append(aName, valAsString);
   1.103 +  return NS_OK;
   1.104 +}
   1.105 +
   1.106 +/* virtual */ JSObject*
   1.107 +nsFormData::WrapObject(JSContext* aCx)
   1.108 +{
   1.109 +  return FormDataBinding::Wrap(aCx, this);
   1.110 +}
   1.111 +
   1.112 +/* static */ already_AddRefed<nsFormData>
   1.113 +nsFormData::Constructor(const GlobalObject& aGlobal,
   1.114 +                        const Optional<NonNull<HTMLFormElement> >& aFormElement,
   1.115 +                        ErrorResult& aRv)
   1.116 +{
   1.117 +  nsRefPtr<nsFormData> formData = new nsFormData(aGlobal.GetAsSupports());
   1.118 +  if (aFormElement.WasPassed()) {
   1.119 +    aRv = aFormElement.Value().WalkFormElements(formData);
   1.120 +  }
   1.121 +  return formData.forget();
   1.122 +}
   1.123 +
   1.124 +// -------------------------------------------------------------------------
   1.125 +// nsIXHRSendable
   1.126 +
   1.127 +NS_IMETHODIMP
   1.128 +nsFormData::GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength,
   1.129 +                        nsACString& aContentType, nsACString& aCharset)
   1.130 +{
   1.131 +  nsFSMultipartFormData fs(NS_LITERAL_CSTRING("UTF-8"), nullptr);
   1.132 +
   1.133 +  for (uint32_t i = 0; i < mFormData.Length(); ++i) {
   1.134 +    if (mFormData[i].valueIsFile) {
   1.135 +      fs.AddNameFilePair(mFormData[i].name, mFormData[i].fileValue,
   1.136 +                         mFormData[i].filename);
   1.137 +    }
   1.138 +    else {
   1.139 +      fs.AddNameValuePair(mFormData[i].name, mFormData[i].stringValue);
   1.140 +    }
   1.141 +  }
   1.142 +
   1.143 +  fs.GetContentType(aContentType);
   1.144 +  aCharset.Truncate();
   1.145 +  *aContentLength = 0;
   1.146 +  NS_ADDREF(*aBody = fs.GetSubmissionBody(aContentLength));
   1.147 +
   1.148 +  return NS_OK;
   1.149 +}

mercurial