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

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: enqueueAttributeChangedCallbackForAllAttributes and test fixes 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..17af27eddc08ed087b75a733daf2f8e409dd7759 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,15 +143,15 @@ 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)
, m_scriptState(scriptState)
, m_constructor(scriptState->isolate(), constructor)
- , m_observedAttributes(observedAttributes)
{
+ m_observedAttributes = observedAttributes;
dominicc (has gone to gerrit) 2016/06/14 09:19:45 We should make the CustomElementDefinition take ob
}
// https://html.spec.whatwg.org/multipage/scripting.html#upgrades
@@ -209,4 +211,72 @@ 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[])
+{
+ if (!m_scriptState->contextIsValid())
+ return;
+ ScriptState::Scope scope(m_scriptState.get());
+ 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(
+ executionContext,
+ callback.newLocal(isolate),
+ elementHandle,
+ argc, argv, isolate);
+}
+
+void ScriptCustomElementDefinition::runConnectedCallback(Element* element)
+{
+ runCallback(m_connectedCallback, element);
+}
+
+void ScriptCustomElementDefinition::runDisconnectedCallback(Element* element)
+{
+ runCallback(m_disconnectedCallback, element);
+}
+
+static v8::Local<v8::Value> v8StringOrNull(v8::Isolate* isolate,
dominicc (has gone to gerrit) 2016/06/14 09:19:45 This is a nice little helper; maybe this should li
+ const AtomicString& value)
+{
+ if (value.isNull())
+ return v8::Null(isolate);
+ return v8String(isolate, value);
+}
+
+void ScriptCustomElementDefinition::runAttributeChangedCallback(
+ Element* element, const QualifiedName& name,
+ const AtomicString& oldValue, const AtomicString& newValue)
+{
+ v8::Isolate* isolate = m_scriptState->isolate();
dominicc (has gone to gerrit) 2016/06/14 09:19:45 This needs to set up the ScriptState::Scope just l
+ 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