Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Unified Diff: third_party/WebKit/Source/bindings/core/v8/ScriptCustomElementDefinition.cpp

Issue 2060753002: Implement script-side of callback reactions for Custom Elements V1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@callback-ce
Patch Set: dominicc review Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/bindings/core/v8/ScriptCustomElementDefinition.cpp
diff --git a/third_party/WebKit/Source/bindings/core/v8/ScriptCustomElementDefinition.cpp b/third_party/WebKit/Source/bindings/core/v8/ScriptCustomElementDefinition.cpp
index de725ff62ad418e8ad81b48123a14d602e1e0ba3..a6e2ce4a5dd2ba752e70e946bfb7cb8fb24753aa 100644
--- a/third_party/WebKit/Source/bindings/core/v8/ScriptCustomElementDefinition.cpp
+++ b/third_party/WebKit/Source/bindings/core/v8/ScriptCustomElementDefinition.cpp
@@ -4,6 +4,7 @@
#include "bindings/core/v8/ScriptCustomElementDefinition.h"
+#include "bindings/core/v8/ScriptController.h"
#include "bindings/core/v8/ScriptState.h"
#include "bindings/core/v8/V8Binding.h"
#include "bindings/core/v8/V8BindingMacros.h"
@@ -80,9 +81,10 @@ ScriptCustomElementDefinition* ScriptCustomElementDefinition::forConstructor(
return static_cast<ScriptCustomElementDefinition*>(definition);
}
+template <typename T>
static void keepAlive(v8::Local<v8::Array>& array, uint32_t index,
- const v8::Local<v8::Object>& value,
- ScopedPersistent<v8::Object>& persistent,
+ const v8::Local<T>& value,
+ ScopedPersistent<T>& persistent,
ScriptState* scriptState)
{
if (value.IsEmpty())
@@ -100,9 +102,9 @@ ScriptCustomElementDefinition* ScriptCustomElementDefinition::create(
const CustomElementDescriptor& descriptor,
const v8::Local<v8::Object>& constructor,
const v8::Local<v8::Object>& prototype,
- const v8::Local<v8::Object>& connectedCallback,
- const v8::Local<v8::Object>& disconnectedCallback,
- const v8::Local<v8::Object>& attributeChangedCallback,
+ const v8::Local<v8::Function>& connectedCallback,
+ const v8::Local<v8::Function>& disconnectedCallback,
+ const v8::Local<v8::Function>& attributeChangedCallback,
const HashSet<AtomicString>& observedAttributes)
{
ScriptCustomElementDefinition* definition =
@@ -141,14 +143,13 @@ ScriptCustomElementDefinition::ScriptCustomElementDefinition(
const CustomElementDescriptor& descriptor,
const v8::Local<v8::Object>& constructor,
const v8::Local<v8::Object>& prototype,
- const v8::Local<v8::Object>& connectedCallback,
- const v8::Local<v8::Object>& disconnectedCallback,
- const v8::Local<v8::Object>& attributeChangedCallback,
+ const v8::Local<v8::Function>& connectedCallback,
+ const v8::Local<v8::Function>& disconnectedCallback,
+ const v8::Local<v8::Function>& attributeChangedCallback,
const HashSet<AtomicString>& observedAttributes)
- : CustomElementDefinition(descriptor)
+ : CustomElementDefinition(descriptor, observedAttributes)
, m_scriptState(scriptState)
, m_constructor(scriptState->isolate(), constructor)
- , m_observedAttributes(observedAttributes)
{
}
@@ -209,4 +210,71 @@ ScriptValue ScriptCustomElementDefinition::getConstructorForScript()
return ScriptValue(m_scriptState.get(), constructor());
}
+bool ScriptCustomElementDefinition::hasConnectedCallback() const
+{
+ return !m_connectedCallback.isEmpty();
+}
+
+bool ScriptCustomElementDefinition::hasDisconnectedCallback() const
+{
+ return !m_disconnectedCallback.isEmpty();
+}
+
+void ScriptCustomElementDefinition::runCallback(
+ ScopedPersistent<v8::Function>& callback,
+ Element* element, int argc, v8::Local<v8::Value> argv[])
+{
+ DCHECK(ScriptState::current(m_scriptState->isolate()) == m_scriptState);
+ v8::Isolate* isolate = m_scriptState->isolate();
+
+ // Invoke custom element reactions
+ // https://html.spec.whatwg.org/multipage/scripting.html#invoke-custom-element-reactions
+ // If this throws any exception, then report the exception.
+ v8::TryCatch tryCatch(isolate);
+ tryCatch.SetVerbose(true);
+
+ ExecutionContext* executionContext = m_scriptState->getExecutionContext();
+ v8::Local<v8::Value> elementHandle = toV8(element,
+ m_scriptState->context()->Global(), isolate);
+ ScriptController::callFunction(
haraken 2016/06/14 11:39:24 Nit: Use V8ScriptRunner::callFunction. ScriptContr
+ executionContext,
+ callback.newLocal(isolate),
+ elementHandle,
+ argc, argv, isolate);
+}
+
+void ScriptCustomElementDefinition::runConnectedCallback(Element* element)
+{
+ if (!m_scriptState->contextIsValid())
+ return;
+ ScriptState::Scope scope(m_scriptState.get());
+ runCallback(m_connectedCallback, element);
+}
+
+void ScriptCustomElementDefinition::runDisconnectedCallback(Element* element)
+{
+ if (!m_scriptState->contextIsValid())
+ return;
+ ScriptState::Scope scope(m_scriptState.get());
+ runCallback(m_disconnectedCallback, element);
+}
+
+void ScriptCustomElementDefinition::runAttributeChangedCallback(
+ Element* element, const QualifiedName& name,
+ const AtomicString& oldValue, const AtomicString& newValue)
+{
+ if (!m_scriptState->contextIsValid())
+ return;
+ ScriptState::Scope scope(m_scriptState.get());
+ v8::Isolate* isolate = m_scriptState->isolate();
+ const int argc = 4;
+ v8::Local<v8::Value> argv[argc] = {
+ v8String(isolate, name.localName()),
+ v8StringOrNull(isolate, oldValue),
+ v8StringOrNull(isolate, newValue),
+ v8String(isolate, name.namespaceURI()),
+ };
+ runCallback(m_attributeChangedCallback, element, argc, argv);
+}
+
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698