content/base/src/nsFormData.cpp

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include "nsFormData.h"
     6 #include "nsIVariant.h"
     7 #include "nsIInputStream.h"
     8 #include "nsIDOMFile.h"
     9 #include "mozilla/dom/HTMLFormElement.h"
    10 #include "mozilla/dom/FormDataBinding.h"
    12 using namespace mozilla;
    13 using namespace mozilla::dom;
    15 nsFormData::nsFormData(nsISupports* aOwner)
    16   : nsFormSubmission(NS_LITERAL_CSTRING("UTF-8"), nullptr)
    17   , mOwner(aOwner)
    18 {
    19   SetIsDOMBinding();
    20 }
    22 // -------------------------------------------------------------------------
    23 // nsISupports
    25 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(nsFormData, mOwner)
    26 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsFormData)
    27 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsFormData)
    28 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsFormData)
    29   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
    30   NS_INTERFACE_MAP_ENTRY(nsIDOMFormData)
    31   NS_INTERFACE_MAP_ENTRY(nsIXHRSendable)
    32   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMFormData)
    33 NS_INTERFACE_MAP_END
    35 // -------------------------------------------------------------------------
    36 // nsFormSubmission
    37 nsresult
    38 nsFormData::GetEncodedSubmission(nsIURI* aURI,
    39                                  nsIInputStream** aPostDataStream)
    40 {
    41   NS_NOTREACHED("Shouldn't call nsFormData::GetEncodedSubmission");
    42   return NS_OK;
    43 }
    45 void
    46 nsFormData::Append(const nsAString& aName, const nsAString& aValue)
    47 {
    48   AddNameValuePair(aName, aValue);
    49 }
    51 void
    52 nsFormData::Append(const nsAString& aName, nsIDOMBlob* aBlob,
    53                    const Optional<nsAString>& aFilename)
    54 {
    55   nsString filename;
    56   if (aFilename.WasPassed()) {
    57     filename = aFilename.Value();
    58   } else {
    59     filename.SetIsVoid(true);
    60   }
    61   AddNameFilePair(aName, aBlob, filename);
    62 }
    64 // -------------------------------------------------------------------------
    65 // nsIDOMFormData
    67 NS_IMETHODIMP
    68 nsFormData::Append(const nsAString& aName, nsIVariant* aValue)
    69 {
    70   uint16_t dataType;
    71   nsresult rv = aValue->GetDataType(&dataType);
    72   NS_ENSURE_SUCCESS(rv, rv);
    74   if (dataType == nsIDataType::VTYPE_INTERFACE ||
    75       dataType == nsIDataType::VTYPE_INTERFACE_IS) {
    76     nsCOMPtr<nsISupports> supports;
    77     nsID *iid;
    78     rv = aValue->GetAsInterface(&iid, getter_AddRefs(supports));
    79     NS_ENSURE_SUCCESS(rv, rv);
    81     nsMemory::Free(iid);
    83     nsCOMPtr<nsIDOMBlob> domBlob = do_QueryInterface(supports);
    84     if (domBlob) {
    85       Optional<nsAString> temp;
    86       Append(aName, domBlob, temp);
    87       return NS_OK;
    88     }
    89   }
    91   char16_t* stringData = nullptr;
    92   uint32_t stringLen = 0;
    93   rv = aValue->GetAsWStringWithSize(&stringLen, &stringData);
    94   NS_ENSURE_SUCCESS(rv, rv);
    96   nsString valAsString;
    97   valAsString.Adopt(stringData, stringLen);
    99   Append(aName, valAsString);
   100   return NS_OK;
   101 }
   103 /* virtual */ JSObject*
   104 nsFormData::WrapObject(JSContext* aCx)
   105 {
   106   return FormDataBinding::Wrap(aCx, this);
   107 }
   109 /* static */ already_AddRefed<nsFormData>
   110 nsFormData::Constructor(const GlobalObject& aGlobal,
   111                         const Optional<NonNull<HTMLFormElement> >& aFormElement,
   112                         ErrorResult& aRv)
   113 {
   114   nsRefPtr<nsFormData> formData = new nsFormData(aGlobal.GetAsSupports());
   115   if (aFormElement.WasPassed()) {
   116     aRv = aFormElement.Value().WalkFormElements(formData);
   117   }
   118   return formData.forget();
   119 }
   121 // -------------------------------------------------------------------------
   122 // nsIXHRSendable
   124 NS_IMETHODIMP
   125 nsFormData::GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength,
   126                         nsACString& aContentType, nsACString& aCharset)
   127 {
   128   nsFSMultipartFormData fs(NS_LITERAL_CSTRING("UTF-8"), nullptr);
   130   for (uint32_t i = 0; i < mFormData.Length(); ++i) {
   131     if (mFormData[i].valueIsFile) {
   132       fs.AddNameFilePair(mFormData[i].name, mFormData[i].fileValue,
   133                          mFormData[i].filename);
   134     }
   135     else {
   136       fs.AddNameValuePair(mFormData[i].name, mFormData[i].stringValue);
   137     }
   138   }
   140   fs.GetContentType(aContentType);
   141   aCharset.Truncate();
   142   *aContentLength = 0;
   143   NS_ADDREF(*aBody = fs.GetSubmissionBody(aContentLength));
   145   return NS_OK;
   146 }

mercurial