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

Side by Side Diff: third_party/WebKit/Source/modules/webaudio/AudioWorkletGlobalScope.cpp

Issue 2727733002: Implement AudioWorkletProcessor interface (Closed)
Patch Set: Added AudioWorkletGlobalScopeTest.cpp 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 unified diff | Download patch
OLDNEW
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 if (name.isEmpty()) {
65 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
66 return;
67 }
68
69 v8::Isolate* isolate = scriptController()->getScriptState()->isolate();
70 v8::Local<v8::Context> context =
71 scriptController()->getScriptState()->context();
72
73 DCHECK(classDefinition.v8Value()->IsFunction());
74
75 v8::Local<v8::Function> constructorLocal =
76 v8::Local<v8::Function>::Cast(classDefinition.v8Value());
77
78 v8::Local<v8::Value> prototypeValue;
79 if (!v8Call(constructorLocal->Get(context, v8String(isolate, "prototype")),
80 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
81 return;
82
83 if (isUndefinedOrNull(prototypeValue)) {
84 exceptionState.throwTypeError(
85 "The 'prototype' object on the class does not exist.");
86 return;
87 }
88
89 if (!prototypeValue->IsObject()) {
90 exceptionState.throwTypeError(
91 "The 'prototype' property on the class is not an object.");
92 return;
93 }
94
95 v8::Local<v8::Object> prototype = v8::Local<v8::Object>::Cast(prototypeValue);
96
97 v8::Local<v8::Value> processValue;
98 if (!v8Call(prototype->Get(context, v8String(isolate, "process")),
99 processValue))
nhiroki 2017/03/16 00:21:35 ditto.
100 return;
101
102 if (isUndefinedOrNull(processValue)) {
103 exceptionState.throwTypeError(
104 "The 'process' function on the prototype does not exist.");
105 return;
106 }
107
108 if (!processValue->IsFunction()) {
109 exceptionState.throwTypeError(
110 "The 'process' property on the prototype is not a function.");
111 return;
112 }
113
114 v8::Local<v8::Function> processLocal =
115 v8::Local<v8::Function>::Cast(processValue);
116
117 AudioWorkletProcessorDefinition* definition =
118 AudioWorkletProcessorDefinition::create(isolate, name, constructorLocal,
119 processLocal);
120
121 m_processorDefinitionMap.set(name, definition);
haraken 2017/03/16 12:07:15 Nit: This will be an extremely corner case but you
122 }
123
124 AudioWorkletProcessor* AudioWorkletGlobalScope::createInstance(
125 const String& name) {
126 DCHECK(isContextThread());
127
128 AudioWorkletProcessorDefinition* definition = findDefinition(name);
129 if (!definition)
130 return nullptr;
131
132 // V8 object instance construction: this construction process is here to make
133 // the AudioWorkletProcessor class a thin wrapper of V8::Object instance.
134 v8::Isolate* isolate = scriptController()->getScriptState()->isolate();
135 v8::Local<v8::Object> instanceLocal;
136 if (!V8ObjectConstructor::newInstance(isolate,
137 definition->constructorLocal(isolate))
138 .ToLocal(&instanceLocal)) {
139 return nullptr;
140 }
141
142 AudioWorkletProcessor* processor = AudioWorkletProcessor::create(this, name);
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 ScriptState* scriptState = scriptController()->getScriptState();
153 ScriptState::Scope scope(scriptState);
154
155 v8::Isolate* isolate = scriptState->isolate();
156 AudioWorkletProcessorDefinition* definition =
157 findDefinition(processor->name());
158 DCHECK(definition);
159
160 v8::Local<v8::Value> argv[] = {
161 ToV8(inputBuffer, scriptState->context()->Global(), isolate),
162 ToV8(outputBuffer, scriptState->context()->Global(), isolate)};
163
164 v8::TryCatch block(isolate);
165 block.SetVerbose(true);
166
167 // Perform JS function process() in AudioWorkletProcessor instance. The actual
168 // V8 operation happens here to make the AudioWorkletProcessor class a thin
169 // wrapper of v8::Object instance.
170 V8ScriptRunner::callFunction(
171 definition->processLocal(isolate), scriptState->getExecutionContext(),
172 processor->instanceLocal(isolate), WTF_ARRAY_LENGTH(argv), argv, isolate);
173
174 if (block.HasCaught()) {
175 return false;
haraken 2017/03/16 12:07:15 It's okay to ignore the exception, right?
176 }
177
178 return true;
179 }
180
181 AudioWorkletProcessorDefinition* AudioWorkletGlobalScope::findDefinition(
182 const String& name) {
183 return m_processorDefinitionMap.at(name);
184 }
185
186 DEFINE_TRACE(AudioWorkletGlobalScope) {
187 visitor->trace(m_processorDefinitionMap);
188 visitor->trace(m_processorInstances);
189 ThreadedWorkletGlobalScope::trace(visitor);
190 }
191
35 } // namespace blink 192 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698