Serenity Operating System
at master 217 lines 8.2 kB view raw
1/* 2 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2021, the SerenityOS developers. 4 * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> 5 * 6 * SPDX-License-Identifier: BSD-2-Clause 7 */ 8 9#include <AK/ByteBuffer.h> 10#include <AK/Debug.h> 11#include <AK/URL.h> 12#include <LibWeb/CSS/Parser/Parser.h> 13#include <LibWeb/DOM/Document.h> 14#include <LibWeb/HTML/HTMLLinkElement.h> 15#include <LibWeb/Infra/CharacterTypes.h> 16#include <LibWeb/Loader/ResourceLoader.h> 17#include <LibWeb/Page/Page.h> 18#include <LibWeb/Platform/ImageCodecPlugin.h> 19 20namespace Web::HTML { 21 22HTMLLinkElement::HTMLLinkElement(DOM::Document& document, DOM::QualifiedName qualified_name) 23 : HTMLElement(document, move(qualified_name)) 24{ 25} 26 27HTMLLinkElement::~HTMLLinkElement() = default; 28 29JS::ThrowCompletionOr<void> HTMLLinkElement::initialize(JS::Realm& realm) 30{ 31 MUST_OR_THROW_OOM(Base::initialize(realm)); 32 set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLLinkElementPrototype>(realm, "HTMLLinkElement")); 33 34 return {}; 35} 36 37void HTMLLinkElement::inserted() 38{ 39 if (has_attribute(AttributeNames::disabled) && (m_relationship & Relationship::Stylesheet)) 40 return; 41 42 HTMLElement::inserted(); 43 44 if (m_relationship & Relationship::Stylesheet && !(m_relationship & Relationship::Alternate)) { 45 auto url = document().parse_url(href()); 46 dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Loading import URL: {}", url); 47 auto request = LoadRequest::create_for_url_on_page(url, document().page()); 48 // NOTE: Mark this element as delaying the document load event *before* calling set_resource() 49 // as it may trigger a synchronous resource_did_load() callback. 50 m_document_load_event_delayer.emplace(document()); 51 set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request)); 52 53 // NOTE: If we ended up not loading a resource for whatever reason, don't delay the load event. 54 if (!resource()) 55 m_document_load_event_delayer.clear(); 56 } 57 58 if (m_relationship & Relationship::Preload) { 59 // FIXME: Respect the "as" attribute. 60 LoadRequest request; 61 request.set_url(document().parse_url(attribute(HTML::AttributeNames::href))); 62 m_preload_resource = ResourceLoader::the().load_resource(Resource::Type::Generic, request); 63 } else if (m_relationship & Relationship::DNSPrefetch) { 64 ResourceLoader::the().prefetch_dns(document().parse_url(attribute(HTML::AttributeNames::href))); 65 } else if (m_relationship & Relationship::Preconnect) { 66 ResourceLoader::the().preconnect(document().parse_url(attribute(HTML::AttributeNames::href))); 67 } else if (m_relationship & Relationship::Icon) { 68 auto favicon_url = document().parse_url(href()); 69 auto favicon_request = LoadRequest::create_for_url_on_page(favicon_url, document().page()); 70 set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, favicon_request)); 71 } 72} 73 74bool HTMLLinkElement::has_loaded_icon() const 75{ 76 return m_relationship & Relationship::Icon && resource() && resource()->is_loaded() && resource()->has_encoded_data(); 77} 78 79void HTMLLinkElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value) 80{ 81 // 4.6.7 Link types - https://html.spec.whatwg.org/multipage/links.html#linkTypes 82 if (name == HTML::AttributeNames::rel) { 83 m_relationship = 0; 84 // Keywords are always ASCII case-insensitive, and must be compared as such. 85 auto lowercased_value = value.to_lowercase(); 86 // To determine which link types apply to a link, a, area, or form element, 87 // the element's rel attribute must be split on ASCII whitespace. 88 // The resulting tokens are the keywords for the link types that apply to that element. 89 auto parts = lowercased_value.split_view(Infra::is_ascii_whitespace); 90 for (auto& part : parts) { 91 if (part == "stylesheet"sv) 92 m_relationship |= Relationship::Stylesheet; 93 else if (part == "alternate"sv) 94 m_relationship |= Relationship::Alternate; 95 else if (part == "preload"sv) 96 m_relationship |= Relationship::Preload; 97 else if (part == "dns-prefetch"sv) 98 m_relationship |= Relationship::DNSPrefetch; 99 else if (part == "preconnect"sv) 100 m_relationship |= Relationship::Preconnect; 101 else if (part == "icon"sv) 102 m_relationship |= Relationship::Icon; 103 } 104 } 105 106 if (name == HTML::AttributeNames::disabled && (m_relationship & Relationship::Stylesheet) && m_loaded_style_sheet) 107 document().style_sheets().remove_sheet(*m_loaded_style_sheet); 108} 109 110void HTMLLinkElement::resource_did_fail() 111{ 112 dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did fail. URL: {}", resource()->url()); 113 114 m_document_load_event_delayer.clear(); 115} 116 117void HTMLLinkElement::resource_did_load() 118{ 119 VERIFY(resource()); 120 VERIFY(m_relationship & (Relationship::Stylesheet | Relationship::Icon)); 121 122 if (m_relationship & Relationship::Stylesheet) 123 resource_did_load_stylesheet(); 124 if (m_relationship & Relationship::Icon) 125 resource_did_load_favicon(); 126} 127 128void HTMLLinkElement::did_remove_attribute(DeprecatedFlyString const& attr) 129{ 130 if (attr == HTML::AttributeNames::disabled && (m_relationship & Relationship::Stylesheet)) { 131 if (!resource()) 132 inserted(); 133 else 134 resource_did_load_stylesheet(); 135 } 136} 137 138void HTMLLinkElement::resource_did_load_stylesheet() 139{ 140 VERIFY(m_relationship & Relationship::Stylesheet); 141 m_document_load_event_delayer.clear(); 142 143 if (!resource()->has_encoded_data()) { 144 dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did load, no encoded data. URL: {}", resource()->url()); 145 } else { 146 dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did load, has encoded data. URL: {}", resource()->url()); 147 148 if (resource()->mime_type() != "text/css"sv) { 149 dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did load, but MIME type was {} instead of text/css. URL: {}", resource()->mime_type(), resource()->url()); 150 return; 151 } 152 } 153 154 CSS::CSSStyleSheet* sheet = m_loaded_style_sheet; 155 if (!sheet) { 156 sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(document(), resource()->url()), resource()->encoded_data()); 157 158 if (!sheet) { 159 dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Failed to parse stylesheet: {}", resource()->url()); 160 return; 161 } 162 163 m_loaded_style_sheet = sheet; 164 } 165 166 sheet->set_owner_node(this); 167 document().style_sheets().add_sheet(*sheet); 168} 169 170void HTMLLinkElement::resource_did_load_favicon() 171{ 172 VERIFY(m_relationship & (Relationship::Icon)); 173 if (!resource()->has_encoded_data()) { 174 dbgln_if(SPAM_DEBUG, "Favicon downloaded, no encoded data"); 175 return; 176 } 177 178 dbgln_if(SPAM_DEBUG, "Favicon downloaded, {} bytes from {}", resource()->encoded_data().size(), resource()->url()); 179 180 document().check_favicon_after_loading_link_resource(); 181} 182 183bool HTMLLinkElement::load_favicon_and_use_if_window_is_active() 184{ 185 if (!has_loaded_icon()) 186 return false; 187 188 RefPtr<Gfx::Bitmap> favicon_bitmap; 189 auto decoded_image = Platform::ImageCodecPlugin::the().decode_image(resource()->encoded_data()); 190 if (!decoded_image.has_value() || decoded_image->frames.is_empty()) { 191 dbgln("Could not decode favicon {}", resource()->url()); 192 return false; 193 } 194 195 favicon_bitmap = decoded_image->frames[0].bitmap; 196 dbgln_if(IMAGE_DECODER_DEBUG, "Decoded favicon, {}", favicon_bitmap->size()); 197 198 auto* page = document().page(); 199 if (!page) 200 return favicon_bitmap; 201 202 if (document().browsing_context() == &page->top_level_browsing_context()) 203 if (favicon_bitmap) { 204 page->client().page_did_change_favicon(*favicon_bitmap); 205 return true; 206 } 207 208 return false; 209} 210 211void HTMLLinkElement::visit_edges(Cell::Visitor& visitor) 212{ 213 Base::visit_edges(visitor); 214 visitor.visit(m_loaded_style_sheet); 215} 216 217}