Blob.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/GenericLexer.h>
  7. #include <AK/StdLibExtras.h>
  8. #include <LibJS/Runtime/ArrayBuffer.h>
  9. #include <LibJS/Runtime/Completion.h>
  10. #include <LibWeb/Bindings/BlobPrototype.h>
  11. #include <LibWeb/Bindings/Intrinsics.h>
  12. #include <LibWeb/FileAPI/Blob.h>
  13. #include <LibWeb/WebIDL/AbstractOperations.h>
  14. namespace Web::FileAPI {
  15. WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create(JS::Realm& realm, ByteBuffer byte_buffer, DeprecatedString type)
  16. {
  17. return MUST_OR_THROW_OOM(realm.heap().allocate<Blob>(realm, realm, move(byte_buffer), move(type)));
  18. }
  19. // https://w3c.github.io/FileAPI/#convert-line-endings-to-native
  20. ErrorOr<DeprecatedString> convert_line_endings_to_native(DeprecatedString const& string)
  21. {
  22. // 1. Let native line ending be be the code point U+000A LF.
  23. auto native_line_ending = "\n"sv;
  24. // 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.
  25. // 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.
  26. // 3. Set result to the empty string.
  27. StringBuilder result;
  28. // 4. Let position be a position variable for s, initially pointing at the start of s.
  29. auto lexer = GenericLexer { string.view() };
  30. // 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.
  31. // 6. Append token to result.
  32. TRY(result.try_append(lexer.consume_until(is_any_of("\n\r"sv))));
  33. // 7. While position is not past the end of s:
  34. while (!lexer.is_eof()) {
  35. // 1. If the code point at position within s equals U+000D CR:
  36. if (lexer.peek() == '\r') {
  37. // 1. Append native line ending to result.
  38. TRY(result.try_append(native_line_ending));
  39. // 2. Advance position by 1.
  40. lexer.ignore(1);
  41. // 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.
  42. if (!lexer.is_eof() && lexer.peek() == '\n')
  43. lexer.ignore(1);
  44. }
  45. // 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.
  46. else if (lexer.peek() == '\n') {
  47. lexer.ignore(1);
  48. TRY(result.try_append(native_line_ending));
  49. }
  50. // 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.
  51. // 4. Append token to result.
  52. TRY(result.try_append(lexer.consume_until(is_any_of("\n\r"sv))));
  53. }
  54. // 5. Return result.
  55. return result.to_deprecated_string();
  56. }
  57. // https://w3c.github.io/FileAPI/#process-blob-parts
  58. ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optional<BlobPropertyBag> const& options)
  59. {
  60. // 1. Let bytes be an empty sequence of bytes.
  61. ByteBuffer bytes {};
  62. // 2. For each element in parts:
  63. for (auto const& blob_part : blob_parts) {
  64. TRY(blob_part.visit(
  65. // 1. If element is a USVString, run the following sub-steps:
  66. [&](DeprecatedString const& string) -> ErrorOr<void> {
  67. // 1. Let s be element.
  68. auto s = string;
  69. // 2. If the endings member of options is "native", set s to the result of converting line endings to native of element.
  70. if (options.has_value() && options->endings == Bindings::EndingType::Native)
  71. s = TRY(convert_line_endings_to_native(s));
  72. // NOTE: The AK::DeprecatedString is always UTF-8.
  73. // 3. Append the result of UTF-8 encoding s to bytes.
  74. return bytes.try_append(s.to_byte_buffer());
  75. },
  76. // 2. If element is a BufferSource, get a copy of the bytes held by the buffer source, and append those bytes to bytes.
  77. [&](JS::Handle<JS::Object> const& buffer_source) -> ErrorOr<void> {
  78. auto data_buffer = TRY(WebIDL::get_buffer_source_copy(*buffer_source.cell()));
  79. return bytes.try_append(data_buffer.bytes());
  80. },
  81. // 3. If element is a Blob, append the bytes it represents to bytes.
  82. [&](JS::Handle<Blob> const& blob) -> ErrorOr<void> {
  83. return bytes.try_append(blob->bytes());
  84. }));
  85. }
  86. // 3. Return bytes.
  87. return bytes;
  88. }
  89. bool is_basic_latin(StringView view)
  90. {
  91. for (auto code_point : view) {
  92. if (code_point < 0x0020 || code_point > 0x007E)
  93. return false;
  94. }
  95. return true;
  96. }
  97. Blob::Blob(JS::Realm& realm)
  98. : PlatformObject(realm)
  99. {
  100. }
  101. Blob::Blob(JS::Realm& realm, ByteBuffer byte_buffer, DeprecatedString type)
  102. : PlatformObject(realm)
  103. , m_byte_buffer(move(byte_buffer))
  104. , m_type(move(type))
  105. {
  106. }
  107. Blob::Blob(JS::Realm& realm, ByteBuffer byte_buffer)
  108. : PlatformObject(realm)
  109. , m_byte_buffer(move(byte_buffer))
  110. {
  111. }
  112. Blob::~Blob() = default;
  113. JS::ThrowCompletionOr<void> Blob::initialize(JS::Realm& realm)
  114. {
  115. MUST_OR_THROW_OOM(Base::initialize(realm));
  116. set_prototype(&Bindings::ensure_web_prototype<Bindings::BlobPrototype>(realm, "Blob"));
  117. return {};
  118. }
  119. // https://w3c.github.io/FileAPI/#ref-for-dom-blob-blob
  120. WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create(JS::Realm& realm, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options)
  121. {
  122. // 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.
  123. if (!blob_parts.has_value() && !options.has_value())
  124. return MUST_OR_THROW_OOM(realm.heap().allocate<Blob>(realm, realm));
  125. ByteBuffer byte_buffer {};
  126. // 2. Let bytes be the result of processing blob parts given blobParts and options.
  127. if (blob_parts.has_value()) {
  128. byte_buffer = TRY_OR_THROW_OOM(realm.vm(), process_blob_parts(blob_parts.value(), options));
  129. }
  130. auto type = DeprecatedString::empty();
  131. // 3. If the type member of the options argument is not the empty string, run the following sub-steps:
  132. if (options.has_value() && !options->type.is_empty()) {
  133. // 1. If the type member is provided and is not the empty string, let t be set to the type dictionary member.
  134. // 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.
  135. // NOTE: t is set to empty string at declaration.
  136. if (!options->type.is_empty()) {
  137. if (is_basic_latin(options->type))
  138. type = options->type;
  139. }
  140. // 2. Convert every character in t to ASCII lowercase.
  141. if (!type.is_empty())
  142. type = options->type.to_lowercase();
  143. }
  144. // 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.
  145. return MUST_OR_THROW_OOM(realm.heap().allocate<Blob>(realm, realm, move(byte_buffer), move(type)));
  146. }
  147. WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::construct_impl(JS::Realm& realm, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options)
  148. {
  149. return Blob::create(realm, blob_parts, options);
  150. }
  151. // https://w3c.github.io/FileAPI/#dfn-slice
  152. WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::slice(Optional<i64> start, Optional<i64> end, Optional<DeprecatedString> const& content_type)
  153. {
  154. // 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.
  155. // User agents must process slice() with start normalized according to the following:
  156. i64 relative_start;
  157. if (!start.has_value()) {
  158. // a. If the optional start parameter is not used as a parameter when making this call, let relativeStart be 0.
  159. relative_start = 0;
  160. } else {
  161. auto start_value = start.value();
  162. // b. If start is negative, let relativeStart be max((size + start), 0).
  163. if (start_value < 0) {
  164. relative_start = max((size() + start_value), 0);
  165. }
  166. // c. Else, let relativeStart be min(start, size).
  167. else {
  168. relative_start = min(start_value, size());
  169. }
  170. }
  171. // 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:
  172. i64 relative_end;
  173. if (!end.has_value()) {
  174. // a. If the optional end parameter is not used as a parameter when making this call, let relativeEnd be size.
  175. relative_end = size();
  176. } else {
  177. auto end_value = end.value();
  178. // b. If end is negative, let relativeEnd be max((size + end), 0).
  179. if (end_value < 0) {
  180. relative_end = max((size() + end_value), 0);
  181. }
  182. // c Else, let relativeEnd be min(end, size).
  183. else {
  184. relative_end = min(end_value, size());
  185. }
  186. }
  187. // 3. The optional contentType parameter is used to set the ASCII-encoded string in lower case representing the media type of the Blob.
  188. // User agents must process the slice() with contentType normalized according to the following:
  189. DeprecatedString relative_content_type;
  190. if (!content_type.has_value()) {
  191. // a. If the contentType parameter is not provided, let relativeContentType be set to the empty string.
  192. relative_content_type = "";
  193. } else {
  194. // b. Else let relativeContentType be set to contentType and run the substeps below:
  195. // FIXME: 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.
  196. // 2. Convert every character in relativeContentType to ASCII lowercase.
  197. relative_content_type = content_type->to_lowercase();
  198. }
  199. // 4. Let span be max((relativeEnd - relativeStart), 0).
  200. auto span = max((relative_end - relative_start), 0);
  201. // 5. Return a new Blob object S with the following characteristics:
  202. // a. S refers to span consecutive bytes from this, beginning with the byte at byte-order position relativeStart.
  203. // b. S.size = span.
  204. // c. S.type = relativeContentType.
  205. auto byte_buffer = TRY_OR_THROW_OOM(realm().vm(), m_byte_buffer.slice(relative_start, span));
  206. return MUST_OR_THROW_OOM(heap().allocate<Blob>(realm(), realm(), move(byte_buffer), move(relative_content_type)));
  207. }
  208. // https://w3c.github.io/FileAPI/#dom-blob-text
  209. JS::Promise* Blob::text()
  210. {
  211. // FIXME: 1. Let stream be the result of calling get stream on this.
  212. // FIXME: 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.
  213. // FIXME: We still need to implement ReadableStream for this step to be fully valid.
  214. // 3. Let promise be the result of reading all bytes from stream with reader
  215. auto promise = JS::Promise::create(realm());
  216. auto result = JS::PrimitiveString::create(vm(), DeprecatedString { m_byte_buffer.bytes() });
  217. // 4. Return the result of transforming promise by a fulfillment handler that returns the result of running UTF-8 decode on its first argument.
  218. promise->fulfill(result);
  219. return promise;
  220. }
  221. // https://w3c.github.io/FileAPI/#dom-blob-arraybuffer
  222. JS::Promise* Blob::array_buffer()
  223. {
  224. // FIXME: 1. Let stream be the result of calling get stream on this.
  225. // FIXME: 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.
  226. // FIXME: We still need to implement ReadableStream for this step to be fully valid.
  227. // 3. Let promise be the result of reading all bytes from stream with reader.
  228. auto promise = JS::Promise::create(realm());
  229. auto buffer_result = JS::ArrayBuffer::create(realm(), m_byte_buffer.size());
  230. if (buffer_result.is_error()) {
  231. promise->reject(buffer_result.release_error().value().release_value());
  232. return promise;
  233. }
  234. auto buffer = buffer_result.release_value();
  235. buffer->buffer().overwrite(0, m_byte_buffer.data(), m_byte_buffer.size());
  236. // 4. Return the result of transforming promise by a fulfillment handler that returns a new ArrayBuffer whose contents are its first argument.
  237. promise->fulfill(buffer);
  238. return promise;
  239. }
  240. }