| 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/webaudio/AudioWorkletGlobalScope.h" | 5 #include "modules/webaudio/AudioWorkletGlobalScope.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ToV8.h" |
| 8 #include "bindings/core/v8/V8Binding.h" |
| 9 #include "bindings/core/v8/V8BindingMacros.h" |
| 10 #include "bindings/core/v8/V8ObjectConstructor.h" |
| 11 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
| 12 #include "core/dom/ExceptionCode.h" |
| 13 #include "modules/webaudio/AudioBuffer.h" |
| 14 #include "modules/webaudio/AudioWorkletProcessor.h" |
| 15 #include "modules/webaudio/AudioWorkletProcessorDefinition.h" |
| 7 #include "platform/weborigin/SecurityOrigin.h" | 16 #include "platform/weborigin/SecurityOrigin.h" |
| 8 | 17 |
| 9 namespace blink { | 18 namespace blink { |
| 10 | 19 |
| 11 AudioWorkletGlobalScope* AudioWorkletGlobalScope::create( | 20 AudioWorkletGlobalScope* AudioWorkletGlobalScope::create( |
| 12 const KURL& url, | 21 const KURL& url, |
| 13 const String& userAgent, | 22 const String& userAgent, |
| 14 PassRefPtr<SecurityOrigin> securityOrigin, | 23 PassRefPtr<SecurityOrigin> securityOrigin, |
| 15 v8::Isolate* isolate, | 24 v8::Isolate* isolate, |
| 16 WorkerThread* thread) { | 25 WorkerThread* thread) { |
| 17 return new AudioWorkletGlobalScope(url, userAgent, std::move(securityOrigin), | 26 return new AudioWorkletGlobalScope(url, userAgent, std::move(securityOrigin), |
| 18 isolate, thread); | 27 isolate, thread); |
| 19 } | 28 } |
| 20 | 29 |
| 21 AudioWorkletGlobalScope::AudioWorkletGlobalScope( | 30 AudioWorkletGlobalScope::AudioWorkletGlobalScope( |
| 22 const KURL& url, | 31 const KURL& url, |
| 23 const String& userAgent, | 32 const String& userAgent, |
| 24 PassRefPtr<SecurityOrigin> securityOrigin, | 33 PassRefPtr<SecurityOrigin> securityOrigin, |
| 25 v8::Isolate* isolate, | 34 v8::Isolate* isolate, |
| 26 WorkerThread* thread) | 35 WorkerThread* thread) |
| 27 : ThreadedWorkletGlobalScope(url, | 36 : ThreadedWorkletGlobalScope(url, |
| 28 userAgent, | 37 userAgent, |
| 29 std::move(securityOrigin), | 38 std::move(securityOrigin), |
| 30 isolate, | 39 isolate, |
| 31 thread) {} | 40 thread) {} |
| 32 | 41 |
| 33 AudioWorkletGlobalScope::~AudioWorkletGlobalScope() {} | 42 AudioWorkletGlobalScope::~AudioWorkletGlobalScope() {} |
| 34 | 43 |
| 44 void AudioWorkletGlobalScope::dispose() { |
| 45 DCHECK(isContextThread()); |
| 46 m_processorDefinitionMap.clear(); |
| 47 m_processorInstances.clear(); |
| 48 ThreadedWorkletGlobalScope::dispose(); |
| 49 } |
| 50 |
| 51 void AudioWorkletGlobalScope::registerProcessor( |
| 52 const String& name, |
| 53 const ScriptValue& classDefinition, |
| 54 ExceptionState& exceptionState) { |
| 55 DCHECK(isContextThread()); |
| 56 |
| 57 if (m_processorDefinitionMap.contains(name)) { |
| 58 exceptionState.throwDOMException( |
| 59 NotSupportedError, |
| 60 "A class with name:'" + name + "' is already registered."); |
| 61 return; |
| 62 } |
| 63 |
| 64 // TODO(hongchan): this is not stated in the spec, but seems necessary. |
| 65 // https://github.com/WebAudio/web-audio-api/issues/1172 |
| 66 if (name.isEmpty()) { |
| 67 exceptionState.throwTypeError("The empty string is not a valid name."); |
| 68 return; |
| 69 } |
| 70 |
| 71 v8::Isolate* isolate = scriptController()->getScriptState()->isolate(); |
| 72 v8::Local<v8::Context> context = |
| 73 scriptController()->getScriptState()->context(); |
| 74 |
| 75 if (!classDefinition.v8Value()->IsFunction()) { |
| 76 exceptionState.throwTypeError( |
| 77 "The processor definition is neither 'class' nor 'function'."); |
| 78 return; |
| 79 } |
| 80 |
| 81 v8::Local<v8::Function> classDefinitionLocal = |
| 82 classDefinition.v8Value().As<v8::Function>(); |
| 83 |
| 84 v8::Local<v8::Value> prototypeValueLocal; |
| 85 bool prototypeExtracted = |
| 86 classDefinitionLocal->Get(context, v8String(isolate, "prototype")) |
| 87 .ToLocal(&prototypeValueLocal); |
| 88 DCHECK(prototypeExtracted); |
| 89 |
| 90 v8::Local<v8::Object> prototypeObjectLocal = |
| 91 prototypeValueLocal.As<v8::Object>(); |
| 92 |
| 93 v8::Local<v8::Value> processValueLocal; |
| 94 bool processExtracted = |
| 95 prototypeObjectLocal->Get(context, v8String(isolate, "process")) |
| 96 .ToLocal(&processValueLocal); |
| 97 DCHECK(processExtracted); |
| 98 |
| 99 if (processValueLocal->IsNullOrUndefined()) { |
| 100 exceptionState.throwTypeError( |
| 101 "The 'process' function does not exist in the prototype."); |
| 102 return; |
| 103 } |
| 104 |
| 105 if (!processValueLocal->IsFunction()) { |
| 106 exceptionState.throwTypeError( |
| 107 "The 'process' property on the prototype is not a function."); |
| 108 return; |
| 109 } |
| 110 |
| 111 v8::Local<v8::Function> processFunctionLocal = |
| 112 processValueLocal.As<v8::Function>(); |
| 113 |
| 114 AudioWorkletProcessorDefinition* definition = |
| 115 AudioWorkletProcessorDefinition::create( |
| 116 isolate, name, classDefinitionLocal, processFunctionLocal); |
| 117 DCHECK(definition); |
| 118 |
| 119 m_processorDefinitionMap.set(name, definition); |
| 120 } |
| 121 |
| 122 AudioWorkletProcessor* AudioWorkletGlobalScope::createInstance( |
| 123 const String& name) { |
| 124 DCHECK(isContextThread()); |
| 125 |
| 126 AudioWorkletProcessorDefinition* definition = findDefinition(name); |
| 127 if (!definition) |
| 128 return nullptr; |
| 129 |
| 130 // V8 object instance construction: this construction process is here to make |
| 131 // the AudioWorkletProcessor class a thin wrapper of V8::Object instance. |
| 132 v8::Isolate* isolate = scriptController()->getScriptState()->isolate(); |
| 133 v8::Local<v8::Object> instanceLocal; |
| 134 if (!V8ObjectConstructor::newInstance(isolate, |
| 135 definition->constructorLocal(isolate)) |
| 136 .ToLocal(&instanceLocal)) { |
| 137 return nullptr; |
| 138 } |
| 139 |
| 140 AudioWorkletProcessor* processor = AudioWorkletProcessor::create(this, name); |
| 141 DCHECK(processor); |
| 142 |
| 143 processor->setInstance(isolate, instanceLocal); |
| 144 m_processorInstances.push_back(processor); |
| 145 |
| 146 return processor; |
| 147 } |
| 148 |
| 149 bool AudioWorkletGlobalScope::process(AudioWorkletProcessor* processor, |
| 150 AudioBuffer* inputBuffer, |
| 151 AudioBuffer* outputBuffer) { |
| 152 CHECK(inputBuffer); |
| 153 CHECK(outputBuffer); |
| 154 |
| 155 ScriptState* scriptState = scriptController()->getScriptState(); |
| 156 ScriptState::Scope scope(scriptState); |
| 157 |
| 158 v8::Isolate* isolate = scriptState->isolate(); |
| 159 AudioWorkletProcessorDefinition* definition = |
| 160 findDefinition(processor->name()); |
| 161 DCHECK(definition); |
| 162 |
| 163 v8::Local<v8::Value> argv[] = { |
| 164 ToV8(inputBuffer, scriptState->context()->Global(), isolate), |
| 165 ToV8(outputBuffer, scriptState->context()->Global(), isolate)}; |
| 166 |
| 167 // TODO(hongchan): Catch exceptions thrown in the process method. The verbose |
| 168 // options forces the TryCatch object to save the exception location. The |
| 169 // pending exception should be handled later. |
| 170 v8::TryCatch block(isolate); |
| 171 block.SetVerbose(true); |
| 172 |
| 173 // Perform JS function process() in AudioWorkletProcessor instance. The actual |
| 174 // V8 operation happens here to make the AudioWorkletProcessor class a thin |
| 175 // wrapper of v8::Object instance. |
| 176 V8ScriptRunner::callFunction( |
| 177 definition->processLocal(isolate), scriptState->getExecutionContext(), |
| 178 processor->instanceLocal(isolate), WTF_ARRAY_LENGTH(argv), argv, isolate); |
| 179 |
| 180 return !block.HasCaught(); |
| 181 } |
| 182 |
| 183 AudioWorkletProcessorDefinition* AudioWorkletGlobalScope::findDefinition( |
| 184 const String& name) { |
| 185 return m_processorDefinitionMap.at(name); |
| 186 } |
| 187 |
| 188 DEFINE_TRACE(AudioWorkletGlobalScope) { |
| 189 visitor->trace(m_processorDefinitionMap); |
| 190 visitor->trace(m_processorInstances); |
| 191 ThreadedWorkletGlobalScope::trace(visitor); |
| 192 } |
| 193 |
| 35 } // namespace blink | 194 } // namespace blink |
| OLD | NEW |