Files
palemoon27/dom/base/FormData.cpp
T
roytam1 137091cd8f import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 912337 - Followup: fix missing #include on a CLOSED TREE. (da77dc4ba0)
- Bug 1232977 - Remove redundant bitand with uint{8,16} and an all-bits-set mask. r=nbp, r=sunfish (164579d9d7)
- Bug 1249235 - Store RegExp flags into single slot. r=h4writer (6f030c9f06)
- Bug 1237445 - Use GCHashSet for RegExpShared sweeping, r=terrence (3aab1397e8)
- Bug 1238536 part 1 - Do not automatically exit fullscreen if restored from minimized state. r=jimm (05dd086d29)
- Bug 1238536 part 2 - Do not trigger fullscreen changed when switching between fullscreen and minimized. r=karlt (83e862cc7a)
- Bug 1233598 - HTMLInputElement must traverse/unlink the getFilesAndDirectories promise, r=smaug (1ff7260803)
- Bug 1187157 - new FormData::get() should return an empty string if the file is not set - part 1, r=smaug (9a255fd884)
- Bug 1187157 - new FormData::get() should return an empty string if the file is not set - part 2, r=smaug (f26712d951)
- Bug 1216793 - check against tracking protection list in XHR. r=gcp (3a827250bc)
- Bug 1203588 - Return the same Promise object from HTMLInputElement.getFilesAndDirectories when possible. r=baku (f036876c1b)
- Bug 1207088 - Don't show the "Choose folder..." button for a directory picking <input> on platforms that don't have a directory picker. r=jfkthame (c5742f75c9)
- Bug 1234192 - OSFileSystem should not be shared between Directory objects, r=smaug (ba258935b7)
- Bug 1202964 - Use the correct nsIFilePicker API for directory picking in HTMLInputElement::nsFilePickerShownCallback::Done. r=baku (ce11a83ace)
- Bug 1237674 - Rename nsFormData to mozilla::dom::FormData, r=smaug (0a18825b2b)
- Bug 1237595 - FormData ctor and form submission should create empty Blob/File when a input type=file is not set, r=smaug (2bce85bbe8)
- cleanup some stuff (cbd553db88)
- Bug 1238515 - nsIFilePicker methods should clearly say when they return directories and files, r=smaug (e903765f0c)
- Bug 1198256 - Replace deprecated GtkColorSelectionDialog with GtkColorChooserDialog in Gtk3. r=karlt (2ff87896a6)
- Bug 1198256 - Reactivate the old Gtk color picker for now. r=karlt (ea42685de9)
- Bug 1213632: Prevent WebExtensions from using versioned JavaScript. r=billm (9d3a9b7511)
- Bug 1219855, Part 1 - Make nsXULAlerts implement nsIAlertsService. r=MattN,wchen (6b21425554)
- Bug 1219855, Part 2 - Always use XUL notifications if the system backend fails. r=wchen (7c5b30fd2c)
- Bug 1219855, Part 3 - Fix variable shadowing in `OSXNotificationCenter::ShowAlert`. r=me (29e442fe45)
- Bug 1241377 - Part 1: Implement nsIFormPOSTActionChannel for the channel accepts form POST. r=mayhemer (f03e9dbece)
- Bug 1241377 - Part 2: Add test for nsIFormPOSTActionChannel. r=mayhemer (0a9e7aab1f)
- Bug 1207824: Add Telemetry for WebRTC call type, simultaneous tracks, and renegotiations r=bwc (38085ce155)
- Bug 1175609 - Bring onnegotiationneeded in line with spec. r=mt (398c03db6e)
- Bug 1209252 - Part 2: typo fix for compile issue. r=bustage on a CLOSED TREE (e1b8f43993)
- Bug 1221786: clear about:webrtc logs for private browsing sessions. r=jib (49615cc3c6)
- Bug 1232082 - add RTCRtpReceiver for each remote track. r=jesup (1231223782)
- Bug 1219711 - Remove fakeness from webrtc tests. r=jib (b421c55124)
- Bug 1232082 - add pc.ontrack and RTCTrackEvent r=jesup,smaug (f915ecc77c)
- Bug 1222127: Use the inner window to compute style. r=baku (a3ea812154)
- Bug 1209634 - Remove unused WindowTarget. r=past (1f2d218cb4)
- Bug 1209634 - Remove unused target.version. r=past (7ddf6cdfb1)
- Bug 1209634 - Reformat target.js to match ESLint rules. r=past (8fe93b4737)
- Bug 1168853 - Implement WorkerDebugger.isInitialized;r=khuey (318bd9516c)
- Bug 1150444 - Intermittent test_WorkerDebugger.isFrozen.xul;r=khuey (e787b9c935)
- Mark test_bug883784.jsm as a support file, no bug (c6c987138d)
- Bug 1178726 - Simplify how we deal with freezing/thawing workers;r=jlongster,khuey (88c7341f8f)
- Bug 1228382 - Expose an API to relate nsIWorkerDebugger to its nsIServiceWorkerInfo instance. r=ejpbruel (26ce55693f)
2023-09-05 16:52:50 +08:00

378 lines
9.8 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "FormData.h"
#include "nsIVariant.h"
#include "nsIInputStream.h"
#include "mozilla/dom/File.h"
#include "mozilla/dom/HTMLFormElement.h"
#include "MultipartBlobImpl.h"
using namespace mozilla;
using namespace mozilla::dom;
FormData::FormData(nsISupports* aOwner)
: nsFormSubmission(NS_LITERAL_CSTRING("UTF-8"), nullptr)
, mOwner(aOwner)
{
}
namespace {
// Implements steps 3 and 4 of the "create an entry" algorithm of FormData.
already_AddRefed<File>
CreateNewFileInstance(Blob& aBlob, const Optional<nsAString>& aFilename,
ErrorResult& aRv)
{
// Step 3 "If value is a Blob object and not a File object, set value to
// a new File object, representing the same bytes, whose name attribute value
// is "blob"."
// Step 4 "If value is a File object and filename is given, set value to
// a new File object, representing the same bytes, whose name attribute
// value is filename."
nsAutoString filename;
if (aFilename.WasPassed()) {
filename = aFilename.Value();
} else {
// If value is already a File and filename is not passed, the spec says not
// to create a new instance.
RefPtr<File> file = aBlob.ToFile();
if (file) {
return file.forget();
}
filename = NS_LITERAL_STRING("blob");
}
RefPtr<File> file = aBlob.ToFile(filename, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
return file.forget();
}
} // namespace
// -------------------------------------------------------------------------
// nsISupports
NS_IMPL_CYCLE_COLLECTION_CLASS(FormData)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(FormData)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwner)
for (uint32_t i = 0, len = tmp->mFormData.Length(); i < len; ++i) {
ImplCycleCollectionUnlink(tmp->mFormData[i].value);
}
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(FormData)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwner)
for (uint32_t i = 0, len = tmp->mFormData.Length(); i < len; ++i) {
ImplCycleCollectionTraverse(cb, tmp->mFormData[i].value,
"mFormData[i].GetAsFile()", 0);
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(FormData)
NS_IMPL_CYCLE_COLLECTING_ADDREF(FormData)
NS_IMPL_CYCLE_COLLECTING_RELEASE(FormData)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FormData)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsIDOMFormData)
NS_INTERFACE_MAP_ENTRY(nsIXHRSendable)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMFormData)
NS_INTERFACE_MAP_END
// -------------------------------------------------------------------------
// nsFormSubmission
nsresult
FormData::GetEncodedSubmission(nsIURI* aURI,
nsIInputStream** aPostDataStream)
{
NS_NOTREACHED("Shouldn't call FormData::GetEncodedSubmission");
return NS_OK;
}
void
FormData::Append(const nsAString& aName, const nsAString& aValue,
ErrorResult& aRv)
{
AddNameValuePair(aName, aValue);
}
void
FormData::Append(const nsAString& aName, Blob& aBlob,
const Optional<nsAString>& aFilename,
ErrorResult& aRv)
{
RefPtr<File> file = CreateNewFileInstance(aBlob, aFilename, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
AddNameFilePair(aName, file);
}
void
FormData::Delete(const nsAString& aName)
{
// We have to use this slightly awkward for loop since uint32_t >= 0 is an
// error for being always true.
for (uint32_t i = mFormData.Length(); i-- > 0; ) {
if (aName.Equals(mFormData[i].name)) {
mFormData.RemoveElementAt(i);
}
}
}
void
FormData::Get(const nsAString& aName,
Nullable<OwningFileOrUSVString>& aOutValue)
{
for (uint32_t i = 0; i < mFormData.Length(); ++i) {
if (aName.Equals(mFormData[i].name)) {
aOutValue.SetValue() = mFormData[i].value;
return;
}
}
aOutValue.SetNull();
}
void
FormData::GetAll(const nsAString& aName,
nsTArray<OwningFileOrUSVString>& aValues)
{
for (uint32_t i = 0; i < mFormData.Length(); ++i) {
if (aName.Equals(mFormData[i].name)) {
OwningFileOrUSVString* element = aValues.AppendElement();
*element = mFormData[i].value;
}
}
}
bool
FormData::Has(const nsAString& aName)
{
for (uint32_t i = 0; i < mFormData.Length(); ++i) {
if (aName.Equals(mFormData[i].name)) {
return true;
}
}
return false;
}
nsresult
FormData::AddNameFilePair(const nsAString& aName, File* aFile)
{
MOZ_ASSERT(aFile);
FormDataTuple* data = mFormData.AppendElement();
SetNameFilePair(data, aName, aFile);
return NS_OK;
}
FormData::FormDataTuple*
FormData::RemoveAllOthersAndGetFirstFormDataTuple(const nsAString& aName)
{
FormDataTuple* lastFoundTuple = nullptr;
uint32_t lastFoundIndex = mFormData.Length();
// We have to use this slightly awkward for loop since uint32_t >= 0 is an
// error for being always true.
for (uint32_t i = mFormData.Length(); i-- > 0; ) {
if (aName.Equals(mFormData[i].name)) {
if (lastFoundTuple) {
// The one we found earlier was not the first one, we can remove it.
mFormData.RemoveElementAt(lastFoundIndex);
}
lastFoundTuple = &mFormData[i];
lastFoundIndex = i;
}
}
return lastFoundTuple;
}
void
FormData::Set(const nsAString& aName, Blob& aBlob,
const Optional<nsAString>& aFilename,
ErrorResult& aRv)
{
FormDataTuple* tuple = RemoveAllOthersAndGetFirstFormDataTuple(aName);
if (tuple) {
RefPtr<File> file = CreateNewFileInstance(aBlob, aFilename, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
SetNameFilePair(tuple, aName, file);
} else {
Append(aName, aBlob, aFilename, aRv);
}
}
void
FormData::Set(const nsAString& aName, const nsAString& aValue,
ErrorResult& aRv)
{
FormDataTuple* tuple = RemoveAllOthersAndGetFirstFormDataTuple(aName);
if (tuple) {
SetNameValuePair(tuple, aName, aValue);
} else {
Append(aName, aValue, aRv);
}
}
uint32_t
FormData::GetIterableLength() const
{
return mFormData.Length();
}
const nsAString&
FormData::GetKeyAtIndex(uint32_t aIndex) const
{
MOZ_ASSERT(aIndex < mFormData.Length());
return mFormData[aIndex].name;
}
const OwningFileOrUSVString&
FormData::GetValueAtIndex(uint32_t aIndex) const
{
MOZ_ASSERT(aIndex < mFormData.Length());
return mFormData[aIndex].value;
}
void
FormData::SetNameValuePair(FormDataTuple* aData,
const nsAString& aName,
const nsAString& aValue)
{
MOZ_ASSERT(aData);
aData->name = aName;
aData->value.SetAsUSVString() = aValue;
}
void
FormData::SetNameFilePair(FormDataTuple* aData,
const nsAString& aName,
File* aFile)
{
MOZ_ASSERT(aData);
MOZ_ASSERT(aFile);
aData->name = aName;
aData->value.SetAsFile() = aFile;
}
// -------------------------------------------------------------------------
// nsIDOMFormData
NS_IMETHODIMP
FormData::Append(const nsAString& aName, nsIVariant* aValue)
{
uint16_t dataType;
nsresult rv = aValue->GetDataType(&dataType);
NS_ENSURE_SUCCESS(rv, rv);
if (dataType == nsIDataType::VTYPE_INTERFACE ||
dataType == nsIDataType::VTYPE_INTERFACE_IS) {
nsCOMPtr<nsISupports> supports;
nsID *iid;
rv = aValue->GetAsInterface(&iid, getter_AddRefs(supports));
NS_ENSURE_SUCCESS(rv, rv);
moz_free(iid);
nsCOMPtr<nsIDOMBlob> domBlob = do_QueryInterface(supports);
RefPtr<Blob> blob = static_cast<Blob*>(domBlob.get());
if (domBlob) {
Optional<nsAString> temp;
ErrorResult rv;
Append(aName, *blob, temp, rv);
if (NS_WARN_IF(rv.Failed())) {
return rv.StealNSResult();
}
return NS_OK;
}
}
char16_t* stringData = nullptr;
uint32_t stringLen = 0;
rv = aValue->GetAsWStringWithSize(&stringLen, &stringData);
NS_ENSURE_SUCCESS(rv, rv);
nsString valAsString;
valAsString.Adopt(stringData, stringLen);
ErrorResult error;
Append(aName, valAsString, error);
if (NS_WARN_IF(error.Failed())) {
return error.StealNSResult();
}
return NS_OK;
}
/* virtual */ JSObject*
FormData::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return FormDataBinding::Wrap(aCx, this, aGivenProto);
}
/* static */ already_AddRefed<FormData>
FormData::Constructor(const GlobalObject& aGlobal,
const Optional<NonNull<HTMLFormElement> >& aFormElement,
ErrorResult& aRv)
{
RefPtr<FormData> formData = new FormData(aGlobal.GetAsSupports());
if (aFormElement.WasPassed()) {
aRv = aFormElement.Value().WalkFormElements(formData);
}
return formData.forget();
}
// -------------------------------------------------------------------------
// nsIXHRSendable
NS_IMETHODIMP
FormData::GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength,
nsACString& aContentType, nsACString& aCharset)
{
nsFSMultipartFormData fs(NS_LITERAL_CSTRING("UTF-8"), nullptr);
for (uint32_t i = 0; i < mFormData.Length(); ++i) {
if (mFormData[i].value.IsFile()) {
fs.AddNameFilePair(mFormData[i].name, mFormData[i].value.GetAsFile());
} else if (mFormData[i].value.IsUSVString()) {
fs.AddNameValuePair(mFormData[i].name,
mFormData[i].value.GetAsUSVString());
} else {
MOZ_CRASH("This should no be possible.");
}
}
fs.GetContentType(aContentType);
aCharset.Truncate();
*aContentLength = 0;
NS_ADDREF(*aBody = fs.GetSubmissionBody(aContentLength));
return NS_OK;
}