Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 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" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 32 | #include "core/dom/CustomElementUpgradeCandidateMap.h" |
| 33 | |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 34 | #include "core/dom/Element.h" |
| 35 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 36 | namespace WebCore { |
| 37 | |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 38 | CustomElementUpgradeCandidateMap::~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 Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 45 | void CustomElementUpgradeCandidateMap::add(const CustomElementDescriptor& descriptor, Element* element) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 46 | { |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 47 | 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) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 53 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 54 | UnresolvedDefinitionMap::iterator it = m_unresolvedDefinitions.find(descriptor); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 55 | if (it == m_unresolvedDefinitions.end()) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 56 | it = m_unresolvedDefinitions.add(descriptor, ElementSet()).iterator; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 57 | it->value.add(element); |
| 58 | } |
| 59 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 60 | void CustomElementUpgradeCandidateMap::remove(Element* element) |
| 61 | { |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 62 | unregisterForElementDestructionNotification(element, this); |
| 63 | |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 64 | UpgradeCandidateMap::iterator candidate = m_upgradeCandidates.find(element); |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 65 | ASSERT(candidate != m_upgradeCandidates.end()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 66 | |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 67 | 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) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 73 | ListHashSet<Element*> CustomElementUpgradeCandidateMap::takeUpgradeCandidatesFor(const CustomElementDescriptor& descriptor) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 74 | { |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 75 | const ListHashSet<Element*>& candidates = m_unresolvedDefinitions.take(descriptor); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 76 | |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 77 | for (ElementSet::const_iterator candidate = candidates.begin(); candidate != candidates.end(); ++candidate) { |
| 78 | unregisterForElementDestructionNotification(*candidate, this); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 79 | m_upgradeCandidates.remove(*candidate); |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 80 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 81 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 82 | return candidates; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 85 | void 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 | |
| 93 | CustomElementUpgradeCandidateMap::DestructionObserverMap& CustomElementUpgradeCandidateMap::destructionObservers() |
| 94 | { |
| 95 | DEFINE_STATIC_LOCAL(DestructionObserverMap, map, ()); |
| 96 | return map; |
| 97 | } |
| 98 | |
| 99 | void CustomElementUpgradeCandidateMap::registerForElementDestructionNotification(Element* element, CustomElementUpgradeCandidateMap* observer) |
| 100 | { |
| 101 | DestructionObserverMap::AddResult result = destructionObservers().add(element, observer); |
| 102 | ASSERT(result.isNewEntry); |
| 103 | } |
| 104 | |
| 105 | void CustomElementUpgradeCandidateMap::unregisterForElementDestructionNotification(Element* element, CustomElementUpgradeCandidateMap* observer) |
| 106 | { |
| 107 | CustomElementUpgradeCandidateMap* map = destructionObservers().take(element); |
| 108 | ASSERT(map == observer); |
| 109 | } |
| 110 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 111 | } |