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 "bindings/core/v8/CustomElementConstructorBuilder.h" 33 34 #include "bindings/core/v8/CustomElementBinding.h" 35 #include "bindings/core/v8/DOMWrapperWorld.h" 36 #include "bindings/core/v8/Dictionary.h" 37 #include "bindings/core/v8/ExceptionState.h" 38 #include "bindings/core/v8/V8Binding.h" 39 #include "bindings/core/v8/V8Document.h" 40 #include "bindings/core/v8/V8HTMLElement.h" 41 #include "bindings/core/v8/V8HiddenValue.h" 42 #include "bindings/core/v8/V8PerContextData.h" 43 #include "bindings/core/v8/V8SVGElement.h" 44 #include "core/HTMLNames.h" 45 #include "core/SVGNames.h" 46 #include "core/dom/Document.h" 47 #include "core/dom/custom/CustomElementDefinition.h" 48 #include "core/dom/custom/CustomElementDescriptor.h" 49 #include "core/dom/custom/CustomElementException.h" 50 #include "core/dom/custom/CustomElementProcessingStack.h" 51 #include "wtf/Assertions.h" 52 53 namespace blink { 54 55 static void constructCustomElement(const v8::FunctionCallbackInfo<v8::Value>&); 56 57 CustomElementConstructorBuilder::CustomElementConstructorBuilder(ScriptState* scriptState, const Dictionary* options) 58 : m_scriptState(scriptState) 59 , m_options(options) 60 { 61 ASSERT(m_scriptState->context() == m_scriptState->isolate()->GetCurrentContext()); 62 } 63 64 bool CustomElementConstructorBuilder::isFeatureAllowed() const 65 { 66 return m_scriptState->world().isMainWorld(); 67 } 68 69 bool CustomElementConstructorBuilder::validateOptions(const AtomicString& type, QualifiedName& tagName, ExceptionState& exceptionState) 70 { 71 ASSERT(m_prototype.IsEmpty()); 72 73 v8::TryCatch tryCatch; 74 75 ScriptValue prototypeScriptValue; 76 if (DictionaryHelper::get(*m_options, "prototype", prototypeScriptValue) && !prototypeScriptValue.isNull()) { 77 ASSERT(!tryCatch.HasCaught()); 78 if (!prototypeScriptValue.isObject()) { 79 CustomElementException::throwException(CustomElementException::PrototypeNotAnObject, type, exceptionState); 80 tryCatch.ReThrow(); 81 return false; 82 } 83 m_prototype = prototypeScriptValue.v8Value().As<v8::Object>(); 84 } else if (!tryCatch.HasCaught()) { 85 m_prototype = v8::Object::New(m_scriptState->isolate()); 86 v8::Local<v8::Object> basePrototype = m_scriptState->perContextData()->prototypeForType(&V8HTMLElement::wrapperTypeInfo); 87 if (!basePrototype.IsEmpty()) 88 m_prototype->SetPrototype(basePrototype); 89 } 90 91 if (tryCatch.HasCaught()) { 92 tryCatch.ReThrow(); 93 return false; 94 } 95 96 AtomicString extends; 97 bool extendsProvidedAndNonNull = DictionaryHelper::get(*m_options, "extends", extends) && extends != "null"; 98 99 if (tryCatch.HasCaught()) { 100 tryCatch.ReThrow(); 101 return false; 102 } 103 104 if (!m_scriptState->perContextData()) { 105 // FIXME: This should generate an InvalidContext exception at a later point. 106 CustomElementException::throwException(CustomElementException::ContextDestroyedCheckingPrototype, type, exceptionState); 107 tryCatch.ReThrow(); 108 return false; 109 } 110 111 AtomicString namespaceURI = HTMLNames::xhtmlNamespaceURI; 112 if (hasValidPrototypeChainFor(&V8SVGElement::wrapperTypeInfo)) 113 namespaceURI = SVGNames::svgNamespaceURI; 114 115 ASSERT(!tryCatch.HasCaught()); 116 117 AtomicString localName; 118 119 if (extendsProvidedAndNonNull) { 120 localName = extends.lower(); 121 122 if (!Document::isValidName(localName)) { 123 CustomElementException::throwException(CustomElementException::ExtendsIsInvalidName, type, exceptionState); 124 tryCatch.ReThrow(); 125 return false; 126 } 127 if (CustomElement::isValidName(localName)) { 128 CustomElementException::throwException(CustomElementException::ExtendsIsCustomElementName, type, exceptionState); 129 tryCatch.ReThrow(); 130 return false; 131 } 132 } else { 133 if (namespaceURI == SVGNames::svgNamespaceURI) { 134 CustomElementException::throwException(CustomElementException::ExtendsIsInvalidName, type, exceptionState); 135 tryCatch.ReThrow(); 136 return false; 137 } 138 localName = type; 139 } 140 141 ASSERT(!tryCatch.HasCaught()); 142 tagName = QualifiedName(nullAtom, localName, namespaceURI); 143 return true; 144 } 145 146 PassRefPtr<CustomElementLifecycleCallbacks> CustomElementConstructorBuilder::createCallbacks() 147 { 148 ASSERT(!m_prototype.IsEmpty()); 149 150 v8::TryCatch exceptionCatcher; 151 exceptionCatcher.SetVerbose(true); 152 153 v8::Isolate* isolate = m_scriptState->isolate(); 154 v8::Handle<v8::Function> created = retrieveCallback(isolate, "createdCallback"); 155 v8::Handle<v8::Function> attached = retrieveCallback(isolate, "attachedCallback"); 156 v8::Handle<v8::Function> detached = retrieveCallback(isolate, "detachedCallback"); 157 v8::Handle<v8::Function> attributeChanged = retrieveCallback(isolate, "attributeChangedCallback"); 158 159 m_callbacks = V8CustomElementLifecycleCallbacks::create(m_scriptState.get(), m_prototype, created, attached, detached, attributeChanged); 160 return m_callbacks.get(); 161 } 162 163 v8::Handle<v8::Function> CustomElementConstructorBuilder::retrieveCallback(v8::Isolate* isolate, const char* name) 164 { 165 v8::Handle<v8::Value> value = m_prototype->Get(v8String(isolate, name)); 166 if (value.IsEmpty() || !value->IsFunction()) 167 return v8::Handle<v8::Function>(); 168 return value.As<v8::Function>(); 169 } 170 171 bool CustomElementConstructorBuilder::createConstructor(Document* document, CustomElementDefinition* definition, ExceptionState& exceptionState) 172 { 173 ASSERT(!m_prototype.IsEmpty()); 174 ASSERT(m_constructor.IsEmpty()); 175 ASSERT(document); 176 177 v8::Isolate* isolate = m_scriptState->isolate(); 178 179 if (!prototypeIsValid(definition->descriptor().type(), exceptionState)) 180 return false; 181 182 v8::Local<v8::FunctionTemplate> constructorTemplate = v8::FunctionTemplate::New(isolate); 183 constructorTemplate->SetCallHandler(constructCustomElement); 184 m_constructor = constructorTemplate->GetFunction(); 185 if (m_constructor.IsEmpty()) { 186 CustomElementException::throwException(CustomElementException::ContextDestroyedRegisteringDefinition, definition->descriptor().type(), exceptionState); 187 return false; 188 } 189 190 const CustomElementDescriptor& descriptor = definition->descriptor(); 191 192 v8::Handle<v8::String> v8TagName = v8String(isolate, descriptor.localName()); 193 v8::Handle<v8::Value> v8Type; 194 if (descriptor.isTypeExtension()) 195 v8Type = v8String(isolate, descriptor.type()); 196 else 197 v8Type = v8::Null(isolate); 198 199 m_constructor->SetName(v8Type->IsNull() ? v8TagName : v8Type.As<v8::String>()); 200 201 V8HiddenValue::setHiddenValue(isolate, m_constructor, V8HiddenValue::customElementDocument(isolate), toV8(document, m_scriptState->context()->Global(), isolate)); 202 V8HiddenValue::setHiddenValue(isolate, m_constructor, V8HiddenValue::customElementNamespaceURI(isolate), v8String(isolate, descriptor.namespaceURI())); 203 V8HiddenValue::setHiddenValue(isolate, m_constructor, V8HiddenValue::customElementTagName(isolate), v8TagName); 204 V8HiddenValue::setHiddenValue(isolate, m_constructor, V8HiddenValue::customElementType(isolate), v8Type); 205 206 v8::Handle<v8::String> prototypeKey = v8String(isolate, "prototype"); 207 ASSERT(m_constructor->HasOwnProperty(prototypeKey)); 208 // This sets the property *value*; calling Set is safe because 209 // "prototype" is a non-configurable data property so there can be 210 // no side effects. 211 m_constructor->Set(prototypeKey, m_prototype); 212 // This *configures* the property. ForceSet of a function's 213 // "prototype" does not affect the value, but can reconfigure the 214 // property. 215 m_constructor->ForceSet(prototypeKey, m_prototype, v8::PropertyAttribute(v8::ReadOnly | v8::DontEnum | v8::DontDelete)); 216 217 V8HiddenValue::setHiddenValue(isolate, m_prototype, V8HiddenValue::customElementIsInterfacePrototypeObject(isolate), v8::True(isolate)); 218 m_prototype->ForceSet(v8String(isolate, "constructor"), m_constructor, v8::DontEnum); 219 220 return true; 221 } 222 223 bool CustomElementConstructorBuilder::prototypeIsValid(const AtomicString& type, ExceptionState& exceptionState) const 224 { 225 if (m_prototype->InternalFieldCount() || !V8HiddenValue::getHiddenValue(m_scriptState->isolate(), m_prototype, V8HiddenValue::customElementIsInterfacePrototypeObject(m_scriptState->isolate())).IsEmpty()) { 226 CustomElementException::throwException(CustomElementException::PrototypeInUse, type, exceptionState); 227 return false; 228 } 229 230 if (m_prototype->GetPropertyAttributes(v8String(m_scriptState->isolate(), "constructor")) & v8::DontDelete) { 231 CustomElementException::throwException(CustomElementException::ConstructorPropertyNotConfigurable, type, exceptionState); 232 return false; 233 } 234 235 return true; 236 } 237 238 bool CustomElementConstructorBuilder::didRegisterDefinition(CustomElementDefinition* definition) const 239 { 240 ASSERT(!m_constructor.IsEmpty()); 241 242 return m_callbacks->setBinding(definition, CustomElementBinding::create(m_scriptState->isolate(), m_prototype)); 243 } 244 245 ScriptValue CustomElementConstructorBuilder::bindingsReturnValue() const 246 { 247 return ScriptValue(m_scriptState.get(), m_constructor); 248 } 249 250 bool CustomElementConstructorBuilder::hasValidPrototypeChainFor(const WrapperTypeInfo* type) const 251 { 252 v8::Handle<v8::Object> elementPrototype = m_scriptState->perContextData()->prototypeForType(type); 253 if (elementPrototype.IsEmpty()) 254 return false; 255 256 v8::Handle<v8::Value> chain = m_prototype; 257 while (!chain.IsEmpty() && chain->IsObject()) { 258 if (chain == elementPrototype) 259 return true; 260 chain = chain.As<v8::Object>()->GetPrototype(); 261 } 262 263 return false; 264 } 265 266 static void constructCustomElement(const v8::FunctionCallbackInfo<v8::Value>& info) 267 { 268 v8::Isolate* isolate = info.GetIsolate(); 269 270 if (!info.IsConstructCall()) { 271 V8ThrowException::throwTypeError("DOM object constructor cannot be called as a function.", isolate); 272 return; 273 } 274 275 if (info.Length() > 0) { 276 V8ThrowException::throwTypeError("This constructor should be called without arguments.", isolate); 277 return; 278 } 279 280 Document* document = V8Document::toImpl(V8HiddenValue::getHiddenValue(info.GetIsolate(), info.Callee(), V8HiddenValue::customElementDocument(isolate)).As<v8::Object>()); 281 TOSTRING_VOID(V8StringResource<>, namespaceURI, V8HiddenValue::getHiddenValue(isolate, info.Callee(), V8HiddenValue::customElementNamespaceURI(isolate))); 282 TOSTRING_VOID(V8StringResource<>, tagName, V8HiddenValue::getHiddenValue(isolate, info.Callee(), V8HiddenValue::customElementTagName(isolate))); 283 v8::Handle<v8::Value> maybeType = V8HiddenValue::getHiddenValue(info.GetIsolate(), info.Callee(), V8HiddenValue::customElementType(isolate)); 284 TOSTRING_VOID(V8StringResource<>, type, maybeType); 285 286 ExceptionState exceptionState(ExceptionState::ConstructionContext, "CustomElement", info.Holder(), info.GetIsolate()); 287 CustomElementProcessingStack::CallbackDeliveryScope deliveryScope; 288 RefPtrWillBeRawPtr<Element> element = document->createElementNS(namespaceURI, tagName, maybeType->IsNull() ? nullAtom : type, exceptionState); 289 if (exceptionState.throwIfNeeded()) 290 return; 291 v8SetReturnValueFast(info, element.release(), document); 292 } 293 294 } // namespace blink 295