2015-05-03 19:32:37 +00: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 11:12:37 +00: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 05:58:18 +00:00
|
|
|
|
2016-01-07 19:30:36 +00:00
|
|
|
#ifndef mozilla_dom_FormData_h
|
|
|
|
#define mozilla_dom_FormData_h
|
2010-02-25 05:58:18 +00:00
|
|
|
|
2013-05-29 20:43:41 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-12-11 18:09:56 +00:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2013-03-03 18:30:13 +00:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2016-06-16 07:24:16 +00:00
|
|
|
#include "mozilla/dom/HTMLFormSubmission.h"
|
2015-05-08 13:17:35 +00:00
|
|
|
#include "mozilla/dom/File.h"
|
2015-01-29 01:04:28 +00:00
|
|
|
#include "mozilla/dom/FormDataBinding.h"
|
2016-01-07 19:30:36 +00:00
|
|
|
#include "nsIDOMFormData.h"
|
|
|
|
#include "nsIXMLHttpRequest.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsWrapperCache.h"
|
2010-02-25 05:58:18 +00:00
|
|
|
|
2012-12-11 18:09:56 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2016-01-07 19:30:36 +00:00
|
|
|
|
2013-06-19 14:24:37 +00:00
|
|
|
class HTMLFormElement;
|
2012-12-03 16:07:49 +00:00
|
|
|
class GlobalObject;
|
2012-12-11 18:09:56 +00:00
|
|
|
|
2016-01-07 19:30:36 +00:00
|
|
|
class FormData final : public nsIDOMFormData,
|
|
|
|
public nsIXHRSendable,
|
2016-06-16 07:24:16 +00:00
|
|
|
public HTMLFormSubmission,
|
2016-01-07 19:30:36 +00:00
|
|
|
public nsWrapperCache
|
2010-02-25 05:58:18 +00:00
|
|
|
{
|
2015-01-29 01:04:28 +00:00
|
|
|
private:
|
2016-01-07 19:30:36 +00:00
|
|
|
~FormData() {}
|
2015-05-12 12:09:51 +00:00
|
|
|
|
2015-01-29 01:04:28 +00:00
|
|
|
struct FormDataTuple
|
|
|
|
{
|
|
|
|
nsString name;
|
2016-02-23 23:01:01 +00:00
|
|
|
bool wasNullBlob;
|
2016-07-14 07:01:31 +00:00
|
|
|
OwningBlobOrDirectoryOrUSVString value;
|
2015-01-29 01:04:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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,
|
2016-02-23 23:01:01 +00:00
|
|
|
const nsAString& aValue,
|
|
|
|
bool aWasNullBlob = false);
|
2015-01-29 01:04:28 +00:00
|
|
|
|
2016-02-17 14:56:19 +00:00
|
|
|
void SetNameFilePair(FormDataTuple* aData,
|
2015-01-29 01:04:28 +00:00
|
|
|
const nsAString& aName,
|
2016-02-17 14:56:19 +00:00
|
|
|
File* aFile);
|
2015-01-29 01:04:28 +00:00
|
|
|
|
2016-07-14 07:01:31 +00:00
|
|
|
void SetNameDirectoryPair(FormDataTuple* aData,
|
|
|
|
const nsAString& aName,
|
|
|
|
Directory* aDirectory);
|
|
|
|
|
2010-02-25 05:58:18 +00:00
|
|
|
public:
|
2016-01-07 19:30:36 +00:00
|
|
|
explicit FormData(nsISupports* aOwner = nullptr);
|
2012-12-11 18:09:56 +00:00
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2016-01-07 19:30:36 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(FormData,
|
2012-12-11 18:09:56 +00:00
|
|
|
nsIDOMFormData)
|
2010-02-25 05:58:18 +00:00
|
|
|
|
|
|
|
NS_DECL_NSIDOMFORMDATA
|
|
|
|
NS_DECL_NSIXHRSENDABLE
|
|
|
|
|
2012-12-11 18:09:56 +00:00
|
|
|
// nsWrapperCache
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
2012-12-11 18:09:56 +00:00
|
|
|
|
|
|
|
// WebIDL
|
|
|
|
nsISupports*
|
|
|
|
GetParentObject() const
|
|
|
|
{
|
|
|
|
return mOwner;
|
|
|
|
}
|
2016-01-07 19:30:36 +00:00
|
|
|
|
|
|
|
static already_AddRefed<FormData>
|
|
|
|
Constructor(const GlobalObject& aGlobal,
|
|
|
|
const Optional<NonNull<HTMLFormElement> >& aFormElement,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2015-12-04 21:15:46 +00:00
|
|
|
void Append(const nsAString& aName, const nsAString& aValue,
|
2016-01-07 19:30:36 +00:00
|
|
|
ErrorResult& aRv);
|
2016-07-14 07:01:31 +00:00
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
void Append(const nsAString& aName, Blob& aBlob,
|
2016-01-07 19:30:36 +00:00
|
|
|
const Optional<nsAString>& aFilename,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2016-07-14 07:01:31 +00:00
|
|
|
void Append(const nsAString& aName, Directory* aDirectory);
|
|
|
|
|
2015-01-29 01:04:28 +00:00
|
|
|
void Delete(const nsAString& aName);
|
2016-01-07 19:30:36 +00:00
|
|
|
|
2016-07-14 07:01:31 +00:00
|
|
|
void Get(const nsAString& aName, Nullable<OwningBlobOrDirectoryOrUSVString>& aOutValue);
|
2016-01-07 19:30:36 +00:00
|
|
|
|
2016-07-14 07:01:31 +00:00
|
|
|
void GetAll(const nsAString& aName, nsTArray<OwningBlobOrDirectoryOrUSVString>& aValues);
|
2016-01-07 19:30:36 +00:00
|
|
|
|
2015-01-29 01:04:28 +00:00
|
|
|
bool Has(const nsAString& aName);
|
2016-01-07 19:30:36 +00:00
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
void Set(const nsAString& aName, Blob& aBlob,
|
2016-01-07 19:30:36 +00:00
|
|
|
const Optional<nsAString>& aFilename,
|
|
|
|
ErrorResult& aRv);
|
2015-12-04 21:15:46 +00:00
|
|
|
void Set(const nsAString& aName, const nsAString& aValue,
|
2016-01-07 19:30:36 +00:00
|
|
|
ErrorResult& aRv);
|
2012-12-11 18:09:56 +00:00
|
|
|
|
2015-10-20 02:06:00 +00:00
|
|
|
uint32_t GetIterableLength() const;
|
2016-01-07 19:30:36 +00:00
|
|
|
|
2015-10-20 02:06:00 +00:00
|
|
|
const nsAString& GetKeyAtIndex(uint32_t aIndex) const;
|
2016-01-07 19:30:36 +00:00
|
|
|
|
2016-07-14 07:01:31 +00:00
|
|
|
const OwningBlobOrDirectoryOrUSVString& GetValueAtIndex(uint32_t aIndex) const;
|
2015-10-20 02:06:00 +00:00
|
|
|
|
2016-06-16 07:24:16 +00:00
|
|
|
// HTMLFormSubmission
|
2016-01-07 19:30:36 +00:00
|
|
|
virtual nsresult
|
|
|
|
GetEncodedSubmission(nsIURI* aURI, nsIInputStream** aPostDataStream) override;
|
|
|
|
|
2010-02-25 05:58:18 +00:00
|
|
|
virtual nsresult AddNameValuePair(const nsAString& aName,
|
2015-03-21 16:28:04 +00:00
|
|
|
const nsAString& aValue) override
|
2012-12-11 18:09:56 +00:00
|
|
|
{
|
2013-03-03 18:30:13 +00:00
|
|
|
FormDataTuple* data = mFormData.AppendElement();
|
2015-01-29 01:04:28 +00:00
|
|
|
SetNameValuePair(data, aName, aValue);
|
2012-12-11 18:09:56 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-01-07 19:30:36 +00:00
|
|
|
|
2016-02-23 23:01:01 +00:00
|
|
|
virtual nsresult AddNameBlobOrNullPair(const nsAString& aName,
|
|
|
|
Blob* aBlob) override;
|
2015-01-27 23:16:21 +00:00
|
|
|
|
2016-07-14 07:01:31 +00:00
|
|
|
virtual nsresult AddNameDirectoryPair(const nsAString& aName,
|
|
|
|
Directory* aDirectory) override;
|
|
|
|
|
2015-10-20 02:06:00 +00:00
|
|
|
typedef bool (*FormDataEntryCallback)(const nsString& aName,
|
2016-07-14 07:01:31 +00:00
|
|
|
const OwningBlobOrDirectoryOrUSVString& aValue,
|
2015-10-20 02:06:00 +00:00
|
|
|
void* aClosure);
|
2015-01-27 23:16:21 +00:00
|
|
|
|
|
|
|
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];
|
2015-10-20 02:06:00 +00:00
|
|
|
if (!aFunc(tuple.name, tuple.value, aClosure)) {
|
2015-01-27 23:16:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-25 05:58:18 +00:00
|
|
|
private:
|
2012-12-11 18:09:56 +00:00
|
|
|
nsCOMPtr<nsISupports> mOwner;
|
|
|
|
|
2010-02-25 05:58:18 +00:00
|
|
|
nsTArray<FormDataTuple> mFormData;
|
|
|
|
};
|
|
|
|
|
2016-01-07 19:30:36 +00:00
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namepsace
|
|
|
|
|
|
|
|
#endif // mozilla_dom_FormData_h
|