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