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