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" |
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/V8HiddenValue.h" | 13 #include "bindings/core/v8/V8HiddenValue.h" |
13 #include "bindings/core/v8/V8ScriptRunner.h" | 14 #include "bindings/core/v8/V8ScriptRunner.h" |
14 #include "bindings/core/v8/V8ThrowException.h" | 15 #include "bindings/core/v8/V8ThrowException.h" |
15 #include "core/dom/ExceptionCode.h" | 16 #include "core/dom/ExceptionCode.h" |
16 #include "core/dom/custom/CustomElement.h" | 17 #include "core/dom/custom/CustomElement.h" |
| 18 #include "core/events/ErrorEvent.h" |
17 #include "core/html/HTMLElement.h" | 19 #include "core/html/HTMLElement.h" |
18 #include "v8.h" | 20 #include "v8.h" |
19 #include "wtf/Allocator.h" | 21 #include "wtf/Allocator.h" |
20 | 22 |
21 namespace blink { | 23 namespace blink { |
22 | 24 |
23 // Retrieves the custom elements constructor -> name map, creating it | 25 // Retrieves the custom elements constructor -> name map, creating it |
24 // if necessary. The same map is used to keep prototypes alive. | 26 // if necessary. The same map is used to keep prototypes alive. |
25 static v8::Local<v8::Map> ensureCustomElementsRegistryMap( | 27 static v8::Local<v8::Map> ensureCustomElementsRegistryMap( |
26 ScriptState* scriptState, | 28 ScriptState* scriptState, |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 return false; | 219 return false; |
218 ScriptState::Scope scope(m_scriptState.get()); | 220 ScriptState::Scope scope(m_scriptState.get()); |
219 v8::Isolate* isolate = m_scriptState->isolate(); | 221 v8::Isolate* isolate = m_scriptState->isolate(); |
220 | 222 |
221 // Step 5 says to rethrow the exception; but there is no one to | 223 // Step 5 says to rethrow the exception; but there is no one to |
222 // catch it. The side effect is to report the error. | 224 // catch it. The side effect is to report the error. |
223 v8::TryCatch tryCatch(isolate); | 225 v8::TryCatch tryCatch(isolate); |
224 tryCatch.SetVerbose(true); | 226 tryCatch.SetVerbose(true); |
225 | 227 |
226 Element* result = runConstructor(); | 228 Element* result = runConstructor(); |
227 if (!result) | 229 |
| 230 // To report exception thrown from runConstructor() |
| 231 if (tryCatch.HasCaught()) |
228 return false; | 232 return false; |
229 | 233 |
| 234 // To report InvalidStateError Exception, when the constructor returns some
differnt object |
230 if (result != element) { | 235 if (result != element) { |
231 V8ThrowException::throwException( | 236 const String& message = "custom element constructors must call super() f
irst and must " |
232 V8ThrowException::createDOMException( | 237 "not return a different object"; |
233 m_scriptState->isolate(), | 238 |
234 InvalidStateError, | 239 std::unique_ptr<SourceLocation> location = SourceLocation::fromFunction(
constructor().As<v8::Function>()); |
235 "custom element constructors must call super() first and must " | 240 v8::Local<v8::Value> exception = V8ThrowException::createDOMException( |
236 "not return a different object", | 241 m_scriptState->isolate(), |
237 constructor()), | 242 InvalidStateError, |
238 m_scriptState->isolate()); | 243 message, |
| 244 constructor()); |
| 245 fireErrorEvent(m_scriptState.get(), message, exception, std::move(locati
on)); |
239 return false; | 246 return false; |
240 } | 247 } |
241 | 248 |
242 return true; | 249 return true; |
243 } | 250 } |
244 | 251 |
245 Element* ScriptCustomElementDefinition::runConstructor() | 252 Element* ScriptCustomElementDefinition::runConstructor() |
246 { | 253 { |
247 v8::Isolate* isolate = m_scriptState->isolate(); | 254 v8::Isolate* isolate = m_scriptState->isolate(); |
248 DCHECK(ScriptState::current(isolate) == m_scriptState); | 255 DCHECK(ScriptState::current(isolate) == m_scriptState); |
249 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); | 256 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); |
250 v8::Local<v8::Value> result; | 257 v8::Local<v8::Value> result; |
251 if (!v8Call(V8ScriptRunner::callAsConstructor( | 258 if (!v8Call(V8ScriptRunner::callAsConstructor( |
252 isolate, | 259 isolate, |
253 constructor(), | 260 constructor(), |
254 executionContext, | 261 executionContext, |
255 0, | 262 0, |
256 nullptr), | 263 nullptr), |
257 result)) { | 264 result)) { |
258 return nullptr; | 265 return nullptr; |
259 } | 266 } |
260 return V8Element::toImplWithTypeCheck(isolate, result); | 267 return V8Element::toImplWithTypeCheck(isolate, result); |
261 } | 268 } |
262 | 269 |
| 270 void ScriptCustomElementDefinition::fireErrorEvent(ScriptState* scriptState, con
st String& message, v8::Local<v8::Value> exception, std::unique_ptr<SourceLocati
on> location) |
| 271 { |
| 272 ErrorEvent* event = ErrorEvent::create(message, std::move(location), &script
State->world()); |
| 273 V8ErrorHandler::storeExceptionOnErrorEventWrapper(scriptState, event, except
ion, scriptState->context()->Global()); |
| 274 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
| 275 executionContext->reportException(event, NotSharableCrossOrigin); |
| 276 } |
| 277 |
263 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const | 278 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const |
264 { | 279 { |
265 DCHECK(!m_constructor.isEmpty()); | 280 DCHECK(!m_constructor.isEmpty()); |
266 return m_constructor.newLocal(m_scriptState->isolate()); | 281 return m_constructor.newLocal(m_scriptState->isolate()); |
267 } | 282 } |
268 | 283 |
269 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const | 284 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const |
270 { | 285 { |
271 DCHECK(!m_prototype.isEmpty()); | 286 DCHECK(!m_prototype.isEmpty()); |
272 return m_prototype.newLocal(m_scriptState->isolate()); | 287 return m_prototype.newLocal(m_scriptState->isolate()); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 v8String(isolate, name.localName()), | 357 v8String(isolate, name.localName()), |
343 v8StringOrNull(isolate, oldValue), | 358 v8StringOrNull(isolate, oldValue), |
344 v8StringOrNull(isolate, newValue), | 359 v8StringOrNull(isolate, newValue), |
345 v8String(isolate, name.namespaceURI()), | 360 v8String(isolate, name.namespaceURI()), |
346 }; | 361 }; |
347 runCallback(m_attributeChangedCallback.newLocal(isolate), element, | 362 runCallback(m_attributeChangedCallback.newLocal(isolate), element, |
348 argc, argv); | 363 argc, argv); |
349 } | 364 } |
350 | 365 |
351 } // namespace blink | 366 } // namespace blink |
OLD | NEW |