Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/CSS/Fetch.cpp (C++)

Code before:
    request->set_origin(environment_settings.origin());
    request->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::SameOrigin);
    request->set_use_url_credentials(true);
    request->set_client(&environment_settings);
    request->set_referrer(environment_settings.api_base_url());

    // 4. If corsMode is "no-cors", set req’s credentials mode to "include".
    if (cors_mode == CorsMode::NoCors)
        request->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::Include);

    // 5. Apply any URL request modifier steps that apply to this request.
    if (auto const* css_url = url_value.get_pointer<CSS::URL>())
        apply_request_modifiers_from_url_value(*css_url, request);

    // 6. If req’s mode is "cors", and sheet is not null, then set req’s referrer to the style resource base URL given cssRuleOrDeclaration. [CSSOM]
    // FIXME: Spec issue - sheet is not defined as a variable, we use the sheet determined from 'style resource base URL' instead.
    auto [sheet, base_url] = style_resource_base_url(css_rule_or_declaration);
    if (request->mode() == Fetch::Infrastructure::Request::Mode::CORS && sheet)
        request->set_referrer(base_url);

    // 7. If sheet’s origin-clean flag is set, set req’s initiator type to "css". [CSSOM]
    if (sheet) {
        if (sheet->is_origin_clean())
            request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::CSS);
    } else {
        // AD-HOC: If the resource is not associated with a stylesheet, we must still set an initiator type in order
        //         for this resource to be observable through a PerformanceObserver. WPT relies on this.
        request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::Script);
    }

    // 9. Fetch req, with processresponseconsumebody set to processResponse.
    // NB: Implemented by caller.
    return request;
}

// https://drafts.csswg.org/css-values-4/#fetch-a-style-resource
GC::Ptr<Fetch::Infrastructure::FetchController> fetch_a_style_resource(StyleResourceURL const& url_value, RuleOrDeclaration css_rule_or_declaration, Fetch::Infrastructure::Request::Destination destination, CorsMode cors_mode, Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_response)
{
    auto request = fetch_a_style_resource_impl(url_value, css_rule_or_declaration, destination, cors_mode);
    if (!request)
        return {};

    auto& environment_settings = *css_rule_or_declaration.environment_settings_object;
    auto& vm = environment_settings.vm();

    Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
    fetch_algorithms_input.process_response_consume_body = move(process_response);

    return Fetch::Fetching::fetch(environment_settings.realm(), *request, Fetch::Infrastructure::FetchAlgorithms::create(vm, move(fetch_algorithms_input)));
}


Review comment (suggestion):
This was already the case, but it seems sketchy to me that we apparently need an initiator type, but don't set one in the `sheet && !sheet->is_origin_clean()` case.

Code after:
    request->set_use_url_credentials(true);
    request->set_client(&environment_settings);
    request->set_referrer(environment_settings.api_base_url());

    // 4. If corsMode is "no-cors", set req’s credentials mode to "include".
    if (cors_mode == CorsMode::NoCors)
        request->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::Include);

    // 5. Apply any URL request modifier steps that apply to this request.
    if (auto const* css_url = url_value.get_pointer<CSS::URL>())
        apply_request_modifiers_from_url_value(*css_url, request);

    // 6. If req’s mode is "cors", and sheet is not null, then set req’s referrer to the style resource base URL given cssRuleOrDeclaration. [CSSOM]
    // FIXME: Spec issue - sheet is not defined as a variable, we use the sheet determined from 'style resource base URL' instead.
    //        https://github.com/w3c/csswg-drafts/issues/12288
    auto [sheet, base_url] = style_resource_base_url(css_rule_or_declaration);
    if (request->mode() == Fetch::Infrastructure::Request::Mode::CORS && sheet)
        request->set_referrer(base_url);

    // 7. If sheet’s origin-clean flag is set, set req’s initiator type to "css". [CSSOM]
    if (sheet) {
        if (sheet->is_origin_clean())
            request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::CSS);
    } else {
        // AD-HOC: If the resource is not associated with a stylesheet, we must still set an initiator type in order
        //         for this resource to be observable through a PerformanceObserver. WPT relies on this.
        request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::Script);
    }

    // 9. Fetch req, with processresponseconsumebody set to processResponse.
    // NB: Implemented by caller.
    return request;
}

// https://drafts.csswg.org/css-values-4/#fetch-a-style-resource
GC::Ptr<Fetch::Infrastructure::FetchController> fetch_a_style_resource(StyleResourceURL const& url_value, RuleOrDeclaration css_rule_or_declaration, Fetch::Infrastructure::Request::Destination destination, CorsMode cors_mode, Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_response)
{
    auto request = fetch_a_style_resource_impl(url_value, css_rule_or_declaration, destination, cors_mode);
    if (!request)
        return {};

    auto& environment_settings = *css_rule_or_declaration.environment_settings_object;
    auto& vm = environment_settings.vm();

    Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
    fetch_algorithms_input.process_response_consume_body = move(process_response);

    return Fetch::Fetching::fetch(environment_settings.realm(), *request, Fetch::Infrastructure::FetchAlgorithms::create(vm, move(fetch_algorithms_input)));
}

// https://drafts.csswg.org/css-images-4/#fetch-an-external-image-for-a-stylesheet