| 1 | /* |
| 2 | * Copyright (C) 2007 Eric Seidel <eric@webkit.org> |
| 3 | * Copyright (C) 2009-2017 Apple Inc. All rights reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Library General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Library General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Library General Public License |
| 16 | * along with this library; see the file COPYING.LIB. If not, write to |
| 17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 | * Boston, MA 02110-1301, USA. |
| 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | |
| 23 | #if ENABLE(SVG_FONTS) |
| 24 | #include "SVGFontFaceUriElement.h" |
| 25 | |
| 26 | #include "CSSFontFaceSrcValue.h" |
| 27 | #include "CachedFont.h" |
| 28 | #include "CachedResourceLoader.h" |
| 29 | #include "CachedResourceRequest.h" |
| 30 | #include "Document.h" |
| 31 | #include "SVGFontFaceElement.h" |
| 32 | #include "SVGNames.h" |
| 33 | #include "XLinkNames.h" |
| 34 | #include <wtf/IsoMallocInlines.h> |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | WTF_MAKE_ISO_ALLOCATED_IMPL(SVGFontFaceUriElement); |
| 39 | |
| 40 | using namespace SVGNames; |
| 41 | |
| 42 | inline SVGFontFaceUriElement::SVGFontFaceUriElement(const QualifiedName& tagName, Document& document) |
| 43 | : SVGElement(tagName, document) |
| 44 | { |
| 45 | ASSERT(hasTagName(font_face_uriTag)); |
| 46 | } |
| 47 | |
| 48 | Ref<SVGFontFaceUriElement> SVGFontFaceUriElement::create(const QualifiedName& tagName, Document& document) |
| 49 | { |
| 50 | return adoptRef(*new SVGFontFaceUriElement(tagName, document)); |
| 51 | } |
| 52 | |
| 53 | SVGFontFaceUriElement::~SVGFontFaceUriElement() |
| 54 | { |
| 55 | if (m_cachedFont) |
| 56 | m_cachedFont->removeClient(*this); |
| 57 | } |
| 58 | |
| 59 | Ref<CSSFontFaceSrcValue> SVGFontFaceUriElement::srcValue() const |
| 60 | { |
| 61 | auto src = CSSFontFaceSrcValue::create(getAttribute(SVGNames::hrefAttr, XLinkNames::hrefAttr), LoadedFromOpaqueSource::No); |
| 62 | AtomicString value(attributeWithoutSynchronization(formatAttr)); |
| 63 | src.get().setFormat(value.isEmpty() ? "svg" : value); // Default format |
| 64 | return src; |
| 65 | } |
| 66 | |
| 67 | void SVGFontFaceUriElement::parseAttribute(const QualifiedName& name, const AtomicString& value) |
| 68 | { |
| 69 | if (name == SVGNames::hrefAttr || name == XLinkNames::hrefAttr) |
| 70 | loadFont(); |
| 71 | else |
| 72 | SVGElement::parseAttribute(name, value); |
| 73 | } |
| 74 | |
| 75 | void SVGFontFaceUriElement::childrenChanged(const ChildChange& change) |
| 76 | { |
| 77 | SVGElement::childrenChanged(change); |
| 78 | |
| 79 | if (!parentNode() || !parentNode()->hasTagName(font_face_srcTag)) |
| 80 | return; |
| 81 | |
| 82 | auto grandparent = makeRefPtr(parentNode()->parentNode()); |
| 83 | if (grandparent && grandparent->hasTagName(font_faceTag)) |
| 84 | downcast<SVGFontFaceElement>(*grandparent).rebuildFontFace(); |
| 85 | } |
| 86 | |
| 87 | Node::InsertedIntoAncestorResult SVGFontFaceUriElement::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree) |
| 88 | { |
| 89 | loadFont(); |
| 90 | return SVGElement::insertedIntoAncestor(insertionType, parentOfInsertedTree); |
| 91 | } |
| 92 | |
| 93 | static bool isSVGFontTarget(const SVGFontFaceUriElement& element) |
| 94 | { |
| 95 | Ref<CSSFontFaceSrcValue> srcValue(element.srcValue()); |
| 96 | return srcValue->isSVGFontTarget(); |
| 97 | } |
| 98 | |
| 99 | void SVGFontFaceUriElement::loadFont() |
| 100 | { |
| 101 | if (m_cachedFont) |
| 102 | m_cachedFont->removeClient(*this); |
| 103 | |
| 104 | const AtomicString& href = getAttribute(SVGNames::hrefAttr, XLinkNames::hrefAttr); |
| 105 | if (!href.isNull()) { |
| 106 | ResourceLoaderOptions options = CachedResourceLoader::defaultCachedResourceOptions(); |
| 107 | options.contentSecurityPolicyImposition = isInUserAgentShadowTree() ? ContentSecurityPolicyImposition::SkipPolicyCheck : ContentSecurityPolicyImposition::DoPolicyCheck; |
| 108 | |
| 109 | CachedResourceLoader& cachedResourceLoader = document().cachedResourceLoader(); |
| 110 | CachedResourceRequest request(ResourceRequest(document().completeURL(href)), options); |
| 111 | request.setInitiator(*this); |
| 112 | m_cachedFont = cachedResourceLoader.requestFont(WTFMove(request), isSVGFontTarget(*this)).value_or(nullptr); |
| 113 | if (m_cachedFont) { |
| 114 | m_cachedFont->addClient(*this); |
| 115 | m_cachedFont->beginLoadIfNeeded(cachedResourceLoader); |
| 116 | } |
| 117 | } else |
| 118 | m_cachedFont = nullptr; |
| 119 | } |
| 120 | |
| 121 | } |
| 122 | |
| 123 | #endif // ENABLE(SVG_FONTS) |
| 124 | |