Skip to content

Truly independent web browser

10.8 KB
Raw Blame
/*
 * Copyright (c) 2022, Linus Groh <[email protected]>
 * Copyright (c) 2023-2024, Shannon Booth <[email protected]>
 * Copyright (c) 2024, Kenneth Myhra <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibJS/HostClassBuilder.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/RustSerialization.h>
#include <LibWeb/Page/RenderingStats.h>
#include <LibWeb/Streams/ReadableStream.h>
#include <LibWeb/Streams/ReadableStreamDefaultReader.h>
#include <LibWeb/WebIDL/ExceptionOrUtils.h>

namespace Web::Streams {

extern "C" {

// Defined in Bindings/Rust/src/streams/exports.rs.
void* web_rust_readable_stream_create(JSVM*, void* realm);
void web_rust_readable_stream_set_up_with_byte_reading_support(JSVM*, void* realm, void* stream, PullAlgorithm* pull_algorithm, CancelAlgorithm* cancel_algorithm, double high_water_mark);
u8 web_rust_readable_stream_state(void* stream);
bool web_rust_readable_stream_is_locked(void* stream);
bool web_rust_readable_stream_is_disturbed(void* stream);
bool web_rust_readable_stream_close_requested(void* stream);
bool web_rust_readable_stream_is_readable_byte_stream(void* stream);
bool web_rust_readable_stream_desired_size(void* stream, double* desired_size);
JSCompletion web_rust_readable_stream_enqueue(JSVM*, void* stream, JSValue chunk);
JSCompletion web_rust_readable_stream_enqueue_native_bytes(JSVM*, void* stream, ReadonlyBytes);
bool web_rust_readable_stream_current_byob_request_view_byte_length(JSVM*, void* stream, u64* byte_length);
JSCompletion web_rust_readable_stream_pull_from_bytes(JSVM*, void* stream, ReadonlyBytes);
void web_rust_readable_stream_close(JSVM*, void* stream);
void web_rust_readable_stream_error(JSVM*, void* stream, JSValue error);
void* web_rust_readable_stream_cancel(JSVM*, void* stream, JSValue reason);
JSCompletion web_rust_readable_stream_tee(JSVM*, void* stream, void** branch1, void** branch2);
JSCompletion web_rust_readable_stream_get_a_reader(JSVM*, void* stream, void** reader);
void* web_rust_readable_stream_piped_through(JSVM*, void* stream, void* transform, bool prevent_close, bool prevent_abort, bool prevent_cancel);
void web_rust_readable_stream_default_reader_read_a_chunk(JSVM*, void* reader, ReadRequest* read_request);
void web_rust_readable_stream_default_reader_read_all_bytes(JSVM*, void* reader, ReadableStreamDefaultReader::SuccessSteps* success_steps, ReadableStreamDefaultReader::FailureSteps* failure_steps);
void* web_rust_readable_stream_default_reader_read(JSVM*, void* reader);
void* web_rust_readable_stream_default_reader_cancel(JSVM*, void* reader, JSValue reason);
void* web_rust_readable_stream_default_reader_closed_promise_realm(void* reader);

// Called by Bindings/Rust/src/streams.
void* web_streams_call_promise_algorithm(GC::Function<GC::Ref<WebIDL::Promise>()>*);
void* web_streams_call_promise_algorithm_with_argument(GC::Function<GC::Ref<WebIDL::Promise>(JS::Value)>*, JSValue argument);
void web_streams_read_request_chunk_steps(ReadRequest*, JSValue chunk);
void web_streams_read_request_close_steps(ReadRequest*);
void web_streams_read_request_error_steps(ReadRequest*, JSValue error);
void web_streams_call_bytes_steps(ReadableStreamDefaultReader::SuccessSteps*, ReadonlyBytes);
void web_streams_call_value_steps(ReadableStreamDefaultReader::FailureSteps*, JSValue);
void web_streams_complete_rendering_pending_stream(ReadableStream*);
}

static JSVM* vm_abi()
{
    return JS::HostABI::vm_to_abi(JS::VM::the());
}

static JS::Realm& realm_of(void* realm)
{
    return *static_cast<JS::Realm*>(realm);
}

static WebIDL::ExceptionOr<void> exception_or_void(JSCompletion completion)
{
    auto result = JS::HostABI::completion_from_abi<JS::Value>(completion);
    if (result.is_error())
        return result.release_error();
    return {};
}

GC::Ref<ReadableStream> ReadableStream::create(JS::Realm& realm)
{
    return GC::Ref { *static_cast<ReadableStream*>(web_rust_readable_stream_create(vm_abi(), &realm)) };
}

void ReadableStream::set_up_with_byte_reading_support(JS::Realm& realm, GC::Ptr<PullAlgorithm> pull_algorithm, GC::Ptr<CancelAlgorithm> cancel_algorithm, double high_water_mark)
{
    web_rust_readable_stream_set_up_with_byte_reading_support(vm_abi(), &realm, this, pull_algorithm.ptr(), cancel_algorithm.ptr(), high_water_mark);
}

bool ReadableStream::is_readable_byte_stream() const
{
    return web_rust_readable_stream_is_readable_byte_stream(const_cast<ReadableStream*>(this));
}

bool ReadableStream::is_readable() const
{
    return web_rust_readable_stream_state(const_cast<ReadableStream*>(this)) == 0;
}

bool ReadableStream::is_closed() const
{
    return web_rust_readable_stream_state(const_cast<ReadableStream*>(this)) == 1;
}

bool ReadableStream::is_errored() const
{
    return web_rust_readable_stream_state(const_cast<ReadableStream*>(this)) == 2;
}

bool ReadableStream::is_locked() const
{
    return web_rust_readable_stream_is_locked(const_cast<ReadableStream*>(this));
}

bool ReadableStream::is_disturbed() const
{
    return web_rust_readable_stream_is_disturbed(const_cast<ReadableStream*>(this));
}

bool ReadableStream::close_requested() const
{
    return web_rust_readable_stream_close_requested(const_cast<ReadableStream*>(this));
}

Optional<double> ReadableStream::desired_size() const
{
    double desired_size = 0;
    if (!web_rust_readable_stream_desired_size(const_cast<ReadableStream*>(this), &desired_size))
        return {};
    return desired_size;
}

WebIDL::ExceptionOr<void> ReadableStream::enqueue(JS::Value chunk)
{
    return exception_or_void(web_rust_readable_stream_enqueue(vm_abi(), this, JS::HostABI::value_to_abi(chunk)));
}

WebIDL::ExceptionOr<void> ReadableStream::enqueue_bytes(ReadonlyBytes bytes)
{
    return exception_or_void(web_rust_readable_stream_enqueue_native_bytes(vm_abi(), this, bytes));
}

Optional<u64> ReadableStream::current_byob_request_view_byte_length()
{
    u64 byte_length = 0;
    if (!web_rust_readable_stream_current_byob_request_view_byte_length(vm_abi(), this, &byte_length))
        return {};
    return byte_length;
}

WebIDL::ExceptionOr<void> ReadableStream::pull_from_bytes(ReadonlyBytes bytes)
{
    return exception_or_void(web_rust_readable_stream_pull_from_bytes(vm_abi(), this, bytes));
}

void ReadableStream::close()
{
    web_rust_readable_stream_close(vm_abi(), this);
}

void ReadableStream::error(JS::Value error)
{
    web_rust_readable_stream_error(vm_abi(), this, JS::HostABI::value_to_abi(error));
}

GC::Ref<WebIDL::Promise> ReadableStream::cancel(JS::Value reason)
{
    return GC::Ref { *static_cast<WebIDL::Promise*>(web_rust_readable_stream_cancel(vm_abi(), this, JS::HostABI::value_to_abi(reason))) };
}

WebIDL::ExceptionOr<ReadableStreamPair> ReadableStream::tee()
{
    void* branch1 = nullptr;
    void* branch2 = nullptr;
    TRY(exception_or_void(web_rust_readable_stream_tee(vm_abi(), this, &branch1, &branch2)));
    return ReadableStreamPair { *static_cast<ReadableStream*>(branch1), *static_cast<ReadableStream*>(branch2) };
}

WebIDL::ExceptionOr<GC::Ref<ReadableStreamDefaultReader>> ReadableStream::get_a_reader()
{
    void* reader = nullptr;
    TRY(exception_or_void(web_rust_readable_stream_get_a_reader(vm_abi(), this, &reader)));
    return GC::Ref { *static_cast<ReadableStreamDefaultReader*>(reader) };
}

GC::Ref<ReadableStream> ReadableStream::piped_through(GC::Ref<TransformStream> transform, bool prevent_close, bool prevent_abort, bool prevent_cancel)
{
    return GC::Ref { *static_cast<ReadableStream*>(web_rust_readable_stream_piped_through(vm_abi(), this, transform.ptr(), prevent_close, prevent_abort, prevent_cancel)) };
}

GC::Ref<WebIDL::Promise> ReadableStreamDefaultReader::read()
{
    return GC::Ref { *static_cast<WebIDL::Promise*>(web_rust_readable_stream_default_reader_read(vm_abi(), this)) };
}

void ReadableStreamDefaultReader::read_a_chunk(ReadRequest& read_request)
{
    web_rust_readable_stream_default_reader_read_a_chunk(vm_abi(), this, &read_request);
}

void ReadableStreamDefaultReader::read_all_bytes(GC::Ref<SuccessSteps> success_steps, GC::Ref<FailureSteps> failure_steps)
{
    web_rust_readable_stream_default_reader_read_all_bytes(vm_abi(), this, success_steps.ptr(), failure_steps.ptr());
}

// FIXME: This function is a promise-based wrapper around "read all bytes". The spec changed this function to not use promises
//        in https://github.com/whatwg/streams/commit/f894acdd417926a2121710803cef593e15127964 - however, it seems that the
//        FileAPI blob specification has not been updated to match, see: https://github.com/w3c/FileAPI/issues/187.
GC::Ref<WebIDL::Promise> ReadableStreamDefaultReader::read_all_bytes_deprecated()
{
    auto& realm = closed_promise_realm();

    auto promise = WebIDL::create_promise(realm);

    auto success_steps = GC::create_function(GC::Heap::the(), GC::captures(promise, realm), [](auto& promise, auto& realm, ByteBuffer bytes) {
        auto buffer = JS::ArrayBuffer::create(realm, move(bytes));
        WebIDL::resolve_promise(promise, buffer);
    });

    auto failure_steps = GC::create_function(GC::Heap::the(), GC::captures(promise), [](auto& promise, JS::Value error) {
        WebIDL::reject_promise(promise, error);
    });

    read_all_bytes(success_steps, failure_steps);

    return promise;
}

GC::Ref<WebIDL::Promise> ReadableStreamDefaultReader::cancel(JS::Value reason)
{
    return GC::Ref { *static_cast<WebIDL::Promise*>(web_rust_readable_stream_default_reader_cancel(vm_abi(), this, JS::HostABI::value_to_abi(reason))) };
}

JS::Realm& ReadableStreamDefaultReader::closed_promise_realm() const
{
    return realm_of(web_rust_readable_stream_default_reader_closed_promise_realm(const_cast<ReadableStreamDefaultReader*>(this)));
}

void* web_streams_call_promise_algorithm(GC::Function<GC::Ref<WebIDL::Promise>()>* algorithm)
{
    return algorithm->function()().ptr();
}

void* web_streams_call_promise_algorithm_with_argument(GC::Function<GC::Ref<WebIDL::Promise>(JS::Value)>* algorithm, JSValue argument)
{
    return algorithm->function()(JS::HostABI::value_from_abi(argument)).ptr();
}

void web_streams_read_request_chunk_steps(ReadRequest* read_request, JSValue chunk)
{
    read_request->on_chunk(JS::HostABI::value_from_abi(chunk));
}

void web_streams_read_request_close_steps(ReadRequest* read_request)
{
    read_request->on_close();
}

void web_streams_read_request_error_steps(ReadRequest* read_request, JSValue error)
{
    read_request->on_error(JS::HostABI::value_from_abi(error));
}

void web_streams_call_bytes_steps(ReadableStreamDefaultReader::SuccessSteps* steps, ReadonlyBytes bytes)
{
    steps->function()(MUST(ByteBuffer::copy(bytes)));
}

void web_streams_call_value_steps(ReadableStreamDefaultReader::FailureSteps* steps, JSValue value)
{
    steps->function()(JS::HostABI::value_from_abi(value));
}

void web_streams_complete_rendering_pending_stream(ReadableStream* stream)
{
    complete_rendering_pending_stream(*stream);
}

}