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"
|
2010-09-05 11:00:05 -07:00
|
|
|
#include "nsIDOMFile.h"
|
2010-09-15 15:52:02 -07:00
|
|
|
#include "nsHTMLFormElement.h"
|
2012-12-11 10:09:56 -08:00
|
|
|
#include "mozilla/dom/FormDataBinding.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
|
|
|
{
|
2012-12-11 10:09:56 -08:00
|
|
|
SetIsDOMBinding();
|
2010-02-24 21:58:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// nsISupports
|
|
|
|
|
2012-12-11 10:09:56 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(nsFormData, mOwner)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsFormData)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsFormData)
|
|
|
|
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
|
|
|
{
|
|
|
|
FormDataTuple* data = mFormData.AppendElement();
|
|
|
|
data->name = aName;
|
|
|
|
data->stringValue = aValue;
|
2011-10-17 07:59:28 -07:00
|
|
|
data->valueIsFile = false;
|
2010-02-24 21:58:18 -08:00
|
|
|
}
|
|
|
|
|
2012-12-11 10:09:56 -08:00
|
|
|
void
|
|
|
|
nsFormData::Append(const nsAString& aName, nsIDOMBlob* aBlob)
|
2010-02-24 21:58:18 -08:00
|
|
|
{
|
|
|
|
FormDataTuple* data = mFormData.AppendElement();
|
|
|
|
data->name = aName;
|
2010-10-13 16:25:33 -07:00
|
|
|
data->fileValue = aBlob;
|
2011-10-17 07:59:28 -07:00
|
|
|
data->valueIsFile = true;
|
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);
|
|
|
|
if (domBlob) {
|
2012-12-11 10:09:56 -08:00
|
|
|
Append(aName, domBlob);
|
|
|
|
return NS_OK;
|
2010-02-24 21:58:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
PRUnichar* 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*
|
|
|
|
nsFormData::WrapObject(JSContext* aCx, JSObject* aScope, bool* aTriedToWrap)
|
|
|
|
{
|
|
|
|
return FormDataBinding::Wrap(aCx, aScope, this, aTriedToWrap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<nsFormData>
|
|
|
|
nsFormData::Constructor(nsISupports* aGlobal,
|
|
|
|
const Optional<nsHTMLFormElement*>& aFormElement,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsFormData> formData = new nsFormData(aGlobal);
|
|
|
|
if (aFormElement.WasPassed()) {
|
|
|
|
MOZ_ASSERT(aFormElement.Value());
|
|
|
|
aRv = aFormElement.Value()->WalkFormElements(formData);
|
|
|
|
}
|
|
|
|
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);
|
2010-02-24 21:58:18 -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) {
|
|
|
|
fs.AddNameFilePair(mFormData[i].name, mFormData[i].fileValue);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|