Chromium Code Reviews| Index: third_party/WebKit/Source/modules/webaudio/AudioWorkletGlobalScope.cpp |
| diff --git a/third_party/WebKit/Source/modules/webaudio/AudioWorkletGlobalScope.cpp b/third_party/WebKit/Source/modules/webaudio/AudioWorkletGlobalScope.cpp |
| index 33abafe5dd2497fded72ea08c2b587cd44bcadeb..fefb9a64042ba760b7f2b646769bdf6606b5170a 100644 |
| --- a/third_party/WebKit/Source/modules/webaudio/AudioWorkletGlobalScope.cpp |
| +++ b/third_party/WebKit/Source/modules/webaudio/AudioWorkletGlobalScope.cpp |
| @@ -4,6 +4,15 @@ |
| #include "modules/webaudio/AudioWorkletGlobalScope.h" |
| +#include "bindings/core/v8/ToV8.h" |
| +#include "bindings/core/v8/V8Binding.h" |
| +#include "bindings/core/v8/V8BindingMacros.h" |
| +#include "bindings/core/v8/V8ObjectConstructor.h" |
| +#include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
| +#include "core/dom/ExceptionCode.h" |
| +#include "modules/webaudio/AudioBuffer.h" |
| +#include "modules/webaudio/AudioWorkletProcessor.h" |
| +#include "modules/webaudio/AudioWorkletProcessorDefinition.h" |
| #include "platform/weborigin/SecurityOrigin.h" |
| namespace blink { |
| @@ -32,4 +41,152 @@ AudioWorkletGlobalScope::AudioWorkletGlobalScope( |
| AudioWorkletGlobalScope::~AudioWorkletGlobalScope() {} |
| +void AudioWorkletGlobalScope::dispose() { |
| + DCHECK(isContextThread()); |
| + m_processorDefinitionMap.clear(); |
| + m_processorInstances.clear(); |
| + ThreadedWorkletGlobalScope::dispose(); |
| +} |
| + |
| +void AudioWorkletGlobalScope::registerProcessor( |
| + const String& name, |
| + const ScriptValue& classDefinition, |
| + ExceptionState& exceptionState) { |
| + DCHECK(isContextThread()); |
| + |
| + if (m_processorDefinitionMap.contains(name)) { |
| + exceptionState.throwDOMException( |
| + NotSupportedError, |
| + "A class with name:'" + name + "' is already registered."); |
| + return; |
| + } |
| + |
| + if (name.isEmpty()) { |
| + exceptionState.throwTypeError("The empty string is not a valid name."); |
|
nhiroki
2017/03/16 00:21:35
This rejects not only a null 'name' but also an em
hongchan
2017/03/16 22:11:28
Do you mean an empty string like ''?
JS object su
|
| + return; |
| + } |
| + |
| + v8::Isolate* isolate = scriptController()->getScriptState()->isolate(); |
| + v8::Local<v8::Context> context = |
| + scriptController()->getScriptState()->context(); |
| + |
| + DCHECK(classDefinition.v8Value()->IsFunction()); |
| + |
| + v8::Local<v8::Function> constructorLocal = |
| + v8::Local<v8::Function>::Cast(classDefinition.v8Value()); |
| + |
| + v8::Local<v8::Value> prototypeValue; |
| + if (!v8Call(constructorLocal->Get(context, v8String(isolate, "prototype")), |
| + prototypeValue)) |
|
nhiroki
2017/03/16 00:21:35
Just curious: Why don't we throw an exception in t
haraken
2017/03/16 12:07:15
Yeah, we need to throw some exception when Get fai
hongchan
2017/03/16 22:11:28
This parsing pattern is largely based on what Pain
|
| + return; |
| + |
| + if (isUndefinedOrNull(prototypeValue)) { |
| + exceptionState.throwTypeError( |
| + "The 'prototype' object on the class does not exist."); |
| + return; |
| + } |
| + |
| + if (!prototypeValue->IsObject()) { |
| + exceptionState.throwTypeError( |
| + "The 'prototype' property on the class is not an object."); |
| + return; |
| + } |
| + |
| + v8::Local<v8::Object> prototype = v8::Local<v8::Object>::Cast(prototypeValue); |
| + |
| + v8::Local<v8::Value> processValue; |
| + if (!v8Call(prototype->Get(context, v8String(isolate, "process")), |
| + processValue)) |
|
nhiroki
2017/03/16 00:21:35
ditto.
|
| + return; |
| + |
| + if (isUndefinedOrNull(processValue)) { |
| + exceptionState.throwTypeError( |
| + "The 'process' function on the prototype does not exist."); |
| + return; |
| + } |
| + |
| + if (!processValue->IsFunction()) { |
| + exceptionState.throwTypeError( |
| + "The 'process' property on the prototype is not a function."); |
| + return; |
| + } |
| + |
| + v8::Local<v8::Function> processLocal = |
| + v8::Local<v8::Function>::Cast(processValue); |
| + |
| + AudioWorkletProcessorDefinition* definition = |
| + AudioWorkletProcessorDefinition::create(isolate, name, constructorLocal, |
| + processLocal); |
| + |
| + m_processorDefinitionMap.set(name, definition); |
|
haraken
2017/03/16 12:07:15
Nit: This will be an extremely corner case but you
|
| +} |
| + |
| +AudioWorkletProcessor* AudioWorkletGlobalScope::createInstance( |
| + const String& name) { |
| + DCHECK(isContextThread()); |
| + |
| + AudioWorkletProcessorDefinition* definition = findDefinition(name); |
| + if (!definition) |
| + return nullptr; |
| + |
| + // V8 object instance construction: this construction process is here to make |
| + // the AudioWorkletProcessor class a thin wrapper of V8::Object instance. |
| + v8::Isolate* isolate = scriptController()->getScriptState()->isolate(); |
| + v8::Local<v8::Object> instanceLocal; |
| + if (!V8ObjectConstructor::newInstance(isolate, |
| + definition->constructorLocal(isolate)) |
| + .ToLocal(&instanceLocal)) { |
| + return nullptr; |
| + } |
| + |
| + AudioWorkletProcessor* processor = AudioWorkletProcessor::create(this, name); |
| + processor->setInstance(isolate, instanceLocal); |
| + m_processorInstances.push_back(processor); |
| + |
| + return processor; |
| +} |
| + |
| +bool AudioWorkletGlobalScope::process(AudioWorkletProcessor* processor, |
| + AudioBuffer* inputBuffer, |
| + AudioBuffer* outputBuffer) { |
| + ScriptState* scriptState = scriptController()->getScriptState(); |
| + ScriptState::Scope scope(scriptState); |
| + |
| + v8::Isolate* isolate = scriptState->isolate(); |
| + AudioWorkletProcessorDefinition* definition = |
| + findDefinition(processor->name()); |
| + DCHECK(definition); |
| + |
| + v8::Local<v8::Value> argv[] = { |
| + ToV8(inputBuffer, scriptState->context()->Global(), isolate), |
| + ToV8(outputBuffer, scriptState->context()->Global(), isolate)}; |
| + |
| + v8::TryCatch block(isolate); |
| + block.SetVerbose(true); |
| + |
| + // Perform JS function process() in AudioWorkletProcessor instance. The actual |
| + // V8 operation happens here to make the AudioWorkletProcessor class a thin |
| + // wrapper of v8::Object instance. |
| + V8ScriptRunner::callFunction( |
| + definition->processLocal(isolate), scriptState->getExecutionContext(), |
| + processor->instanceLocal(isolate), WTF_ARRAY_LENGTH(argv), argv, isolate); |
| + |
| + if (block.HasCaught()) { |
| + return false; |
|
haraken
2017/03/16 12:07:15
It's okay to ignore the exception, right?
|
| + } |
| + |
| + return true; |
| +} |
| + |
| +AudioWorkletProcessorDefinition* AudioWorkletGlobalScope::findDefinition( |
| + const String& name) { |
| + return m_processorDefinitionMap.at(name); |
| +} |
| + |
| +DEFINE_TRACE(AudioWorkletGlobalScope) { |
| + visitor->trace(m_processorDefinitionMap); |
| + visitor->trace(m_processorInstances); |
| + ThreadedWorkletGlobalScope::trace(visitor); |
| +} |
| + |
| } // namespace blink |