IncrementalReadLoopReadRequest.cpp 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2024, Kenneth Myhra <kennethmyhra@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/TypedArray.h>
  7. #include <LibWeb/Bindings/HostDefined.h>
  8. #include <LibWeb/Fetch/Infrastructure/IncrementalReadLoopReadRequest.h>
  9. #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
  10. namespace Web::Fetch::Infrastructure {
  11. JS_DEFINE_ALLOCATOR(IncrementalReadLoopReadRequest);
  12. void IncrementalReadLoopReadRequest::on_chunk(JS::Value chunk)
  13. {
  14. auto& realm = m_reader->realm();
  15. // 1. Let continueAlgorithm be null.
  16. JS::GCPtr<JS::HeapFunction<void()>> continue_algorithm;
  17. // 2. If chunk is not a Uint8Array object, then set continueAlgorithm to this step: run processBodyError given a TypeError.
  18. if (!chunk.is_object() || !is<JS::Uint8Array>(chunk.as_object())) {
  19. continue_algorithm = JS::create_heap_function(realm.heap(), [&realm, process_body_error = m_process_body_error] {
  20. process_body_error->function()(JS::TypeError::create(realm, "Chunk data is not Uint8Array"sv));
  21. });
  22. }
  23. // 3. Otherwise:
  24. else {
  25. // 1. Let bytes be a copy of chunk.
  26. // NOTE: Implementations are strongly encouraged to use an implementation strategy that avoids this copy where possible.
  27. auto& uint8_array = static_cast<JS::Uint8Array&>(chunk.as_object());
  28. auto bytes = MUST(ByteBuffer::copy(uint8_array.data()));
  29. // 2. Set continueAlgorithm to these steps:
  30. continue_algorithm = JS::create_heap_function(realm.heap(), [bytes = move(bytes), body = m_body, reader = m_reader, task_destination = m_task_destination, process_body_chunk = m_process_body_chunk, process_end_of_body = m_process_end_of_body, process_body_error = m_process_body_error] {
  31. HTML::TemporaryExecutionContext execution_context { reader->realm(), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
  32. // 1. Run processBodyChunk given bytes.
  33. process_body_chunk->function()(move(bytes));
  34. // 2. Perform the incrementally-read loop given reader, taskDestination, processBodyChunk, processEndOfBody, and processBodyError.
  35. body->incrementally_read_loop(reader, task_destination, process_body_chunk, process_end_of_body, process_body_error);
  36. });
  37. }
  38. // 4. Queue a fetch task given continueAlgorithm and taskDestination.
  39. Fetch::Infrastructure::queue_fetch_task(m_task_destination, *continue_algorithm);
  40. }
  41. void IncrementalReadLoopReadRequest::on_close()
  42. {
  43. // 1. Queue a fetch task given processEndOfBody and taskDestination.
  44. Fetch::Infrastructure::queue_fetch_task(m_task_destination, JS::create_heap_function(m_reader->heap(), [this] {
  45. m_process_end_of_body->function()();
  46. }));
  47. }
  48. void IncrementalReadLoopReadRequest::on_error(JS::Value error)
  49. {
  50. // 1. Queue a fetch task to run processBodyError given e, with taskDestination.
  51. Fetch::Infrastructure::queue_fetch_task(m_task_destination, JS::create_heap_function(m_reader->heap(), [this, error = move(error)] {
  52. m_process_body_error->function()(error);
  53. }));
  54. }
  55. IncrementalReadLoopReadRequest::IncrementalReadLoopReadRequest(JS::NonnullGCPtr<Body> body, JS::NonnullGCPtr<Streams::ReadableStreamDefaultReader> reader, JS::NonnullGCPtr<JS::Object> task_destination, Body::ProcessBodyChunkCallback process_body_chunk, Body::ProcessEndOfBodyCallback process_end_of_body, Body::ProcessBodyErrorCallback process_body_error)
  56. : m_body(body)
  57. , m_reader(reader)
  58. , m_task_destination(task_destination)
  59. , m_process_body_chunk(process_body_chunk)
  60. , m_process_end_of_body(process_end_of_body)
  61. , m_process_body_error(process_body_error)
  62. {
  63. }
  64. void IncrementalReadLoopReadRequest::visit_edges(Visitor& visitor)
  65. {
  66. Base::visit_edges(visitor);
  67. visitor.visit(m_body);
  68. visitor.visit(m_reader);
  69. visitor.visit(m_task_destination);
  70. visitor.visit(m_process_body_chunk);
  71. visitor.visit(m_process_end_of_body);
  72. visitor.visit(m_process_body_error);
  73. }
  74. }