Merge all StyleSheetCollections.

We don't need all these subtypes, or to create the base class as to
what amounts to a thin wrapper around a Vector. Instead lets merge all
the types together. Future patches will remove the special cases inside
the collection for Document vs ShadowRoot.

R=eseidel@chromium.org

Review URL: https://codereview.chromium.org/799393005
diff --git a/sky/engine/core/core.gni b/sky/engine/core/core.gni
index f43c470..4fa6426 100644
--- a/sky/engine/core/core.gni
+++ b/sky/engine/core/core.gni
@@ -428,8 +428,6 @@
   "dom/DocumentOrderedMap.h",
   "dom/DocumentParser.cpp",
   "dom/DocumentParser.h",
-  "dom/DocumentStyleSheetCollection.cpp",
-  "dom/DocumentStyleSheetCollection.h",
   "dom/DocumentSupplementable.h",
   "dom/DOMError.cpp",
   "dom/DOMError.h",
@@ -512,8 +510,6 @@
   "dom/shadow/ShadowRoot.cpp",
   "dom/shadow/ShadowRoot.h",
   "dom/shadow/ShadowRootRareData.h",
-  "dom/ShadowTreeStyleSheetCollection.cpp",
-  "dom/ShadowTreeStyleSheetCollection.h",
   "dom/SimulatedClickOptions.h",
   "dom/SpaceSplitString.cpp",
   "dom/SpaceSplitString.h",
@@ -535,8 +531,6 @@
   "dom/TreeScope.h",
   "dom/TreeScopeAdopter.cpp",
   "dom/TreeScopeAdopter.h",
-  "dom/TreeScopeStyleSheetCollection.cpp",
-  "dom/TreeScopeStyleSheetCollection.h",
   "dom/TreeShared.h",
   "dom/UserActionElementSet.cpp",
   "dom/UserActionElementSet.h",
diff --git a/sky/engine/core/dom/DocumentStyleSheetCollection.cpp b/sky/engine/core/dom/DocumentStyleSheetCollection.cpp
deleted file mode 100644
index 3d4526a..0000000
--- a/sky/engine/core/dom/DocumentStyleSheetCollection.cpp
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
- *           (C) 1999 Antti Koivisto (koivisto@kde.org)
- *           (C) 2001 Dirk Mueller (mueller@kde.org)
- *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
- * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All rights reserved.
- * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
- * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
- * Copyright (C) 2013 Google Inc. All rights reserved.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public License
- * along with this library; see the file COPYING.LIB.  If not, write to
- * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- */
-
-#include "sky/engine/config.h"
-#include "sky/engine/core/dom/DocumentStyleSheetCollection.h"
-
-#include "gen/sky/platform/RuntimeEnabledFeatures.h"
-#include "sky/engine/core/css/resolver/StyleResolver.h"
-#include "sky/engine/core/dom/Document.h"
-#include "sky/engine/core/dom/StyleEngine.h"
-#include "sky/engine/core/html/HTMLStyleElement.h"
-
-namespace blink {
-
-DocumentStyleSheetCollection::DocumentStyleSheetCollection(TreeScope& treeScope)
-    : TreeScopeStyleSheetCollection(treeScope)
-{
-    ASSERT(treeScope.rootNode() == treeScope.rootNode().document());
-}
-
-void DocumentStyleSheetCollection::collectStyleSheetsFromCandidates(StyleEngine* engine, StyleSheetCollection& collection)
-{
-    DocumentOrderedList::iterator begin = m_styleSheetCandidateNodes.begin();
-    DocumentOrderedList::iterator end = m_styleSheetCandidateNodes.end();
-    for (DocumentOrderedList::iterator it = begin; it != end; ++it) {
-        Node* node = *it;
-        if (!isHTMLStyleElement(*node))
-            continue;
-        if (CSSStyleSheet* sheet = toHTMLStyleElement(node)->sheet())
-            collection.appendActiveStyleSheet(sheet);
-    }
-}
-
-void DocumentStyleSheetCollection::collectStyleSheets(StyleEngine* engine, StyleSheetCollection& collection)
-{
-    ASSERT(document().styleEngine() == engine);
-    collection.appendActiveStyleSheets(engine->documentAuthorStyleSheets());
-    collectStyleSheetsFromCandidates(engine, collection);
-}
-
-void DocumentStyleSheetCollection::updateActiveStyleSheets(StyleEngine* engine)
-{
-    StyleSheetCollection collection;
-    collectStyleSheets(engine, collection);
-
-    engine->clearMasterResolver();
-    // FIMXE: The following depends on whether StyleRuleFontFace was modified or not.
-    // No need to always-clear font cache.
-    engine->clearFontCache();
-
-    // TODO(esprehn): This is terrible and not needed in Sky, we should mark
-    // specific tree scopes dirty instead.
-    document().setNeedsStyleRecalc(SubtreeStyleChange);
-
-    collection.swap(*this);
-}
-
-}
diff --git a/sky/engine/core/dom/DocumentStyleSheetCollection.h b/sky/engine/core/dom/DocumentStyleSheetCollection.h
deleted file mode 100644
index 50fce86..0000000
--- a/sky/engine/core/dom/DocumentStyleSheetCollection.h
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
- *           (C) 1999 Antti Koivisto (koivisto@kde.org)
- *           (C) 2001 Dirk Mueller (mueller@kde.org)
- *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
- * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All rights reserved.
- * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
- * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
- * Copyright (C) 2013 Google Inc. All rights reserved.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public License
- * along with this library; see the file COPYING.LIB.  If not, write to
- * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- *
- */
-
-#ifndef SKY_ENGINE_CORE_DOM_DOCUMENTSTYLESHEETCOLLECTION_H_
-#define SKY_ENGINE_CORE_DOM_DOCUMENTSTYLESHEETCOLLECTION_H_
-
-#include "sky/engine/core/dom/TreeScopeStyleSheetCollection.h"
-
-namespace blink {
-
-class StyleEngine;
-class TreeScope;
-
-class DocumentStyleSheetCollection final : public TreeScopeStyleSheetCollection {
-    WTF_MAKE_NONCOPYABLE(DocumentStyleSheetCollection);
-    WTF_MAKE_FAST_ALLOCATED;
-public:
-    static PassOwnPtr<DocumentStyleSheetCollection> create(TreeScope& treeScope)
-    {
-        return adoptPtr(new DocumentStyleSheetCollection(treeScope));
-    }
-
-    void updateActiveStyleSheets(StyleEngine*);
-
-private:
-    explicit DocumentStyleSheetCollection(TreeScope&);
-
-    void collectStyleSheets(StyleEngine*, StyleSheetCollection&);
-    void collectStyleSheetsFromCandidates(StyleEngine*, StyleSheetCollection&);
-};
-
-}
-
-#endif  // SKY_ENGINE_CORE_DOM_DOCUMENTSTYLESHEETCOLLECTION_H_
-
diff --git a/sky/engine/core/dom/ShadowTreeStyleSheetCollection.cpp b/sky/engine/core/dom/ShadowTreeStyleSheetCollection.cpp
deleted file mode 100644
index 8d73870..0000000
--- a/sky/engine/core/dom/ShadowTreeStyleSheetCollection.cpp
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
- *           (C) 1999 Antti Koivisto (koivisto@kde.org)
- *           (C) 2001 Dirk Mueller (mueller@kde.org)
- *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
- * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All rights reserved.
- * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
- * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
- * Copyright (C) 2013 Google Inc. All rights reserved.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public License
- * along with this library; see the file COPYING.LIB.  If not, write to
- * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- */
-
-#include "sky/engine/config.h"
-#include "sky/engine/core/dom/ShadowTreeStyleSheetCollection.h"
-
-#include "sky/engine/core/css/CSSStyleSheet.h"
-#include "sky/engine/core/css/resolver/StyleResolver.h"
-#include "sky/engine/core/dom/Element.h"
-#include "sky/engine/core/dom/StyleEngine.h"
-#include "sky/engine/core/dom/shadow/ShadowRoot.h"
-#include "sky/engine/core/html/HTMLStyleElement.h"
-
-namespace blink {
-
-ShadowTreeStyleSheetCollection::ShadowTreeStyleSheetCollection(ShadowRoot& shadowRoot)
-    : TreeScopeStyleSheetCollection(shadowRoot)
-{
-}
-
-void ShadowTreeStyleSheetCollection::collectStyleSheets(StyleEngine* engine, StyleSheetCollection& collection)
-{
-    DocumentOrderedList::iterator begin = m_styleSheetCandidateNodes.begin();
-    DocumentOrderedList::iterator end = m_styleSheetCandidateNodes.end();
-    for (DocumentOrderedList::iterator it = begin; it != end; ++it) {
-        Node* node = *it;
-        if (!isHTMLStyleElement(*node))
-            continue;
-        if (CSSStyleSheet* sheet = toHTMLStyleElement(node)->sheet())
-            collection.appendActiveStyleSheet(sheet);
-    }
-}
-
-void ShadowTreeStyleSheetCollection::updateActiveStyleSheets(StyleEngine* engine)
-{
-    StyleSheetCollection collection;
-    collectStyleSheets(engine, collection);
-
-    if (StyleResolver* styleResolver = engine->resolver()) {
-        // We should not destroy StyleResolver when we find any stylesheet update in a shadow tree.
-        // In this case, we will reset rulesets created from style elements in the shadow tree.
-        styleResolver->resetAuthorStyle(treeScope());
-        styleResolver->removePendingAuthorStyleSheets(m_activeAuthorStyleSheets);
-        styleResolver->lazyAppendAuthorStyleSheets(0, collection.activeAuthorStyleSheets());
-    }
-
-    toShadowRoot(treeScope().rootNode()).host()->setNeedsStyleRecalc(SubtreeStyleChange);
-
-    collection.swap(*this);
-}
-
-}
diff --git a/sky/engine/core/dom/ShadowTreeStyleSheetCollection.h b/sky/engine/core/dom/ShadowTreeStyleSheetCollection.h
deleted file mode 100644
index dcd8d67..0000000
--- a/sky/engine/core/dom/ShadowTreeStyleSheetCollection.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
- *           (C) 1999 Antti Koivisto (koivisto@kde.org)
- *           (C) 2001 Dirk Mueller (mueller@kde.org)
- *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
- * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All rights reserved.
- * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
- * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
- * Copyright (C) 2013 Google Inc. All rights reserved.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public License
- * along with this library; see the file COPYING.LIB.  If not, write to
- * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- *
- */
-
-#ifndef SKY_ENGINE_CORE_DOM_SHADOWTREESTYLESHEETCOLLECTION_H_
-#define SKY_ENGINE_CORE_DOM_SHADOWTREESTYLESHEETCOLLECTION_H_
-
-#include "sky/engine/core/dom/TreeScopeStyleSheetCollection.h"
-
-namespace blink {
-
-class ShadowRoot;
-class StyleSheetCollection;
-class StyleEngine;
-
-class ShadowTreeStyleSheetCollection final : public TreeScopeStyleSheetCollection {
-    WTF_MAKE_NONCOPYABLE(ShadowTreeStyleSheetCollection);
-    WTF_MAKE_FAST_ALLOCATED;
-public:
-    explicit ShadowTreeStyleSheetCollection(ShadowRoot&);
-
-    void updateActiveStyleSheets(StyleEngine*);
-
-private:
-    void collectStyleSheets(StyleEngine*, StyleSheetCollection&);
-};
-
-}
-
-#endif  // SKY_ENGINE_CORE_DOM_SHADOWTREESTYLESHEETCOLLECTION_H_
-
diff --git a/sky/engine/core/dom/StyleEngine.cpp b/sky/engine/core/dom/StyleEngine.cpp
index 9a862e7..1e3764c 100644
--- a/sky/engine/core/dom/StyleEngine.cpp
+++ b/sky/engine/core/dom/StyleEngine.cpp
@@ -33,7 +33,7 @@
 #include "sky/engine/core/css/FontFaceCache.h"
 #include "sky/engine/core/css/StyleSheetContents.h"
 #include "sky/engine/core/dom/Element.h"
-#include "sky/engine/core/dom/ShadowTreeStyleSheetCollection.h"
+#include "sky/engine/core/dom/StyleSheetCollection.h"
 #include "sky/engine/core/dom/shadow/ShadowRoot.h"
 #include "sky/engine/core/frame/Settings.h"
 #include "sky/engine/core/html/HTMLStyleElement.h"
@@ -45,7 +45,7 @@
 StyleEngine::StyleEngine(Document& document)
     : m_document(&document)
     , m_isMaster(!document.importsController() || document.importsController()->master() == &document)
-    , m_documentStyleSheetCollection(DocumentStyleSheetCollection::create(document))
+    , m_documentStyleSheetCollection(StyleSheetCollection::create(document))
     , m_documentScopeDirty(true)
     , m_ignorePendingStylesheets(false)
     // We don't need to create CSSFontSelector for imported document or
@@ -60,14 +60,11 @@
 {
 }
 
-#if !ENABLE(OILPAN)
 void StyleEngine::detachFromDocument()
 {
     // Cleanup is performed eagerly when the StyleEngine is removed from the
     // document. The StyleEngine is unreachable after this, since only the
     // document has a reference to it.
-    for (unsigned i = 0; i < m_authorStyleSheets.size(); ++i)
-        m_authorStyleSheets[i]->clearOwnerNode();
 
     if (m_fontSelector) {
         m_fontSelector->clearDocument();
@@ -82,7 +79,6 @@
         const_cast<TreeScope&>((*it)->treeScope()).clearScopedStyleResolver();
     m_scopedStyleResolvers.clear();
 }
-#endif
 
 inline Document* StyleEngine::master()
 {
@@ -128,18 +124,18 @@
     treeScopes.insertBefore(followingTreeScope, treeScope);
 }
 
-TreeScopeStyleSheetCollection* StyleEngine::ensureStyleSheetCollectionFor(TreeScope& treeScope)
+StyleSheetCollection* StyleEngine::ensureStyleSheetCollectionFor(TreeScope& treeScope)
 {
     if (treeScope == m_document)
         return documentStyleSheetCollection();
 
     StyleSheetCollectionMap::AddResult result = m_styleSheetCollectionMap.add(&treeScope, nullptr);
     if (result.isNewEntry)
-        result.storedValue->value = adoptPtr(new ShadowTreeStyleSheetCollection(toShadowRoot(treeScope)));
+        result.storedValue->value = StyleSheetCollection::create(treeScope);
     return result.storedValue->value.get();
 }
 
-TreeScopeStyleSheetCollection* StyleEngine::styleSheetCollectionFor(TreeScope& treeScope)
+StyleSheetCollection* StyleEngine::styleSheetCollectionFor(TreeScope& treeScope)
 {
     if (treeScope == m_document)
         return documentStyleSheetCollection();
@@ -172,7 +168,7 @@
 
     TreeScope& treeScope = isHTMLStyleElement(*node) ? node->treeScope() : *m_document;
     ASSERT(isHTMLStyleElement(node) || treeScope == m_document);
-    TreeScopeStyleSheetCollection* collection = ensureStyleSheetCollectionFor(treeScope);
+    StyleSheetCollection* collection = ensureStyleSheetCollectionFor(treeScope);
     ASSERT(collection);
     collection->addStyleSheetCandidateNode(node, createdByParser);
 
@@ -185,7 +181,7 @@
 {
     ASSERT(isHTMLStyleElement(node) || treeScope == m_document);
 
-    TreeScopeStyleSheetCollection* collection = styleSheetCollectionFor(treeScope);
+    StyleSheetCollection* collection = styleSheetCollectionFor(treeScope);
     ASSERT(collection);
     collection->removeStyleSheetCandidateNode(node, scopingNode);
 
@@ -209,7 +205,7 @@
     for (TreeScopeSet::iterator it = treeScopes.begin(); it != treeScopes.end(); ++it) {
         TreeScope* treeScope = *it;
         ASSERT(treeScope != m_document);
-        ShadowTreeStyleSheetCollection* collection = static_cast<ShadowTreeStyleSheetCollection*>(styleSheetCollectionFor(*treeScope));
+        StyleSheetCollection* collection = styleSheetCollectionFor(*treeScope);
         ASSERT(collection);
         collection->updateActiveStyleSheets(this);
         if (!collection->hasStyleSheetCandidateNodes())
@@ -237,7 +233,7 @@
     TreeScopeSet::iterator begin = m_activeTreeScopes.begin();
     TreeScopeSet::iterator end = m_activeTreeScopes.end();
     for (TreeScopeSet::iterator it = begin; it != end; ++it) {
-        if (TreeScopeStyleSheetCollection* collection = m_styleSheetCollectionMap.get(*it))
+        if (StyleSheetCollection* collection = m_styleSheetCollectionMap.get(*it))
             m_resolver->appendAuthorStyleSheets(collection->activeAuthorStyleSheets());
     }
     m_resolver->finishAppendAuthorStyleSheets();
diff --git a/sky/engine/core/dom/StyleEngine.h b/sky/engine/core/dom/StyleEngine.h
index 7182a5a..e59a50c 100644
--- a/sky/engine/core/dom/StyleEngine.h
+++ b/sky/engine/core/dom/StyleEngine.h
@@ -32,7 +32,7 @@
 #include "sky/engine/core/css/resolver/StyleResolver.h"
 #include "sky/engine/core/dom/Document.h"
 #include "sky/engine/core/dom/DocumentOrderedList.h"
-#include "sky/engine/core/dom/DocumentStyleSheetCollection.h"
+#include "sky/engine/core/dom/StyleSheetCollection.h"
 #include "sky/engine/platform/heap/Handle.h"
 #include "sky/engine/wtf/FastAllocBase.h"
 #include "sky/engine/wtf/ListHashSet.h"
@@ -47,7 +47,6 @@
 class CSSStyleSheet;
 class Node;
 class RuleFeatureSet;
-class ShadowTreeStyleSheetCollection;
 class StyleRuleFontFace;
 class StyleSheetContents;
 
@@ -74,7 +73,6 @@
 #endif
 
     const Vector<RefPtr<CSSStyleSheet>>& activeAuthorStyleSheetsFor(TreeScope&);
-    const Vector<RefPtr<CSSStyleSheet> >& documentAuthorStyleSheets() const { return m_authorStyleSheets; }
 
     void modifiedStyleSheet(CSSStyleSheet*);
     void addStyleSheetCandidateNode(Node*, bool createdByParser);
@@ -136,8 +134,8 @@
 private:
     StyleEngine(Document&);
 
-    TreeScopeStyleSheetCollection* ensureStyleSheetCollectionFor(TreeScope&);
-    TreeScopeStyleSheetCollection* styleSheetCollectionFor(TreeScope&);
+    StyleSheetCollection* ensureStyleSheetCollectionFor(TreeScope&);
+    StyleSheetCollection* styleSheetCollectionFor(TreeScope&);
 
     void markTreeScopeDirty(TreeScope&);
 
@@ -150,12 +148,12 @@
 
     void createResolver();
 
-    const DocumentStyleSheetCollection* documentStyleSheetCollection() const
+    const StyleSheetCollection* documentStyleSheetCollection() const
     {
         return m_documentStyleSheetCollection.get();
     }
 
-    DocumentStyleSheetCollection* documentStyleSheetCollection()
+    StyleSheetCollection* documentStyleSheetCollection()
     {
         return m_documentStyleSheetCollection.get();
     }
@@ -163,11 +161,10 @@
     RawPtr<Document> m_document;
     bool m_isMaster;
 
-    Vector<RefPtr<CSSStyleSheet> > m_authorStyleSheets;
+    // TODO(esprehn): Remove special separate collection for document.
+    OwnPtr<StyleSheetCollection> m_documentStyleSheetCollection;
 
-    OwnPtr<DocumentStyleSheetCollection> m_documentStyleSheetCollection;
-
-    typedef HashMap<RawPtr<TreeScope>, OwnPtr<ShadowTreeStyleSheetCollection> > StyleSheetCollectionMap;
+    typedef HashMap<RawPtr<TreeScope>, OwnPtr<StyleSheetCollection> > StyleSheetCollectionMap;
     StyleSheetCollectionMap m_styleSheetCollectionMap;
     typedef HashSet<RawPtr<const ScopedStyleResolver> > ScopedStyleResolverSet;
     ScopedStyleResolverSet m_scopedStyleResolvers;
diff --git a/sky/engine/core/dom/StyleSheetCollection.cpp b/sky/engine/core/dom/StyleSheetCollection.cpp
index 8a864d9..4e99b28 100644
--- a/sky/engine/core/dom/StyleSheetCollection.cpp
+++ b/sky/engine/core/dom/StyleSheetCollection.cpp
@@ -28,10 +28,17 @@
 #include "sky/engine/core/dom/StyleSheetCollection.h"
 
 #include "sky/engine/core/css/CSSStyleSheet.h"
+#include "sky/engine/core/css/resolver/StyleResolver.h"
+#include "sky/engine/core/dom/Document.h"
+#include "sky/engine/core/dom/StyleEngine.h"
+#include "sky/engine/core/dom/shadow/ShadowRoot.h"
+#include "sky/engine/core/dom/TreeScope.h"
+#include "sky/engine/core/html/HTMLStyleElement.h"
 
 namespace blink {
 
-StyleSheetCollection::StyleSheetCollection()
+StyleSheetCollection::StyleSheetCollection(TreeScope& treeScope)
+    : m_treeScope(treeScope)
 {
 }
 
@@ -39,19 +46,67 @@
 {
 }
 
-void StyleSheetCollection::swap(StyleSheetCollection& other)
+void StyleSheetCollection::addStyleSheetCandidateNode(Node* node, bool)
 {
-    m_activeAuthorStyleSheets.swap(other.m_activeAuthorStyleSheets);
+    if (!node->inDocument())
+        return;
+
+    // Until the <body> exists, we have no choice but to compare document positions,
+    // since styles outside of the body and head continue to be shunted into the head
+    // (and thus can shift to end up before dynamically added DOM content that is also
+    // outside the body).
+    m_styleSheetCandidateNodes.add(node);
 }
 
-void StyleSheetCollection::appendActiveStyleSheets(const Vector<RefPtr<CSSStyleSheet> >& sheets)
+void StyleSheetCollection::removeStyleSheetCandidateNode(Node* node, ContainerNode* scopingNode)
 {
-    m_activeAuthorStyleSheets.appendVector(sheets);
+    m_styleSheetCandidateNodes.remove(node);
 }
 
-void StyleSheetCollection::appendActiveStyleSheet(CSSStyleSheet* sheet)
+void StyleSheetCollection::collectStyleSheets(Vector<RefPtr<CSSStyleSheet>>& sheets)
 {
-    m_activeAuthorStyleSheets.append(sheet);
+    DocumentOrderedList::iterator begin = m_styleSheetCandidateNodes.begin();
+    DocumentOrderedList::iterator end = m_styleSheetCandidateNodes.end();
+    for (DocumentOrderedList::iterator it = begin; it != end; ++it) {
+        Node* node = *it;
+        if (!isHTMLStyleElement(*node))
+            continue;
+        if (CSSStyleSheet* sheet = toHTMLStyleElement(node)->sheet())
+            sheets.append(sheet);
+    }
+}
+
+void StyleSheetCollection::updateActiveStyleSheets(StyleEngine* engine)
+{
+    Vector<RefPtr<CSSStyleSheet>> candidateSheets;
+    collectStyleSheets(candidateSheets);
+
+    Node& root = m_treeScope.rootNode();
+
+    // TODO(esprehn): Remove special casing for Document.
+    if (root.isDocumentNode()) {
+        engine->clearMasterResolver();
+        // FIMXE: The following depends on whether StyleRuleFontFace was modified or not.
+        // No need to always-clear font cache.
+        engine->clearFontCache();
+    } else if (StyleResolver* styleResolver = engine->resolver()) {
+        // We should not destroy StyleResolver when we find any stylesheet update in a shadow tree.
+        // In this case, we will reset rulesets created from style elements in the shadow tree.
+        styleResolver->resetAuthorStyle(m_treeScope);
+        styleResolver->removePendingAuthorStyleSheets(m_activeAuthorStyleSheets);
+        styleResolver->lazyAppendAuthorStyleSheets(0, candidateSheets);
+    }
+
+    // TODO(esprehn): We should avoid subtree recalcs in sky when rules change
+    // and only recalc specific tree scopes.
+    root.setNeedsStyleRecalc(SubtreeStyleChange);
+
+    // TODO(esprehn): We should use LocalStyleChange, :host rule changes
+    // can only impact the host directly as Sky has no descendant selectors.
+    if (root.isShadowRoot())
+        toShadowRoot(root).host()->setNeedsStyleRecalc(SubtreeStyleChange);
+
+    m_activeAuthorStyleSheets.swap(candidateSheets);
 }
 
 }
diff --git a/sky/engine/core/dom/StyleSheetCollection.h b/sky/engine/core/dom/StyleSheetCollection.h
index b140fe8..48d2377 100644
--- a/sky/engine/core/dom/StyleSheetCollection.h
+++ b/sky/engine/core/dom/StyleSheetCollection.h
@@ -28,31 +28,46 @@
 #ifndef SKY_ENGINE_CORE_DOM_STYLESHEETCOLLECTION_H_
 #define SKY_ENGINE_CORE_DOM_STYLESHEETCOLLECTION_H_
 
-#include "sky/engine/platform/heap/Handle.h"
+#include "sky/engine/core/dom/DocumentOrderedList.h"
 #include "sky/engine/wtf/FastAllocBase.h"
+#include "sky/engine/wtf/PassOwnPtr.h"
 #include "sky/engine/wtf/RefPtr.h"
 #include "sky/engine/wtf/Vector.h"
 
 namespace blink {
 
 class CSSStyleSheet;
+class ContainerNode;
+class StyleEngine;
+class TreeScope;
 
 class StyleSheetCollection {
     WTF_MAKE_NONCOPYABLE(StyleSheetCollection);
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    StyleSheetCollection();
-    virtual ~StyleSheetCollection();
+    static PassOwnPtr<StyleSheetCollection> create(TreeScope& treeScope)
+    {
+        return adoptPtr(new StyleSheetCollection(treeScope));
+    }
+    ~StyleSheetCollection();
 
     Vector<RefPtr<CSSStyleSheet> >& activeAuthorStyleSheets() { return m_activeAuthorStyleSheets; }
     const Vector<RefPtr<CSSStyleSheet> >& activeAuthorStyleSheets() const { return m_activeAuthorStyleSheets; }
 
-    void swap(StyleSheetCollection&);
-    void appendActiveStyleSheets(const Vector<RefPtr<CSSStyleSheet> >&);
-    void appendActiveStyleSheet(CSSStyleSheet*);
+    void addStyleSheetCandidateNode(Node*, bool createdByParser);
+    void removeStyleSheetCandidateNode(Node*, ContainerNode* scopingNode);
+    bool hasStyleSheetCandidateNodes() const { return !m_styleSheetCandidateNodes.isEmpty(); }
 
-protected:
-    Vector<RefPtr<CSSStyleSheet> > m_activeAuthorStyleSheets;
+    void updateActiveStyleSheets(StyleEngine*);
+
+private:
+    explicit StyleSheetCollection(TreeScope&);
+
+    void collectStyleSheets(Vector<RefPtr<CSSStyleSheet>>& candidateSheets);
+
+    TreeScope& m_treeScope;
+    DocumentOrderedList m_styleSheetCandidateNodes;    
+    Vector<RefPtr<CSSStyleSheet>> m_activeAuthorStyleSheets;
 };
 
 }
diff --git a/sky/engine/core/dom/TreeScopeStyleSheetCollection.cpp b/sky/engine/core/dom/TreeScopeStyleSheetCollection.cpp
deleted file mode 100644
index dd5038a..0000000
--- a/sky/engine/core/dom/TreeScopeStyleSheetCollection.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
- *           (C) 1999 Antti Koivisto (koivisto@kde.org)
- *           (C) 2001 Dirk Mueller (mueller@kde.org)
- *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
- * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All rights reserved.
- * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
- * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
- * Copyright (C) 2013 Google Inc. All rights reserved.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public License
- * along with this library; see the file COPYING.LIB.  If not, write to
- * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- */
-
-#include "sky/engine/config.h"
-#include "sky/engine/core/dom/TreeScopeStyleSheetCollection.h"
-
-#include "sky/engine/core/css/CSSStyleSheet.h"
-#include "sky/engine/core/css/StyleSheetContents.h"
-#include "sky/engine/core/css/resolver/StyleResolver.h"
-#include "sky/engine/core/dom/Element.h"
-#include "sky/engine/core/dom/StyleEngine.h"
-#include "sky/engine/core/html/HTMLStyleElement.h"
-
-namespace blink {
-
-TreeScopeStyleSheetCollection::TreeScopeStyleSheetCollection(TreeScope& treeScope)
-    : m_treeScope(treeScope)
-{
-}
-
-void TreeScopeStyleSheetCollection::addStyleSheetCandidateNode(Node* node, bool)
-{
-    if (!node->inDocument())
-        return;
-
-    // Until the <body> exists, we have no choice but to compare document positions,
-    // since styles outside of the body and head continue to be shunted into the head
-    // (and thus can shift to end up before dynamically added DOM content that is also
-    // outside the body).
-    m_styleSheetCandidateNodes.add(node);
-}
-
-void TreeScopeStyleSheetCollection::removeStyleSheetCandidateNode(Node* node, ContainerNode* scopingNode)
-{
-    m_styleSheetCandidateNodes.remove(node);
-}
-
-}
diff --git a/sky/engine/core/dom/TreeScopeStyleSheetCollection.h b/sky/engine/core/dom/TreeScopeStyleSheetCollection.h
deleted file mode 100644
index 47a60dd..0000000
--- a/sky/engine/core/dom/TreeScopeStyleSheetCollection.h
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
- *           (C) 1999 Antti Koivisto (koivisto@kde.org)
- *           (C) 2001 Dirk Mueller (mueller@kde.org)
- *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
- * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All rights reserved.
- * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
- * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
- * Copyright (C) 2011 Google Inc. All rights reserved.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public License
- * along with this library; see the file COPYING.LIB.  If not, write to
- * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- *
- */
-
-#ifndef SKY_ENGINE_CORE_DOM_TREESCOPESTYLESHEETCOLLECTION_H_
-#define SKY_ENGINE_CORE_DOM_TREESCOPESTYLESHEETCOLLECTION_H_
-
-#include "sky/engine/core/dom/Document.h"
-#include "sky/engine/core/dom/DocumentOrderedList.h"
-#include "sky/engine/core/dom/StyleSheetCollection.h"
-#include "sky/engine/core/dom/TreeScope.h"
-#include "sky/engine/wtf/FastAllocBase.h"
-#include "sky/engine/wtf/HashMap.h"
-#include "sky/engine/wtf/ListHashSet.h"
-#include "sky/engine/wtf/RefPtr.h"
-#include "sky/engine/wtf/Vector.h"
-#include "sky/engine/wtf/text/WTFString.h"
-
-namespace blink {
-
-class ContainerNode;
-class Node;
-class StyleSheetContents;
-class StyleRuleFontFace;
-
-class TreeScopeStyleSheetCollection : public StyleSheetCollection {
-public:
-    void addStyleSheetCandidateNode(Node*, bool createdByParser);
-    void removeStyleSheetCandidateNode(Node*, ContainerNode* scopingNode);
-    bool hasStyleSheetCandidateNodes() const { return !m_styleSheetCandidateNodes.isEmpty(); }
-
-    DocumentOrderedList& styleSheetCandidateNodes() { return m_styleSheetCandidateNodes; }
-
-protected:
-    explicit TreeScopeStyleSheetCollection(TreeScope&);
-
-    Document& document() const { return treeScope().document(); }
-    TreeScope& treeScope() const { return *m_treeScope; }
-
-    enum StyleResolverUpdateType {
-        Reconstruct,
-        Reset,
-        Additive
-    };
-
-protected:
-    RawPtr<TreeScope> m_treeScope;
-
-    DocumentOrderedList m_styleSheetCandidateNodes;
-};
-
-}
-
-#endif  // SKY_ENGINE_CORE_DOM_TREESCOPESTYLESHEETCOLLECTION_H_