2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2006, 2007, 2012 Apple Inc. All rights reserved.
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.
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.
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.
22 #include "core/css/StyleSheetContents.h"
24 #include "core/css/CSSStyleSheet.h"
25 #include "core/css/MediaList.h"
26 #include "core/css/StylePropertySet.h"
27 #include "core/css/StyleRule.h"
28 #include "core/css/StyleRuleImport.h"
29 #include "core/css/parser/CSSParser.h"
30 #include "core/dom/Document.h"
31 #include "core/dom/Node.h"
32 #include "core/dom/StyleEngine.h"
33 #include "core/fetch/CSSStyleSheetResource.h"
34 #include "core/frame/UseCounter.h"
35 #include "platform/TraceEvent.h"
36 #include "platform/weborigin/SecurityOrigin.h"
37 #include "wtf/Deque.h"
41 // Rough size estimate for the memory cache.
42 unsigned StyleSheetContents::estimatedSizeInBytes() const
44 // Note that this does not take into account size of the strings hanging from various objects.
45 // The assumption is that nearly all of of them are atomic and would exist anyway.
46 unsigned size = sizeof(*this);
48 // FIXME: This ignores the children of media rules.
49 // Most rules are StyleRules.
50 size += ruleCount() * StyleRule::averageSizeInBytes();
52 for (unsigned i = 0; i < m_importRules.size(); ++i) {
53 if (StyleSheetContents* sheet = m_importRules[i]->styleSheet())
54 size += sheet->estimatedSizeInBytes();
59 StyleSheetContents::StyleSheetContents(StyleRuleImport* ownerRule, const String& originalURL, const CSSParserContext& context)
60 : m_ownerRule(ownerRule)
61 , m_originalURL(originalURL)
62 , m_hasSyntacticallyValidCSSHeader(true)
63 , m_didLoadErrorOccur(false)
64 , m_usesRemUnits(false)
66 , m_isInMemoryCache(false)
67 , m_hasFontFaceRule(false)
68 , m_hasMediaQueries(false)
69 , m_hasSingleOwnerDocument(true)
70 , m_parserContext(context)
74 StyleSheetContents::StyleSheetContents(const StyleSheetContents& o)
75 : m_ownerRule(nullptr)
76 , m_originalURL(o.m_originalURL)
77 , m_encodingFromCharsetRule(o.m_encodingFromCharsetRule)
78 , m_importRules(o.m_importRules.size())
79 , m_childRules(o.m_childRules.size())
80 , m_namespaces(o.m_namespaces)
81 , m_hasSyntacticallyValidCSSHeader(o.m_hasSyntacticallyValidCSSHeader)
82 , m_didLoadErrorOccur(false)
83 , m_usesRemUnits(o.m_usesRemUnits)
85 , m_isInMemoryCache(false)
86 , m_hasFontFaceRule(o.m_hasFontFaceRule)
87 , m_hasMediaQueries(o.m_hasMediaQueries)
88 , m_hasSingleOwnerDocument(true)
89 , m_parserContext(o.m_parserContext)
91 ASSERT(o.isCacheable());
93 // FIXME: Copy import rules.
94 ASSERT(o.m_importRules.isEmpty());
96 for (unsigned i = 0; i < m_childRules.size(); ++i)
97 m_childRules[i] = o.m_childRules[i]->copy();
100 StyleSheetContents::~StyleSheetContents()
107 void StyleSheetContents::setHasSyntacticallyValidCSSHeader(bool isValidCss)
110 if (Document* document = clientSingleOwnerDocument())
111 removeSheetFromCache(document);
113 m_hasSyntacticallyValidCSSHeader = isValidCss;
116 bool StyleSheetContents::isCacheable() const
118 // This would require dealing with multiple clients for load callbacks.
119 if (!loadCompleted())
121 // FIXME: StyleSheets with media queries can't be cached because their RuleSet
122 // is processed differently based off the media queries, which might resolve
123 // differently depending on the context of the parent CSSStyleSheet (e.g.
124 // if they are in differently sized iframes). Once RuleSets are media query
125 // agnostic, we can restore sharing of StyleSheetContents with medea queries.
126 if (m_hasMediaQueries)
128 // FIXME: Support copying import rules.
129 if (!m_importRules.isEmpty())
131 // FIXME: Support cached stylesheets in import rules.
134 if (m_didLoadErrorOccur)
136 // It is not the original sheet anymore.
139 // If the header is valid we are not going to need to check the SecurityOrigin.
140 // FIXME: Valid mime type avoids the check too.
141 if (!m_hasSyntacticallyValidCSSHeader)
146 void StyleSheetContents::parserAppendRule(PassRefPtrWillBeRawPtr<StyleRuleBase> rule)
148 ASSERT(!rule->isCharsetRule());
149 if (rule->isImportRule()) {
150 // Parser enforces that @import rules come before anything else except @charset.
151 ASSERT(m_childRules.isEmpty());
152 StyleRuleImport* importRule = toStyleRuleImport(rule.get());
153 if (importRule->mediaQueries())
154 setHasMediaQueries();
155 m_importRules.append(importRule);
156 m_importRules.last()->setParentStyleSheet(this);
157 m_importRules.last()->requestStyleSheet();
161 // Add warning message to inspector if dpi/dpcm values are used for screen media.
162 if (rule->isMediaRule()) {
163 setHasMediaQueries();
164 reportMediaQueryWarningIfNeeded(singleOwnerDocument(), toStyleRuleMedia(rule.get())->mediaQueries());
167 m_childRules.append(rule);
170 void StyleSheetContents::setHasMediaQueries()
172 m_hasMediaQueries = true;
173 if (parentStyleSheet())
174 parentStyleSheet()->setHasMediaQueries();
177 StyleRuleBase* StyleSheetContents::ruleAt(unsigned index) const
179 ASSERT_WITH_SECURITY_IMPLICATION(index < ruleCount());
181 unsigned childVectorIndex = index;
182 if (hasCharsetRule()) {
187 if (childVectorIndex < m_importRules.size())
188 return m_importRules[childVectorIndex].get();
190 childVectorIndex -= m_importRules.size();
191 return m_childRules[childVectorIndex].get();
194 unsigned StyleSheetContents::ruleCount() const
197 result += hasCharsetRule() ? 1 : 0;
198 result += m_importRules.size();
199 result += m_childRules.size();
203 void StyleSheetContents::clearCharsetRule()
205 m_encodingFromCharsetRule = String();
208 void StyleSheetContents::clearRules()
210 for (unsigned i = 0; i < m_importRules.size(); ++i) {
211 ASSERT(m_importRules.at(i)->parentStyleSheet() == this);
212 m_importRules[i]->clearParentStyleSheet();
214 m_importRules.clear();
215 m_childRules.clear();
219 void StyleSheetContents::parserSetEncodingFromCharsetRule(const String& encoding)
221 // Parser enforces that there is ever only one @charset.
222 ASSERT(m_encodingFromCharsetRule.isNull());
223 m_encodingFromCharsetRule = encoding;
226 bool StyleSheetContents::wrapperInsertRule(PassRefPtrWillBeRawPtr<StyleRuleBase> rule, unsigned index)
229 ASSERT_WITH_SECURITY_IMPLICATION(index <= ruleCount());
230 // Parser::parseRule doesn't currently allow @charset so we don't need to deal with it.
231 ASSERT(!rule->isCharsetRule());
233 unsigned childVectorIndex = index;
234 // m_childRules does not contain @charset which is always in index 0 if it exists.
235 if (hasCharsetRule()) {
236 if (childVectorIndex == 0) {
237 // Nothing can be inserted before @charset.
243 if (childVectorIndex < m_importRules.size() || (childVectorIndex == m_importRules.size() && rule->isImportRule())) {
244 // Inserting non-import rule before @import is not allowed.
245 if (!rule->isImportRule())
248 StyleRuleImport* importRule = toStyleRuleImport(rule.get());
249 if (importRule->mediaQueries())
250 setHasMediaQueries();
252 m_importRules.insert(childVectorIndex, importRule);
253 m_importRules[childVectorIndex]->setParentStyleSheet(this);
254 m_importRules[childVectorIndex]->requestStyleSheet();
255 // FIXME: Stylesheet doesn't actually change meaningfully before the imported sheets are loaded.
258 // Inserting @import rule after a non-import rule is not allowed.
259 if (rule->isImportRule())
262 if (rule->isMediaRule())
263 setHasMediaQueries();
265 childVectorIndex -= m_importRules.size();
267 if (rule->isFontFaceRule())
268 setHasFontFaceRule(true);
269 m_childRules.insert(childVectorIndex, rule);
273 void StyleSheetContents::wrapperDeleteRule(unsigned index)
276 ASSERT_WITH_SECURITY_IMPLICATION(index < ruleCount());
278 unsigned childVectorIndex = index;
279 if (hasCharsetRule()) {
280 if (childVectorIndex == 0) {
286 if (childVectorIndex < m_importRules.size()) {
287 m_importRules[childVectorIndex]->clearParentStyleSheet();
288 if (m_importRules[childVectorIndex]->isFontFaceRule())
289 notifyRemoveFontFaceRule(toStyleRuleFontFace(m_importRules[childVectorIndex].get()));
290 m_importRules.remove(childVectorIndex);
293 childVectorIndex -= m_importRules.size();
295 if (m_childRules[childVectorIndex]->isFontFaceRule())
296 notifyRemoveFontFaceRule(toStyleRuleFontFace(m_childRules[childVectorIndex].get()));
297 m_childRules.remove(childVectorIndex);
300 void StyleSheetContents::parserAddNamespace(const AtomicString& prefix, const AtomicString& uri)
302 if (uri.isNull() || prefix.isNull())
304 PrefixNamespaceURIMap::AddResult result = m_namespaces.add(prefix, uri);
305 if (result.isNewEntry)
307 result.storedValue->value = uri;
310 const AtomicString& StyleSheetContents::determineNamespace(const AtomicString& prefix)
313 return nullAtom; // No namespace. If an element/attribute has a namespace, we won't match it.
314 if (prefix == starAtom)
315 return starAtom; // We'll match any namespace.
316 return m_namespaces.get(prefix);
319 void StyleSheetContents::parseAuthorStyleSheet(const CSSStyleSheetResource* cachedStyleSheet, const SecurityOrigin* securityOrigin)
321 TRACE_EVENT0("blink", "StyleSheetContents::parseAuthorStyleSheet");
323 bool quirksMode = isQuirksModeBehavior(m_parserContext.mode());
325 bool enforceMIMEType = !quirksMode;
326 bool hasValidMIMEType = false;
327 String sheetText = cachedStyleSheet->sheetText(enforceMIMEType, &hasValidMIMEType);
329 CSSParserContext context(parserContext(), UseCounter::getFrom(this));
330 CSSParser::parseSheet(context, this, sheetText, TextPosition::minimumPosition(), 0, true);
332 // If we're loading a stylesheet cross-origin, and the MIME type is not standard, require the CSS
333 // to at least start with a syntactically valid CSS rule.
334 // This prevents an attacker playing games by injecting CSS strings into HTML, XML, JSON, etc. etc.
335 if (!hasValidMIMEType && !hasSyntacticallyValidCSSHeader()) {
336 bool isCrossOriginCSS = !securityOrigin || !securityOrigin->canRequest(baseURL());
337 if (isCrossOriginCSS) {
344 bool StyleSheetContents::parseString(const String& sheetText)
346 return parseStringAtPosition(sheetText, TextPosition::minimumPosition(), false);
349 bool StyleSheetContents::parseStringAtPosition(const String& sheetText, const TextPosition& startPosition, bool createdByParser)
351 CSSParserContext context(parserContext(), UseCounter::getFrom(this));
352 CSSParser::parseSheet(context, this, sheetText, startPosition, 0, createdByParser);
357 bool StyleSheetContents::isLoading() const
359 for (unsigned i = 0; i < m_importRules.size(); ++i) {
360 if (m_importRules[i]->isLoading())
366 bool StyleSheetContents::loadCompleted() const
368 StyleSheetContents* parentSheet = parentStyleSheet();
370 return parentSheet->loadCompleted();
372 StyleSheetContents* root = rootStyleSheet();
373 return root->m_loadingClients.isEmpty();
376 void StyleSheetContents::checkLoaded()
381 // Avoid |this| being deleted by scripts that run via
382 // ScriptableDocumentParser::executeScriptsWaitingForResources().
383 // See https://bugs.webkit.org/show_bug.cgi?id=95106
384 RefPtrWillBeRawPtr<StyleSheetContents> protect(this);
386 StyleSheetContents* parentSheet = parentStyleSheet();
388 parentSheet->checkLoaded();
392 ASSERT(this == rootStyleSheet());
393 if (m_loadingClients.isEmpty())
396 // Avoid |CSSSStyleSheet| and |ownerNode| being deleted by scripts that run via
397 // ScriptableDocumentParser::executeScriptsWaitingForResources(). Also protect
398 // the |CSSStyleSheet| from being deleted during iteration via the |sheetLoaded|
401 // When a sheet is loaded it is moved from the set of loading clients
402 // to the set of completed clients. We therefore need the copy in order to
403 // not modify the set while iterating it.
404 WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> > loadingClients;
405 copyToVector(m_loadingClients, loadingClients);
407 for (unsigned i = 0; i < loadingClients.size(); ++i) {
408 if (loadingClients[i]->loadCompleted())
411 // sheetLoaded might be invoked after its owner node is removed from document.
412 if (RefPtrWillBeRawPtr<Node> ownerNode = loadingClients[i]->ownerNode()) {
413 if (loadingClients[i]->sheetLoaded())
414 ownerNode->notifyLoadedSheetAndAllCriticalSubresources(m_didLoadErrorOccur);
419 void StyleSheetContents::notifyLoadedSheet(const CSSStyleSheetResource* sheet)
422 m_didLoadErrorOccur |= sheet->errorOccurred();
423 // updateLayoutIgnorePendingStyleSheets can cause us to create the RuleSet on this
424 // sheet before its imports have loaded. So clear the RuleSet when the imports
425 // load since the import's subrules are flattened into its parent sheet's RuleSet.
429 void StyleSheetContents::startLoadingDynamicSheet()
431 StyleSheetContents* root = rootStyleSheet();
432 for (const auto& client : root->m_loadingClients)
433 client->startLoadingDynamicSheet();
434 // Copy the completed clients to a vector for iteration.
435 // startLoadingDynamicSheet will move the style sheet from the
436 // completed state to the loading state which modifies the set of
437 // completed clients. We therefore need the copy in order to not
438 // modify the set of completed clients while iterating it.
439 WillBeHeapVector<RawPtrWillBeMember<CSSStyleSheet> > completedClients;
440 copyToVector(root->m_completedClients, completedClients);
441 for (unsigned i = 0; i < completedClients.size(); ++i)
442 completedClients[i]->startLoadingDynamicSheet();
445 StyleSheetContents* StyleSheetContents::rootStyleSheet() const
447 const StyleSheetContents* root = this;
448 while (root->parentStyleSheet())
449 root = root->parentStyleSheet();
450 return const_cast<StyleSheetContents*>(root);
453 bool StyleSheetContents::hasSingleOwnerNode() const
455 return rootStyleSheet()->hasOneClient();
458 Node* StyleSheetContents::singleOwnerNode() const
460 StyleSheetContents* root = rootStyleSheet();
461 if (!root->hasOneClient())
463 if (root->m_loadingClients.size())
464 return (*root->m_loadingClients.begin())->ownerNode();
465 return (*root->m_completedClients.begin())->ownerNode();
468 Document* StyleSheetContents::singleOwnerDocument() const
470 StyleSheetContents* root = rootStyleSheet();
471 return root->clientSingleOwnerDocument();
474 KURL StyleSheetContents::completeURL(const String& url) const
476 // FIXME: This is only OK when we have a singleOwnerNode, right?
477 return m_parserContext.completeURL(url);
480 static bool childRulesHaveFailedOrCanceledSubresources(const WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& rules)
482 for (unsigned i = 0; i < rules.size(); ++i) {
483 const StyleRuleBase* rule = rules[i].get();
484 switch (rule->type()) {
485 case StyleRuleBase::Style:
486 if (toStyleRule(rule)->properties().hasFailedOrCanceledSubresources())
489 case StyleRuleBase::FontFace:
490 if (toStyleRuleFontFace(rule)->properties().hasFailedOrCanceledSubresources())
493 case StyleRuleBase::Media:
494 if (childRulesHaveFailedOrCanceledSubresources(toStyleRuleMedia(rule)->childRules()))
497 case StyleRuleBase::Import:
498 ASSERT_NOT_REACHED();
499 case StyleRuleBase::Page:
500 case StyleRuleBase::Keyframes:
501 case StyleRuleBase::Unknown:
502 case StyleRuleBase::Charset:
503 case StyleRuleBase::Keyframe:
504 case StyleRuleBase::Supports:
505 case StyleRuleBase::Viewport:
506 case StyleRuleBase::Filter:
513 bool StyleSheetContents::hasFailedOrCanceledSubresources() const
515 ASSERT(isCacheable());
516 return childRulesHaveFailedOrCanceledSubresources(m_childRules);
519 Document* StyleSheetContents::clientSingleOwnerDocument() const
521 if (!m_hasSingleOwnerDocument || clientSize() <= 0)
524 if (m_loadingClients.size())
525 return (*m_loadingClients.begin())->ownerDocument();
526 return (*m_completedClients.begin())->ownerDocument();
529 StyleSheetContents* StyleSheetContents::parentStyleSheet() const
531 return m_ownerRule ? m_ownerRule->parentStyleSheet() : 0;
534 void StyleSheetContents::registerClient(CSSStyleSheet* sheet)
536 ASSERT(!m_loadingClients.contains(sheet) && !m_completedClients.contains(sheet));
538 // InspectorCSSAgent::buildObjectForRule creates CSSStyleSheet without any owner node.
539 if (!sheet->ownerDocument())
542 if (Document* document = clientSingleOwnerDocument()) {
543 if (sheet->ownerDocument() != document)
544 m_hasSingleOwnerDocument = false;
546 m_loadingClients.add(sheet);
549 void StyleSheetContents::unregisterClient(CSSStyleSheet* sheet)
551 m_loadingClients.remove(sheet);
552 m_completedClients.remove(sheet);
554 if (!sheet->ownerDocument() || !m_loadingClients.isEmpty() || !m_completedClients.isEmpty())
557 if (m_hasSingleOwnerDocument)
558 removeSheetFromCache(sheet->ownerDocument());
559 m_hasSingleOwnerDocument = true;
562 void StyleSheetContents::clientLoadCompleted(CSSStyleSheet* sheet)
564 ASSERT(m_loadingClients.contains(sheet) || !sheet->ownerDocument());
565 m_loadingClients.remove(sheet);
566 // In m_ownerNode->sheetLoaded, the CSSStyleSheet might be detached.
567 // (i.e. clearOwnerNode was invoked.)
568 // In this case, we don't need to add the stylesheet to completed clients.
569 if (!sheet->ownerDocument())
571 m_completedClients.add(sheet);
574 void StyleSheetContents::clientLoadStarted(CSSStyleSheet* sheet)
576 ASSERT(m_completedClients.contains(sheet));
577 m_completedClients.remove(sheet);
578 m_loadingClients.add(sheet);
581 void StyleSheetContents::removeSheetFromCache(Document* document)
584 document->styleEngine()->removeSheet(this);
587 void StyleSheetContents::addedToMemoryCache()
589 ASSERT(!m_isInMemoryCache);
590 ASSERT(isCacheable());
591 m_isInMemoryCache = true;
594 void StyleSheetContents::removedFromMemoryCache()
596 ASSERT(m_isInMemoryCache);
597 ASSERT(isCacheable());
598 m_isInMemoryCache = false;
601 void StyleSheetContents::shrinkToFit()
603 m_importRules.shrinkToFit();
604 m_childRules.shrinkToFit();
607 RuleSet& StyleSheetContents::ensureRuleSet(const MediaQueryEvaluator& medium, AddRuleFlags addRuleFlags)
610 m_ruleSet = RuleSet::create();
611 m_ruleSet->addRulesFromSheet(this, medium, addRuleFlags);
613 return *m_ruleSet.get();
616 static void clearResolvers(WillBeHeapHashSet<RawPtrWillBeWeakMember<CSSStyleSheet> >& clients)
618 for (const auto& sheet : clients) {
619 if (Document* document = sheet->ownerDocument())
620 document->styleEngine()->clearResolver();
624 void StyleSheetContents::clearRuleSet()
626 if (StyleSheetContents* parentSheet = parentStyleSheet())
627 parentSheet->clearRuleSet();
629 // Don't want to clear the StyleResolver if the RuleSet hasn't been created
630 // since we only clear the StyleResolver so that it's members are properly
631 // updated in ScopedStyleResolver::addRulesFromSheet.
635 // Clearing the ruleSet means we need to recreate the styleResolver data structures.
636 // See the StyleResolver calls in ScopedStyleResolver::addRulesFromSheet.
637 clearResolvers(m_loadingClients);
638 clearResolvers(m_completedClients);
642 static void removeFontFaceRules(WillBeHeapHashSet<RawPtrWillBeWeakMember<CSSStyleSheet> >& clients, const StyleRuleFontFace* fontFaceRule)
644 for (const auto& sheet : clients) {
645 if (Node* ownerNode = sheet->ownerNode())
646 ownerNode->document().styleEngine()->removeFontFaceRules(WillBeHeapVector<RawPtrWillBeMember<const StyleRuleFontFace> >(1, fontFaceRule));
650 void StyleSheetContents::notifyRemoveFontFaceRule(const StyleRuleFontFace* fontFaceRule)
652 StyleSheetContents* root = rootStyleSheet();
653 removeFontFaceRules(root->m_loadingClients, fontFaceRule);
654 removeFontFaceRules(root->m_completedClients, fontFaceRule);
657 static void findFontFaceRulesFromRules(const WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& rules, WillBeHeapVector<RawPtrWillBeMember<const StyleRuleFontFace> >& fontFaceRules)
659 for (unsigned i = 0; i < rules.size(); ++i) {
660 StyleRuleBase* rule = rules[i].get();
662 if (rule->isFontFaceRule()) {
663 fontFaceRules.append(toStyleRuleFontFace(rule));
664 } else if (rule->isMediaRule()) {
665 StyleRuleMedia* mediaRule = toStyleRuleMedia(rule);
666 // We cannot know whether the media rule matches or not, but
667 // for safety, remove @font-face in the media rule (if exists).
668 findFontFaceRulesFromRules(mediaRule->childRules(), fontFaceRules);
673 void StyleSheetContents::findFontFaceRules(WillBeHeapVector<RawPtrWillBeMember<const StyleRuleFontFace> >& fontFaceRules)
675 for (unsigned i = 0; i < m_importRules.size(); ++i) {
676 if (!m_importRules[i]->styleSheet())
678 m_importRules[i]->styleSheet()->findFontFaceRules(fontFaceRules);
681 findFontFaceRulesFromRules(childRules(), fontFaceRules);
684 void StyleSheetContents::trace(Visitor* visitor)
687 visitor->trace(m_ownerRule);
688 visitor->trace(m_importRules);
689 visitor->trace(m_childRules);
690 visitor->trace(m_loadingClients);
691 visitor->trace(m_completedClients);
692 visitor->trace(m_ruleSet);