/* * Copyright (c) 2020-2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Web { static DeprecatedString s_default_favicon_path = "/res/icons/16x16/app-browser.png"; static RefPtr s_default_favicon_bitmap; void FrameLoader::set_default_favicon_path(DeprecatedString path) { s_default_favicon_path = move(path); } FrameLoader::FrameLoader(HTML::BrowsingContext& browsing_context) : m_browsing_context(browsing_context) { if (!s_default_favicon_bitmap) { s_default_favicon_bitmap = Gfx::Bitmap::load_from_file(s_default_favicon_path).release_value_but_fixme_should_propagate_errors(); VERIFY(s_default_favicon_bitmap); } } FrameLoader::~FrameLoader() = default; bool FrameLoader::load(LoadRequest& request, Type type) { if (!request.is_valid()) { load_error_page(request.url(), "Invalid request"); return false; } if (!m_browsing_context->is_frame_nesting_allowed(request.url())) { dbgln("No further recursion is allowed for the frame, abort load!"); return false; } request.set_main_resource(true); auto& url = request.url(); if (type == Type::Navigation || type == Type::Reload || type == Type::Redirect) { if (auto* page = browsing_context().page()) { if (&page->top_level_browsing_context() == m_browsing_context) page->client().page_did_start_loading(url, type == Type::Redirect); } } // https://fetch.spec.whatwg.org/#concept-fetch // Step 12: If request’s header list does not contain `Accept`, then: // 1. Let value be `*/*`. (NOTE: Not necessary as we're about to override it) // 2. A user agent should set value to the first matching statement, if any, switching on request’s destination: // -> "document" // -> "frame" // -> "iframe" // `text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8` if (!request.headers().contains("Accept")) request.set_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request)); if (type == Type::IFrame) return true; if (url.scheme() == "http" || url.scheme() == "https") { AK::URL favicon_url; favicon_url.set_scheme(url.scheme()); favicon_url.set_host(url.host()); favicon_url.set_port(url.port_or_default()); favicon_url.set_paths({ "favicon.ico" }); ResourceLoader::the().load( favicon_url, [this, favicon_url](auto data, auto&, auto) { // Always fetch the current document auto* document = this->browsing_context().active_document(); if (document && document->has_active_favicon()) return; dbgln_if(SPAM_DEBUG, "Favicon downloaded, {} bytes from {}", data.size(), favicon_url); if (data.is_empty()) return; RefPtr favicon_bitmap; auto decoded_image = Platform::ImageCodecPlugin::the().decode_image(data); if (!decoded_image.has_value() || decoded_image->frames.is_empty()) { dbgln("Could not decode favicon {}", favicon_url); } else { favicon_bitmap = decoded_image->frames[0].bitmap; dbgln_if(IMAGE_DECODER_DEBUG, "Decoded favicon, {}", favicon_bitmap->size()); } load_favicon(favicon_bitmap); }, [this](auto&, auto) { // Always fetch the current document auto* document = this->browsing_context().active_document(); if (document && document->has_active_favicon()) return; load_favicon(); }); } else { load_favicon(); } return true; } bool FrameLoader::load(const AK::URL& url, Type type) { dbgln_if(SPAM_DEBUG, "FrameLoader::load: {}", url); if (!url.is_valid()) { load_error_page(url, "Invalid URL"); return false; } auto request = LoadRequest::create_for_url_on_page(url, browsing_context().page()); return load(request, type); } void FrameLoader::load_html(StringView html, const AK::URL& url) { auto& vm = Bindings::main_thread_vm(); auto response = Fetch::Infrastructure::Response::create(vm); response->url_list().append(url); HTML::NavigationParams navigation_params { .id = {}, .request = nullptr, .response = response, .origin = HTML::Origin {}, .policy_container = HTML::PolicyContainer {}, .final_sandboxing_flag_set = HTML::SandboxingFlagSet {}, .cross_origin_opener_policy = HTML::CrossOriginOpenerPolicy {}, .coop_enforcement_result = HTML::CrossOriginOpenerPolicyEnforcementResult {}, .reserved_environment = {}, .browsing_context = browsing_context(), .navigable = nullptr, }; auto document = DOM::Document::create_and_initialize(DOM::Document::Type::HTML, "text/html", move(navigation_params)).release_value_but_fixme_should_propagate_errors(); browsing_context().set_active_document(document); auto parser = HTML::HTMLParser::create(document, html, "utf-8"); parser->run(url); } static DeprecatedString s_error_page_url = "file:///res/html/error.html"; void FrameLoader::set_error_page_url(DeprecatedString error_page_url) { s_error_page_url = error_page_url; } // FIXME: Use an actual templating engine (our own one when it's built, preferably // with a way to check these usages at compile time) void FrameLoader::load_error_page(const AK::URL& failed_url, DeprecatedString const& error) { LoadRequest request = LoadRequest::create_for_url_on_page(s_error_page_url, browsing_context().page()); ResourceLoader::the().load( request, [this, failed_url, error](auto data, auto&, auto) { VERIFY(!data.is_null()); StringBuilder builder; SourceGenerator generator { builder }; generator.set("failed_url", escape_html_entities(failed_url.to_deprecated_string())); generator.set("error", escape_html_entities(error)); generator.append(data); load_html(generator.as_string_view(), s_error_page_url); }, [](auto& error, auto) { dbgln("Failed to load error page: {}", error); VERIFY_NOT_REACHED(); }); } void FrameLoader::load_favicon(RefPtr bitmap) { if (auto* page = browsing_context().page()) { if (bitmap) page->client().page_did_change_favicon(*bitmap); else if (s_default_favicon_bitmap) page->client().page_did_change_favicon(*s_default_favicon_bitmap); } } void FrameLoader::resource_did_load() { // This prevents us setting up the document of a removed browsing context container (BCC, e.g.