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
|
|
|
|
2016-01-07 22:30:36 +03:00
|
|
|
#ifndef mozilla_dom_FormData_h
|
|
|
|
#define mozilla_dom_FormData_h
|
2010-02-25 08:58:18 +03:00
|
|
|
|
2013-05-30 00:43:41 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-03-03 22:30:13 +04:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2016-06-16 10:24:16 +03:00
|
|
|
#include "mozilla/dom/HTMLFormSubmission.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"
|
2023-02-02 15:26:50 +03:00
|
|
|
#include "nsGenericHTMLElement.h"
|
2016-01-07 22:30:36 +03:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsWrapperCache.h"
|
2010-02-25 08:58:18 +03:00
|
|
|
|
2012-12-11 22:09:56 +04:00
|
|
|
namespace mozilla {
|
2020-11-23 19:12:02 +03:00
|
|
|
class ErrorResult;
|
|
|
|
|
2012-12-11 22:09:56 +04:00
|
|
|
namespace dom {
|
2016-01-07 22:30:36 +03:00
|
|
|
|
2023-06-28 19:28:27 +03:00
|
|
|
class CustomElementFormValue;
|
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
|
|
|
|
2018-02-22 22:42:12 +03:00
|
|
|
class FormData final : public nsISupports,
|
2016-06-16 10:24:16 +03:00
|
|
|
public HTMLFormSubmission,
|
2016-01-07 22:30:36 +03:00
|
|
|
public nsWrapperCache {
|
2015-01-29 04:04:28 +03:00
|
|
|
private:
|
2019-09-09 16:53:27 +03:00
|
|
|
FormData(const FormData& aFormData);
|
2020-02-21 13:41:47 +03:00
|
|
|
~FormData() = default;
|
2015-05-12 15:09:51 +03:00
|
|
|
|
2015-01-29 04:04:28 +03:00
|
|
|
struct FormDataTuple {
|
|
|
|
nsString name;
|
2016-07-14 10:01:31 +03:00
|
|
|
OwningBlobOrDirectoryOrUSVString value;
|
2015-01-29 04:04:28 +03: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,
|
2021-03-24 21:43:58 +03:00
|
|
|
const nsAString& aValue);
|
2015-01-29 04:04:28 +03:00
|
|
|
|
2016-02-17 17:56:19 +03:00
|
|
|
void SetNameFilePair(FormDataTuple* aData, const nsAString& aName,
|
|
|
|
File* aFile);
|
2015-01-29 04:04:28 +03:00
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
void SetNameDirectoryPair(FormDataTuple* aData, const nsAString& aName,
|
|
|
|
Directory* aDirectory);
|
|
|
|
|
2010-02-25 08:58:18 +03:00
|
|
|
public:
|
2019-09-09 16:53:27 +03:00
|
|
|
explicit FormData(nsISupports* aOwner = nullptr,
|
|
|
|
NotNull<const Encoding*> aEncoding = UTF_8_ENCODING,
|
2020-02-11 15:46:57 +03:00
|
|
|
Element* aSubmitter = nullptr);
|
2019-09-09 16:53:27 +03:00
|
|
|
|
|
|
|
already_AddRefed<FormData> Clone();
|
2012-12-11 22:09:56 +04:00
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2022-09-07 14:22:51 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(FormData)
|
2010-02-25 08:58:18 +03:00
|
|
|
|
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; }
|
2016-01-07 22:30:36 +03:00
|
|
|
|
|
|
|
static already_AddRefed<FormData> Constructor(
|
|
|
|
const GlobalObject& aGlobal,
|
|
|
|
const Optional<NonNull<HTMLFormElement> >& aFormElement,
|
2023-02-02 15:26:50 +03:00
|
|
|
nsGenericHTMLElement* aSubmitter, ErrorResult& aRv);
|
2016-01-07 22:30:36 +03:00
|
|
|
|
2015-12-05 00:15:46 +03:00
|
|
|
void Append(const nsAString& aName, const nsAString& aValue,
|
2016-01-07 22:30:36 +03:00
|
|
|
ErrorResult& aRv);
|
2016-07-14 10:01:31 +03:00
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
void Append(const nsAString& aName, Blob& aBlob,
|
2016-01-07 22:30:36 +03:00
|
|
|
const Optional<nsAString>& aFilename, ErrorResult& aRv);
|
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
void Append(const nsAString& aName, Directory* aDirectory);
|
|
|
|
|
2021-10-21 21:02:52 +03:00
|
|
|
void Append(const FormData& aFormData);
|
|
|
|
|
2015-01-29 04:04:28 +03:00
|
|
|
void Delete(const nsAString& aName);
|
2016-01-07 22:30:36 +03:00
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
void Get(const nsAString& aName,
|
|
|
|
Nullable<OwningBlobOrDirectoryOrUSVString>& aOutValue);
|
2016-01-07 22:30:36 +03:00
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
void GetAll(const nsAString& aName,
|
|
|
|
nsTArray<OwningBlobOrDirectoryOrUSVString>& aValues);
|
2016-01-07 22:30:36 +03:00
|
|
|
|
2015-01-29 04:04:28 +03:00
|
|
|
bool Has(const nsAString& aName);
|
2016-01-07 22:30:36 +03:00
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
void Set(const nsAString& aName, Blob& aBlob,
|
2016-01-07 22:30:36 +03:00
|
|
|
const Optional<nsAString>& aFilename, ErrorResult& aRv);
|
2015-12-05 00:15:46 +03:00
|
|
|
void Set(const nsAString& aName, const nsAString& aValue, ErrorResult& aRv);
|
2012-12-11 22:09:56 +04:00
|
|
|
|
2015-10-20 05:06:00 +03:00
|
|
|
uint32_t GetIterableLength() const;
|
2016-01-07 22:30:36 +03:00
|
|
|
|
2015-10-20 05:06:00 +03:00
|
|
|
const nsAString& GetKeyAtIndex(uint32_t aIndex) const;
|
2016-01-07 22:30:36 +03:00
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
const OwningBlobOrDirectoryOrUSVString& GetValueAtIndex(
|
|
|
|
uint32_t aIndex) const;
|
2015-10-20 05:06:00 +03:00
|
|
|
|
2016-06-16 10:24:16 +03:00
|
|
|
// HTMLFormSubmission
|
2017-09-22 09:12:03 +03:00
|
|
|
virtual nsresult GetEncodedSubmission(nsIURI* aURI,
|
|
|
|
nsIInputStream** aPostDataStream,
|
2018-05-23 08:12:36 +03:00
|
|
|
nsCOMPtr<nsIURI>& aOutURI) override;
|
2016-01-07 22:30:36 +03:00
|
|
|
|
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 {
|
2021-05-10 19:06:34 +03:00
|
|
|
nsAutoString usvName(aName);
|
|
|
|
nsAutoString usvValue(aValue);
|
|
|
|
if (!NormalizeUSVString(usvName) || !NormalizeUSVString(usvValue)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2013-03-03 22:30:13 +04:00
|
|
|
FormDataTuple* data = mFormData.AppendElement();
|
2021-05-10 19:06:34 +03:00
|
|
|
SetNameValuePair(data, usvName, usvValue);
|
2012-12-11 22:09:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-01-07 22:30:36 +03:00
|
|
|
|
2021-03-24 21:43:58 +03:00
|
|
|
virtual nsresult AddNameBlobPair(const nsAString& aName,
|
|
|
|
Blob* aBlob) override;
|
2015-01-28 02:16:21 +03:00
|
|
|
|
2016-07-14 10:01:31 +03:00
|
|
|
virtual nsresult AddNameDirectoryPair(const nsAString& aName,
|
|
|
|
Directory* aDirectory) override;
|
|
|
|
|
2015-01-28 02:16:21 +03:00
|
|
|
uint32_t Length() const { return mFormData.Length(); }
|
|
|
|
|
|
|
|
// Stops iteration and returns false if any invocation of callback returns
|
|
|
|
// false. Returns true otherwise.
|
2023-06-16 21:20:48 +03:00
|
|
|
// Accepts callbacks of the form `bool(const nsString&, const
|
|
|
|
// OwningBlobOrDirectoryOrUSVString&)`.
|
|
|
|
template <typename F>
|
|
|
|
bool ForEach(F&& aCallback) {
|
2015-01-28 02:16:21 +03:00
|
|
|
for (uint32_t i = 0; i < mFormData.Length(); ++i) {
|
|
|
|
FormDataTuple& tuple = mFormData[i];
|
2023-06-16 21:20:48 +03:00
|
|
|
if (!aCallback(tuple.name, tuple.value)) {
|
2015-01-28 02:16:21 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-20 04:32:12 +03:00
|
|
|
nsresult GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength,
|
|
|
|
nsACString& aContentTypeWithCharset,
|
|
|
|
nsACString& aCharset) const;
|
|
|
|
|
2019-09-09 16:53:27 +03:00
|
|
|
nsresult CopySubmissionDataTo(HTMLFormSubmission* aFormSubmission) const;
|
|
|
|
|
2021-07-21 12:52:20 +03:00
|
|
|
Element* GetSubmitterElement() const { return mSubmitter.get(); }
|
|
|
|
|
2023-06-28 19:28:27 +03:00
|
|
|
CustomElementFormValue ConvertToCustomElementFormValue();
|
|
|
|
|
2010-02-25 08:58:18 +03:00
|
|
|
private:
|
2012-12-11 22:09:56 +04:00
|
|
|
nsCOMPtr<nsISupports> mOwner;
|
|
|
|
|
2021-07-21 12:52:20 +03:00
|
|
|
// Submitter element.
|
|
|
|
RefPtr<Element> mSubmitter;
|
|
|
|
|
2010-02-25 08:58:18 +03:00
|
|
|
nsTArray<FormDataTuple> mFormData;
|
|
|
|
};
|
|
|
|
|
2016-01-07 22:30:36 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_FormData_h
|