Blob.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (c) 2022-2023, Kenneth Myhra <kennethmyhra@serenityos.org>
  3. * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/GenericLexer.h>
  8. #include <LibJS/Runtime/ArrayBuffer.h>
  9. #include <LibJS/Runtime/Completion.h>
  10. #include <LibJS/Runtime/TypedArray.h>
  11. #include <LibTextCodec/Decoder.h>
  12. #include <LibWeb/Bindings/BlobPrototype.h>
  13. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  14. #include <LibWeb/Bindings/Intrinsics.h>
  15. #include <LibWeb/FileAPI/Blob.h>
  16. #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
  17. #include <LibWeb/Infra/Strings.h>
  18. #include <LibWeb/Streams/AbstractOperations.h>
  19. #include <LibWeb/Streams/ReadableStreamDefaultReader.h>
  20. #include <LibWeb/WebIDL/AbstractOperations.h>
  21. #include <LibWeb/WebIDL/Buffers.h>
  22. namespace Web::FileAPI {
  23. JS_DEFINE_ALLOCATOR(Blob);
  24. JS::NonnullGCPtr<Blob> Blob::create(JS::Realm& realm, ByteBuffer byte_buffer, String type)
  25. {
  26. return realm.heap().allocate<Blob>(realm, realm, move(byte_buffer), move(type));
  27. }
  28. // https://w3c.github.io/FileAPI/#convert-line-endings-to-native
  29. ErrorOr<String> convert_line_endings_to_native(StringView string)
  30. {
  31. // 1. Let native line ending be be the code point U+000A LF.
  32. auto native_line_ending = "\n"sv;
  33. // 2. If the underlying platform’s conventions are to represent newlines as a carriage return and line feed sequence, set native line ending to the code point U+000D CR followed by the code point U+000A LF.
  34. // NOTE: this step is a no-op since LibWeb does not compile on Windows, which is the only platform we know of that that uses a carriage return and line feed sequence for line endings.
  35. // 3. Set result to the empty string.
  36. StringBuilder result;
  37. // 4. Let position be a position variable for s, initially pointing at the start of s.
  38. auto lexer = GenericLexer { string };
  39. // 5. Let token be the result of collecting a sequence of code points that are not equal to U+000A LF or U+000D CR from s given position.
  40. // 6. Append token to result.
  41. TRY(result.try_append(lexer.consume_until(is_any_of("\n\r"sv))));
  42. // 7. While position is not past the end of s:
  43. while (!lexer.is_eof()) {
  44. // 1. If the code point at position within s equals U+000D CR:
  45. if (lexer.peek() == '\r') {
  46. // 1. Append native line ending to result.
  47. TRY(result.try_append(native_line_ending));
  48. // 2. Advance position by 1.
  49. lexer.ignore(1);
  50. // 3. If position is not past the end of s and the code point at position within s equals U+000A LF advance position by 1.
  51. if (!lexer.is_eof() && lexer.peek() == '\n')
  52. lexer.ignore(1);
  53. }
  54. // 2. Otherwise if the code point at position within s equals U+000A LF, advance position by 1 and append native line ending to result.
  55. else if (lexer.peek() == '\n') {
  56. lexer.ignore(1);
  57. TRY(result.try_append(native_line_ending));
  58. }
  59. // 3. Let token be the result of collecting a sequence of code points that are not equal to U+000A LF or U+000D CR from s given position.
  60. // 4. Append token to result.
  61. TRY(result.try_append(lexer.consume_until(is_any_of("\n\r"sv))));
  62. }
  63. // 5. Return result.
  64. return result.to_string();
  65. }
  66. // https://w3c.github.io/FileAPI/#process-blob-parts
  67. ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optional<BlobPropertyBag> const& options)
  68. {
  69. // 1. Let bytes be an empty sequence of bytes.
  70. ByteBuffer bytes {};
  71. // 2. For each element in parts:
  72. for (auto const& blob_part : blob_parts) {
  73. TRY(blob_part.visit(
  74. // 1. If element is a USVString, run the following sub-steps:
  75. [&](String const& string) -> ErrorOr<void> {
  76. // 1. Let s be element.
  77. auto s = string;
  78. // 2. If the endings member of options is "native", set s to the result of converting line endings to native of element.
  79. if (options.has_value() && options->endings == Bindings::EndingType::Native)
  80. s = TRY(convert_line_endings_to_native(s));
  81. // NOTE: The AK::String is always UTF-8.
  82. // 3. Append the result of UTF-8 encoding s to bytes.
  83. return bytes.try_append(s.bytes());
  84. },
  85. // 2. If element is a BufferSource, get a copy of the bytes held by the buffer source, and append those bytes to bytes.
  86. [&](JS::Handle<WebIDL::BufferSource> const& buffer_source) -> ErrorOr<void> {
  87. auto data_buffer = TRY(WebIDL::get_buffer_source_copy(*buffer_source->raw_object()));
  88. return bytes.try_append(data_buffer.bytes());
  89. },
  90. // 3. If element is a Blob, append the bytes it represents to bytes.
  91. [&](JS::Handle<Blob> const& blob) -> ErrorOr<void> {
  92. return bytes.try_append(blob->bytes());
  93. }));
  94. }
  95. // 3. Return bytes.
  96. return bytes;
  97. }
  98. bool is_basic_latin(StringView view)
  99. {
  100. for (auto code_point : view) {
  101. if (code_point < 0x0020 || code_point > 0x007E)
  102. return false;
  103. }
  104. return true;
  105. }
  106. Blob::Blob(JS::Realm& realm)
  107. : PlatformObject(realm)
  108. {
  109. }
  110. Blob::Blob(JS::Realm& realm, ByteBuffer byte_buffer, String type)
  111. : PlatformObject(realm)
  112. , m_byte_buffer(move(byte_buffer))
  113. , m_type(move(type))
  114. {
  115. }
  116. Blob::Blob(JS::Realm& realm, ByteBuffer byte_buffer)
  117. : PlatformObject(realm)
  118. , m_byte_buffer(move(byte_buffer))
  119. {
  120. }
  121. Blob::~Blob() = default;
  122. void Blob::initialize(JS::Realm& realm)
  123. {
  124. Base::initialize(realm);
  125. set_prototype(&Bindings::ensure_web_prototype<Bindings::BlobPrototype>(realm, "Blob"_fly_string));
  126. }
  127. // https://w3c.github.io/FileAPI/#ref-for-dom-blob-blob
  128. JS::NonnullGCPtr<Blob> Blob::create(JS::Realm& realm, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options)
  129. {
  130. // 1. If invoked with zero parameters, return a new Blob object consisting of 0 bytes, with size set to 0, and with type set to the empty string.
  131. if (!blob_parts.has_value() && !options.has_value())
  132. return realm.heap().allocate<Blob>(realm, realm);
  133. ByteBuffer byte_buffer {};
  134. // 2. Let bytes be the result of processing blob parts given blobParts and options.
  135. if (blob_parts.has_value()) {
  136. byte_buffer = MUST(process_blob_parts(blob_parts.value(), options));
  137. }
  138. auto type = String {};
  139. // 3. If the type member of the options argument is not the empty string, run the following sub-steps:
  140. if (options.has_value() && !options->type.is_empty()) {
  141. // 1. If the type member is provided and is not the empty string, let t be set to the type dictionary member.
  142. // If t contains any characters outside the range U+0020 to U+007E, then set t to the empty string and return from these substeps.
  143. // NOTE: t is set to empty string at declaration.
  144. if (!options->type.is_empty()) {
  145. if (is_basic_latin(options->type))
  146. type = options->type;
  147. }
  148. // 2. Convert every character in t to ASCII lowercase.
  149. if (!type.is_empty())
  150. type = MUST(Infra::to_ascii_lowercase(type));
  151. }
  152. // 4. Return a Blob object referring to bytes as its associated byte sequence, with its size set to the length of bytes, and its type set to the value of t from the substeps above.
  153. return realm.heap().allocate<Blob>(realm, realm, move(byte_buffer), move(type));
  154. }
  155. WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::construct_impl(JS::Realm& realm, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options)
  156. {
  157. return Blob::create(realm, blob_parts, options);
  158. }
  159. // https://w3c.github.io/FileAPI/#dfn-slice
  160. WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::slice(Optional<i64> start, Optional<i64> end, Optional<String> const& content_type)
  161. {
  162. auto& vm = realm().vm();
  163. // 1. The optional start parameter is a value for the start point of a slice() call, and must be treated as a byte-order position, with the zeroth position representing the first byte.
  164. // User agents must process slice() with start normalized according to the following:
  165. i64 relative_start;
  166. if (!start.has_value()) {
  167. // a. If the optional start parameter is not used as a parameter when making this call, let relativeStart be 0.
  168. relative_start = 0;
  169. } else {
  170. auto start_value = start.value();
  171. // b. If start is negative, let relativeStart be max((size + start), 0).
  172. if (start_value < 0) {
  173. relative_start = max((size() + start_value), 0);
  174. }
  175. // c. Else, let relativeStart be min(start, size).
  176. else {
  177. relative_start = min(start_value, size());
  178. }
  179. }
  180. // 2. The optional end parameter is a value for the end point of a slice() call. User agents must process slice() with end normalized according to the following:
  181. i64 relative_end;
  182. if (!end.has_value()) {
  183. // a. If the optional end parameter is not used as a parameter when making this call, let relativeEnd be size.
  184. relative_end = size();
  185. } else {
  186. auto end_value = end.value();
  187. // b. If end is negative, let relativeEnd be max((size + end), 0).
  188. if (end_value < 0) {
  189. relative_end = max((size() + end_value), 0);
  190. }
  191. // c Else, let relativeEnd be min(end, size).
  192. else {
  193. relative_end = min(end_value, size());
  194. }
  195. }
  196. // 3. The optional contentType parameter is used to set the ASCII-encoded string in lower case representing the media type of the Blob.
  197. // User agents must process the slice() with contentType normalized according to the following:
  198. String relative_content_type;
  199. if (!content_type.has_value()) {
  200. // a. If the contentType parameter is not provided, let relativeContentType be set to the empty string.
  201. relative_content_type = String {};
  202. } else {
  203. // b. Else let relativeContentType be set to contentType and run the substeps below:
  204. // 1. If relativeContentType contains any characters outside the range of U+0020 to U+007E, then set relativeContentType to the empty string and return from these substeps.
  205. // NOTE: contentType is set to empty string at declaration.
  206. if (is_basic_latin(content_type.value())) {
  207. // 2. Convert every character in relativeContentType to ASCII lowercase.
  208. relative_content_type = TRY_OR_THROW_OOM(vm, Infra::to_ascii_lowercase(content_type.value()));
  209. }
  210. }
  211. // 4. Let span be max((relativeEnd - relativeStart), 0).
  212. auto span = max((relative_end - relative_start), 0);
  213. // 5. Return a new Blob object S with the following characteristics:
  214. // a. S refers to span consecutive bytes from this, beginning with the byte at byte-order position relativeStart.
  215. // b. S.size = span.
  216. // c. S.type = relativeContentType.
  217. auto byte_buffer = TRY_OR_THROW_OOM(vm, m_byte_buffer.slice(relative_start, span));
  218. return heap().allocate<Blob>(realm(), realm(), move(byte_buffer), move(relative_content_type));
  219. }
  220. // https://w3c.github.io/FileAPI/#dom-blob-stream
  221. WebIDL::ExceptionOr<JS::NonnullGCPtr<Streams::ReadableStream>> Blob::stream()
  222. {
  223. // The stream() method, when invoked, must return the result of calling get stream on this.
  224. return this->get_stream();
  225. }
  226. // https://w3c.github.io/FileAPI/#blob-get-stream
  227. WebIDL::ExceptionOr<JS::NonnullGCPtr<Streams::ReadableStream>> Blob::get_stream()
  228. {
  229. auto& realm = this->realm();
  230. // 1. Let stream be a new ReadableStream created in blob’s relevant Realm.
  231. auto stream = realm.heap().allocate<Streams::ReadableStream>(realm, realm);
  232. // 2. Set up stream with byte reading support.
  233. TRY(set_up_readable_stream_controller_with_byte_reading_support(stream));
  234. // FIXME: 3. Run the following steps in parallel:
  235. {
  236. // 1. While not all bytes of blob have been read:
  237. // NOTE: for simplicity the chunk is the entire buffer for now.
  238. {
  239. // 1. Let bytes be the byte sequence that results from reading a chunk from blob, or failure if a chunk cannot be read.
  240. auto bytes = m_byte_buffer;
  241. // 2. Queue a global task on the file reading task source given blob’s relevant global object to perform the following steps:
  242. HTML::queue_global_task(HTML::Task::Source::FileReading, realm.global_object(), [stream, bytes = move(bytes)]() {
  243. // NOTE: Using an TemporaryExecutionContext here results in a crash in the method HTML::incumbent_settings_object()
  244. // since we end up in a state where we have no execution context + an event loop with an empty incumbent
  245. // settings object stack. We still need an execution context therefore we push the realm's execution context
  246. // onto the realm's VM, and we need an incumbent settings object which is pushed onto the incumbent settings
  247. // object stack by EnvironmentSettings::prepare_to_run_callback().
  248. auto& realm = stream->realm();
  249. auto& environment_settings = Bindings::host_defined_environment_settings_object(realm);
  250. realm.vm().push_execution_context(environment_settings.realm_execution_context());
  251. environment_settings.prepare_to_run_callback();
  252. ScopeGuard const guard = [&environment_settings, &realm] {
  253. environment_settings.clean_up_after_running_callback();
  254. realm.vm().pop_execution_context();
  255. };
  256. // 1. If bytes is failure, then error stream with a failure reason and abort these steps.
  257. // 2. Let chunk be a new Uint8Array wrapping an ArrayBuffer containing bytes. If creating the ArrayBuffer throws an exception, then error stream with that exception and abort these steps.
  258. auto array_buffer = JS::ArrayBuffer::create(stream->realm(), bytes);
  259. auto chunk = JS::Uint8Array::create(stream->realm(), bytes.size(), *array_buffer);
  260. // 3. Enqueue chunk in stream.
  261. auto maybe_error = Bindings::throw_dom_exception_if_needed(stream->realm().vm(), [&]() {
  262. return readable_stream_enqueue(*stream->controller(), chunk);
  263. });
  264. if (maybe_error.is_error()) {
  265. readable_stream_error(*stream, maybe_error.release_error().value().value());
  266. return;
  267. }
  268. // FIXME: Close the stream now that we have finished enqueuing all chunks to the stream. Without this, ReadableStream.read will never resolve the second time around with 'done' set.
  269. // Nowhere in the spec seems to mention this - but testing against other implementations the stream does appear to be closed after reading all data (closed callback is fired).
  270. // Probably there is a better way of doing this.
  271. readable_stream_close(*stream);
  272. });
  273. }
  274. }
  275. // 4. Return stream.
  276. return stream;
  277. }
  278. // https://w3c.github.io/FileAPI/#dom-blob-text
  279. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> Blob::text()
  280. {
  281. auto& realm = this->realm();
  282. auto& vm = realm.vm();
  283. // 1. Let stream be the result of calling get stream on this.
  284. auto stream = TRY(this->get_stream());
  285. // 2. Let reader be the result of getting a reader from stream. If that threw an exception, return a new promise rejected with that exception.
  286. auto reader_or_exception = acquire_readable_stream_default_reader(*stream);
  287. if (reader_or_exception.is_exception()) {
  288. auto throw_completion = Bindings::dom_exception_to_throw_completion(vm, reader_or_exception.exception());
  289. auto promise_capability = WebIDL::create_rejected_promise(realm, *throw_completion.value());
  290. return JS::NonnullGCPtr { verify_cast<JS::Promise>(*promise_capability->promise().ptr()) };
  291. }
  292. auto reader = reader_or_exception.release_value();
  293. // 3. Let promise be the result of reading all bytes from stream with reader
  294. auto promise = TRY(reader->read_all_bytes_deprecated());
  295. // 4. Return the result of transforming promise by a fulfillment handler that returns the result of running UTF-8 decode on its first argument.
  296. return WebIDL::upon_fulfillment(*promise, [&](auto const& first_argument) -> WebIDL::ExceptionOr<JS::Value> {
  297. auto const& object = first_argument.as_object();
  298. VERIFY(is<JS::ArrayBuffer>(object));
  299. auto const& buffer = static_cast<const JS::ArrayBuffer&>(object).buffer();
  300. auto decoder = TextCodec::decoder_for("UTF-8"sv);
  301. auto utf8_text = TRY_OR_THROW_OOM(vm, TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, buffer));
  302. return JS::PrimitiveString::create(vm, move(utf8_text));
  303. });
  304. }
  305. // https://w3c.github.io/FileAPI/#dom-blob-arraybuffer
  306. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> Blob::array_buffer()
  307. {
  308. auto& realm = this->realm();
  309. auto& vm = realm.vm();
  310. // 1. Let stream be the result of calling get stream on this.
  311. auto stream = TRY(this->get_stream());
  312. // 2. Let reader be the result of getting a reader from stream. If that threw an exception, return a new promise rejected with that exception.
  313. auto reader_or_exception = acquire_readable_stream_default_reader(*stream);
  314. if (reader_or_exception.is_exception()) {
  315. auto throw_completion = Bindings::dom_exception_to_throw_completion(vm, reader_or_exception.exception());
  316. auto promise_capability = WebIDL::create_rejected_promise(realm, *throw_completion.value());
  317. return JS::NonnullGCPtr { verify_cast<JS::Promise>(*promise_capability->promise().ptr()) };
  318. }
  319. auto reader = reader_or_exception.release_value();
  320. // 3. Let promise be the result of reading all bytes from stream with reader.
  321. auto promise = TRY(reader->read_all_bytes_deprecated());
  322. // 4. Return the result of transforming promise by a fulfillment handler that returns a new ArrayBuffer whose contents are its first argument.
  323. return WebIDL::upon_fulfillment(*promise, [&](auto const& first_argument) -> WebIDL::ExceptionOr<JS::Value> {
  324. auto const& object = first_argument.as_object();
  325. VERIFY(is<JS::ArrayBuffer>(object));
  326. auto const& buffer = static_cast<const JS::ArrayBuffer&>(object).buffer();
  327. return JS::ArrayBuffer::create(realm, buffer);
  328. });
  329. }
  330. }