Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "bindings/core/v8/ScriptCustomElementDefinition.h" | 5 #include "bindings/core/v8/ScriptCustomElementDefinition.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptState.h" | 7 #include "bindings/core/v8/ScriptState.h" |
| 8 #include "bindings/core/v8/V8Binding.h" | 8 #include "bindings/core/v8/V8Binding.h" |
| 9 #include "bindings/core/v8/V8BindingMacros.h" | 9 #include "bindings/core/v8/V8BindingMacros.h" |
| 10 #include "bindings/core/v8/V8CustomElementRegistry.h" | 10 #include "bindings/core/v8/V8CustomElementRegistry.h" |
| 11 #include "bindings/core/v8/V8Element.h" | 11 #include "bindings/core/v8/V8Element.h" |
| 12 #include "bindings/core/v8/V8ErrorHandler.h" | 12 #include "bindings/core/v8/V8ErrorHandler.h" |
| 13 #include "bindings/core/v8/V8HiddenValue.h" | 13 #include "bindings/core/v8/V8HiddenValue.h" |
| 14 #include "bindings/core/v8/V8ScriptRunner.h" | 14 #include "bindings/core/v8/V8ScriptRunner.h" |
| 15 #include "bindings/core/v8/V8ThrowException.h" | 15 #include "bindings/core/v8/V8ThrowException.h" |
| 16 #include "core/dom/ExceptionCode.h" | 16 #include "core/dom/ExceptionCode.h" |
| 17 #include "core/dom/custom/CustomElement.h" | 17 #include "core/dom/custom/CustomElement.h" |
| 18 #include "core/events/ErrorEvent.h" | 18 #include "core/events/ErrorEvent.h" |
| 19 #include "core/html/HTMLElement.h" | 19 #include "core/html/HTMLElement.h" |
| 20 #include "core/html/imports/HTMLImportsController.h" | |
| 20 #include "v8.h" | 21 #include "v8.h" |
| 21 #include "wtf/Allocator.h" | 22 #include "wtf/Allocator.h" |
| 22 | 23 |
| 23 namespace blink { | 24 namespace blink { |
| 24 | 25 |
| 25 // Retrieves the custom elements constructor -> name map, creating it | 26 // Retrieves the custom elements constructor -> name map, creating it |
| 26 // if necessary. | 27 // if necessary. |
| 27 static v8::Local<v8::Map> ensureCustomElementRegistryMap( | 28 static v8::Local<v8::Map> ensureCustomElementRegistryMap( |
| 28 ScriptState* scriptState, | 29 ScriptState* scriptState, |
| 29 CustomElementRegistry* registry) { | 30 CustomElementRegistry* registry) { |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 178 return CustomElement::createFailedElement(document, tagName); | 179 return CustomElement::createFailedElement(document, tagName); |
| 179 ScriptState::Scope scope(m_scriptState.get()); | 180 ScriptState::Scope scope(m_scriptState.get()); |
| 180 v8::Isolate* isolate = m_scriptState->isolate(); | 181 v8::Isolate* isolate = m_scriptState->isolate(); |
| 181 | 182 |
| 182 ExceptionState exceptionState(isolate, ExceptionState::ConstructionContext, | 183 ExceptionState exceptionState(isolate, ExceptionState::ConstructionContext, |
| 183 "CustomElement"); | 184 "CustomElement"); |
| 184 | 185 |
| 185 // Create an element with the synchronous custom elements flag set. | 186 // Create an element with the synchronous custom elements flag set. |
| 186 // https://dom.spec.whatwg.org/#concept-create-element | 187 // https://dom.spec.whatwg.org/#concept-create-element |
| 187 | 188 |
| 188 // Create an element and push to the construction stack. | 189 // TODO(dominicc): Implement step 5 which constructs customized |
| 189 // V8HTMLElement::constructorCustom() can only refer to | 190 // built-in elements. |
| 190 // window.document(), but it is different from the document here | 191 |
| 191 // when it is an import document. This is not exactly what the | 192 Element* element = nullptr; |
| 192 // spec defines, but the non-imports behavior matches to the spec. | |
| 193 Element* element = createElementForConstructor(document); | |
| 194 { | 193 { |
| 195 ConstructionStackScope constructionStackScope(this, element); | |
| 196 v8::TryCatch tryCatch(m_scriptState->isolate()); | 194 v8::TryCatch tryCatch(m_scriptState->isolate()); |
| 197 element = runConstructor(); | 195 |
| 196 bool isImportDocument = document.importsController() && | |
| 197 document.importsController()->master() != document; | |
| 198 if (isImportDocument) { | |
| 199 // V8HTMLElement::constructorCustom() can only refer to | |
| 200 // window.document() which is not the import document. Create | |
| 201 // elements in import documents ahead of time so they end up in | |
| 202 // the right document. This subtly violates recursive | |
| 203 // construction semantics, but only in import documents. | |
| 204 element = createElementForConstructor(document); | |
| 205 DCHECK(!tryCatch.HasCaught()); | |
| 206 { | |
|
haraken
2016/12/21 07:43:57
This clause looks redundant.
| |
| 207 ConstructionStackScope constructionStackScope(this, element); | |
| 208 element = callConstructor(); | |
| 209 } | |
| 210 } else { | |
| 211 element = callConstructor(); | |
| 212 } | |
| 213 | |
| 198 if (tryCatch.HasCaught()) { | 214 if (tryCatch.HasCaught()) { |
| 199 exceptionState.rethrowV8Exception(tryCatch.Exception()); | 215 exceptionState.rethrowV8Exception(tryCatch.Exception()); |
| 200 return handleCreateElementSyncException(document, tagName, isolate, | 216 return handleCreateElementSyncException(document, tagName, isolate, |
| 201 exceptionState); | 217 exceptionState); |
| 202 } | 218 } |
| 203 } | 219 } |
| 204 | 220 |
| 205 // 6.1.3. through 6.1.9. | 221 // 6.1.3. through 6.1.9. |
| 206 checkConstructorResult(element, document, tagName, exceptionState); | 222 checkConstructorResult(element, document, tagName, exceptionState); |
| 207 if (exceptionState.hadException()) { | 223 if (exceptionState.hadException()) { |
| 208 return handleCreateElementSyncException(document, tagName, isolate, | 224 return handleCreateElementSyncException(document, tagName, isolate, |
| 209 exceptionState); | 225 exceptionState); |
| 210 } | 226 } |
| 211 DCHECK_EQ(element->getCustomElementState(), CustomElementState::Custom); | 227 DCHECK_EQ(element->getCustomElementState(), CustomElementState::Custom); |
| 212 return toHTMLElement(element); | 228 return toHTMLElement(element); |
| 213 } | 229 } |
| 214 | 230 |
| 215 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades | 231 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades |
| 216 bool ScriptCustomElementDefinition::runConstructor(Element* element) { | 232 bool ScriptCustomElementDefinition::runConstructor(Element* element) { |
| 217 if (!m_scriptState->contextIsValid()) | 233 if (!m_scriptState->contextIsValid()) |
| 218 return false; | 234 return false; |
| 219 ScriptState::Scope scope(m_scriptState.get()); | 235 ScriptState::Scope scope(m_scriptState.get()); |
| 220 v8::Isolate* isolate = m_scriptState->isolate(); | 236 v8::Isolate* isolate = m_scriptState->isolate(); |
| 221 | 237 |
| 222 // Step 5 says to rethrow the exception; but there is no one to | 238 // Step 5 says to rethrow the exception; but there is no one to |
| 223 // catch it. The side effect is to report the error. | 239 // catch it. The side effect is to report the error. |
| 224 v8::TryCatch tryCatch(isolate); | 240 v8::TryCatch tryCatch(isolate); |
| 225 tryCatch.SetVerbose(true); | 241 tryCatch.SetVerbose(true); |
| 226 | 242 |
| 227 Element* result = runConstructor(); | 243 Element* result = callConstructor(); |
| 228 | 244 |
| 229 // To report exception thrown from runConstructor() | 245 // To report exception thrown from callConstructor() |
| 230 if (tryCatch.HasCaught()) | 246 if (tryCatch.HasCaught()) |
| 231 return false; | 247 return false; |
| 232 | 248 |
| 233 // To report InvalidStateError Exception, when the constructor returns some | 249 // To report InvalidStateError Exception, when the constructor returns some |
| 234 // different object | 250 // different object |
| 235 if (result != element) { | 251 if (result != element) { |
| 236 const String& message = | 252 const String& message = |
| 237 "custom element constructors must call super() first and must " | 253 "custom element constructors must call super() first and must " |
| 238 "not return a different object"; | 254 "not return a different object"; |
| 239 v8::Local<v8::Value> exception = V8ThrowException::createDOMException( | 255 v8::Local<v8::Value> exception = V8ThrowException::createDOMException( |
| 240 m_scriptState->isolate(), InvalidStateError, message); | 256 m_scriptState->isolate(), InvalidStateError, message); |
| 241 dispatchErrorEvent(isolate, exception, constructor()); | 257 dispatchErrorEvent(isolate, exception, constructor()); |
| 242 return false; | 258 return false; |
| 243 } | 259 } |
| 244 | 260 |
| 245 return true; | 261 return true; |
| 246 } | 262 } |
| 247 | 263 |
| 248 Element* ScriptCustomElementDefinition::runConstructor() { | 264 Element* ScriptCustomElementDefinition::callConstructor() { |
| 249 v8::Isolate* isolate = m_scriptState->isolate(); | 265 v8::Isolate* isolate = m_scriptState->isolate(); |
| 250 DCHECK(ScriptState::current(isolate) == m_scriptState); | 266 DCHECK(ScriptState::current(isolate) == m_scriptState); |
| 251 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); | 267 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); |
| 252 v8::Local<v8::Value> result; | 268 v8::Local<v8::Value> result; |
| 253 if (!v8Call(V8ScriptRunner::callAsConstructor(isolate, constructor(), | 269 if (!v8Call(V8ScriptRunner::callAsConstructor(isolate, constructor(), |
| 254 executionContext, 0, nullptr), | 270 executionContext, 0, nullptr), |
| 255 result)) { | 271 result)) { |
| 256 return nullptr; | 272 return nullptr; |
| 257 } | 273 } |
| 258 return V8Element::toImplWithTypeCheck(isolate, result); | 274 return V8Element::toImplWithTypeCheck(isolate, result); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 v8::Local<v8::Value> argv[] = { | 359 v8::Local<v8::Value> argv[] = { |
| 344 v8String(isolate, name.localName()), v8StringOrNull(isolate, oldValue), | 360 v8String(isolate, name.localName()), v8StringOrNull(isolate, oldValue), |
| 345 v8StringOrNull(isolate, newValue), | 361 v8StringOrNull(isolate, newValue), |
| 346 v8StringOrNull(isolate, name.namespaceURI()), | 362 v8StringOrNull(isolate, name.namespaceURI()), |
| 347 }; | 363 }; |
| 348 runCallback(m_attributeChangedCallback.newLocal(isolate), element, | 364 runCallback(m_attributeChangedCallback.newLocal(isolate), element, |
| 349 WTF_ARRAY_LENGTH(argv), argv); | 365 WTF_ARRAY_LENGTH(argv), argv); |
| 350 } | 366 } |
| 351 | 367 |
| 352 } // namespace blink | 368 } // namespace blink |
| OLD | NEW |