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.)

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

mercurial