Skip to content

Truly independent web browser

LibWeb: Queue AudioContext close statechange after settlement

Shannon Booth2890771b2e0c
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_audio/audio_context.rs b/Libraries/LibWeb/Bindings/Rust/src/web_audio/audio_context.rs
index 55a25d5508d..296b8c22794 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_audio/audio_context.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_audio/audio_context.rs
@@ -238,7 +238,7 @@ impl AudioContext {
.context
.queue_a_media_element_task(cx.vm(), context, |_, context| {
// 1. Set the state attribute of the AudioContext to "running".
- context.context.set_control_state(AudioContextState::Running);
+ context.context.set_state(AudioContextState::Running);
// 2. Fire an event named statechange at the AudioContext.
context.context.dispatch_statechange();
@@ -316,7 +316,7 @@ impl AudioContext {
// NB: The bindings create the promise.
// 3. If the [[control thread state]] on the AudioContext is closed reject the promise with InvalidStateError, abort these steps, returning promise.
- if self.context.state_value() == AudioContextState::Closed {
+ if self.context.control_state() == AudioContextState::Closed {
reject_promise_with_exception(
cx.vm(),
promise,
@@ -410,7 +410,7 @@ impl AudioContext {
// 7.5.4: If the state attribute of the AudioContext is not already "running":
if context.context.state_value() != AudioContextState::Running {
// 7.5.4.1: Set the state attribute of the AudioContext to "running".
- context.context.set_control_state(AudioContextState::Running);
+ context.context.set_state(AudioContextState::Running);
// 7.5.4.2: queue a media element task to fire an event named statechange at the AudioContext.
context.context.queue_a_media_element_task(vm, context, |_, context| {
@@ -432,7 +432,7 @@ impl AudioContext {
}
// 3. If the [[control thread state]] on the AudioContext is closed reject the promise with InvalidStateError, abort these steps, returning promise.
- if self.context.state_value() == AudioContextState::Closed {
+ if self.context.control_state() == AudioContextState::Closed {
reject_promise_with_exception(
cx.vm(),
promise,
@@ -485,7 +485,7 @@ impl AudioContext {
// 7.3.2: If the state attribute of the AudioContext is not already "suspended":
if context.context.state_value() != AudioContextState::Suspended {
// 7.3.2.1: Set the state attribute of the AudioContext to "suspended".
- context.context.set_control_state(AudioContextState::Suspended);
+ context.context.set_state(AudioContextState::Suspended);
// 7.3.2.2: queue a media element task to fire an event named statechange at the AudioContext.
context.context.queue_a_media_element_task(vm, context, |_, context| {
@@ -496,9 +496,9 @@ impl AudioContext {
});
}
- // https://www.w3.org/TR/webaudio/#dom-audiocontext-close
+ // https://webaudio.github.io/web-audio-api/#dom-audiocontext-close
pub(crate) fn close(&self, cx: &mut CallContext, promise: WebIdlPromise) -> WebIdlResult<()> {
- // 1. If this's relevant global object's associated Document is not fully active then return a promise rejected with "InvalidStateError" DOMException.
+ // 1. If this’s relevant global object’s associated Document is not fully active then return a promise rejected with "InvalidStateError" DOMException.
if !self.context.document_is_fully_active() {
return Err(Exception::Dom(
"InvalidStateError",
@@ -506,8 +506,11 @@ impl AudioContext {
));
}
+ // 2. Let promise be a new Promise.
+ // NB: The bindings create the promise.
+
// 3. If the [[control thread state]] flag on the AudioContext is closed reject the promise with InvalidStateError, abort these steps, returning promise.
- if self.context.state_value() == AudioContextState::Closed {
+ if self.context.control_state() == AudioContextState::Closed {
reject_promise_with_exception(
cx.vm(),
promise,
@@ -524,36 +527,42 @@ impl AudioContext {
// 5. Queue a control message to close the AudioContext.
self.run_control_message_to_close(cx.vm(), promise);
+
+ // 6. Return promise.
Ok(())
}
fn run_control_message_to_close(&self, vm: &Vm, promise: WebIdlPromise) {
- // 5.1: Attempt to release system resources.
+ // 1. Attempt to release system resources.
self.context.shutdown_audio_worklet();
if let Some(renderer) = &*self.renderer.borrow() {
renderer.stop();
renderer.set_client(None);
}
- // 5.2: Set the [[rendering thread state]] to "suspended".
+ // 2. Set the [[rendering thread state]] to suspended.
self.context.set_rendering_state(AudioContextState::Suspended);
- // FIXME: 5.3: If this control message is being run in a reaction to the document being unloaded, abort this algorithm.
- // 5.4: queue a media element task to execute the following steps:
+ // 3. If this control message is being run in a reaction to the document being unloaded, abort this algorithm.
+ // NOTE: There is no need to notify the control thread in this case.
+ // NB: Document teardown releases the renderer directly.
+ // 4. queue a media element task to execute the following steps:
self.context
.queue_a_media_element_task(vm, (self.this(), promise), |vm, (context, promise)| {
with_temporary_execution_context_and_callbacks(promise_realm(promise), || {
- // 5.4.1: Resolve promise.
+ // 4.1. Resolve promise.
resolve_promise(vm, promise, Value::UNDEFINED);
- // 5.4.2: If the state attribute of the AudioContext is not already "closed":
+ // 4.2. If the state attribute of the AudioContext is not already "closed":
if context.context.state_value() != AudioContextState::Closed {
- // 5.4.2.1: Set the state attribute of the AudioContext to "closed".
- context.context.set_control_state(AudioContextState::Closed);
- }
+ // 4.2.1. Set the state attribute of the AudioContext to "closed".
+ context.context.set_state(AudioContextState::Closed);
- // 5.4.2.2: queue a media element task to fire an event named statechange at the AudioContext.
- context.context.dispatch_statechange();
+ // 4.2.2. queue a media element task to fire an event named statechange at the AudioContext.
+ context.context.queue_a_media_element_task(vm, context, |_, context| {
+ context.context.dispatch_statechange();
+ });
+ }
});
});
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_audio/base_audio_context.rs b/Libraries/LibWeb/Bindings/Rust/src/web_audio/base_audio_context.rs
index 8b238024355..86455f83cdf 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_audio/base_audio_context.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_audio/base_audio_context.rs
@@ -100,6 +100,7 @@ pub(crate) struct BaseAudioContext {
sample_rate: Cell<f32>,
pub(crate) listener: Cell<Option<Ref<AudioListener>>>,
global_object: CppCell,
+ state: Cell<AudioContextState>,
control_thread_state: Cell<AudioContextState>,
rendering_thread_state: Cell<AudioContextState>,
media_element_event_task_source: MediaElementTaskSource,
@@ -209,6 +210,7 @@ impl BaseAudioContext {
sample_rate: Cell::new(sample_rate),
listener: Cell::new(None),
global_object,
+ state: Cell::new(AudioContextState::Suspended),
control_thread_state: Cell::new(AudioContextState::Suspended),
rendering_thread_state: Cell::new(AudioContextState::Suspended),
media_element_event_task_source: MediaElementTaskSource(
@@ -281,6 +283,14 @@ impl BaseAudioContext {
}
pub(crate) fn state_value(&self) -> AudioContextState {
+ self.state.get()
+ }
+
+ pub(crate) fn set_state(&self, state: AudioContextState) {
+ self.state.set(state);
+ }
+
+ pub(crate) fn control_state(&self) -> AudioContextState {
self.control_thread_state.get()
}
@@ -432,7 +442,11 @@ impl BaseAudioContext {
Ok(self.listener_value())
}
+ // https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-state
pub(crate) fn state(&self, _: &mut CallContext) -> WebIdlResult<AudioContextState> {
+ // Getting this attribute returns the contents of the [[control thread state]] slot.
+ // AD-HOC: The tasks queued by resume(), suspend() and close() set the state attribute after the
+ // [[control thread state]] has changed, so the attribute has its own slot.
Ok(self.state_value())
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_audio/offline_audio_context.rs b/Libraries/LibWeb/Bindings/Rust/src/web_audio/offline_audio_context.rs
index ac9ce928ef1..573d2889372 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_audio/offline_audio_context.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_audio/offline_audio_context.rs
@@ -148,6 +148,7 @@ impl OfflineAudioContext {
// 1. Set the [[control thread state]] for c to "suspended".
c.context.set_control_state(AudioContextState::Suspended);
+ c.context.set_state(AudioContextState::Suspended);
// 2. Set the [[rendering thread state]] for c to "suspended".
c.context.set_rendering_state(AudioContextState::Suspended);
@@ -291,6 +292,7 @@ impl OfflineAudioContext {
// AD-HOC: Other engines transition the context to "running" once rendering starts.
self.context.set_control_state(AudioContextState::Running);
+ self.context.set_state(AudioContextState::Running);
self.context.set_rendering_state(AudioContextState::Running);
self.queue_a_statechange_event(vm);
renderer.start_rendering();
@@ -308,6 +310,7 @@ impl OfflineAudioContext {
// AD-HOC: Other engines transition the context to "closed" once rendering completes.
self.context.set_control_state(AudioContextState::Closed);
+ self.context.set_state(AudioContextState::Closed);
self.context.set_rendering_state(AudioContextState::Closed);
self.queue_a_statechange_event(vm);
@@ -358,6 +361,7 @@ impl OfflineAudioContext {
fn handle_suspended(&self, vm: &Vm, suspend_time: f64) {
self.context.set_control_state(AudioContextState::Suspended);
+ self.context.set_state(AudioContextState::Suspended);
self.context.set_rendering_state(AudioContextState::Suspended);
let frame = (suspend_time * f64::from(self.context.sample_rate_value())).round() as u64;
@@ -415,6 +419,7 @@ impl OfflineAudioContext {
// 4. Set the [[control thread state]] flag on the OfflineAudioContext to running.
self.context.set_control_state(AudioContextState::Running);
+ self.context.set_state(AudioContextState::Running);
self.context.set_rendering_state(AudioContextState::Running);
self.queue_a_statechange_event(cx.vm());
diff --git a/Tests/LibWeb/Text/expected/WebAudio/AudioContext-close-statechange-order.txt b/Tests/LibWeb/Text/expected/WebAudio/AudioContext-close-statechange-order.txt
new file mode 100644
index 00000000000..c211f0ee55b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/WebAudio/AudioContext-close-statechange-order.txt
@@ -0,0 +1,4 @@
+State before close settles: running
+State after close settles: closed
+Resume during close: InvalidStateError
+Closed statechange
diff --git a/Tests/LibWeb/Text/input/WebAudio/AudioContext-close-statechange-order.html b/Tests/LibWeb/Text/input/WebAudio/AudioContext-close-statechange-order.html
new file mode 100644
index 00000000000..744f43ba8c5
--- /dev/null
+++ b/Tests/LibWeb/Text/input/WebAudio/AudioContext-close-statechange-order.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const context = new AudioContext();
+ await context.resume();
+
+ const statechange = new Promise(resolve => {
+ context.addEventListener("statechange", () => {
+ if (context.state === "closed") {
+ println("Closed statechange");
+ resolve();
+ }
+ });
+ });
+ const closed = context.close();
+ println(`State before close settles: ${context.state}`);
+ const rejected = context.resume().catch(error => error.name);
+ await closed;
+ println(`State after close settles: ${context.state}`);
+ println(`Resume during close: ${await rejected}`);
+ await statechange;
+ });
+</script>