| 1 | /* |
| 2 | * This file is part of the XSL implementation. |
| 3 | * |
| 4 | * Copyright (C) 2004-2017 Apple Inc. All rights reserved. |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Library General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Library General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Library General Public License |
| 17 | * along with this library; see the file COPYING.LIB. If not, write to |
| 18 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 | * Boston, MA 02110-1301, USA. |
| 20 | */ |
| 21 | |
| 22 | #include "config.h" |
| 23 | #include "XSLImportRule.h" |
| 24 | |
| 25 | #if ENABLE(XSLT) |
| 26 | |
| 27 | #include "CachedXSLStyleSheet.h" |
| 28 | #include "CachedResourceLoader.h" |
| 29 | #include "CachedResourceRequest.h" |
| 30 | #include "Document.h" |
| 31 | |
| 32 | namespace WebCore { |
| 33 | |
| 34 | XSLImportRule::XSLImportRule(XSLStyleSheet* parent, const String& href) |
| 35 | : m_parentStyleSheet(parent) |
| 36 | , m_strHref(href) |
| 37 | , m_cachedSheet(nullptr) |
| 38 | , m_loading(false) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | XSLImportRule::~XSLImportRule() |
| 43 | { |
| 44 | if (m_styleSheet) |
| 45 | m_styleSheet->setParentStyleSheet(nullptr); |
| 46 | |
| 47 | if (m_cachedSheet) |
| 48 | m_cachedSheet->removeClient(*this); |
| 49 | } |
| 50 | |
| 51 | void XSLImportRule::setXSLStyleSheet(const String& href, const URL& baseURL, const String& sheet) |
| 52 | { |
| 53 | if (m_styleSheet) |
| 54 | m_styleSheet->setParentStyleSheet(nullptr); |
| 55 | |
| 56 | m_styleSheet = XSLStyleSheet::create(this, href, baseURL); |
| 57 | |
| 58 | XSLStyleSheet* parent = parentStyleSheet(); |
| 59 | if (parent) |
| 60 | m_styleSheet->setParentStyleSheet(parent); |
| 61 | |
| 62 | m_styleSheet->parseString(sheet); |
| 63 | m_loading = false; |
| 64 | |
| 65 | if (parent) |
| 66 | parent->checkLoaded(); |
| 67 | } |
| 68 | |
| 69 | bool XSLImportRule::isLoading() |
| 70 | { |
| 71 | return (m_loading || (m_styleSheet && m_styleSheet->isLoading())); |
| 72 | } |
| 73 | |
| 74 | void XSLImportRule::loadSheet() |
| 75 | { |
| 76 | CachedResourceLoader* cachedResourceLoader = nullptr; |
| 77 | |
| 78 | XSLStyleSheet* rootSheet = parentStyleSheet(); |
| 79 | |
| 80 | if (rootSheet) { |
| 81 | while (XSLStyleSheet* parentSheet = rootSheet->parentStyleSheet()) |
| 82 | rootSheet = parentSheet; |
| 83 | } |
| 84 | |
| 85 | if (rootSheet) |
| 86 | cachedResourceLoader = rootSheet->cachedResourceLoader(); |
| 87 | |
| 88 | String absHref = m_strHref; |
| 89 | XSLStyleSheet* parentSheet = parentStyleSheet(); |
| 90 | if (!parentSheet->baseURL().isNull()) |
| 91 | // use parent styleheet's URL as the base URL |
| 92 | absHref = URL(parentSheet->baseURL(), m_strHref).string(); |
| 93 | |
| 94 | // Check for a cycle in our import chain. If we encounter a stylesheet |
| 95 | // in our parent chain with the same URL, then just bail. |
| 96 | for (XSLStyleSheet* parentSheet = parentStyleSheet(); parentSheet; parentSheet = parentSheet->parentStyleSheet()) { |
| 97 | if (absHref == parentSheet->baseURL().string()) |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | if (m_cachedSheet) |
| 102 | m_cachedSheet->removeClient(*this); |
| 103 | |
| 104 | auto options = CachedResourceLoader::defaultCachedResourceOptions(); |
| 105 | options.mode = FetchOptions::Mode::SameOrigin; |
| 106 | m_cachedSheet = cachedResourceLoader->requestXSLStyleSheet({ResourceRequest(cachedResourceLoader->document()->completeURL(absHref)), options}).value_or(nullptr); |
| 107 | |
| 108 | if (m_cachedSheet) { |
| 109 | m_cachedSheet->addClient(*this); |
| 110 | |
| 111 | // If the imported sheet is in the cache, then setXSLStyleSheet gets called, |
| 112 | // and the sheet even gets parsed (via parseString). In this case we have |
| 113 | // loaded (even if our subresources haven't), so if we have a stylesheet after |
| 114 | // checking the cache, then we've clearly loaded. |
| 115 | if (!m_styleSheet) |
| 116 | m_loading = true; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | } // namespace WebCore |
| 121 | |
| 122 | #endif // ENABLE(XSLT) |
| 123 | |