Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/HTML/Worker.cpp (C++)

Code before:
void Worker::visit_edges(Cell::Visitor& visitor)
{
    Base::visit_edges(visitor);
    visitor.visit(m_document);
    visitor.visit(m_outside_port);
    visitor.visit(m_agent);
}

// https://html.spec.whatwg.org/multipage/workers.html#dom-worker
// https://whatpr.org/html/9893/workers.html#dom-worker
WebIDL::ExceptionOr<GC::Ref<Worker>> Worker::create(TrustedTypes::TrustedScriptURLOrString const& script_url, WorkerOptions const& options, DOM::Document& document)
{
    dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Creating worker with script_url = {}", script_url);

    // Returns a new Worker object. scriptURL will be fetched and executed in the background,
    // creating a new global environment for which worker represents the communication channel.
    // options can be used to define the name of that global environment via the name option,
    // primarily for debugging purposes. It can also ensure this new global environment supports
    // JavaScript modules (specify type: "module"), and if that is specified, can also be used
    // to specify how scriptURL is fetched through the credentials option.

    // 1. Let compliantScriptURL be the result of invoking the Get Trusted Type compliant string algorithm with
    //    TrustedScriptURL, this's relevant global object, scriptURL, "Worker constructor", and "script".
    auto const compliaint_script_url = TRY(TrustedTypes::get_trusted_type_compliant_string(
        TrustedTypes::TrustedTypeName::TrustedScriptURL,
        HTML::relevant_global_object(document),
        script_url,
        TrustedTypes::InjectionSink::Workerconstructor,
        TrustedTypes::Script.to_string()));

    // 2. Let outside settings be the current principal settings object.
    auto& outside_settings = current_principal_settings_object();

    // 3. Parse the scriptURL argument relative to outside settings.
    auto url = outside_settings.parse_url(compliaint_script_url.to_utf8_but_should_be_ported_to_utf16());

    // 4. If this fails, throw a "SyntaxError" DOMException.
    if (!url.has_value()) {
        dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Invalid URL loaded '{}'.", compliaint_script_url);
        return WebIDL::SyntaxError::create(document.realm(), "url is not valid"_utf16);
    }

    // 5. Let worker URL be the resulting URL record.

    // 6. Let worker be a new Worker object.
    auto worker = document.realm().create<Worker>(compliaint_script_url.to_utf8_but_should_be_ported_to_utf16(), options, document);

    // 7. Let outside port be a new MessagePort in outside settings's Realm.
    auto outside_port = MessagePort::create(outside_settings.realm());

    // 8. Associate the outside port with worker

Review comment (suggestion):
suggestion:
```suggestion
        HTML::relevant_global_object(*this),
```

Code after:
void Worker::visit_edges(Cell::Visitor& visitor)
{
    Base::visit_edges(visitor);
    visitor.visit(m_document);
    visitor.visit(m_outside_port);
    visitor.visit(m_agent);
}

// https://html.spec.whatwg.org/multipage/workers.html#dom-worker
// https://whatpr.org/html/9893/workers.html#dom-worker
WebIDL::ExceptionOr<GC::Ref<Worker>> Worker::create(TrustedTypes::TrustedScriptURLOrString const& script_url, WorkerOptions const& options, DOM::Document& document)
{
    dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Creating worker with script_url = {}", script_url);

    // Returns a new Worker object. scriptURL will be fetched and executed in the background,
    // creating a new global environment for which worker represents the communication channel.
    // options can be used to define the name of that global environment via the name option,
    // primarily for debugging purposes. It can also ensure this new global environment supports
    // JavaScript modules (specify type: "module"), and if that is specified, can also be used
    // to specify how scriptURL is fetched through the credentials option.

    // 1. Let compliantScriptURL be the result of invoking the Get Trusted Type compliant string algorithm with
    //    TrustedScriptURL, this's relevant global object, scriptURL, "Worker constructor", and "script".
    auto const compliant_script_url = TRY(TrustedTypes::get_trusted_type_compliant_string(
        TrustedTypes::TrustedTypeName::TrustedScriptURL,
        HTML::relevant_global_object(document),
        script_url,
        TrustedTypes::InjectionSink::Workerconstructor,
        TrustedTypes::Script.to_string()));

    // 2. Let outside settings be the current principal settings object.
    auto& outside_settings = current_principal_settings_object();

    // 3. Parse the scriptURL argument relative to outside settings.
    auto url = outside_settings.parse_url(compliant_script_url.to_utf8_but_should_be_ported_to_utf16());

    // 4. If this fails, throw a "SyntaxError" DOMException.
    if (!url.has_value()) {
        dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Invalid URL loaded '{}'.", compliant_script_url);
        return WebIDL::SyntaxError::create(document.realm(), "url is not valid"_utf16);
    }

    // 5. Let worker URL be the resulting URL record.

    // 6. Let worker be a new Worker object.
    auto worker = document.realm().create<Worker>(compliant_script_url.to_utf8_but_should_be_ported_to_utf16(), options, document);

    // 7. Let outside port be a new MessagePort in outside settings's Realm.
    auto outside_port = MessagePort::create(outside_settings.realm());

    // 8. Associate the outside port with worker