2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * (C) 2002-2003 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2002, 2005, 2006, 2008, 2009, 2010, 2012 Apple Inc. All rights reserved.
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.
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.
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.
23 #include "StyleRuleImport.h"
25 #include "CSSStyleSheet.h"
26 #include "CachedCSSStyleSheet.h"
27 #include "CachedResourceLoader.h"
28 #include "CachedResourceRequest.h"
29 #include "CachedResourceRequestInitiators.h"
31 #include "MediaList.h"
32 #include "SecurityOrigin.h"
33 #include "StyleSheetContents.h"
34 #include <wtf/StdLibExtras.h>
38 Ref<StyleRuleImport> StyleRuleImport::create(const String& href, Ref<MediaQuerySet>&& media)
40 return adoptRef(*new StyleRuleImport(href, WTFMove(media)));
43 StyleRuleImport::StyleRuleImport(const String& href, Ref<MediaQuerySet>&& media)
44 : StyleRuleBase(Import, 0)
45 , m_parentStyleSheet(0)
46 , m_styleSheetClient(this)
48 , m_mediaQueries(WTFMove(media))
53 m_mediaQueries = MediaQuerySet::create(String());
56 StyleRuleImport::~StyleRuleImport()
59 m_styleSheet->clearOwnerRule();
61 m_cachedSheet->removeClient(m_styleSheetClient);
64 void StyleRuleImport::setCSSStyleSheet(const String& href, const URL& baseURL, const String& charset, const CachedCSSStyleSheet* cachedStyleSheet)
67 m_styleSheet->clearOwnerRule();
69 CSSParserContext context = m_parentStyleSheet ? m_parentStyleSheet->parserContext() : HTMLStandardMode;
70 context.charset = charset;
71 if (!baseURL.isNull())
72 context.baseURL = baseURL;
74 Document* document = m_parentStyleSheet ? m_parentStyleSheet->singleOwnerDocument() : nullptr;
75 m_styleSheet = StyleSheetContents::create(this, href, context);
76 m_styleSheet->parseAuthorStyleSheet(cachedStyleSheet, document ? document->securityOrigin() : nullptr);
80 if (m_parentStyleSheet) {
81 m_parentStyleSheet->notifyLoadedSheet(cachedStyleSheet);
82 m_parentStyleSheet->checkLoaded();
86 bool StyleRuleImport::isLoading() const
88 return m_loading || (m_styleSheet && m_styleSheet->isLoading());
91 void StyleRuleImport::requestStyleSheet()
93 if (!m_parentStyleSheet)
95 Document* document = m_parentStyleSheet->singleOwnerDocument();
100 if (!m_parentStyleSheet->baseURL().isNull())
101 // use parent styleheet's URL as the base URL
102 absURL = URL(m_parentStyleSheet->baseURL(), m_strHref);
104 absURL = document->completeURL(m_strHref);
106 // Check for a cycle in our import chain. If we encounter a stylesheet
107 // in our parent chain with the same URL, then just bail.
108 StyleSheetContents* rootSheet = m_parentStyleSheet;
109 for (StyleSheetContents* sheet = m_parentStyleSheet; sheet; sheet = sheet->parentStyleSheet()) {
110 if (equalIgnoringFragmentIdentifier(absURL, sheet->baseURL())
111 || equalIgnoringFragmentIdentifier(absURL, document->completeURL(sheet->originalURL())))
116 // FIXME: Skip Content Security Policy check when stylesheet is in a user agent shadow tree.
117 // See <https://bugs.webkit.org/show_bug.cgi?id=146663>.
118 CachedResourceRequest request(ResourceRequest(absURL), m_parentStyleSheet->charset());
119 request.setInitiator(cachedResourceRequestInitiators().css);
121 m_cachedSheet->removeClient(m_styleSheetClient);
122 if (m_parentStyleSheet->isUserStyleSheet()) {
123 request.setOptions(ResourceLoaderOptions(DoNotSendCallbacks, SniffContent, BufferData, AllowStoredCredentials, ClientCredentialPolicy::MayAskClientForCredentials, FetchOptions::Credentials::Include, SkipSecurityCheck, FetchOptions::Mode::NoCors, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::SkipPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::AllowCaching));
124 m_cachedSheet = document->cachedResourceLoader().requestUserCSSStyleSheet(WTFMove(request));
126 m_cachedSheet = document->cachedResourceLoader().requestCSSStyleSheet(WTFMove(request));
128 // if the import rule is issued dynamically, the sheet may be
129 // removed from the pending sheet count, so let the doc know
130 // the sheet being imported is pending.
131 if (m_parentStyleSheet && m_parentStyleSheet->loadCompleted() && rootSheet == m_parentStyleSheet)
132 m_parentStyleSheet->startLoadingDynamicSheet();
134 m_cachedSheet->addClient(m_styleSheetClient);
138 } // namespace WebCore