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

Side by Side Diff: third_party/WebKit/Source/modules/compositorworker/AnimationWorkletGlobalScope.cpp

Issue 2745823002: [animation-worklet] Implement registerAnimator in worklet scope (Closed)
Patch Set: Address feedback and use new animationWorklet.addModule syntax 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 unified diff | Download patch
OLDNEW
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 "modules/compositorworker/AnimationWorkletGlobalScope.h" 5 #include "modules/compositorworker/AnimationWorkletGlobalScope.h"
6 6
7 #include "platform/weborigin/SecurityOrigin.h" 7 #include "platform/weborigin/SecurityOrigin.h"
8 8
9 #include "bindings/core/v8/ExceptionState.h"
10 #include "bindings/core/v8/WorkerOrWorkletScriptController.h"
11 #include "core/dom/ExceptionCode.h"
12 #include "platform/bindings/V8BindingMacros.h"
13 #include "platform/bindings/V8ObjectConstructor.h"
14
15 #include <utility>
16
9 namespace blink { 17 namespace blink {
10 18
11 AnimationWorkletGlobalScope* AnimationWorkletGlobalScope::Create( 19 AnimationWorkletGlobalScope* AnimationWorkletGlobalScope::Create(
12 const KURL& url, 20 const KURL& url,
13 const String& user_agent, 21 const String& user_agent,
14 PassRefPtr<SecurityOrigin> security_origin, 22 PassRefPtr<SecurityOrigin> security_origin,
15 v8::Isolate* isolate, 23 v8::Isolate* isolate,
16 WorkerThread* thread) { 24 WorkerThread* thread) {
17 return new AnimationWorkletGlobalScope( 25 return new AnimationWorkletGlobalScope(
18 url, user_agent, std::move(security_origin), isolate, thread); 26 url, user_agent, std::move(security_origin), isolate, thread);
19 } 27 }
20 28
21 AnimationWorkletGlobalScope::AnimationWorkletGlobalScope( 29 AnimationWorkletGlobalScope::AnimationWorkletGlobalScope(
22 const KURL& url, 30 const KURL& url,
23 const String& user_agent, 31 const String& user_agent,
24 PassRefPtr<SecurityOrigin> security_origin, 32 PassRefPtr<SecurityOrigin> security_origin,
25 v8::Isolate* isolate, 33 v8::Isolate* isolate,
26 WorkerThread* thread) 34 WorkerThread* thread)
27 : ThreadedWorkletGlobalScope(url, 35 : ThreadedWorkletGlobalScope(url,
28 user_agent, 36 user_agent,
29 std::move(security_origin), 37 std::move(security_origin),
30 isolate, 38 isolate,
31 thread) {} 39 thread) {}
32 40
33 AnimationWorkletGlobalScope::~AnimationWorkletGlobalScope() {} 41 AnimationWorkletGlobalScope::~AnimationWorkletGlobalScope() {}
34 42
43 DEFINE_TRACE(AnimationWorkletGlobalScope) {
44 visitor->Trace(m_animatorDefinitions);
45 visitor->Trace(m_animators);
46 ThreadedWorkletGlobalScope::Trace(visitor);
47 }
48
49 void AnimationWorkletGlobalScope::Dispose() {
50 // Clear animators and definitions to avoid reference cycle.
51 m_animatorDefinitions.clear();
52 m_animators.clear();
53 ThreadedWorkletGlobalScope::Dispose();
54 }
55
56 void AnimationWorkletGlobalScope::registerAnimator(
57 const String& name,
58 const ScriptValue& ctorValue,
59 ExceptionState& exceptionState) {
nhiroki 2017/05/22 00:20:20 Can you add a thread check? DCHECK(IsContextThrea
majidvp 2017/05/24 14:24:16 Done.
60 if (m_animatorDefinitions.Contains(name)) {
61 exceptionState.ThrowDOMException(
62 kNotSupportedError,
63 "A class with name:'" + name + "' is already registered.");
64 return;
65 }
66
67 if (name.IsEmpty()) {
68 exceptionState.ThrowTypeError("The empty string is not a valid name.");
69 return;
70 }
71
72 v8::Isolate* isolate = ScriptController()->GetScriptState()->GetIsolate();
73 v8::Local<v8::Context> context = ScriptController()->GetContext();
74
75 DCHECK(ctorValue.V8Value()->IsFunction());
76 v8::Local<v8::Function> constructor =
77 v8::Local<v8::Function>::Cast(ctorValue.V8Value());
78
79 v8::Local<v8::Value> prototypeValue;
80 if (!constructor->Get(context, V8String(isolate, "prototype"))
81 .ToLocal(&prototypeValue))
82 return;
83
84 if (IsUndefinedOrNull(prototypeValue)) {
85 exceptionState.ThrowTypeError(
86 "The 'prototype' object on the class does not exist.");
87 return;
88 }
89
90 if (!prototypeValue->IsObject()) {
91 exceptionState.ThrowTypeError(
92 "The 'prototype' property on the class is not an object.");
93 return;
94 }
95
96 v8::Local<v8::Object> prototype = v8::Local<v8::Object>::Cast(prototypeValue);
97
98 v8::Local<v8::Value> animateValue;
99 if (!prototype->Get(context, V8String(isolate, "animate"))
100 .ToLocal(&animateValue))
101 return;
102
103 if (IsUndefinedOrNull(animateValue)) {
104 exceptionState.ThrowTypeError(
105 "The 'animate' function on the prototype does not exist.");
106 return;
107 }
108
109 if (!animateValue->IsFunction()) {
110 exceptionState.ThrowTypeError(
111 "The 'animate' property on the prototype is not a function.");
112 return;
113 }
114
115 v8::Local<v8::Function> animate = v8::Local<v8::Function>::Cast(animateValue);
116
117 AnimatorDefinition* definition =
118 AnimatorDefinition::Create(isolate, constructor, animate);
119 m_animatorDefinitions.Set(name, definition);
120
121 // Immediately instantiate an animator for the registered definition.
122 // TODO(majidvp): Remove this once you add alternative way to instantiate
123 m_animators.push_back(CreateInstance(name));
124 }
125
126 Animator* AnimationWorkletGlobalScope::CreateInstance(const String& name) {
nhiroki 2017/05/22 00:20:20 Ditto(thread check)
majidvp 2017/05/24 14:24:16 Done.
127 AnimatorDefinition* definition = m_animatorDefinitions.at(name);
128 if (!definition)
129 return nullptr;
130
131 v8::Isolate* isolate = ScriptController()->GetScriptState()->GetIsolate();
132 v8::Local<v8::Function> constructor = definition->ConstructorLocal(isolate);
133 DCHECK(!IsUndefinedOrNull(constructor));
134
135 v8::Local<v8::Object> instance;
136 if (!V8ObjectConstructor::NewInstance(isolate, constructor)
137 .ToLocal(&instance))
138 return nullptr;
139
140 return Animator::Create(isolate, definition, instance);
141 }
142
35 } // namespace blink 143 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698