1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_FormData_h
8 #define mozilla_dom_FormData_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/dom/BindingDeclarations.h"
12 #include "mozilla/dom/HTMLFormSubmission.h"
13 #include "mozilla/dom/File.h"
14 #include "mozilla/dom/FormDataBinding.h"
16 #include "nsWrapperCache.h"
23 class HTMLFormElement
;
26 class FormData final
: public nsISupports
,
27 public HTMLFormSubmission
,
28 public nsWrapperCache
{
30 FormData(const FormData
& aFormData
);
31 ~FormData() = default;
33 struct FormDataTuple
{
35 OwningBlobOrDirectoryOrUSVString value
;
38 // Returns the FormDataTuple to modify. This may be null, in which case
39 // no element with aName was found.
40 FormDataTuple
* RemoveAllOthersAndGetFirstFormDataTuple(
41 const nsAString
& aName
);
43 void SetNameValuePair(FormDataTuple
* aData
, const nsAString
& aName
,
44 const nsAString
& aValue
);
46 void SetNameFilePair(FormDataTuple
* aData
, const nsAString
& aName
,
49 void SetNameDirectoryPair(FormDataTuple
* aData
, const nsAString
& aName
,
50 Directory
* aDirectory
);
53 explicit FormData(nsISupports
* aOwner
= nullptr,
54 NotNull
<const Encoding
*> aEncoding
= UTF_8_ENCODING
,
55 Element
* aSubmitter
= nullptr);
57 already_AddRefed
<FormData
> Clone();
59 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
60 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(FormData
)
63 virtual JSObject
* WrapObject(JSContext
* aCx
,
64 JS::Handle
<JSObject
*> aGivenProto
) override
;
67 nsISupports
* GetParentObject() const { return mOwner
; }
69 static already_AddRefed
<FormData
> Constructor(
70 const GlobalObject
& aGlobal
,
71 const Optional
<NonNull
<HTMLFormElement
> >& aFormElement
,
74 void Append(const nsAString
& aName
, const nsAString
& aValue
,
77 void Append(const nsAString
& aName
, Blob
& aBlob
,
78 const Optional
<nsAString
>& aFilename
, ErrorResult
& aRv
);
80 void Append(const nsAString
& aName
, Directory
* aDirectory
);
82 void Delete(const nsAString
& aName
);
84 void Get(const nsAString
& aName
,
85 Nullable
<OwningBlobOrDirectoryOrUSVString
>& aOutValue
);
87 void GetAll(const nsAString
& aName
,
88 nsTArray
<OwningBlobOrDirectoryOrUSVString
>& aValues
);
90 bool Has(const nsAString
& aName
);
92 void Set(const nsAString
& aName
, Blob
& aBlob
,
93 const Optional
<nsAString
>& aFilename
, ErrorResult
& aRv
);
94 void Set(const nsAString
& aName
, const nsAString
& aValue
, ErrorResult
& aRv
);
96 uint32_t GetIterableLength() const;
98 const nsAString
& GetKeyAtIndex(uint32_t aIndex
) const;
100 const OwningBlobOrDirectoryOrUSVString
& GetValueAtIndex(
101 uint32_t aIndex
) const;
103 // HTMLFormSubmission
104 virtual nsresult
GetEncodedSubmission(nsIURI
* aURI
,
105 nsIInputStream
** aPostDataStream
,
106 nsCOMPtr
<nsIURI
>& aOutURI
) override
;
108 virtual nsresult
AddNameValuePair(const nsAString
& aName
,
109 const nsAString
& aValue
) override
{
110 FormDataTuple
* data
= mFormData
.AppendElement();
111 SetNameValuePair(data
, aName
, aValue
);
115 virtual nsresult
AddNameBlobPair(const nsAString
& aName
,
116 Blob
* aBlob
) override
;
118 virtual nsresult
AddNameDirectoryPair(const nsAString
& aName
,
119 Directory
* aDirectory
) override
;
121 typedef bool (*FormDataEntryCallback
)(
122 const nsString
& aName
, const OwningBlobOrDirectoryOrUSVString
& aValue
,
125 uint32_t Length() const { return mFormData
.Length(); }
127 // Stops iteration and returns false if any invocation of callback returns
128 // false. Returns true otherwise.
129 bool ForEach(FormDataEntryCallback aFunc
, void* aClosure
) {
130 for (uint32_t i
= 0; i
< mFormData
.Length(); ++i
) {
131 FormDataTuple
& tuple
= mFormData
[i
];
132 if (!aFunc(tuple
.name
, tuple
.value
, aClosure
)) {
140 nsresult
GetSendInfo(nsIInputStream
** aBody
, uint64_t* aContentLength
,
141 nsACString
& aContentTypeWithCharset
,
142 nsACString
& aCharset
) const;
144 nsresult
CopySubmissionDataTo(HTMLFormSubmission
* aFormSubmission
) const;
147 nsCOMPtr
<nsISupports
> mOwner
;
149 nsTArray
<FormDataTuple
> mFormData
;
153 } // namespace mozilla
155 #endif // mozilla_dom_FormData_h