blob: c84bcb7dec56a504de7c372580cac0a3da08e967 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010032#include "core/dom/CustomElementUpgradeCandidateMap.h"
33
Ben Murdoch02772c62013-07-26 10:21:05 +010034#include "core/dom/Element.h"
35
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010036namespace WebCore {
37
Ben Murdoch02772c62013-07-26 10:21:05 +010038CustomElementUpgradeCandidateMap::~CustomElementUpgradeCandidateMap()
39{
40 UpgradeCandidateMap::const_iterator::Keys end = m_upgradeCandidates.end().keys();
41 for (UpgradeCandidateMap::const_iterator::Keys it = m_upgradeCandidates.begin().keys(); it != end; ++it)
42 unregisterForElementDestructionNotification(*it, this);
43}
44
Ben Murdoche69819b2013-07-17 14:56:49 +010045void CustomElementUpgradeCandidateMap::add(const CustomElementDescriptor& descriptor, Element* element)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010046{
Ben Murdoch02772c62013-07-26 10:21:05 +010047 element->setCustomElementState(Element::UpgradeCandidate);
48
49 registerForElementDestructionNotification(element, this);
50
51 UpgradeCandidateMap::AddResult result = m_upgradeCandidates.add(element, descriptor);
52 ASSERT(result.isNewEntry);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010053
Ben Murdoche69819b2013-07-17 14:56:49 +010054 UnresolvedDefinitionMap::iterator it = m_unresolvedDefinitions.find(descriptor);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010055 if (it == m_unresolvedDefinitions.end())
Ben Murdoche69819b2013-07-17 14:56:49 +010056 it = m_unresolvedDefinitions.add(descriptor, ElementSet()).iterator;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010057 it->value.add(element);
58}
59
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010060void CustomElementUpgradeCandidateMap::remove(Element* element)
61{
Ben Murdoch02772c62013-07-26 10:21:05 +010062 unregisterForElementDestructionNotification(element, this);
63
Ben Murdoch7757ec22013-07-23 11:17:36 +010064 UpgradeCandidateMap::iterator candidate = m_upgradeCandidates.find(element);
Ben Murdoch02772c62013-07-26 10:21:05 +010065 ASSERT(candidate != m_upgradeCandidates.end());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010066
Ben Murdoch7757ec22013-07-23 11:17:36 +010067 UnresolvedDefinitionMap::iterator elements = m_unresolvedDefinitions.find(candidate->value);
68 ASSERT(elements != m_unresolvedDefinitions.end());
69 elements->value.remove(element);
70 m_upgradeCandidates.remove(candidate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010071}
72
Ben Murdoche69819b2013-07-17 14:56:49 +010073ListHashSet<Element*> CustomElementUpgradeCandidateMap::takeUpgradeCandidatesFor(const CustomElementDescriptor& descriptor)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010074{
Ben Murdoche69819b2013-07-17 14:56:49 +010075 const ListHashSet<Element*>& candidates = m_unresolvedDefinitions.take(descriptor);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010076
Ben Murdoch02772c62013-07-26 10:21:05 +010077 for (ElementSet::const_iterator candidate = candidates.begin(); candidate != candidates.end(); ++candidate) {
78 unregisterForElementDestructionNotification(*candidate, this);
Ben Murdoche69819b2013-07-17 14:56:49 +010079 m_upgradeCandidates.remove(*candidate);
Ben Murdoch02772c62013-07-26 10:21:05 +010080 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010081
Ben Murdoche69819b2013-07-17 14:56:49 +010082 return candidates;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010083}
84
Ben Murdoch02772c62013-07-26 10:21:05 +010085void CustomElementUpgradeCandidateMap::elementWasDestroyed(Element* element)
86{
87 DestructionObserverMap::iterator it = destructionObservers().find(element);
88 if (it == destructionObservers().end())
89 return;
90 it->value->remove(element); // will also remove the destruction observer
91}
92
93CustomElementUpgradeCandidateMap::DestructionObserverMap& CustomElementUpgradeCandidateMap::destructionObservers()
94{
95 DEFINE_STATIC_LOCAL(DestructionObserverMap, map, ());
96 return map;
97}
98
99void CustomElementUpgradeCandidateMap::registerForElementDestructionNotification(Element* element, CustomElementUpgradeCandidateMap* observer)
100{
101 DestructionObserverMap::AddResult result = destructionObservers().add(element, observer);
102 ASSERT(result.isNewEntry);
103}
104
105void CustomElementUpgradeCandidateMap::unregisterForElementDestructionNotification(Element* element, CustomElementUpgradeCandidateMap* observer)
106{
107 CustomElementUpgradeCandidateMap* map = destructionObservers().take(element);
108 ASSERT(map == observer);
109}
110
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100111}