2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-02-24 21:58:18 -08:00
|
|
|
|
|
|
|
#include "nsFormData.h"
|
|
|
|
#include "nsIVariant.h"
|
|
|
|
#include "nsIInputStream.h"
|
2014-10-08 09:15:23 -07:00
|
|
|
#include "mozilla/dom/File.h"
|
2013-06-19 07:24:37 -07:00
|
|
|
#include "mozilla/dom/HTMLFormElement.h"
|
2010-02-24 21:58:18 -08:00
|
|
|
|
2012-12-11 10:09:56 -08:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
nsFormData::nsFormData(nsISupports* aOwner)
|
2012-07-30 07:20:58 -07:00
|
|
|
: nsFormSubmission(NS_LITERAL_CSTRING("UTF-8"), nullptr)
|
2012-12-11 10:09:56 -08:00
|
|
|
, mOwner(aOwner)
|
2010-02-24 21:58:18 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// nsISupports
|
|
|
|
|
2014-10-08 09:15:22 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsFormData)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsFormData)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwner)
|
|
|
|
|
|
|
|
for (uint32_t i = 0, len = tmp->mFormData.Length(); i < len; ++i) {
|
|
|
|
ImplCycleCollectionUnlink(tmp->mFormData[i].fileValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsFormData)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwner)
|
|
|
|
|
|
|
|
for (uint32_t i = 0, len = tmp->mFormData.Length(); i < len; ++i) {
|
|
|
|
ImplCycleCollectionTraverse(cb,tmp->mFormData[i].fileValue,
|
|
|
|
"mFormData[i].fileValue", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(nsFormData)
|
|
|
|
|
2012-12-11 10:09:56 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsFormData)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsFormData)
|
2014-10-08 09:15:22 -07:00
|
|
|
|
2012-12-11 10:09:56 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsFormData)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2010-02-24 21:58:18 -08:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMFormData)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIXHRSendable)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMFormData)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// nsFormSubmission
|
|
|
|
nsresult
|
|
|
|
nsFormData::GetEncodedSubmission(nsIURI* aURI,
|
|
|
|
nsIInputStream** aPostDataStream)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("Shouldn't call nsFormData::GetEncodedSubmission");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-11 10:09:56 -08:00
|
|
|
void
|
|
|
|
nsFormData::Append(const nsAString& aName, const nsAString& aValue)
|
2010-02-24 21:58:18 -08:00
|
|
|
{
|
2013-03-03 10:30:13 -08:00
|
|
|
AddNameValuePair(aName, aValue);
|
2010-02-24 21:58:18 -08:00
|
|
|
}
|
|
|
|
|
2012-12-11 10:09:56 -08:00
|
|
|
void
|
2014-10-08 09:15:23 -07:00
|
|
|
nsFormData::Append(const nsAString& aName, File& aBlob,
|
2013-03-03 10:30:13 -08:00
|
|
|
const Optional<nsAString>& aFilename)
|
2010-02-24 21:58:18 -08:00
|
|
|
{
|
2013-03-03 10:30:13 -08:00
|
|
|
nsString filename;
|
|
|
|
if (aFilename.WasPassed()) {
|
|
|
|
filename = aFilename.Value();
|
|
|
|
} else {
|
|
|
|
filename.SetIsVoid(true);
|
|
|
|
}
|
2014-10-08 09:15:22 -07:00
|
|
|
AddNameFilePair(aName, &aBlob, filename);
|
2010-02-24 21:58:18 -08:00
|
|
|
}
|
|
|
|
|
2015-01-28 17:04:28 -08:00
|
|
|
void
|
|
|
|
nsFormData::Delete(const nsAString& aName)
|
|
|
|
{
|
|
|
|
// We have to use this slightly awkward for loop since uint32_t >= 0 is an
|
|
|
|
// error for being always true.
|
|
|
|
for (uint32_t i = mFormData.Length(); i-- > 0; ) {
|
|
|
|
if (aName.Equals(mFormData[i].name)) {
|
|
|
|
mFormData.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsFormData::ExtractValue(const FormDataTuple& aTuple,
|
|
|
|
OwningFileOrUSVString* aOutValue)
|
|
|
|
{
|
|
|
|
if (aTuple.valueIsFile) {
|
|
|
|
aOutValue->SetAsFile() = aTuple.fileValue;
|
|
|
|
} else {
|
|
|
|
aOutValue->SetAsUSVString() = aTuple.stringValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsFormData::Get(const nsAString& aName,
|
|
|
|
Nullable<OwningFileOrUSVString>& aOutValue)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mFormData.Length(); ++i) {
|
|
|
|
if (aName.Equals(mFormData[i].name)) {
|
|
|
|
ExtractValue(mFormData[i], &aOutValue.SetValue());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aOutValue.SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsFormData::GetAll(const nsAString& aName,
|
|
|
|
nsTArray<OwningFileOrUSVString>& aValues)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mFormData.Length(); ++i) {
|
|
|
|
if (aName.Equals(mFormData[i].name)) {
|
|
|
|
OwningFileOrUSVString* element = aValues.AppendElement();
|
|
|
|
ExtractValue(mFormData[i], element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsFormData::Has(const nsAString& aName)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mFormData.Length(); ++i) {
|
|
|
|
if (aName.Equals(mFormData[i].name)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsFormData::FormDataTuple*
|
|
|
|
nsFormData::RemoveAllOthersAndGetFirstFormDataTuple(const nsAString& aName)
|
|
|
|
{
|
|
|
|
FormDataTuple* lastFoundTuple = nullptr;
|
|
|
|
uint32_t lastFoundIndex = mFormData.Length();
|
|
|
|
// We have to use this slightly awkward for loop since uint32_t >= 0 is an
|
|
|
|
// error for being always true.
|
|
|
|
for (uint32_t i = mFormData.Length(); i-- > 0; ) {
|
|
|
|
if (aName.Equals(mFormData[i].name)) {
|
|
|
|
if (lastFoundTuple) {
|
|
|
|
// The one we found earlier was not the first one, we can remove it.
|
|
|
|
mFormData.RemoveElementAt(lastFoundIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
lastFoundTuple = &mFormData[i];
|
|
|
|
lastFoundIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastFoundTuple;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsFormData::Set(const nsAString& aName, File& aBlob,
|
|
|
|
const Optional<nsAString>& aFilename)
|
|
|
|
{
|
|
|
|
FormDataTuple* tuple = RemoveAllOthersAndGetFirstFormDataTuple(aName);
|
|
|
|
if (tuple) {
|
|
|
|
nsAutoString filename;
|
|
|
|
if (aFilename.WasPassed()) {
|
|
|
|
filename = aFilename.Value();
|
|
|
|
} else {
|
|
|
|
filename.SetIsVoid(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetNameFilePair(tuple, aName, &aBlob, filename);
|
|
|
|
} else {
|
|
|
|
Append(aName, aBlob, aFilename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsFormData::Set(const nsAString& aName, const nsAString& aValue)
|
|
|
|
{
|
|
|
|
FormDataTuple* tuple = RemoveAllOthersAndGetFirstFormDataTuple(aName);
|
|
|
|
if (tuple) {
|
|
|
|
SetNameValuePair(tuple, aName, aValue);
|
|
|
|
} else {
|
|
|
|
Append(aName, aValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-24 21:58:18 -08:00
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// nsIDOMFormData
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFormData::Append(const nsAString& aName, nsIVariant* aValue)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t dataType;
|
2010-02-24 21:58:18 -08:00
|
|
|
nsresult rv = aValue->GetDataType(&dataType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (dataType == nsIDataType::VTYPE_INTERFACE ||
|
|
|
|
dataType == nsIDataType::VTYPE_INTERFACE_IS) {
|
|
|
|
nsCOMPtr<nsISupports> supports;
|
|
|
|
nsID *iid;
|
|
|
|
rv = aValue->GetAsInterface(&iid, getter_AddRefs(supports));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsMemory::Free(iid);
|
|
|
|
|
2010-10-13 16:25:33 -07:00
|
|
|
nsCOMPtr<nsIDOMBlob> domBlob = do_QueryInterface(supports);
|
2014-10-08 09:15:23 -07:00
|
|
|
nsRefPtr<File> blob = static_cast<File*>(domBlob.get());
|
2010-10-13 16:25:33 -07:00
|
|
|
if (domBlob) {
|
2013-03-03 12:34:47 -08:00
|
|
|
Optional<nsAString> temp;
|
2014-10-08 09:15:22 -07:00
|
|
|
Append(aName, *blob, temp);
|
2012-12-11 10:09:56 -08:00
|
|
|
return NS_OK;
|
2010-02-24 21:58:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t* stringData = nullptr;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t stringLen = 0;
|
2010-02-24 21:58:18 -08:00
|
|
|
rv = aValue->GetAsWStringWithSize(&stringLen, &stringData);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsString valAsString;
|
|
|
|
valAsString.Adopt(stringData, stringLen);
|
|
|
|
|
2012-12-11 10:09:56 -08:00
|
|
|
Append(aName, valAsString);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
nsFormData::WrapObject(JSContext* aCx)
|
2012-12-11 10:09:56 -08:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return FormDataBinding::Wrap(aCx, this);
|
2012-12-11 10:09:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<nsFormData>
|
2012-12-03 08:07:49 -08:00
|
|
|
nsFormData::Constructor(const GlobalObject& aGlobal,
|
2013-06-19 07:24:37 -07:00
|
|
|
const Optional<NonNull<HTMLFormElement> >& aFormElement,
|
2012-12-11 10:09:56 -08:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2013-08-22 22:17:08 -07:00
|
|
|
nsRefPtr<nsFormData> formData = new nsFormData(aGlobal.GetAsSupports());
|
2012-12-11 10:09:56 -08:00
|
|
|
if (aFormElement.WasPassed()) {
|
2013-06-19 11:48:43 -07:00
|
|
|
aRv = aFormElement.Value().WalkFormElements(formData);
|
2012-12-11 10:09:56 -08:00
|
|
|
}
|
|
|
|
return formData.forget();
|
2010-02-24 21:58:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
2010-09-15 15:52:02 -07:00
|
|
|
// nsIXHRSendable
|
2010-02-24 21:58:18 -08:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-09-19 15:15:32 -07:00
|
|
|
nsFormData::GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength,
|
|
|
|
nsACString& aContentType, nsACString& aCharset)
|
2010-02-24 21:58:18 -08:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
nsFSMultipartFormData fs(NS_LITERAL_CSTRING("UTF-8"), nullptr);
|
2013-03-03 10:30:13 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mFormData.Length(); ++i) {
|
2010-02-24 21:58:18 -08:00
|
|
|
if (mFormData[i].valueIsFile) {
|
2013-03-03 10:30:13 -08:00
|
|
|
fs.AddNameFilePair(mFormData[i].name, mFormData[i].fileValue,
|
|
|
|
mFormData[i].filename);
|
2010-02-24 21:58:18 -08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
fs.AddNameValuePair(mFormData[i].name, mFormData[i].stringValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.GetContentType(aContentType);
|
|
|
|
aCharset.Truncate();
|
2012-09-19 15:15:32 -07:00
|
|
|
*aContentLength = 0;
|
|
|
|
NS_ADDREF(*aBody = fs.GetSubmissionBody(aContentLength));
|
2010-02-24 21:58:18 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|