FileReader.h 3.9 KB

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