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"
|
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"
|
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"
|
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 {
|
|
|
|
namespace dom {
|
2016-01-07 22:30:36 +03:00
|
|
|
|
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
|
2010-02-25 08:58:18 +03:00
|
|
|
{
|
2015-01-29 04:04:28 +03:00
|
|
|
private:
|
2016-01-07 22:30:36 +03:00
|
|
|
~FormData() {}
|
2015-05-12 15:09:51 +03:00
|
|
|
|
2015-01-29 04:04:28 +03:00
|
|
|
struct FormDataTuple
|
|
|
|
{
|
|
|
|
nsString name;
|
2016-02-24 02:01:01 +03:00
|
|
|
bool wasNullBlob;
|
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,
|
2016-02-24 02:01:01 +03:00
|
|
|
const nsAString& aValue,
|
|
|
|
bool aWasNullBlob = false);
|
2015-01-29 04:04:28 +03:00
|
|
|
|
2016-02-17 17:56:19 +03:00
|
|
|
void SetNameFilePair(FormDataTuple* aData,
|
2015-01-29 04:04:28 +03:00
|
|
|
const nsAString& aName,
|
2016-02-17 17:56:19 +03:00
|
|
|
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:
|
2016-01-07 22:30:36 +03:00
|
|
|
explicit FormData(nsISupports* aOwner = nullptr);
|
2012-12-11 22:09:56 +04:00
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2018-02-22 22:42:12 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_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,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
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);
|
|
|
|
|
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,
|
2016-01-07 22:30:36 +03:00
|
|
|
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
|
2016-01-07 22:30:36 +03:00
|
|
|
virtual nsresult
|
2017-09-22 09:12:03 +03:00
|
|
|
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
|
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;
|
|
|
|
}
|
2016-01-07 22:30:36 +03:00
|
|
|
|
2016-02-24 02:01:01 +03:00
|
|
|
virtual nsresult AddNameBlobOrNullPair(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-10-20 05:06:00 +03:00
|
|
|
typedef bool (*FormDataEntryCallback)(const nsString& aName,
|
2016-07-14 10:01:31 +03:00
|
|
|
const OwningBlobOrDirectoryOrUSVString& aValue,
|
2015-10-20 05:06:00 +03:00
|
|
|
void* aClosure);
|
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.
|
|
|
|
bool
|
|
|
|
ForEach(FormDataEntryCallback aFunc, void* aClosure)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mFormData.Length(); ++i) {
|
|
|
|
FormDataTuple& tuple = mFormData[i];
|
2015-10-20 05:06:00 +03:00
|
|
|
if (!aFunc(tuple.name, tuple.value, aClosure)) {
|
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;
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2016-01-07 22:30:36 +03:00
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namepsace
|
|
|
|
|
|
|
|
#endif // mozilla_dom_FormData_h
|