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 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * 3. Neither the name of Google Inc. nor the names of its contributors 15 * may be used to endorse or promote products derived from this 16 * 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#ifndef CustomElementRegistry_h 32#define CustomElementRegistry_h 33 34#if ENABLE(CUSTOM_ELEMENTS) 35 36#include "ContextDestructionObserver.h" 37#include "ExceptionCode.h" 38#include "QualifiedName.h" 39#include "ScriptValue.h" 40#include "Supplementable.h" 41#include <wtf/HashSet.h> 42#include <wtf/ListHashSet.h> 43#include <wtf/PassRefPtr.h> 44#include <wtf/RefCounted.h> 45#include <wtf/RefPtr.h> 46#include <wtf/Vector.h> 47#include <wtf/text/AtomicStringHash.h> 48 49namespace WebCore { 50 51class CustomElementConstructor; 52class Dictionary; 53class Document; 54class Element; 55class ScriptExecutionContext; 56class QualifiedName; 57 58class CustomElementInvocation { 59public: 60 explicit CustomElementInvocation(PassRefPtr<Element>); 61 ~CustomElementInvocation(); 62 63 Element* element() const { return m_element.get(); } 64 65private: 66 RefPtr<Element> m_element; 67}; 68 69class CustomElementRegistry : public RefCounted<CustomElementRegistry> , public ContextDestructionObserver { 70 WTF_MAKE_NONCOPYABLE(CustomElementRegistry); WTF_MAKE_FAST_ALLOCATED; 71public: 72 class CallbackDeliveryScope { 73 public: 74 CallbackDeliveryScope() { } 75 ~CallbackDeliveryScope() { CustomElementRegistry::deliverAllLifecycleCallbacksIfNeeded(); } 76 }; 77 78 explicit CustomElementRegistry(Document*); 79 ~CustomElementRegistry(); 80 81 PassRefPtr<CustomElementConstructor> registerElement(WebCore::ScriptState*, const AtomicString& name, const Dictionary& options, ExceptionCode&); 82 PassRefPtr<CustomElementConstructor> findFor(Element*) const; 83 PassRefPtr<CustomElementConstructor> find(const QualifiedName& elementName, const QualifiedName& localName) const; 84 PassRefPtr<Element> createElement(const QualifiedName& localName, const AtomicString& typeExtension) const; 85 86 Document* document() const; 87 88 void didGiveTypeExtension(Element*); 89 void didCreateElement(Element*); 90 91 static void deliverAllLifecycleCallbacks(); 92 static void deliverAllLifecycleCallbacksIfNeeded(); 93 94private: 95 typedef HashMap<std::pair<QualifiedName, QualifiedName>, RefPtr<CustomElementConstructor> > ConstructorMap; 96 typedef HashSet<AtomicString> NameSet; 97 typedef ListHashSet<CustomElementRegistry*> InstanceSet; 98 99 static bool isValidName(const AtomicString&); 100 static InstanceSet& activeCustomElementRegistries(); 101 102 void activate(const CustomElementInvocation&); 103 void deactivate(); 104 void deliverLifecycleCallbacks(); 105 106 ConstructorMap m_constructors; 107 NameSet m_names; 108 Vector<CustomElementInvocation> m_invocations; 109}; 110 111inline void CustomElementRegistry::deliverAllLifecycleCallbacksIfNeeded() 112{ 113 if (!activeCustomElementRegistries().isEmpty()) 114 deliverAllLifecycleCallbacks(); 115 ASSERT(activeCustomElementRegistries().isEmpty()); 116} 117 118inline CustomElementRegistry::InstanceSet& CustomElementRegistry::activeCustomElementRegistries() 119{ 120 DEFINE_STATIC_LOCAL(InstanceSet, activeInstances, ()); 121 return activeInstances; 122} 123 124 125} // namespace WebCore 126 127#endif // ENABLE(CUSTOM_ELEMENTS) 128#endif 129