FileReader.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/Base64.h>
  8. #include <AK/ByteBuffer.h>
  9. #include <AK/Time.h>
  10. #include <LibJS/Heap/Heap.h>
  11. #include <LibJS/Runtime/Promise.h>
  12. #include <LibJS/Runtime/Realm.h>
  13. #include <LibJS/Runtime/TypedArray.h>
  14. #include <LibTextCodec/Decoder.h>
  15. #include <LibWeb/Bindings/FileReaderPrototype.h>
  16. #include <LibWeb/Bindings/Intrinsics.h>
  17. #include <LibWeb/DOM/Event.h>
  18. #include <LibWeb/DOM/EventTarget.h>
  19. #include <LibWeb/FileAPI/Blob.h>
  20. #include <LibWeb/FileAPI/FileReader.h>
  21. #include <LibWeb/HTML/EventLoop/EventLoop.h>
  22. #include <LibWeb/HTML/EventNames.h>
  23. #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
  24. #include <LibWeb/MimeSniff/MimeType.h>
  25. #include <LibWeb/Platform/EventLoopPlugin.h>
  26. #include <LibWeb/Streams/AbstractOperations.h>
  27. #include <LibWeb/Streams/ReadableStream.h>
  28. #include <LibWeb/Streams/ReadableStreamDefaultReader.h>
  29. #include <LibWeb/WebIDL/DOMException.h>
  30. #include <LibWeb/WebIDL/ExceptionOr.h>
  31. namespace Web::FileAPI {
  32. JS_DEFINE_ALLOCATOR(FileReader);
  33. FileReader::~FileReader() = default;
  34. FileReader::FileReader(JS::Realm& realm)
  35. : DOM::EventTarget(realm)
  36. {
  37. }
  38. void FileReader::initialize(JS::Realm& realm)
  39. {
  40. Base::initialize(realm);
  41. WEB_SET_PROTOTYPE_FOR_INTERFACE(FileReader);
  42. }
  43. void FileReader::visit_edges(JS::Cell::Visitor& visitor)
  44. {
  45. Base::visit_edges(visitor);
  46. visitor.visit(m_error);
  47. }
  48. JS::NonnullGCPtr<FileReader> FileReader::create(JS::Realm& realm)
  49. {
  50. return realm.heap().allocate<FileReader>(realm, realm);
  51. }
  52. JS::NonnullGCPtr<FileReader> FileReader::construct_impl(JS::Realm& realm)
  53. {
  54. return FileReader::create(realm);
  55. }
  56. // https://w3c.github.io/FileAPI/#blob-package-data
  57. WebIDL::ExceptionOr<FileReader::Result> FileReader::blob_package_data(JS::Realm& realm, ByteBuffer bytes, Type type, Optional<String> const& mime_type, Optional<String> const& encoding_name)
  58. {
  59. // A Blob has an associated package data algorithm, given bytes, a type, a optional mimeType, and a optional encodingName, which switches on type and runs the associated steps:
  60. switch (type) {
  61. case Type::DataURL:
  62. // Return bytes as a DataURL [RFC2397] subject to the considerations below:
  63. // Use mimeType as part of the Data URL if it is available in keeping with the Data URL specification [RFC2397].
  64. // If mimeType is not available return a Data URL without a media-type. [RFC2397].
  65. return MUST(URL::create_with_data(mime_type.value_or(String {}), MUST(encode_base64(bytes)), true).to_string());
  66. case Type::Text: {
  67. // 1. Let encoding be failure.
  68. Optional<StringView> encoding;
  69. // 2. If the encodingName is present, set encoding to the result of getting an encoding from encodingName.
  70. if (encoding_name.has_value())
  71. encoding = TextCodec::get_standardized_encoding(encoding_name.value());
  72. // 3. If encoding is failure, and mimeType is present:
  73. if (!encoding.has_value() && mime_type.has_value()) {
  74. // 1. Let type be the result of parse a MIME type given mimeType.
  75. auto maybe_type = MimeSniff::MimeType::parse(mime_type.value());
  76. // 2. If type is not failure, set encoding to the result of getting an encoding from type’s parameters["charset"].
  77. if (maybe_type.has_value()) {
  78. auto const& type = maybe_type.value();
  79. auto it = type.parameters().find("charset"sv);
  80. if (it != type.parameters().end())
  81. encoding = TextCodec::get_standardized_encoding(it->value);
  82. }
  83. }
  84. // 4. If encoding is failure, then set encoding to UTF-8.
  85. // 5. Decode bytes using fallback encoding encoding, and return the result.
  86. auto decoder = TextCodec::decoder_for(encoding.value_or("UTF-8"sv));
  87. VERIFY(decoder.has_value());
  88. return TRY_OR_THROW_OOM(realm.vm(), convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(decoder.value(), bytes));
  89. }
  90. case Type::ArrayBuffer:
  91. // Return a new ArrayBuffer whose contents are bytes.
  92. return JS::ArrayBuffer::create(realm, move(bytes));
  93. case Type::BinaryString:
  94. // FIXME: Return bytes as a binary string, in which every byte is represented by a code unit of equal value [0..255].
  95. return WebIDL::NotSupportedError::create(realm, "BinaryString not supported yet"_string);
  96. }
  97. VERIFY_NOT_REACHED();
  98. }
  99. // https://w3c.github.io/FileAPI/#readOperation
  100. WebIDL::ExceptionOr<void> FileReader::read_operation(Blob& blob, Type type, Optional<String> const& encoding_name)
  101. {
  102. auto& realm = this->realm();
  103. auto const blobs_type = blob.type();
  104. // 1. If fr’s state is "loading", throw an InvalidStateError DOMException.
  105. if (m_state == State::Loading)
  106. return WebIDL::InvalidStateError::create(realm, "Read already in progress"_string);
  107. // 2. Set fr’s state to "loading".
  108. m_state = State::Loading;
  109. // 3. Set fr’s result to null.
  110. m_result = {};
  111. // 4. Set fr’s error to null.
  112. m_error = {};
  113. // 5. Let stream be the result of calling get stream on blob.
  114. auto stream = blob.get_stream();
  115. // 6. Let reader be the result of getting a reader from stream.
  116. auto reader = TRY(acquire_readable_stream_default_reader(*stream));
  117. // 7. Let bytes be an empty byte sequence.
  118. ByteBuffer bytes;
  119. // 8. Let chunkPromise be the result of reading a chunk from stream with reader.
  120. auto chunk_promise = reader->read();
  121. // 9. Let isFirstChunk be true.
  122. bool is_first_chunk = true;
  123. // 10. In parallel, while true:
  124. Platform::EventLoopPlugin::the().deferred_invoke(JS::create_heap_function(heap(), [this, chunk_promise, reader, bytes, is_first_chunk, &realm, type, encoding_name, blobs_type]() mutable {
  125. HTML::TemporaryExecutionContext execution_context { realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
  126. Optional<MonotonicTime> progress_timer;
  127. while (true) {
  128. auto& vm = realm.vm();
  129. // FIXME: Try harder to not reach into the [[Promise]] slot of chunkPromise
  130. auto promise = JS::NonnullGCPtr { verify_cast<JS::Promise>(*chunk_promise->promise()) };
  131. // 1. Wait for chunkPromise to be fulfilled or rejected.
  132. // FIXME: Create spec issue to use WebIDL react to promise steps here instead of this custom logic
  133. Platform::EventLoopPlugin::the().spin_until(JS::create_heap_function(heap(), [promise]() {
  134. return promise->state() == JS::Promise::State::Fulfilled || promise->state() == JS::Promise::State::Rejected;
  135. }));
  136. // 2. If chunkPromise is fulfilled, and isFirstChunk is true, queue a task to fire a progress event called loadstart at fr.
  137. // NOTE: ISSUE 2 We might change loadstart to be dispatched synchronously, to align with XMLHttpRequest behavior. [Issue #119]
  138. if (promise->state() == JS::Promise::State::Fulfilled && is_first_chunk) {
  139. HTML::queue_global_task(HTML::Task::Source::FileReading, realm.global_object(), JS::create_heap_function(heap(), [this, &realm]() {
  140. dispatch_event(DOM::Event::create(realm, HTML::EventNames::loadstart));
  141. }));
  142. }
  143. // 3. Set isFirstChunk to false.
  144. is_first_chunk = false;
  145. VERIFY(promise->result().is_object());
  146. auto& result = promise->result().as_object();
  147. auto value = MUST(result.get(vm.names.value));
  148. auto done = MUST(result.get(vm.names.done));
  149. // 4. If chunkPromise is fulfilled with an object whose done property is false and whose value property is a Uint8Array object, run these steps:
  150. if (promise->state() == JS::Promise::State::Fulfilled && !done.as_bool() && is<JS::Uint8Array>(value.as_object())) {
  151. // 1. Let bs be the byte sequence represented by the Uint8Array object.
  152. auto const& byte_sequence = verify_cast<JS::Uint8Array>(value.as_object());
  153. // 2. Append bs to bytes.
  154. bytes.append(byte_sequence.data());
  155. // 3. If roughly 50ms have passed since these steps were last invoked, queue a task to fire a progress event called progress at fr.
  156. auto now = MonotonicTime::now();
  157. bool enough_time_passed = !progress_timer.has_value() || (now - progress_timer.value() >= AK::Duration::from_milliseconds(50));
  158. // WPT tests for this and expects no progress event to fire when there isn't any data.
  159. // See http://wpt.live/FileAPI/reading-data-section/filereader_events.any.html
  160. bool contained_data = byte_sequence.array_length().length() > 0;
  161. if (enough_time_passed && contained_data) {
  162. HTML::queue_global_task(HTML::Task::Source::FileReading, realm.global_object(), JS::create_heap_function(heap(), [this, &realm]() {
  163. dispatch_event(DOM::Event::create(realm, HTML::EventNames::progress));
  164. }));
  165. progress_timer = now;
  166. }
  167. // 4. Set chunkPromise to the result of reading a chunk from stream with reader.
  168. chunk_promise = reader->read();
  169. }
  170. // 5. Otherwise, if chunkPromise is fulfilled with an object whose done property is true, queue a task to run the following steps and abort this algorithm:
  171. else if (promise->state() == JS::Promise::State::Fulfilled && done.as_bool()) {
  172. HTML::queue_global_task(HTML::Task::Source::FileReading, realm.global_object(), JS::create_heap_function(heap(), [this, bytes, type, &realm, encoding_name, blobs_type]() {
  173. // 1. Set fr’s state to "done".
  174. m_state = State::Done;
  175. // 2. Let result be the result of package data given bytes, type, blob’s type, and encodingName.
  176. auto result = blob_package_data(realm, bytes, type, blobs_type, encoding_name);
  177. // 3. If package data threw an exception error:
  178. if (result.is_error()) {
  179. // FIXME: 1. Set fr’s error to error.
  180. // 2. Fire a progress event called error at fr.
  181. dispatch_event(DOM::Event::create(realm, HTML::EventNames::error));
  182. }
  183. // 4. Else:
  184. else {
  185. // 1. Set fr’s result to result.
  186. m_result = result.release_value();
  187. // 2. Fire a progress event called load at the fr.
  188. dispatch_event(DOM::Event::create(realm, HTML::EventNames::load));
  189. }
  190. // 5. If fr’s state is not "loading", fire a progress event called loadend at the fr.
  191. if (m_state != State::Loading)
  192. dispatch_event(DOM::Event::create(realm, HTML::EventNames::loadend));
  193. // NOTE: Event handler for the load or error events could have started another load, if that happens the loadend event for this load is not fired.
  194. }));
  195. return;
  196. }
  197. // 6. Otherwise, if chunkPromise is rejected with an error error, queue a task to run the following steps and abort this algorithm:
  198. else if (promise->state() == JS::Promise::State::Rejected) {
  199. HTML::queue_global_task(HTML::Task::Source::FileReading, realm.global_object(), JS::create_heap_function(heap(), [this, &realm]() {
  200. // 1. Set fr’s state to "done".
  201. m_state = State::Done;
  202. // FIXME: 2. Set fr’s error to error.
  203. // 5. Fire a progress event called error at fr.
  204. dispatch_event(DOM::Event::create(realm, HTML::EventNames::loadend));
  205. // 4. If fr’s state is not "loading", fire a progress event called loadend at fr.
  206. if (m_state != State::Loading)
  207. dispatch_event(DOM::Event::create(realm, HTML::EventNames::loadend));
  208. // 5. Note: Event handler for the error event could have started another load, if that happens the loadend event for this load is not fired.
  209. }));
  210. return;
  211. }
  212. }
  213. }));
  214. return {};
  215. }
  216. // https://w3c.github.io/FileAPI/#dfn-readAsDataURL
  217. WebIDL::ExceptionOr<void> FileReader::read_as_data_url(Blob& blob)
  218. {
  219. // The readAsDataURL(blob) method, when invoked, must initiate a read operation for blob with DataURL.
  220. return read_operation(blob, Type::DataURL);
  221. }
  222. // https://w3c.github.io/FileAPI/#dfn-readAsText
  223. WebIDL::ExceptionOr<void> FileReader::read_as_text(Blob& blob, Optional<String> const& encoding)
  224. {
  225. // The readAsText(blob, encoding) method, when invoked, must initiate a read operation for blob with Text and encoding.
  226. return read_operation(blob, Type::Text, encoding);
  227. }
  228. // https://w3c.github.io/FileAPI/#dfn-readAsArrayBuffer
  229. WebIDL::ExceptionOr<void> FileReader::read_as_array_buffer(Blob& blob)
  230. {
  231. // The readAsArrayBuffer(blob) method, when invoked, must initiate a read operation for blob with ArrayBuffer.
  232. return read_operation(blob, Type::ArrayBuffer);
  233. }
  234. // https://w3c.github.io/FileAPI/#dfn-readAsBinaryString
  235. WebIDL::ExceptionOr<void> FileReader::read_as_binary_string(Blob& blob)
  236. {
  237. // The readAsBinaryString(blob) method, when invoked, must initiate a read operation for blob with BinaryString.
  238. // NOTE: The use of readAsArrayBuffer() is preferred over readAsBinaryString(), which is provided for backwards compatibility.
  239. return read_operation(blob, Type::BinaryString);
  240. }
  241. // https://w3c.github.io/FileAPI/#dfn-abort
  242. void FileReader::abort()
  243. {
  244. auto& realm = this->realm();
  245. // 1. If this's state is "empty" or if this's state is "done" set this's result to null and terminate this algorithm.
  246. if (m_state == State::Empty || m_state == State::Done) {
  247. m_result = {};
  248. return;
  249. }
  250. // 2. If this's state is "loading" set this's state to "done" and set this's result to null.
  251. if (m_state == State::Loading) {
  252. m_state = State::Done;
  253. m_result = {};
  254. }
  255. // FIXME: 3. If there are any tasks from this on the file reading task source in an affiliated task queue, then remove those tasks from that task queue.
  256. // FIXME: 4. Terminate the algorithm for the read method being processed.
  257. // 5. Fire a progress event called abort at this.
  258. dispatch_event(DOM::Event::create(realm, HTML::EventNames::abort));
  259. // 6. If this's state is not "loading", fire a progress event called loadend at this.
  260. if (m_state != State::Loading)
  261. dispatch_event(DOM::Event::create(realm, HTML::EventNames::loadend));
  262. }
  263. void FileReader::set_onloadstart(WebIDL::CallbackType* value)
  264. {
  265. set_event_handler_attribute(HTML::EventNames::loadstart, value);
  266. }
  267. WebIDL::CallbackType* FileReader::onloadstart()
  268. {
  269. return event_handler_attribute(HTML::EventNames::loadstart);
  270. }
  271. void FileReader::set_onprogress(WebIDL::CallbackType* value)
  272. {
  273. set_event_handler_attribute(HTML::EventNames::progress, value);
  274. }
  275. WebIDL::CallbackType* FileReader::onprogress()
  276. {
  277. return event_handler_attribute(HTML::EventNames::progress);
  278. }
  279. void FileReader::set_onload(WebIDL::CallbackType* value)
  280. {
  281. set_event_handler_attribute(HTML::EventNames::load, value);
  282. }
  283. WebIDL::CallbackType* FileReader::onload()
  284. {
  285. return event_handler_attribute(HTML::EventNames::load);
  286. }
  287. void FileReader::set_onabort(WebIDL::CallbackType* value)
  288. {
  289. set_event_handler_attribute(HTML::EventNames::abort, value);
  290. }
  291. WebIDL::CallbackType* FileReader::onabort()
  292. {
  293. return event_handler_attribute(HTML::EventNames::abort);
  294. }
  295. void FileReader::set_onerror(WebIDL::CallbackType* value)
  296. {
  297. set_event_handler_attribute(HTML::EventNames::error, value);
  298. }
  299. WebIDL::CallbackType* FileReader::onerror()
  300. {
  301. return event_handler_attribute(HTML::EventNames::error);
  302. }
  303. void FileReader::set_onloadend(WebIDL::CallbackType* value)
  304. {
  305. set_event_handler_attribute(HTML::EventNames::loadend, value);
  306. }
  307. WebIDL::CallbackType* FileReader::onloadend()
  308. {
  309. return event_handler_attribute(HTML::EventNames::loadend);
  310. }
  311. }