1 /*
2 * Copyright (C) 2013 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 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "core/dom/custom/CustomElementUpgradeCandidateMap.h"
33
34 #include "core/dom/Element.h"
35
36 namespace WebCore {
37
create()38 PassOwnPtrWillBeRawPtr<CustomElementUpgradeCandidateMap> CustomElementUpgradeCandidateMap::create()
39 {
40 return adoptPtrWillBeNoop(new CustomElementUpgradeCandidateMap());
41 }
42
~CustomElementUpgradeCandidateMap()43 CustomElementUpgradeCandidateMap::~CustomElementUpgradeCandidateMap()
44 {
45 #if !ENABLE(OILPAN)
46 // With Oilpan enabled, the observer table keeps a weak reference to the
47 // element; no need for explicit removal.
48 UpgradeCandidateMap::const_iterator::Keys end = m_upgradeCandidates.end().keys();
49 for (UpgradeCandidateMap::const_iterator::Keys it = m_upgradeCandidates.begin().keys(); it != end; ++it)
50 unobserve(*it);
51 #endif
52 }
53
add(const CustomElementDescriptor & descriptor,Element * element)54 void CustomElementUpgradeCandidateMap::add(const CustomElementDescriptor& descriptor, Element* element)
55 {
56 observe(element);
57
58 UpgradeCandidateMap::AddResult result = m_upgradeCandidates.add(element, descriptor);
59 ASSERT_UNUSED(result, result.isNewEntry);
60
61 UnresolvedDefinitionMap::iterator it = m_unresolvedDefinitions.find(descriptor);
62 ElementSet* elements;
63 if (it == m_unresolvedDefinitions.end())
64 elements = m_unresolvedDefinitions.add(descriptor, adoptPtrWillBeNoop(new ElementSet())).storedValue->value.get();
65 else
66 elements = it->value.get();
67 elements->add(element);
68 }
69
elementWasDestroyed(Element * element)70 void CustomElementUpgradeCandidateMap::elementWasDestroyed(Element* element)
71 {
72 CustomElementObserver::elementWasDestroyed(element);
73 UpgradeCandidateMap::iterator candidate = m_upgradeCandidates.find(element);
74 ASSERT_WITH_SECURITY_IMPLICATION(candidate != m_upgradeCandidates.end());
75
76 UnresolvedDefinitionMap::iterator elements = m_unresolvedDefinitions.find(candidate->value);
77 ASSERT_WITH_SECURITY_IMPLICATION(elements != m_unresolvedDefinitions.end());
78 elements->value->remove(element);
79 m_upgradeCandidates.remove(candidate);
80 }
81
elementDidFinishParsingChildren(Element * element)82 void CustomElementUpgradeCandidateMap::elementDidFinishParsingChildren(Element* element)
83 {
84 // An upgrade candidate finished parsing; reorder so that eventual
85 // upgrade order matches finished-parsing order.
86 moveToEnd(element);
87 }
88
moveToEnd(Element * element)89 void CustomElementUpgradeCandidateMap::moveToEnd(Element* element)
90 {
91 UpgradeCandidateMap::iterator candidate = m_upgradeCandidates.find(element);
92 ASSERT_WITH_SECURITY_IMPLICATION(candidate != m_upgradeCandidates.end());
93
94 UnresolvedDefinitionMap::iterator elements = m_unresolvedDefinitions.find(candidate->value);
95 ASSERT_WITH_SECURITY_IMPLICATION(elements != m_unresolvedDefinitions.end());
96 elements->value->appendOrMoveToLast(element);
97 }
98
takeUpgradeCandidatesFor(const CustomElementDescriptor & descriptor)99 PassOwnPtrWillBeRawPtr<CustomElementUpgradeCandidateMap::ElementSet> CustomElementUpgradeCandidateMap::takeUpgradeCandidatesFor(const CustomElementDescriptor& descriptor)
100 {
101 OwnPtrWillBeRawPtr<ElementSet> candidates = m_unresolvedDefinitions.take(descriptor);
102
103 if (!candidates)
104 return nullptr;
105
106 for (ElementSet::const_iterator candidate = candidates->begin(); candidate != candidates->end(); ++candidate) {
107 unobserve(*candidate);
108 m_upgradeCandidates.remove(*candidate);
109 }
110 return candidates.release();
111 }
112
trace(Visitor * visitor)113 void CustomElementUpgradeCandidateMap::trace(Visitor* visitor)
114 {
115 visitor->trace(m_upgradeCandidates);
116 visitor->trace(m_unresolvedDefinitions);
117 CustomElementObserver::trace(visitor);
118 }
119
120 }
121