Only the main document should have a StyleEngine.
In Sky <style> elements in import documents don't apply to the main document,
so we don't need the StyleEngine to be aware of the import tree. This change
makes us only create the StyleEngine for active documents.
R=eseidel@chromium.org
Review URL: https://codereview.chromium.org/813493003
diff --git a/sky/engine/core/dom/Document.cpp b/sky/engine/core/dom/Document.cpp
index feeb300..3180121 100644
--- a/sky/engine/core/dom/Document.cpp
+++ b/sky/engine/core/dom/Document.cpp
@@ -292,11 +292,6 @@
m_lifecycle.advanceTo(DocumentLifecycle::Inactive);
- // Since CSSFontSelector requires Document::m_fetcher and StyleEngine owns
- // CSSFontSelector, need to initialize m_styleEngine after initializing
- // m_fetcher.
- m_styleEngine = StyleEngine::create(*this);
-
#ifndef NDEBUG
liveDocumentSet().add(this);
#endif
@@ -1230,11 +1225,14 @@
StyleResolver* Document::styleResolver() const
{
+ if (!isActive())
+ return 0;
return m_styleEngine->resolver();
}
StyleResolver& Document::ensureStyleResolver() const
{
+ ASSERT(isActive());
return m_styleEngine->ensureResolver();
}
@@ -1247,6 +1245,8 @@
{
ASSERT(m_lifecycle.state() == DocumentLifecycle::Inactive);
+ m_styleEngine = StyleEngine::create(*this);
+
m_renderView = new RenderView(this);
setRenderer(m_renderView);
@@ -1284,6 +1284,7 @@
ContainerNode::detach(context);
m_styleEngine->didDetach();
+ m_styleEngine = nullptr;
// This is required, as our LocalFrame might delete itself as soon as it detaches
// us. However, this violates Node::detach() semantics, as it's never
diff --git a/sky/engine/core/dom/StyleEngine.cpp b/sky/engine/core/dom/StyleEngine.cpp
index 1e3764c..79af43e 100644
--- a/sky/engine/core/dom/StyleEngine.cpp
+++ b/sky/engine/core/dom/StyleEngine.cpp
@@ -44,9 +44,7 @@
StyleEngine::StyleEngine(Document& document)
: m_document(&document)
- , m_isMaster(!document.importsController() || document.importsController()->master() == &document)
, m_documentStyleSheetCollection(StyleSheetCollection::create(document))
- , m_documentScopeDirty(true)
, m_ignorePendingStylesheets(false)
// We don't need to create CSSFontSelector for imported document or
// HTMLTemplateElement's document, because those documents have no frame.
@@ -80,16 +78,6 @@
m_scopedStyleResolvers.clear();
}
-inline Document* StyleEngine::master()
-{
- if (isMaster())
- return m_document;
- HTMLImportsController* import = document().importsController();
- if (!import) // Document::import() can return null while executing its destructor.
- return 0;
- return import->master();
-}
-
const Vector<RefPtr<CSSStyleSheet>>& StyleEngine::activeAuthorStyleSheetsFor(TreeScope& treeScope)
{
if (treeScope == m_document)
@@ -191,10 +179,9 @@
void StyleEngine::updateActiveStyleSheets()
{
- ASSERT(isMaster());
- ASSERT(!document().inStyleRecalc());
+ ASSERT(!m_document->inStyleRecalc());
- if (!document().isActive())
+ if (!m_document->isActive())
return;
documentStyleSheetCollection()->updateActiveStyleSheets(this);
@@ -214,7 +201,6 @@
m_activeTreeScopes.removeAll(treeScopesRemoved);
m_dirtyTreeScopes.clear();
- m_documentScopeDirty = false;
}
void StyleEngine::didRemoveShadowRoot(ShadowRoot* shadowRoot)
@@ -226,8 +212,6 @@
void StyleEngine::appendActiveAuthorStyleSheets()
{
- ASSERT(isMaster());
-
m_resolver->appendAuthorStyleSheets(documentStyleSheetCollection()->activeAuthorStyleSheets());
TreeScopeSet::iterator begin = m_activeTreeScopes.begin();
@@ -245,7 +229,7 @@
// which is not in a frame. Code which hits this should have checked
// Document::isActive() before calling into code which could get here.
- ASSERT(document().frame());
+ ASSERT(m_document->frame());
m_resolver = adoptPtr(new StyleResolver(*m_document));
addScopedStyleResolver(&m_document->ensureScopedStyleResolver());
@@ -255,8 +239,7 @@
void StyleEngine::clearResolver()
{
- ASSERT(!document().inStyleRecalc());
- ASSERT(isMaster() || !m_resolver);
+ ASSERT(!m_document->inStyleRecalc());
for (ScopedStyleResolverSet::iterator it = m_scopedStyleResolvers.begin(); it != m_scopedStyleResolvers.end(); ++it)
const_cast<TreeScope&>((*it)->treeScope()).clearScopedStyleResolver();
@@ -264,12 +247,6 @@
m_resolver.clear();
}
-void StyleEngine::clearMasterResolver()
-{
- if (Document* master = this->master())
- master->styleEngine()->clearResolver();
-}
-
unsigned StyleEngine::resolverAccessCount() const
{
return m_resolver ? m_resolver->accessCount() : 0;
@@ -282,15 +259,9 @@
void StyleEngine::resolverChanged()
{
- if (!isMaster()) {
- if (Document* master = this->master())
- master->styleResolverChanged();
- return;
- }
-
// Don't bother updating, since we haven't loaded all our style info yet
// and haven't calculated the style selector for the first time.
- if (!document().isActive()) {
+ if (!m_document->isActive()) {
clearResolver();
return;
}
@@ -310,7 +281,7 @@
{
// FIXME: we should not update generic font family settings when
// document is inactive.
- ASSERT(document().isActive());
+ ASSERT(m_document->isActive());
if (!m_fontSelector)
return;
@@ -334,21 +305,12 @@
void StyleEngine::markTreeScopeDirty(TreeScope& scope)
{
- if (scope == m_document) {
- markDocumentDirty();
+ // TODO(esprehn): Make document not special.
+ if (scope == m_document)
return;
- }
-
m_dirtyTreeScopes.add(&scope);
}
-void StyleEngine::markDocumentDirty()
-{
- m_documentScopeDirty = true;
- if (document().importLoader())
- document().importsController()->master()->styleEngine()->markDocumentDirty();
-}
-
PassRefPtr<CSSStyleSheet> StyleEngine::createSheet(Element* e, const String& text)
{
RefPtr<CSSStyleSheet> styleSheet;
@@ -391,12 +353,9 @@
void StyleEngine::fontsNeedUpdate(CSSFontSelector*)
{
- if (!document().isActive())
- return;
-
if (m_resolver)
m_resolver->invalidateMatchedPropertiesCache();
- document().setNeedsStyleRecalc(SubtreeStyleChange);
+ m_document->setNeedsStyleRecalc(SubtreeStyleChange);
}
}
diff --git a/sky/engine/core/dom/StyleEngine.h b/sky/engine/core/dom/StyleEngine.h
index e59a50c..9be10a3 100644
--- a/sky/engine/core/dom/StyleEngine.h
+++ b/sky/engine/core/dom/StyleEngine.h
@@ -68,9 +68,7 @@
~StyleEngine();
-#if !ENABLE(OILPAN)
void detachFromDocument();
-#endif
const Vector<RefPtr<CSSStyleSheet>>& activeAuthorStyleSheetsFor(TreeScope&);
@@ -105,7 +103,6 @@
bool hasResolver() const { return m_resolver.get(); }
void clearResolver();
- void clearMasterResolver();
CSSFontSelector* fontSelector() { return m_fontSelector.get(); }
void removeFontFaceRules(const Vector<RawPtr<const StyleRuleFontFace> >&);
@@ -117,8 +114,6 @@
void resolverChanged();
unsigned resolverAccessCount() const;
- void markDocumentDirty();
-
PassRefPtr<CSSStyleSheet> createSheet(Element*, const String& text);
void removeSheet(StyleSheetContents*);
@@ -139,10 +134,6 @@
void markTreeScopeDirty(TreeScope&);
- bool isMaster() const { return m_isMaster; }
- Document* master();
- Document& document() const { return *m_document; }
-
typedef ListHashSet<TreeScope*, 16> TreeScopeSet;
static void insertTreeScopeInDocumentOrder(TreeScopeSet&, TreeScope*);
@@ -159,7 +150,6 @@
}
RawPtr<Document> m_document;
- bool m_isMaster;
// TODO(esprehn): Remove special separate collection for document.
OwnPtr<StyleSheetCollection> m_documentStyleSheetCollection;
@@ -169,7 +159,6 @@
typedef HashSet<RawPtr<const ScopedStyleResolver> > ScopedStyleResolverSet;
ScopedStyleResolverSet m_scopedStyleResolvers;
- bool m_documentScopeDirty;
TreeScopeSet m_dirtyTreeScopes;
TreeScopeSet m_activeTreeScopes;
diff --git a/sky/engine/core/dom/StyleSheetCollection.cpp b/sky/engine/core/dom/StyleSheetCollection.cpp
index 4e99b28..9ffc0a3 100644
--- a/sky/engine/core/dom/StyleSheetCollection.cpp
+++ b/sky/engine/core/dom/StyleSheetCollection.cpp
@@ -85,7 +85,7 @@
// TODO(esprehn): Remove special casing for Document.
if (root.isDocumentNode()) {
- engine->clearMasterResolver();
+ engine->clearResolver();
// FIMXE: The following depends on whether StyleRuleFontFace was modified or not.
// No need to always-clear font cache.
engine->clearFontCache();
diff --git a/sky/engine/core/html/HTMLStyleElement.cpp b/sky/engine/core/html/HTMLStyleElement.cpp
index d7ce4d9..265fd75 100644
--- a/sky/engine/core/html/HTMLStyleElement.cpp
+++ b/sky/engine/core/html/HTMLStyleElement.cpp
@@ -51,7 +51,8 @@
if (inDocument()) {
ContainerNode* scopingNode = this->scopingNode();
TreeScope& scope = scopingNode ? scopingNode->treeScope() : treeScope();
- document().styleEngine()->removeStyleSheetCandidateNode(this, scopingNode, scope);
+ if (StyleEngine* styleEngine = document().styleEngine())
+ styleEngine->removeStyleSheetCandidateNode(this, scopingNode, scope);
}
if (m_sheet)
@@ -76,15 +77,17 @@
void HTMLStyleElement::insertedInto(ContainerNode* insertionPoint)
{
HTMLElement::insertedInto(insertionPoint);
- document().styleEngine()->addStyleSheetCandidateNode(this, false);
- process();
+ if (inActiveDocument()) {
+ document().styleEngine()->addStyleSheetCandidateNode(this, false);
+ process();
+ }
}
void HTMLStyleElement::removedFrom(ContainerNode* insertionPoint)
{
HTMLElement::removedFrom(insertionPoint);
- if (!insertionPoint->inDocument())
+ if (!insertionPoint->inActiveDocument())
return;
ShadowRoot* scopingNode = containingShadowRoot();
@@ -117,7 +120,7 @@
ContainerNode* HTMLStyleElement::scopingNode()
{
- if (!inDocument())
+ if (!inActiveDocument())
return 0;
if (isInShadowTree())
@@ -134,7 +137,7 @@
void HTMLStyleElement::process()
{
- if (!inDocument())
+ if (!inActiveDocument())
return;
TRACE_EVENT0("blink", "StyleElement::process");
diff --git a/sky/engine/core/page/Page.cpp b/sky/engine/core/page/Page.cpp
index ae9af7e..af0be64 100644
--- a/sky/engine/core/page/Page.cpp
+++ b/sky/engine/core/page/Page.cpp
@@ -223,7 +223,7 @@
}
break;
case SettingsDelegate::FontFamilyChange:
- if (mainFrame()->document())
+ if (mainFrame()->document() && mainFrame()->document()->isActive())
mainFrame()->document()->styleEngine()->updateGenericFontFamilySettings();
setNeedsRecalcStyleInAllFrames();
break;