/src/serenity/Userland/Libraries/LibWeb/FileAPI/FileReader.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/NonnullRefPtr.h> |
10 | | #include <LibWeb/Bindings/PlatformObject.h> |
11 | | #include <LibWeb/DOM/EventTarget.h> |
12 | | #include <LibWeb/Forward.h> |
13 | | #include <LibWeb/WebIDL/ExceptionOr.h> |
14 | | |
15 | | namespace Web::FileAPI { |
16 | | |
17 | | // https://w3c.github.io/FileAPI/#dfn-filereader |
18 | | class FileReader : public DOM::EventTarget { |
19 | | WEB_PLATFORM_OBJECT(FileReader, DOM::EventTarget); |
20 | | JS_DECLARE_ALLOCATOR(FileReader); |
21 | | |
22 | | public: |
23 | | using Result = Variant<Empty, String, JS::Handle<JS::ArrayBuffer>>; |
24 | | |
25 | | virtual ~FileReader() override; |
26 | | |
27 | | [[nodiscard]] static JS::NonnullGCPtr<FileReader> create(JS::Realm&); |
28 | | static JS::NonnullGCPtr<FileReader> construct_impl(JS::Realm&); |
29 | | |
30 | | // async read methods |
31 | | WebIDL::ExceptionOr<void> read_as_array_buffer(Blob&); |
32 | | WebIDL::ExceptionOr<void> read_as_binary_string(Blob&); |
33 | | WebIDL::ExceptionOr<void> read_as_text(Blob&, Optional<String> const& encoding = {}); |
34 | | WebIDL::ExceptionOr<void> read_as_data_url(Blob&); |
35 | | |
36 | | void abort(); |
37 | | |
38 | | // states |
39 | | enum class State : u16 { |
40 | | // The FileReader object has been constructed, and there are no pending reads. None of the read methods have been called. |
41 | | // This is the default state of a newly minted FileReader object, until one of the read methods have been called on it. |
42 | | Empty = 0, |
43 | | |
44 | | // A File or Blob is being read. One of the read methods is being processed, and no error has occurred during the read. |
45 | | Loading = 1, |
46 | | |
47 | | // The entire File or Blob has been read into memory, OR a file read error occurred, OR the read was aborted using abort(). |
48 | | // The FileReader is no longer reading a File or Blob. |
49 | | // If readyState is set to DONE it means at least one of the read methods have been called on this FileReader. |
50 | | Done = 2, |
51 | | }; |
52 | | |
53 | | // https://w3c.github.io/FileAPI/#dom-filereader-readystate |
54 | 0 | State ready_state() const { return m_state; } |
55 | | |
56 | | // File or Blob data |
57 | | |
58 | | // https://w3c.github.io/FileAPI/#dom-filereader-result |
59 | 0 | Result result() const { return m_result; } |
60 | | |
61 | | // https://w3c.github.io/FileAPI/#dom-filereader-error |
62 | 0 | JS::GCPtr<WebIDL::DOMException> error() const { return m_error; } |
63 | | |
64 | | // event handler attributes |
65 | | void set_onloadstart(WebIDL::CallbackType*); |
66 | | WebIDL::CallbackType* onloadstart(); |
67 | | |
68 | | void set_onprogress(WebIDL::CallbackType*); |
69 | | WebIDL::CallbackType* onprogress(); |
70 | | |
71 | | void set_onload(WebIDL::CallbackType*); |
72 | | WebIDL::CallbackType* onload(); |
73 | | |
74 | | void set_onabort(WebIDL::CallbackType*); |
75 | | WebIDL::CallbackType* onabort(); |
76 | | |
77 | | void set_onerror(WebIDL::CallbackType*); |
78 | | WebIDL::CallbackType* onerror(); |
79 | | |
80 | | void set_onloadend(WebIDL::CallbackType*); |
81 | | WebIDL::CallbackType* onloadend(); |
82 | | |
83 | | protected: |
84 | | FileReader(JS::Realm&, ByteBuffer); |
85 | | |
86 | | virtual void initialize(JS::Realm&) override; |
87 | | |
88 | | virtual void visit_edges(JS::Cell::Visitor&) override; |
89 | | |
90 | | private: |
91 | | explicit FileReader(JS::Realm&); |
92 | | |
93 | | enum class Type { |
94 | | ArrayBuffer, |
95 | | BinaryString, |
96 | | Text, |
97 | | DataURL, |
98 | | }; |
99 | | |
100 | | WebIDL::ExceptionOr<void> read_operation(Blob&, Type, Optional<String> const& encoding_name = {}); |
101 | | |
102 | | static WebIDL::ExceptionOr<Result> blob_package_data(JS::Realm& realm, ByteBuffer, FileReader::Type type, Optional<String> const&, Optional<String> const& encoding_name); |
103 | | |
104 | | // A FileReader has an associated state, that is "empty", "loading", or "done". It is initially "empty". |
105 | | // https://w3c.github.io/FileAPI/#filereader-state |
106 | | State m_state { State::Empty }; |
107 | | |
108 | | // A FileReader has an associated result (null, a DOMString or an ArrayBuffer). It is initially null. |
109 | | // https://w3c.github.io/FileAPI/#filereader-result |
110 | | Result m_result; |
111 | | |
112 | | // A FileReader has an associated error (null or a DOMException). It is initially null. |
113 | | // https://w3c.github.io/FileAPI/#filereader-error |
114 | | JS::GCPtr<WebIDL::DOMException> m_error; |
115 | | }; |
116 | | |
117 | | } |