feat(media): add remote playback interface members

This commit is contained in:
ldm0
2026-11-19 00:59:46 +08:00
parent c5bb32ba12
commit 2bd3a230fc
11 changed files with 675 additions and 0 deletions
@@ -52,6 +52,7 @@ pub(crate) use picture_in_picture_tests::closed_window_for_test as closed_pictur
mod media_metadata;
mod media_recorder;
mod media_streams;
pub(crate) mod remote_playback;
#[cfg(test)]
pub(crate) use media_streams::inert_track_for_test;
mod video_color_space;
@@ -40,6 +40,7 @@ use super::super::{
opfs::install_opfs_constructor_template_bindings,
performance_runtime::install_performance_template_bindings,
range_surface::install_range_template_bindings,
remote_playback::install as install_remote_playback_template_bindings,
selection_surface::install_selection_template_bindings,
shared::{
install_abort_template_bindings, install_attr_template_bindings,
@@ -421,6 +422,7 @@ pub(super) fn install_constructor_template_bindings<'s>(
}
install_media_cue_template_bindings(scope, template, spec.interface.name());
install_media_source_template_bindings(scope, template, spec.interface.name());
install_remote_playback_template_bindings(scope, template, spec.interface.name());
super::super::picture_in_picture::install(scope, template, spec.interface.name());
super::super::media_streams::install(scope, template, spec.interface.name());
super::super::media_recorder::install(scope, template, spec.interface.name());
@@ -0,0 +1,330 @@
//! Remote Playback frontend for a UA without device discovery or a connection
//! backend. The specification's lifetime-unavailable branch resolves a watch
//! without an id, then queues one false availability notification.
use super::{media_queries, shared};
use crate::{
document_runtime::DomHandle,
host::report_event_callback_exception,
native_bridge::{
PageScriptHost, document::document_is_fully_active,
node_runtime_and_handle_from_object_or_detached, throw_dom_exception,
},
util::{get_private_object, get_private_value, set_private_value, v8str},
web_api_interfaces, webidl,
window_webidl_callback::{
WindowWebIdlCallbackFunction, WindowWebIdlCallbackFunctionOutcome,
invoke_window_webidl_callback_function,
},
};
use moli_webapi_declare::{WebApiFunctionTemplate, WebApiObject};
const ASSOCIATED: &str = "__moliMediaRemotePlayback";
const MEDIA: &str = "__moliRemotePlaybackMedia";
const LISTENERS: &str = "__moliRemotePlaybackListeners";
#[derive(WebApiObject)]
#[webapi(interface = web_api_interfaces::RemotePlayback, require_prototype)]
struct RemotePlaybackObject<'s> {
#[webapi(slot = MEDIA)]
media: v8::Local<'s, v8::Object>,
#[webapi(slot = shared::SIMPLE_EVENT_TARGET_SLOT, value = LISTENERS)]
event_target: (),
#[webapi(slot = shared::SIMPLE_EVENT_TARGET_ORDERED_HANDLERS_SLOT, init = true)]
ordered_handlers: (),
}
#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::RemotePlayback, enumerable)]
struct RemotePlaybackPrototype {
#[webapi(method, returns_promise, length = 1, callback = watch_availability)]
watch_availability: (),
#[webapi(method, returns_promise, length = 0, callback = cancel_watch_availability)]
cancel_watch_availability: (),
#[webapi(method, returns_promise, length = 0, callback = prompt)]
prompt: (),
#[webapi(accessor_property, getter = state)]
state: (),
#[webapi(accessor_property, getter = handler_getter, setter = handler_setter, data = v8str(scope, "connecting"))]
onconnecting: (),
#[webapi(accessor_property, getter = handler_getter, setter = handler_setter, data = v8str(scope, "connect"))]
onconnect: (),
#[webapi(accessor_property, getter = handler_getter, setter = handler_setter, data = v8str(scope, "disconnect"))]
ondisconnect: (),
}
#[derive(webidl::WebIdlArgs)]
#[webidl(prefix = "RemotePlayback.watchAvailability")]
struct WatchArgs {
#[webidl(required, converter = "callback_function")]
callback: webidl::WebIdlCallbackFunction,
}
#[derive(webidl::WebIdlArgs)]
#[webidl(prefix = "RemotePlayback.cancelWatchAvailability")]
struct CancelArgs {
#[webidl(converter = "long")]
id: Option<i32>,
}
pub(super) fn install<'s>(
scope: &mut v8::PinScope<'s, '_, ()>,
template: v8::Local<'s, v8::FunctionTemplate>,
name: &str,
) {
if name == "RemotePlayback" {
RemotePlaybackPrototype::initialize_prototype_template(
scope,
template.prototype_template(scope),
);
}
}
fn target<'s>(
scope: &mut v8::PinScope<'s, '_>,
receiver: v8::Local<'s, v8::Object>,
) -> v8::Local<'s, v8::Object> {
moli_webapi_declare::web_api_object_target(scope, receiver)
.expect("RemotePlayback receiver was validated")
}
pub(crate) fn media_remote_getter<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
let media = target(scope, args.this());
if let Some(remote) = get_private_object(scope, media, ASSOCIATED) {
rv.set(remote.into());
return;
}
let context = media
.get_creation_context(scope)
.expect("native media element has a creation realm");
let scope = &mut v8::ContextScope::new(scope, context);
let remote = RemotePlaybackObject::new(media)
.bind(scope)
.expect("native RemotePlayback object should bind");
set_private_value(scope, media, ASSOCIATED, remote.into());
rv.set(remote.into());
}
fn media_owner<'s>(
scope: &mut v8::PinScope<'s, '_>,
receiver: v8::Local<'s, v8::Object>,
) -> Option<(*mut PageScriptHost, DomHandle)> {
let remote = target(scope, receiver);
let media = get_private_object(scope, remote, MEDIA)
.expect("RemotePlayback retains its native media element");
node_runtime_and_handle_from_object_or_detached(scope, media).ok()
}
fn enabled_media_owner<'s>(
scope: &mut v8::PinScope<'s, '_>,
receiver: v8::Local<'s, v8::Object>,
) -> Option<(*mut PageScriptHost, DomHandle)> {
let Some((host_ptr, media)) = media_owner(scope, receiver) else {
throw_dom_exception(
scope,
"InvalidStateError",
11,
"The media element is no longer available.",
);
return None;
};
if unsafe { &*host_ptr }
.dom_host()
.node(media)
.and_then(|node| node.as_element())
.is_some_and(|element| element.has_attribute("disableremoteplayback"))
{
throw_dom_exception(
scope,
"InvalidStateError",
11,
"Remote playback is disabled for this media element.",
);
return None;
}
Some((host_ptr, media))
}
fn resolved_undefined<'s>(scope: &mut v8::PinScope<'s, '_>) -> v8::Local<'s, v8::Promise> {
let resolver = v8::PromiseResolver::new(scope).expect("RemotePlayback promise should allocate");
resolver.resolve(scope, v8::undefined(scope).into());
resolver.get_promise(scope)
}
fn watch_availability<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
let Some(parsed) = webidl::parse_args::<WatchArgs>(scope, &args) else {
return;
};
let Some((host_ptr, media)) = enabled_media_owner(scope, args.this()) else {
return;
};
let host = unsafe { &mut *host_ptr };
let callback = RemotePlaybackAvailabilityTask {
callback: WindowWebIdlCallbackFunction::new(scope, host, parsed.callback),
};
// The lifetime-unavailable algorithm does not register a callback or
// allocate an id. Exact Window/Document admission handles retired owners.
host.queue_remote_playback_availability(scope, media, callback);
rv.set(resolved_undefined(scope).into());
}
fn cancel_watch_availability<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
let Some(parsed) = webidl::parse_args::<CancelArgs>(scope, &args) else {
return;
};
if enabled_media_owner(scope, args.this()).is_none() {
return;
}
// There are no registered monitors in the lifetime-unavailable branch.
if parsed.id.is_some() {
throw_dom_exception(
scope,
"NotFoundError",
8,
"No availability callback has this id.",
);
return;
}
rv.set(resolved_undefined(scope).into());
}
fn prompt<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
_rv: v8::ReturnValue<'_, v8::Value>,
) {
let Some((host_ptr, media)) = enabled_media_owner(scope, args.this()) else {
return;
};
let host = unsafe { &*host_ptr };
let active_owner = host.owner_dispatch_scope_for_node(media).filter(|_| {
host.dom_host()
.node(media)
.and_then(|node| node.owner_document())
.is_some_and(|document| document_is_fully_active(host, document))
});
if active_owner.is_none_or(|owner| {
!host.window_has_transient_user_activation(owner)
&& !host.protocol_user_gesture_activation()
}) {
throw_dom_exception(
scope,
"InvalidAccessError",
15,
"Remote playback requires an active window with transient user activation.",
);
return;
}
throw_dom_exception(
scope,
"NotSupportedError",
9,
"Remote playback connections are unavailable on this platform.",
);
}
fn state<'s>(
scope: &mut v8::PinScope<'s, '_>,
_args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
rv.set(v8str(scope, "disconnected").into());
}
fn handler_getter<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
let remote = target(scope, args.this());
let event = args.data().to_rust_string_lossy(scope);
rv.set(
get_private_value(scope, remote, handler_slot(&event))
.unwrap_or_else(|| v8::null(scope).into()),
);
}
fn handler_setter<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
_rv: v8::ReturnValue<'_, v8::Value>,
) {
let remote = target(scope, args.this());
let event = args.data().to_rust_string_lossy(scope);
let slot = handler_slot(&event);
let active = args.get(0).is_object();
let handler = if active {
args.get(0)
} else {
v8::null(scope).into()
};
set_private_value(scope, remote, slot, handler);
media_queries::simple_object_event_set_ordered_handler(
scope, remote, LISTENERS, &event, slot, active,
);
}
fn handler_slot(event: &str) -> &'static str {
match event {
"connecting" => "__moliRemotePlaybackOnconnecting",
"connect" => "__moliRemotePlaybackOnconnect",
"disconnect" => "__moliRemotePlaybackOndisconnect",
_ => unreachable!("RemotePlayback handler callback data"),
}
}
/// One initial unavailable notification, on the media element event task
/// source. Callback realm/incumbent context and retired-realm checks use the
/// shared Web IDL callback infrastructure.
pub(crate) struct RemotePlaybackAvailabilityTask {
callback: WindowWebIdlCallbackFunction,
}
impl RemotePlaybackAvailabilityTask {
pub(crate) fn invoke(
&self,
scope: &mut v8::PinScope<'_, '_>,
host_ptr: *mut PageScriptHost,
) -> bool {
let callback = self.callback.prepare(scope);
let relevant_identity = callback.relevant_identity();
let receiver = v8::undefined(scope).into();
let available = v8::Boolean::new(scope, false).into();
match invoke_window_webidl_callback_function(
scope,
host_ptr,
"RemotePlaybackAvailabilityCallback",
"RemotePlayback availability callback threw",
"RemotePlayback availability callback",
&callback,
receiver,
&[available],
) {
WindowWebIdlCallbackFunctionOutcome::Returned => true,
WindowWebIdlCallbackFunctionOutcome::Threw(report) => {
report_event_callback_exception(
scope,
host_ptr,
"remoteplayback",
relevant_identity,
None,
&report,
);
true
}
WindowWebIdlCallbackFunctionOutcome::Retired => false,
}
}
}
@@ -27,6 +27,9 @@ pub(super) enum PendingMediaElementEventPayload {
track: v8::Global<v8::Object>,
event_type: String,
},
RemotePlaybackAvailability(
crate::context_bootstrap::remote_playback::RemotePlaybackAvailabilityTask,
),
}
pub(super) type MediaElementEventState = ExactWindowDocumentTaskLedger<
@@ -49,6 +52,20 @@ impl PageScriptHost {
)
}
pub(crate) fn queue_remote_playback_availability(
&mut self,
scope: &mut v8::PinScope<'_, '_>,
media: DomHandle,
callback: crate::context_bootstrap::remote_playback::RemotePlaybackAvailabilityTask,
) -> bool {
self.queue_media_element_event_for_node(
scope,
media,
RendererPageMediaElementEventTaskKind::RemotePlaybackAvailability,
PendingMediaElementEventPayload::RemotePlaybackAvailability(callback),
)
}
pub(crate) fn queue_media_seeking_event(
&mut self,
scope: &mut v8::PinScope<'_, '_>,
@@ -203,6 +220,9 @@ impl PageScriptHost {
media_handle,
)
}
PendingMediaElementEventPayload::RemotePlaybackAvailability(callback) => {
callback.invoke(scope, host_ptr)
}
PendingMediaElementEventPayload::Seeking { media_handle } => {
crate::native_bridge::element::dispatch_media_seeking_event(
scope,
@@ -277,6 +297,7 @@ impl PageScriptHost {
}
PendingMediaElementEventPayload::VolumeChange { .. }
| PendingMediaElementEventPayload::Seeking { .. }
| PendingMediaElementEventPayload::RemotePlaybackAvailability(_)
| PendingMediaElementEventPayload::SeekCompletion { .. }
| PendingMediaElementEventPayload::TextTrackListEvent { .. } => {}
}
@@ -5,6 +5,10 @@ mod text_tracks;
mod time_ranges;
mod video;
pub(super) use attributes::{
media_disable_remote_playback_getter, media_disable_remote_playback_setter,
};
pub(crate) use attributes::{
MediaLoadEventPhase, dispatch_media_load_event_phase, queue_media_canplay_after_text_tracks,
queue_media_load_if_needed, queue_media_load_if_source_or_loading_change,
@@ -1,4 +1,5 @@
use super::super::geometry::read_bounding_client_rect;
use super::super::reflection::element_reflection_receiver_or_throw;
use super::super::{
canonical_cross_origin_value, canonical_loading_value, canonical_preload_value,
construct_simple_event, dispatch_public_event, has_reflected_attribute,
@@ -16,6 +17,40 @@ use crate::native_bridge::{
use crate::util::v8_string;
use anyhow::Context;
pub(in crate::native_bridge::element) fn media_disable_remote_playback_getter<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'s, v8::Value>,
) {
let Some((runtime_ptr, handle)) = element_reflection_receiver_or_throw(scope, args.this())
else {
return;
};
rv.set_bool(has_reflected_attribute(
unsafe { &*runtime_ptr },
handle,
"disableremoteplayback",
));
}
pub(in crate::native_bridge::element) fn media_disable_remote_playback_setter<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
_rv: v8::ReturnValue<'s, v8::Value>,
) {
let Some((runtime_ptr, handle)) = element_reflection_receiver_or_throw(scope, args.this())
else {
return;
};
set_reflected_boolean_attribute(
scope,
runtime_ptr,
handle,
"disableremoteplayback",
args.get(0).boolean_value(scope),
);
}
pub(in crate::native_bridge) fn media_autoplay_getter_function<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
@@ -15,6 +15,10 @@ pub(super) struct HtmlHtmlElementPrototypeDeclaration {
#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::HTMLMediaElement, enumerable)]
pub(super) struct HtmlMediaElementPrototypeDeclaration {
#[webapi(accessor_property, getter = crate::context_bootstrap::remote_playback::media_remote_getter)]
remote: (),
#[webapi(accessor_property, getter = media::media_disable_remote_playback_getter, setter = media::media_disable_remote_playback_setter)]
disable_remote_playback: (),
#[webapi(accessor_property, getter = media_buffered_getter_function)]
buffered: (),
#[webapi(accessor_property, getter = media_played_getter_function)]
@@ -31,6 +31,7 @@ pub(crate) enum RendererPageMediaElementEventTaskKind {
SeekCompletion,
LoadEventPhase,
TextTrackListEvent,
RemotePlaybackAvailability,
}
pub(crate) type RendererPageMediaElementEventOwner = RendererPageWindowDocumentTaskOwner;
@@ -2462,6 +2462,7 @@ mod svg_gradient_native_receivers;
mod readable_algorithm_arrays;
mod remote_playback_interface;
mod remote_playback_members;
mod rendering_observers;
mod rendering_update;
mod rtc_configuration;
@@ -0,0 +1,164 @@
(async () => {
const checks = [], watches = [];
async function check(name, action) {
try { checks.push({name, passed: (await action()) === true}); }
catch (error) { checks.push({name, passed: false, error: String(error)}); }
}
const caught = action => {try {action(); return null;} catch (error) {return error;}};
const rejected = async action => {try {await action(); return null;} catch (error) {return error;}};
const frame = document.createElement('iframe'); document.body.appendChild(frame);
const popup = window.open('about:blank', '_blank');
try {
for (const [realm, owner] of [window, frame.contentWindow, popup].entries()) {
const C = owner.RemotePlayback, P = C.prototype, M = owner.HTMLMediaElement.prototype;
const typeError = error => error !== null && Object.getPrototypeOf(error) === owner.TypeError.prototype;
const domError = (error, name) => error?.name === name && Object.getPrototypeOf(error) === owner.DOMException.prototype;
const media = owner.document.createElement('video');
const remoteDescriptor = Object.getOwnPropertyDescriptor(M, 'remote');
let remote;
await check(`${realm}/associated native object`, () => {
remote = remoteDescriptor.get.call(media);
return remote instanceof C && remote instanceof owner.EventTarget && Object.getPrototypeOf(remote) === P &&
remote === remoteDescriptor.get.call(media) && remote !== owner.document.createElement('audio').remote;
});
await check(`${realm}/constructor inheritance and descriptors`, () => C.name === 'RemotePlayback' && C.length === 0 &&
Object.getPrototypeOf(C) === owner.EventTarget && Object.getPrototypeOf(P) === owner.EventTarget.prototype &&
Object.getOwnPropertyDescriptor(P, Symbol.toStringTag).value === 'RemotePlayback');
for (const construct of [false, true]) {
await check(`${realm}/illegal constructor/${construct}`, () => typeError(caught(() => construct ? new C() : C())));
}
let traps = 0;
const author = new Proxy(remote ?? {}, {get() {traps++; throw Error('get');}, getPrototypeOf() {traps++; throw Error('prototype');}});
const revoked = Proxy.revocable(remote ?? {}, {}); revoked.revoke();
const invalid = [undefined, null, {}, P, Object.create(P), Object.create(remote ?? {}), author, revoked.proxy,
new owner.EventTarget(), media];
for (const name of ['state', 'onconnecting', 'onconnect', 'ondisconnect']) {
const d = Object.getOwnPropertyDescriptor(P, name), setter = name !== 'state';
await check(`${realm}/${name}/descriptor`, () => d?.enumerable === true && d.configurable && typeof d.get === 'function' && d.get.length === 0 &&
(setter ? typeof d.set === 'function' && d.set.length === 1 : d.set === undefined));
for (const [index, value] of invalid.entries()) {
await check(`${realm}/${name}/getter receiver ${index}`, () => typeError(caught(() => d.get.call(value))));
if (setter) await check(`${realm}/${name}/setter receiver ${index}`, () => typeError(caught(() => d.set.call(value, () => {}))));
}
}
for (const [name, length] of [['watchAvailability', 1], ['cancelWatchAvailability', 0], ['prompt', 0]]) {
const d = Object.getOwnPropertyDescriptor(P, name);
await check(`${realm}/${name}/descriptor`, () => d?.writable === true && d.enumerable && d.configurable &&
typeof d.value === 'function' && d.value.length === length && d.value.name === name);
for (const [index, value] of invalid.entries()) {
await check(`${realm}/${name}/Promise receiver ${index}`, async () => {
let conversions = 0, promise;
const arg = name === 'watchAvailability' ? () => {} : {valueOf() {conversions++; throw Error('conversion');}};
const synchronous = caught(() => {promise = d.value.call(value, arg);});
return synchronous === null && promise instanceof owner.Promise && typeError(await rejected(() => promise)) && conversions === 0;
});
}
}
for (const [name, setter] of [['remote', false], ['disableRemotePlayback', true]]) {
const d = Object.getOwnPropertyDescriptor(M, name);
await check(`${realm}/media/${name}/descriptor`, () => d?.enumerable === true && d.configurable && d.get.length === 0 &&
(setter ? typeof d.set === 'function' && d.set.length === 1 : d.set === undefined));
const badMedia = [undefined, null, {}, M, Object.create(M), Object.create(media),
new Proxy(media, {}), revoked.proxy, owner.document.createElement('div')];
for (const [index, value] of badMedia.entries()) {
await check(`${realm}/media/${name}/getter receiver ${index}`, () => typeError(caught(() => d.get.call(value))));
if (setter) await check(`${realm}/media/${name}/setter receiver ${index}`, () => typeError(caught(() => d.set.call(value, {}))));
}
}
await check(`${realm}/native disabled state ignores public expandos`, async () => {
media.setAttribute('disableremoteplayback', '');
Object.defineProperty(media, 'disableRemotePlayback', {value: false, configurable: true});
const error = await rejected(() => P.prompt.call(remote));
delete media.disableRemotePlayback; media.removeAttribute('disableremoteplayback');
return domError(error, 'InvalidStateError');
});
const disabled = Object.getOwnPropertyDescriptor(M, 'disableRemotePlayback');
for (const [index, value] of [undefined, null, false, true, 0, 1, '', 'false', {}, Symbol('flag'), 0n, 1n].entries()) {
await check(`${realm}/boolean reflection/${index}`, () => {
disabled.set.call(media, value);
return disabled.get.call(media) === Boolean(value) && media.hasAttribute('disableremoteplayback') === Boolean(value);
});
}
await check(`${realm}/boolean reflection does not coerce objects`, () => {
let conversions = 0;
disabled.set.call(media, {valueOf() {conversions++; throw Error('valueOf');}, toString() {conversions++; throw Error('toString');}});
return disabled.get.call(media) && conversions === 0;
});
for (const [index, callback] of [undefined, null, false, 1, 'x', 1n, Symbol('callback'), {}, {handleEvent() {}}].entries()) {
await check(`${realm}/callback conversion before disabled check/${index}`, async () =>
typeError(await rejected(() => P.watchAvailability.call(remote, callback))));
}
await check(`${realm}/required callback is a rejected Promise`, async () => typeError(await rejected(() => P.watchAvailability.call(remote))));
await check(`${realm}/disabled watch`, async () => domError(await rejected(() => P.watchAvailability.call(remote, () => {})), 'InvalidStateError'));
for (const id of [undefined, 0, 1]) {
await check(`${realm}/disabled cancel/${id}`, async () => domError(await rejected(() => P.cancelWatchAvailability.call(remote, id)), 'InvalidStateError'));
}
await check(`${realm}/conversion exception identity before disabled cancel`, async () => {
const marker = {}, log = [];
const error = await rejected(() => P.cancelWatchAvailability.call(remote, {valueOf() {log.push('valueOf'); throw marker;}}));
return error === marker && log.join() === 'valueOf';
});
for (const id of [Symbol('id'), 1n]) {
await check(`${realm}/invalid long before disabled cancel/${typeof id}`, async () => typeError(await rejected(() => P.cancelWatchAvailability.call(remote, id))));
}
await check(`${realm}/disabled prompt`, async () => domError(await rejected(() => P.prompt.call(remote)), 'InvalidStateError'));
if (disabled) disabled.set.call(media, false);
for (const id of [null, 0, -1, 1, 2**32 + 1, NaN, Infinity, -Infinity, '1']) {
await check(`${realm}/unknown callback id/${String(id)}`, async () => domError(await rejected(() => P.cancelWatchAvailability.call(remote, id)), 'NotFoundError'));
}
await check(`${realm}/cancel omitted and undefined`, async () => await P.cancelWatchAvailability.call(remote) === undefined &&
await P.cancelWatchAvailability.call(remote, undefined) === undefined);
await check(`${realm}/long conversion occurs once`, async () => {
let count = 0;
const error = await rejected(() => P.cancelWatchAvailability.call(remote, {valueOf() {count++; return 1;}}));
return count === 1 && domError(error, 'NotFoundError');
});
await check(`${realm}/initial disconnected state`, () => remote.state === 'disconnected');
for (const eventType of ['connecting', 'connect', 'disconnect']) {
await check(`${realm}/ordered EventHandler/${eventType}`, () => {
const target = remote, log = [], before = () => log.push('before'), after = () => log.push('after');
const name = 'on' + eventType;
target.addEventListener(eventType, before);
target[name] = () => log.push('old');
target.addEventListener(eventType, after);
target[name] = function(event) {log.push(this === target && event.currentTarget === target ? 'handler' : 'wrong'); return false;};
const event = new owner.Event(eventType, {cancelable:true});
const result = !target.dispatchEvent(event) && event.defaultPrevented && log.join() === 'before,handler,after';
target[name] = null; target.removeEventListener(eventType, before); target.removeEventListener(eventType, after);
return result && target.state === 'disconnected';
});
for (const [index, value] of [undefined, null, false, 1, 'x', 1n, Symbol('handler')].entries()) {
await check(`${realm}/handler primitive/${eventType}/${index}`, () => {remote['on'+eventType] = value; return remote['on'+eventType] === null;});
}
}
await check(`${realm}/availability resolves before one boolean callback`, async () => {
const log = [];
let count = 0, thisValue, resolveCallback;
const callback = new Promise(resolve => {resolveCallback = resolve;});
const pending = P.watchAvailability.call(remote, function(available) {'use strict'; count++; thisValue = this; log.push('callback'); resolveCallback(available);});
const value = await pending.then(value => {log.push('resolved'); return value;});
const available = await callback;
watches.push({realm, resolvedType:typeof value, resolvedValue:value ?? null, available, count, order:log,
undefinedThis:thisValue === undefined, remoteThis:thisValue === remote});
// The lifetime-unavailable branch has no id; monitoring-capable UAs
// instead return a positive id. Original WPT id checks stay separate.
return (value === undefined || Number.isInteger(value) && value > 0) && available === false && count === 1 &&
thisValue === undefined && log.join() === 'resolved,callback';
});
await check(`${realm}/borrowed media getter keeps owner realm`, () => {
const foreign = window.document.createElement('audio');
const result = remoteDescriptor.get.call(foreign);
return result instanceof window.RemotePlayback && result === foreign.remote && (owner === window || !(result instanceof C));
});
await check(`${realm}/author and revoked Proxy checks execute no traps`, () => traps === 0);
await check(`${realm}/public constructor replacement preserves native source`, () => {
const original = owner.RemotePlayback;
try {owner.RemotePlayback = function() {throw Error('author constructor');}; return owner.document.createElement('audio').remote instanceof original;}
finally {owner.RemotePlayback = original;}
});
}
} finally {popup.close(); frame.remove();}
const facts = {complete:true, total:checks.length, passed:checks.filter(row => row.passed).length, checks, watches};
globalThis.__uiEventResults = facts;
return facts.passed === facts.total;
})()
@@ -0,0 +1,112 @@
use super::*;
#[tokio::test]
async fn remote_playback_members_preserve_receivers_realms_and_unavailable_callback_order() {
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).unwrap();
let mut vm =
new_storage_page_task_executor_test_vm_with_loader("https://remote-members.test/", &loader);
vm.eval(include_str!("remote_playback_members.js")).unwrap();
for realm in ["main", "iframe", "popup"] {
run_next_page_media_element_event_for_test(&mut vm, &loader, realm).await;
}
assert_eq!(vm.eval("__uiEventResults.complete").unwrap(), "true");
assert_eq!(
vm.eval("JSON.stringify(__uiEventResults.checks.filter(row => !row.passed))")
.unwrap(),
"[]"
);
assert_eq!(vm.eval("__uiEventResults.watches.every(row => row.resolvedType === 'undefined' && row.available === false && row.count === 1)").unwrap(), "true");
}
#[test]
fn remote_playback_registered_native_proxies_share_media_and_handler_identity() {
let mut vm = new_storage_page_task_executor_test_vm("https://remote-native-proxy.test/");
vm.eval("globalThis.media = document.createElement('video'); globalThis.remote = media.remote")
.unwrap();
let context_ptr = &vm.current_main_world().unwrap().context as *const _;
vm.with_context_scope_by_ptr_and_checkpoint_for_test(context_ptr, |scope, _| {
let global = scope.get_current_context().global(scope);
for (original, alias) in [("media", "mediaProxy"), ("remote", "remoteProxy")] {
let key = crate::util::v8str(scope, original);
let object =
v8::Local::<v8::Object>::try_from(global.get(scope, key.into()).unwrap()).unwrap();
let handler = crate::util::new_null_prototype_object(scope);
let proxy = v8::Proxy::new(scope, object, handler).unwrap();
moli_webapi_declare::register_web_api_proxy(scope, proxy).unwrap();
let key = crate::util::v8str(scope, alias);
assert_eq!(
global.create_data_property(scope, key.into(), proxy.into()),
Some(true)
);
}
Ok(())
})
.unwrap();
assert_eq!(vm.eval(r#"(() => {
const assert = (ok, name) => {if (!ok) throw Error(name)};
assert(mediaProxy.remote === remote && remoteProxy.state === 'disconnected', 'native identity');
mediaProxy.disableRemotePlayback = true;
assert(media.hasAttribute('disableremoteplayback') && mediaProxy.disableRemotePlayback, 'native proxy boolean reflection');
const log = [];
remote.addEventListener('connect', () => log.push('before'));
remoteProxy.onconnect = () => log.push('original');
remote.addEventListener('connect', () => log.push('after'));
remoteProxy.onconnect = () => {log.push('replacement'); return false};
assert(remote.onconnect === remoteProxy.onconnect, 'handler native identity');
assert(!remote.dispatchEvent(new Event('connect', {cancelable:true})), 'EventHandler cancellation');
assert(log.join() === 'before,replacement,after' && remote.state === 'disconnected', 'ordered handler without a connection');
Object.setPrototypeOf(remote, null);
assert(Object.getOwnPropertyDescriptor(RemotePlayback.prototype, 'state').get.call(remoteProxy) === 'disconnected', 'brand survives prototype replacement');
let error; try {Object.getOwnPropertyDescriptor(RemotePlayback.prototype, 'state').get.call(new Proxy(remoteProxy, {}))} catch (caught) {error=caught}
assert(error instanceof TypeError, 'author wrapping native Proxy is rejected');
return true;
})()"#).unwrap(), "true");
}
#[tokio::test]
async fn remote_playback_callback_exceptions_do_not_reject_watches_or_stop_media_tasks() {
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).unwrap();
let mut vm =
new_storage_page_task_executor_test_vm_with_loader("https://remote-errors.test/", &loader);
vm.eval(r#"
globalThis.order = []; globalThis.errors = 0;
window.onerror = () => {errors++; return true};
const remote = document.createElement('video').remote;
remote.watchAvailability(() => {order.push('throwing'); throw Error('availability callback')}).then(value => order.push(String(value)));
remote.watchAvailability(available => order.push(String(available))).then(() => order.push('resolved'));
"#).unwrap();
assert_eq!(vm.eval("order.join()").unwrap(), "undefined,resolved");
for _ in 0..2 {
run_next_page_media_element_event_for_test(&mut vm, &loader, "availability notification")
.await;
}
assert_eq!(
vm.eval("order.join()").unwrap(),
"undefined,resolved,throwing,false"
);
assert_eq!(vm.eval("errors").unwrap(), "1");
}
#[tokio::test]
async fn remote_playback_retired_iframe_discards_callbacks_and_rejects_prompt() {
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).unwrap();
let mut vm = new_storage_page_task_executor_test_vm_with_loader(
"https://remote-retirement.test/",
&loader,
);
vm.eval(r#"
globalThis.called = 0; globalThis.promptResult = '';
const frame = document.createElement('iframe'); document.body.appendChild(frame);
const other = frame.contentWindow;
const media = other.document.createElement('video'); other.document.body.appendChild(media);
const remote = media.remote;
remote.watchAvailability(() => {called++});
frame.remove();
remote.prompt().catch(error => {promptResult = error.name + ':' + (error instanceof other.DOMException)});
"#).unwrap();
vm.run_one_media_element_event_executor_turn(&loader)
.await
.unwrap();
assert_eq!(vm.eval("called").unwrap(), "0");
assert_eq!(vm.eval("promptResult").unwrap(), "InvalidAccessError:true");
}