| 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/V8CustomElementsRegistry.h" | 10 #include "bindings/core/v8/V8CustomElementsRegistry.h" |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 ScriptState::Scope scope(m_scriptState.get()); | 195 ScriptState::Scope scope(m_scriptState.get()); |
| 196 v8::Isolate* isolate = m_scriptState->isolate(); | 196 v8::Isolate* isolate = m_scriptState->isolate(); |
| 197 | 197 |
| 198 // When invoked from "create an element for a token": | 198 // When invoked from "create an element for a token": |
| 199 // https://html.spec.whatwg.org/multipage/syntax.html#create-an-element-for-
the-token | 199 // https://html.spec.whatwg.org/multipage/syntax.html#create-an-element-for-
the-token |
| 200 | 200 |
| 201 ExceptionState exceptionState(ExceptionState::ConstructionContext, | 201 ExceptionState exceptionState(ExceptionState::ConstructionContext, |
| 202 "CustomElement", constructor(), isolate); | 202 "CustomElement", constructor(), isolate); |
| 203 HTMLElement* element = createElementSync(document, tagName, exceptionState); | 203 HTMLElement* element = createElementSync(document, tagName, exceptionState); |
| 204 | 204 |
| 205 if (exceptionState.hadException() || !element) { | 205 if (exceptionState.hadException()) { |
| 206 DCHECK(!element); |
| 206 // 7. If this step throws an exception, then report the exception, ... | 207 // 7. If this step throws an exception, then report the exception, ... |
| 207 { | 208 exceptionState.reportException(m_scriptState.get(), |
| 208 v8::TryCatch tryCatch(isolate); | 209 constructor().As<v8::Function>()); |
| 209 tryCatch.SetVerbose(true); | |
| 210 exceptionState.throwIfNeeded(); | |
| 211 } | |
| 212 | |
| 213 return CustomElement::createFailedElement(document, tagName); | 210 return CustomElement::createFailedElement(document, tagName); |
| 214 } | 211 } |
| 212 DCHECK(element); |
| 215 return element; | 213 return element; |
| 216 } | 214 } |
| 217 | 215 |
| 218 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades | 216 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades |
| 219 bool ScriptCustomElementDefinition::runConstructor(Element* element) | 217 bool ScriptCustomElementDefinition::runConstructor(Element* element) |
| 220 { | 218 { |
| 221 if (!m_scriptState->contextIsValid()) | 219 if (!m_scriptState->contextIsValid()) |
| 222 return false; | 220 return false; |
| 223 ScriptState::Scope scope(m_scriptState.get()); | 221 ScriptState::Scope scope(m_scriptState.get()); |
| 224 v8::Isolate* isolate = m_scriptState->isolate(); | 222 v8::Isolate* isolate = m_scriptState->isolate(); |
| 225 | 223 |
| 226 // Step 5 says to rethrow the exception; but there is no one to | 224 // Step 5 says to rethrow the exception; but there is no one to |
| 227 // catch it. The side effect is to report the error. | 225 // catch it. The side effect is to report the error. |
| 228 v8::TryCatch tryCatch(isolate); | 226 v8::TryCatch tryCatch(isolate); |
| 229 tryCatch.SetVerbose(true); | 227 tryCatch.SetVerbose(true); |
| 230 | 228 |
| 231 Element* result = runConstructor(); | 229 Element* result = runConstructor(); |
| 232 | 230 |
| 233 // To report exception thrown from runConstructor() | 231 // To report exception thrown from runConstructor() |
| 234 if (tryCatch.HasCaught()) | 232 if (tryCatch.HasCaught()) |
| 235 return false; | 233 return false; |
| 236 | 234 |
| 237 // To report InvalidStateError Exception, when the constructor returns some
different object | 235 // To report InvalidStateError Exception, when the constructor returns some
different object |
| 238 if (result != element) { | 236 if (result != element) { |
| 239 const String& message = "custom element constructors must call super() f
irst and must " | 237 const String& message = "custom element constructors must call super() f
irst and must " |
| 240 "not return a different object"; | 238 "not return a different object"; |
| 241 std::unique_ptr<SourceLocation> location = SourceLocation::fromFunction(
constructor().As<v8::Function>()); | |
| 242 v8::Local<v8::Value> exception = V8ThrowException::createDOMException( | 239 v8::Local<v8::Value> exception = V8ThrowException::createDOMException( |
| 243 m_scriptState->isolate(), | 240 m_scriptState->isolate(), |
| 244 InvalidStateError, | 241 InvalidStateError, |
| 245 message); | 242 message); |
| 246 fireErrorEvent(m_scriptState.get(), message, exception, std::move(locati
on)); | 243 V8ThrowException::reportException(m_scriptState.get(), exception, |
| 244 message, constructor().As<v8::Function>()); |
| 247 return false; | 245 return false; |
| 248 } | 246 } |
| 249 | 247 |
| 250 return true; | 248 return true; |
| 251 } | 249 } |
| 252 | 250 |
| 253 Element* ScriptCustomElementDefinition::runConstructor() | 251 Element* ScriptCustomElementDefinition::runConstructor() |
| 254 { | 252 { |
| 255 v8::Isolate* isolate = m_scriptState->isolate(); | 253 v8::Isolate* isolate = m_scriptState->isolate(); |
| 256 DCHECK(ScriptState::current(isolate) == m_scriptState); | 254 DCHECK(ScriptState::current(isolate) == m_scriptState); |
| 257 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); | 255 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); |
| 258 v8::Local<v8::Value> result; | 256 v8::Local<v8::Value> result; |
| 259 if (!v8Call(V8ScriptRunner::callAsConstructor( | 257 if (!v8Call(V8ScriptRunner::callAsConstructor( |
| 260 isolate, | 258 isolate, |
| 261 constructor(), | 259 constructor(), |
| 262 executionContext, | 260 executionContext, |
| 263 0, | 261 0, |
| 264 nullptr), | 262 nullptr), |
| 265 result)) { | 263 result)) { |
| 266 return nullptr; | 264 return nullptr; |
| 267 } | 265 } |
| 268 return V8Element::toImplWithTypeCheck(isolate, result); | 266 return V8Element::toImplWithTypeCheck(isolate, result); |
| 269 } | 267 } |
| 270 | 268 |
| 271 void ScriptCustomElementDefinition::fireErrorEvent(ScriptState* scriptState, con
st String& message, v8::Local<v8::Value> exception, std::unique_ptr<SourceLocati
on> location) | |
| 272 { | |
| 273 ErrorEvent* event = ErrorEvent::create(message, std::move(location), &script
State->world()); | |
| 274 V8ErrorHandler::storeExceptionOnErrorEventWrapper(scriptState, event, except
ion, scriptState->context()->Global()); | |
| 275 ExecutionContext* executionContext = scriptState->getExecutionContext(); | |
| 276 executionContext->reportException(event, NotSharableCrossOrigin); | |
| 277 } | |
| 278 | |
| 279 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const | 269 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const |
| 280 { | 270 { |
| 281 DCHECK(!m_constructor.isEmpty()); | 271 DCHECK(!m_constructor.isEmpty()); |
| 282 return m_constructor.newLocal(m_scriptState->isolate()); | 272 return m_constructor.newLocal(m_scriptState->isolate()); |
| 283 } | 273 } |
| 284 | 274 |
| 285 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const | 275 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const |
| 286 { | 276 { |
| 287 DCHECK(!m_prototype.isEmpty()); | 277 DCHECK(!m_prototype.isEmpty()); |
| 288 return m_prototype.newLocal(m_scriptState->isolate()); | 278 return m_prototype.newLocal(m_scriptState->isolate()); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 v8String(isolate, name.localName()), | 362 v8String(isolate, name.localName()), |
| 373 v8StringOrNull(isolate, oldValue), | 363 v8StringOrNull(isolate, oldValue), |
| 374 v8StringOrNull(isolate, newValue), | 364 v8StringOrNull(isolate, newValue), |
| 375 v8String(isolate, name.namespaceURI()), | 365 v8String(isolate, name.namespaceURI()), |
| 376 }; | 366 }; |
| 377 runCallback(m_attributeChangedCallback.newLocal(isolate), element, | 367 runCallback(m_attributeChangedCallback.newLocal(isolate), element, |
| 378 argc, argv); | 368 argc, argv); |
| 379 } | 369 } |
| 380 | 370 |
| 381 } // namespace blink | 371 } // namespace blink |
| OLD | NEW |