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

Unified Diff: third_party/WebKit/Source/modules/webaudio/AudioWorkletGlobalScope.cpp

Issue 2727733002: Implement AudioWorkletProcessor interface (Closed)
Patch Set: Refactored AudioWorkletGlobalScope to centralize V8 operation Created 3 years, 9 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 side-by-side diff with in-line comments
Download patch
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..83ecab3a70d46f2459613decded0f1eb83995658 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,149 @@ AudioWorkletGlobalScope::AudioWorkletGlobalScope(
AudioWorkletGlobalScope::~AudioWorkletGlobalScope() {}
+void AudioWorkletGlobalScope::dispose() {
ikilpatrick 2017/03/09 21:45:23 DCHECK(isContextThread()) ?
hongchan 2017/03/15 22:30:18 Done.
+ m_processorDefinitionMap.clear();
haraken 2017/03/10 13:43:22 Should we also clear m_processorInstances ?
hongchan 2017/03/15 22:30:18 Done.
+ ThreadedWorkletGlobalScope::dispose();
+}
+
+void AudioWorkletGlobalScope::registerProcessor(
+ ScriptState* scriptState,
haraken 2017/03/10 13:43:22 You won't need to pass in a ScriptState because Au
hongchan 2017/03/15 22:30:17 Done.
+ 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.");
haraken 2017/03/10 13:43:22 I want to have tests for these exceptions.
hongchan 2017/03/15 22:30:18 You mean in the layout test? Or unit test?
+ return;
+ }
+
+ if (name.isEmpty()) {
+ exceptionState.throwTypeError("The empty string is not a valid name.");
+ return;
+ }
+
+ v8::Isolate* isolate = scriptState->isolate();
+ v8::Local<v8::Context> context = scriptState->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")),
haraken 2017/03/10 13:43:21 Should we probably use GetPrototype()?
hongchan 2017/03/15 22:30:17 I tried, but it doesn't seem to work. Can you show
+ prototypeValue))
+ 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))
+ 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);
+}
+
+AudioWorkletProcessor* AudioWorkletGlobalScope::createInstance(
haraken 2017/03/10 13:43:21 Who calls this method? I couldn't find a caller in
hongchan 2017/03/15 22:30:18 The caller part is not connected yet (AudioWorklet
+ 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))
ikilpatrick 2017/03/09 21:45:23 what happens here if the constructor throws? i.e.
hongchan 2017/03/15 22:30:18 I have to think about it. Not sure if the construc
+ .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());
haraken 2017/03/10 13:43:21 DCHECK(definition) ?
hongchan 2017/03/15 22:30:18 Done.
+
+ 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;
+ }
+
+ 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

Powered by Google App Engine
This is Rietveld 408576698