Blob.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. JS::NonnullGCPtr<Blob> Blob::create(JS::Realm& realm, ByteBuffer byte_buffer, DeprecatedString type)
  16. {
  17. return 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. void Blob::initialize(JS::Realm& realm)
  114. {
  115. Base::initialize(realm);
  116. set_prototype(&Bindings::ensure_web_prototype<Bindings::BlobPrototype>(realm, "Blob"));
  117. }
  118. // https://w3c.github.io/FileAPI/#ref-for-dom-blob-blob
  119. WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create(JS::Realm& realm, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options)
  120. {
  121. // 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.
  122. if (!blob_parts.has_value() && !options.has_value())
  123. return realm.heap().allocate<Blob>(realm, realm);
  124. ByteBuffer byte_buffer {};
  125. // 2. Let bytes be the result of processing blob parts given blobParts and options.
  126. if (blob_parts.has_value()) {
  127. byte_buffer = TRY_OR_THROW_OOM(realm.vm(), process_blob_parts(blob_parts.value(), options));
  128. }
  129. auto type = DeprecatedString::empty();
  130. // 3. If the type member of the options argument is not the empty string, run the following sub-steps:
  131. if (options.has_value() && !options->type.is_empty()) {
  132. // 1. If the type member is provided and is not the empty string, let t be set to the type dictionary member.
  133. // 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.
  134. // NOTE: t is set to empty string at declaration.
  135. if (!options->type.is_empty()) {
  136. if (is_basic_latin(options->type))
  137. type = options->type;
  138. }
  139. // 2. Convert every character in t to ASCII lowercase.
  140. if (!type.is_empty())
  141. type = options->type.to_lowercase();
  142. }
  143. // 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.
  144. return realm.heap().allocate<Blob>(realm, realm, move(byte_buffer), move(type));
  145. }
  146. WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::construct_impl(JS::Realm& realm, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options)
  147. {
  148. return Blob::create(realm, blob_parts, options);
  149. }
  150. // https://w3c.github.io/FileAPI/#dfn-slice
  151. WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::slice(Optional<i64> start, Optional<i64> end, Optional<DeprecatedString> const& content_type)
  152. {
  153. // 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.
  154. // User agents must process slice() with start normalized according to the following:
  155. i64 relative_start;
  156. if (!start.has_value()) {
  157. // a. If the optional start parameter is not used as a parameter when making this call, let relativeStart be 0.
  158. relative_start = 0;
  159. } else {
  160. auto start_value = start.value();
  161. // b. If start is negative, let relativeStart be max((size + start), 0).
  162. if (start_value < 0) {
  163. relative_start = max((size() + start_value), 0);
  164. }
  165. // c. Else, let relativeStart be min(start, size).
  166. else {
  167. relative_start = min(start_value, size());
  168. }
  169. }
  170. // 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:
  171. i64 relative_end;
  172. if (!end.has_value()) {
  173. // a. If the optional end parameter is not used as a parameter when making this call, let relativeEnd be size.
  174. relative_end = size();
  175. } else {
  176. auto end_value = end.value();
  177. // b. If end is negative, let relativeEnd be max((size + end), 0).
  178. if (end_value < 0) {
  179. relative_end = max((size() + end_value), 0);
  180. }
  181. // c Else, let relativeEnd be min(end, size).
  182. else {
  183. relative_end = min(end_value, size());
  184. }
  185. }
  186. // 3. The optional contentType parameter is used to set the ASCII-encoded string in lower case representing the media type of the Blob.
  187. // User agents must process the slice() with contentType normalized according to the following:
  188. DeprecatedString relative_content_type;
  189. if (!content_type.has_value()) {
  190. // a. If the contentType parameter is not provided, let relativeContentType be set to the empty string.
  191. relative_content_type = "";
  192. } else {
  193. // b. Else let relativeContentType be set to contentType and run the substeps below:
  194. // 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.
  195. // 2. Convert every character in relativeContentType to ASCII lowercase.
  196. relative_content_type = content_type->to_lowercase();
  197. }
  198. // 4. Let span be max((relativeEnd - relativeStart), 0).
  199. auto span = max((relative_end - relative_start), 0);
  200. // 5. Return a new Blob object S with the following characteristics:
  201. // a. S refers to span consecutive bytes from this, beginning with the byte at byte-order position relativeStart.
  202. // b. S.size = span.
  203. // c. S.type = relativeContentType.
  204. auto byte_buffer = TRY_OR_THROW_OOM(realm().vm(), m_byte_buffer.slice(relative_start, span));
  205. return heap().allocate<Blob>(realm(), realm(), move(byte_buffer), move(relative_content_type));
  206. }
  207. // https://w3c.github.io/FileAPI/#dom-blob-text
  208. JS::Promise* Blob::text()
  209. {
  210. // FIXME: 1. Let stream be the result of calling get stream on this.
  211. // 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.
  212. // FIXME: We still need to implement ReadableStream for this step to be fully valid.
  213. // 3. Let promise be the result of reading all bytes from stream with reader
  214. auto promise = JS::Promise::create(realm());
  215. auto result = JS::PrimitiveString::create(vm(), DeprecatedString { m_byte_buffer.bytes() });
  216. // 4. Return the result of transforming promise by a fulfillment handler that returns the result of running UTF-8 decode on its first argument.
  217. promise->fulfill(result);
  218. return promise;
  219. }
  220. // https://w3c.github.io/FileAPI/#dom-blob-arraybuffer
  221. JS::Promise* Blob::array_buffer()
  222. {
  223. // FIXME: 1. Let stream be the result of calling get stream on this.
  224. // 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.
  225. // FIXME: We still need to implement ReadableStream for this step to be fully valid.
  226. // 3. Let promise be the result of reading all bytes from stream with reader.
  227. auto promise = JS::Promise::create(realm());
  228. auto buffer_result = JS::ArrayBuffer::create(realm(), m_byte_buffer.size());
  229. if (buffer_result.is_error()) {
  230. promise->reject(buffer_result.release_error().value().release_value());
  231. return promise;
  232. }
  233. auto buffer = buffer_result.release_value();
  234. buffer->buffer().overwrite(0, m_byte_buffer.data(), m_byte_buffer.size());
  235. // 4. Return the result of transforming promise by a fulfillment handler that returns a new ArrayBuffer whose contents are its first argument.
  236. promise->fulfill(buffer);
  237. return promise;
  238. }
  239. }