| OLD | NEW |
| 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" | 9 #include "bindings/core/v8/ExceptionState.h" |
| 10 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" | 10 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 WorkerThread* thread) | 34 WorkerThread* thread) |
| 35 : ThreadedWorkletGlobalScope(url, | 35 : ThreadedWorkletGlobalScope(url, |
| 36 user_agent, | 36 user_agent, |
| 37 std::move(security_origin), | 37 std::move(security_origin), |
| 38 isolate, | 38 isolate, |
| 39 thread) {} | 39 thread) {} |
| 40 | 40 |
| 41 AnimationWorkletGlobalScope::~AnimationWorkletGlobalScope() {} | 41 AnimationWorkletGlobalScope::~AnimationWorkletGlobalScope() {} |
| 42 | 42 |
| 43 DEFINE_TRACE(AnimationWorkletGlobalScope) { | 43 DEFINE_TRACE(AnimationWorkletGlobalScope) { |
| 44 visitor->Trace(m_animatorDefinitions); | 44 visitor->Trace(animator_definitions_); |
| 45 visitor->Trace(m_animators); | 45 visitor->Trace(animators_); |
| 46 ThreadedWorkletGlobalScope::Trace(visitor); | 46 ThreadedWorkletGlobalScope::Trace(visitor); |
| 47 } | 47 } |
| 48 | 48 |
| 49 void AnimationWorkletGlobalScope::Dispose() { | 49 DEFINE_TRACE_WRAPPERS(AnimationWorkletGlobalScope) { |
| 50 DCHECK(IsContextThread()); | 50 for (auto animator : animators_) |
| 51 // Clear animators and definitions to avoid reference cycle. | 51 visitor->TraceWrappers(animator); |
| 52 m_animatorDefinitions.clear(); | 52 |
| 53 m_animators.clear(); | 53 for (auto definition : animator_definitions_) |
| 54 ThreadedWorkletGlobalScope::Dispose(); | 54 visitor->TraceWrappers(definition.value); |
| 55 |
| 56 ThreadedWorkletGlobalScope::TraceWrappers(visitor); |
| 55 } | 57 } |
| 56 | 58 |
| 57 void AnimationWorkletGlobalScope::registerAnimator( | 59 void AnimationWorkletGlobalScope::registerAnimator( |
| 58 const String& name, | 60 const String& name, |
| 59 const ScriptValue& ctorValue, | 61 const ScriptValue& ctorValue, |
| 60 ExceptionState& exceptionState) { | 62 ExceptionState& exceptionState) { |
| 61 DCHECK(IsContextThread()); | 63 DCHECK(IsContextThread()); |
| 62 if (m_animatorDefinitions.Contains(name)) { | 64 if (animator_definitions_.Contains(name)) { |
| 63 exceptionState.ThrowDOMException( | 65 exceptionState.ThrowDOMException( |
| 64 kNotSupportedError, | 66 kNotSupportedError, |
| 65 "A class with name:'" + name + "' is already registered."); | 67 "A class with name:'" + name + "' is already registered."); |
| 66 return; | 68 return; |
| 67 } | 69 } |
| 68 | 70 |
| 69 if (name.IsEmpty()) { | 71 if (name.IsEmpty()) { |
| 70 exceptionState.ThrowTypeError("The empty string is not a valid name."); | 72 exceptionState.ThrowTypeError("The empty string is not a valid name."); |
| 71 return; | 73 return; |
| 72 } | 74 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 if (!animateValue->IsFunction()) { | 113 if (!animateValue->IsFunction()) { |
| 112 exceptionState.ThrowTypeError( | 114 exceptionState.ThrowTypeError( |
| 113 "The 'animate' property on the prototype is not a function."); | 115 "The 'animate' property on the prototype is not a function."); |
| 114 return; | 116 return; |
| 115 } | 117 } |
| 116 | 118 |
| 117 v8::Local<v8::Function> animate = v8::Local<v8::Function>::Cast(animateValue); | 119 v8::Local<v8::Function> animate = v8::Local<v8::Function>::Cast(animateValue); |
| 118 | 120 |
| 119 AnimatorDefinition* definition = | 121 AnimatorDefinition* definition = |
| 120 new AnimatorDefinition(isolate, constructor, animate); | 122 new AnimatorDefinition(isolate, constructor, animate); |
| 121 m_animatorDefinitions.Set(name, definition); | 123 animator_definitions_.Set( |
| 124 name, TraceWrapperMember<AnimatorDefinition>(this, definition)); |
| 122 | 125 |
| 123 // Immediately instantiate an animator for the registered definition. | 126 // Immediately instantiate an animator for the registered definition. |
| 124 // TODO(majidvp): Remove this once you add alternative way to instantiate | 127 // TODO(majidvp): Remove this once you add alternative way to instantiate |
| 125 m_animators.push_back(CreateInstance(name)); | 128 Animator* animator = CreateInstance(name); |
| 129 animators_.push_back(TraceWrapperMember<Animator>(this, animator)); |
| 126 } | 130 } |
| 127 | 131 |
| 128 Animator* AnimationWorkletGlobalScope::CreateInstance(const String& name) { | 132 Animator* AnimationWorkletGlobalScope::CreateInstance(const String& name) { |
| 129 DCHECK(IsContextThread()); | 133 DCHECK(IsContextThread()); |
| 130 AnimatorDefinition* definition = m_animatorDefinitions.at(name); | 134 AnimatorDefinition* definition = animator_definitions_.at(name); |
| 131 if (!definition) | 135 if (!definition) |
| 132 return nullptr; | 136 return nullptr; |
| 133 | 137 |
| 134 v8::Isolate* isolate = ScriptController()->GetScriptState()->GetIsolate(); | 138 v8::Isolate* isolate = ScriptController()->GetScriptState()->GetIsolate(); |
| 135 v8::Local<v8::Function> constructor = definition->ConstructorLocal(isolate); | 139 v8::Local<v8::Function> constructor = definition->ConstructorLocal(isolate); |
| 136 DCHECK(!IsUndefinedOrNull(constructor)); | 140 DCHECK(!IsUndefinedOrNull(constructor)); |
| 137 | 141 |
| 138 v8::Local<v8::Object> instance; | 142 v8::Local<v8::Object> instance; |
| 139 if (!V8ObjectConstructor::NewInstance(isolate, constructor) | 143 if (!V8ObjectConstructor::NewInstance(isolate, constructor) |
| 140 .ToLocal(&instance)) | 144 .ToLocal(&instance)) |
| 141 return nullptr; | 145 return nullptr; |
| 142 | 146 |
| 143 return new Animator(isolate, definition, instance); | 147 return new Animator(isolate, definition, instance); |
| 144 } | 148 } |
| 145 | 149 |
| 146 } // namespace blink | 150 } // namespace blink |
| OLD | NEW |