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

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

Issue 2793593002: AudioWorklet prototype
Patch Set: Merge changes, AudioParam bug fix Created 3 years, 5 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/AudioWorkletMessagingProxy.h" 5 #include "modules/webaudio/AudioWorkletMessagingProxy.h"
6 6
7 #include "core/workers/ThreadedWorkletObjectProxy.h" 7 #include "bindings/core/v8/ScriptSourceCode.h"
8 #include "core/dom/Document.h"
9 #include "core/dom/SecurityContext.h"
10 #include "core/dom/TaskRunnerHelper.h"
11 #include "core/frame/csp/ContentSecurityPolicy.h"
12 #include "core/loader/WorkletScriptLoader.h"
13 #include "core/origin_trials/OriginTrialContext.h"
14 #include "core/workers/WorkerInspectorProxy.h"
15 #include "core/workers/WorkerThreadStartupData.h"
16 #include "core/workers/WorkletGlobalScope.h"
17 #include "core/workers/WorkletPendingTasks.h"
18 #include "modules/webaudio/AudioWorkletNode.h"
19 #include "modules/webaudio/AudioWorkletObjectProxy.h"
8 #include "modules/webaudio/AudioWorkletThread.h" 20 #include "modules/webaudio/AudioWorkletThread.h"
21 #include "platform/CrossThreadFunctional.h"
22 #include "platform/WaitableEvent.h"
23 #include "platform/WebTaskRunner.h"
9 24
10 namespace blink { 25 namespace blink {
11 26
27 class AudioWorkletMessagingProxy::LoaderClient final
28 : public GarbageCollectedFinalized<
29 AudioWorkletMessagingProxy::LoaderClient>,
30 public WorkletScriptLoader::Client {
31 USING_GARBAGE_COLLECTED_MIXIN(LoaderClient);
32
33 public:
34 LoaderClient(RefPtr<WebTaskRunner> outside_settings_task_runner,
35 WorkletPendingTasks* pending_tasks,
36 AudioWorkletMessagingProxy* proxy)
37 : outside_settings_task_runner_(std::move(outside_settings_task_runner)),
38 pending_tasks_(pending_tasks),
39 proxy_(proxy) {}
40
41 // Implementation of the second half of the "fetch and invoke a worklet
42 // script" algorithm:
43 // https://drafts.css-houdini.org/worklets/#fetch-and-invoke-a-worklet-script
44 void NotifyWorkletScriptLoadingFinished(
45 WorkletScriptLoader* loader,
46 const ScriptSourceCode& source_code) final {
47 DCHECK(IsMainThread());
48 proxy_->NotifyLoadingFinished(loader);
49
50 if (!loader->WasScriptLoadSuccessful()) {
51 // Step 3: "If script is null, then queue a task on outsideSettings's
52 // responsible event loop to run these steps:"
53 // The steps are implemented in WorkletPendingTasks::Abort().
54 outside_settings_task_runner_->PostTask(
55 BLINK_FROM_HERE, WTF::Bind(&WorkletPendingTasks::Abort,
56 WrapPersistent(pending_tasks_.Get())));
57 return;
58 }
59
60 // Step 4: "Run a module script given script."
61 proxy_->EvaluateScript(source_code);
62
63 // Step 5: "Queue a task on outsideSettings's responsible event loop to run
64 // these steps:"
65 // The steps are implemented in WorkletPendingTasks::DecrementCounter().
66 outside_settings_task_runner_->PostTask(
67 BLINK_FROM_HERE, WTF::Bind(&WorkletPendingTasks::DecrementCounter,
68 WrapPersistent(pending_tasks_.Get())));
69 }
70
71 DEFINE_INLINE_TRACE() {
72 visitor->Trace(pending_tasks_);
73 visitor->Trace(proxy_);
74 }
75
76 private:
77 RefPtr<WebTaskRunner> outside_settings_task_runner_;
78 Member<WorkletPendingTasks> pending_tasks_;
79 Member<AudioWorkletMessagingProxy> proxy_;
80 };
81
12 AudioWorkletMessagingProxy::AudioWorkletMessagingProxy( 82 AudioWorkletMessagingProxy::AudioWorkletMessagingProxy(
13 ExecutionContext* execution_context, 83 ExecutionContext* execution_context,
14 WorkerClients* worker_clients) 84 WorkerClients* worker_clients)
15 : ThreadedWorkletMessagingProxy(execution_context, worker_clients) {} 85 : ThreadedMessagingProxyBase(execution_context, worker_clients) {
86 worklet_object_proxy_ =
87 AudioWorkletObjectProxy::Create(this, GetParentFrameTaskRunners());
88 }
16 89
17 AudioWorkletMessagingProxy::~AudioWorkletMessagingProxy() {} 90 void AudioWorkletMessagingProxy::Initialize() {
91 DCHECK(IsMainThread());
92 if (AskedToTerminate())
93 return;
94
95 Document* document = ToDocument(GetExecutionContext());
96 SecurityOrigin* starter_origin = document->GetSecurityOrigin();
97 KURL script_url = document->Url();
98
99 ContentSecurityPolicy* csp = document->GetContentSecurityPolicy();
100 DCHECK(csp);
101
102 WorkerThreadStartMode start_mode =
103 GetWorkerInspectorProxy()->WorkerStartMode(document);
104 std::unique_ptr<WorkerSettings> worker_settings =
105 WTF::WrapUnique(new WorkerSettings(document->GetSettings()));
106
107 // TODO(ikilpatrick): Decide on sensible a value for referrerPolicy.
108 std::unique_ptr<WorkerThreadStartupData> startup_data =
109 WorkerThreadStartupData::Create(
110 script_url, document->UserAgent(), String(), nullptr, start_mode,
111 csp->Headers().get(), /* referrerPolicy */ String(), starter_origin,
112 ReleaseWorkerClients(), document->AddressSpace(),
113 OriginTrialContext::GetTokens(document).get(),
114 std::move(worker_settings), WorkerV8Settings::Default());
115 InitializeWorkerThread(std::move(startup_data), script_url);
116 }
117
118 DEFINE_TRACE(AudioWorkletMessagingProxy) {
119 visitor->Trace(loaders_);
120 ThreadedMessagingProxyBase::Trace(visitor);
121 }
122
123 void AudioWorkletMessagingProxy::FetchAndInvokeScript(
124 const KURL& module_url_record,
125 WebURLRequest::FetchCredentialsMode credentials_mode,
126 RefPtr<WebTaskRunner> outside_settings_task_runner,
127 WorkletPendingTasks* pending_tasks) {
128 DCHECK(IsMainThread());
129 LoaderClient* client = new LoaderClient(
130 std::move(outside_settings_task_runner), pending_tasks, this);
131 WorkletScriptLoader* loader = WorkletScriptLoader::Create(
132 ToDocument(GetExecutionContext())->Fetcher(), client);
133 loaders_.insert(loader);
134 loader->FetchScript(module_url_record);
135 }
136
137 void AudioWorkletMessagingProxy::WorkletObjectDestroyed() {
138 DCHECK(IsMainThread());
139 ParentObjectDestroyed();
140 }
141
142 void AudioWorkletMessagingProxy::TerminateWorkletGlobalScope() {
143 DCHECK(IsMainThread());
144 for (const auto& loader : loaders_)
145 loader->Cancel();
146 loaders_.clear();
147 TerminateGlobalScope();
148 }
149
150 void AudioWorkletMessagingProxy::NotifyLoadingFinished(
151 WorkletScriptLoader* loader) {
152 loaders_.erase(loader);
153 }
154
155 void AudioWorkletMessagingProxy::EvaluateScript(
156 const ScriptSourceCode& script_source_code) {
157 DCHECK(IsMainThread());
158 TaskRunnerHelper::Get(TaskType::kMiscPlatformAPI, GetWorkerThread())
159 ->PostTask(
160 BLINK_FROM_HERE,
161 CrossThreadBind(&AudioWorkletObjectProxy::EvaluateScript,
162 CrossThreadUnretained(worklet_object_proxy_.get()),
163 script_source_code.Source(), script_source_code.Url(),
164 CrossThreadUnretained(GetWorkerThread())));
165 }
166
167 void AudioWorkletMessagingProxy::CreateProcessorInstance(
168 const String& name, AudioWorkletHandler* handler, WaitableEvent* done) {
169 TaskRunnerHelper::Get(TaskType::kUnthrottled, GetWorkerThread())
170 ->PostTask(
171 BLINK_FROM_HERE,
172 CrossThreadBind(&AudioWorkletObjectProxy::CreateProcessorInstance,
173 CrossThreadUnretained(worklet_object_proxy_.get()),
174 name,
175 CrossThreadUnretained(handler),
176 CrossThreadUnretained(done),
177 CrossThreadUnretained(GetWorkerThread())));
178 }
18 179
19 std::unique_ptr<WorkerThread> AudioWorkletMessagingProxy::CreateWorkerThread( 180 std::unique_ptr<WorkerThread> AudioWorkletMessagingProxy::CreateWorkerThread(
20 double origin_time) { 181 double origin_time) {
21 return AudioWorkletThread::Create(CreateThreadableLoadingContext(), 182 return AudioWorkletThread::Create(CreateThreadableLoadingContext(),
22 WorkletObjectProxy()); 183 WorkletObjectProxy());
23 } 184 }
24 185
186 WebThreadSupportingGC* AudioWorkletMessagingProxy::GetRenderingThread() {
187 return &(GetWorkerThread()->GetWorkerBackingThread().BackingThread());
188 };
189
25 } // namespace blink 190 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698