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

Unified Diff: third_party/WebKit/Source/modules/compositorworker/AnimationWorkletGlobalScope.cpp

Issue 2745823002: [animation-worklet] Implement registerAnimator in worklet scope (Closed)
Patch Set: Address nhiroki feedback Created 3 years, 7 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/modules/compositorworker/AnimationWorkletGlobalScope.cpp
diff --git a/third_party/WebKit/Source/modules/compositorworker/AnimationWorkletGlobalScope.cpp b/third_party/WebKit/Source/modules/compositorworker/AnimationWorkletGlobalScope.cpp
index 41c5f1164f6a2ef98068ada739b5a031654953f7..5266de02f6bb10a681fac6b8b3e091ed99e47ee1 100644
--- a/third_party/WebKit/Source/modules/compositorworker/AnimationWorkletGlobalScope.cpp
+++ b/third_party/WebKit/Source/modules/compositorworker/AnimationWorkletGlobalScope.cpp
@@ -6,6 +6,14 @@
#include "platform/weborigin/SecurityOrigin.h"
+#include "bindings/core/v8/ExceptionState.h"
+#include "bindings/core/v8/WorkerOrWorkletScriptController.h"
+#include "core/dom/ExceptionCode.h"
+#include "platform/bindings/V8BindingMacros.h"
+#include "platform/bindings/V8ObjectConstructor.h"
+
+#include <utility>
+
namespace blink {
AnimationWorkletGlobalScope* AnimationWorkletGlobalScope::Create(
@@ -32,4 +40,107 @@ AnimationWorkletGlobalScope::AnimationWorkletGlobalScope(
AnimationWorkletGlobalScope::~AnimationWorkletGlobalScope() {}
+DEFINE_TRACE(AnimationWorkletGlobalScope) {
+ visitor->Trace(m_animatorDefinitions);
+ visitor->Trace(m_animators);
+ ThreadedWorkletGlobalScope::Trace(visitor);
+}
+
+void AnimationWorkletGlobalScope::Dispose() {
+ DCHECK(IsContextThread());
+ // Clear animators and definitions to avoid reference cycle.
+ m_animatorDefinitions.clear();
+ m_animators.clear();
+ ThreadedWorkletGlobalScope::Dispose();
+}
+
+void AnimationWorkletGlobalScope::registerAnimator(
+ const String& name,
+ const ScriptValue& ctorValue,
+ ExceptionState& exceptionState) {
+ DCHECK(IsContextThread());
+ if (m_animatorDefinitions.Contains(name)) {
+ exceptionState.ThrowDOMException(
+ kNotSupportedError,
+ "A class with name:'" + name + "' is already registered.");
+ return;
+ }
+
+ if (name.IsEmpty()) {
+ exceptionState.ThrowTypeError("The empty string is not a valid name.");
+ return;
+ }
+
+ v8::Isolate* isolate = ScriptController()->GetScriptState()->GetIsolate();
+ v8::Local<v8::Context> context = ScriptController()->GetContext();
+
+ DCHECK(ctorValue.V8Value()->IsFunction());
+ v8::Local<v8::Function> constructor =
+ v8::Local<v8::Function>::Cast(ctorValue.V8Value());
+
+ v8::Local<v8::Value> prototypeValue;
+ if (!constructor->Get(context, V8String(isolate, "prototype"))
+ .ToLocal(&prototypeValue))
+ return;
+
+ if (IsUndefinedOrNull(prototypeValue)) {
+ exceptionState.ThrowTypeError(
+ "The 'prototype' object on the class does not exist.");
+ return;
+ }
+
+ if (!prototypeValue->IsObject()) {
+ exceptionState.ThrowTypeError(
+ "The 'prototype' property on the class is not an object.");
+ return;
+ }
+
+ v8::Local<v8::Object> prototype = v8::Local<v8::Object>::Cast(prototypeValue);
+
+ v8::Local<v8::Value> animateValue;
+ if (!prototype->Get(context, V8String(isolate, "animate"))
+ .ToLocal(&animateValue))
+ return;
+
+ if (IsUndefinedOrNull(animateValue)) {
+ exceptionState.ThrowTypeError(
+ "The 'animate' function on the prototype does not exist.");
+ return;
+ }
+
+ if (!animateValue->IsFunction()) {
+ exceptionState.ThrowTypeError(
+ "The 'animate' property on the prototype is not a function.");
+ return;
+ }
+
+ v8::Local<v8::Function> animate = v8::Local<v8::Function>::Cast(animateValue);
+
+ AnimatorDefinition* definition =
+ new AnimatorDefinition(isolate, constructor, animate);
+ m_animatorDefinitions.Set(name, definition);
+
+ // Immediately instantiate an animator for the registered definition.
+ // TODO(majidvp): Remove this once you add alternative way to instantiate
+ m_animators.push_back(CreateInstance(name));
+}
+
+Animator* AnimationWorkletGlobalScope::CreateInstance(const String& name) {
+ DCHECK(IsContextThread());
+ AnimatorDefinition* definition = m_animatorDefinitions.at(name);
+ if (!definition)
+ return nullptr;
+
+ v8::Isolate* isolate = ScriptController()->GetScriptState()->GetIsolate();
+ v8::Local<v8::Function> constructor = definition->ConstructorLocal(isolate);
+ DCHECK(!IsUndefinedOrNull(constructor));
+
+ v8::Local<v8::Object> instance;
+ if (!V8ObjectConstructor::NewInstance(isolate, constructor)
+ .ToLocal(&instance))
+ return nullptr;
+
+ return new Animator(isolate, definition, instance);
+}
+
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698