blob: 9d9195cef5a8d291e9a9ed746e6362ef75a88a9b [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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#include "config.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010032#include "core/dom/CustomElementRegistry.h"
33
34#include "HTMLNames.h"
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010035#include "SVGNames.h"
Ben Murdoch591b9582013-07-10 11:41:44 +010036#include "bindings/v8/CustomElementConstructorBuilder.h"
Ben Murdochdf957042013-08-06 11:01:27 +010037#include "bindings/v8/ExceptionState.h"
Ben Murdoch1fad5ca2013-08-07 11:05:11 +010038#include "core/dom/CustomElement.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010039#include "core/dom/CustomElementDefinition.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010040#include "core/dom/CustomElementRegistrationContext.h"
41#include "core/dom/DocumentLifecycleObserver.h"
Ben Murdochdf957042013-08-06 11:01:27 +010042#include "core/dom/ExceptionCode.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010043
44namespace WebCore {
45
Ben Murdoche69819b2013-07-17 14:56:49 +010046class RegistrationContextObserver : public DocumentLifecycleObserver {
47public:
48 explicit RegistrationContextObserver(Document* document)
49 : DocumentLifecycleObserver(document)
50 , m_wentAway(!document)
51 {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010052 }
53
Ben Murdoche69819b2013-07-17 14:56:49 +010054 bool registrationContextWentAway() { return m_wentAway; }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010055
Ben Murdoche69819b2013-07-17 14:56:49 +010056private:
57 virtual void documentWasDisposed() OVERRIDE { m_wentAway = true; }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010058
Ben Murdoche69819b2013-07-17 14:56:49 +010059 bool m_wentAway;
60};
61
Ben Murdochdf957042013-08-06 11:01:27 +010062CustomElementDefinition* CustomElementRegistry::registerElement(Document* document, CustomElementConstructorBuilder* constructorBuilder, const AtomicString& userSuppliedName, ExceptionState& es)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010063{
Ben Murdoche69819b2013-07-17 14:56:49 +010064 // FIXME: In every instance except one it is the
65 // CustomElementConstructorBuilder that observes document
66 // destruction during registration. This responsibility should be
67 // consolidated in one place.
68 RegistrationContextObserver observer(document);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010069
Ben Murdochfff88842013-07-30 15:20:09 +010070 if (!constructorBuilder->isFeatureAllowed()) {
Ben Murdochdf957042013-08-06 11:01:27 +010071 es.throwDOMException(NotSupportedError);
Ben Murdoche69819b2013-07-17 14:56:49 +010072 return 0;
Ben Murdochfff88842013-07-30 15:20:09 +010073 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010074
Ben Murdoch591b9582013-07-10 11:41:44 +010075 AtomicString type = userSuppliedName.lower();
Ben Murdoch1fad5ca2013-08-07 11:05:11 +010076 if (!CustomElement::isValidTypeName(type)) {
Ben Murdochdf957042013-08-06 11:01:27 +010077 es.throwDOMException(InvalidCharacterError);
Ben Murdoche69819b2013-07-17 14:56:49 +010078 return 0;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010079 }
80
Ben Murdoch591b9582013-07-10 11:41:44 +010081 if (!constructorBuilder->validateOptions()) {
Ben Murdochdf957042013-08-06 11:01:27 +010082 es.throwDOMException(InvalidStateError);
Ben Murdoche69819b2013-07-17 14:56:49 +010083 return 0;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010084 }
85
Ben Murdoch591b9582013-07-10 11:41:44 +010086 QualifiedName tagName = nullQName();
87 if (!constructorBuilder->findTagName(type, tagName)) {
Ben Murdochdf957042013-08-06 11:01:27 +010088 es.throwDOMException(NamespaceError);
Ben Murdoche69819b2013-07-17 14:56:49 +010089 return 0;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010090 }
Ben Murdoch591b9582013-07-10 11:41:44 +010091 ASSERT(tagName.namespaceURI() == HTMLNames::xhtmlNamespaceURI || tagName.namespaceURI() == SVGNames::svgNamespaceURI);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010092
Ben Murdoche69819b2013-07-17 14:56:49 +010093 if (m_registeredTypeNames.contains(type)) {
Ben Murdochdf957042013-08-06 11:01:27 +010094 es.throwDOMException(InvalidStateError);
Ben Murdoche69819b2013-07-17 14:56:49 +010095 return 0;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010096 }
97
Ben Murdoche69819b2013-07-17 14:56:49 +010098 ASSERT(!observer.registrationContextWentAway());
99
Ben Murdoch02772c62013-07-26 10:21:05 +0100100 RefPtr<CustomElementLifecycleCallbacks> lifecycleCallbacks = constructorBuilder->createCallbacks();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100101
Ben Murdoch591b9582013-07-10 11:41:44 +0100102 // Consulting the constructor builder could execute script and
103 // kill the document.
Ben Murdoche69819b2013-07-17 14:56:49 +0100104 if (observer.registrationContextWentAway()) {
Ben Murdochdf957042013-08-06 11:01:27 +0100105 es.throwDOMException(InvalidStateError);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100106 return 0;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100107 }
108
Ben Murdoche69819b2013-07-17 14:56:49 +0100109 const CustomElementDescriptor descriptor(type, tagName.namespaceURI(), tagName.localName());
110 RefPtr<CustomElementDefinition> definition = CustomElementDefinition::create(descriptor, lifecycleCallbacks);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100111
Ben Murdoche69819b2013-07-17 14:56:49 +0100112 if (!constructorBuilder->createConstructor(document, definition.get())) {
Ben Murdochdf957042013-08-06 11:01:27 +0100113 es.throwDOMException(NotSupportedError);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100114 return 0;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100115 }
116
Ben Murdoche69819b2013-07-17 14:56:49 +0100117 m_definitions.add(descriptor, definition);
118 m_registeredTypeNames.add(descriptor.type());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100119
Ben Murdoche69819b2013-07-17 14:56:49 +0100120 if (!constructorBuilder->didRegisterDefinition(definition.get())) {
Ben Murdochdf957042013-08-06 11:01:27 +0100121 es.throwDOMException(NotSupportedError);
Ben Murdoche69819b2013-07-17 14:56:49 +0100122 return 0;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100123 }
Ben Murdoche69819b2013-07-17 14:56:49 +0100124
125 return definition.get();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100126}
127
Ben Murdoche69819b2013-07-17 14:56:49 +0100128CustomElementDefinition* CustomElementRegistry::find(const CustomElementDescriptor& descriptor) const
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100129{
Ben Murdoche69819b2013-07-17 14:56:49 +0100130 return m_definitions.get(descriptor);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100131}
132
Ben Murdoche69819b2013-07-17 14:56:49 +0100133} // namespace WebCore