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 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 | 182 |
183 // 6.1.3. through 6.1.9. | 183 // 6.1.3. through 6.1.9. |
184 checkConstructorResult(element, document, tagName, exceptionState); | 184 checkConstructorResult(element, document, tagName, exceptionState); |
185 if (exceptionState.hadException()) | 185 if (exceptionState.hadException()) |
186 return nullptr; | 186 return nullptr; |
187 | 187 |
188 DCHECK_EQ(element->getCustomElementState(), CustomElementState::Custom); | 188 DCHECK_EQ(element->getCustomElementState(), CustomElementState::Custom); |
189 return toHTMLElement(element); | 189 return toHTMLElement(element); |
190 } | 190 } |
191 | 191 |
192 static void dispatchErrorEvent(v8::Isolate* isolate, | |
haraken
2016/08/22 06:36:47
origin => constructor ?
| |
193 v8::Local<v8::Value> exception, v8::Local<v8::Object> origin) | |
194 { | |
195 v8::TryCatch tryCatch(isolate); | |
196 tryCatch.SetVerbose(true); | |
197 V8ScriptRunner::throwException(isolate, exception, | |
198 origin.As<v8::Function>()->GetScriptOrigin()); | |
199 } | |
200 | |
192 HTMLElement* ScriptCustomElementDefinition::createElementSync( | 201 HTMLElement* ScriptCustomElementDefinition::createElementSync( |
193 Document& document, const QualifiedName& tagName) | 202 Document& document, const QualifiedName& tagName) |
194 { | 203 { |
195 ScriptState::Scope scope(m_scriptState.get()); | 204 ScriptState::Scope scope(m_scriptState.get()); |
196 v8::Isolate* isolate = m_scriptState->isolate(); | 205 v8::Isolate* isolate = m_scriptState->isolate(); |
197 | 206 |
198 // When invoked from "create an element for a token": | 207 // When invoked from "create an element for a token": |
199 // https://html.spec.whatwg.org/multipage/syntax.html#create-an-element-for- the-token | 208 // https://html.spec.whatwg.org/multipage/syntax.html#create-an-element-for- the-token |
200 | 209 |
201 ExceptionState exceptionState(ExceptionState::ConstructionContext, | 210 ExceptionState exceptionState(ExceptionState::ConstructionContext, |
202 "CustomElement", constructor(), isolate); | 211 "CustomElement", constructor(), isolate); |
203 HTMLElement* element = createElementSync(document, tagName, exceptionState); | 212 HTMLElement* element = createElementSync(document, tagName, exceptionState); |
204 | 213 |
205 if (exceptionState.hadException() || !element) { | 214 if (exceptionState.hadException()) { |
215 DCHECK(!element); | |
206 // 7. If this step throws an exception, then report the exception, ... | 216 // 7. If this step throws an exception, then report the exception, ... |
207 { | 217 dispatchErrorEvent(isolate, exceptionState.getException(), constructor() ); |
208 v8::TryCatch tryCatch(isolate); | 218 exceptionState.clearException(); |
haraken
2016/08/22 06:36:47
Do you need to clear the exception here?
kojii
2016/08/22 06:41:33
Not today, but when we remove throwIfNeeded()[1],
Yuki
2016/08/22 06:42:08
I think we'll need to clear the exception once har
| |
209 tryCatch.SetVerbose(true); | 219 // and return HTMLUnknownElement. |
210 exceptionState.throwIfNeeded(); | |
211 } | |
212 | |
213 return CustomElement::createFailedElement(document, tagName); | 220 return CustomElement::createFailedElement(document, tagName); |
214 } | 221 } |
222 DCHECK(element); | |
215 return element; | 223 return element; |
216 } | 224 } |
217 | 225 |
218 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades | 226 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades |
219 bool ScriptCustomElementDefinition::runConstructor(Element* element) | 227 bool ScriptCustomElementDefinition::runConstructor(Element* element) |
220 { | 228 { |
221 if (!m_scriptState->contextIsValid()) | 229 if (!m_scriptState->contextIsValid()) |
222 return false; | 230 return false; |
223 ScriptState::Scope scope(m_scriptState.get()); | 231 ScriptState::Scope scope(m_scriptState.get()); |
224 v8::Isolate* isolate = m_scriptState->isolate(); | 232 v8::Isolate* isolate = m_scriptState->isolate(); |
225 | 233 |
226 // Step 5 says to rethrow the exception; but there is no one to | 234 // Step 5 says to rethrow the exception; but there is no one to |
227 // catch it. The side effect is to report the error. | 235 // catch it. The side effect is to report the error. |
228 v8::TryCatch tryCatch(isolate); | 236 v8::TryCatch tryCatch(isolate); |
229 tryCatch.SetVerbose(true); | 237 tryCatch.SetVerbose(true); |
230 | 238 |
231 Element* result = runConstructor(); | 239 Element* result = runConstructor(); |
232 | 240 |
233 // To report exception thrown from runConstructor() | 241 // To report exception thrown from runConstructor() |
234 if (tryCatch.HasCaught()) | 242 if (tryCatch.HasCaught()) |
235 return false; | 243 return false; |
236 | 244 |
237 // To report InvalidStateError Exception, when the constructor returns some different object | 245 // To report InvalidStateError Exception, when the constructor returns some different object |
238 if (result != element) { | 246 if (result != element) { |
239 const String& message = "custom element constructors must call super() f irst and must " | 247 const String& message = "custom element constructors must call super() f irst and must " |
240 "not return a different object"; | 248 "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( | 249 v8::Local<v8::Value> exception = V8ThrowException::createDOMException( |
243 m_scriptState->isolate(), | 250 m_scriptState->isolate(), |
244 InvalidStateError, | 251 InvalidStateError, |
245 message); | 252 message); |
246 fireErrorEvent(m_scriptState.get(), message, exception, std::move(locati on)); | 253 dispatchErrorEvent(isolate, exception, constructor()); |
247 return false; | 254 return false; |
248 } | 255 } |
249 | 256 |
250 return true; | 257 return true; |
251 } | 258 } |
252 | 259 |
253 Element* ScriptCustomElementDefinition::runConstructor() | 260 Element* ScriptCustomElementDefinition::runConstructor() |
254 { | 261 { |
255 v8::Isolate* isolate = m_scriptState->isolate(); | 262 v8::Isolate* isolate = m_scriptState->isolate(); |
256 DCHECK(ScriptState::current(isolate) == m_scriptState); | 263 DCHECK(ScriptState::current(isolate) == m_scriptState); |
257 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); | 264 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); |
258 v8::Local<v8::Value> result; | 265 v8::Local<v8::Value> result; |
259 if (!v8Call(V8ScriptRunner::callAsConstructor( | 266 if (!v8Call(V8ScriptRunner::callAsConstructor( |
260 isolate, | 267 isolate, |
261 constructor(), | 268 constructor(), |
262 executionContext, | 269 executionContext, |
263 0, | 270 0, |
264 nullptr), | 271 nullptr), |
265 result)) { | 272 result)) { |
266 return nullptr; | 273 return nullptr; |
267 } | 274 } |
268 return V8Element::toImplWithTypeCheck(isolate, result); | 275 return V8Element::toImplWithTypeCheck(isolate, result); |
269 } | 276 } |
270 | 277 |
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->dispatchErrorEvent(event, NotSharableCrossOrigin); | |
277 } | |
278 | |
279 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const | 278 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const |
280 { | 279 { |
281 DCHECK(!m_constructor.isEmpty()); | 280 DCHECK(!m_constructor.isEmpty()); |
282 return m_constructor.newLocal(m_scriptState->isolate()); | 281 return m_constructor.newLocal(m_scriptState->isolate()); |
283 } | 282 } |
284 | 283 |
285 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const | 284 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const |
286 { | 285 { |
287 DCHECK(!m_prototype.isEmpty()); | 286 DCHECK(!m_prototype.isEmpty()); |
288 return m_prototype.newLocal(m_scriptState->isolate()); | 287 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()), | 371 v8String(isolate, name.localName()), |
373 v8StringOrNull(isolate, oldValue), | 372 v8StringOrNull(isolate, oldValue), |
374 v8StringOrNull(isolate, newValue), | 373 v8StringOrNull(isolate, newValue), |
375 v8String(isolate, name.namespaceURI()), | 374 v8String(isolate, name.namespaceURI()), |
376 }; | 375 }; |
377 runCallback(m_attributeChangedCallback.newLocal(isolate), element, | 376 runCallback(m_attributeChangedCallback.newLocal(isolate), element, |
378 argc, argv); | 377 argc, argv); |
379 } | 378 } |
380 | 379 |
381 } // namespace blink | 380 } // namespace blink |
OLD | NEW |