Refactor ShadowRoot exception handling
[WebKit-https.git] / Source / WebCore / dom / ElementShadow.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Neither the name of Google Inc. nor the names of its
11  * contributors may be used to endorse or promote products derived from
12  * this software without specific prior written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "ElementShadow.h"
29
30 #include "ContainerNodeAlgorithms.h"
31 #include "InspectorInstrumentation.h"
32
33 namespace WebCore {
34
35 void ElementShadow::addShadowRoot(Element* shadowHost, PassRefPtr<ShadowRoot> shadowRoot, ShadowRoot::ShadowRootType type)
36 {
37     ASSERT(shadowHost);
38     ASSERT(shadowRoot);
39     ASSERT(!shadowRoot->host());
40     ASSERT(shadowHost->document() == shadowRoot->document());
41
42     if (type == ShadowRoot::AuthorShadowRoot)
43         shadowHost->willAddAuthorShadowRoot();
44
45     shadowRoot->setHost(shadowHost);
46     shadowRoot->setParentTreeScope(shadowHost->treeScope());
47     m_shadowRoots.push(shadowRoot.get());
48     m_distributor.didShadowBoundaryChange(shadowHost);
49     ChildNodeInsertionNotifier(shadowHost).notify(shadowRoot.get());
50
51     // Existence of shadow roots requires the host and its children to do traversal using ComposedShadowTreeWalker.
52     shadowHost->setNeedsShadowTreeWalker();
53
54     // FIXME(94905): ShadowHost should be reattached during recalcStyle.
55     // Set some flag here and recreate shadow hosts' renderer in
56     // Element::recalcStyle.
57     if (shadowHost->attached())
58         shadowHost->lazyReattach();
59
60     InspectorInstrumentation::didPushShadowRoot(shadowHost, shadowRoot.get());
61 }
62
63 void ElementShadow::removeAllShadowRoots()
64 {
65     // Dont protect this ref count.
66     Element* shadowHost = host();
67
68     while (RefPtr<ShadowRoot> oldRoot = m_shadowRoots.head()) {
69         InspectorInstrumentation::willPopShadowRoot(shadowHost, oldRoot.get());
70         shadowHost->document()->removeFocusedNodeOfSubtree(oldRoot.get());
71
72         if (oldRoot->attached())
73             oldRoot->detach();
74
75         m_shadowRoots.removeHead();
76         oldRoot->setHost(0);
77         oldRoot->setParentTreeScope(shadowHost->document());
78         oldRoot->setPrev(0);
79         oldRoot->setNext(0);
80         ChildNodeRemovalNotifier(shadowHost).notify(oldRoot.get());
81     }
82
83     m_distributor.invalidateDistribution(shadowHost);
84 }
85
86 void ElementShadow::attach()
87 {
88     ContentDistributor::ensureDistribution(youngestShadowRoot());
89
90     for (ShadowRoot* root = youngestShadowRoot(); root; root = root->olderShadowRoot()) {
91         if (!root->attached())
92             root->attach();
93     }
94 }
95
96 void ElementShadow::detach()
97 {
98     for (ShadowRoot* root = youngestShadowRoot(); root; root = root->olderShadowRoot()) {
99         if (root->attached())
100             root->detach();
101     }
102 }
103
104 bool ElementShadow::childNeedsStyleRecalc() const
105 {
106     ASSERT(youngestShadowRoot());
107     for (ShadowRoot* root = youngestShadowRoot(); root; root = root->olderShadowRoot())
108         if (root->childNeedsStyleRecalc())
109             return true;
110
111     return false;
112 }
113
114 bool ElementShadow::needsStyleRecalc() const
115 {
116     ASSERT(youngestShadowRoot());
117     for (ShadowRoot* root = youngestShadowRoot(); root; root = root->olderShadowRoot())
118         if (root->needsStyleRecalc())
119             return true;
120
121     return false;
122 }
123
124 void ElementShadow::recalcStyle(Node::StyleChange change)
125 {
126     for (ShadowRoot* root = youngestShadowRoot(); root; root = root->olderShadowRoot())
127         root->recalcStyle(change);
128 }
129
130 void ElementShadow::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
131 {
132     MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::DOM);
133     info.addMember(m_shadowRoots);
134     ShadowRoot* shadowRoot = m_shadowRoots.head();
135     while (shadowRoot) {
136         info.addMember(shadowRoot);
137         shadowRoot = shadowRoot->next();
138     }
139     info.addMember(m_distributor);
140 }
141
142 } // namespace