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

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 nhiroki feedback Created 3 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 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 DCHECK(IsContextThread());
51 // Clear animators and definitions to avoid reference cycle.
52 m_animatorDefinitions.clear();
53 m_animators.clear();
54 ThreadedWorkletGlobalScope::Dispose();
55 }
56
57 void AnimationWorkletGlobalScope::registerAnimator(
58 const String& name,
59 const ScriptValue& ctorValue,
60 ExceptionState& exceptionState) {
61 DCHECK(IsContextThread());
62 if (m_animatorDefinitions.Contains(name)) {
63 exceptionState.ThrowDOMException(
64 kNotSupportedError,
65 "A class with name:'" + name + "' is already registered.");
66 return;
67 }
68
69 if (name.IsEmpty()) {
70 exceptionState.ThrowTypeError("The empty string is not a valid name.");
71 return;
72 }
73
74 v8::Isolate* isolate = ScriptController()->GetScriptState()->GetIsolate();
75 v8::Local<v8::Context> context = ScriptController()->GetContext();
76
77 DCHECK(ctorValue.V8Value()->IsFunction());
78 v8::Local<v8::Function> constructor =
79 v8::Local<v8::Function>::Cast(ctorValue.V8Value());
80
81 v8::Local<v8::Value> prototypeValue;
82 if (!constructor->Get(context, V8String(isolate, "prototype"))
83 .ToLocal(&prototypeValue))
84 return;
85
86 if (IsUndefinedOrNull(prototypeValue)) {
87 exceptionState.ThrowTypeError(
88 "The 'prototype' object on the class does not exist.");
89 return;
90 }
91
92 if (!prototypeValue->IsObject()) {
93 exceptionState.ThrowTypeError(
94 "The 'prototype' property on the class is not an object.");
95 return;
96 }
97
98 v8::Local<v8::Object> prototype = v8::Local<v8::Object>::Cast(prototypeValue);
99
100 v8::Local<v8::Value> animateValue;
101 if (!prototype->Get(context, V8String(isolate, "animate"))
102 .ToLocal(&animateValue))
103 return;
104
105 if (IsUndefinedOrNull(animateValue)) {
106 exceptionState.ThrowTypeError(
107 "The 'animate' function on the prototype does not exist.");
108 return;
109 }
110
111 if (!animateValue->IsFunction()) {
112 exceptionState.ThrowTypeError(
113 "The 'animate' property on the prototype is not a function.");
114 return;
115 }
116
117 v8::Local<v8::Function> animate = v8::Local<v8::Function>::Cast(animateValue);
118
119 AnimatorDefinition* definition =
120 new AnimatorDefinition(isolate, constructor, animate);
121 m_animatorDefinitions.Set(name, definition);
122
123 // Immediately instantiate an animator for the registered definition.
124 // TODO(majidvp): Remove this once you add alternative way to instantiate
125 m_animators.push_back(CreateInstance(name));
126 }
127
128 Animator* AnimationWorkletGlobalScope::CreateInstance(const String& name) {
129 DCHECK(IsContextThread());
130 AnimatorDefinition* definition = m_animatorDefinitions.at(name);
131 if (!definition)
132 return nullptr;
133
134 v8::Isolate* isolate = ScriptController()->GetScriptState()->GetIsolate();
135 v8::Local<v8::Function> constructor = definition->ConstructorLocal(isolate);
136 DCHECK(!IsUndefinedOrNull(constructor));
137
138 v8::Local<v8::Object> instance;
139 if (!V8ObjectConstructor::NewInstance(isolate, constructor)
140 .ToLocal(&instance))
141 return nullptr;
142
143 return new Animator(isolate, definition, instance);
144 }
145
35 } // namespace blink 146 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698