2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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-25 08:58:18 +03:00
|
|
|
|
|
|
|
#ifndef nsFormData_h__
|
|
|
|
#define nsFormData_h__
|
|
|
|
|
2013-05-30 00:43:41 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2010-02-25 08:58:18 +03:00
|
|
|
#include "nsIDOMFormData.h"
|
|
|
|
#include "nsIXMLHttpRequest.h"
|
2010-05-26 16:49:38 +04:00
|
|
|
#include "nsFormSubmission.h"
|
2012-12-11 22:09:56 +04:00
|
|
|
#include "nsWrapperCache.h"
|
2010-02-25 08:58:18 +03:00
|
|
|
#include "nsTArray.h"
|
2012-12-11 22:09:56 +04:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2013-03-03 22:30:13 +04:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2015-05-08 16:17:35 +03:00
|
|
|
#include "mozilla/dom/File.h"
|
2015-01-29 04:04:28 +03:00
|
|
|
#include "mozilla/dom/FormDataBinding.h"
|
2010-02-25 08:58:18 +03:00
|
|
|
|
2012-12-11 22:09:56 +04:00
|
|
|
namespace mozilla {
|
|
|
|
class ErrorResult;
|
|
|
|
|
|
|
|
namespace dom {
|
2013-06-19 18:24:37 +04:00
|
|
|
class HTMLFormElement;
|
2012-12-03 20:07:49 +04:00
|
|
|
class GlobalObject;
|
2012-12-11 22:09:56 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsFormData final : public nsIDOMFormData,
|
2015-03-27 21:52:19 +03:00
|
|
|
public nsIXHRSendable,
|
|
|
|
public nsFormSubmission,
|
|
|
|
public nsWrapperCache
|
2010-02-25 08:58:18 +03:00
|
|
|
{
|
2015-01-29 04:04:28 +03:00
|
|
|
private:
|
2014-06-25 06:09:15 +04:00
|
|
|
~nsFormData() {}
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
typedef mozilla::dom::Blob Blob;
|
2015-01-29 04:04:28 +03:00
|
|
|
typedef mozilla::dom::File File;
|
2015-05-12 15:09:51 +03:00
|
|
|
|
2015-01-29 04:04:28 +03:00
|
|
|
struct FormDataTuple
|
|
|
|
{
|
|
|
|
nsString name;
|
|
|
|
nsString stringValue;
|
|
|
|
nsRefPtr<File> fileValue;
|
|
|
|
bool valueIsFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Returns the FormDataTuple to modify. This may be null, in which case
|
|
|
|
// no element with aName was found.
|
|
|
|
FormDataTuple*
|
|
|
|
RemoveAllOthersAndGetFirstFormDataTuple(const nsAString& aName);
|
|
|
|
|
|
|
|
void SetNameValuePair(FormDataTuple* aData,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aValue)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aData);
|
|
|
|
aData->name = aName;
|
|
|
|
aData->stringValue = aValue;
|
|
|
|
aData->valueIsFile = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNameFilePair(FormDataTuple* aData,
|
|
|
|
const nsAString& aName,
|
2015-05-12 15:09:51 +03:00
|
|
|
File* aFile)
|
2015-01-29 04:04:28 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aData);
|
|
|
|
aData->name = aName;
|
2015-05-12 15:09:51 +03:00
|
|
|
aData->fileValue = aFile;
|
2015-01-29 04:04:28 +03:00
|
|
|
aData->valueIsFile = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExtractValue(const FormDataTuple& aTuple,
|
|
|
|
mozilla::dom::OwningFileOrUSVString* aOutValue);
|
2010-02-25 08:58:18 +03:00
|
|
|
public:
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit nsFormData(nsISupports* aOwner = nullptr);
|
2012-12-11 22:09:56 +04:00
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(nsFormData,
|
|
|
|
nsIDOMFormData)
|
2010-02-25 08:58:18 +03:00
|
|
|
|
|
|
|
NS_DECL_NSIDOMFORMDATA
|
|
|
|
NS_DECL_NSIXHRSENDABLE
|
|
|
|
|
2012-12-11 22:09:56 +04:00
|
|
|
// nsWrapperCache
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
2012-12-11 22:09:56 +04:00
|
|
|
|
|
|
|
// WebIDL
|
|
|
|
nsISupports*
|
|
|
|
GetParentObject() const
|
|
|
|
{
|
|
|
|
return mOwner;
|
|
|
|
}
|
|
|
|
static already_AddRefed<nsFormData>
|
2012-12-03 20:07:49 +04:00
|
|
|
Constructor(const mozilla::dom::GlobalObject& aGlobal,
|
2013-06-19 18:24:37 +04:00
|
|
|
const mozilla::dom::Optional<mozilla::dom::NonNull<mozilla::dom::HTMLFormElement> >& aFormElement,
|
2012-12-11 22:09:56 +04:00
|
|
|
mozilla::ErrorResult& aRv);
|
|
|
|
void Append(const nsAString& aName, const nsAString& aValue);
|
2015-05-12 15:09:51 +03:00
|
|
|
void Append(const nsAString& aName, Blob& aBlob,
|
2013-03-03 22:30:13 +04:00
|
|
|
const mozilla::dom::Optional<nsAString>& aFilename);
|
2015-01-29 04:04:28 +03:00
|
|
|
void Delete(const nsAString& aName);
|
|
|
|
void Get(const nsAString& aName, mozilla::dom::Nullable<mozilla::dom::OwningFileOrUSVString>& aOutValue);
|
|
|
|
void GetAll(const nsAString& aName, nsTArray<mozilla::dom::OwningFileOrUSVString>& aValues);
|
|
|
|
bool Has(const nsAString& aName);
|
2015-05-12 15:09:51 +03:00
|
|
|
void Set(const nsAString& aName, Blob& aBlob,
|
2015-01-29 04:04:28 +03:00
|
|
|
const mozilla::dom::Optional<nsAString>& aFilename);
|
|
|
|
void Set(const nsAString& aName, const nsAString& aValue);
|
2012-12-11 22:09:56 +04:00
|
|
|
|
2010-02-25 08:58:18 +03:00
|
|
|
// nsFormSubmission
|
|
|
|
virtual nsresult GetEncodedSubmission(nsIURI* aURI,
|
2015-03-21 19:28:04 +03:00
|
|
|
nsIInputStream** aPostDataStream) override;
|
2010-02-25 08:58:18 +03:00
|
|
|
virtual nsresult AddNameValuePair(const nsAString& aName,
|
2015-03-21 19:28:04 +03:00
|
|
|
const nsAString& aValue) override
|
2012-12-11 22:09:56 +04:00
|
|
|
{
|
2013-03-03 22:30:13 +04:00
|
|
|
FormDataTuple* data = mFormData.AppendElement();
|
2015-01-29 04:04:28 +03:00
|
|
|
SetNameValuePair(data, aName, aValue);
|
2012-12-11 22:09:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-02-25 08:58:18 +03:00
|
|
|
virtual nsresult AddNameFilePair(const nsAString& aName,
|
2015-05-12 15:09:51 +03:00
|
|
|
File* aFile) override;
|
2015-01-28 02:16:21 +03:00
|
|
|
|
|
|
|
typedef bool (*FormDataEntryCallback)(const nsString& aName, bool aIsFile,
|
|
|
|
const nsString& aValue,
|
|
|
|
File* aFile, void* aClosure);
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
Length() const
|
|
|
|
{
|
|
|
|
return mFormData.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stops iteration and returns false if any invocation of callback returns
|
|
|
|
// false. Returns true otherwise.
|
|
|
|
bool
|
|
|
|
ForEach(FormDataEntryCallback aFunc, void* aClosure)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mFormData.Length(); ++i) {
|
|
|
|
FormDataTuple& tuple = mFormData[i];
|
|
|
|
if (!aFunc(tuple.name, tuple.valueIsFile, tuple.stringValue,
|
|
|
|
tuple.fileValue, aClosure)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:58:18 +03:00
|
|
|
private:
|
2012-12-11 22:09:56 +04:00
|
|
|
nsCOMPtr<nsISupports> mOwner;
|
|
|
|
|
2010-02-25 08:58:18 +03:00
|
|
|
nsTArray<FormDataTuple> mFormData;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // nsFormData_h__
|