FileReader.h 3.9 KB

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