summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/XHR/FormData.h
blob: 56734a2784400745c2c0f93ba10da59aaa3cedfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibWeb/Bindings/FormDataPrototype.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/HTMLFormElement.h>
#include <LibWeb/WebIDL/ExceptionOr.h>

namespace Web::XHR {

// https://xhr.spec.whatwg.org/#formdataentryvalue
using FormDataEntryValue = Variant<JS::Handle<FileAPI::File>, String>;

struct FormDataEntry {
    String name;
    FormDataEntryValue value;
};

// https://xhr.spec.whatwg.org/#interface-formdata
class FormData : public Bindings::PlatformObject {
    WEB_PLATFORM_OBJECT(FormData, Bindings::PlatformObject);

public:
    virtual ~FormData() override;

    static WebIDL::ExceptionOr<JS::NonnullGCPtr<FormData>> construct_impl(JS::Realm&, Optional<JS::NonnullGCPtr<HTML::HTMLFormElement>> form = {});
    static WebIDL::ExceptionOr<JS::NonnullGCPtr<FormData>> construct_impl(JS::Realm&, Vector<FormDataEntry> entry_list);

    WebIDL::ExceptionOr<void> append(String const& name, String const& value);
    WebIDL::ExceptionOr<void> append(String const& name, JS::NonnullGCPtr<FileAPI::Blob> const& blob_value, Optional<String> const& filename = {});
    void delete_(String const& name);
    Variant<JS::Handle<FileAPI::File>, String, Empty> get(String const& name);
    WebIDL::ExceptionOr<Vector<FormDataEntryValue>> get_all(String const& name);
    bool has(String const& name);
    WebIDL::ExceptionOr<void> set(String const& name, String const& value);
    WebIDL::ExceptionOr<void> set(String const& name, JS::NonnullGCPtr<FileAPI::Blob> const& blob_value, Optional<String> const& filename = {});

    Vector<FormDataEntry> const& entry_list() const { return m_entry_list; }

    using ForEachCallback = Function<JS::ThrowCompletionOr<void>(String const&, FormDataEntryValue const&)>;
    JS::ThrowCompletionOr<void> for_each(ForEachCallback);

private:
    friend class FormDataIterator;

    explicit FormData(JS::Realm&, Vector<FormDataEntry> entry_list = {});

    virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;

    WebIDL::ExceptionOr<void> append_impl(String const& name, Variant<JS::NonnullGCPtr<FileAPI::Blob>, String> const& value, Optional<String> const& filename = {});
    WebIDL::ExceptionOr<void> set_impl(String const& name, Variant<JS::NonnullGCPtr<FileAPI::Blob>, String> const& value, Optional<String> const& filename = {});

    Vector<FormDataEntry> m_entry_list;
};

}