This source file includes following definitions.
- estimatedSizeInBytes
- m_parserContext
- m_parserContext
- setHasSyntacticallyValidCSSHeader
- isCacheable
- parserAppendRule
- setHasMediaQueries
- ruleAt
- ruleCount
- clearCharsetRule
- clearRules
- parserSetEncodingFromCharsetRule
- wrapperInsertRule
- wrapperDeleteRule
- parserAddNamespace
- determineNamespace
- parseAuthorStyleSheet
- parseString
- parseStringAtPosition
- isLoading
- loadCompleted
- checkLoaded
- notifyLoadedSheet
- startLoadingDynamicSheet
- rootStyleSheet
- hasSingleOwnerNode
- singleOwnerNode
- singleOwnerDocument
- completeURL
- childRulesHaveFailedOrCanceledSubresources
- hasFailedOrCanceledSubresources
- clientSingleOwnerDocument
- parentStyleSheet
- registerClient
- unregisterClient
- clientLoadCompleted
- clientLoadStarted
- removeSheetFromCache
- addedToMemoryCache
- removedFromMemoryCache
- shrinkToFit
- ensureRuleSet
- clearResolvers
- clearRuleSet
- removeFontFaceRules
- notifyRemoveFontFaceRule
- findFontFaceRulesFromRules
- findFontFaceRules
- trace
#include "config.h"
#include "core/css/StyleSheetContents.h"
#include "core/css/parser/BisonCSSParser.h"
#include "core/css/CSSStyleSheet.h"
#include "core/css/MediaList.h"
#include "core/css/StylePropertySet.h"
#include "core/css/StyleRule.h"
#include "core/css/StyleRuleImport.h"
#include "core/dom/Document.h"
#include "core/dom/Node.h"
#include "core/dom/StyleEngine.h"
#include "core/fetch/CSSStyleSheetResource.h"
#include "core/frame/UseCounter.h"
#include "platform/TraceEvent.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "wtf/Deque.h"
namespace WebCore {
unsigned StyleSheetContents::estimatedSizeInBytes() const
{
unsigned size = sizeof(*this);
size += ruleCount() * StyleRule::averageSizeInBytes();
for (unsigned i = 0; i < m_importRules.size(); ++i) {
if (StyleSheetContents* sheet = m_importRules[i]->styleSheet())
size += sheet->estimatedSizeInBytes();
}
return size;
}
StyleSheetContents::StyleSheetContents(StyleRuleImport* ownerRule, const String& originalURL, const CSSParserContext& context)
: m_ownerRule(ownerRule)
, m_originalURL(originalURL)
, m_hasSyntacticallyValidCSSHeader(true)
, m_didLoadErrorOccur(false)
, m_usesRemUnits(false)
, m_isMutable(false)
, m_isInMemoryCache(false)
, m_hasFontFaceRule(false)
, m_hasMediaQueries(false)
, m_hasSingleOwnerDocument(true)
, m_parserContext(context)
{
}
StyleSheetContents::StyleSheetContents(const StyleSheetContents& o)
: m_ownerRule(nullptr)
, m_originalURL(o.m_originalURL)
, m_encodingFromCharsetRule(o.m_encodingFromCharsetRule)
, m_importRules(o.m_importRules.size())
, m_childRules(o.m_childRules.size())
, m_namespaces(o.m_namespaces)
, m_hasSyntacticallyValidCSSHeader(o.m_hasSyntacticallyValidCSSHeader)
, m_didLoadErrorOccur(false)
, m_usesRemUnits(o.m_usesRemUnits)
, m_isMutable(false)
, m_isInMemoryCache(false)
, m_hasFontFaceRule(o.m_hasFontFaceRule)
, m_hasMediaQueries(o.m_hasMediaQueries)
, m_hasSingleOwnerDocument(true)
, m_parserContext(o.m_parserContext)
{
ASSERT(o.isCacheable());
ASSERT(o.m_importRules.isEmpty());
for (unsigned i = 0; i < m_childRules.size(); ++i)
m_childRules[i] = o.m_childRules[i]->copy();
}
StyleSheetContents::~StyleSheetContents()
{
#if !ENABLE(OILPAN)
clearRules();
#endif
}
void StyleSheetContents::setHasSyntacticallyValidCSSHeader(bool isValidCss)
{
if (!isValidCss) {
if (Document* document = clientSingleOwnerDocument())
removeSheetFromCache(document);
}
m_hasSyntacticallyValidCSSHeader = isValidCss;
}
bool StyleSheetContents::isCacheable() const
{
if (!loadCompleted())
return false;
if (m_hasMediaQueries)
return false;
if (!m_importRules.isEmpty())
return false;
if (m_ownerRule)
return false;
if (m_didLoadErrorOccur)
return false;
if (m_isMutable)
return false;
if (!m_hasSyntacticallyValidCSSHeader)
return false;
return true;
}
void StyleSheetContents::parserAppendRule(PassRefPtrWillBeRawPtr<StyleRuleBase> rule)
{
ASSERT(!rule->isCharsetRule());
if (rule->isImportRule()) {
ASSERT(m_childRules.isEmpty());
StyleRuleImport* importRule = toStyleRuleImport(rule.get());
if (importRule->mediaQueries())
setHasMediaQueries();
m_importRules.append(importRule);
m_importRules.last()->setParentStyleSheet(this);
m_importRules.last()->requestStyleSheet();
return;
}
if (rule->isMediaRule()) {
setHasMediaQueries();
reportMediaQueryWarningIfNeeded(singleOwnerDocument(), toStyleRuleMedia(rule.get())->mediaQueries());
}
m_childRules.append(rule);
}
void StyleSheetContents::setHasMediaQueries()
{
m_hasMediaQueries = true;
if (parentStyleSheet())
parentStyleSheet()->setHasMediaQueries();
}
StyleRuleBase* StyleSheetContents::ruleAt(unsigned index) const
{
ASSERT_WITH_SECURITY_IMPLICATION(index < ruleCount());
unsigned childVectorIndex = index;
if (hasCharsetRule()) {
if (index == 0)
return 0;
--childVectorIndex;
}
if (childVectorIndex < m_importRules.size())
return m_importRules[childVectorIndex].get();
childVectorIndex -= m_importRules.size();
return m_childRules[childVectorIndex].get();
}
unsigned StyleSheetContents::ruleCount() const
{
unsigned result = 0;
result += hasCharsetRule() ? 1 : 0;
result += m_importRules.size();
result += m_childRules.size();
return result;
}
void StyleSheetContents::clearCharsetRule()
{
m_encodingFromCharsetRule = String();
}
void StyleSheetContents::clearRules()
{
for (unsigned i = 0; i < m_importRules.size(); ++i) {
ASSERT(m_importRules.at(i)->parentStyleSheet() == this);
m_importRules[i]->clearParentStyleSheet();
}
m_importRules.clear();
m_childRules.clear();
clearCharsetRule();
}
void StyleSheetContents::parserSetEncodingFromCharsetRule(const String& encoding)
{
ASSERT(m_encodingFromCharsetRule.isNull());
m_encodingFromCharsetRule = encoding;
}
bool StyleSheetContents::wrapperInsertRule(PassRefPtrWillBeRawPtr<StyleRuleBase> rule, unsigned index)
{
ASSERT(m_isMutable);
ASSERT_WITH_SECURITY_IMPLICATION(index <= ruleCount());
ASSERT(!rule->isCharsetRule());
unsigned childVectorIndex = index;
if (hasCharsetRule()) {
if (childVectorIndex == 0) {
return false;
}
--childVectorIndex;
}
if (childVectorIndex < m_importRules.size() || (childVectorIndex == m_importRules.size() && rule->isImportRule())) {
if (!rule->isImportRule())
return false;
StyleRuleImport* importRule = toStyleRuleImport(rule.get());
if (importRule->mediaQueries())
setHasMediaQueries();
m_importRules.insert(childVectorIndex, importRule);
m_importRules[childVectorIndex]->setParentStyleSheet(this);
m_importRules[childVectorIndex]->requestStyleSheet();
return true;
}
if (rule->isImportRule())
return false;
if (rule->isMediaRule())
setHasMediaQueries();
childVectorIndex -= m_importRules.size();
if (rule->isFontFaceRule())
setHasFontFaceRule(true);
m_childRules.insert(childVectorIndex, rule);
return true;
}
void StyleSheetContents::wrapperDeleteRule(unsigned index)
{
ASSERT(m_isMutable);
ASSERT_WITH_SECURITY_IMPLICATION(index < ruleCount());
unsigned childVectorIndex = index;
if (hasCharsetRule()) {
if (childVectorIndex == 0) {
clearCharsetRule();
return;
}
--childVectorIndex;
}
if (childVectorIndex < m_importRules.size()) {
m_importRules[childVectorIndex]->clearParentStyleSheet();
if (m_importRules[childVectorIndex]->isFontFaceRule())
notifyRemoveFontFaceRule(toStyleRuleFontFace(m_importRules[childVectorIndex].get()));
m_importRules.remove(childVectorIndex);
return;
}
childVectorIndex -= m_importRules.size();
if (m_childRules[childVectorIndex]->isFontFaceRule())
notifyRemoveFontFaceRule(toStyleRuleFontFace(m_childRules[childVectorIndex].get()));
m_childRules.remove(childVectorIndex);
}
void StyleSheetContents::parserAddNamespace(const AtomicString& prefix, const AtomicString& uri)
{
if (uri.isNull() || prefix.isNull())
return;
PrefixNamespaceURIMap::AddResult result = m_namespaces.add(prefix, uri);
if (result.isNewEntry)
return;
result.storedValue->value = uri;
}
const AtomicString& StyleSheetContents::determineNamespace(const AtomicString& prefix)
{
if (prefix.isNull())
return nullAtom;
if (prefix == starAtom)
return starAtom;
return m_namespaces.get(prefix);
}
void StyleSheetContents::parseAuthorStyleSheet(const CSSStyleSheetResource* cachedStyleSheet, const SecurityOrigin* securityOrigin)
{
TRACE_EVENT0("webkit", "StyleSheetContents::parseAuthorStyleSheet");
bool quirksMode = isQuirksModeBehavior(m_parserContext.mode());
bool enforceMIMEType = !quirksMode;
bool hasValidMIMEType = false;
String sheetText = cachedStyleSheet->sheetText(enforceMIMEType, &hasValidMIMEType);
CSSParserContext context(parserContext(), UseCounter::getFrom(this));
BisonCSSParser p(context);
p.parseSheet(this, sheetText, TextPosition::minimumPosition(), 0, true);
if (!hasValidMIMEType && !hasSyntacticallyValidCSSHeader()) {
bool isCrossOriginCSS = !securityOrigin || !securityOrigin->canRequest(baseURL());
if (isCrossOriginCSS) {
clearRules();
return;
}
}
}
bool StyleSheetContents::parseString(const String& sheetText)
{
return parseStringAtPosition(sheetText, TextPosition::minimumPosition(), false);
}
bool StyleSheetContents::parseStringAtPosition(const String& sheetText, const TextPosition& startPosition, bool createdByParser)
{
CSSParserContext context(parserContext(), UseCounter::getFrom(this));
BisonCSSParser p(context);
p.parseSheet(this, sheetText, startPosition, 0, createdByParser);
return true;
}
bool StyleSheetContents::isLoading() const
{
for (unsigned i = 0; i < m_importRules.size(); ++i) {
if (m_importRules[i]->isLoading())
return true;
}
return false;
}
bool StyleSheetContents::loadCompleted() const
{
StyleSheetContents* parentSheet = parentStyleSheet();
if (parentSheet)
return parentSheet->loadCompleted();
StyleSheetContents* root = rootStyleSheet();
return root->m_loadingClients.isEmpty();
}
void StyleSheetContents::checkLoaded()
{
if (isLoading())
return;
RefPtrWillBeRawPtr<StyleSheetContents> protect(this);
StyleSheetContents* parentSheet = parentStyleSheet();
if (parentSheet) {
parentSheet->checkLoaded();
return;
}
StyleSheetContents* root = rootStyleSheet();
if (root->m_loadingClients.isEmpty())
return;
WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> > loadingClients;
copyToVector(m_loadingClients, loadingClients);
for (unsigned i = 0; i < loadingClients.size(); ++i) {
if (loadingClients[i]->loadCompleted())
continue;
if (RefPtr<Node> ownerNode = loadingClients[i]->ownerNode()) {
if (loadingClients[i]->sheetLoaded())
ownerNode->notifyLoadedSheetAndAllCriticalSubresources(m_didLoadErrorOccur);
}
}
}
void StyleSheetContents::notifyLoadedSheet(const CSSStyleSheetResource* sheet)
{
ASSERT(sheet);
m_didLoadErrorOccur |= sheet->errorOccurred();
clearRuleSet();
}
void StyleSheetContents::startLoadingDynamicSheet()
{
StyleSheetContents* root = rootStyleSheet();
for (ClientsIterator it = root->m_loadingClients.begin(); it != root->m_loadingClients.end(); ++it)
(*it)->startLoadingDynamicSheet();
WillBeHeapVector<RawPtrWillBeMember<CSSStyleSheet> > completedClients;
copyToVector(root->m_completedClients, completedClients);
for (unsigned i = 0; i < completedClients.size(); ++i)
completedClients[i]->startLoadingDynamicSheet();
}
StyleSheetContents* StyleSheetContents::rootStyleSheet() const
{
const StyleSheetContents* root = this;
while (root->parentStyleSheet())
root = root->parentStyleSheet();
return const_cast<StyleSheetContents*>(root);
}
bool StyleSheetContents::hasSingleOwnerNode() const
{
return rootStyleSheet()->hasOneClient();
}
Node* StyleSheetContents::singleOwnerNode() const
{
StyleSheetContents* root = rootStyleSheet();
if (!root->hasOneClient())
return 0;
if (root->m_loadingClients.size())
return (*root->m_loadingClients.begin())->ownerNode();
return (*root->m_completedClients.begin())->ownerNode();
}
Document* StyleSheetContents::singleOwnerDocument() const
{
StyleSheetContents* root = rootStyleSheet();
return root->clientSingleOwnerDocument();
}
KURL StyleSheetContents::completeURL(const String& url) const
{
return m_parserContext.completeURL(url);
}
static bool childRulesHaveFailedOrCanceledSubresources(const WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& rules)
{
for (unsigned i = 0; i < rules.size(); ++i) {
const StyleRuleBase* rule = rules[i].get();
switch (rule->type()) {
case StyleRuleBase::Style:
if (toStyleRule(rule)->properties().hasFailedOrCanceledSubresources())
return true;
break;
case StyleRuleBase::FontFace:
if (toStyleRuleFontFace(rule)->properties().hasFailedOrCanceledSubresources())
return true;
break;
case StyleRuleBase::Media:
if (childRulesHaveFailedOrCanceledSubresources(toStyleRuleMedia(rule)->childRules()))
return true;
break;
case StyleRuleBase::Import:
ASSERT_NOT_REACHED();
case StyleRuleBase::Page:
case StyleRuleBase::Keyframes:
case StyleRuleBase::Unknown:
case StyleRuleBase::Charset:
case StyleRuleBase::Keyframe:
case StyleRuleBase::Supports:
case StyleRuleBase::Viewport:
case StyleRuleBase::Filter:
break;
}
}
return false;
}
bool StyleSheetContents::hasFailedOrCanceledSubresources() const
{
ASSERT(isCacheable());
return childRulesHaveFailedOrCanceledSubresources(m_childRules);
}
Document* StyleSheetContents::clientSingleOwnerDocument() const
{
if (!m_hasSingleOwnerDocument || clientSize() <= 0)
return 0;
if (m_loadingClients.size())
return (*m_loadingClients.begin())->ownerDocument();
return (*m_completedClients.begin())->ownerDocument();
}
StyleSheetContents* StyleSheetContents::parentStyleSheet() const
{
return m_ownerRule ? m_ownerRule->parentStyleSheet() : 0;
}
void StyleSheetContents::registerClient(CSSStyleSheet* sheet)
{
ASSERT(!m_loadingClients.contains(sheet) && !m_completedClients.contains(sheet));
if (!sheet->ownerDocument())
return;
if (Document* document = clientSingleOwnerDocument()) {
if (sheet->ownerDocument() != document)
m_hasSingleOwnerDocument = false;
}
m_loadingClients.add(sheet);
}
void StyleSheetContents::unregisterClient(CSSStyleSheet* sheet)
{
m_loadingClients.remove(sheet);
m_completedClients.remove(sheet);
if (!sheet->ownerDocument() || !m_loadingClients.isEmpty() || !m_completedClients.isEmpty())
return;
if (m_hasSingleOwnerDocument)
removeSheetFromCache(sheet->ownerDocument());
m_hasSingleOwnerDocument = true;
}
void StyleSheetContents::clientLoadCompleted(CSSStyleSheet* sheet)
{
ASSERT(m_loadingClients.contains(sheet) || !sheet->ownerDocument());
m_loadingClients.remove(sheet);
if (!sheet->ownerDocument())
return;
m_completedClients.add(sheet);
}
void StyleSheetContents::clientLoadStarted(CSSStyleSheet* sheet)
{
ASSERT(m_completedClients.contains(sheet));
m_completedClients.remove(sheet);
m_loadingClients.add(sheet);
}
void StyleSheetContents::removeSheetFromCache(Document* document)
{
ASSERT(document);
document->styleEngine()->removeSheet(this);
}
void StyleSheetContents::addedToMemoryCache()
{
ASSERT(!m_isInMemoryCache);
ASSERT(isCacheable());
m_isInMemoryCache = true;
}
void StyleSheetContents::removedFromMemoryCache()
{
ASSERT(m_isInMemoryCache);
ASSERT(isCacheable());
m_isInMemoryCache = false;
}
void StyleSheetContents::shrinkToFit()
{
m_importRules.shrinkToFit();
m_childRules.shrinkToFit();
}
RuleSet& StyleSheetContents::ensureRuleSet(const MediaQueryEvaluator& medium, AddRuleFlags addRuleFlags)
{
if (!m_ruleSet) {
m_ruleSet = RuleSet::create();
m_ruleSet->addRulesFromSheet(this, medium, addRuleFlags);
}
return *m_ruleSet.get();
}
static void clearResolvers(WillBeHeapHashSet<RawPtrWillBeWeakMember<CSSStyleSheet> >& clients)
{
for (WillBeHeapHashSet<RawPtrWillBeWeakMember<CSSStyleSheet> >::iterator it = clients.begin(); it != clients.end(); ++it) {
if (Document* document = (*it)->ownerDocument())
document->styleEngine()->clearResolver();
}
}
void StyleSheetContents::clearRuleSet()
{
if (StyleSheetContents* parentSheet = parentStyleSheet())
parentSheet->clearRuleSet();
if (!m_ruleSet)
return;
clearResolvers(m_loadingClients);
clearResolvers(m_completedClients);
m_ruleSet.clear();
}
static void removeFontFaceRules(WillBeHeapHashSet<RawPtrWillBeWeakMember<CSSStyleSheet> >& clients, const StyleRuleFontFace* fontFaceRule)
{
for (WillBeHeapHashSet<RawPtrWillBeWeakMember<CSSStyleSheet> >::iterator it = clients.begin(); it != clients.end(); ++it) {
if (Node* ownerNode = (*it)->ownerNode())
ownerNode->document().styleEngine()->removeFontFaceRules(WillBeHeapVector<RawPtrWillBeMember<const StyleRuleFontFace> >(1, fontFaceRule));
}
}
void StyleSheetContents::notifyRemoveFontFaceRule(const StyleRuleFontFace* fontFaceRule)
{
StyleSheetContents* root = rootStyleSheet();
removeFontFaceRules(root->m_loadingClients, fontFaceRule);
removeFontFaceRules(root->m_completedClients, fontFaceRule);
}
static void findFontFaceRulesFromRules(const WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& rules, WillBeHeapVector<RawPtrWillBeMember<const StyleRuleFontFace> >& fontFaceRules)
{
for (unsigned i = 0; i < rules.size(); ++i) {
StyleRuleBase* rule = rules[i].get();
if (rule->isFontFaceRule()) {
fontFaceRules.append(toStyleRuleFontFace(rule));
} else if (rule->isMediaRule()) {
StyleRuleMedia* mediaRule = toStyleRuleMedia(rule);
findFontFaceRulesFromRules(mediaRule->childRules(), fontFaceRules);
}
}
}
void StyleSheetContents::findFontFaceRules(WillBeHeapVector<RawPtrWillBeMember<const StyleRuleFontFace> >& fontFaceRules)
{
for (unsigned i = 0; i < m_importRules.size(); ++i) {
if (!m_importRules[i]->styleSheet())
continue;
m_importRules[i]->styleSheet()->findFontFaceRules(fontFaceRules);
}
findFontFaceRulesFromRules(childRules(), fontFaceRules);
}
void StyleSheetContents::trace(Visitor* visitor)
{
visitor->trace(m_ownerRule);
visitor->trace(m_importRules);
visitor->trace(m_childRules);
visitor->trace(m_loadingClients);
visitor->trace(m_completedClients);
visitor->trace(m_ruleSet);
}
}